QcodeAdd.php 14 KB

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