97 lines
3.8 KiB
PHP
97 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Modules\EcommerceFinance\Services\DingInvoiceSyncService;
|
||
use Modules\EcommerceFinance\Services\DingProcessDetailMirrorSynchronizer;
|
||
use Throwable;
|
||
|
||
class SyncDingProcessDetailMirror extends Command
|
||
{
|
||
protected $signature = 'business-mirror:sync-ding-process-details
|
||
{--full : 从配置起始日期完整重建明细和付款事实}
|
||
{--business-id= : 只同步指定审批编号,多个用逗号分隔(最多 50 个)}
|
||
{--skip-approval-api : 定向同步时不调用钉钉官方审批接口}
|
||
{--skip-invoices : 不执行同轮发票增量识别}
|
||
{--dry-run : 只读取上游并校验,不写入镜像}';
|
||
|
||
protected $description = '同步 OA 流程结构化明细,并在同一批处理中增量识别发票';
|
||
|
||
public function handle(
|
||
DingProcessDetailMirrorSynchronizer $synchronizer,
|
||
DingInvoiceSyncService $invoiceSync,
|
||
): int {
|
||
$businessIds = array_values(array_filter(array_map(
|
||
'trim',
|
||
explode(',', (string) $this->option('business-id')),
|
||
)));
|
||
|
||
try {
|
||
if ($businessIds !== []) {
|
||
if ((bool) $this->option('full')) {
|
||
throw new \InvalidArgumentException('--full 不能和 --business-id 同时使用。');
|
||
}
|
||
$result = $synchronizer->syncBusinessIds(
|
||
$businessIds,
|
||
(bool) $this->option('dry-run'),
|
||
! (bool) $this->option('skip-approval-api'),
|
||
);
|
||
} else {
|
||
$result = $synchronizer->sync(
|
||
(bool) $this->option('full'),
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
}
|
||
} catch (Throwable $exception) {
|
||
$this->error('同步失败,已保留上一版共享数据:' . $exception->getMessage());
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($result['status'] === 'requires_full_sync') {
|
||
$this->warn('流程明细尚未完成结构化全量同步,请使用 --full。');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$this->info(sprintf(
|
||
'%s:%d 个审批,%d 条明细,%d 笔付款;金额合计 %.2f,银行付款合计 %.2f。',
|
||
$result['status'] === 'validated' ? '校验通过' : '同步完成',
|
||
$result['workflows'],
|
||
$result['details'],
|
||
$result['payments'],
|
||
$result['amount_total'],
|
||
$result['bank_amount_total'],
|
||
));
|
||
|
||
if ($businessIds === []
|
||
&& ! (bool) $this->option('dry-run')
|
||
&& ! (bool) $this->option('skip-invoices')
|
||
&& $result['workflows'] > 0
|
||
&& (bool) config('ding_invoice_data.sync_with_oa_process', true)) {
|
||
try {
|
||
$month = $invoiceSync->latestPendingMonth();
|
||
if ($month !== null) {
|
||
$invoiceResult = $invoiceSync->syncMonth(
|
||
$month,
|
||
(int) config('ding_invoice_data.auto_scan_limit', 5),
|
||
);
|
||
$this->info(sprintf(
|
||
'发票增量识别完成:%d 个流程,新识别 %d 张,保留 %d 张,剩余 %d 个流程。',
|
||
$invoiceResult['processed_flows'],
|
||
$invoiceResult['recognized_invoices'],
|
||
$invoiceResult['preserved_invoices'],
|
||
$invoiceResult['remaining_flows'],
|
||
));
|
||
}
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->warn('发票增量识别失败,OA 流程镜像已正常更新:' . $exception->getMessage());
|
||
}
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|