mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 06:24:35 -04:00
Fixes for migration (#3418)
* Fix for setting a currency! * Additional logic to ensure invites are only created for the client_contact -> client * Move credit number generation after the save to ensure we do not fail on the credit creation because of a number race condition * fixes for migration
This commit is contained in:
parent
b157ee3917
commit
d14b21f471
@ -431,162 +431,159 @@ class CreateTestData extends Command
|
|||||||
|
|
||||||
private function createInvoice($client)
|
private function createInvoice($client)
|
||||||
{
|
{
|
||||||
for($x=0; $x<$this->count; $x++){
|
// for($x=0; $x<$this->count; $x++){
|
||||||
dispatch(new CreateTestInvoiceJob($client));
|
// dispatch(new CreateTestInvoiceJob($client));
|
||||||
|
// }
|
||||||
|
|
||||||
|
$faker = \Faker\Factory::create();
|
||||||
|
|
||||||
|
$invoice = InvoiceFactory::create($client->company->id, $client->user->id);//stub the company and user_id
|
||||||
|
$invoice->client_id = $client->id;
|
||||||
|
// $invoice->date = $faker->date();
|
||||||
|
$dateable = Carbon::now()->subDays(rand(0,90));
|
||||||
|
$invoice->date = $dateable;
|
||||||
|
|
||||||
|
$invoice->line_items = $this->buildLineItems(rand(1,10));
|
||||||
|
$invoice->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$invoice->tax_name1 = 'GST';
|
||||||
|
$invoice->tax_rate1 = 10.00;
|
||||||
}
|
}
|
||||||
|
|
||||||
// $faker = \Faker\Factory::create();
|
if (rand(0, 1)) {
|
||||||
|
$invoice->tax_name2 = 'VAT';
|
||||||
|
$invoice->tax_rate2 = 17.50;
|
||||||
|
}
|
||||||
|
|
||||||
// $invoice = InvoiceFactory::create($client->company->id, $client->user->id);//stub the company and user_id
|
if (rand(0, 1)) {
|
||||||
// $invoice->client_id = $client->id;
|
$invoice->tax_name3 = 'CA Sales Tax';
|
||||||
// // $invoice->date = $faker->date();
|
$invoice->tax_rate3 = 5;
|
||||||
// $dateable = Carbon::now()->subDays(rand(0,90));
|
}
|
||||||
// $invoice->date = $dateable;
|
|
||||||
|
|
||||||
// $invoice->line_items = $this->buildLineItems(rand(1,10));
|
$invoice->save();
|
||||||
// $invoice->uses_inclusive_taxes = false;
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$invoice_calc = new InvoiceSum($invoice);
|
||||||
// $invoice->tax_name1 = 'GST';
|
$invoice_calc->build();
|
||||||
// $invoice->tax_rate1 = 10.00;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$invoice = $invoice_calc->getInvoice();
|
||||||
// $invoice->tax_name2 = 'VAT';
|
|
||||||
// $invoice->tax_rate2 = 17.50;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$invoice->save();
|
||||||
// $invoice->tax_name3 = 'CA Sales Tax';
|
$invoice->service()->createInvitations();
|
||||||
// $invoice->tax_rate3 = 5;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $invoice->save();
|
$invoice->ledger()->updateInvoiceBalance($invoice->balance);
|
||||||
|
|
||||||
// $invoice_calc = new InvoiceSum($invoice);
|
//UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
|
||||||
// $invoice_calc->build();
|
|
||||||
|
|
||||||
// $invoice = $invoice_calc->getInvoice();
|
$this->invoice_repo->markSent($invoice);
|
||||||
|
|
||||||
// $invoice->save();
|
$invoice->service()->createInvitations();
|
||||||
// $invoice->service()->createInvitations();
|
|
||||||
|
|
||||||
// $invoice->ledger()->updateInvoiceBalance($invoice->balance);
|
if (rand(0, 1)) {
|
||||||
|
$payment = PaymentFactory::create($client->company->id, $client->user->id);
|
||||||
|
$payment->date = $dateable;
|
||||||
|
$payment->client_id = $client->id;
|
||||||
|
$payment->amount = $invoice->balance;
|
||||||
|
$payment->transaction_reference = rand(0, 500);
|
||||||
|
$payment->type_id = PaymentType::CREDIT_CARD_OTHER;
|
||||||
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
||||||
|
$payment->number = $client->getNextPaymentNumber($client);
|
||||||
|
$payment->save();
|
||||||
|
|
||||||
// //UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
|
$payment->invoices()->save($invoice);
|
||||||
|
|
||||||
// $this->invoice_repo->markSent($invoice);
|
event(new PaymentWasCreated($payment, $payment->company));
|
||||||
|
|
||||||
// $invoice->service()->createInvitations();
|
$payment->service()->updateInvoicePayment();
|
||||||
|
//UpdateInvoicePayment::dispatchNow($payment, $payment->company);
|
||||||
// if (rand(0, 1)) {
|
}
|
||||||
// $payment = PaymentFactory::create($client->company->id, $client->user->id);
|
//@todo this slow things down, but gives us PDFs of the invoices for inspection whilst debugging.
|
||||||
// $payment->date = $dateable;
|
event(new InvoiceWasCreated($invoice, $invoice->company));
|
||||||
// $payment->client_id = $client->id;
|
|
||||||
// $payment->amount = $invoice->balance;
|
|
||||||
// $payment->transaction_reference = rand(0, 500);
|
|
||||||
// $payment->type_id = PaymentType::CREDIT_CARD_OTHER;
|
|
||||||
// $payment->status_id = Payment::STATUS_COMPLETED;
|
|
||||||
// $payment->number = $client->getNextPaymentNumber($client);
|
|
||||||
// $payment->save();
|
|
||||||
|
|
||||||
// $payment->invoices()->save($invoice);
|
|
||||||
|
|
||||||
// event(new PaymentWasCreated($payment, $payment->company));
|
|
||||||
|
|
||||||
// $payment->service()->updateInvoicePayment();
|
|
||||||
// //UpdateInvoicePayment::dispatchNow($payment, $payment->company);
|
|
||||||
// }
|
|
||||||
// //@todo this slow things down, but gives us PDFs of the invoices for inspection whilst debugging.
|
|
||||||
// event(new InvoiceWasCreated($invoice, $invoice->company));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createCredit($client)
|
private function createCredit($client)
|
||||||
{
|
{
|
||||||
for($x=0; $x<$this->count; $x++){
|
// for($x=0; $x<$this->count; $x++){
|
||||||
|
|
||||||
dispatch(new CreateTestCreditJob($client));
|
// dispatch(new CreateTestCreditJob($client));
|
||||||
|
|
||||||
|
// }
|
||||||
|
$faker = \Faker\Factory::create();
|
||||||
|
|
||||||
|
$credit = factory(\App\Models\Credit::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]);
|
||||||
|
|
||||||
|
$dateable = Carbon::now()->subDays(rand(0,90));
|
||||||
|
$credit->date = $dateable;
|
||||||
|
|
||||||
|
$credit->line_items = $this->buildLineItems(rand(1,10));
|
||||||
|
$credit->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$credit->tax_name1 = 'GST';
|
||||||
|
$credit->tax_rate1 = 10.00;
|
||||||
}
|
}
|
||||||
// $faker = \Faker\Factory::create();
|
|
||||||
|
|
||||||
// $credit = factory(\App\Models\Credit::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]);
|
if (rand(0, 1)) {
|
||||||
|
$credit->tax_name2 = 'VAT';
|
||||||
|
$credit->tax_rate2 = 17.50;
|
||||||
|
}
|
||||||
|
|
||||||
// //$invoice = InvoiceFactory::create($client->company->id, $client->user->id);//stub the company and user_id
|
if (rand(0, 1)) {
|
||||||
// //$invoice->client_id = $client->id;
|
$credit->tax_name3 = 'CA Sales Tax';
|
||||||
// // $invoice->date = $faker->date();
|
$credit->tax_rate3 = 5;
|
||||||
// $dateable = Carbon::now()->subDays(rand(0,90));
|
}
|
||||||
// $credit->date = $dateable;
|
|
||||||
|
|
||||||
// $credit->line_items = $this->buildLineItems(rand(1,10));
|
$credit->save();
|
||||||
// $credit->uses_inclusive_taxes = false;
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$invoice_calc = new InvoiceSum($credit);
|
||||||
// $credit->tax_name1 = 'GST';
|
$invoice_calc->build();
|
||||||
// $credit->tax_rate1 = 10.00;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$credit = $invoice_calc->getCredit();
|
||||||
// $credit->tax_name2 = 'VAT';
|
|
||||||
// $credit->tax_rate2 = 17.50;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$credit->save();
|
||||||
// $credit->tax_name3 = 'CA Sales Tax';
|
|
||||||
// $credit->tax_rate3 = 5;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $credit->save();
|
event(new CreateCreditInvitation($credit));
|
||||||
|
|
||||||
// $invoice_calc = new InvoiceSum($credit);
|
|
||||||
// $invoice_calc->build();
|
|
||||||
|
|
||||||
// $credit = $invoice_calc->getCredit();
|
|
||||||
|
|
||||||
// $credit->save();
|
|
||||||
|
|
||||||
// event(new CreateCreditInvitation($credit));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createQuote($client)
|
private function createQuote($client)
|
||||||
{
|
{
|
||||||
for($x=0; $x<$this->count; $x++){
|
// for($x=0; $x<$this->count; $x++){
|
||||||
|
|
||||||
dispatch(new CreateTestQuoteJob($client));
|
// dispatch(new CreateTestQuoteJob($client));
|
||||||
|
// }
|
||||||
|
$faker = \Faker\Factory::create();
|
||||||
|
|
||||||
|
$quote =factory(\App\Models\Quote::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]);
|
||||||
|
$quote->date = $faker->date();
|
||||||
|
|
||||||
|
$quote->line_items = $this->buildLineItems(rand(1,10));
|
||||||
|
$quote->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
|
if (rand(0, 1)) {
|
||||||
|
$quote->tax_name1 = 'GST';
|
||||||
|
$quote->tax_rate1 = 10.00;
|
||||||
}
|
}
|
||||||
// $faker = \Faker\Factory::create();
|
|
||||||
|
|
||||||
// $quote =factory(\App\Models\Quote::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]);
|
if (rand(0, 1)) {
|
||||||
// $quote->date = $faker->date();
|
$quote->tax_name2 = 'VAT';
|
||||||
|
$quote->tax_rate2 = 17.50;
|
||||||
|
}
|
||||||
|
|
||||||
// $quote->line_items = $this->buildLineItems(rand(1,10));
|
if (rand(0, 1)) {
|
||||||
// $quote->uses_inclusive_taxes = false;
|
$quote->tax_name3 = 'CA Sales Tax';
|
||||||
|
$quote->tax_rate3 = 5;
|
||||||
|
}
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$quote->save();
|
||||||
// $quote->tax_name1 = 'GST';
|
|
||||||
// $quote->tax_rate1 = 10.00;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$quote_calc = new InvoiceSum($quote);
|
||||||
// $quote->tax_name2 = 'VAT';
|
$quote_calc->build();
|
||||||
// $quote->tax_rate2 = 17.50;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (rand(0, 1)) {
|
$quote = $quote_calc->getQuote();
|
||||||
// $quote->tax_name3 = 'CA Sales Tax';
|
$quote->service()->markSent()->save();
|
||||||
// $quote->tax_rate3 = 5;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $quote->save();
|
CreateQuoteInvitations::dispatch($quote, $quote->company);
|
||||||
|
|
||||||
// $quote_calc = new InvoiceSum($quote);
|
|
||||||
// $quote_calc->build();
|
|
||||||
|
|
||||||
// $quote = $quote_calc->getQuote();
|
|
||||||
// $quote->service()->markSent()->save();
|
|
||||||
|
|
||||||
// CreateQuoteInvitations::dispatch($quote, $quote->company);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildLineItems($count = 1)
|
private function buildLineItems($count = 1)
|
||||||
|
@ -81,22 +81,25 @@ class StoreClientRequest extends Request
|
|||||||
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
|
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if(empty($input['settings']) || empty($input['settings']->currency_id))
|
if(empty($input['settings']) || empty($input['settings']->currency_id))
|
||||||
// {
|
{
|
||||||
// if(empty($input['group_settings_id']))
|
if(!is_object($input['settings']))
|
||||||
// {
|
$input['settings'] = ClientSettings::defaults();
|
||||||
// $input['settings']->currency_id = auth()->user()->company()->settings->currency_id;
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// $group_settings = GroupSetting::find($input['group_settings_id']);
|
|
||||||
|
|
||||||
// if($group_settings && property_exists($group_settings, 'currency_id') && is_int($group_settings->currency_id))
|
if(empty($input['group_settings_id']))
|
||||||
// $input['settings']->currency_id = $group_settings->currency_id;
|
{
|
||||||
// else
|
$input['settings']->currency_id = auth()->user()->company()->settings->currency_id;
|
||||||
// $input['settings']->currency_id = auth()->user()->company()->settings->currency_id;
|
}
|
||||||
// }
|
else
|
||||||
// }
|
{
|
||||||
|
$group_settings = GroupSetting::find($input['group_settings_id']);
|
||||||
|
|
||||||
|
if($group_settings && property_exists($group_settings, 'currency_id') && is_int($group_settings->currency_id))
|
||||||
|
$input['settings']->currency_id = $group_settings->currency_id;
|
||||||
|
else
|
||||||
|
$input['settings']->currency_id = auth()->user()->company()->settings->currency_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(isset($input['contacts']))
|
if(isset($input['contacts']))
|
||||||
{
|
{
|
||||||
|
@ -102,6 +102,8 @@ class Import implements ShouldQueue
|
|||||||
*/
|
*/
|
||||||
private $ids = [];
|
private $ids = [];
|
||||||
|
|
||||||
|
public $tries = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new job instance.
|
* Create a new job instance.
|
||||||
*
|
*
|
||||||
@ -131,6 +133,7 @@ class Import implements ShouldQueue
|
|||||||
if (!in_array($key, $this->available_imports)) {
|
if (!in_array($key, $this->available_imports)) {
|
||||||
throw new ResourceNotAvailableForMigration($key);
|
throw new ResourceNotAvailableForMigration($key);
|
||||||
}
|
}
|
||||||
|
\Log::error($key);
|
||||||
|
|
||||||
$method = sprintf("process%s", Str::ucfirst(Str::camel($key)));
|
$method = sprintf("process%s", Str::ucfirst(Str::camel($key)));
|
||||||
|
|
||||||
@ -546,6 +549,7 @@ class Import implements ShouldQueue
|
|||||||
|
|
||||||
private function processDocuments(array $data): void
|
private function processDocuments(array $data): void
|
||||||
{
|
{
|
||||||
|
Document::unguard();
|
||||||
/** No validators since data provided by database is already valid. */
|
/** No validators since data provided by database is already valid. */
|
||||||
|
|
||||||
foreach ($data as $resource) {
|
foreach ($data as $resource) {
|
||||||
@ -579,6 +583,9 @@ class Import implements ShouldQueue
|
|||||||
|
|
||||||
$document = Document::create($modified);
|
$document = Document::create($modified);
|
||||||
|
|
||||||
|
// $entity = $modified['documentable_type']::find($modified['documentable_id']);
|
||||||
|
// $entity->documents()->save($modified);
|
||||||
|
|
||||||
$old_user_key = array_key_exists('user_id', $resource) ?? $this->user->id;
|
$old_user_key = array_key_exists('user_id', $resource) ?? $this->user->id;
|
||||||
|
|
||||||
$this->ids['documents'] = [
|
$this->ids['documents'] = [
|
||||||
@ -589,6 +596,7 @@ class Import implements ShouldQueue
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Document::reguard();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processCompanyGateways(array $data) :void
|
private function processCompanyGateways(array $data) :void
|
||||||
|
@ -60,6 +60,10 @@ class StartMigration implements ShouldQueue
|
|||||||
{
|
{
|
||||||
MultiDB::setDb($this->company->db);
|
MultiDB::setDb($this->company->db);
|
||||||
|
|
||||||
|
auth()->login($this->user, false);
|
||||||
|
|
||||||
|
auth()->user()->setCompany($this->company);
|
||||||
|
|
||||||
$zip = new \ZipArchive();
|
$zip = new \ZipArchive();
|
||||||
$archive = $zip->open($this->filepath);
|
$archive = $zip->open($this->filepath);
|
||||||
|
|
||||||
@ -77,7 +81,9 @@ class StartMigration implements ShouldQueue
|
|||||||
|
|
||||||
$this->start($filename);
|
$this->start($filename);
|
||||||
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
} catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
|
||||||
Mail::to(auth()->user())->send(new MigrationFailed($e->getMessage()));
|
Mail::to($this->user)
|
||||||
|
->send(new MigrationFailed($e, $e->getMessage()));
|
||||||
|
|
||||||
if(app()->environment() !== 'production') info($e->getMessage());
|
if(app()->environment() !== 'production') info($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -232,10 +232,16 @@ class BaseRepository
|
|||||||
unset($invitation['id']);
|
unset($invitation['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id);
|
//make sure we are creating an invite for a contact who belongs to the client only!
|
||||||
$new_invitation->{$lcfirst_resource_id} = $model->id;
|
$contact = ClientContact::find($invitation['client_contact_id']);
|
||||||
$new_invitation->client_contact_id = $invitation['client_contact_id'];
|
|
||||||
$new_invitation->save();
|
if($model->client_id == $contact->client_id);
|
||||||
|
{
|
||||||
|
$new_invitation = $invitation_factory_class::create($model->company_id, $model->user_id);
|
||||||
|
$new_invitation->{$lcfirst_resource_id} = $model->id;
|
||||||
|
$new_invitation->client_contact_id = $invitation['client_contact_id'];
|
||||||
|
$new_invitation->save();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,9 @@ trait Refundable
|
|||||||
$line_items[] = $credit_line_item;
|
$line_items[] = $credit_line_item;
|
||||||
|
|
||||||
$credit_note->save();
|
$credit_note->save();
|
||||||
|
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
||||||
|
$credit_note->save();
|
||||||
|
|
||||||
$this->createActivity($data, $credit_note->id);
|
$this->createActivity($data, $credit_note->id);
|
||||||
|
|
||||||
//determine if we need to refund via gateway
|
//determine if we need to refund via gateway
|
||||||
@ -156,6 +158,9 @@ trait Refundable
|
|||||||
$credit_note->line_items = $line_items;
|
$credit_note->line_items = $line_items;
|
||||||
$credit_note->save();
|
$credit_note->save();
|
||||||
|
|
||||||
|
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
||||||
|
$credit_note->save();
|
||||||
|
|
||||||
//determine if we need to refund via gateway
|
//determine if we need to refund via gateway
|
||||||
if($data['gateway_refund'] !== false)
|
if($data['gateway_refund'] !== false)
|
||||||
{
|
{
|
||||||
@ -210,8 +215,7 @@ trait Refundable
|
|||||||
$credit_note = CreditFactory::create($this->company_id, $this->user_id);
|
$credit_note = CreditFactory::create($this->company_id, $this->user_id);
|
||||||
$credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null;
|
$credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null;
|
||||||
$credit_note->date = $data['date'];
|
$credit_note->date = $data['date'];
|
||||||
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
$credit_note->status_id = Credit::STATUS_SENT;
|
||||||
$credit_note->status_id = Credit::STATUS_DRAFT;
|
|
||||||
$credit_note->client_id = $this->client->id;
|
$credit_note->client_id = $this->client->id;
|
||||||
$credit_note->amount = $data['amount'];
|
$credit_note->amount = $data['amount'];
|
||||||
$credit_note->balance = $data['amount'];
|
$credit_note->balance = $data['amount'];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user