Deliver.php 42 KB

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