Order.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. $arr['orderstu'] = 1;
  102. $this->model->save($arr);/////log
  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. public function stockad(){//传达到库存管理 如果数据库有新增数据返回1
  114. // $auth_group = Db::name('auth_group')->where('name','仓库')->find();
  115. // if($auth_group['name']=='仓库'){//是不是仓库管理员
  116. $order = Db::name('order')->where('orderstu',1)->select();
  117. foreach ($order as $v=>$k){
  118. if($k['orderstu']){
  119. return $k['orderstu'];
  120. }else{
  121. return '';
  122. }
  123. }
  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. if ($k == 'no') {
  158. $arr['field'] = '编号';
  159. } elseif ($k == 'customer') {
  160. $arr['field'] = '订货单位';
  161. } elseif ($k == 'product') {
  162. $arr['field'] = '品名';
  163. } elseif ($k == 'specs') {
  164. $arr['field'] = '包装规格';
  165. } elseif ($k == 'unit') {
  166. $arr['field'] = '单位';
  167. } elseif ($k == 'number') {
  168. $arr['field'] = '数量';
  169. } elseif ($k == 'price') {
  170. $arr['field'] = '单价';
  171. } elseif ($k == 'delivery_date') {
  172. $arr['field'] = '交货期';
  173. } elseif ($k == 'remark') {
  174. $arr['field'] = '备注';
  175. } elseif ($k == 'date') {
  176. $arr['field'] = '日期';
  177. } elseif ($k == 'user_name') {
  178. $arr['field'] = '经办';
  179. } elseif ($k == 'examine') {
  180. $arr['field'] = '审核';
  181. } elseif ($k == 'status') {
  182. $arr['field'] = '状态';
  183. }
  184. $user_info = Session::get('admin');
  185. $arr['userid'] = $user_info['id'];
  186. $arr['username'] = $user_info['nickname'];
  187. $arr['oid'] = $row['id'];
  188. $arr['type'] = 2;
  189. $res[]=$arr;
  190. }
  191. }
  192. $this->model->saveAll($res);////log
  193. $result = $row->allowField(true)->save($params);
  194. Db::commit();
  195. } catch (ValidateException|PDOException|Exception $e) {
  196. Db::rollback();
  197. $this->error($e->getMessage());
  198. }
  199. if (false === $result) {
  200. $this->error(__('No rows were updated'));
  201. }
  202. $this->success();
  203. }
  204. //生产作业票
  205. public function task(){
  206. $ids = input('ids');
  207. if (!$ids) {
  208. $this->error(__('No Results were found'));
  209. }
  210. if (false === $this->request->isPost()) {
  211. $order = Db::name('order')->where('id',$ids)->find();
  212. $map = [];
  213. $map['name'] = array('like','%'.$order['product'].'%');
  214. $map['examine_status'] = 2;
  215. $list = Db::name('formula')->where($map)->field('id,name,version')->order('id desc')->select();
  216. $res = [];
  217. if (!empty($list)){
  218. $res = $this->get_repeat_data($list);
  219. }
  220. // halt($res);die;
  221. $this->view->assign('ids',$ids);
  222. $this->view->assign('row', $res);
  223. return $this->view->fetch();
  224. }
  225. }
  226. //获取二维数组的重复数据
  227. function get_repeat_data($array){
  228. //计算出数组的总数量
  229. $count = count($array);
  230. $num = [];//算出那个值是不要的 去掉就行
  231. for($i=0;$i<$count;$i++){
  232. $i_data = $array[$i]['name'];
  233. for($j=$i+1;$j<$count;$j++){
  234. $j_data = $array[$j]['name'];
  235. if($i_data == $j_data){
  236. $i_version = substr($array[$i]['version'],1);
  237. $j_version = substr($array[$j]['version'],1);
  238. if ($j_version > $i_version){
  239. $num[$i] = $i;
  240. }else{
  241. $num[$i] = $j;
  242. }
  243. }
  244. }
  245. }
  246. foreach ($num as $key=>$value){
  247. unset($array[$value]);
  248. }
  249. $result = array_values($array);
  250. return $result;
  251. }
  252. }