QcodeAdd.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. 'kes' => $userinfo['kes'],
  161. 'supplier_id' => $userinfo['id'],
  162. 'matter_name' => $productList['product_name'],
  163. 'matter_no' => $productList['product_code'],
  164. 'matter_id' => substr(json_encode($productList['_id']),9,-2),
  165. 'matter_type' => $productList['temple'],
  166. 'num' => $num,
  167. 'larger_num' => $large_num,
  168. 'small_num' => $small_num,
  169. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),
  170. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),
  171. 'box_num' => $box_number,
  172. 'tray_num' => $tray_num1,
  173. 'l_reservation' => '',
  174. 'l_flow' => $data['big_liushui'],
  175. 'l_weight' => $data['big_weight'],
  176. 's_flow' => $data['small_start_liushui'],
  177. 's_weight' => $data['small_weight'],
  178. 's_reservation' => '',
  179. 'bach_status' => 0,
  180. 'userid' => $userinfo['id'],
  181. 'bach_num' => $data['batch'],
  182. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  183. 'create_time' => time(),
  184. ];
  185. $res = $bach->save($batchList);
  186. if ($res === 0){
  187. $this->error('添加失败');
  188. }
  189. $flow = [
  190. 'l_flow' => (int)$batchList['large_endnum'],
  191. 'bach_num' => $batchList['bach_num'],
  192. ];
  193. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  194. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  195. }else{
  196. $flow['product_id'] = $batchList['matter_id'];
  197. $resetFlow->save($flow);
  198. }
  199. $last_id = $bach->getLastInsID();
  200. if ($last_id){
  201. //插入大小二维码数据
  202. //二维码数据不变区域
  203. // echo "<pre>";
  204. // print_r($batchList);
  205. // echo "<pre>";
  206. $fixed_code = '';
  207. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  208. // $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位 供应商编码
  209. $fixed_code .= $this->intTochar($batchList['kes'], 12);//12位 供应商编码
  210. // $fixed_code .= '9'; // 补0一位
  211. $fixed_code .= '0000000000'; // 补0一位
  212. $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位 辅料编码
  213. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  214. $print_code=$batchList['print_date'];//6位 打码日期
  215. $small_liushui = [
  216. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  217. 'last_num' => 0,
  218. 'user_id' => $userinfo['company'],
  219. 'stype' => 2,
  220. 'dateTime' => time(),
  221. ];
  222. $whereSmall = [
  223. 'onlycode' => $small_liushui['onlycode'],
  224. 'user_id' => $userinfo['company'],
  225. ];
  226. //echo "<pre>";
  227. //print_r($whereSmall);
  228. //echo "<pre>";
  229. if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  230. //小件二维码存在,更新小件二维码最后流水号
  231. $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
  232. }else{
  233. //小件二维码不存在,新增记录
  234. $liushui->save($small_liushui);
  235. $lastNum['last_num'] = 0;
  236. }
  237. //循环插入大件二维码数据
  238. for ($i=0;$i<$batchList['larger_num'];$i++){
  239. $large = new QcodeLarge();
  240. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  241. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  242. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  243. $l_reservation = $l_reservation.'0000000000';
  244. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  245. if ($remainder < $batchList['tray_num']){
  246. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  247. }else{
  248. $small_n = $this->intTochar($batchList['tray_num'],3);
  249. }
  250. $l_num = 0;
  251. if ($data['danwei'] == 1){
  252. //以箱为单位时
  253. $l_num = $small_n * $batchList['box_num'];
  254. }
  255. //大件二维码数据
  256. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  257. // echo "<pre>";
  258. // print_r($code_data);
  259. // echo "<pre>";
  260. //大码数据信息
  261. $l_data = [
  262. 'bach_id' => $last_id,
  263. 'code' => $code_data['code'],
  264. 'code_cp1' => $code_data['code_cp1'],
  265. 'code_cp2' => $code_data['code_cp2'],
  266. 'print_date' => $print_code,
  267. 'create_time' => time(),
  268. 'p_nums' => 0,
  269. 'userid' =>$userinfo['id'],
  270. 'l_weight' =>$batchList['l_weight']*100,
  271. 'l_num' =>$l_num,
  272. 'l_status' => 0,
  273. 'l_print' => 0
  274. ];
  275. // echo "<pre>";
  276. // print_r($l_data);
  277. // echo "<pre>";die;
  278. $l_res = $large->save($l_data);
  279. if ($l_res === 0){
  280. $this->error('大件码插入失败');
  281. }
  282. $large_id = $large->getLastInsID();
  283. if ($large_id){
  284. // //小件码循环插入
  285. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  286. $small = new QcodeSmall();
  287. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
  288. $s_weight = $this->intTochar($batchList['s_weight'],6);
  289. $small_sign = '000';
  290. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  291. $s_reservation = $s_reservation . '0000000000';
  292. //生成小件码
  293. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  294. //小码数据
  295. $s_data = [
  296. 'large_id'=>$large_id,
  297. 'bach_id'=>$last_id,
  298. 'code'=>$small_code_data['code'],
  299. 'code_cp1'=>$small_code_data['code_cp1'],
  300. 'code_cp2'=>$small_code_data['code_cp2'],
  301. 'l_flow'=>$j+1,
  302. 'print_date'=>$print_code,
  303. 'create_time'=>time(),
  304. 'p_nums'=>0,
  305. 'userid'=>$userinfo['id'],//小码绑定用户id
  306. 's_weight'=>$batchList['s_weight'],//单个小件重量
  307. 'status' => 0,
  308. ];
  309. $s_res = $small->save($s_data);
  310. if ($s_res === 0){
  311. $this->error('小件码插入失败');
  312. }
  313. }
  314. }else{
  315. $this->error('添加失败');
  316. }
  317. }
  318. }
  319. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  320. if ($liushui_res === false){
  321. $this->error('添加失败');
  322. }
  323. $this->success('成功');
  324. }
  325. /**
  326. * 编码补位
  327. * @param $num
  328. * @param $len
  329. * @return string
  330. */
  331. function intTochar($num=0,$len){
  332. //规定的不足的时候自动补足零
  333. $code=(string)$num;
  334. $buwei='';
  335. if(strlen($code)<$len){
  336. for($i=strlen($code);$i<$len;$i++){
  337. $buwei.='0';
  338. }
  339. }
  340. return $buwei.$code;
  341. }
  342. /**
  343. * 二维码编码生成
  344. * @param $sign
  345. * @param $fixed_code
  346. * @param $small_num
  347. * @param $print_date
  348. * @param $flow
  349. * @param $weight
  350. * @param $large_sign
  351. * @param $reservation
  352. * @return array
  353. */
  354. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  355. $code=$sign;//4 位固定标志位
  356. $code.=$fixed_code; // 固定字符串
  357. $code.=$small_num;//3位 小件数量
  358. $code.=$print_date;//6 位 日期
  359. $code.=$flow;//6位打印流水号
  360. $code.=$weight;//6位辅料重量
  361. $code.=$large_sign;//大小件标示位
  362. $code.=$reservation;//20 位 预留号
  363. //大码数据信息
  364. $data=[
  365. 'code'=>str_replace(" ","",$code),
  366. 'code_cp1'=>$print_date.$flow,
  367. 'code_cp2'=>$weight.$reservation,//20位补充
  368. 'print_date'=>time(),
  369. 'p_nums'=>0,
  370. ];
  371. return $data;
  372. }
  373. }