QcodeAdd.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. $tray_num1 = $data['tray_num'];
  111. $box_number = $data['box_number'];
  112. $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
  113. $large_num = (int)ceil($small_num/$tray_num);
  114. }else{
  115. $num = 0;
  116. $tray_num = $data['volume_num'];
  117. $small_num = $data['small_num'];
  118. $large_num = (int)ceil($small_num/$tray_num);
  119. $box_number = 1;
  120. $tray_num1 = 1;
  121. }
  122. //插入批次信息
  123. $userinfo = Session::get('admin');//获取用户信息
  124. $productList = $product->where('product_code',$data['product_code'])->find();//获取材料信息
  125. if (empty($productList)){
  126. $this->error('未找到该产品数据');
  127. }
  128. $batchList = [
  129. 'supplier_name' => $data['company_name'],
  130. 'supplier_code' => $userinfo['printer_code'],
  131. 'supplier_id' => $userinfo['id'],
  132. 'matter_name' => $productList['product_name'],
  133. 'matter_no' => $productList['product_code'],
  134. 'matter_id' => substr(json_encode($productList['_id']),9,-2),
  135. 'matter_type' => $productList['temple'],
  136. 'num' => $num,
  137. 'larger_num' => $large_num,
  138. 'small_num' => $small_num,
  139. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),
  140. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),
  141. 'box_num' => $box_number,
  142. 'tray_num' => $tray_num1,
  143. 'l_reservation' => '',
  144. 'l_flow' => $data['big_liushui'],
  145. 'l_weight' => $data['big_weight'],
  146. 's_flow' => $data['small_start_liushui'],
  147. 's_weight' => $data['small_weight'],
  148. 's_reservation' => '',
  149. 'bach_status' => 0,
  150. 'userid' => $userinfo['id'],
  151. 'bach_num' => $data['batch'],
  152. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  153. 'create_time' => time(),
  154. ];
  155. $res = $bach->save($batchList);
  156. if ($res === 0){
  157. $this->error('添加失败');
  158. }
  159. $flow = [
  160. 'l_flow' => (int)$batchList['large_endnum'],
  161. 'bach_num' => $batchList['bach_num'],
  162. ];
  163. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  164. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  165. }else{
  166. $flow['product_id'] = $batchList['matter_id'];
  167. $resetFlow->save($flow);
  168. }
  169. $last_id = $bach->getLastInsID();
  170. if ($last_id){
  171. //插入大小二维码数据
  172. //二维码数据不变区域
  173. $fixed_code = '';
  174. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  175. $fixed_code.=$this->intTochar($batchList['supplier_code'],12);//12位 供应商编码
  176. $fixed_code.=$this->intTochar($batchList['matter_no'],20);//20位 辅料编码
  177. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  178. $print_code=$batchList['print_date'];//6位 打码日期
  179. $small_liushui = [
  180. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  181. 'last_num' => 0,
  182. 'user_id' => $userinfo['id'],
  183. 'stype' => 2,
  184. 'dateTime' => time(),
  185. ];
  186. $whereSmall = [
  187. 'onlycode' => $small_liushui['onlycode'],
  188. 'user_id' => $userinfo['id'],
  189. ];
  190. if ($liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  191. //小件二维码存在,更新小件二维码最后流水号
  192. $lastNum = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->find();
  193. }else{
  194. //小件二维码不存在,新增记录
  195. $liushui->save($small_liushui);
  196. $lastNum['last_num'] = 0;
  197. }
  198. //循环插入大件二维码数据
  199. for ($i=0;$i<$batchList['larger_num'];$i++){
  200. $large = new QcodeLarge();
  201. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  202. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  203. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  204. $l_reservation = $l_reservation.'0000000000';
  205. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  206. if ($remainder < $batchList['tray_num']){
  207. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  208. }else{
  209. $small_n = $this->intTochar($batchList['tray_num'],3);
  210. }
  211. $l_num = 0;
  212. if ($data['danwei'] == 1){
  213. //以箱为单位时
  214. $l_num = $small_n * $batchList['box_num'];
  215. }
  216. //大件二维码数据
  217. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  218. //大码数据信息
  219. $l_data = [
  220. 'bach_id' => $last_id,
  221. 'code' => $code_data['code'],
  222. 'code_cp1' => $code_data['code_cp1'],
  223. 'code_cp2' => $code_data['code_cp2'],
  224. 'print_date' => $print_code,
  225. 'create_time' => time(),
  226. 'p_nums' => 0,
  227. 'userid' =>$userinfo['id'],
  228. 'l_weight' =>$batchList['l_weight'],
  229. 'l_num' =>$l_num,
  230. 'l_status' => 0,
  231. 'l_print' => 0
  232. ];
  233. $l_res = $large->save($l_data);
  234. if ($l_res === 0){
  235. $this->error('大件码插入失败');
  236. }
  237. $large_id = $large->getLastInsID();
  238. if ($large_id){
  239. // //小件码循环插入
  240. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  241. $small = new QcodeSmall();
  242. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
  243. $s_weight = $this->intTochar($batchList['s_weight'],6);
  244. $small_sign = '000';
  245. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  246. $s_reservation = $s_reservation . '0000000000';
  247. //生成小件码
  248. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  249. //小码数据
  250. $s_data = [
  251. 'large_id'=>$large_id,
  252. 'bach_id'=>$last_id,
  253. 'code'=>$small_code_data['code'],
  254. 'code_cp1'=>$small_code_data['code_cp1'],
  255. 'code_cp2'=>$small_code_data['code_cp2'],
  256. 'l_flow'=>$j+1,
  257. 'print_date'=>$print_code,
  258. 'create_time'=>time(),
  259. 'p_nums'=>0,
  260. 'userid'=>$userinfo['id'],//小码绑定用户id
  261. 's_weight'=>$batchList['s_weight'],//单个小件重量
  262. 'status' => 0,
  263. ];
  264. $s_res = $small->save($s_data);
  265. if ($s_res === 0){
  266. $this->error('小件码插入失败');
  267. }
  268. }
  269. }else{
  270. $this->error('添加失败');
  271. }
  272. }
  273. }
  274. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  275. if ($liushui_res === false){
  276. $this->error('添加失败');
  277. }
  278. $this->success('成功');
  279. }
  280. /**
  281. * 编码补位
  282. * @param $num
  283. * @param $len
  284. * @return string
  285. */
  286. function intTochar($num=0,$len){
  287. //规定的不足的时候自动补足零
  288. $code=(string)$num;
  289. $buwei='';
  290. if(strlen($code)<$len){
  291. for($i=strlen($code);$i<$len;$i++){
  292. $buwei.='0';
  293. }
  294. }
  295. return $buwei.$code;
  296. }
  297. /**
  298. * 二维码编码生成
  299. * @param $sign
  300. * @param $fixed_code
  301. * @param $small_num
  302. * @param $print_date
  303. * @param $flow
  304. * @param $weight
  305. * @param $large_sign
  306. * @param $reservation
  307. * @return array
  308. */
  309. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  310. $code=$sign;//4 位固定标志位
  311. $code.=$fixed_code; // 固定字符串
  312. $code.=$small_num;//3位 小件数量
  313. $code.=$print_date;//6 位 日期
  314. $code.=$flow;//6位打印流水号
  315. $code.=$weight;//6位辅料重量
  316. $code.=$large_sign;//大小件标示位
  317. $code.=$reservation;//20 位 预留号
  318. //大码数据信息
  319. $data=[
  320. 'code'=>str_replace(" ","",$code),
  321. 'code_cp1'=>$print_date.$flow,
  322. 'code_cp2'=>$weight.$reservation,//20位补充
  323. 'print_date'=>time(),
  324. 'p_nums'=>0,
  325. ];
  326. return $data;
  327. }
  328. }