QcodeAdd.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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['l_flow'],
  159. 'bach_num' => $batchList['bach_num'],
  160. ];
  161. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  162. $last_id = $bach->getLastInsID();
  163. if ($last_id){
  164. //插入大小二维码数据
  165. //二维码数据不变区域
  166. $fixed_code = '';
  167. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  168. $fixed_code.=$this->intTochar($batchList['supplier_code'],12);//12位 供应商编码
  169. $fixed_code.=$this->intTochar($batchList['matter_no'],20);//20位 辅料编码
  170. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  171. $print_code=$batchList['print_date'];//6位 打码日期
  172. $small_liushui = [
  173. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  174. 'last_num' => 0,
  175. 'user_id' => $userinfo['id'],
  176. 'stype' => 2,
  177. 'dateTime' => time(),
  178. ];
  179. $whereSmall = [
  180. 'onlycode' => $small_liushui['onlycode'],
  181. 'user_id' => $userinfo['id'],
  182. ];
  183. if ($liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  184. //小件二维码存在,更新小件二维码最后流水号
  185. $lastNum = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find();
  186. }else{
  187. //小件二维码不存在,新增记录
  188. $liushui->save($small_liushui);
  189. $lastNum['last_num'] = 1;
  190. }
  191. //循环插入大件二维码数据
  192. for ($i=0;$i<$batchList['larger_num'];$i++){
  193. $large = new QcodeLarge();
  194. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  195. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  196. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  197. $l_reservation = $l_reservation.'0000000000';
  198. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  199. if ($remainder < $batchList['tray_num']){
  200. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  201. }else{
  202. $small_n = $this->intTochar($batchList['tray_num'],3);
  203. }
  204. $l_num = 0;
  205. if ($data['danwei'] == 1){
  206. //以箱为单位时
  207. $l_num = $small_n * $batchList['box_num'];
  208. }
  209. //大件二维码数据
  210. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  211. //大码数据信息
  212. $l_data = [
  213. 'bach_id' => $last_id,
  214. 'code' => $code_data['code'],
  215. 'code_cp1' => $code_data['code_cp1'],
  216. 'code_cp2' => $code_data['code_cp2'],
  217. 'print_date' => $print_code,
  218. 'create_time' => time(),
  219. 'p_nums' => 0,
  220. 'userid' =>$userinfo['id'],
  221. 'l_weight' =>$l_weight,
  222. 'l_num' =>$l_num,
  223. 'l_status' => 0,
  224. 'l_print' => 0
  225. ];
  226. $l_res = $large->save($l_data);
  227. if ($l_res === 0){
  228. $this->error('大件码插入失败');
  229. }
  230. $large_id = $large->getLastInsID();
  231. if ($large_id){
  232. // //小件码循环插入
  233. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  234. $small = new QcodeSmall();
  235. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$batchList['tray_num']+$lastNum['last_num'],6);//小件码序号从1开始
  236. $s_weight = $this->intTochar($batchList['s_weight'],6);
  237. $small_sign = '000';
  238. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  239. $s_reservation = $s_reservation . '0000000000';
  240. //生成小件码
  241. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  242. //小码数据
  243. $s_data = [
  244. 'large_id'=>$large_id,
  245. 'bach_id'=>$last_id,
  246. 'code'=>$small_code_data['code'],
  247. 'code_cp1'=>$small_code_data['code_cp1'],
  248. 'code_cp2'=>$small_code_data['code_cp2'],
  249. 'l_flow'=>$j+1,
  250. 'print_date'=>$print_code,
  251. 'create_time'=>time(),
  252. 'p_nums'=>0,
  253. 'userid'=>$userinfo['id'],//小码绑定用户id
  254. 's_weight'=>$s_weight,//单个小件重量
  255. 'status' => 0,
  256. ];
  257. $s_res = $small->save($s_data);
  258. if ($s_res === 0){
  259. $this->error('小件码插入失败');
  260. }
  261. }
  262. }else{
  263. $this->error('添加失败');
  264. }
  265. }
  266. }
  267. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  268. if ($liushui_res === false){
  269. $this->error('添加失败');
  270. }
  271. $this->success('成功');
  272. }
  273. /**
  274. * 编码补位
  275. * @param $num
  276. * @param $len
  277. * @return string
  278. */
  279. function intTochar($num=0,$len){
  280. //规定的不足的时候自动补足零
  281. $code=(string)$num;
  282. $buwei='';
  283. if(strlen($code)<$len){
  284. for($i=strlen($code);$i<$len;$i++){
  285. $buwei.='0';
  286. }
  287. }
  288. return $buwei.$code;
  289. }
  290. /**
  291. * 二维码编码生成
  292. * @param $sign
  293. * @param $fixed_code
  294. * @param $small_num
  295. * @param $print_date
  296. * @param $flow
  297. * @param $weight
  298. * @param $large_sign
  299. * @param $reservation
  300. * @return array
  301. */
  302. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  303. $code=$sign;//4 位固定标志位
  304. $code.=$fixed_code; // 固定字符串
  305. $code.=$small_num;//3位 小件数量
  306. $code.=$print_date;//6 位 日期
  307. $code.=$flow;//6位打印流水号
  308. $code.=$weight;//6位辅料重量
  309. $code.=$large_sign;//大小件标示位
  310. $code.=$reservation;//20 位 预留号
  311. //大码数据信息
  312. $data=[
  313. 'code'=>str_replace(" ","",$code),
  314. 'code_cp1'=>$print_date.$flow,
  315. 'code_cp2'=>$weight.$reservation,//20位补充
  316. 'print_date'=>time(),
  317. 'p_nums'=>0,
  318. ];
  319. return $data;
  320. }
  321. }