ApplyBatchController.class.php 11.28 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * author Gentle
  4. * intro 申请批次
  5. */
  6. namespace Admin\Controller;
  7. use Think\Page;
  8. class ApplyBatchController extends AdminController
  9. {
  10. /**
  11. * author Gentle
  12. * intro 类初始化方法
  13. * param
  14. * return
  15. */
  16. public function _initialize()
  17. {
  18. parent::_initialize(); // TODO: Change the autogenerated stub
  19. $this->uid = session('user_auth.uid');
  20. $this->redundancy_s = 0.1;//冗码率
  21. }
  22. /**
  23. * author Gentle
  24. * intro 获取用户组
  25. * param
  26. */
  27. public function getGroups()
  28. {
  29. $uid = $this->uid;
  30. $admin_access_model = D(Admin_access);
  31. $groups = $admin_access_model->where('uid=' . $uid)->getField('group');//1 超级管理 2 印刷厂 3 中烟
  32. return $groups;
  33. }
  34. /**
  35. * author Gentle
  36. * intro 申请批次首页
  37. * param
  38. */
  39. public function index()
  40. {
  41. /**
  42. * @author Gentle
  43. * @tro 遍历展示产品名称、盒型
  44. * @time 2017年9月11日09:35:58
  45. */
  46. if (session('user_auth.uid') == 1) {
  47. $this->success('暂不支持管理员操作');
  48. exit;
  49. }
  50. $printer_list = M(Admin_access)
  51. ->join('__ADMIN_USER__ ON __ADMIN_USER__.id = __ADMIN_ACCESS__.uid')
  52. ->field('QR_ADMIN_USER.id printer_id,QR_ADMIN_USER.nickname printer_name')
  53. ->where('QR_ADMIN_ACCESS.group=2')->order('QR_ADMIN_USER.id asc')->select();
  54. foreach ($printer_list as $val) {
  55. $printer[] = $val;
  56. }
  57. $factory_model = D(Factory);
  58. $list2 = $factory_model->field('id,name')->where('id>0')->order('id asc')->select();
  59. foreach ($list2 as $val) {
  60. $factory_result[] = $val;
  61. $factory_id[] = $val['id'];
  62. }
  63. $product_model = D(Product);
  64. $list = $product_model->field('id,name')->where('factory_id IN (' . implode(',', $factory_id) . ')')->order('id asc')->select();
  65. foreach ($list as $val) {
  66. $product_result[] = $val;
  67. $product_id[] = $val['id'];
  68. }
  69. //暂时不使用
  70. $list2 = M(Product_box_type)
  71. ->join('__BOX_TYPE__ ON __BOX_TYPE__.id = __PRODUCT_BOX_TYPE__.box_type_id')
  72. ->field('QR_BOX_TYPE.id type_id,QR_BOX_TYPE.name box_type_name')
  73. ->where('QR_PRODUCT_BOX_TYPE.product_id>0')->order('QR_PRODUCT_BOX_TYPE.id asc')->select();
  74. foreach ($list2 as $val) {
  75. $box_type_result[] = $val['type_id'];
  76. }
  77. $this->assign('printer', $printer);
  78. $this->assign('product_result', $product_result);
  79. $this->assign('factory_result', $factory_result);
  80. $this->assign('meta_title', '申请批次');
  81. $this->display();
  82. }
  83. /**
  84. * author Gentle
  85. * intro 申请批次列表页面
  86. * param
  87. */
  88. public function applyList()
  89. {
  90. /**
  91. * @author Gentle
  92. * @tro 遍历展示申请批次列表
  93. * @time 2017年9月12日14:17:17
  94. */
  95. //分页
  96. $bach = M(Bach);
  97. $uid = $this->uid;
  98. $groups = $this->GetGroups();
  99. if ($groups == '3') {
  100. $where = 'QR_BACH.owner_id=' . $uid;
  101. $page = new Page(
  102. $bach->where('owner_id=' . $uid)->count(),
  103. C('ADMIN_PAGE_ROWS')
  104. );
  105. } else if ($groups == '2') {
  106. $where = 'QR_BACH.printer_id=' . $uid;
  107. $page = new Page(
  108. $bach->where('printer_id=' . $uid)->count(),
  109. C('ADMIN_PAGE_ROWS')
  110. );
  111. } else {
  112. $where = 'QR_BACH.id > 0';
  113. $page = new Page(
  114. $bach->where('id>0')->count(),
  115. C('ADMIN_PAGE_ROWS')
  116. );
  117. }
  118. $show = $page->show();
  119. $bach_list = M(Bach)
  120. ->join('__ADMIN_USER__ ON __ADMIN_USER__.id = __BACH__.printer_id')
  121. ->field('QR_BACH.id bach_id,QR_BACH.name bach_name,QR_BACH.add_time,QR_ADMIN_USER.nickname printer_name')
  122. ->limit($page->firstRow . ',' . $page->listRows)
  123. ->where($where)->order('QR_BACH.id desc')->select();
  124. $task = M(Task);
  125. foreach ($bach_list as $value) {
  126. $task_ids[] = $task->field('id')->where('bach_id=' . $value['bach_id'])->order('id desc')->select();
  127. }
  128. foreach ($task_ids as $vals) {
  129. foreach ($vals as $val) {
  130. $task_list[] = M(Task)
  131. ->join('__FACTORY__ ON __FACTORY__.id=__TASK__.factory_id ')
  132. ->join('__ADMIN_USER__ ON __ADMIN_USER__.id = __TASK__.printer_id')
  133. ->join('__PRODUCT_BOX_TYPE__ ON __PRODUCT_BOX_TYPE__.id=__TASK__.product_id')
  134. ->join('__PRODUCT__ ON __PRODUCT__.id=__PRODUCT_BOX_TYPE__.product_id')
  135. ->join('__BOX_TYPE__ ON __BOX_TYPE__.id=__PRODUCT_BOX_TYPE__.box_type_id')
  136. ->field('QR_TASK.id task_id,QR_TASK.quality,QR_TASK.bach_id,QR_PRODUCT.name product_name,QR_BOX_TYPE.name box_type_name')->where('QR_TASK.id= ' . $val['id'])->select();
  137. }
  138. }
  139. foreach ($task_list as $val) {
  140. foreach ($val as $v) {
  141. $task_list2[] = $v;
  142. }
  143. }
  144. $this->assign('bach_page', $show);
  145. $this->assign('bach_list', $bach_list);
  146. $this->assign('task_list2', $task_list2);
  147. $this->assign('meta_title', '批次列表');
  148. $this->display();
  149. }
  150. /**
  151. * author Gentle
  152. * intro 任务列表页面
  153. * param
  154. */
  155. public function taskList()
  156. {
  157. /**
  158. * @author Gentle
  159. * @tro 遍历展示申请批次列表
  160. * @time 2017年9月15日13:17:17
  161. */
  162. if (empty($_GET['bach_id'])) {
  163. $this->error('批次id不能为空');
  164. }
  165. $bach_id = $_GET['bach_id'];
  166. //分页
  167. $task = M(Task);
  168. $page = new Page(
  169. $task->where('bach_id=' . $bach_id)->count(),
  170. C('ADMIN_PAGE_ROWS')
  171. );
  172. $show = $page->show();
  173. $task_ids = $task->field('id')->where('bach_id=' . $bach_id)->order('id desc')->select();
  174. foreach ($task_ids as $val) {
  175. $task_list[] = M(Task)
  176. ->join('__FACTORY__ ON __FACTORY__.id=__TASK__.factory_id ')
  177. ->join('__ADMIN_USER__ ON __ADMIN_USER__.id = __TASK__.printer_id')
  178. ->join('__PRODUCT_BOX_TYPE__ ON __PRODUCT_BOX_TYPE__.id=__TASK__.product_id')
  179. ->join('__PRODUCT__ ON __PRODUCT__.id=__PRODUCT_BOX_TYPE__.product_id')
  180. ->join('__BOX_TYPE__ ON __BOX_TYPE__.id=__PRODUCT_BOX_TYPE__.box_type_id')
  181. ->field('printer_id,QR_PRODUCT_BOX_TYPE.id product_id,QR_ADMIN_USER.nickname printer_name,QR_TASK.id task_id,QR_TASK.name task_name,QR_TASK.quality,QR_TASK.add_time,QR_TASK.is_verif,QR_TASK.bach_id,QR_TASK.status,QR_FACTORY.name factory_name,QR_PRODUCT.name product_name,QR_BOX_TYPE.name box_type_name')->where('QR_TASK.id= ' . $val['id'])->select();
  182. }
  183. foreach ($task_list as $val) {
  184. foreach ($val as $v) {
  185. $task_list2[] = $v;
  186. }
  187. }
  188. $this->assign('task_page', $show);
  189. $this->assign('task_list', $task_list2);
  190. $this->assign('meta_title', '任务列表');
  191. $this->display();
  192. }
  193. /**
  194. * author Gentle
  195. * intro 任务详情页面
  196. * param 2017年9月15日15:31:22
  197. */
  198. public function taskDetail()
  199. {
  200. /**
  201. * @author Gentle
  202. * @tro 批次详情页面展示
  203. * @time 2017年9月13日19:27:28
  204. */
  205. if (empty($_GET['task_id'])) {
  206. $this->error('任务id不能为空');
  207. }
  208. $task_id = $_GET['task_id'];
  209. $task_detail = M(Task)
  210. ->join('__FACTORY__ ON __FACTORY__.id=__TASK__.factory_id ')
  211. ->join('__PRODUCT_BOX_TYPE__ ON __PRODUCT_BOX_TYPE__.id=__TASK__.product_id')
  212. ->join('__PRODUCT__ ON __PRODUCT__.id=__PRODUCT_BOX_TYPE__.product_id')
  213. ->join('__BOX_TYPE__ ON __BOX_TYPE__.id=__PRODUCT_BOX_TYPE__.box_type_id')
  214. ->field('QR_TASK.id task_id,QR_TASK.name task_name,QR_TASK.bach_id,QR_TASK.quality,QR_TASK.redundancy,QR_TASK.add_time,QR_TASK.is_verif,QR_TASK.bach_id,QR_TASK.status,QR_TASK.checked_num,QR_TASK.wrong_num,QR_FACTORY.name factory_name,QR_PRODUCT.name product_name,QR_BOX_TYPE.name box_type_name')
  215. ->where('QR_TASK.id=' . $task_id)->order('QR_TASK.id asc')->select();
  216. /*
  217. foreach ($task_detail as $k=>$v){
  218. $task_ids=M('task')->where(array('name'=>$v['task_name']))->getField('id',true);
  219. if(in_array($v['task_id'],$task_ids)){
  220. }
  221. }*/
  222. $this->assign('task_detail', $task_detail);
  223. $this->assign('meta_title', '任务详情');
  224. $this->display();
  225. }
  226. /**
  227. * author Gentle
  228. * intro 申请批次方法
  229. * param
  230. * return bool
  231. */
  232. public function applyForm()
  233. {
  234. if (IS_POST) {
  235. $bach_object = D("Bach");
  236. $bach_product_object = D("Bach_product");
  237. $task_object = D("Task");
  238. //校验$_POST数据
  239. if (empty($_POST['product_id'])) {
  240. alert(array('info' => '产品不能为空', 'status' => 2));
  241. exit;
  242. }
  243. if (empty($_POST['factory_id'])) {
  244. alert(array('info' => '工厂名称不能为空', 'status' => 3));
  245. exit;
  246. }
  247. if (empty($_POST['printer_id'])) {
  248. alert(array('info' => '印刷厂名称不能为空', 'status' => 4));
  249. exit;
  250. }
  251. if (empty($_POST['code_num1']) or empty($_POST['code_num2'])) {
  252. alert(array('info' => '条盒、小包数量都不能小于0', 'status' => 5));
  253. exit;
  254. }
  255. $product_id = $_POST['product_id'];
  256. $factory_id = $_POST['factory_id'];
  257. $printer_id = $_POST['printer_id'];
  258. $is_verif = (int)$_POST['is_verif'];
  259. $remark = $_POST['remark'];
  260. $code_num = [
  261. 'code_num1' => $_POST['code_num1'],
  262. 'code_num2' => $_POST['code_num2']
  263. ];
  264. //查找产品、类别关联表
  265. $product_box_type_ids = M(Product_box_type)
  266. ->field('id')
  267. ->where('product_id=' . $product_id)->order('box_type_id asc')->select();
  268. foreach ($product_box_type_ids as $val) {
  269. $product_box_type_id[] = $val['id'];
  270. }
  271. $data = [];
  272. $data['name'] = 'MNYH_' . date("Ymdhi", time());
  273. $data['add_time'] = time();
  274. $data['remark'] = $remark;
  275. $data['printer_id'] = $printer_id;
  276. $data['owner_id'] = $this->uid;
  277. $param = [];
  278. $param['product_id'] = $product_id;
  279. $param['is_verif'] = $is_verif;
  280. $param['add_time'] = time();
  281. $param['remark'] = $remark;
  282. $param1 = $param;
  283. $param2 = $param;
  284. $param1['num'] = $code_num['code_num1'];
  285. $param1['product_id'] = $product_box_type_id[0];
  286. $param2['num'] = $code_num['code_num2'];
  287. $param2['product_id'] = $product_box_type_id[1];
  288. $add_param = [
  289. $param1,
  290. $param2
  291. ];
  292. $params = [];
  293. $params['name'] = $data['name'];
  294. $params['factory_id'] = $factory_id;
  295. $params['is_verif'] = $is_verif;
  296. $params['add_time'] = time();
  297. $params['remark'] = $remark;
  298. $params['status'] = '0';
  299. $params['printer_id'] = $printer_id;
  300. $params['owner_id'] = $this->uid;
  301. $params1 = $params;
  302. $params2 = $params;
  303. $params1['quality'] = $code_num['code_num1'];
  304. $params1['redundancy'] = $code_num['code_num1'] * $this->redundancy_s;
  305. $params1['product_id'] = $product_box_type_id[0];
  306. $params2['quality'] = $code_num['code_num2'];
  307. $params2['redundancy'] = $code_num['code_num2'] * $this->redundancy_s;
  308. $params2['product_id'] = $product_box_type_id[1];
  309. if ($data && $add_param) {
  310. $bach_id = $bach_object->add($data);
  311. $param1['bach_id'] = $bach_id;
  312. $param2['bach_id'] = $bach_id;
  313. $add_param = [
  314. $param1,
  315. $param2
  316. ];
  317. $bach_product_id = $bach_product_object->addAll($add_param);
  318. if ($bach_id && $bach_product_id) {
  319. $params1['bach_id'] = $bach_id;
  320. $params2['bach_id'] = $bach_id;
  321. $add_params = [
  322. $params1,
  323. $params2
  324. ];
  325. $task_id = $task_object->addAll($add_params);
  326. if ($task_id) {
  327. alert(array('info' => '申请成功', 'url' => "index.php?s=/Admin/ApplyBatch/applylist.html", 'status' => 1));
  328. } else {
  329. alert(array('info' => '申请失败', 'url' => "index.php?s=/Admin/ApplyBatch/Index.html", 'status' => 0));
  330. }
  331. } else {
  332. $this->error($bach_object->getError());
  333. }
  334. } else {
  335. $this->error($bach_object->getError());
  336. }
  337. } else {
  338. // 跳转到申请批次页面
  339. header("Location: {:U('ApplyBatch/applylist')}");
  340. }
  341. }
  342. /**
  343. * author Gentle
  344. * intro 批次补单申请页面
  345. * param
  346. */
  347. public function addTaskIndex()
  348. {
  349. if (empty($_GET['task_id'])) {
  350. $this->error('补单任务id不能为空');
  351. }
  352. $task_id = $_GET['task_id'];
  353. $task_model = D("Task");
  354. $tasks = $task_model->field('bach_id')->where('id=' . $task_id)->order('id asc')->find();
  355. $bach_id = $tasks['bach_id'];
  356. $bach_model = D("Bach");
  357. $bach_name = $bach_model->field('name')->where('id=' . $bach_id)->order('id asc')->find();
  358. $bach_contents['task_id'] = $task_id;
  359. $bach_contents['bach_id'] = $bach_id;
  360. $bach_contents['bach_name'] = $bach_name['name'];
  361. $task_list = $task_model->field('product_id')->where('bach_id=' . $bach_id)->order('id asc')->select();
  362. if (empty($task_list)) {
  363. $this->error('您未创建过该任务,请核对任务后操作!');
  364. }
  365. foreach ($task_list as $val) {
  366. $product_id[] = $val['product_id'];
  367. $product_id = array_unique($product_id);
  368. }
  369. foreach ($product_id as $val) {
  370. $product_contents[] = M(Box_type)
  371. ->join('__PRODUCT_BOX_TYPE__ ON __PRODUCT_BOX_TYPE__.box_type_id = __BOX_TYPE__.id')
  372. ->field('QR_PRODUCT_BOX_TYPE.id product_id,QR_BOX_TYPE.name box_type_name')
  373. ->where('QR_PRODUCT_BOX_TYPE.id=' . $val)->order('QR_PRODUCT_BOX_TYPE.id asc')->select();
  374. }
  375. foreach ($product_contents as $val) {
  376. foreach ($val as $v) {
  377. $product_contents_list[] = $v;
  378. }
  379. }
  380. $this->assign('bach_contents', $bach_contents);
  381. $this->assign('product_contents_list', $product_contents_list);
  382. $this->assign('meta_title', '任务补单申请');
  383. $this->display();
  384. }
  385. /**
  386. * author Gentle
  387. * intro 批次补单申请方法
  388. * param
  389. */
  390. public function addTaskForm()
  391. {
  392. if ($_POST) {
  393. //校验$_POST数据
  394. if (empty($_POST['bach_id'])) {
  395. alert(array('info' => '补单批次id不能为空', 'status' => 2));
  396. exit;
  397. }
  398. if (empty($_POST['product_id'])) {
  399. alert(array('info' => '补单产品盒型不能为空', 'status' => 3));
  400. exit;
  401. }
  402. if (empty($_POST['code_num'])) {
  403. alert(array('info' => '补单产品数量不能为空', 'status' => 4));
  404. exit;
  405. }
  406. $datas['bach_id'] = $_POST['bach_id'];
  407. $datas['product_id'] = $_POST['product_id'];
  408. $datas['quality'] = $_POST['code_num'];
  409. $datas['redundancy'] = $_POST['code_num'] * $this->redundancy_s;
  410. $datas['remark'] = $_POST['remark'];
  411. $datas['add_time'] = time();
  412. $task_model = D("Task");
  413. $task_contents = $task_model->field('name,factory_id,is_verif,printer_id,owner_id')->where('bach_id=' . $_POST['bach_id'])->order('id asc')->find();
  414. $data = array_merge($datas, $task_contents);
  415. $task_id = $task_model->add($data);
  416. if ($task_id) {
  417. alert(array('info' => '补单申请成功', 'url' => "index.php?s=/Admin/ApplyBatch/tasklist/bach_id/" . $_POST['bach_id'] . ".html", 'status' => 1));
  418. } else {
  419. alert(array('info' => '补单申请失败', 'url' => "index.php?s=/Admin/ApplyBatch/addTaskIndex/bach_id/" . $_POST['bach_id'] . ".html", 'status' => 0));
  420. }
  421. }
  422. }
  423. /**
  424. * author Gentle
  425. * intro 调用下载函数
  426. * param
  427. */
  428. public function downloadZip()
  429. {
  430. $bach_name = I('bach_name');
  431. $printer_id = I('printer_id');
  432. if (!downZip($bach_name, $printer_id)) {
  433. $this->error('文件不存在,请联系管理员');
  434. }
  435. }
  436. }