Refactor submission of updated job order. #265
This commit is contained in:
parent
ca97897a2a
commit
b129609d53
2 changed files with 56 additions and 113 deletions
|
|
@ -251,7 +251,7 @@ 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, InvoiceGeneratorInterface $ic, $id)
|
public function openEditSubmit(Request $req, ValidatorInterface $validator, JobOrderGeneratorInterface $joc, $id)
|
||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
|
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
|
||||||
|
|
||||||
|
|
@ -296,68 +296,11 @@ class JobOrderController extends Controller
|
||||||
|
|
||||||
// did they change invoice?
|
// did they change invoice?
|
||||||
$invoice_items = $req->request->get('invoice_items', []);
|
$invoice_items = $req->request->get('invoice_items', []);
|
||||||
$invoice_change = $req->request->get('invoice_change', 0);
|
$promo_id = $req->request->get('invoice_promo');
|
||||||
if ($invoice_change)
|
|
||||||
{
|
// call service to generate job order and invoice
|
||||||
// instantiate invoice criteria
|
$joc->generateJobOrder($obj, $promo_id, $invoice_items, $error_array);
|
||||||
$criteria = new InvoiceCriteria();
|
|
||||||
$criteria->setServiceType($stype)
|
|
||||||
->setCustomerVehicle($obj->getCustomerVehicle());
|
|
||||||
|
|
||||||
$ierror = $this->invoicePromo($em, $criteria, $req->request->get('invoice_promo'));
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$ierror)
|
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove previous invoice
|
|
||||||
$old_invoice = $obj->getInvoice();
|
|
||||||
$em->remove($old_invoice);
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
// add invoice to JO
|
|
||||||
$obj->setInvoice($iobj);
|
|
||||||
|
|
||||||
// persist invoice
|
|
||||||
$em->persist($iobj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// validate
|
|
||||||
$errors = $validator->validate($obj);
|
|
||||||
|
|
||||||
// add errors to list
|
|
||||||
foreach ($errors as $error) {
|
|
||||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if any errors were found
|
// check if any errors were found
|
||||||
|
|
@ -369,17 +312,6 @@ class JobOrderController extends Controller
|
||||||
], 422);
|
], 422);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the event
|
|
||||||
$event = new JOEvent();
|
|
||||||
$event->setDateHappen(new DateTime())
|
|
||||||
->setTypeID(JOEventType::OPEN_EDIT)
|
|
||||||
->setUser($this->getUser())
|
|
||||||
->setJobOrder($obj);
|
|
||||||
$em->persist($event);
|
|
||||||
|
|
||||||
// validated! save the entity
|
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
// return successful response
|
// return successful response
|
||||||
return $this->json([
|
return $this->json([
|
||||||
'success' => 'Changes have been saved!'
|
'success' => 'Changes have been saved!'
|
||||||
|
|
@ -494,7 +426,6 @@ class JobOrderController extends Controller
|
||||||
$invoice_items = $req->request->get('invoice_items', []);
|
$invoice_items = $req->request->get('invoice_items', []);
|
||||||
$promo_id = $req->request->get('invoice_promo');
|
$promo_id = $req->request->get('invoice_promo');
|
||||||
|
|
||||||
error_log('number of invoice items ' . count($invoice_items));
|
|
||||||
$joc->generateJobOrder($obj, $promo_id, $invoice_items, $error_array);
|
$joc->generateJobOrder($obj, $promo_id, $invoice_items, $error_array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,9 +37,47 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
|
||||||
public function generateJobOrder(JobOrder $jo, $promo_id, $invoice_items, &$error_array)
|
public function generateJobOrder(JobOrder $jo, $promo_id, $invoice_items, &$error_array)
|
||||||
{
|
{
|
||||||
// TODO: data validation to be moved here
|
// TODO: data validation to be moved here
|
||||||
$validator = $this->validator;
|
|
||||||
$em = $this->em;
|
$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
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->persist($event);
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $error_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function processInvoice($jo, $promo_id, $invoice_items, &$error_array)
|
||||||
|
{
|
||||||
// instantiate the invoice criteria
|
// instantiate the invoice criteria
|
||||||
$criteria = new InvoiceCriteria();
|
$criteria = new InvoiceCriteria();
|
||||||
$criteria->setServiceType($jo->getServiceType())
|
$criteria->setServiceType($jo->getServiceType())
|
||||||
|
|
@ -73,54 +111,28 @@ class ResqJobOrderGenerator implements JobOrderGeneratorInterface
|
||||||
$iobj = $this->ic->generateInvoice($criteria);
|
$iobj = $this->ic->generateInvoice($criteria);
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$ierrors = $validator->validate($iobj);
|
$ierrors = $this->validator->validate($iobj);
|
||||||
|
|
||||||
// add errors to list
|
// add errors to list
|
||||||
foreach ($ierrors as $error) {
|
foreach ($ierrors as $error) {
|
||||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if invoice already exists for JO
|
||||||
|
$old_invoice = $jo->getInvoice();
|
||||||
|
if ($old_invoice != null)
|
||||||
|
{
|
||||||
|
// remove old invoice
|
||||||
|
$this->em->remove($old_invoice);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
// add invoice to JO
|
// add invoice to JO
|
||||||
$jo->setInvoice($iobj);
|
$jo->setInvoice($iobj);
|
||||||
|
|
||||||
$em->persist($iobj);
|
$this->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)
|
protected function invoicePromo(InvoiceCriteria $criteria, $promo_id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue