Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. $arr['after'] = '计划中';
  223. }
  224. if ($row['status'] == 2) {
  225. $arr['before'] = '生产中';
  226. $arr['after'] = '生产中';
  227. }
  228. if ($row['status'] == 3) {
  229. $arr['before'] = '已完成';
  230. $arr['after'] = '已完成';
  231. }
  232. continue;
  233. }
  234. // if ($k == 'no') {
  235. // $arr['field'] = '编号';
  236. // $arr['before'] = $row['no'];//写入订单管理日志表 查看修改前的字段值
  237. // $arr['after'] = $params['no'];//写入订单管理日志表 查看修改后的字段值
  238. // } elseif ($k == 'customer') {
  239. // $arr['field'] = '订货单位';
  240. // $arr['before'] = $row['customer'];
  241. // $arr['after'] = $params['customer'];
  242. // } elseif ($k == 'product') {
  243. // $arr['field'] = '品名';
  244. // $arr['before'] = $row['product'];
  245. // $arr['after'] = $params['product'];
  246. // } elseif ($k == 'specs') {
  247. // $arr['field'] = '包装规格';
  248. // $arr['before'] = $row['specs'];
  249. // $arr['after'] = $params['specs'];
  250. // } elseif ($k == 'unit') {
  251. // $arr['field'] = '单位';
  252. // $arr['before'] = $row['unit'];
  253. // $arr['after'] = $params['unit'];
  254. // } elseif ($k == 'number') {
  255. // $arr['field'] = '数量';
  256. // $arr['before'] = $row['number'];
  257. // $arr['after'] = $params['number'];
  258. // } elseif ($k == 'price') {
  259. // $arr['field'] = '单价';
  260. // $arr['add_text'] = $row['price'];
  261. // $arr['after'] = $params['price'];
  262. // } elseif ($k == 'delivery_date') {
  263. // $arr['field'] = '交货期';
  264. // $arr['before'] = $row['delivery_date'];
  265. // $arr['after'] = $params['delivery_date'];
  266. // } elseif ($k == 'remark') {
  267. // $arr['field'] = '备注';
  268. // $arr['before'] = $row['remark'];
  269. // $arr['after'] = $params['remark'];
  270. // } elseif ($k == 'date') {
  271. // $arr['field'] = '日期';
  272. // $arr['before'] = $row['date'];
  273. // $arr['after'] = $params['date'];
  274. // } elseif ($k == 'user_name') {
  275. // $arr['field'] = '经办';
  276. // $arr['before'] = $row['user_name'];
  277. // $arr['after'] = $params['user_name'];
  278. // } elseif ($k == 'examine') {
  279. // $arr['field'] = '审核';
  280. // $arr['before'] = $row['examine'];
  281. // $arr['after'] = $params['examine'];
  282. // } elseif ($k == 'status') {
  283. // $arr['field'] = '状态';
  284. // if( $row['status'] == 1){
  285. // $arr['before'] = '计划中';
  286. // $arr['after'] = '计划中';
  287. // }
  288. // if( $row['status'] == 2){
  289. // $arr['before'] = '生产中';
  290. // $arr['after'] = '生产中';
  291. // }
  292. // if( $row['status'] == 3){
  293. // $arr['before'] = '已完成';
  294. // $arr['after'] = '已完成';
  295. // }
  296. // }
  297. $user_info = Session::get('admin');
  298. $arr['userid'] = $user_info['id'];//用户id
  299. $arr['username'] = $user_info['nickname'];//用户名
  300. $arr['oid'] = $row['id'];//订单表id
  301. $arr['type'] = 2;//订单日志类型 2 修改后
  302. $arr['orderstu'] = 2;//订单状态
  303. $res[]=$arr;
  304. }
  305. }
  306. $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值
  307. $result = $row->allowField(true)->save($params);
  308. Db::commit();
  309. } catch (ValidateException|PDOException|Exception $e) {
  310. Db::rollback();
  311. $this->error($e->getMessage());
  312. }
  313. if (false === $result) {
  314. $this->error(__('No rows were updated'));
  315. }
  316. $this->success();
  317. }
  318. //生产作业票
  319. public function task(){
  320. $ids = input('ids');
  321. if (!$ids) {
  322. $this->error(__('No Results were found'));
  323. }
  324. if (false === $this->request->isPost()) {
  325. $order = Db::name('order')->where('id',$ids)->find();
  326. $map = [];
  327. $map['name'] = array('like','%'.$order['product'].'%');
  328. $map['examine_status'] = 2;
  329. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  330. $res = [];
  331. if (!empty($list)){
  332. $res = $this->get_repeat_data($list);
  333. }
  334. // halt($res);die;
  335. $this->view->assign('ids',$ids);
  336. $this->view->assign('row', $res);
  337. return $this->view->fetch();
  338. }
  339. }
  340. //获取二维数组的重复数据
  341. function get_repeat_data($array){
  342. //计算出数组的总数量
  343. $count = count($array);
  344. $num = [];//算出那个值是不要的 去掉就行
  345. for($i=0;$i<$count;$i++){
  346. $i_data = $array[$i]['name'];
  347. for($j=$i+1;$j<$count;$j++){
  348. $j_data = $array[$j]['name'];
  349. if($i_data == $j_data){
  350. $i_version = substr($array[$i]['version'],1);
  351. $j_version = substr($array[$j]['version'],1);
  352. if ($j_version > $i_version){
  353. $num[$i] = $i;
  354. }else{
  355. $num[$i] = $j;
  356. }
  357. }
  358. }
  359. }
  360. foreach ($num as $key=>$value){
  361. unset($array[$value]);
  362. }
  363. $result = array_values($array);
  364. return $result;
  365. }
  366. }