/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new GoodsTransaction();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['GoodsTransaction'])) {
         $model->attributes = $_POST['GoodsTransaction'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
    public function actionOut()
    {
        if (isset($_POST['GoodsTransaction'])) {
            $err = 0;
            foreach ($_POST['GoodsTransaction'] as $postrans) {
                $model = new GoodsTransaction();
                //id_trans, qty, date_time, decsription, balance_history, goods_id, user_id
                $model->goods_id = $postrans['goods_id'];
                $model->decsription = $postrans['decsription'];
                $model->balance_history = 0;
                $model->qty = $postrans['qty'];
                $model->in = 0;
                $model->out = $postrans['qty'];
                $model->type_order = $postrans['order_name'];
                $model->warehouse_from = $postrans['warehouse_from'];
                $model->warehouse_to = $postrans['warehouse_to'];
                $model->date_time = date("Y-m-d H:i:s");
                $model->user_id = 1;
                if (!$model->save()) {
                    $errors = $model->getErrors();
                    foreach ($errors as $e) {
                        $messageError .= '
						<p id="error" class="info">
							<span class="info_inner">' . $e[0] . '</span>
						</p>';
                        var_dum($model->save);
                    }
                    Yii::app()->user->setFlash('error', $messageError);
                    $err++;
                } else {
                    $stock_input = StockGoods::model()->findByAttributes(array('goods_id' => $model->goods_id, 'warehouse_id' => $model->warehouse_to));
                    //var_dump($stock_input);
                    if ($stock_input != null) {
                        $stock_input->stock = $stock_input->stock + $model->out;
                        $stock_input->save();
                    } else {
                        $stock_input = new StockGoods();
                        $stock_input->goods_id = $model->goods_id;
                        $stock_input->warehouse_id = $model->warehouse_to;
                        $stock_input->stock = $model->out;
                        $stock_input->save();
                        //var_dump($stock_input->save);
                        //break;
                    }
                    $stock_output = StockGoods::model()->findByAttributes(array('goods_id' => $model->goods_id, 'warehouse_id' => $model->warehouse_from));
                    if ($stock_output != null) {
                        $stock_output->stock = $stock_output->stock - $model->out;
                        $stock_output->save();
                    }
                }
            }
            if ($err == 0) {
                Yii::app()->user->setFlash('success', '<p id="success" class="info">
							<span class="info_inner">Your transaction has been save successfully</span>
						</p>');
            }
            //echo "kookokokok";
            $this->redirect(array('out'));
        }
        $this->render('exit');
    }