Deliver.php 31 KB

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