CollarUse.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. $result = $this->validateAddOrder(input('bach'));
  103. if (!$result['valid']) {
  104. $this->dialogError($result['msg']);
  105. }
  106. $params = $result['data']['params'];
  107. $product_name = $result['data']['product_name'];
  108. $colorResult = $result['data']['colorResult'];
  109. $inkData = $result['data']['inkData'];
  110. $jar = Db::name('jar')->order('sort')->select();
  111. return $this->view->fetch('', compact('params', 'product_name', 'colorResult', 'jar', 'inkData'));
  112. }
  113. /**
  114. * 领用前校验工单数据(Ajax)
  115. */
  116. public function checkOrder()
  117. {
  118. $result = $this->validateAddOrder(input('bach'));
  119. if ($result['valid']) {
  120. $this->success('可以领用');
  121. }
  122. $this->error($result['msg']);
  123. }
  124. /**
  125. * 校验领用工单是否具备配方及原墨数据
  126. */
  127. protected function validateAddOrder($params)
  128. {
  129. if ($params === '' || $params === null) {
  130. return ['valid' => false, 'msg' => '请输入正确的工单号'];
  131. }
  132. $product = Db::connect('db2')->query("select Gd_cpdh,Gd_cpmc from 工单_基本资料 where Gd_gdbh = ? limit 1", [$params]);
  133. if (empty($product)) {
  134. return ['valid' => false, 'msg' => '查不到工单【' . $params . '】,请确认工单号是否正确'];
  135. }
  136. $product_number = rtrim($product[0]['Gd_cpdh']);
  137. $product_name = trim($product[0]['Gd_cpmc']);
  138. if ($product_number === '') {
  139. return ['valid' => false, 'msg' => '工单【' . $params . '】产品编码为空,无法领用,请联系管理员'];
  140. }
  141. $where[] = ['exp', Db::raw("FIND_IN_SET(:pn, a.product_number)")];
  142. $colorResult = Db::name('formula')->alias('a')
  143. ->bind(['pn' => $product_number])
  144. ->join('warehousing b', 'a.id=b.cid', 'left')
  145. ->where($where)
  146. ->field('a.*,b.weight')
  147. ->select();
  148. foreach ($colorResult as $key => $value) {
  149. if (empty($value['weight'])) {
  150. $colorResult[$key]['weight'] = 0;
  151. } else {
  152. $colorResult[$key]['weight'] = $value['weight'] / 1000;
  153. }
  154. }
  155. if (empty($colorResult)) {
  156. return ['valid' => false, 'msg' => '工单【' . $params . '】产品【' . $product_name . '】未配置专色墨配方,请维护配方后再领用'];
  157. }
  158. $map[] = ['exp', Db::raw("FIND_IN_SET(:pn, product_number)")];
  159. $inkData = Db::name('formula_consume')
  160. ->bind(['pn' => $product_number])
  161. ->where($map)
  162. ->where('code', 'neq', '')
  163. ->field('id,ink,code')
  164. ->select();
  165. $inkData = $this->assoc_unique($inkData, 'ink');
  166. if (empty($inkData)) {
  167. return ['valid' => false, 'msg' => '工单【' . $params . '】产品【' . $product_name . '】未配置原墨消耗数据,请维护配方后再领用'];
  168. }
  169. return [
  170. 'valid' => true,
  171. 'msg' => 'ok',
  172. 'data' => [
  173. 'params' => $params,
  174. 'product_name' => $product_name,
  175. 'colorResult' => $colorResult,
  176. 'inkData' => $inkData,
  177. ],
  178. ];
  179. }
  180. //退料
  181. public function back(){
  182. if ($this->request->isAjax()){
  183. $data = input('data/a');
  184. $code = input('code');
  185. $order_number = input('bach');
  186. // print_r($order_number);die;
  187. $idArray = array();
  188. if ($code == 1){//代表扫描的是工单号
  189. foreach ($data as $key=>$value){
  190. if (strlen($value['cid']) >= 9){ //大于等于9代表退的是原墨
  191. if (!empty($value['weight'])){
  192. $back = array();
  193. $back['order_number'] = $order_number;
  194. $back['code'] = $value['cid'];
  195. $back['ink_name'] = $value['name'];
  196. $back['status'] = 2;
  197. $bach = Db::name('matter_use')->where('order_number',$order_number)->value('out');
  198. $back['back'] = $bach;
  199. $back['back_weight'] = $value['weight'] * 1000;
  200. $back['machine_number'] = substr($value['machine'],0,1);
  201. $back['create'] = date('Y-m-d H:i:s');
  202. $res = Db::name('matter_use')->insert($back);
  203. if ($res){
  204. $matter = Db::name('matter_detail')->where('code',$back['code'])->order('id')->find();
  205. $matterWeight = $matter['weight'] + $back['back_weight'];
  206. Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
  207. $t_matter = Db::name('matter')->where('code',$back['code'])->find();
  208. $t_weight = $t_matter['weight'] + $back['back_weight'];
  209. Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
  210. }
  211. }
  212. }else{ //长度小于9代表退的是专色墨 //先退回到专墨,再插入一条退回数据
  213. if (!empty($value['weight'])){
  214. $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->order('id desc')->find();
  215. $total = Db::name('warehousing')->where('cid',$value['cid'])->find();
  216. if (empty($warehousing) || empty($total)){
  217. return array('status'=>0);
  218. }
  219. $warehousingWeight = $warehousing['weight'] + $value['weight'] * 1000;//详情表退回重量
  220. $updateWarehousing = Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$warehousingWeight);
  221. $totalWeight = $total['weight'] + $value['weight'] * 1000;
  222. $updateWeight = Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
  223. $insert = array();
  224. $insert['order_number'] = $order_number;
  225. $insert['product_name'] = Db::name('collar_use')->where('order_number',$order_number)->value('product_name');
  226. $insert['status'] = 2;
  227. $insert['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  228. $insert['back'] = $warehousing['bach_number'];
  229. $insert['back_weight'] = $value['weight'] * 1000;
  230. $insert['machine_number'] = substr($value['machine'],0,1);
  231. $insert['create'] = date('Y-m-d H:i:s');
  232. $id = Db::name('collar_use')->insertGetId($insert);
  233. $idArray[$key] = $id;
  234. }
  235. }
  236. }
  237. $result = Db::name('collar_use')->where('id','in',$idArray)->select();
  238. return array('status'=>1,'data'=>$result);
  239. }else{//扫描的是专墨批次号
  240. foreach ($data as $key=>$value){
  241. $warehousing = Db::name('warehousing_detail')->where('bach_number',$value['bach'])->find();
  242. $weight = $warehousing['weight'] + $value['weight'] * 1000;
  243. Db::name('warehousing_detail')->where('id',$warehousing['id'])->setField('weight',$weight);
  244. $total = Db::name('warehousing')->where('cid',$warehousing['cid'])->find();
  245. $totalWeight = $total['weight'] + $value['weight'] * 1000;
  246. Db::name('warehousing')->where('id',$total['id'])->setField('weight',$totalWeight);
  247. $insert = array();
  248. $insert['order_number'] = $warehousing['order_number'];
  249. $insert['product_name'] = $warehousing['product_name'];
  250. $insert['status'] = 2;
  251. $insert['cid'] = $warehousing['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  252. $insert['back'] = $value['bach'];
  253. $insert['back_weight'] = $value['weight'] * 1000;
  254. $insert['machine_number'] = substr($value['machine'],0,1);
  255. $insert['create'] = date('Y-m-d H:i:s');
  256. $id = Db::name('collar_use')->insertGetId($insert);
  257. $idArray[$key] = $id;
  258. }
  259. $result = Db::name('collar_use')->alias('a')
  260. ->join('warehousing_detail b','a.back = b.bach_number')
  261. ->where('a.id','in',$idArray)
  262. ->field('a.*,b.color')
  263. ->select();
  264. return array('status'=>1,'data'=>$result);
  265. }
  266. }
  267. $jar = Db::name('jar')->order('sort')->select();
  268. return $this->view->fetch('',compact('jar'));
  269. }
  270. //出库
  271. public function out(){
  272. // return array('status'=>1,'msg'=>'出库成功');
  273. $params = input('data/a');
  274. $inkData = input('ink/a');
  275. // print_r($params);die;
  276. //专墨出库
  277. if (!empty($params)){
  278. foreach ($params as $key=>$value){
  279. if (empty($value['weight']) || $value['weight'] == 0){
  280. continue;
  281. }else{
  282. //根据工单批次号获取对应配方、库存信息
  283. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$value["order_number"]}' limit 1");
  284. if (empty($product)){
  285. return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
  286. }
  287. // $product_number = rtrim($product[0]['Gd_cpdh']);
  288. $product_name = rtrim($product[0]['成品名称']);
  289. $warehousing = Db::name('warehousing_detail')->where('cid',$value['cid'])->where('weight','>',0)->order('id')->select();
  290. if (empty($warehousing)){
  291. return array('status'=>0,'msg'=>'库存重量为0,出库失败');
  292. }
  293. //插入一条出库数据
  294. $collarData = array();
  295. $collarData['order_number'] = $value['order_number'];
  296. $collarData['product_name'] = $product_name;
  297. $collarData['warehousing'] = $warehousing[0]['bach_number'];
  298. $collarData['status'] = 1;
  299. $collarData['cid'] = $value['cid'];//2022年7月18日10:41:20 新增一条字段 cid 方便统计
  300. $collarData['weight'] = $value['weight'] * 1000;
  301. $collarData['l_status'] = 2;
  302. $collarData['machine_number'] = $value['machine_number'];
  303. $collarData['create'] = date('Y-m-d H:i:s');
  304. $collarId = Db::name('collar_use')->insertGetId($collarData);
  305. //扣减库存,递归模式,直到$warehousing[x]['weight] - $weight(这个值是各个详细重量减去后的值) > 0, 返回数量,扣减这个数量之前所有的库存
  306. if(count($warehousing) == 1){
  307. $newWeight = $warehousing[0]['weight'] - $collarData['weight'];
  308. if ($newWeight < 0){
  309. Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',0);
  310. }else{
  311. Db::name('warehousing_detail')->where('id',$warehousing[0]['id'])->setField('weight',$newWeight);
  312. }
  313. }else{
  314. $key = $this->whatNumber($collarData['weight'],$warehousing);
  315. $weight = 0;
  316. $idArray = [];
  317. $collarBach = '';//领走的批次号
  318. $collarWeight= '';//领走的批次号重量
  319. for ($i=0;$i<$key;$i++){
  320. $weight = $weight + $warehousing[$i]['weight'];
  321. $idArray[$i] = $warehousing[$i]['id'];
  322. //2022年3月26日11:09:38 新增
  323. $collarBach = $warehousing[$i]['bach_number'].','.$collarBach;//将领走的各个批次号做成字符串
  324. $collarWeight = $warehousing[$i]['weight'].','.$collarWeight;//将领走的各个批次号重量做成字符串
  325. }
  326. $weight = $collarData['weight'] - $weight ; //这个值是大于0的 将前面的库存扣减为0 这个按值扣减
  327. $newWeight = $warehousing[$key]['weight'] - $weight;
  328. //2022年3月26日11:09:44 新增
  329. $collarBach = $warehousing[$key]['bach_number'].','.$collarBach;
  330. $collarWeight = $weight.','.$collarWeight;
  331. // $collarData['process'] = $collarBach;//暂时将此字段作为领走时的专墨批次号字段
  332. // $collarData['current_process'] = $collarWeight;//暂时将此字段作为领走时的专墨批次号字段
  333. Db::name('collar_use')->where('id',$collarId)->setField('process',$collarBach);
  334. Db::name('collar_use')->where('id',$collarId)->setField('current_process',$collarWeight);
  335. if ($newWeight < 0){
  336. $newWeight = 0;
  337. }
  338. Db::name('warehousing_detail')->where('id','in',$idArray)->setField('weight',0);
  339. Db::name('warehousing_detail')->where('id',$warehousing[$key]['id'])->setField('weight',$newWeight);
  340. }
  341. //扣减总库存
  342. $warehousingWeight = Db::name('warehousing')->where('cid',$value['cid'])->value('weight');
  343. $totalWeight = $warehousingWeight - $collarData['weight'];
  344. if ($totalWeight < 0){
  345. Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',0);
  346. }else{
  347. Db::name('warehousing')->where('cid',$value['cid'])->setField('weight',$totalWeight);
  348. }
  349. }
  350. }
  351. }
  352. if (!empty($inkData)){
  353. foreach ($inkData as $k=>$v){
  354. //插入一条领用数据
  355. $ink = array();
  356. $ink['order_number'] = $v['order_number'];
  357. $ink['code'] = $v['code'];
  358. $ink['ink_name'] = $v['ink'];
  359. $ink['machine_number'] = $v['machine_number'];
  360. $ink['out_weight'] = $v['weight'] * 1000;
  361. $ink['status'] = 1;
  362. $ink['create'] = date('Y-m-d H:i:s');
  363. $bach = Db::name('matter_detail')->where('code',$v['code'])->value('bach');
  364. if (!empty($bach)){
  365. $ink['out'] = $bach;
  366. }else{
  367. $ink['out'] = '';
  368. }
  369. $insert = Db::name('matter_use')->insert($ink);
  370. //扣减库存
  371. if ($insert){
  372. $matter = Db::name('matter_detail')->where('code',$v['code'])->order('id')->find();
  373. $matterWeight = $matter['weight'] - $ink['out_weight'];
  374. Db::name('matter_detail')->where('id',$matter['id'])->setField('weight',$matterWeight);
  375. $t_matter = Db::name('matter')->where('code',$v['code'])->find();
  376. $t_weight = $t_matter['weight'] - $ink['out_weight'];
  377. Db::name('matter')->where('id',$t_matter['id'])->setField('weight',$t_weight);
  378. }
  379. }
  380. }
  381. return array('status'=>1,'msg'=>'出库成功');
  382. }
  383. //根据生产批次去线上库获取领用记录
  384. public function getOrder(){
  385. $params = input('order_number');
  386. if (!empty($params)){
  387. $where['a.status'] = 1;
  388. $time = date('Y-m-d H:i:s');
  389. $lastTime = date('Y-m-d H:i:s',strtotime($time) - 60);//上一分钟
  390. $data = Db::name('collar_use')->alias('a')
  391. ->join('warehousing_detail b','a.warehousing = b.bach_number')
  392. ->where('a.order_number',$params)
  393. ->where($where)
  394. ->where('a.create','between',[$lastTime,$time])
  395. ->field('a.*,b.color')
  396. ->select();
  397. return array('status'=>1,'data'=>$data);
  398. }
  399. }
  400. //获取机台号
  401. public function getWeight(){
  402. $params = input('bach');
  403. if (empty($params)){
  404. return array('status'=>0,'msg'=>'数据不能为空');
  405. }
  406. $data = Db::name('collar_use')->where('warehousing',$params)->order('id desc')->find();
  407. $machine = $data['machine_number'].'#';
  408. return array('status'=>1,'data'=>$machine);
  409. }
  410. //根据工单号获取配方
  411. public function getColor(){
  412. $params = input('order');
  413. if (empty($params)){
  414. return array('status'=>0,'msg'=>'数据不能为空');
  415. }
  416. //根据工单批次号获取对应配方、库存信息
  417. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  418. if (empty($product)){
  419. return array('status'=>0,'msg'=>'查不到工单数据,请联系管理员');
  420. }
  421. $product_number = rtrim($product[0]['Gd_cpdh']);
  422. //修改前
  423. // $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  424. // $colorResult = Db::name('formula')
  425. // ->where($where)
  426. // ->select();
  427. // $inkResult = Db::name('formula_consume')
  428. // ->where($where)
  429. // ->where('code','neq','')
  430. // ->field('id,ink,code')
  431. // ->select();
  432. //修改后
  433. $where[] = ['exp', Db::raw("FIND_IN_SET(:pn, product_number)")];
  434. $colorResult = Db::name('formula')
  435. ->bind(['pn' => $product_number])
  436. ->where($where)
  437. ->select();
  438. $inkResult = Db::name('formula_consume')
  439. ->bind(['pn' => $product_number])
  440. ->where($where)
  441. ->where('code', 'neq', '')
  442. ->field('id,ink,code')
  443. ->select();
  444. $inkResult = $this->assoc_unique($inkResult,'code');
  445. $machine = Db::name('collar_use')->where('order_number',$params)->order('id asc')->find();
  446. $machine_number = $machine['machine_number'].'#';
  447. return array('status'=>1,'data'=>$colorResult,'machine_number'=>$machine_number,'ink'=>$inkResult);
  448. }
  449. //二维数组去重
  450. function assoc_unique($arr, $key) {
  451. $tmp_arr = array();
  452. foreach ($arr as $k => $v) {
  453. if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
  454. unset($arr[$k]);
  455. } else {
  456. $tmp_arr[] = $v[$key];
  457. }
  458. }
  459. sort($arr); //sort函数对数组进行排序
  460. return $arr;
  461. }
  462. //扣减库存递归
  463. public function whatNumber($weight,$array=[],$i=0){
  464. $surplusWeight =$weight - $array[$i]['weight'] ;
  465. if ($surplusWeight > 0){
  466. $i = $i + 1;
  467. return $this->whatNumber($surplusWeight,$array,$i);
  468. }else{
  469. return $i;
  470. }
  471. }
  472. }