/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new PurchasesInfo();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['PurchasesInfo'])) {
         $model->attributes = $_POST['PurchasesInfo'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * 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->purchasesInfos;
     $paidModel = $this->loadpaidModel($id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['PurchasesInvoices'])) {
         $model1->attributes = $_POST['PurchasesInvoices'];
         $model1->save();
         $purchases_invoice_id = $model1->id;
         if (isset($_POST['PurchasesInfo'])) {
             $item_qty = array();
             foreach ($model2 as $cnt => $indModel) {
                 $item_qty[$cnt] = $indModel->qty;
                 $indModel->delete();
             }
             while ($i <= $_SESSION['sts']) {
                 if (isset($_POST['PurchasesInfo'][$i])) {
                     $indModel = new PurchasesInfo();
                     $indModel->attributes = $_POST['PurchasesInfo'][$i];
                     $indModel->purchases_invoice_id = $purchases_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['MoneyPaid'])) {
             $paidModel->attributes = $_POST['MoneyPaid'];
             $paidModel->save();
             $total_paid = 0;
             foreach (MoneyPaid::model()->findAll(array('condition' => 'purchases_invoice_id=:invoice', 'params' => array(':invoice' => $id))) as $receipts) {
                 $total_paid += $receipts->amount;
             }
             $tempBalance = $model1->total_amount - $total_paid;
             $creditRow = SuppliersCredit::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 SuppliersCredit();
                     $creditRow->credited_from = $id;
                     $creditRow->supplier_id = $model1->supplier_id;
                 }
                 $creditRow->amount = -1 * $tempBalance;
                 $creditRow->credited_date = $model1->issue_date;
                 $creditRow->supplier_id = $model1->supplier_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, 'paidModel' => $paidModel));
 }