diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 7f5c97ebf293..e84a733baa66 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -12,8 +12,6 @@ use App\Factory\InvoiceItemFactory; use App\Factory\PaymentFactory; use App\Factory\QuoteFactory; use App\Helpers\Invoice\InvoiceSum; -use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; -use App\Jobs\Invoice\UpdateInvoicePayment; use App\Jobs\Quote\CreateQuoteInvitations; use App\Listeners\Credit\CreateCreditInvitation; use App\Listeners\Invoice\CreateInvoiceInvitation; @@ -459,7 +457,9 @@ class CreateTestData extends Command $invoice->save(); $invoice->service()->createInvitations(); - UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company); + $invoice->ledger()->updateInvoiceBalance($invoice->balance); + + //UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company); $this->invoice_repo->markSent($invoice); @@ -480,7 +480,8 @@ class CreateTestData extends Command event(new PaymentWasCreated($payment, $payment->company)); - UpdateInvoicePayment::dispatchNow($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)); diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index fe14aad48641..e421fd7f97e5 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -442,8 +442,8 @@ class PaymentController extends BaseController */ public function destroy(DestroyPaymentRequest $request, Payment $payment) { - ReverseInvoicePayment::dispatchNow($payment, $payment->company); - + $payment->service()->reversePayment(); + $payment->is_deleted = true; $payment->save(); $payment->delete(); diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php deleted file mode 100644 index 390ae5368650..000000000000 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ /dev/null @@ -1,76 +0,0 @@ -invoice = $invoice; - - $this->adjustment = $adjustment; - - $this->company = $company; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - MultiDB::setDB($this->company->db); - - $balance = 0; - - $ledger = CompanyLedger::whereClientId($this->invoice->client_id) - ->whereCompanyId($this->invoice->company_id) - ->orderBy('id', 'DESC') - ->first(); - - if ($ledger) { - $balance = $ledger->balance; - } - - $adjustment = $balance + $this->adjustment; - - $company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id); - $company_ledger->client_id = $this->invoice->client_id; - $company_ledger->adjustment = $this->adjustment; - $company_ledger->balance = $balance + $this->adjustment; - $company_ledger->save(); - - $this->invoice->company_ledger()->save($company_ledger); - } -} diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php deleted file mode 100644 index 1b61202e43ad..000000000000 --- a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php +++ /dev/null @@ -1,80 +0,0 @@ -payment = $payment; - - $this->adjustment = $adjustment; - - $this->company = $company; - } - - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - MultiDB::setDB($this->company->db); - - - $balance = 0; - - /* Get the last record for the client and set the current balance*/ - $ledger = CompanyLedger::whereClientId($this->payment->client_id) - ->whereCompanyId($this->payment->company_id) - ->orderBy('id', 'DESC') - ->first(); - - if ($ledger) { - $balance = $ledger->balance; - } - - - $company_ledger = CompanyLedgerFactory::create($this->payment->company_id, $this->payment->user_id); - $company_ledger->client_id = $this->payment->client_id; - $company_ledger->adjustment = $this->adjustment; - $company_ledger->balance = $balance + $this->adjustment; - $company_ledger->save(); - - $this->payment->company_ledger()->save($company_ledger); //todo add model directive here - } -} diff --git a/app/Jobs/Credit/ApplyCreditPayment.php b/app/Jobs/Credit/ApplyCreditPayment.php index 4b7d47fde424..113763062a33 100644 --- a/app/Jobs/Credit/ApplyCreditPayment.php +++ b/app/Jobs/Credit/ApplyCreditPayment.php @@ -13,7 +13,6 @@ namespace App\Jobs\Credit; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Credit\ApplyPaymentToCredit; use App\Libraries\MultiDB; use App\Models\Company; diff --git a/app/Jobs/Invitation/MarkOpened.php b/app/Jobs/Invitation/MarkOpened.php index c2a7bf894544..688a2dbeb0b8 100644 --- a/app/Jobs/Invitation/MarkOpened.php +++ b/app/Jobs/Invitation/MarkOpened.php @@ -61,7 +61,8 @@ class MarkOpened implements ShouldQueue return false; } - $invitation->email_error = $error; + $invitation->opened_date = now(); + //$invitation->email_error = $error; $invitation->save(); } } diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php deleted file mode 100644 index 5cb03fe495e8..000000000000 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ /dev/null @@ -1,120 +0,0 @@ -invoice = $invoice; - - $this->payment = $payment; - - $this->company = $company; - } - - /** - * Execute the job. - * - * - * @return void - */ - public function handle() - { - MultiDB::setDB($this->company->db); - - /* The amount we are adjusting the invoice by*/ - $adjustment = $this->payment->amount * -1; - - /* Calculate if the amount paid is less than the partial value. - * Needed if there is a condition under which a value LESS - * than the partial amount has been paid. The Invoice will - * be updated to reflect the NEW partial amount - */ - $partial = max(0, $this->invoice->partial - $this->payment->amount); - - /* check if partial exists */ - if ($this->invoice->partial > 0) { - - //if payment amount = partial - if ($this->formatvalue($this->invoice->partial, 4) == $this->formatValue($this->payment->amount, 4)) { - $this->invoice->partial = 0; - - $this->invoice->partial_due_date = null; - } - - //if payment amount < partial amount - if ($this->formatvalue($this->invoice->partial, 4) > $this->formatValue($this->payment->amount, 4)) { - //set the new partial amount to the balance - $this->invoice->partial = $partial; - } - - - if (!$this->invoice->due_date) { - $this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days); - } - } - - /* Update Invoice Balance */ - $this->invoice->balance = $this->invoice->balance + $adjustment; - - /* Update Invoice Status */ - if ($this->invoice->balance == 0) { - $this->invoice->status_id = Invoice::STATUS_PAID; - $this->invoice->save(); - event(new InvoiceWasPaid($this->invoice, $this->invoice->company)); - } elseif ($this->payment->amount > 0 && $this->invoice->balance > 0) { - $this->invoice->status_id = Invoice::STATUS_PARTIAL; - } - - /*If auto-archive is enabled, and balance = 0 - archive invoice */ - if ($this->invoice->settings->auto_archive_invoice && $this->invoice->balance == 0) { - $invoiceRepo = app('App\Repositories\InvoiceRepository'); - $invoiceRepo->archive($this->invoice); - } - - $this->invoice->save(); - - $this->invoice = $this->invoice->service()->applyNumber()->save(); - - return $this->invoice; - } -} diff --git a/app/Jobs/Invoice/CreateInvoicePdf.php b/app/Jobs/Invoice/CreateInvoicePdf.php index 58524715060c..a005efd9e260 100644 --- a/app/Jobs/Invoice/CreateInvoicePdf.php +++ b/app/Jobs/Invoice/CreateInvoicePdf.php @@ -100,7 +100,7 @@ class CreateInvoicePdf implements ShouldQueue { $instance = Storage::disk($this->disk)->put($file_path, $pdf); //$instance= Storage::disk($this->disk)->path($file_path); - + // return $file_path; } diff --git a/app/Jobs/Invoice/ReverseInvoicePayment.php b/app/Jobs/Invoice/ReverseInvoicePayment.php deleted file mode 100644 index 692499f0b910..000000000000 --- a/app/Jobs/Invoice/ReverseInvoicePayment.php +++ /dev/null @@ -1,77 +0,0 @@ -payment = $payment; - $this->company = $company; - } - - /** - * Handle the event. - * - * @param object $event - * @return void - */ - public function handle() - { - MultiDB::setDB($this->company->db); - - $invoices = $this->payment->invoices()->get(); - $client = $this->payment->client; - - $invoices->each(function ($invoice) { - if ($invoice->pivot->amount > 0) { - $invoice->status_id = Invoice::STATUS_SENT; - $invoice->balance = $invoice->pivot->amount; - $invoice->save(); - } - }); - - UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment->amount), $this->company); - - $client->service() - ->updateBalance($this->payment->amount) - ->updatePaidToDate($this->payment->amount*-1) - ->save(); - - } -} diff --git a/app/Jobs/Quote/CreateQuotePdf.php b/app/Jobs/Quote/CreateQuotePdf.php index 98d991734cc4..495b60544f9f 100644 --- a/app/Jobs/Quote/CreateQuotePdf.php +++ b/app/Jobs/Quote/CreateQuotePdf.php @@ -100,28 +100,8 @@ class CreateQuotePdf implements ShouldQueue { $instance = Storage::disk($this->disk)->put($file_path, $pdf); //$instance= Storage::disk($this->disk)->path($file_path); - + // return $file_path; } - /** - * Returns a PDF stream - * - * @param string $header Header to be included in PDF - * @param string $footer Footer to be included in PDF - * @param string $html The HTML object to be converted into PDF - * - * @return string The PDF string - */ - private function makePdf($header, $footer, $html) { - return Browsershot::html($html) - //->showBrowserHeaderAndFooter() - //->headerHtml($header) - //->footerHtml($footer) - ->deviceScaleFactor(1) - ->showBackground() - ->waitUntilNetworkIdle(true) ->pdf(); - //->margins(10,10,10,10) - //->savePdf('test.pdf'); - } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 7de1e96398ab..6e5e01af4442 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -19,10 +19,12 @@ use App\Helpers\Invoice\InvoiceSumInclusive; use App\Jobs\Client\UpdateClientBalance; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Invoice\CreateInvoicePdf; +use App\Models\CompanyLedger; use App\Models\Currency; use App\Models\Filterable; use App\Models\PaymentTerm; use App\Services\Invoice\InvoiceService; +use App\Services\Ledger\LedgerService; use App\Utils\Number; use App\Utils\Traits\InvoiceEmailBuilder; use App\Utils\Traits\MakesDates; @@ -163,11 +165,16 @@ class Invoice extends BaseModel /** * Service entry points */ - public function service(): InvoiceService + public function service() :InvoiceService { return new InvoiceService($this); } + public function ledger() + { + return new LedgerService($this); + } + /* ---------------- */ /* Settings getters */ /* ---------------- */ diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 09d76117b17b..3873f843fe97 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -17,6 +17,7 @@ use App\Models\Credit; use App\Models\DateFormat; use App\Models\Filterable; use App\Models\Paymentable; +use App\Services\Ledger\LedgerService; use App\Utils\Number; use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; @@ -169,6 +170,11 @@ class Payment extends BaseModel } } + public function ledger() + { + return new LedgerService($this); + } + public function resolveRouteBinding($value) { return $this diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index c3986d24ccd0..dc7b8193fe51 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -12,7 +12,7 @@ namespace App\PaymentDrivers; use App\Events\Payment\PaymentWasCreated; -use App\Jobs\Invoice\UpdateInvoicePayment; +//use App\Jobs\Invoice\UpdateInvoicePayment; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -160,7 +160,9 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver event(new PaymentWasCreated($payment, $payment->company)); - UpdateInvoicePayment::dispatchNow($payment, $payment->company); + $payment->service()->UpdateInvoicePayment(); + + //UpdateInvoicePayment::dispatchNow($payment, $payment->company); return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]); } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index d41b960f3286..ae09a938f688 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -13,7 +13,7 @@ namespace App\PaymentDrivers; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Invoice\UpdateInvoicePayment; +//use App\Jobs\Invoice\UpdateInvoicePayment; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; @@ -361,7 +361,8 @@ class StripePaymentDriver extends BasePaymentDriver event(new PaymentWasCreated($payment, $payment->company)); - UpdateInvoicePayment::dispatchNow($payment, $payment->company); + $payment->service()->UpdateInvoicePayment(); + //UpdateInvoicePayment::dispatchNow($payment, $payment->company); SystemLogger::dispatch( [ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index cbdbc97533e9..5a284faffd17 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -38,7 +38,6 @@ use App\Listeners\Invoice\InvoiceEmailActivity; use App\Listeners\Invoice\InvoiceEmailFailedActivity; use App\Listeners\Invoice\UpdateInvoiceActivity; use App\Listeners\Invoice\UpdateInvoiceInvitations; -use App\Listeners\Invoice\UpdateInvoicePayment; use App\Listeners\SendVerificationNotification; use App\Listeners\User\UpdateUserLastLogin; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -73,8 +72,6 @@ class EventServiceProvider extends ServiceProvider ], PaymentWasCreated::class => [ PaymentCreatedActivity::class, - //UpdateInvoicePayment::class, - //UpdateInvoiceInvitations::class, ], PaymentWasDeleted::class => [ PaymentDeletedActivity::class, diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index a46ef46a1329..550f7a403a1c 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -13,9 +13,7 @@ namespace App\Repositories; use App\Factory\InvoiceInvitationFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Jobs\Product\UpdateOrCreateProduct; - use App\Models\ClientContact; use App\Models\Invoice; use App\Models\InvoiceInvitation; @@ -109,7 +107,7 @@ class InvoiceRepository extends BaseRepository { /**/ if (($finished_amount != $starting_amount) && ($invoice->status_id != Invoice::STATUS_DRAFT)) { - UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount-$starting_amount), $invoice->company); + $invoice->ledger()->updateInvoiceBalance(($finished_amount-$starting_amount)); } $invoice = $invoice->service()->applyNumber()->save(); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 22aa797438cf..3d8eca644cb4 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -13,9 +13,8 @@ namespace App\Repositories; use App\Events\Payment\PaymentWasCreated; use App\Factory\CreditFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Credit\ApplyCreditPayment; -use App\Jobs\Invoice\UpdateInvoicePayment; +//use App\Jobs\Invoice\UpdateInvoicePayment; use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; @@ -130,7 +129,6 @@ class PaymentRepository extends BaseRepository elseif ($invoice_totals < $payment->amount) $payment->applied += $invoice_totals; - //UpdateInvoicePayment::dispatchNow($payment); $payment->save(); return $payment->fresh(); diff --git a/app/Services/Credit/ApplyNumber.php b/app/Services/Credit/ApplyNumber.php index 3e9753aa6c6f..6b81e9d4db9d 100644 --- a/app/Services/Credit/ApplyNumber.php +++ b/app/Services/Credit/ApplyNumber.php @@ -5,7 +5,6 @@ namespace App\Services\Credit; use App\Credit; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Customer\UpdateCustomerBalance; use App\Jobs\Customer\UpdateCustomerPaidToDate; use App\Models\Client; diff --git a/app/Services/Invoice/ApplyNumber.php b/app/Services/Invoice/ApplyNumber.php index 97cfb2467db1..ca49defd1b0a 100644 --- a/app/Services/Invoice/ApplyNumber.php +++ b/app/Services/Invoice/ApplyNumber.php @@ -13,7 +13,6 @@ namespace App\Services\Invoice; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Models\Client; use App\Models\Invoice; use App\Models\Payment; diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index a8ec2b0ad46c..a99bba072b11 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -11,7 +11,6 @@ namespace App\Services\Invoice; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Models\Invoice; use App\Models\Payment; use App\Services\AbstractService; @@ -35,8 +34,9 @@ class ApplyPayment extends AbstractService public function run() { - - UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment_amount*-1), $this->payment->company); + $this->payment + ->ledger() + ->updatePaymentBalance($this->payment_amount*-1); $this->payment->client->service()->updateBalance($this->payment_amount*-1)->save(); diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index 42879a1c17c5..831c9f84c799 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -13,7 +13,6 @@ namespace App\Services\Invoice; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; -use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Models\Invoice; use App\Models\Payment; use App\Services\AbstractService; @@ -61,7 +60,8 @@ class MarkPaid extends AbstractService /* Update Invoice balance */ event(new PaymentWasCreated($payment, $payment->company)); - UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1), $payment->company); + $payment->ledger() + ->updatePaymentBalance($payment->amount*-1); $this->client_service ->updateBalance($payment->amount*-1) diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 3b5dac621f45..9f23f12b65e0 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -12,7 +12,6 @@ namespace App\Services\Invoice; use App\Events\Invoice\InvoiceWasMarkedSent; -use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Models\Invoice; use App\Services\AbstractService; @@ -47,7 +46,9 @@ class MarkSent extends AbstractService $this->invoice->service()->setStatus(Invoice::STATUS_SENT)->applyNumber()->save(); - UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->balance, $this->invoice->company); + $this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance); + + //UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->balance, $this->invoice->company); return $this->invoice; diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 6133cbbb45dc..a9bc17fbc313 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -11,6 +11,7 @@ namespace App\Services\Ledger; +use App\Factory\CompanyLedgerFactory; use App\Models\CompanyLedger; class LedgerService @@ -27,8 +28,10 @@ class LedgerService { $balance = 0; - if ($this->ledger()) { - $balance = $this->ledger->balance; + $company_ledger = $this->ledger(); + + if ($company_ledger) { + $balance = $company_ledger->balance; } $adjustment = $balance + $adjustment; @@ -44,13 +47,35 @@ class LedgerService return $this; } - private function ledger() :CompanyLedger + public function updatePaymentBalance($adjustment) + { + $balance = 0; + + /* Get the last record for the client and set the current balance*/ + $company_ledger = $this->ledger(); + + if ($company_ledger) { + $balance = $company_ledger->balance; + } + + + $company_ledger = CompanyLedgerFactory::create($this->entity->company_id, $this->entity->user_id); + $company_ledger->client_id = $this->entity->client_id; + $company_ledger->adjustment = $adjustment; + $company_ledger->balance = $balance + $adjustment; + $company_ledger->save(); + + $this->entity->company_ledger()->save($company_ledger); + } + + private function ledger() :?CompanyLedger { return CompanyLedger::whereClientId($this->entity->client_id) ->whereCompanyId($this->entity->company_id) ->orderBy('id', 'DESC') ->first(); + } public function save() diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 581262b344e6..5945c971ac92 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -12,7 +12,9 @@ namespace App\Services\Payment; use App\Factory\PaymentFactory; +use App\Models\Invoice; use App\Models\Payment; +use App\Services\Payment\UpdateInvoicePayment; class PaymentService @@ -45,8 +47,32 @@ class PaymentService public function sendEmail($contact = null) { - $send_email = new SendEmail($this->payment); + return (new SendEmail($this->payment))->run(null, $contact); + } - return $send_email->run(null, $contact); + public function reversePayment() + { + $invoices = $this->payment->invoices()->get(); + $client = $this->payment->client; + + $invoices->each(function ($invoice) { + if ($invoice->pivot->amount > 0) { + $invoice->status_id = Invoice::STATUS_SENT; + $invoice->balance = $invoice->pivot->amount; + $invoice->save(); + } + }); + + $this->payment->ledger()->updatePaymentBalance($this->payment->amount); + + $client->service() + ->updateBalance($this->payment->amount) + ->updatePaidToDate($this->payment->amount*-1) + ->save(); + } + + public function updateInvoicePayment() + { + return ((new UpdateInvoicePayment($this->payment)))->run(); } } diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php similarity index 69% rename from app/Jobs/Invoice/UpdateInvoicePayment.php rename to app/Services/Payment/UpdateInvoicePayment.php index 3da29bdf2f8b..b4563bc9b146 100644 --- a/app/Jobs/Invoice/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -1,68 +1,35 @@ payment = $payment; - $this->company = $company; } - /** - * Handle the event. - * - * @param object $event - * @return void - */ - public function handle() + public function run() { - MultiDB::setDB($this->company->db); - - $invoices = $this->payment->invoices()->get(); + $invoices = $this->payment->invoices()->get(); $invoices_total = $invoices->sum('balance'); /* Simplest scenario - All invoices are paid in full*/ if (strval($invoices_total) === strval($this->payment->amount)) { $invoices->each(function ($invoice) { - UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company); + + $this->payment + ->ledger() + ->updatePaymentBalance($this->payment, ($invoice->balance*-1)); $this->payment->client ->service() @@ -96,7 +63,10 @@ class UpdateInvoicePayment implements ShouldQueue if ($this->payment->amount == $total) { $invoices->each(function ($invoice) { if ($invoice->hasPartial()) { - UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1), $this->company); + + $this->payment + ->ledger() + ->updatePaymentBalance($this->payment, ($invoice->partial*-1)); $this->payment->client->service() ->updateBalance($invoice->partial*-1) @@ -112,7 +82,10 @@ class UpdateInvoicePayment implements ShouldQueue ->setStatus(Invoice::STATUS_PARTIAL) ->save(); } else { - UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company); + + $this->payment + ->ledger() + ->updatePaymentBalance($this->payment, ($invoice->balance*-1)); $this->payment->client->service() ->updateBalance($invoice->balance*-1) @@ -148,5 +121,8 @@ class UpdateInvoicePayment implements ShouldQueue $this->payment->delete(); } } + + return $this->payment; } -} + +} \ No newline at end of file diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 0a83d67f76e7..afc6dd973a3d 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -312,6 +312,8 @@ trait MakesInvoiceValues $data['$invoice3'] = $this->custom_value3 ?: ' '; $data['$invoice4'] = $this->custom_value4 ?: ' '; $data['$invoice.public_notes'] = $this->public_notes ?: ' '; + $data['$entity.public_notes'] = &$data['$invoice.public_notes']; + // $data['$your_invoice'] = ; // $data['$quote'] = ; // $data['$your_quote'] = ; diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index f110724058a3..c964ca101805 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -9,7 +9,7 @@ use App\Events\Payment\PaymentWasCreated; use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; -use App\Jobs\Invoice\UpdateInvoicePayment; +//use App\Jobs\Invoice\UpdateInvoicePayment; use App\Listeners\Credit\CreateCreditInvitation; use App\Listeners\Invoice\CreateInvoiceInvitation; use App\Models\Account; @@ -187,7 +187,9 @@ class RandomDataSeeder extends Seeder event(new PaymentWasCreated($payment, $payment->company)); - UpdateInvoicePayment::dispatchNow($payment, $payment->company); + $payment->service()->UpdateInvoicePayment(); + + // UpdateInvoicePayment::dispatchNow($payment, $payment->company); } }); diff --git a/tests/Integration/DesignTest.php b/tests/Integration/DesignTest.php index 7303175068a5..f92e09e7c928 100644 --- a/tests/Integration/DesignTest.php +++ b/tests/Integration/DesignTest.php @@ -38,7 +38,7 @@ class DesignTest extends TestCase //\Log::error($html); $settings = $this->invoice->client->settings; - $settings->invoice_design_id = "4"; + $settings->invoice_design_id = "5"; $this->client->settings = $settings; $this->client->save(); @@ -60,7 +60,7 @@ class DesignTest extends TestCase //\Log::error($html); $settings = $this->invoice->client->settings; - $settings->quote_design_id = "4"; + $settings->quote_design_id = "10"; $this->client->settings = $settings; $this->client->save(); diff --git a/tests/Integration/UpdateCompanyLedgerTest.php b/tests/Integration/UpdateCompanyLedgerTest.php index 066ac53829f5..7a30b753cae7 100644 --- a/tests/Integration/UpdateCompanyLedgerTest.php +++ b/tests/Integration/UpdateCompanyLedgerTest.php @@ -18,6 +18,9 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\MockAccountData; use Tests\TestCase; +/** @test +/** @covers App\Services\Ledger\LedgerService */ + class UpdateCompanyLedgerTest extends TestCase { use MockAccountData; diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 87303517db5c..9a3d91aa190e 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -22,7 +22,6 @@ use App\Factory\InvoiceInvitationFactory; use App\Factory\InvoiceItemFactory; use App\Factory\InvoiceToRecurringInvoiceFactory; use App\Helpers\Invoice\InvoiceSum; -use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Models\Client; use App\Models\CompanyGateway; use App\Models\CompanyToken; @@ -272,7 +271,8 @@ trait MockAccountData $this->invoice->save(); - UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company); + $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount); + // UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company); $recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice); $recurring_invoice->next_send_date = Carbon::now();