/** * POST接收一个JSON数组 * 保存单据数据 */ public function actionSave() { if (isset($_POST['jsonData'])) { $jsonData = $_POST['jsonData']; } if ($jsonData != null) { $json = array(); $idArray = array(); $dataArray = json_decode($jsonData); //解析JSON数据 $sellnum = $dataArray[0]->sellnum; //第一个数组0 代表销售单据 //查询单据号的所有的单据,用来比较是否更改数据 $sell = Sell::find()->where(['uid' => Yii::$app->user->id, 'sellnum' => $sellnum])->all(); $jsonCount = count($dataArray); //获取JSON数组总和 $json['return_code'] = 1; for ($i = 1; $i < $jsonCount; $i++) { //数组0是单据号,所以 i 从1 开始循环 $id = $dataArray[$i]->id; if ($id == 0) { //表单JSON数据会提交一个 id,代表单据在数据的id,如果为0,是新数据 $model = new Sell(); //直接把数据保存在Model 在 model->save(); $model->sellnum = $sellnum; $model->addnum = $dataArray[$i]->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 = $dataArray[$i]->count; $model->price = $dataArray[$i]->price; $model->addprice = $dataArray[$i]->addprice; $model->oldprice = $dataArray[$i]->oldprice; $model->color = $dataArray[$i]->color; $model->barcode = $dataArray[$i]->barcode; $model->supplier = $dataArray[$i]->supplier; $model->warehouse = $dataArray[$i]->warehouse; $model->remark = $dataArray[$i]->remark; $model->employee = Yii::$app->user->identity->username; $model->status = 0; $model->pid = $dataArray[$i]->pid; $model->uid = Yii::$app->user->id; //检测是否填写客户信息,有的话保存在客户表tbl_customer,在把返回的客户ID 保存在出库单据cid上 if ($dataArray[$i]->custom != "" || $dataArray[$i]->tell != "") { $customModel = new Customer(); if ($dataArray[$i]->custom == "") { $customModel->name = "无"; } else { $customModel->name = $dataArray[$i]->custom; } if ($dataArray[$i]->tell == "") { $customModel->tell = "无"; } else { $customModel->tell = $dataArray[$i]->tell; } $customModel->uid = Yii::$app->user->id; $customModel->save(); //保存客户数据 $cid = $customModel->getAttribute('id'); //获取客户数据的ID $model->cid = $cid; } $model->save(); $idArray[] = $model->getAttribute('id'); //更新条码为 出库状态 status=1 if ($dataArray[$i]->barcode != "") { //如果产品有条码,更新条码数据库tab_barcode的状态为1,代表出库了 Barcode::updateAll(['status' => 1], 'barcode=:barcode', [':barcode' => $dataArray[$i]->barcode]); } } else { if ($id > 0) { //大于0 代表数据已经在库,直接更新数据库 //先查询这个单据号的所以数据,用来循环比较表格数据 $timeNow = date('Y-m-d H:i:s', time()); //表格JSON数据 与 数据库查询数据循环比较,查找对应ID的数据 foreach ($sell as $item) { //ID一样说明数据找到 if ($item->getAttribute('id') == $id) { //获取客户ID $cid = $item->getAttribute('cid'); //查询客户信息,对比表格输入数据是否有更改,然后update tbl_customer if ($cid != null && $cid > 0) { $customModel = Customer::find()->where(['uid' => Yii::$app->user->id, 'id' => $cid])->one(); if ($customModel->getAttribute('name') != $dataArray[$i]->custom) { Customer::updateAll(['name' => $dataArray[$i]->custom], 'id=:id', [':id' => $cid]); } if ($customModel->getAttribute('tell') != $dataArray[$i]->tell) { Customer::updateAll(['tell' => $dataArray[$i]->tell], 'id=:id', [':id' => $cid]); } } //数据库对比表格输入信息是否有更改,然后update tbl_sell if ($item->getAttribute('count') != $dataArray[$i]->count || $item->getAttribute('price') != $dataArray[$i]->price || $item->getAttribute('remark') != $dataArray[$i]->remark) { Sell::updateAll(['count' => $dataArray[$i]->count, 'price' => $dataArray[$i]->price, 'remark' => $dataArray[$i]->remark], 'id=:id', [':id' => $id]); } } } } } } $json['idArray'] = $idArray; echo json_encode($json); } }