Order.php 17 KB

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