CaiWuXiTong/tests/Unit/EcommerceFinance/PlantingDingTalkFormTest.php

140 lines
8.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Unit\EcommerceFinance;
use Modules\EcommerceFinance\Services\PlantingDingTalkForm as Form;
use PHPUnit\Framework\TestCase;
class PlantingDingTalkFormTest extends TestCase
{
/** 结构仿照钉钉表单接口返回(schemaContent.items)。 */
public static function schema(): array
{
$field = static fn (string $type, string $label, bool $required = true, array $extra = []): array => ['componentName' => $type, 'props' => ['label' => $label, 'required' => $required] + $extra];
return [
$field('TextNote', '说明'),
$field('DDSelectField', '报销类型', true, ['options' => [['key' => 'a', 'value' => '种菜核销'], ['key' => 'b', 'value' => '日常报销']]]),
['componentName' => 'TableField', 'props' => ['label' => '收款费用明细', 'required' => true], 'children' => [
$field('NumberField', '序号'),
$field('DepartmentField', '费用承担部门(利润中心)'),
$field('TextField', '店铺'),
$field('TextField', '用途'),
$field('MoneyField', '收款金额'),
$field('TextField', '备注', false),
]],
$field('MoneyField', '收款合计'),
$field('TextField', '收款人'),
$field('TextField', '开户行'),
$field('TextField', '账号'),
['componentName' => 'TableField', 'props' => ['label' => '付款明细', 'required' => false], 'children' => [
$field('TextField', '对应序号'),
$field('MoneyField', '付款金额'),
$field('DDSelectField', '付款方式', true, ['options' => ['{"key":"1","value":"银行卡"}', '{"key":"2","value":"支付宝"}']]),
$field('DDDateField', '打款时间', false),
]],
$field('DDAttachment', '附件'),
];
}
public static function summary(): array
{
return [
'bill_date' => '2026-11-11', 'purpose' => '11.11种菜报销', 'total' => '130.50',
'ding_lines' => [
['department' => '运营中心A', 'shop' => '欧意朗', 'purpose' => '11.11种菜报销', 'payment_method' => '银行卡', 'cents' => 10050],
['department' => '运营中心G', 'shop' => '其他', 'purpose' => '11.11种菜报销', 'payment_method' => '银行卡', 'cents' => 3000],
],
];
}
public function test_fills_form_by_labels_and_attachment(): void
{
$attachment = ['spaceId' => 'S1', 'fileName' => '种菜报销明细11.11.xlsx', 'fileSize' => '123', 'fileType' => 'xlsx', 'fileId' => 'F1'];
$result = (new Form())->build(self::schema(), self::summary(), $attachment, '种菜报销明细11.11.xlsx');
$this->assertSame([], $result['missing']);
$this->assertSame([], $result['invalid']);
$values = array_column($result['values'], 'value', 'name');
$this->assertSame('种菜核销', $values['报销类型']);
$this->assertSame('130.50', $values['收款合计']);
$this->assertSame('账号详见附件', $values['收款人']);
$this->assertSame('账号详见附件', $values['账号']);
$this->assertSame([
[['name' => '序号', 'value' => '1'], ['name' => '费用承担部门(利润中心)', 'value' => '运营中心A'], ['name' => '店铺', 'value' => '天猫-欧意朗旗舰店'], ['name' => '用途', 'value' => '11.11种菜报销'], ['name' => '收款金额', 'value' => '100.50']],
[['name' => '序号', 'value' => '2'], ['name' => '费用承担部门(利润中心)', 'value' => '运营中心G'], ['name' => '店铺', 'value' => '其他'], ['name' => '用途', 'value' => '11.11种菜报销'], ['name' => '收款金额', 'value' => '30.00']],
], json_decode($values['收款费用明细'], true));
$this->assertSame([[['name' => '对应序号', 'value' => '1-2'], ['name' => '付款金额', 'value' => '130.50'], ['name' => '付款方式', 'value' => '银行卡']]], json_decode($values['付款明细'], true));
$this->assertSame([$attachment], json_decode($values['附件'], true));
$this->assertArrayNotHasKey('说明', $values);
// 预览时不带附件:不出附件值,但不算缺字段。
$preview = (new Form())->build(self::schema(), self::summary(), null, '种菜报销明细11.11.xlsx');
$this->assertSame([], $preview['missing']);
$this->assertArrayNotHasKey('附件', array_column($preview['values'], 'value', 'name'));
}
/** 生产表单(2026-12-01 预览):收款人全称、使用部门、钉钉自动带出的字段、店铺下拉。 */
public function test_production_layout_fills_payee_department_and_shop_options(): void
{
$field = static fn (string $type, string $label, bool $required = true, array $extra = []): array => ['componentName' => $type, 'props' => ['label' => $label, 'required' => $required] + $extra];
$schema = [
$field('TextField', '收款人全称'),
$field('TextField', '开户银行'),
$field('TextField', '银行账号'),
$field('DDSelectField', '报销类型', true, ['options' => [['key' => 'a', 'value' => '种菜核销']]]),
['componentName' => 'TableField', 'props' => ['label' => '收款费用明细', 'required' => false], 'children' => [
$field('TextField', '序号'),
$field('DDSelectField', '使用部门', true, ['options' => [['key' => '1', 'value' => '运营中心A'], ['key' => '2', 'value' => '运营中心G']]]),
$field('NumberField', '各部门金额占比'),
$field('DDSelectField', '店铺', true, ['options' => [['key' => '1', 'value' => '天猫-欧意朗旗舰店'], ['key' => '2', 'value' => '卓禾家具'], ['key' => '3', 'value' => '其他']]]),
$field('TextField', '科目编码'),
$field('TextField', '科目名称'),
$field('TextField', '用途'),
$field('MoneyField', '收款金额'),
]],
$field('CalculateField', '收款合计'),
$field('DDAttachment', '收款明细附件', false),
['componentName' => 'TableField', 'props' => ['label' => '付款明细', 'required' => false], 'children' => [
$field('TextField', '对应序号'),
$field('TextField', '账户简称'),
$field('TextField', '科目编码'),
$field('TextField', '科目名称'),
$field('MoneyField', '付款金额'),
$field('DDDateField', '打款时间'),
]],
$field('CalculateField', '付款合计'),
];
$summary = self::summary();
$summary['ding_lines'][] = ['department' => '运营中心A', 'shop' => '卓禾', 'purpose' => '11.11种菜报销', 'payment_method' => '银行卡', 'cents' => 100];
$result = (new Form())->build($schema, $summary, null, '种菜报销明细11.11.xlsx');
$this->assertSame([], $result['missing']);
$this->assertSame([], $result['invalid']);
$values = array_column($result['values'], 'value', 'name');
$this->assertSame('账号详见附件', $values['收款人全称']);
$this->assertSame('131.50', $values['付款合计']);
$rows = json_decode($values['收款费用明细'], true);
$this->assertSame(['序号', '使用部门', '店铺', '用途', '收款金额'], array_column($rows[0], 'name'));
$this->assertSame(['天猫-欧意朗旗舰店', '其他', '卓禾家具'], array_map(static fn (array $row): string => $row[2]['value'], $rows));
$this->assertSame('运营中心G', $rows[1][1]['value']);
$this->assertSame([[['name' => '对应序号', 'value' => '1-3'], ['name' => '付款金额', 'value' => '131.50']]], json_decode($values['付款明细'], true));
}
public function test_reports_required_fields_it_cannot_fill_and_bad_options(): void
{
$schema = self::schema();
$schema[] = ['componentName' => 'DDDateField', 'props' => ['label' => '报销日期', 'required' => true]];
$schema[1]['props']['options'] = [['key' => 'b', 'value' => '日常报销']];
array_pop($schema[2]['children']);
$schema[2]['children'][] = ['componentName' => 'TextField', 'props' => ['label' => '经办人', 'required' => true]];
$withoutAttachment = array_values(array_filter($schema, static fn (array $item): bool => $item['componentName'] !== 'DDAttachment'));
$result = (new Form())->build($withoutAttachment, self::summary(), null, 'x.xlsx');
$this->assertSame(['收款费用明细 → 经办人', '报销日期', '附件(表单里没有附件字段,无法附上打款明细)'], $result['missing']);
$this->assertSame(['报销类型(选项里没有「种菜核销」)'], $result['invalid']);
}
}