CaiWuXiTong/app/Http/Controllers/Business/PlantingReimbursementWorkbench.php
limeng cc40d9dcfe feat(种菜报销): 额度改为财务系统自算,按 11-27 财务确认调整校验口径
- 报销金额公式:空包费改为加(任务付款+快递费+佣金+京喜差价−扣款+空包费)
- 去掉「以旧换新超买家实付」「查不到标签待复查」两项校验
- 种菜额度不再上传看板:按《种菜明细报表生成逻辑》(业务交付包 2026-11-27)自算,
  商品ID→ID负责人、职级、店铺信息直接只读查经营分析副本库(JSReportReplica.rpt),
  金额取实付金额字段(同看板);单笔/每日/月度超额、未匹配负责人、职级为空、职级未配置、
  额度规则未配置全部取消;额度数据取不到时整批待复查
- 额度规则 309 条(脉象导出)随代码初始化,工作台可新增/修改/删除/整体导入/导出
- 利润中心直接读工作平台店铺信息(rpt.fys_shopInfo),查不到改为待复查;去掉店铺信息、看板上传
- 种菜任务工单导入模板执行人统一填李斯
- 每日群汇总推送改到 11:20(部署脚本 cron)
- 用看板 11 月 1–27 日 13,941 单对照:规则来源、日/月累计、风险类型逐单一致

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UNTa9ohkCsVFACqjz3KMcG
2026-12-01 05:49:55 +00:00

