예제 #1
0
파일: Sale.php 프로젝트: robby-xp/IMS
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSalesDetails()
 {
     return $this->hasMany(SaleDetail::className(), ['sale_id' => 'id']);
 }
예제 #2
0
 /**
  * Updates an existing Sale model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelDetails = $model->getSalesDetails()->all();
     if ($model->load(Yii::$app->request->post())) {
         $oldModelDetails = array();
         foreach ($modelDetails as $modelDetail) {
             $oldModelDetails[$modelDetail->id] = ['item_id' => $modelDetail->item_id, 'quantity' => $modelDetail->quantity];
         }
         $oldIDs = ArrayHelper::map($modelDetails, 'id', 'id');
         $modelDetails = Model::createMultiple(SaleDetail::classname(), $modelDetails);
         Model::loadMultiple($modelDetails, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelDetails, 'id', 'id')));
         // validate all models
         $valid = $model->validate() & Model::validateMultiple($modelDetails);
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         if ($flag = SaleDetail::deleteAll(['id' => $deletedIDs]) > 0) {
                             foreach ($deletedIDs as $id) {
                                 $item = Item::findOne($oldModelDetails[$id]['item_id']);
                                 $item->stock += $oldModelDetails[$id]['quantity'];
                                 if (!($flag = $item->save())) {
                                     $transaction->rollBack();
                                     break;
                                 }
                             }
                         } else {
                             $transaction->rollBack();
                         }
                     }
                     if ($flag) {
                         foreach ($modelDetails as $modelDetail) {
                             $quantity = $modelDetail->quantity;
                             if (!empty($modelDetail->id) && $modelDetail->item_id == $oldModelDetails[$modelDetail->id]['item_id']) {
                                 $quantity -= $oldModelDetails[$modelDetail->id]['quantity'];
                             }
                             $modelDetail->sale_id = $model->id;
                             if (($flag = $modelDetail->save(false)) && $quantity !== 0) {
                                 $item = Item::findOne($modelDetail->item_id);
                                 $item->stock -= $quantity;
                                 $flag = $item->save();
                             }
                             if (!$flag) {
                                 $transaction->rollBack();
                                 break;
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('update', ['model' => $model, 'modelDetails' => empty($modelDetails) ? [new SaleDetail()] : $modelDetails]);
 }