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:
David Bomba 2020-03-04 15:06:27 +11:00 committed by GitHub
parent b157ee3917
commit d14b21f471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 137 deletions

View File

@ -431,162 +431,159 @@ class CreateTestData extends Command
private function createInvoice($client)
{
for($x=0; $x<$this->count; $x++){
dispatch(new CreateTestInvoiceJob($client));
// for($x=0; $x<$this->count; $x++){
// 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
// $invoice->client_id = $client->id;
// // $invoice->date = $faker->date();
// $dateable = Carbon::now()->subDays(rand(0,90));
// $invoice->date = $dateable;
if (rand(0, 1)) {
$invoice->tax_name3 = 'CA Sales Tax';
$invoice->tax_rate3 = 5;
}
// $invoice->line_items = $this->buildLineItems(rand(1,10));
// $invoice->uses_inclusive_taxes = false;
$invoice->save();
// if (rand(0, 1)) {
// $invoice->tax_name1 = 'GST';
// $invoice->tax_rate1 = 10.00;
// }
$invoice_calc = new InvoiceSum($invoice);
$invoice_calc->build();
// if (rand(0, 1)) {
// $invoice->tax_name2 = 'VAT';
// $invoice->tax_rate2 = 17.50;
// }
$invoice = $invoice_calc->getInvoice();
// if (rand(0, 1)) {
// $invoice->tax_name3 = 'CA Sales Tax';
// $invoice->tax_rate3 = 5;
// }
$invoice->save();
$invoice->service()->createInvitations();
// $invoice->save();
$invoice->ledger()->updateInvoiceBalance($invoice->balance);
// $invoice_calc = new InvoiceSum($invoice);
// $invoice_calc->build();
//UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
// $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();
// 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();
// $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));
$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)
{
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
// //$invoice->client_id = $client->id;
// // $invoice->date = $faker->date();
// $dateable = Carbon::now()->subDays(rand(0,90));
// $credit->date = $dateable;
if (rand(0, 1)) {
$credit->tax_name3 = 'CA Sales Tax';
$credit->tax_rate3 = 5;
}
// $credit->line_items = $this->buildLineItems(rand(1,10));
// $credit->uses_inclusive_taxes = false;
$credit->save();
// if (rand(0, 1)) {
// $credit->tax_name1 = 'GST';
// $credit->tax_rate1 = 10.00;
// }
$invoice_calc = new InvoiceSum($credit);
$invoice_calc->build();
// if (rand(0, 1)) {
// $credit->tax_name2 = 'VAT';
// $credit->tax_rate2 = 17.50;
// }
$credit = $invoice_calc->getCredit();
// if (rand(0, 1)) {
// $credit->tax_name3 = 'CA Sales Tax';
// $credit->tax_rate3 = 5;
// }
$credit->save();
// $credit->save();
// $invoice_calc = new InvoiceSum($credit);
// $invoice_calc->build();
// $credit = $invoice_calc->getCredit();
// $credit->save();
// event(new CreateCreditInvitation($credit));
event(new CreateCreditInvitation($credit));
}
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]);
// $quote->date = $faker->date();
if (rand(0, 1)) {
$quote->tax_name2 = 'VAT';
$quote->tax_rate2 = 17.50;
}
// $quote->line_items = $this->buildLineItems(rand(1,10));
// $quote->uses_inclusive_taxes = false;
if (rand(0, 1)) {
$quote->tax_name3 = 'CA Sales Tax';
$quote->tax_rate3 = 5;
}
// if (rand(0, 1)) {
// $quote->tax_name1 = 'GST';
// $quote->tax_rate1 = 10.00;
// }
$quote->save();
// if (rand(0, 1)) {
// $quote->tax_name2 = 'VAT';
// $quote->tax_rate2 = 17.50;
// }
$quote_calc = new InvoiceSum($quote);
$quote_calc->build();
// if (rand(0, 1)) {
// $quote->tax_name3 = 'CA Sales Tax';
// $quote->tax_rate3 = 5;
// }
$quote = $quote_calc->getQuote();
$quote->service()->markSent()->save();
// $quote->save();
// $quote_calc = new InvoiceSum($quote);
// $quote_calc->build();
// $quote = $quote_calc->getQuote();
// $quote->service()->markSent()->save();
// CreateQuoteInvitations::dispatch($quote, $quote->company);
CreateQuoteInvitations::dispatch($quote, $quote->company);
}
private function buildLineItems($count = 1)

View File

@ -81,22 +81,25 @@ class StoreClientRequest extends Request
$input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']);
}
// if(empty($input['settings']) || empty($input['settings']->currency_id))
// {
// if(empty($input['group_settings_id']))
// {
// $input['settings']->currency_id = auth()->user()->company()->settings->currency_id;
// }
// else
// {
// $group_settings = GroupSetting::find($input['group_settings_id']);
if(empty($input['settings']) || empty($input['settings']->currency_id))
{
if(!is_object($input['settings']))
$input['settings'] = ClientSettings::defaults();
// 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(empty($input['group_settings_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']))
{

View File

@ -102,6 +102,8 @@ class Import implements ShouldQueue
*/
private $ids = [];
public $tries = 1;
/**
* Create a new job instance.
*
@ -131,6 +133,7 @@ class Import implements ShouldQueue
if (!in_array($key, $this->available_imports)) {
throw new ResourceNotAvailableForMigration($key);
}
\Log::error($key);
$method = sprintf("process%s", Str::ucfirst(Str::camel($key)));
@ -546,6 +549,7 @@ class Import implements ShouldQueue
private function processDocuments(array $data): void
{
Document::unguard();
/** No validators since data provided by database is already valid. */
foreach ($data as $resource) {
@ -579,6 +583,9 @@ class Import implements ShouldQueue
$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;
$this->ids['documents'] = [
@ -589,6 +596,7 @@ class Import implements ShouldQueue
];
}
Document::reguard();
}
private function processCompanyGateways(array $data) :void

View File

@ -60,6 +60,10 @@ class StartMigration implements ShouldQueue
{
MultiDB::setDb($this->company->db);
auth()->login($this->user, false);
auth()->user()->setCompany($this->company);
$zip = new \ZipArchive();
$archive = $zip->open($this->filepath);
@ -77,7 +81,9 @@ class StartMigration implements ShouldQueue
$this->start($filename);
} 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());
}
}

View File

@ -232,10 +232,16 @@ class BaseRepository
unset($invitation['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();
//make sure we are creating an invite for a contact who belongs to the client only!
$contact = ClientContact::find($invitation['client_contact_id']);
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();
}
}
}

View File

@ -49,7 +49,9 @@ trait Refundable
$line_items[] = $credit_line_item;
$credit_note->save();
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->save();
$this->createActivity($data, $credit_note->id);
//determine if we need to refund via gateway
@ -156,6 +158,9 @@ trait Refundable
$credit_note->line_items = $line_items;
$credit_note->save();
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->save();
//determine if we need to refund via gateway
if($data['gateway_refund'] !== false)
{
@ -210,8 +215,7 @@ trait Refundable
$credit_note = CreditFactory::create($this->company_id, $this->user_id);
$credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null;
$credit_note->date = $data['date'];
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->status_id = Credit::STATUS_DRAFT;
$credit_note->status_id = Credit::STATUS_SENT;
$credit_note->client_id = $this->client->id;
$credit_note->amount = $data['amount'];
$credit_note->balance = $data['amount'];