QcodeAdd.php 13 KB

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