$form = $this->beginWidget('CActiveForm', array('id' => 'item-on-order-form', 'enableAjaxValidation' => false));
?>

<?php 
/**COLLECTING THE DATA*/
$purchase_id = $_GET['po_id'];
$item_id = $_GET['item_id'];
$model->purchase_order_id = $purchase_id;
$model->items_id = $item_id;
//*SINCE FIRST STATUS OF ITEM IS DRAFT*//
$model->item_status = 1;
/*THIS IS FOR THE CURRENT ITEMS Which is being added*/
$itemModel = Items::model()->findByPk($item_id);
$purchaseModel = PurchaseOrder::model()->findByPk($purchase_id);
/*THIS IS To get the items which are already orrderd**/
$items_on_order_model = PurchaseOrder::model()->getItemsOnOrder($purchase_id);
$model->suppliers_id = $purchaseModel->suppliers_id;
$model->unit_price = $itemModel->sale_price;
?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php 
echo $form->errorSummary($model);
?>

<!-- FIRST PART OF THE FORM WHICH DISPLAYS PURCHASE ORDER DETAILS --> 
	
	<div class="row">
	<table>
示例#2
0
<?php

namespace Afosto\ActiveAnts;

require_once dirname(__FILE__) . '/vendor/autoload.php';
//Include our configs
require_once dirname(__FILE__) . '/../config.php';
//Make sure this directory is writable
$cacheDirectory = dirname(__FILE__) . '/../cache/';
App::start($url, $user, $password, $cacheDirectory);
$product = Product::model()->setName('testProduct')->setSku('testSku');
if (!$product->save()) {
    echo $product->getMessage();
}
$item = OrderItem::model()->setSku('testSku', false)->setGrossPrice(1.21)->setName('testProduct')->setTaxRate(21);
$address = Address::model()->setName('Afosto SaaS BV')->setAddress('Protonstraat', 9, 'a')->setCity('Groningen')->setCountry('NL')->setPostalcode('9743AL');
$order = Order::model()->setEmail('*****@*****.**')->setOrderId('#' . rand(100, 999))->setPhoneNumber('test')->addOrderItem($item)->setBillingAddress($address)->setShippingAddress();
//$order->setPickupPoint('NL-111101', '1111AA', 'Straatnaam 10a' , 'Groningen');
if (!$order->save()) {
    echo $order->getMessage();
}
$purchase = PurchaseOrder::model()->addItem('testSku', 1)->addReference('testPurchaseOrder');
if (!$purchase->save()) {
    echo $purchase->getMessage();
}
foreach (Stock::model()->findAll() as $stock) {
    echo $stock->sku . ': ' . $stock->stock . "\n";
}
 protected function afterSave()
 {
     $vat_percentage = Yii::app()->params['vat_in_percentage'];
     $purchaseOrderQueryModel = PurchaseOrder::model()->findByPk($this->purchase_order_id);
     $total_cost = $purchaseOrderQueryModel->total_cost + $this->total_price;
     $vat_amount = $total_cost * $vat_percentage / 100;
     $net_cost = $total_cost + $vat_amount;
     //echo $total_cost;
     $purchaseOrderUpdateModel = PurchaseOrder::model()->updateByPk($this->purchase_order_id, array('total_cost' => $total_cost, 'vat' => $vat_amount, 'net_cost' => $net_cost));
 }
 /**
  * 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 the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = PurchaseOrder::model()->with('comp')->with('paymentTerm')->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionOrderRecieved($id)
 {
     $model = $this->loadModel($id);
     /*THIS WILL CALCULATE THE STATUS OF THE PURCHASE ORDER ON THE BASIS OF ALL OTHER ITEMS*/
     $items_on_order_model = PurchaseOrder::model()->getItemsOnOrder($id);
     $flag1 = 0;
     $flag2 = 0;
     $flag3 = 0;
     foreach ($items_on_order_model as $ordered_items) {
         $item_status_id = $ordered_items->item_status;
         /**
          * 2 means all items are on order and when recieved for first time all items will have the status 2. 
          * NOTE:-- THIS IS ALSO CONSIDERED AS RESET POSITION
          */
         if ($item_status_id == 2) {
             $flag1 = 2;
         } elseif ($item_status_id == 3 || $item_status_id >= 10) {
             $flag2 = 3;
         } else {
             $flag3 = 4;
             break;
         }
     }
     //end of foreach
     $order_status = '';
     if ($flag1 == 2 && $flag2 == 0 && $flag3 == 0) {
         $order_status = 3;
         ///i.e. order is recieved
         PurchaseOrder::model()->updateByPk($id, array('date_of_order_recieved' => time()));
     } else {
         if ($flag1 == 0 && $flag2 == 3 && $flag3 == 0) {
             $order_status = 10;
         } else {
             $order_status = 4;
             //i.e. order is partially recieved and some more order is to come
         }
     }
     PurchaseOrder::model()->updateByPk($id, array('order_status' => $order_status));
     $model = $this->loadModel($id);
     $this->render('orderRecieved', array('model' => $model));
 }
	 			<td><?php 
    echo $ordered_items->quantity_ordered;
    ?>
