示例#1
0
 /**
  * Finds the Barcode model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Barcode the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Barcode::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
示例#2
0
 /**
  * 获取提交的JSON数据
  * 保存单据数据
  */
 public function actionSave()
 {
     if (isset($_POST['jsonData'])) {
         $jsonData = $_POST['jsonData'];
     }
     if ($jsonData) {
         //如果有数据
         $json = array();
         $idArray = array();
         $dataArray = json_decode($jsonData);
         //解码JSON格式数据
         $addnum = $dataArray[0]->addnum;
         //第一个数组是单据号 供应商 仓库
         $supplier = $dataArray[0]->supplier;
         $warehouse = $dataArray[0]->warehouse;
         //查询单据号的所有的单据,用来比较是否更改数据
         $add = Add::find()->where(['uid' => Yii::$app->user->id, 'addnum' => $addnum])->all();
         $jsonCount = count($dataArray);
         //获取数组数量
         $json['return_code'] = 1;
         //成功获取数据返回1
         for ($i = 1; $i < $jsonCount; $i++) {
             //JSON数组 产品数据从数组1开始 $i = 1
             $id = $dataArray[$i]->id;
             //获取产品ID
             if ($id == 0) {
                 //ID为0 代表是新添加数据,否则 ID是之前保存数据之后返回的 tbl_add表 id
                 $model = new Add();
                 //直接保存在ADD模型
                 $model->addnum = $addnum;
                 $product = $dataArray[$i]->product;
                 $brand = explode('-', $product);
                 if (count($brand) == 1) {
                     $model->brand = $product;
                 } else {
                     $model->brand = $brand[0];
                     $model->model = $brand[1];
                 }
                 $model->count = $model->restcount = $dataArray[$i]->count;
                 $model->price = $dataArray[$i]->price;
                 $model->addprice = $dataArray[$i]->addprice;
                 $model->color = $dataArray[$i]->color;
                 $model->barcode = $dataArray[$i]->barcode;
                 $model->supplier = $supplier;
                 $model->warehouse = $warehouse;
                 $model->remark = $dataArray[$i]->remark;
                 $model->employee = Yii::$app->user->identity->username;
                 $model->imeilen = $dataArray[$i]->imeilen;
                 $model->status = 0;
                 $model->pid = $dataArray[$i]->pid;
                 $model->uid = Yii::$app->user->id;
                 $model->save();
                 $idArray[] = $model->getAttribute('id');
                 //save之后返回单据ID,用于返回JSON数据在保存在页面
                 //保存条码到 tbl_barcode
                 if ($dataArray[$i]->barcode != "") {
                     //有条码的产品,批量保存条码在 tbl_barcode表中
                     $barcodeArray = explode(",", $dataArray[$i]->barcode);
                     for ($j = 0; $j < count($barcodeArray); $j++) {
                         $barModel = new Barcode();
                         $barModel->barcode = $barcodeArray[$j];
                         $barModel->num = $addnum;
                         $barModel->status = 0;
                         $barModel->addid = $id;
                         $barModel->pid = $pid;
                         $barModel->uid = Yii::$app->user->id;
                         $barModel->save();
                     }
                 }
             } else {
                 if ($id > 0) {
                     //如果ID 不为0,代表数据已经在数据库,直接根据
                     $product = $dataArray[$i]->product;
                     $brand = explode('-', $product);
                     //获取 产品-型号 用-分割
                     $mybrand = "";
                     $mymodel = "";
                     if (count($brand) == 1) {
                         $mybrand = $product;
                     } else {
                         $mybrand = $brand[0];
                         $mymodel = $brand[1];
                     }
                     $count = $dataArray[$i]->count;
                     $price = $dataArray[$i]->price;
                     $addprice = $dataArray[$i]->addprice;
                     $color = $dataArray[$i]->color;
                     $barcode = $dataArray[$i]->barcode;
                     $supplier = $supplier;
                     $warehouse = $warehouse;
                     $remark = $dataArray[$i]->remark;
                     $employee = Yii::$app->user->identity->username;
                     $imeilen = $dataArray[$i]->imeilen;
                     $pid = $dataArray[$i]->pid;
                     $timeNow = date('Y-m-d H:i:s', time());
                     //获取当前时间,保存数据的更新时间
                     foreach ($add as $item) {
                         //$add是查询出来所有当前单据号的 数据,循环比较
                         if ($item->getAttribute('id') == $id) {
                             //id一样 开始比较数据是否有更改
                             $isUpdate = false;
                             if ($item->getAttribute('brand') != $mybrand || $item->getAttribute('model') != $mymodel || $item->getAttribute('count') != $count || $item->getAttribute('price') != $price || $item->getAttribute('addprice') != $addprice || $item->getAttribute('color') != $color || $item->getAttribute('barcode') != $barcode || $item->getAttribute('supplier') != $supplier || $item->getAttribute('warehouse') != $warehouse || $item->getAttribute('remark') != $remark) {
                                 $isUpdate = true;
                             }
                             //如果条码有更改
                             if ($item->getAttribute('barcode') != $barcode) {
                                 $isUpdate = true;
                                 //根据单据ID 查询所以条码
                                 $codeModel = Barcode::find()->where(['uid' => Yii::$app->user->id, 'addid' => $id])->all();
                                 $barcodeArray = explode(",", $barcode);
                                 for ($j = 0; $j < count($barcodeArray); $j++) {
                                     //JSON单据条码
                                     $onSave = true;
                                     //onSave为 true,新添加的条码
                                     foreach ($codeModel as $item) {
                                         //循环对比数据库条码
                                         if ($barcodeArray[$j] == $item->getAttribute('barcode')) {
                                             //条码相同不需要保存
                                             $onSave = false;
                                             //false 直接跳出
                                             break;
                                         }
                                     }
                                     if ($onSave) {
                                         //判断是否为新条码,然后保存
                                         $barModel = new Barcode();
                                         $barModel->barcode = $barcodeArray[$j];
                                         $barModel->num = $addnum;
                                         $barModel->status = 0;
                                         $barModel->addid = $id;
                                         $barModel->pid = $pid;
                                         $barModel->uid = Yii::$app->user->id;
                                         $barModel->save();
                                     }
                                 }
                                 foreach ($codeModel as $item) {
                                     //再次循环对比数据库,删除JSON数组立没有的条码
                                     $onDelete = true;
                                     for ($j = 0; $j < count($barcodeArray); $j++) {
                                         if ($item->getAttribute('barcode') == $barcodeArray[$j]) {
                                             $onDelete = false;
                                             break;
                                         }
                                     }
                                     if ($onDelete) {
                                         //删除条码
                                         $deleteModel = Barcode::findOne($item->getAttribute('id'));
                                         $deleteModel->delete();
                                     }
                                 }
                             }
                             if ($isUpdate) {
                                 //有更改的话,更新新的数据到数据库 设置更新时间
                                 Add::updateAll(['brand' => $mybrand, 'model' => $mymodel, 'count' => $count, 'price' => $price, 'addprice' => $addprice, 'color' => $color, 'barcode' => $barcode, 'supplier' => $supplier, 'warehouse' => $warehouse, 'remark' => $remark, 'employee' => $employee, 'imeilen' => $imeilen, 'update_at' => $timeNow, 'pid' => $pid], 'id=:id', [':id' => $id]);
                             }
                         }
                     }
                 }
             }
         }
         $json['idArray'] = $idArray;
         echo json_encode($json);
     } else {
         echo "null";
     }
 }