QcodeAdd.php 16 KB

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