Deliver.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeLarge;
  4. use app\admin\model\QcodeSmall;
  5. use app\common\controller\Backend;
  6. use app\admin\model\QcodeProduct;
  7. use app\admin\model\QcodeCompany;
  8. use app\admin\model\QcodeBach;
  9. use app\admin\model\QcodeExport;
  10. use fast\Arr;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  14. use PhpOffice\PhpSpreadsheet\Exception;
  15. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  16. use app\admin\model\QcodeGoods;
  17. use think\Session;
  18. class Deliver extends Backend
  19. {
  20. /**
  21. * 首页
  22. * @return string|\think\response\Json
  23. * @throws \think\Exception
  24. */
  25. public function index()
  26. {
  27. return $this->view->fetch();
  28. }
  29. /**
  30. * 大件发货列表
  31. * @return string|\think\response\Json
  32. * @throws \think\Exception
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. // public function lager()
  38. // {
  39. // $company = new QcodeCompany();
  40. // $product = new QcodeProduct();
  41. // $large = new QcodeLarge();
  42. // $bach = new QcodeBach();
  43. // $small = new QcodeSmall();
  44. // $this->request->filter(['strip_tags', 'trim']);
  45. // if (false === $this->request->isAjax()) {
  46. // return $this->view->fetch();
  47. // }
  48. // if ($this->request->request('keyField')) {
  49. // return $this->selectpage();
  50. // }
  51. // // 接收参数
  52. // $userInfo = Session::get('admin');
  53. // $where = [
  54. // 'delete_time'=> '',
  55. // ];
  56. // $filter = input('filter');
  57. // $filter = json_decode($filter,true);
  58. // $whereList = [
  59. // 'delete_time'=>'',
  60. // ];
  61. // //样品编号查询
  62. // if (isset($filter['matter_name'])){
  63. // $whereList['matter_no'] =$filter['matter_name'];
  64. // }
  65. // if (isset($filter['bach'])){
  66. // $whereList['bach_num'] = $filter['bach'];
  67. // }
  68. // if (isset($filter['manufacture_date'])){
  69. // $begin = substr($filter['manufacture_date'],0,19);
  70. // $end = substr($filter['manufacture_date'],22);
  71. // $begin = (int)date('ymd',strtotime($begin));
  72. // $end = (int)date('ymd',strtotime($end));
  73. // $whereList['manufacture_date'] = ['between',[$begin,$end]];
  74. // }
  75. //
  76. // $bach_list = $bach->name($userInfo['company'].'_'.'qcode_bach')->where($whereList)->column('_id');
  77. // $bach_id = [];
  78. // foreach ($bach_list as $v){
  79. // $id = substr(json_encode($v),9,-2);
  80. // array_push($bach_id,$id);
  81. // }
  82. // $order = input('order');
  83. //// $offset = input('offset');
  84. //// $limit = input('limit');
  85. //// $limit = input('limit', 10); // 每页数量,默认10条
  86. //
  87. // $offset = input('offset', 0);
  88. // $limit = input('limit', 10);
  89. // $skip = $offset;
  90. //
  91. // $total = $large->name($userInfo['company'].'_'.'qcode_large')
  92. // ->where('l_status', 0)
  93. // ->where($where)
  94. // ->whereIn('bach_id', $bach_id)
  95. // ->count();
  96. //
  97. // $large_list = $large->name($userInfo['company'].'_'.'qcode_large')
  98. // ->order('l_flow')
  99. // ->where($where)
  100. // ->where('l_status', 0)
  101. // ->whereIn('bach_id', $bach_id)
  102. // ->skip($skip)
  103. // ->limit($limit)
  104. // ->select();
  105. //
  106. // $list=[];
  107. // foreach ($large_list as $k=>$v) {
  108. // $bach_detail = $bach
  109. // ->name($userInfo['company'].'_qcode_bach')
  110. // ->where('_id', $v['bach_id'])
  111. // ->find();
  112. // //新增取 $bach 字段
  113. // $list[$k]['num'] = $bach_detail['num'];
  114. // $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
  115. // $list[$k]['tray_num'] = $bach_detail['tray_num'];
  116. // $list[$k]['box_num'] = $bach_detail['box_num'];
  117. // $list[$k]['small_num'] = $bach_detail['small_num'];
  118. // $list[$k]['pallet_height'] = $bach_detail['pallet_height'];
  119. // $list[$k]['pallet_length'] = $bach_detail['pallet_length'];
  120. // $list[$k]['pallet_width'] = $bach_detail['pallet_width'];
  121. // $list[$k]['small_num'] = $bach_detail['small_num'];
  122. // $list[$k]['larger_num'] = $bach_detail['larger_num'];
  123. //
  124. // $bach_detail = $bach->name($userInfo['company'].'_'.'qcode_bach')->where('_id',$v['bach_id'])->find();
  125. // $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  126. // $list[$k]['bach'] = $bach_detail['bach_num'];
  127. // $list[$k]['l_flow'] = ltrim(substr($v['code'],53,6),'0');
  128. // $list[$k]['matter_name'] = $bach_detail['matter_name'];
  129. // $list[$k]['manufacture_date'] = date('Y-m-d',strtotime('20'.$bach_detail['manufacture_date']));
  130. // $list[$k]['code'] = $v['code'];
  131. // $small_num = $small->name($userInfo['company'].'_'.'qcode_small')->where('large_id',substr(json_encode($v['_id']),9,-2))->count();
  132. // $list[$k]['small_num'] = $small_num;
  133. // }
  134. //
  135. //// $result = ['total'=>$total,'rows'=>$list];
  136. // // 分组
  137. // $grouped = [];
  138. // foreach ($list as $item) {
  139. // $grouped[$item['matter_name']][] = $item;
  140. // }
  141. //
  142. // // 每组排序
  143. // foreach ($grouped as &$items) {
  144. // usort($items, function ($a, $b) {
  145. // return intval($a['l_flow']) <=> intval($b['l_flow']);
  146. // });
  147. // }
  148. // unset($items);
  149. //
  150. // // 合并
  151. // $sortedList = array_merge(...array_values($grouped));
  152. //
  153. // // 输出
  154. //// echo "<pre>";
  155. //// print_r($sortedList);
  156. //// echo "</pre>";
  157. // return json(['total'=>$total,'rows'=>$sortedList]);
  158. // }
  159. public function lager()
  160. {
  161. $company = new QcodeCompany();
  162. $product = new QcodeProduct();
  163. $large = new QcodeLarge();
  164. $bach = new QcodeBach();
  165. $small = new QcodeSmall();
  166. $this->request->filter(['strip_tags', 'trim']);
  167. if (false === $this->request->isAjax()) {
  168. return $this->view->fetch();
  169. }
  170. if ($this->request->request('keyField')) {
  171. return $this->selectpage();
  172. }
  173. $userInfo = Session::get('admin');
  174. $where = ['delete_time'=> ''];
  175. $filter = input('filter');
  176. $filter = json_decode($filter, true);
  177. $whereList = ['delete_time'=>''];
  178. if (isset($filter['matter_name'])){
  179. $whereList['matter_no'] = $filter['matter_name'];
  180. }
  181. if (isset($filter['bach'])){
  182. $whereList['bach_num'] = $filter['bach'];
  183. }
  184. if (isset($filter['manufacture_date'])){
  185. $begin = substr($filter['manufacture_date'], 0, 19);
  186. $end = substr($filter['manufacture_date'], 22);
  187. $begin = (int)date('ymd', strtotime($begin));
  188. $end = (int)date('ymd', strtotime($end));
  189. $whereList['manufacture_date'] = ['between', [$begin, $end]];
  190. }
  191. // 查出相关 bach_id
  192. $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
  193. $bach_id = [];
  194. foreach ($bach_list as $v){
  195. $id = substr(json_encode($v), 9, -2);
  196. $bach_id[] = $id;
  197. }
  198. // 获取分页参数
  199. $offset = input('offset', 0);
  200. $limit = input('limit', 10);
  201. $skip = $offset;
  202. // 总数量
  203. $total = $large->name($userInfo['company'].'_qcode_large')
  204. ->where('l_status', 0)
  205. ->where($where)
  206. ->whereIn('bach_id', $bach_id)
  207. ->count();
  208. // 当前页数据
  209. $large_list = $large->name($userInfo['company'].'_qcode_large')
  210. ->order('l_flow')
  211. ->where($where)
  212. ->where('l_status', 0)
  213. ->whereIn('bach_id', $bach_id)
  214. // ->skip($skip)
  215. // ->limit($limit)
  216. ->select();
  217. // 数据处理
  218. $list = [];
  219. foreach ($large_list as $k => $v) {
  220. $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
  221. $list[$k]['id'] = substr(json_encode($v['_id']), 9, -2);
  222. $list[$k]['l_flow'] = $bach_detail['l_flow'];
  223. $list[$k]['bach'] = $bach_detail['bach_num'];
  224. $list[$k]['num'] = $bach_detail['num'];
  225. $list[$k]['matter_name'] = $bach_detail['matter_name'];
  226. $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
  227. $list[$k]['tray_num'] = $bach_detail['tray_num'];
  228. $list[$k]['box_num'] = $bach_detail['box_num'];
  229. $list[$k]['pallet_height'] = $bach_detail['pallet_height'];
  230. $list[$k]['pallet_length'] = $bach_detail['pallet_length'];
  231. $list[$k]['pallet_width'] = $bach_detail['pallet_width'];
  232. $list[$k]['larger_num'] = $bach_detail['larger_num'];
  233. $list[$k]['manufacture_date'] = date('Y-m-d', strtotime('20'.$bach_detail['manufacture_date']));
  234. $list[$k]['code'] = $v['code'];
  235. $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
  236. $small_num = $small->name($userInfo['company'].'_qcode_small')
  237. ->where('large_id', $list[$k]['id'])
  238. ->count();
  239. $list[$k]['small_num'] = $small_num;
  240. }
  241. // 返回分页数据(关键!不要再做分组排序!)
  242. return json(['total' => $total, 'rows' => $list]);
  243. }
  244. /**
  245. * 小件列表
  246. * @return \think\response\Json
  247. * @throws \think\Exception
  248. * @throws \think\db\exception\DataNotFoundException
  249. * @throws \think\db\exception\ModelNotFoundException
  250. * @throws \think\exception\DbException
  251. */
  252. public function smallList()
  253. {
  254. $userinfo = Session::get('admin');
  255. $lager = new QcodeLarge();
  256. $small = new QcodeSmall();
  257. if ($this->request->isAjax() === false){
  258. $this->error('请求错误');
  259. }
  260. $filter = input('filter');
  261. $filter = json_decode($filter,true);
  262. $order = input('order');
  263. $offset = input('offset');
  264. $limit = input('limit');
  265. $where = [
  266. 'delete_time' => '',
  267. ];
  268. if (isset($filter['large_id'])){
  269. $where['large_id'] =$filter['large_id'];
  270. }
  271. $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
  272. ->where($where)
  273. ->skip($offset)
  274. ->limit($limit)
  275. ->select();
  276. $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
  277. $list = [];
  278. foreach ($smallList as $k=>$v){
  279. $list[$k] = [
  280. 'code' => $v['code'],
  281. 'l_flow' => $v['l_flow'],
  282. 'print_num' => $v['p_nums'],
  283. 'status' => $v['status'],
  284. ];
  285. }
  286. return json(['total'=>$total,'rows'=>$list]);
  287. }
  288. /**
  289. * 导出发货
  290. * @return void
  291. * @throws \think\Exception
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\ModelNotFoundException
  294. * @throws \think\exception\DbException
  295. */
  296. public function print()
  297. {
  298. $userinfo = Session::get('admin');
  299. $lager = new QcodeLarge();
  300. $bach = new QcodeBach();
  301. $small = new QcodeSmall();
  302. $export = new QcodeExport();
  303. if ($this->request->isAjax() === false){
  304. $this->error('请求错误');
  305. }
  306. $lager_id = input('lager_id');
  307. if ($lager_id === ''){
  308. $this->error('请至少选择一个大件');
  309. }
  310. $lagerId = [];
  311. if (strpos($lager_id,',') === false){
  312. $lagerId[0] = $lager_id;
  313. }else{
  314. $lagerId = explode(',',$lager_id);
  315. }
  316. $lager_num = count($lagerId);
  317. $bach_id = [];
  318. foreach ($lagerId as $k=>$v){
  319. $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
  320. $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
  321. $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
  322. }
  323. $bach_id = array_unique($bach_id);
  324. $data = [];
  325. foreach ($bach_id as $k=>$v){
  326. $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
  327. }
  328. $data = array_unique($data);
  329. if (count($data)>1){
  330. $this->error('请选择同一产品');
  331. }
  332. $n =0;
  333. foreach (explode(',',$lager_id) as $kk=>$vv){
  334. $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
  335. }
  336. // $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
  337. $bachArray = $bach->name($userinfo['company'].'_'.'qcode_bach')
  338. ->where('_id', $bach_id[0])
  339. ->find();
  340. $bachDetail = $bachArray ? $bachArray->toArray() : [];
  341. if ($bachDetail['box_num'] === 1){
  342. $mater_type = 2;
  343. }else{
  344. $mater_type = 1;
  345. }
  346. $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
  347. $file_dir = $this->exportExcel($exportData);
  348. $row = [
  349. 'username' => $bachDetail['supplier_name'],//公司
  350. 'matter_no' => $bachDetail['matter_no'],
  351. 'large_str' => $lager_id,
  352. 'status' => 0,
  353. 'bach_num' => $bachDetail['bach_num'],
  354. 'note' => '',
  355. 'user_id' => $userinfo['id'],
  356. 'create_time' => time(),
  357. 'file_dir' => $file_dir,
  358. 'company_id' => $userinfo['company'],
  359. 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
  360. 'small_num' => $n,//共包含了 N 个小件
  361. 'matter_name' => $bachDetail['matter_name'],//产品名称
  362. 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
  363. 'tray_num' => $bachDetail['tray_num'],//每层箱数
  364. 'box_num' => $bachDetail['box_num'],//每托层数
  365. 'pallet_height' => $bachDetail['pallet_height'],//每托高度
  366. 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
  367. 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
  368. 'larger_num' => $bachDetail['larger_num'],//总箱数
  369. ];
  370. if ($bachDetail['box_num'] != 1){
  371. $row['num'] = $bachDetail['box_num'];
  372. $row['mater_type'] = 1;
  373. }else{
  374. $row['num'] = $bachDetail['s_weight'];
  375. $row['mater_type'] = 2;
  376. }
  377. $res = $export->save($row);
  378. if ($res === false){
  379. $this->error('发货失败');
  380. }
  381. $lager = new QcodeLarge();
  382. $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
  383. $this->success('成功','',$file_dir);
  384. }
  385. /**
  386. * 表格数据处理
  387. * @param $bach_id
  388. * @param $large_str
  389. * @param $company
  390. * @param $large_num
  391. * @param $small_num
  392. * @return array
  393. * @throws \think\db\exception\DataNotFoundException
  394. * @throws \think\db\exception\ModelNotFoundException
  395. * @throws \think\exception\DbException
  396. */
  397. public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
  398. {
  399. $bach = new QcodeBach();
  400. $large = new QcodeLarge();
  401. $small = new QcodeSmall();
  402. $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
  403. $data = [
  404. 'supplier_name' => $bachList['supplier_name'],
  405. 'supplier_no' => $bachList['supplier_code'],
  406. 'matter_name' => $bachList['matter_name'],
  407. 'matter_no' => $bachList['matter_no'],
  408. 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
  409. 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
  410. 'box_num' => $large_num,
  411. 'tray_num' => $bachList['tray_num'],
  412. 'small_num' => $small_num,
  413. 'l_reservation' => $bachList['l_reservation'],
  414. 's_reservation' => $bachList['s_reservation'],
  415. 'l_flow' => $bachList['l_flow'],
  416. 's_flow' => $bachList['s_flow'],
  417. 'l_weight' => 0,
  418. 's_weight' => $bachList['s_weight']/1000,
  419. 'code' => [],
  420. ];
  421. $large_id = explode(',', $large_str);
  422. foreach ($large_id as $k => $v) {
  423. $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
  424. $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
  425. $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
  426. foreach ($small_list as $value) {
  427. $code = [
  428. 'large_code' => $largeList['code'],
  429. 'small_code' => $value['code'],
  430. ];
  431. array_push($data['code'],$code);
  432. }
  433. }
  434. return $data;
  435. }
  436. /**
  437. * 生成excel
  438. * @param $data
  439. * @return string
  440. * @throws Exception
  441. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  442. */
  443. public function exportExcel($data)
  444. {
  445. $objexcel = new Spreadsheet();
  446. $sheet = $objexcel->getActiveSheet();
  447. //设置全局单元格垂直居中
  448. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  449. //设置全局单元格水平居中
  450. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  451. //设置全局单元格自动换行
  452. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  453. //设置单元格宽度
  454. $sheet->getDefaultColumnDimension()->setWidth(50);
  455. //设置表头
  456. $sheet->setCellValue('A1', '文件流水号');
  457. $sheet->setCellValue('A2', '订单号');
  458. $sheet->setCellValue('A3', '供应商');
  459. $sheet->setCellValue('A4', '供应商代码');
  460. $sheet->setCellValue('A5', '物料名称');
  461. $sheet->setCellValue('A6', '物料代码');
  462. $sheet->setCellValue('A7', '生产日期');
  463. $sheet->setCellValue('A8', '打码日期');
  464. $sheet->setCellValue('A9', '大件数量');
  465. $sheet->setCellValue('A10', '托盘小件数量');
  466. $sheet->setCellValue('A11', '小件总数量');
  467. $sheet->setCellValue('C3', '大件预留码');
  468. $sheet->setCellValue('C5', '小件预留码');
  469. $sheet->setCellValue('C7', '大件开始流水号');
  470. $sheet->setCellValue('C9', '小件开始流水号');
  471. $sheet->setCellValue('C11', '大件重量(Kg)');
  472. $sheet->setCellValue('C13', '小件重量(Kg)');
  473. $sheet->setCellValue('A15', '大件二维码');
  474. $sheet->setCellValue('B15', '大件条码(预留)');
  475. $sheet->setCellValue('C15', '小件二维码');
  476. $sheet->setCellValue('D15', '小件条码(预留)');
  477. //插入表格数据
  478. $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
  479. $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
  480. $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
  481. $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
  482. $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
  483. $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
  484. $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
  485. $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
  486. $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
  487. $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
  488. $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
  489. $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
  490. $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
  491. $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
  492. $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
  493. foreach ($data['code'] as $k=>$v){
  494. $m = $k+16;
  495. $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
  496. $sheet->getCell('B'.$m)->setValue('');
  497. $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
  498. $sheet->getCell('D'.$m)->setValue('');
  499. }
  500. //文件另存
  501. $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
  502. // $filename = iconv("utf-8","gb2312",$filename);
  503. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  504. header('Content-Disposition:attachment;filename='.$filename);
  505. header('Cache-Control: max-age=0');
  506. header("Content-Type:text/html;charset=utf-8");
  507. if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
  508. {
  509. mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
  510. }
  511. $writer = IOFactory::createWriter($objexcel, 'Xlsx');
  512. $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
  513. return 'uploads/export/'.$filename;
  514. }
  515. /**
  516. * 批次发货申请
  517. * @return string|\think\response\Json
  518. * @throws \think\Exception
  519. */
  520. public function apply()
  521. {
  522. $export = new QcodeExport();
  523. $this->request->filter(['strip_tags', 'trim']);
  524. if (false === $this->request->isAjax()) {
  525. return $this->view->fetch();
  526. }
  527. if ($this->request->request('keyField')) {
  528. return $this->selectpage();
  529. }
  530. // 接收参数
  531. $userInfo = Session::get('admin');
  532. $where = [
  533. 'company_id' => $userInfo['company'],
  534. 'delete_time'=> ''
  535. ];
  536. $filter = input('filter');
  537. $filter = json_decode($filter,true);
  538. //样品编号查询
  539. if (isset($filter['matter_name'])){
  540. $where['matter_no'] =$filter['matter_name'];
  541. }
  542. if (isset($filter['username'])){
  543. $where['username'] = $filter['username'];
  544. }
  545. if (isset($filter['status'])){
  546. $where['status'] = (int)$filter['status'];
  547. }
  548. if (isset($filter['create_time'])){
  549. $begin = substr($filter['create_time'],0,19);
  550. $end = substr($filter['create_time'],22);
  551. $begin = strtotime($begin);
  552. $end = strtotime($end);
  553. $where['create_time'] = ['between',[$begin,$end]];
  554. }
  555. $order = input('order');
  556. $offset = input('offset');
  557. $limit = input('limit');
  558. $total = $export->where($where)->count();
  559. $export_list = $export
  560. ->order('_id',$order)
  561. ->where($where)
  562. ->whereIn('status',[0,1])
  563. ->skip($offset)
  564. ->limit($limit)
  565. ->select();
  566. $list=[];
  567. foreach ($export_list as $k=>$v) {
  568. $list[$k]['large_num'] = $v['large_num'];//托盘数量
  569. $list[$k]['total_boxes'] = $v['total_boxes'];//每箱托数
  570. $list[$k]['tray_num'] = $v['tray_num'];//每层箱数
  571. $list[$k]['box_num'] = $v['box_num'];//每托层数
  572. $list[$k]['pallet_height'] = $v['pallet_height'];//托盘高度
  573. $list[$k]['pallet_length'] = $v['pallet_length'];//托盘规格
  574. $list[$k]['pallet_width'] = $v['pallet_width'];//托盘规格
  575. $list[$k]['small_num'] = $v['small_num'];//总箱数
  576. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  577. $list[$k]['matter_name'] = $v['matter_name'];
  578. $list[$k]['matter_no'] = $v['matter_no'];
  579. $list[$k]['username'] = $v['username'];
  580. $list[$k]['large_num'] = $v['large_num'];
  581. $list[$k]['small_num'] = $v['small_num'];
  582. $list[$k]['create_time'] = $v['create_time'];
  583. $list[$k]['file_dir'] = $v['file_dir'];
  584. $list[$k]['status'] = $v['status'];
  585. }
  586. $result = ['total'=>$total,'rows'=>$list];
  587. return json($result);
  588. }
  589. /**
  590. * 批次发货删除
  591. * @param $ids
  592. * @return void
  593. */
  594. public function apply_del($ids)
  595. {
  596. $export = new QcodeExport();
  597. $userinfo = Session::get('admin');
  598. $large = new QcodeLarge();
  599. if (empty($ids)){
  600. $this->error('请求错误');
  601. }
  602. $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  603. if ($res === false){
  604. $this->error('删除失败');
  605. }
  606. $large_list = $export->where('_id',$ids)->find();
  607. $large_id = explode(',',$large_list['large_str']);
  608. foreach ($large_id as $v){
  609. $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
  610. if ($result === 0){
  611. $this->error('删除失败');
  612. }
  613. }
  614. $this->success('成功');
  615. }
  616. /**
  617. * 司机弹窗
  618. * @param $ids
  619. * @return string
  620. * @throws \think\Exception
  621. */
  622. public function goods($ids)
  623. {
  624. $this->view->assign('ids',$ids);
  625. return $this->view->fetch();
  626. }
  627. /**
  628. * 批次发货提交
  629. * @return void
  630. * @throws \think\db\exception\DataNotFoundException
  631. * @throws \think\db\exception\ModelNotFoundException
  632. * @throws \think\exception\DbException
  633. */
  634. public function apply_add()
  635. {
  636. $goods = new QcodeGoods();
  637. $export = new QcodeExport();
  638. $userinfo = Session::get('admin');
  639. if ($this->request->isAjax() === false){
  640. $this->error('请求错误');
  641. }
  642. $ids = input('ids');
  643. $order_number = input('order_number');
  644. $deliveryman = input('deliveryman');
  645. $shr_phone = input('shr_phone');
  646. $plate_number = input('plate_number');
  647. $note = input('note');
  648. $id_list = [];
  649. if (strpos($ids,',') === false){
  650. $id_list[0] = $ids;
  651. }else{
  652. $id_list = explode(',',$ids);
  653. }
  654. $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
  655. if ($update_status){
  656. $export_list = $export->whereIn('_id',$id_list)->select();
  657. $daterq = date('ymd',time());
  658. $data = [
  659. 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),
  660. 'order_number' => $order_number,
  661. 'deliveryman' => $deliveryman,
  662. 'shr_phone' => $shr_phone,
  663. 'plate_number' => $plate_number,
  664. 'shrq_date' => '',
  665. 'supplier_name' => $export_list[0]['username'],
  666. 'supplier_id' => $userinfo['id'],
  667. 'user_id' => $userinfo['id'],
  668. 'status' => 0,
  669. 'export_id' => $ids,
  670. 'create_time' => time(),
  671. 'note' => $note,
  672. 'company_id' => $userinfo['company'],
  673. ];
  674. $data['qrcode_add'] = $this->Qrcode($data['shdh']);
  675. $res = $goods->save($data);
  676. if ($res === false){
  677. $this->error('失败');
  678. }
  679. $this->success('成功');
  680. }else{
  681. $this->error('失败');
  682. }
  683. }
  684. /**
  685. * 生成二维码
  686. * @param $url
  687. * @return string
  688. */
  689. public function Qrcode($url)
  690. {
  691. $level=2;
  692. $size=5;
  693. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  694. $errorCorrectionLevel =intval($level) ;//容错级别
  695. $matrixPointSize = intval($size);//生成图片大小
  696. //生成二维码图片
  697. $object = new \QRcode();
  698. //打开缓冲区
  699. ob_start();
  700. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  701. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  702. $imageString = base64_encode(ob_get_contents());
  703. //关闭缓冲区
  704. ob_end_clean();
  705. //把生成的base64字符串返回给前端
  706. return 'data:image/png;base64,'.$imageString;
  707. }
  708. /**
  709. * 发货单
  710. * @return string|\think\response\Json
  711. * @throws \think\Exception
  712. */
  713. public function dispatch()
  714. {
  715. $export = new QcodeGoods();
  716. $this->request->filter(['strip_tags', 'trim']);
  717. if (false === $this->request->isAjax()) {
  718. return $this->view->fetch();
  719. }
  720. if ($this->request->request('keyField')) {
  721. return $this->selectpage();
  722. }
  723. // 接收参数
  724. $userInfo = Session::get('admin');
  725. $where = [
  726. 'company_id' => $userInfo['company'],
  727. 'delete_time'=> ''
  728. ];
  729. $search = input('search');
  730. if (isset($search)){
  731. $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
  732. }
  733. $filter = input('filter');
  734. $filter = json_decode($filter,true);
  735. //订单编号查询
  736. if (isset($filter['order_number'])){
  737. $where['order_number'] =$filter['order_number'];
  738. }
  739. //所属公司查询
  740. if (isset($filter['supplier_name'])){
  741. $where['supplier_name'] = $filter['supplier_name'];
  742. }
  743. if (isset($filter['status'])){
  744. $where['status'] = (int)$filter['status'];
  745. }
  746. if (isset($filter['create_time'])){
  747. $begin = substr($filter['create_time'],0,19);
  748. $end = substr($filter['create_time'],22);
  749. $begin = strtotime($begin);
  750. $end = strtotime($end);
  751. $where['create_time'] = ['between',[$begin,$end]];
  752. }
  753. $order = input('order');
  754. $offset = input('offset');
  755. $limit = input('limit');
  756. $total = $export->where($where)->count();
  757. $export_list = $export
  758. ->order('_id',$order)
  759. ->where($where)
  760. ->whereIn('status',[0,1])
  761. ->skip($offset)
  762. ->limit($limit)
  763. ->select();
  764. $list=[];
  765. foreach ($export_list as $k=>$v) {
  766. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  767. $list[$k]['shdh'] = $v['shdh'];
  768. $list[$k]['order_number'] = $v['order_number'];
  769. $list[$k]['supplier_name'] = $v['supplier_name'];
  770. $list[$k]['deliveryman'] = $v['deliveryman'];
  771. $list[$k]['plate_number'] = $v['plate_number'];
  772. $list[$k]['create_time'] = $v['create_time'];
  773. $list[$k]['status'] = $v['status'];
  774. }
  775. $result = ['total'=>$total,'rows'=>$list];
  776. return json($result);
  777. }
  778. /**
  779. * 发货单打印
  780. * @return \think\response\Json|void
  781. * @throws \think\db\exception\DataNotFoundException
  782. * @throws \think\db\exception\ModelNotFoundException
  783. * @throws \think\exception\DbException
  784. */
  785. public function printqrcode()
  786. {
  787. $goods = new QcodeGoods();
  788. $export = new QcodeExport();
  789. $large = new QcodeLarge();
  790. $userinfo = Session::get('admin');
  791. if ($this->request->isAjax() === false) {
  792. $this->error('请求错误');
  793. }
  794. $id = input('id');
  795. $good_list = $goods->where('_id',$id)->find();
  796. if ($good_list){
  797. if (strpos($good_list['export_id'],',') === false){
  798. $export_id[0] = $good_list['export_id'];
  799. }else{
  800. $export_id = explode(',',$good_list['export_id']);
  801. }
  802. $data = [];
  803. foreach ($export_id as $k=>$v){
  804. $data[$k] = $export->where('_id',$v)->find();
  805. $large_str = explode(',',$data[$k]['large_str']);
  806. $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
  807. $large_num = $large_weight = 0;
  808. foreach ($large_list as $value){
  809. $large_num = $large_num + $value['l_num'];
  810. $large_weight = $large_weight + $value['l_weight'];
  811. }
  812. $data[$k]['l_num'] = $large_num/10000;
  813. $data[$k]['l_weight'] = $large_weight;
  814. }
  815. $good_list['address'] = $userinfo['company_address'];
  816. $good_list['data'] = $data;
  817. $good_list['count'] = count($data);
  818. $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
  819. if (empty($good_list['qrcode_add'])){
  820. $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  821. $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
  822. }
  823. if ($good_list['note'] == 'NULL'){
  824. $good_list['note'] = '';
  825. }
  826. //新增代码
  827. $good_array = $good_list ? $good_list->toArray() : [];
  828. // 假设 $goodArray 是已经转为普通数组的结构
  829. $dataList = $good_array['data'] ?? [];
  830. // 步骤1:按照 create_time 升序排序
  831. usort($dataList, function ($a, $b) {
  832. return strtotime($a['create_time']) - strtotime($b['create_time']);
  833. });
  834. // 步骤2:添加 large_ber 字段
  835. $start = 1;
  836. foreach ($dataList as &$item) {
  837. $count = intval($item['large_num']);
  838. $end = $start + $count - 1;
  839. $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  840. $start = $end + 1;
  841. }
  842. unset($item);
  843. $good_list['data'] = $dataList;
  844. // echo "<pre>";
  845. // print_r($dataList);
  846. // echo "</pre>";die;
  847. return json(['code'=>1,'data'=>$good_list]);
  848. }
  849. }
  850. /**
  851. * 获取辅料名称
  852. * @return \think\response\Json
  853. * @throws \think\db\exception\DataNotFoundException
  854. * @throws \think\db\exception\ModelNotFoundException
  855. * @throws \think\exception\DbException
  856. */
  857. public function matterName()
  858. {
  859. $userinfo = Session::get('admin');
  860. $lager = new QcodeLarge();
  861. $bach = new QcodeBach();
  862. $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
  863. $matter_name = [];
  864. if (!empty($bach_id)){
  865. foreach ($bach_id as $v){
  866. $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
  867. $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
  868. }
  869. }
  870. $matter_name = array_unique($matter_name);
  871. return json($matter_name);
  872. }
  873. /**
  874. * 发货单删除
  875. * @param $ids
  876. * @return void
  877. */
  878. public function dispatch_del($ids)
  879. {
  880. $goods = new QcodeGoods();
  881. $export = new QcodeExport();
  882. if (empty($ids)){
  883. $this->error('参数错误');
  884. }
  885. $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  886. if ($res === false){
  887. $this->error('删除失败');
  888. }
  889. $export_list = $goods->where('_id',$ids)->find();
  890. $export_id = explode(',',$export_list['export_id']);
  891. foreach ($export_id as $v){
  892. $result = $export->where('_id',$v)->update(['status'=>0]);
  893. if ($result === false){
  894. $this->error('删除失败');
  895. }
  896. }
  897. $this->success('成功');
  898. }
  899. /**
  900. * 收货列表
  901. */
  902. public function receive()
  903. {
  904. //当前是否为关联查询
  905. $this->relationSearch = false;
  906. //设置过滤方法
  907. $this->request->filter(['strip_tags', 'trim']);
  908. if ($this->request->isAjax()) {
  909. //如果发送的来源是Selectpage,则转发到Selectpage
  910. if ($this->request->request('keyField')) {
  911. return $this->selectpage();
  912. }
  913. $where = [
  914. 'delete_time'=> ''
  915. ];
  916. $req = input();
  917. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  918. $order = $req['order'];
  919. $offset = $req['offset'];
  920. $limit = $req['limit'];
  921. // 构造模糊查询条件
  922. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  923. // $filter = ['field' => $regex];
  924. $filter = json_decode($req['filter'], true);
  925. if (isset($filter['status'])){
  926. $where['status'] = intval($filter['status']);
  927. unset($filter['status']);
  928. }
  929. if (isset($filter['create_time'])){
  930. $begin = substr($filter['create_time'],0,19);
  931. $end = substr($filter['create_time'],22);
  932. $begin = strtotime($begin);
  933. $end = strtotime($end);
  934. $where['create_time'] = ['between',[$begin,$end]];
  935. unset($filter['create_time']);
  936. }
  937. foreach ($filter as $k => $v){
  938. $where[$k] = new \MongoDB\BSON\Regex($v);
  939. }
  940. $goods = new QcodeGoods();
  941. $total = $goods->where($where)->count();
  942. $list = $goods->where($where)
  943. ->order($sort,$order)
  944. ->limit($limit)
  945. ->skip($offset)
  946. ->select();
  947. foreach ($list as $k=>$v) {
  948. $oid = $v['_id']->jsonSerialize();
  949. $list[$k]['id'] = $oid['$oid'];
  950. }
  951. $result = array("total" => $total, "rows" => $list);
  952. return json($result);
  953. }
  954. return $this->view->fetch();
  955. }
  956. /**
  957. * 扫码/输入收货
  958. */
  959. public function receive_add()
  960. {
  961. $req = $this->request->param();
  962. if(!isset($req['shdh']) || empty($req['shdh'])){
  963. $this->error('请输入送货单号');
  964. }
  965. $data = [
  966. 'shdh'=>$req['shdh'],
  967. 'delete_time'=>'',
  968. ];
  969. $goods = new QcodeGoods();
  970. $row = $goods->where($data)->find();
  971. if (!$row) $this->error('未查询到该单号');
  972. if ($row['status']==2) $this->error('该单号已收货');
  973. //收货---修改status为2
  974. $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
  975. if ($bool){
  976. $this->success('成功');
  977. }else{
  978. $this->error('失败');
  979. }
  980. }
  981. /**
  982. * 取消收货
  983. */
  984. public function receive_del()
  985. {
  986. $req = $this->request->param();
  987. if(!isset($req['ids']) || empty($req['ids'])){
  988. $this->error('id不能为空');
  989. }
  990. $data = [
  991. '_id'=>$req['ids'],
  992. 'delete_time'=>'',
  993. ];
  994. $goods = new QcodeGoods();
  995. //收货---修改status为2
  996. $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
  997. if ($bool){
  998. $this->success('成功');
  999. }else{
  1000. $this->error('失败');
  1001. }
  1002. }
  1003. }