seed_mini_commissions_demo.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. require dirname(__DIR__) . '/vendor/autoload.php';
  4. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  5. \support\App::loadAllConfig(['route']);
  6. date_default_timezone_set('Asia/Shanghai');
  7. use app\miniapi\auth\TestPhoneIdentityProvider;
  8. use plugin\saiadmin\app\model\water\WaterAgent;
  9. use plugin\saiadmin\app\model\water\WaterEnterprise;
  10. use plugin\saiadmin\app\model\water\WaterLedger;
  11. use plugin\saiadmin\app\model\water\WaterModel;
  12. if (!TestPhoneIdentityProvider::enabled()) {
  13. throw new RuntimeException('Requires local test environment');
  14. }
  15. $result = WaterModel::transaction(function (): array {
  16. $agent = WaterAgent::query()->where('id', 7)->lockForUpdate()->first();
  17. $enterprise = WaterEnterprise::query()->find(49);
  18. if (!$agent || $agent->name !== '测试商贸' || !$enterprise || $enterprise->name !== '【测试0910】科技园配送测试') {
  19. throw new RuntimeException('Expected named test agent and enterprise');
  20. }
  21. $key = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => 'TEST-MINI-COMMISSIONS-7-20261007'];
  22. $existing = WaterLedger::query()->where($key)->first();
  23. if ($existing) {
  24. $adjustmentKey = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $key['reference'] . '-ITEM-6'];
  25. if (WaterLedger::query()->where($adjustmentKey)->exists()) {
  26. return ['replayed' => true];
  27. }
  28. $adjustment = [
  29. 'id' => 6, 'customer_name' => '【测试】海博设计', 'title' => '售后退款调整',
  30. 'status' => 'adjustment', 'status_text' => '部分退款冲减',
  31. 'amount_cents' => -1600, 'created_at' => '2026-10-07',
  32. ];
  33. WaterLedger::query()->create($adjustmentKey + [
  34. 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
  35. 'note' => json_encode($adjustment, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
  36. 'created_at' => date('Y-m-d H:i:s'),
  37. ]);
  38. $summary = json_decode($existing->note, true, 512, JSON_THROW_ON_ERROR);
  39. $summary['withdrawable_cents'] -= 1600;
  40. $summary['total_cents'] -= 1600;
  41. $existing->note = json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  42. $existing->save();
  43. return ['replayed' => false, 'adjustment_added' => true];
  44. }
  45. $summary = [
  46. 'enabled' => false, 'demo' => true,
  47. 'reason' => '测试佣金数据,仅用于展示,不可提现,不产生真实佣金或结算。',
  48. 'withdrawable_cents' => 84400, 'pending_cents' => 82000,
  49. 'total_cents' => 266400, 'withdrawn_cents' => 100000, 'items' => [],
  50. ];
  51. WaterLedger::query()->create($key + [
  52. 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
  53. 'note' => json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
  54. 'created_at' => date('Y-m-d H:i:s'),
  55. ]);
  56. $samples = [
  57. ['【测试】云启商务', '客户购水佣金', 'settled', '已结算', 50000, '2026-10-06'],
  58. ['【测试】海博设计', '客户购水佣金', 'pending', '待结算', 50000, '2026-10-05'],
  59. ['【测试】星辰科技', '客户续购佣金', 'settled', '已结算', 36000, '2026-10-04'],
  60. ['【测试】远航商贸', '客户购水佣金', 'pending', '观察期', 32000, '2026-10-03'],
  61. ['【测试】历史结算', '佣金提现记录', 'withdrawn', '已提现(模拟)', 100000, '2026-10-01'],
  62. ['【测试】海博设计', '售后退款调整', 'adjustment', '部分退款冲减', -1600, '2026-10-07'],
  63. ];
  64. foreach ($samples as $index => [$customer, $title, $status, $statusText, $amount, $day]) {
  65. $item = [
  66. 'id' => $index + 1, 'customer_name' => $customer, 'title' => $title,
  67. 'status' => $status, 'status_text' => $statusText,
  68. 'amount_cents' => $amount, 'created_at' => $day,
  69. ];
  70. WaterLedger::query()->create([
  71. 'enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $key['reference'] . '-ITEM-' . ($index + 1),
  72. 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
  73. 'note' => json_encode($item, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
  74. 'created_at' => date('Y-m-d H:i:s'),
  75. ]);
  76. }
  77. return ['replayed' => false, 'agent_id' => 7, 'records' => count($samples)];
  78. });
  79. echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;