QcodeAdd.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. // echo "<pre>";
  65. // print_r($product_id);
  66. // echo "<pre>";
  67. // $product_name = [];
  68. // foreach ($product_id as $v){
  69. // $list = $product->where('_id',$v)->where('delete_time','')->find();
  70. // $product_name[$list['product_code']] = $list['product_name'];
  71. // }
  72. // echo "<pre>";
  73. // print_r($product_name);
  74. // echo "<pre>";
  75. // $this->view->assign('product', $product_name);
  76. // 1. 获取所有产品的 ID
  77. $product_id = $company
  78. ->name((int)$userinfo['company'].'_'.'qcode_company')
  79. ->where('delete_time','')
  80. ->column('product_id');
  81. // 2. 查找产品信息:构建 product_code => 全部信息数组
  82. $product_info = [];
  83. foreach ($product_id as $v) {
  84. $list = $product
  85. ->where('_id', $v)
  86. ->where('delete_time', '')
  87. ->find();
  88. if ($list) {
  89. // 记录完整产品信息
  90. $product_info[$list['product_code']] = [
  91. 'jjcp_cpmc' => $list['product_name'], // 产品名称
  92. 'jjcp_cpdh' => $list['product_code'], // 产品代码
  93. ];
  94. }
  95. }
  96. // 3. 从 Mongo 查询 jjcp_cpmc 名称
  97. $mongo = \think\Db::connect('mongodb');
  98. $mongo_products = $mongo
  99. ->name('finished_products')
  100. ->field('jjcp_cpmc')
  101. ->select();
  102. // 4. 比对名称,匹配成功的组装完整 vo 结构
  103. $matched_products = [];
  104. foreach ($product_info as $code => $item) {
  105. foreach ($mongo_products as $mp) {
  106. if (trim($item['jjcp_cpmc']) === trim($mp['jjcp_cpmc'])) {
  107. $matched_products[] = [
  108. 'jjcp_cpmc' => $item['jjcp_cpmc'],
  109. 'jjcp_cpdh' => $item['jjcp_cpdh'],
  110. 'product_code' => $code,
  111. ];
  112. }
  113. }
  114. }
  115. $this->view->assign('product', $matched_products);
  116. $this->view->assign('row',$data);
  117. return $this->view->fetch();
  118. }
  119. /**
  120. * 获取产品信息
  121. * @return \think\response\Json
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. public function product(){
  127. $QcodeProduct = new QcodeProduct();
  128. $ResetFlow = new ResetFlow();
  129. $userinfo = Session::get('admin');
  130. if ($this->request->isAjax() === false){
  131. $this->error('请求错误');
  132. }
  133. $product_code = input('product_code');
  134. if (empty($product_code)){
  135. $this->error('参数错误');
  136. }
  137. $product = $QcodeProduct->where('product_code',$product_code)->find();
  138. if (empty($product)){
  139. $this->error('未找到该产品信息');
  140. }
  141. $flow = $ResetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',substr(json_encode($product['_id']),9,-2))->find();
  142. $row = [
  143. 'temple' => $product['temple'],
  144. 'flow' => isset($flow['l_flow'])?(int)$flow['l_flow']+1:'',
  145. 'bach' => isset($flow['bach_num'])?(int)$flow['bach_num']+1:'',
  146. ];
  147. if (!empty($flow)){
  148. $row['flow'] = $flow['l_flow']+1;
  149. $row['bach'] = $flow['bach_num']+1;
  150. }
  151. return json(['code'=>1,'data'=>$row]);
  152. }
  153. /**
  154. * 增加新批次
  155. * @return void
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. * @throws \think\exception\DbException
  159. */
  160. public function add()
  161. {
  162. $product = new QcodeProduct();
  163. $bach = new QcodeBach();
  164. $large = new QcodeLarge();
  165. $small = new QcodeSmall();
  166. $liushui = new QcodeLiushui();
  167. $resetFlow = new ResetFlow();
  168. $QcodeCompany = new QcodeCompany();
  169. if ($this->request->isAjax() === false){
  170. $this->error('请求错误');
  171. }
  172. $rows = input('row');
  173. if (empty($rows)){
  174. $this->error('参数错误');
  175. }
  176. $rows = json_decode($rows);
  177. $data = [];
  178. foreach ($rows as $value){
  179. foreach ($value as $k=>$v){
  180. $data[$k] = $v;
  181. }
  182. }
  183. if ($data['danwei'] == 1){
  184. $num = $data['number'];
  185. $tray_num = $data['tray_num'];
  186. $tray_num1 = $data['tray_num'];
  187. $box_number = $data['box_number'];
  188. //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
  189. $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
  190. $large_num = (int)ceil($small_num/$tray_num);
  191. }else{
  192. $num = 0;
  193. $tray_num = $data['volume_num'];
  194. $small_num = $data['small_num'];
  195. $large_num = (int)ceil($small_num/$tray_num);
  196. $box_number = 1;
  197. $tray_num1 = 1;
  198. }
  199. $userinfo = Session::get('admin');
  200. $productList = $product->where('product_code',$data['product_code'])->find();
  201. $arr = [
  202. 'product_id' => $productList['_id'],
  203. 'create_time' => time(),
  204. 'delect_time' => '',
  205. 'sync_flag' => 0,
  206. ];
  207. $productres = $QcodeCompany->save($arr);
  208. if ($productres === 0){
  209. $this->error('添加失败');
  210. }
  211. if (empty($productList)){
  212. $this->error('未找到该产品数据');
  213. }
  214. $batchList = [
  215. 'supplier_name' => $data['company_name'],
  216. 'supplier_code' => $userinfo['printer_code'],
  217. 'supplier_id' => $userinfo['id'],
  218. 'matter_name' => $productList['product_name'],
  219. 'matter_no' => $productList['product_code'],//生产批次号
  220. 'matter_id' => substr(json_encode($productList['_id']),9,-2),
  221. 'matter_type' => $productList['temple'],
  222. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),
  223. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),
  224. 'danwei' => $data['danwei'],//单位
  225. 'num' => $num,//总数量(张/个)
  226. 'tray_num' => $tray_num1,//每层箱数
  227. 'box_num' => $box_number,//每托层数
  228. 'total_boxes' => $data['total_boxes'],//每托盘箱数
  229. 'small_num' => $small_num,//每托盘箱数
  230. 'pallet_height' => $data['pallet_height'],//每托高度
  231. 'pallet_length' => $data['pallet_length'],//托盘规格
  232. 'pallet_width' => $data['pallet_width'],//托盘规格
  233. 'larger_num' => $large_num,//大件(总托数)
  234. 'l_reservation' => '',
  235. 'l_flow' => $data['big_liushui'],
  236. 'l_weight' => $data['big_weight'],
  237. 's_flow' => $data['small_start_liushui'],
  238. 's_weight' => $data['small_weight'],
  239. 's_reservation' => '',
  240. 'bach_status' => 0,
  241. 'userid' => $userinfo['id'],
  242. 'bach_num' => $data['batch'],
  243. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  244. 'create_time' => time(),//新增时间
  245. ];
  246. $res = $bach->save($batchList);
  247. if ($res === 0){
  248. $this->error('添加失败');
  249. }
  250. $flow = [
  251. 'l_flow' => (int)$batchList['large_endnum'],
  252. 'bach_num' => $batchList['bach_num'],
  253. ];
  254. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  255. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  256. }else{
  257. $flow['product_id'] = $batchList['matter_id'];
  258. $resetFlow->save($flow);
  259. }
  260. $last_id = $bach->getLastInsID();
  261. if ($last_id){
  262. //插入大小二维码数据
  263. //二维码数据不变区域
  264. // echo "<pre>";
  265. // print_r($batchList['matter_type']);
  266. // echo "<pre>";
  267. // echo "<pre>";
  268. // print_r($batchList['supplier_code']);
  269. // echo "<pre>";
  270. // echo "<pre>";
  271. // print_r('9');
  272. // echo "<pre>";
  273. // echo "<pre>";
  274. // print_r($batchList['matter_no']);
  275. // echo "<pre>";
  276. // echo "<pre>";
  277. // print_r($batchList['manufacture_date']);
  278. // echo "<pre>";
  279. // echo "<pre>";
  280. // print_r($batchList['print_date']);
  281. // echo "<pre>";
  282. $fixed_code = '';
  283. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  284. // $fixed_code .= substr($this->intTochar($batchList['supplier_code'], 12), 1);//12位 供应商编码 // 去掉第一位0
  285. $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位 供应商编码
  286. $fixed_code .= '9'; // 补0一位
  287. $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位 辅料编码
  288. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  289. $print_code=$batchList['print_date'];//6位 打码日期
  290. // echo "<pre>";
  291. // print_r($fixed_code);
  292. // echo "<pre>";
  293. // echo "<pre>";
  294. // print_r($print_code);
  295. // echo "<pre>";
  296. $small_liushui = [
  297. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  298. 'last_num' => 0,
  299. 'user_id' => $userinfo['company'],
  300. 'stype' => 2,
  301. 'dateTime' => time(),
  302. ];
  303. $whereSmall = [
  304. 'onlycode' => $small_liushui['onlycode'],
  305. 'user_id' => $userinfo['company'],
  306. ];
  307. if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  308. //小件二维码存在,更新小件二维码最后流水号
  309. $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
  310. }else{
  311. //小件二维码不存在,新增记录
  312. $liushui->save($small_liushui);
  313. $lastNum['last_num'] = 0;
  314. }
  315. //循环插入大件二维码数据
  316. for ($i=0;$i<$batchList['larger_num'];$i++){
  317. $large = new QcodeLarge();
  318. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  319. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  320. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  321. $l_reservation = $l_reservation.'0000000000';
  322. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  323. if ($remainder < $batchList['tray_num']){
  324. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  325. }else{
  326. $small_n = $this->intTochar($batchList['tray_num'],3);
  327. }
  328. $l_num = 0;
  329. if ($data['danwei'] == 1){
  330. //以箱为单位时
  331. $l_num = $small_n * $batchList['box_num'];
  332. }
  333. //大件二维码数据
  334. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  335. //大码数据信息
  336. $l_data = [
  337. 'bach_id' => $last_id,
  338. 'code' => $code_data['code'],
  339. 'code_cp1' => $code_data['code_cp1'],
  340. 'code_cp2' => $code_data['code_cp2'],
  341. 'print_date' => $print_code,
  342. 'create_time' => time(),
  343. 'p_nums' => 0,
  344. 'userid' =>$userinfo['id'],
  345. 'l_weight' =>$batchList['l_weight']*100,
  346. 'l_num' =>$l_num,
  347. 'l_status' => 0,
  348. 'l_print' => 0
  349. ];
  350. $l_res = $large->save($l_data);
  351. if ($l_res === 0){
  352. $this->error('大件码插入失败');
  353. }
  354. $large_id = $large->getLastInsID();
  355. if ($large_id){
  356. // //小件码循环插入
  357. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  358. $small = new QcodeSmall();
  359. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
  360. $s_weight = $this->intTochar($batchList['s_weight'],6);
  361. $small_sign = '000';
  362. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  363. $s_reservation = $s_reservation . '0000000000';
  364. //生成小件码
  365. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  366. //小码数据
  367. $s_data = [
  368. 'large_id'=>$large_id,
  369. 'bach_id'=>$last_id,
  370. 'code'=>$small_code_data['code'],
  371. 'code_cp1'=>$small_code_data['code_cp1'],
  372. 'code_cp2'=>$small_code_data['code_cp2'],
  373. 'l_flow'=>$j+1,
  374. 'print_date'=>$print_code,
  375. 'create_time'=>time(),
  376. 'p_nums'=>0,
  377. 'userid'=>$userinfo['id'],//小码绑定用户id
  378. 's_weight'=>$batchList['s_weight'],//单个小件重量
  379. 'status' => 0,
  380. ];
  381. $s_res = $small->save($s_data);
  382. if ($s_res === 0){
  383. $this->error('小件码插入失败');
  384. }
  385. }
  386. }else{
  387. $this->error('添加失败');
  388. }
  389. }
  390. }
  391. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  392. if ($liushui_res === false){
  393. $this->error('添加失败');
  394. }
  395. $this->success('成功');
  396. }
  397. /**
  398. * 编码补位
  399. * @param $num
  400. * @param $len
  401. * @return string
  402. */
  403. function intTochar($num=0,$len){
  404. //规定的不足的时候自动补足零
  405. $code=(string)$num;
  406. $buwei='';
  407. if(strlen($code)<$len){
  408. for($i=strlen($code);$i<$len;$i++){
  409. $buwei.='0';
  410. }
  411. }
  412. return $buwei.$code;
  413. }
  414. /**
  415. * 二维码编码生成
  416. * @param $sign
  417. * @param $fixed_code
  418. * @param $small_num
  419. * @param $print_date
  420. * @param $flow
  421. * @param $weight
  422. * @param $large_sign
  423. * @param $reservation
  424. * @return array
  425. */
  426. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  427. $code=$sign;//4 位固定标志位
  428. $code.=$fixed_code; // 固定字符串
  429. $code.=$small_num;//3位 小件数量
  430. $code.=$print_date;//6 位 日期
  431. $code.=$flow;//6位打印流水号
  432. $code.=$weight;//6位辅料重量
  433. $code.=$large_sign;//大小件标示位
  434. $code.=$reservation;//20 位 预留号
  435. //大码数据信息
  436. $data=[
  437. 'code'=>str_replace(" ","",$code),
  438. 'code_cp1'=>$print_date.$flow,
  439. 'code_cp2'=>$weight.$reservation,//20位补充
  440. 'print_date'=>time(),
  441. 'p_nums'=>0,
  442. ];
  443. return $data;
  444. }
  445. }