diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 14afd00d8b40..8a13ebe42512 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -192,19 +192,17 @@ class Import implements ShouldQueue nlog("Starting Migration"); nlog($this->user->email); - info("Starting Migration"); - info($this->user->email); + nlog("Company ID = "); + nlog($this->company->id); auth()->login($this->user, false); auth()->user()->setCompany($this->company); - // $jsonStream = \JsonMachine\JsonMachine::fromFile($this->file_path, "/data"); $array = json_decode(file_get_contents($this->file_path), 1); $data = $array['data']; foreach ($this->available_imports as $import) { if (! array_key_exists($import, $data)) { - //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); info("Resource {$import} is not available for migration."); continue; } @@ -249,7 +247,17 @@ class Import implements ShouldQueue } // CreateCompanyPaymentTerms::dispatchNow($sp035a66, $spaa9f78); - CreateCompanyTaskStatuses::dispatchNow($this->company, $this->user); + // CreateCompanyTaskStatuses::dispatchNow($this->company, $this->user); + + $task_statuses = [ + ['name' => ctrans('texts.backlog'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 1], + ['name' => ctrans('texts.ready_to_do'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 2], + ['name' => ctrans('texts.in_progress'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 3], + ['name' => ctrans('texts.done'), 'company_id' => $this->company->id, 'user_id' => $this->user->id, 'created_at' => now(), 'updated_at' => now(), 'status_order' => 4], + + ]; + + TaskStatus::insert($task_statuses); info('CompletedπŸš€πŸš€πŸš€πŸš€πŸš€ at '.now()); @@ -293,7 +301,7 @@ class Import implements ShouldQueue private function setInitialCompanyLedgerBalances() { - Client::cursor()->each(function ($client) { + Client::where('company_id', $this->company->id)->cursor()->each(function ($client) { $invoice_balances = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); diff --git a/app/Models/Presenters/CompanyPresenter.php b/app/Models/Presenters/CompanyPresenter.php index 6a0e5a9e6902..aa347a86709d 100644 --- a/app/Models/Presenters/CompanyPresenter.php +++ b/app/Models/Presenters/CompanyPresenter.php @@ -54,6 +54,9 @@ class CompanyPresenter extends EntityPresenter $settings = $this->entity->settings; } + if(config('ninja.is_docker')) + return $this->logo($settings); + $context_options =array( "ssl"=>array( "verify_peer"=>false, diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 574153e94631..783f79cc94f4 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -174,8 +174,8 @@ class BaseRepository $model->client_id = $data['client_id']; //pickup changes here to recalculate reminders - if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) - $model->service()->setReminder()->save(); + //if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) + // $model->service()->setReminder()->save(); $client = Client::where('id', $model->client_id)->withTrashed()->first(); @@ -213,6 +213,9 @@ class BaseRepository /* Model now persisted, now lets do some child tasks */ + if($model instanceof Invoice) + $model->service()->setReminder()->save(); + /* Save any documents */ if (array_key_exists('documents', $data)) $this->saveDocuments($data['documents'], $model); diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index 536125441af7..e1615b3ab16f 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -46,6 +46,15 @@ class HandleRestore extends AbstractService ->where('paymentable_type', '=', 'invoices') ->sum(\DB::raw('amount')); +nlog("first pre restore amount = {$pre_restore_amount}"); + + $pre_restore_amount -= $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->sum(\DB::raw('refunded')); + +nlog("second pre restore amount = {$pre_restore_amount}"); + + //restore the paymentables $payment->paymentables() ->where('paymentable_type', '=', 'invoices') @@ -57,7 +66,15 @@ class HandleRestore extends AbstractService ->where('paymentable_type', '=', 'invoices') ->sum(\DB::raw('amount')); - info($payment->amount . " == " . $payment_amount); +nlog("first payment_amount = {$payment_amount}"); + + $payment_amount -= $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->sum(\DB::raw('refunded')); + +nlog("second payment_amount = {$payment_amount}"); + + nlog($payment->amount . " == " . $payment_amount); if ($payment->amount == $payment_amount) { $payment->is_deleted = false; diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index e92a31457fbd..1fcf97268075 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -36,7 +36,7 @@ class MarkInvoiceDeleted extends AbstractService if ($this->invoice->is_deleted) { return $this->invoice; } - + $this->cleanup() ->setAdjustmentAmount() ->deletePaymentables() @@ -86,6 +86,11 @@ class MarkInvoiceDeleted extends AbstractService ->where('paymentable_id', $this->invoice->id) ->sum(DB::raw('amount')); + $payment_adjustment -= $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('refunded')); + $payment->amount -= $payment_adjustment; $payment->applied -= $payment_adjustment; $payment->save(); @@ -108,12 +113,19 @@ class MarkInvoiceDeleted extends AbstractService ->where('paymentable_type', '=', 'invoices') ->where('paymentable_id', $this->invoice->id) ->sum(DB::raw('amount')); - //->sum(DB::raw('amount - refunded')); + + $this->adjustment_amount -= $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('refunded')); } - $this->total_payments = $this->invoice->payments->sum('amount'); - // $this->total_payments = $this->invoice->payments->sum('amount - refunded'); + $this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded');; + //$this->total_payments = $this->invoice->payments->sum('amount - refunded'); + +nlog("adjustment amount = {$this->adjustment_amount}"); +nlog("total payments = {$this->total_payments}"); return $this; } diff --git a/app/Services/Invoice/UpdateReminder.php b/app/Services/Invoice/UpdateReminder.php index 0b655ee916dc..e693da6f6d21 100644 --- a/app/Services/Invoice/UpdateReminder.php +++ b/app/Services/Invoice/UpdateReminder.php @@ -42,6 +42,9 @@ class UpdateReminder extends AbstractService return $this->invoice; //exit early } + if($this->invoice->next_send_date) + $this->invoice->next_send_date = null; + $offset = $this->invoice->client->timezone_offset(); $date_collection = collect(); @@ -52,7 +55,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 after due date"); } if (is_null($this->invoice->reminder1_sent) && @@ -61,7 +64,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 before_due_date"); } if (is_null($this->invoice->reminder1_sent) && @@ -70,7 +73,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder1)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 1 after_due_date"); } if (is_null($this->invoice->reminder2_sent) && @@ -79,7 +82,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 after_invoice_date = ".$reminder_date->format('Y-m-d')); } if (is_null($this->invoice->reminder2_sent) && @@ -88,7 +91,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 before_due_date"); } if (is_null($this->invoice->reminder2_sent) && @@ -97,7 +100,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder2)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 2 after_due_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -106,7 +109,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 after_invoice_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -115,7 +118,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->subDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 before_due_date"); } if (is_null($this->invoice->reminder3_sent) && @@ -124,7 +127,7 @@ class UpdateReminder extends AbstractService $reminder_date = Carbon::parse($this->invoice->due_date)->startOfDay()->addDays($this->settings->num_days_reminder3)->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings reminder 3 after_due_date"); } if ($this->invoice->last_sent_date && @@ -137,7 +140,7 @@ class UpdateReminder extends AbstractService $reminder_date->addSeconds($offset); if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) - $date_collection->push($reminder_date); + $date_collection->push($reminder_date); nlog("settings endless reminder"); } diff --git a/tests/Feature/DeleteInvoiceTest.php b/tests/Feature/DeleteInvoiceTest.php index 7bc62b00fb3d..ad34dc9da427 100644 --- a/tests/Feature/DeleteInvoiceTest.php +++ b/tests/Feature/DeleteInvoiceTest.php @@ -40,9 +40,6 @@ class DeleteInvoiceTest extends TestCase ); } - - - public function testInvoiceDeletionAfterCancellation() { $data = [ @@ -118,36 +115,45 @@ class DeleteInvoiceTest extends TestCase $this->assertEquals(0, $invoice->client->balance); $this->assertEquals(20, $invoice->client->paid_to_date); -/* + //partially refund payment $payment = $invoice->fresh()->payments()->first(); $data = [ - 'id' => $this->encodePrimaryKey($payment->id), + 'id' => $payment->id, 'amount' => 10, 'invoices' => [ [ - 'invoice_id' => $invoice->hashed_id, + 'invoice_id' => $invoice->id, 'amount' => 10, ], ], 'date' => '2020/12/12', + 'gateway_refund' => false ]; $payment->refund($data); - $this->assertEquals(10, $payment->fresh()->refunded); - -*/ //test balances + $this->assertEquals(10, $payment->fresh()->refunded); + $this->assertEquals(10, $invoice->client->fresh()->paid_to_date); + $this->assertEquals(10, $invoice->fresh()->balance); //cancel invoice and paid_to_date + $invoice->fresh()->service()->handleCancellation()->save(); //test balances and paid_to_date + $this->assertEquals(0, $invoice->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->balance); + $this->assertEquals(10, $invoice->client->fresh()->paid_to_date); //delete invoice + $invoice->fresh()->service()->markDeleted()->save(); //test balances and paid_to_date + $this->assertEquals(0, $invoice->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->paid_to_date); } diff --git a/tests/Feature/ReminderTest.php b/tests/Feature/ReminderTest.php index ef62098163d4..54cfc5cc52dd 100644 --- a/tests/Feature/ReminderTest.php +++ b/tests/Feature/ReminderTest.php @@ -82,11 +82,36 @@ class ReminderTest extends TestCase // ReminderJob::dispatchNow(); } + public function testReminderHitsScenarioH1() + { + $this->invoice->date = now()->format('Y-m-d'); + $this->invoice->due_date = Carbon::now()->addDays(30)->format('Y-m-d'); + + $settings = $this->company->settings; + $settings->enable_reminder1 = true; + $settings->schedule_reminder1 = 'before_due_date'; + $settings->num_days_reminder1 = 2; + $settings->enable_reminder2 = true; + $settings->schedule_reminder2 = 'after_due_date'; + $settings->num_days_reminder2 = 14; + $settings->enable_reminder3 = true; + $settings->schedule_reminder3 = 'after_due_date'; + $settings->num_days_reminder3 = 30; + + $this->company->settings = $settings; + $this->invoice->service()->markSent(); + $this->invoice->service()->setReminder($settings)->save(); + + $this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), Carbon::now()->addDays(30)->subDays(2)->format('Y-m-d')); + + // ReminderJob::dispatchNow(); + } + + /* Cant set a reminder in the past so need to skip reminder 2 and go straigh to reminder 3*/ public function testReminderNextSendRecalculation() { $this->invoice->date = now()->subDays(2)->format('Y-m-d'); - $this->invoice->due_date = now()->addDays(30)->format('Y-m-d'); $this->invoice->reminder1_sent = now()->subDays(1)->format('Y-m-d'); $this->invoice->last_sent_date = now()->subDays(1)->format('Y-m-d'); @@ -110,7 +135,7 @@ class ReminderTest extends TestCase $this->invoice->fresh(); - $this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->format('Y-m-d')); + $this->assertEquals(Carbon::parse($this->invoice->next_send_date)->format('Y-m-d'), now()->addDay()->format('Y-m-d')); }