/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $i = 0;
     $model1 = $this->loadModel($id);
     $model2 = $model1->salesInfos;
     $receivedModel = $this->loadReceivedModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['SalesInvoices'])) {
         $model1->attributes = $_POST['SalesInvoices'];
         $model1->save();
         $sales_invoice_id = $model1->id;
         if (isset($_POST['SalesInfo'])) {
             $item_qty = array();
             foreach ($model2 as $cnt => $indModel) {
                 $item_qty[$cnt] = $indModel->qty;
                 $indModel->delete();
             }
             while ($i <= $_SESSION['sts']) {
                 if (isset($_POST['SalesInfo'][$i])) {
                     $indModel = new SalesInfo();
                     $indModel->attributes = $_POST['SalesInfo'][$i];
                     $indModel->sales_invoice_id = $sales_invoice_id;
                     $indModel->save();
                     $item = InventoryItems::model()->findByPk($indModel->item_id);
                     $item->qty -= $indModel->qty - $item_qty[$i];
                     $item->save();
                 }
                 $i++;
             }
             //$model2->save();
         }
         if (isset($_POST['MoneyReceived'])) {
             $receivedModel->attributes = $_POST['MoneyReceived'];
             $receivedModel->save();
             $total_received = 0;
             foreach (MoneyReceived::model()->findAll(array('condition' => 'sales_invoice_id=:invoice', 'params' => array(':invoice' => $id))) as $receipts) {
                 $total_received += $receipts->amount;
             }
             $tempBalance = $model1->total_amount - $total_received;
             $creditRow = CustomersCredit::model()->find(array('condition' => 'credited_from=:invoice', 'params' => array(':invoice' => $id)));
             if ($tempBalance >= 0) {
                 $model1->balance = $tempBalance;
                 if (isset($creditRow)) {
                     $creditRow->amount = 0;
                     $creditRow->save();
                 }
             } else {
                 $model1->balance = 0;
                 if (!isset($creditRow)) {
                     $creditRow = new CustomersCredit();
                     $creditRow->credited_from = $id;
                     $creditRow->customer_id = $model1->customer_id;
                 }
                 $creditRow->amount = -1 * $tempBalance;
                 $creditRow->credited_date = $model1->issue_date;
                 $creditRow->customer_id = $model1->customer_id;
                 $creditRow->save();
             }
             $model1->save();
         }
         $this->redirect(array('AutomatedTransaction', 'id' => $model1->id, 'mode' => ''));
         //$this->redirect(array('view','id'=>$model1->id));
     }
     $this->render('update', array('model1' => $model1, 'model2' => $model2, 'receivedModel' => $receivedModel));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $InvoiceId = MoneyReceived::model()->findByPk($id)->sales_invoice_id;
     $model = $this->loadModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['MoneyReceived'])) {
         $model->attributes = $_POST['MoneyReceived'];
         $model->sales_invoice_id = $InvoiceId;
         $total_received = 0;
         $model1 = SalesInvoices::model()->findByPk($InvoiceId);
         $model->customer_id = $model1->customer_id;
         $model->save();
         foreach (MoneyReceived::model()->findAll(array('condition' => 'sales_invoice_id=:invoice', 'params' => array(':invoice' => $model->sales_invoice_id))) as $receipts) {
             $total_received += $receipts->amount;
         }
         $tempBalance = $model1->total_amount - $total_received;
         $creditRow = CustomersCredit::model()->find(array('condition' => 'credited_from=:invoice', 'params' => array(':invoice' => $InvoiceId)));
         if ($tempBalance >= 0) {
             $model1->balance = $tempBalance;
             if (isset($creditRow)) {
                 $creditRow->amount = 0;
                 $creditRow->save();
             }
         } else {
             $model1->balance = 0;
             if (!isset($creditRow)) {
                 $creditRow = new CustomersCredit();
                 $creditRow->credited_from = $InvoiceId;
                 $creditRow->customer_id = $model1->customer_id;
             }
             $creditRow->amount = -1 * $tempBalance;
             $creditRow->credited_date = $model->received_date;
             $creditRow->customer_id = $model1->customer_id;
             $creditRow->save();
         }
         $model1->save();
         $this->redirect(array('automatedTransaction', 'id' => $model1->id, 'mode' => ''));
     }
     $this->render('update', array('model' => $model, 'id' => $InvoiceId));
 }