WarehousingBook.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * author xtj
  4. * createtime 2020/7/14 0014 上午 10:13
  5. * return array obj json bool
  6. * param
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use think\Db;
  11. use think\Where;
  12. use think\Session;
  13. use think\Request;
  14. //油墨指令书
  15. class WarehousingBook extends Backend{
  16. public function index(){
  17. //设置过滤方法
  18. $this->request->filter(['strip_tags']);
  19. if ($this->request->isAjax()) {
  20. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  21. $search = input('search');
  22. $option = array();
  23. if (!empty($search)){
  24. if (strlen($search) == 7){
  25. $option['order_number'] = $search;
  26. }else{
  27. $option['bach'] = $search;
  28. }
  29. }
  30. $total = Db::name('formula_book_detail')
  31. ->where($option)
  32. // ->where($where)
  33. // ->where('status',1)
  34. ->order($sort, $order)
  35. ->count();
  36. // print_r($option);die;
  37. $list = Db::name('formula_book_detail')
  38. // ->where($where)
  39. ->where($option)
  40. // ->where('status',1)
  41. // ->fetchSql(true)
  42. ->order($sort, $order)
  43. ->limit($offset, $limit)
  44. ->select();
  45. $result = array("total" => $total, "rows" => $list);
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. //获取基础数据
  51. public function add(){
  52. $params = input('ids');
  53. if (empty($params)){
  54. $this->error('未知数据格式');
  55. }
  56. $result = Db::name('formula_book_detail')->where('id',$params)->find();
  57. $formula = json_decode($result['warehousing'],true);
  58. //获取专色调配数据
  59. $warehousingData = [];
  60. foreach ($formula as $k=>$v){
  61. if (count($v) == 6){
  62. $warehousingData[$k]['color'] = $v[0];
  63. $warehousingData[$k]['need_weight'] = $v[4];
  64. $warehousingData[$k]['has_weight'] = 0;
  65. $warehousingData[$k]['book_weight'] = 0;
  66. }
  67. }
  68. $warehousingData = array_values($warehousingData);
  69. foreach ($warehousingData as $k1=>$v1){
  70. $cid = Db::name('warehousing')->where('formula',$result['formula'])->where('color',$v1['color'])->find();
  71. if (!empty($cid)){
  72. $weight = Db::name('warehousing_detail')->where('order_number',$result['order_number'])->where('cid',$cid['cid'])->column('book_weight');
  73. $weight = array_sum($weight);
  74. // print_r($result['order_number']);
  75. // print_r($weight);die;
  76. if (!empty($weight)){
  77. $warehousingData[$k1]['book_weight'] = $weight / 1000;
  78. }
  79. $warehousingData[$k1]['has_weight'] = $cid['weight'] / 1000;
  80. }else{
  81. $warehousingData[$k1]['has_weight'] = 0;
  82. }
  83. }
  84. return $this->view->fetch('',compact('result','warehousingData'));
  85. }
  86. //渲染表格里配方和大小盒计划数据
  87. public function doDetail(){
  88. $params = input('id');
  89. if (empty($params)){
  90. $this->error('未知数据格式');
  91. }
  92. $result = Db::name('formula_book_detail')->where('id',$params)->find();
  93. $formula = json_decode($result['warehousing'],true);
  94. $consume = json_decode($result['consume'],true);
  95. $formulaSpan = json_decode($result['formula_span'],true);
  96. $formulaSpanData = [];
  97. $i = 0;
  98. foreach ($formulaSpan as $key=>$value){
  99. $formulaSpanData[$key]['beginRow'] = $i;
  100. $count = $value;
  101. $formulaSpanData[$key]['rowSpan'] = $value;
  102. $i = $count + $i;
  103. }
  104. return array('status'=>1,'data'=>$formula,'consume'=>$consume,'formulaSpanData'=>$formulaSpanData,'consumeSpan'=>$result['consume_span']);
  105. }
  106. //入库
  107. public function warehousing(){
  108. $data = input('data/a');
  109. // print_r($data);die;
  110. if (empty($data)){
  111. return array('status'=>0,'msg'=>'数据错误,请联系管理员');
  112. }
  113. foreach ($data as $key=>$params){
  114. if ($params['weight'] == 0 || $params['weight'] == ''){
  115. continue;
  116. }else{
  117. //产品编号
  118. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params['order_number']}' order by Uniqid asc limit 1");
  119. if (empty($product)){
  120. return array('status'=>0,'msg'=>'数据错误,请联系管理员');
  121. }
  122. $product_number = rtrim($product[0]['Gd_cpdh']);
  123. // print_r($product_number);die;
  124. $params['product_number'] = $product_number;
  125. //时间取6位
  126. $time = substr(date('Ymd'),-6);
  127. $user = Session::get('admin');
  128. $name = $user['nickname'];
  129. //人员编号,根据缓存的nickname确认人员编号
  130. $people_number = Db::name('people')->where('name',$name)->value('number');
  131. //色号,根据产品编码和颜色 确定它的色号
  132. $params['color'] = trim($params['color']);//去掉空格
  133. $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  134. $colorInfo = Db::name('formula')->where($where)->where('color',$params['color'])->find();
  135. // print_r($colorInfo);die;
  136. if(!empty($colorInfo)){
  137. $params['cid'] = $colorInfo['id'];
  138. $color_number = $colorInfo['color_number'];
  139. }else{
  140. return array('status'=>0,'msg'=>'未查到配方数据');
  141. }
  142. // print_r($params);die;
  143. //查这个色号当日第几次调配
  144. $num = Db::name('warehousing_detail')->where('product_number',$product_number)->where('cid',$params['cid'])->whereTime('create','today')->count();
  145. if (empty($num)){
  146. $colorNum = 1;
  147. }else{
  148. $colorNum = $num + 1;
  149. }
  150. //专色墨批次号
  151. $params['bach_number'] = $product_number.$time.$color_number.$people_number.'-'.$colorNum;
  152. $params['weight'] = $params['weight'] * 1000;
  153. $params['book_weight'] = $params['weight'];
  154. $params['create'] = date('Y-m-d H:i:s');
  155. /* 专色墨数据整合完毕,下面整理分解原墨数据
  156. 根据产品编号,配方颜色获得配方原墨
  157. 获取配方总量,算出占比,再根据入库的专色墨重量相乘,得到各原墨的消耗重量,扣减库存
  158. */
  159. $matterTotal = Db::name('formula_detail')->where('product_name',$params['formula'])->where('color',$params['color'])->sum('ink_number');
  160. $formula = Db::name('formula_detail')->where('product_name',$params['formula'])->where('color',$params['color'])->select();
  161. foreach ($formula as $k=>$v){
  162. $matterMix = ceil((intval($v['ink_number']) / $matterTotal) * $params['weight']);//本次调墨消耗重量(向上取整)
  163. $matterDetail = Db::name('matter_detail')->where('code',$v['code'])->where('weight','>',0)->order('id desc')->limit(1)->select();//按照最新调拨单 扣减原墨库存
  164. if(empty($matterDetail)){
  165. $matter = array();
  166. $matter['code'] = $v['code'];
  167. $matter['name'] = $v['ink'];
  168. $matter['weight'] = -$matterMix;
  169. $matter['create'] = date('Y-m-d H:i:s');
  170. $isExit = Db::name('matter_detail')->where('code',$v['code'])->find();
  171. if ($isExit){
  172. $weight = $isExit['weight'] + $matter['weight'];
  173. Db::name('matter_detail')->where('code',$v['code'])->setField('weight',$weight);
  174. Db::name('matter')->where('code',$v['code'])->setField('weight',$weight);
  175. }else{
  176. Db::name('matter')->insert($matter);
  177. Db::name('matter_detail')->insert($matter);
  178. }
  179. }else{
  180. $matterDeductions = $matterDetail[0];
  181. $matterDetailWeight = $matterDeductions['weight'] - $matterMix;//详细表库存-本次调墨消耗
  182. $matterWeight = Db::name('matter')->where('code',$v['code'])->value('weight');
  183. $newMatterWeight = $matterWeight - $matterMix;//总表库存重量
  184. Db::name('matter_detail')->where('id',$matterDeductions['id'])->setField('weight',$matterDetailWeight);
  185. Db::name('matter')->where('code',$v['code'])->setField('weight',$newMatterWeight);
  186. }
  187. $log = array();
  188. $log['code'] = $v['code'];
  189. $log['people'] = $name;
  190. $log['content'] = '扣减了存货编码为'.$v['code'].'的油墨'.$matterMix.'g库存';
  191. $log['weight'] = $matterMix;
  192. $log['create'] = date('Y-m-d H:i:s');
  193. Db::name('matter_log')->insert($log);
  194. }
  195. //专色墨入库
  196. Db::startTrans();
  197. try{
  198. $id = Db::name('warehousing_detail')->insertGetId($params);
  199. if (empty($id)){
  200. throw new Exception('入库失败,请联系开发人员');
  201. }
  202. $totalWeight = Db::name('warehousing')->where('cid',$params['cid'])->find();
  203. if (!empty($totalWeight)){
  204. $totalWeight['weight'] = $totalWeight['weight'] + $params['weight'];
  205. Db::name('warehousing')->where('formula',$params['formula'])->where('cid',$params['cid'])->setField('weight',$totalWeight['weight']);
  206. Db::name('warehousing')->where('formula',$params['formula'])->where('cid',$params['cid'])->setField('update',date('Y-m-d H:i:s'));
  207. }else{
  208. $data = array();
  209. $data['weight'] = $params['weight'];
  210. $data['product_name'] = $params['product_name'];
  211. $data['product_number'] = $params['product_number'];
  212. $data['formula'] = $params['formula'];
  213. $data['cid'] = $params['cid'];
  214. $data['color'] = $params['color'];
  215. $data['update'] = date('Y-m-d H:i:s');
  216. Db::name('warehousing')->insert($data);
  217. }
  218. Db::commit();
  219. $result = 1;
  220. }catch (\think\Exception\DbException $exception){
  221. Db::rollback();
  222. $result = -1;
  223. }
  224. }
  225. }
  226. if ($result = 1){
  227. $this->success('入库成功');
  228. }else{
  229. $this->error('入库失败');
  230. }
  231. }
  232. //打印数据
  233. public function getData(){
  234. $params = input('order_number');
  235. if (empty($params)){
  236. return array('status'=>-1,'msg'=>'数据错误');
  237. }
  238. $time = date('Y-m-d H:i:s');
  239. $lastTime = date('Y-m-d H:i:s',strtotime($time) - 60);//上一分钟
  240. $result['warehousing'] = Db::name('warehousing_detail')
  241. ->where('order_number',$params)
  242. ->where('create','between',[$lastTime,$time])
  243. ->field('order_number,product_name,product_number,formula,color,bach_number,weight,create')
  244. ->select();
  245. $peopleNumber = strpos($result['warehousing'][0]['bach_number'],'-');
  246. $peopleNumber = substr($result['warehousing'][0]['bach_number'],$peopleNumber-2,2);
  247. $result['name'] = Db::name('people')->where('number',$peopleNumber)->value('name');
  248. // $result['formula'] = $result['warehousing'][0]['formula'];
  249. $result['order_number'] = $result['warehousing'][0]['order_number'];
  250. if (!empty($result)){
  251. return array('status'=>1,'data'=>$result,'warehousing'=>$result['warehousing']);
  252. }else{
  253. return array('status'=>-1,'data'=>'');
  254. }
  255. }
  256. }