Deliver.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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\Config;
  18. use think\Db;
  19. use think\Session;
  20. class Deliver extends Backend
  21. {
  22. /**
  23. * 首页
  24. */
  25. public function index()
  26. {
  27. return $this->view->fetch();
  28. }
  29. /**
  30. * 大件发货
  31. */
  32. public function lager()
  33. {
  34. $company = new QcodeCompany();
  35. $product = new QcodeProduct();
  36. $large = new QcodeLarge();
  37. $bach = new QcodeBach();
  38. $small = new QcodeSmall();
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. $userInfo = Session::get('admin');
  47. $where = ['delete_time' => ''];
  48. $filter = input('filter');
  49. $filter = json_decode($filter, true);
  50. $whereList = ['delete_time' => ''];
  51. if (isset($filter['matter_name'])) {
  52. $whereList['matter_no'] = $filter['matter_name'];
  53. }
  54. if (isset($filter['bach'])) {
  55. $whereList['bach_num'] = $filter['bach'];
  56. }
  57. if (isset($filter['manufacture_date'])) {
  58. $begin = substr($filter['manufacture_date'], 0, 19);
  59. $end = substr($filter['manufacture_date'], 22);
  60. $begin = (int)date('ymd', strtotime($begin));
  61. $end = (int)date('ymd', strtotime($end));
  62. $whereList['manufacture_date'] = ['between', [$begin, $end]];
  63. }
  64. // 查出相关 bach_id
  65. $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
  66. $bach_id = [];
  67. foreach ($bach_list as $v) {
  68. $id = substr(json_encode($v), 9, -2);
  69. $bach_id[] = $id;
  70. }
  71. // 获取分页参数
  72. $offset = input('offset', 0);
  73. $limit = input('limit', 10);
  74. // 获取所有大件记录用于总量判断
  75. $all_large_list = $large->name($userInfo['company'].'_qcode_large')
  76. ->where($where)
  77. ->where('l_status', 0)
  78. ->whereIn('bach_id', $bach_id)
  79. ->select();
  80. // 统计总数量
  81. $total = count($all_large_list);
  82. // 分页
  83. $large_list = array_slice($all_large_list, $offset, $limit);
  84. $list = [];
  85. foreach ($large_list as $k => $v) {
  86. $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
  87. // $num = (int)$bach_detail['num']; // 总张数
  88. // $small_num = (int)$bach_detail['small_num']; // 每箱张数
  89. // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
  90. // // 重新计算总箱数
  91. // $larger_num = $num / $small_num;
  92. // $full_pallets = floor($larger_num / $boxes_per_pallet);
  93. // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
  94. $num = (float)$bach_detail['num'];
  95. $small_num = (float)$bach_detail['small_num'];
  96. $boxes_per_pallet = (float)$bach_detail['total_boxes'];
  97. $larger_num = $num / $small_num;
  98. $full_pallets = floor($larger_num / $boxes_per_pallet);
  99. $last_pallet_boxes = fmod($larger_num, $boxes_per_pallet); // 真正浮点取余
  100. $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
  101. // 当前是第几托(code中解析)
  102. $current_flow_str = substr($v['code'], 53, 6);
  103. $current_flow = (int) ltrim($current_flow_str, '0');
  104. $total_pallets = (int) $total_pallets;
  105. // 是否是最后一托
  106. $is_last = ($current_flow === $total_pallets) ? 1 : 0;
  107. // 修正箱数
  108. $total_boxes = $boxes_per_pallet;
  109. if ($is_last && $last_pallet_boxes > 0) {
  110. // 精准保留1位小数,不做四舍五入
  111. $total_boxes = number_format($last_pallet_boxes, 1, '.', '');
  112. // $total_boxes = $last_pallet_boxes;
  113. }
  114. $list[$k] = [
  115. 'id' => substr(json_encode($v['_id']), 9, -2),
  116. 'l_flow' => $current_flow,
  117. 'bach' => $bach_detail['bach_num'],
  118. 'num' => $num,
  119. 'small_num' => $small_num,
  120. 'order_ddbh' => $bach_detail['order_ddbh'],
  121. 'danwei' => $bach_detail['danwei'],
  122. 'cpbm' => $bach_detail['cpbm'],
  123. 'matter_name' => $bach_detail['matter_name'],
  124. 'total_boxes' => $total_boxes,
  125. 'tray_num' => $bach_detail['tray_num'],
  126. 'box_num' => $bach_detail['box_num'],
  127. 'pallet_height' => $bach_detail['pallet_height'],
  128. 'pallet_length' => $bach_detail['pallet_length'],
  129. 'pallet_width' => $bach_detail['pallet_width'],
  130. 'larger_num' => $larger_num,
  131. 'manufacture_date' => date('Y-m-d', strtotime('20' . $bach_detail['manufacture_date'])),
  132. 'code' => $v['code'],
  133. 'is_last' => $is_last,
  134. ];
  135. }
  136. return json(['total' => $total, 'rows' => $list]);
  137. }
  138. /**
  139. * 小件列表
  140. * @return \think\response\Json
  141. * @throws \think\Exception
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. * @throws \think\exception\DbException
  145. */
  146. public function smallList()
  147. {
  148. $userinfo = Session::get('admin');
  149. $lager = new QcodeLarge();
  150. $small = new QcodeSmall();
  151. if ($this->request->isAjax() === false){
  152. $this->error('请求错误');
  153. }
  154. $filter = input('filter');
  155. $filter = json_decode($filter,true);
  156. $order = input('order');
  157. $offset = input('offset');
  158. $limit = input('limit');
  159. $where = [
  160. 'delete_time' => '',
  161. ];
  162. if (isset($filter['large_id'])){
  163. $where['large_id'] =$filter['large_id'];
  164. }
  165. $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
  166. ->where($where)
  167. ->skip($offset)
  168. ->limit($limit)
  169. ->select();
  170. $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
  171. $list = [];
  172. foreach ($smallList as $k=>$v){
  173. $list[$k] = [
  174. 'code' => $v['code'],
  175. 'l_flow' => $v['l_flow'],
  176. 'print_num' => $v['p_nums'],
  177. 'status' => $v['status'],
  178. ];
  179. }
  180. return json(['total'=>$total,'rows'=>$list]);
  181. }
  182. /**
  183. * 导出发货
  184. * @return void
  185. * @throws \think\Exception
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. * @throws \think\exception\DbException
  189. */
  190. public function print()
  191. {
  192. $userinfo = Session::get('admin');
  193. $lager = new QcodeLarge();
  194. $bach = new QcodeBach();
  195. $small = new QcodeSmall();
  196. $export = new QcodeExport();
  197. if ($this->request->isAjax() === false){
  198. $this->error('请求错误');
  199. }
  200. $lager_id = input('lager_id');
  201. if ($lager_id === ''){
  202. $this->error('请至少选择一个大件');
  203. }
  204. $lagerId = [];
  205. if (strpos($lager_id,',') === false){
  206. $lagerId[0] = $lager_id;
  207. }else{
  208. $lagerId = explode(',',$lager_id);
  209. }
  210. $lager_num = count($lagerId);
  211. $bach_id = [];
  212. foreach ($lagerId as $k=>$v){
  213. $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
  214. $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
  215. $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
  216. }
  217. $bach_id = array_unique($bach_id);
  218. $data = [];
  219. foreach ($bach_id as $k=>$v){
  220. $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
  221. }
  222. $data = array_unique($data);
  223. if (count($data)>1){
  224. $this->error('请选择同一产品');
  225. }
  226. $n =0;
  227. foreach (explode(',',$lager_id) as $kk=>$vv){
  228. $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
  229. }
  230. // $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
  231. $bachArray = $bach->name($userinfo['company'].'_'.'qcode_bach')
  232. ->where('_id', $bach_id[0])
  233. ->find();
  234. $bachDetail = $bachArray ? $bachArray->toArray() : [];
  235. if ($bachDetail['box_num'] === 1){
  236. $mater_type = 2;
  237. }else{
  238. $mater_type = 1;
  239. }
  240. $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
  241. $file_dir = $this->exportExcel($exportData);
  242. $row = [
  243. 'username' => $bachDetail['supplier_name'],//公司
  244. 'matter_no' => $bachDetail['matter_no'],
  245. 'order_ddbh' => $bachDetail['order_ddbh'],
  246. 'large_str' => $lager_id,
  247. 'status' => 0,
  248. 'bach_num' => $bachDetail['bach_num'],
  249. 'note' => '',
  250. 'user_id' => $userinfo['id'],
  251. 'create_time' => time(),
  252. 'file_dir' => $file_dir,
  253. 'company_id' => $userinfo['company'],
  254. 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
  255. 'small_num' => $n,//共包含了 N 个小件
  256. 'matter_name' => $bachDetail['matter_name'],//产品名称
  257. 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
  258. 'tray_num' => $bachDetail['tray_num'],//每层箱数
  259. 'box_num' => $bachDetail['box_num'],//每托层数
  260. 'pallet_height' => $bachDetail['pallet_height'],//每托高度
  261. 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
  262. 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
  263. 'larger_num' => $bachDetail['larger_num'],//总箱数
  264. ];
  265. if ($bachDetail['box_num'] != 1){
  266. $row['num'] = $bachDetail['box_num'];
  267. $row['mater_type'] = 1;
  268. }else{
  269. $row['num'] = $bachDetail['s_weight'];
  270. $row['mater_type'] = 2;
  271. }
  272. $res = $export->save($row);
  273. if ($res === false){
  274. $this->error('发货失败');
  275. }
  276. $lager = new QcodeLarge();
  277. $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
  278. $this->success('成功','',$file_dir);
  279. }
  280. /**
  281. * 表格数据处理
  282. * @param $bach_id
  283. * @param $large_str
  284. * @param $company
  285. * @param $large_num
  286. * @param $small_num
  287. * @return array
  288. * @throws \think\db\exception\DataNotFoundException
  289. * @throws \think\db\exception\ModelNotFoundException
  290. * @throws \think\exception\DbException
  291. */
  292. public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
  293. {
  294. $bach = new QcodeBach();
  295. $large = new QcodeLarge();
  296. $small = new QcodeSmall();
  297. $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
  298. $data = [
  299. 'supplier_name' => $bachList['supplier_name'],
  300. 'supplier_no' => $bachList['supplier_code'],
  301. 'matter_name' => $bachList['matter_name'],
  302. 'matter_no' => $bachList['matter_no'],
  303. 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
  304. 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
  305. 'box_num' => $large_num,
  306. 'tray_num' => $bachList['tray_num'],
  307. 'small_num' => $small_num,
  308. 'l_reservation' => $bachList['l_reservation'],
  309. 's_reservation' => $bachList['s_reservation'],
  310. 'l_flow' => $bachList['l_flow'],
  311. 's_flow' => $bachList['s_flow'],
  312. 'l_weight' => 0,
  313. 's_weight' => $bachList['s_weight']/1000,
  314. 'code' => [],
  315. ];
  316. $large_id = explode(',', $large_str);
  317. foreach ($large_id as $k => $v) {
  318. $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
  319. $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
  320. $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
  321. foreach ($small_list as $value) {
  322. $code = [
  323. 'large_code' => $largeList['code'],
  324. 'small_code' => $value['code'],
  325. ];
  326. array_push($data['code'],$code);
  327. }
  328. }
  329. return $data;
  330. }
  331. /**
  332. * 生成excel
  333. * @param $data
  334. * @return string
  335. * @throws Exception
  336. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  337. */
  338. public function exportExcel($data)
  339. {
  340. $objexcel = new Spreadsheet();
  341. $sheet = $objexcel->getActiveSheet();
  342. //设置全局单元格垂直居中
  343. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  344. //设置全局单元格水平居中
  345. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  346. //设置全局单元格自动换行
  347. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  348. //设置单元格宽度
  349. $sheet->getDefaultColumnDimension()->setWidth(50);
  350. //设置表头
  351. $sheet->setCellValue('A1', '文件流水号');
  352. $sheet->setCellValue('A2', '订单号');
  353. $sheet->setCellValue('A3', '供应商');
  354. $sheet->setCellValue('A4', '供应商代码');
  355. $sheet->setCellValue('A5', '物料名称');
  356. $sheet->setCellValue('A6', '物料代码');
  357. $sheet->setCellValue('A7', '生产日期');
  358. $sheet->setCellValue('A8', '打码日期');
  359. $sheet->setCellValue('A9', '大件数量');
  360. $sheet->setCellValue('A10', '托盘小件数量');
  361. $sheet->setCellValue('A11', '小件总数量');
  362. $sheet->setCellValue('C3', '大件预留码');
  363. $sheet->setCellValue('C5', '小件预留码');
  364. $sheet->setCellValue('C7', '大件开始流水号');
  365. $sheet->setCellValue('C9', '小件开始流水号');
  366. $sheet->setCellValue('C11', '大件重量(Kg)');
  367. $sheet->setCellValue('C13', '小件重量(Kg)');
  368. $sheet->setCellValue('A15', '大件二维码');
  369. $sheet->setCellValue('B15', '大件条码(预留)');
  370. $sheet->setCellValue('C15', '小件二维码');
  371. $sheet->setCellValue('D15', '小件条码(预留)');
  372. //插入表格数据
  373. $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
  374. $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
  375. $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
  376. $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
  377. $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
  378. $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
  379. $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
  380. $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
  381. $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
  382. $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
  383. $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
  384. $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
  385. $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
  386. $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
  387. $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
  388. foreach ($data['code'] as $k=>$v){
  389. $m = $k+16;
  390. $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
  391. $sheet->getCell('B'.$m)->setValue('');
  392. $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
  393. $sheet->getCell('D'.$m)->setValue('');
  394. }
  395. //文件另存
  396. $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
  397. // $filename = iconv("utf-8","gb2312",$filename);
  398. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  399. header('Content-Disposition:attachment;filename='.$filename);
  400. header('Cache-Control: max-age=0');
  401. header("Content-Type:text/html;charset=utf-8");
  402. if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
  403. {
  404. mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
  405. }
  406. $writer = IOFactory::createWriter($objexcel, 'Xlsx');
  407. $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
  408. return 'uploads/export/'.$filename;
  409. }
  410. //获取日期
  411. public function applydsit_list()
  412. {
  413. $userinfo = Session::get('admin');
  414. $mongo = \think\Db::connect('mongodb');
  415. try {
  416. // 查询数据并按日期排序
  417. $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
  418. ->order('sys_rq', 'desc')
  419. ->select();
  420. // 初始化结果数组
  421. $result = [
  422. 'code' => 0,
  423. 'msg' => '成功',
  424. 'time' => time(),
  425. 'data' => []
  426. ];
  427. // 处理数据
  428. foreach ($bach_list as $item) {
  429. if (isset($item['sys_rq'])) {
  430. $date = $item['sys_rq'];
  431. list($year, $month, $day) = explode('-', $date);
  432. // 构建日期格式
  433. $yearMonth = "$year-$month";
  434. $dayFormat = "$month-$day";
  435. // 按年-月-日分组
  436. if (!isset($result['data'][$year])) {
  437. $result['data'][$year] = [];
  438. }
  439. if (!isset($result['data'][$year][$yearMonth])) {
  440. $result['data'][$year][$yearMonth] = [];
  441. }
  442. // 添加日期,避免重复
  443. if (!in_array($dayFormat, $result['data'][$year][$yearMonth])) {
  444. $result['data'][$year][$yearMonth][] = $dayFormat;
  445. }
  446. }
  447. }
  448. // 返回JSON格式数据
  449. return json($result);
  450. } catch (\Exception $e) {
  451. // 错误处理
  452. return json([
  453. 'code' => 1,
  454. 'msg' => '查询失败: ' . $e->getMessage(),
  455. 'time' => time(),
  456. 'data' => []
  457. ]);
  458. }
  459. }
  460. //获取发货单日期
  461. public function dispatch_list()
  462. {
  463. $userinfo = Session::get('admin');
  464. $mongo = \think\Db::connect('mongodb');
  465. try {
  466. // 查询数据并按日期排序
  467. $bach_list = $mongo->name("qcode_goods")
  468. ->order('sys_rq', 'desc')
  469. ->select();
  470. // 初始化结果数组
  471. $result = [
  472. 'code' => 0,
  473. 'msg' => '成功',
  474. 'time' => time(),
  475. 'data' => []
  476. ];
  477. // 处理数据
  478. foreach ($bach_list as $item) {
  479. if (isset($item['sys_rq'])) {
  480. $date = $item['sys_rq'];
  481. list($year, $month, $day) = explode('-', $date);
  482. // 构建日期格式
  483. $yearMonth = "$year-$month";
  484. $dayFormat = "$month-$day";
  485. // 按年-月-日分组
  486. if (!isset($result['data'][$year])) {
  487. $result['data'][$year] = [];
  488. }
  489. if (!isset($result['data'][$year][$yearMonth])) {
  490. $result['data'][$year][$yearMonth] = [];
  491. }
  492. // 添加日期,避免重复
  493. if (!in_array($dayFormat, $result['data'][$year][$yearMonth])) {
  494. $result['data'][$year][$yearMonth][] = $dayFormat;
  495. }
  496. }
  497. }
  498. // 返回JSON格式数据
  499. return json($result);
  500. } catch (\Exception $e) {
  501. // 错误处理
  502. return json([
  503. 'code' => 1,
  504. 'msg' => '查询失败: ' . $e->getMessage(),
  505. 'time' => time(),
  506. 'data' => []
  507. ]);
  508. }
  509. }
  510. // 获取主数据
  511. public function apply()
  512. {
  513. $mongo = Db::connect('mongodb');
  514. $this->request->filter(['strip_tags', 'trim']);
  515. if (false === $this->request->isAjax()) {
  516. return $this->view->fetch();
  517. }
  518. if ($this->request->request('keyField')) {
  519. return $this->selectpage();
  520. }
  521. // 保存编辑请求
  522. if ($this->request->isPost() && $this->request->post('data')) {
  523. return $this->saveEdits();
  524. }
  525. $userinfo = Session::get('admin');
  526. $where = ['delete_time' => ''];
  527. // 处理日期筛选条件
  528. $date = $this->request->get('date', '');
  529. $month = $this->request->get('month', '');
  530. $year = $this->request->get('year', '');
  531. if ($date && $date !== 'all') {
  532. $where['sys_rq'] = $date;
  533. } elseif ($month) {
  534. $where['sys_rq'] = new \MongoDB\BSON\Regex($month, 'i');
  535. } elseif ($year) {
  536. $where['sys_rq'] = new \MongoDB\BSON\Regex($year, 'i');
  537. } else {
  538. // ✅ 默认查询近两天
  539. $today = date('Y-m-d');
  540. $yesterday = date('Y-m-d', strtotime('-1 day'));
  541. $where['sys_rq'] = ['in', [$today, $yesterday]];
  542. }
  543. // 时间范围查询(高级过滤)
  544. $filter = input('filter');
  545. $filter = json_decode($filter, true);
  546. if (isset($filter['create_time'])) {
  547. $begin = substr($filter['create_time'], 0, 19);
  548. $end = substr($filter['create_time'], 22);
  549. $begin = strtotime($begin);
  550. $end = strtotime($end);
  551. $where['create_time'] = ['between', [$begin, $end]];
  552. }
  553. $order = input('order') ?: 'desc';
  554. $offset = (int)input('offset', 0);
  555. $limit = (int)input('limit', 20);
  556. // 总数统计
  557. $total = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
  558. ->where($where)
  559. ->count([]);
  560. // 列表数据
  561. $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
  562. ->where($where)
  563. ->order('_id', $order)
  564. ->skip($offset)
  565. ->limit($limit)
  566. ->select();
  567. $list = [];
  568. foreach ($bach_list as $k => $v) {
  569. $list[$k]['bach_ids'] = $v['bach_ids'];
  570. $list[$k]['total_quantity'] = $v['total_quantity'];
  571. $list[$k]['total_boxes'] = $v['total_boxes'];
  572. $list[$k]['total_pallets'] = $v['total_pallets'];
  573. $list[$k]['product_count'] = $v['product_count'];
  574. $list[$k]['sys_rq'] = $v['sys_rq'];
  575. $list[$k]['created_at'] = $v['created_at'];
  576. $list[$k]['id'] = (string)$v['_id'];
  577. }
  578. return json(['total' => $total, 'rows' => $list]);
  579. }
  580. /**
  581. * 明细数据 (通过 bach_ids 获取)
  582. */
  583. public function bachids()
  584. {
  585. $mongo = \think\Db::connect('mongodb');
  586. $this->request->filter(['strip_tags', 'trim']);
  587. if (false === $this->request->isAjax()) {
  588. return $this->view->fetch();
  589. }
  590. $userinfo = Session::get('admin');
  591. $bach_ids = input('bach_ids');
  592. $where = [
  593. 'bach_ids' => $bach_ids,
  594. 'delete_time' => ''
  595. ];
  596. $filter = input('filter');
  597. $filter = json_decode($filter, true);
  598. if (isset($filter['matter_name'])) {
  599. $where['matter_no'] = $filter['matter_name'];
  600. }
  601. if (isset($filter['create_time'])) {
  602. $begin = substr($filter['create_time'], 0, 19);
  603. $end = substr($filter['create_time'], 22);
  604. $begin = strtotime($begin);
  605. $end = strtotime($end);
  606. $where['create_time'] = ['between', [$begin, $end]];
  607. }
  608. $order = input('order') ?: 'desc';
  609. $offset = (int)input('offset', 0);
  610. $limit = (int)input('limit', 20);
  611. $total = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
  612. ->where($where)
  613. ->count([]);
  614. // 查询数据
  615. $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
  616. ->where($where)
  617. ->select(); // 去掉 order、skip、limit,让我们用 PHP 自定义排序
  618. // 自定义排序规则:按 pallet_range 起始数字升序排列
  619. usort($bach_list, function ($a, $b) {
  620. // 提取起始数字
  621. $getStart = function ($val) {
  622. if (strpos($val, '-') !== false) {
  623. return (int)explode('-', $val)[0];
  624. }
  625. return (int)$val;
  626. };
  627. return $getStart($a['pallet_range']) <=> $getStart($b['pallet_range']);
  628. });
  629. // 手动分页(如有分页需求)
  630. $total = count($bach_list);
  631. $offset = (int)input('offset', 0);
  632. $limit = (int)input('limit', 20);
  633. $pagedList = array_slice($bach_list, $offset, $limit);
  634. // 格式化返回数据
  635. $list = [];
  636. foreach ($pagedList as $k => $v) {
  637. $list[$k]['start_pallet_no'] = $v['start_pallet_no'];
  638. $list[$k]['end_pallet_no'] = $v['end_pallet_no'];
  639. $list[$k]['pallet_range'] = $v['pallet_range'];
  640. $list[$k]['total_boxes'] = $v['total_boxes'];
  641. $list[$k]['pallet_count'] = $v['pallet_count'];
  642. $list[$k]['actual_quantity'] = $v['actual_quantity'];
  643. $list[$k]['gdbh'] = $v['gdbh'];
  644. $list[$k]['matter_no'] = $v['gdbh'];
  645. $list[$k]['order_ddbh'] = $v['order_ddbh'];
  646. $list[$k]['cpbm'] = $v['cpbm'];
  647. $list[$k]['matter_name'] = $v['matter_name'];
  648. $list[$k]['remark'] = $v['remark'];
  649. $list[$k]['small_num'] = $v['small_num'];
  650. $list[$k]['tray_num'] = $v['tray_num'];
  651. $list[$k]['box_num'] = $v['box_num'];
  652. $list[$k]['tray_count'] = $v['tray_count'];
  653. $list[$k]['layer_height'] = $v['layer_height'];
  654. $list[$k]['pallet_length'] = $v['pallet_length'];
  655. $list[$k]['pallet_width'] = $v['pallet_width'];
  656. $list[$k]['sys_rq'] = $v['sys_rq'];
  657. $list[$k]['id'] = (string)$v['_id'];
  658. }
  659. return json(['total' => $total, 'rows' => $list]);
  660. }
  661. public function applydsit_add()
  662. {
  663. if ($this->request->isPost()) {
  664. // 获取前端提交的数据
  665. $params = $this->request->post();
  666. $bach_ids = $params['bach_ids'] ?? '';
  667. $pallet_nos = $params['pallet_no'] ?? [];
  668. $product_names = $params['product_name'] ?? [];
  669. $remarks = $params['remark'] ?? [];
  670. $quantities = $params['quantity'] ?? [];
  671. $unit = $params['unit'] ?? [];
  672. if (!$bach_ids) {
  673. return json(['code' => 0, 'msg' => '缺少单据编号']);
  674. }
  675. // 获取当前登录用户的公司名
  676. $userinfo = Session::get('admin');
  677. if (!$userinfo || empty($userinfo['company'])) {
  678. return json(['code' => 0, 'msg' => '未获取到用户公司信息']);
  679. }
  680. // 连接 MongoDB
  681. try {
  682. $mongo = Db::connect('mongodb');
  683. } catch (\Exception $e) {
  684. return json(['code' => 0, 'msg' => 'MongoDB 连接失败: ' . $e->getMessage()]);
  685. }
  686. // 查询汇总表
  687. $summaryTable = $userinfo['company'] . '_qcode_bachsummary';
  688. $summaryData = $mongo->name($summaryTable)->where(['bach_ids' => $bach_ids])->find();
  689. if (!$summaryData) {
  690. return json(['code' => 0, 'msg' => '未找到对应的汇总单据']);
  691. }
  692. // 写入明细表
  693. $detailTable = $userinfo['company'] . '_qcode_bach';
  694. $insertedCount = 0;
  695. foreach ($pallet_nos as $i => $palletNo) {
  696. if (empty($palletNo)) continue;
  697. $data = [
  698. 'bach_ids' => $bach_ids,//关联
  699. 'userid' => $userinfo['id'],//用户ID
  700. 'pallet_range' => $palletNo,//托盘号
  701. 'cpmc' => $product_names[$i],//产品名称
  702. 'matter_name' => $product_names[$i],//产品名称
  703. 'remark' => $remarks[$i],//备注
  704. 'total_boxes' => '',//总箱数
  705. 'actual_quantity' => (int)($quantities[$i] ?? 0),//数量
  706. 'pallet_count' => '',//托盘数
  707. 'sys_rq' => date('Y-m-d'),
  708. 'created_at' => date('Y-m-d H:i:s'),
  709. 'create_time' => time(),
  710. 'unit' => $unit[$i],//单位
  711. // 结构字段保留,但设为空
  712. 'gdbh' => '',
  713. 'order_ddbh' => '',
  714. 'cpbm' => '',
  715. 'small_num' => '',
  716. 'tray_num' => '',
  717. 'box_num' => '',
  718. 'tray_count' => '',
  719. 'layer_height' => '',
  720. 'pallet_length' => '',
  721. 'pallet_width' => '',
  722. 'pallet_type' => '',
  723. 'start_pallet_no' => '',
  724. 'end_pallet_no' => '',
  725. 'bach_status' => 0,
  726. 'delete_time' => '',
  727. ];
  728. $mongo->name($detailTable)->insert($data);
  729. $insertedCount++;
  730. }
  731. return json(['code' => 1, 'msg' => "成功插入 {$insertedCount} 条托盘信息"]);
  732. }
  733. return json(['code' => 0, 'msg' => '非法请求方式']);
  734. }
  735. /**
  736. * 保存编辑的数据
  737. */
  738. public function apply_update()
  739. {
  740. $this->request->filter(['strip_tags', 'trim']);
  741. if (!$this->request->isPost()) {
  742. $this->error('请求错误');
  743. }
  744. $userinfo = Session::get('admin');
  745. $tableName = $userinfo['company'] . '_' . "qcode_bach";
  746. $id = $this->request->post('id');
  747. $field = $this->request->post('field');
  748. $value = $this->request->post('value');
  749. // 允许前端编辑的字段
  750. $allowedFields = ['pallet_range', 'start_pallet_no', 'end_pallet_no'];
  751. if (!in_array($field, $allowedFields)) {
  752. $this->error('该字段不允许编辑');
  753. }
  754. $mongo = \think\Db::connect('mongodb');
  755. $bach = $mongo->name($tableName)->where('_id', new \MongoDB\BSON\ObjectId($id))->find();
  756. if (!$bach) {
  757. $this->error('数据未找到');
  758. }
  759. // 更新字段值
  760. $bach[$field] = is_numeric($value) ? (int)$value : $value;
  761. // 整理更新字段(去除 _id)
  762. $updateData = [];
  763. foreach ($bach as $k => $v) {
  764. if ($k != '_id') {
  765. $updateData[$k] = $v;
  766. }
  767. }
  768. $result = $mongo->name($tableName)
  769. ->where('_id', new \MongoDB\BSON\ObjectId($id))
  770. ->update($updateData);
  771. if ($result) {
  772. $this->success('更新成功');
  773. } else {
  774. $this->error('更新失败');
  775. }
  776. }
  777. /**
  778. * 批次发货删除
  779. * @param $ids
  780. * @return void
  781. */
  782. public function apply_del($ids)
  783. {
  784. $export = new QcodeExport();
  785. $userinfo = Session::get('admin');
  786. $large = new QcodeLarge();
  787. if (empty($ids)){
  788. $this->error('请求错误');
  789. }
  790. $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  791. if ($res === false){
  792. $this->error('删除失败');
  793. }
  794. $large_list = $export->where('_id',$ids)->find();
  795. $large_id = explode(',',$large_list['large_str']);
  796. foreach ($large_id as $v){
  797. $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
  798. if ($result === 0){
  799. $this->error('删除失败');
  800. }
  801. }
  802. $this->success('成功');
  803. }
  804. /**
  805. * 司机弹窗
  806. * @param $ids
  807. * @return string
  808. * @throws \think\Exception
  809. */
  810. public function goods($ids)
  811. {
  812. $this->view->assign('ids',$ids);
  813. return $this->view->fetch();
  814. }
  815. /**
  816. * 批次发货提交
  817. * @return void
  818. * @throws \think\db\exception\DataNotFoundException
  819. * @throws \think\db\exception\ModelNotFoundException
  820. * @throws \think\exception\DbException
  821. */
  822. public function apply_add()
  823. {
  824. $goods = new QcodeGoods();
  825. $export = new QcodeExport();
  826. $userinfo = Session::get('admin');
  827. if ($this->request->isAjax() === false){
  828. $this->error('请求错误');
  829. }
  830. $ids = input('ids');
  831. $order_number = input('order_number');
  832. $deliveryman = input('deliveryman');
  833. $shr_phone = input('shr_phone');
  834. $plate_number = input('plate_number');
  835. $note = input('note');
  836. $mongo = \think\Db::connect('mongodb');
  837. $collection = $userinfo['company'] . '_qcode_bach';
  838. $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
  839. $update_status = $mongo->name($bachsummary)->where('_id',$ids)->update(
  840. [
  841. 'bach_status'=>2,
  842. 'delete_time' => date('Y-m-d H:i:s')
  843. ]);
  844. if ($update_status){
  845. $bachsummary_list = $mongo->name($bachsummary)->where('_id',$ids)->select();
  846. $daterq = date('ymd',time());
  847. $data = [
  848. 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),//收货单号
  849. 'order_number' => $order_number,//订单号
  850. 'deliveryman' => $deliveryman,//订单号
  851. 'shr_phone' => $shr_phone,//电话
  852. 'plate_number' => $plate_number,//车牌号
  853. 'shrq_date' => '',
  854. 'supplier_name' => $userinfo['company_name'],//供应商名称
  855. 'supplier_id' => $userinfo['id'],//供应商名称id
  856. 'user_id' => $userinfo['id'],//用户id
  857. 'status' => 0,
  858. 'qcode_bachsummary_id' => $ids,
  859. 'export_id' => '',
  860. 'create_time' => time(),
  861. 'sys_rq' => date('Y-m-d'),
  862. 'note' => $note,
  863. 'company_id' => $userinfo['company'],
  864. ];
  865. $data['qrcode_add'] = $this->Qrcode($data['shdh']);
  866. $res = $goods->save($data);
  867. if ($res === false){
  868. $this->error('失败');
  869. }
  870. $this->success('成功');
  871. }else{
  872. $this->error('失败');
  873. }
  874. }
  875. /**
  876. * 生成二维码
  877. * @param $url
  878. * @return string
  879. */
  880. public function Qrcode($url)
  881. {
  882. $level=2;
  883. $size=5;
  884. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  885. $errorCorrectionLevel =intval($level) ;//容错级别
  886. $matrixPointSize = intval($size);//生成图片大小
  887. //生成二维码图片
  888. $object = new \QRcode();
  889. //打开缓冲区
  890. ob_start();
  891. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  892. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  893. $imageString = base64_encode(ob_get_contents());
  894. //关闭缓冲区
  895. ob_end_clean();
  896. //把生成的base64字符串返回给前端
  897. return 'data:image/png;base64,'.$imageString;
  898. }
  899. /**
  900. * 发货单
  901. * @return string|\think\response\Json
  902. * @throws \think\Exception
  903. */
  904. public function dispatch()
  905. {
  906. $export = new QcodeGoods();
  907. $this->request->filter(['strip_tags', 'trim']);
  908. // 非Ajax请求返回视图
  909. if (false === $this->request->isAjax()) {
  910. return $this->view->fetch();
  911. }
  912. // 处理selectpage请求
  913. if ($this->request->request('keyField')) {
  914. return $this->selectpage();
  915. }
  916. // 获取用户信息
  917. $userInfo = Session::get('admin');
  918. $where = [
  919. 'company_id' => $userInfo['company'],
  920. 'delete_time' => ''
  921. ];
  922. // 处理搜索条件
  923. $search = input('search');
  924. if (isset($search)) {
  925. $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] =
  926. new \MongoDB\BSON\Regex($search);
  927. }
  928. // 处理筛选条件
  929. $filter = input('filter');
  930. $filter = json_decode($filter, true) ?: [];
  931. // 订单编号查询
  932. if (isset($filter['order_number'])) {
  933. $where['order_number'] = $filter['order_number'];
  934. }
  935. // 所属公司查询
  936. if (isset($filter['supplier_name'])) {
  937. $where['supplier_name'] = $filter['supplier_name'];
  938. }
  939. // 状态查询
  940. if (isset($filter['status'])) {
  941. $where['status'] = (int)$filter['status'];
  942. }
  943. // 处理日期筛选条件
  944. $date = $this->request->get('date', '');
  945. $month = $this->request->get('month', '');
  946. $year = $this->request->get('year', '');
  947. if ($date && $date !== 'all') {
  948. // 精确日期查询
  949. $where['sys_rq'] = $date;
  950. } elseif ($month) {
  951. // 月份模糊查询(如 2023-05)
  952. $where['sys_rq'] = new \MongoDB\BSON\Regex($month, 'i');
  953. } elseif ($year) {
  954. // 年份模糊查询(如 2023)
  955. $where['sys_rq'] = new \MongoDB\BSON\Regex($year, 'i');
  956. } else {
  957. // 默认查询近两天数据
  958. $today = date('Y-m-d');
  959. $yesterday = date('Y-m-d', strtotime('-1 day'));
  960. $where['sys_rq'] = ['in', [$today, $yesterday]];
  961. }
  962. // 分页和排序参数
  963. $order = input('order', 'desc'); // 默认降序
  964. $offset = input('offset', 0);
  965. $limit = input('limit', 10); // 默认每页10条
  966. // 查询数据
  967. $total = $export->where($where)->count();
  968. $export_list = $export
  969. ->order('_id', $order)
  970. ->where($where)
  971. ->whereIn('status', [0, 1])
  972. ->skip($offset)
  973. ->limit($limit)
  974. ->select();
  975. // 处理批次汇总数据
  976. $userinfo = Session::get('admin');
  977. $mongo = \think\Db::connect('mongodb');
  978. $bachsummary = $userinfo['company'] . '_qcode_bachsummary'; // 批次汇总表
  979. $list = [];
  980. foreach ($export_list as $k => $v) {
  981. $bachsummary_list = $mongo->name($bachsummary)->where("_id", $v['qcode_bachsummary_id'])->find();
  982. $list[$k] = [
  983. 'bach_ids' => $bachsummary_list['bach_ids'] ?? '',
  984. 'total_quantity' => $bachsummary_list['total_quantity'] ?? 0,
  985. 'total_boxes' => $bachsummary_list['total_boxes'] ?? 0,
  986. 'total_pallets' => $bachsummary_list['total_pallets'] ?? 0,
  987. 'product_count' => $bachsummary_list['product_count'] ?? 0,
  988. 'id' => substr(json_encode($v['_id']), 9, -2),
  989. 'shdh' => $v['shdh'] ?? '',
  990. 'order_number' => $v['order_number'] ?? '',
  991. 'supplier_name' => $v['supplier_name'] ?? '',
  992. 'deliveryman' => $v['deliveryman'] ?? '',
  993. 'plate_number' => $v['plate_number'] ?? '',
  994. 'create_time' => $v['create_time'] ?? 0,
  995. 'status' => $v['status'] ?? 0
  996. ];
  997. }
  998. // 返回结果
  999. return json([
  1000. 'total' => $total,
  1001. 'rows' => $list
  1002. ]);
  1003. }
  1004. /**
  1005. * 发货单打印
  1006. * @return \think\response\Json|void
  1007. * @throws \think\db\exception\DataNotFoundException
  1008. * @throws \think\db\exception\ModelNotFoundException
  1009. * @throws \think\exception\DbException
  1010. */
  1011. public function printqrcode()
  1012. {
  1013. if (!$this->request->isAjax()) {
  1014. return json(['code' => 0, 'msg' => '请求错误']);
  1015. }
  1016. try {
  1017. $mongo = \think\Db::connect('mongodb');
  1018. $userinfo = Session::get('admin');
  1019. if (empty($userinfo) || empty($userinfo['company'])) {
  1020. return json(['code' => 0, 'msg' => '用户信息获取失败']);
  1021. }
  1022. $goodsCollection = '_qcode_goods';
  1023. $bachCollection = $userinfo['company'] . '_qcode_bach';
  1024. $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  1025. $id = input('id');
  1026. if (empty($id)) {
  1027. return json(['code' => 0, 'msg' => 'ID不能为空']);
  1028. }
  1029. $goodList = $mongo->name($goodsCollection)->where('_id', $id)->find();
  1030. if (empty($goodList)) {
  1031. return json(['code' => 0, 'msg' => '未找到发货单数据']);
  1032. }
  1033. if (empty($goodList['qcode_bachsummary_id'])) {
  1034. return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  1035. }
  1036. $bachsummaryData = $mongo->name($bachsummaryCollection)
  1037. ->where('_id', $goodList['qcode_bachsummary_id'])
  1038. ->find();
  1039. if (empty($bachsummaryData)) {
  1040. return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  1041. }
  1042. $bachData = $mongo->name($bachCollection)
  1043. ->where('bach_ids', $bachsummaryData['bach_ids'])
  1044. ->select();
  1045. if (empty($bachData)) {
  1046. return json(['code' => 0, 'msg' => '未找到对应的批次数据']);
  1047. }
  1048. $groupedData = [];
  1049. $totalTray = 0;
  1050. $totalBox = 0;
  1051. $productSummary = [];
  1052. $totalTao = 0;
  1053. $totalZhang = 0;
  1054. $totalGe = 0;
  1055. foreach ($bachData as $item) {
  1056. $actualQty = is_numeric($item['actual_quantity']) ? (int)$item['actual_quantity'] : 0;
  1057. $boxCount = is_numeric($item['total_boxes']) ? (int)$item['total_boxes'] : 0;
  1058. $trayCount = is_numeric($item['pallet_count']) ? (int)$item['pallet_count'] : 0;
  1059. $totalTray += $trayCount;
  1060. $totalBox += $boxCount;
  1061. $unit = $item['unit'] ?? '个';
  1062. switch ($unit) {
  1063. case '套': $totalTao += $actualQty; break;
  1064. case '张': $totalZhang += $actualQty; break;
  1065. default: $totalGe += $actualQty; break;
  1066. }
  1067. $productName = $item['matter_name'] ?? '';
  1068. if (!isset($productSummary[$productName])) {
  1069. $productSummary[$productName] = ['qty' => 0, 'unit' => $unit];
  1070. }
  1071. $productSummary[$productName]['qty'] += $actualQty;
  1072. $boxesPerPallet = (is_numeric($item['tray_num']) && is_numeric($item['box_num']))
  1073. ? $item['tray_num'] * $item['box_num'] : $boxCount;
  1074. $groupedData[] = [
  1075. 'pallet' => $item['pallet_range'] ?? '',
  1076. 'matter_name' => $item['matter_name'] ?? '',
  1077. 'small_num' => $item['small_num'] ?? '',
  1078. 'total_boxes' => $boxesPerPallet,
  1079. 'tray_num' => $item['tray_num'] ?? '',
  1080. 'box_num' => $item['box_num'] ?? '',
  1081. 'pallet_height' => $item['layer_height'] ?? '',
  1082. 'pallet_length' => $item['pallet_length'] ?? '',
  1083. 'pallet_width' => $item['pallet_width'] ?? '',
  1084. 'large_num' => $trayCount,
  1085. 'larger_num' => $boxCount,
  1086. 'num' => $actualQty,
  1087. 'danwei' => $unit,
  1088. 'order_ddbh' => $item['order_ddbh'] ?? '',
  1089. 'gdbh' => $item['gdbh'] ?? '',
  1090. 'cpbm' => $item['cpbm'] ?? ''
  1091. ];
  1092. }
  1093. $responseData = [
  1094. '_id' => $goodList['_id'],
  1095. 'shrq_date' => date('Y-m-d'),
  1096. 'shdh' => $goodList['shdh'] ?? '',
  1097. 'order_number' => $goodList['order_number'] ?? '',
  1098. 'address' => $goodList['address'] ?? '',
  1099. 'company_id' => $goodList['company_id'] ?? 0,
  1100. 'count' => $goodList['count'] ?? 0,
  1101. 'create_time' => $goodList['create_time'] ?? '',
  1102. 'data' => $groupedData,
  1103. 'summary' => [
  1104. 'totalTray' => $totalTray,
  1105. 'totalBox' => $totalBox,
  1106. 'totalTao' => $totalTao,
  1107. 'totalZhang' => $totalZhang,
  1108. 'totalGe' => $totalGe,
  1109. 'products' => $productSummary
  1110. ],
  1111. 'delete_time' => $goodList['delete_time'] ?? '',
  1112. 'deliveryman' => $goodList['deliveryman'] ?? '',
  1113. 'export_id' => $goodList['export_id'] ?? '',
  1114. 'note' => $goodList['note'] ?? '',
  1115. 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1116. 'plate_number' => $goodList['plate_number'] ?? '',
  1117. 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1118. 'shr_phone' => $goodList['shr_phone'] ?? '',
  1119. 'status' => $goodList['status'] ?? 0,
  1120. 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1121. 'supplier_name' => $goodList['supplier_name'] ?? '',
  1122. 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1123. 'user_id' => $goodList['user_id'] ?? 0
  1124. ];
  1125. return json(['code' => 1, 'data' => $responseData]);
  1126. } catch (\Exception $e) {
  1127. return json(['code' => 0, 'msg' => '系统错误: ' . $e->getMessage()]);
  1128. }
  1129. }
  1130. // public function printqrcode()
  1131. // {
  1132. // // 1. 验证请求类型
  1133. // if (!$this->request->isAjax()) {
  1134. // return json(['code' => 0, 'msg' => '请求错误']);
  1135. // }
  1136. //
  1137. // try {
  1138. // // 2. 初始化数据库连接
  1139. // $mongo = \think\Db::connect('mongodb');
  1140. // $userinfo = Session::get('admin');
  1141. //
  1142. // if (empty($userinfo) || empty($userinfo['company'])) {
  1143. // return json(['code' => 0, 'msg' => '用户信息获取失败']);
  1144. // }
  1145. //
  1146. // // 3. 设置集合名称
  1147. // $goodsCollection = '_qcode_goods';
  1148. // $bachCollection = $userinfo['company'] . '_qcode_bach';
  1149. // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  1150. //
  1151. // // 4. 获取请求参数
  1152. // $id = input('id');
  1153. // if (empty($id)) {
  1154. // return json(['code' => 0, 'msg' => 'ID不能为空']);
  1155. // }
  1156. //
  1157. // // 5. 查询发货单信息
  1158. // $goodList = $mongo->name($goodsCollection)
  1159. // ->where('_id', $id)
  1160. // ->find();
  1161. //
  1162. // if (empty($goodList)) {
  1163. // return json(['code' => 0, 'msg' => '未找到发货单数据']);
  1164. // }
  1165. //
  1166. // // 6. 获取批次数据
  1167. // if (isset($goodList['data']) && is_array($goodList['data'])) {
  1168. // $detailData = $goodList['data'];
  1169. // } else {
  1170. // if (empty($goodList['qcode_bachsummary_id'])) {
  1171. // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  1172. // }
  1173. //
  1174. // $bachsummaryData = $mongo->name($bachsummaryCollection)
  1175. // ->where('_id', $goodList['qcode_bachsummary_id'])
  1176. // ->find();
  1177. //
  1178. // if (empty($bachsummaryData)) {
  1179. // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  1180. // }
  1181. //
  1182. // $detailData = $mongo->name($bachCollection)
  1183. // ->where('bach_ids', $bachsummaryData['bach_ids'])
  1184. // ->select();
  1185. // }
  1186. //
  1187. // // 7. 按产品名称分组处理
  1188. // $productGroups = [];
  1189. // foreach ($detailData as $item) {
  1190. // $productName = $item['matter_name'];
  1191. // if (!isset($productGroups[$productName])) {
  1192. // $productGroups[$productName] = [];
  1193. // }
  1194. // $productGroups[$productName][] = $item;
  1195. // }
  1196. //
  1197. // // 8. 处理分组数据
  1198. // $groupedData = [];
  1199. // $totalTray = 0;
  1200. // $totalBox = 0;
  1201. // $productSummary = [];
  1202. //
  1203. // // 存储完整托盘和部分托盘
  1204. // $allFullPallets = [];
  1205. // $allPartialPallets = [];
  1206. //
  1207. // foreach ($productGroups as $productName => $items) {
  1208. // foreach ($items as $item) {
  1209. // if ($item['pallet_type'] === 'full') {
  1210. // $allFullPallets[] = [
  1211. // 'productName' => $productName,
  1212. // 'data' => $item
  1213. // ];
  1214. // } else {
  1215. // $allPartialPallets[] = [
  1216. // 'productName' => $productName,
  1217. // 'data' => $item
  1218. // ];
  1219. // }
  1220. // }
  1221. // }
  1222. //
  1223. // // 对完整托盘按产品名称和托盘序号排序
  1224. // usort($allFullPallets, function($a, $b) {
  1225. // $nameCompare = strcmp($a['productName'], $b['productName']);
  1226. // if ($nameCompare !== 0) {
  1227. // return $nameCompare;
  1228. // }
  1229. // return $a['data']['pallet_sequence'] <=> $b['data']['pallet_sequence'];
  1230. // });
  1231. //
  1232. // // 处理完整托盘
  1233. // $currentPalletNumber = 1;
  1234. // $currentProduct = null;
  1235. // $rangeStart = null;
  1236. // $rangeCount = 0;
  1237. //
  1238. // foreach ($allFullPallets as $index => $pallet) {
  1239. // if ($pallet['productName'] !== $currentProduct) {
  1240. // // 完成上一个产品范围
  1241. // if ($rangeStart !== null) {
  1242. // $first = $allFullPallets[$rangeStart]['data'];
  1243. //
  1244. // $groupedData[] = [
  1245. // 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
  1246. // 'matter_name' => $currentProduct,
  1247. // 'small_num' => $first['small_num'],
  1248. // 'total_boxes' => $first['boxes_in_pallet'],
  1249. // 'tray_num' => $first['tray_num'],
  1250. // 'box_num' => $first['box_num'],
  1251. // 'pallet_height' => $first['pallet_height'],
  1252. // 'pallet_length' => $first['pallet_length'],
  1253. // 'pallet_width' => $first['pallet_width'],
  1254. // 'large_num' => $rangeCount,
  1255. // 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
  1256. // 'num' => $first['num'] * $rangeCount,
  1257. // 'danwei' => $first['danwei']
  1258. // ];
  1259. //
  1260. // $totalTray += $rangeCount;
  1261. // $totalBox += $first['boxes_in_pallet'] * $rangeCount;
  1262. //
  1263. // if (!isset($productSummary[$currentProduct])) {
  1264. // $productSummary[$currentProduct] = [
  1265. // 'qty' => 0,
  1266. // 'unit' => $first['danwei']
  1267. // ];
  1268. // }
  1269. // $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
  1270. //
  1271. // $currentPalletNumber += $rangeCount;
  1272. // }
  1273. //
  1274. // // 开始新的产品范围
  1275. // $currentProduct = $pallet['productName'];
  1276. // $rangeStart = $index;
  1277. // $rangeCount = 1;
  1278. // } else {
  1279. // $rangeCount++;
  1280. // }
  1281. // }
  1282. //
  1283. // // 处理最后一个产品范围
  1284. // if ($rangeStart !== null) {
  1285. // $first = $allFullPallets[$rangeStart]['data'];
  1286. //
  1287. // $groupedData[] = [
  1288. // 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
  1289. // 'matter_name' => $currentProduct,
  1290. // 'small_num' => $first['small_num'],
  1291. // 'total_boxes' => $first['boxes_in_pallet'],
  1292. // 'tray_num' => $first['tray_num'],
  1293. // 'box_num' => $first['box_num'],
  1294. // 'pallet_height' => $first['pallet_height'],
  1295. // 'pallet_length' => $first['pallet_length'],
  1296. // 'pallet_width' => $first['pallet_width'],
  1297. // 'large_num' => $rangeCount,
  1298. // 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
  1299. // 'num' => $first['num'] * $rangeCount,
  1300. // 'danwei' => $first['danwei']
  1301. // ];
  1302. //
  1303. // $totalTray += $rangeCount;
  1304. // $totalBox += $first['boxes_in_pallet'] * $rangeCount;
  1305. //
  1306. // if (!isset($productSummary[$currentProduct])) {
  1307. // $productSummary[$currentProduct] = [
  1308. // 'qty' => 0,
  1309. // 'unit' => $first['danwei']
  1310. // ];
  1311. // }
  1312. // $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
  1313. //
  1314. // $currentPalletNumber += $rangeCount;
  1315. // }
  1316. //
  1317. // // 处理部分托盘 - 合并显示但分开产品信息
  1318. // $warning4Partial = null;
  1319. // $warning10Partial = null;
  1320. //
  1321. // // 查找可以合并的部分托盘
  1322. // foreach ($allPartialPallets as $partial) {
  1323. // if (strpos($partial['productName'], '警语4') !== false) {
  1324. // $warning4Partial = $partial;
  1325. // } elseif (strpos($partial['productName'], '警语10') !== false) {
  1326. // $warning10Partial = $partial;
  1327. // }
  1328. // }
  1329. //
  1330. // // 检查是否可以合并(7+13=20箱)
  1331. // if ($warning4Partial && $warning10Partial &&
  1332. // ($warning4Partial['data']['boxes_in_pallet'] + $warning10Partial['data']['boxes_in_pallet']) == 20) {
  1333. //
  1334. // // 警语4版本(7箱)
  1335. // $groupedData[] = [
  1336. // 'pallet' => $currentPalletNumber,
  1337. // 'matter_name' => $warning4Partial['productName'],
  1338. // 'small_num' => $warning4Partial['data']['small_num'],
  1339. // 'total_boxes' => $warning4Partial['data']['boxes_in_pallet'],
  1340. // 'tray_num' => $warning4Partial['data']['tray_num'],
  1341. // 'box_num' => $warning4Partial['data']['box_num'],
  1342. // 'pallet_height' => $warning4Partial['data']['pallet_height'],
  1343. // 'pallet_length' => $warning4Partial['data']['pallet_length'],
  1344. // 'pallet_width' => $warning4Partial['data']['pallet_width'],
  1345. // 'large_num' => 1,
  1346. // 'larger_num' => $warning4Partial['data']['boxes_in_pallet'],
  1347. // 'num' => $warning4Partial['data']['num'],
  1348. // 'danwei' => $warning4Partial['data']['danwei'],
  1349. // 'is_combined' => true
  1350. // ];
  1351. //
  1352. // // 警语10版本(13箱)
  1353. // $groupedData[] = [
  1354. // 'pallet' => $currentPalletNumber,
  1355. // 'matter_name' => $warning10Partial['productName'],
  1356. // 'small_num' => $warning10Partial['data']['small_num'],
  1357. // 'total_boxes' => $warning10Partial['data']['boxes_in_pallet'],
  1358. // 'tray_num' => $warning10Partial['data']['tray_num'],
  1359. // 'box_num' => $warning10Partial['data']['box_num'],
  1360. // 'pallet_height' => $warning10Partial['data']['pallet_height'],
  1361. // 'pallet_length' => $warning10Partial['data']['pallet_length'],
  1362. // 'pallet_width' => $warning10Partial['data']['pallet_width'],
  1363. // 'large_num' => 1,
  1364. // 'larger_num' => $warning10Partial['data']['boxes_in_pallet'],
  1365. // 'num' => $warning10Partial['data']['num'],
  1366. // 'danwei' => $warning10Partial['data']['danwei'],
  1367. // 'is_combined' => true
  1368. // ];
  1369. //
  1370. // $totalTray += 1; // 只算一个托盘
  1371. // $totalBox += 20; // 总箱数7+13=20
  1372. //
  1373. // // 产品统计
  1374. // foreach ([$warning4Partial, $warning10Partial] as $partial) {
  1375. // if (!isset($productSummary[$partial['productName']])) {
  1376. // $productSummary[$partial['productName']] = [
  1377. // 'qty' => 0,
  1378. // 'unit' => $partial['data']['danwei']
  1379. // ];
  1380. // }
  1381. // $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
  1382. // }
  1383. //
  1384. // $currentPalletNumber++;
  1385. // } else {
  1386. // // 不能合并,单独显示每个部分托盘
  1387. // foreach ($allPartialPallets as $partial) {
  1388. // $groupedData[] = [
  1389. // 'pallet' => $currentPalletNumber,
  1390. // 'matter_name' => $partial['productName'],
  1391. // 'small_num' => $partial['data']['small_num'],
  1392. // 'total_boxes' => $partial['data']['boxes_in_pallet'],
  1393. // 'tray_num' => $partial['data']['tray_num'],
  1394. // 'box_num' => $partial['data']['box_num'],
  1395. // 'pallet_height' => $partial['data']['pallet_height'],
  1396. // 'pallet_length' => $partial['data']['pallet_length'],
  1397. // 'pallet_width' => $partial['data']['pallet_width'],
  1398. // 'large_num' => 1,
  1399. // 'larger_num' => $partial['data']['boxes_in_pallet'],
  1400. // 'num' => $partial['data']['num'],
  1401. // 'danwei' => $partial['data']['danwei']
  1402. // ];
  1403. //
  1404. // $totalTray += 1;
  1405. // $totalBox += $partial['data']['boxes_in_pallet'];
  1406. //
  1407. // if (!isset($productSummary[$partial['productName']])) {
  1408. // $productSummary[$partial['productName']] = [
  1409. // 'qty' => 0,
  1410. // 'unit' => $partial['data']['danwei']
  1411. // ];
  1412. // }
  1413. // $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
  1414. //
  1415. // $currentPalletNumber++;
  1416. // }
  1417. // }
  1418. //
  1419. // // 9. 单位分类统计
  1420. // $totalTao = 0;
  1421. // $totalZhang = 0;
  1422. // $totalGe = 0;
  1423. //
  1424. // foreach ($productSummary as $product) {
  1425. // switch ($product['unit']) {
  1426. // case '套':
  1427. // $totalTao += $product['qty'];
  1428. // break;
  1429. // case '张':
  1430. // $totalZhang += $product['qty'];
  1431. // break;
  1432. // case '个':
  1433. // $totalGe += $product['qty'];
  1434. // break;
  1435. // }
  1436. // }
  1437. //
  1438. // // 10. 构造返回数据
  1439. // $responseData = [
  1440. // '_id' => $goodList['_id'],
  1441. // 'shrq_date' => date('Y-m-d'),
  1442. // 'shdh' => $goodList['shdh'] ?? '',
  1443. // 'order_number' => $goodList['order_number'] ?? '',
  1444. // 'address' => $goodList['address'] ?? '',
  1445. // 'company_id' => $goodList['company_id'] ?? 0,
  1446. // 'count' => $goodList['count'] ?? 0,
  1447. // 'create_time' => $goodList['create_time'] ?? '',
  1448. // 'data' => $groupedData,
  1449. // 'summary' => [
  1450. // 'totalTray' => $totalTray,
  1451. // 'totalBox' => $totalBox,
  1452. // 'totalTao' => $totalTao,
  1453. // 'totalZhang' => $totalZhang,
  1454. // 'totalGe' => $totalGe,
  1455. // 'products' => $productSummary
  1456. // ],
  1457. // 'delete_time' => $goodList['delete_time'] ?? '',
  1458. // 'deliveryman' => $goodList['deliveryman'] ?? '',
  1459. // 'export_id' => $goodList['export_id'] ?? '',
  1460. // 'note' => $goodList['note'] ?? '',
  1461. // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1462. // 'plate_number' => $goodList['plate_number'] ?? '',
  1463. // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1464. // 'shr_phone' => $goodList['shr_phone'] ?? '',
  1465. // 'status' => $goodList['status'] ?? 0,
  1466. // 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1467. // 'supplier_name' => $goodList['supplier_name'] ?? '',
  1468. // 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1469. // 'user_id' => $goodList['user_id'] ?? 0
  1470. // ];
  1471. //
  1472. // return json([
  1473. // 'code' => 1,
  1474. // 'data' => $responseData
  1475. // ]);
  1476. //
  1477. // } catch (\Exception $e) {
  1478. // return json([
  1479. // 'code' => 0,
  1480. // 'msg' => '系统错误: ' . $e->getMessage()
  1481. // ]);
  1482. // }
  1483. // }
  1484. /**
  1485. * 获取辅料名称
  1486. * @return \think\response\Json
  1487. * @throws \think\db\exception\DataNotFoundException
  1488. * @throws \think\db\exception\ModelNotFoundException
  1489. * @throws \think\exception\DbException
  1490. */
  1491. public function matterName()
  1492. {
  1493. // $userinfo = Session::get('admin');
  1494. // $lager = new QcodeLarge();
  1495. // $bach = new QcodeBach();
  1496. // $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
  1497. // $matter_name = [];
  1498. // if (!empty($bach_id)){
  1499. // foreach ($bach_id as $v){
  1500. // $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
  1501. // $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
  1502. // }
  1503. // }
  1504. // $matter_name = array_unique($matter_name);
  1505. // return json($matter_name);
  1506. }
  1507. /**
  1508. * 发货单删除
  1509. * @param $ids
  1510. * @return void
  1511. */
  1512. public function dispatch_del($ids)
  1513. {
  1514. $goods = new QcodeGoods();
  1515. $export = new QcodeExport();
  1516. if (empty($ids)){
  1517. $this->error('参数错误');
  1518. }
  1519. $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  1520. if ($res === false){
  1521. $this->error('删除失败');
  1522. }
  1523. $userinfo = Session::get('admin');
  1524. $mongo = \think\Db::connect('mongodb');
  1525. $collection = $userinfo['company'] . '_qcode_bach';
  1526. $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
  1527. $res_find = $goods->where('_id',$ids)->find();
  1528. $update_status = $mongo->name($bachsummary)->where('_id',$res_find['qcode_bachsummary_id'])->update(
  1529. [
  1530. 'bach_status'=>0,
  1531. 'delete_time' => ''
  1532. ]);
  1533. if ($update_status === false){
  1534. $this->error('删除失败');
  1535. }
  1536. $this->success('成功');
  1537. }
  1538. /**
  1539. * 收货列表
  1540. */
  1541. public function receive()
  1542. {
  1543. //当前是否为关联查询
  1544. $this->relationSearch = false;
  1545. //设置过滤方法
  1546. $this->request->filter(['strip_tags', 'trim']);
  1547. if ($this->request->isAjax()) {
  1548. //如果发送的来源是Selectpage,则转发到Selectpage
  1549. if ($this->request->request('keyField')) {
  1550. return $this->selectpage();
  1551. }
  1552. $where = [
  1553. 'delete_time'=> ''
  1554. ];
  1555. $req = input();
  1556. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  1557. $order = $req['order'];
  1558. $offset = $req['offset'];
  1559. $limit = $req['limit'];
  1560. // 构造模糊查询条件
  1561. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  1562. // $filter = ['field' => $regex];
  1563. $filter = json_decode($req['filter'], true);
  1564. if (isset($filter['status'])){
  1565. $where['status'] = intval($filter['status']);
  1566. unset($filter['status']);
  1567. }
  1568. if (isset($filter['create_time'])){
  1569. $begin = substr($filter['create_time'],0,19);
  1570. $end = substr($filter['create_time'],22);
  1571. $begin = strtotime($begin);
  1572. $end = strtotime($end);
  1573. $where['create_time'] = ['between',[$begin,$end]];
  1574. unset($filter['create_time']);
  1575. }
  1576. foreach ($filter as $k => $v){
  1577. $where[$k] = new \MongoDB\BSON\Regex($v);
  1578. }
  1579. $goods = new QcodeGoods();
  1580. $total = $goods->where($where)->count();
  1581. $list = $goods->where($where)
  1582. ->order($sort,$order)
  1583. ->limit($limit)
  1584. ->skip($offset)
  1585. ->select();
  1586. foreach ($list as $k=>$v) {
  1587. $oid = $v['_id']->jsonSerialize();
  1588. $list[$k]['id'] = $oid['$oid'];
  1589. }
  1590. $result = array("total" => $total, "rows" => $list);
  1591. return json($result);
  1592. }
  1593. return $this->view->fetch();
  1594. }
  1595. /**
  1596. * 扫码/输入收货
  1597. */
  1598. public function receive_add()
  1599. {
  1600. $req = $this->request->param();
  1601. if(!isset($req['shdh']) || empty($req['shdh'])){
  1602. $this->error('请输入送货单号');
  1603. }
  1604. $data = [
  1605. 'shdh'=>$req['shdh'],
  1606. 'delete_time'=>'',
  1607. ];
  1608. $goods = new QcodeGoods();
  1609. $row = $goods->where($data)->find();
  1610. if (!$row) $this->error('未查询到该单号');
  1611. if ($row['status']==2) $this->error('该单号已收货');
  1612. //收货---修改status为2
  1613. $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
  1614. if ($bool){
  1615. $this->success('成功');
  1616. }else{
  1617. $this->error('失败');
  1618. }
  1619. }
  1620. /**
  1621. * 取消收货
  1622. */
  1623. public function receive_del()
  1624. {
  1625. $req = $this->request->param();
  1626. if(!isset($req['ids']) || empty($req['ids'])){
  1627. $this->error('id不能为空');
  1628. }
  1629. $data = [
  1630. '_id'=>$req['ids'],
  1631. 'delete_time'=>'',
  1632. ];
  1633. $goods = new QcodeGoods();
  1634. //收货---修改status为2
  1635. $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
  1636. if ($bool){
  1637. $this->success('成功');
  1638. }else{
  1639. $this->error('失败');
  1640. }
  1641. }
  1642. }