Merge pull request #6368 from turbo124/v5-develop

Fixes for reminders
This commit is contained in:
David Bomba 2021-08-01 08:58:50 +10:00 committed by GitHub
commit 326cc09c0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 34 deletions

View File

@ -192,19 +192,17 @@ class Import implements ShouldQueue
nlog("Starting Migration"); nlog("Starting Migration");
nlog($this->user->email); nlog($this->user->email);
info("Starting Migration"); nlog("Company ID = ");
info($this->user->email); nlog($this->company->id);
auth()->login($this->user, false); auth()->login($this->user, false);
auth()->user()->setCompany($this->company); auth()->user()->setCompany($this->company);
// $jsonStream = \JsonMachine\JsonMachine::fromFile($this->file_path, "/data");
$array = json_decode(file_get_contents($this->file_path), 1); $array = json_decode(file_get_contents($this->file_path), 1);
$data = $array['data']; $data = $array['data'];
foreach ($this->available_imports as $import) { foreach ($this->available_imports as $import) {
if (! array_key_exists($import, $data)) { if (! array_key_exists($import, $data)) {
//throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration.");
info("Resource {$import} is not available for migration."); info("Resource {$import} is not available for migration.");
continue; continue;
} }
@ -249,7 +247,17 @@ class Import implements ShouldQueue
} }
// CreateCompanyPaymentTerms::dispatchNow($sp035a66, $spaa9f78); // 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()); info('Completed🚀🚀🚀🚀🚀 at '.now());
@ -293,7 +301,7 @@ class Import implements ShouldQueue
private function setInitialCompanyLedgerBalances() 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'); $invoice_balances = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance');

View File

@ -54,6 +54,9 @@ class CompanyPresenter extends EntityPresenter
$settings = $this->entity->settings; $settings = $this->entity->settings;
} }
if(config('ninja.is_docker'))
return $this->logo($settings);
$context_options =array( $context_options =array(
"ssl"=>array( "ssl"=>array(
"verify_peer"=>false, "verify_peer"=>false,

View File

@ -174,8 +174,8 @@ class BaseRepository
$model->client_id = $data['client_id']; $model->client_id = $data['client_id'];
//pickup changes here to recalculate reminders //pickup changes here to recalculate reminders
if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) //if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date')))
$model->service()->setReminder()->save(); // $model->service()->setReminder()->save();
$client = Client::where('id', $model->client_id)->withTrashed()->first(); $client = Client::where('id', $model->client_id)->withTrashed()->first();
@ -213,6 +213,9 @@ class BaseRepository
/* Model now persisted, now lets do some child tasks */ /* Model now persisted, now lets do some child tasks */
if($model instanceof Invoice)
$model->service()->setReminder()->save();
/* Save any documents */ /* Save any documents */
if (array_key_exists('documents', $data)) if (array_key_exists('documents', $data))
$this->saveDocuments($data['documents'], $model); $this->saveDocuments($data['documents'], $model);

View File

@ -46,6 +46,15 @@ class HandleRestore extends AbstractService
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
->sum(\DB::raw('amount')); ->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 //restore the paymentables
$payment->paymentables() $payment->paymentables()
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
@ -57,7 +66,15 @@ class HandleRestore extends AbstractService
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
->sum(\DB::raw('amount')); ->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) { if ($payment->amount == $payment_amount) {
$payment->is_deleted = false; $payment->is_deleted = false;

View File

@ -86,6 +86,11 @@ class MarkInvoiceDeleted extends AbstractService
->where('paymentable_id', $this->invoice->id) ->where('paymentable_id', $this->invoice->id)
->sum(DB::raw('amount')); ->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->amount -= $payment_adjustment;
$payment->applied -= $payment_adjustment; $payment->applied -= $payment_adjustment;
$payment->save(); $payment->save();
@ -108,12 +113,19 @@ class MarkInvoiceDeleted extends AbstractService
->where('paymentable_type', '=', 'invoices') ->where('paymentable_type', '=', 'invoices')
->where('paymentable_id', $this->invoice->id) ->where('paymentable_id', $this->invoice->id)
->sum(DB::raw('amount')); ->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') - $this->invoice->payments->sum('refunded');;
// $this->total_payments = $this->invoice->payments->sum('amount - refunded'); //$this->total_payments = $this->invoice->payments->sum('amount - refunded');
nlog("adjustment amount = {$this->adjustment_amount}");
nlog("total payments = {$this->total_payments}");
return $this; return $this;
} }

View File

@ -42,6 +42,9 @@ class UpdateReminder extends AbstractService
return $this->invoice; //exit early return $this->invoice; //exit early
} }
if($this->invoice->next_send_date)
$this->invoice->next_send_date = null;
$offset = $this->invoice->client->timezone_offset(); $offset = $this->invoice->client->timezone_offset();
$date_collection = collect(); $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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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) && 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); $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))) 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 && if ($this->invoice->last_sent_date &&
@ -137,7 +140,7 @@ class UpdateReminder extends AbstractService
$reminder_date->addSeconds($offset); $reminder_date->addSeconds($offset);
if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))) 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");
} }

View File

@ -40,9 +40,6 @@ class DeleteInvoiceTest extends TestCase
); );
} }
public function testInvoiceDeletionAfterCancellation() public function testInvoiceDeletionAfterCancellation()
{ {
$data = [ $data = [
@ -118,36 +115,45 @@ class DeleteInvoiceTest extends TestCase
$this->assertEquals(0, $invoice->client->balance); $this->assertEquals(0, $invoice->client->balance);
$this->assertEquals(20, $invoice->client->paid_to_date); $this->assertEquals(20, $invoice->client->paid_to_date);
/*
//partially refund payment //partially refund payment
$payment = $invoice->fresh()->payments()->first(); $payment = $invoice->fresh()->payments()->first();
$data = [ $data = [
'id' => $this->encodePrimaryKey($payment->id), 'id' => $payment->id,
'amount' => 10, 'amount' => 10,
'invoices' => [ 'invoices' => [
[ [
'invoice_id' => $invoice->hashed_id, 'invoice_id' => $invoice->id,
'amount' => 10, 'amount' => 10,
], ],
], ],
'date' => '2020/12/12', 'date' => '2020/12/12',
'gateway_refund' => false
]; ];
$payment->refund($data); $payment->refund($data);
$this->assertEquals(10, $payment->fresh()->refunded);
*/
//test balances //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 //cancel invoice and paid_to_date
$invoice->fresh()->service()->handleCancellation()->save();
//test balances and paid_to_date //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 //delete invoice
$invoice->fresh()->service()->markDeleted()->save();
//test balances and paid_to_date //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);
} }

View File

@ -82,11 +82,36 @@ class ReminderTest extends TestCase
// ReminderJob::dispatchNow(); // 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() public function testReminderNextSendRecalculation()
{ {
$this->invoice->date = now()->subDays(2)->format('Y-m-d'); $this->invoice->date = now()->subDays(2)->format('Y-m-d');
$this->invoice->due_date = now()->addDays(30)->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->reminder1_sent = now()->subDays(1)->format('Y-m-d');
$this->invoice->last_sent_date = 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->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'));
} }