public function saveSale($items, $payments, $customer_id, $employee_id, $sub_total, $comment) { if (count($items) == 0) { return -1; } $model = new SaleSuspended(); $payment_types = ''; //$error=''; foreach ($payments as $payment_id => $payment) { $payment_types = $payment_types . $payment['payment_type'] . ': ' . $payment['payment_amount'] . '<br />'; } $transaction = $model->dbConnection->beginTransaction(); try { $model->sale_time = date('Y-m-d H:i:s'); $model->client_id = $customer_id; $model->employee_id = $employee_id; $model->payment_type = $payment_types; $model->remark = $comment; $model->sub_total = $sub_total; if ($model->save()) { $sale_id = $model->id; // Saving payment items to sale_payment table foreach ($payments as $payment) { $sale_payment = new SaleSuspendedPayment(); $sale_payment->sale_id = $sale_id; $sale_payment->payment_type = $payment['payment_type']; $sale_payment->payment_amount = $payment['payment_amount']; $sale_payment->save(); } // Saving sale item to sale_item table foreach ($items as $line => $item) { $cur_item_info = Item::model()->findbyPk($item['item_id']); $sale_item = new SaleSuspendedItem(); if (substr($item['discount'], 0, 1) == '$') { $discount_amount = substr($item['discount'], 1); $discount_type = '$'; } else { $discount_amount = $item['discount']; $discount_type = '%'; } $sale_item->sale_id = $sale_id; $sale_item->item_id = $item['item_id']; $sale_item->line = $line; $sale_item->quantity = $item['quantity']; $sale_item->cost_price = $cur_item_info->cost_price; $sale_item->unit_price = $cur_item_info->unit_price; $sale_item->price = $item['price']; // The exact selling price $sale_item->discount_amount = $discount_amount == null ? 0 : $discount_amount; $sale_item->discount_type = $discount_type; if (!$sale_item->save()) { $transaction->rollback(); return -1; } } $transaction->commit(); } } catch (Exception $e) { $transaction->rollback(); return -1; //return $e; } return $sale_id; }
public function copyEntireSuspendSale($sale_id) { $this->clearAll(); $sale = SaleSuspended::model()->findbyPk($sale_id); $sale_item = SaleSuspendedItem::model()->getSaleItem($sale_id); $payments = SaleSuspendedPayment::model()->getPayment($sale_id); foreach ($sale_item as $row) { if ($row->discount_type == '$') { $discount_amount = $row->discount_type . $row->discount_amount; } else { $discount_amount = $row->discount_amount; } $this->addItem($row->item_id, $row->quantity, $discount_amount, $row->price, $row->description); } foreach ($payments as $row) { $this->addPayment($row->payment_type, $row->payment_amount); } $this->setCustomer($sale->client_id); $this->setComment($sale->remark); }
public function getPayment($sale_id) { $model = SaleSuspendedPayment::model()->findAll('sale_id=:saleId', array(':saleId' => (int) $sale_id)); return $model; }