AgentDailySettle.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\Agent\AgentCommissionService;
  4. use App\Services\Agent\AgentReportService;
  5. use Illuminate\Console\Command;
  6. class AgentDailySettle extends Command
  7. {
  8. protected $signature = 'agent:settle {date? : Y-m-d,默认昨天} {--no-credit : 只生成佣金记录,不入账}';
  9. protected $description = '生成代理日报并结算流水/盈亏佣金';
  10. public function handle(AgentReportService $reports, AgentCommissionService $commissions): int
  11. {
  12. $date = (string) ($this->argument('date') ?: now()->subDay()->toDateString());
  13. try {
  14. $parsed = \Carbon\Carbon::createFromFormat('!Y-m-d', $date);
  15. if (!$parsed || $parsed->format('Y-m-d') !== $date) {
  16. throw new \InvalidArgumentException('结算日期必须是有效的 Y-m-d 日期');
  17. }
  18. if ($parsed->gte(now()->startOfDay())) {
  19. throw new \InvalidArgumentException('只能结算已结束的日期');
  20. }
  21. $stats = $reports->refreshDaily($date);
  22. $result = $commissions->settle($date, !$this->option('no-credit'));
  23. $this->info("代理日报 {$stats} 条,佣金新建 {$result['created']} 条,入账 {$result['credited']} 条");
  24. return self::SUCCESS;
  25. } catch (\Throwable $e) {
  26. $this->error($e->getMessage());
  27. report($e);
  28. return self::FAILURE;
  29. }
  30. }
  31. }