173 lines
4.9 KiB
PHP
173 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Catalyst\APIBundle\Controller\APIController;
|
|
use Catalyst\APIBundle\Response\APIResponse;
|
|
|
|
use App\Entity\SAPBattery;
|
|
use App\Entity\SAPBatterySize;
|
|
use App\Entity\SAPBatteryBrand;
|
|
|
|
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
|
|
|
class BatteryController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
public function getBatteries(EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('battery.list', null, 'No access.');
|
|
|
|
$batteries = $em->getRepository(SAPBattery::class)->findBy([], ['id' => 'ASC']);
|
|
|
|
$result = [];
|
|
foreach ($batteries as $batt)
|
|
{
|
|
$csize_id = '';
|
|
$csize_name = '';
|
|
if ($batt->getContainerSize() != null)
|
|
{
|
|
$csize_id = $batt->getContainerSize()->getID();
|
|
$csize_name = $batt->getContainerSize()->getName();
|
|
}
|
|
|
|
$result[] = [
|
|
'id' => $batt->getID(),
|
|
'size' => $batt->getSize()->getID(),
|
|
'brand' => $batt->getBrand()->getID(),
|
|
'container_size_id' => $csize_id,
|
|
'container_size_name' => $csize_name,
|
|
'date_update' => $batt->getDateUpdate()->format('YmdHis'),
|
|
'flag_new' => $batt->isNew(),
|
|
'flag_inventory' => $batt->isInventory(),
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
'batt' => $result,
|
|
];
|
|
|
|
return new APIResponse(true, 'Batteries loaded.', $data);
|
|
}
|
|
|
|
public function getBrands(EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('batterybrand.list', null, 'No access.');
|
|
$brands = $em->getRepository(SAPBatteryBrand::class)->findBy([], ['name' => 'ASC']);
|
|
|
|
$result = [];
|
|
foreach ($brands as $brand)
|
|
{
|
|
$result[] = [
|
|
'id' => $brand->getID(),
|
|
'name' => $brand->getName(),
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
'brands' => $result,
|
|
];
|
|
|
|
return new APIResponse(true, 'Battery brands loaded.', $data);
|
|
}
|
|
|
|
public function getSizes(EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('batterysize.list', null, 'No access.');
|
|
|
|
$sizes = $em->getRepository(SAPBatterySize::class)->findBy([], ['name' => 'ASC']);
|
|
|
|
$result = [];
|
|
foreach ($sizes as $size)
|
|
{
|
|
$result[] = [
|
|
'id' => $size->getID(),
|
|
'name' => $size->getName(),
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
'sizes' => $result,
|
|
];
|
|
|
|
return new APIResponse(true, 'Battery sizes loaded.', $data);
|
|
}
|
|
|
|
public function addBattery(Request $req, EntityManagerInterface $em)
|
|
{
|
|
// required parameters
|
|
$params = [
|
|
'sku',
|
|
'brand_id',
|
|
'size_id',
|
|
];
|
|
|
|
$msg = $this->checkRequiredParameters($req, $params);
|
|
// error_log('msg - ' . $msg);
|
|
if ($msg)
|
|
return new APIResponse(false, $msg);
|
|
|
|
$sku = $req->request->get('sku');
|
|
$brand_id = $req->request->get('brand_id');
|
|
$size_id = $req->request->get('size_id');
|
|
|
|
// check if sku already exists
|
|
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
|
if ($batt != null)
|
|
return new APIResponse(false, 'Battery SKU already exists.');
|
|
|
|
// check if brand exists
|
|
$batt_brand = $em->getRepository(SAPBatteryBrand::class)->find($brand_id);
|
|
if ($batt_brand == null)
|
|
return new APIResponse(false, 'Invalid brand.');
|
|
|
|
// check if size exists
|
|
$batt_size = $em->getRepository(SAPBatterySize::class)->find($size_id);
|
|
if ($batt_size == null)
|
|
return new APIResponse(false, 'Invalid size.');
|
|
|
|
|
|
$new_batt = new SAPBattery();
|
|
$new_batt->setID($sku)
|
|
->setBrand($batt_brand)
|
|
->setSize($batt_size);
|
|
|
|
try
|
|
{
|
|
$em->persist($new_batt);
|
|
$em->flush();
|
|
}
|
|
catch (UniqueConstraintViolationException $e)
|
|
{
|
|
return new APIResponse(false, 'Battery SKU already exists.');
|
|
}
|
|
|
|
// return the new battery data
|
|
$data = [
|
|
'battery' => $this->generateBatteryData($new_batt),
|
|
];
|
|
|
|
return new APIResponse(true, 'Battery added.', $data);
|
|
}
|
|
|
|
protected function generateBatteryData(SAPBattery $batt)
|
|
{
|
|
$data = [
|
|
'sku' => $batt->getID(),
|
|
'brand' => $batt->getBrand()->getID(),
|
|
'size' => $batt->getSize()->getID(),
|
|
];
|
|
|
|
return $data;
|
|
}
|
|
}
|