Merge remote-tracking branch 'origin/main' into feature/limeng-portal-freight-bills
This commit is contained in:
commit
adf8137057
@ -13,11 +13,13 @@ class SupplierPortalSupplier extends Model
|
||||
'supplier_name',
|
||||
'dingtalk_group_id',
|
||||
'dingtalk_group_name',
|
||||
'manually_bound',
|
||||
'last_verified_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'dingtalk_group_id' => 'integer',
|
||||
'manually_bound' => 'boolean',
|
||||
'last_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ use App\Models\Auth\SupplierPortalSupplier;
|
||||
use App\Models\Auth\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use InvalidArgumentException;
|
||||
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
|
||||
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
|
||||
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
||||
@ -223,24 +224,27 @@ class SupplierPortalAccess
|
||||
);
|
||||
|
||||
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(),
|
||||
],
|
||||
);
|
||||
$binding = SupplierPortalSupplier::query()->firstOrNew([
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => $resolvedSupplier['supplier_name'],
|
||||
]);
|
||||
$binding->fill([
|
||||
'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(),
|
||||
]);
|
||||
if (! $binding->exists) {
|
||||
$binding->manually_bound = false;
|
||||
}
|
||||
$binding->save();
|
||||
}
|
||||
|
||||
SupplierPortalSupplier::query()
|
||||
->where('company_id', $companyId)
|
||||
->where('user_id', $user->id)
|
||||
->where('manually_bound', false)
|
||||
->whereNotIn('supplier_name', $suppliers->pluck('supplier_name')->all())
|
||||
->delete();
|
||||
|
||||
@ -252,6 +256,41 @@ class SupplierPortalAccess
|
||||
});
|
||||
}
|
||||
|
||||
public function bindManualSupplier(
|
||||
User $user,
|
||||
int $companyId,
|
||||
string $supplierName,
|
||||
): SupplierPortalSupplier {
|
||||
$supplierName = trim($supplierName);
|
||||
if ($supplierName === '') {
|
||||
throw new InvalidArgumentException('供应商名称不能为空。');
|
||||
}
|
||||
|
||||
$matches = array_values(array_filter(
|
||||
$this->suppliers->suppliersByNames([$supplierName]),
|
||||
static fn (object $supplier): bool =>
|
||||
trim((string) ($supplier->scmname ?? '')) === $supplierName,
|
||||
));
|
||||
if (count($matches) !== 1) {
|
||||
throw new InvalidArgumentException('供应商主档中必须存在唯一同名厂家。');
|
||||
}
|
||||
|
||||
return SupplierPortalSupplier::query()->updateOrCreate(
|
||||
[
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => $supplierName,
|
||||
],
|
||||
[
|
||||
'supplier_code' => trim((string) ($matches[0]->scmcode ?? '')),
|
||||
'dingtalk_group_id' => 0,
|
||||
'dingtalk_group_name' => $supplierName,
|
||||
'manually_bound' => true,
|
||||
'last_verified_at' => now(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function mappingFor(User $user): ?SupplierPortalIdentity
|
||||
{
|
||||
if (! self::isSupplierUser($user)) {
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('supplier_portal_suppliers')
|
||||
|| Schema::hasColumn('supplier_portal_suppliers', 'manually_bound')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('supplier_portal_suppliers', function (Blueprint $table): void {
|
||||
$table->boolean('manually_bound')
|
||||
->default(false)
|
||||
->after('dingtalk_group_name');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('supplier_portal_suppliers')
|
||||
|| ! Schema::hasColumn('supplier_portal_suppliers', 'manually_bound')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('supplier_portal_suppliers', function (Blueprint $table): void {
|
||||
$table->dropColumn('manually_bound');
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -168,6 +168,74 @@ class SupplierPortalAccessTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_manual_binding_survives_automatic_rebinding_and_pruning(): void
|
||||
{
|
||||
$user = user_model_class()::query()->firstOrFail();
|
||||
$companyId = (int) $user->companies()->firstOrFail()->id;
|
||||
$access = new SupplierPortalAccess(
|
||||
Mockery::mock(UpstreamReadOnlyQueryFactory::class),
|
||||
Mockery::mock(SupplierManagementMirrorRepository::class),
|
||||
);
|
||||
|
||||
SupplierPortalSupplier::create([
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => '河北乐鑫达',
|
||||
'supplier_code' => 'N602',
|
||||
'dingtalk_group_id' => 0,
|
||||
'dingtalk_group_name' => '河北乐鑫达',
|
||||
'manually_bound' => true,
|
||||
'last_verified_at' => now(),
|
||||
]);
|
||||
|
||||
$access->bindMany($user, $companyId, [[
|
||||
'supplier_name' => '河北雅柏',
|
||||
'supplier_code' => 'N1201',
|
||||
'dingtalk_group_id' => 965879834,
|
||||
'dingtalk_group_name' => '河北雅柏',
|
||||
]]);
|
||||
|
||||
$this->assertEqualsCanonicalizing(
|
||||
['河北乐鑫达', '河北雅柏'],
|
||||
SupplierPortalSupplier::query()
|
||||
->where('company_id', $companyId)
|
||||
->where('user_id', $user->id)
|
||||
->pluck('supplier_name')
|
||||
->all(),
|
||||
);
|
||||
$this->assertDatabaseHas('supplier_portal_suppliers', [
|
||||
'company_id' => $companyId,
|
||||
'user_id' => $user->id,
|
||||
'supplier_name' => '河北乐鑫达',
|
||||
'manually_bound' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_manual_binding_uses_unique_supplier_master_row(): void
|
||||
{
|
||||
$user = user_model_class()::query()->firstOrFail();
|
||||
$companyId = (int) $user->companies()->firstOrFail()->id;
|
||||
$suppliers = Mockery::mock(SupplierManagementMirrorRepository::class);
|
||||
$suppliers->shouldReceive('suppliersByNames')->once()
|
||||
->with(['河北乐鑫达'])
|
||||
->andReturn([(object) [
|
||||
'Id' => 7959,
|
||||
'scmcode' => 'N602',
|
||||
'scmname' => '河北乐鑫达',
|
||||
]]);
|
||||
$access = new SupplierPortalAccess(
|
||||
Mockery::mock(UpstreamReadOnlyQueryFactory::class),
|
||||
$suppliers,
|
||||
);
|
||||
|
||||
$binding = $access->bindManualSupplier($user, $companyId, '河北乐鑫达');
|
||||
|
||||
$this->assertSame('N602', $binding->supplier_code);
|
||||
$this->assertSame('河北乐鑫达', $binding->supplier_name);
|
||||
$this->assertTrue($binding->manually_bound);
|
||||
$this->assertSame(0, $binding->dingtalk_group_id);
|
||||
}
|
||||
|
||||
public function test_current_dingtalk_departments_resolve_when_hr_employee_is_missing(): void
|
||||
{
|
||||
$factory = Mockery::mock(UpstreamReadOnlyQueryFactory::class);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user