public function saveFromBasket(Basket $basket) { $transaction = $this->getDb()->beginTransaction(); try { $this->due_amount = $basket->getTotalDue(false); if (!$this->save()) { throw new \RuntimeException('Could not save order model'); } foreach ($basket->getItems(Basket::ITEM_PRODUCT) as $item) { $model = new OrderLine(['order_id' => $this->id, 'product_id' => $item->id, 'quantity' => $item->basketQuantity, 'due_amount' => $item->totalPrice]); if (!$model->save()) { throw new \RuntimeException('Could not save order line model'); } } $transaction->commit(); } catch (\Exception $exception) { $transaction->rollback(); throw $exception; } }
public static function Create($orderId, $itemId, $itemName, $quantity, $vatrate, $unitPrice, $unitCost, $discount) { //check whether available and make necessary inventory deductions, then $vat = intval($vatrate) / (intval($vatrate) + 100) * (intval($quantity) * floatval($unitPrice)); $discount = floatval($discount); $lineItem = new OrderLine($orderId, $itemId, $itemName, $quantity, $vat, $unitPrice, $unitCost, $discount); try { $sql = 'SELECT * FROM stock_accounts WHERE resource_id = ' . $itemId; $res = DatabaseHandler::GetRow($sql); if ($res['stock_bal'] >= intval($quantity)) { $lineItem->setAvailabile(); $sql = 'UPDATE stock_accounts SET stock_bal = ' . (intval($res['stock_bal']) - intval($quantity)) . ' WHERE resource_id = ' . $itemId; DatabaseHandler::Execute($sql); } else { } $lineItem->save(); return $lineItem; } catch (Exception $e) { } }