diff --git a/app/Jobs/Credit/ApplyCreditPayment.php b/app/Jobs/Credit/ApplyCreditPayment.php new file mode 100644 index 0000000000..2ebdf56988 --- /dev/null +++ b/app/Jobs/Credit/ApplyCreditPayment.php @@ -0,0 +1,110 @@ +invoice = $invoice; + $this->payment = $payment; + $this->amount = $amount; + $this->company = $company; + } + + /** + * Execute the job. + * + * + * @return void + */ + public function handle() + { + MultiDB::setDB($this->company->db); + + UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->amount*-1), $this->company); + UpdateClientBalance::dispatchNow($this->payment->client, $this->amount*-1, $this->company); + UpdateClientPaidToDate::dispatchNow($this->payment->client, $this->amount, $this->company); + + /* Update Pivot Record amount */ + $this->payment->invoices->each(function ($inv) { + if ($inv->id == $this->invoice->id) { + $inv->pivot->amount = $this->amount; + $inv->pivot->save(); + } + }); + + if ($this->invoice->hasPartial()) { + //is partial and amount is exactly the partial amount + if ($this->invoice->partial == $this->amount) { + $this->invoice->clearPartial(); + $this->invoice->setDueDate(); + $this->invoice->setStatus(Invoice::STATUS_PARTIAL); + $this->invoice->updateBalance($this->amount*-1); + } elseif ($this->invoice->partial > 0 && $this->invoice->partial > $this->amount) { //partial amount exists, but the amount is less than the partial amount + $this->invoice->partial -= $this->amount; + $this->invoice->updateBalance($this->amount*-1); + } elseif ($this->invoice->partial > 0 && $this->invoice->partial < $this->amount) { //partial exists and the amount paid is GREATER than the partial amount + $this->invoice->clearPartial(); + $this->invoice->setDueDate(); + $this->invoice->setStatus(Invoice::STATUS_PARTIAL); + $this->invoice->updateBalance($this->amount*-1); + } + } elseif ($this->amount == $this->invoice->balance) { //total invoice paid. + $this->invoice->clearPartial(); + //$this->invoice->setDueDate(); + $this->invoice->setStatus(Invoice::STATUS_PAID); + $this->invoice->updateBalance($this->amount*-1); + } elseif($this->amount < $this->invoice->balance) { //partial invoice payment made + $this->invoice->clearPartial(); + $this->invoice->updateBalance($this->amount*-1); + } + + /* Update Payment Applied Amount*/ + $this->payment->applied += $this->amount; + $this->payment->save(); + } + + +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 2175441dc3..f86b2d991a 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -103,7 +103,7 @@ class Company extends BaseModel public function users() { - return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id')->withTimestamps(); + return $this->hasManyThrough(User::class, CompanyUser::class, 'company_id', 'id', 'id', 'user_id'); } /** diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index f711b34582..7a5f03ce5b 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -17,6 +17,7 @@ use App\Jobs\Company\UpdateCompanyLedgerWithPayment; use App\Jobs\Invoice\ApplyClientPayment; use App\Jobs\Invoice\ApplyInvoicePayment; use App\Jobs\Invoice\UpdateInvoicePayment; +use App\Models\Credit; use App\Models\Invoice; use App\Models\Payment; use App\Repositories\CreditRepository; @@ -27,8 +28,10 @@ use Illuminate\Http\Request; */ class PaymentRepository extends BaseRepository { + protected $credit_repo; + public function __construct(CreditRepository $credit_repo) { @@ -59,14 +62,14 @@ class PaymentRepository extends BaseRepository $payment->save(); - if ($request->input('invoices')) { + if ($request->input('invoices') && is_array($request->input('invoices'))) { $invoices = Invoice::whereIn('id', array_column($request->input('invoices'), 'id'))->company()->get(); $payment->invoices()->saveMany($invoices); foreach ($request->input('invoices') as $paid_invoice) { - $invoice = Invoice::whereId($paid_invoice['id'])->company()->first(); + $invoice = Invoice::whereId($this->decodePrimaryKey($paid_invoice['id'])->company()->first(); if ($invoice) { ApplyInvoicePayment::dispatchNow($invoice, $payment, $paid_invoice['amount'], $invoice->company); @@ -77,6 +80,22 @@ class PaymentRepository extends BaseRepository ApplyClientPayment::dispatchNow($payment, $payment->company); } + if($request->input('credits') && is_array($request->input('credits'))) + { + $credits = Credit::whereIn('id', array_column($request->input('credits'), 'id'))->company()->get(); + + $payment->credits()->saveMany($credits); + + foreach ($request->input('credits') as $paid_credit) + { + $credit = Credit::whereId($this->decodePrimaryKey($paid_credit['id'])->company()->first(); + + if($credit) + ApplyCreditPayment::dispatchNow($paid_credit, $payment, $paid_credit['amount'], $credit->company); + } + + } + event(new PaymentWasCreated($payment, $payment->company)); //UpdateInvoicePayment::dispatchNow($payment);