Deliver.php 32 KB

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