CollarUse.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Request;
  7. /**
  8. * 油墨领用退还管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class CollarUse extends Backend
  13. {
  14. /**
  15. * CollarUse模型对象
  16. * @var \app\admin\model\CollarUse
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\CollarUse;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("isScrapList", $this->model->getIsScrapList());
  25. $this->view->assign("lStatusList", $this->model->getLStatusList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. //专墨
  33. public function index(){
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags']);
  36. if ($this->request->isAjax()) {
  37. // $params = $this->request->request();
  38. // print_r($params);die;
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $total = $this->model
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $list = collection($list)->toArray();
  54. foreach ($list as $key=>$value){
  55. if ($value['status'] == 1){
  56. $ink = Db::name('warehousing_detail')->where('bach_number',$value['warehousing'])->find();
  57. }else{
  58. $ink = Db::name('warehousing_detail')->where('bach_number',$value['back'])->find();
  59. }
  60. $list[$key]['color'] = $ink['color'];
  61. $list[$key]['formula'] = $ink['formula'];
  62. $list[$key]['weight'] = $value['weight']/1000;
  63. $list[$key]['back_weight'] = $value['back_weight']/1000;
  64. }
  65. $result = array("total" => $total, "rows" => $list);
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. //原墨
  71. public function ink(){
  72. //设置过滤方法
  73. $this->request->filter(['strip_tags']);
  74. if ($this->request->isAjax()) {
  75. // $params = $this->request->request();
  76. // print_r($params);die;
  77. //如果发送的来源是Selectpage,则转发到Selectpage
  78. if ($this->request->request('keyField')) {
  79. return $this->selectpage();
  80. }
  81. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  82. $total = Db::name('matter_use')
  83. ->where($where)
  84. ->order($sort, $order)
  85. ->count();
  86. $list = Db::name('matter_use')
  87. ->where($where)
  88. ->order($sort, $order)
  89. ->limit($offset, $limit)
  90. ->select();
  91. $list = collection($list)->toArray();
  92. foreach ($list as $key=>$value){
  93. $list[$key]['out_weight'] = $value['out_weight']/1000;
  94. $list[$key]['back_weight'] = $value['back_weight']/1000;
  95. }
  96. $result = array("total" => $total, "rows" => $list);
  97. return json($result);
  98. }
  99. return $this->view->fetch();
  100. }
  101. public function add(){
  102. $params = input('bach');
  103. if ($params == '' || empty($params) || !is_numeric($params)){
  104. return array('status'=>0,'msg'=>'工单输入错误');
  105. }
  106. //根据工单批次号获取对应配方、库存信息
  107. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  108. if (empty($product)){
  109. return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
  110. }
  111. $product_number = rtrim($product[0]['Gd_cpdh']);
  112. $product_name = trim($product[0]['Gd_cpmc']);
  113. $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,a.product_number)")];
  114. //专墨颜色
  115. $colorResult = Db::name('formula')->alias('a')
  116. ->join('warehousing b','a.id=b.cid','left')
  117. ->where($where)
  118. ->field('a.*,b.weight')
  119. ->select();
  120. // print_r($colorResult);die;
  121. foreach ($colorResult as $key=>$value){
  122. if (empty($value['weight'])){
  123. $colorResult[$key]['weight'] = 0;
  124. }else{
  125. $colorResult[$key]['weight'] = $value['weight'] / 1000;
  126. }
  127. }
  128. //原墨
  129. $map[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  130. $inkData = Db::name('formula_consume')->where($map)->where('code','neq','')->field('id,ink,code')->select();
  131. $inkData = $this->assoc_unique($inkData,'ink');
  132. $jar = Db::name('jar')->order('sort')->select();
  133. return $this->view->fetch('',compact('params','product_name','colorResult','jar','inkData'));
  134. }
  135. //退料
  136. public function back(){
  137. if ($this->request->isAjax()){
  138. $data = input('data/a');
  139. $code = input('code');
  140. $order_number = input('bach');
  141. // print_r($order_number);die;
  142. $idArray = array();
  143. if ($code == 1){//代表扫描的是工单号
  144. foreach ($data as $key=>$value){
  145. if (strlen($value['cid']) >= 9){ //大于等于9代表退的是原墨
  146. if (!empty($value['weight'])){
  147. $back = array();
  148. $back['order_number'] = $order_number;
  149. $back['code'] = $value['cid'];
  150. $back['ink_name'] = $value['name'];
  151. $back['status'] = 2;
  152. $bach = Db::name('matter_use')->where('order_number',$order_number)->value('out');
  153. $back['back'] = $bach;
  154. $back['back_weight'] = $value['weight'] * 1000;
  155. $back['machine_number'] = substr($value['machine'],0,1);
  156. $back['create'] = date('Y-m-d H:i:s');
  157. $res = Db::name('matter_use')->insert($back);
  158. if ($res){
  159. $matter = Db::name('matter_detail')->where('code',$back['code'])->order('id')->find();
  160. $matterWeight = $matter['weight'] + $back['back_weight'];
  161. Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
  162. $t_matter = Db::name('matter')->where('code',$back['code'])->find();
  163. $t_weight = $t_matter['weight'] + $back['back_weight'];
  164. Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
  165. }
  166. }
  167. }else{ //长度小于9代表退的是专色墨 //先退回到专墨,再插入一条退回数据
  168. if (!empty($value['weight'])){
  169. $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->order('id desc')->find();
  170. $total = Db::name('warehousing')->where('cid',$value['cid'])->find();
  171. if (empty($warehousing) || empty($total)){
  172. return array('status'=>0);
  173. }
  174. $warehousingWeight = $warehousing['weight'] + $value['weight'] * 1000;//详情表退回重量
  175. $updateWarehousing = Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$warehousingWeight);
  176. $totalWeight = $total['weight'] + $value['weight'] * 1000;
  177. $updateWeight = Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
  178. $insert = array();
  179. $insert['order_number'] = $order_number;
  180. $insert['product_name'] = Db::name('collar_use')->where('order_number',$order_number)->value('product_name');
  181. $insert['status'] = 2;
  182. $insert['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  183. $insert['back'] = $warehousing['bach_number'];
  184. $insert['back_weight'] = $value['weight'] * 1000;
  185. $insert['machine_number'] = substr($value['machine'],0,1);
  186. $insert['create'] = date('Y-m-d H:i:s');
  187. $id = Db::name('collar_use')->insertGetId($insert);
  188. $idArray[$key] = $id;
  189. }
  190. }
  191. }
  192. $result = Db::name('collar_use')->where('id','in',$idArray)->select();
  193. return array('status'=>1,'data'=>$result);
  194. }else{//扫描的是专墨批次号
  195. foreach ($data as $key=>$value){
  196. $warehousing = Db::name('warehousing_detail')->where('bach_number',$value['bach'])->find();
  197. $weight = $warehousing['weight'] + $value['weight'] * 1000;
  198. Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$weight);
  199. $total = Db::name('warehousing')->where('cid',$warehousing['cid'])->find();
  200. $totalWeight = $total['weight'] + $value['weight'] * 1000;
  201. Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
  202. $insert = array();
  203. $insert['order_number'] = $warehousing['order_number'];
  204. $insert['product_name'] = $warehousing['product_name'];
  205. $insert['status'] = 2;
  206. $insert['cid'] = $warehousing['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  207. $insert['back'] = $value['bach'];
  208. $insert['back_weight'] = $value['weight'] * 1000;
  209. $insert['machine_number'] = substr($value['machine'],0,1);
  210. $insert['create'] = date('Y-m-d H:i:s');
  211. $id = Db::name('collar_use')->insertGetId($insert);
  212. $idArray[$key] = $id;
  213. }
  214. $result = Db::name('collar_use')->alias('a')
  215. ->join('warehousing_detail b','a.back = b.bach_number')
  216. ->where('a.id','in',$idArray)
  217. ->field('a.*,b.color')
  218. ->select();
  219. return array('status'=>1,'data'=>$result);
  220. }
  221. }
  222. $jar = Db::name('jar')->order('sort')->select();
  223. return $this->view->fetch('',compact('jar'));
  224. }
  225. //出库
  226. public function out(){
  227. // return array('status'=>1,'msg'=>'出库成功');
  228. $params = input('data/a');
  229. $inkData = input('ink/a');
  230. // print_r($params);die;
  231. //专墨出库
  232. if (!empty($params)){
  233. foreach ($params as $key=>$value){
  234. if (empty($value['weight']) || $value['weight'] == 0){
  235. continue;
  236. }else{
  237. //根据工单批次号获取对应配方、库存信息
  238. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$value["order_number"]}' limit 1");
  239. if (empty($product)){
  240. return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
  241. }
  242. // $product_number = rtrim($product[0]['Gd_cpdh']);
  243. $product_name = rtrim($product[0]['成品名称']);
  244. $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->where('weight','>',0)->order('id')->select();
  245. if (empty($warehousing)){
  246. return array('status'=>0,'msg'=>'库存重量为0,出库失败');
  247. }
  248. //插入一条出库数据
  249. $collarData = array();
  250. $collarData['order_number'] = $value['order_number'];
  251. $collarData['product_name'] = $product_name;
  252. $collarData['warehousing'] = $warehousing[0]['bach_number'];
  253. $collarData['status'] = 1;
  254. $collarData['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  255. $collarData['weight'] = $value['weight'] * 1000;
  256. $collarData['l_status'] = 2;
  257. $collarData['machine_number'] = $value['machine_number'];
  258. $collarData['create'] = date('Y-m-d H:i:s');
  259. $collarId = Db::name('collar_use')->insertGetId($collarData);
  260. //扣减库存,递归模式,直到$warehousing[x]['weight] - $weight(这个值是各个详细重量减去后的值) > 0, 返回数量,扣减这个数量之前所有的库存
  261. if(count($warehousing) == 1){
  262. $newWeight = $warehousing[0]['weight'] - $collarData['weight'];
  263. if ($newWeight < 0){
  264. Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',0);
  265. }else{
  266. Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',$newWeight);
  267. }
  268. }else{
  269. $key = $this->whatNumber($collarData['weight'],$warehousing);
  270. $weight = 0;
  271. $idArray = [];
  272. $collarBach = '';//领走的批次号
  273. $collarWeight= '';//领走的批次号重量
  274. for ($i=0;$i<$key;$i++){
  275. $weight = $weight + $warehousing[$i]['weight'];
  276. $idArray[$i] = $warehousing[$i]['id'];
  277. //2022年3月26日11:09:38 新增
  278. $collarBach = $warehousing[$i]['bach_number'].','.$collarBach;//将领走的各个批次号做成字符串
  279. $collarWeight = $warehousing[$i]['weight'].','.$collarWeight;//将领走的各个批次号重量做成字符串
  280. }
  281. $weight = $collarData['weight'] - $weight ; //这个值是大于0的 将前面的库存扣减为0 这个按值扣减
  282. $newWeight = $warehousing[$key]['weight'] - $weight;
  283. //2022年3月26日11:09:44 新增
  284. $collarBach = $warehousing[$key]['bach_number'].','.$collarBach;
  285. $collarWeight = $weight.','.$collarWeight;
  286. // $collarData['process'] = $collarBach;//暂时将此字段作为领走时的专墨批次号字段
  287. // $collarData['current_process'] = $collarWeight;//暂时将此字段作为领走时的专墨批次号字段
  288. Db::name('collar_use')->where('id',$collarId)->setField('process',$collarBach);
  289. Db::name('collar_use')->where('id',$collarId)->setField('current_process',$collarWeight);
  290. if ($newWeight < 0){
  291. $newWeight = 0;
  292. }
  293. Db::name('warehousing_detail')->where('id','in',$idArray)->setField('weight',0);
  294. Db::name('warehousing_detail')->where('id',$warehousing[$key]['id'])->setField('weight',$newWeight);
  295. }
  296. //扣减总库存
  297. $warehousingWeight = Db::name('warehousing')->where('cid',$value['cid'])->value('weight');
  298. $totalWeight = $warehousingWeight - $collarData['weight'];
  299. if ($totalWeight < 0){
  300. Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',0);
  301. }else{
  302. Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',$totalWeight);
  303. }
  304. }
  305. }
  306. }
  307. if (!empty($inkData)){
  308. foreach ($inkData as $k=>$v){
  309. //插入一条领用数据
  310. $ink = array();
  311. $ink['order_number'] = $v['order_number'];
  312. $ink['code'] = $v['code'];
  313. $ink['ink_name'] = $v['ink'];
  314. $ink['machine_number'] = $v['machine_number'];
  315. $ink['out_weight'] = $v['weight'] * 1000;
  316. $ink['status'] = 1;
  317. $ink['create'] = date('Y-m-d H:i:s');
  318. $bach = Db::name('matter_detail')->where('code',$v['code'])->value('bach');
  319. if (!empty($bach)){
  320. $ink['out'] = $bach;
  321. }else{
  322. $ink['out'] = '';
  323. }
  324. $insert = Db::name('matter_use')->insert($ink);
  325. //扣减库存
  326. if ($insert){
  327. $matter = Db::name('matter_detail')->where('code',$v['code'])->order('id')->find();
  328. $matterWeight = $matter['weight'] - $ink['out_weight'];
  329. Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
  330. $t_matter = Db::name('matter')->where('code',$v['code'])->find();
  331. $t_weight = $t_matter['weight'] - $ink['out_weight'];
  332. Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
  333. }
  334. }
  335. }
  336. return array('status'=>1,'msg'=>'出库成功');
  337. }
  338. //根据生产批次去线上库获取领用记录
  339. public function getOrder(){
  340. $params = input('order_number');
  341. if (!empty($params)){
  342. $where['a.status'] = 1;
  343. $time = date('Y-m-d H:i:s');
  344. $lastTime = date('Y-m-d H:i:s',strtotime($time) - 60);//上一分钟
  345. $data = Db::name('collar_use')->alias('a')
  346. ->join('warehousing_detail b','a.warehousing = b.bach_number')
  347. ->where('a.order_number',$params)
  348. ->where($where)
  349. ->where('a.create','between',[$lastTime,$time])
  350. ->field('a.*,b.color')
  351. ->select();
  352. return array('status'=>1,'data'=>$data);
  353. }
  354. }
  355. //获取机台号
  356. public function getWeight(){
  357. $params = input('bach');
  358. if (empty($params)){
  359. return array('status'=>0,'msg'=>'数据不能为空');
  360. }
  361. $data = Db::name('collar_use')->where('warehousing',$params)->order('id desc')->find();
  362. $machine = $data['machine_number'].'#';
  363. return array('status'=>1,'data'=>$machine);
  364. }
  365. //根据工单号获取配方
  366. public function getColor(){
  367. $params = input('order');
  368. if (empty($params)){
  369. return array('status'=>0,'msg'=>'数据不能为空');
  370. }
  371. //根据工单批次号获取对应配方、库存信息
  372. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  373. if (empty($product)){
  374. return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
  375. }
  376. $product_number = rtrim($product[0]['Gd_cpdh']);
  377. $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  378. $colorResult = Db::name('formula')
  379. ->where($where)
  380. ->select();
  381. $inkResult = Db::name('formula_consume')
  382. ->where($where)
  383. ->where('code','neq','')
  384. ->field('id,ink,code')
  385. ->select();
  386. $inkResult = $this->assoc_unique($inkResult,'code');
  387. $machine = Db::name('collar_use')->where('order_number',$params)->order('id asc')->find();
  388. $machine_number = $machine['machine_number'].'#';
  389. return array('status'=>1,'data'=>$colorResult,'machine_number'=>$machine_number,'ink'=>$inkResult);
  390. }
  391. //二维数组去重
  392. function assoc_unique($arr, $key) {
  393. $tmp_arr = array();
  394. foreach ($arr as $k => $v) {
  395. if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
  396. unset($arr[$k]);
  397. } else {
  398. $tmp_arr[] = $v[$key];
  399. }
  400. }
  401. sort($arr); //sort函数对数组进行排序
  402. return $arr;
  403. }
  404. //扣减库存递归
  405. public function whatNumber($weight,$array=[],$i=0){
  406. $surplusWeight =$weight - $array[$i]['weight'] ;
  407. if ($surplusWeight > 0){
  408. $i = $i + 1;
  409. return $this->whatNumber($surplusWeight,$array,$i);
  410. }else{
  411. return $i;
  412. }
  413. }
  414. }