QcodeBach.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. ->order('id','desc')
  978. ->limit(0,10)
  979. ->select();
  980. foreach ($sel as $k=>$v) {
  981. $oid = $v['_id']->jsonSerialize();
  982. $sel[$k]['id'] = $oid['$oid'];
  983. }
  984. return json(['list' => $sel, 'total' => count($sel)]);
  985. }
  986. /**
  987. * 二维码生成类
  988. */
  989. public function qrcode($url)//二维码生成类
  990. {
  991. $url=$url;
  992. $level=3;
  993. $size=6;
  994. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  995. $errorCorrectionLevel =intval($level) ;//容错级别
  996. $matrixPointSize = intval($size);//生成图片大小
  997. //生成二维码图片
  998. $object = new \QRcode();
  999. //打开缓冲区
  1000. ob_start();
  1001. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  1002. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  1003. $imageString = base64_encode(ob_get_contents());
  1004. //关闭缓冲区
  1005. ob_end_clean();
  1006. //把生成的base64字符串返回给前端
  1007. // $data = array(
  1008. // 'labelcode'=>$url,
  1009. // 'code'=>200,
  1010. // 'data'=>$imageString,
  1011. // 'product_code'=>$url
  1012. // );
  1013. return 'data:image/png;base64,'.$imageString;
  1014. }
  1015. }