public function saveSale($in_sale_id, $items, $payments, $payment_received, $customer_id, $employee_id, $sub_total, $total, $comment, $status = self::sale_complete_status, $discount_amount, $discount_symbol, $gst_amount, $sale_rep_id) { if (count($items) == 0) { return '-1'; } $message = ""; //Check if invoice already exists $model = Sale::model()->findSale($in_sale_id); $payment_types = ''; foreach ($payments as $payment_id => $payment) { $payment_types = $payment_types . $payment['payment_type'] . ': ' . $payment['payment_amount'] . '<br />'; } $transaction = Yii::app()->db->beginTransaction(); try { // Transaction Date for Inventory, Payment and sale trans date $trans_date = date('Y-m-d H:i:s'); // Getting Customer Account Info $account = Account::model()->getAccountInfo($customer_id); $old_total = $this->getOldSaleTotal($in_sale_id); //Rolling back the Sale Total of old / previous Sale & Saving Change / Edit Sale into [account receivable] table if ($account && $in_sale_id) { AccountReceivable::model()->saveAccountRecv($account->id, $employee_id, $in_sale_id, -$old_total, $trans_date, 'Edit Sale', 'CHASALE', 'R'); Account::model()->withdrawAccountBal($account, $old_total); } //Saving existing Sale Item to Inventory table and removing it out $this->updateSale($in_sale_id, $employee_id, $trans_date); $model->client_id = $customer_id; $model->employee_id = $employee_id; $model->payment_type = $payment_types; $model->remark = $comment; $model->sub_total = $sub_total; $model->status = $status; $model->discount_amount = $discount_amount === null ? 0 : $discount_amount; $model->discount_type = $discount_symbol === null ? '%' : $discount_symbol; $model->vat = $gst_amount == null ? 0 : $gst_amount; $model->salerep_id = $sale_rep_id; if ($model->save()) { $sale_id = $model->id; $date_paid = $trans_date; $trans_code = 'CHSALE'; $trans_status = $total > 0 ? 'N' : 'R'; // If Return Sale Transaction Type = 'CHSALE' same but Transaction Status = 'R' reverse // Saving Sale Item (Sale & Sale Item gotta save firstly even for Suspended Sale) $this->saveSaleItem($items, $sale_id, $employee_id); // We only save Sale Payment, Account Receivable transaction and update Account (outstanding balance) of completed sale transaction if ($status == self::sale_complete_status) { //$account = Account::model()->getAccountInfo($customer_id); if ($account) { // Add hot bill before proceed payment Account::model()->depositAccountBal($account, $total); SalePayment::model()->batchPayment($customer_id, $employee_id, $account, $payment_received, $date_paid, $comment); //Saving Account Receivable for Sale transaction code = 'CHSALE' AccountReceivable::model()->saveAccountRecv($account->id, $employee_id, $sale_id, $total, $trans_date, $comment, $trans_code, $trans_status); } else { // If no customer selected only Sale Payment History PaymentHistory::model()->savePaymentHistory($customer_id, $payment_received, $date_paid, $employee_id, $comment); } } $message = $sale_id; $transaction->commit(); } } catch (Exception $e) { $transaction->rollback(); $message = '-1' . $e->getMessage(); } return $message; }
public function batchPayment($client_id, $employee_id, $account, $total_paid, $paid_date, $note) { $sql = "SELECT sale_id,sale_time,client_name,amount_to_paid\n FROM (\n SELECT s.sale_id,sale_time,client_name,(s.`sub_total`-IFNULL(sp.payment_amount,0)) amount_to_paid\n FROM \n (SELECT s.id sale_id,s.`sale_time`,CONCAT(c.first_name,' ',last_name) client_name,s.`sub_total`\n FROM sale s, `client` c\n WHERE s.`client_id` = c.id\n AND c.id=:client_id) s LEFT JOIN v_sale_payment sp ON sp.sale_id=s.sale_id\n ) AS t\n WHERE amount_to_paid>0\n ORDER BY sale_time"; $result = Yii::app()->db->createCommand($sql)->queryAll(true, array(':client_id' => $client_id)); $paid_amount = $total_paid; $transaction = Yii::app()->db->beginTransaction(); try { $payment_id = PaymentHistory::model()->savePaymentHistory($client_id, $total_paid, $paid_date, $employee_id, $note); foreach ($result as $record) { if ($paid_amount <= $record["amount_to_paid"]) { $payment_amount = $paid_amount; $this->saveSalePayment($record["sale_id"], $payment_id, $payment_amount, $paid_date, $note); $this->saveAccuntRecv($account->id, $employee_id, $record["sale_id"], $payment_amount, $paid_date, $note); break; } else { $paid_amount = $paid_amount - $record["amount_to_paid"]; $payment_amount = $record["amount_to_paid"]; $this->saveSalePayment($record["sale_id"], $payment_id, $payment_amount, $paid_date, $note); $this->saveAccuntRecv($account->id, $employee_id, $record["sale_id"], $payment_amount, $paid_date, $note); } } Account::model()->updateAccountBal($account, $total_paid); $transaction->commit(); $message = $payment_id; } catch (Exception $e) { $transaction->rollback(); //return $e->getMessage(); $message = '-1' . $e->getMessage(); } return $message; }
public function batchPayment($client_id, $employee_id, $account, $total_paid, $paid_date, $note) { $sql = "SELECT s.id sale_id,s.`sale_time`,CONCAT(c.first_name,' ',c.last_name) client_name\n ,(s.total - IFNULL(sp.payment_amount,0)) amount_to_paid\n FROM v_sale s JOIN `client` c ON s.`client_id` = c.id AND c.id=:client_id\n LEFT JOIN v_sale_payment sp ON sp.sale_id=s.id\n WHERE s.status=:status\n AND (s.total - IFNULL(sp.payment_amount,0))>0\n ORDER BY sale_time"; $result = Yii::app()->db->createCommand($sql)->queryAll(true, array(':client_id' => $client_id, ':status' => $this->_sale_completed)); $paid_amount = $total_paid; $trans_code = 'PAY'; $trans_status = $total_paid > 0 ? 'N' : 'R'; // If [Payment Amount] > 0 then Paid else Return to Customer $payment_id = PaymentHistory::model()->savePaymentHistory($client_id, $total_paid, $paid_date, $employee_id, $note); if ($payment_id != 0) { foreach ($result as $record) { if ($paid_amount <= $record["amount_to_paid"]) { $payment_amount = $paid_amount; $this->saveSalePayment($record["sale_id"], $payment_id, $payment_amount, $paid_date, $note); AccountReceivable::model()->saveAccountRecv($account->id, $employee_id, $record["sale_id"], -$payment_amount, $paid_date, $note, $trans_code, $trans_status); break; } else { $paid_amount = $paid_amount - $record["amount_to_paid"]; $payment_amount = $record["amount_to_paid"]; $this->saveSalePayment($record["sale_id"], $payment_id, $payment_amount, $paid_date, $note); AccountReceivable::model()->saveAccountRecv($account->id, $employee_id, $record["sale_id"], -$payment_amount, $paid_date, $note, $trans_code, $trans_status); } } Account::model()->withdrawAccountBal($account, $total_paid); } $message = $payment_id; return $message; }