</td>
	 			<td><?php 
    echo $ordered_items->quantity_recieved;
    ?>
</td>
	 			<td>

	 			<?php 
    $item_on_order_id = $ordered_items->id;
    //echo $item_on_order_id;
    $item_status_code = $ordered_items->item_status;
    /*THIS ITEM MODEL IS FOR THE FORM OF EACH ROW*/
    $item_model = PurchaseOrder::model()->getItemOnOrder($item_on_order_id);
    if ($item_status_code == 3 || $item_status_code >= 10) {
        $item_status = ItemOnOrder::model()->getItemStatus($item_status_code);
        echo $item_status;
        if ($model->order_status < 10) {
            $reset_url = Yii::app()->baseUrl . '/ItemOnOrder/updateStatus/' . $item_on_order_id . '?item_status=2&purchase_id=' . $purchase_id . '&comments=' . $ordered_items->comments;
            echo '<b>' . CHtml::link('Reset', $reset_url) . '</b><br>';
        }
        echo "</td>";
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        //	echo "<td style='vertical-align:top;'>".$ordered_items->comments."</td>";
    } else {
        $url = "/ItemOnOrder/updateStatus/" . $item_on_order_id;
        $action_url = $this->createUrl($url);
        $item_form = $this->beginWidget('CActiveForm', array('id' => 'items_on_order_form', 'enableAjaxValidation' => false, 'action' => $action_url, 'method' => 'post'));
        if (empty($item_model->quantity_recieved)) {
 public function actionCancelItems($id)
 {
     $model = $this->loadModel($id);
     $quantity_cancelled = $model->quantity_ordered - $model->quantity_recieved;
     $model->comments .= '<br>' . $quantity_cancelled . ' quantity have been cancelled by the user ' . Yii::app()->user->name;
     $model->comments .= ' on ' . date("F j, Y, g:i a");
     $model->quantity_ordered = $model->quantity_recieved;
     if ($quantity_cancelled == $model->quantity_ordered) {
         $model->item_status = 11;
         //item status will be changed to cancelled
     } else {
         $model->item_status = 3;
         //item status will be changed to recieeved
     }
     $item_total_price_before = $model->total_price;
     $model->total_price = $model->quantity_recieved * $model->unit_price;
     $purchaseOrderQueryModel = PurchaseOrder::model()->findByPk($model->purchase_order_id);
     /*HERE IT IS ONLY DEDUCTED BECASUSE IT WILL BE AUTOMATIUCALLY ADDED LATER AFTER FUNCTION*/
     $ameneded_total_cost = $purchaseOrderQueryModel->total_cost - $item_total_price_before;
     PurchaseOrder::model()->updateByPk($model->purchase_order_id, array('total_cost' => $ameneded_total_cost));
     if ($model->save()) {
         /*I WIL SEND EMAIL FIRST*/
         $reciever_email = $purchaseOrderQueryModel->suppliers->email;
         $reciever_name = $purchaseOrderQueryModel->suppliers->contact_person;
         $sender_email = Yii::app()->params['adminEmail'];
         $sender_name = Yii::app()->params['company_name'];
         $message = new YiiMailMessage();
         $message->setTo(array($reciever_email => $reciever_name));
         $message->setFrom(array($sender_email => $sender_name));
         $message->setSubject('Items Cancelled - Purchase Order: ' . $purchaseOrderQueryModel->order_number . ' ');
         $msg_body = 'Dear Sir, <br> This is to remind you that we are cancelling the following items from the order as we have not got the delivery of it.<br>';
         $msg_body .= '<table><tr><th>Name</th><th>Part Number</th><th>Quantity Ordered</th><th>Quantity Cancelled</th></tr>';
         $msg_body .= '<tr><td>' . $model->items->name . '</td><td>' . $model->items->part_number . '</td><td>' . $model->quantity_ordered . '</td><td>' . $quantity_cancelled . '</td></tr></table>';
         $msg_body .= 'Please Update the Purchase Order Copy <hr>';
         $msg_body .= $this->renderPartial('/purchaseOrder/finaliseOrder', array('model' => $purchaseOrderQueryModel), true);
         $message->setBody($msg_body, 'text/html');
         $numsent = Yii::app()->mail->send($message);
         $numsent = 1;
         if ($numsent == 1) {
             echo 'Supplier Notified for cancellation';
             $this->redirect(array('/purchaseOrder/orderRecieved/', 'id' => $model->purchase_order_id));
         } else {
             $this->raiseEvent('Error', 'error');
         }
         //end of else
         //echo "in itemOnOrder controller";
         $this->redirect(array('/PurchaseOrder/orderRecieved', 'id' => $model->purchase_order_id));
     } else {
         echo 'error in updating ';
     }
 }