public function actionStatistics() { $males = User::find()->where(['sex' => 'MALE', 'type' => 'CLIENT'])->count(); $females = User::find()->where(['sex' => 'FEMALE', 'type' => 'CLIENT'])->count(); $sellc = Sell::find()->where(['status' => 'COMPLETE'])->count(); $selli = Sell::find()->where(['status' => 'INCOMPLETE'])->count(); $totalsell = Sell::find()->all(); return $this->render('statistics', ['males' => $males, 'females' => $females, 'sellc' => $sellc, 'selli' => $selli, 'totalsell' => $totalsell]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $isBack = false) { $query = Sell::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, 'addnum' => $this->addnum, 'sellnum' => $this->sellnum, 'count' => $this->count, 'price' => $this->price, 'addprice' => $this->addprice, 'create_at' => $this->create_at, 'update_at' => $this->update_at, 'status' => $isBack ? 1 : 0, 'pid' => $this->pid, 'uid' => Yii::$app->user->id]); $query->andFilterWhere(['like', 'brand', $this->brand])->andFilterWhere(['like', 'model', $this->model])->andFilterWhere(['like', 'barcode', $this->barcode])->andFilterWhere(['like', 'supplier', $this->supplier])->andFilterWhere(['like', 'warehouse', $this->warehouse])->andFilterWhere(['like', 'remark', $this->remark])->andFilterWhere(['like', 'employee', $this->employee]); return $dataProvider; }
/** * 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); } }
$isNewbill = false; //是否为新单据 if ($sellNum == "") { $isNewbill = true; $num = date("Ymd", time()); $count = 0; $sql = "SELECT * FROM tbl_sell WHERE uid=" . Yii::$app->user->id . " AND left(sellnum,8)=" . $num . " order by id desc"; $sell = Sell::findBySql($sql)->one(); if ($sell) { $sellNum = $sell->getAttribute('sellnum'); $sellNum += 1; } else { $sellNum = $num . "01"; } } else { $sell = Sell::find()->where(['uid' => Yii::$app->user->id, 'sellnum' => $sellNum, 'status' => 0])->all(); } ?> <?php echo Html::label('单据号:'); ?> <?php echo Html::label($sellNum, null, ['id' => 'sellNum']); ?> <button id="sell-save-bill">保存单据</button> <div id="debug">1</div> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>条码</th>