MatterUse.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 原墨领用退还管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class MatterUse extends Backend
  12. {
  13. /**
  14. * MatterUse模型对象
  15. * @var \app\admin\model\MatterUse
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\MatterUse;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("isScrapList", $this->model->getIsScrapList());
  24. $this->view->assign("lStatusList", $this->model->getLStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. public function index(){
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax()) {
  35. // $params = $this->request->request();
  36. // print_r($params);die;
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->where($where)
  44. ->order($sort, $order)
  45. ->count();
  46. $list = $this->model
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->limit($offset, $limit)
  50. ->select();
  51. $list = collection($list)->toArray();
  52. foreach ($list as $key=>$value){
  53. $list[$key]['out_weight'] = $value['out_weight']/1000;
  54. $list[$key]['back_weight'] = $value['back_weight']/1000;
  55. }
  56. $result = array("total" => $total, "rows" => $list);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. $params['status'] = 1;
  66. $params['out_weight'] = $params['out_weight'] * 1000;
  67. $params['create'] = date('Y-m-d H:i:s');
  68. Db::startTrans();
  69. try{
  70. //查询nfc表是否有数据
  71. $nfc = Db::name('bnfc')->whereTime('create','today')->where('status',0)->order('id desc')->find();
  72. if (empty($nfc)){
  73. throw new Exception('没有查询到nfc标签,请贴nfc标签并扫描');
  74. }
  75. //将绑定的nfc插入到nfc总表,绑定到领用的数据上
  76. $nfcData = array();
  77. $nfcData['content'] = preg_replace('# #', '',$nfc['content']);
  78. $nfcData['b_nfc'] = $nfc['id'];
  79. $nfcData['create'] = date('Y-m-d H:i:s');
  80. $nfc_id = Db::name('nfc')->insertGetId($nfcData);
  81. if (empty($nfc_id)){
  82. throw new Exception('插入数据失败,请联系开发人员');
  83. }
  84. $info = Db::name('matter_detail')->where('code',$params['code'])->order('id desc')->find();
  85. $params['ink_name'] = $info['name'];
  86. $params['out'] = $info['bach'];
  87. $matter_id = Db::name('matter_use')->insertGetId($params);
  88. if (empty($matter_id)){
  89. throw new Exception('插入领用数据失败,请联系开发人员');
  90. }
  91. //绑定
  92. Db::name('matter_use')->where('id',$matter_id)->setField('nfc',$nfc_id);
  93. //将ndc状态改为已绑定
  94. Db::name('nfc')->where('id',$nfc_id)->setField('status',2);
  95. //将bnfc状态更改
  96. $bnfcData = array();
  97. $bnfcData['status'] = 1;
  98. $bnfcData['content'] = $nfcData['content'];
  99. $bnfcData['update'] = date('Y-m-d H:i:s');
  100. Db::name('bnfc')->where('id',$nfc['id'])->update($bnfcData);
  101. Db::commit();
  102. $this->success();
  103. }catch (\think\Exception\DbException $exception){
  104. Db::rollback();
  105. $this->error($exception->getMessage());
  106. }
  107. }
  108. return $this->view->fetch();
  109. }
  110. //出库
  111. public function out(){
  112. if ($this->request->isPost()){
  113. $params = input('order_number');
  114. $order_number = substr($params,0,7);
  115. $process = substr($params,8,3);
  116. $current_process = substr($params,12,3);
  117. $machine_number = input('machine_number');
  118. Db::startTrans();
  119. try{
  120. $cnfc = Db::name('cnfc')->where('status',0)->whereTime('create','today')->order('id desc')->select();
  121. if (empty($cnfc)){
  122. throw new Exception('暂无数据');
  123. }
  124. $data = array();
  125. //查出所有机台号nfc,排除人员信息
  126. $allMachine = Db::name('machine_nfc')->column('content');
  127. foreach ($cnfc as $key => $value){
  128. $value['content'] = preg_replace('# #', '',$value['content']);
  129. if (in_array($value['content'],$allMachine)){
  130. continue;
  131. // $machine_number = Db::name('machine_nfc')->where('content',$value['content'])->value('machine_number');
  132. }else{
  133. $data[$key] = Db::name('nfc')->where('content',$value['content'])->where('status',2)->value('id');
  134. if (empty($data[$key])){
  135. throw new Exception('未查询到nfc数据');
  136. }
  137. }
  138. $changeNfc = Db::name('nfc')->where('content',$value['content'])->where('status',2)->setField('c_nfc',$value['id']);
  139. Db::name('nfc')->where('content',$value['content'])->setField('status',3);
  140. if (!$changeNfc){
  141. throw new Exception('更改nfc出库id失败');
  142. }
  143. $changeCnfc = Db::name('cnfc')->where('id',$value['id'])->setField('status',1);
  144. $changeContent = Db::name('cnfc')->where('id',$value['id'])->setField('content',$value['content']);
  145. $changeUpdate = Db::name('cnfc')->where('id',$value['id'])->setField('update',date('Y-m-d H:i:s'));
  146. if (!$changeCnfc || !$changeUpdate){
  147. throw new Exception('更改cnfc状态失败');
  148. }
  149. }
  150. //专色墨领用的记录
  151. // $collar = Db::name('collar_use')->where('nfc','in',$data)->where('l_status',1)->where('status',1)->select();
  152. $matter = Db::name('matter_use')->where('nfc','in',$data)->where('l_status',1)->where('status',1)->select();
  153. if (!empty($matter)){
  154. //扣减油墨重量,更改领用状态,更改nfc状态
  155. foreach ($matter as $k1=>$v1){
  156. $totalWeight = Db::name('matter')->where('code',$v1['code'])->find();
  157. $matterWeight = $totalWeight['weight'] - $v1['out_weight'];
  158. $res = Db::name('matter')->where('id',$totalWeight['id'])->setField('weight',$matterWeight);
  159. //更改详细库存
  160. $detailWeight = Db::name('matter_detail')->where('code',$v1['code'])->order('weight desc')->find();
  161. $newDetailWeight = $detailWeight['weight'] - $v1['out_weight'];
  162. $resDetail = Db::name('matter_detail')->where('id',$detailWeight['id'])->setField('weight',$newDetailWeight);
  163. if (!$res){
  164. throw new Exception('扣减油墨库存重量失败');
  165. }
  166. $matterUpdate= array();
  167. $matterUpdate['l_status'] = 2;
  168. $matterUpdate['process'] = $process;
  169. $matterUpdate['current_process'] = $current_process;
  170. $matterUpdate['machine_number'] = $machine_number;
  171. $result = Db::name('matter_use')->where('id',$v1['id'])->update($matterUpdate);
  172. if (!$result){
  173. throw new Exception('更新领用状态失败');
  174. }
  175. }
  176. }else{
  177. throw new Exception('未查询到领用数据或此批次已出库');
  178. }
  179. // if (!empty($collar)){
  180. // //扣减专色墨重量,更改领用状态,更改nfc状态
  181. // foreach ($collar as $k=>$v){
  182. // //根据批次号去查详细重量和总重量
  183. // $detail = Db::name('warehousing_detail')->where('bach_number',$v['warehousing'])->find();
  184. // $detailWeight = $detail['weight'] - $v['weight'];
  185. // //根据产品编号和专色墨颜色,查出总重量,扣减
  186. // $total = Db::name('warehousing')->where('product_number',$detail['product_number'])->where('cid',$detail['cid'])->find();
  187. // $totalWeight = $total['weight'] - $v['weight'];
  188. // $detailRes = Db::name('warehousing_detail')->where('bach_number',$v['warehousing'])->setField('weight',$detailWeight);
  189. // if (!$detailRes){
  190. // throw new Exception('扣减详细库存重量失败');
  191. // }
  192. // $totalRes = Db::name('warehousing')->where('product_number',$detail['product_number'])->where('cid',$detail['cid'])->setField('weight',$totalWeight);
  193. // if (!$totalRes){
  194. // throw new Exception('扣减总库存重量失败');
  195. // }
  196. // $update = array();
  197. // $update['l_status'] = 2;
  198. // $update['process'] = $process;
  199. // $update['current_process'] = $current_process;
  200. // $update['machine_number'] = $machine_number;
  201. // $changData = Db::name('collar_use')->where('id',$v['id'])->update($update);
  202. // if (!$changData){
  203. // throw new Exception('更改数据失败');
  204. // }
  205. // }
  206. // }
  207. Db::commit();
  208. $this->success();
  209. }catch (\think\Exception\DbException $exception){
  210. Db::rollback();
  211. $this->error($exception->getMessage());
  212. }
  213. }
  214. return $this->view->fetch();
  215. }
  216. //退回
  217. public function back(){
  218. if ($this->request->isPost()){
  219. $params = input('row/a');
  220. $params = $this->preExcludeFields($params);
  221. if (!is_numeric($params['back_weight'])){
  222. $this->error('重量只能为数字');
  223. }
  224. $params['back_weight'] = $params['back_weight'] * 1000;
  225. //暂时退回锁定不是下机墨
  226. $params['is_scrap'] = 0;
  227. Db::startTrans();
  228. try{
  229. //先去查tnfc有没有数据
  230. $nfc = Db::name('tnfc')->whereTime('create','today')->where('status',0)->order('id desc')->find();
  231. if (empty($nfc)){
  232. throw new Exception('没有查询到nfc标签');
  233. }
  234. //去nfc总表查出id
  235. $nfcData = preg_replace('# #', '',$nfc['content']);
  236. $nfcId = Db::name('nfc')->where('content',$nfcData)->order('id desc')->value('id');
  237. $matterData = Db::name('matter_use')->where('nfc',$nfcId)->where('out_weight','neq','')->find();
  238. $backData = array();
  239. $backData['order_number'] = $matterData['order_number'];
  240. $backData['code'] = $matterData['code'];
  241. $backData['ink_name'] = $matterData['ink_name'];
  242. $backData['back'] = $matterData['out'];
  243. $backData['back_weight'] = $params['back_weight'];
  244. $backData['nfc'] = $nfcId;
  245. $backData['l_status'] = 2;
  246. $backData['status'] = 2;
  247. $backData['machine_number'] = $matterData['machine_number'];
  248. $backData['create'] = date('Y-m-d H:i:s');
  249. $backResult = Db::name('matter_use')->insert($backData);
  250. if (!$backResult){
  251. throw new Exception('插入数据失败,请联系开发人员');
  252. }
  253. //将ndc状态改为已绑定
  254. Db::name('nfc')->where('id',$nfcId)->setField('status',4);
  255. //将tnfc状态更改
  256. $tnfcData = array();
  257. $tnfcData['status'] = 1;
  258. $tnfcData['content'] = $nfcData;
  259. $tnfcData['update'] = date('Y-m-d H:i:s');
  260. Db::name('tnfc')->where('id',$nfc['id'])->update($tnfcData);
  261. //增加库存
  262. $detail = Db::name('matter_detail')->where('code',$matterData['code'])->order('id desc')->find();
  263. $detailWeight = $detail['weight'] + $params['back_weight'];
  264. $total = Db::name('matter')->where('code',$matterData['code'])->find();
  265. $totalWeight = $total['weight'] + $params['back_weight'];
  266. $detailResult = Db::name('matter_detail')->where('id',$detail['id'])->setField('weight',$detailWeight);
  267. $totalResult = Db::name('matter')->where('id',$total['id'])->setField('weight',$totalWeight);
  268. if (empty($detailResult) || empty($totalResult)){
  269. throw new Exception('退库失败');
  270. }
  271. Db::commit();
  272. $this->success();
  273. }catch (\think\Exception\DbException $exception){
  274. Db::rollback();
  275. $this->error($exception->getMessage());
  276. }
  277. }
  278. return $this->view->fetch();
  279. }
  280. public function getData(){
  281. $params = input('');
  282. if (empty($params['id'])){
  283. return array('status'=>0,'msg'=>'请选中打印数据');
  284. }
  285. $res = Db::name('matter_use')->where('id',$params['id'])->find();
  286. if (!empty($res)){
  287. return array('status'=>1,'data'=>$res);
  288. }else{
  289. return array('status'=>0,'msg'=>'数据错误');
  290. }
  291. }
  292. //根据生产批次号获取原墨重量
  293. public function getCode(){
  294. $code = input('code');
  295. if (empty($code)){
  296. return array('status'=>0,'msg'=>'参数错误');
  297. }
  298. $productInfo = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = $code limit 1");
  299. $productNumber = $productInfo[0]['Gd_cpdh'];
  300. $productName = $productInfo[0]['Gd_cpmc'];
  301. //在大小盒计划消耗表查出所有原墨,去掉数据里的专色墨
  302. $where[] = ['exp',Db::raw("FIND_IN_SET($productNumber,product_number)")];
  303. $result = Db::name('formula_consume')->where($where)->field('ink,code')->select();
  304. //查出所有专色墨
  305. $warehousing = Db::name('formula')->where($where)->column('color');
  306. foreach ($result as $key=>$value){
  307. if (in_array($value['ink'],$warehousing) || empty($value['ink'])){
  308. unset($result[$key]);
  309. }else{
  310. $result[$key]['weight'] = Db::name('matter')->where('code',$value['code'])->value('weight');
  311. if ($result[$key]['weight'] == null){
  312. $result[$key]['weight'] = 0;
  313. }
  314. }
  315. }
  316. $result = array_merge($result);
  317. if ($result){
  318. return array('status'=>1,'msg'=>'请求成功','result'=>$result,'name'=>$productName);
  319. }else{
  320. return array('status'=>0,'msg'=>'请求失败');
  321. }
  322. }
  323. }