287 lines
13 KiB
PHP
287 lines
13 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
/**
|
||
* 按钉钉「种菜报销流程」的实际表单结构(接口读取),把工作台的钉钉流程明细填成发起审批的 formComponentValues。
|
||
*
|
||
* 字段按标题匹配(兼容常见写法),以前手工发起时的填法:
|
||
* - 收款费用明细:一行一条钉钉流程明细(使用部门 = 费用承担部门、店铺 = 钉钉下拉里的简称(卓禾 / 欧意朗 / 亿家达 / 其他)、用途 = M.D种菜报销、收款金额、支付方式 = 银行卡);
|
||
* - 收款人全称 / 开户银行 / 银行账号:填「账号详见附件」,附件 = 种菜报销明细M.D.xlsx;
|
||
* - 付款明细:一行,付款金额 = 合计,付款方式 = 银行卡;
|
||
* - 各部门金额占比、科目编码 / 科目名称、账户简称、打款时间由钉钉自动带出,不填也不算缺失;
|
||
* - 报销类型:种菜核销。
|
||
* 必填字段填不上、下拉选项不匹配时列出来,不允许提交。纯计算类。
|
||
*/
|
||
final class PlantingDingTalkForm
|
||
{
|
||
public const PAYEE_TEXT = '账号详见附件';
|
||
|
||
public const REIMBURSE_TYPE = '种菜核销';
|
||
|
||
public const PAYMENT_METHOD = '银行卡';
|
||
|
||
private const MAIN = [
|
||
'total' => ['收款合计', '报销合计', '合计金额', '金额合计', '报销金额合计', '付款合计', '总金额', '报销总金额'],
|
||
'type' => ['报销类型', '核销类型', '费用类型'],
|
||
'payee' => ['收款人', '收款人全称', '收款户名', '收款人姓名', '开户行', '开户银行', '收款银行', '账号', '收款账号', '银行账号', '银行卡号', '收款人账号'],
|
||
'purpose' => ['用途', '事由', '报销事由'],
|
||
'payment' => ['支付方式', '付款方式'],
|
||
];
|
||
|
||
private const EXPENSE_ROW = [
|
||
'seq' => ['序号'],
|
||
'department' => ['使用部门', '费用承担部门', '承担部门', '费用部门', '所属部门', '利润中心', '所属利润中心'],
|
||
'shop' => ['店铺', '店铺名称', '所属店铺'],
|
||
'purpose' => ['用途', '事由'],
|
||
'amount' => ['收款金额', '金额', '报销金额', '费用金额'],
|
||
'payment' => ['支付方式', '付款方式'],
|
||
'type' => ['报销类型', '核销类型', '费用类型'],
|
||
'payee' => ['收款人', '收款人全称', '收款户名', '开户行', '开户银行', '账号', '收款账号', '银行账号'],
|
||
];
|
||
|
||
private const PAYMENT_ROW = [
|
||
'seq' => ['对应序号', '序号'],
|
||
'amount' => ['付款金额', '金额'],
|
||
'payment' => ['付款方式', '支付方式'],
|
||
'payee' => ['收款人', '收款人全称', '收款户名', '开户行', '开户银行', '账号', '收款账号', '银行账号'],
|
||
];
|
||
|
||
/** 钉钉表单自动换算 / 带出的字段(财务确认 2026-12-01),系统不填,也不算缺失。 */
|
||
private const AUTO = ['各部门金额占比', '金额占比', '科目编码', '科目名称', '账户简称', '打款时间', '银行科目编码', '银行科目名称'];
|
||
|
||
/** 三大天猫店:工单里的全称 ↔ 钉钉「店铺」下拉里的简称(财务确认 2026-12-01)。下拉用哪个写法就填哪个。 */
|
||
public const SHOP_FULL_NAMES = [
|
||
'欧意朗' => '天猫-欧意朗旗舰店',
|
||
'亿家达' => '天猫-亿家达旗舰店',
|
||
'卓禾' => '天猫-卓禾家具旗舰店',
|
||
];
|
||
|
||
/** 不需要填写的说明类组件。 */
|
||
private const SKIP = ['TextNote', 'DDBizSuite'];
|
||
|
||
/**
|
||
* @param list<array<string,mixed>> $schema 钉钉表单组件(schemaContent.items)
|
||
* @param array<string,mixed> $summary 工作台批次汇总(ding_lines、total、purpose、bill_date)
|
||
* @param array<string,string>|null $attachment 已上传附件;预览时为 null
|
||
* @return array{values:list<array<string,string>>,preview:list<array<string,mixed>>,missing:list<string>,invalid:list<string>,attachment_label:?string}
|
||
*/
|
||
public function build(array $schema, array $summary, ?array $attachment, string $attachmentName): array
|
||
{
|
||
$lines = array_values($summary['ding_lines'] ?? []);
|
||
$total = self::money(array_sum(array_column($lines, 'cents')));
|
||
$main = [
|
||
'total' => $total,
|
||
'type' => self::REIMBURSE_TYPE,
|
||
'payee' => self::PAYEE_TEXT,
|
||
'purpose' => (string) ($summary['purpose'] ?? ''),
|
||
'payment' => self::PAYMENT_METHOD,
|
||
];
|
||
|
||
$values = [];
|
||
$preview = [];
|
||
$missing = [];
|
||
$invalid = [];
|
||
$attachmentLabel = null;
|
||
foreach ($schema as $item) {
|
||
$type = (string) ($item['componentName'] ?? '');
|
||
$props = (array) ($item['props'] ?? []);
|
||
$label = trim((string) ($props['label'] ?? ''));
|
||
$required = (bool) ($props['required'] ?? false);
|
||
if ($label === '' || in_array($type, self::SKIP, true)) {
|
||
continue;
|
||
}
|
||
|
||
if ($type === 'TableField') {
|
||
$isPayment = str_contains($label, '付款');
|
||
$rows = $isPayment ? [$this->paymentRow($lines, $total)] : $this->expenseRows($lines, $summary);
|
||
$map = $isPayment ? self::PAYMENT_ROW : self::EXPENSE_ROW;
|
||
$table = [];
|
||
foreach ($rows as $rowIndex => $row) {
|
||
$cells = [];
|
||
foreach ((array) ($item['children'] ?? []) as $child) {
|
||
$childLabel = trim((string) ($child['props']['label'] ?? ''));
|
||
$childType = (string) ($child['componentName'] ?? '');
|
||
if ($childLabel === '' || in_array($childType, self::SKIP, true)) {
|
||
continue;
|
||
}
|
||
if (self::isAuto($childLabel)) {
|
||
continue;
|
||
}
|
||
$key = self::key($map, $childLabel);
|
||
$value = $key === null ? '' : (string) ($row[$key] ?? '');
|
||
if ($key === 'shop' && $value !== '') {
|
||
$value = self::shopOption($child, $value);
|
||
}
|
||
if ($value === '') {
|
||
if ((bool) ($child['props']['required'] ?? false) && $rowIndex === 0) {
|
||
$missing[] = $label . ' → ' . $childLabel;
|
||
}
|
||
continue;
|
||
}
|
||
if (! self::optionOk($child, $value)) {
|
||
$invalid[] = $label . ' → ' . $childLabel . '(选项里没有「' . $value . '」)';
|
||
}
|
||
$cells[] = ['name' => $childLabel, 'value' => $value];
|
||
}
|
||
$table[] = $cells;
|
||
}
|
||
if ($table === [] || $table[0] === []) {
|
||
if ($required) {
|
||
$missing[] = $label;
|
||
}
|
||
continue;
|
||
}
|
||
$values[] = ['name' => $label, 'value' => json_encode($table, JSON_UNESCAPED_UNICODE)];
|
||
$preview[] = ['label' => $label, 'type' => $type, 'required' => $required, 'rows' => $table];
|
||
continue;
|
||
}
|
||
|
||
if ($type === 'DDAttachment') {
|
||
$attachmentLabel ??= $label;
|
||
if ($attachmentLabel !== $label) {
|
||
continue;
|
||
}
|
||
$value = $attachment === null ? '' : json_encode([$attachment], JSON_UNESCAPED_UNICODE);
|
||
if ($attachment !== null) {
|
||
$values[] = ['name' => $label, 'value' => $value];
|
||
}
|
||
$preview[] = ['label' => $label, 'type' => $type, 'required' => $required, 'value' => '提交时上传:' . $attachmentName];
|
||
continue;
|
||
}
|
||
|
||
if (self::isAuto($label)) {
|
||
continue;
|
||
}
|
||
$key = self::key(self::MAIN, $label);
|
||
$value = $key === null ? '' : $main[$key];
|
||
if ($value === '') {
|
||
if ($required) {
|
||
$missing[] = $label;
|
||
}
|
||
$preview[] = ['label' => $label, 'type' => $type, 'required' => $required, 'value' => ''];
|
||
continue;
|
||
}
|
||
if (! self::optionOk($item, $value)) {
|
||
$invalid[] = $label . '(选项里没有「' . $value . '」)';
|
||
}
|
||
$values[] = ['name' => $label, 'value' => $value];
|
||
$preview[] = ['label' => $label, 'type' => $type, 'required' => $required, 'value' => $value];
|
||
}
|
||
|
||
if ($attachmentLabel === null) {
|
||
$missing[] = '附件(表单里没有附件字段,无法附上打款明细)';
|
||
}
|
||
|
||
return ['values' => $values, 'preview' => $preview, 'missing' => array_values(array_unique($missing)), 'invalid' => $invalid, 'attachment_label' => $attachmentLabel];
|
||
}
|
||
|
||
/** @return list<array<string,string>> */
|
||
private function expenseRows(array $lines, array $summary): array
|
||
{
|
||
$rows = [];
|
||
foreach ($lines as $index => $line) {
|
||
$rows[] = [
|
||
'seq' => (string) ($index + 1),
|
||
'department' => (string) ($line['department'] ?? ''),
|
||
'shop' => (string) ($line['shop'] ?? ''),
|
||
'purpose' => (string) ($line['purpose'] ?? $summary['purpose'] ?? ''),
|
||
'amount' => self::money((int) $line['cents']),
|
||
'payment' => (string) ($line['payment_method'] ?? self::PAYMENT_METHOD),
|
||
'type' => self::REIMBURSE_TYPE,
|
||
'payee' => self::PAYEE_TEXT,
|
||
];
|
||
}
|
||
|
||
return $rows;
|
||
}
|
||
|
||
/** @return array<string,string> */
|
||
private function paymentRow(array $lines, string $total): array
|
||
{
|
||
return [
|
||
'seq' => count($lines) > 1 ? '1-' . count($lines) : '1',
|
||
'amount' => $total,
|
||
'payment' => self::PAYMENT_METHOD,
|
||
'payee' => self::PAYEE_TEXT,
|
||
];
|
||
}
|
||
|
||
/** @param array<string,list<string>> $map */
|
||
private static function key(array $map, string $label): ?string
|
||
{
|
||
$normalized = preg_replace('/[\s((].*$/u', '', $label) ?? $label;
|
||
foreach ($map as $key => $aliases) {
|
||
if (in_array($label, $aliases, true) || in_array($normalized, $aliases, true)) {
|
||
return $key;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private static function isAuto(string $label): bool
|
||
{
|
||
$normalized = preg_replace('/[\s((].*$/u', '', $label) ?? $label;
|
||
|
||
return in_array($label, self::AUTO, true) || in_array($normalized, self::AUTO, true);
|
||
}
|
||
|
||
/**
|
||
* 店铺填钉钉下拉里实际存在的那一项:先按全称、再按简称精确匹配;
|
||
* 都没有时取唯一一个「被全称包含 / 包含简称」的选项(如「卓禾家具」)。
|
||
* 拿不到选项时用全称。
|
||
*/
|
||
private static function shopOption(array $component, string $shop): string
|
||
{
|
||
$full = self::SHOP_FULL_NAMES[$shop] ?? $shop;
|
||
$options = self::options($component);
|
||
if ($options === []) {
|
||
return $full;
|
||
}
|
||
foreach ([$full, $shop] as $candidate) {
|
||
if (in_array($candidate, $options, true)) {
|
||
return $candidate;
|
||
}
|
||
}
|
||
$hits = array_values(array_filter(
|
||
$options,
|
||
static fn (string $o): bool => $o !== '' && (str_contains($full, $o) || str_contains($o, $shop)),
|
||
));
|
||
|
||
return count($hits) === 1 ? $hits[0] : $full;
|
||
}
|
||
|
||
/** @return list<string> */
|
||
private static function options(array $component): array
|
||
{
|
||
if (! in_array((string) ($component['componentName'] ?? ''), ['DDSelectField', 'DDMultiSelectField'], true)) {
|
||
return [];
|
||
}
|
||
$options = [];
|
||
foreach ((array) ($component['props']['options'] ?? []) as $option) {
|
||
if (is_string($option)) {
|
||
$decoded = json_decode($option, true);
|
||
$options[] = is_array($decoded) ? (string) ($decoded['value'] ?? '') : $option;
|
||
} elseif (is_array($option)) {
|
||
$options[] = (string) ($option['value'] ?? $option['text'] ?? '');
|
||
}
|
||
}
|
||
|
||
return $options;
|
||
}
|
||
|
||
/** 单选 / 多选组件的值必须是选项之一;拿不到选项时不拦。 */
|
||
private static function optionOk(array $component, string $value): bool
|
||
{
|
||
$options = self::options($component);
|
||
|
||
return $options === [] || in_array($value, $options, true);
|
||
}
|
||
|
||
private static function money(int $cents): string
|
||
{
|
||
return number_format($cents / 100, 2, '.', '');
|
||
}
|
||
}
|