| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Session;
- use think\Db;
- /**
- * 订单管理
- *
- * @icon fa fa-circle-o
- */
- class Order extends Backend
- {
- protected $searchFields = 'customer,product';
- /**
- * Order模型对象
- * @var \app\admin\model\Order
- */
- protected $model = null;
- protected $logmodel = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Order;
- $this->logmodel = new \app\admin\model\OrderLog;
- $this->view->assign("statusList", $this->model->getStatusList());
- // $this->view->assign("personnel", $this->model->getpersonnel());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- *
- * @return string|Json
- * @throws \think\Exception
- * @throws DbException
- */
- public function index(){
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $user_info = Session::get('admin');
- $map = [];
- if ($user_info['id'] !== 1){
- $map['company_id'] = $user_info['company_id'];
- }
- $list = $this->model
- ->where($where)
- ->where($map)
- ->order($sort, $order)
- ->paginate($limit);
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 添加
- *
- * @return string
- * @throws \think\Exception
- */
- public function add(){
- if (false === $this->request->isPost()) {
- $params = input('product');
- if ($params){
- $customer = Db::name('order')->where('product','like','%'.$params.'%')->select();
- }else{
- $customer = Db::name('order')->where('product','neq','')->field('id,product')->limit(20)->select();
- }
- $this->assign('customer',$customer);
- //营销部=>经办
- $yxjb = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxjb")->order('name desc')->select();
- //营销部=>审核
- $yxsh = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxsh")->order('name desc')->select();
- $this->assign('yxjb',$yxjb);
- $this->assign('yxsh',$yxsh);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $user_info = Session::get('admin');
- $params['user_id'] = $user_info['id'];
- $params['company_id'] = $user_info['company_id'];
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException()->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- $arr['oid'] = $this->model->getLastInsID();
- $arr['userid'] = $user_info['id'];
- $arr['username'] = $user_info['nickname'];
- $arr['type'] = 1;
- $arr['orderstu'] = 1;
- $this->model->save($arr);//添加到订单管理
- $this->logmodel->save($arr);//在添加订单管理日志
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- $this->success();
- }
- /**
- * 根据订单表 stock.js检测是否有新订单 有返回值为1 代表有新增订单数据
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function stockad(){
- $company_id = $_SESSION['think']['admin']['company_id'];//查询对应公司id
- $order = Db::name('order')->where('company_id',$company_id)->where('orderstu',1)->find();
- if(!empty($order)){
- return $order['orderstu'];
- }
- }
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $ids = input('ids');
- $order = Db::name('order')->field('user_name,examine,customer')->where('id',"=",$ids)->find();
- //查询公司
- $customer_name = Db::name('customer')->field('id,customer_name')->where('customer_name','=',$order['customer'])->find();
- $this->assign('customer_name',$customer_name);
- $customer = Db::name('customer')->field('customer_name')->select();
- $this->assign('customer',$customer);
- //查询 经办 审核
- $yxjb = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxjb")->order('name desc')->select();
- $yxsh = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxsh")->order('name desc')->select();
- $personnel_yxjb = Db::name('personnel')->where('name','=',$order['user_name'])->find();
- $personnel_yxsh = Db::name('personnel')->where('name','=',$order['examine'])->find();
- $this->assign('yxjb',$yxjb);
- $this->assign('yxsh',$yxsh);
- $this->assign('personnel_yxjb',$personnel_yxjb);
- $this->assign('personnel_yxsh',$personnel_yxsh);
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException()->validate($validate);
- }
- $arr = [];
- foreach ($params as $k=>$v){
- if($v!=$row[$k]) {
- switch ($k){
- case 'no':
- $arr['field'] = '编号';
- $arr['before'] = $row['no']; //写入订单管理日志表 查看修改前的字段值
- $arr['after'] = $params['no']; //写入订单管理日志表 查看修改后的字段值
- // continue;
- break;
- case 'customer':
- $arr['field'] = '订货单位';
- $arr['before'] = $row['customer'];
- $arr['after'] = $params['customer'];
- break;
- case 'product':
- $arr['field'] = '品名';
- $arr['before'] = $row['product'];
- $arr['after'] = $params['product'];
- break;
- case 'specs':
- $arr['field'] = '包装规格';
- $arr['before'] = $row['specs'];
- $arr['after'] = $params['specs'];
- break;
- case 'unit':
- $arr['field'] = '单位';
- $arr['before'] = $row['unit'];
- $arr['after'] = $params['unit'];
- break;
- case 'number':
- $arr['field'] = '数量';
- $arr['before'] = $row['number'];
- $arr['after'] = $params['number'];
- break;
- case 'price':
- $arr['field'] = '单价';
- $arr['before'] = $row['price'];
- $arr['after'] = $params['price'];
- break;
- case 'delivery_date':
- $arr['field'] = '交货期';
- $arr['before'] = $row['delivery_date'];
- $arr['after'] = $params['delivery_date'];
- break;
- case 'remark':
- $arr['field'] = '备注';
- $arr['before'] = $row['remark'];
- $arr['after'] = $params['remark'];
- break;
- case 'date':
- $arr['field'] = '日期';
- $arr['before'] = $row['date'];
- $arr['after'] = $params['date'];
- break;
- case 'user_name':
- $arr['field'] = '经办';
- $arr['before'] = $row['user_name'];
- $arr['after'] = $params['user_name'];
- break;
- case 'examine':
- $arr['field'] = '审核';
- $arr['before'] = $row['examine'];
- $arr['after'] = $params['examine'];
- break;
- case 'status':
- $arr['field'] = '状态';
- if ($row['status'] == 1) {
- $arr['before'] = '计划中';
- if($params['status'] == 2){
- $arr['after'] = '生产中';
- }else if($params['status'] == 3){
- $arr['after'] = '已完成';
- }
- }
- if ($row['status'] == 2) {
- $arr['before'] = '生产中';
- if($params['status'] == 1){
- $arr['after'] = '计划中';
- }else if($params['status'] == 3){
- $arr['after'] = '已完成';
- }
- }
- if ($row['status'] == 3) {
- $arr['before'] = '已完成';
- if($params['status'] == 2){
- $arr['after'] = '生产中';
- }else if($params['status'] == 1){
- $arr['after'] = '计划中';
- }
- }
- break;
- }
- $user_info = Session::get('admin');
- $arr['userid'] = $user_info['id'];//用户id
- $arr['username'] = $user_info['nickname'];//用户名
- $arr['oid'] = $row['id'];//订单表id
- $arr['type'] = 2;//订单日志类型 2 修改后
- $arr['orderstu'] = 2;//订单状态
- $res[]=$arr;
- }
- }
- $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- //生产作业票
- public function task(){
- $ids = input('ids');
- if (!$ids) {
- $this->error(__('No Results were found'));
- }
- if (false === $this->request->isPost()) {
- //生产部=>开票人
- $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
- //生产部=>审核人
- $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
- $this->assign('sckp',$sckp);
- $this->assign('scsh',$scsh);
- $order = Db::name('order')->where('id',$ids)->find();
- $map = [];
- $map['name'] = array('like','%'.$order['product'].'%');
- $map['examine_status'] = 2;
- $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
- $res = [];
- if (!empty($list)){
- $res = $this->get_repeat_data($list);
- }
- //批次号顺序新增
- $tastbach = Db::name('task')->order('bach desc')->find();
- $bach = $tastbach['bach'] +1;
- $this->view->assign('bach',$bach);
- // halt($res);die;
- $this->view->assign('ids',$ids);
- $this->view->assign('row', $res);
- return $this->view->fetch();
- }
- }
- //获取二维数组的重复数据
- function get_repeat_data($array){
- //计算出数组的总数量
- $count = count($array);
- $num = [];//算出那个值是不要的 去掉就行
- for($i=0;$i<$count;$i++){
- $i_data = $array[$i]['name'];
- for($j=$i+1;$j<$count;$j++){
- $j_data = $array[$j]['name'];
- if($i_data == $j_data){
- $i_version = substr($array[$i]['version'],1);
- $j_version = substr($array[$j]['version'],1);
- if ($j_version > $i_version){
- $num[$i] = $i;
- }else{
- $num[$i] = $j;
- }
- }
- }
- }
- foreach ($num as $key=>$value){
- unset($array[$value]);
- }
- $result = array_values($array);
- return $result;
- }
- //关联客户管理中:客户名称 => 订货单位
- public function customer_customer(){
- $params = input('customer');
- $user_info = Session::get('admin');
- $map = [];
- if($user_info['id'] !== 1){
- $map['company_id'] = $user_info['company_id'];
- }
- if ($params){
- $customer = Db::name('customer')->where('customer_name','like','%'.$params.'%')->where($map)->select();
- }else{
- $customer = Db::name('customer')->where('customer_name','neq','')->where($map)->field('id,customer_name')->limit(20)->select();
- }
- $result = ['rows'=>$customer];
- return json($result);
- }
- //关联订单品名
- // public function order_product(){
- // $params = input('product');
- // if ($params){
- // $customer = Db::name('order')->where('product','like','%'.$params.'%')->select();
- // }else{
- // $customer = Db::name('order')->where('product','neq','')->field('id,product')->limit(20)->select();
- // }
- // $total = count($customer);
- // $result = ['total'=>$total,'rows'=>$customer];
- // return json($result);
- // }
- }
|