125 lines
5.1 KiB
PHP
125 lines
5.1 KiB
PHP
<?php
|
||
|
||
namespace App\Support\DropshipFreight;
|
||
|
||
use Carbon\CarbonImmutable;
|
||
use Carbon\CarbonInterface;
|
||
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
||
use Throwable;
|
||
|
||
/**
|
||
* 代发运费账单上传提醒:往厂家钉钉对账群(和货款对账同一个群)发厂家对账入口链接。
|
||
* - 手动:上传厂家账单页「通知厂家上传」、代发厂家设置里单家「通知上传」;
|
||
* - 自动:每月 1–14 日 10:00(dropship-freight:remind-upload),提醒上个月的账单,14 日截止,逾期本月货款和运费不予结算。
|
||
* 批量提醒只发给「这个月有代发发货、还没上传、配了机器人」的厂家;单家提醒照发。
|
||
*/
|
||
final class DropshipUploadReminder
|
||
{
|
||
public const DEADLINE_DAY = 14;
|
||
public const SEND_TIME = '10:00';
|
||
public const SETTING_KEY = 'dropship_freight.auto_upload_reminder';
|
||
public const TIMEZONE = 'Asia/Shanghai';
|
||
|
||
public function __construct(
|
||
private readonly DropshipFreightStore $store,
|
||
private readonly DropshipReconciler $reconciler,
|
||
private readonly SupplierDingTalkNotifier $notifier,
|
||
) {}
|
||
|
||
/** 自动提醒开关(按公司),默认开。 */
|
||
public static function enabled(): bool
|
||
{
|
||
return (string) setting(self::SETTING_KEY, '1') !== '0';
|
||
}
|
||
|
||
public static function setEnabled(bool $on): void
|
||
{
|
||
setting()->set(self::SETTING_KEY, $on ? '1' : '0');
|
||
setting()->save();
|
||
}
|
||
|
||
public static function today(): CarbonImmutable
|
||
{
|
||
return CarbonImmutable::now(self::TIMEZONE)->startOfDay();
|
||
}
|
||
|
||
/** 这天要催的账单月:上个月。 */
|
||
public static function billMonth(CarbonInterface $day): string
|
||
{
|
||
return CarbonImmutable::parse($day->format('Y-m-d'), self::TIMEZONE)->startOfMonth()->subMonth()->format('Y-m');
|
||
}
|
||
|
||
/** 账单月的上传截止日:次月 14 日。 */
|
||
public static function deadline(string $month): CarbonImmutable
|
||
{
|
||
return CarbonImmutable::createFromFormat('Y-m-d', $month . '-01', self::TIMEZONE)->startOfDay()->addMonth()->day(self::DEADLINE_DAY);
|
||
}
|
||
|
||
/** 今天还在这个账单月的上传期(次月 1–14 日)里,就返回截止日,提醒里写明截止和逾期后果;否则 null。 */
|
||
public static function deadlineFor(string $month, CarbonInterface $today): ?CarbonImmutable
|
||
{
|
||
$deadline = self::deadline($month);
|
||
$day = CarbonImmutable::parse($today->format('Y-m-d'), self::TIMEZONE);
|
||
|
||
return $day->greaterThanOrEqualTo($deadline->startOfMonth()) && $day->lessThanOrEqualTo($deadline) ? $deadline : null;
|
||
}
|
||
|
||
/**
|
||
* 这个月要提醒的厂家。
|
||
*
|
||
* @return array{targets:list<object>,done:list<string>,no_shipment:list<string>,no_robot:list<string>}
|
||
*/
|
||
public function targets(string $month, ?int $factoryId = null): array
|
||
{
|
||
$factories = collect($this->store->factories());
|
||
$single = $factoryId !== null;
|
||
if ($single) {
|
||
$factories = $factories->where('id', $factoryId)->values();
|
||
}
|
||
$uploaded = array_flip($this->store->db()->table('dropship_bill_files')->where('bill_month', $month)->whereNotNull('factory_id')
|
||
->where('status', '!=', 'failed')->distinct()->pluck('factory_id')->map('intval')->all());
|
||
$robots = $this->notifier->configurationMap($factories->pluck('name')->map('strval')->all());
|
||
$own = $single ? [] : $this->store->ownExpressNames();
|
||
$result = ['targets' => [], 'done' => [], 'no_shipment' => [], 'no_robot' => []];
|
||
foreach ($factories as $factory) {
|
||
$name = (string) $factory->name;
|
||
if (! $single && isset($uploaded[(int) $factory->id])) {
|
||
$result['done'][] = $name;
|
||
} elseif (! ($robots[$name]['configured'] ?? false)) {
|
||
$result['no_robot'][] = $name;
|
||
} elseif (! $single && ! $this->reconciler->hasDropship($factory, $month, $own)) {
|
||
$result['no_shipment'][] = $name;
|
||
} else {
|
||
$result['targets'][] = $factory;
|
||
}
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 发提醒。
|
||
*
|
||
* @return array{sent:list<string>,failed:list<string>,done:list<string>,no_shipment:list<string>,no_robot:list<string>,deadline:?CarbonImmutable}
|
||
*/
|
||
public function remind(string $month, string $portalUrl, ?int $factoryId = null, ?CarbonInterface $today = null): array
|
||
{
|
||
$today ??= self::today();
|
||
$deadline = self::deadlineFor($month, $today);
|
||
$plan = $this->targets($month, $factoryId);
|
||
$sent = $failed = [];
|
||
foreach ($plan['targets'] as $factory) {
|
||
try {
|
||
$this->notifier->pushDropshipUploadReminder((string) $factory->name, $month, $portalUrl, $deadline, $today);
|
||
$sent[] = (string) $factory->name;
|
||
} catch (Throwable $error) {
|
||
report($error);
|
||
$failed[] = (string) $factory->name;
|
||
}
|
||
}
|
||
unset($plan['targets']);
|
||
|
||
return ['sent' => $sent, 'failed' => $failed, 'deadline' => $deadline] + $plan;
|
||
}
|
||
}
|