CaiWuXiTong/modules/EcommerceFinance/Services/PlantingDingTalkForm.php

227 lines
10 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 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' => ['收款人', '收款户名', '开户行', '开户银行', '账号', '收款账号', '银行账号'],
];
/** 不需要填写的说明类组件。 */
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;
}
$key = self::key($map, $childLabel);
$value = $key === null ? '' : (string) ($row[$key] ?? '');
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;
}
$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 optionOk(array $component, string $value): bool
{
if (! in_array((string) ($component['componentName'] ?? ''), ['DDSelectField', 'DDMultiSelectField'], true)) {
return true;
}
$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 === [] || in_array($value, $options, true);
}
private static function money(int $cents): string
{
return number_format($cents / 100, 2, '.', '');
}
}