Order.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. //设置过滤方法
  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. {
  73. if (false === $this->request->isPost()) {
  74. return $this->view->fetch();
  75. }
  76. $params = $this->request->post('row/a');
  77. if (empty($params)) {
  78. $this->error(__('Parameter %s can not be empty', ''));
  79. }
  80. $params = $this->preExcludeFields($params);
  81. $user_info = Session::get('admin');
  82. $params['user_id'] = $user_info['id'];
  83. $params['company_id'] = $user_info['company_id'];
  84. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  85. $params[$this->dataLimitField] = $this->auth->id;
  86. }
  87. $result = false;
  88. Db::startTrans();
  89. try {
  90. //是否采用模型验证
  91. if ($this->modelValidate) {
  92. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  93. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  94. $this->model->validateFailException()->validate($validate);
  95. }
  96. $result = $this->model->allowField(true)->save($params);
  97. $arr['oid'] = $this->model->getLastInsID();
  98. $arr['userid'] = $user_info['id'];
  99. $arr['username'] = $user_info['nickname'];
  100. $arr['type'] = 1;
  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. public function edit($ids = null)
  113. {
  114. $row = $this->model->get($ids);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $adminIds = $this->getDataLimitAdminIds();
  119. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  120. $this->error(__('You have no permission'));
  121. }
  122. if (false === $this->request->isPost()) {
  123. $this->view->assign('row', $row);
  124. return $this->view->fetch();
  125. }
  126. $params = $this->request->post('row/a');
  127. if (empty($params)) {
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. $params = $this->preExcludeFields($params);
  131. $result = false;
  132. Db::startTrans();
  133. try {
  134. //是否采用模型验证
  135. if ($this->modelValidate) {
  136. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  137. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  138. $row->validateFailException()->validate($validate);
  139. }
  140. $arr = [];
  141. foreach ($params as $k=>$v){
  142. if($v!=$row[$k]) {
  143. if ($k == 'no') {
  144. $arr['field'] = '编号';
  145. } elseif ($k == 'customer') {
  146. $arr['field'] = '订货单位';
  147. } elseif ($k == 'product') {
  148. $arr['field'] = '品名';
  149. } elseif ($k == 'specs') {
  150. $arr['field'] = '包装规格';
  151. } elseif ($k == 'unit') {
  152. $arr['field'] = '单位';
  153. } elseif ($k == 'number') {
  154. $arr['field'] = '数量';
  155. } elseif ($k == 'price') {
  156. $arr['field'] = '单价';
  157. } elseif ($k == 'delivery_date') {
  158. $arr['field'] = '交货期';
  159. } elseif ($k == 'remark') {
  160. $arr['field'] = '备注';
  161. } elseif ($k == 'date') {
  162. $arr['field'] = '日期';
  163. } elseif ($k == 'user_name') {
  164. $arr['field'] = '经办';
  165. } elseif ($k == 'examine') {
  166. $arr['field'] = '审核';
  167. } elseif ($k == 'status') {
  168. $arr['field'] = '状态';
  169. }
  170. $user_info = Session::get('admin');
  171. $arr['userid'] = $user_info['id'];
  172. $arr['username'] = $user_info['nickname'];
  173. $arr['oid'] = $row['id'];
  174. $arr['type'] = 2;
  175. $res[]=$arr;
  176. }
  177. }
  178. $this->logmodel->saveAll($res);
  179. $result = $row->allowField(true)->save($params);
  180. Db::commit();
  181. } catch (ValidateException|PDOException|Exception $e) {
  182. Db::rollback();
  183. $this->error($e->getMessage());
  184. }
  185. if (false === $result) {
  186. $this->error(__('No rows were updated'));
  187. }
  188. $this->success();
  189. }
  190. //生产作业票
  191. public function task(){
  192. $ids = input('ids');
  193. if (!$ids) {
  194. $this->error(__('No Results were found'));
  195. }
  196. if (false === $this->request->isPost()) {
  197. $order = Db::name('order')->where('id',$ids)->find();
  198. $map = [];
  199. $map['name'] = array('like','%'.$order['product'].'%');
  200. $map['examine_status'] = 2;
  201. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  202. $res = [];
  203. if (!empty($list)){
  204. $res = $this->get_repeat_data($list);
  205. }
  206. // halt($res);die;
  207. $this->view->assign('ids',$ids);
  208. $this->view->assign('row', $res);
  209. return $this->view->fetch();
  210. }
  211. }
  212. //获取二维数组的重复数据
  213. function get_repeat_data($array){
  214. //计算出数组的总数量
  215. $count = count($array);
  216. $num = [];//算出那个值是不要的 去掉就行
  217. for($i=0;$i<$count;$i++){
  218. $i_data = $array[$i]['name'];
  219. for($j=$i+1;$j<$count;$j++){
  220. $j_data = $array[$j]['name'];
  221. if($i_data == $j_data){
  222. $i_version = substr($array[$i]['version'],1);
  223. $j_version = substr($array[$j]['version'],1);
  224. if ($j_version > $i_version){
  225. $num[$i] = $i;
  226. }else{
  227. $num[$i] = $j;
  228. }
  229. }
  230. }
  231. }
  232. foreach ($num as $key=>$value){
  233. unset($array[$value]);
  234. }
  235. $result = array_values($array);
  236. return $result;
  237. }
  238. }