Deliver.php 31 KB

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