Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. break;
  164. case 'customer':
  165. $arr['field'] = '订货单位';
  166. $arr['before'] = $row['customer'];
  167. $arr['after'] = $params['customer'];
  168. break;
  169. case 'product':
  170. $arr['field'] = '品名';
  171. $arr['before'] = $row['product'];
  172. $arr['after'] = $params['product'];
  173. break;
  174. case 'specs':
  175. $arr['field'] = '包装规格';
  176. $arr['before'] = $row['specs'];
  177. $arr['after'] = $params['specs'];
  178. break;
  179. case 'unit':
  180. $arr['field'] = '单位';
  181. $arr['before'] = $row['unit'];
  182. $arr['after'] = $params['unit'];
  183. break;
  184. case 'number':
  185. $arr['field'] = '数量';
  186. $arr['before'] = $row['number'];
  187. $arr['after'] = $params['number'];
  188. break;
  189. case 'price':
  190. $arr['field'] = '单价';
  191. $arr['before'] = $row['price'];
  192. $arr['after'] = $params['price'];
  193. break;
  194. case 'delivery_date':
  195. $arr['field'] = '交货期';
  196. $arr['before'] = $row['delivery_date'];
  197. $arr['after'] = $params['delivery_date'];
  198. break;
  199. case 'remark':
  200. $arr['field'] = '备注';
  201. $arr['before'] = $row['remark'];
  202. $arr['after'] = $params['remark'];
  203. break;
  204. case 'date':
  205. $arr['field'] = '日期';
  206. $arr['before'] = $row['date'];
  207. $arr['after'] = $params['date'];
  208. break;
  209. case 'user_name':
  210. $arr['field'] = '经办';
  211. $arr['before'] = $row['user_name'];
  212. $arr['after'] = $params['user_name'];
  213. break;
  214. case 'examine':
  215. $arr['field'] = '审核';
  216. $arr['before'] = $row['examine'];
  217. $arr['after'] = $params['examine'];
  218. break;
  219. case 'status':
  220. $arr['field'] = '状态';
  221. if ($row['status'] == 1) {
  222. $arr['before'] = '计划中';
  223. if($params['status'] == 2){
  224. $arr['after'] = '生产中';
  225. }else if($params['status'] == 3){
  226. $arr['after'] = '已完成';
  227. }
  228. }
  229. if ($row['status'] == 2) {
  230. $arr['before'] = '生产中';
  231. if($params['status'] == 1){
  232. $arr['after'] = '计划中';
  233. }else if($params['status'] == 3){
  234. $arr['after'] = '已完成';
  235. }
  236. }
  237. if ($row['status'] == 3) {
  238. $arr['before'] = '已完成';
  239. if($params['status'] == 2){
  240. $arr['after'] = '生产中';
  241. }else if($params['status'] == 1){
  242. $arr['after'] = '计划中';
  243. }
  244. }
  245. break;
  246. }
  247. $user_info = Session::get('admin');
  248. $arr['userid'] = $user_info['id'];//用户id
  249. $arr['username'] = $user_info['nickname'];//用户名
  250. $arr['oid'] = $row['id'];//订单表id
  251. $arr['type'] = 2;//订单日志类型 2 修改后
  252. $arr['orderstu'] = 2;//订单状态
  253. $res[]=$arr;
  254. }
  255. }
  256. $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值
  257. $result = $row->allowField(true)->save($params);
  258. Db::commit();
  259. } catch (ValidateException|PDOException|Exception $e) {
  260. Db::rollback();
  261. $this->error($e->getMessage());
  262. }
  263. if (false === $result) {
  264. $this->error(__('No rows were updated'));
  265. }
  266. $this->success();
  267. }
  268. //生产作业票
  269. public function task(){
  270. $ids = input('ids');
  271. if (!$ids) {
  272. $this->error(__('No Results were found'));
  273. }
  274. if (false === $this->request->isPost()) {
  275. $order = Db::name('order')->where('id',$ids)->find();
  276. $map = [];
  277. $map['name'] = array('like','%'.$order['product'].'%');
  278. $map['examine_status'] = 2;
  279. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  280. $res = [];
  281. if (!empty($list)){
  282. $res = $this->get_repeat_data($list);
  283. }
  284. // halt($res);die;
  285. $this->view->assign('ids',$ids);
  286. $this->view->assign('row', $res);
  287. return $this->view->fetch();
  288. }
  289. }
  290. //获取二维数组的重复数据
  291. function get_repeat_data($array){
  292. //计算出数组的总数量
  293. $count = count($array);
  294. $num = [];//算出那个值是不要的 去掉就行
  295. for($i=0;$i<$count;$i++){
  296. $i_data = $array[$i]['name'];
  297. for($j=$i+1;$j<$count;$j++){
  298. $j_data = $array[$j]['name'];
  299. if($i_data == $j_data){
  300. $i_version = substr($array[$i]['version'],1);
  301. $j_version = substr($array[$j]['version'],1);
  302. if ($j_version > $i_version){
  303. $num[$i] = $i;
  304. }else{
  305. $num[$i] = $j;
  306. }
  307. }
  308. }
  309. }
  310. foreach ($num as $key=>$value){
  311. unset($array[$value]);
  312. }
  313. $result = array_values($array);
  314. return $result;
  315. }
  316. }