Order.php 17 KB

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