public function batchPayment($client_id, $paid_amount, $paid_date, $note)
 {
     $sql = "SELECT sale_id,(amount-paid) amount_to_paid\n                        FROM (\n                        SELECT s.id sale_id,s.`sale_time`,CONCAT_WS(' ',first_name,last_name) client_id,IFNULL(s.`sub_total`,0) amount,IFNULL(sa.`paid`,0) paid,IFNULL(sa.`balance`,0) balance\n                        FROM sale s INNER JOIN sale_amount sa ON sa.`sale_id`=s.`id` AND balance>0\n                                        INNER JOIN `client` c ON c.`id`=s.`client_id`\n                                                AND c.id=:client_id\n                        -- WHERE s.status IS NULL                        \n                        ) AS t\n                        GROUP BY sale_id\n                        ORDER BY sale_time";
     $result = Yii::app()->db->createCommand($sql)->queryAll(true, array(':client_id' => $client_id));
     foreach ($result as $record) {
         $sale_amount = SaleAmount::model()->find('sale_id=:sale_id', array(':sale_id' => $record["sale_id"]));
         $transaction = Yii::app()->db->beginTransaction();
         try {
             if ($paid_amount <= $record["amount_to_paid"]) {
                 $payment_amount = $paid_amount;
                 $sale_amount->paid = $sale_amount->paid + $paid_amount;
                 $sale_amount->balance = $sale_amount->total - $sale_amount->paid;
                 $sale_amount->save();
                 $sale_payment = new SalePayment();
                 $sale_payment->sale_id = $record["sale_id"];
                 $sale_payment->payment_type = 'Cash';
                 $sale_payment->payment_amount = $payment_amount;
                 $sale_payment->date_paid = $paid_date;
                 $sale_payment->note = $note;
                 $sale_payment->save();
                 $transaction->commit();
                 break;
             } else {
                 $sale_amount->paid = $sale_amount->paid + $record["amount_to_paid"];
                 $sale_amount->balance = $sale_amount->total - $sale_amount->paid;
                 $sale_amount->save();
                 $paid_amount = $paid_amount - $record["amount_to_paid"];
                 $payment_amount = $record["amount_to_paid"];
                 $sale_payment = new SalePayment();
                 $sale_payment->sale_id = $record["sale_id"];
                 $sale_payment->payment_type = 'Cash';
                 $sale_payment->payment_amount = $payment_amount;
                 $sale_payment->date_paid = $paid_date;
                 $sale_payment->note = $note;
                 $sale_payment->save();
                 $transaction->commit();
             }
         } catch (Exception $e) {
             $transaction->rollback();
             return $e->getMessage();
         }
     }
 }
Пример #2
0
 public function copyEntireSuspendSale($sale_id)
 {
     $this->clearAll();
     $sale = Sale::model()->findbyPk($sale_id);
     $sale_item = SaleItem::model()->getSaleItem($sale_id);
     $payments = SalePayment::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);
     $this->setTotalDiscount($sale->discount_amount);
     $this->setSaleId($sale_id);
 }
Пример #3
0
 protected function saveSalePayment($sale_id, $payment_id, $payment_amount, $paid_date, $note, $payment_type = 'Cash')
 {
     $sale_payment = new SalePayment();
     $sale_payment->sale_id = $sale_id;
     $sale_payment->payment_id = $payment_id;
     $sale_payment->payment_type = $payment_type;
     $sale_payment->payment_amount = $payment_amount;
     $sale_payment->date_paid = $paid_date;
     $sale_payment->note = $note;
     $sale_payment->save();
 }
 public function actionPaymentDetail($id)
 {
     $model = new SalePayment('search');
     $model->unsetAttributes();
     // clear any default values
     if (isset($_GET['SalePayment'])) {
         $model->attributes = $_GET['SalePayment'];
     }
     $this->renderPartial('sale_payment', array('model' => $model, 'id' => $id));
 }
Пример #5
0
 protected function saveSalePayment($sale_id, $payments, $payment_received, $date_paid, $payment_id)
 {
     if ($payment_received > 0) {
         // Saving payment items to sale_payment table
         foreach ($payments as $payment) {
             $sale_payment = new SalePayment();
             $sale_payment->sale_id = $sale_id;
             $sale_payment->payment_type = $payment['payment_type'];
             $sale_payment->payment_amount = $payment['payment_amount'];
             $sale_payment->date_paid = $date_paid;
             $sale_payment->note = Yii::app()->shoppingCart->getPaymentNote();
             $sale_payment->payment_id = $payment_id;
             $sale_payment->save();
         }
     }
 }
Пример #6
0
 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;
 }