public function afterSave()
 {
     Goods::model()->updateByPk($this->goods_id, array('stock' => $this->balance_history));
     StockComponents::model()->updateByPk($this->goods_id, array('stock' => $this->balance_history));
 }
    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if (isset($_POST['TComponentsIn'])) {
            $err = 0;
            foreach ($_POST['TComponentsIn'] as $postrans) {
                $model = new TComponentsIn();
                //id_trans, qty, date_time, decsription, balance_history, component_id, user_id
                $model->component_id = $postrans['component_id'];
                //$model->warehouse_from=$postrans['warehouse_from'];
                $model->warehouse_to = $postrans['warehouse_to'];
                $model->decsription = $postrans['decsription'];
                $model->balance_history = 0;
                $model->qty = $postrans['qty'];
                $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>';
                    }
                    Yii::app()->user->setFlash('error', $messageError);
                    $err++;
                } else {
                    $stock_input = StockComponents::model()->findByAttributes(array('component_id' => $model->component_id, 'warehouse_id' => $model->warehouse_to));
                    //var_dump($stock_input);
                    if ($stock_input != null) {
                        $stock_input->stock = $stock_input->stock + $model->qty;
                        $stock_input->save();
                    } else {
                        $stock_input = new StockComponents();
                        $stock_input->component_id = $model->component_id;
                        $stock_input->warehouse_id = $model->warehouse_to;
                        $stock_input->stock = $model->qty;
                        $stock_input->save();
                        //var_dump($stock_input->attributes);
                        //break;
                    }
                    /**$stock_output=StockComponents::model()->findByAttributes(array('component_id'=>$model->component_id,'warehouse_id'=>$model->warehouse_from));
                    			if($stock_output != null){
                    				$stock_output->stock=$stock_output->stock-$model->qty;
                    				$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('create'));
        }
        $this->render('index');
    }
 public function afterSave()
 {
     $component_id = $this->getAttribute('component_id');
     $stok = intval(Components::model()->findByPk($component_id)->stock);
     $balance = $stok + intval($this->getAttribute('qty'));
     Components::model()->updateByPk($component_id, array('stock' => $balance));
     StockComponents::model()->updateByPk($component_id, array('stock' => $balance));
     $this->updateByPk($this->id_trans, array('balance_history' => $balance));
 }