QcodeAdd.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\admin\model\QcodeProduct;
  5. use app\common\controller\Backend;
  6. use app\admin\model\ResetFlow;
  7. use app\admin\model\QcodeLarge;
  8. use app\admin\model\QcodeBach;
  9. use app\admin\model\QcodeSmall;
  10. use app\admin\model\QcodeLiushui;
  11. use fast\Arr;
  12. use think\Session;
  13. class QcodeAdd extends Backend
  14. {
  15. /**
  16. * 首页展示
  17. * @return string|\think\response\Json
  18. * @throws \think\Exception
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function index()
  24. {
  25. $company = new QcodeCompany();
  26. $product = new QcodeProduct();
  27. $userinfo = Session::get('admin');
  28. $data = [
  29. 'nickname' => $userinfo['company_name'],
  30. 'postcode' => $userinfo['postcode'],
  31. 'mobile' => $userinfo['mobile'],
  32. 'company_address' => $userinfo['company_address']
  33. ];
  34. $product_id = $company->name((int)$userinfo['company'].'_'.'qcode_company')->where('delete_time','')->column('product_id');
  35. $product_name = [];
  36. foreach ($product_id as $v){
  37. $list = $product->where('_id',$v)->where('delete_time','')->find();
  38. $product_name[$list['product_code']] = $list['product_name'];
  39. }
  40. $this->view->assign('row',$data);
  41. $this->view->assign('product',$product_name);
  42. return $this->view->fetch();
  43. }
  44. /**
  45. * 获取产品信息
  46. * @return \think\response\Json
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public function product(){
  52. $QcodeProduct = new QcodeProduct();
  53. $ResetFlow = new ResetFlow();
  54. $userinfo = Session::get('admin');
  55. if ($this->request->isAjax() === false){
  56. $this->error('请求错误');
  57. }
  58. $product_code = input('product_code');
  59. if (empty($product_code)){
  60. $this->error('参数错误');
  61. }
  62. $product = $QcodeProduct->where('product_code',$product_code)->find();
  63. if (empty($product)){
  64. $this->error('未找到该产品信息');
  65. }
  66. $flow = $ResetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',substr(json_encode($product['_id']),9,-2))->find();
  67. $row = [
  68. 'temple' => $product['temple'],
  69. 'flow' => isset($flow['l_flow'])?(int)$flow['l_flow']+1:'',
  70. 'bach' => isset($flow['bach_num'])?(int)$flow['bach_num']+1:'',
  71. ];
  72. if (!empty($flow)){
  73. $row['flow'] = $flow['l_flow']+1;
  74. $row['bach'] = $flow['bach_num']+1;
  75. }
  76. return json(['code'=>1,'data'=>$row]);
  77. }
  78. /**
  79. * 增加新批次
  80. * @return void
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. public function add()
  86. {
  87. $product = new QcodeProduct();
  88. $bach = new QcodeBach();
  89. $large = new QcodeLarge();
  90. $small = new QcodeSmall();
  91. $liushui = new QcodeLiushui();
  92. $resetFlow = new ResetFlow();
  93. if ($this->request->isAjax() === false){
  94. $this->error('请求错误');
  95. }
  96. $rows = input('row');
  97. if (empty($rows)){
  98. $this->error('参数错误');
  99. }
  100. $rows = json_decode($rows);
  101. $data = [];
  102. foreach ($rows as $value){
  103. foreach ($value as $k=>$v){
  104. $data[$k] = $v;
  105. }
  106. }
  107. if ($data['danwei'] == 1){
  108. $num = $data['number'];
  109. $tray_num = $data['tray_num'];
  110. $box_number = $data['box_number'];
  111. $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
  112. $large_num = (int)ceil($small_num/$tray_num);
  113. }else{
  114. $num = 0;
  115. $tray_num = $data['volume_num'];
  116. $small_num = $data['small_num'];
  117. $large_num = (int)ceil($small_num/$tray_num);
  118. $box_number = 1;
  119. }
  120. //插入批次信息
  121. $userinfo = Session::get('admin');//获取用户信息
  122. $productList = $product->where('product_code',$data['product_code'])->find();//获取材料信息
  123. if (empty($productList)){
  124. $this->error('未找到该产品数据');
  125. }
  126. $batchList = [
  127. 'supplier_name' => $data['company_name'],
  128. 'supplier_code' => $userinfo['printer_code'],
  129. 'supplier_id' => $userinfo['id'],
  130. 'matter_name' => $productList['product_name'],
  131. 'matter_no' => $productList['product_code'],
  132. 'matter_id' => substr(json_encode($productList['_id']),9,-2),
  133. 'matter_type' => $productList['temple'],
  134. 'num' => $num,
  135. 'larger_num' => $large_num,
  136. 'small_num' => $small_num,
  137. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),
  138. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),
  139. 'box_num' => $box_number,
  140. 'tray_num' => $tray_num,
  141. 'l_reservation' => '',
  142. 'l_flow' => $data['big_liushui'],
  143. 'l_weight' => $data['big_weight'],
  144. 's_flow' => $data['small_start_liushui'],
  145. 's_weight' => $data['small_weight'],
  146. 's_reservation' => '',
  147. 'bach_status' => 0,
  148. 'userid' => $userinfo['id'],
  149. 'bach_num' => $data['batch'],
  150. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  151. 'create_time' => time(),
  152. ];
  153. $res = $bach->save($batchList);
  154. if ($res === 0){
  155. $this->error('添加失败');
  156. }
  157. $flow = [
  158. 'l_flow' => (int)$batchList['large_endnum'],
  159. 'bach_num' => $batchList['bach_num'],
  160. ];
  161. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  162. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  163. }else{
  164. $flow['product_id'] = $batchList['matter_id'];
  165. $resetFlow->save($flow);
  166. }
  167. $last_id = $bach->getLastInsID();
  168. if ($last_id){
  169. //插入大小二维码数据
  170. //二维码数据不变区域
  171. $fixed_code = '';
  172. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  173. $fixed_code.=$this->intTochar($batchList['supplier_code'],12);//12位 供应商编码
  174. $fixed_code.=$this->intTochar($batchList['matter_no'],20);//20位 辅料编码
  175. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  176. $print_code=$batchList['print_date'];//6位 打码日期
  177. $small_liushui = [
  178. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  179. 'last_num' => 0,
  180. 'user_id' => $userinfo['id'],
  181. 'stype' => 2,
  182. 'dateTime' => time(),
  183. ];
  184. $whereSmall = [
  185. 'onlycode' => $small_liushui['onlycode'],
  186. 'user_id' => $userinfo['id'],
  187. ];
  188. if ($liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  189. //小件二维码存在,更新小件二维码最后流水号
  190. $lastNum = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find();
  191. }else{
  192. //小件二维码不存在,新增记录
  193. $liushui->save($small_liushui);
  194. $lastNum['last_num'] = 1;
  195. }
  196. //循环插入大件二维码数据
  197. for ($i=0;$i<$batchList['larger_num'];$i++){
  198. $large = new QcodeLarge();
  199. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  200. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  201. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  202. $l_reservation = $l_reservation.'0000000000';
  203. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  204. if ($remainder < $batchList['tray_num']){
  205. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  206. }else{
  207. $small_n = $this->intTochar($batchList['tray_num'],3);
  208. }
  209. $l_num = 0;
  210. if ($data['danwei'] == 1){
  211. //以箱为单位时
  212. $l_num = $small_n * $batchList['box_num'];
  213. }
  214. //大件二维码数据
  215. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  216. //大码数据信息
  217. $l_data = [
  218. 'bach_id' => $last_id,
  219. 'code' => $code_data['code'],
  220. 'code_cp1' => $code_data['code_cp1'],
  221. 'code_cp2' => $code_data['code_cp2'],
  222. 'print_date' => $print_code,
  223. 'create_time' => time(),
  224. 'p_nums' => 0,
  225. 'userid' =>$userinfo['id'],
  226. 'l_weight' =>$batchList['l_weight'],
  227. 'l_num' =>$l_num,
  228. 'l_status' => 0,
  229. 'l_print' => 0
  230. ];
  231. $l_res = $large->save($l_data);
  232. if ($l_res === 0){
  233. $this->error('大件码插入失败');
  234. }
  235. $large_id = $large->getLastInsID();
  236. if ($large_id){
  237. // //小件码循环插入
  238. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  239. $small = new QcodeSmall();
  240. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$batchList['tray_num']+$lastNum['last_num'],6);//小件码序号从1开始
  241. $s_weight = $this->intTochar($batchList['s_weight'],6);
  242. $small_sign = '000';
  243. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  244. $s_reservation = $s_reservation . '0000000000';
  245. //生成小件码
  246. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  247. //小码数据
  248. $s_data = [
  249. 'large_id'=>$large_id,
  250. 'bach_id'=>$last_id,
  251. 'code'=>$small_code_data['code'],
  252. 'code_cp1'=>$small_code_data['code_cp1'],
  253. 'code_cp2'=>$small_code_data['code_cp2'],
  254. 'l_flow'=>$j+1,
  255. 'print_date'=>$print_code,
  256. 'create_time'=>time(),
  257. 'p_nums'=>0,
  258. 'userid'=>$userinfo['id'],//小码绑定用户id
  259. 's_weight'=>$s_weight,//单个小件重量
  260. 'status' => 0,
  261. ];
  262. $s_res = $small->save($s_data);
  263. if ($s_res === 0){
  264. $this->error('小件码插入失败');
  265. }
  266. }
  267. }else{
  268. $this->error('添加失败');
  269. }
  270. }
  271. }
  272. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  273. if ($liushui_res === false){
  274. $this->error('添加失败');
  275. }
  276. $this->success('成功');
  277. }
  278. /**
  279. * 编码补位
  280. * @param $num
  281. * @param $len
  282. * @return string
  283. */
  284. function intTochar($num=0,$len){
  285. //规定的不足的时候自动补足零
  286. $code=(string)$num;
  287. $buwei='';
  288. if(strlen($code)<$len){
  289. for($i=strlen($code);$i<$len;$i++){
  290. $buwei.='0';
  291. }
  292. }
  293. return $buwei.$code;
  294. }
  295. /**
  296. * 二维码编码生成
  297. * @param $sign
  298. * @param $fixed_code
  299. * @param $small_num
  300. * @param $print_date
  301. * @param $flow
  302. * @param $weight
  303. * @param $large_sign
  304. * @param $reservation
  305. * @return array
  306. */
  307. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  308. $code=$sign;//4 位固定标志位
  309. $code.=$fixed_code; // 固定字符串
  310. $code.=$small_num;//3位 小件数量
  311. $code.=$print_date;//6 位 日期
  312. $code.=$flow;//6位打印流水号
  313. $code.=$weight;//6位辅料重量
  314. $code.=$large_sign;//大小件标示位
  315. $code.=$reservation;//20 位 预留号
  316. //大码数据信息
  317. $data=[
  318. 'code'=>str_replace(" ","",$code),
  319. 'code_cp1'=>$print_date.$flow,
  320. 'code_cp2'=>$weight.$reservation,//20位补充
  321. 'print_date'=>time(),
  322. 'p_nums'=>0,
  323. ];
  324. return $data;
  325. }
  326. }