/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $i = 0;
     $model1 = new PurchasesInvoices();
     $model2 = new PurchasesInfo();
     $paidModel = new MoneyPaid();
     if (isset($_POST['PurchasesInvoices'])) {
         $model1->attributes = $_POST['PurchasesInvoices'];
         $model1->save();
         $purchases_invoice_id = $model1->id;
         if (isset($_POST['PurchasesInfo'])) {
             $testModel = array();
             while ($i <= $_SESSION['sts']) {
                 $testModel[$i] = new PurchasesInfo();
                 if (isset($_POST['PurchasesInfo'][$i])) {
                     $testModel[$i]->attributes = $_POST['PurchasesInfo'][$i];
                     $testModel[$i]->purchases_invoice_id = $purchases_invoice_id;
                     if (!empty($testModel[$i]->total_amount)) {
                         $testModel[$i]->save();
                         $item = InventoryItems::model()->findByPk($testModel[$i]->item_id);
                         $item->qty += $testModel[$i]->qty;
                         $item->save();
                     }
                 }
                 $i++;
             }
         }
         if (isset($_POST['MoneyPaid'])) {
             $paidModel->attributes = $_POST['MoneyPaid'];
             $paidModel->paid_date = $model1->issue_date;
             $paidModel->purchases_invoice_id = $model1->id;
             $paidModel->supplier_id = $model1->supplier_id;
             $paidModel->status = 1;
             if ($paidModel->save()) {
                 $balance = $paidModel->amount * 1.0 - $model1->total_amount * 1.0;
                 if ($balance <= 0) {
                     $model1->balance = $balance * -1.0;
                 } else {
                     $model1->balance = 0;
                     $creditModel = new SuppliersCredit();
                     $creditModel->amount = $balance;
                     $creditModel->credited_from = $model1->id;
                     $creditModel->supplier_id = $model1->supplier_id;
                     $creditModel->credited_date = $model1->issue_date;
                     $creditModel->save();
                 }
                 //PurchasesInvoices::model()->findByPk($model1->id)->delete();
                 $model1->save();
                 //Yii::app()->AutomatedExecution->AutomatedExecutionHandler();
             }
         }
         $this->redirect(array('AutomatedTransaction', 'id' => $model1->id, 'mode' => 'create'));
     }
     $this->render('create', array('model1' => $model1, 'model2' => $model2, 'paidModel' => $paidModel));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $InvoiceId = $_GET['id'];
     $model = new MoneyPaid();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['MoneyPaid'])) {
         $model->attributes = $_POST['MoneyPaid'];
         $model->purchases_invoice_id = $InvoiceId;
         $total_paid = 0;
         $model1 = PurchasesInvoices::model()->findByPk($InvoiceId);
         $model->supplier_id = $model1->supplier_id;
         $model->save();
         foreach (MoneyPaid::model()->findAll(array('condition' => 'purchases_invoice_id=:invoice', 'params' => array(':invoice' => $InvoiceId))) as $receipts) {
             $total_paid += $receipts->amount;
         }
         $tempBalance = $model1->total_amount - $total_paid;
         $creditRow = SuppliersCredit::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 SuppliersCredit();
                 $creditRow->credited_from = $InvoiceId;
                 $creditRow->supplier_id = $model1->supplier_id;
             }
             $creditRow->amount = -1 * $tempBalance;
             $creditRow->credited_date = $model->paid_date;
             $creditRow->supplier_id = $model1->supplier_id;
             $creditRow->save();
         }
         $model1->save();
         $this->redirect(array('automatedTransaction', 'id' => $model1->id, 'mode' => ''));
     }
     $this->render('create', array('model' => $model, 'id' => $InvoiceId));
 }