QcodeAdd.php 14 KB

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