Product.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 生产管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Product extends Backend
  13. {
  14. /**
  15. *
  16. *
  17. *
  18. *
  19. *
  20. * Product模型对象
  21. * @var \app\admin\model\Product
  22. */
  23. protected $model = null;
  24. protected $detail = null;
  25. protected $noNeedRight = ['ajax','get_formula'];
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\admin\model\Product;
  30. $this->detail = new \app\admin\model\ProductDetail;
  31. $this->view->assign("statusList", $this->model->getStatusList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //当前是否为关联查询
  44. $this->relationSearch = false;
  45. //设置过滤方法
  46. $this->request->filter(['strip_tags', 'trim']);
  47. if ($this->request->isAjax()) {
  48. //如果发送的来源是Selectpage,则转发到Selectpage
  49. if ($this->request->request('keyField')) {
  50. return $this->selectpage();
  51. }
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $list = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. //获取生产批次号等相关信息
  63. public function ajax(){
  64. $bach = $this->request->post('bach');
  65. $str = explode(';',$bach);
  66. $batch = $str[(count($str))-1];
  67. $bach_info = Db::name('task')->where('bach',$batch)->find();
  68. Cache::set('bach',serialize($bach_info),0);
  69. $form = Db::name('formula')->find($bach_info['fid']);
  70. $res = Db::name('formula_detail')->where('pid = '.$bach_info['fid'].' and version = "'.$form['version'].'"')->field('id,pid,material,version,is_replace')->select();
  71. foreach($res as &$v){
  72. if($v['is_replace']==1){
  73. $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
  74. }else{
  75. $v['replace'] = [];
  76. }
  77. }
  78. //查出所有关于批次号的数据存储
  79. Cache::set('data',serialize($res),0);
  80. return json($bach_info);
  81. }
  82. //获取配方单信息
  83. public function get_formula(){
  84. $bach = unserialize(Cache::get('bach'));//获取配方单信息
  85. $formula = $this->request->post('formula');//配方名称
  86. $data = unserialize(Cache::get('data'));//获取当前批次号缓存数据
  87. $arr = [];
  88. $arr['material']=$formula;
  89. foreach($data as $vv) {
  90. //没有替代料 error==0为有当前配方, error==1为没有配方,,前台提示
  91. if ($vv['is_replace'] == 0) {
  92. if ($vv['material'] == $formula) {
  93. $arr['error'] = 0;
  94. break;
  95. } else {
  96. $arr['error'] = 1;
  97. }
  98. } else {//有替代料
  99. foreach ($vv['replace'] as $val) {
  100. if ($val['material'] == $formula) {
  101. $arr['error'] = 0;
  102. break;
  103. } else {
  104. $arr['error'] = 1;
  105. }
  106. }
  107. if ($arr['error'] == 0) {
  108. break;
  109. }
  110. }
  111. }
  112. //查询数据库↓ 缓存↑
  113. /*$form = Db::name('formula')->find($bach['fid']);
  114. $res = Db::name('formula_detail')->where('pid = '.$bach['fid'].' and material like "%'.encrypt($formula).'%" and version = "'.$form['version'].'"')->select();
  115. $arr = [];
  116. if(!$res){
  117. $arr['material'] = $formula;
  118. $arr['error']=1;
  119. }else{
  120. foreach($res as &$v){
  121. if($v['is_replace']==1){
  122. $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
  123. }
  124. $arr=$v;
  125. $arr['error']=0;
  126. }
  127. }*/
  128. $arr['pname'] = $bach['name'];//配方名称
  129. $arr['time'] = date("Y-m-d H:i:s");
  130. $arr['bach'] = $bach['bach'];
  131. return json($arr);
  132. }
  133. public function add()
  134. {
  135. if (false === $this->request->isPost()) {
  136. return $this->view->fetch();
  137. }
  138. $params = $this->request->post('row/a');
  139. if (empty($params)) {
  140. $this->error(__('Parameter %s can not be empty', ''));
  141. }
  142. Db::startTrans();
  143. try {
  144. $pro = [];
  145. foreach(explode(';',$params['bach']) as $key=> $val){
  146. if(!empty($val)){
  147. $res = [];
  148. $res['time'] = date("Y-m-d H:i:s");
  149. $res['audit'] = $params['audit'][$key];
  150. $res['status'] = $params['status'];
  151. $res['pickor'] = $params['pickor'];
  152. $res['note'] = $params['note'];
  153. $res['warehouseor'] = $params['warehouseor'];
  154. $res['batch'] = $val;
  155. $res['pname'] = $params['proname'][$key];
  156. $res['specifications'] = $params['specifications'][$key];
  157. $res['unit'] = $params['unit'][$key];
  158. $pro[] = $res;
  159. }
  160. }
  161. foreach($pro as $v){
  162. $this->model->insert($v);
  163. $id = $this->model->getLastInsID();
  164. $arr = [];
  165. $prodet = [];
  166. foreach($params['material'] as $k=>$vv){
  167. if($v['batch']==$params['batch'][$k]){
  168. $arr['weight'] = $params['weight'][$k];
  169. $arr['pid'] = /*1;//*/$id;
  170. $arr['material'] = $params['material'][$k];
  171. $arr['createtime'] = date("Y-m-d H:i:s");
  172. $prodet[] = $arr;
  173. }
  174. }
  175. $this->detail->saveAll($prodet);
  176. }
  177. Db::commit();
  178. }catch(Exception $e){
  179. Db::rollback();
  180. $this->error($e->getMessage());
  181. }
  182. $this->success();
  183. return $this->view->fetch();
  184. }
  185. public function show($ids){
  186. $row = $this->model->get($ids);
  187. $row['data'] =Db::query("select *,sum(weight) as sum from mn_product_detail where pid= {$ids} group by material");
  188. $status = Db::name('product')->where('id',$ids)->value('status');
  189. $this->view->assign("row", $row);
  190. $this->view->assign("status", $status);
  191. return $this->view->fetch();
  192. }
  193. }