Deliver.php 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  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\Session;
  19. class Deliver extends Backend
  20. {
  21. /**
  22. * 首页
  23. */
  24. public function index()
  25. {
  26. return $this->view->fetch();
  27. }
  28. /**
  29. * 大件发货
  30. */
  31. public function lager()
  32. {
  33. $company = new QcodeCompany();
  34. $product = new QcodeProduct();
  35. $large = new QcodeLarge();
  36. $bach = new QcodeBach();
  37. $small = new QcodeSmall();
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if (false === $this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. $userInfo = Session::get('admin');
  46. $where = ['delete_time' => ''];
  47. $filter = input('filter');
  48. $filter = json_decode($filter, true);
  49. $whereList = ['delete_time' => ''];
  50. if (isset($filter['matter_name'])) {
  51. $whereList['matter_no'] = $filter['matter_name'];
  52. }
  53. if (isset($filter['bach'])) {
  54. $whereList['bach_num'] = $filter['bach'];
  55. }
  56. if (isset($filter['manufacture_date'])) {
  57. $begin = substr($filter['manufacture_date'], 0, 19);
  58. $end = substr($filter['manufacture_date'], 22);
  59. $begin = (int)date('ymd', strtotime($begin));
  60. $end = (int)date('ymd', strtotime($end));
  61. $whereList['manufacture_date'] = ['between', [$begin, $end]];
  62. }
  63. // 查出相关 bach_id
  64. $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
  65. $bach_id = [];
  66. foreach ($bach_list as $v) {
  67. $id = substr(json_encode($v), 9, -2);
  68. $bach_id[] = $id;
  69. }
  70. // 获取分页参数
  71. $offset = input('offset', 0);
  72. $limit = input('limit', 10);
  73. // 获取所有大件记录用于总量判断
  74. $all_large_list = $large->name($userInfo['company'].'_qcode_large')
  75. ->where($where)
  76. ->where('l_status', 0)
  77. ->whereIn('bach_id', $bach_id)
  78. ->select();
  79. // 统计总数量
  80. $total = count($all_large_list);
  81. // 分页
  82. $large_list = array_slice($all_large_list, $offset, $limit);
  83. $list = [];
  84. foreach ($large_list as $k => $v) {
  85. $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
  86. // $num = (int)$bach_detail['num']; // 总张数
  87. // $small_num = (int)$bach_detail['small_num']; // 每箱张数
  88. // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
  89. // // 重新计算总箱数
  90. // $larger_num = $num / $small_num;
  91. // $full_pallets = floor($larger_num / $boxes_per_pallet);
  92. // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
  93. $num = (float)$bach_detail['num'];
  94. $small_num = (float)$bach_detail['small_num'];
  95. $boxes_per_pallet = (float)$bach_detail['total_boxes'];
  96. $larger_num = $num / $small_num;
  97. $full_pallets = floor($larger_num / $boxes_per_pallet);
  98. $last_pallet_boxes = fmod($larger_num, $boxes_per_pallet); // 真正浮点取余
  99. $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
  100. // 当前是第几托(code中解析)
  101. $current_flow_str = substr($v['code'], 53, 6);
  102. $current_flow = (int) ltrim($current_flow_str, '0');
  103. $total_pallets = (int) $total_pallets;
  104. // 是否是最后一托
  105. $is_last = ($current_flow === $total_pallets) ? 1 : 0;
  106. // 修正箱数
  107. $total_boxes = $boxes_per_pallet;
  108. if ($is_last && $last_pallet_boxes > 0) {
  109. // 精准保留1位小数,不做四舍五入
  110. $total_boxes = number_format($last_pallet_boxes, 1, '.', '');
  111. // $total_boxes = $last_pallet_boxes;
  112. }
  113. $list[$k] = [
  114. 'id' => substr(json_encode($v['_id']), 9, -2),
  115. 'l_flow' => $current_flow,
  116. 'bach' => $bach_detail['bach_num'],
  117. 'num' => $num,
  118. 'small_num' => $small_num,
  119. 'order_ddbh' => $bach_detail['order_ddbh'],
  120. 'danwei' => $bach_detail['danwei'],
  121. 'cpbm' => $bach_detail['cpbm'],
  122. 'matter_name' => $bach_detail['matter_name'],
  123. 'total_boxes' => $total_boxes,
  124. 'tray_num' => $bach_detail['tray_num'],
  125. 'box_num' => $bach_detail['box_num'],
  126. 'pallet_height' => $bach_detail['pallet_height'],
  127. 'pallet_length' => $bach_detail['pallet_length'],
  128. 'pallet_width' => $bach_detail['pallet_width'],
  129. 'larger_num' => $larger_num,
  130. 'manufacture_date' => date('Y-m-d', strtotime('20' . $bach_detail['manufacture_date'])),
  131. 'code' => $v['code'],
  132. 'is_last' => $is_last,
  133. ];
  134. }
  135. return json(['total' => $total, 'rows' => $list]);
  136. }
  137. /**
  138. * 小件列表
  139. * @return \think\response\Json
  140. * @throws \think\Exception
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public function smallList()
  146. {
  147. $userinfo = Session::get('admin');
  148. $lager = new QcodeLarge();
  149. $small = new QcodeSmall();
  150. if ($this->request->isAjax() === false){
  151. $this->error('请求错误');
  152. }
  153. $filter = input('filter');
  154. $filter = json_decode($filter,true);
  155. $order = input('order');
  156. $offset = input('offset');
  157. $limit = input('limit');
  158. $where = [
  159. 'delete_time' => '',
  160. ];
  161. if (isset($filter['large_id'])){
  162. $where['large_id'] =$filter['large_id'];
  163. }
  164. $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
  165. ->where($where)
  166. ->skip($offset)
  167. ->limit($limit)
  168. ->select();
  169. $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
  170. $list = [];
  171. foreach ($smallList as $k=>$v){
  172. $list[$k] = [
  173. 'code' => $v['code'],
  174. 'l_flow' => $v['l_flow'],
  175. 'print_num' => $v['p_nums'],
  176. 'status' => $v['status'],
  177. ];
  178. }
  179. return json(['total'=>$total,'rows'=>$list]);
  180. }
  181. /**
  182. * 导出发货
  183. * @return void
  184. * @throws \think\Exception
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. * @throws \think\exception\DbException
  188. */
  189. public function print()
  190. {
  191. $userinfo = Session::get('admin');
  192. $lager = new QcodeLarge();
  193. $bach = new QcodeBach();
  194. $small = new QcodeSmall();
  195. $export = new QcodeExport();
  196. if ($this->request->isAjax() === false){
  197. $this->error('请求错误');
  198. }
  199. $lager_id = input('lager_id');
  200. if ($lager_id === ''){
  201. $this->error('请至少选择一个大件');
  202. }
  203. $lagerId = [];
  204. if (strpos($lager_id,',') === false){
  205. $lagerId[0] = $lager_id;
  206. }else{
  207. $lagerId = explode(',',$lager_id);
  208. }
  209. $lager_num = count($lagerId);
  210. $bach_id = [];
  211. foreach ($lagerId as $k=>$v){
  212. $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
  213. $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
  214. $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
  215. }
  216. $bach_id = array_unique($bach_id);
  217. $data = [];
  218. foreach ($bach_id as $k=>$v){
  219. $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
  220. }
  221. $data = array_unique($data);
  222. if (count($data)>1){
  223. $this->error('请选择同一产品');
  224. }
  225. $n =0;
  226. foreach (explode(',',$lager_id) as $kk=>$vv){
  227. $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
  228. }
  229. // $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
  230. $bachArray = $bach->name($userinfo['company'].'_'.'qcode_bach')
  231. ->where('_id', $bach_id[0])
  232. ->find();
  233. $bachDetail = $bachArray ? $bachArray->toArray() : [];
  234. if ($bachDetail['box_num'] === 1){
  235. $mater_type = 2;
  236. }else{
  237. $mater_type = 1;
  238. }
  239. $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
  240. $file_dir = $this->exportExcel($exportData);
  241. $row = [
  242. 'username' => $bachDetail['supplier_name'],//公司
  243. 'matter_no' => $bachDetail['matter_no'],
  244. 'order_ddbh' => $bachDetail['order_ddbh'],
  245. 'large_str' => $lager_id,
  246. 'status' => 0,
  247. 'bach_num' => $bachDetail['bach_num'],
  248. 'note' => '',
  249. 'user_id' => $userinfo['id'],
  250. 'create_time' => time(),
  251. 'file_dir' => $file_dir,
  252. 'company_id' => $userinfo['company'],
  253. 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
  254. 'small_num' => $n,//共包含了 N 个小件
  255. 'matter_name' => $bachDetail['matter_name'],//产品名称
  256. 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
  257. 'tray_num' => $bachDetail['tray_num'],//每层箱数
  258. 'box_num' => $bachDetail['box_num'],//每托层数
  259. 'pallet_height' => $bachDetail['pallet_height'],//每托高度
  260. 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
  261. 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
  262. 'larger_num' => $bachDetail['larger_num'],//总箱数
  263. ];
  264. if ($bachDetail['box_num'] != 1){
  265. $row['num'] = $bachDetail['box_num'];
  266. $row['mater_type'] = 1;
  267. }else{
  268. $row['num'] = $bachDetail['s_weight'];
  269. $row['mater_type'] = 2;
  270. }
  271. $res = $export->save($row);
  272. if ($res === false){
  273. $this->error('发货失败');
  274. }
  275. $lager = new QcodeLarge();
  276. $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
  277. $this->success('成功','',$file_dir);
  278. }
  279. /**
  280. * 表格数据处理
  281. * @param $bach_id
  282. * @param $large_str
  283. * @param $company
  284. * @param $large_num
  285. * @param $small_num
  286. * @return array
  287. * @throws \think\db\exception\DataNotFoundException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. * @throws \think\exception\DbException
  290. */
  291. public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
  292. {
  293. $bach = new QcodeBach();
  294. $large = new QcodeLarge();
  295. $small = new QcodeSmall();
  296. $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
  297. $data = [
  298. 'supplier_name' => $bachList['supplier_name'],
  299. 'supplier_no' => $bachList['supplier_code'],
  300. 'matter_name' => $bachList['matter_name'],
  301. 'matter_no' => $bachList['matter_no'],
  302. 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
  303. 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
  304. 'box_num' => $large_num,
  305. 'tray_num' => $bachList['tray_num'],
  306. 'small_num' => $small_num,
  307. 'l_reservation' => $bachList['l_reservation'],
  308. 's_reservation' => $bachList['s_reservation'],
  309. 'l_flow' => $bachList['l_flow'],
  310. 's_flow' => $bachList['s_flow'],
  311. 'l_weight' => 0,
  312. 's_weight' => $bachList['s_weight']/1000,
  313. 'code' => [],
  314. ];
  315. $large_id = explode(',', $large_str);
  316. foreach ($large_id as $k => $v) {
  317. $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
  318. $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
  319. $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
  320. foreach ($small_list as $value) {
  321. $code = [
  322. 'large_code' => $largeList['code'],
  323. 'small_code' => $value['code'],
  324. ];
  325. array_push($data['code'],$code);
  326. }
  327. }
  328. return $data;
  329. }
  330. /**
  331. * 生成excel
  332. * @param $data
  333. * @return string
  334. * @throws Exception
  335. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  336. */
  337. public function exportExcel($data)
  338. {
  339. $objexcel = new Spreadsheet();
  340. $sheet = $objexcel->getActiveSheet();
  341. //设置全局单元格垂直居中
  342. $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
  343. //设置全局单元格水平居中
  344. $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  345. //设置全局单元格自动换行
  346. $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
  347. //设置单元格宽度
  348. $sheet->getDefaultColumnDimension()->setWidth(50);
  349. //设置表头
  350. $sheet->setCellValue('A1', '文件流水号');
  351. $sheet->setCellValue('A2', '订单号');
  352. $sheet->setCellValue('A3', '供应商');
  353. $sheet->setCellValue('A4', '供应商代码');
  354. $sheet->setCellValue('A5', '物料名称');
  355. $sheet->setCellValue('A6', '物料代码');
  356. $sheet->setCellValue('A7', '生产日期');
  357. $sheet->setCellValue('A8', '打码日期');
  358. $sheet->setCellValue('A9', '大件数量');
  359. $sheet->setCellValue('A10', '托盘小件数量');
  360. $sheet->setCellValue('A11', '小件总数量');
  361. $sheet->setCellValue('C3', '大件预留码');
  362. $sheet->setCellValue('C5', '小件预留码');
  363. $sheet->setCellValue('C7', '大件开始流水号');
  364. $sheet->setCellValue('C9', '小件开始流水号');
  365. $sheet->setCellValue('C11', '大件重量(Kg)');
  366. $sheet->setCellValue('C13', '小件重量(Kg)');
  367. $sheet->setCellValue('A15', '大件二维码');
  368. $sheet->setCellValue('B15', '大件条码(预留)');
  369. $sheet->setCellValue('C15', '小件二维码');
  370. $sheet->setCellValue('D15', '小件条码(预留)');
  371. //插入表格数据
  372. $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
  373. $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
  374. $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
  375. $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
  376. $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
  377. $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
  378. $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
  379. $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
  380. $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
  381. $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
  382. $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
  383. $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
  384. $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
  385. $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
  386. $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
  387. foreach ($data['code'] as $k=>$v){
  388. $m = $k+16;
  389. $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
  390. $sheet->getCell('B'.$m)->setValue('');
  391. $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
  392. $sheet->getCell('D'.$m)->setValue('');
  393. }
  394. //文件另存
  395. $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
  396. // $filename = iconv("utf-8","gb2312",$filename);
  397. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  398. header('Content-Disposition:attachment;filename='.$filename);
  399. header('Cache-Control: max-age=0');
  400. header("Content-Type:text/html;charset=utf-8");
  401. if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
  402. {
  403. mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
  404. }
  405. $writer = IOFactory::createWriter($objexcel, 'Xlsx');
  406. $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
  407. return 'uploads/export/'.$filename;
  408. }
  409. /**
  410. * 汇总数据 (apply)
  411. */
  412. public function apply()
  413. {
  414. $mongo = \think\Db::connect('mongodb');
  415. $this->request->filter(['strip_tags', 'trim']);
  416. if (false === $this->request->isAjax()) {
  417. return $this->view->fetch();
  418. }
  419. if ($this->request->request('keyField')) {
  420. return $this->selectpage();
  421. }
  422. // 如果是保存编辑请求
  423. if ($this->request->isPost() && $this->request->post('data')) {
  424. return $this->saveEdits();
  425. }
  426. // 接收参数
  427. $userinfo = Session::get('admin');
  428. $where = [
  429. 'delete_time' => ''
  430. ];
  431. $filter = input('filter');
  432. $filter = json_decode($filter, true);
  433. // 时间范围查询
  434. if (isset($filter['create_time'])) {
  435. $begin = substr($filter['create_time'], 0, 19);
  436. $end = substr($filter['create_time'], 22);
  437. $begin = strtotime($begin);
  438. $end = strtotime($end);
  439. $where['create_time'] = ['between', [$begin, $end]];
  440. }
  441. $order = input('order') ?: 'desc';
  442. $offset = (int)input('offset', 0);
  443. $limit = (int)input('limit', 20);
  444. // 总数统计
  445. $total = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
  446. ->where($where)
  447. ->count([]);
  448. // 列表数据
  449. $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
  450. ->where($where)
  451. ->order('_id', $order)
  452. ->skip($offset)
  453. ->limit($limit)
  454. ->select();
  455. $list = [];
  456. foreach ($bach_list as $k => $v) {
  457. $list[$k]['bach_ids'] = $v['bach_ids'];
  458. $list[$k]['total_quantity'] = $v['total_quantity'];
  459. $list[$k]['total_boxes'] = $v['total_boxes'];
  460. $list[$k]['total_pallets'] = $v['total_pallets'];
  461. $list[$k]['product_count'] = $v['product_count'];
  462. $list[$k]['sys_rq'] = $v['sys_rq'];
  463. $list[$k]['created_at'] = $v['created_at'];
  464. $list[$k]['id'] = (string)$v['_id']; // ✅ 安全转换 ObjectId
  465. }
  466. return json(['total' => $total, 'rows' => $list]);
  467. }
  468. /**
  469. * 明细数据 (通过 bach_ids 获取)
  470. */
  471. public function bachids()
  472. {
  473. $mongo = \think\Db::connect('mongodb');
  474. $this->request->filter(['strip_tags', 'trim']);
  475. if (false === $this->request->isAjax()) {
  476. return $this->view->fetch();
  477. }
  478. $userinfo = Session::get('admin');
  479. $bach_ids = input('bach_ids');
  480. $where = [
  481. 'bach_ids' => $bach_ids,
  482. 'delete_time' => ''
  483. ];
  484. $filter = input('filter');
  485. $filter = json_decode($filter, true);
  486. if (isset($filter['matter_name'])) {
  487. $where['matter_no'] = $filter['matter_name'];
  488. }
  489. if (isset($filter['create_time'])) {
  490. $begin = substr($filter['create_time'], 0, 19);
  491. $end = substr($filter['create_time'], 22);
  492. $begin = strtotime($begin);
  493. $end = strtotime($end);
  494. $where['create_time'] = ['between', [$begin, $end]];
  495. }
  496. $order = input('order') ?: 'desc';
  497. $offset = (int)input('offset', 0);
  498. $limit = (int)input('limit', 20);
  499. $total = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
  500. ->where($where)
  501. ->count([]);
  502. $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
  503. ->where($where)
  504. // ->order('_id', $order)
  505. ->skip($offset)
  506. ->limit($limit)
  507. ->select();
  508. $list = [];
  509. foreach ($bach_list as $k => $v) {
  510. $list[$k]['pallet'] = $v['pallet'];
  511. $list[$k]['pallet_sequence'] = $v['pallet_sequence'];
  512. $list[$k]['pallet_type'] = $v['pallet_type'];
  513. $list[$k]['matter_no'] = $v['matter_no'];
  514. $list[$k]['order_ddbh'] = $v['order_ddbh'];
  515. $list[$k]['cpbm'] = $v['cpbm'];
  516. $list[$k]['matter_name'] = $v['matter_name'];
  517. $list[$k]['remark'] = $v['remark'];
  518. $list[$k]['num'] = $v['num'];
  519. $list[$k]['small_num'] = $v['small_num'];
  520. $list[$k]['tray_num'] = $v['tray_num'];
  521. $list[$k]['box_num'] = $v['box_num'];
  522. $list[$k]['total_boxes'] = $v['total_boxes'];
  523. $list[$k]['large_num'] = $v['large_num'];
  524. $list[$k]['larger_num'] = $v['larger_num'];
  525. $list[$k]['pallet_height'] = $v['pallet_height'];
  526. $list[$k]['pallet_length'] = $v['pallet_length'];
  527. $list[$k]['pallet_width'] = $v['pallet_width'];
  528. $list[$k]['created_at'] = $v['created_at'];
  529. $list[$k]['id'] = (string)$v['_id'];
  530. }
  531. return json(['total' => $total, 'rows' => $list]);
  532. }
  533. /**
  534. * 保存编辑的数据(带重新计算逻辑)
  535. */
  536. public function apply_update()
  537. {
  538. $this->request->filter(['strip_tags', 'trim']);
  539. if (!$this->request->isPost()) {
  540. $this->error('请求错误');
  541. }
  542. $userinfo = Session::get('admin');
  543. $tableName = $userinfo['company'] . '_' . "qcode_bach";
  544. $qcode_bachsummary = $userinfo['company'] . '_' . "qcode_bachsummary";
  545. $id = $this->request->post('id');
  546. $field = $this->request->post('field');
  547. $value = $this->request->post('value');
  548. // 允许前端编辑的字段
  549. $allowedFields = ['pallet', 'num', 'small_num', 'tray_num', 'box_num', 'remark', 'pallet_length', 'pallet_width'];
  550. if (!in_array($field, $allowedFields)) {
  551. $this->error('该字段不允许编辑');
  552. }
  553. $mongo = \think\Db::connect('mongodb');
  554. $bach = $mongo->name($tableName)->where('_id', new \MongoDB\BSON\ObjectId($id))->find();
  555. if (!$bach) {
  556. $this->error('数据未找到');
  557. }
  558. // 更新字段值
  559. $bach[$field] = is_numeric($value) ? (int)$value : $value;
  560. // 触发联动计算的字段
  561. $recalcFields = ['num', 'small_num', 'tray_num', 'box_num', 'pallet'];
  562. if (in_array($field, $recalcFields)) {
  563. $num = (int)$bach['num'];
  564. $small_num = (int)$bach['small_num'];
  565. $tray_num = (int)$bach['tray_num'];
  566. $box_num = (int)$bach['box_num'];
  567. // 安全判断避免除以 0
  568. if ($small_num <= 0 || $tray_num <= 0 || $box_num <= 0) {
  569. $this->error('数据错误:请确保每箱、每层、每托数值均大于 0');
  570. }
  571. // 重新计算
  572. $total_boxes = $tray_num * $box_num;
  573. $larger_num = ceil($num / $small_num);
  574. $large_num = ceil($larger_num / $total_boxes);
  575. $pallet_range = '1-' . $large_num;
  576. $bach['total_boxes'] = $total_boxes;
  577. $bach['larger_num'] = $larger_num;
  578. $bach['large_num'] = $large_num;
  579. $bach['pallet'] = $pallet_range; // ✳️ pallet 联动更新
  580. }
  581. // 整理更新字段(去除 _id)
  582. $updateData = [];
  583. foreach ($bach as $k => $v) {
  584. if ($k != '_id') {
  585. $updateData[$k] = $v;
  586. }
  587. }
  588. $result = $mongo->name($tableName)
  589. ->where('_id', new \MongoDB\BSON\ObjectId($id))
  590. ->update($updateData);
  591. // $batchRecords = $mongo->name($tableName)
  592. // ->where('bach_ids', $bach['bach_ids']) // 这里使用字符串匹配,不是ObjectId
  593. // ->select();
  594. //
  595. // $total_quantity = 0;
  596. // $total_boxes_sum = 0;
  597. // $total_pallets_sum = 0;
  598. // foreach ($batchRecords as $record) {
  599. // $total_quantity += (int)$record['num'];
  600. // $total_boxes_sum += (int)$record['larger_num'];
  601. // $total_pallets_sum += (int)$record['large_num'];
  602. // }
  603. //echo "<pre>";
  604. //print_r($bach['bach_ids']);
  605. //echo "<pre>";die;
  606. // $summaryUpdate = $mongo->name($qcode_bachsummary)
  607. // ->where('_id', new \MongoDB\BSON\ObjectId($bach['bach_ids'])) // 这里也要根据实际字段类型处理
  608. // ->find();
  609. // echo "<pre>";
  610. // print_r($summaryUpdate);
  611. // echo "<pre>";
  612. if ($result) {
  613. $this->success('更新成功');
  614. } else {
  615. $this->error('更新失败');
  616. }
  617. }
  618. /**
  619. * 批次发货删除
  620. * @param $ids
  621. * @return void
  622. */
  623. public function apply_del($ids)
  624. {
  625. $export = new QcodeExport();
  626. $userinfo = Session::get('admin');
  627. $large = new QcodeLarge();
  628. if (empty($ids)){
  629. $this->error('请求错误');
  630. }
  631. $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  632. if ($res === false){
  633. $this->error('删除失败');
  634. }
  635. $large_list = $export->where('_id',$ids)->find();
  636. $large_id = explode(',',$large_list['large_str']);
  637. foreach ($large_id as $v){
  638. $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
  639. if ($result === 0){
  640. $this->error('删除失败');
  641. }
  642. }
  643. $this->success('成功');
  644. }
  645. /**
  646. * 司机弹窗
  647. * @param $ids
  648. * @return string
  649. * @throws \think\Exception
  650. */
  651. public function goods($ids)
  652. {
  653. $this->view->assign('ids',$ids);
  654. return $this->view->fetch();
  655. }
  656. /**
  657. * 批次发货提交
  658. * @return void
  659. * @throws \think\db\exception\DataNotFoundException
  660. * @throws \think\db\exception\ModelNotFoundException
  661. * @throws \think\exception\DbException
  662. */
  663. public function apply_add()
  664. {
  665. $goods = new QcodeGoods();
  666. $export = new QcodeExport();
  667. $userinfo = Session::get('admin');
  668. if ($this->request->isAjax() === false){
  669. $this->error('请求错误');
  670. }
  671. $ids = input('ids');
  672. $order_number = input('order_number');
  673. $deliveryman = input('deliveryman');
  674. $shr_phone = input('shr_phone');
  675. $plate_number = input('plate_number');
  676. $note = input('note');
  677. // $id_list = [];
  678. // if (strpos($ids,',') === false){
  679. // $id_list[0] = $ids;
  680. // }else{
  681. // $id_list = explode(',',$ids);
  682. // }
  683. // $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
  684. $mongo = \think\Db::connect('mongodb');
  685. $collection = $userinfo['company'] . '_qcode_bach';
  686. $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
  687. $update_status = $mongo->name($bachsummary)->where('_id',$ids)->update(
  688. [
  689. 'bach_status'=>2,
  690. 'delete_time' => date('Y-m-d H:i:s')
  691. ]);
  692. if ($update_status){
  693. $bachsummary_list = $mongo->name($bachsummary)->where('_id',$ids)->select();
  694. $daterq = date('ymd',time());
  695. $data = [
  696. 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),//收货单号
  697. 'order_number' => $order_number,//订单号
  698. 'deliveryman' => $deliveryman,//订单号
  699. 'shr_phone' => $shr_phone,//电话
  700. 'plate_number' => $plate_number,//车牌号
  701. 'shrq_date' => '',
  702. 'supplier_name' => $bachsummary_list[0]['supplier_name'],//供应商名称
  703. 'supplier_id' => $userinfo['id'],//供应商名称id
  704. 'user_id' => $userinfo['id'],//用户id
  705. 'status' => 0,
  706. 'qcode_bachsummary_id' => $ids,
  707. 'export_id' => '',
  708. 'create_time' => time(),
  709. 'note' => $note,
  710. 'company_id' => $userinfo['company'],
  711. ];
  712. $data['qrcode_add'] = $this->Qrcode($data['shdh']);
  713. $res = $goods->save($data);
  714. if ($res === false){
  715. $this->error('失败');
  716. }
  717. $this->success('成功');
  718. }else{
  719. $this->error('失败');
  720. }
  721. }
  722. /**
  723. * 生成二维码
  724. * @param $url
  725. * @return string
  726. */
  727. public function Qrcode($url)
  728. {
  729. $level=2;
  730. $size=5;
  731. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  732. $errorCorrectionLevel =intval($level) ;//容错级别
  733. $matrixPointSize = intval($size);//生成图片大小
  734. //生成二维码图片
  735. $object = new \QRcode();
  736. //打开缓冲区
  737. ob_start();
  738. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  739. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  740. $imageString = base64_encode(ob_get_contents());
  741. //关闭缓冲区
  742. ob_end_clean();
  743. //把生成的base64字符串返回给前端
  744. return 'data:image/png;base64,'.$imageString;
  745. }
  746. /**
  747. * 发货单
  748. * @return string|\think\response\Json
  749. * @throws \think\Exception
  750. */
  751. public function dispatch()
  752. {
  753. $export = new QcodeGoods();
  754. $this->request->filter(['strip_tags', 'trim']);
  755. if (false === $this->request->isAjax()) {
  756. return $this->view->fetch();
  757. }
  758. if ($this->request->request('keyField')) {
  759. return $this->selectpage();
  760. }
  761. // 接收参数
  762. $userInfo = Session::get('admin');
  763. $where = [
  764. 'company_id' => $userInfo['company'],
  765. 'delete_time'=> ''
  766. ];
  767. $search = input('search');
  768. if (isset($search)){
  769. $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
  770. }
  771. $filter = input('filter');
  772. $filter = json_decode($filter,true);
  773. //订单编号查询
  774. if (isset($filter['order_number'])){
  775. $where['order_number'] =$filter['order_number'];
  776. }
  777. //所属公司查询
  778. if (isset($filter['supplier_name'])){
  779. $where['supplier_name'] = $filter['supplier_name'];
  780. }
  781. if (isset($filter['status'])){
  782. $where['status'] = (int)$filter['status'];
  783. }
  784. if (isset($filter['create_time'])){
  785. $begin = substr($filter['create_time'],0,19);
  786. $end = substr($filter['create_time'],22);
  787. $begin = strtotime($begin);
  788. $end = strtotime($end);
  789. $where['create_time'] = ['between',[$begin,$end]];
  790. }
  791. $order = input('order');
  792. $offset = input('offset');
  793. $limit = input('limit');
  794. $total = $export->where($where)->count();
  795. $export_list = $export
  796. ->order('_id',$order)
  797. ->where($where)
  798. ->whereIn('status',[0,1])
  799. ->skip($offset)
  800. ->limit($limit)
  801. ->select();
  802. $list=[];
  803. foreach ($export_list as $k=>$v) {
  804. $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
  805. $list[$k]['shdh'] = $v['shdh'];
  806. $list[$k]['order_number'] = $v['order_number'];
  807. $list[$k]['supplier_name'] = $v['supplier_name'];
  808. $list[$k]['deliveryman'] = $v['deliveryman'];
  809. $list[$k]['plate_number'] = $v['plate_number'];
  810. $list[$k]['create_time'] = $v['create_time'];
  811. $list[$k]['status'] = $v['status'];
  812. }
  813. $result = ['total'=>$total,'rows'=>$list];
  814. return json($result);
  815. }
  816. /**
  817. * 发货单打印
  818. * @return \think\response\Json|void
  819. * @throws \think\db\exception\DataNotFoundException
  820. * @throws \think\db\exception\ModelNotFoundException
  821. * @throws \think\exception\DbException
  822. */
  823. public function printqrcode()
  824. {
  825. // 1. 验证请求类型
  826. if (!$this->request->isAjax()) {
  827. return json(['code' => 0, 'msg' => '请求错误']);
  828. }
  829. try {
  830. // 2. 初始化数据库连接
  831. $mongo = \think\Db::connect('mongodb');
  832. $userinfo = Session::get('admin');
  833. if (empty($userinfo) || empty($userinfo['company'])) {
  834. return json(['code' => 0, 'msg' => '用户信息获取失败']);
  835. }
  836. // 3. 设置集合名称
  837. $goodsCollection = '_qcode_goods';
  838. $bachCollection = $userinfo['company'] . '_qcode_bach';
  839. $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  840. // 4. 获取请求参数
  841. $id = input('id');
  842. if (empty($id)) {
  843. return json(['code' => 0, 'msg' => 'ID不能为空']);
  844. }
  845. // 5. 查询发货单信息
  846. $goodList = $mongo->name($goodsCollection)
  847. ->where('_id', $id)
  848. ->find();
  849. if (empty($goodList)) {
  850. return json(['code' => 0, 'msg' => '未找到发货单数据']);
  851. }
  852. // 6. 获取批次数据
  853. if (isset($goodList['data']) && is_array($goodList['data'])) {
  854. $detailData = $goodList['data'];
  855. } else {
  856. if (empty($goodList['qcode_bachsummary_id'])) {
  857. return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  858. }
  859. $bachsummaryData = $mongo->name($bachsummaryCollection)
  860. ->where('_id', $goodList['qcode_bachsummary_id'])
  861. ->find();
  862. if (empty($bachsummaryData)) {
  863. return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  864. }
  865. $detailData = $mongo->name($bachCollection)
  866. ->where('bach_ids', $bachsummaryData['bach_ids'])
  867. ->select();
  868. }
  869. // 7. 按产品名称分组处理
  870. $productGroups = [];
  871. foreach ($detailData as $item) {
  872. $productName = $item['matter_name'];
  873. if (!isset($productGroups[$productName])) {
  874. $productGroups[$productName] = [];
  875. }
  876. $productGroups[$productName][] = $item;
  877. }
  878. // 8. 处理分组数据
  879. $groupedData = [];
  880. $totalTray = 0;
  881. $totalBox = 0;
  882. $productSummary = [];
  883. // 存储完整托盘和部分托盘
  884. $allFullPallets = [];
  885. $allPartialPallets = [];
  886. foreach ($productGroups as $productName => $items) {
  887. foreach ($items as $item) {
  888. if ($item['pallet_type'] === 'full') {
  889. $allFullPallets[] = [
  890. 'productName' => $productName,
  891. 'data' => $item
  892. ];
  893. } else {
  894. $allPartialPallets[] = [
  895. 'productName' => $productName,
  896. 'data' => $item
  897. ];
  898. }
  899. }
  900. }
  901. // 对完整托盘按产品名称和托盘序号排序
  902. usort($allFullPallets, function($a, $b) {
  903. $nameCompare = strcmp($a['productName'], $b['productName']);
  904. if ($nameCompare !== 0) {
  905. return $nameCompare;
  906. }
  907. return $a['data']['pallet_sequence'] <=> $b['data']['pallet_sequence'];
  908. });
  909. // 处理完整托盘
  910. $currentPalletNumber = 1;
  911. $currentProduct = null;
  912. $rangeStart = null;
  913. $rangeCount = 0;
  914. foreach ($allFullPallets as $index => $pallet) {
  915. if ($pallet['productName'] !== $currentProduct) {
  916. // 完成上一个产品范围
  917. if ($rangeStart !== null) {
  918. $first = $allFullPallets[$rangeStart]['data'];
  919. $groupedData[] = [
  920. 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
  921. 'matter_name' => $currentProduct,
  922. 'small_num' => $first['small_num'],
  923. 'total_boxes' => $first['boxes_in_pallet'],
  924. 'tray_num' => $first['tray_num'],
  925. 'box_num' => $first['box_num'],
  926. 'pallet_height' => $first['pallet_height'],
  927. 'pallet_length' => $first['pallet_length'],
  928. 'pallet_width' => $first['pallet_width'],
  929. 'large_num' => $rangeCount,
  930. 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
  931. 'num' => $first['num'] * $rangeCount,
  932. 'danwei' => $first['danwei']
  933. ];
  934. $totalTray += $rangeCount;
  935. $totalBox += $first['boxes_in_pallet'] * $rangeCount;
  936. if (!isset($productSummary[$currentProduct])) {
  937. $productSummary[$currentProduct] = [
  938. 'qty' => 0,
  939. 'unit' => $first['danwei']
  940. ];
  941. }
  942. $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
  943. $currentPalletNumber += $rangeCount;
  944. }
  945. // 开始新的产品范围
  946. $currentProduct = $pallet['productName'];
  947. $rangeStart = $index;
  948. $rangeCount = 1;
  949. } else {
  950. $rangeCount++;
  951. }
  952. }
  953. // 处理最后一个产品范围
  954. if ($rangeStart !== null) {
  955. $first = $allFullPallets[$rangeStart]['data'];
  956. $groupedData[] = [
  957. 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
  958. 'matter_name' => $currentProduct,
  959. 'small_num' => $first['small_num'],
  960. 'total_boxes' => $first['boxes_in_pallet'],
  961. 'tray_num' => $first['tray_num'],
  962. 'box_num' => $first['box_num'],
  963. 'pallet_height' => $first['pallet_height'],
  964. 'pallet_length' => $first['pallet_length'],
  965. 'pallet_width' => $first['pallet_width'],
  966. 'large_num' => $rangeCount,
  967. 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
  968. 'num' => $first['num'] * $rangeCount,
  969. 'danwei' => $first['danwei']
  970. ];
  971. $totalTray += $rangeCount;
  972. $totalBox += $first['boxes_in_pallet'] * $rangeCount;
  973. if (!isset($productSummary[$currentProduct])) {
  974. $productSummary[$currentProduct] = [
  975. 'qty' => 0,
  976. 'unit' => $first['danwei']
  977. ];
  978. }
  979. $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
  980. $currentPalletNumber += $rangeCount;
  981. }
  982. // 处理部分托盘 - 合并显示但分开产品信息
  983. $warning4Partial = null;
  984. $warning10Partial = null;
  985. // 查找可以合并的部分托盘
  986. foreach ($allPartialPallets as $partial) {
  987. if (strpos($partial['productName'], '警语4') !== false) {
  988. $warning4Partial = $partial;
  989. } elseif (strpos($partial['productName'], '警语10') !== false) {
  990. $warning10Partial = $partial;
  991. }
  992. }
  993. // 检查是否可以合并(7+13=20箱)
  994. if ($warning4Partial && $warning10Partial &&
  995. ($warning4Partial['data']['boxes_in_pallet'] + $warning10Partial['data']['boxes_in_pallet']) == 20) {
  996. // 警语4版本(7箱)
  997. $groupedData[] = [
  998. 'pallet' => $currentPalletNumber,
  999. 'matter_name' => $warning4Partial['productName'],
  1000. 'small_num' => $warning4Partial['data']['small_num'],
  1001. 'total_boxes' => $warning4Partial['data']['boxes_in_pallet'],
  1002. 'tray_num' => $warning4Partial['data']['tray_num'],
  1003. 'box_num' => $warning4Partial['data']['box_num'],
  1004. 'pallet_height' => $warning4Partial['data']['pallet_height'],
  1005. 'pallet_length' => $warning4Partial['data']['pallet_length'],
  1006. 'pallet_width' => $warning4Partial['data']['pallet_width'],
  1007. 'large_num' => 1,
  1008. 'larger_num' => $warning4Partial['data']['boxes_in_pallet'],
  1009. 'num' => $warning4Partial['data']['num'],
  1010. 'danwei' => $warning4Partial['data']['danwei'],
  1011. 'is_combined' => true
  1012. ];
  1013. // 警语10版本(13箱)
  1014. $groupedData[] = [
  1015. 'pallet' => $currentPalletNumber,
  1016. 'matter_name' => $warning10Partial['productName'],
  1017. 'small_num' => $warning10Partial['data']['small_num'],
  1018. 'total_boxes' => $warning10Partial['data']['boxes_in_pallet'],
  1019. 'tray_num' => $warning10Partial['data']['tray_num'],
  1020. 'box_num' => $warning10Partial['data']['box_num'],
  1021. 'pallet_height' => $warning10Partial['data']['pallet_height'],
  1022. 'pallet_length' => $warning10Partial['data']['pallet_length'],
  1023. 'pallet_width' => $warning10Partial['data']['pallet_width'],
  1024. 'large_num' => 1,
  1025. 'larger_num' => $warning10Partial['data']['boxes_in_pallet'],
  1026. 'num' => $warning10Partial['data']['num'],
  1027. 'danwei' => $warning10Partial['data']['danwei'],
  1028. 'is_combined' => true
  1029. ];
  1030. $totalTray += 1; // 只算一个托盘
  1031. $totalBox += 20; // 总箱数7+13=20
  1032. // 产品统计
  1033. foreach ([$warning4Partial, $warning10Partial] as $partial) {
  1034. if (!isset($productSummary[$partial['productName']])) {
  1035. $productSummary[$partial['productName']] = [
  1036. 'qty' => 0,
  1037. 'unit' => $partial['data']['danwei']
  1038. ];
  1039. }
  1040. $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
  1041. }
  1042. $currentPalletNumber++;
  1043. } else {
  1044. // 不能合并,单独显示每个部分托盘
  1045. foreach ($allPartialPallets as $partial) {
  1046. $groupedData[] = [
  1047. 'pallet' => $currentPalletNumber,
  1048. 'matter_name' => $partial['productName'],
  1049. 'small_num' => $partial['data']['small_num'],
  1050. 'total_boxes' => $partial['data']['boxes_in_pallet'],
  1051. 'tray_num' => $partial['data']['tray_num'],
  1052. 'box_num' => $partial['data']['box_num'],
  1053. 'pallet_height' => $partial['data']['pallet_height'],
  1054. 'pallet_length' => $partial['data']['pallet_length'],
  1055. 'pallet_width' => $partial['data']['pallet_width'],
  1056. 'large_num' => 1,
  1057. 'larger_num' => $partial['data']['boxes_in_pallet'],
  1058. 'num' => $partial['data']['num'],
  1059. 'danwei' => $partial['data']['danwei']
  1060. ];
  1061. $totalTray += 1;
  1062. $totalBox += $partial['data']['boxes_in_pallet'];
  1063. if (!isset($productSummary[$partial['productName']])) {
  1064. $productSummary[$partial['productName']] = [
  1065. 'qty' => 0,
  1066. 'unit' => $partial['data']['danwei']
  1067. ];
  1068. }
  1069. $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
  1070. $currentPalletNumber++;
  1071. }
  1072. }
  1073. // 9. 单位分类统计
  1074. $totalTao = 0;
  1075. $totalZhang = 0;
  1076. $totalGe = 0;
  1077. foreach ($productSummary as $product) {
  1078. switch ($product['unit']) {
  1079. case '套':
  1080. $totalTao += $product['qty'];
  1081. break;
  1082. case '张':
  1083. $totalZhang += $product['qty'];
  1084. break;
  1085. case '个':
  1086. $totalGe += $product['qty'];
  1087. break;
  1088. }
  1089. }
  1090. // 10. 构造返回数据
  1091. $responseData = [
  1092. '_id' => $goodList['_id'],
  1093. 'shrq_date' => date('Y-m-d'),
  1094. 'shdh' => $goodList['shdh'] ?? '',
  1095. 'order_number' => $goodList['order_number'] ?? '',
  1096. 'address' => $goodList['address'] ?? '',
  1097. 'company_id' => $goodList['company_id'] ?? 0,
  1098. 'count' => $goodList['count'] ?? 0,
  1099. 'create_time' => $goodList['create_time'] ?? '',
  1100. 'data' => $groupedData,
  1101. 'summary' => [
  1102. 'totalTray' => $totalTray,
  1103. 'totalBox' => $totalBox,
  1104. 'totalTao' => $totalTao,
  1105. 'totalZhang' => $totalZhang,
  1106. 'totalGe' => $totalGe,
  1107. 'products' => $productSummary
  1108. ],
  1109. 'delete_time' => $goodList['delete_time'] ?? '',
  1110. 'deliveryman' => $goodList['deliveryman'] ?? '',
  1111. 'export_id' => $goodList['export_id'] ?? '',
  1112. 'note' => $goodList['note'] ?? '',
  1113. 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1114. 'plate_number' => $goodList['plate_number'] ?? '',
  1115. 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1116. 'shr_phone' => $goodList['shr_phone'] ?? '',
  1117. 'status' => $goodList['status'] ?? 0,
  1118. 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1119. 'supplier_name' => $goodList['supplier_name'] ?? '',
  1120. 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1121. 'user_id' => $goodList['user_id'] ?? 0
  1122. ];
  1123. return json([
  1124. 'code' => 1,
  1125. 'data' => $responseData
  1126. ]);
  1127. } catch (\Exception $e) {
  1128. return json([
  1129. 'code' => 0,
  1130. 'msg' => '系统错误: ' . $e->getMessage()
  1131. ]);
  1132. }
  1133. }
  1134. // public function printqrcode()
  1135. // {
  1136. // // 1. 验证请求类型
  1137. // if (!$this->request->isAjax()) {
  1138. // return json(['code' => 0, 'msg' => '请求错误']);
  1139. // }
  1140. //
  1141. // try {
  1142. // // 2. 初始化数据库连接
  1143. // $mongo = \think\Db::connect('mongodb');
  1144. // $userinfo = Session::get('admin');
  1145. //
  1146. // if (empty($userinfo) || empty($userinfo['company'])) {
  1147. // return json(['code' => 0, 'msg' => '用户信息获取失败']);
  1148. // }
  1149. //
  1150. // // 3. 设置集合名称
  1151. // $goodsCollection = '_qcode_goods';
  1152. // $bachCollection = $userinfo['company'] . '_qcode_bach';
  1153. // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  1154. //
  1155. // // 4. 获取请求参数
  1156. // $id = input('id');
  1157. // if (empty($id)) {
  1158. // return json(['code' => 0, 'msg' => 'ID不能为空']);
  1159. // }
  1160. //
  1161. // // 5. 查询发货单信息
  1162. // $goodList = $mongo->name($goodsCollection)
  1163. // ->where('_id', $id)
  1164. // ->find();
  1165. //
  1166. // if (empty($goodList)) {
  1167. // return json(['code' => 0, 'msg' => '未找到发货单数据']);
  1168. // }
  1169. //
  1170. // // 6. 获取批次数据
  1171. // if (isset($goodList['data']) && is_array($goodList['data'])) {
  1172. // $detailData = $goodList['data'];
  1173. // } else {
  1174. // if (empty($goodList['qcode_bachsummary_id'])) {
  1175. // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  1176. // }
  1177. //
  1178. // $bachsummaryData = $mongo->name($bachsummaryCollection)
  1179. // ->where('_id', $goodList['qcode_bachsummary_id'])
  1180. // ->find();
  1181. //
  1182. // if (empty($bachsummaryData)) {
  1183. // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  1184. // }
  1185. //
  1186. // $detailData = $mongo->name($bachCollection)
  1187. // ->where('bach_ids', $bachsummaryData['bach_ids'])
  1188. // ->select();
  1189. // }
  1190. //
  1191. // // 7. 按产品名称和规格分组处理
  1192. // $productGroups = [];
  1193. // foreach ($detailData as $item) {
  1194. // // 创建分组键,包含产品名称和规格
  1195. // $groupKey = $item['matter_name'] . '|' .
  1196. // $item['pallet_height'] . '|' .
  1197. // $item['pallet_length'] . '|' .
  1198. // $item['pallet_width'];
  1199. //
  1200. // if (!isset($productGroups[$groupKey])) {
  1201. // $productGroups[$groupKey] = [
  1202. // 'product_name' => $item['matter_name'],
  1203. // 'height' => $item['pallet_height'],
  1204. // 'length' => $item['pallet_length'],
  1205. // 'width' => $item['pallet_width'],
  1206. // 'full' => [],
  1207. // 'partial' => []
  1208. // ];
  1209. // }
  1210. //
  1211. // if ($item['pallet_type'] === 'full') {
  1212. // $productGroups[$groupKey]['full'][] = $item;
  1213. // } else {
  1214. // $productGroups[$groupKey]['partial'][] = $item;
  1215. // }
  1216. // }
  1217. //
  1218. // // 8. 处理分组数据,实现拼托逻辑
  1219. // $groupedData = [];
  1220. // $totalTray = 0;
  1221. // $totalBox = 0;
  1222. // $productSummary = [];
  1223. //
  1224. // foreach ($productGroups as $group) {
  1225. // $productName = $group['product_name'];
  1226. // $palletSpec = $group['length'] . ' * ' . $group['width'];
  1227. //
  1228. // // 处理完整托盘
  1229. // if (!empty($group['full'])) {
  1230. // usort($group['full'], function($a, $b) {
  1231. // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
  1232. // });
  1233. //
  1234. // $firstFull = $group['full'][0];
  1235. // $lastFull = end($group['full']);
  1236. // $palletCount = count($group['full']);
  1237. // $totalBoxes = $firstFull['boxes_in_pallet'] * $palletCount;
  1238. //
  1239. // $groupedData[] = [
  1240. // 'pallet' => $firstFull['pallet_sequence'].'-'.$lastFull['pallet_sequence'],
  1241. // 'matter_name' => $productName,
  1242. // 'small_num' => $firstFull['small_num'],
  1243. // 'total_boxes' => $firstFull['boxes_in_pallet'],
  1244. // 'tray_num' => $firstFull['tray_num'],
  1245. // 'box_num' => $firstFull['box_num'],
  1246. // 'pallet_height' => $group['height'],
  1247. // 'pallet_length' => $group['length'],
  1248. // 'pallet_width' => $group['width'],
  1249. // 'large_num' => $palletCount,
  1250. // 'larger_num' => $totalBoxes,
  1251. // 'num' => $firstFull['num'] * $palletCount,
  1252. // 'danwei' => $firstFull['danwei']
  1253. // ];
  1254. //
  1255. // $totalTray += $palletCount;
  1256. // $totalBox += $totalBoxes;
  1257. //
  1258. // if (!isset($productSummary[$productName])) {
  1259. // $productSummary[$productName] = [
  1260. // 'qty' => 0,
  1261. // 'unit' => $firstFull['danwei']
  1262. // ];
  1263. // }
  1264. // $productSummary[$productName]['qty'] += $firstFull['num'] * $palletCount;
  1265. // }
  1266. //
  1267. // // 处理部分托盘 - 实现拼托逻辑
  1268. // $partialPallets = $group['partial'];
  1269. // if (!empty($partialPallets)) {
  1270. // // 按托盘序号排序
  1271. // usort($partialPallets, function($a, $b) {
  1272. // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
  1273. // });
  1274. //
  1275. // // 尝试将部分托盘合并
  1276. // $mergedPartials = [];
  1277. // $currentMerge = null;
  1278. //
  1279. // foreach ($partialPallets as $partial) {
  1280. // if ($currentMerge === null) {
  1281. // $currentMerge = $partial;
  1282. // $currentMerge['merged_count'] = 1;
  1283. // } else {
  1284. // // 检查是否可以合并(相同产品且箱子数相加不超过18)
  1285. // $canMerge = ($currentMerge['matter_name'] === $partial['matter_name']) &&
  1286. // (($currentMerge['boxes_in_pallet'] + $partial['boxes_in_pallet']) <= 18);
  1287. //
  1288. // if ($canMerge) {
  1289. // $currentMerge['boxes_in_pallet'] += $partial['boxes_in_pallet'];
  1290. // $currentMerge['num'] += $partial['num'];
  1291. // $currentMerge['merged_count']++;
  1292. // } else {
  1293. // $mergedPartials[] = $currentMerge;
  1294. // $currentMerge = $partial;
  1295. // $currentMerge['merged_count'] = 1;
  1296. // }
  1297. // }
  1298. // }
  1299. //
  1300. // if ($currentMerge !== null) {
  1301. // $mergedPartials[] = $currentMerge;
  1302. // }
  1303. //
  1304. // // 处理合并后的部分托盘
  1305. // foreach ($mergedPartials as $merged) {
  1306. // $sequenceText = $merged['merged_count'] > 1 ?
  1307. // $merged['pallet_sequence'].'-'.($merged['pallet_sequence'] + $merged['merged_count'] - 1):
  1308. // $merged['pallet_sequence'];
  1309. //
  1310. // $groupedData[] = [
  1311. // 'pallet' => $sequenceText,
  1312. // 'matter_name' => $productName,
  1313. // 'small_num' => $merged['small_num'],
  1314. // 'total_boxes' => $merged['boxes_in_pallet'],
  1315. // 'tray_num' => $merged['tray_num'],
  1316. // 'box_num' => $merged['box_num'],
  1317. // 'pallet_height' => $group['height'],
  1318. // 'pallet_length' => $group['length'],
  1319. // 'pallet_width' => $group['width'],
  1320. // 'large_num' => $merged['merged_count'],
  1321. // 'larger_num' => $merged['boxes_in_pallet'],
  1322. // 'num' => $merged['num'],
  1323. // 'danwei' => $merged['danwei']
  1324. // ];
  1325. //
  1326. // $totalTray += $merged['merged_count'];
  1327. // $totalBox += $merged['boxes_in_pallet'];
  1328. //
  1329. // if (!isset($productSummary[$productName])) {
  1330. // $productSummary[$productName] = [
  1331. // 'qty' => 0,
  1332. // 'unit' => $merged['danwei']
  1333. // ];
  1334. // }
  1335. // $productSummary[$productName]['qty'] += $merged['num'];
  1336. // }
  1337. // }
  1338. // }
  1339. //
  1340. // // 9. 单位分类统计
  1341. // $totalTao = 0;
  1342. // $totalZhang = 0;
  1343. // $totalGe = 0;
  1344. //
  1345. // foreach ($productSummary as $product) {
  1346. // switch ($product['unit']) {
  1347. // case '套':
  1348. // $totalTao += $product['qty'];
  1349. // break;
  1350. // case '张':
  1351. // $totalZhang += $product['qty'];
  1352. // break;
  1353. // case '个':
  1354. // $totalGe += $product['qty'];
  1355. // break;
  1356. // }
  1357. // }
  1358. //
  1359. // // 10. 构造返回数据
  1360. // $responseData = [
  1361. // '_id' => $goodList['_id'],
  1362. // 'shrq_date' => date('Y-m-d'),
  1363. // 'shdh' => $goodList['shdh'] ?? '',
  1364. // 'order_number' => $goodList['order_number'] ?? '',
  1365. // 'address' => $goodList['address'] ?? '',
  1366. // 'company_id' => $goodList['company_id'] ?? 0,
  1367. // 'count' => $goodList['count'] ?? 0,
  1368. // 'create_time' => $goodList['create_time'] ?? '',
  1369. // 'data' => $groupedData,
  1370. // 'summary' => [
  1371. // 'totalTray' => $totalTray,
  1372. // 'totalBox' => $totalBox,
  1373. // 'totalTao' => $totalTao,
  1374. // 'totalZhang' => $totalZhang,
  1375. // 'totalGe' => $totalGe,
  1376. // 'products' => $productSummary
  1377. // ],
  1378. // // 其他字段保持不变
  1379. // 'delete_time' => $goodList['delete_time'] ?? '',
  1380. // 'deliveryman' => $goodList['deliveryman'] ?? '',
  1381. // 'export_id' => $goodList['export_id'] ?? '',
  1382. // 'note' => $goodList['note'] ?? '',
  1383. // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1384. // 'plate_number' => $goodList['plate_number'] ?? '',
  1385. // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1386. // 'shr_phone' => $goodList['shr_phone'] ?? '',
  1387. // 'status' => $goodList['status'] ?? 0,
  1388. // 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1389. // 'supplier_name' => $goodList['supplier_name'] ?? '',
  1390. // 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1391. // 'user_id' => $goodList['user_id'] ?? 0
  1392. // ];
  1393. //
  1394. // return json([
  1395. // 'code' => 1,
  1396. // 'data' => $responseData
  1397. // ]);
  1398. //
  1399. // } catch (\Exception $e) {
  1400. // return json([
  1401. // 'code' => 0,
  1402. // 'msg' => '系统错误: ' . $e->getMessage()
  1403. // ]);
  1404. // }
  1405. // }
  1406. // public function printqrcode()
  1407. // {
  1408. // // 1. 验证请求类型
  1409. // if (!$this->request->isAjax()) {
  1410. // return json(['code' => 0, 'msg' => '请求错误']);
  1411. // }
  1412. //
  1413. // try {
  1414. // // 2. 初始化数据库连接
  1415. // $mongo = \think\Db::connect('mongodb');
  1416. // $userinfo = Session::get('admin');
  1417. //
  1418. // if (empty($userinfo) || empty($userinfo['company'])) {
  1419. // return json(['code' => 0, 'msg' => '用户信息获取失败']);
  1420. // }
  1421. //
  1422. // // 3. 设置集合名称
  1423. // $goodsCollection = '_qcode_goods';
  1424. // $bachCollection = $userinfo['company'] . '_qcode_bach';
  1425. // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  1426. //
  1427. // // 4. 获取请求参数
  1428. // $id = input('id');
  1429. // if (empty($id)) {
  1430. // return json(['code' => 0, 'msg' => 'ID不能为空']);
  1431. // }
  1432. //
  1433. // // 5. 查询发货单信息
  1434. // $goodList = $mongo->name($goodsCollection)
  1435. // ->where('_id', $id)
  1436. // ->find();
  1437. //
  1438. // if (empty($goodList)) {
  1439. // return json(['code' => 0, 'msg' => '未找到发货单数据']);
  1440. // }
  1441. //
  1442. // // 6. 获取批次数据
  1443. // if (isset($goodList['data']) && is_array($goodList['data'])) {
  1444. // $detailData = $goodList['data'];
  1445. // } else {
  1446. // if (empty($goodList['qcode_bachsummary_id'])) {
  1447. // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  1448. // }
  1449. //
  1450. // $bachsummaryData = $mongo->name($bachsummaryCollection)
  1451. // ->where('_id', $goodList['qcode_bachsummary_id'])
  1452. // ->find();
  1453. //
  1454. // if (empty($bachsummaryData)) {
  1455. // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  1456. // }
  1457. //
  1458. // $detailData = $mongo->name($bachCollection)
  1459. // ->where('bach_ids', $bachsummaryData['bach_ids'])
  1460. // ->select();
  1461. // }
  1462. //
  1463. // // 7. 按产品名称分组处理
  1464. // $productGroups = [];
  1465. // foreach ($detailData as $item) {
  1466. // $productName = $item['matter_name'];
  1467. // if (!isset($productGroups[$productName])) {
  1468. // $productGroups[$productName] = [
  1469. // 'full' => [],
  1470. // 'partial' => []
  1471. // ];
  1472. // }
  1473. //
  1474. // if ($item['pallet_type'] === 'full') {
  1475. // $productGroups[$productName]['full'][] = $item;
  1476. // } else {
  1477. // $productGroups[$productName]['partial'][] = $item;
  1478. // }
  1479. // }
  1480. //
  1481. // // 8. 处理分组数据
  1482. // $groupedData = [];
  1483. // $totalTray = 0;
  1484. // $totalBox = 0;
  1485. // $productSummary = [];
  1486. //
  1487. // foreach ($productGroups as $productName => $group) {
  1488. // // 处理完整托盘
  1489. // if (!empty($group['full'])) {
  1490. // // 按托盘序号排序
  1491. // usort($group['full'], function($a, $b) {
  1492. // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
  1493. // });
  1494. //
  1495. // $firstFull = $group['full'][0];
  1496. // $lastFull = end($group['full']);
  1497. // $palletCount = count($group['full']);
  1498. // $totalBoxes = $firstFull['boxes_in_pallet'] * $palletCount;
  1499. //
  1500. // $groupedData[] = [
  1501. // 'pallet' => $firstFull['pallet_sequence'].'-'.$lastFull['pallet_sequence'],
  1502. // 'matter_name' => $productName,
  1503. // 'small_num' => $firstFull['small_num'],
  1504. // 'total_boxes' => $firstFull['boxes_in_pallet'],
  1505. // 'tray_num' => $firstFull['tray_num'],
  1506. // 'box_num' => $firstFull['box_num'],
  1507. // 'pallet_height' => $firstFull['pallet_height'],
  1508. // 'pallet_length' => $firstFull['pallet_length'],
  1509. // 'pallet_width' => $firstFull['pallet_width'],
  1510. // 'large_num' => $palletCount,
  1511. // 'larger_num' => $totalBoxes,
  1512. // 'num' => $firstFull['num'] * $palletCount,
  1513. // 'danwei' => $firstFull['danwei']
  1514. // ];
  1515. //
  1516. // $totalTray += $palletCount;
  1517. // $totalBox += $totalBoxes;
  1518. //
  1519. // // 产品汇总
  1520. // if (!isset($productSummary[$productName])) {
  1521. // $productSummary[$productName] = [
  1522. // 'qty' => 0,
  1523. // 'unit' => $firstFull['danwei']
  1524. // ];
  1525. // }
  1526. // $productSummary[$productName]['qty'] += $firstFull['num'] * $palletCount;
  1527. // }
  1528. //
  1529. // // 处理部分托盘
  1530. // foreach ($group['partial'] as $partial) {
  1531. // $groupedData[] = [
  1532. // 'pallet' => $partial['pallet_sequence'],
  1533. // 'matter_name' => $productName,
  1534. // 'small_num' => $partial['small_num'],
  1535. // 'total_boxes' => $partial['boxes_in_pallet'],
  1536. // 'tray_num' => $partial['tray_num'],
  1537. // 'box_num' => $partial['box_num'],
  1538. // 'pallet_height' => $partial['pallet_height'],
  1539. // 'pallet_length' => $partial['pallet_length'],
  1540. // 'pallet_width' => $partial['pallet_width'],
  1541. // 'large_num' => 1,
  1542. // 'larger_num' => $partial['boxes_in_pallet'],
  1543. // 'num' => $partial['num'],
  1544. // 'danwei' => $partial['danwei']
  1545. // ];
  1546. //
  1547. // $totalTray += 1;
  1548. // $totalBox += $partial['boxes_in_pallet'];
  1549. //
  1550. // // 产品汇总
  1551. // if (!isset($productSummary[$productName])) {
  1552. // $productSummary[$productName] = [
  1553. // 'qty' => 0,
  1554. // 'unit' => $partial['danwei']
  1555. // ];
  1556. // }
  1557. // $productSummary[$productName]['qty'] += $partial['num'];
  1558. // }
  1559. // }
  1560. //
  1561. // // 9. 单位分类统计
  1562. // $totalTao = 0;
  1563. // $totalZhang = 0;
  1564. // $totalGe = 0;
  1565. //
  1566. // foreach ($productSummary as $product) {
  1567. // switch ($product['unit']) {
  1568. // case '套':
  1569. // $totalTao += $product['qty'];
  1570. // break;
  1571. // case '张':
  1572. // $totalZhang += $product['qty'];
  1573. // break;
  1574. // case '个':
  1575. // $totalGe += $product['qty'];
  1576. // break;
  1577. // }
  1578. // }
  1579. //
  1580. // // 10. 构造返回数据
  1581. // $responseData = [
  1582. // '_id' => $goodList['_id'],
  1583. // 'shrq_date' => date('Y-m-d'),
  1584. // 'shdh' => $goodList['shdh'] ?? '',
  1585. // 'order_number' => $goodList['order_number'] ?? '',
  1586. // 'address' => $goodList['address'] ?? '',
  1587. // 'company_id' => $goodList['company_id'] ?? 0,
  1588. // 'count' => $goodList['count'] ?? 0,
  1589. // 'create_time' => $goodList['create_time'] ?? '',
  1590. // 'data' => $groupedData,
  1591. // 'summary' => [
  1592. // 'totalTray' => $totalTray,
  1593. // 'totalBox' => $totalBox,
  1594. // 'totalTao' => $totalTao,
  1595. // 'totalZhang' => $totalZhang,
  1596. // 'totalGe' => $totalGe,
  1597. // 'products' => $productSummary
  1598. // ],
  1599. // // 其他字段保持不变
  1600. // 'delete_time' => $goodList['delete_time'] ?? '',
  1601. // 'deliveryman' => $goodList['deliveryman'] ?? '',
  1602. // 'export_id' => $goodList['export_id'] ?? '',
  1603. // 'note' => $goodList['note'] ?? '',
  1604. // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1605. // 'plate_number' => $goodList['plate_number'] ?? '',
  1606. // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1607. // 'shr_phone' => $goodList['shr_phone'] ?? '',
  1608. // 'status' => $goodList['status'] ?? 0,
  1609. // 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1610. // 'supplier_name' => $goodList['supplier_name'] ?? '',
  1611. // 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1612. // 'user_id' => $goodList['user_id'] ?? 0
  1613. // ];
  1614. //
  1615. // return json([
  1616. // 'code' => 1,
  1617. // 'data' => $responseData
  1618. // ]);
  1619. //
  1620. // } catch (\Exception $e) {
  1621. // return json([
  1622. // 'code' => 0,
  1623. // 'msg' => '系统错误: ' . $e->getMessage()
  1624. // ]);
  1625. // }
  1626. // }
  1627. // public function printqrcode()
  1628. // {
  1629. // // 1. 验证请求类型
  1630. // if (!$this->request->isAjax()) {
  1631. // return json(['code' => 0, 'msg' => '请求错误']);
  1632. // }
  1633. //
  1634. // try {
  1635. // // 2. 初始化数据库连接
  1636. // $mongo = \think\Db::connect('mongodb');
  1637. // $userinfo = Session::get('admin');
  1638. //
  1639. // if (empty($userinfo) || empty($userinfo['company'])) {
  1640. // return json(['code' => 0, 'msg' => '用户信息获取失败']);
  1641. // }
  1642. //
  1643. // // 3. 设置集合名称
  1644. // $goodsCollection = '_qcode_goods';
  1645. // $bachCollection = $userinfo['company'] . '_qcode_bach';
  1646. // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
  1647. //
  1648. // // 4. 获取请求参数
  1649. // $id = input('id');
  1650. // if (empty($id)) {
  1651. // return json(['code' => 0, 'msg' => 'ID不能为空']);
  1652. // }
  1653. //
  1654. // // 5. 查询发货单信息
  1655. // $goodList = $mongo->name($goodsCollection)
  1656. // ->where('_id', $id)
  1657. // ->find();
  1658. //
  1659. // if (empty($goodList)) {
  1660. // return json(['code' => 0, 'msg' => '未找到发货单数据']);
  1661. // }
  1662. //
  1663. // // 6. 检查是否需要查询批次数据(根据您的数据结构,可能已包含在data字段)
  1664. // if (isset($goodList['data']) && is_array($goodList['data'])) {
  1665. // // 如果goods数据中已包含批次数据,直接使用
  1666. // $detailData = $goodList['data'];
  1667. // } else {
  1668. // // 否则执行原有查询逻辑
  1669. // if (empty($goodList['qcode_bachsummary_id'])) {
  1670. // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
  1671. // }
  1672. //
  1673. // // 查询批次汇总数据
  1674. // $bachsummaryData = $mongo->name($bachsummaryCollection)
  1675. // ->where('_id', $goodList['qcode_bachsummary_id'])
  1676. // ->find();
  1677. //
  1678. // if (empty($bachsummaryData)) {
  1679. // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
  1680. // }
  1681. //
  1682. // // 查询批次明细数据
  1683. // $detailData = $mongo->name($bachCollection)
  1684. // ->where('bach_ids', $bachsummaryData['bach_ids'])
  1685. // ->select();
  1686. // }
  1687. //
  1688. // // 7. 构造返回数据(完全匹配您提供的格式)
  1689. // $responseData = [
  1690. // '_id' => $goodList['_id'],
  1691. // 'shrq_date' => date('Y-m-d'),
  1692. // 'shdh' => $goodList['shdh'] ?? '',
  1693. // 'order_number' => $goodList['order_number'] ?? '',
  1694. // 'address' => $goodList['address'] ?? '',
  1695. // 'company_id' => $goodList['company_id'] ?? 0,
  1696. // 'count' => $goodList['count'] ?? 0,
  1697. // 'create_time' => $goodList['create_time'] ?? '',
  1698. // 'data' => $detailData, // 批次明细数据
  1699. // 'delete_time' => $goodList['delete_time'] ?? '',
  1700. // 'deliveryman' => $goodList['deliveryman'] ?? '',
  1701. // 'export_id' => $goodList['export_id'] ?? '',
  1702. // 'note' => $goodList['note'] ?? '',
  1703. // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
  1704. // 'plate_number' => $goodList['plate_number'] ?? '',
  1705. // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
  1706. // 'shr_phone' => $goodList['shr_phone'] ?? '',
  1707. // 'status' => $goodList['status'] ?? 0,
  1708. // 'supplier_id' => $goodList['supplier_id'] ?? 0,
  1709. // 'supplier_name' => $goodList['supplier_name'] ?? '',
  1710. // 'sync_flag' => $goodList['sync_flag'] ?? 0,
  1711. // 'user_id' => $goodList['user_id'] ?? 0
  1712. // ];
  1713. //echo "<pre>";
  1714. //print_r($responseData);
  1715. //echo "<pre>";die;
  1716. // return json([
  1717. // 'code' => 1,
  1718. // 'data' => $responseData
  1719. // ]);
  1720. //
  1721. // } catch (\Exception $e) {
  1722. // // 异常处理
  1723. // return json([
  1724. // 'code' => 0,
  1725. // 'msg' => '系统错误: ' . $e->getMessage()
  1726. // ]);
  1727. // }
  1728. // }
  1729. // public function printqrcode()
  1730. // {
  1731. // $goods = new QcodeGoods();
  1732. // $export = new QcodeExport();
  1733. // $large = new QcodeLarge();
  1734. // $bach = new QcodeBach();
  1735. // $userinfo = Session::get('admin');
  1736. //
  1737. // if ($this->request->isAjax() === false) {
  1738. // $this->error('请求错误');
  1739. // }
  1740. //
  1741. // $id = input('id');
  1742. // $good_list = $goods->where('_id', $id)->find();
  1743. //
  1744. // if ($good_list) {
  1745. // if (strpos($good_list['export_id'], ',') === false) {
  1746. // $export_id[0] = $good_list['export_id'];
  1747. // } else {
  1748. // $export_id = explode(',', $good_list['export_id']);
  1749. // }
  1750. //
  1751. // $data = [];
  1752. // $motter_nos = []; // 用于存储 motter_no 字段
  1753. //
  1754. // foreach ($export_id as $k => $v) {
  1755. // $data[$k] = $export->where('_id', $v)->find();
  1756. // // 确保 $data[$k] 是一个有效的对象
  1757. // if ($data[$k] instanceof QcodeExport) {
  1758. // if (!empty($data[$k]['order_ddbh'])) {
  1759. // $motter_nos[] = $data[$k]['order_ddbh'];
  1760. // }
  1761. // }
  1762. //
  1763. // $large_str = explode(',', $data[$k]['large_str']);
  1764. // $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
  1765. // $large_num = $large_weight = 0;
  1766. //
  1767. // foreach ($large_list as $value) {
  1768. // $bach_detail = $bach->name($userinfo['company'].'_qcode_bach')->where('_id', $value['bach_id'])->find();
  1769. //
  1770. // $num = (int)$bach_detail['num']; // 总张数
  1771. // $small_num = (int)$bach_detail['small_num']; // 每箱张数
  1772. // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
  1773. //
  1774. // // 重新计算总箱数
  1775. // $larger_num = (int)ceil($num / $small_num);
  1776. //
  1777. // // 当前批次对应的总托数
  1778. // $full_pallets = floor($larger_num / $boxes_per_pallet);
  1779. // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
  1780. // $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
  1781. //
  1782. // // 当前是第几托(code中解析)
  1783. // $current_flow_str = substr($value['code'], 53, 6);
  1784. // $current_flow = (int) ltrim($current_flow_str, '0');
  1785. // $total_pallets = (int) $total_pallets;
  1786. //
  1787. // // 是否是最后一托
  1788. // $is_last = ($current_flow === $total_pallets) ? 1 : 0;
  1789. //
  1790. // // 修正箱数
  1791. // $data[$k]['larger_num'] = $boxes_per_pallet;
  1792. // if ($is_last && $last_pallet_boxes > 0) {
  1793. // $data[$k]['larger_num'] = $last_pallet_boxes;
  1794. // }
  1795. //
  1796. // $data[$k]['l_num'] += $value['l_num']; // 累加数量
  1797. // $data[$k]['l_weight'] += $value['l_weight']; // 累加重量
  1798. // }
  1799. //
  1800. // // 更新数据
  1801. //// $data[$k]['l_num'] = $large_num / 10000; // 转换数量
  1802. //// $data[$k]['l_weight'] = $large_weight; // 更新重量
  1803. //
  1804. // // 添加 motter_no 到数组
  1805. // if (!empty($data[$k]['order_ddbh'])) {
  1806. // $order_ddbh[] = $data[$k]['order_ddbh'];
  1807. // }
  1808. //
  1809. // $bach_record = $bach->name($userinfo['company'] . '_' . 'qcode_bach')->where([
  1810. // 'order_ddbh' => $data[$k]['order_ddbh'],
  1811. // 'matter_no' => $data[$k]['matter_no'],
  1812. // 'matter_name' => $data[$k]['matter_name'],
  1813. // ])->find();
  1814. //
  1815. //
  1816. // //计算当前总箱数
  1817. // $bach_id = (string)$bach_record['_id'];
  1818. // $qcode_large = $bach->name($userinfo['company'] . '_' . 'qcode_large')->where([
  1819. // 'bach_id' => $bach_id,
  1820. // 'l_status' => 1,
  1821. // ])->count();
  1822. //
  1823. // $tuo_count = $bach_record['total_boxes'] * $qcode_large;
  1824. //
  1825. // if ($bach_record) {
  1826. // $data[$k]['QcodeBach_num'] = $tuo_count * $bach_record['small_num'];
  1827. // $data[$k]['QcodeBach_danwei'] = $bach_record['danwei'];
  1828. // $data[$k]['QcodeBach_small_num'] = $bach_record['small_num'];
  1829. // $data[$k]['larger_num'] = $tuo_count;
  1830. // } else {
  1831. // $data[$k]['QcodeBach_num'] = 0;
  1832. // }
  1833. // }
  1834. //
  1835. // // 拼接 motter_no 字段
  1836. // $good_list['order_ddbh'] = implode(',', $order_ddbh);
  1837. //
  1838. // // 继续处理其他数据
  1839. // $good_list['address'] = $userinfo['company_address'];
  1840. // $good_list['data'] = $data;
  1841. // $good_list['count'] = count($data);
  1842. // $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
  1843. //
  1844. // if (empty($good_list['qrcode_add'])) {
  1845. // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  1846. // $goods->where('_id', $id)->update(['qrcode_add' => $good_list['qrcode_add']]);
  1847. // }
  1848. //
  1849. // if ($good_list['note'] === 'NULL') {
  1850. // $good_list['note'] = '';
  1851. // }
  1852. //
  1853. // // 新增代码
  1854. // $good_array = $good_list ? $good_list->toArray() : [];
  1855. // $dataList = $good_array['data'] ?? [];
  1856. //
  1857. // // 步骤1:按照 create_time 升序排序
  1858. // usort($dataList, function ($a, $b) {
  1859. // return strtotime($a['create_time']) - strtotime($b['create_time']);
  1860. // });
  1861. //
  1862. // // 步骤2:添加 large_ber 字段
  1863. // $start = 1;
  1864. // foreach ($dataList as &$item) {
  1865. // $count = intval($item['large_num']);
  1866. // $end = $start + $count - 1;
  1867. // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  1868. // $start = $end + 1;
  1869. // }
  1870. // unset($item);
  1871. //echo "<pre>";
  1872. //print_r($dataList);
  1873. //echo "<pre>";
  1874. // $good_list['data'] = $dataList;
  1875. // return json(['code' => 1, 'data' => $good_list]);
  1876. // }
  1877. // }
  1878. // public function printqrcode()
  1879. // {
  1880. // $goods = new QcodeGoods();
  1881. // $export = new QcodeExport();
  1882. // $large = new QcodeLarge();
  1883. // $userinfo = Session::get('admin');
  1884. // if ($this->request->isAjax() === false) {
  1885. // $this->error('请求错误');
  1886. // }
  1887. // $id = input('id');
  1888. // $good_list = $goods->where('_id',$id)->find();
  1889. //
  1890. // if ($good_list){
  1891. // if (strpos($good_list['export_id'],',') === false){
  1892. // $export_id[0] = $good_list['export_id'];
  1893. // }else{
  1894. // $export_id = explode(',',$good_list['export_id']);
  1895. // }
  1896. // $data = [];
  1897. // foreach ($export_id as $k=>$v){
  1898. // $data[$k] = $export->where('_id',$v)->find();
  1899. // $large_str = explode(',',$data[$k]['large_str']);
  1900. // $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
  1901. // $large_num = $large_weight = 0;
  1902. // foreach ($large_list as $value){
  1903. // $large_num = $large_num + $value['l_num'];
  1904. // $large_weight = $large_weight + $value['l_weight'];
  1905. // }
  1906. // $data[$k]['l_num'] = $large_num/10000;
  1907. // $data[$k]['l_weight'] = $large_weight;
  1908. // }
  1909. // $good_list['address'] = $userinfo['company_address'];
  1910. // $good_list['data'] = $data;
  1911. // $good_list['count'] = count($data);
  1912. // $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
  1913. // if (empty($good_list['qrcode_add'])){
  1914. // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
  1915. // $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
  1916. // }
  1917. // if ($good_list['note'] == 'NULL'){
  1918. // $good_list['note'] = '';
  1919. // }
  1920. //
  1921. // //新增代码
  1922. // $good_array = $good_list ? $good_list->toArray() : [];
  1923. // // 假设 $goodArray 是已经转为普通数组的结构
  1924. // $dataList = $good_array['data'] ?? [];
  1925. // // 步骤1:按照 create_time 升序排序
  1926. // usort($dataList, function ($a, $b) {
  1927. // return strtotime($a['create_time']) - strtotime($b['create_time']);
  1928. // });
  1929. // // 步骤2:添加 large_ber 字段
  1930. // $start = 1;
  1931. // foreach ($dataList as &$item) {
  1932. // $count = intval($item['large_num']);
  1933. // $end = $start + $count - 1;
  1934. // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
  1935. // $start = $end + 1;
  1936. // }
  1937. // unset($item);
  1938. // $good_list['data'] = $dataList;
  1939. //
  1940. // return json(['code'=>1,'data'=>$good_list]);
  1941. // }
  1942. // }
  1943. /**
  1944. * 获取辅料名称
  1945. * @return \think\response\Json
  1946. * @throws \think\db\exception\DataNotFoundException
  1947. * @throws \think\db\exception\ModelNotFoundException
  1948. * @throws \think\exception\DbException
  1949. */
  1950. public function matterName()
  1951. {
  1952. // $userinfo = Session::get('admin');
  1953. // $lager = new QcodeLarge();
  1954. // $bach = new QcodeBach();
  1955. // $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
  1956. // $matter_name = [];
  1957. // if (!empty($bach_id)){
  1958. // foreach ($bach_id as $v){
  1959. // $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
  1960. // $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
  1961. // }
  1962. // }
  1963. // $matter_name = array_unique($matter_name);
  1964. // return json($matter_name);
  1965. }
  1966. /**
  1967. * 发货单删除
  1968. * @param $ids
  1969. * @return void
  1970. */
  1971. public function dispatch_del($ids)
  1972. {
  1973. $goods = new QcodeGoods();
  1974. $export = new QcodeExport();
  1975. if (empty($ids)){
  1976. $this->error('参数错误');
  1977. }
  1978. $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
  1979. if ($res === false){
  1980. $this->error('删除失败');
  1981. }
  1982. $userinfo = Session::get('admin');
  1983. $mongo = \think\Db::connect('mongodb');
  1984. $collection = $userinfo['company'] . '_qcode_bach';
  1985. $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
  1986. $res_find = $goods->where('_id',$ids)->find();
  1987. $update_status = $mongo->name($bachsummary)->where('_id',$res_find['qcode_bachsummary_id'])->update(
  1988. [
  1989. 'bach_status'=>0,
  1990. 'delete_time' => ''
  1991. ]);
  1992. if ($update_status === false){
  1993. $this->error('删除失败');
  1994. }
  1995. // $export_list = $goods->where('_id',$ids)->find();
  1996. // $export_id = explode(',',$export_list['export_id']);
  1997. // foreach ($export_id as $v){
  1998. // $result = $export->where('_id',$v)->update(['status'=>0]);
  1999. // if ($result === false){
  2000. // $this->error('删除失败');
  2001. // }
  2002. // }
  2003. $this->success('成功');
  2004. }
  2005. /**
  2006. * 收货列表
  2007. */
  2008. public function receive()
  2009. {
  2010. //当前是否为关联查询
  2011. $this->relationSearch = false;
  2012. //设置过滤方法
  2013. $this->request->filter(['strip_tags', 'trim']);
  2014. if ($this->request->isAjax()) {
  2015. //如果发送的来源是Selectpage,则转发到Selectpage
  2016. if ($this->request->request('keyField')) {
  2017. return $this->selectpage();
  2018. }
  2019. $where = [
  2020. 'delete_time'=> ''
  2021. ];
  2022. $req = input();
  2023. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  2024. $order = $req['order'];
  2025. $offset = $req['offset'];
  2026. $limit = $req['limit'];
  2027. // 构造模糊查询条件
  2028. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  2029. // $filter = ['field' => $regex];
  2030. $filter = json_decode($req['filter'], true);
  2031. if (isset($filter['status'])){
  2032. $where['status'] = intval($filter['status']);
  2033. unset($filter['status']);
  2034. }
  2035. if (isset($filter['create_time'])){
  2036. $begin = substr($filter['create_time'],0,19);
  2037. $end = substr($filter['create_time'],22);
  2038. $begin = strtotime($begin);
  2039. $end = strtotime($end);
  2040. $where['create_time'] = ['between',[$begin,$end]];
  2041. unset($filter['create_time']);
  2042. }
  2043. foreach ($filter as $k => $v){
  2044. $where[$k] = new \MongoDB\BSON\Regex($v);
  2045. }
  2046. $goods = new QcodeGoods();
  2047. $total = $goods->where($where)->count();
  2048. $list = $goods->where($where)
  2049. ->order($sort,$order)
  2050. ->limit($limit)
  2051. ->skip($offset)
  2052. ->select();
  2053. foreach ($list as $k=>$v) {
  2054. $oid = $v['_id']->jsonSerialize();
  2055. $list[$k]['id'] = $oid['$oid'];
  2056. }
  2057. $result = array("total" => $total, "rows" => $list);
  2058. return json($result);
  2059. }
  2060. return $this->view->fetch();
  2061. }
  2062. /**
  2063. * 扫码/输入收货
  2064. */
  2065. public function receive_add()
  2066. {
  2067. $req = $this->request->param();
  2068. if(!isset($req['shdh']) || empty($req['shdh'])){
  2069. $this->error('请输入送货单号');
  2070. }
  2071. $data = [
  2072. 'shdh'=>$req['shdh'],
  2073. 'delete_time'=>'',
  2074. ];
  2075. $goods = new QcodeGoods();
  2076. $row = $goods->where($data)->find();
  2077. if (!$row) $this->error('未查询到该单号');
  2078. if ($row['status']==2) $this->error('该单号已收货');
  2079. //收货---修改status为2
  2080. $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
  2081. if ($bool){
  2082. $this->success('成功');
  2083. }else{
  2084. $this->error('失败');
  2085. }
  2086. }
  2087. /**
  2088. * 取消收货
  2089. */
  2090. public function receive_del()
  2091. {
  2092. $req = $this->request->param();
  2093. if(!isset($req['ids']) || empty($req['ids'])){
  2094. $this->error('id不能为空');
  2095. }
  2096. $data = [
  2097. '_id'=>$req['ids'],
  2098. 'delete_time'=>'',
  2099. ];
  2100. $goods = new QcodeGoods();
  2101. //收货---修改status为2
  2102. $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
  2103. if ($bool){
  2104. $this->success('成功');
  2105. }else{
  2106. $this->error('失败');
  2107. }
  2108. }
  2109. }