예제 #1
0
 public function actionCreate()
 {
     $goods = json_decode(Yii::$app->request->getBodyParam('goods'), true);
     $date = Yii::$app->request->getBodyParam('date');
     foreach ($goods as $key => $value) {
         if ($value['cnt'] < 1) {
             unset($goods[$key]);
         }
     }
     if (count($goods) < 1) {
         $this->ajax_return(false, '明细填写不正确!');
     }
     $model = new StoreOutstore();
     $model->order_id = StoreOutstore::genOrderId();
     $model->status = 'wait';
     $model->company = Yii::$app->user->identity->getDept();
     $model->user = Yii::$app->user->identity->getName();
     $model->datetime = date('Y-m-d', strtotime($date));
     //开启事务
     $transaction = Yii::$app->db->beginTransaction();
     try {
         $storeStoreDetailModel = new StoreStoreDetail();
         $detailModelInsertFlag = true;
         foreach ($goods as $value) {
             $detailModel = clone $storeStoreDetailModel;
             $detailModel->setAttributes(['order_id' => $model->order_id, 'goods_id' => $value['id'], 'count' => $value['cnt'], 'remark' => $value['remark'], 'datetime' => $model->datetime, 'flag' => 'OUT', 'status' => 'off']);
             if (!$detailModel->insert()) {
                 $detailModelInsertFlag = false;
                 break;
             }
         }
         if (!$model->insert() || $detailModelInsertFlag == false) {
             $transaction->rollBack();
             $this->ajax_return(false, '插入数据失败!');
         }
     } catch (Exception $e) {
         $transaction->rollBack();
         $this->ajax_return(false, '其他异常!');
     }
     $transaction->commit();
     $this->ajax_return(true);
 }