Deliver.php 31 KB

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