Deliver.php 32 KB

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