Advertisement
fernandezekiel

Untitled

Sep 16th, 2013
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. public function actionCreate() {
  2.         $model = new StockTransfer;
  3.         $details = array();
  4.  
  5.         if (Yii::app()->user->branch_id != Branch::ROOT_BRANCH) {
  6.             $model->source_branch_id = Yii::app()->user->branch_id;
  7.         }
  8.         if (!Yii::app()->user->checkAccess('create.any.stocktransfer')){
  9.             $model->source_keeper_id = Yii::app()->user->id;
  10.         }
  11.         if (isset($_POST['StockTransfer'])) {
  12.             $model->attributes = $_POST['StockTransfer'];
  13.             $valid = $model->validate();
  14.             if (isset($_POST['StockTransferDetail'])) {
  15.                 $details = array();
  16.  
  17.                 foreach ($_POST['StockTransferDetail'] as $key => $stockTransferDetail) {
  18.                     $details[$key] = new StockTransferDetail('batchSave'); // so that item_header_id will not  be required
  19.                     $details[$key]->attributes = $stockTransferDetail;
  20.                     $details[$key]->stockTransfer = $model;
  21.                     $valid = $details[$key]->validate() && $valid;
  22.                     $details[$key]->stockTransfer = null;
  23.                 }
  24.             }
  25.  
  26.             if ($valid) {
  27.                 $model->save();
  28.                 $model->refresh();
  29.  
  30.                 foreach ($details as $detail) {
  31.                     $detail->stockTransfer = $model;
  32.                     $detail->save(false);
  33.                 }
  34.                
  35.                 $model->refresh();
  36.                 $model->markStocksForTransferring(); // will update the item_details
  37.                 $this->checkStockLevels($model->item_header_ids);
  38.                 $this->redirect(array('view', 'id' => $model->id));
  39.             }
  40.         }
  41.        
  42.         $this->render('create', array(
  43.             'model' => $model,
  44.             'details' => $details
  45.         ));
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement