Add battery size id to invoice item. Add saving of trade in items with battery size to invoice item. #803

This commit is contained in:
Korina Cordero 2024-06-20 03:29:24 -04:00
parent cec648f894
commit 4b5ad97225
6 changed files with 151 additions and 27 deletions

View file

@ -50,6 +50,13 @@ class InvoiceItem
*/
protected $battery;
// battery size for trade in items
/**
* @ORM\ManyToOne(targetEntity="BatterySize")
* @ORM\JoinColumn(name="battery_size_id", referencedColumnName="id")
*/
protected $battery_size;
public function __construct()
{
$this->title = '';
@ -115,4 +122,15 @@ class InvoiceItem
{
return $this->battery;
}
public function setBatterySize(BatterySize $battery_size)
{
$this->battery_size = $battery_size;
return $this;
}
public function getBatterySize()
{
return $this->battery_size;
}
}

View file

@ -117,12 +117,17 @@ class BatterySales implements InvoiceRuleInterface
continue;
// if this is a trade in, add trade in
/*
if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in']))
$trade_in = $item['trade_in'];
else
$trade_in = null;
$trade_in = null; */
$criteria->addEntry($battery, $trade_in, $qty);
if (empty($item['trade_in']))
{
$trade_in = null;
$criteria->addEntry($battery, $trade_in, $qty);
}
}
}

View file

@ -2,12 +2,24 @@
namespace App\InvoiceRule;
use Doctrine\ORM\EntityManagerInterface;
use App\InvoiceRuleInterface;
use App\Ramcar\TradeInType;
use App\Ramcar\ServiceType;
use App\Entity\Battery;
class TradeIn implements InvoiceRuleInterface
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getID()
{
return 'trade-in';
@ -32,12 +44,12 @@ class TradeIn implements InvoiceRuleInterface
// at this point, entry is a trade in
// need to check if battery (coming from CRM) is set
// or battery_size is set (coming from rider app)
if (isset($entry['battery']))
{
$battery = $entry['battery'];
$batt_size = $battery->getSize();
}
else
//if (isset($entry['battery']))
//{
// $battery = $entry['battery'];
// $batt_size = $battery->getSize();
//}
//else
$batt_size = $entry['battery_size'];
$ti_rate = $this->getTradeInRate($batt_size, $trade_in_type);
@ -50,6 +62,7 @@ class TradeIn implements InvoiceRuleInterface
$price = bcmul($ti_rate, -1, 2);
$items[] = [
'battery_size' => $batt_size,
'qty' => $qty,
'title' => $this->getTitle($batt_size, $trade_in_type),
'price' => $price,
@ -67,6 +80,41 @@ class TradeIn implements InvoiceRuleInterface
public function validateInvoiceItems($criteria, $invoice_items)
{
// check service type. Only battery sales and battery warranty should have invoice items. Since this is the battery sales
// rule, we only check for battery sales.
$stype = $criteria->getServiceType();
if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW)
return null;
// return error if there's a problem, false otherwise
if (!empty($invoice_items))
{
// check if this is a valid battery
foreach ($invoice_items as $item)
{
$battery = $this->em->getRepository(Battery::class)->find($item['battery']);
if (empty($battery))
{
$error = 'Invalid battery specified.';
return $error;
}
// quantity
$qty = $item['quantity'];
if ($qty < 1)
continue;
// if this is a trade in, add trade in
if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in']))
{
$trade_in = $item['trade_in'];
$battery_size = $battery->getSize();
$criteria->addTradeInEntry($battery_size, $trade_in, $qty);
}
}
}
return null;
}

View file

@ -55,7 +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\TradeIn(),
new InvoiceRule\TradeIn($this->em),
new InvoiceRule\Tax($this->em, $this->pt_manager),
];
}
@ -166,6 +166,7 @@ class InvoiceManager implements InvoiceGeneratorInterface
// (3) generateInvoiceCriteria
// (4) RiderAPIHandler's changeService
// (5) TAPI's JobOrderController
// (6) CAPI's RiderAppController
public function generateInvoice($criteria)
{
// no need to validate since generateDraftInvoice was called before this was called
@ -214,17 +215,22 @@ class InvoiceManager implements InvoiceGeneratorInterface
$price = $item['price'];
$battery = null;
$battery_size = null;
if (isset($item['battery']))
$battery = $item['battery'];
if (isset($item['promo']))
$promo = $item['promo'];
if (isset($item['battery_size']))
$battery_size = $item['battery_size'];
$invoice_items[] = [
'title' => $title,
'quantity' => $quantity,
'price' => $price,
'battery' => $battery,
'battery_size' => $battery_size,
];
}
}
@ -278,6 +284,9 @@ class InvoiceManager implements InvoiceGeneratorInterface
if ($item['battery'] != null)
$invoice_item->setBattery($item['battery']);
if ($item['battery_size'] != null)
$invoice_item->setBatterySize($item['battery_size']);
$invoice->addItem($invoice_item);
}

