QcodeAdd.php 16 KB

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