Merge branch '172-new-battery-api-module' into 'master'

Resolve "New battery api module"

Closes #172

See merge request jankstudio/resq!206
This commit is contained in:
Kendrick Chan 2019-01-21 08:56:14 +00:00
commit 963fb849be
16 changed files with 1325 additions and 570 deletions

View file

@ -39,8 +39,9 @@ class TestCommand extends Command
// test
$api->get('/capi/test');
/*
// TODO: shift this out of the bundle, since it's project specific
// warranty register
$serial = 'AJ34LJADR12134LKJL';
$plate_num = 'XEN918';
@ -48,22 +49,23 @@ class TestCommand extends Command
'serial' => $serial,
'plate_number' => $plate_num,
'warranty_class' => 'private',
/*
'battery_model_id' => 438,
'battery_size_id' => 1171,
*/
'sku' => 'WMEB24CB-CPN00-LX',
'date_purchase' => '20181001',
'date_expire' => '20191001',
];
$api->post('/capi/warranties', $params);
*/
// get all warranties
$api->get('/capi/warranties');
/*
// warranty find
$api->get('/capi/warranties/' . $serial);
*/
// warranty claim
$id = 86811;
@ -73,18 +75,17 @@ class TestCommand extends Command
];
$api->post('/capi/warranties/' . $id . '/claim', $params);
/*
// plate warranty
$api->get('/capi/plates/' . $plate_num . '/warranties');
*/
// battery
// $api->get('/capi/battery_models');
// $api->get('/capi/battery_sizes');
$api->get('/capi/battery_brands');
$api->get('/capi/battery_sizes');
$api->get('/capi/batteries');
// vehicle
// $api->get('/capi/vehicle_manufacturers');
// $api->get('/capi/vehicles');
*/
}
}

View file