View file

@ -2312,6 +2312,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
// get the job order's invoice items
$invoice = $jo->getInvoice();
$invoice_items = [];
if ($invoice != null)
$invoice_items = $invoice->getItems();
$params['invoice_items'] = $invoice_items;
$params['obj'] = $jo;
$params['mode'] = 'open_edit';
$params['cvid'] = $jo->getCustomerVehicle()->getID();

View file

@ -630,20 +630,24 @@
</div>
</div>
<div class="form-group m-form__group row">
<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>
<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>
{% endif %}
</div>
<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 %}
{% if obj.getInvoice and obj.getInvoice.getPromo %}
<option value="{{ promo.getID() }}" {{ obj.getInvoice.getPromo.getID == promo.getID ? ' selected'}}>{{ promo.getName() ~ ' (' ~ promo.getDiscountRate * 100 ~ '% applied to ' ~ discount_apply[promo.getDiscountApply] ~ ')' }}</option>
{% else %}
<option value="{{ promo.getID() }}">{{ promo.getName() ~ ' (' ~ promo.getDiscountRate * 100 ~ '% applied to ' ~ discount_apply[promo.getDiscountApply] ~ ')' }}</option>
{% endif %}
{% endfor %}
</select>
<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>
{% 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>
@ -1194,6 +1198,8 @@
<script src="/assets/vendors/custom/gmaps/gmaps.js" type="text/javascript"></script>
<script>
var invoiceItems = [];
// location search autocomplete
var input = document.getElementById('m_gmap_address');
@ -1218,6 +1224,7 @@ autocomplete.addListener('place_changed', function() {
$(function() {
var form_in_process = false;
var invoiceItems = [];
// openstreet maps stuff
// TODO: move this to a service
@ -1228,8 +1235,30 @@ $(function() {
var markerLayerGroup = L.layerGroup().addTo(osm_map);
function populateInvoiceItems()
{
console.log('populateInvoiceItems start');
{% if invoice_items is defined %}
{% for item in invoice_items %}
{% if item.getBattery is not null %}
var battery_id = {{ item.getBattery.getID }};
var qty = {{ item.getQuantity }};
console.log(battery_id);
{% else %}
// check if it's a trade in entry
var title = '{{ item.getTitle }}';
if (title.includes('Trade-in')) {
console.log(title + ' a trade in');
}
{% endif %}
{% endfor %}
{% endif %}
}
function selectPoint(lat, lng)
{
console.log('selectPoint start');
// check if point is in coverage area
// commenting out the geofence call for CRM
/*
@ -1266,12 +1295,13 @@ $(function() {
$('#map_lat').val(lat);
$('#map_lng').val(lng);
// regenerate invoice
generateInvoice();
// regenerate invoice
// generateInvoice();
}
osm_map.on('click', function(e) {
console.log('point clicked');
selectPoint(e.latlng.lat, e.latlng.lng);
});
@ -1285,6 +1315,7 @@ $(function() {
console.log(results);
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
console.log('geocode point clicked');
selectPoint(lat, lng);
}
},
@ -1312,6 +1343,14 @@ $(function() {
// OSM code
var lat = {{ obj.getCoordinates.getLatitude }};
var lng = {{ obj.getCoordinates.getLongitude }};
console.log('set coordinate point clicked');
var promo = $("#invoice-promo").val();
console.log(promo);
populateInvoiceItems();
selectPoint(lat, lng);
// remove placeholder text
@ -1695,8 +1734,6 @@ $(function() {
placeholder: ""
});
var invoiceItems = [];
{% include 'invoice/trade_in.js.twig' %}
// add to invoice