QcodeAdd.php 12 KB

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