diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 0c9af4989870..02cd86de56ef 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -347,9 +347,6 @@ class BaseRepository } - if(isset($data['mark_sent']) && $data['mark_sent'] == true) - $model->service()->markSent()->save(); - $model->save(); return $model->fresh(); diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 8a231513ce66..07838ce12484 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -71,7 +71,7 @@ class AddGatewayFee extends AbstractService private function processGatewayFee($gateway_fee) { $invoice_item = new InvoiceItem; - $invoice_item->type_id = 3; + $invoice_item->type_id = 4; $invoice_item->product_key = ctrans('texts.surcharge'); $invoice_item->notes = ctrans('texts.online_payment_surcharge'); $invoice_item->quantity = 1; @@ -89,8 +89,14 @@ class AddGatewayFee extends AbstractService $this->invoice->line_items = $invoice_items; + /**Refresh Invoice values*/ $this->invoice = $this->invoice->calc()->getInvoice(); + /*Update client balance*/ + $this->invoice->client->service()->updateBalance($gateway_fee)->save(); + + $this->invoice->ledger()->updateInvoiceBalance($gateway_fee, $notes = 'Gateway fee adjustment'); + return $this->invoice; } @@ -118,6 +124,10 @@ class AddGatewayFee extends AbstractService $this->invoice = $this->invoice->calc()->getInvoice(); + $this->invoice->client->service()->updateBalance($gateway_fee)->save(); + + $this->invoice->ledger()->updateInvoiceBalance($gateway_fee, $notes = 'Discount fee adjustment'); + return $this->invoice; } } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index d76c5033dcdd..64ab746d9533 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -62,17 +62,16 @@ class AutoBillInvoice extends AbstractService $amount = $this->invoice->balance + $fee; } - /* Make sure we remove any stale fees*/ - $this->purgeStaleGatewayFees(); - - if($fee > 0) - $this->addFeeToInvoice($fee); - $payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount, $this->invoice); if($payment){ - - $this->invoice = $this->invoice->service()->toggleFeesPaid()->save(); + + if($this->invoice->partial > 0) + $amount = $this->invoice->partial; + else + $amount = $this->invoice->balance; + + $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $amount)->save(); } else @@ -144,34 +143,34 @@ class AutoBillInvoice extends AbstractService * * @return $this */ - private function purgeStaleGatewayFees() - { - $starting_amount = $this->invoice->amount; + // private function purgeStaleGatewayFees() + // { + // $starting_amount = $this->invoice->amount; - $line_items = $this->invoice->line_items; + // $line_items = $this->invoice->line_items; - $new_items = []; + // $new_items = []; - foreach($line_items as $item) - { + // foreach($line_items as $item) + // { - if($item->type_id != 3) - $new_items[] = $item; + // if($item->type_id != 3) + // $new_items[] = $item; - } + // } - $this->invoice->line_items = $new_items; - $this->invoice->save(); + // $this->invoice->line_items = $new_items; + // $this->invoice->save(); - $this->invoice = $this->invoice->calc()->getInvoice(); + // $this->invoice = $this->invoice->calc()->getInvoice(); - if($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT){ - $this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save(); - $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, 'Invoice balance updated after stale gateway fee removed')->save(); - } + // if($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT){ + // $this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save(); + // $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, 'Invoice balance updated after stale gateway fee removed')->save(); + // } - return $this; - } + // return $this; + // } /** * Checks whether a given gateway token is able diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index f86c2736d64a..a3d729a51b18 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -79,15 +79,18 @@ class InvoiceService public function addGatewayFee(CompanyGateway $company_gateway, float $amount) { + $this->invoice = (new AddGatewayFee($company_gateway, $this->invoice, $amount))->run(); return $this; } /** * Update an invoice balance + * * @param float $balance_adjustment The amount to adjust the invoice by * a negative amount will REDUCE the invoice balance, a positive amount will INCREASE * the invoice balance + * * @return InvoiceService Parent class object */ public function updateBalance($balance_adjustment) diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index e5b1bfd204f1..786c79c52342 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -56,6 +56,10 @@ class TriggeredActions extends AbstractService $this->sendEmail(); } + if($this->request->has('mark_sent') && $this->request->input('mark_sent') == 'true'){ + $this->invoice = $this->invoice->service()->markSent()->save(); + } + return $this->invoice; } diff --git a/app/Services/Invoice/UpdateBalance.php b/app/Services/Invoice/UpdateBalance.php index 34d06809f04e..6decec7a7f7e 100644 --- a/app/Services/Invoice/UpdateBalance.php +++ b/app/Services/Invoice/UpdateBalance.php @@ -37,8 +37,6 @@ class UpdateBalance extends AbstractService if ($this->invoice->balance == 0) { $this->invoice->status_id = Invoice::STATUS_PAID; - // $this->save(); - // event(new InvoiceWasPaid($this, $this->company)); } return $this->invoice;