@ -38,7 +38,8 @@
"autoload": {
"psr-4": {
"App\\": "src/",
"Catalyst\\APIBundle\\": "catalyst/api-bundle/"
"Catalyst\\APIBundle\\": "catalyst/api-bundle/",
"RamcarBattery\\APIBundle\\": "ramcar-batery/api-bundle/"
}
},
"autoload-dev": {

1367
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,7 +0,0 @@
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks:
- '%locale%'

View file

@ -7,9 +7,21 @@ capi_test:
# battery api
# battery models
capi_battery_models:
path: /capi/battery_models
controller: App\Controller\CAPI\BatteryController::getModels
#capi_battery_models:
# path: /capi/battery_models
# controller: App\Controller\CAPI\BatteryController::getModels
# methods: [GET]
# battery models
capi_battery_brands:
path: /capi/battery_brands
controller: App\Controller\CAPI\BatteryController::getBrands
methods: [GET]
# batteries
capi_batteries:
path: /capi/batteries
controller: App\Controller\CAPI\BatteryController::getBatteries
methods: [GET]
# battery sizes

View file

@ -1,7 +1,6 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: 'en'
map_default:
latitude: 14.6091
longitude: 121.0223

View file

@ -0,0 +1,10 @@
<?php
namespace RamcarBattery\APIBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class RamcarBatteryAPIBundle extends Bundle
{
}

View file

@ -0,0 +1,118 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\SAPBattery;
use App\Entity\SAPBatteryBrand;
use App\Entity\SAPBatterySize;
use DateTime;
class ImportSAPBatteryCommand extends Command
{
private $em;
public function __construct(ObjectManager $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('sap_battery:import')
->setDescription('Import a CSV file with SAP battery data.')
->setHelp('Creates SAP battery data entries off imported CSV.')
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv_file = $input->getArgument('file');
// CSV column order:
// 0 - brand
// 1 - sku
// 2 - size
// attempt to open file
try
{
$handle = fopen($csv_file, "r");
}
catch (Exception $e)
{
throw new Exception('The file "' . $csv_file . '" could not be read.');
}
// get entity manager
$em = $this->em;
// row counter
$row = 1;
// has error?
$has_error = false;
// hashes
$brand_hash = [];
$size_hash = [];
$battery_hash = [];
// loop through rows and build hashes
while (($fields = fgetcsv($handle)) !== false)
{
// check if blank
if (strlen(trim($fields[0])) == 0)
continue;
// clean up fields
$clean_brand = trim($fields[0]);
$clean_sku = trim($fields[1]);
$clean_size = trim($fields[2]);
$output->writeln("Parsing $clean_sku...");
// brand hash
if (!isset($brand_hash[$clean_brand]))
{
$brand = new SAPBatteryBrand();
$brand->setName($clean_brand);
$em->persist($brand);
$brand_hash[$clean_brand] = $brand;
}
// size hash
if (!isset($size_hash[$clean_size]))
{
$size = new SAPBatterySize();
$size->setName($clean_size);
$em->persist($size);
$size_hash[$clean_size] = $size;
}
// create battery entry
$battery = new SAPBattery();
$battery->setID($clean_sku)
->setSize($size_hash[$clean_size])
->setBrand($brand_hash[$clean_brand]);
$em->persist($battery);
}
$em->flush();
}
}

View file

@ -8,34 +8,57 @@ use Doctrine\ORM\Query;
use Doctrine\ORM\EntityManagerInterface;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Entity\SAPBattery;
use App\Entity\SAPBatterySize;
use App\Entity\SAPBatteryBrand;
class BatteryController extends APIController
{
public function getModels(EntityManagerInterface $em)
public function getBatteries(EntityManagerInterface $em)
{
$models = $em->getRepository(BatteryModel::class)->findBy([], ['name' => 'ASC']);
$batteries = $em->getRepository(SAPBattery::class)->findBy([], ['id' => 'ASC']);
$result = [];
foreach ($models as $model)
foreach ($batteries as $batt)
{
$result[] = [
'id' => $model->getID(),
'name' => $model->getName(),
'id' => $batt->getID(),
'size' => $batt->getSize()->getID(),
'brand' => $batt->getBrand()->getID(),
];
}
$data = [
'models' => $result,
'batt' => $result,
];
return new APIResponse(true, 'Battery models loaded.', $data);
return new APIResponse(true, 'Batteries loaded.', $data);
}
public function getBrands(EntityManagerInterface $em)
{
$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)
{
$sizes = $em->getRepository(BatterySize::class)->findBy([], ['name' => 'ASC']);
$sizes = $em->getRepository(SAPBatterySize::class)->findBy([], ['name' => 'ASC']);
$result = [];
foreach ($sizes as $size)

View file

@ -9,9 +9,14 @@ use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\Warranty;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Entity\SAPBattery;
use App\Entity\SAPBatterySize;
use App\Entity\SAPBatteryBrand;
use App\Ramcar\NameValue;
use App\Ramcar\WarrantyClass;
use App\Ramcar\WarrantyStatus;
@ -26,20 +31,17 @@ class WarrantyController extends APIController
protected function generateWarrantyData(Warranty $warr)
{
$model = $warr->getBatteryModel();
$size = $warr->getBatterySize();
$batt = $warr->getSAPBattery();
$data = [
'id' => (int) $warr->getID(),
'serial' => (string) $warr->getSerial(),
'warranty_class' => (string) $warr->getWarrantyClass(),
'plate_number' => (string) $warr->getPlateNumber(),
'battery_model' => [
(int) ($model == null ? 0 : $model->getID()),
(string) ($model == null ? '' : $model->getName()),
],
'battery_size' => [
(int) ($size == null ? 0 : $size->getID()),
(string) ($size == null ? '' : $size->getName()),
'battery' => [
'sku' => (string) ($batt == null ? '' : $batt->getID()),
'brand' => (int) ($batt == null ? 0 : $batt->getBrand()->getID()),
'size' => (int) ($batt == null ? 0 : $batt->getSize()->getID()),
],
'status' => (string) $warr->getStatus(),
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
@ -117,8 +119,11 @@ class WarrantyController extends APIController
'plate_number',
'date_expire',
'date_purchase',
'sku',
/*
'battery_model_id',
'battery_size_id',
*/
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
@ -130,8 +135,12 @@ class WarrantyController extends APIController
$date_pur_string = $req->request->get('date_purchase');
$warr_class = $req->request->get('warranty_class');
$plate = $req->request->get('plate_number');
$sku = $req->request->get('sku');
/*
$bmodel_id = $req->request->get('battery_model_id');
$bsize_id = $req->request->get('battery_size_id');
*/
// wrong date expire format
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
@ -152,6 +161,12 @@ class WarrantyController extends APIController
if (!$plate)
return new APIResponse(false, 'Invalid plate number.');
// battery
$batt = $em->getRepository(SAPBattery::class)->find($sku);
if ($batt == null)
return new APIResponse(false, 'Invalid battery SKU.');
/*
// battery model
$model = $em->getRepository(BatteryModel::class)->find($bmodel_id);
if ($model == null)
@ -161,14 +176,18 @@ class WarrantyController extends APIController
$size = $em->getRepository(BatterySize::class)->find($bsize_id);
if ($size == null)
return new APIResponse(false, 'Invalid battery size id.');
*/
// warranty
$warr = new Warranty();
$warr->setSerial($serial)
->setWarrantyClass($warr_class)
->setPlateNumber($plate)
/*
->setBatteryModel($model)
->setBatterySize($size)
*/
->setSAPBattery($batt)
->setDatePurchase($date_pur)
->setDateClaim(null)
->setDateExpire($date_expire);
@ -229,6 +248,7 @@ class WarrantyController extends APIController
->setPlateNumber($warr->getPlateNumber())
->setBatteryModel($warr->getBatteryModel())
->setBatterySize($warr->getBatterySize())
->setSAPBattery($warr->getSAPBattery())
->setDatePurchase($warr->getDatePurchase())
->setDateClaim(null)
->setDateExpire($warr->getDateExpire())

74
src/Entity/SAPBattery.php Normal file
View file

@ -0,0 +1,74 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="sap_battery")
*/
class SAPBattery
{
// battery sku
/**
* @ORM\Id
* @ORM\Column(type="string", length=25)
*/
protected $id;
// brand
/**
* @ORM\ManyToOne(targetEntity="SAPBatteryBrand", inversedBy="batteries")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brand;
// size
/**
* @ORM\ManyToOne(targetEntity="SAPBatterySize", inversedBy="batteries")
* @ORM\JoinColumn(name="size_id", referencedColumnName="id")
*/
protected $size;
public function __construct()
{
}
public function setID($id)
{
$this->id = $id;
return $this;
}
public function getID()
{
return $this->id;
}
public function setBrand($brand)
{
$this->brand = $brand;
return $this;
}
public function getBrand()
{
return $this->brand;
}
public function setSize($size)
{
$this->size = $size;
return $this;
}
public function getSize()
{
return $this->size;
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="sap_battery_brand")
*/
class SAPBatteryBrand
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// name
/**
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
*/
protected $name;
// battery
/**
* @ORM\OneToMany(targetEntity="Battery", mappedBy="size")
*/
protected $batteries;
public function __construct()
{
$this->batteries = new ArrayCollection();
}
public function getID()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function addBattery(Battery $battery)
{
$this->batteries->add($battery);
return $this;
}
public function clearBatteries()
{
$this->batteries->clear();
return $this;
}
public function getBatteries()
{
return $this->batteries;
}
}

View file

@ -0,0 +1,73 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="sap_battery_size")
*/
class SAPBatterySize
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// name
/**
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
*/
protected $name;
// battery
/**
* @ORM\OneToMany(targetEntity="Battery", mappedBy="size")
*/
protected $batteries;
public function __construct()
{
$this->batteries = new ArrayCollection();
}
public function getID()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function addBattery(Battery $battery)
{
$this->batteries->add($battery);
return $this;
}
public function clearBatteries()
{
$this->batteries->clear();
return $this;
}
public function getBatteries()
{
return $this->batteries;
}
}

View file

@ -59,6 +59,13 @@ class Warranty
*/
protected $bty_size;
// sap battery
/**
* @ORM\ManyToOne(targetEntity="SAPBattery", inversedBy="warranties")
* @ORM\JoinColumn(name="sap_bty_id", referencedColumnName="id", nullable=true)
*/
protected $sap_bty;
// status
/**
* @ORM\Column(type="string", length=25)
@ -202,6 +209,17 @@ class Warranty
return $this->bty_size;
}
public function setSAPBattery(SAPBattery $sap_bty)
{
$this->sap_bty = $sap_bty;
return $this;
}
public function getSAPBattery()
{
return $this->sap_bty;
}
public function setStatus($status)
{
$this->status = $status;

View file

@ -119,6 +119,9 @@
"psr/simple-cache": {
"version": "1.0.0"
},
"ralouphie/getallheaders": {
"version": "2.0.5"
},
"sensio/framework-extra-bundle": {
"version": "4.0",
"recipe": {
@ -146,6 +149,9 @@
"ref": "9f94d3ea453cd8a3b95db7f82592d7344fe3a76a"
}
},
"symfony/contracts": {
"version": "v1.0.2"
},
"symfony/debug": {
"version": "v4.0.2"
},
@ -233,9 +239,6 @@
"ref": "cda8b550123383d25827705d05a42acf6819fe4e"
}
},
"symfony/security": {
"version": "v4.0.2"
},
"symfony/security-bundle": {
"version": "3.3",
"recipe": {
@ -245,18 +248,21 @@
"ref": "85834af1496735f28d831489d12ab1921a875e0d"
}
},
"symfony/security-core": {
"version": "v4.2.2"
},
"symfony/security-csrf": {
"version": "v4.2.2"
},
"symfony/security-guard": {
"version": "v4.2.2"
},
"symfony/security-http": {
"version": "v4.2.2"
},
"symfony/stopwatch": {
"version": "v4.0.2"
},
"symfony/translation": {
"version": "3.3",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "3.3",
"ref": "6bcd6c570c017ea6ae5a7a6a027c929fd3542cd8"
}
},
"symfony/twig-bridge": {
"version": "v4.0.2"
},
@ -275,6 +281,9 @@
"symfony/var-dumper": {
"version": "v4.0.2"
},
"symfony/var-exporter": {
"version": "v4.2.2"
},
"symfony/web-profiler-bundle": {
"version": "3.3",
"recipe": {

View file