diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 608ed3d285c9..9c4bcafccdff 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -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) diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 2c7039af14bf..43cf27f17797 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -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'])) { diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 7547d9f7b866..8f6e0c4e9a3b 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -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 diff --git a/app/Jobs/Util/StartMigration.php b/app/Jobs/Util/StartMigration.php index 20116ff50eae..50a5c14797ef 100644 --- a/app/Jobs/Util/StartMigration.php +++ b/app/Jobs/Util/StartMigration.php @@ -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()); } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 7ab6db4bc08f..e8a2f2331ec0 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -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(); + } } } diff --git a/app/Utils/Traits/Payment/Refundable.php b/app/Utils/Traits/Payment/Refundable.php index 9d8604db2771..c5663d2c2962 100644 --- a/app/Utils/Traits/Payment/Refundable.php +++ b/app/Utils/Traits/Payment/Refundable.php @@ -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'];