Exemplo n.º 1
0
<table class="table table-bordered">
    <tr>
        <th>Edit</th>
        <th>View</th>
        <th>Date</th>
        <th>Transacation</th>
        <th>#</th>
        <th>Description</th>
        <th>Contact</th>
        <th>Amount</th>
        <th>Balance</th>
    </tr>

        <?php 
$balance = 0;
$PurchasesInfos = PurchasesInfo::model()->findAll(array('condition' => 'item_id=:item', 'params' => array(':item' => $id)));
foreach ($PurchasesInfos as $PurchasesInfo) {
    ?>
            <tr>
            <td><?php 
    echo CHtml::link('<button class="btn btn-default btn-sm"><span class="btn-label-style">Edit</span></button>', $this->createAbsoluteUrl('purchasesInfos/update', array('id' => $PurchasesInfo->purchasesInvoice->id)));
    ?>
            </td>
            <td><?php 
    echo CHtml::link('<button class="btn btn-default btn-sm"><span class="btn-label-style">View</span></button>', $this->createAbsoluteUrl('purchasesInfos/view', array('id' => $PurchasesInfo->purchasesInvoice->id)));
    ?>
            </td>
            <td>
                <?php 
    echo $PurchasesInfo->purchasesInvoice->issue_date;
    ?>
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return PurchasesInfo the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = PurchasesInfo::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $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));
 }