Move the saving of new job order to a job order generator service. #265
This commit is contained in:
parent
cf8c3bb8e1
commit
ca97897a2a
4 changed files with 221 additions and 70 deletions
|
|
@ -131,8 +131,14 @@ services:
|
|||
tags:
|
||||
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
|
||||
|
||||
# invoice creator
|
||||
# invoice generator
|
||||
App\Service\InvoiceGenerator\ResqInvoiceGenerator: ~
|
||||
|
||||
# invoice creator interface
|
||||
# invoice generator interface
|
||||
App\Service\InvoiceGeneratorInterface: "@App\\Service\\InvoiceGenerator\\ResqInvoiceGenerator"
|
||||
|
||||
# job order generator
|
||||
App\Service\JobOrderGenerator\ResqJobOrderGenerator: ~
|
||||
|
||||
#job order generator interface
|
||||
App\Service\JobOrderGeneratorInterface: "@App\\Service\\JobOrderGenerator\\ResqJobOrderGenerator"
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ use App\Entity\JOEvent;
|
|||
use App\Entity\JORejection;
|
||||
|
||||
use App\Service\InvoiceGeneratorInterface;
|
||||
use App\Service\JobOrderGeneratorInterface;
|
||||
use App\Service\MapTools;
|
||||
use App\Service\HubCounter;
|
||||
use App\Service\MQTTClient;
|
||||
|
|
@ -422,7 +423,8 @@ class JobOrderController extends Controller
|
|||
return $this->render('job-order/form.html.twig', $params);
|
||||
}
|
||||
|
||||
public function incomingSubmit(Request $req, ValidatorInterface $validator, InvoiceGeneratorInterface $ic)
|
||||
public function incomingSubmit(Request $req, ValidatorInterface $validator,
|
||||
JobOrderGeneratorInterface $joc)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
|
||||
|
||||
|
|
@ -488,61 +490,12 @@ class JobOrderController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
// instantiate invoice criteria
|
||||
$criteria = new InvoiceCriteria();
|
||||
$criteria->setServiceType($stype)
|
||||
->setCustomerVehicle($cust_vehicle);
|
||||
// call service to generate job order and invoice
|
||||
$invoice_items = $req->request->get('invoice_items', []);
|
||||
$promo_id = $req->request->get('invoice_promo');
|
||||
|
||||
$ierror = $this->invoicePromo($em, $criteria, $req->request->get('invoice_promo'));
|
||||
$invoice_items = $req->request->get('invoice_items');
|
||||
|
||||
if (!$ierror && !empty($invoice_items))
|
||||
{
|
||||
// check for trade in so we can mark it for mobile app
|
||||
foreach ($invoice_items as $item)
|
||||
{
|
||||
// get first trade-in
|
||||
if (!empty($item['trade_in']))
|
||||
{
|
||||
$obj->setTradeInType($item['trade_in']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$ierror = $this->invoiceBatteries($em, $criteria, $invoice_items);
|
||||
}
|
||||
|
||||
if ($ierror)
|
||||
{
|
||||
$error_array['invoice'] = $ierror;
|
||||
}
|
||||
else
|
||||
{
|
||||
// generate the invoice
|
||||
$iobj = $ic->generateInvoice($criteria);
|
||||
|
||||
// validate
|
||||
$ierrors = $validator->validate($iobj);
|
||||
|
||||
// add errors to list
|
||||
foreach ($ierrors as $error) {
|
||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||
}
|
||||
|
||||
// add invoice to JO
|
||||
$obj->setInvoice($iobj);
|
||||
|
||||
// save
|
||||
$em->persist($iobj);
|
||||
}
|
||||
|
||||
// validate
|
||||
$errors = $validator->validate($obj);
|
||||
|
||||
// add errors to list
|
||||
foreach ($errors as $error) {
|
||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||
}
|
||||
error_log('number of invoice items ' . count($invoice_items));
|
||||
$joc->generateJobOrder($obj, $promo_id, $invoice_items, $error_array);
|
||||
}
|
||||
|
||||
// check if any errors were found
|
||||
|
|
@ -554,19 +507,6 @@ class JobOrderController extends Controller
|
|||
], 422);
|
||||
}
|
||||
|
||||
// validated! save the entity
|
||||
$em->persist($obj);
|
||||
|
||||
// the event
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::CREATE)
|
||||
->setUser($this->getUser())
|
||||
->setJobOrder($obj);
|
||||
$em->persist($event);
|
||||
|
||||
$em->flush();
|
||||
|
||||
// return successful response
|
||||
return $this->json([
|
||||
'success' => 'Changes have been saved!'
|
||||
|
|
|
|||
193
src/Service/JobOrderGenerator/ResqJobOrderGenerator.php
Normal file
193
src/Service/JobOrderGenerator/ResqJobOrderGenerator.php
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service\JobOrderGenerator;
|
||||
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\Entity\JobOrder;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\JOEvent;
|
||||
|
||||
use App\Ramcar\InvoiceCriteria;
|
||||
use App\Ramcar\ServiceType;
|
||||
use App\Ramcar\TradeInType;
|
||||
use App\Ramcar\JOEventType;
|
||||
|
||||
use App\Service\InvoiceGeneratorInterface;
|
||||
use App\Service\JobOrderGeneratorInterface;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class ResqJobOrderGenerator implements JobOrderGeneratorInterface
|
||||
{
|
||||
protected $em;
|
||||
protected $ic;
|
||||
|
||||
public function __construct(Security $security, EntityManagerInterface $em,
|
||||
InvoiceGeneratorInterface $ic, ValidatorInterface $validator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->ic = $ic;
|
||||
$this->security = $security;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
public function generateJobOrder(JobOrder $jo, $promo_id, $invoice_items, &$error_array)
|
||||
{
|
||||
// TODO: data validation to be moved here
|
||||
$validator = $this->validator;
|
||||
$em = $this->em;
|
||||
|
||||
// instantiate the invoice criteria
|
||||
$criteria = new InvoiceCriteria();
|
||||
$criteria->setServiceType($jo->getServiceType())
|
||||
->setCustomerVehicle($jo->getCustomerVehicle());
|
||||
|
||||
$ierror = $this->invoicePromo($criteria, $promo_id);
|
||||
|
||||
if (!$ierror && !empty($invoice_items))
|
||||
{
|
||||
// check for trade-in so we can mark it for mobile app
|
||||
foreach ($invoice_items as $item)
|
||||
{
|
||||
// get first trade-in
|
||||
if (!empty($item['trade_in']))
|
||||
{
|
||||
$jo->getTradeInType($item['trade_in']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$ierror = $this->invoiceBatteries($criteria, $invoice_items);
|
||||
}
|
||||
|
||||
if ($ierror)
|
||||
{
|
||||
$error_array['invoice'] = $ierror;
|
||||
}
|
||||
else
|
||||
{
|
||||
// generate the invoice
|
||||
$iobj = $this->ic->generateInvoice($criteria);
|
||||
|
||||
// validate
|
||||
$ierrors = $validator->validate($iobj);
|
||||
|
||||
// add errors to list
|
||||
foreach ($ierrors as $error) {
|
||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||
}
|
||||
|
||||
// add invoice to JO
|
||||
$jo->setInvoice($iobj);
|
||||
|
||||
$em->persist($iobj);
|
||||
|
||||
}
|
||||
|
||||
// validate
|
||||
$errors = $validator->validate($jo);
|
||||
|
||||
// add errors to list
|
||||
foreach ($errors as $error) {
|
||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||
}
|
||||
|
||||
// check if errors are found
|
||||
if (empty($error_array))
|
||||
{
|
||||
// validated, no error. save the job order
|
||||
$em->persist($jo);
|
||||
|
||||
// get current user
|
||||
$user = $this->security->getUser();
|
||||
|
||||
// the event
|
||||
$event = new JOEvent();
|
||||
$event->setDateHappen(new DateTime())
|
||||
->setTypeID(JOEventType::CREATE)
|
||||
->setJobOrder($jo);
|
||||
|
||||
if ($user != null)
|
||||
{
|
||||
$event->setUser($user);
|
||||
}
|
||||
|
||||
$em->persist($event);
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
return $error_array;
|
||||
}
|
||||
|
||||
protected function invoicePromo(InvoiceCriteria $criteria, $promo_id)
|
||||
{
|
||||
// return error if there's a problem, false otherwise
|
||||
// check service type
|
||||
$stype = $criteria->getServiceType();
|
||||
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW)
|
||||
return null;
|
||||
|
||||
|
||||
if (empty($promo_id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if this is a valid promo
|
||||
$promo = $this->em->getRepository(Promo::class)->find($promo_id);
|
||||
|
||||
if (empty($promo))
|
||||
return 'Invalid promo specified.';
|
||||
|
||||
$criteria->addPromo($promo);
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function invoiceBatteries(InvoiceCriteria $criteria, $items)
|
||||
{
|
||||
// check service type
|
||||
$stype = $criteria->getServiceType();
|
||||
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY)
|
||||
return null;
|
||||
|
||||
// return error if there's a problem, false otherwise
|
||||
if (!empty($items))
|
||||
{
|
||||
foreach ($items as $item)
|
||||
{
|
||||
// check if this is a valid battery
|
||||
$battery = $this->em->getRepository(Battery::class)->find($item['battery']);
|
||||
|
||||
if (empty($battery))
|
||||
{
|
||||
$error = 'Invalid battery specified.';
|
||||
return $error;
|
||||
}
|
||||
|
||||
// quantity
|
||||
$qty = $item['quantity'];
|
||||
if ($qty < 1)
|
||||
continue;
|
||||
|
||||
/*
|
||||
// add to criteria
|
||||
$criteria->addBattery($battery, $qty);
|
||||
*/
|
||||
|
||||
// if this is a trade in, add trade in
|
||||
if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in']))
|
||||
$trade_in = $item['trade_in'];
|
||||
else
|
||||
$trade_in = null;
|
||||
|
||||
$criteria->addEntry($battery, $trade_in, $qty);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
12
src/Service/JobOrderGeneratorInterface.php
Normal file
12
src/Service/JobOrderGeneratorInterface.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\JobOrder;
|
||||
use App\Entity\InvoiceItem;
|
||||
|
||||
interface JobOrderGeneratorInterface
|
||||
{
|
||||
// generate job order
|
||||
public function generateJobOrder(JobOrder $jo, int $promo_id, array $invoice_items, array &$error_array);
|
||||
}
|
||||
Loading…
Reference in a new issue