Change computation of discount for CMB. #343
This commit is contained in:
parent
d9c3a4dff8
commit
d439a411a8
6 changed files with 40 additions and 49 deletions
|
|
@ -736,7 +736,7 @@ class JobOrderController extends Controller
|
|||
*/
|
||||
|
||||
// TODO: this snippet should be in the invoice generator
|
||||
$error = $ic->invoicePromo($criteria, $promo_id);
|
||||
$error = $ic->validateDiscount($criteria, $promo_id);
|
||||
|
||||
if (!$error)
|
||||
$error = $ic->invoiceBatteries($criteria, $items);
|
||||
|
|
|
|||
|
|
@ -59,7 +59,8 @@ class Invoice
|
|||
*/
|
||||
protected $items;
|
||||
|
||||
// total discount (amount, not %)
|
||||
// total discount (amount, not %) for resq
|
||||
// for cmb, discount is the percentage
|
||||
/**
|
||||
* @ORM\Column(type="decimal", precision=9, scale=2)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ class InvoiceCriteria
|
|||
protected $promos;
|
||||
protected $cv;
|
||||
protected $flag_coolant;
|
||||
// for discount and other info
|
||||
protected $meta;
|
||||
|
||||
// entries are battery and trade-in combos
|
||||
protected $entries;
|
||||
|
|
@ -23,6 +25,7 @@ class InvoiceCriteria
|
|||
$this->entries = [];
|
||||
$this->cv = null;
|
||||
$this->flag_coolant = false;
|
||||
$this->meta = [];
|
||||
}
|
||||
|
||||
public function setServiceType($stype)
|
||||
|
|
@ -125,4 +128,15 @@ class InvoiceCriteria
|
|||
{
|
||||
return $this->flag_coolant;
|
||||
}
|
||||
|
||||
public function addMeta($id, $value)
|
||||
{
|
||||
$this->meta[$id] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMeta()
|
||||
{
|
||||
return $this->meta;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ use App\Ramcar\FuelType;
|
|||
use App\Entity\Invoice;
|
||||
use App\Entity\InvoiceItem;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\Promo;
|
||||
use App\Entity\User;
|
||||
|
||||
use App\Service\InvoiceGeneratorInterface;
|
||||
|
|
@ -106,10 +105,6 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
// break;
|
||||
}
|
||||
|
||||
// TODO: check if any promo is applied
|
||||
// apply discounts
|
||||
$promos = $criteria->getPromos();
|
||||
|
||||
// get current user
|
||||
$user = $this->security->getUser();
|
||||
if ($user != null)
|
||||
|
|
@ -140,7 +135,8 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
$criteria->setServiceType($jo->getServiceType())
|
||||
->setCustomerVehicle($jo->getCustomerVehicle());
|
||||
|
||||
$ierror = $this->invoicePromo($criteria, $promo_id);
|
||||
$discount = $promo_id;
|
||||
$ierror = $this->validateDiscount($criteria, $discount);
|
||||
|
||||
if (!$ierror && !empty($invoice_items))
|
||||
{
|
||||
|
|
@ -224,8 +220,9 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
return 0;
|
||||
}
|
||||
|
||||
public function invoicePromo(InvoiceCriteria $criteria, $promo_id)
|
||||
public function validateDiscount(InvoiceCriteria $criteria, $discount)
|
||||
{
|
||||
|
||||
// return error if there's a problem, false otherwise
|
||||
// check service type
|
||||
$stype = $criteria->getServiceType();
|
||||
|
|
@ -233,18 +230,17 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
return null;
|
||||
|
||||
|
||||
if (empty($promo_id))
|
||||
// check if discount is blank or 0
|
||||
if ((empty($discount)) || ($discount == 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if this is a valid promo
|
||||
$promo = $this->em->getRepository(Promo::class)->find($promo_id);
|
||||
// check if discount is greater than 50
|
||||
if ($discount > 50)
|
||||
return 'Invalid discount specified';
|
||||
|
||||
if (empty($promo))
|
||||
return 'Invalid promo specified.';
|
||||
|
||||
$criteria->addPromo($promo);
|
||||
$criteria->addMeta('discount', $discount);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -389,33 +385,23 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
|
||||
protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice)
|
||||
{
|
||||
$promos = $criteria->getPromos();
|
||||
if (count($promos) < 1)
|
||||
$rate = 0;
|
||||
$meta = $criteria->getMeta();
|
||||
if (isset($meta['discount']))
|
||||
$rate = $meta['discount'];
|
||||
else
|
||||
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)
|
||||
{
|
||||
case DiscountApply::SRP:
|
||||
// compute discount
|
||||
$rate = $rate * 0.01;
|
||||
$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')
|
||||
->setTitle('Discount')
|
||||
->setQuantity(1)
|
||||
->setPrice(-1 * $discount);
|
||||
$invoice->addItem($item);
|
||||
|
|
@ -425,7 +411,7 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface
|
|||
$total['total_price'] -= $discount;
|
||||
|
||||
// process
|
||||
$invoice->setPromo($promo);
|
||||
$invoice->setDiscount($discount);
|
||||
}
|
||||
|
||||
protected function processJumpstart(&$total, $invoice)
|
||||
|
|
|
|||
|
|
@ -2670,7 +2670,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
|
|||
$params['warranty_classes'] = CMBWarrantyClass::getCollection();
|
||||
$params['modes_of_payment'] = CMBModeOfPayment::getCollection();
|
||||
$params['statuses'] = JOStatus::getCollection();
|
||||
$params['discount_apply'] = DiscountApply::getCollection();
|
||||
$params['trade_in_types'] = CMBTradeInType::getCollection();
|
||||
$params['facilitated_types'] = FacilitatedType::getCollection();
|
||||
$params['facilitated_hubs'] = $fac_hubs;
|
||||
|
|
|
|||
|
|
@ -500,22 +500,13 @@
|
|||
<div class="col-lg-6">
|
||||
<label>Discount Type</label>
|
||||
{% if ftags.invoice_edit %}
|
||||
<select class="form-control m-input" id="invoice-promo" name="invoice_promo">
|
||||
<option value="">None</option>
|
||||
{% for promo in promos %}
|
||||
<option value="{{ promo.getID() }}">{{ promo.getName() ~ ' (' ~ promo.getDiscountRate * 100 ~ '% applied to ' ~ discount_apply[promo.getDiscountApply] ~ ')' }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="number" id="invoice-promo" name="invoice_promo" class="form-control m-input min = "0" max="50" value="{{ obj.getInvoice ? obj.getInvoice.getDiscount }}">
|
||||
<div class="form-control-feedback hide" data-field="invoice_promo"></div>
|
||||
{% else %}
|
||||
<input type="text" id="invoice-promo" class="form-control m-input" value="{{ obj.getInvoice.getPromo.getName|default('None') }}" disabled>
|
||||
<input type="number" id="invoice-promo" name="invoice_promo" class="form-control m-input min="0" max="50" value="{{ obj.getInvoice ? obj.getInvoice.getDiscount }}" disabled>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="col-lg-3">
|
||||
<label>Promo Discount</label>
|
||||
<input type="text" id="invoice-promo-discount" class="form-control m-input text-right" value="{{ obj.getInvoice ? obj.getInvoice.getDiscount|number_format(2) : '0.00' }}" disabled>
|
||||
</div>
|
||||
<div class="col-lg-3">
|
||||
<div class="col-lg-6">
|
||||
<label>Trade In</label>
|
||||
<input type="text" id="invoice-trade-in" class="form-control m-input text-right" value="{{ obj.getInvoice ? obj.getInvoice.getTradeIn|number_format(2) : '0.00' }}" disabled>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue