diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 0e00124ad72e..9c4913102102 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -122,6 +122,20 @@ class CreateTestData extends Command $client = ClientFactory::create($company->id, $user->id); $client->save(); + + factory(\App\Models\ClientContact::class,1)->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'is_primary' => 1 + ]); + + factory(\App\Models\ClientContact::class,rand(1,50))->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id + ]); + $y = $this->count * rand(1,5); $this->info("Creating {$y} invoices"); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 3f2132130017..f8b59b58308e 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -625,6 +625,10 @@ class InvoiceController extends BaseController $invoice = MarkInvoicePaid::dispatchNow($invoice); return $this->itemResponse($invoice); break; + case 'mark_sent': + $invoice->markSent(); + return $this->itemResponse($invoice); + break; case 'download': # code... break; diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index bcca69716c1c..cbf7cd7e6dc8 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -487,6 +487,7 @@ class PaymentController extends BaseController ReverseInvoicePayment::dispatchNow($payment); $payment->is_deleted = true; + $payment->save(); $payment->delete(); return $this->itemResponse($payment); diff --git a/app/Http/Requests/Payment/DestroyPaymentRequest.php b/app/Http/Requests/Payment/DestroyPaymentRequest.php index a83499206fab..ac2fc9252485 100644 --- a/app/Http/Requests/Payment/DestroyPaymentRequest.php +++ b/app/Http/Requests/Payment/DestroyPaymentRequest.php @@ -24,21 +24,8 @@ class DestroyPaymentRequest extends Request public function authorize() : bool { - return auth()->user()->can('edit', $this->payment); + return auth()->user()->can('edit', $this->payment) && $this->payment->is_deleted === false; } - // public function rules() - // { - // return [ - // 'deletable' - // ]; - // } - // public function messages() - // { - // return [ - // 'deletable' => 'Payment cannot be deleted', - // ]; - - // } } \ No newline at end of file diff --git a/app/Jobs/Client/UpdateClientBalance.php b/app/Jobs/Client/UpdateClientBalance.php index 31c75b4c2444..9ee0cd0ae6b3 100644 --- a/app/Jobs/Client/UpdateClientBalance.php +++ b/app/Jobs/Client/UpdateClientBalance.php @@ -44,7 +44,6 @@ class UpdateClientBalance { $this->client->balance += $this->amount; - $this->client->paid_to_date += $this->amount; $this->client->save(); } diff --git a/app/Jobs/Invoice/MarkInvoicePaid.php b/app/Jobs/Invoice/MarkInvoicePaid.php index c153f726a1ab..daf7c08c890d 100644 --- a/app/Jobs/Invoice/MarkInvoicePaid.php +++ b/app/Jobs/Invoice/MarkInvoicePaid.php @@ -13,6 +13,8 @@ namespace App\Jobs\Invoice; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; +use App\Jobs\Client\UpdateClientBalance; +use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyPaymentToInvoice; use App\Models\Invoice; @@ -70,6 +72,8 @@ class MarkInvoicePaid implements ShouldQueue event(new PaymentWasCreated($payment)); UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1)); + UpdateClientBalance::dispatchNow($payment->client, $this->payment->amount*-1); + UpdateClientPaidToDate::dispatchNow($payment->client, $this->payment->amount); return $this->invoice; } diff --git a/app/Jobs/Invoice/ReverseInvoicePayment.php b/app/Jobs/Invoice/ReverseInvoicePayment.php index c2564a5e4263..a21d985f1de5 100644 --- a/app/Jobs/Invoice/ReverseInvoicePayment.php +++ b/app/Jobs/Invoice/ReverseInvoicePayment.php @@ -12,6 +12,7 @@ namespace App\Jobs\Invoice; use App\Jobs\Client\UpdateClientBalance; +use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Util\SystemLogger; @@ -67,7 +68,7 @@ class ReverseInvoicePayment implements ShouldQueue UpdateClientBalance::dispatchNow($client, $this->payment->amount); - + UpdateClientPaidToDate::dispatchNow($client, $this->payment->amount*-1); } } \ No newline at end of file diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Jobs/Invoice/UpdateInvoicePayment.php index 9d33d49a6ff5..6f5fa686d4a3 100644 --- a/app/Jobs/Invoice/UpdateInvoicePayment.php +++ b/app/Jobs/Invoice/UpdateInvoicePayment.php @@ -12,6 +12,7 @@ namespace App\Jobs\Invoice; use App\Jobs\Client\UpdateClientBalance; +use App\Jobs\Client\UpdateClientPaidToDate; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Util\SystemLogger; @@ -58,15 +59,14 @@ class UpdateInvoicePayment implements ShouldQueue $invoices->each(function ($invoice){ UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1)); + UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1); + UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance); $invoice->pivot->amount = $invoice->balance; $invoice->pivot->save(); $invoice->clearPartial(); $invoice->updateBalance($invoice->balance*-1); - - - UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1); }); @@ -96,7 +96,8 @@ class UpdateInvoicePayment implements ShouldQueue if($invoice->hasPartial()) { UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1)); - + UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1); + UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->partial); $invoice->pivot->amount = $invoice->partial; $invoice->pivot->save(); @@ -105,12 +106,15 @@ class UpdateInvoicePayment implements ShouldQueue $invoice->setDueDate(); $invoice->setStatus(Invoice::STATUS_PARTIAL); - UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1); - + + } else { + UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1)); + UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1); + UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance); $invoice->pivot->amount = $invoice->balance; $invoice->pivot->save(); @@ -118,7 +122,7 @@ class UpdateInvoicePayment implements ShouldQueue $invoice->clearPartial(); $invoice->updateBalance($invoice->balance*-1); - UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1); + } @@ -140,7 +144,12 @@ class UpdateInvoicePayment implements ShouldQueue $this->payment->client ); - throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total}"); + throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total} reversing payment"); + + $this->payment->invoice()->delete(); + $this->payment->is_deleted=true; + $this->payment->save(); + $this->payment->delete(); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 320528aa2cdb..3fc46992aa84 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -14,6 +14,7 @@ namespace App\Models; use App\Events\Invoice\InvoiceWasUpdated; use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; +use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Invoice\CreateInvoicePdf; use App\Models\Currency; use App\Models\Filterable; @@ -388,4 +389,35 @@ class Invoice extends BaseModel $this->save(); } + public function markSent() + { + /* Return immediately if status is not draft */ + if($this->status_id != Invoice::STATUS_DRAFT) + return $this; + + $this->status_id = Invoice::STATUS_SENT; + + $this->markInvitationsSent(); + + UpdateClientBalance::dispatchNow($this->client, $this->balance); + + $this->save(); + } + + /** + * Updates Invites to SENT + * + */ + private function markInvitationsSent() + { + $this->invitations->each(function($invitation) { + + if(!isset($invitation->sent_date)) + { + $invitation->sent_date = Carbon::now(); + $invitation->save(); + } + + }); + } } \ No newline at end of file diff --git a/app/Models/Payment.php b/app/Models/Payment.php index ab39ae141fde..18ca88b16cc2 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -62,6 +62,7 @@ class Payment extends BaseModel 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', + 'is_deleted' => 'bool', ]; public function client() diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index b96e94993741..8e3260878451 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -140,15 +140,7 @@ class InvoiceRepository extends BaseRepository */ public function markSent(Invoice $invoice) : ?Invoice { - /* Return immediately if status is not draft */ - if($invoice->status_id != Invoice::STATUS_DRAFT) - return $invoice; - - $invoice->status_id = Invoice::STATUS_SENT; - - $this->markInvitationsSent($invoice); - - $invoice->save(); + $invoice->markSent(); /* * Why? because up until this point the invoice was a draft. @@ -164,22 +156,6 @@ class InvoiceRepository extends BaseRepository } - /** - * Updates Invites to SENT - * - * @param \App\Models\Invoice $invoice The invoice - */ - private function markInvitationsSent(Invoice $invoice) :void - { - $invoice->invitations->each(function($invitation) { - if(!isset($invitation->sent_date)) - { - $invitation->sent_date = Carbon::now(); - $invitation->save(); - } - - }); - } } \ No newline at end of file