QcodeAdd.php 18 KB

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