/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate($main_item_id)
 {
     $model = new OutboundItemsHistory();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $model->main_item_id = $main_item_id;
     if (isset($_POST['OutboundItemsHistory'])) {
         $model->attributes = $_POST['OutboundItemsHistory'];
         if ($model->save()) {
             $available_quantity = $model->available_quantity_in_stock - $model->quantity_moved;
             $current_quantity = $model->current_quantity_in_stock - $model->quantity_moved;
             //$q_moved=$model->quantity_moved;
             $itemModel = Items::model()->updateByPk($model->main_item_id, array('available_quantity' => $available_quantity + $model->quantity_moved, 'current_quantity' => $current_quantity + $model->quantity_moved));
             //if($model->save())
             $this->redirect(array('view', 'id' => $model->history_id_item));
         }
     }
     $this->render('create', array('model' => $model));
     /*original
     		if(isset($_POST['OutboundItemsHistory']))
     		{
     			$model->attributes=$_POST['OutboundItemsHistory'];
     			if($model->save())
     				$this->redirect(array('view','id'=>$model->history_id_item));
     		}
     
     		$this->render('create',array(
     			'model'=>$model,
     		));*/
 }
 public function actionOutbound()
 {
     //getting values 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;
     //getting values from items table.
     $model = new OutboundItemsHistory();
     $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 outbound
     $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;
     //$model->save();
     if ($model->save()) {
         //			echo "<br>";
         //			echo "in model->save()";
         //			echo "current quantiy :".$model->current_quantity_in_stock."  available quantity:".$model->available_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
 }
 /**
  * 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));
 }