| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- declare(strict_types=1);
- require dirname(__DIR__) . '/vendor/autoload.php';
- \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
- \support\App::loadAllConfig(['route']);
- date_default_timezone_set('Asia/Shanghai');
- use app\miniapi\auth\TestPhoneIdentityProvider;
- use plugin\saiadmin\app\model\water\WaterAgent;
- use plugin\saiadmin\app\model\water\WaterEnterprise;
- use plugin\saiadmin\app\model\water\WaterLedger;
- use plugin\saiadmin\app\model\water\WaterModel;
- if (!TestPhoneIdentityProvider::enabled()) {
- throw new RuntimeException('Requires local test environment');
- }
- $result = WaterModel::transaction(function (): array {
- $agent = WaterAgent::query()->where('id', 7)->lockForUpdate()->first();
- $enterprise = WaterEnterprise::query()->find(49);
- if (!$agent || $agent->name !== '测试商贸' || !$enterprise || $enterprise->name !== '【测试0910】科技园配送测试') {
- throw new RuntimeException('Expected named test agent and enterprise');
- }
- $key = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => 'TEST-MINI-COMMISSIONS-7-20261007'];
- $existing = WaterLedger::query()->where($key)->first();
- if ($existing) {
- $adjustmentKey = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $key['reference'] . '-ITEM-6'];
- if (WaterLedger::query()->where($adjustmentKey)->exists()) {
- return ['replayed' => true];
- }
- $adjustment = [
- 'id' => 6, 'customer_name' => '【测试】海博设计', 'title' => '售后退款调整',
- 'status' => 'adjustment', 'status_text' => '部分退款冲减',
- 'amount_cents' => -1600, 'created_at' => '2026-10-07',
- ];
- WaterLedger::query()->create($adjustmentKey + [
- 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
- 'note' => json_encode($adjustment, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
- 'created_at' => date('Y-m-d H:i:s'),
- ]);
- $summary = json_decode($existing->note, true, 512, JSON_THROW_ON_ERROR);
- $summary['withdrawable_cents'] -= 1600;
- $summary['total_cents'] -= 1600;
- $existing->note = json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
- $existing->save();
- return ['replayed' => false, 'adjustment_added' => true];
- }
- $summary = [
- 'enabled' => false, 'demo' => true,
- 'reason' => '测试佣金数据,仅用于展示,不可提现,不产生真实佣金或结算。',
- 'withdrawable_cents' => 84400, 'pending_cents' => 82000,
- 'total_cents' => 266400, 'withdrawn_cents' => 100000, 'items' => [],
- ];
- WaterLedger::query()->create($key + [
- 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
- 'note' => json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
- 'created_at' => date('Y-m-d H:i:s'),
- ]);
- $samples = [
- ['【测试】云启商务', '客户购水佣金', 'settled', '已结算', 50000, '2026-10-06'],
- ['【测试】海博设计', '客户购水佣金', 'pending', '待结算', 50000, '2026-10-05'],
- ['【测试】星辰科技', '客户续购佣金', 'settled', '已结算', 36000, '2026-10-04'],
- ['【测试】远航商贸', '客户购水佣金', 'pending', '观察期', 32000, '2026-10-03'],
- ['【测试】历史结算', '佣金提现记录', 'withdrawn', '已提现(模拟)', 100000, '2026-10-01'],
- ['【测试】海博设计', '售后退款调整', 'adjustment', '部分退款冲减', -1600, '2026-10-07'],
- ];
- foreach ($samples as $index => [$customer, $title, $status, $statusText, $amount, $day]) {
- $item = [
- 'id' => $index + 1, 'customer_name' => $customer, 'title' => $title,
- 'status' => $status, 'status_text' => $statusText,
- 'amount_cents' => $amount, 'created_at' => $day,
- ];
- WaterLedger::query()->create([
- 'enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $key['reference'] . '-ITEM-' . ($index + 1),
- 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
- 'note' => json_encode($item, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
- 'created_at' => date('Y-m-d H:i:s'),
- ]);
- }
- return ['replayed' => false, 'agent_id' => 7, 'records' => count($samples)];
- });
- echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;
|