em = $em; } public function getID() { return 'battery_warranty'; } public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); $items = []; if ($stype == $this->getID()) { // get the entries $entries = $criteria->getEntries(); foreach($entries as $entry) { $batt = $entry['battery']; $qty = 1; $price = $this->getServiceTypeFee(); $items[] = [ 'service_type' => $this->getID(), 'battery' => $batt, 'qty' => $qty, 'title' => $this->getTitle($batt), 'price' => $price, ]; $qty_price = bcmul($price, $qty, 2); $total['total_price'] = bcadd($total['total_price'], $qty_price, 2); } } return $items; } public function getServiceTypeFee() { // TODO: we need to to put this somewhere like in .env // so that if any chanages are to be made, we just edit the file // instead of the code return 0; } public function validatePromo($criteria, $promo_id) { return false; } public function validateInvoiceItems($criteria, $invoice_items) { // check service type. Only battery sales and battery warranty should have invoice items $stype = $criteria->getServiceType(); if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY) 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']; else $trade_in = null; $criteria->addEntry($battery, $trade_in, $qty); } } return null; } protected function getTitle($battery) { $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'; return $title; } }