Add computation for customer tags in invoice. #558
This commit is contained in:
parent
c41c824744
commit
f1c45af22e
5 changed files with 143 additions and 40 deletions
|
|
@ -138,9 +138,11 @@ class CustomerTagController extends Controller
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$obj = new CustomerTag();
|
$obj = new CustomerTag();
|
||||||
|
|
||||||
|
$tag_details = $req->request->get('tag_details');
|
||||||
|
$tag_details_json = json_decode($tag_details, true);
|
||||||
$obj->setID($req->request->get('id'))
|
$obj->setID($req->request->get('id'))
|
||||||
->setName($req->request->get('name'))
|
->setName($req->request->get('name'))
|
||||||
->setTagDetails($req->request->get('tag_details'));
|
->setTagDetails($tag_details_json);
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$errors = $validator->validate($obj);
|
$errors = $validator->validate($obj);
|
||||||
|
|
@ -206,9 +208,11 @@ class CustomerTagController extends Controller
|
||||||
if (empty($obj))
|
if (empty($obj))
|
||||||
throw $this->createNotFoundException('The item does not exist');
|
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'))
|
$obj->setID($req->request->get('id'))
|
||||||
->setName($req->request->get('name'))
|
->setName($req->request->get('name'))
|
||||||
->setTagDetails($req->request->get('tag_details'));
|
->setTagDetails($tag_details_json);
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$errors = $validator->validate($obj);
|
$errors = $validator->validate($obj);
|
||||||
|
|
|
||||||
|
|
@ -638,4 +638,9 @@ class Customer
|
||||||
return $str_cust_tags;
|
return $str_cust_tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCustomerTagObjects()
|
||||||
|
{
|
||||||
|
return $this->customer_tags;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,6 @@ class CustomerTag
|
||||||
|
|
||||||
public function getAllTagDetails()
|
public function getAllTagDetails()
|
||||||
{
|
{
|
||||||
return $this->tag_details;
|
return json_encode($this->tag_details);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ use App\Entity\InvoiceItem;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Entity\Battery;
|
use App\Entity\Battery;
|
||||||
use App\Entity\Promo;
|
use App\Entity\Promo;
|
||||||
|
use App\Entity\Customer;
|
||||||
|
|
||||||
use App\Service\InvoiceGeneratorInterface;
|
use App\Service\InvoiceGeneratorInterface;
|
||||||
|
|
||||||
|
|
@ -66,6 +67,13 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
$stype = $criteria->getServiceType();
|
$stype = $criteria->getServiceType();
|
||||||
$cv = $criteria->getCustomerVehicle();
|
$cv = $criteria->getCustomerVehicle();
|
||||||
$has_coolant = $criteria->hasCoolant();
|
$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);
|
// error_log($stype);
|
||||||
switch ($stype)
|
switch ($stype)
|
||||||
{
|
{
|
||||||
|
|
@ -81,7 +89,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
$this->processBatteries($total, $criteria, $invoice);
|
$this->processBatteries($total, $criteria, $invoice);
|
||||||
$this->processTradeIns($total, $criteria, $invoice);
|
$this->processTradeIns($total, $criteria, $invoice);
|
||||||
*/
|
*/
|
||||||
$this->processDiscount($total, $criteria, $invoice);
|
$this->processDiscount($total, $criteria, $invoice, $cust_tag_info);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
|
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
|
||||||
|
|
@ -404,8 +412,11 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice)
|
protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice, $cust_tag_info)
|
||||||
{
|
{
|
||||||
|
if (empty($cust_tag_info))
|
||||||
|
{
|
||||||
|
error_log('empty cust tag');
|
||||||
$promos = $criteria->getPromos();
|
$promos = $criteria->getPromos();
|
||||||
if (count($promos) < 1)
|
if (count($promos) < 1)
|
||||||
return;
|
return;
|
||||||
|
|
@ -444,6 +455,53 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
// process
|
// process
|
||||||
$invoice->setPromo($promo);
|
$invoice->setPromo($promo);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function processJumpstart(&$total, $invoice)
|
protected function processJumpstart(&$total, $invoice)
|
||||||
{
|
{
|
||||||
|
|
@ -663,6 +721,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
$cv = $criteria->getCustomerVehicle();
|
$cv = $criteria->getCustomerVehicle();
|
||||||
$has_coolant = $criteria->hasCoolant();
|
$has_coolant = $criteria->hasCoolant();
|
||||||
// error_log($stype);
|
// error_log($stype);
|
||||||
|
$cust_tag_info = [];
|
||||||
switch ($stype)
|
switch ($stype)
|
||||||
{
|
{
|
||||||
case ServiceType::JUMPSTART_TROUBLESHOOT:
|
case ServiceType::JUMPSTART_TROUBLESHOOT:
|
||||||
|
|
@ -677,7 +736,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
$this->processBatteries($total, $criteria, $invoice);
|
$this->processBatteries($total, $criteria, $invoice);
|
||||||
$this->processTradeIns($total, $criteria, $invoice);
|
$this->processTradeIns($total, $criteria, $invoice);
|
||||||
*/
|
*/
|
||||||
$this->processDiscount($total, $criteria, $invoice);
|
$this->processDiscount($total, $criteria, $invoice, $cust_tag_info);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
|
case ServiceType::BATTERY_REPLACEMENT_WARRANTY:
|
||||||
|
|
@ -719,4 +778,39 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface
|
||||||
|
|
||||||
return $invoice;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@
|
||||||
</label>
|
</label>
|
||||||
<input type="text" name="tag_details" class="form-control m-input" value="{{ obj.getAllTagDetails|default('') }}">
|
<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>
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue