Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. //营销部=>经办
  153. $yxjb = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxjb")->order('name desc')->select();
  154. //营销部=>审核
  155. $yxsh = Db::name('personnel')->where('bid',"=",1)->where('position','=',"yxsh")->order('name desc')->select();
  156. $this->assign('yxjb',$yxjb);
  157. $this->assign('yxsh',$yxsh);
  158. $this->view->assign('row', $row);
  159. return $this->view->fetch();
  160. }
  161. $params = $this->request->post('row/a');
  162. if (empty($params)) {
  163. $this->error(__('Parameter %s can not be empty', ''));
  164. }
  165. $params = $this->preExcludeFields($params);
  166. $result = false;
  167. Db::startTrans();
  168. try {
  169. //是否采用模型验证
  170. if ($this->modelValidate) {
  171. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  172. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  173. $row->validateFailException()->validate($validate);
  174. }
  175. $arr = [];
  176. foreach ($params as $k=>$v){
  177. if($v!=$row[$k]) {
  178. switch ($k){
  179. case 'no':
  180. $arr['field'] = '编号';
  181. $arr['before'] = $row['no']; //写入订单管理日志表 查看修改前的字段值
  182. $arr['after'] = $params['no']; //写入订单管理日志表 查看修改后的字段值
  183. // continue;
  184. break;
  185. case 'customer':
  186. $arr['field'] = '订货单位';
  187. $arr['before'] = $row['customer'];
  188. $arr['after'] = $params['customer'];
  189. break;
  190. case 'product':
  191. $arr['field'] = '品名';
  192. $arr['before'] = $row['product'];
  193. $arr['after'] = $params['product'];
  194. break;
  195. case 'specs':
  196. $arr['field'] = '包装规格';
  197. $arr['before'] = $row['specs'];
  198. $arr['after'] = $params['specs'];
  199. break;
  200. case 'unit':
  201. $arr['field'] = '单位';
  202. $arr['before'] = $row['unit'];
  203. $arr['after'] = $params['unit'];
  204. break;
  205. case 'number':
  206. $arr['field'] = '数量';
  207. $arr['before'] = $row['number'];
  208. $arr['after'] = $params['number'];
  209. break;
  210. case 'price':
  211. $arr['field'] = '单价';
  212. $arr['before'] = $row['price'];
  213. $arr['after'] = $params['price'];
  214. break;
  215. case 'delivery_date':
  216. $arr['field'] = '交货期';
  217. $arr['before'] = $row['delivery_date'];
  218. $arr['after'] = $params['delivery_date'];
  219. break;
  220. case 'remark':
  221. $arr['field'] = '备注';
  222. $arr['before'] = $row['remark'];
  223. $arr['after'] = $params['remark'];
  224. break;
  225. case 'date':
  226. $arr['field'] = '日期';
  227. $arr['before'] = $row['date'];
  228. $arr['after'] = $params['date'];
  229. break;
  230. case 'user_name':
  231. $arr['field'] = '经办';
  232. $arr['before'] = $row['user_name'];
  233. $arr['after'] = $params['user_name'];
  234. break;
  235. case 'examine':
  236. $arr['field'] = '审核';
  237. $arr['before'] = $row['examine'];
  238. $arr['after'] = $params['examine'];
  239. break;
  240. case 'status':
  241. $arr['field'] = '状态';
  242. if ($row['status'] == 1) {
  243. $arr['before'] = '计划中';
  244. if($params['status'] == 2){
  245. $arr['after'] = '生产中';
  246. }else if($params['status'] == 3){
  247. $arr['after'] = '已完成';
  248. }
  249. }
  250. if ($row['status'] == 2) {
  251. $arr['before'] = '生产中';
  252. if($params['status'] == 1){
  253. $arr['after'] = '计划中';
  254. }else if($params['status'] == 3){
  255. $arr['after'] = '已完成';
  256. }
  257. }
  258. if ($row['status'] == 3) {
  259. $arr['before'] = '已完成';
  260. if($params['status'] == 2){
  261. $arr['after'] = '生产中';
  262. }else if($params['status'] == 1){
  263. $arr['after'] = '计划中';
  264. }
  265. }
  266. break;
  267. }
  268. $user_info = Session::get('admin');
  269. $arr['userid'] = $user_info['id'];//用户id
  270. $arr['username'] = $user_info['nickname'];//用户名
  271. $arr['oid'] = $row['id'];//订单表id
  272. $arr['type'] = 2;//订单日志类型 2 修改后
  273. $arr['orderstu'] = 2;//订单状态
  274. $res[]=$arr;
  275. }
  276. }
  277. $this->logmodel->saveAll($res);//添加订单日志表,查哪个用户在什么时间修改哪个字段值
  278. $result = $row->allowField(true)->save($params);
  279. Db::commit();
  280. } catch (ValidateException|PDOException|Exception $e) {
  281. Db::rollback();
  282. $this->error($e->getMessage());
  283. }
  284. if (false === $result) {
  285. $this->error(__('No rows were updated'));
  286. }
  287. $this->success();
  288. }
  289. //生产作业票
  290. public function task(){
  291. $ids = input('ids');
  292. if (!$ids) {
  293. $this->error(__('No Results were found'));
  294. }
  295. if (false === $this->request->isPost()) {
  296. //生产部=>开票人
  297. $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
  298. //生产部=>审核人
  299. $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
  300. $this->assign('sckp',$sckp);
  301. $this->assign('scsh',$scsh);
  302. $order = Db::name('order')->where('id',$ids)->find();
  303. $map = [];
  304. $map['name'] = array('like','%'.$order['product'].'%');
  305. $map['examine_status'] = 2;
  306. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  307. $res = [];
  308. if (!empty($list)){
  309. $res = $this->get_repeat_data($list);
  310. }
  311. //批次号顺序新增
  312. $tastbach = Db::name('task')->order('bach desc')->find();
  313. $bach = $tastbach['bach'] +1;
  314. $this->view->assign('bach',$bach);
  315. // halt($res);die;
  316. $this->view->assign('ids',$ids);
  317. $this->view->assign('row', $res);
  318. return $this->view->fetch();
  319. }
  320. }
  321. //获取二维数组的重复数据
  322. function get_repeat_data($array){
  323. //计算出数组的总数量
  324. $count = count($array);
  325. $num = [];//算出那个值是不要的 去掉就行
  326. for($i=0;$i<$count;$i++){
  327. $i_data = $array[$i]['name'];
  328. for($j=$i+1;$j<$count;$j++){
  329. $j_data = $array[$j]['name'];
  330. if($i_data == $j_data){
  331. $i_version = substr($array[$i]['version'],1);
  332. $j_version = substr($array[$j]['version'],1);
  333. if ($j_version > $i_version){
  334. $num[$i] = $i;
  335. }else{
  336. $num[$i] = $j;
  337. }
  338. }
  339. }
  340. }
  341. foreach ($num as $key=>$value){
  342. unset($array[$value]);
  343. }
  344. $result = array_values($array);
  345. return $result;
  346. }
  347. //关联客户管理中:客户名称 => 订货单位
  348. public function customer_customer(){
  349. $params = input('customer');
  350. if ($params){
  351. $customer = Db::name('customer')->where('customer_name','like','%'.$params.'%')->select();
  352. }else{
  353. $customer = Db::name('customer')->where('customer_name','neq','')->field('id,customer_name')->limit(20)->select();
  354. }
  355. $result = ['rows'=>$customer];
  356. return json($result);
  357. }
  358. //关联订单品名
  359. public function order_product(){
  360. $params = input('product');
  361. if ($params){
  362. $customer = Db::name('order')->where('product','like','%'.$params.'%')->select();
  363. }else{
  364. $customer = Db::name('order')->where('product','neq','')->field('id,product')->limit(20)->select();
  365. }
  366. $total = count($customer);
  367. $result = ['total'=>$total,'rows'=>$customer];
  368. return json($result);
  369. }
  370. }