Order.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\exception\PDOException;
  5. use think\exception\ValidateException;
  6. use think\Session;
  7. use think\Db;
  8. /**
  9. * 订单管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Order extends Backend
  14. {
  15. protected $searchFields = 'customer,product,id,remark';
  16. /**
  17. * Order模型对象
  18. * @var \app\admin\model\Order
  19. */
  20. protected $model = null;
  21. protected $logmodel = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Order;
  26. $this->logmodel = new \app\admin\model\OrderLog;
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. $this->view->assign("eseList", $this->model->getEseList());
  29. // $this->view->assign("personnel", $this->model->getpersonnel());
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. *
  39. * @return string|Json
  40. * @throws \think\Exception
  41. * @throws DbException
  42. */
  43. public function index(){
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if (false === $this->request->isAjax()) {
  47. return $this->view->fetch();
  48. }
  49. //如果发送的来源是 Selectpage,则转发到 Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  54. $user_info = Session::get('admin');
  55. $map = [];
  56. if ($user_info['id'] !== 1){
  57. $map['company_id'] = $user_info['company_id'];
  58. }
  59. $list = $this->model
  60. ->where($where)
  61. ->where($map)
  62. ->order($sort, $order)
  63. ->paginate($limit);
  64. $result = ['total' => $list->total(), 'rows' => $list->items()];
  65. return json($result);
  66. }
  67. /**
  68. * 添加
  69. *
  70. * @return string
  71. * @throws \think\Exception
  72. */
  73. public function add(){
  74. if (false === $this->request->isPost()) {
  75. $params = input('product');
  76. if ($params){
  77. $customer = Db::name('order')->where('product','like','%'.$params.'%')->select();
  78. }else{
  79. $customer = Db::name('order')->where('product','neq','')->field('id,product')->limit(20)->select();
  80. }
  81. $this->assign('customer',$customer);
  82. //营销部=>经办
  83. $yxjb = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxjb")->order('name desc')->select();
  84. //营销部=>审核
  85. $yxsh = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxsh")->order('name desc')->select();
  86. $this->assign('yxjb',$yxjb);
  87. $this->assign('yxsh',$yxsh);
  88. return $this->view->fetch();
  89. }
  90. $params = $this->request->post('row/a');
  91. if (empty($params)) {
  92. $this->error(__('Parameter %s can not be empty', ''));
  93. }
  94. $params = $this->preExcludeFields($params);
  95. $user_info = Session::get('admin');
  96. $params['user_id'] = $user_info['id'];
  97. $params['company_id'] = $user_info['company_id'];
  98. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  99. $params[$this->dataLimitField] = $this->auth->id;
  100. }
  101. $result = false;
  102. Db::startTrans();
  103. try {
  104. //是否采用模型验证
  105. if ($this->modelValidate) {
  106. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  107. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  108. $this->model->validateFailException()->validate($validate);
  109. }
  110. $result = $this->model->allowField(true)->save($params);
  111. $arr['oid'] = $this->model->getLastInsID();
  112. $arr['userid'] = $user_info['id'];
  113. $arr['username'] = $user_info['nickname'];
  114. $arr['type'] = 1;
  115. $arr['orderstu'] = 1;
  116. $this->model->save($arr);//添加到订单管理
  117. $this->logmodel->save($arr);//在添加订单管理日志
  118. Db::commit();
  119. } catch (ValidateException|PDOException|Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result === false) {
  124. $this->error(__('No rows were inserted'));
  125. }
  126. $this->success();
  127. }
  128. /**
  129. * 根据订单表 stock.js检测是否有新订单 有返回值为1 代表有新增订单数据
  130. * @return string
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public function stockad(){
  136. $company_id = $_SESSION['think']['admin']['company_id'];//查询对应公司id
  137. $order = Db::name('order')->where('company_id',$company_id)->where('orderstu',1)->find();
  138. if(!empty($order)){
  139. return $order['orderstu'];
  140. }
  141. }
  142. public function edit($ids = null)
  143. {
  144. $row = $this->model->get($ids);
  145. if (!$row) {
  146. $this->error(__('No Results were found'));
  147. }
  148. $adminIds = $this->getDataLimitAdminIds();
  149. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  150. $this->error(__('You have no permission'));
  151. }
  152. if (false === $this->request->isPost()) {
  153. $ids = input('ids');
  154. $order = Db::name('order')->field('user_name,examine,customer')->where('id',"=",$ids)->find();
  155. //查询公司
  156. $customer_name = Db::name('customer')->field('id,customer_name')->where('customer_name','=',$order['customer'])->find();
  157. $this->assign('customer_name',$customer_name);
  158. $customer = Db::name('customer')->field('customer_name')->select();
  159. $this->assign('customer',$customer);
  160. //查询 经办 审核
  161. $yxjb = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxjb")->order('name desc')->select();
  162. $yxsh = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxsh")->order('name desc')->select();
  163. $personnel_yxjb = Db::name('personnel')->where('name','=',$order['user_name'])->find();
  164. $personnel_yxsh = Db::name('personnel')->where('name','=',$order['examine'])->find();
  165. $this->assign('yxjb',$yxjb);
  166. $this->assign('yxsh',$yxsh);
  167. $this->assign('personnel_yxjb',$personnel_yxjb);
  168. $this->assign('personnel_yxsh',$personnel_yxsh);
  169. $this->view->assign('row', $row);
  170. return $this->view->fetch();
  171. }
  172. $params = $this->request->post('row/a');
  173. if (empty($params)) {
  174. $this->error(__('Parameter %s can not be empty', ''));
  175. }
  176. $params = $this->preExcludeFields($params);
  177. $result = false;
  178. Db::startTrans();
  179. try {
  180. //是否采用模型验证
  181. if ($this->modelValidate) {
  182. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  183. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  184. $row->validateFailException()->validate($validate);
  185. }
  186. $arr = [];
  187. foreach ($params as $k=>$v){
  188. if($v!=$row[$k]) {
  189. switch ($k){
  190. case 'no':
  191. $arr['field'] = '编号';
  192. $arr['before'] = $row['no']; //写入订单管理日志表 查看修改前的字段值
  193. $arr['after'] = $params['no']; //写入订单管理日志表 查看修改后的字段值
  194. // continue;
  195. break;
  196. case 'customer':
  197. $arr['field'] = '订货单位';
  198. $arr['before'] = $row['customer'];
  199. $arr['after'] = $params['customer'];
  200. break;
  201. case 'product':
  202. $arr['field'] = '品名';
  203. $arr['before'] = $row['product'];
  204. $arr['after'] = $params['product'];
  205. break;
  206. case 'specs':
  207. $arr['field'] = '包装规格';
  208. $arr['before'] = $row['specs'];
  209. $arr['after'] = $params['specs'];
  210. break;
  211. case 'unit':
  212. $arr['field'] = '单位';
  213. $arr['before'] = $row['unit'];
  214. $arr['after'] = $params['unit'];
  215. break;
  216. case 'number':
  217. $arr['field'] = '数量';
  218. $arr['before'] = $row['number'];
  219. $arr['after'] = $params['number'];
  220. break;
  221. case 'price':
  222. $arr['field'] = '单价';
  223. $arr['before'] = $row['price'];
  224. $arr['after'] = $params['price'];
  225. break;
  226. case 'delivery_date':
  227. $arr['field'] = '交货期';
  228. $arr['before'] = $row['delivery_date'];
  229. $arr['after'] = $params['delivery_date'];
  230. break;
  231. case 'remark':
  232. $arr['field'] = '备注';
  233. $arr['before'] = $row['remark'];
  234. $arr['after'] = $params['remark'];
  235. break;
  236. case 'date':
  237. $arr['field'] = '日期';
  238. $arr['before'] = $row['date'];
  239. $arr['after'] = $params['date'];
  240. break;
  241. case 'user_name':
  242. $arr['field'] = '经办';
  243. $arr['before'] = $row['user_name'];
  244. $arr['after'] = $params['user_name'];
  245. break;
  246. case 'examine':
  247. $arr['field'] = '审核';
  248. $arr['before'] = $row['examine'];
  249. $arr['after'] = $params['examine'];
  250. break;
  251. case 'status':
  252. $arr['field'] = '状态';
  253. if ($row['status'] == 1) {
  254. $arr['before'] = '计划中';
  255. if($params['status'] == 2){
  256. $arr['after'] = '生产中';
  257. }else if($params['status'] == 3){
  258. $arr['after'] = '已完成';
  259. }else if($params['status'] == 4){
  260. $arr['after'] = '待申购';
  261. }
  262. }
  263. if ($row['status'] == 2) {
  264. $arr['before'] = '生产中';
  265. if($params['status'] == 1){
  266. $arr['after'] = '计划中';
  267. }else if($params['status'] == 3){
  268. $arr['after'] = '已完成';
  269. }else if($params['status'] == 4){
  270. $arr['after'] = '待申购';
  271. }
  272. }
  273. if ($row['status'] == 3) {
  274. $arr['before'] = '已完成';
  275. if($params['status'] == 2){
  276. $arr['after'] = '生产中';
  277. }else if($params['status'] == 1){
  278. $arr['after'] = '计划中';
  279. }else if($params['status'] == 4){
  280. $arr['after'] = '待申购';
  281. }
  282. }
  283. if ($row['status'] == 4) {
  284. $arr['before'] = '待申购';
  285. if($params['status'] == 2){
  286. $arr['after'] = '生产中';
  287. }else if($params['status'] == 1){
  288. $arr['after'] = '计划中';
  289. }else if($params['status'] == 3){
  290. $arr['after'] = '已完成';
  291. }
  292. }
  293. break;
  294. case 'ese':
  295. $arr['field'] = '是否烟标';
  296. if ($row['ese'] == 1) {
  297. $arr['before'] = '烟标';
  298. if($params['ese'] == 2){
  299. $arr['after'] = '非烟标';
  300. }
  301. }
  302. if ($row['ese'] == 2) {
  303. $arr['before'] = '非烟标';
  304. if($params['ese'] == 1){
  305. $arr['after'] = '烟标';
  306. }
  307. }
  308. break;
  309. }
  310. $user_info = Session::get('admin');
  311. $arr['userid'] = $user_info['id'];//用户id
  312. $arr['username'] = $user_info['nickname'];//用户名
  313. $arr['oid'] = $row['id'];//订单表id
  314. $arr['type'] = 2;//订单日志类型 2 修改后
  315. $arr['orderstu'] = 2;//订单状态
  316. $res[]=$arr;
  317. }
  318. }
  319. $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值
  320. $result = $row->allowField(true)->save($params);
  321. Db::commit();
  322. } catch (ValidateException|PDOException|Exception $e) {
  323. Db::rollback();
  324. $this->error($e->getMessage());
  325. }
  326. if (false === $result) {
  327. $this->error(__('No rows were updated'));
  328. }
  329. $this->success();
  330. }
  331. //生产作业票
  332. public function task(){
  333. $ids = input('ids');//获取订单id
  334. if (!$ids) {
  335. $this->error(__('No Results were found'));
  336. }
  337. if (false === $this->request->isPost()) {
  338. //生产部=>开票人=>审核人
  339. $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
  340. $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
  341. $this->assign('sckp',$sckp);
  342. $this->assign('scsh',$scsh);
  343. $order = Db::name('order')->where('id',$ids)->find();
  344. $map = [];
  345. $map['name'] = array('like','%'.$order['product'].'%');
  346. $map['examine_status'] = 2;
  347. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  348. $res = [];
  349. if (!empty($list)){
  350. $res = $this->get_repeat_data($list);
  351. }
  352. //查询该配方是否生产过
  353. // $list = Db::name('formula')->where($map)->find();
  354. // $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find();
  355. // $count = Db::name('task')->where('fid',$list['id'])->count();//生产次数
  356. // if($taskbach){
  357. // $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次';
  358. // }else{
  359. // $erro = '该配方未生产过';
  360. // }
  361. // $this->view->assign('erro',$erro);
  362. //批次号顺序新增
  363. $tastbach = Db::name('task')->order('bach desc')->find();
  364. $bach = $tastbach['bach'] +1;
  365. $this->view->assign('bach',$bach);
  366. $this->view->assign('ids',$ids);
  367. $this->view->assign('row', $res);
  368. $this->view->assign('machineList',\app\admin\model\Machine::select());
  369. return $this->view->fetch();
  370. }
  371. }
  372. //获取二维数组的重复数据
  373. function get_repeat_data($array){
  374. //计算出数组的总数量
  375. $count = count($array);
  376. $num = [];//算出那个值是不要的 去掉就行
  377. for($i=0;$i<$count;$i++){
  378. $i_data = $array[$i]['name'];
  379. for($j=$i+1;$j<$count;$j++){
  380. $j_data = $array[$j]['name'];
  381. if($i_data == $j_data){
  382. $i_version = substr($array[$i]['version'],1);
  383. $j_version = substr($array[$j]['version'],1);
  384. if ($j_version > $i_version){
  385. $num[$i] = $i;
  386. }else{
  387. $num[$i] = $j;
  388. }
  389. }
  390. }
  391. }
  392. foreach ($num as $key=>$value){
  393. unset($array[$value]);
  394. }
  395. $result = array_values($array);
  396. return $result;
  397. }
  398. //关联客户管理中:客户名称 => 订货单位
  399. public function customer_customer(){
  400. $params = $this->request->post();
  401. $user_info = Session::get('admin');
  402. $map = [];
  403. if($user_info['id'] !== 1){
  404. $map['company_id'] = $user_info['company_id'];
  405. }
  406. if ($params){
  407. $customer = Db::name('customer')->where('customer_name','like','%'.$params['q_word'][0].'%')->order('id desc')->where($map)->select();
  408. // $customer = Db::name('customer')->where('customer_name','like','%'.$params['q_word'][0].'%')->order('id desc')->where($map)->limit(20)->select();
  409. }else{
  410. $customer = Db::name('customer')->where('customer_name','neq','')->where($map)->field('id,customer_name')->order('id desc')->select();
  411. }
  412. $total = count($customer);
  413. $result = ['total'=>$total,'rows'=>$customer];
  414. return json($result);
  415. }
  416. // public function customer_customer(){
  417. // $params = input('customer');
  418. // $user_info = Session::get('admin');
  419. // $map = [];
  420. // if($user_info['id'] !== 1){
  421. // $map['company_id'] = $user_info['company_id'];
  422. // }
  423. // if ($params){
  424. // $customer = Db::name('customer')->where('customer_name','like','%'.$params.'%')->where($map)->select();
  425. // }else{
  426. // $customer = Db::name('customer')->where('customer_name','neq','')->where($map)->field('id,customer_name')->order('id desc')->select();
  427. // }
  428. //// $result = ['rows'=>$customer];
  429. //
  430. // $total = count($customer);
  431. // $result = ['total'=>$total,'rows'=>$customer];
  432. // return json($result);
  433. // }
  434. //关联订单品名
  435. // public function order_product(){
  436. // $params = input('product');
  437. // if ($params){
  438. // $customer = Db::name('order')->where('product','like','%'.$params.'%')->select();
  439. // }else{
  440. // $customer = Db::name('order')->where('product','neq','')->field('id,product')->limit(20)->select();
  441. // }
  442. // $total = count($customer);
  443. // $result = ['total'=>$total,'rows'=>$customer];
  444. // return json($result);
  445. // }
  446. }