Order.php 13 KB

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