Deliver.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. 'order_ddbh' => $bachDetail['order_ddbh'],
  352. 'large_str' => $lager_id,
  353. 'status' => 0,
  354. 'bach_num' => $bachDetail['bach_num'],
  355. 'note' => '',
  356. 'user_id' => $userinfo['id'],
  357. 'create_time' => time(),
  358. 'file_dir' => $file_dir,
  359. 'company_id' => $userinfo['company'],
  360. 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
  361. 'small_num' => $n,//共包含了 N 个小件
  362. 'matter_name' => $bachDetail['matter_name'],//产品名称
  363. 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
  364. 'tray_num' => $bachDetail['tray_num'],//每层箱数
  365. 'box_num' => $bachDetail['box_num'],//每托层数
  366. 'pallet_height' => $bachDetail['pallet_height'],//每托高度
  367. 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
  368. 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
  369. 'larger_num' => $bachDetail['larger_num'],//总箱数
  370. ];
  371. if ($bachDetail['box_num'] != 1){
  372. $row['num'] = $bachDetail['box_num'];
  373. $row['mater_type'] = 1;
  374. }else{
  375. $row['num'] = $bachDetail['s_weight'];
  376. $row['mater_type'] = 2;
  377. }
  378. $res = $export->save($row);
  379. if ($res === false){
  380. $this->error('发货失败');
  381. }
  382. $lager = new QcodeLarge();
  383. $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
  384. $this->success('成功','',$file_dir);
  385. }
  386. /**
  387. * 表格数据处理
  388. * @param $bach_id
  389. * @param $large_str
  390. * @param $company
  391. * @param $large_num
  392. * @param $small_num
  393. * @return array
  394. * @throws \think\db\exception\DataNotFoundException
  395. * @throws \think\db\exception\ModelNotFoundException
  396. * @throws \think\exception\DbException
  397. */
  398. public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
  399. {
  400. $bach = new QcodeBach();
  401. $large = new QcodeLarge();
  402. $small = new QcodeSmall();
  403. $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
  404. $data = [
  405. 'supplier_name' => $bachList['supplier_name'],
  406. 'supplier_no' => $bachList['supplier_code'],
  407. 'matter_name' => $bachList['matter_name'],
  408. 'matter_no' => $bachList['matter_no'],
  409. 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
  410. 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
  411. 'box_num' => $large_num,
  412. 'tray_num' => $bachList['tray_num'],
  413. 'small_num' => $small_num,
  414. 'l_reservation' => $bachList['l_reservation'],
  415. 's_reservation' => $bachList['s_reservation'],
  416. 'l_flow' => $bachList['l_flow'],
  417. 's_flow' => $bachList['s_flow'],
  418. 'l_weight' => 0,
  419. 's_weight' => $bachList['s_weight']/1000,
  420. 'code' => [],
  421. ];
  422. $large_id = explode(',', $large_str);
  423. foreach ($large_id as $k => $v) {
  424. $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
  425. $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
  426. $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
  427. foreach ($small_list as $value) {
  428. $code = [
  429. 'large_code' => $largeList['code'],
  430. 'small_code' => $value['code'],
  431. ];
  432. array_push($data['code'],$code);
  433. }
  434. }
  435. return $data;
  436. }
  437. /**
  438. * 生成excel
  439. * @param $data
  440. * @return string
  441. * @throws Exception
  442. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  443. */
  444. public function exportExcel($data)
  445. {
  446. $objexcel = new Spreadsheet();
  447. $sheet = $objexcel->getActiveSheet();
  448. //设置全局单元格垂直居中
  449. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  450. //设置全局单元格水平居中
  451. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  452. //设置全局单元格自动换行
  453. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  454. //设置单元格宽度
  455. $sheet->getDefaultColumnDimension()->setWidth(50);
  456. //设置表头
  457. $sheet->setCellValue('A1', '文件流水号');
  458. $sheet->setCellValue('A2', '订单号');
  459. $sheet->setCellValue('A3', '供应商');
  460. $sheet->setCellValue('A4', '供应商代码');
  461. $sheet->setCellValue('A5', '物料名称');
  462. $sheet->setCellValue('A6', '物料代码');
  463. $sheet->setCellValue('A7', '生产日期');
  464. $sheet->setCellValue('A8', '打码日期');
  465. $sheet->setCellValue('A9', '大件数量');
  466. $sheet->setCellValue('A10', '托盘小件数量');
  467. $sheet->setCellValue('A11', '小件总数量');
  468. $sheet->setCellValue('C3', '大件预留码');
  469. $sheet->setCellValue('C5', '小件预留码');
  470. $sheet->setCellValue('C7', '大件开始流水号');
  471. $sheet->setCellValue('C9', '小件开始流水号');
  472. $sheet->setCellValue('C11', '大件重量(Kg)');
  473. $sheet->setCellValue('C13', '小件重量(Kg)');
  474. $sheet->setCellValue('A15', '大件二维码');
  475. $sheet->setCellValue('B15', '大件条码(预留)');
  476. $sheet->setCellValue('C15', '小件二维码');
  477. $sheet->setCellValue('D15', '小件条码(预留)');
  478. //插入表格数据
  479. $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
  480. $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
  481. $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
  482. $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
  483. $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
  484. $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
  485. $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
  486. $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
  487. $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
  488. $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
  489. $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
  490. $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
  491. $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
  492. $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
  493. $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
  494. foreach ($data['code'] as $k=>$v){
  495. $m = $k+16;
  496. $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
  497. $sheet->getCell('B'.$m)->setValue('');
  498. $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
  499. $sheet->getCell('D'.$m)->setValue('');
  500. }
  501. //文件另存
  502. $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
  503. // $filename = iconv("utf-8","gb2312",$filename);
  504. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  505. header('Content-Disposition:attachment;filename='.$filename);
  506. header('Cache-Control: max-age=0');
  507. header("Content-Type:text/html;charset=utf-8");
  508. if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
  509. {
  510. mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
  511. }
  512. $writer = IOFactory::createWriter($objexcel, 'Xlsx');
  513. $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
  514. return 'uploads/export/'.$filename;
  515. }
  516. /**
  517. * 批次发货申请
  518. * @return string|\think\response\Json
  519. * @throws \think\Exception
  520. */
  521. public function apply()
  522. {
  523. $export = new QcodeExport();
  524. $this->request->filter(['strip_tags', 'trim']);
  525. if (false === $this->request->isAjax()) {
  526. return $this->view->fetch();
  527. }
  528. if ($this->request->request('keyField')) {
  529. return $this->selectpage();
  530. }
  531. // 接收参数
  532. $userInfo = Session::get('admin');
  533. $where = [
  534. 'company_id' => $userInfo['company'],
  535. 'delete_time'=> ''
  536. ];
  537. $filter = input('filter');
  538. $filter = json_decode($filter,true);
  539. //样品编号查询
  540. if (isset($filter['matter_name'])){
  541. $where['matter_no'] =$filter['matter_name'];
  542. }
  543. if (isset($filter['username'])){
  544. $where['username'] = $filter['username'];
  545. }
  546. if (isset($filter['status'])){
  547. $where['status'] = (int)$filter['status'];
  548. }
  549. if (isset($filter['create_time'])){
  550. $begin = substr($filter['create_time'],0,19);
  551. $end = substr($filter['create_time'],22);
  552. $begin = strtotime($begin);
  553. $end = strtotime($end);
  554. $where['create_time'] = ['between',[$begin,$end]];
  555. }
  556. $order = input('order');
  557. $offset = input('offset');
  558. $limit = input('limit');
  559. $total = $export->where($where)->count();
  560. $export_list = $export
  561. ->order('_id',$order)
  562. ->where($where)
  563. ->whereIn('status',[0,1])
  564. ->skip($offset)
  565. ->limit($limit)
  566. ->select();
  567. $list=[];
  568. foreach ($export_list as $k=>$v) {
  569. $list[$k]['large_num'] = $v['large_num'];//托盘数量
  570. $list[$k]['total_boxes'] = $v['total_boxes'];//每箱托数
  571. $list[$k]['tray_num'] = $v['tray_num'];//每层箱数
  572. $list[$k]['box_num'] = $v['box_num'];//每托层数
  573. $list[$k]['pallet_height'] = $v['pallet_height'];//托盘高度
  574. $list[$k]['pallet_length'] = $v['pallet_length'];//托盘规格
  575. $list[$k]['pallet_width'] = $v['pallet_width'];//托盘规格
  576. $list[$k]['small_num'] = $v['small_num'];//总箱数
  577. $list[$k]['larger_num'] = $v['larger_num'];//总箱数
  578. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  579. $list[$k]['matter_name'] = $v['matter_name'];
  580. $list[$k]['matter_no'] = $v['matter_no'];
  581. $list[$k]['username'] = $v['username'];
  582. $list[$k]['large_num'] = $v['large_num'];
  583. $list[$k]['small_num'] = $v['small_num'];
  584. $list[$k]['create_time'] = $v['create_time'];
  585. $list[$k]['file_dir'] = $v['file_dir'];
  586. $list[$k]['status'] = $v['status'];
  587. }
  588. $result = ['total'=>$total,'rows'=>$list];
  589. return json($result);
  590. }
  591. /**
  592. * 批次发货删除
  593. * @param $ids
  594. * @return void
  595. */
  596. public function apply_del($ids)
  597. {
  598. $export = new QcodeExport();
  599. $userinfo = Session::get('admin');
  600. $large = new QcodeLarge();
  601. if (empty($ids)){
  602. $this->error('请求错误');
  603. }
  604. $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  605. if ($res === false){
  606. $this->error('删除失败');
  607. }
  608. $large_list = $export->where('_id',$ids)->find();
  609. $large_id = explode(',',$large_list['large_str']);
  610. foreach ($large_id as $v){
  611. $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
  612. if ($result === 0){
  613. $this->error('删除失败');
  614. }
  615. }
  616. $this->success('成功');
  617. }
  618. /**
  619. * 司机弹窗
  620. * @param $ids
  621. * @return string
  622. * @throws \think\Exception
  623. */
  624. public function goods($ids)
  625. {
  626. $this->view->assign('ids',$ids);
  627. return $this->view->fetch();
  628. }
  629. /**
  630. * 批次发货提交
  631. * @return void
  632. * @throws \think\db\exception\DataNotFoundException
  633. * @throws \think\db\exception\ModelNotFoundException
  634. * @throws \think\exception\DbException
  635. */
  636. public function apply_add()
  637. {
  638. $goods = new QcodeGoods();
  639. $export = new QcodeExport();
  640. $userinfo = Session::get('admin');
  641. if ($this->request->isAjax() === false){
  642. $this->error('请求错误');
  643. }
  644. $ids = input('ids');
  645. $order_number = input('order_number');
  646. $deliveryman = input('deliveryman');
  647. $shr_phone = input('shr_phone');
  648. $plate_number = input('plate_number');
  649. $note = input('note');
  650. $id_list = [];
  651. if (strpos($ids,',') === false){
  652. $id_list[0] = $ids;
  653. }else{
  654. $id_list = explode(',',$ids);
  655. }
  656. $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
  657. if ($update_status){
  658. $export_list = $export->whereIn('_id',$id_list)->select();
  659. $daterq = date('ymd',time());
  660. $data = [
  661. 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),
  662. 'order_number' => $order_number,
  663. 'deliveryman' => $deliveryman,
  664. 'shr_phone' => $shr_phone,
  665. 'plate_number' => $plate_number,
  666. 'shrq_date' => '',
  667. 'supplier_name' => $export_list[0]['username'],
  668. 'supplier_id' => $userinfo['id'],
  669. 'user_id' => $userinfo['id'],
  670. 'status' => 0,
  671. 'export_id' => $ids,
  672. 'create_time' => time(),
  673. 'note' => $note,
  674. 'company_id' => $userinfo['company'],
  675. ];
  676. $data['qrcode_add'] = $this->Qrcode($data['shdh']);
  677. $res = $goods->save($data);
  678. if ($res === false){
  679. $this->error('失败');
  680. }
  681. $this->success('成功');
  682. }else{
  683. $this->error('失败');
  684. }
  685. }
  686. /**
  687. * 生成二维码
  688. * @param $url
  689. * @return string
  690. */
  691. public function Qrcode($url)
  692. {
  693. $level=2;
  694. $size=5;
  695. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  696. $errorCorrectionLevel =intval($level) ;//容错级别
  697. $matrixPointSize = intval($size);//生成图片大小
  698. //生成二维码图片
  699. $object = new \QRcode();
  700. //打开缓冲区
  701. ob_start();
  702. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  703. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  704. $imageString = base64_encode(ob_get_contents());
  705. //关闭缓冲区
  706. ob_end_clean();
  707. //把生成的base64字符串返回给前端
  708. return 'data:image/png;base64,'.$imageString;
  709. }
  710. /**
  711. * 发货单
  712. * @return string|\think\response\Json
  713. * @throws \think\Exception
  714. */
  715. public function dispatch()
  716. {
  717. $export = new QcodeGoods();
  718. $this->request->filter(['strip_tags', 'trim']);
  719. if (false === $this->request->isAjax()) {
  720. return $this->view->fetch();
  721. }
  722. if ($this->request->request('keyField')) {
  723. return $this->selectpage();
  724. }
  725. // 接收参数
  726. $userInfo = Session::get('admin');
  727. $where = [
  728. 'company_id' => $userInfo['company'],
  729. 'delete_time'=> ''
  730. ];
  731. $search = input('search');
  732. if (isset($search)){
  733. $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
  734. }
  735. $filter = input('filter');
  736. $filter = json_decode($filter,true);
  737. //订单编号查询
  738. if (isset($filter['order_number'])){
  739. $where['order_number'] =$filter['order_number'];
  740. }
  741. //所属公司查询
  742. if (isset($filter['supplier_name'])){
  743. $where['supplier_name'] = $filter['supplier_name'];
  744. }
  745. if (isset($filter['status'])){
  746. $where['status'] = (int)$filter['status'];
  747. }
  748. if (isset($filter['create_time'])){
  749. $begin = substr($filter['create_time'],0,19);
  750. $end = substr($filter['create_time'],22);
  751. $begin = strtotime($begin);
  752. $end = strtotime($end);
  753. $where['create_time'] = ['between',[$begin,$end]];
  754. }
  755. $order = input('order');
  756. $offset = input('offset');
  757. $limit = input('limit');
  758. $total = $export->where($where)->count();
  759. $export_list = $export
  760. ->order('_id',$order)
  761. ->where($where)
  762. ->whereIn('status',[0,1])
  763. ->skip($offset)
  764. ->limit($limit)
  765. ->select();
  766. $list=[];
  767. foreach ($export_list as $k=>$v) {
  768. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  769. $list[$k]['shdh'] = $v['shdh'];
  770. $list[$k]['order_number'] = $v['order_number'];
  771. $list[$k]['supplier_name'] = $v['supplier_name'];
  772. $list[$k]['deliveryman'] = $v['deliveryman'];
  773. $list[$k]['plate_number'] = $v['plate_number'];
  774. $list[$k]['create_time'] = $v['create_time'];
  775. $list[$k]['status'] = $v['status'];
  776. }
  777. $result = ['total'=>$total,'rows'=>$list];
  778. return json($result);
  779. }
  780. /**
  781. * 发货单打印
  782. * @return \think\response\Json|void
  783. * @throws \think\db\exception\DataNotFoundException
  784. * @throws \think\db\exception\ModelNotFoundException
  785. * @throws \think\exception\DbException
  786. */
  787. public function printqrcode()
  788. {
  789. $goods = new QcodeGoods();
  790. $export = new QcodeExport();
  791. $large = new QcodeLarge();
  792. $userinfo = Session::get('admin');
  793. if ($this->request->isAjax() === false) {
  794. $this->error('请求错误');
  795. }
  796. $id = input('id');
  797. $good_list = $goods->where('_id', $id)->find();
  798. if ($good_list) {
  799. if (strpos($good_list['export_id'], ',') === false) {
  800. $export_id[0] = $good_list['export_id'];
  801. } else {
  802. $export_id = explode(',', $good_list['export_id']);
  803. }
  804. $data = [];
  805. $motter_nos = []; // 用于存储 motter_no 字段
  806. foreach ($export_id as $k => $v) {
  807. $data[$k] = $export->where('_id', $v)->find();
  808. // 确保 $data[$k] 是一个有效的对象
  809. if ($data[$k] instanceof QcodeExport) {
  810. if (!empty($data[$k]['order_ddbh'])) {
  811. $motter_nos[] = $data[$k]['order_ddbh'];
  812. }
  813. }
  814. $large_str = explode(',', $data[$k]['large_str']);
  815. $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
  816. $large_num = $large_weight = 0;
  817. foreach ($large_list as $value) {
  818. $large_num += $value['l_num']; // 累加数量
  819. $large_weight += $value['l_weight']; // 累加重量
  820. }
  821. // 更新数据
  822. $data[$k]['l_num'] = $large_num / 10000; // 转换数量
  823. $data[$k]['l_weight'] = $large_weight; // 更新重量
  824. // 添加 motter_no 到数组
  825. if (!empty($data[$k]['order_ddbh'])) {
  826. $order_ddbh[] = $data[$k]['order_ddbh'];
  827. }
  828. }
  829. // 拼接 motter_no 字段
  830. $good_list['order_ddbh'] = implode(',', $order_ddbh);
  831. // 继续处理其他数据
  832. $good_list['address'] = $userinfo['company_address'];
  833. $good_list['data'] = $data;
  834. $good_list['count'] = count($data);
  835. $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
  836. if (empty($good_list['qrcode_add'])) {
  837. $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  838. $goods->where('_id', $id)->update(['qrcode_add' => $good_list['qrcode_add']]);
  839. }
  840. if ($good_list['note'] === 'NULL') {
  841. $good_list['note'] = '';
  842. }
  843. // 新增代码
  844. $good_array = $good_list ? $good_list->toArray() : [];
  845. $dataList = $good_array['data'] ?? [];
  846. // 步骤1:按照 create_time 升序排序
  847. usort($dataList, function ($a, $b) {
  848. return strtotime($a['create_time']) - strtotime($b['create_time']);
  849. });
  850. // 步骤2:添加 large_ber 字段
  851. $start = 1;
  852. foreach ($dataList as &$item) {
  853. $count = intval($item['large_num']);
  854. $end = $start + $count - 1;
  855. $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  856. $start = $end + 1;
  857. }
  858. unset($item);
  859. $good_list['data'] = $dataList;
  860. return json(['code' => 1, 'data' => $good_list]);
  861. }
  862. }
  863. // public function printqrcode()
  864. // {
  865. // $goods = new QcodeGoods();
  866. // $export = new QcodeExport();
  867. // $large = new QcodeLarge();
  868. // $userinfo = Session::get('admin');
  869. // if ($this->request->isAjax() === false) {
  870. // $this->error('请求错误');
  871. // }
  872. // $id = input('id');
  873. // $good_list = $goods->where('_id',$id)->find();
  874. //
  875. // if ($good_list){
  876. // if (strpos($good_list['export_id'],',') === false){
  877. // $export_id[0] = $good_list['export_id'];
  878. // }else{
  879. // $export_id = explode(',',$good_list['export_id']);
  880. // }
  881. // $data = [];
  882. // foreach ($export_id as $k=>$v){
  883. // $data[$k] = $export->where('_id',$v)->find();
  884. // $large_str = explode(',',$data[$k]['large_str']);
  885. // $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
  886. // $large_num = $large_weight = 0;
  887. // foreach ($large_list as $value){
  888. // $large_num = $large_num + $value['l_num'];
  889. // $large_weight = $large_weight + $value['l_weight'];
  890. // }
  891. // $data[$k]['l_num'] = $large_num/10000;
  892. // $data[$k]['l_weight'] = $large_weight;
  893. // }
  894. // $good_list['address'] = $userinfo['company_address'];
  895. // $good_list['data'] = $data;
  896. // $good_list['count'] = count($data);
  897. // $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
  898. // if (empty($good_list['qrcode_add'])){
  899. // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  900. // $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
  901. // }
  902. // if ($good_list['note'] == 'NULL'){
  903. // $good_list['note'] = '';
  904. // }
  905. //
  906. // //新增代码
  907. // $good_array = $good_list ? $good_list->toArray() : [];
  908. // // 假设 $goodArray 是已经转为普通数组的结构
  909. // $dataList = $good_array['data'] ?? [];
  910. // // 步骤1:按照 create_time 升序排序
  911. // usort($dataList, function ($a, $b) {
  912. // return strtotime($a['create_time']) - strtotime($b['create_time']);
  913. // });
  914. // // 步骤2:添加 large_ber 字段
  915. // $start = 1;
  916. // foreach ($dataList as &$item) {
  917. // $count = intval($item['large_num']);
  918. // $end = $start + $count - 1;
  919. // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  920. // $start = $end + 1;
  921. // }
  922. // unset($item);
  923. // $good_list['data'] = $dataList;
  924. //
  925. // return json(['code'=>1,'data'=>$good_list]);
  926. // }
  927. // }
  928. /**
  929. * 获取辅料名称
  930. * @return \think\response\Json
  931. * @throws \think\db\exception\DataNotFoundException
  932. * @throws \think\db\exception\ModelNotFoundException
  933. * @throws \think\exception\DbException
  934. */
  935. public function matterName()
  936. {
  937. $userinfo = Session::get('admin');
  938. $lager = new QcodeLarge();
  939. $bach = new QcodeBach();
  940. $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
  941. $matter_name = [];
  942. if (!empty($bach_id)){
  943. foreach ($bach_id as $v){
  944. $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
  945. $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
  946. }
  947. }
  948. $matter_name = array_unique($matter_name);
  949. return json($matter_name);
  950. }
  951. /**
  952. * 发货单删除
  953. * @param $ids
  954. * @return void
  955. */
  956. public function dispatch_del($ids)
  957. {
  958. $goods = new QcodeGoods();
  959. $export = new QcodeExport();
  960. if (empty($ids)){
  961. $this->error('参数错误');
  962. }
  963. $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  964. if ($res === false){
  965. $this->error('删除失败');
  966. }
  967. $export_list = $goods->where('_id',$ids)->find();
  968. $export_id = explode(',',$export_list['export_id']);
  969. foreach ($export_id as $v){
  970. $result = $export->where('_id',$v)->update(['status'=>0]);
  971. if ($result === false){
  972. $this->error('删除失败');
  973. }
  974. }
  975. $this->success('成功');
  976. }
  977. /**
  978. * 收货列表
  979. */
  980. public function receive()
  981. {
  982. //当前是否为关联查询
  983. $this->relationSearch = false;
  984. //设置过滤方法
  985. $this->request->filter(['strip_tags', 'trim']);
  986. if ($this->request->isAjax()) {
  987. //如果发送的来源是Selectpage,则转发到Selectpage
  988. if ($this->request->request('keyField')) {
  989. return $this->selectpage();
  990. }
  991. $where = [
  992. 'delete_time'=> ''
  993. ];
  994. $req = input();
  995. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  996. $order = $req['order'];
  997. $offset = $req['offset'];
  998. $limit = $req['limit'];
  999. // 构造模糊查询条件
  1000. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  1001. // $filter = ['field' => $regex];
  1002. $filter = json_decode($req['filter'], true);
  1003. if (isset($filter['status'])){
  1004. $where['status'] = intval($filter['status']);
  1005. unset($filter['status']);
  1006. }
  1007. if (isset($filter['create_time'])){
  1008. $begin = substr($filter['create_time'],0,19);
  1009. $end = substr($filter['create_time'],22);
  1010. $begin = strtotime($begin);
  1011. $end = strtotime($end);
  1012. $where['create_time'] = ['between',[$begin,$end]];
  1013. unset($filter['create_time']);
  1014. }
  1015. foreach ($filter as $k => $v){
  1016. $where[$k] = new \MongoDB\BSON\Regex($v);
  1017. }
  1018. $goods = new QcodeGoods();
  1019. $total = $goods->where($where)->count();
  1020. $list = $goods->where($where)
  1021. ->order($sort,$order)
  1022. ->limit($limit)
  1023. ->skip($offset)
  1024. ->select();
  1025. foreach ($list as $k=>$v) {
  1026. $oid = $v['_id']->jsonSerialize();
  1027. $list[$k]['id'] = $oid['$oid'];
  1028. }
  1029. $result = array("total" => $total, "rows" => $list);
  1030. return json($result);
  1031. }
  1032. return $this->view->fetch();
  1033. }
  1034. /**
  1035. * 扫码/输入收货
  1036. */
  1037. public function receive_add()
  1038. {
  1039. $req = $this->request->param();
  1040. if(!isset($req['shdh']) || empty($req['shdh'])){
  1041. $this->error('请输入送货单号');
  1042. }
  1043. $data = [
  1044. 'shdh'=>$req['shdh'],
  1045. 'delete_time'=>'',
  1046. ];
  1047. $goods = new QcodeGoods();
  1048. $row = $goods->where($data)->find();
  1049. if (!$row) $this->error('未查询到该单号');
  1050. if ($row['status']==2) $this->error('该单号已收货');
  1051. //收货---修改status为2
  1052. $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
  1053. if ($bool){
  1054. $this->success('成功');
  1055. }else{
  1056. $this->error('失败');
  1057. }
  1058. }
  1059. /**
  1060. * 取消收货
  1061. */
  1062. public function receive_del()
  1063. {
  1064. $req = $this->request->param();
  1065. if(!isset($req['ids']) || empty($req['ids'])){
  1066. $this->error('id不能为空');
  1067. }
  1068. $data = [
  1069. '_id'=>$req['ids'],
  1070. 'delete_time'=>'',
  1071. ];
  1072. $goods = new QcodeGoods();
  1073. //收货---修改status为2
  1074. $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
  1075. if ($bool){
  1076. $this->success('成功');
  1077. }else{
  1078. $this->error('失败');
  1079. }
  1080. }
  1081. }