QcodeBach.php 43 KB

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