protected function saveAccuntRecv($account_id, $employee_id, $sale_id, $amount, $trans_date, $note)
 {
     $account_recv = new AccountReceivable();
     $account_recv->account_id = $account_id;
     $account_recv->employee_id = $employee_id;
     $account_recv->trans_id = $sale_id;
     $account_recv->trans_amount = -$amount;
     $account_recv->trans_code = 'PAY';
     $account_recv->trans_datetime = $trans_date;
     $account_recv->trans_status = 'N';
     $account_recv->note = $note;
     $account_recv->save();
 }
 public function saveAccountRecv($account_id, $employee_id, $sale_id, $amount, $trans_date, $note, $trans_code = 'CHSALE', $trans_status = 'N')
 {
     $model = new AccountReceivable();
     $model->account_id = $account_id;
     $model->employee_id = $employee_id;
     $model->trans_id = $sale_id;
     $model->trans_amount = $amount;
     $model->trans_code = $trans_code;
     $model->trans_datetime = $trans_date;
     $model->trans_status = $trans_status;
     $model->note = $note;
     if ($model->validate()) {
         $model->save();
     } else {
         var_dump($model->getErrors());
     }
 }
Exemple #3
0
 public function deleteSale($sale_id, $remark, $customer_id, $employee_id, $status = self::sale_cancel_status)
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         $trans_date = date('Y-m-d H:i:s');
         $trans_comment = 'Cancel Sale';
         $trans_code = 'CHSALE';
         $trans_status = 'R';
         $this->updateItemInventory($sale_id, $trans_date, $trans_comment, $employee_id);
         $sale = $this->findByPk($sale_id);
         //$sale_amount = ($sale->sub_total - ($sale->sub_total*$sale->discount_amount)/100);
         //$customer_id = $sale->client_id;
         $total = $this->getOldSaleTotal($sale_id);
         $sale->status = $status;
         $sale->remark = $remark;
         $sale->save();
         // Updating current_balance in account table
         //$account = $this->updateAccount($customer_id, $sale_amount);
         $account = Account::model()->getAccountInfo($customer_id);
         if ($account) {
             // Rollback Account = Outstanding Balance + Total Sub
             Account::model()->withdrawAccountBal($account, $total);
             // Saving to Account Receivable where trans_cod='R' reverse (Payment, Sale Transaction ..)
             AccountReceivable::model()->saveAccountRecv($account->id, $employee_id, $sale_id, $total, $trans_date, $trans_comment, $trans_code, $trans_status);
             //$this->saveAR($account->id, $employee_id, $sale_id, $sale_amount, 0, $trans_date,'CHSALE','R');
         }
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollback();
         return -1;
     }
     //2nd Way using store procedure in mysql database
     //$sql = "CALL pro_cancelsale(:sale_id,:remark)";
     //return Yii::app()->db->createCommand($sql)->queryAll(true, array(':sale_id' => $sale_id, ':remark' => $remark));
 }
Exemple #4
0
 protected function saveAR($account_id, $employee_id, $sale_id, $sale_amount, $actual_paid, $trans_date, $trans_code = 'CHSALE', $trans_status = 'N')
 {
     // Save payment transaction
     if ($actual_paid > 0) {
         $account_receivable = new AccountReceivable();
         $account_receivable->account_id = $account_id;
         $account_receivable->employee_id = $employee_id;
         $account_receivable->trans_id = $sale_id;
         $account_receivable->trans_amount = -$actual_paid;
         $account_receivable->trans_code = 'PAY';
         $account_receivable->trans_datetime = $trans_date;
         $account_receivable->trans_status = $trans_status;
         $account_receivable->save();
     }
     //Saving Sale Transaction
     $ar_sale = new AccountReceivable();
     $ar_sale->account_id = $account_id;
     $ar_sale->employee_id = $employee_id;
     $ar_sale->trans_id = $sale_id;
     $ar_sale->trans_amount = $sale_amount;
     $ar_sale->trans_code = $trans_code;
     $ar_sale->trans_datetime = $trans_date;
     $ar_sale->trans_status = $trans_status;
     $ar_sale->save();
 }
Exemple #5
0
 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;
 }