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"
# job order generator
App\Service\JobOrderGenerator\CMBJobOrderGenerator: ~
App\Service\JobOrderHandler\CMBJobOrderHandler: ~
#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\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
use App\Service\MapTools;
use App\Service\HubCounter;
use App\Service\MQTTClient;
@ -251,58 +251,12 @@ class JobOrderController extends Controller
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.');
// get object data
$em = $this->getDoctrine()->getManager();
$obj = $em->getRepository(JobOrder::class)->find($id);
$user = $this->getUser();
// initialize error list
$error_array = [];
// 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);
}
$error_array = $joc->generateJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {
@ -356,80 +310,14 @@ class JobOrderController extends Controller
return $this->render('job-order/form.html.twig', $params);
}
public function incomingSubmit(Request $req, ValidatorInterface $validator,
JobOrderGeneratorInterface $joc)
public function incomingSubmit(Request $req, JobOrderHandlerInterface $joc)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
// initialize error list
$error_array = [];
// create new row
$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);
}
$id = -1;
$error_array = $joc->generateJobOrder($req, $id);
// check if any errors were found
if (!empty($error_array)) {

View file

@ -108,6 +108,9 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$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'])
->setVATExclusivePrice($total['vat_ex_price'])
->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
namespace App\Service\JobOrderGenerator;
namespace App\Service\JobOrderHandler;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Entity\Battery;
use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Ramcar\InvoiceCriteria;
use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
class CMBJobOrderGenerator implements JobOrderGeneratorInterface
class CMBJobOrderHandler implements JobOrderHandlerInterface
{
protected $em;
protected $ic;
@ -34,47 +39,123 @@ class CMBJobOrderGenerator implements JobOrderGeneratorInterface
$this->security = $security;
$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
// check if invoice changed
if ($invoice_change)
// initialize error list
$error_array = [];
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
if (empty($jo))
{
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
// new job order
$jo = new JobOrder();
}
// validate
$errors = $this->validator->validate($jo);
// add errors to list
foreach ($errors as $error) {
$error_array[$error->getPropertyPath()] = $error->getMessage();
// 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 errors are found
if (empty($error_array))
{
// validated, no error. save the job order
$this->em->persist($jo);
// 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();
// the event
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID(JOEventType::CREATE)
->setJobOrder($jo);
// 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)
{
$event->setUser($user);
$jo->setCreatedBy($user);
}
$this->em->persist($event);
// 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'));
$this->em->flush();
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
if ($invoice_change)
{
// TODO: move invoice processing to InvoiceGenerator
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
}
// validate
$errors = $this->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);
// 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;

View file

@ -1,27 +1,32 @@
<?php
namespace App\Service\JobOrderGenerator;
namespace App\Service\JobOrderHandler;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Entity\Battery;
use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Ramcar\InvoiceCriteria;
use App\Ramcar\ServiceType;
use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
class ResqJobOrderGenerator implements JobOrderGeneratorInterface
class ResqJobOrderHandler implements JobOrderHandlerInterface
{
protected $em;
protected $ic;
@ -34,47 +39,123 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
$this->security = $security;
$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
// check if invoice changed
if ($invoice_change)
// initialize error list
$error_array = [];
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
if (empty($jo))
{
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
// new job order
$jo = new JobOrder();
}
// validate
$errors = $this->validator->validate($jo);
// add errors to list
foreach ($errors as $error) {
$error_array[$error->getPropertyPath()] = $error->getMessage();
// 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 errors are found
if (empty($error_array))
{
// validated, no error. save the job order
$this->em->persist($jo);
// 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();
// the event
$event = new JOEvent();
$event->setDateHappen(new DateTime())
->setTypeID(JOEventType::CREATE)
->setJobOrder($jo);
// 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)
{
$event->setUser($user);
$jo->setCreatedBy($user);
}
$this->em->persist($event);
// 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'));
$this->em->flush();
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
if ($invoice_change)
{
// TODO: move invoice processing to InvoiceGenerator
$this->processInvoice($jo, $promo_id, $invoice_items, $error_array);
}
// validate
$errors = $this->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);
// 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;

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);
}