mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor for migrations
This commit is contained in:
parent
e216def2ff
commit
5855f3ea1f
@ -16,6 +16,7 @@ use App\Exceptions\MigrationValidatorFailed;
|
||||
use App\Exceptions\ResourceDependencyMissing;
|
||||
use App\Exceptions\ResourceNotAvailableForMigration;
|
||||
use App\Factory\ClientFactory;
|
||||
use App\Factory\CompanyLedgerFactory;
|
||||
use App\Factory\CreditFactory;
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Factory\PaymentFactory;
|
||||
@ -30,6 +31,7 @@ use App\Jobs\Company\CreateCompanyToken;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\MigrationCompleted;
|
||||
use App\Mail\MigrationFailed;
|
||||
use App\Models\Activity;
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\ClientGatewayToken;
|
||||
@ -169,6 +171,8 @@ class Import implements ShouldQueue
|
||||
$this->{$method}($resource);
|
||||
}
|
||||
|
||||
$this->setInitialCompanyLedgerBalances();
|
||||
|
||||
Mail::to($this->user)->send(new MigrationCompleted());
|
||||
|
||||
info('Completed🚀🚀🚀🚀🚀 at '.now());
|
||||
@ -176,6 +180,25 @@ class Import implements ShouldQueue
|
||||
return true;
|
||||
}
|
||||
|
||||
private function setInitialCompanyLedgerBalances()
|
||||
{
|
||||
|
||||
Client::cursor()->each(function ($client){
|
||||
|
||||
$company_ledger = CompanyLedgerFactory::create($client->company_id, $client->user_id);
|
||||
$company_ledger->client_id = $client->id;
|
||||
$company_ledger->adjustment = $client->balance;
|
||||
$company_ledger->notes = 'Migrated Client Balance';
|
||||
$company_ledger->balance = $client->balance;
|
||||
$company_ledger->activity_id = Activity::STORE_CLIENT;
|
||||
$company_ledger->save();
|
||||
|
||||
$client->company_ledger()->save($company_ledger);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @throws \Exception
|
||||
|
@ -13,9 +13,11 @@ namespace App\Models;
|
||||
|
||||
use App\DataMapper\ClientSettings;
|
||||
use App\DataMapper\CompanySettings;
|
||||
use App\Factory\CompanyLedgerFactory;
|
||||
use App\Factory\CreditFactory;
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Factory\QuoteFactory;
|
||||
use App\Models\Activity;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyGateway;
|
||||
use App\Models\Country;
|
||||
@ -124,6 +126,11 @@ class Client extends BaseModel implements HasLocalePreference
|
||||
return $this->hasMany(CompanyLedger::class)->orderBy('id', 'desc');
|
||||
}
|
||||
|
||||
public function company_ledger()
|
||||
{
|
||||
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
||||
}
|
||||
|
||||
public function gateway_tokens()
|
||||
{
|
||||
return $this->hasMany(ClientGatewayToken::class);
|
||||
|
@ -38,7 +38,7 @@ class ClientObserver
|
||||
* @return void
|
||||
*/
|
||||
public function updated(Client $client)
|
||||
{
|
||||
{
|
||||
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_CLIENT, $client);
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ namespace App\Repositories\Migration;
|
||||
use App\Events\Payment\PaymentWasCreated;
|
||||
use App\Factory\CreditFactory;
|
||||
use App\Jobs\Credit\ApplyCreditPayment;
|
||||
use App\Jobs\Product\UpdateOrCreateProduct;
|
||||
use App\Libraries\Currency\Conversion\CurrencyApi;
|
||||
use App\Models\Activity;
|
||||
use App\Models\Client;
|
||||
@ -29,6 +30,7 @@ use App\Utils\Traits\MakesHash;
|
||||
use App\Utils\Traits\SavesDocuments;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* InvoiceMigrationRepository
|
||||
|
@ -107,14 +107,11 @@ class PaymentMigrationRepository extends BaseRepository
|
||||
|
||||
$invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
|
||||
|
||||
info("invoice totals = {$invoice_totals}");
|
||||
|
||||
$invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get();
|
||||
|
||||
$payment->invoices()->saveMany($invoices);
|
||||
|
||||
$payment->invoices->each(function ($inv) use($invoice_totals){
|
||||
info("updating the pivot to {$invoice_totals}");
|
||||
$inv->pivot->amount = $invoice_totals;
|
||||
$inv->pivot->save();
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user