Deliver.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. $bach_list = $bach->name($userInfo['company'].'_'.'qcode_bach')->where($whereList)->column('_id');
  76. $bach_id = [];
  77. foreach ($bach_list as $v){
  78. $id = substr(json_encode($v),9,-2);
  79. array_push($bach_id,$id);
  80. }
  81. $order = input('order');
  82. $offset = input('offset');
  83. $limit = input('limit');
  84. $total = $large->name($userInfo['company'].'_'.'qcode_large')->where('l_status',0)->where($where)->whereIn('bach_id',$bach_id)->count();
  85. $large_list = $large->name($userInfo['company'].'_'.'qcode_large')
  86. ->order('create_time')
  87. ->where($where)
  88. ->where('l_status',0)
  89. ->whereIn('bach_id',$bach_id)
  90. ->skip($offset)
  91. ->limit($limit)
  92. ->select();
  93. $list=[];
  94. foreach ($large_list as $k=>$v) {
  95. $bach_detail = $bach->name($userInfo['company'].'_'.'qcode_bach')->where('_id',$v['bach_id'])->find();
  96. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  97. $list[$k]['bach'] = $bach_detail['bach_num'];
  98. $list[$k]['l_flow'] = ltrim(substr($v['code'],53,6),'0');
  99. $list[$k]['matter_name'] = $bach_detail['matter_name'];
  100. $list[$k]['manufacture_date'] = date('Y-m-d',strtotime('20'.$bach_detail['manufacture_date']));
  101. $list[$k]['code'] = $v['code'];
  102. $small_num = $small->name($userInfo['company'].'_'.'qcode_small')->where('large_id',substr(json_encode($v['_id']),9,-2))->count();
  103. $list[$k]['small_num'] = $small_num;
  104. }
  105. $result = ['total'=>$total,'rows'=>$list];
  106. return json($result);
  107. }
  108. /**
  109. * 小件列表
  110. * @return \think\response\Json
  111. * @throws \think\Exception
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. */
  116. public function smallList()
  117. {
  118. $userinfo = Session::get('admin');
  119. $lager = new QcodeLarge();
  120. $small = new QcodeSmall();
  121. if ($this->request->isAjax() === false){
  122. $this->error('请求错误');
  123. }
  124. $filter = input('filter');
  125. $filter = json_decode($filter,true);
  126. $order = input('order');
  127. $offset = input('offset');
  128. $limit = input('limit');
  129. $where = [
  130. 'delete_time' => '',
  131. ];
  132. if (isset($filter['large_id'])){
  133. $where['large_id'] =$filter['large_id'];
  134. }
  135. $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
  136. ->where($where)
  137. ->skip($offset)
  138. ->limit($limit)
  139. ->select();
  140. $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
  141. $list = [];
  142. foreach ($smallList as $k=>$v){
  143. $list[$k] = [
  144. 'code' => $v['code'],
  145. 'l_flow' => $v['l_flow'],
  146. 'print_num' => $v['p_nums'],
  147. 'status' => $v['status'],
  148. ];
  149. }
  150. return json(['total'=>$total,'rows'=>$list]);
  151. }
  152. /**
  153. * 导出发货
  154. * @return void
  155. * @throws \think\Exception
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. * @throws \think\exception\DbException
  159. */
  160. public function print()
  161. {
  162. $userinfo = Session::get('admin');
  163. $lager = new QcodeLarge();
  164. $bach = new QcodeBach();
  165. $small = new QcodeSmall();
  166. $export = new QcodeExport();
  167. if ($this->request->isAjax() === false){
  168. $this->error('请求错误');
  169. }
  170. $lager_id = input('lager_id');
  171. if ($lager_id === ''){
  172. $this->error('请至少选择一个大件');
  173. }
  174. $lagerId = [];
  175. if (strpos($lager_id,',') === false){
  176. $lagerId[0] = $lager_id;
  177. }else{
  178. $lagerId = explode(',',$lager_id);
  179. }
  180. $lager_num = count($lagerId);
  181. $bach_id = [];
  182. foreach ($lagerId as $k=>$v){
  183. $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
  184. $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
  185. $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
  186. }
  187. $bach_id = array_unique($bach_id);
  188. $data = [];
  189. foreach ($bach_id as $k=>$v){
  190. $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
  191. }
  192. $data = array_unique($data);
  193. if (count($data)>1){
  194. $this->error('请选择同一产品');
  195. }
  196. $n =0;
  197. foreach (explode(',',$lager_id) as $kk=>$vv){
  198. $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
  199. }
  200. $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
  201. if ($bachDetail['box_num'] === 1){
  202. $mater_type = 2;
  203. }else{
  204. $mater_type = 1;
  205. }
  206. $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
  207. $file_dir = $this->exportExcel($exportData);
  208. $row = [
  209. 'username' => $bachDetail['supplier_name'],
  210. 'matter_name' => $bachDetail['matter_name'],
  211. 'matter_no' => $bachDetail['matter_no'],
  212. 'large_str' => $lager_id,
  213. 'large_num' => $lager_num,
  214. 'small_num' => $n,
  215. 'status' => 0,
  216. 'bach_num' => $bachDetail['bach_num'],
  217. 'note' => '',
  218. 'user_id' => $userinfo['id'],
  219. 'create_time' => time(),
  220. 'file_dir' => $file_dir,
  221. 'company_id' => $userinfo['company'],
  222. ];
  223. if ($bachDetail['box_num'] != 1){
  224. $row['num'] = $bachDetail['box_num'];
  225. $row['mater_type'] = 1;
  226. }else{
  227. $row['num'] = $bachDetail['s_weight'];
  228. $row['mater_type'] = 2;
  229. }
  230. $res = $export->save($row);
  231. if ($res === false){
  232. $this->error('发货失败');
  233. }
  234. $lager = new QcodeLarge();
  235. $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
  236. $this->success('成功','',$file_dir);
  237. }
  238. /**
  239. * 表格数据处理
  240. * @param $bach_id
  241. * @param $large_str
  242. * @param $company
  243. * @param $large_num
  244. * @param $small_num
  245. * @return array
  246. * @throws \think\db\exception\DataNotFoundException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. * @throws \think\exception\DbException
  249. */
  250. public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
  251. {
  252. $bach = new QcodeBach();
  253. $large = new QcodeLarge();
  254. $small = new QcodeSmall();
  255. $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
  256. $data = [
  257. 'supplier_name' => $bachList['supplier_name'],
  258. 'supplier_no' => $bachList['supplier_code'],
  259. 'matter_name' => $bachList['matter_name'],
  260. 'matter_no' => $bachList['matter_no'],
  261. 'manufacture_date' => $bachList['manufacture_date'],
  262. 'print_date' => $bachList['print_date'],
  263. 'box_num' => $large_num,
  264. 'tray_num' => $bachList['tray_num'],
  265. 'small_num' => $small_num,
  266. 'l_reservation' => $bachList['l_reservation'],
  267. 's_reservation' => $bachList['s_reservation'],
  268. 'l_flow' => $bachList['l_flow'],
  269. 's_flow' => $bachList['s_flow'],
  270. 'l_weight' => $bachList['l_weight'],
  271. 's_weight' => $bachList['s_weight'],
  272. 'code' => [],
  273. ];
  274. $large_id = explode(',', $large_str);
  275. foreach ($large_id as $k => $v) {
  276. $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
  277. $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
  278. foreach ($small_list as $value) {
  279. $code = [
  280. 'large_code' => $largeList['code'],
  281. 'small_code' => $value['code'],
  282. ];
  283. $data['code'][] = $code;
  284. }
  285. }
  286. return $data;
  287. }
  288. /**
  289. * 生成excel
  290. * @param $data
  291. * @return string
  292. * @throws Exception
  293. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  294. */
  295. public function exportExcel($data)
  296. {
  297. $objexcel = new Spreadsheet();
  298. $sheet = $objexcel->getActiveSheet();
  299. //设置全局单元格垂直居中
  300. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  301. //设置全局单元格水平居中
  302. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  303. //设置全局单元格自动换行
  304. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  305. //设置单元格宽度
  306. $sheet->getDefaultColumnDimension()->setWidth(50);
  307. //设置表头
  308. $sheet->setCellValue('A1', '文件流水号');
  309. $sheet->setCellValue('A2', '订单号');
  310. $sheet->setCellValue('A3', '供应商');
  311. $sheet->setCellValue('A4', '供应商代码');
  312. $sheet->setCellValue('A5', '物料名称');
  313. $sheet->setCellValue('A6', '物料代码');
  314. $sheet->setCellValue('A7', '生产日期');
  315. $sheet->setCellValue('A8', '打码日期');
  316. $sheet->setCellValue('A9', '大件数量');
  317. $sheet->setCellValue('A10', '托盘小件数量');
  318. $sheet->setCellValue('A11', '小件总数量');
  319. $sheet->setCellValue('C3', '大件预留码');
  320. $sheet->setCellValue('C5', '小件预留码');
  321. $sheet->setCellValue('C7', '大件开始流水号');
  322. $sheet->setCellValue('C9', '小件开始流水号');
  323. $sheet->setCellValue('C11', '大件重量(Kg)');
  324. $sheet->setCellValue('C13', '小件重量(Kg)');
  325. $sheet->setCellValue('A15', '大件二维码');
  326. $sheet->setCellValue('B15', '大件条码(预留)');
  327. $sheet->setCellValue('C15', '小件二维码');
  328. $sheet->setCellValue('D15', '小件条码(预留)');
  329. //插入表格数据
  330. $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
  331. $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
  332. $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
  333. $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
  334. $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
  335. $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
  336. $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
  337. $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
  338. $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
  339. $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
  340. $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
  341. $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
  342. $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
  343. $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
  344. $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
  345. foreach ($data['code'] as $v){
  346. $sheet->getCell('A16')->setValue(isset($v['large_code'])?$v['large_code']:'');
  347. $sheet->getCell('B16')->setValue('');
  348. $sheet->getCell('C16')->setValue(isset($v['small_code'])?$v['small_code']:'');
  349. $sheet->getCell('D16')->setValue('');
  350. }
  351. //文件另存
  352. $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
  353. // $filename = iconv("utf-8","gb2312",$filename);
  354. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  355. header('Content-Disposition:attachment;filename='.$filename);
  356. header('Cache-Control: max-age=0');
  357. header("Content-Type:text/html;charset=utf-8");
  358. if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
  359. {
  360. mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
  361. }
  362. $writer = IOFactory::createWriter($objexcel, 'Xlsx');
  363. $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
  364. return 'uploads/export/'.$filename;
  365. }
  366. /**
  367. * 批次发货申请
  368. * @return string|\think\response\Json
  369. * @throws \think\Exception
  370. */
  371. public function apply()
  372. {
  373. $export = new QcodeExport();
  374. $this->request->filter(['strip_tags', 'trim']);
  375. if (false === $this->request->isAjax()) {
  376. return $this->view->fetch();
  377. }
  378. if ($this->request->request('keyField')) {
  379. return $this->selectpage();
  380. }
  381. // 接收参数
  382. $userInfo = Session::get('admin');
  383. $where = [
  384. 'company_id' => $userInfo['company'],
  385. 'delete_time'=> ''
  386. ];
  387. $filter = input('filter');
  388. $filter = json_decode($filter,true);
  389. //样品编号查询
  390. if (isset($filter['matter_name'])){
  391. $where['matter_no'] =$filter['matter_name'];
  392. }
  393. if (isset($filter['username'])){
  394. $where['username'] = $filter['username'];
  395. }
  396. if (isset($filter['status'])){
  397. $where['status'] = (int)$filter['status'];
  398. }
  399. if (isset($filter['create_time'])){
  400. $begin = substr($filter['create_time'],0,19);
  401. $end = substr($filter['create_time'],22);
  402. $begin = strtotime($begin);
  403. $end = strtotime($end);
  404. $where['create_time'] = ['between',[$begin,$end]];
  405. }
  406. $order = input('order');
  407. $offset = input('offset');
  408. $limit = input('limit');
  409. $total = $export->where($where)->count();
  410. $export_list = $export
  411. ->order('_id',$order)
  412. ->where($where)
  413. ->whereIn('status',[0,1])
  414. ->skip($offset)
  415. ->limit($limit)
  416. ->select();
  417. $list=[];
  418. foreach ($export_list as $k=>$v) {
  419. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  420. $list[$k]['matter_name'] = $v['matter_name'];
  421. $list[$k]['matter_no'] = $v['matter_no'];
  422. $list[$k]['username'] = $v['username'];
  423. $list[$k]['large_num'] = $v['large_num'];
  424. $list[$k]['small_num'] = $v['small_num'];
  425. $list[$k]['create_time'] = $v['create_time'];
  426. $list[$k]['file_dir'] = $v['file_dir'];
  427. $list[$k]['status'] = $v['status'];
  428. }
  429. $result = ['total'=>$total,'rows'=>$list];
  430. return json($result);
  431. }
  432. /**
  433. * 批次发货删除
  434. * @param $ids
  435. * @return void
  436. */
  437. public function apply_del($ids)
  438. {
  439. $export = new QcodeExport();
  440. $userinfo = Session::get('admin');
  441. $large = new QcodeLarge();
  442. if (empty($ids)){
  443. $this->error('请求错误');
  444. }
  445. $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  446. if ($res === false){
  447. $this->error('删除失败');
  448. }
  449. $large_list = $export->where('_id',$ids)->find();
  450. $large_id = explode(',',$large_list['large_str']);
  451. foreach ($large_id as $v){
  452. $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
  453. if ($result === 0){
  454. $this->error('删除失败');
  455. }
  456. }
  457. $this->success('成功');
  458. }
  459. /**
  460. * 司机弹窗
  461. * @param $ids
  462. * @return string
  463. * @throws \think\Exception
  464. */
  465. public function goods($ids)
  466. {
  467. $this->view->assign('ids',$ids);
  468. return $this->view->fetch();
  469. }
  470. /**
  471. * 批次发货提交
  472. * @return void
  473. * @throws \think\db\exception\DataNotFoundException
  474. * @throws \think\db\exception\ModelNotFoundException
  475. * @throws \think\exception\DbException
  476. */
  477. public function apply_add()
  478. {
  479. $goods = new QcodeGoods();
  480. $export = new QcodeExport();
  481. $userinfo = Session::get('admin');
  482. if ($this->request->isAjax() === false){
  483. $this->error('请求错误');
  484. }
  485. $ids = input('ids');
  486. $order_number = input('order_number');
  487. $deliveryman = input('deliveryman');
  488. $shr_phone = input('shr_phone');
  489. $plate_number = input('plate_number');
  490. $note = input('note');
  491. $id_list = [];
  492. if (strpos($ids,',') === false){
  493. $id_list[0] = $ids;
  494. }else{
  495. $id_list = explode(',',$ids);
  496. }
  497. $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
  498. if ($update_status){
  499. $export_list = $export->whereIn('_id',$id_list)->select();
  500. $daterq = date('ymd',time());
  501. $data = [
  502. 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),
  503. 'order_number' => $order_number,
  504. 'deliveryman' => $deliveryman,
  505. 'shr_phone' => $shr_phone,
  506. 'plate_number' => $plate_number,
  507. 'shrq_date' => '',
  508. 'supplier_name' => $export_list[0]['username'],
  509. 'supplier_id' => $userinfo['id'],
  510. 'user_id' => $userinfo['id'],
  511. 'status' => 0,
  512. 'export_id' => $ids,
  513. 'create_time' => time(),
  514. 'note' => $note,
  515. 'company_id' => $userinfo['company'],
  516. ];
  517. $data['qrcode_add'] = $this->Qrcode($data['shdh']);
  518. $res = $goods->save($data);
  519. if ($res === false){
  520. $this->error('失败');
  521. }
  522. $this->success('成功');
  523. }else{
  524. $this->error('失败');
  525. }
  526. }
  527. /**
  528. * 生成二维码
  529. * @param $url
  530. * @return string
  531. */
  532. public function Qrcode($url)
  533. {
  534. $level=2;
  535. $size=5;
  536. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  537. $errorCorrectionLevel =intval($level) ;//容错级别
  538. $matrixPointSize = intval($size);//生成图片大小
  539. //生成二维码图片
  540. $object = new \QRcode();
  541. //打开缓冲区
  542. ob_start();
  543. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  544. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  545. $imageString = base64_encode(ob_get_contents());
  546. //关闭缓冲区
  547. ob_end_clean();
  548. //把生成的base64字符串返回给前端
  549. return 'data:image/png;base64,'.$imageString;
  550. }
  551. /**
  552. * 发货单
  553. * @return string|\think\response\Json
  554. * @throws \think\Exception
  555. */
  556. public function dispatch()
  557. {
  558. $export = new QcodeGoods();
  559. $this->request->filter(['strip_tags', 'trim']);
  560. if (false === $this->request->isAjax()) {
  561. return $this->view->fetch();
  562. }
  563. if ($this->request->request('keyField')) {
  564. return $this->selectpage();
  565. }
  566. // 接收参数
  567. $userInfo = Session::get('admin');
  568. $where = [
  569. 'company_id' => $userInfo['company'],
  570. 'delete_time'=> ''
  571. ];
  572. $search = input('search');
  573. if (isset($search)){
  574. $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
  575. }
  576. $filter = input('filter');
  577. $filter = json_decode($filter,true);
  578. //订单编号查询
  579. if (isset($filter['order_number'])){
  580. $where['order_number'] =$filter['order_number'];
  581. }
  582. //所属公司查询
  583. if (isset($filter['supplier_name'])){
  584. $where['supplier_name'] = $filter['supplier_name'];
  585. }
  586. if (isset($filter['status'])){
  587. $where['status'] = (int)$filter['status'];
  588. }
  589. if (isset($filter['create_time'])){
  590. $begin = substr($filter['create_time'],0,19);
  591. $end = substr($filter['create_time'],22);
  592. $begin = strtotime($begin);
  593. $end = strtotime($end);
  594. $where['create_time'] = ['between',[$begin,$end]];
  595. }
  596. $order = input('order');
  597. $offset = input('offset');
  598. $limit = input('limit');
  599. $total = $export->where($where)->count();
  600. $export_list = $export
  601. ->order('_id',$order)
  602. ->where($where)
  603. ->whereIn('status',[0,1])
  604. ->skip($offset)
  605. ->limit($limit)
  606. ->select();
  607. $list=[];
  608. foreach ($export_list as $k=>$v) {
  609. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  610. $list[$k]['shdh'] = $v['shdh'];
  611. $list[$k]['order_number'] = $v['order_number'];
  612. $list[$k]['supplier_name'] = $v['supplier_name'];
  613. $list[$k]['deliveryman'] = $v['deliveryman'];
  614. $list[$k]['plate_number'] = $v['plate_number'];
  615. $list[$k]['create_time'] = $v['create_time'];
  616. $list[$k]['status'] = $v['status'];
  617. }
  618. $result = ['total'=>$total,'rows'=>$list];
  619. return json($result);
  620. }
  621. /**
  622. * 发货单打印
  623. * @return \think\response\Json|void
  624. * @throws \think\db\exception\DataNotFoundException
  625. * @throws \think\db\exception\ModelNotFoundException
  626. * @throws \think\exception\DbException
  627. */
  628. public function printqrcode()
  629. {
  630. $goods = new QcodeGoods();
  631. $export = new QcodeExport();
  632. $large = new QcodeLarge();
  633. $userinfo = Session::get('admin');
  634. if ($this->request->isAjax() === false) {
  635. $this->error('请求错误');
  636. }
  637. $id = input('id');
  638. $good_list = $goods->where('_id',$id)->find();
  639. if ($good_list){
  640. if (strpos($good_list['export_id'],',') === false){
  641. $export_id[0] = $good_list['export_id'];
  642. }else{
  643. $export_id = explode(',',$good_list['export_id']);
  644. }
  645. $data = [];
  646. foreach ($export_id as $k=>$v){
  647. $data[$k] = $export->where('_id',$v)->find();
  648. $large_str = explode(',',$data[$k]['large_str']);
  649. $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
  650. $large_num = $large_weight = 0;
  651. foreach ($large_list as $value){
  652. $large_num = $large_num + $value['l_num'];
  653. $large_weight = $large_weight + $value['l_weight'];
  654. }
  655. $data[$k]['l_num'] = $large_num/10000;
  656. $data[$k]['l_weight'] = $large_weight;
  657. }
  658. $good_list['address'] = $userinfo['company_address'];
  659. $good_list['data'] = $data;
  660. $good_list['count'] = count($data);
  661. $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
  662. if (empty($good_list['qrcode_add'])){
  663. $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  664. $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
  665. }
  666. if ($good_list['note'] == 'NULL'){
  667. $good_list['note'] = '';
  668. }
  669. return json(['code'=>1,'data'=>$good_list]);
  670. }
  671. }
  672. /**
  673. * 获取辅料名称
  674. * @return \think\response\Json
  675. * @throws \think\db\exception\DataNotFoundException
  676. * @throws \think\db\exception\ModelNotFoundException
  677. * @throws \think\exception\DbException
  678. */
  679. public function matterName()
  680. {
  681. $userinfo = Session::get('admin');
  682. $lager = new QcodeLarge();
  683. $bach = new QcodeBach();
  684. $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
  685. $matter_name = [];
  686. if (!empty($bach_id)){
  687. foreach ($bach_id as $v){
  688. $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
  689. $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
  690. }
  691. }
  692. $matter_name = array_unique($matter_name);
  693. return json($matter_name);
  694. }
  695. /**
  696. * 发货单删除
  697. * @param $ids
  698. * @return void
  699. */
  700. public function dispatch_del($ids)
  701. {
  702. $goods = new QcodeGoods();
  703. $export = new QcodeExport();
  704. if (empty($ids)){
  705. $this->error('参数错误');
  706. }
  707. $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  708. if ($res === false){
  709. $this->error('删除失败');
  710. }
  711. $export_list = $goods->where('_id',$ids)->find();
  712. $export_id = explode(',',$export_list['export_id']);
  713. foreach ($export_id as $v){
  714. $result = $export->where('_id',$v)->update(['status'=>0]);
  715. if ($result === false){
  716. $this->error('删除失败');
  717. }
  718. }
  719. $this->success('成功');
  720. }
  721. /**
  722. * 收货列表
  723. */
  724. public function receive()
  725. {
  726. //当前是否为关联查询
  727. $this->relationSearch = false;
  728. //设置过滤方法
  729. $this->request->filter(['strip_tags', 'trim']);
  730. if ($this->request->isAjax()) {
  731. //如果发送的来源是Selectpage,则转发到Selectpage
  732. if ($this->request->request('keyField')) {
  733. return $this->selectpage();
  734. }
  735. $where = [
  736. 'delete_time'=> ''
  737. ];
  738. $req = input();
  739. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  740. $order = $req['order'];
  741. $offset = $req['offset'];
  742. $limit = $req['limit'];
  743. // 构造模糊查询条件
  744. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  745. // $filter = ['field' => $regex];
  746. $filter = json_decode($req['filter'], true);
  747. if (isset($filter['status'])){
  748. $where['status'] = intval($filter['status']);
  749. unset($filter['status']);
  750. }
  751. if (isset($filter['create_time'])){
  752. $begin = substr($filter['create_time'],0,19);
  753. $end = substr($filter['create_time'],22);
  754. $begin = strtotime($begin);
  755. $end = strtotime($end);
  756. $where['create_time'] = ['between',[$begin,$end]];
  757. unset($filter['create_time']);
  758. }
  759. foreach ($filter as $k => $v){
  760. $where[$k] = new \MongoDB\BSON\Regex($v);
  761. }
  762. $goods = new QcodeGoods();
  763. $total = $goods->where($where)->count();
  764. $list = $goods->where($where)
  765. ->order($sort,$order)
  766. ->limit($limit)
  767. ->skip($offset)
  768. ->select();
  769. foreach ($list as $k=>$v) {
  770. $oid = $v['_id']->jsonSerialize();
  771. $list[$k]['id'] = $oid['$oid'];
  772. }
  773. $result = array("total" => $total, "rows" => $list);
  774. return json($result);
  775. }
  776. return $this->view->fetch();
  777. }
  778. /**
  779. * 扫码/输入收货
  780. */
  781. public function receive_add()
  782. {
  783. $req = $this->request->param();
  784. if(!isset($req['shdh']) || empty($req['shdh'])){
  785. $this->error('请输入送货单号');
  786. }
  787. $data = [
  788. 'shdh'=>$req['shdh'],
  789. 'delete_time'=>'',
  790. ];
  791. $goods = new QcodeGoods();
  792. $row = $goods->where($data)->find();
  793. if (!$row) $this->error('未查询到该单号');
  794. if ($row['status']==2) $this->error('该单号已收货');
  795. //收货---修改status为2
  796. $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
  797. if ($bool){
  798. $this->success('成功');
  799. }else{
  800. $this->error('失败');
  801. }
  802. }
  803. /**
  804. * 取消收货
  805. */
  806. public function receive_del()
  807. {
  808. $req = $this->request->param();
  809. if(!isset($req['ids']) || empty($req['ids'])){
  810. $this->error('id不能为空');
  811. }
  812. $data = [
  813. '_id'=>$req['ids'],
  814. 'delete_time'=>'',
  815. ];
  816. $goods = new QcodeGoods();
  817. //收货---修改status为2
  818. $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
  819. if ($bool){
  820. $this->success('成功');
  821. }else{
  822. $this->error('失败');
  823. }
  824. }
  825. }