Product.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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,gy_name')->select();
  71. $res = Db::name('formula_detail')->where('pid',$bach_info['fid'])->where('version',$form['version'])->field('id,pid,material,version,is_replace,gy_name')->select();
  72. foreach($res as &$v){
  73. // if($v['gy_name'] == null){
  74. // $v['gy_name'] = '';
  75. // }
  76. if($v['is_replace']==1){
  77. $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
  78. }else{
  79. $v['replace'] = [];
  80. }
  81. }
  82. //查出所有关于批次号的数据存储
  83. // Cache::set('data',serialize($res),0);
  84. return json($bach_info);
  85. }
  86. //获取配方单信息
  87. public function get_formula(){
  88. $bach = unserialize(Cache::get('bach'));//获取配方单信息
  89. $formula = $this->request->post('formula');//配方名称
  90. $data = unserialize(Cache::get('data'));//获取当前批次号缓存数据
  91. $arr = [];
  92. $arr['material']=$formula;
  93. foreach($data as $vv) {
  94. //没有替代料 error==0为有当前配方, error==1为没有配方,,前台提示
  95. if ($vv['is_replace'] == 0) {
  96. if ($vv['material'] == $formula) {
  97. $arr['error'] = 0;
  98. break;
  99. } else {
  100. $arr['error'] = 1;
  101. }
  102. } else {//有替代料
  103. foreach ($vv['replace'] as $val) {
  104. if ($val['material'] == $formula) {
  105. $arr['error'] = 0;
  106. break;
  107. } else {
  108. $arr['error'] = 1;
  109. }
  110. }
  111. if ($arr['error'] == 0) {
  112. break;
  113. }
  114. }
  115. }
  116. //查询数据库↓ 缓存↑
  117. $form = Db::name('formula')->find($bach['fid']);
  118. $res = Db::name('formula_detail')->where('pid = '.$bach['fid'].' and material like "%'.encrypt($formula).'%" and version = "'.$form['version'].'"')->select();
  119. $arr = [];
  120. if(!$res){
  121. $arr['material'] = $formula;
  122. $arr['error']=1;
  123. }else{
  124. foreach($res as &$v){
  125. if($v['is_replace']==1){
  126. $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
  127. }
  128. $arr=$v;
  129. $arr['error']=0;
  130. }
  131. }
  132. $arr['pname'] = $bach['name'];//配方名称
  133. $arr['time'] = date("Y-m-d H:i:s");
  134. $arr['bach'] = $bach['bach'];
  135. return json($arr);
  136. }
  137. public function add()
  138. {
  139. if (false === $this->request->isPost()) {
  140. return $this->view->fetch();
  141. }
  142. $params = $this->request->post('row/a');
  143. if (empty($params)) {
  144. $this->error(__('Parameter %s can not be empty', ''));
  145. }
  146. Db::startTrans();
  147. try {
  148. $pro = [];
  149. foreach(explode(';',$params['bach']) as $key=> $val){
  150. if(!empty($val)){
  151. $res = [];
  152. $res['time'] = date("Y-m-d H:i:s");
  153. $res['audit'] = $params['audit'][$key];
  154. $res['status'] = $params['status'];
  155. $res['pickor'] = $params['pickor'];
  156. $res['note'] = $params['note'];
  157. $res['warehouseor'] = $params['warehouseor'];
  158. $res['batch'] = $val;
  159. $res['pname'] = $params['proname'][$key];
  160. $res['specifications'] = $params['specifications'][$key];
  161. $res['unit'] = $params['unit'][$key];
  162. $pro[] = $res;
  163. }
  164. }
  165. foreach($pro as $v){
  166. $this->model->insert($v);
  167. $id = $this->model->getLastInsID();
  168. $arr = [];
  169. $prodet = [];
  170. foreach($params['material'] as $k=>$vv){
  171. if($v['batch']==$params['batch'][$k]){
  172. $arr['weight'] = $params['weight'][$k];
  173. $arr['pid'] = /*1;//*/$id;
  174. $arr['material'] = $params['material'][$k];
  175. $arr['createtime'] = date("Y-m-d H:i:s");
  176. $prodet[] = $arr;
  177. }
  178. }
  179. $this->detail->saveAll($prodet);
  180. }
  181. Db::commit();
  182. }catch(Exception $e){
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. $this->success();
  187. return $this->view->fetch();
  188. }
  189. public function show($ids){
  190. $row = $this->model->get($ids);
  191. $row['data'] =Db::query("select *,sum(weight) as sum from mn_product_detail where pid= {$ids} group by material");
  192. $status = Db::name('product')->where('id',$ids)->value('status');
  193. $this->view->assign("row", $row);
  194. $this->view->assign("status", $status);
  195. return $this->view->fetch();
  196. }
  197. }