option('company'))->makeCurrent(); if ($this->option('all-months')) { if (! $this->option('all') || ! $this->option('existing-only') || $this->option('month')) { $this->error('--all-months 需要 --all --existing-only,且不能指定 --month'); return self::FAILURE; } $months = DB::connection('business_mirror')->table(SurchargeSettlementMatcher::TABLE)->select('bill_month') ->union(DB::connection('business_mirror')->table('courier_bill_statements')->select('bill_month')->where('status', '<>', 'confirmed')) ->orderBy('bill_month')->pluck('bill_month'); foreach ($months as $month) { $status = $this->call(self::class, ['--month' => $month, '--all' => true, '--existing-only' => true, '--refresh-rebooks' => (bool) $this->option('refresh-rebooks'), '--company' => $this->option('company')]); if ($status !== self::SUCCESS) return $status; } return self::SUCCESS; } $month=(string)($this->option('month')?:now('Asia/Shanghai')->subMonthNoOverflow()->format('Y-m')); if(!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D',$month)){$this->error('请填写有效账单月份 YYYY-MM');return self::FAILURE;} if(!$this->option('all')&&$this->option('statement')===[]&&$this->option('factory')===[]){$this->error('请选择对账单、厂家或 --all');return self::FAILURE;} if(app()->environment('production')&&!env('FINANCE_BATCH_CONTAINER')){$this->error('请通过 run-finance-batch.sh 执行。');return self::FAILURE;} $lock=Cache::lock('bill-surcharge-matching',3900);if(!$lock->get()){$this->error('附加费匹配正在运行');return self::FAILURE;} $started = microtime(true); try{ $db=DB::connection('business_mirror');$statements=[];$factories=[]; $holds=[]; if($this->option('existing-only'))foreach(app(SupplierPayableOverrideStore::class)->listRows(self::AUTO_MATCH_HOLDS,(int)$this->option('company')) as $hold){ if(($hold['enabled']??false)===true)$holds[$hold['_action_key']]=true; } if($this->option('all')||$this->option('statement')!==[]){ $statements=$db->table('courier_bill_statements')->where('bill_month',$month)->where('status','<>','confirmed') ->when($this->option('existing-only'),fn($q)=>$q->whereIn('id',$db->table(SurchargeSettlementMatcher::TABLE)->where('scope','courier')->where('bill_month',$month)->select('owner_id'))) ->when(!$this->option('all'),fn($q)=>$q->whereIn('id',array_map('intval',$this->option('statement'))))->orderBy('id')->pluck('id')->all(); foreach($statements as $id){app(CourierBillMatcher::class)->refreshSurchargeSummary((int)$id);$this->line('快递对账单 #'.$id.' 附加费已匹配');} } if($this->option('all')||$this->option('factory')!==[]){ $store=app(DropshipFreightStore::class); $factories=$db->table('dropship_bill_files')->where('bill_month',$month)->where('status','parsed')->whereNotNull('factory_id') ->when($this->option('existing-only'),fn($q)=>$q->whereIn('factory_id',$db->table('dropship_bill_surcharges')->where('bill_month',$month)->select('factory_id'))) ->when(!$this->option('all'),fn($q)=>$q->whereIn('factory_id',array_map('intval',$this->option('factory'))))->distinct()->pluck('factory_id')->all(); foreach($factories as $id){ if(isset($holds['dropship|'.$id.'|'.$month])){$this->line('厂家 #'.$id.' 已保留当前附加费,跳过自动重匹配');continue;} $state=app(DropshipConfirmation::class)->state($month,(int)$id,(int)$this->option('company')); if(($state['status']??'')==='confirmed'){$this->line('厂家 #'.$id.' 已确认,跳过');continue;} $db->transaction(function()use($db,$store,$id,$month){foreach($db->table('dropship_bill_files')->where('factory_id',$id)->where('bill_month',$month)->where('status','parsed')->get() as $file)$store->storeSurchargeRows($file,(int)$id);}); $matcher->dropship((int)$id,$month);$this->line('代发厂家 #'.$id.' 附加费已匹配'); } } if ($this->option('refresh-rebooks')) { foreach ($db->table('courier_bill_statements')->where('bill_month', $month)->where('status', '<>', 'confirmed') ->when(! $this->option('all'), fn ($q) => $q->whereIn('id', array_map('intval', $this->option('statement'))))->orderBy('id')->pluck('id') as $id) { $db->transaction(function () use ($db, $id): void { $statement = $db->table('courier_bill_statements')->where('id', $id)->lockForUpdate()->first(); if (! $statement || $statement->status === 'confirmed') return; $items = $db->table('courier_bill_summary_items')->where('statement_id', $id)->get(); foreach (app(Rebooks::class)->summaryItems((int) $id, $items) as $item) { if (! $item->manual && Rebooks::isRebookLabel($item->label)) { $db->table('courier_bill_summary_items')->where('id', $item->id)->update(['self_amount' => $item->self_amount]); } } }); } } $this->info('附加费匹配完成:'.$month.';快递对账单 '.count($statements).' 张,代发厂家 '.count($factories).' 家。'); $this->line(sprintf('耗时 %.2f 秒,PHP 峰值 %.2f MiB。', microtime(true) - $started, memory_get_peak_usage(true) / 1048576)); return self::SUCCESS; }catch(Throwable $e){report($e);$this->error('附加费匹配失败:'.$e->getMessage());return self::FAILURE;} finally{$lock->release();} } }