QcodeBach.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\admin\model\QcodeProduct;
  5. use app\admin\model\QcodeGsmc;
  6. use app\admin\model\QcodeLarge;
  7. use app\admin\model\QcodeSmall;
  8. use app\common\controller\Backend;
  9. use \think\Session;
  10. use think\Db;
  11. use MongoDB\BSON\ObjectId;
  12. /**
  13. * 批次列表
  14. */
  15. class QcodeBach extends Backend
  16. {
  17. /**
  18. * Product模型对象
  19. * @var \app\admin\model\QcodeBach
  20. */
  21. protected $model = null;
  22. protected $multiFields = 'status';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\QcodeBach();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. return $this->view->fetch();
  43. }
  44. /**
  45. * 主表查询
  46. */
  47. public function bach()
  48. {
  49. $this->relationSearch = false;
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if ($this->request->isAjax()) {
  52. // 如果是 Selectpage 请求则跳转
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. $userInfo = Session::get('admin');
  57. $company_id = (int)$userInfo['company'];
  58. // 默认条件:未删除
  59. $where = ['delete_time' => ''];
  60. // 获取前端传参
  61. $req = input();
  62. $sort = $req['sort'] == 'id' ? '_id' : $req['sort'];
  63. $order = $req['order'] ?? 'desc';
  64. $offset = $req['offset'] ?? 0;
  65. $limit = $req['limit'] ?? 20;
  66. // 解析 filter 筛选条件
  67. $filter = json_decode($req['filter'], true);
  68. if (is_array($filter)) {
  69. foreach ($filter as $k => $v) {
  70. $where[$k] = new \MongoDB\BSON\Regex($v);
  71. }
  72. }
  73. $list = [];
  74. // 超级管理员:查询所有公司
  75. if (isSuperAdmin()) {
  76. $companies = Db::name('admin')
  77. ->field('company')
  78. ->where('company', '<>', '')
  79. ->where('kes', '<>', '')
  80. ->distinct(true)
  81. ->select();
  82. foreach ($companies as $row) {
  83. $cid = $row['company'];
  84. $rows = $this->model->name($cid . '_qcode_bach')
  85. ->where($where)
  86. ->select(); // 不分页,后面统一处理
  87. foreach ($rows as &$item) {
  88. $oid = $item['_id']->jsonSerialize();
  89. $item['id'] = $oid['$oid'];
  90. }
  91. $list = array_merge($list, $rows);
  92. }
  93. // 全局排序(按 create_time 排序)
  94. usort($list, function ($a, $b) use ($sort, $order) {
  95. $valA = $a[$sort] ?? 0;
  96. $valB = $b[$sort] ?? 0;
  97. return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
  98. });
  99. // 总数与分页
  100. $total = count($list);
  101. $list = array_slice($list, $offset, $limit);
  102. } else {
  103. // 普通用户:只查本公司
  104. $model = $this->model->name($company_id . '_qcode_bach');
  105. $total = $model->where($where)->count();
  106. $list = $model->where($where)
  107. ->order([$sort => $order])
  108. ->limit($limit)
  109. ->skip($offset)
  110. ->select();
  111. foreach ($list as &$item) {
  112. $oid = $item['_id']->jsonSerialize();
  113. $item['id'] = $oid['$oid'];
  114. }
  115. }
  116. return json(["total" => $total, "rows" => $list]);
  117. }
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 大件列表
  122. */
  123. /**
  124. * 大件列表
  125. */
  126. public function large()
  127. {
  128. $this->relationSearch = false;
  129. $this->request->filter(['strip_tags', 'trim']);
  130. if ($this->request->isAjax()) {
  131. if ($this->request->request('keyField')) {
  132. return $this->selectpage();
  133. }
  134. $userInfo = Session::get('admin');
  135. $company_id = (int)$userInfo['company'];
  136. $where = ['delete_time' => ''];
  137. $req = input();
  138. if ($req['filter'] == '{}') {
  139. return json(["total" => 0, "rows" => []]);
  140. }
  141. $sort = ($req['sort'] == 'id') ? '_id' : $req['sort'];
  142. $order = $req['order'];
  143. $filter = json_decode($req['filter'], true);
  144. if (is_array($filter)) {
  145. foreach ($filter as $k => $v) {
  146. $where[$k] = new \MongoDB\BSON\Regex($v);
  147. }
  148. }
  149. $list = [];
  150. $total = 0;
  151. $db = new QcodeLarge();
  152. $qcodeSmall = new QcodeSmall();
  153. // 超级管理员查询所有公司数据
  154. if (isSuperAdmin()) {
  155. $companies = Db::name('admin')
  156. ->field('company')
  157. ->where('company', '<>', '')
  158. ->where('kes', '<>', '')
  159. ->distinct(true)
  160. ->select();
  161. foreach ($companies as $row) {
  162. $cid = $row['company'];
  163. $table = $cid . '_qcode_large';
  164. $rows = $db->name($table)->where($where)->order($sort, $order)->select();
  165. $total += count($rows);
  166. foreach ($rows as $k => $v) {
  167. $oid = $v['_id']->jsonSerialize();
  168. $v['id'] = $oid['$oid'];
  169. $v['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
  170. $v['small_num'] = $qcodeSmall->name($cid . '_qcode_small')->where('large_id', $oid['$oid'])->count();
  171. $v['l_weight'] = floatval($v['l_weight']) / 100;
  172. $list[] = $v;
  173. }
  174. }
  175. } else {
  176. // 普通用户只查自己公司表
  177. $table = $company_id . '_qcode_large';
  178. $list = $db->name($table)->where($where)->order($sort, $order)->select();
  179. $total = count($list);
  180. foreach ($list as $k => $v) {
  181. $oid = $v['_id']->jsonSerialize();
  182. $list[$k]['id'] = $oid['$oid'];
  183. $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
  184. $list[$k]['small_num'] = $qcodeSmall->name($company_id . '_qcode_small')->where('large_id', $oid['$oid'])->count();
  185. $list[$k]['l_weight'] = floatval($v['l_weight']) / 100;
  186. }
  187. }
  188. return json(["total" => $total, "rows" => $list]);
  189. }
  190. return $this->view->fetch();
  191. }
  192. /**
  193. * 小件列表
  194. */
  195. public function small()
  196. {
  197. //设置过滤方法
  198. $this->request->filter(['strip_tags', 'trim']);
  199. $req = $this->request->param();
  200. if ($this->request->isAjax()) {
  201. //如果发送的来源是Selectpage,则转发到Selectpage
  202. if ($this->request->request('keyField')) {
  203. return $this->selectpage();
  204. }
  205. $userInfo = Session::get('admin');
  206. $company_id = (int)$userInfo['company'];
  207. $where = [
  208. 'delete_time'=> '',
  209. 'large_id'=> $req['large_id']
  210. ];
  211. $req = input();
  212. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  213. $order = $req['order'];
  214. $offset = $req['offset'];
  215. $limit = $req['limit'];
  216. // 构造模糊查询条件
  217. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  218. // $filter = ['field' => $regex];
  219. $filter = json_decode($req['filter'], true);
  220. foreach ($filter as $k => $v){
  221. $where[$k] = new \MongoDB\BSON\Regex($v);
  222. }
  223. $list = [];
  224. $total = 0;
  225. $qcodeSmall = new QcodeSmall();
  226. // 超级管理员查询所有公司
  227. if (isSuperAdmin()) {
  228. $companies = Db::name('admin')
  229. ->field('company')
  230. ->where('company', '<>', '')
  231. ->where('kes', '<>', '')
  232. ->distinct(true)
  233. ->select();
  234. foreach ($companies as $row) {
  235. $cid = $row['company'];
  236. $table = $cid . '_qcode_small';
  237. // 获取数据(不分页,后面统一分页)
  238. $rows = $qcodeSmall->name($table)->where($where)->select();
  239. foreach ($rows as &$v) {
  240. $oid = $v['_id']->jsonSerialize();
  241. $v['id'] = $oid['$oid'];
  242. $v['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
  243. if (strpos($v['l_flow'], '-') !== false) {
  244. $parts = explode('-', $v['l_flow']);
  245. $v['l_flows'] = end($parts);
  246. } else {
  247. $v['l_flows'] = '';
  248. }
  249. }
  250. $list = array_merge($list, $rows);
  251. }
  252. // 全部公司数据合并后,统一排序
  253. usort($list, function ($a, $b) use ($sort, $order) {
  254. $valA = $a[$sort] ?? '';
  255. $valB = $b[$sort] ?? '';
  256. return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
  257. });
  258. // 总条数
  259. $total = count($list);
  260. // 分页截取
  261. $list = array_slice($list, $offset, $limit);
  262. } else {
  263. // 普通用户仅查本公司
  264. $table = $company_id . '_qcode_small';
  265. $total = $qcodeSmall->name($table)->where($where)->count();
  266. $list = $qcodeSmall->name($table)->where($where)
  267. ->order([$sort => $order])
  268. ->limit($limit)
  269. ->skip($offset)
  270. ->select();
  271. foreach ($list as $k => $v) {
  272. $oid = $v['_id']->jsonSerialize();
  273. $list[$k]['id'] = $oid['$oid'];
  274. $list[$k]['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
  275. }
  276. }
  277. return json(["total" => $total, "rows" => $list]);
  278. }
  279. return $this->view->fetch();
  280. }
  281. /**
  282. * 小件导出状态修改(支持超级管理员操作单个公司表)
  283. * 支持处理单个ID和多个ID(逗号分隔)的情况
  284. */
  285. public function small_status()
  286. {
  287. $req = $this->request->param();
  288. $ids = $req['ids'];
  289. // 统一处理ids参数,转换为数组
  290. if (!is_array($ids)) {
  291. $ids = explode(',', $ids);
  292. }
  293. // 过滤空值
  294. $ids = array_filter($ids);
  295. if (empty($ids)) {
  296. $this->error("参数错误:ids不能为空");
  297. }
  298. // 判断是否是超级管理员
  299. if (isSuperAdmin()) {
  300. $this->handleSuperAdminMode($ids);
  301. } else {
  302. $this->handleNormalUserMode($ids);
  303. }
  304. }
  305. /**
  306. * 超级管理员处理模式
  307. */
  308. protected function handleSuperAdminMode($ids)
  309. {
  310. $qcodeSmall = new QcodeSmall();
  311. $companyTables = Db::name('admin')
  312. ->field('company')
  313. ->where('company', '<>', '')
  314. ->where('kes', '<>', '')
  315. ->distinct(true)
  316. ->select();
  317. $found = false;
  318. $successIds = [];
  319. $failedIds = [];
  320. foreach ($companyTables as $row) {
  321. $cid = $row['company'];
  322. $tableName = $cid . '_qcode_small';
  323. try {
  324. // 查询该表中存在的记录
  325. $records = $qcodeSmall->name($tableName)
  326. ->whereIn('_id', $ids)
  327. ->select();
  328. if (!empty($records)) {
  329. $found = true;
  330. // 批量更新状态
  331. foreach ($records as $record) {
  332. $newStatus = ($record['status'] == 0) ? 1 : 0;
  333. $result = $qcodeSmall->name($tableName)
  334. ->where('_id', $record['_id'])
  335. ->update(['status' => $newStatus]);
  336. if ($result) {
  337. $successIds[] = $record['_id'];
  338. } else {
  339. $failedIds[] = $record['_id'];
  340. }
  341. }
  342. }
  343. } catch (\Exception $e) {
  344. continue; // 表不存在,跳过
  345. }
  346. }
  347. if ($found) {
  348. $message = "超级管理员批量切换完成";
  349. if (!empty($successIds)) {
  350. $message .= ",成功:";
  351. }
  352. if (!empty($failedIds)) {
  353. $message .= ",失败:";
  354. }
  355. $this->success($message);
  356. } else {
  357. $this->error("在所有公司中未找到这些记录:" . implode(',', $ids));
  358. }
  359. }
  360. /**
  361. * 普通用户处理模式
  362. */
  363. protected function handleNormalUserMode($ids)
  364. {
  365. $userInfo = Session::get('admin');
  366. $company_id = (int)$userInfo['company'];
  367. $qcodeSmall = new QcodeSmall();
  368. $tableName = $company_id . '_qcode_small';
  369. $successIds = [];
  370. $failedIds = [];
  371. try {
  372. // 查询存在的记录
  373. $records = $qcodeSmall->name($tableName)
  374. ->whereIn('_id', $ids)
  375. ->select();
  376. if (empty($records)) {
  377. $this->error("未找到符合条件的记录");
  378. }
  379. // 批量更新状态
  380. foreach ($records as $record) {
  381. $newStatus = ($record['status'] == 0) ? 1 : 0;
  382. $result = $qcodeSmall->name($tableName)
  383. ->where('_id', $record['_id'])
  384. ->update(['status' => $newStatus]);
  385. if ($result) {
  386. $successIds[] = $record['_id'];
  387. } else {
  388. $failedIds[] = $record['_id'];
  389. }
  390. }
  391. $message = "批量切换完成";
  392. if (!empty($successIds)) {
  393. $message .= ",成功:" ;
  394. }
  395. if (!empty($failedIds)) {
  396. $message .= ",失败:" ;
  397. }
  398. $this->success($message);
  399. } catch (\Exception $e) {
  400. $this->error("操作失败:" . $e->getMessage());
  401. }
  402. }
  403. /**
  404. * 自动打码(大件)
  405. */
  406. public function print_l()
  407. {
  408. $this->request->filter(['strip_tags', 'trim']);
  409. $req = $this->request->param();
  410. if (!$this->request->isAjax()) {
  411. $this->view->assign('ids', $req['ids']);
  412. return $this->view->fetch();
  413. }
  414. $ids = $req['ids'];
  415. $type = $req['type'];
  416. $numn = $req['numn'];
  417. $qcodeLarge = new QcodeLarge();
  418. $qcodeSmall = new QcodeSmall();
  419. $qcodeProduct = new QcodeProduct();
  420. $rows = [];
  421. // 超级管理员逻辑:遍历所有公司表查找匹配ID
  422. if (isSuperAdmin()) {
  423. $companyList = Db::name('admin')
  424. ->field('company')
  425. ->where('company', '<>', '')
  426. ->where('kes', '<>', '')
  427. ->distinct(true)
  428. ->select();
  429. foreach ($companyList as $company) {
  430. $cid = $company['company'];
  431. $tableLarge = $cid . '_qcode_large';
  432. $tableBach = $cid . '_qcode_bach';
  433. $tableSmall = $cid . '_qcode_small';
  434. // 检查是否有匹配的大件ID
  435. try {
  436. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  437. if (empty($large)) continue;
  438. $largeArr = json_decode($large, true);
  439. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  440. $bachArr = json_decode($bach, true);
  441. $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
  442. $rowArr = json_decode($row, true);
  443. $company_name = $bachArr['supplier_name'];
  444. $product_name = $bachArr['matter_name'];
  445. $main_unit = $rowArr['main_unit'];
  446. $sec_unit = $rowArr['sec_unit'];
  447. $proportion = $rowArr['proportion'];
  448. foreach ($ids as $key => $value) {
  449. $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  450. $rowData = json_decode($rowData, true);
  451. $code = $rowData['code'];
  452. $rows[$key] = [
  453. 'company_name' => $company_name,
  454. 'product_name' => $product_name,
  455. 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
  456. 'main_unit' => $main_unit,
  457. 'sec_unit' => $sec_unit,
  458. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  459. 'l_flow' => ltrim(substr($code, 53, 6), '0'),
  460. 'qrcode' => $code,
  461. 'pCode' => $this->qrcode($code),
  462. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  463. ltrim(substr($code, 66, 10), '0') :
  464. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  465. ];
  466. // 数量处理
  467. if ($rowData['l_num'] === null) {
  468. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  469. } elseif ($rowData['l_num'] == 0) {
  470. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] : (
  471. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  472. );
  473. } else {
  474. $rows[$key]['num'] = $rowData['l_num'];
  475. }
  476. }
  477. // 查到就停止遍历
  478. break;
  479. } catch (\Exception $e) {
  480. continue;
  481. }
  482. }
  483. } else {
  484. // 普通用户逻辑,只能查看所属公司数据
  485. $userInfo = Session::get('admin');
  486. $company_id = (int)$userInfo['company'];
  487. $tableLarge = $company_id . '_qcode_large';
  488. $tableBach = $company_id . '_qcode_bach';
  489. $tableSmall = $company_id . '_qcode_small';
  490. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  491. $largeArr = json_decode($large, true);
  492. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  493. $bachArr = json_decode($bach, true);
  494. $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
  495. $rowArr = json_decode($row, true);
  496. $company_name = $bachArr['supplier_name'];
  497. $product_name = $bachArr['matter_name'];
  498. $main_unit = $rowArr['main_unit'];
  499. $sec_unit = $rowArr['sec_unit'];
  500. $proportion = $rowArr['proportion'];
  501. foreach ($ids as $key => $value) {
  502. $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  503. $rowData = json_decode($rowData, true);
  504. $code = $rowData['code'];
  505. $rows[$key] = [
  506. 'company_name' => $company_name,
  507. 'product_name' => $product_name,
  508. 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
  509. 'main_unit' => $main_unit,
  510. 'sec_unit' => $sec_unit,
  511. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  512. 'l_flow' => ltrim(substr($code, 53, 6), '0'),
  513. 'qrcode' => $code,
  514. 'pCode' => $this->qrcode($code),
  515. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  516. ltrim(substr($code, 66, 10), '0') :
  517. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  518. ];
  519. if ($rowData['l_num'] === null) {
  520. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  521. } elseif ($rowData['l_num'] == 0) {
  522. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] : (
  523. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  524. );
  525. } else {
  526. $rows[$key]['num'] = $rowData['l_num'];
  527. }
  528. }
  529. }
  530. // 最终返回数据
  531. $data = [
  532. 'type' => $type,
  533. 'numn' => $numn,
  534. 'rows' => $rows,
  535. 'ids' => $ids,
  536. ];
  537. $this->success('成功', '', $data);
  538. }
  539. /**
  540. * 自动打码(小件)
  541. */
  542. public function print_s()
  543. {
  544. // 过滤请求参数
  545. $this->request->filter(['strip_tags', 'trim']);
  546. $req = $this->request->param();
  547. if (!$this->request->isAjax()) {
  548. $this->view->assign('ids', $req['ids']);
  549. return $this->view->fetch();
  550. }
  551. $ids = $req['ids'];
  552. $type = $req['type'];
  553. $numn = $req['numn'];
  554. $qcodeLarge = new QcodeLarge();
  555. $qcodeSmall = new QcodeSmall();
  556. $rows = [];
  557. $key = 0;
  558. // 超级管理员逻辑
  559. if (isSuperAdmin()) {
  560. $companyList = Db::name('admin')
  561. ->field('company')
  562. ->where('company', '<>', '')
  563. ->where('kes', '<>', '')
  564. ->distinct(true)
  565. ->select();
  566. foreach ($companyList as $company) {
  567. $cid = $company['company'];
  568. $tableLarge = $cid . '_qcode_large';
  569. $tableSmall = $cid . '_qcode_small';
  570. $tableBach = $cid . '_qcode_bach';
  571. try {
  572. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  573. if (empty($large)) continue;
  574. $largeArr = json_decode($large, true);
  575. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  576. $bachArr = json_decode($bach, true);
  577. $company_name = $bachArr['supplier_name'];
  578. $product_name = $bachArr['matter_name'];
  579. foreach ($ids as $value) {
  580. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  581. $large = json_decode($large, true);
  582. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  583. foreach ($arr as $v) {
  584. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  585. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  586. $small = json_decode($small, true);
  587. $code = $small['code'];
  588. $rows[$key] = [
  589. 'company_name' => $company_name,
  590. 'product_name' => $product_name,
  591. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  592. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  593. 'qrcode' => $code,
  594. // 'pCode' => $this->qrcode($code), // 如需启用二维码图像,取消注释
  595. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  596. ltrim(substr($code, 66, 10), '0') :
  597. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  598. ];
  599. $key++;
  600. }
  601. }
  602. break; // 找到数据即跳出公司循环
  603. } catch (\Exception $e) {
  604. continue;
  605. }
  606. }
  607. } else {
  608. // 普通管理员逻辑
  609. $userInfo = Session::get('admin');
  610. $company_id = (int)$userInfo['company'];
  611. $tableLarge = $company_id . '_qcode_large';
  612. $tableSmall = $company_id . '_qcode_small';
  613. $tableBach = $company_id . '_qcode_bach';
  614. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  615. $bach = $this->model->name($tableBach)->where('_id', json_decode($large, true)['bach_id'])->find();
  616. $company_name = json_decode($bach, true)['supplier_name'];
  617. $product_name = json_decode($bach, true)['matter_name'];
  618. foreach ($ids as $value) {
  619. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  620. $large = json_decode($large, true);
  621. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  622. foreach ($arr as $v) {
  623. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  624. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  625. $small = json_decode($small, true);
  626. $code = $small['code'];
  627. $rows[$key] = [
  628. 'company_name' => $company_name,
  629. 'product_name' => $product_name,
  630. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  631. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  632. 'qrcode' => $code,
  633. // 'pCode' => $this->qrcode($code),
  634. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  635. ltrim(substr($code, 66, 10), '0') :
  636. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  637. ];
  638. $key++;
  639. }
  640. }
  641. }
  642. $data = [
  643. 'type' => $type,
  644. 'numn' => $numn,
  645. 'rows' => $rows,
  646. 'ids' => $ids,
  647. ];
  648. $this->success('成功', '', $data);
  649. }
  650. /**
  651. * 手动打码(小件)
  652. */
  653. public function print_ls()
  654. {
  655. // 设置过滤方法
  656. $this->request->filter(['strip_tags', 'trim']);
  657. $req = $this->request->param();
  658. if (!$this->request->isAjax()) {
  659. $this->view->assign('ids', $req['ids']);
  660. return $this->view->fetch();
  661. }
  662. $ids = $req['ids'];
  663. $type = $req['type'];
  664. $numn = $req['numn'];
  665. $qcodeLarge = new QcodeLarge();
  666. $qcodeSmall = new QcodeSmall();
  667. $rows = [];
  668. // 超级管理员逻辑
  669. if (isSuperAdmin()) {
  670. $companyList = Db::name('admin')
  671. ->field('company')
  672. ->where('company', '<>', '')
  673. ->where('kes', '<>', '')
  674. ->distinct(true)
  675. ->select();
  676. foreach ($companyList as $company) {
  677. $cid = $company['company'];
  678. $tableSmall = $cid . '_qcode_small';
  679. $tableLarge = $cid . '_qcode_large';
  680. $tableBach = $cid . '_qcode_bach';
  681. try {
  682. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  683. if (empty($small)) continue;
  684. $small = json_decode($small, true);
  685. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  686. $large = json_decode($large, true);
  687. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  688. $bach = json_decode($bach, true);
  689. $code = $small['code'];
  690. $rows[0] = [
  691. 'company_name' => $bach['supplier_name'],
  692. 'product_name' => $bach['matter_name'],
  693. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  694. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  695. 'qrcode' => $code,
  696. // 'pCode' => $this->qrcode($code),
  697. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  698. ltrim(substr($code, 66, 10), '0') :
  699. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  700. ];
  701. break; // 查到就退出循环
  702. } catch (\Exception $e) {
  703. continue;
  704. }
  705. }
  706. } else {
  707. // 普通管理员逻辑
  708. $userInfo = Session::get('admin');
  709. $company_id = (int)$userInfo['company'];
  710. $tableSmall = $company_id . '_qcode_small';
  711. $tableLarge = $company_id . '_qcode_large';
  712. $tableBach = $company_id . '_qcode_bach';
  713. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  714. $small = json_decode($small, true);
  715. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  716. $large = json_decode($large, true);
  717. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  718. $bach = json_decode($bach, true);
  719. $code = $small['code'];
  720. $rows[0] = [
  721. 'company_name' => $bach['supplier_name'],
  722. 'product_name' => $bach['matter_name'],
  723. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  724. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  725. 'qrcode' => $code,
  726. // 'pCode' => $this->qrcode($code),
  727. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  728. ltrim(substr($code, 66, 10), '0') :
  729. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  730. ];
  731. }
  732. $data = [
  733. 'type' => $type,
  734. 'numn' => $numn,
  735. 'rows' => $rows,
  736. 'ids' => $ids,
  737. ];
  738. $this->success('成功', '', $data);
  739. }
  740. /**
  741. * 打印数量设置
  742. */
  743. public function set_num()
  744. {
  745. $req = $this->request->param();
  746. //获取数据
  747. $status = $req['status'];
  748. $num = (int)$req['num'];
  749. $ids = $req['ids'];
  750. //获取company_id
  751. $userInfo = Session::get('admin');
  752. $company_id = (int)$userInfo['company'];
  753. //自动打印大件
  754. if($status==1){
  755. $qcodeLarge = new QcodeLarge();
  756. foreach ($ids as $v){
  757. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setInc('p_nums',$num);
  758. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setField('l_print','1');
  759. }
  760. }elseif($status==2){
  761. $qcodeSmall = new QcodeSmall();
  762. foreach ($ids as $v){
  763. $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$v)->setInc('p_nums',$num);
  764. }
  765. }elseif($status==3){
  766. $qcodeSmall = new QcodeSmall();
  767. $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids)->setInc('p_nums',$num);
  768. }
  769. }
  770. /**
  771. * 导出excel跳转
  772. */
  773. public function exp()
  774. {
  775. //获取产品配置菜单id
  776. $bool = db('auth_rule')->field('id')->where('title','发货管理')->find();
  777. if ($bool){
  778. $this->success('成功','',$bool);
  779. }else{
  780. $this->error('未获取到菜单','',$bool);
  781. }
  782. }
  783. /**
  784. * 修改大件重量数量
  785. */
  786. public function edit($ids = NULL)
  787. {
  788. $req = $this->request->param();
  789. if ($this->request->isAjax()){
  790. parse_str($req['data'],$req);
  791. if(!isset($req['row']['type']) || empty($req['row']['type'])){
  792. $this->error('请选择修改类型');
  793. }
  794. if(!isset($req['row']['numn']) || empty($req['row']['numn'])){
  795. $this->error('请填写修改数量');
  796. }
  797. $ids = explode(',',$req['row']['ids']);
  798. $data = [
  799. 'update_time'=>time(),
  800. 'sync_flag'=>0
  801. ];
  802. $userInfo = Session::get('admin');
  803. $company_id = (int)$userInfo['company'];
  804. $qcodeLarge = new QcodeLarge();
  805. if($req['row']['type']==1){//修改大件重量
  806. $data['l_weight'] = intval($req['row']['numn']*100);
  807. foreach($ids as $val){
  808. //查询code
  809. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('code')->where('_id',$val)->find();
  810. $code = json_decode($large,true)['code'];
  811. if($code){
  812. $code1 = substr($code,0,59);
  813. $code2 = substr($code,65,85);
  814. $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
  815. //修改code和大件重量
  816. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  817. }
  818. }
  819. $this->success('修改成功');
  820. }
  821. if($req['row']['type']==2){//修改大件总张数
  822. $data['l_num'] = intval($req['row']['numn']);
  823. foreach($ids as $val){
  824. //查询l_num, 判断是否可以修改总张数
  825. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('l_num')->where('_id',$val)->find();
  826. $l_num = json_decode($large,true)['l_num'];
  827. if($l_num > 0 || $l_num==null){
  828. //可以修改
  829. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  830. }else{
  831. $this->error('该产品不能修改大件数量');
  832. }
  833. }
  834. $this->success('修改成功');
  835. }
  836. }
  837. $this->view->assign('ids',$req['ids']);
  838. return $this->view->fetch();
  839. }
  840. /**
  841. * 删除批次
  842. */
  843. public function del($ids = NULL)
  844. {
  845. $userInfo = Session::get('admin');
  846. $company_id = (int)$userInfo['company'];
  847. $qcodeLarge = new QcodeLarge();
  848. $qcodeSmall = new QcodeSmall();
  849. $data = [
  850. 'delete_time'=>time(),
  851. ];
  852. $bool1 = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->update($data);
  853. $bool2 = $qcodeLarge->name($company_id.'_'."qcode_large")->where('bach_id',$ids)->update($data);
  854. $bool3 = $qcodeSmall->name($company_id.'_'."qcode_small")->where('bach_id',$ids)->update($data);
  855. if($bool1!==false && $bool2!==false && $bool3!==false){
  856. $this->success('删除成功');
  857. }else{
  858. $this->error('删除失败');
  859. }
  860. }
  861. /**
  862. * 补打标签
  863. */
  864. public function reprint()
  865. {
  866. if ($this->request->isAjax()) {
  867. $data = $this->request->param();
  868. parse_str($data['data'],$req);
  869. if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
  870. $this->error('请填写公司名称');
  871. }
  872. if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
  873. $this->error('请填写辅料名称或辅料编号');
  874. }
  875. if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
  876. $this->error('请填写配盘数');
  877. }
  878. if(!isset($req['row']['num']) || empty($req['row']['num'])){
  879. $this->error('请填写数量');
  880. }
  881. //查询公司信息
  882. $qcodeGsmc = new QcodeGsmc();
  883. $print_code = $qcodeGsmc
  884. ->field('id, nickname, print_code')
  885. ->where('nickname',$req['row']['nickname'])
  886. ->find();
  887. if (!$print_code) $this->error('未查到公司信息');
  888. $print_code = json_decode($print_code,true);
  889. //查询产品信息
  890. $qcodeProduct = new QcodeProduct();
  891. if(!empty($req['row']['product_code'])){
  892. $sel = $qcodeProduct
  893. ->field('id, product_name, product_code, temple')
  894. ->where('product_code',$req['row']['product_code'])
  895. ->find();
  896. }else{
  897. $sel = $qcodeProduct
  898. ->field('id, product_name, product_code, temple')
  899. ->where('product_name',$req['row']['product_name'])
  900. ->find();
  901. }
  902. if (!$sel) $this->error('未查到辅料信息');
  903. $sel = json_decode($sel,true);
  904. $data = [
  905. 'company_name'=>$req['row']['nickname'],
  906. 'product_name'=>$sel['product_name'],
  907. 'sqrcd'=>$req['row']['sqrcd'],
  908. 'num'=>$req['row']['num'],
  909. 'main_unit'=>'盘',
  910. 'sec_unit'=>'万米',
  911. 'date'=>date('Y-m-d'),
  912. 'batch'=>substr(date('Ymd'),2,6),
  913. 'l_flow'=>1,
  914. ];
  915. $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
  916. $qrcode = 'AB'.'92'.$sel['temple'].'0'.$print_code['print_code'].('000'.$sel['product_code']).$data['batch'].$num.$data['batch'].'000001'.'000000'.'2'.'0000'.$data['batch'].'0000000000';
  917. $data['pCode'] = $this->qrcode($qrcode);
  918. $data['qrcode'] = $qrcode;
  919. $this->success('成功','',$data);
  920. }
  921. return $this->view->fetch();
  922. }
  923. /**
  924. * 公司名称搜索
  925. */
  926. public function sel_n()
  927. {
  928. $name = $this->request->param()['q_word'][0];
  929. $where = [
  930. 'nickname'=>new \MongoDB\BSON\Regex($name),
  931. 'delete_time'=>'',
  932. ];
  933. $qcodeGsmc = new QcodeGsmc();
  934. $sel = $qcodeGsmc
  935. ->field('nickname')
  936. ->where($where)
  937. ->limit(0,10)
  938. ->select();
  939. return json(['list' => $sel, 'total' => count($sel)]);
  940. }
  941. /**
  942. * 辅料名称搜索
  943. */
  944. public function sel_p()
  945. {
  946. $name = $this->request->param()['q_word'][0];
  947. $where = [
  948. 'product_name'=>new \MongoDB\BSON\Regex($name),
  949. 'delete_time'=>'',
  950. ];
  951. $qcodeProduct = new QcodeProduct();
  952. $sel = $qcodeProduct
  953. ->field('product_name, product_code')
  954. ->where($where)
  955. ->limit(0,10)
  956. ->select();
  957. foreach ($sel as $k=>$v) {
  958. $oid = $v['_id']->jsonSerialize();
  959. $sel[$k]['id'] = $oid['$oid'];
  960. }
  961. return json(['list' => $sel, 'total' => count($sel)]);
  962. }
  963. /**
  964. * 辅料编码搜索
  965. */
  966. public function sel_c()
  967. {
  968. $name = $this->request->param()['q_word'][0];
  969. $where = [
  970. 'product_code'=>new \MongoDB\BSON\Regex($name),
  971. 'delete_time'=>'',
  972. ];
  973. $qcodeProduct = new QcodeProduct();
  974. $sel = $qcodeProduct
  975. ->field('id, product_name, product_code')
  976. ->where($where)
  977. ->limit(0,10)
  978. ->select();
  979. foreach ($sel as $k=>$v) {
  980. $oid = $v['_id']->jsonSerialize();
  981. $sel[$k]['id'] = $oid['$oid'];
  982. }
  983. return json(['list' => $sel, 'total' => count($sel)]);
  984. }
  985. /**
  986. * 二维码生成类
  987. */
  988. public function qrcode($url)//二维码生成类
  989. {
  990. $url=$url;
  991. $level=3;
  992. $size=6;
  993. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  994. $errorCorrectionLevel =intval($level) ;//容错级别
  995. $matrixPointSize = intval($size);//生成图片大小
  996. //生成二维码图片
  997. $object = new \QRcode();
  998. //打开缓冲区
  999. ob_start();
  1000. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  1001. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  1002. $imageString = base64_encode(ob_get_contents());
  1003. //关闭缓冲区
  1004. ob_end_clean();
  1005. //把生成的base64字符串返回给前端
  1006. // $data = array(
  1007. // 'labelcode'=>$url,
  1008. // 'code'=>200,
  1009. // 'data'=>$imageString,
  1010. // 'product_code'=>$url
  1011. // );
  1012. return 'data:image/png;base64,'.$imageString;
  1013. }
  1014. }