| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\QcodeLarge;
- use app\admin\model\QcodeSmall;
- use app\common\controller\Backend;
- use app\admin\model\QcodeProduct;
- use app\admin\model\QcodeCompany;
- use app\admin\model\QcodeBach;
- use app\admin\model\QcodeExport;
- use fast\Arr;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Exception;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use app\admin\model\QcodeGoods;
- use think\Config;
- use think\Session;
- class Deliver extends Backend
- {
- /**
- * 首页
- */
- public function index()
- {
- return $this->view->fetch();
- }
- /**
- * 大件发货
- */
- public function lager()
- {
- $company = new QcodeCompany();
- $product = new QcodeProduct();
- $large = new QcodeLarge();
- $bach = new QcodeBach();
- $small = new QcodeSmall();
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $userInfo = Session::get('admin');
- $where = ['delete_time' => ''];
- $filter = input('filter');
- $filter = json_decode($filter, true);
- $whereList = ['delete_time' => ''];
- if (isset($filter['matter_name'])) {
- $whereList['matter_no'] = $filter['matter_name'];
- }
- if (isset($filter['bach'])) {
- $whereList['bach_num'] = $filter['bach'];
- }
- if (isset($filter['manufacture_date'])) {
- $begin = substr($filter['manufacture_date'], 0, 19);
- $end = substr($filter['manufacture_date'], 22);
- $begin = (int)date('ymd', strtotime($begin));
- $end = (int)date('ymd', strtotime($end));
- $whereList['manufacture_date'] = ['between', [$begin, $end]];
- }
- // 查出相关 bach_id
- $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
- $bach_id = [];
- foreach ($bach_list as $v) {
- $id = substr(json_encode($v), 9, -2);
- $bach_id[] = $id;
- }
- // 获取分页参数
- $offset = input('offset', 0);
- $limit = input('limit', 10);
- // 获取所有大件记录用于总量判断
- $all_large_list = $large->name($userInfo['company'].'_qcode_large')
- ->where($where)
- ->where('l_status', 0)
- ->whereIn('bach_id', $bach_id)
- ->select();
- // 统计总数量
- $total = count($all_large_list);
- // 分页
- $large_list = array_slice($all_large_list, $offset, $limit);
- $list = [];
- foreach ($large_list as $k => $v) {
- $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
- // $num = (int)$bach_detail['num']; // 总张数
- // $small_num = (int)$bach_detail['small_num']; // 每箱张数
- // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
- // // 重新计算总箱数
- // $larger_num = $num / $small_num;
- // $full_pallets = floor($larger_num / $boxes_per_pallet);
- // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
- $num = (float)$bach_detail['num'];
- $small_num = (float)$bach_detail['small_num'];
- $boxes_per_pallet = (float)$bach_detail['total_boxes'];
- $larger_num = $num / $small_num;
- $full_pallets = floor($larger_num / $boxes_per_pallet);
- $last_pallet_boxes = fmod($larger_num, $boxes_per_pallet); // 真正浮点取余
- $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
- // 当前是第几托(code中解析)
- $current_flow_str = substr($v['code'], 53, 6);
- $current_flow = (int) ltrim($current_flow_str, '0');
- $total_pallets = (int) $total_pallets;
- // 是否是最后一托
- $is_last = ($current_flow === $total_pallets) ? 1 : 0;
- // 修正箱数
- $total_boxes = $boxes_per_pallet;
- if ($is_last && $last_pallet_boxes > 0) {
- // 精准保留1位小数,不做四舍五入
- $total_boxes = number_format($last_pallet_boxes, 1, '.', '');
- // $total_boxes = $last_pallet_boxes;
- }
- $list[$k] = [
- 'id' => substr(json_encode($v['_id']), 9, -2),
- 'l_flow' => $current_flow,
- 'bach' => $bach_detail['bach_num'],
- 'num' => $num,
- 'small_num' => $small_num,
- 'order_ddbh' => $bach_detail['order_ddbh'],
- 'danwei' => $bach_detail['danwei'],
- 'cpbm' => $bach_detail['cpbm'],
- 'matter_name' => $bach_detail['matter_name'],
- 'total_boxes' => $total_boxes,
- 'tray_num' => $bach_detail['tray_num'],
- 'box_num' => $bach_detail['box_num'],
- 'pallet_height' => $bach_detail['pallet_height'],
- 'pallet_length' => $bach_detail['pallet_length'],
- 'pallet_width' => $bach_detail['pallet_width'],
- 'larger_num' => $larger_num,
- 'manufacture_date' => date('Y-m-d', strtotime('20' . $bach_detail['manufacture_date'])),
- 'code' => $v['code'],
- 'is_last' => $is_last,
- ];
- }
- return json(['total' => $total, 'rows' => $list]);
- }
- /**
- * 小件列表
- * @return \think\response\Json
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function smallList()
- {
- $userinfo = Session::get('admin');
- $lager = new QcodeLarge();
- $small = new QcodeSmall();
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $filter = input('filter');
- $filter = json_decode($filter,true);
- $order = input('order');
- $offset = input('offset');
- $limit = input('limit');
- $where = [
- 'delete_time' => '',
- ];
- if (isset($filter['large_id'])){
- $where['large_id'] =$filter['large_id'];
- }
- $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
- ->where($where)
- ->skip($offset)
- ->limit($limit)
- ->select();
- $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
- $list = [];
- foreach ($smallList as $k=>$v){
- $list[$k] = [
- 'code' => $v['code'],
- 'l_flow' => $v['l_flow'],
- 'print_num' => $v['p_nums'],
- 'status' => $v['status'],
- ];
- }
- return json(['total'=>$total,'rows'=>$list]);
- }
- /**
- * 导出发货
- * @return void
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function print()
- {
- $userinfo = Session::get('admin');
- $lager = new QcodeLarge();
- $bach = new QcodeBach();
- $small = new QcodeSmall();
- $export = new QcodeExport();
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $lager_id = input('lager_id');
- if ($lager_id === ''){
- $this->error('请至少选择一个大件');
- }
- $lagerId = [];
- if (strpos($lager_id,',') === false){
- $lagerId[0] = $lager_id;
- }else{
- $lagerId = explode(',',$lager_id);
- }
- $lager_num = count($lagerId);
- $bach_id = [];
- foreach ($lagerId as $k=>$v){
- $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
- $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
- $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
- }
- $bach_id = array_unique($bach_id);
- $data = [];
- foreach ($bach_id as $k=>$v){
- $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
- }
- $data = array_unique($data);
- if (count($data)>1){
- $this->error('请选择同一产品');
- }
- $n =0;
- foreach (explode(',',$lager_id) as $kk=>$vv){
- $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
- }
- // $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
- $bachArray = $bach->name($userinfo['company'].'_'.'qcode_bach')
- ->where('_id', $bach_id[0])
- ->find();
- $bachDetail = $bachArray ? $bachArray->toArray() : [];
- if ($bachDetail['box_num'] === 1){
- $mater_type = 2;
- }else{
- $mater_type = 1;
- }
- $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
- $file_dir = $this->exportExcel($exportData);
- $row = [
- 'username' => $bachDetail['supplier_name'],//公司
- 'matter_no' => $bachDetail['matter_no'],
- 'order_ddbh' => $bachDetail['order_ddbh'],
- 'large_str' => $lager_id,
- 'status' => 0,
- 'bach_num' => $bachDetail['bach_num'],
- 'note' => '',
- 'user_id' => $userinfo['id'],
- 'create_time' => time(),
- 'file_dir' => $file_dir,
- 'company_id' => $userinfo['company'],
- 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
- 'small_num' => $n,//共包含了 N 个小件
- 'matter_name' => $bachDetail['matter_name'],//产品名称
- 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
- 'tray_num' => $bachDetail['tray_num'],//每层箱数
- 'box_num' => $bachDetail['box_num'],//每托层数
- 'pallet_height' => $bachDetail['pallet_height'],//每托高度
- 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
- 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
- 'larger_num' => $bachDetail['larger_num'],//总箱数
- ];
- if ($bachDetail['box_num'] != 1){
- $row['num'] = $bachDetail['box_num'];
- $row['mater_type'] = 1;
- }else{
- $row['num'] = $bachDetail['s_weight'];
- $row['mater_type'] = 2;
- }
- $res = $export->save($row);
- if ($res === false){
- $this->error('发货失败');
- }
- $lager = new QcodeLarge();
- $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
- $this->success('成功','',$file_dir);
- }
- /**
- * 表格数据处理
- * @param $bach_id
- * @param $large_str
- * @param $company
- * @param $large_num
- * @param $small_num
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
- {
- $bach = new QcodeBach();
- $large = new QcodeLarge();
- $small = new QcodeSmall();
- $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
- $data = [
- 'supplier_name' => $bachList['supplier_name'],
- 'supplier_no' => $bachList['supplier_code'],
- 'matter_name' => $bachList['matter_name'],
- 'matter_no' => $bachList['matter_no'],
- 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
- 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
- 'box_num' => $large_num,
- 'tray_num' => $bachList['tray_num'],
- 'small_num' => $small_num,
- 'l_reservation' => $bachList['l_reservation'],
- 's_reservation' => $bachList['s_reservation'],
- 'l_flow' => $bachList['l_flow'],
- 's_flow' => $bachList['s_flow'],
- 'l_weight' => 0,
- 's_weight' => $bachList['s_weight']/1000,
- 'code' => [],
- ];
- $large_id = explode(',', $large_str);
- foreach ($large_id as $k => $v) {
- $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
- $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
- $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
- foreach ($small_list as $value) {
- $code = [
- 'large_code' => $largeList['code'],
- 'small_code' => $value['code'],
- ];
- array_push($data['code'],$code);
- }
- }
- return $data;
- }
- /**
- * 生成excel
- * @param $data
- * @return string
- * @throws Exception
- * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
- */
- public function exportExcel($data)
- {
- $objexcel = new Spreadsheet();
- $sheet = $objexcel->getActiveSheet();
- //设置全局单元格垂直居中
- $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
- //设置全局单元格水平居中
- $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- //设置全局单元格自动换行
- $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
- //设置单元格宽度
- $sheet->getDefaultColumnDimension()->setWidth(50);
- //设置表头
- $sheet->setCellValue('A1', '文件流水号');
- $sheet->setCellValue('A2', '订单号');
- $sheet->setCellValue('A3', '供应商');
- $sheet->setCellValue('A4', '供应商代码');
- $sheet->setCellValue('A5', '物料名称');
- $sheet->setCellValue('A6', '物料代码');
- $sheet->setCellValue('A7', '生产日期');
- $sheet->setCellValue('A8', '打码日期');
- $sheet->setCellValue('A9', '大件数量');
- $sheet->setCellValue('A10', '托盘小件数量');
- $sheet->setCellValue('A11', '小件总数量');
- $sheet->setCellValue('C3', '大件预留码');
- $sheet->setCellValue('C5', '小件预留码');
- $sheet->setCellValue('C7', '大件开始流水号');
- $sheet->setCellValue('C9', '小件开始流水号');
- $sheet->setCellValue('C11', '大件重量(Kg)');
- $sheet->setCellValue('C13', '小件重量(Kg)');
- $sheet->setCellValue('A15', '大件二维码');
- $sheet->setCellValue('B15', '大件条码(预留)');
- $sheet->setCellValue('C15', '小件二维码');
- $sheet->setCellValue('D15', '小件条码(预留)');
- //插入表格数据
- $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
- $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
- $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
- $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
- $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
- $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
- $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
- $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
- $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
- $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
- $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
- $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
- $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
- $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
- $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
- foreach ($data['code'] as $k=>$v){
- $m = $k+16;
- $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
- $sheet->getCell('B'.$m)->setValue('');
- $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
- $sheet->getCell('D'.$m)->setValue('');
- }
- //文件另存
- $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
- // $filename = iconv("utf-8","gb2312",$filename);
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition:attachment;filename='.$filename);
- header('Cache-Control: max-age=0');
- header("Content-Type:text/html;charset=utf-8");
- if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
- {
- mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
- }
- $writer = IOFactory::createWriter($objexcel, 'Xlsx');
- $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
- return 'uploads/export/'.$filename;
- }
- /**
- * 汇总数据 (apply)
- */
- public function apply()
- {
- $mongo = \think\Db::connect('mongodb');
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- // 如果是保存编辑请求
- if ($this->request->isPost() && $this->request->post('data')) {
- return $this->saveEdits();
- }
- // 接收参数
- $userinfo = Session::get('admin');
- $where = [
- 'delete_time' => ''
- ];
- $filter = input('filter');
- $filter = json_decode($filter, true);
- // 时间范围查询
- if (isset($filter['create_time'])) {
- $begin = substr($filter['create_time'], 0, 19);
- $end = substr($filter['create_time'], 22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between', [$begin, $end]];
- }
- $order = input('order') ?: 'desc';
- $offset = (int)input('offset', 0);
- $limit = (int)input('limit', 20);
- // 总数统计
- $total = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
- ->where($where)
- ->count([]);
- // 列表数据
- $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bachsummary")
- ->where($where)
- ->order('_id', $order)
- ->skip($offset)
- ->limit($limit)
- ->select();
- $list = [];
- foreach ($bach_list as $k => $v) {
- $list[$k]['bach_ids'] = $v['bach_ids'];
- $list[$k]['total_quantity'] = $v['total_quantity'];
- $list[$k]['total_boxes'] = $v['total_boxes'];
- $list[$k]['total_pallets'] = $v['total_pallets'];
- $list[$k]['product_count'] = $v['product_count'];
- $list[$k]['sys_rq'] = $v['sys_rq'];
- $list[$k]['created_at'] = $v['created_at'];
- $list[$k]['id'] = (string)$v['_id']; // ✅ 安全转换 ObjectId
- }
- return json(['total' => $total, 'rows' => $list]);
- }
- /**
- * 明细数据 (通过 bach_ids 获取)
- */
- public function bachids()
- {
- $mongo = \think\Db::connect('mongodb');
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- $userinfo = Session::get('admin');
- $bach_ids = input('bach_ids');
- $where = [
- 'bach_ids' => $bach_ids,
- 'delete_time' => ''
- ];
- $filter = input('filter');
- $filter = json_decode($filter, true);
- if (isset($filter['matter_name'])) {
- $where['matter_no'] = $filter['matter_name'];
- }
- if (isset($filter['create_time'])) {
- $begin = substr($filter['create_time'], 0, 19);
- $end = substr($filter['create_time'], 22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between', [$begin, $end]];
- }
- $order = input('order') ?: 'desc';
- $offset = (int)input('offset', 0);
- $limit = (int)input('limit', 20);
- $total = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
- ->where($where)
- ->count([]);
- $bach_list = $mongo->name($userinfo['company'] . '_' . "qcode_bach")
- ->where($where)
- // ->order('_id', $order)
- ->skip($offset)
- ->limit($limit)
- ->select();
- $list = [];
- foreach ($bach_list as $k => $v) {
- $list[$k]['pallet'] = $v['pallet'];
- $list[$k]['pallet_sequence'] = $v['pallet_sequence'];
- $list[$k]['pallet_type'] = $v['pallet_type'];
- $list[$k]['matter_no'] = $v['matter_no'];
- $list[$k]['order_ddbh'] = $v['order_ddbh'];
- $list[$k]['cpbm'] = $v['cpbm'];
- $list[$k]['matter_name'] = $v['matter_name'];
- $list[$k]['remark'] = $v['remark'];
- $list[$k]['num'] = $v['num'];
- $list[$k]['small_num'] = $v['small_num'];
- $list[$k]['tray_num'] = $v['tray_num'];
- $list[$k]['box_num'] = $v['box_num'];
- $list[$k]['total_boxes'] = $v['total_boxes'];
- $list[$k]['large_num'] = $v['large_num'];
- $list[$k]['larger_num'] = $v['larger_num'];
- $list[$k]['pallet_height'] = $v['pallet_height'];
- $list[$k]['pallet_length'] = $v['pallet_length'];
- $list[$k]['pallet_width'] = $v['pallet_width'];
- $list[$k]['created_at'] = $v['created_at'];
- $list[$k]['id'] = (string)$v['_id'];
- }
- return json(['total' => $total, 'rows' => $list]);
- }
- /**
- * 保存编辑的数据(带重新计算逻辑)
- */
- public function apply_update()
- {
- $this->request->filter(['strip_tags', 'trim']);
- if (!$this->request->isPost()) {
- $this->error('请求错误');
- }
- $userinfo = Session::get('admin');
- $tableName = $userinfo['company'] . '_' . "qcode_bach";
- $qcode_bachsummary = $userinfo['company'] . '_' . "qcode_bachsummary";
- $id = $this->request->post('id');
- $field = $this->request->post('field');
- $value = $this->request->post('value');
- // 允许前端编辑的字段
- $allowedFields = ['pallet', 'num', 'small_num', 'tray_num', 'box_num', 'remark', 'pallet_length', 'pallet_width'];
- if (!in_array($field, $allowedFields)) {
- $this->error('该字段不允许编辑');
- }
- $mongo = \think\Db::connect('mongodb');
- $bach = $mongo->name($tableName)->where('_id', new \MongoDB\BSON\ObjectId($id))->find();
- if (!$bach) {
- $this->error('数据未找到');
- }
- // 更新字段值
- $bach[$field] = is_numeric($value) ? (int)$value : $value;
- // 触发联动计算的字段
- $recalcFields = ['num', 'small_num', 'tray_num', 'box_num', 'pallet'];
- if (in_array($field, $recalcFields)) {
- $num = (int)$bach['num'];
- $small_num = (int)$bach['small_num'];
- $tray_num = (int)$bach['tray_num'];
- $box_num = (int)$bach['box_num'];
- // 安全判断避免除以 0
- if ($small_num <= 0 || $tray_num <= 0 || $box_num <= 0) {
- $this->error('数据错误:请确保每箱、每层、每托数值均大于 0');
- }
- // 重新计算
- $total_boxes = $tray_num * $box_num;
- $larger_num = ceil($num / $small_num);
- $large_num = ceil($larger_num / $total_boxes);
- $pallet_range = '1-' . $large_num;
- $bach['total_boxes'] = $total_boxes;
- $bach['larger_num'] = $larger_num;
- $bach['large_num'] = $large_num;
- $bach['pallet'] = $pallet_range; // ✳️ pallet 联动更新
- }
- // 整理更新字段(去除 _id)
- $updateData = [];
- foreach ($bach as $k => $v) {
- if ($k != '_id') {
- $updateData[$k] = $v;
- }
- }
- $result = $mongo->name($tableName)
- ->where('_id', new \MongoDB\BSON\ObjectId($id))
- ->update($updateData);
- // $batchRecords = $mongo->name($tableName)
- // ->where('bach_ids', $bach['bach_ids']) // 这里使用字符串匹配,不是ObjectId
- // ->select();
- //
- // $total_quantity = 0;
- // $total_boxes_sum = 0;
- // $total_pallets_sum = 0;
- // foreach ($batchRecords as $record) {
- // $total_quantity += (int)$record['num'];
- // $total_boxes_sum += (int)$record['larger_num'];
- // $total_pallets_sum += (int)$record['large_num'];
- // }
- //echo "<pre>";
- //print_r($bach['bach_ids']);
- //echo "<pre>";die;
- // $summaryUpdate = $mongo->name($qcode_bachsummary)
- // ->where('_id', new \MongoDB\BSON\ObjectId($bach['bach_ids'])) // 这里也要根据实际字段类型处理
- // ->find();
- // echo "<pre>";
- // print_r($summaryUpdate);
- // echo "<pre>";
- if ($result) {
- $this->success('更新成功');
- } else {
- $this->error('更新失败');
- }
- }
- /**
- * 批次发货删除
- * @param $ids
- * @return void
- */
- public function apply_del($ids)
- {
- $export = new QcodeExport();
- $userinfo = Session::get('admin');
- $large = new QcodeLarge();
- if (empty($ids)){
- $this->error('请求错误');
- }
- $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
- if ($res === false){
- $this->error('删除失败');
- }
- $large_list = $export->where('_id',$ids)->find();
- $large_id = explode(',',$large_list['large_str']);
- foreach ($large_id as $v){
- $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
- if ($result === 0){
- $this->error('删除失败');
- }
- }
- $this->success('成功');
- }
- /**
- * 司机弹窗
- * @param $ids
- * @return string
- * @throws \think\Exception
- */
- public function goods($ids)
- {
- $this->view->assign('ids',$ids);
- return $this->view->fetch();
- }
- /**
- * 批次发货提交
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function apply_add()
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- $userinfo = Session::get('admin');
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $ids = input('ids');
- $order_number = input('order_number');
- $deliveryman = input('deliveryman');
- $shr_phone = input('shr_phone');
- $plate_number = input('plate_number');
- $note = input('note');
- // $id_list = [];
- // if (strpos($ids,',') === false){
- // $id_list[0] = $ids;
- // }else{
- // $id_list = explode(',',$ids);
- // }
- // $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
- $mongo = \think\Db::connect('mongodb');
- $collection = $userinfo['company'] . '_qcode_bach';
- $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
- $update_status = $mongo->name($bachsummary)->where('_id',$ids)->update(
- [
- 'bach_status'=>2,
- 'delete_time' => date('Y-m-d H:i:s')
- ]);
- if ($update_status){
- $bachsummary_list = $mongo->name($bachsummary)->where('_id',$ids)->select();
- $daterq = date('ymd',time());
- $data = [
- 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),//收货单号
- 'order_number' => $order_number,//订单号
- 'deliveryman' => $deliveryman,//订单号
- 'shr_phone' => $shr_phone,//电话
- 'plate_number' => $plate_number,//车牌号
- 'shrq_date' => '',
- 'supplier_name' => $bachsummary_list[0]['supplier_name'],//供应商名称
- 'supplier_id' => $userinfo['id'],//供应商名称id
- 'user_id' => $userinfo['id'],//用户id
- 'status' => 0,
- 'qcode_bachsummary_id' => $ids,
- 'export_id' => '',
- 'create_time' => time(),
- 'note' => $note,
- 'company_id' => $userinfo['company'],
- ];
- $data['qrcode_add'] = $this->Qrcode($data['shdh']);
- $res = $goods->save($data);
- if ($res === false){
- $this->error('失败');
- }
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- /**
- * 生成二维码
- * @param $url
- * @return string
- */
- public function Qrcode($url)
- {
- $level=2;
- $size=5;
- Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
- $errorCorrectionLevel =intval($level) ;//容错级别
- $matrixPointSize = intval($size);//生成图片大小
- //生成二维码图片
- $object = new \QRcode();
- //打开缓冲区
- ob_start();
- $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
- //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
- $imageString = base64_encode(ob_get_contents());
- //关闭缓冲区
- ob_end_clean();
- //把生成的base64字符串返回给前端
- return 'data:image/png;base64,'.$imageString;
- }
- /**
- * 发货单
- * @return string|\think\response\Json
- * @throws \think\Exception
- */
- public function dispatch()
- {
- $export = new QcodeGoods();
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- // 接收参数
- $userInfo = Session::get('admin');
- $where = [
- 'company_id' => $userInfo['company'],
- 'delete_time'=> ''
- ];
- $search = input('search');
- if (isset($search)){
- $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
- }
- $filter = input('filter');
- $filter = json_decode($filter,true);
- //订单编号查询
- if (isset($filter['order_number'])){
- $where['order_number'] =$filter['order_number'];
- }
- //所属公司查询
- if (isset($filter['supplier_name'])){
- $where['supplier_name'] = $filter['supplier_name'];
- }
- if (isset($filter['status'])){
- $where['status'] = (int)$filter['status'];
- }
- if (isset($filter['create_time'])){
- $begin = substr($filter['create_time'],0,19);
- $end = substr($filter['create_time'],22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between',[$begin,$end]];
- }
- $order = input('order');
- $offset = input('offset');
- $limit = input('limit');
- $total = $export->where($where)->count();
- $export_list = $export
- ->order('_id',$order)
- ->where($where)
- ->whereIn('status',[0,1])
- ->skip($offset)
- ->limit($limit)
- ->select();
- $list=[];
- foreach ($export_list as $k=>$v) {
- $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
- $list[$k]['shdh'] = $v['shdh'];
- $list[$k]['order_number'] = $v['order_number'];
- $list[$k]['supplier_name'] = $v['supplier_name'];
- $list[$k]['deliveryman'] = $v['deliveryman'];
- $list[$k]['plate_number'] = $v['plate_number'];
- $list[$k]['create_time'] = $v['create_time'];
- $list[$k]['status'] = $v['status'];
- }
- $result = ['total'=>$total,'rows'=>$list];
- return json($result);
- }
- /**
- * 发货单打印
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function printqrcode()
- {
- // 1. 验证请求类型
- if (!$this->request->isAjax()) {
- return json(['code' => 0, 'msg' => '请求错误']);
- }
- try {
- // 2. 初始化数据库连接
- $mongo = \think\Db::connect('mongodb');
- $userinfo = Session::get('admin');
- if (empty($userinfo) || empty($userinfo['company'])) {
- return json(['code' => 0, 'msg' => '用户信息获取失败']);
- }
- // 3. 设置集合名称
- $goodsCollection = '_qcode_goods';
- $bachCollection = $userinfo['company'] . '_qcode_bach';
- $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
- // 4. 获取请求参数
- $id = input('id');
- if (empty($id)) {
- return json(['code' => 0, 'msg' => 'ID不能为空']);
- }
- // 5. 查询发货单信息
- $goodList = $mongo->name($goodsCollection)
- ->where('_id', $id)
- ->find();
- if (empty($goodList)) {
- return json(['code' => 0, 'msg' => '未找到发货单数据']);
- }
- // 6. 获取批次数据
- if (isset($goodList['data']) && is_array($goodList['data'])) {
- $detailData = $goodList['data'];
- } else {
- if (empty($goodList['qcode_bachsummary_id'])) {
- return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
- }
- $bachsummaryData = $mongo->name($bachsummaryCollection)
- ->where('_id', $goodList['qcode_bachsummary_id'])
- ->find();
- if (empty($bachsummaryData)) {
- return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
- }
- $detailData = $mongo->name($bachCollection)
- ->where('bach_ids', $bachsummaryData['bach_ids'])
- ->select();
- }
- // 7. 按产品名称分组处理
- $productGroups = [];
- foreach ($detailData as $item) {
- $productName = $item['matter_name'];
- if (!isset($productGroups[$productName])) {
- $productGroups[$productName] = [];
- }
- $productGroups[$productName][] = $item;
- }
- // 8. 处理分组数据
- $groupedData = [];
- $totalTray = 0;
- $totalBox = 0;
- $productSummary = [];
- // 存储完整托盘和部分托盘
- $allFullPallets = [];
- $allPartialPallets = [];
- foreach ($productGroups as $productName => $items) {
- foreach ($items as $item) {
- if ($item['pallet_type'] === 'full') {
- $allFullPallets[] = [
- 'productName' => $productName,
- 'data' => $item
- ];
- } else {
- $allPartialPallets[] = [
- 'productName' => $productName,
- 'data' => $item
- ];
- }
- }
- }
- // 对完整托盘按产品名称和托盘序号排序
- usort($allFullPallets, function($a, $b) {
- $nameCompare = strcmp($a['productName'], $b['productName']);
- if ($nameCompare !== 0) {
- return $nameCompare;
- }
- return $a['data']['pallet_sequence'] <=> $b['data']['pallet_sequence'];
- });
- // 处理完整托盘
- $currentPalletNumber = 1;
- $currentProduct = null;
- $rangeStart = null;
- $rangeCount = 0;
- foreach ($allFullPallets as $index => $pallet) {
- if ($pallet['productName'] !== $currentProduct) {
- // 完成上一个产品范围
- if ($rangeStart !== null) {
- $first = $allFullPallets[$rangeStart]['data'];
- $groupedData[] = [
- 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
- 'matter_name' => $currentProduct,
- 'small_num' => $first['small_num'],
- 'total_boxes' => $first['boxes_in_pallet'],
- 'tray_num' => $first['tray_num'],
- 'box_num' => $first['box_num'],
- 'pallet_height' => $first['pallet_height'],
- 'pallet_length' => $first['pallet_length'],
- 'pallet_width' => $first['pallet_width'],
- 'large_num' => $rangeCount,
- 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
- 'num' => $first['num'] * $rangeCount,
- 'danwei' => $first['danwei']
- ];
- $totalTray += $rangeCount;
- $totalBox += $first['boxes_in_pallet'] * $rangeCount;
- if (!isset($productSummary[$currentProduct])) {
- $productSummary[$currentProduct] = [
- 'qty' => 0,
- 'unit' => $first['danwei']
- ];
- }
- $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
- $currentPalletNumber += $rangeCount;
- }
- // 开始新的产品范围
- $currentProduct = $pallet['productName'];
- $rangeStart = $index;
- $rangeCount = 1;
- } else {
- $rangeCount++;
- }
- }
- // 处理最后一个产品范围
- if ($rangeStart !== null) {
- $first = $allFullPallets[$rangeStart]['data'];
- $groupedData[] = [
- 'pallet' => $currentPalletNumber . '-' . ($currentPalletNumber + $rangeCount - 1),
- 'matter_name' => $currentProduct,
- 'small_num' => $first['small_num'],
- 'total_boxes' => $first['boxes_in_pallet'],
- 'tray_num' => $first['tray_num'],
- 'box_num' => $first['box_num'],
- 'pallet_height' => $first['pallet_height'],
- 'pallet_length' => $first['pallet_length'],
- 'pallet_width' => $first['pallet_width'],
- 'large_num' => $rangeCount,
- 'larger_num' => $first['boxes_in_pallet'] * $rangeCount,
- 'num' => $first['num'] * $rangeCount,
- 'danwei' => $first['danwei']
- ];
- $totalTray += $rangeCount;
- $totalBox += $first['boxes_in_pallet'] * $rangeCount;
- if (!isset($productSummary[$currentProduct])) {
- $productSummary[$currentProduct] = [
- 'qty' => 0,
- 'unit' => $first['danwei']
- ];
- }
- $productSummary[$currentProduct]['qty'] += $first['num'] * $rangeCount;
- $currentPalletNumber += $rangeCount;
- }
- // 处理部分托盘 - 合并显示但分开产品信息
- $warning4Partial = null;
- $warning10Partial = null;
- // 查找可以合并的部分托盘
- foreach ($allPartialPallets as $partial) {
- if (strpos($partial['productName'], '警语4') !== false) {
- $warning4Partial = $partial;
- } elseif (strpos($partial['productName'], '警语10') !== false) {
- $warning10Partial = $partial;
- }
- }
- // 检查是否可以合并(7+13=20箱)
- if ($warning4Partial && $warning10Partial &&
- ($warning4Partial['data']['boxes_in_pallet'] + $warning10Partial['data']['boxes_in_pallet']) == 20) {
- // 警语4版本(7箱)
- $groupedData[] = [
- 'pallet' => $currentPalletNumber,
- 'matter_name' => $warning4Partial['productName'],
- 'small_num' => $warning4Partial['data']['small_num'],
- 'total_boxes' => $warning4Partial['data']['boxes_in_pallet'],
- 'tray_num' => $warning4Partial['data']['tray_num'],
- 'box_num' => $warning4Partial['data']['box_num'],
- 'pallet_height' => $warning4Partial['data']['pallet_height'],
- 'pallet_length' => $warning4Partial['data']['pallet_length'],
- 'pallet_width' => $warning4Partial['data']['pallet_width'],
- 'large_num' => 1,
- 'larger_num' => $warning4Partial['data']['boxes_in_pallet'],
- 'num' => $warning4Partial['data']['num'],
- 'danwei' => $warning4Partial['data']['danwei'],
- 'is_combined' => true
- ];
- // 警语10版本(13箱)
- $groupedData[] = [
- 'pallet' => $currentPalletNumber,
- 'matter_name' => $warning10Partial['productName'],
- 'small_num' => $warning10Partial['data']['small_num'],
- 'total_boxes' => $warning10Partial['data']['boxes_in_pallet'],
- 'tray_num' => $warning10Partial['data']['tray_num'],
- 'box_num' => $warning10Partial['data']['box_num'],
- 'pallet_height' => $warning10Partial['data']['pallet_height'],
- 'pallet_length' => $warning10Partial['data']['pallet_length'],
- 'pallet_width' => $warning10Partial['data']['pallet_width'],
- 'large_num' => 1,
- 'larger_num' => $warning10Partial['data']['boxes_in_pallet'],
- 'num' => $warning10Partial['data']['num'],
- 'danwei' => $warning10Partial['data']['danwei'],
- 'is_combined' => true
- ];
- $totalTray += 1; // 只算一个托盘
- $totalBox += 20; // 总箱数7+13=20
- // 产品统计
- foreach ([$warning4Partial, $warning10Partial] as $partial) {
- if (!isset($productSummary[$partial['productName']])) {
- $productSummary[$partial['productName']] = [
- 'qty' => 0,
- 'unit' => $partial['data']['danwei']
- ];
- }
- $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
- }
- $currentPalletNumber++;
- } else {
- // 不能合并,单独显示每个部分托盘
- foreach ($allPartialPallets as $partial) {
- $groupedData[] = [
- 'pallet' => $currentPalletNumber,
- 'matter_name' => $partial['productName'],
- 'small_num' => $partial['data']['small_num'],
- 'total_boxes' => $partial['data']['boxes_in_pallet'],
- 'tray_num' => $partial['data']['tray_num'],
- 'box_num' => $partial['data']['box_num'],
- 'pallet_height' => $partial['data']['pallet_height'],
- 'pallet_length' => $partial['data']['pallet_length'],
- 'pallet_width' => $partial['data']['pallet_width'],
- 'large_num' => 1,
- 'larger_num' => $partial['data']['boxes_in_pallet'],
- 'num' => $partial['data']['num'],
- 'danwei' => $partial['data']['danwei']
- ];
- $totalTray += 1;
- $totalBox += $partial['data']['boxes_in_pallet'];
- if (!isset($productSummary[$partial['productName']])) {
- $productSummary[$partial['productName']] = [
- 'qty' => 0,
- 'unit' => $partial['data']['danwei']
- ];
- }
- $productSummary[$partial['productName']]['qty'] += $partial['data']['num'];
- $currentPalletNumber++;
- }
- }
- // 9. 单位分类统计
- $totalTao = 0;
- $totalZhang = 0;
- $totalGe = 0;
- foreach ($productSummary as $product) {
- switch ($product['unit']) {
- case '套':
- $totalTao += $product['qty'];
- break;
- case '张':
- $totalZhang += $product['qty'];
- break;
- case '个':
- $totalGe += $product['qty'];
- break;
- }
- }
- // 10. 构造返回数据
- $responseData = [
- '_id' => $goodList['_id'],
- 'shrq_date' => date('Y-m-d'),
- 'shdh' => $goodList['shdh'] ?? '',
- 'order_number' => $goodList['order_number'] ?? '',
- 'address' => $goodList['address'] ?? '',
- 'company_id' => $goodList['company_id'] ?? 0,
- 'count' => $goodList['count'] ?? 0,
- 'create_time' => $goodList['create_time'] ?? '',
- 'data' => $groupedData,
- 'summary' => [
- 'totalTray' => $totalTray,
- 'totalBox' => $totalBox,
- 'totalTao' => $totalTao,
- 'totalZhang' => $totalZhang,
- 'totalGe' => $totalGe,
- 'products' => $productSummary
- ],
- 'delete_time' => $goodList['delete_time'] ?? '',
- 'deliveryman' => $goodList['deliveryman'] ?? '',
- 'export_id' => $goodList['export_id'] ?? '',
- 'note' => $goodList['note'] ?? '',
- 'order_ddbh' => $goodList['order_ddbh'] ?? '',
- 'plate_number' => $goodList['plate_number'] ?? '',
- 'qrcode_add' => $goodList['qrcode_add'] ?? '',
- 'shr_phone' => $goodList['shr_phone'] ?? '',
- 'status' => $goodList['status'] ?? 0,
- 'supplier_id' => $goodList['supplier_id'] ?? 0,
- 'supplier_name' => $goodList['supplier_name'] ?? '',
- 'sync_flag' => $goodList['sync_flag'] ?? 0,
- 'user_id' => $goodList['user_id'] ?? 0
- ];
- return json([
- 'code' => 1,
- 'data' => $responseData
- ]);
- } catch (\Exception $e) {
- return json([
- 'code' => 0,
- 'msg' => '系统错误: ' . $e->getMessage()
- ]);
- }
- }
- // public function printqrcode()
- // {
- // // 1. 验证请求类型
- // if (!$this->request->isAjax()) {
- // return json(['code' => 0, 'msg' => '请求错误']);
- // }
- //
- // try {
- // // 2. 初始化数据库连接
- // $mongo = \think\Db::connect('mongodb');
- // $userinfo = Session::get('admin');
- //
- // if (empty($userinfo) || empty($userinfo['company'])) {
- // return json(['code' => 0, 'msg' => '用户信息获取失败']);
- // }
- //
- // // 3. 设置集合名称
- // $goodsCollection = '_qcode_goods';
- // $bachCollection = $userinfo['company'] . '_qcode_bach';
- // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
- //
- // // 4. 获取请求参数
- // $id = input('id');
- // if (empty($id)) {
- // return json(['code' => 0, 'msg' => 'ID不能为空']);
- // }
- //
- // // 5. 查询发货单信息
- // $goodList = $mongo->name($goodsCollection)
- // ->where('_id', $id)
- // ->find();
- //
- // if (empty($goodList)) {
- // return json(['code' => 0, 'msg' => '未找到发货单数据']);
- // }
- //
- // // 6. 获取批次数据
- // if (isset($goodList['data']) && is_array($goodList['data'])) {
- // $detailData = $goodList['data'];
- // } else {
- // if (empty($goodList['qcode_bachsummary_id'])) {
- // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
- // }
- //
- // $bachsummaryData = $mongo->name($bachsummaryCollection)
- // ->where('_id', $goodList['qcode_bachsummary_id'])
- // ->find();
- //
- // if (empty($bachsummaryData)) {
- // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
- // }
- //
- // $detailData = $mongo->name($bachCollection)
- // ->where('bach_ids', $bachsummaryData['bach_ids'])
- // ->select();
- // }
- //
- // // 7. 按产品名称和规格分组处理
- // $productGroups = [];
- // foreach ($detailData as $item) {
- // // 创建分组键,包含产品名称和规格
- // $groupKey = $item['matter_name'] . '|' .
- // $item['pallet_height'] . '|' .
- // $item['pallet_length'] . '|' .
- // $item['pallet_width'];
- //
- // if (!isset($productGroups[$groupKey])) {
- // $productGroups[$groupKey] = [
- // 'product_name' => $item['matter_name'],
- // 'height' => $item['pallet_height'],
- // 'length' => $item['pallet_length'],
- // 'width' => $item['pallet_width'],
- // 'full' => [],
- // 'partial' => []
- // ];
- // }
- //
- // if ($item['pallet_type'] === 'full') {
- // $productGroups[$groupKey]['full'][] = $item;
- // } else {
- // $productGroups[$groupKey]['partial'][] = $item;
- // }
- // }
- //
- // // 8. 处理分组数据,实现拼托逻辑
- // $groupedData = [];
- // $totalTray = 0;
- // $totalBox = 0;
- // $productSummary = [];
- //
- // foreach ($productGroups as $group) {
- // $productName = $group['product_name'];
- // $palletSpec = $group['length'] . ' * ' . $group['width'];
- //
- // // 处理完整托盘
- // if (!empty($group['full'])) {
- // usort($group['full'], function($a, $b) {
- // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
- // });
- //
- // $firstFull = $group['full'][0];
- // $lastFull = end($group['full']);
- // $palletCount = count($group['full']);
- // $totalBoxes = $firstFull['boxes_in_pallet'] * $palletCount;
- //
- // $groupedData[] = [
- // 'pallet' => $firstFull['pallet_sequence'].'-'.$lastFull['pallet_sequence'],
- // 'matter_name' => $productName,
- // 'small_num' => $firstFull['small_num'],
- // 'total_boxes' => $firstFull['boxes_in_pallet'],
- // 'tray_num' => $firstFull['tray_num'],
- // 'box_num' => $firstFull['box_num'],
- // 'pallet_height' => $group['height'],
- // 'pallet_length' => $group['length'],
- // 'pallet_width' => $group['width'],
- // 'large_num' => $palletCount,
- // 'larger_num' => $totalBoxes,
- // 'num' => $firstFull['num'] * $palletCount,
- // 'danwei' => $firstFull['danwei']
- // ];
- //
- // $totalTray += $palletCount;
- // $totalBox += $totalBoxes;
- //
- // if (!isset($productSummary[$productName])) {
- // $productSummary[$productName] = [
- // 'qty' => 0,
- // 'unit' => $firstFull['danwei']
- // ];
- // }
- // $productSummary[$productName]['qty'] += $firstFull['num'] * $palletCount;
- // }
- //
- // // 处理部分托盘 - 实现拼托逻辑
- // $partialPallets = $group['partial'];
- // if (!empty($partialPallets)) {
- // // 按托盘序号排序
- // usort($partialPallets, function($a, $b) {
- // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
- // });
- //
- // // 尝试将部分托盘合并
- // $mergedPartials = [];
- // $currentMerge = null;
- //
- // foreach ($partialPallets as $partial) {
- // if ($currentMerge === null) {
- // $currentMerge = $partial;
- // $currentMerge['merged_count'] = 1;
- // } else {
- // // 检查是否可以合并(相同产品且箱子数相加不超过18)
- // $canMerge = ($currentMerge['matter_name'] === $partial['matter_name']) &&
- // (($currentMerge['boxes_in_pallet'] + $partial['boxes_in_pallet']) <= 18);
- //
- // if ($canMerge) {
- // $currentMerge['boxes_in_pallet'] += $partial['boxes_in_pallet'];
- // $currentMerge['num'] += $partial['num'];
- // $currentMerge['merged_count']++;
- // } else {
- // $mergedPartials[] = $currentMerge;
- // $currentMerge = $partial;
- // $currentMerge['merged_count'] = 1;
- // }
- // }
- // }
- //
- // if ($currentMerge !== null) {
- // $mergedPartials[] = $currentMerge;
- // }
- //
- // // 处理合并后的部分托盘
- // foreach ($mergedPartials as $merged) {
- // $sequenceText = $merged['merged_count'] > 1 ?
- // $merged['pallet_sequence'].'-'.($merged['pallet_sequence'] + $merged['merged_count'] - 1):
- // $merged['pallet_sequence'];
- //
- // $groupedData[] = [
- // 'pallet' => $sequenceText,
- // 'matter_name' => $productName,
- // 'small_num' => $merged['small_num'],
- // 'total_boxes' => $merged['boxes_in_pallet'],
- // 'tray_num' => $merged['tray_num'],
- // 'box_num' => $merged['box_num'],
- // 'pallet_height' => $group['height'],
- // 'pallet_length' => $group['length'],
- // 'pallet_width' => $group['width'],
- // 'large_num' => $merged['merged_count'],
- // 'larger_num' => $merged['boxes_in_pallet'],
- // 'num' => $merged['num'],
- // 'danwei' => $merged['danwei']
- // ];
- //
- // $totalTray += $merged['merged_count'];
- // $totalBox += $merged['boxes_in_pallet'];
- //
- // if (!isset($productSummary[$productName])) {
- // $productSummary[$productName] = [
- // 'qty' => 0,
- // 'unit' => $merged['danwei']
- // ];
- // }
- // $productSummary[$productName]['qty'] += $merged['num'];
- // }
- // }
- // }
- //
- // // 9. 单位分类统计
- // $totalTao = 0;
- // $totalZhang = 0;
- // $totalGe = 0;
- //
- // foreach ($productSummary as $product) {
- // switch ($product['unit']) {
- // case '套':
- // $totalTao += $product['qty'];
- // break;
- // case '张':
- // $totalZhang += $product['qty'];
- // break;
- // case '个':
- // $totalGe += $product['qty'];
- // break;
- // }
- // }
- //
- // // 10. 构造返回数据
- // $responseData = [
- // '_id' => $goodList['_id'],
- // 'shrq_date' => date('Y-m-d'),
- // 'shdh' => $goodList['shdh'] ?? '',
- // 'order_number' => $goodList['order_number'] ?? '',
- // 'address' => $goodList['address'] ?? '',
- // 'company_id' => $goodList['company_id'] ?? 0,
- // 'count' => $goodList['count'] ?? 0,
- // 'create_time' => $goodList['create_time'] ?? '',
- // 'data' => $groupedData,
- // 'summary' => [
- // 'totalTray' => $totalTray,
- // 'totalBox' => $totalBox,
- // 'totalTao' => $totalTao,
- // 'totalZhang' => $totalZhang,
- // 'totalGe' => $totalGe,
- // 'products' => $productSummary
- // ],
- // // 其他字段保持不变
- // 'delete_time' => $goodList['delete_time'] ?? '',
- // 'deliveryman' => $goodList['deliveryman'] ?? '',
- // 'export_id' => $goodList['export_id'] ?? '',
- // 'note' => $goodList['note'] ?? '',
- // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
- // 'plate_number' => $goodList['plate_number'] ?? '',
- // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
- // 'shr_phone' => $goodList['shr_phone'] ?? '',
- // 'status' => $goodList['status'] ?? 0,
- // 'supplier_id' => $goodList['supplier_id'] ?? 0,
- // 'supplier_name' => $goodList['supplier_name'] ?? '',
- // 'sync_flag' => $goodList['sync_flag'] ?? 0,
- // 'user_id' => $goodList['user_id'] ?? 0
- // ];
- //
- // return json([
- // 'code' => 1,
- // 'data' => $responseData
- // ]);
- //
- // } catch (\Exception $e) {
- // return json([
- // 'code' => 0,
- // 'msg' => '系统错误: ' . $e->getMessage()
- // ]);
- // }
- // }
- // public function printqrcode()
- // {
- // // 1. 验证请求类型
- // if (!$this->request->isAjax()) {
- // return json(['code' => 0, 'msg' => '请求错误']);
- // }
- //
- // try {
- // // 2. 初始化数据库连接
- // $mongo = \think\Db::connect('mongodb');
- // $userinfo = Session::get('admin');
- //
- // if (empty($userinfo) || empty($userinfo['company'])) {
- // return json(['code' => 0, 'msg' => '用户信息获取失败']);
- // }
- //
- // // 3. 设置集合名称
- // $goodsCollection = '_qcode_goods';
- // $bachCollection = $userinfo['company'] . '_qcode_bach';
- // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
- //
- // // 4. 获取请求参数
- // $id = input('id');
- // if (empty($id)) {
- // return json(['code' => 0, 'msg' => 'ID不能为空']);
- // }
- //
- // // 5. 查询发货单信息
- // $goodList = $mongo->name($goodsCollection)
- // ->where('_id', $id)
- // ->find();
- //
- // if (empty($goodList)) {
- // return json(['code' => 0, 'msg' => '未找到发货单数据']);
- // }
- //
- // // 6. 获取批次数据
- // if (isset($goodList['data']) && is_array($goodList['data'])) {
- // $detailData = $goodList['data'];
- // } else {
- // if (empty($goodList['qcode_bachsummary_id'])) {
- // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
- // }
- //
- // $bachsummaryData = $mongo->name($bachsummaryCollection)
- // ->where('_id', $goodList['qcode_bachsummary_id'])
- // ->find();
- //
- // if (empty($bachsummaryData)) {
- // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
- // }
- //
- // $detailData = $mongo->name($bachCollection)
- // ->where('bach_ids', $bachsummaryData['bach_ids'])
- // ->select();
- // }
- //
- // // 7. 按产品名称分组处理
- // $productGroups = [];
- // foreach ($detailData as $item) {
- // $productName = $item['matter_name'];
- // if (!isset($productGroups[$productName])) {
- // $productGroups[$productName] = [
- // 'full' => [],
- // 'partial' => []
- // ];
- // }
- //
- // if ($item['pallet_type'] === 'full') {
- // $productGroups[$productName]['full'][] = $item;
- // } else {
- // $productGroups[$productName]['partial'][] = $item;
- // }
- // }
- //
- // // 8. 处理分组数据
- // $groupedData = [];
- // $totalTray = 0;
- // $totalBox = 0;
- // $productSummary = [];
- //
- // foreach ($productGroups as $productName => $group) {
- // // 处理完整托盘
- // if (!empty($group['full'])) {
- // // 按托盘序号排序
- // usort($group['full'], function($a, $b) {
- // return $a['pallet_sequence'] <=> $b['pallet_sequence'];
- // });
- //
- // $firstFull = $group['full'][0];
- // $lastFull = end($group['full']);
- // $palletCount = count($group['full']);
- // $totalBoxes = $firstFull['boxes_in_pallet'] * $palletCount;
- //
- // $groupedData[] = [
- // 'pallet' => $firstFull['pallet_sequence'].'-'.$lastFull['pallet_sequence'],
- // 'matter_name' => $productName,
- // 'small_num' => $firstFull['small_num'],
- // 'total_boxes' => $firstFull['boxes_in_pallet'],
- // 'tray_num' => $firstFull['tray_num'],
- // 'box_num' => $firstFull['box_num'],
- // 'pallet_height' => $firstFull['pallet_height'],
- // 'pallet_length' => $firstFull['pallet_length'],
- // 'pallet_width' => $firstFull['pallet_width'],
- // 'large_num' => $palletCount,
- // 'larger_num' => $totalBoxes,
- // 'num' => $firstFull['num'] * $palletCount,
- // 'danwei' => $firstFull['danwei']
- // ];
- //
- // $totalTray += $palletCount;
- // $totalBox += $totalBoxes;
- //
- // // 产品汇总
- // if (!isset($productSummary[$productName])) {
- // $productSummary[$productName] = [
- // 'qty' => 0,
- // 'unit' => $firstFull['danwei']
- // ];
- // }
- // $productSummary[$productName]['qty'] += $firstFull['num'] * $palletCount;
- // }
- //
- // // 处理部分托盘
- // foreach ($group['partial'] as $partial) {
- // $groupedData[] = [
- // 'pallet' => $partial['pallet_sequence'],
- // 'matter_name' => $productName,
- // 'small_num' => $partial['small_num'],
- // 'total_boxes' => $partial['boxes_in_pallet'],
- // 'tray_num' => $partial['tray_num'],
- // 'box_num' => $partial['box_num'],
- // 'pallet_height' => $partial['pallet_height'],
- // 'pallet_length' => $partial['pallet_length'],
- // 'pallet_width' => $partial['pallet_width'],
- // 'large_num' => 1,
- // 'larger_num' => $partial['boxes_in_pallet'],
- // 'num' => $partial['num'],
- // 'danwei' => $partial['danwei']
- // ];
- //
- // $totalTray += 1;
- // $totalBox += $partial['boxes_in_pallet'];
- //
- // // 产品汇总
- // if (!isset($productSummary[$productName])) {
- // $productSummary[$productName] = [
- // 'qty' => 0,
- // 'unit' => $partial['danwei']
- // ];
- // }
- // $productSummary[$productName]['qty'] += $partial['num'];
- // }
- // }
- //
- // // 9. 单位分类统计
- // $totalTao = 0;
- // $totalZhang = 0;
- // $totalGe = 0;
- //
- // foreach ($productSummary as $product) {
- // switch ($product['unit']) {
- // case '套':
- // $totalTao += $product['qty'];
- // break;
- // case '张':
- // $totalZhang += $product['qty'];
- // break;
- // case '个':
- // $totalGe += $product['qty'];
- // break;
- // }
- // }
- //
- // // 10. 构造返回数据
- // $responseData = [
- // '_id' => $goodList['_id'],
- // 'shrq_date' => date('Y-m-d'),
- // 'shdh' => $goodList['shdh'] ?? '',
- // 'order_number' => $goodList['order_number'] ?? '',
- // 'address' => $goodList['address'] ?? '',
- // 'company_id' => $goodList['company_id'] ?? 0,
- // 'count' => $goodList['count'] ?? 0,
- // 'create_time' => $goodList['create_time'] ?? '',
- // 'data' => $groupedData,
- // 'summary' => [
- // 'totalTray' => $totalTray,
- // 'totalBox' => $totalBox,
- // 'totalTao' => $totalTao,
- // 'totalZhang' => $totalZhang,
- // 'totalGe' => $totalGe,
- // 'products' => $productSummary
- // ],
- // // 其他字段保持不变
- // 'delete_time' => $goodList['delete_time'] ?? '',
- // 'deliveryman' => $goodList['deliveryman'] ?? '',
- // 'export_id' => $goodList['export_id'] ?? '',
- // 'note' => $goodList['note'] ?? '',
- // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
- // 'plate_number' => $goodList['plate_number'] ?? '',
- // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
- // 'shr_phone' => $goodList['shr_phone'] ?? '',
- // 'status' => $goodList['status'] ?? 0,
- // 'supplier_id' => $goodList['supplier_id'] ?? 0,
- // 'supplier_name' => $goodList['supplier_name'] ?? '',
- // 'sync_flag' => $goodList['sync_flag'] ?? 0,
- // 'user_id' => $goodList['user_id'] ?? 0
- // ];
- //
- // return json([
- // 'code' => 1,
- // 'data' => $responseData
- // ]);
- //
- // } catch (\Exception $e) {
- // return json([
- // 'code' => 0,
- // 'msg' => '系统错误: ' . $e->getMessage()
- // ]);
- // }
- // }
- // public function printqrcode()
- // {
- // // 1. 验证请求类型
- // if (!$this->request->isAjax()) {
- // return json(['code' => 0, 'msg' => '请求错误']);
- // }
- //
- // try {
- // // 2. 初始化数据库连接
- // $mongo = \think\Db::connect('mongodb');
- // $userinfo = Session::get('admin');
- //
- // if (empty($userinfo) || empty($userinfo['company'])) {
- // return json(['code' => 0, 'msg' => '用户信息获取失败']);
- // }
- //
- // // 3. 设置集合名称
- // $goodsCollection = '_qcode_goods';
- // $bachCollection = $userinfo['company'] . '_qcode_bach';
- // $bachsummaryCollection = $userinfo['company'] . '_qcode_bachsummary';
- //
- // // 4. 获取请求参数
- // $id = input('id');
- // if (empty($id)) {
- // return json(['code' => 0, 'msg' => 'ID不能为空']);
- // }
- //
- // // 5. 查询发货单信息
- // $goodList = $mongo->name($goodsCollection)
- // ->where('_id', $id)
- // ->find();
- //
- // if (empty($goodList)) {
- // return json(['code' => 0, 'msg' => '未找到发货单数据']);
- // }
- //
- // // 6. 检查是否需要查询批次数据(根据您的数据结构,可能已包含在data字段)
- // if (isset($goodList['data']) && is_array($goodList['data'])) {
- // // 如果goods数据中已包含批次数据,直接使用
- // $detailData = $goodList['data'];
- // } else {
- // // 否则执行原有查询逻辑
- // if (empty($goodList['qcode_bachsummary_id'])) {
- // return json(['code' => 0, 'msg' => '发货单缺少批次汇总ID']);
- // }
- //
- // // 查询批次汇总数据
- // $bachsummaryData = $mongo->name($bachsummaryCollection)
- // ->where('_id', $goodList['qcode_bachsummary_id'])
- // ->find();
- //
- // if (empty($bachsummaryData)) {
- // return json(['code' => 0, 'msg' => '未找到对应的批次汇总数据']);
- // }
- //
- // // 查询批次明细数据
- // $detailData = $mongo->name($bachCollection)
- // ->where('bach_ids', $bachsummaryData['bach_ids'])
- // ->select();
- // }
- //
- // // 7. 构造返回数据(完全匹配您提供的格式)
- // $responseData = [
- // '_id' => $goodList['_id'],
- // 'shrq_date' => date('Y-m-d'),
- // 'shdh' => $goodList['shdh'] ?? '',
- // 'order_number' => $goodList['order_number'] ?? '',
- // 'address' => $goodList['address'] ?? '',
- // 'company_id' => $goodList['company_id'] ?? 0,
- // 'count' => $goodList['count'] ?? 0,
- // 'create_time' => $goodList['create_time'] ?? '',
- // 'data' => $detailData, // 批次明细数据
- // 'delete_time' => $goodList['delete_time'] ?? '',
- // 'deliveryman' => $goodList['deliveryman'] ?? '',
- // 'export_id' => $goodList['export_id'] ?? '',
- // 'note' => $goodList['note'] ?? '',
- // 'order_ddbh' => $goodList['order_ddbh'] ?? '',
- // 'plate_number' => $goodList['plate_number'] ?? '',
- // 'qrcode_add' => $goodList['qrcode_add'] ?? '',
- // 'shr_phone' => $goodList['shr_phone'] ?? '',
- // 'status' => $goodList['status'] ?? 0,
- // 'supplier_id' => $goodList['supplier_id'] ?? 0,
- // 'supplier_name' => $goodList['supplier_name'] ?? '',
- // 'sync_flag' => $goodList['sync_flag'] ?? 0,
- // 'user_id' => $goodList['user_id'] ?? 0
- // ];
- //echo "<pre>";
- //print_r($responseData);
- //echo "<pre>";die;
- // return json([
- // 'code' => 1,
- // 'data' => $responseData
- // ]);
- //
- // } catch (\Exception $e) {
- // // 异常处理
- // return json([
- // 'code' => 0,
- // 'msg' => '系统错误: ' . $e->getMessage()
- // ]);
- // }
- // }
- // public function printqrcode()
- // {
- // $goods = new QcodeGoods();
- // $export = new QcodeExport();
- // $large = new QcodeLarge();
- // $bach = new QcodeBach();
- // $userinfo = Session::get('admin');
- //
- // if ($this->request->isAjax() === false) {
- // $this->error('请求错误');
- // }
- //
- // $id = input('id');
- // $good_list = $goods->where('_id', $id)->find();
- //
- // if ($good_list) {
- // if (strpos($good_list['export_id'], ',') === false) {
- // $export_id[0] = $good_list['export_id'];
- // } else {
- // $export_id = explode(',', $good_list['export_id']);
- // }
- //
- // $data = [];
- // $motter_nos = []; // 用于存储 motter_no 字段
- //
- // foreach ($export_id as $k => $v) {
- // $data[$k] = $export->where('_id', $v)->find();
- // // 确保 $data[$k] 是一个有效的对象
- // if ($data[$k] instanceof QcodeExport) {
- // if (!empty($data[$k]['order_ddbh'])) {
- // $motter_nos[] = $data[$k]['order_ddbh'];
- // }
- // }
- //
- // $large_str = explode(',', $data[$k]['large_str']);
- // $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
- // $large_num = $large_weight = 0;
- //
- // foreach ($large_list as $value) {
- // $bach_detail = $bach->name($userinfo['company'].'_qcode_bach')->where('_id', $value['bach_id'])->find();
- //
- // $num = (int)$bach_detail['num']; // 总张数
- // $small_num = (int)$bach_detail['small_num']; // 每箱张数
- // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
- //
- // // 重新计算总箱数
- // $larger_num = (int)ceil($num / $small_num);
- //
- // // 当前批次对应的总托数
- // $full_pallets = floor($larger_num / $boxes_per_pallet);
- // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
- // $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
- //
- // // 当前是第几托(code中解析)
- // $current_flow_str = substr($value['code'], 53, 6);
- // $current_flow = (int) ltrim($current_flow_str, '0');
- // $total_pallets = (int) $total_pallets;
- //
- // // 是否是最后一托
- // $is_last = ($current_flow === $total_pallets) ? 1 : 0;
- //
- // // 修正箱数
- // $data[$k]['larger_num'] = $boxes_per_pallet;
- // if ($is_last && $last_pallet_boxes > 0) {
- // $data[$k]['larger_num'] = $last_pallet_boxes;
- // }
- //
- // $data[$k]['l_num'] += $value['l_num']; // 累加数量
- // $data[$k]['l_weight'] += $value['l_weight']; // 累加重量
- // }
- //
- // // 更新数据
- //// $data[$k]['l_num'] = $large_num / 10000; // 转换数量
- //// $data[$k]['l_weight'] = $large_weight; // 更新重量
- //
- // // 添加 motter_no 到数组
- // if (!empty($data[$k]['order_ddbh'])) {
- // $order_ddbh[] = $data[$k]['order_ddbh'];
- // }
- //
- // $bach_record = $bach->name($userinfo['company'] . '_' . 'qcode_bach')->where([
- // 'order_ddbh' => $data[$k]['order_ddbh'],
- // 'matter_no' => $data[$k]['matter_no'],
- // 'matter_name' => $data[$k]['matter_name'],
- // ])->find();
- //
- //
- // //计算当前总箱数
- // $bach_id = (string)$bach_record['_id'];
- // $qcode_large = $bach->name($userinfo['company'] . '_' . 'qcode_large')->where([
- // 'bach_id' => $bach_id,
- // 'l_status' => 1,
- // ])->count();
- //
- // $tuo_count = $bach_record['total_boxes'] * $qcode_large;
- //
- // if ($bach_record) {
- // $data[$k]['QcodeBach_num'] = $tuo_count * $bach_record['small_num'];
- // $data[$k]['QcodeBach_danwei'] = $bach_record['danwei'];
- // $data[$k]['QcodeBach_small_num'] = $bach_record['small_num'];
- // $data[$k]['larger_num'] = $tuo_count;
- // } else {
- // $data[$k]['QcodeBach_num'] = 0;
- // }
- // }
- //
- // // 拼接 motter_no 字段
- // $good_list['order_ddbh'] = implode(',', $order_ddbh);
- //
- // // 继续处理其他数据
- // $good_list['address'] = $userinfo['company_address'];
- // $good_list['data'] = $data;
- // $good_list['count'] = count($data);
- // $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
- //
- // if (empty($good_list['qrcode_add'])) {
- // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
- // $goods->where('_id', $id)->update(['qrcode_add' => $good_list['qrcode_add']]);
- // }
- //
- // if ($good_list['note'] === 'NULL') {
- // $good_list['note'] = '';
- // }
- //
- // // 新增代码
- // $good_array = $good_list ? $good_list->toArray() : [];
- // $dataList = $good_array['data'] ?? [];
- //
- // // 步骤1:按照 create_time 升序排序
- // usort($dataList, function ($a, $b) {
- // return strtotime($a['create_time']) - strtotime($b['create_time']);
- // });
- //
- // // 步骤2:添加 large_ber 字段
- // $start = 1;
- // foreach ($dataList as &$item) {
- // $count = intval($item['large_num']);
- // $end = $start + $count - 1;
- // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
- // $start = $end + 1;
- // }
- // unset($item);
- //echo "<pre>";
- //print_r($dataList);
- //echo "<pre>";
- // $good_list['data'] = $dataList;
- // return json(['code' => 1, 'data' => $good_list]);
- // }
- // }
- // public function printqrcode()
- // {
- // $goods = new QcodeGoods();
- // $export = new QcodeExport();
- // $large = new QcodeLarge();
- // $userinfo = Session::get('admin');
- // if ($this->request->isAjax() === false) {
- // $this->error('请求错误');
- // }
- // $id = input('id');
- // $good_list = $goods->where('_id',$id)->find();
- //
- // if ($good_list){
- // if (strpos($good_list['export_id'],',') === false){
- // $export_id[0] = $good_list['export_id'];
- // }else{
- // $export_id = explode(',',$good_list['export_id']);
- // }
- // $data = [];
- // foreach ($export_id as $k=>$v){
- // $data[$k] = $export->where('_id',$v)->find();
- // $large_str = explode(',',$data[$k]['large_str']);
- // $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
- // $large_num = $large_weight = 0;
- // foreach ($large_list as $value){
- // $large_num = $large_num + $value['l_num'];
- // $large_weight = $large_weight + $value['l_weight'];
- // }
- // $data[$k]['l_num'] = $large_num/10000;
- // $data[$k]['l_weight'] = $large_weight;
- // }
- // $good_list['address'] = $userinfo['company_address'];
- // $good_list['data'] = $data;
- // $good_list['count'] = count($data);
- // $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
- // if (empty($good_list['qrcode_add'])){
- // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
- // $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
- // }
- // if ($good_list['note'] == 'NULL'){
- // $good_list['note'] = '';
- // }
- //
- // //新增代码
- // $good_array = $good_list ? $good_list->toArray() : [];
- // // 假设 $goodArray 是已经转为普通数组的结构
- // $dataList = $good_array['data'] ?? [];
- // // 步骤1:按照 create_time 升序排序
- // usort($dataList, function ($a, $b) {
- // return strtotime($a['create_time']) - strtotime($b['create_time']);
- // });
- // // 步骤2:添加 large_ber 字段
- // $start = 1;
- // foreach ($dataList as &$item) {
- // $count = intval($item['large_num']);
- // $end = $start + $count - 1;
- // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
- // $start = $end + 1;
- // }
- // unset($item);
- // $good_list['data'] = $dataList;
- //
- // return json(['code'=>1,'data'=>$good_list]);
- // }
- // }
- /**
- * 获取辅料名称
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function matterName()
- {
- // $userinfo = Session::get('admin');
- // $lager = new QcodeLarge();
- // $bach = new QcodeBach();
- // $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
- // $matter_name = [];
- // if (!empty($bach_id)){
- // foreach ($bach_id as $v){
- // $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
- // $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
- // }
- // }
- // $matter_name = array_unique($matter_name);
- // return json($matter_name);
- }
- /**
- * 发货单删除
- * @param $ids
- * @return void
- */
- public function dispatch_del($ids)
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- if (empty($ids)){
- $this->error('参数错误');
- }
- $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
- if ($res === false){
- $this->error('删除失败');
- }
- $userinfo = Session::get('admin');
- $mongo = \think\Db::connect('mongodb');
- $collection = $userinfo['company'] . '_qcode_bach';
- $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
- $res_find = $goods->where('_id',$ids)->find();
- $update_status = $mongo->name($bachsummary)->where('_id',$res_find['qcode_bachsummary_id'])->update(
- [
- 'bach_status'=>0,
- 'delete_time' => ''
- ]);
- if ($update_status === false){
- $this->error('删除失败');
- }
- // $export_list = $goods->where('_id',$ids)->find();
- // $export_id = explode(',',$export_list['export_id']);
- // foreach ($export_id as $v){
- // $result = $export->where('_id',$v)->update(['status'=>0]);
- // if ($result === false){
- // $this->error('删除失败');
- // }
- // }
- $this->success('成功');
- }
- /**
- * 收货列表
- */
- public function receive()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $where = [
- 'delete_time'=> ''
- ];
- $req = input();
- $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- $order = $req['order'];
- $offset = $req['offset'];
- $limit = $req['limit'];
- // 构造模糊查询条件
- // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- // $filter = ['field' => $regex];
- $filter = json_decode($req['filter'], true);
- if (isset($filter['status'])){
- $where['status'] = intval($filter['status']);
- unset($filter['status']);
- }
- if (isset($filter['create_time'])){
- $begin = substr($filter['create_time'],0,19);
- $end = substr($filter['create_time'],22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between',[$begin,$end]];
- unset($filter['create_time']);
- }
- foreach ($filter as $k => $v){
- $where[$k] = new \MongoDB\BSON\Regex($v);
- }
- $goods = new QcodeGoods();
- $total = $goods->where($where)->count();
- $list = $goods->where($where)
- ->order($sort,$order)
- ->limit($limit)
- ->skip($offset)
- ->select();
- foreach ($list as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $list[$k]['id'] = $oid['$oid'];
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 扫码/输入收货
- */
- public function receive_add()
- {
- $req = $this->request->param();
- if(!isset($req['shdh']) || empty($req['shdh'])){
- $this->error('请输入送货单号');
- }
- $data = [
- 'shdh'=>$req['shdh'],
- 'delete_time'=>'',
- ];
- $goods = new QcodeGoods();
- $row = $goods->where($data)->find();
- if (!$row) $this->error('未查询到该单号');
- if ($row['status']==2) $this->error('该单号已收货');
- //收货---修改status为2
- $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
- if ($bool){
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- /**
- * 取消收货
- */
- public function receive_del()
- {
- $req = $this->request->param();
- if(!isset($req['ids']) || empty($req['ids'])){
- $this->error('id不能为空');
- }
- $data = [
- '_id'=>$req['ids'],
- 'delete_time'=>'',
- ];
- $goods = new QcodeGoods();
- //收货---修改status为2
- $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
- if ($bool){
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- }
|