diff --git a/src/Controller/CustomerAppAPI/InvoiceController.php b/src/Controller/CustomerAppAPI/InvoiceController.php index 67522c96..5f72ca19 100644 --- a/src/Controller/CustomerAppAPI/InvoiceController.php +++ b/src/Controller/CustomerAppAPI/InvoiceController.php @@ -17,6 +17,7 @@ use App\Entity\Battery; use App\Entity\BatterySize; use App\Entity\Customer; use App\Entity\CustomerMetadata; +use App\Ramcar\ServiceType; class InvoiceController extends ApiController { @@ -55,7 +56,16 @@ class InvoiceController extends ApiController // make invoice criteria $icrit = new InvoiceCriteria(); - $icrit->setServiceType($req->request->get('service_type')); + + // if service type is subscription, set to battery replacement but add extra criteria + $stype = $req->request->get('service_type'); + if ($stype === 'subscription') { + $stype = ServiceType::BATTERY_REPLACEMENT_NEW; + $icrit->setSubscription(true); + } + + // set the service type + $icrit->setServiceType($stype); // check promo $promo_id = $req->request->get('promo_id'); diff --git a/src/Controller/CustomerAppAPI/JobOrderController.php b/src/Controller/CustomerAppAPI/JobOrderController.php index 64da86e5..8764759b 100644 --- a/src/Controller/CustomerAppAPI/JobOrderController.php +++ b/src/Controller/CustomerAppAPI/JobOrderController.php @@ -42,7 +42,7 @@ use App\Entity\JOEvent; use App\Entity\Warranty; use App\Entity\JobOrder; use App\Entity\CustomerVehicle; - +use App\Ramcar\SubscriptionStatus; use DateTime; class JobOrderController extends ApiController @@ -616,9 +616,16 @@ class JobOrderController extends ApiController // validate service type $stype = $req->request->get('service_type'); + + // if this is a subscription, change to battery replacement so we follow invoice rules for this + if ($stype === 'subscription') { + $stype = ServiceType::BATTERY_REPLACEMENT_NEW; + } + if (!ServiceType::validate($stype)) { return new ApiResponse(false, 'Invalid service type.'); } + $jo->setServiceType($stype); // validate warranty @@ -712,12 +719,24 @@ class JobOrderController extends ApiController $pt_id = $pt_manager->getPriceTier($jo->getCoordinates()); $icrit->setPriceTier($pt_id); + // if subscription, set subscription on invoice criteria and find the subscription row to associate with the JO + if ($req->request->get('service_type') === 'subscription') { + $icrit->setSubscription(true); + + // get subscription row and check state + $sub = $cv->getLatestSubscription(); + if (empty($sub) || $sub->getStatus() !== SubscriptionStatus::ACTIVE) { + return new ApiResponse(false, 'Invalid subscription state for this vehicle.'); + } + + // associate subscription with JO + $jo->setSubscription($sub); + } + // send to invoice generator $invoice = $ic->generateInvoice($icrit); $jo->setInvoice($invoice); - //error_log("GENERATED INVOICE"); - // save here first so we have a JO ID which is required for the hub selector $this->em->persist($invoice); $this->em->persist($jo); @@ -1406,6 +1425,8 @@ class JobOrderController extends ApiController $dest = $jo->getCoordinates(); + $sub = $jo->getSubscription(); + $jo_data = [ 'id' => $jo->getID(), 'date_create' => $jo->getDateCreate()->format('M d, Y'), @@ -1420,6 +1441,7 @@ class JobOrderController extends ApiController 'landmark' => $jo->getLandmark(), 'jo_status' => $status, 'status' => $this->generateAPIRiderStatus($status), + 'subscription' => !empty($sub) ? $sub->getID() : null, ]; // customer vehicle and warranty diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index c39e46c0..e1675975 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -13,8 +13,8 @@ use Doctrine\Common\Collections\Criteria; /** * @ORM\Entity - * @ORM\Table(name="customer_vehicle", indexes={@ORM\Index(columns={"plate_number"}, flags={"fulltext"}), - @ORM\Index(name="plate_number_idx", columns={"plate_number"})}) + * @ORM\Table(name="customer_vehicle", indexes={@ORM\Index(columns={"plate_number"}, flags={"fulltext"}) + * @ORM\Index(name="plate_number_idx", columns={"plate_number"})}) */ class CustomerVehicle { diff --git a/src/InvoiceRule/IsSubscription.php b/src/InvoiceRule/IsSubscription.php new file mode 100644 index 00000000..d7597f8d --- /dev/null +++ b/src/InvoiceRule/IsSubscription.php @@ -0,0 +1,69 @@ +em = $em; + } + + public function getID() + { + return 'discount'; + } + + public function compute($criteria, &$total) + { + $items = []; + + // set the discount to the total selling price + $discount = $total['sell_price']; + $qty = 1; + $price = bcmul(-1, $discount, 2); + + $items[] = [ + 'title' => $this->getTitle(), + 'qty' => $qty, + 'price' => $price, + ]; + + $total['discount'] = $discount; + $total['total_price'] = bcsub($total['total_price'], $discount, 2); + + return $items; + } + + public function validatePromo($criteria, $promo_id) + { + // only applies to battery sales + if ($criteria->getServiceType() != ServiceType::BATTERY_REPLACEMENT_NEW) + return null; + + // only applies if this is a subscription order + if ($criteria->isSubscription() === false) + return null; + + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + + protected function getTitle() + { + $title = 'Waived for subscription'; + + return $title; + } +} + diff --git a/src/Ramcar/InvoiceCriteria.php b/src/Ramcar/InvoiceCriteria.php index 3c1f907f..fd46e3f3 100644 --- a/src/Ramcar/InvoiceCriteria.php +++ b/src/Ramcar/InvoiceCriteria.php @@ -18,6 +18,7 @@ class InvoiceCriteria protected $flag_taxable; protected $source; // use Ramcar's TransactionOrigin protected $price_tier; + protected $is_subscription; // entries are battery and trade-in combos protected $entries; @@ -34,6 +35,7 @@ class InvoiceCriteria $this->flag_taxable = false; $this->source = ''; $this->price_tier = 0; // set to default + $this->is_subscription = false; } public function setServiceType($stype) @@ -202,4 +204,15 @@ class InvoiceCriteria { return $this->price_tier; } + + public function setSubscription($is_subscription) + { + $this->is_subscription = $is_subscription; + return $this; + } + + public function isSubscription() + { + return $this->is_subscription; + } } diff --git a/src/Service/InvoiceManager.php b/src/Service/InvoiceManager.php index da8770cc..be4c5f08 100644 --- a/src/Service/InvoiceManager.php +++ b/src/Service/InvoiceManager.php @@ -55,6 +55,7 @@ class InvoiceManager implements InvoiceGeneratorInterface new InvoiceRule\Fuel($this->em, $this->pt_manager), new InvoiceRule\TireRepair($this->em, $this->pt_manager), new InvoiceRule\DiscountType($this->em), + new InvoiceRule\IsSubscription($this->em), new InvoiceRule\TradeIn($this->em), new InvoiceRule\Tax($this->em, $this->pt_manager), ];