feat: 支持供应商联系人绑定多个厂家 #922
@ -334,8 +334,18 @@ class SupplierReconciliation extends Controller
|
||||
{
|
||||
$user = $request->user();
|
||||
if ($user && SupplierPortalAccess::isSupplierUser($user)) {
|
||||
$supplier = $this->supplierPortalAccess->supplierFor($user);
|
||||
abort_if(! $supplier, 403, '当前钉钉账号未绑定有效供应商,请联系财务人员。');
|
||||
$suppliers = $this->supplierPortalAccess->supplierNamesFor($user);
|
||||
abort_if($suppliers === [], 403, '当前钉钉账号未绑定有效供应商,请联系财务人员。');
|
||||
|
||||
if (count($suppliers) === 1) {
|
||||
return $suppliers[0];
|
||||
}
|
||||
$supplier = trim((string) $request->input('supplier', $request->query('supplier', '')));
|
||||
abort_unless(
|
||||
in_array($supplier, $suppliers, true),
|
||||
403,
|
||||
'该供应商不在当前账号的授权范围内。',
|
||||
);
|
||||
|
||||
return $supplier;
|
||||
}
|
||||
|
||||
@ -25,4 +25,9 @@ class SupplierPortalIdentity extends Model
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function suppliers()
|
||||
{
|
||||
return $this->hasMany(SupplierPortalSupplier::class, 'user_id', 'user_id');
|
||||
}
|
||||
}
|
||||
|
||||
28
app/Models/Auth/SupplierPortalSupplier.php
Normal file
28
app/Models/Auth/SupplierPortalSupplier.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Auth;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SupplierPortalSupplier extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'company_id',
|
||||
'user_id',
|
||||
'supplier_code',
|
||||
'supplier_name',
|
||||
'dingtalk_group_id',
|
||||
'dingtalk_group_name',
|
||||
'last_verified_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'dingtalk_group_id' => 'integer',
|
||||
'last_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@ -88,6 +88,11 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
return $this->hasOne(SupplierPortalIdentity::class, 'user_id');
|
||||
}
|
||||
|
||||
public function supplierPortalSuppliers()
|
||||
{
|
||||
return $this->hasMany(SupplierPortalSupplier::class, 'user_id');
|
||||
}
|
||||
|
||||
public function contact()
|
||||
{
|
||||
return $this->hasOne('App\Models\Common\Contact', 'user_id', 'id');
|
||||
|
||||
@ -29,11 +29,11 @@ class DingTalkClosedAccess
|
||||
|
||||
$roster = app(FinanceRoster::class)->findEligible((string) ($profile['dingtalk_user_id'] ?? ''));
|
||||
$restricted = $roster !== null && ($roster['restricted'] ?? false) === true;
|
||||
$supplierRoster = $roster ? null : app(SupplierPortalAccess::class)->resolve(
|
||||
$supplierRosters = $roster ? [] : app(SupplierPortalAccess::class)->resolveAll(
|
||||
(string) ($profile['dingtalk_user_id'] ?? ''),
|
||||
(string) ($profile['union_id'] ?? ''),
|
||||
);
|
||||
if (! $roster && ! $supplierRoster) {
|
||||
if (! $roster && $supplierRosters === []) {
|
||||
$this->audit('login_denied', 'denied', null, null, $profile['union_id'] ?? null, $request, [
|
||||
'reason' => 'outside_authorized_roster',
|
||||
]);
|
||||
@ -56,14 +56,14 @@ class DingTalkClosedAccess
|
||||
}
|
||||
if (SupplierPortalAccess::isSupplierUser($identity->user)) {
|
||||
$company = $this->enabledCompany($identity->user);
|
||||
if (! $supplierRoster || ! $company) {
|
||||
if ($supplierRosters === [] || ! $company) {
|
||||
$this->audit('login_denied', 'denied', $identity->user_id, null, $profile['union_id'], $request, [
|
||||
'reason' => 'outside_supplier_roster',
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
app(SupplierPortalAccess::class)->bind($identity->user, (int) $company->id, $supplierRoster);
|
||||
app(SupplierPortalAccess::class)->bindMany($identity->user, (int) $company->id, $supplierRosters);
|
||||
} elseif (! $roster) {
|
||||
$this->audit('login_denied', 'denied', $identity->user_id, null, $profile['union_id'], $request, [
|
||||
'reason' => 'outside_finance_roster',
|
||||
@ -94,7 +94,7 @@ class DingTalkClosedAccess
|
||||
return null;
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($profile, $roster, $restricted, $supplierRoster, $request): ?DingTalkIdentity {
|
||||
return DB::transaction(function () use ($profile, $roster, $restricted, $supplierRosters, $request): ?DingTalkIdentity {
|
||||
// Serialize first-time provisioning on the target company. Both the
|
||||
// account and unique identity are committed together, including retries.
|
||||
$companies = Company::enabled()
|
||||
@ -117,7 +117,7 @@ class DingTalkClosedAccess
|
||||
return null;
|
||||
}
|
||||
|
||||
$supplierPortal = $supplierRoster !== null;
|
||||
$supplierPortal = $supplierRosters !== [];
|
||||
$roleName = ! $supplierPortal && FinanceRoster::isDesignatedSuperAdmin((string) $profile['dingtalk_user_id'])
|
||||
? SystemAccess::SUPER_ADMIN : SystemAccess::USER;
|
||||
$role = Role::where('name', $roleName)->firstOrFail();
|
||||
@ -149,7 +149,7 @@ class DingTalkClosedAccess
|
||||
$account->permissions()->attach(Permission::where('name', 'read-admin-panel')->firstOrFail()->id);
|
||||
$account->companies()->attach($companies->first()->id);
|
||||
if ($supplierPortal) {
|
||||
app(SupplierPortalAccess::class)->bind($account, (int) $companies->first()->id, $supplierRoster);
|
||||
app(SupplierPortalAccess::class)->bindMany($account, (int) $companies->first()->id, $supplierRosters);
|
||||
} elseif ($mobile = $this->normalizeMobile($profile['mobile'] ?? null)) {
|
||||
Contact::create([
|
||||
'company_id' => $companies->first()->id, 'type' => Contact::EMPLOYEE_TYPE,
|
||||
|
||||
@ -4,15 +4,18 @@ namespace App\Services\Auth;
|
||||
|
||||
use App\Models\Auth\DingTalkIdentity;
|
||||
use App\Models\Auth\SupplierPortalIdentity;
|
||||
use App\Models\Auth\SupplierPortalSupplier;
|
||||
use App\Models\Auth\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
|
||||
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
||||
|
||||
/**
|
||||
* 钉钉企业组织中的供应商成员映射。
|
||||
*
|
||||
* 只有当前有效的钉钉企业员工、且其部门祖先精确对应唯一供应商主档名称时,
|
||||
* 才建立供应商门户身份。财务花名册不是供应商人员的准入前提。
|
||||
* 只有当前有效的钉钉企业员工、且其部门祖先精确对应一个或多个供应商主档名称时,
|
||||
* 才建立供应商门户身份。一个联系人可绑定多家供应商,财务花名册不是准入前提。
|
||||
*/
|
||||
class SupplierPortalAccess
|
||||
{
|
||||
@ -33,11 +36,21 @@ class SupplierPortalAccess
|
||||
|
||||
/** @return array{supplier_name:string,supplier_code:string,dingtalk_group_id:int,dingtalk_group_name:string}|null */
|
||||
public function resolve(string $userId, string $unionId = ''): ?array
|
||||
{
|
||||
$resolved = $this->resolveAll($userId, $unionId);
|
||||
|
||||
return count($resolved) === 1 ? $resolved[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{supplier_name:string,supplier_code:string,dingtalk_group_id:int,dingtalk_group_name:string}>
|
||||
*/
|
||||
public function resolveAll(string $userId, string $unionId = ''): array
|
||||
{
|
||||
$userId = trim($userId);
|
||||
$unionId = trim($unionId);
|
||||
if ($userId === '') {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = $this->queries->forSource('operations_replica');
|
||||
@ -47,17 +60,17 @@ class SupplierPortalAccess
|
||||
[$userId],
|
||||
);
|
||||
if (count($employees) !== 1 || ! (bool) ($employees[0]['active'] ?? false)) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
$employee = $employees[0];
|
||||
$employeeUnionId = trim((string) ($employee['unionid'] ?? ''));
|
||||
if ($employeeUnionId === '' || ($unionId !== '' && ! hash_equals($employeeUnionId, $unionId))) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$departmentIds = $this->departmentIds($employee['dept_id_list'] ?? null);
|
||||
if ($departmentIds === [] || count($departmentIds) > 64) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($departmentIds), '?'));
|
||||
@ -73,7 +86,7 @@ class SupplierPortalAccess
|
||||
);
|
||||
if ($ancestors === [] || count($ancestors) >= 256
|
||||
|| array_filter($ancestors, static fn (array $row): bool => (int) ($row['depth'] ?? 0) >= 20)) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$groupsByName = [];
|
||||
@ -84,7 +97,7 @@ class SupplierPortalAccess
|
||||
}
|
||||
}
|
||||
if ($groupsByName === []) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
|
||||
$supplierRows = $this->suppliers->suppliersByNames(array_keys($groupsByName));
|
||||
@ -95,44 +108,95 @@ class SupplierPortalAccess
|
||||
$matchedSupplierNames[$name] = true;
|
||||
}
|
||||
}
|
||||
if (count($matchedSupplierNames) !== 1) {
|
||||
return null;
|
||||
|
||||
$resolved = [];
|
||||
foreach (array_keys($matchedSupplierNames) as $supplierName) {
|
||||
$matchingRows = array_values(array_filter(
|
||||
$supplierRows,
|
||||
static fn (object $supplier): bool => trim((string) ($supplier->scmname ?? '')) === $supplierName,
|
||||
));
|
||||
if (count($matchingRows) !== 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
usort($groupsByName[$supplierName], static fn (array $left, array $right): int =>
|
||||
(int) ($right['depth'] ?? 0) <=> (int) ($left['depth'] ?? 0));
|
||||
$group = $groupsByName[$supplierName][0];
|
||||
$resolved[] = [
|
||||
'supplier_name' => $supplierName,
|
||||
'supplier_code' => trim((string) ($matchingRows[0]->scmcode ?? '')),
|
||||
'dingtalk_group_id' => (int) ($group['Id'] ?? 0),
|
||||
'dingtalk_group_name' => $supplierName,
|
||||
];
|
||||
}
|
||||
|
||||
$supplierName = (string) array_key_first($matchedSupplierNames);
|
||||
$matchingRows = array_values(array_filter(
|
||||
$supplierRows,
|
||||
static fn (object $supplier): bool => trim((string) ($supplier->scmname ?? '')) === $supplierName,
|
||||
));
|
||||
if (count($matchingRows) !== 1) {
|
||||
return null;
|
||||
}
|
||||
usort($resolved, static fn (array $left, array $right): int =>
|
||||
strnatcasecmp($left['supplier_name'], $right['supplier_name']));
|
||||
|
||||
usort($groupsByName[$supplierName], static fn (array $left, array $right): int =>
|
||||
(int) ($right['depth'] ?? 0) <=> (int) ($left['depth'] ?? 0));
|
||||
$group = $groupsByName[$supplierName][0];
|
||||
|
||||
return [
|
||||
'supplier_name' => $supplierName,
|
||||
'supplier_code' => trim((string) ($matchingRows[0]->scmcode ?? '')),
|
||||
'dingtalk_group_id' => (int) ($group['Id'] ?? 0),
|
||||
'dingtalk_group_name' => $supplierName,
|
||||
];
|
||||
return $resolved;
|
||||
}
|
||||
|
||||
public function bind(User $user, int $companyId, array $resolved): SupplierPortalIdentity
|
||||
{
|
||||
return SupplierPortalIdentity::query()->updateOrCreate(
|
||||
['user_id' => $user->id],
|
||||
[
|
||||
'company_id' => $companyId,
|
||||
'supplier_code' => $resolved['supplier_code'] ?? null,
|
||||
'supplier_name' => $resolved['supplier_name'],
|
||||
'dingtalk_group_id' => (int) $resolved['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $resolved['dingtalk_group_name'],
|
||||
'last_verified_at' => now(),
|
||||
],
|
||||
);
|
||||
$this->bindMany($user, $companyId, [$resolved]);
|
||||
|
||||
return SupplierPortalIdentity::query()->where('user_id', $user->id)->firstOrFail();
|
||||
}
|
||||
|
||||
/** @param list<array{supplier_name:string,supplier_code?:?string,dingtalk_group_id:int,dingtalk_group_name:string}> $resolved */
|
||||
public function bindMany(User $user, int $companyId, array $resolved): Collection
|
||||
{
|
||||
$suppliers = collect($resolved)
|
||||
->filter(static fn (mixed $row): bool => is_array($row) && trim((string) ($row['supplier_name'] ?? '')) !== '')
|
||||
->keyBy(static fn (array $row): string => trim((string) $row['supplier_name']))
|
||||
->sortBy(static fn (array $row): string => trim((string) $row['supplier_name']))
|
||||
->values();
|
||||
if ($suppliers->isEmpty()) {
|
||||
return new Collection();
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($user, $companyId, $suppliers): Collection {
|
||||
$first = $suppliers->first();
|
||||
SupplierPortalIdentity::query()->updateOrCreate(
|
||||
['user_id' => $user->id],
|
||||
[
|
||||
'company_id' => $companyId,
|
||||
'supplier_code' => $first['supplier_code'] ?? null,
|
||||
'supplier_name' => $first['supplier_name'],
|
||||
'dingtalk_group_id' => (int) $first['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $first['dingtalk_group_name'],
|
||||
'last_verified_at' => now(),
|
||||
],
|
||||
);
|
||||
|
||||
foreach ($suppliers as $resolvedSupplier) {
|
||||
SupplierPortalSupplier::query()->updateOrCreate(
|
||||
[
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => $resolvedSupplier['supplier_name'],
|
||||
],
|
||||
[
|
||||
'supplier_code' => $resolvedSupplier['supplier_code'] ?? null,
|
||||
'dingtalk_group_id' => (int) $resolvedSupplier['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $resolvedSupplier['dingtalk_group_name'],
|
||||
'last_verified_at' => now(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
SupplierPortalSupplier::query()
|
||||
->where('company_id', $companyId)
|
||||
->where('user_id', $user->id)
|
||||
->whereNotIn('supplier_name', $suppliers->pluck('supplier_name')->all())
|
||||
->delete();
|
||||
|
||||
return SupplierPortalSupplier::query()
|
||||
->where('company_id', $companyId)
|
||||
->where('user_id', $user->id)
|
||||
->orderBy('supplier_name')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
public function mappingFor(User $user): ?SupplierPortalIdentity
|
||||
@ -149,10 +213,39 @@ class SupplierPortalAccess
|
||||
|
||||
public function supplierFor(User $user): ?string
|
||||
{
|
||||
$mapping = $this->mappingFor($user);
|
||||
$name = trim((string) ($mapping?->supplier_name ?? ''));
|
||||
$names = $this->supplierNamesFor($user);
|
||||
|
||||
return $name !== '' ? $name : null;
|
||||
return count($names) === 1 ? $names[0] : null;
|
||||
}
|
||||
|
||||
public function bindingsFor(User $user): Collection
|
||||
{
|
||||
if (! self::isSupplierUser($user)) {
|
||||
return new Collection();
|
||||
}
|
||||
|
||||
return SupplierPortalSupplier::query()
|
||||
->where('company_id', company_id())
|
||||
->where('user_id', $user->id)
|
||||
->orderBy('supplier_name')
|
||||
->get();
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function supplierNamesFor(User $user): array
|
||||
{
|
||||
return $this->bindingsFor($user)
|
||||
->pluck('supplier_name')
|
||||
->map(static fn (mixed $name): string => trim((string) $name))
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function canAccessSupplier(User $user, string $supplier): bool
|
||||
{
|
||||
return in_array(trim($supplier), $this->supplierNamesFor($user), true);
|
||||
}
|
||||
|
||||
public function revalidate(User $user): ?SupplierPortalIdentity
|
||||
@ -167,12 +260,23 @@ class SupplierPortalAccess
|
||||
return null;
|
||||
}
|
||||
|
||||
$resolved = $this->resolve(
|
||||
$resolved = $this->resolveAll(
|
||||
(string) $dingTalk->dingtalk_user_id,
|
||||
(string) $dingTalk->union_id,
|
||||
);
|
||||
|
||||
return $resolved ? $this->bind($user, (int) $company->id, $resolved) : null;
|
||||
if ($resolved === []) {
|
||||
SupplierPortalSupplier::query()
|
||||
->where('company_id', $company->id)
|
||||
->where('user_id', $user->id)
|
||||
->delete();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->bindMany($user, (int) $company->id, $resolved);
|
||||
|
||||
return $this->mappingFor($user);
|
||||
}
|
||||
|
||||
private function departmentIds(mixed $value): array
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (Schema::hasTable('supplier_portal_suppliers')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::create('supplier_portal_suppliers', function (Blueprint $table): void {
|
||||
$table->bigIncrements('id');
|
||||
$table->unsignedInteger('company_id');
|
||||
$table->unsignedInteger('user_id');
|
||||
$table->string('supplier_code', 191)->nullable();
|
||||
$table->string('supplier_name', 191);
|
||||
$table->unsignedBigInteger('dingtalk_group_id');
|
||||
$table->string('dingtalk_group_name', 191);
|
||||
$table->timestamp('last_verified_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(
|
||||
['company_id', 'user_id', 'supplier_name'],
|
||||
'supplier_portal_supplier_company_user_name',
|
||||
);
|
||||
$table->index(
|
||||
['company_id', 'supplier_name'],
|
||||
'supplier_portal_supplier_company_name',
|
||||
);
|
||||
$table->index(
|
||||
['company_id', 'dingtalk_group_id'],
|
||||
'supplier_portal_supplier_company_group',
|
||||
);
|
||||
});
|
||||
|
||||
DB::table('supplier_portal_identities')
|
||||
->orderBy('id')
|
||||
->chunkById(500, function ($rows): void {
|
||||
$now = now();
|
||||
$values = [];
|
||||
foreach ($rows as $row) {
|
||||
$values[] = [
|
||||
'company_id' => $row->company_id,
|
||||
'user_id' => $row->user_id,
|
||||
'supplier_code' => $row->supplier_code,
|
||||
'supplier_name' => $row->supplier_name,
|
||||
'dingtalk_group_id' => $row->dingtalk_group_id,
|
||||
'dingtalk_group_name' => $row->dingtalk_group_name,
|
||||
'last_verified_at' => $row->last_verified_at,
|
||||
'created_at' => $row->created_at,
|
||||
'updated_at' => $row->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
if ($values !== []) {
|
||||
DB::table('supplier_portal_suppliers')->insertOrIgnore($values);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('supplier_portal_suppliers');
|
||||
}
|
||||
};
|
||||
@ -248,12 +248,30 @@ class SupplierReconciliationPageData
|
||||
$companyId = $this->companyId();
|
||||
$portalUser = request()->user();
|
||||
$supplierLocked = $portalUser && SupplierPortalAccess::isSupplierUser($portalUser);
|
||||
$supplierBindings = $supplierLocked
|
||||
? $this->supplierPortalAccess->bindingsFor($portalUser)
|
||||
: collect();
|
||||
$supplierCount = $supplierBindings->count();
|
||||
$supplierNames = [];
|
||||
$supplierNotFound = false;
|
||||
|
||||
if ($supplierLocked) {
|
||||
$selected = $this->supplierPortalAccess->supplierFor($portalUser);
|
||||
abort_if(! $selected, 403, '当前钉钉账号未绑定有效供应商,请联系财务人员。');
|
||||
abort_if($supplierCount === 0, 403, '当前钉钉账号未绑定有效供应商,请联系财务人员。');
|
||||
$supplierNames = $supplierBindings
|
||||
->pluck('supplier_name')
|
||||
->map(static fn (mixed $name): string => trim((string) $name))
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
$selected = $supplierCount === 1
|
||||
? $supplierNames[0]
|
||||
: trim((string) request()->query('supplier', ''));
|
||||
abort_if(
|
||||
$selected !== '' && ! in_array($selected, $supplierNames, true),
|
||||
403,
|
||||
'该供应商不在当前账号的授权范围内。',
|
||||
);
|
||||
$supplierQuery = $selected;
|
||||
} else {
|
||||
$mine = $this->store->listRows(
|
||||
@ -303,6 +321,8 @@ class SupplierReconciliationPageData
|
||||
'supplierNotFound' => $supplierNotFound,
|
||||
'selectedSupplier' => $selected,
|
||||
'supplierLocked' => $supplierLocked,
|
||||
'supplierBindings' => $supplierBindings,
|
||||
'supplierCount' => $supplierCount,
|
||||
'uploads' => $selected !== '' ? $this->statements->uploads($selected, $companyId) : [],
|
||||
'statementRows' => array_slice($statementRows, ($page - 1) * self::PER_PAGE, self::PER_PAGE),
|
||||
'statementTotal' => count($statementRows),
|
||||
|
||||
@ -22,6 +22,13 @@
|
||||
.supplier-reconciliation-page .sr-locked-supplier { display: grid; gap: 4px; min-width: 260px; }
|
||||
.supplier-reconciliation-page .sr-locked-supplier span { color: var(--vip-content-muted, #595959); font-size: .75rem; font-weight: 600; }
|
||||
.supplier-reconciliation-page .sr-locked-supplier strong { min-height: 36px; padding: 8px 10px; border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; background: var(--vip-content-bg, #F8F6F1); font-size: .8125rem; font-weight: 600; }
|
||||
.supplier-reconciliation-page .sr-supplier-picker { display: grid; gap: 10px; }
|
||||
.supplier-reconciliation-page .sr-supplier-options { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
|
||||
.supplier-reconciliation-page .sr-supplier-option { display: grid; gap: 5px; padding: 14px; border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; background: #fff; color: var(--vip-content-ink, #171717); text-decoration: none; }
|
||||
.supplier-reconciliation-page .sr-supplier-option:hover,
|
||||
.supplier-reconciliation-page .sr-supplier-option:focus-visible { border-color: var(--vip-content-accent-dark, #856632); background: var(--vip-content-accent-soft, #F7F0DE); }
|
||||
.supplier-reconciliation-page .sr-supplier-option strong { font-size: .875rem; }
|
||||
.supplier-reconciliation-page .sr-supplier-option span { color: var(--vip-content-muted, #595959); font-size: .75rem; }
|
||||
.supplier-reconciliation-page .sr-scroll { min-width: 0; max-width: 100%; overflow-x: auto; scrollbar-width: thin; }
|
||||
.supplier-reconciliation-page table { width: 100%; min-width: 720px; border-collapse: collapse; font-size: .8125rem; }
|
||||
.supplier-reconciliation-page th { padding: 8px 10px; background: var(--vip-content-accent-soft, #F7F0DE); border-bottom: 1px solid var(--vip-border, #D8D2C7); color: #5B5343; font-size: .75rem; font-weight: 600; text-align: left; white-space: nowrap; }
|
||||
@ -94,13 +101,39 @@
|
||||
<div class="sr-card">
|
||||
<h2>厂家对账单 · 上传与实时数据</h2>
|
||||
@if ($supplierLocked)
|
||||
<div class="sr-toolbar" aria-label="当前厂家">
|
||||
<div class="sr-locked-supplier">
|
||||
<span>当前厂家(由钉钉组织自动确定)</span>
|
||||
<strong>{{ $selectedSupplier }}</strong>
|
||||
@if ($selectedSupplier !== '')
|
||||
<div class="sr-toolbar" aria-label="当前厂家">
|
||||
<div class="sr-locked-supplier">
|
||||
<span>当前厂家(由钉钉组织自动确定)</span>
|
||||
<strong>{{ $selectedSupplier }}</strong>
|
||||
</div>
|
||||
<a href="{{ $statementTemplateUrl }}" class="sr-button">下载对账单模板</a>
|
||||
@if (($supplierCount ?? 1) > 1)
|
||||
<a href="{{ route('business.pages.show', ['section' => $section['slug'], 'page' => $page['slug']]) }}"
|
||||
class="sr-button">切换厂家</a>
|
||||
@endif
|
||||
</div>
|
||||
<a href="{{ $statementTemplateUrl }}" class="sr-button">下载对账单模板</a>
|
||||
</div>
|
||||
@else
|
||||
<div class="sr-supplier-picker">
|
||||
<div>
|
||||
<strong>请选择厂家</strong>
|
||||
<p class="sr-hint">当前钉钉账号可操作 {{ number_format($supplierCount) }} 家供应商,进入后只能查看和操作所选厂家的数据。</p>
|
||||
</div>
|
||||
<div class="sr-supplier-options">
|
||||
@foreach ($supplierBindings as $supplierBinding)
|
||||
<a class="sr-supplier-option"
|
||||
href="{{ route('business.pages.show', [
|
||||
'section' => $section['slug'],
|
||||
'page' => $page['slug'],
|
||||
'supplier' => $supplierBinding->supplier_name,
|
||||
]) }}">
|
||||
<strong>{{ $supplierBinding->supplier_name }}</strong>
|
||||
<span>{{ filled($supplierBinding->supplier_code) ? '供应商代码:' . $supplierBinding->supplier_code : '已通过钉钉组织验证' }}</span>
|
||||
</a>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
@else
|
||||
<form method="GET" class="sr-toolbar" aria-label="查询厂家">
|
||||
<label class="sr-field"><span>厂家名称</span>
|
||||
|
||||
@ -6,12 +6,14 @@ use App\Models\Auth\DingTalkBindingRequest;
|
||||
use App\Models\Auth\DingTalkIdentity;
|
||||
use App\Models\Auth\DingTalkOAuthState;
|
||||
use App\Models\Auth\SupplierPortalIdentity;
|
||||
use App\Models\Auth\SupplierPortalSupplier;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Services\Auth\FinanceRoster;
|
||||
use App\Services\Auth\SupplierPortalAccess;
|
||||
use App\Utilities\SidebarPermissions;
|
||||
use Illuminate\Http\Client\Request as ClientRequest;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
@ -43,7 +45,7 @@ class DingTalkLoginTest extends FeatureTestCase
|
||||
$this->mock(FinanceRoster::class)->shouldReceive('findEligible')->andReturn([
|
||||
'roster_id' => 'fixture-1', 'display_name' => '财务测试员工', 'department_ids' => ['22848291'],
|
||||
])->byDefault();
|
||||
$this->mock(SupplierPortalAccess::class)->shouldReceive('resolve')->andReturnNull()->byDefault();
|
||||
$this->mock(SupplierPortalAccess::class)->shouldReceive('resolveAll')->andReturn([])->byDefault();
|
||||
}
|
||||
|
||||
public function test_login_page_shows_dingtalk_entry_without_password_binding_fields(): void
|
||||
@ -273,27 +275,48 @@ class DingTalkLoginTest extends FeatureTestCase
|
||||
$this->mock(FinanceRoster::class)
|
||||
->shouldReceive('findEligible')->with('ding-user-1')->andReturnNull();
|
||||
$supplierAccess = $this->mock(SupplierPortalAccess::class);
|
||||
$supplierAccess->shouldReceive('resolve')
|
||||
$supplierAccess->shouldReceive('resolveAll')
|
||||
->once()
|
||||
->with('ding-user-1', 'union-supplier')
|
||||
->andReturn([
|
||||
'supplier_name' => '晨丰',
|
||||
'supplier_code' => 'CF-001',
|
||||
'dingtalk_group_id' => 902833372,
|
||||
'dingtalk_group_name' => '晨丰',
|
||||
[
|
||||
'supplier_name' => '晨丰',
|
||||
'supplier_code' => 'CF-001',
|
||||
'dingtalk_group_id' => 902833372,
|
||||
'dingtalk_group_name' => '晨丰',
|
||||
],
|
||||
[
|
||||
'supplier_name' => '鸣飞家具',
|
||||
'supplier_code' => 'MF-001',
|
||||
'dingtalk_group_id' => 902833373,
|
||||
'dingtalk_group_name' => '鸣飞家具',
|
||||
],
|
||||
]);
|
||||
$supplierAccess->shouldReceive('bind')
|
||||
$supplierAccess->shouldReceive('bindMany')
|
||||
->once()
|
||||
->andReturnUsing(function (User $user, int $companyId, array $resolved): SupplierPortalIdentity {
|
||||
return SupplierPortalIdentity::create([
|
||||
->andReturnUsing(function (User $user, int $companyId, array $resolved): Collection {
|
||||
SupplierPortalIdentity::create([
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_code' => $resolved['supplier_code'],
|
||||
'supplier_name' => $resolved['supplier_name'],
|
||||
'dingtalk_group_id' => $resolved['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $resolved['dingtalk_group_name'],
|
||||
'supplier_code' => $resolved[0]['supplier_code'],
|
||||
'supplier_name' => $resolved[0]['supplier_name'],
|
||||
'dingtalk_group_id' => $resolved[0]['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $resolved[0]['dingtalk_group_name'],
|
||||
'last_verified_at' => now(),
|
||||
]);
|
||||
|
||||
return new Collection(array_map(
|
||||
fn (array $supplier): SupplierPortalSupplier => SupplierPortalSupplier::create([
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_code' => $supplier['supplier_code'],
|
||||
'supplier_name' => $supplier['supplier_name'],
|
||||
'dingtalk_group_id' => $supplier['dingtalk_group_id'],
|
||||
'dingtalk_group_name' => $supplier['dingtalk_group_name'],
|
||||
'last_verified_at' => now(),
|
||||
]),
|
||||
$resolved,
|
||||
));
|
||||
});
|
||||
|
||||
Http::preventStrayRequests();
|
||||
@ -315,6 +338,18 @@ class DingTalkLoginTest extends FeatureTestCase
|
||||
'supplier_name' => '晨丰',
|
||||
'supplier_code' => 'CF-001',
|
||||
]);
|
||||
$this->assertDatabaseHas('supplier_portal_suppliers', [
|
||||
'company_id' => $this->company->id,
|
||||
'user_id' => $created->id,
|
||||
'supplier_name' => '晨丰',
|
||||
'supplier_code' => 'CF-001',
|
||||
]);
|
||||
$this->assertDatabaseHas('supplier_portal_suppliers', [
|
||||
'company_id' => $this->company->id,
|
||||
'user_id' => $created->id,
|
||||
'supplier_name' => '鸣飞家具',
|
||||
'supplier_code' => 'MF-001',
|
||||
]);
|
||||
$this->assertDatabaseMissing('contacts', ['user_id' => $created->id]);
|
||||
}
|
||||
|
||||
|
||||
@ -7,6 +7,7 @@ use App\Models\Auth\DingTalkIdentity;
|
||||
use App\Models\Auth\Permission;
|
||||
use App\Models\Auth\Role;
|
||||
use App\Models\Auth\SupplierPortalIdentity;
|
||||
use App\Models\Auth\SupplierPortalSupplier;
|
||||
use App\Models\Auth\User;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Services\Auth\SupplierPortalAccess;
|
||||
@ -408,6 +409,68 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_supplier_portal_user_can_choose_between_their_suppliers(): void
|
||||
{
|
||||
config(['app.throttles.import' => 100]);
|
||||
$this->seedStatement('晨丰', [
|
||||
$this->statementRow('CF-1', '128.00', '2', '256.00'),
|
||||
]);
|
||||
$this->seedStatement('鸣飞家具', [
|
||||
array_merge($this->statementRow('MF-1', '96.00', '1', '96.00'), ['supplier' => '鸣飞家具']),
|
||||
]);
|
||||
$supplierUser = $this->createSupplierPortalUser('晨丰');
|
||||
$this->bindSupplierPortalSupplier($supplierUser, '鸣飞家具', 'MF-001', 2);
|
||||
$pageUrl = route('business.pages.show', [
|
||||
'company_id' => $this->company->id,
|
||||
'section' => 'supplier-reconciliation',
|
||||
'page' => 'supplier-data',
|
||||
]);
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)->get($pageUrl)
|
||||
->assertOk()
|
||||
->assertSeeText('请选择厂家')
|
||||
->assertSeeText('当前钉钉账号可操作 2 家供应商')
|
||||
->assertSeeText('晨丰')
|
||||
->assertSeeText('鸣飞家具')
|
||||
->assertDontSeeText('当前厂家(由钉钉组织自动确定)');
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->get($pageUrl . '?supplier=' . urlencode('鸣飞家具'))
|
||||
->assertOk()
|
||||
->assertSeeText('当前厂家(由钉钉组织自动确定)')
|
||||
->assertSeeText('鸣飞家具')
|
||||
->assertSeeText('切换厂家')
|
||||
->assertSeeText('鸣飞家具 · 上传的对账数据')
|
||||
->assertDontSeeText('晨丰 · 上传的对账数据');
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->post(route('business.supplier-reconciliation.statement.upload', ['company_id' => $this->company->id]), [
|
||||
'supplier' => '鸣飞家具',
|
||||
'import' => UploadedFile::fake()->createWithContent(
|
||||
'statement.csv',
|
||||
"\xEF\xBB\xBF" . self::HEADER . "\n"
|
||||
. ",,MF-UPLOAD,2026-09-30,鸣飞家具,MF-2,规格乙,1,96.00,96.00\n",
|
||||
),
|
||||
])
|
||||
->assertStatus(302)
|
||||
->assertSessionHas('success', '对账单已保存:鸣飞家具 共 1 行。');
|
||||
|
||||
$this->withExceptionHandling();
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->get($pageUrl . '?supplier=' . urlencode('未授权厂家'))
|
||||
->assertForbidden();
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->post(route('business.supplier-reconciliation.statement.upload', ['company_id' => $this->company->id]), [
|
||||
'supplier' => '未授权厂家',
|
||||
'import' => UploadedFile::fake()->createWithContent(
|
||||
'statement.csv',
|
||||
"\xEF\xBB\xBF" . self::HEADER . "\n"
|
||||
. ",,OTHER-UPLOAD,2026-09-30,未授权厂家,OTHER-1,规格丙,1,10.00,10.00\n",
|
||||
),
|
||||
])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_supplier_portal_upload_and_return_actions_ignore_spoofed_supplier(): void
|
||||
{
|
||||
$supplierUser = $this->createSupplierPortalUser('晨丰');
|
||||
@ -1333,10 +1396,28 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
'dingtalk_group_name' => $supplier,
|
||||
'last_verified_at' => now(),
|
||||
]);
|
||||
$this->bindSupplierPortalSupplier($supplierUser, $supplier, 'SUPPLIER-TEST', 1);
|
||||
|
||||
return $supplierUser;
|
||||
}
|
||||
|
||||
private function bindSupplierPortalSupplier(
|
||||
User $user,
|
||||
string $supplier,
|
||||
string $supplierCode,
|
||||
int $dingtalkGroupId,
|
||||
): SupplierPortalSupplier {
|
||||
return SupplierPortalSupplier::create([
|
||||
'company_id' => $this->company->id,
|
||||
'user_id' => $user->id,
|
||||
'supplier_code' => $supplierCode,
|
||||
'supplier_name' => $supplier,
|
||||
'dingtalk_group_id' => $dingtalkGroupId,
|
||||
'dingtalk_group_name' => $supplier,
|
||||
'last_verified_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function actingAsSupplierPortal(User $user): self
|
||||
{
|
||||
$identity = DingTalkIdentity::query()->where('user_id', $user->id)->firstOrFail();
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Tests\Unit\Auth;
|
||||
|
||||
use App\Models\Auth\SupplierPortalIdentity;
|
||||
use App\Models\Auth\SupplierPortalSupplier;
|
||||
use App\Services\Auth\SupplierPortalAccess;
|
||||
use Mockery;
|
||||
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
|
||||
@ -68,7 +70,7 @@ class SupplierPortalAccessTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_multiple_supplier_ancestors_are_ambiguous_and_denied(): void
|
||||
public function test_multiple_supplier_ancestors_resolve_to_all_suppliers(): void
|
||||
{
|
||||
$query = Mockery::mock(UpstreamReadOnlyQuery::class);
|
||||
$query->shouldReceive('select')->twice()->andReturnUsing(function (string $sql): array {
|
||||
@ -90,7 +92,12 @@ class SupplierPortalAccessTest extends TestCase
|
||||
(object) ['Id' => 2, 'scmcode' => 'MF', 'scmname' => '鸣飞家具'],
|
||||
]);
|
||||
|
||||
$this->assertNull((new SupplierPortalAccess($factory, $suppliers))->resolve('ding-supplier', 'union-supplier'));
|
||||
$access = new SupplierPortalAccess($factory, $suppliers);
|
||||
|
||||
$this->assertSame(
|
||||
['晨丰', '鸣飞家具'],
|
||||
array_column($access->resolveAll('ding-supplier', 'union-supplier'), 'supplier_name'),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_organization_cycle_is_rejected(): void
|
||||
@ -111,4 +118,50 @@ class SupplierPortalAccessTest extends TestCase
|
||||
|
||||
$this->assertNull((new SupplierPortalAccess($factory, $suppliers))->resolve('ding-supplier', 'union-supplier'));
|
||||
}
|
||||
|
||||
public function test_binding_multiple_suppliers_keeps_all_and_prunes_only_removed_ones(): void
|
||||
{
|
||||
$user = user_model_class()::query()->firstOrFail();
|
||||
$companyId = (int) $user->companies()->firstOrFail()->id;
|
||||
$access = new SupplierPortalAccess(
|
||||
Mockery::mock(UpstreamReadOnlyQueryFactory::class),
|
||||
Mockery::mock(SupplierManagementMirrorRepository::class),
|
||||
);
|
||||
|
||||
$access->bindMany($user, $companyId, [
|
||||
[
|
||||
'supplier_name' => '晨丰',
|
||||
'supplier_code' => 'CF-001',
|
||||
'dingtalk_group_id' => 10,
|
||||
'dingtalk_group_name' => '晨丰',
|
||||
],
|
||||
[
|
||||
'supplier_name' => '鸣飞家具',
|
||||
'supplier_code' => 'MF-001',
|
||||
'dingtalk_group_id' => 20,
|
||||
'dingtalk_group_name' => '鸣飞家具',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(2, SupplierPortalSupplier::query()->where('user_id', $user->id)->count());
|
||||
$this->assertSame(1, SupplierPortalIdentity::query()->where('user_id', $user->id)->count());
|
||||
|
||||
$access->bindMany($user, $companyId, [[
|
||||
'supplier_name' => '鸣飞家具',
|
||||
'supplier_code' => 'MF-001',
|
||||
'dingtalk_group_id' => 20,
|
||||
'dingtalk_group_name' => '鸣飞家具',
|
||||
]]);
|
||||
|
||||
$this->assertDatabaseMissing('supplier_portal_suppliers', [
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => '晨丰',
|
||||
]);
|
||||
$this->assertDatabaseHas('supplier_portal_suppliers', [
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => '鸣飞家具',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user