Add computation for customer tags in invoice. #558

This commit is contained in:
Korina Cordero 2021-05-06 11:09:49 +00:00
parent c41c824744
commit f1c45af22e
5 changed files with 143 additions and 40 deletions

View file

@ -138,9 +138,11 @@ class CustomerTagController extends Controller
$em = $this->getDoctrine()->getManager();
$obj = new CustomerTag();
$tag_details = $req->request->get('tag_details');
$tag_details_json = json_decode($tag_details, true);
$obj->setID($req->request->get('id'))
->setName($req->request->get('name'))
->setTagDetails($req->request->get('tag_details'));
->setTagDetails($tag_details_json);
// validate
$errors = $validator->validate($obj);
@ -206,9 +208,11 @@ class CustomerTagController extends Controller
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
$tag_details = $req->request->get('tag_details');
$tag_details_json = json_decode($tag_details, true);
$obj->setID($req->request->get('id'))
->setName($req->request->get('name'))
->setTagDetails($req->request->get('tag_details'));
->setTagDetails($tag_details_json);
// validate
$errors = $validator->validate($obj);

View file

@ -638,4 +638,9 @@ class Customer
return $str_cust_tags;
}
public function getCustomerTagObjects()
{
return $this->customer_tags;
}
}

View file

@ -114,6 +114,6 @@ class CustomerTag
public function getAllTagDetails()
{
return $this->tag_details;
return json_encode($this->tag_details);
}
}

View file

@ -19,6 +19,7 @@ use App\Entity\InvoiceItem;
use App\Entity\User;
use App\Entity\Battery;
use App\Entity\Promo;
use App\Entity\Customer;
use App\Service\InvoiceGeneratorInterface;
@ -66,6 +67,13 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$stype = $criteria->getServiceType();
$cv = $criteria->getCustomerVehicle();
$has_coolant = $criteria->hasCoolant();
$cust_tag_info = [];
if ($stype == ServiceType::BATTERY_REPLACEMENT_NEW)
{
error_log('calling getCustomerTagInfo');
$cust_tag_info = $this->getCustomerTagInfo($cv);
}
// error_log($stype);
switch ($stype)
{
@ -81,7 +89,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$this->processBatteries($total, $criteria, $invoice);
$this->processTradeIns($total, $criteria, $invoice);
*/
$this->processDiscount($total, $criteria, $invoice);
$this->processDiscount($total, $criteria, $invoice, $cust_tag_info);
break;
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
@ -404,45 +412,95 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
}
}
protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice)
protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice, $cust_tag_info)
{
$promos = $criteria->getPromos();
if (count($promos) < 1)
return;
// NOTE: only get first promo because only one is applicable anyway
$promo = $promos[0];
$rate = $promo->getDiscountRate();
$apply_to = $promo->getDiscountApply();
switch ($apply_to)
if (empty($cust_tag_info))
{
case DiscountApply::SRP:
$discount = round($total['sell_price'] * $rate, 2);
break;
case DiscountApply::OPL:
// $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2);
$discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2);
break;
}
error_log('empty cust tag');
$promos = $criteria->getPromos();
if (count($promos) < 1)
return;
// if discount is higher than 0, display in invoice
if ($discount > 0)
// NOTE: only get first promo because only one is applicable anyway
$promo = $promos[0];
$rate = $promo->getDiscountRate();
$apply_to = $promo->getDiscountApply();
switch ($apply_to)
{
case DiscountApply::SRP:
$discount = round($total['sell_price'] * $rate, 2);
break;
case DiscountApply::OPL:
// $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2);
$discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2);
break;
}
// if discount is higher than 0, display in invoice
if ($discount > 0)
{
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle('Promo discount')
->setQuantity(1)
->setPrice(-1 * $discount);
$invoice->addItem($item);
}
$total['discount'] = $discount;
$total['total_price'] -= $discount;
// process
$invoice->setPromo($promo);
}
else
{
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle('Promo discount')
->setQuantity(1)
->setPrice(-1 * $discount);
$invoice->addItem($item);
// since only one promo can only be used, we prioritize the tag promos
// TODO: need to test this for multiple tags
$total_discount_amount = 0;
$total_amount = $total['total_price'];
foreach ($cust_tag_info as $ct_info)
{
// check discount type
$discount_type = '';
$discount_value = 0;
$discount_amount = 0;
$discounted_total = 0;
if (isset($ct_info['discount_type']))
$discount_type = $ct_info['discount_type'];
if (isset($ct_info['discount_value']))
{
$discount_value = $ct_info['discount_value'];
}
if ($discount_type == 'percent')
{
$discount = round(($discount_value / 100), 2);
$discount_amount = $total_amount * $discount;
$discounted_total = $total_amount - $discount_amount;
}
else
{
// assume fixed amount for this
$discount = $discount_value;
$discounted_total = $total_amount - $discount;
}
$total_discount_amount += $discounted_total;
$item = new InvoiceItem();
$item->setInvoice($invoice)
->setTitle($ct_info['invoice_display'])
->setQuantity(1)
->setPrice(-1 * $total_discount_amount);
$invoice->addItem($item);
}
$total['discount'] = $total_discount_amount;
$total['total_price'] -= $total_discount_amount;
}
$total['discount'] = $discount;
$total['total_price'] -= $discount;
// process
$invoice->setPromo($promo);
}
protected function processJumpstart(&$total, $invoice)
@ -663,6 +721,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$cv = $criteria->getCustomerVehicle();
$has_coolant = $criteria->hasCoolant();
// error_log($stype);
$cust_tag_info = [];
switch ($stype)
{
case ServiceType::JUMPSTART_TROUBLESHOOT:
@ -677,7 +736,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
$this->processBatteries($total, $criteria, $invoice);
$this->processTradeIns($total, $criteria, $invoice);
*/
$this->processDiscount($total, $criteria, $invoice);
$this->processDiscount($total, $criteria, $invoice, $cust_tag_info);
break;
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
@ -719,4 +778,39 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
return $invoice;
}
protected function getCustomerTagInfo($cv)
{
$cust_tag_info = [];
// get customer and customer tags using customer vehicle
$customer = $cv->getCustomer();
$customer_tags = $customer->getCustomerTagObjects();
if (!empty($customer_tags))
{
foreach ($customer_tags as $customer_tag)
{
// check tag details
$cust_tag_type = $customer_tag->getTagDetails('type');
error_log($cust_tag_type);
if (($cust_tag_type != null) && ($cust_tag_type == 'one-time-discount'))
{
// get the discount type and discount amount
$cust_tag_discount_amount = $customer_tag->getTagDetails('discount_amount');
$cust_tag_invoice_display = $customer_tag->getTagDetails('invoice_display');
error_log($cust_tag_invoice_display);
$cust_tag_info[] = [
'type' => $cust_tag_type,
'discount_type' => $customer_tag->getTagDetails('discount_type'),
'discount_amount' => $customer_tag->getTagDetails('discount_amount'),
'discount_value' => $customer_tag->getTagDetails('discount_value'),
'invoice_display' => $customer_tag->getTagDetails('invoice_display'),
];
}
}
}
return $cust_tag_info;
}
}

View file

@ -61,7 +61,7 @@
</label>
<input type="text" name="tag_details" class="form-control m-input" value="{{ obj.getAllTagDetails|default('') }}">
<div class="form-control-feedback hide" data-field="tag_details"></div>
<span class="m-form__help">JSON format e.g. {type: 'discount', discount_type: 'percent', ...}</span>
<span class="m-form__help">JSON format e.g. {"type":"discount", "discount_type":"percent", ...}</span>
</div>
</div>
</div>