Move the validation for incoming and editing job orders from the controller to the service. #270

This commit is contained in:
Korina Cordero 2019-09-27 06:56:17 +00:00
parent 6c3bc652fa
commit 37c0945db9
7 changed files with 240 additions and 189 deletions

View file

@ -138,7 +138,7 @@ services:
App\Service\InvoiceGeneratorInterface: "@App\\Service\\InvoiceGenerator\\CMBInvoiceGenerator" App\Service\InvoiceGeneratorInterface: "@App\\Service\\InvoiceGenerator\\CMBInvoiceGenerator"
# job order generator # job order generator
App\Service\JobOrderGenerator\CMBJobOrderGenerator: ~ App\Service\JobOrderHandler\CMBJobOrderHandler: ~
#job order generator interface #job order generator interface
App\Service\JobOrderGeneratorInterface: "@App\\Service\\JobOrderGenerator\\CMBJobOrderGenerator" App\Service\JobOrderHandlerInterface: "@App\\Service\\JobOrderHandler\\CMBJobOrderHandler"

View file

@ -27,7 +27,7 @@ use App\Entity\JOEvent;
use App\Entity\JORejection; use App\Entity\JORejection;
use App\Service\InvoiceGeneratorInterface; use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface; use App\Service\JobOrderHandlerInterface;
use App\Service\MapTools; use App\Service\MapTools;
use App\Service\HubCounter; use App\Service\HubCounter;
use App\Service\MQTTClient; use App\Service\MQTTClient;
@ -251,58 +251,12 @@ class JobOrderController extends Controller
return $this->render('job-order/form.html.twig', $params); return $this->render('job-order/form.html.twig', $params);
} }
public function openEditSubmit(Request $req, ValidatorInterface $validator, JobOrderGeneratorInterface $joc, $id) public function openEditSubmit(Request $req, JobOrderHandlerInterface $joc, $id)
{ {
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.'); $this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
// get object data
$em = $this->getDoctrine()->getManager();
$obj = $em->getRepository(JobOrder::class)->find($id);
$user = $this->getUser();
// initialize error list
$error_array = []; $error_array = [];
$error_array = $joc->generateJobOrder($req, $id);
// make sure this object exists
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// check if lat and lng are provided
if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) {
$error_array['coordinates'] = 'No map coordinates provided. Please click on a location on the map.';
}
if (empty($error_array)) {
// coordinates
$point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
$stype = $req->request->get('service_type');
// set and save values
$obj->setDateSchedule(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_schedule_date') . " " . $req->request->get('date_schedule_time')))
->setCoordinates($point)
->setAdvanceOrder($req->request->get('flag_advance') ?? false)
->setServiceType($stype)
->setWarrantyClass($req->request->get('warranty_class'))
->setSource($req->request->get('source'))
->setDeliveryInstructions($req->request->get('delivery_instructions'))
->setTier1Notes($req->request->get('tier1_notes'))
->setTier2Notes($req->request->get('tier2_notes'))
->setDeliveryAddress($req->request->get('delivery_address'))
->setORName($req->request->get('or_name'))
->setPromoDetail($req->request->get('promo_detail'))
->setModeOfPayment($req->request->get('mode_of_payment'))
->setLandmark($req->request->get('landmark'));
// did they change invoice?
$invoice_items = $req->request->get('invoice_items', []);
$invoice_change = $req->request->get('invoice_change', 0);
$promo_id = $req->request->get('invoice_promo');
// call service to generate job order and invoice
$joc->generateJobOrder($obj, $promo_id, $invoice_change, $invoice_items, $error_array);
}
// check if any errors were found // check if any errors were found
if (!empty($error_array)) { if (!empty($error_array)) {
@ -356,80 +310,14 @@ class JobOrderController extends Controller
return $this->render('job-order/form.html.twig', $params); return $this->render('job-order/form.html.twig', $params);
} }
public function incomingSubmit(Request $req, ValidatorInterface $validator, public function incomingSubmit(Request $req, JobOrderHandlerInterface $joc)
JobOrderGeneratorInterface $joc)
{ {
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.'); $this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
// initialize error list // initialize error list
$error_array = []; $error_array = [];
$id = -1;
// create new row $error_array = $joc->generateJobOrder($req, $id);
$em = $this->getDoctrine()->getManager();
$obj = new JobOrder();
// check if lat and lng are provided
if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) {
$error_array['coordinates'] = 'No map coordinates provided. Please click on a location on the map.';
}
// check if customer vehicle is set
if (empty($req->request->get('customer_vehicle'))) {
$error_array['customer_vehicle'] = 'No vehicle selected.';
} else {
// get customer vehicle
$cust_vehicle = $em->getRepository(CustomerVehicle::class)->find($req->request->get('customer_vehicle'));
if (empty($cust_vehicle)) {
$error_array['customer_vehicle'] = 'Invalid vehicle specified.';
}
}
if (empty($error_array)) {
// coordinates
$point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
$stype = $req->request->get('service_type');
// set and save values
$obj->setDateSchedule(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_schedule_date') . " " . $req->request->get('date_schedule_time')))
->setCoordinates($point)
->setAdvanceOrder($req->request->get('flag_advance') ?? false)
->setCreatedBy($this->getUser())
->setServiceType($stype)
->setWarrantyClass($req->request->get('warranty_class'))
->setCustomer($cust_vehicle->getCustomer())
->setCustomerVehicle($cust_vehicle)
->setSource($req->request->get('source'))
->setStatus(JOStatus::PENDING)
->setDeliveryInstructions($req->request->get('delivery_instructions'))
->setTier1Notes($req->request->get('tier1_notes'))
->setTier2Notes($req->request->get('tier2_notes'))
->setDeliveryAddress($req->request->get('delivery_address'))
->setORName($req->request->get('or_name'))
->setPromoDetail($req->request->get('promo_detail'))
->setModeOfPayment($req->request->get('mode_of_payment'))
->setLandmark($req->request->get('landmark'));
// check if reference JO is set and validate
if (!empty($req->request->get('ref_jo'))) {
// get reference JO
$ref_jo = $em->getRepository(JobOrder::class)->find($req->request->get('ref_jo'));
if (empty($ref_jo)) {
$error_array['ref_jo'] = 'Invalid reference job order specified.';
} else {
$obj->setReferenceJO($ref_jo);
}
}
// call service to generate job order and invoice
$invoice_items = $req->request->get('invoice_items', []);
$promo_id = $req->request->get('invoice_promo');
$invoice_change = true;
$joc->generateJobOrder($obj, $promo_id, $invoice_change, $invoice_items, $error_array);
}
// check if any errors were found // check if any errors were found
if (!empty($error_array)) { if (!empty($error_array)) {

View file

@ -108,6 +108,9 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$invoice->setCreatedBy($user); $invoice->setCreatedBy($user);
} }
// NOTE: we compute and save VAT up to the 2nd decimal place.
// it's only in the display when viewing a job order, where
// VAT is seen as a whole number.
$invoice->setTotalPrice($total['total_price']) $invoice->setTotalPrice($total['total_price'])
->setVATExclusivePrice($total['vat_ex_price']) ->setVATExclusivePrice($total['vat_ex_price'])
->setVAT($total['vat']) ->setVAT($total['vat'])

View file

@ -1,13 +0,0 @@
<?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, bool $invoice_change,
array $invoice_items, array &$error_array);
}

View file

@ -1,27 +1,32 @@
<?php <?php
namespace App\Service\JobOrderGenerator; namespace App\Service\JobOrderHandler;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder; use App\Entity\JobOrder;
use App\Entity\Battery; use App\Entity\Battery;
use App\Entity\JOEvent; use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Ramcar\InvoiceCriteria; use App\Ramcar\InvoiceCriteria;
use App\Ramcar\ServiceType; use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType; use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType; use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Service\InvoiceGeneratorInterface; use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface; use App\Service\JobOrderHandlerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime; use DateTime;
class CMBJobOrderGenerator implements JobOrderGeneratorInterface class CMBJobOrderHandler implements JobOrderHandlerInterface
{ {
protected $em; protected $em;
protected $ic; protected $ic;
@ -34,13 +39,92 @@ class CMBJobOrderGenerator implements JobOrderGeneratorInterface
$this->security = $security; $this->security = $security;
$this->validator = $validator; $this->validator = $validator;
} }
public function generateJobOrder(JobOrder $jo, $promo_id, $invoice_change, $invoice_items, &$error_array) public function generateJobOrder(Request $req, $id)
{ {
// TODO: data validation to be moved here // initialize error list
$error_array = [];
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
if (empty($jo))
{
// new job order
$jo = new JobOrder();
}
// check if lat and lng are provided
if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) {
$error_array['coordinates'] = 'No map coordinates provided. Please click on a location on the map.';
}
// check if customer vehicle is set
if (empty($req->request->get('customer_vehicle'))) {
$error_array['customer_vehicle'] = 'No vehicle selected.';
} else {
// get customer vehicle
$cust_vehicle = $em->getRepository(CustomerVehicle::class)->find($req->request->get('customer_vehicle'));
if (empty($cust_vehicle)) {
$error_array['customer_vehicle'] = 'Invalid vehicle specified.';
}
}
if (empty($error_array)) {
// get current user
$user = $this->security->getUser();
// coordinates
$point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
$stype = $req->request->get('service_type');
// set and save values
$jo->setDateSchedule(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_schedule_date') . " " . $req->request->get('date_schedule_time')))
->setCoordinates($point)
->setAdvanceOrder($req->request->get('flag_advance') ?? false)
->setServiceType($stype)
->setWarrantyClass($req->request->get('warranty_class'))
->setCustomer($cust_vehicle->getCustomer())
->setCustomerVehicle($cust_vehicle)
->setSource($req->request->get('source'))
->setStatus(JOStatus::PENDING)
->setDeliveryInstructions($req->request->get('delivery_instructions'))
->setTier1Notes($req->request->get('tier1_notes'))
->setTier2Notes($req->request->get('tier2_notes'))
->setDeliveryAddress($req->request->get('delivery_address'))
->setORName($req->request->get('or_name'))
->setPromoDetail($req->request->get('promo_detail'))
->setModeOfPayment($req->request->get('mode_of_payment'))
->setLandmark($req->request->get('landmark'));
// check if user is null, meaning call to create came from API
if ($user != null)
{
$jo->setCreatedBy($user);
}
// check if reference JO is set and validate
if (!empty($req->request->get('ref_jo'))) {
// get reference JO
$ref_jo = $em->getRepository(JobOrder::class)->find($req->request->get('ref_jo'));
if (empty($ref_jo)) {
$error_array['ref_jo'] = 'Invalid reference job order specified.';
} else {
$jo->setReferenceJO($ref_jo);
}
}
// call service to generate job order and invoice
$invoice_items = $req->request->get('invoice_items', []);
$promo_id = $req->request->get('invoice_promo');
$invoice_change = $req->request->get('invoice_change', 0);
// check if invoice changed // check if invoice changed
if ($invoice_change) if ($invoice_change)
{ {
// TODO: move invoice processing to InvoiceGenerator
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array); $this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
} }
@ -56,10 +140,7 @@ class CMBJobOrderGenerator implements JobOrderGeneratorInterface
if (empty($error_array)) if (empty($error_array))
{ {
// validated, no error. save the job order // validated, no error. save the job order
$this->em->persist($jo); $em->persist($jo);
// get current user
$user = $this->security->getUser();
// the event // the event
$event = new JOEvent(); $event = new JOEvent();
@ -72,9 +153,9 @@ class CMBJobOrderGenerator implements JobOrderGeneratorInterface
$event->setUser($user); $event->setUser($user);
} }
$this->em->persist($event); $em->persist($event);
$em->flush();
$this->em->flush(); }
} }
return $error_array; return $error_array;

View file

@ -1,27 +1,32 @@
<?php <?php
namespace App\Service\JobOrderGenerator; namespace App\Service\JobOrderHandler;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder; use App\Entity\JobOrder;
use App\Entity\Battery; use App\Entity\Battery;
use App\Entity\JOEvent; use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Ramcar\InvoiceCriteria; use App\Ramcar\InvoiceCriteria;
use App\Ramcar\ServiceType; use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType; use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType; use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Service\InvoiceGeneratorInterface; use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface; use App\Service\JobOrderHandlerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime; use DateTime;
class ResqJobOrderGenerator implements JobOrderGeneratorInterface class ResqJobOrderHandler implements JobOrderHandlerInterface
{ {
protected $em; protected $em;
protected $ic; protected $ic;
@ -34,13 +39,92 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
$this->security = $security; $this->security = $security;
$this->validator = $validator; $this->validator = $validator;
} }
public function generateJobOrder(JobOrder $jo, $promo_id, $invoice_change, $invoice_items, &$error_array) public function generateJobOrder(Request $req, $id)
{ {
// TODO: data validation to be moved here // initialize error list
$error_array = [];
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
if (empty($jo))
{
// new job order
$jo = new JobOrder();
}
// check if lat and lng are provided
if (empty($req->request->get('coord_lng')) || empty($req->request->get('coord_lat'))) {
$error_array['coordinates'] = 'No map coordinates provided. Please click on a location on the map.';
}
// check if customer vehicle is set
if (empty($req->request->get('customer_vehicle'))) {
$error_array['customer_vehicle'] = 'No vehicle selected.';
} else {
// get customer vehicle
$cust_vehicle = $em->getRepository(CustomerVehicle::class)->find($req->request->get('customer_vehicle'));
if (empty($cust_vehicle)) {
$error_array['customer_vehicle'] = 'Invalid vehicle specified.';
}
}
if (empty($error_array)) {
// get current user
$user = $this->security->getUser();
// coordinates
$point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
$stype = $req->request->get('service_type');
// set and save values
$jo->setDateSchedule(DateTime::createFromFormat("d M Y h:i A", $req->request->get('date_schedule_date') . " " . $req->request->get('date_schedule_time')))
->setCoordinates($point)
->setAdvanceOrder($req->request->get('flag_advance') ?? false)
->setServiceType($stype)
->setWarrantyClass($req->request->get('warranty_class'))
->setCustomer($cust_vehicle->getCustomer())
->setCustomerVehicle($cust_vehicle)
->setSource($req->request->get('source'))
->setStatus(JOStatus::PENDING)
->setDeliveryInstructions($req->request->get('delivery_instructions'))
->setTier1Notes($req->request->get('tier1_notes'))
->setTier2Notes($req->request->get('tier2_notes'))
->setDeliveryAddress($req->request->get('delivery_address'))
->setORName($req->request->get('or_name'))
->setPromoDetail($req->request->get('promo_detail'))
->setModeOfPayment($req->request->get('mode_of_payment'))
->setLandmark($req->request->get('landmark'));
// check if user is null, meaning call to create came from API
if ($user != null)
{
$jo->setCreatedBy($user);
}
// check if reference JO is set and validate
if (!empty($req->request->get('ref_jo'))) {
// get reference JO
$ref_jo = $em->getRepository(JobOrder::class)->find($req->request->get('ref_jo'));
if (empty($ref_jo)) {
$error_array['ref_jo'] = 'Invalid reference job order specified.';
} else {
$jo->setReferenceJO($ref_jo);
}
}
// call service to generate job order and invoice
$invoice_items = $req->request->get('invoice_items', []);
$promo_id = $req->request->get('invoice_promo');
$invoice_change = $req->request->get('invoice_change', 0);
// check if invoice changed // check if invoice changed
if ($invoice_change) if ($invoice_change)
{ {
// TODO: move invoice processing to InvoiceGenerator
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array); $this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
} }
@ -56,10 +140,7 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
if (empty($error_array)) if (empty($error_array))
{ {
// validated, no error. save the job order // validated, no error. save the job order
$this->em->persist($jo); $em->persist($jo);
// get current user
$user = $this->security->getUser();
// the event // the event
$event = new JOEvent(); $event = new JOEvent();
@ -72,9 +153,9 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
$event->setUser($user); $event->setUser($user);
} }
$this->em->persist($event); $em->persist($event);
$em->flush();
$this->em->flush(); }
} }
return $error_array; return $error_array;

View file

@ -0,0 +1,11 @@
<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\Request;
interface JobOrderHandlerInterface
{
// generate job order
public function generateJobOrder(Request $req, int $id);
}