/**
  * 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)
 {
     $model = $this->loadModel($id);
     //$actual_quantity=$model->current_quantity;
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Items'])) {
         $model->attributes = $_POST['Items'];
         //echo "DUE ADTE  ".$model->factory_due_date;
         if ($model->save()) {
             $original_quantity = $_POST['original_quantity'];
             $original_available_quantity = $_POST['original_available_quantity'];
             //echo "Actual ".$original_quantity;
             //echo "Current ".$model->current_quantity;
             if ($original_quantity == $model->current_quantity && $original_available_quantity == $model->available_quantity) {
                 //echo "value is same";
                 $this->redirect(array('view', 'id' => $model->item_id));
             } elseif ($original_quantity > $model->current_quantity && $original_available_quantity == $model->available_quantity) {
                 //echo "ORIGINAL QUANTITY WAS MORE, ITEM REMOVED";
                 $OutModel = new OutboundItemsHistory();
                 $OutModel->main_item_id = $model->item_id;
                 $OutModel->quantity_moved = $original_quantity - $model->current_quantity;
                 //current_quantity is already having quantity_moved value also,so it is not subtracted here as it will be subtracted again in beforeSave() func.
                 $OutModel->current_quantity_in_stock = $model->current_quantity + $OutModel->quantity_moved;
                 $OutModel->available_quantity_in_stock = $model->current_quantity + $OutModel->quantity_moved;
                 //$OutModel->quantity_moved=$original_quantity-$model->current_quantity;
                 if ($OutModel->save()) {
                     $this->redirect(array('view', 'id' => $model->item_id));
                     //echo "SAVED";
                 } else {
                     echo $OutModel->getError();
                 }
                 //$this->redirect(array('view','id'=>$model->item_id));
             } elseif ($original_quantity < $model->current_quantity && $original_available_quantity == $model->available_quantity) {
                 //echo "ORIGINAL QUANTITY WAS LESS, ITEM ADDED";
                 $InModel = new InboundItemsHistory();
                 $InModel->main_item_id = $model->item_id;
                 $InModel->quantity_moved = $model->current_quantity - $original_quantity;
                 //current_quantity is already having quantity_moved value also,so it is not added here as it will be added again in beforeSave() func.
                 $InModel->current_quantity_in_stock = $model->current_quantity - $InModel->quantity_moved;
                 $InModel->available_quantity_in_stock = $model->current_quantity - $InModel->quantity_moved;
                 //$original_available_quantity=$original_available_quantity+
                 //$InModel->quantity_moved=0;
                 //$InModel->quantity_moved=$model->current_quantity-$original_quantity;
                 if ($InModel->save()) {
                     //$model->available_quantity=$model->current_quantity;
                     $this->redirect(array('view', 'id' => $model->item_id));
                     //echo "SAVED";
                 } else {
                     echo $InModel->getError();
                 }
                 //$this->redirect(array('view','id'=>$model->item_id));
             } elseif ($original_quantity == $model->current_quantity && $original_available_quantity > $model->available_quantity) {
                 //echo "ORIGINAL AVAILABLE QUANTITY WAS MORE, ITEM REMOVED";
                 $OutModel = new OutboundItemsHistory();
                 $OutModel->main_item_id = $model->item_id;
                 $OutModel->quantity_moved = $original_available_quantity - $model->available_quantity;
                 //current_quantity is already having quantity_moved value also,so it is not subtracted here as it will be subtracted again in beforeSave() func.
                 $OutModel->current_quantity_in_stock = $model->current_quantity + $OutModel->quantity_moved;
                 $OutModel->available_quantity_in_stock = $model->current_quantity + $OutModel->quantity_moved;
                 //$OutModel->quantity_moved=$original_quantity-$model->current_quantity;
                 if ($OutModel->save()) {
                     $this->redirect(array('view', 'id' => $model->item_id));
                     //echo "SAVED";
                 } else {
                     echo $OutModel->getError();
                 }
                 //$this->redirect(array('view','id'=>$model->item_id));
             } elseif ($original_quantity == $model->current_quantity && $original_available_quantity < $model->available_quantity) {
                 //echo "ORIGINAL QUANTITY WAS LESS, ITEM ADDED";
                 $InModel = new InboundItemsHistory();
                 $InModel->main_item_id = $model->item_id;
                 $InModel->quantity_moved = $model->available_quantity - $original_available_quantity;
                 //current_quantity is already having quantity_moved value also,so it is not added here as it will be added again in beforeSave() func.
                 $InModel->current_quantity_in_stock = $model->current_quantity - $InModel->quantity_moved;
                 $InModel->available_quantity_in_stock = $model->current_quantity - $InModel->quantity_moved;
                 //$original_available_quantity=$original_available_quantity+
                 //$InModel->quantity_moved=0;
                 //$InModel->quantity_moved=$model->current_quantity-$original_quantity;
                 if ($InModel->save()) {
                     //$model->available_quantity=$model->current_quantity;
                     $this->redirect(array('view', 'id' => $model->item_id));
                     //echo "SAVED";
                 } else {
                     echo $InModel->getError();
                 }
                 //$this->redirect(array('view','id'=>$model->item_id));
             }
             //end of 4th elseif
         }
         //end of if(model->save())
     }
     //if(isset($_POST['Items']))
     $this->render('update', array('model' => $model));
 }
 public function actionUpdateStatus($id)
 {
     echo 'ID ID ' . $id;
     if (!empty($_GET['all_recieved'])) {
         $all_recieved = $_GET['all_recieved'];
     }
     if ($id == 0 && $all_recieved == 'true') {
         $purchase_id = $_GET['purchase_id'];
         echo $purchase_id;
         $items_on_order_model = PurchaseOrder::model()->getItemsOnOrder($purchase_id);
         foreach ($items_on_order_model as $ordered_items) {
             $item_id = $ordered_items->id;
             echo '<hr>ITEM IUS IS     ' . $item_id;
             if ($ordered_items->item_status == 2) {
                 ItemOnOrder::model()->updateByPk($item_id, array('item_status' => 3, 'quantity_recieved' => $ordered_items->quantity_ordered));
             }
             //end of if
         }
         //end of foreach
         $this->redirect(array('/purchaseOrder/orderRecieved/', 'id' => $purchase_id));
     } else {
         $model = $this->loadModel($id);
         $current_quantity_recieved = $model->quantity_recieved;
         if (isset($_POST['ItemOnOrder'])) {
             $model->attributes = $_POST['ItemOnOrder'];
             $status = $model->item_status;
             $diff = $model->ordered_recieved_difference;
             $model->quantity_recieved = $model->quantity_recieved + $diff;
             $quantity_recieved = $model->quantity_recieved;
             $quantity_ordered = $model->quantity_ordered;
             echo "<br> QUANTIY RECIEVED  " . $quantity_recieved;
             echo "<br> QUANTIY ORDERED  " . $quantity_ordered;
             echo "<br> DIFF  " . $diff;
             /*
              * WE have set the flag as 3 for recieved and 6 for damaged
              * we will do funny calculations based on that
              * 
              */
             if ($status == 3) {
                 $comments = $diff . ' recieved. ';
                 //$comments.=' under the purchase order number '.$model->purchaseOrder->order_number;
                 $comments .= ' Processed by ' . Yii::app()->user->name . " on " . date("d-M-y H:i") . '<br>';
                 $model->comments .= $comments;
                 if ($quantity_ordered == $quantity_recieved) {
                     $model->item_status = 3;
                     //i.e. all items are recieved
                 } elseif ($quantity_ordered > $quantity_recieved) {
                     $model->item_status = 4;
                     ////i.e. partiually recieved
                     echo "RECIEVD is  " . $model->quantity_recieved;
                 } elseif ($quantity_ordered < $quantity_recieved) {
                     $model->item_status = 2;
                     //status reset
                 } else {
                     $this->redirect(array('/purchaseOrder/orderRecieved/', 'id' => $purchase_id));
                 }
                 //end of else
             }
             //end of if of status 50
             if ($status == 6) {
                 /*THIS MEANS ALL PARTS WERE DAMAGED*/
                 if ($quantity_ordered == $quantity_recieved) {
                     $model->item_status = 6;
                 } else {
                     $model->item_status = 7;
                     //this is partially damaged item model
                 }
                 //echo 'QUANTITRY DAMAGED  '.$diff;
                 $model->quantity_damaged = $diff;
                 $model->quantity_recieved = $current_quantity_recieved;
                 //$model->quantity_recieved=0.0;
                 /*THIS IS DONE, else the system will store in quantity recieved also*/
                 //$model->quantity_recieved=$current_quantity_recieved;
                 //	echo 'QUANTITRY Recieved'.$model->quantity_recieved;
                 $comments = $diff;
                 $comments .= ' damaged. ';
                 $comments .= ' Processed by: ' . Yii::app()->user->name . " on " . date("d-M-y H:i") . '<br>';
                 $quantity_recieved = 0.0;
                 $model->comments .= $comments;
             }
             ///end of if status 51
             /*IF any quantity is recieved it wil be added to inbound and the items details will also be updated*/
             if ($quantity_recieved > 0) {
                 echo 'Logic to add in inbound';
                 $inbound_model = new InboundItemsHistory();
                 $main_item_id = $model->items_id;
                 $item_model = Items::model()->findByPk($main_item_id);
                 $current_stock = $item_model->current_quantity;
                 $available_stock = $item_model->available_quantity;
                 $quantity_moved = $quantity_recieved;
                 $inbound_model->main_item_id = $model->items_id;
                 $inbound_model->quantity_moved = $quantity_recieved;
                 $inbound_model->items_on_order_id = $model->id;
                 $inbound_model->comments .= $comments . 'Purchase Order Number =' . $model->purchaseOrder->order_number;
                 $inbound_model->available_quantity_in_stock = $quantity_moved + $available_stock;
                 $inbound_model->current_quantity_in_stock = $quantity_moved + $current_stock;
                 if ($inbound_model->save()) {
                     echo 'saved';
                     Items::model()->updateByPk($main_item_id, array('available_quantity' => $quantity_moved + $available_stock, 'current_quantity' => $quantity_moved + $current_stock));
                 } else {
                     echo 'NOT SAVING INBOUND ENTRY';
                 }
             }
             //end of logic to make entry in inbound
             if ($model->save()) {
                 $this->redirect(array('/purchaseOrder/orderRecieved/', 'id' => $model->purchase_order_id));
             }
             ///end of save
             // 			$redirect_url='/purchaseOrder/orderRecieved/'.$purchase_id;
             // 			$url=$this->createUrl($redirect_url);
             // 			$this->redirect($url);
         } else {
             $item_status = $_GET['item_status'];
             $purchase_id = $_GET['purchase_id'];
             $comments = $_GET['comments'];
             $comments .= '<br>Status Reset to On Order by: ' . Yii::app()->user->name . " on " . date("d-M-y H:i") . '<br>';
             echo 'THIS IHJS CPDE   ' . $item_status;
             ItemOnOrder::model()->updateByPk($id, array('item_status' => $item_status, 'quantity_recieved' => '0.0', 'comments' => $comments));
             $this->redirect(array('/purchaseOrder/orderRecieved/', 'id' => $purchase_id));
         }
         //end of else _POST
     }
     //end of else id=0
 }
 /**
  * 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 = InboundItemsHistory::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionInbound()
 {
     //getting data from url.
     $main_item_id = $_GET['item_id'];
     $oldmodel = $_GET['model'];
     $quantity_moved = $_GET['quantity_moved'];
     //    	echo "quantity Moved is : ".$quantity_moved."		Item id is: ".$main_item_id."		CLASSmodel is: ".$oldmodel;
     $model = new InboundItemsHistory();
     $itemModel = Items::model()->findByPk($main_item_id);
     if ($itemModel) {
         $model->current_quantity_in_stock = $itemModel->current_quantity;
         $model->available_quantity_in_stock = $itemModel->available_quantity;
         $current_quantity_in_stock = $model->current_quantity_in_stock;
         $available_quantity_in_stock = $model->available_quantity_in_stock;
         //			echo "<br>";
         //			echo "current quantiy :".$current_quantity_in_stock."  available quantity:".$available_quantity_in_stock;
     } else {
         echo "Item not found";
     }
     //saving values to inbound table
     $model->main_item_id = $main_item_id;
     $model->available_quantity_in_stock = $available_quantity_in_stock;
     $model->current_quantity_in_stock = $current_quantity_in_stock;
     $model->quantity_moved = $quantity_moved;
     if ($model->save()) {
         //			echo "available_quantity_in_stock = ".$available_quantity_in_stock."current_quantity_in_stock = ".$current_quantity_in_stock;
     } else {
         echo "Unable to save";
     }
     //updating items table.
     $itemModel->updateByPk($model->main_item_id, array('available_quantity' => $itemModel->available_quantity + $model->quantity_moved, 'current_quantity' => $itemModel->current_quantity + $model->quantity_moved));
     //output in JSON
     if (is_null($model)) {
         $this->_sendResponse(404, 'No Item found with id ' . $_GET['id']);
     } else {
         // $this->_sendResponse(200, CJSON::encode($_GET['model']));
         $results = array('results' => $model);
         $this->_sendResponse(200, CJSON::encode($results));
     }
     ///end of else of if
 }