174 lines
8.3 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 App\Http\Controllers\Business;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use InvalidArgumentException;
use Modules\EcommerceFinance\Services\PlantingQuotaRules as QuotaRules;
use Modules\EcommerceFinance\Services\PlantingReimbursementReferences as References;
use Modules\EcommerceFinance\Services\PlantingReimbursementRobot as Robot;
use Modules\EcommerceFinance\Services\PlantingReimbursementWorkbookExporter as Exporter;
use Modules\EcommerceFinance\View\PlantingReimbursementWorkbenchPageData as PageData;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/** 种菜报销工作台:上传参考名单、维护种菜额度规则、导出对账 / 打款明细 / 种菜任务工单导入模板。 */
class PlantingReimbursementWorkbench extends Controller
{
public function upload(Request $request, string $kind, References $references)
{
abort_unless(isset(References::KINDS[$kind]), 404);
$request->validate(['file' => ['required', 'file', 'max:51200']]);
$file = $request->file('file');
$name = (string) $file->getClientOriginalName();
$redirect = redirect()->route('business.pages.show', [
'company_id' => company_id(),
'section' => 'planting-data',
'page' => 'planting-reimbursement-workbench',
'date' => (string) $request->input('date', ''),
'paid_from' => (string) $request->input('paid_from', ''),
'scope' => (string) $request->input('scope', ''),
]);
try {
$rows = $references->parse($kind, (string) $file->getRealPath(), $name);
$references->replace((int) company_id(), $kind, $rows, $name, optional(user())->id);
} catch (InvalidArgumentException $exception) {
return $redirect->with('error', References::KINDS[$kind] . '上传失败:' . $exception->getMessage());
} catch (\Throwable $exception) {
report($exception);
return $redirect->with('error', References::KINDS[$kind] . '上传失败,请检查文件格式后重新上传。');
}
return $redirect->with('success', sprintf('%s已更新:%s 条。', References::KINDS[$kind], number_format(count($rows))));
}
public function export(string $type, PageData $page, Exporter $exporter)
{
abort_unless(isset(Exporter::TYPES[$type]), 404);
$filters = $page->filters();
// 导出前必须先在聚工单核对条数、金额一致。
if (request('verified') !== '1') {
return redirect()->route('business.pages.show', [
'company_id' => company_id(), 'section' => 'planting-data', 'page' => 'planting-reimbursement-workbench',
] + array_diff_key($filters, ['decision' => true]))->with('error', '请先在聚工单按同样条件查询,核对条数和最终报销金额一致后,勾选「已核对」再导出。');
}
try {
$result = $page->build($filters);
$spreadsheet = $exporter->build($type, $result);
} catch (\Throwable $exception) {
report($exception);
abort(503, '聚工单镜像未就绪,未生成不完整的导出文件,请稍后重试。');
}
return response()->streamDownload(static function () use ($spreadsheet): void {
(new Xlsx($spreadsheet))->save('php://output');
}, $exporter->fileName($type, $result['summary']), [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
]);
}
public function saveRobot(Request $request, Robot $robot)
{
$robot->save([
'webhook' => (string) $request->input('webhook', ''),
'secret' => (string) $request->input('secret', ''),
'keyword' => (string) $request->input('keyword', ''),
'reporters' => (string) $request->input('reporters', ''),
'digest_webhook' => (string) $request->input('digest_webhook', ''),
'digest_secret' => (string) $request->input('digest_secret', ''),
'clear_digest' => $request->boolean('clear_digest'),
]);
return $this->back($request)->with('success', '钉钉机器人设置已保存。');
}
/** 按发起人把取消 / 待复查通知发到钉钉群(须先勾选已与聚工单核对)。 */
public function notify(Request $request, PageData $page, Robot $robot)
{
$filters = $page->filters();
if ($request->input('verified') !== '1') {
return $this->back($request)->with('error', '请先在聚工单核对条数和金额一致,勾选「已核对」后再发通知。');
}
try {
$result = $page->build($filters);
$sent = $robot->sendNotices((int) company_id(), $filters['date'], $result['summary']['notices'], $request->boolean('force'));
} catch (\Throwable $exception) {
report($exception);
return $this->back($request)->with('error', '群通知发送失败:' . mb_substr($exception->getMessage(), 0, 120));
}
$message = sprintf('群通知:发送 %d 人,跳过已发 %d 人', $sent['sent'], $sent['skipped']);
if ($sent['failed'] > 0) {
$message .= ',失败 ' . $sent['failed'] . ' 人';
}
if ($sent['unmatched'] !== []) {
$message .= ';钉钉里没匹配到、未能 @ 的:' . implode('、', $sent['unmatched']);
}
return $this->back($request)->with($sent['failed'] > 0 ? 'error' : 'success', $message . '。');
}
/** 新增 / 修改一条种菜额度规则。 */
public function saveRule(Request $request, QuotaRules $rules)
{
$id = $request->filled('id') ? (int) $request->input('id') : null;
try {
$rule = $rules->save((int) company_id(), $id, $request->only(QuotaRules::FIELDS), optional(user())->id);
} catch (InvalidArgumentException $exception) {
return $this->back($request, 'pwb-rules')->withInput()->with('error', '额度规则保存失败:' . $exception->getMessage());
}
return $this->back($request, 'pwb-rules')->with('success', ($id === null ? '已新增' : '已修改') . '额度规则:' . \Modules\EcommerceFinance\Services\PlantingQuotaCalculator::ruleLabel($rule) . '。');
}
public function deleteRule(Request $request, int $rule, QuotaRules $rules)
{
try {
$rules->delete((int) company_id(), $rule, optional(user())->id);
} catch (InvalidArgumentException $exception) {
return $this->back($request, 'pwb-rules')->with('error', $exception->getMessage());
}
return $this->back($request, 'pwb-rules')->with('success', '已删除额度规则。');
}
/** 用 CSV 整体替换额度规则(脉象导出格式或本页「导出规则」的文件)。 */
public function importRules(Request $request, QuotaRules $rules)
{
$request->validate(['file' => ['required', 'file', 'max:5120']]);
try {
$count = $rules->import((int) company_id(), (string) file_get_contents((string) $request->file('file')->getRealPath()), optional(user())->id);
} catch (InvalidArgumentException $exception) {
return $this->back($request, 'pwb-rules')->with('error', '额度规则导入失败:' . $exception->getMessage());
}
return $this->back($request, 'pwb-rules')->with('success', sprintf('额度规则已整体替换:%d 条。', $count));
}
public function exportRules(QuotaRules $rules)
{
$csv = $rules->exportCsv((int) company_id());
return response()->streamDownload(static function () use ($csv): void {
echo $csv;
}, '种菜额度规则_' . now()->format('Ymd') . '.csv', ['Content-Type' => 'text/csv; charset=UTF-8']);
}
private function back(Request $request, string $fragment = '')
{
return redirect()->to(route('business.pages.show', array_filter([
'company_id' => company_id(),
'section' => 'planting-data',
'page' => 'planting-reimbursement-workbench',
'date' => (string) $request->input('date', ''),
'paid_from' => (string) $request->input('paid_from', ''),
'scope' => (string) $request->input('scope', ''),
], static fn ($v) => $v !== '')) . ($fragment !== '' ? '#' . $fragment : ''));
}
}