Merge Vendors
This commit is contained in:
parent
141f45b4ee
commit
310353ab28
|
|
@ -11,27 +11,28 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\Vendor\VendorWasCreated;
|
||||
use App\Events\Vendor\VendorWasUpdated;
|
||||
use App\Utils\Ninja;
|
||||
use App\Models\Vendor;
|
||||
use App\Models\Account;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Factory\VendorFactory;
|
||||
use App\Filters\VendorFilters;
|
||||
use App\Http\Requests\Vendor\CreateVendorRequest;
|
||||
use App\Http\Requests\Vendor\DestroyVendorRequest;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\Uploadable;
|
||||
use App\Utils\Traits\BulkOptions;
|
||||
use App\Utils\Traits\SavesDocuments;
|
||||
use App\Repositories\VendorRepository;
|
||||
use App\Events\Vendor\VendorWasCreated;
|
||||
use App\Events\Vendor\VendorWasUpdated;
|
||||
use App\Transformers\VendorTransformer;
|
||||
use App\Http\Requests\Vendor\EditVendorRequest;
|
||||
use App\Http\Requests\Vendor\ShowVendorRequest;
|
||||
use App\Http\Requests\Vendor\PurgeVendorRequest;
|
||||
use App\Http\Requests\Vendor\StoreVendorRequest;
|
||||
use App\Http\Requests\Vendor\CreateVendorRequest;
|
||||
use App\Http\Requests\Vendor\UpdateVendorRequest;
|
||||
use App\Http\Requests\Vendor\UploadVendorRequest;
|
||||
use App\Models\Account;
|
||||
use App\Models\Vendor;
|
||||
use App\Repositories\VendorRepository;
|
||||
use App\Transformers\VendorTransformer;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\BulkOptions;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\SavesDocuments;
|
||||
use App\Utils\Traits\Uploadable;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Requests\Vendor\DestroyVendorRequest;
|
||||
|
||||
/**
|
||||
* Class VendorController.
|
||||
|
|
@ -584,4 +585,38 @@ class VendorController extends BaseController
|
|||
|
||||
return $this->itemResponse($vendor->fresh());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param PurgeVendorRequest $request
|
||||
* @param Vendor $vendor
|
||||
* @param string $mergeable_vendor
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
||||
*
|
||||
*/
|
||||
|
||||
public function merge(PurgeVendorRequest $request, Vendor $vendor, string $mergeable_vendor)
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
$m_vendor = Vendor::withTrashed()
|
||||
->where('id', $this->decodePrimaryKey($mergeable_vendor))
|
||||
->where('company_id', $user->company()->id)
|
||||
->first();
|
||||
|
||||
if (!$m_vendor) {
|
||||
return response()->json(['message' => "Vendor not found"], 400);
|
||||
}
|
||||
|
||||
if($m_vendor->id == $vendor->id) {
|
||||
return response()->json(['message' => "Attempting to merge the same vendor is not possible."], 400);
|
||||
}
|
||||
|
||||
$merged_vendor = $vendor->service()->merge($m_vendor)->save();
|
||||
|
||||
return $this->itemResponse($merged_vendor);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests\Vendor;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
|
||||
class PurgeVendorRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
|
||||
return $user->isAdmin();
|
||||
}
|
||||
}
|
||||
|
|
@ -296,4 +296,29 @@ class Vendor extends BaseModel
|
|||
{
|
||||
return new VendorService($this);
|
||||
}
|
||||
|
||||
public function credits(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Credit::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function expenses(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Expense::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function invoices(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Invoice::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function payments(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Payment::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function quotes(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Quote::class)->withTrashed();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Services\Vendor;
|
||||
|
||||
use App\Factory\CompanyLedgerFactory;
|
||||
use App\Models\Activity;
|
||||
use App\Models\Vendor;
|
||||
use App\Models\CompanyLedger;
|
||||
use App\Services\AbstractService;
|
||||
|
||||
class Merge extends AbstractService
|
||||
{
|
||||
public $vendor;
|
||||
|
||||
public $mergable_vendor;
|
||||
|
||||
public function __construct(Vendor $vendor, Vendor $mergable_vendor)
|
||||
{
|
||||
$this->vendor = $vendor;
|
||||
$this->mergable_vendor = $mergable_vendor;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
|
||||
$this->mergable_vendor->activities()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->contacts()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->credits()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->expenses()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->invoices()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->payments()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->quotes()->update(['vendor_id' => $this->vendor->id]);
|
||||
$this->mergable_vendor->documents()->update(['documentable_id' => $this->vendor->id]);
|
||||
|
||||
/* Loop through contacts an only merge distinct contacts by email */
|
||||
$this->mergable_vendor->contacts->each(function ($contact) {
|
||||
$exist = $this->vendor->contacts->contains(function ($vendor_contact) use ($contact) {
|
||||
return $vendor_contact->email == $contact->email || empty($contact->email) || $contact->email == ' ';
|
||||
});
|
||||
|
||||
if ($exist) {
|
||||
$contact->delete();
|
||||
$contact->save();
|
||||
}
|
||||
});
|
||||
|
||||
$this->mergable_vendor->forceDelete();
|
||||
|
||||
return $this->vendor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -51,6 +51,13 @@ class VendorService
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function merge(Vendor $mergable_vendor)
|
||||
{
|
||||
$this->vendor = (new Merge($this->vendor, $mergable_vendor))->run();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the vendor instance
|
||||
*
|
||||
|
|
|
|||
|
|
@ -387,6 +387,7 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale']
|
|||
Route::resource('vendors', VendorController::class); // name = (vendors. index / create / show / update / destroy / edit
|
||||
Route::post('vendors/bulk', [VendorController::class, 'bulk'])->name('vendors.bulk');
|
||||
Route::put('vendors/{vendor}/upload', [VendorController::class, 'upload']);
|
||||
Route::post('vendors/{vendor}/{mergeable_vendor}/merge', [VendorController::class, 'merge'])->name('vendors.merge')->middleware('password_protected');
|
||||
|
||||
Route::get('users', [UserController::class, 'index']);
|
||||
Route::get('users/create', [UserController::class, 'create'])->middleware('password_protected');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace Tests\Feature\Vendor;
|
||||
|
||||
use App\Models\Account;
|
||||
use App\Models\Vendor;
|
||||
use App\Models\VendorContact;
|
||||
use App\Models\Company;
|
||||
use App\Models\User;
|
||||
use App\Utils\Traits\AppSetup;
|
||||
use Faker\Factory;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\TestCase;
|
||||
|
||||
class VendorMergeTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use AppSetup;
|
||||
|
||||
private $user;
|
||||
|
||||
private $company;
|
||||
|
||||
private $account;
|
||||
|
||||
public $vendor;
|
||||
|
||||
private $primary_contact;
|
||||
|
||||
public $faker;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->faker = Factory::create();
|
||||
}
|
||||
|
||||
public function testSearchingForContacts()
|
||||
{
|
||||
$account = Account::factory()->create();
|
||||
|
||||
$this->user = User::factory()->create([
|
||||
'account_id' => $account->id,
|
||||
'email' => $this->faker->safeEmail(),
|
||||
]);
|
||||
|
||||
$this->company = Company::factory()->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$this->vendor = Vendor::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'company_id' => $this->company->id,
|
||||
]);
|
||||
|
||||
$this->primary_contact = VendorContact::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'vendor_id' => $this->vendor->id,
|
||||
'company_id' => $this->company->id,
|
||||
'is_primary' => 1,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->count(2)->create([
|
||||
'user_id' => $this->user->id,
|
||||
'vendor_id' => $this->vendor->id,
|
||||
'company_id' => $this->company->id,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'vendor_id' => $this->vendor->id,
|
||||
'company_id' => $this->company->id,
|
||||
'email' => 'search@gmail.com',
|
||||
]);
|
||||
|
||||
$this->assertEquals(4, $this->vendor->contacts->count());
|
||||
$this->assertTrue($this->vendor->contacts->contains(function ($contact) {
|
||||
return $contact->email == 'search@gmail.com';
|
||||
}));
|
||||
|
||||
$this->assertFalse($this->vendor->contacts->contains(function ($contact) {
|
||||
return $contact->email == 'false@gmail.com';
|
||||
}));
|
||||
}
|
||||
|
||||
public function testMergeVendors()
|
||||
{
|
||||
$account = Account::factory()->create();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'account_id' => $account->id,
|
||||
'email' => $this->faker->safeEmail(),
|
||||
]);
|
||||
|
||||
$company = Company::factory()->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$vendor = Vendor::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'company_id' => $company->id,
|
||||
]);
|
||||
|
||||
$primary_contact = VendorContact::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $vendor->id,
|
||||
'company_id' => $company->id,
|
||||
'is_primary' => 1,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->count(2)->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $vendor->id,
|
||||
'company_id' => $company->id,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $vendor->id,
|
||||
'company_id' => $company->id,
|
||||
'email' => 'search@gmail.com',
|
||||
]);
|
||||
//4contacts
|
||||
|
||||
$mergable_vendor = Vendor::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'company_id' => $company->id,
|
||||
]);
|
||||
|
||||
$primary_contact = VendorContact::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $mergable_vendor->id,
|
||||
'company_id' => $company->id,
|
||||
'is_primary' => 1,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->count(2)->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $mergable_vendor->id,
|
||||
'company_id' => $company->id,
|
||||
]);
|
||||
|
||||
VendorContact::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'vendor_id' => $mergable_vendor->id,
|
||||
'company_id' => $company->id,
|
||||
'email' => 'search@gmail.com',
|
||||
]);
|
||||
//4 contacts
|
||||
|
||||
$this->assertEquals(4, $vendor->contacts->count());
|
||||
$this->assertEquals(4, $mergable_vendor->contacts->count());
|
||||
|
||||
$vendor = $vendor->service()->merge($mergable_vendor)->save();
|
||||
|
||||
// nlog($vendor->contacts->fresh()->toArray());
|
||||
// $this->assertEquals(7, $vendor->fresh()->contacts->count());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue