示例#1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Barcode::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'num' => $this->num, 'status' => $this->status, 'create_at' => $this->create_at, 'update_at' => $this->update_at, 'pid' => $this->pid, 'uid' => Yii::$app->user->id]);
     $query->andFilterWhere(['like', 'barcode', $this->barcode]);
     return $dataProvider;
 }
示例#2
0
 /**
  * AJAX匹配查询产品
  */
 public function actionSearchproduct()
 {
     if (isset($_POST['product'])) {
         $product = $_POST['product'];
     }
     if ($product != null) {
         $sql = "SELECT * FROM tbl_add WHERE uid=" . Yii::$app->user->id . " AND brand LIKE '%" . $product . "%' or model LIKE '%" . $product . "%'";
         $add = Add::findBySql($sql)->all();
         if ($add) {
             $json['return_code'] = 1;
             $count = 0;
             $barcodeArray = array();
             $productArray = array();
             $priceArray = array();
             $colorArray = array();
             $numArray = array();
             $supplierArray = array();
             $warehouseArray = array();
             $pidArray = array();
             foreach ($add as $item) {
                 $brand = $item->getAttribute('brand');
                 $model = $item->getAttribute('model');
                 if ($model == "") {
                     $myProduct = $brand;
                 } else {
                     $myProduct = $brand . "-" . $model;
                 }
                 $barcode = $item->getAttribute('barcode');
                 if (strlen($barcode) > 0) {
                     $codeArray = explode(",", $barcode);
                     for ($i = 0; $i < count($codeArray); $i++) {
                         $barcodeModel = Barcode::find()->where(['uid' => Yii::$app->user->id, 'barcode' => $codeArray[$i], 'status' => 0])->one();
                         if ($barcodeModel) {
                             $count++;
                             $barcodeArray[] = $codeArray[$i];
                             $productArray[] = $myProduct;
                             $priceArray[] = $item->getAttribute('price');
                             $colorArray[] = $item->getAttribute('color');
                             $numArray[] = $item->getAttribute('addnum');
                             $supplierArray[] = $item->getAttribute('supplier');
                             $warehouseArray[] = $item->getAttribute('warehouse');
                             $pidArray[] = $item->getAttribute('pid');
                         }
                     }
                 } else {
                     if ($item->getAttribute('imeilen') == 0) {
                         $count++;
                         $barcodeArray[] = "无";
                         $productArray[] = $myProduct;
                         $priceArray[] = $item->getAttribute('price');
                         $colorArray[] = $item->getAttribute('color');
                         $numArray[] = $item->getAttribute('addnum');
                         $supplierArray[] = $item->getAttribute('supplier');
                         $warehouseArray[] = $item->getAttribute('warehouse');
                         $pidArray[] = $item->getAttribute('pid');
                     }
                 }
             }
             $json['barcodeArray'] = $barcodeArray;
             $json['productArray'] = $productArray;
             $json['priceArray'] = $priceArray;
             $json['colorArray'] = $colorArray;
             $json['numArray'] = $numArray;
             $json['supplierArray'] = $supplierArray;
             $json['warehouseArray'] = $warehouseArray;
             $json['pidArray'] = $pidArray;
             $json['count'] = $count;
         } else {
             $json['return_code'] = 0;
         }
         echo json_encode($json);
     }
 }
示例#3
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";
     }
 }
示例#4
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     // Update
     $barcode = Barcode::find($id);
     $barcode->encoding_format = Input::get('encoding_format');
     $barcode->barcode_width = Input::get('barcode_width');
     $barcode->barcode_height = Input::get('barcode_height');
     $barcode->text_size = Input::get('text_size');
     $barcode->save();
     // redirect
     $url = Session::get('SOURCE_URL');
     return redirect()->to($url)->with('message', trans('messages.barcode-update-success'))->with('activebarcode', $barcode->id);
 }