Working on fees and limits

This commit is contained in:
David Bomba 2020-07-07 22:33:11 +10:00
parent 89d15f1e2a
commit 83fc30f7e7
7 changed files with 110 additions and 32 deletions

View File

@ -452,11 +452,17 @@ class Client extends BaseModel implements HasLocalePreference
}
$gateways->filter(function ($method) use ($amount) {
if ($method->min_limit !== null && $amount < $method->min_limit) {
$fees_and_limits = $method->fees_and_limits;
if(!$fees_and_limits)
return false;
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) {
return false;
}
if ($method->max_limit !== null && $amount > $method->min_limit) {
if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->min_limit) {
return false;
}
});

View File

@ -268,7 +268,9 @@ class Invoice extends BaseModel
public function isPayable(): bool
{
if ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) {
if($this->status_id == Invoice::STATUS_DRAFT && $this->is_deleted == false){
return true;
}else if ($this->status_id == Invoice::STATUS_SENT && $this->is_deleted == false) {
return true;
} elseif ($this->status_id == Invoice::STATUS_PARTIAL && $this->is_deleted == false) {
return true;

View File

@ -0,0 +1,61 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Services\Invoice;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
use App\Services\Client\ClientService;
use App\Services\Payment\PaymentService;
use App\Utils\Traits\GeneratesCounter;
class AutoBillInvoice extends AbstractService
{
private $invoice;
private $client;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
$this->client = $invoice->client;
}
public function run()
{
if(!$invoice->isPayable())
return $invoice;
}
private function getGateway($amount)
{
$gateway_tokens = $this->client->gateway_tokens->orderBy('is_default', 'DESC');
$gateways->filter(function ($method) use ($amount) {
if ($method->min_limit !== null && $amount < $method->min_limit) {
return false;
}
if ($method->max_limit !== null && $amount > $method->min_limit) {
return false;
}
});
}
}

View File

@ -16,6 +16,7 @@ use App\Models\Payment;
use App\Services\Client\ClientService;
use App\Services\Invoice\ApplyNumber;
use App\Services\Invoice\ApplyPayment;
use App\Services\Invoice\AutoBillInvoice;
use App\Services\Invoice\CreateInvitations;
use App\Services\Invoice\GetInvoicePdf;
use App\Services\Invoice\HandleCancellation;
@ -103,7 +104,6 @@ class InvoiceService
return $this;
}
public function getInvoicePdf($contact = null)
{
return (new GetInvoicePdf($this->invoice, $contact))->run();
@ -144,6 +144,13 @@ class InvoiceService
return $this;
}
public function autoBill()
{
$this->invoice = (new AutoBillInvoice($this->invoice))->run();
return $this;
}
public function markViewed()
{
$this->invoice->last_viewed = Carbon::now()->format('Y-m-d H:i');

View File

@ -42,20 +42,17 @@ class TriggeredActions extends AbstractService
public function run()
{
//the request may have buried in it additional actions we should automatically perform on the invoice
if($this->request->has('send_email')) {
}
if($this->request->has('auto_bill')) {
}
if($this->request->has('paid') && (bool)$this->request->input('paid') !== false) {
$this->invoice->service()->markPaid()->save();
}
if($this->request->has('send_email') && (bool)$this->request->input('send_email') !== false) {
$this->sendEmail();
}
return $this->invoice;

View File

@ -104,14 +104,15 @@ trait CompanyGatewayFeesAndLimitsSaver
foreach ($fees_and_limits as $key => $value) {
$fal = new FeesAndLimits;
// $fal->{$key} = $value;
$fal->{$key} = $value;
// foreach ($value as $k => $v) {
// $fal->{$k} = $v;
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
// }
foreach ($value as $k => $v) {
// $new_arr[$key] = (array)$fal;
$fal->{$k} = $v;
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
}
$new_arr[$key] = (array)$fal;
}
return $new_arr;

View File

@ -22,14 +22,17 @@ class FeesAndLimitsTest extends TestCase
$data['max_limit'] = 65317;
$data['fee_amount'] = 0.00;
$data['fee_percent'] = 0.000;
$data['tax_name1'] = '' ;
$data['tax_rate1'] = '';
$data['tax_name2'] = '';
$data['tax_rate2'] = '';
$data['tax_name3'] = '';
$data['tax_rate3'] = 0;
$data['fee_tax_name1'] = '';
$data['fee_tax_rate1'] = '';
$data['fee_tax_name2'] = '';
$data['fee_tax_rate2'] = '';
$data['fee_tax_name3'] = '';
$data['fee_tax_rate3'] = 0;
$transformed = $this->cleanFeesAndLimits($data);
$fees_and_limits_array = [];
$fees_and_limits_array[] = $data;
$transformed = $this->cleanFeesAndLimits($fees_and_limits_array);
$this->assertTrue(is_array($transformed));
}
@ -41,14 +44,15 @@ class FeesAndLimitsTest extends TestCase
foreach ($fees_and_limits as $key => $value) {
$fal = new FeesAndLimits;
// $fal->{$key} = $value;
$fal->{$key} = $value;
// foreach ($value as $k => $v) {
// $fal->{$k} = $v;
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
// }
foreach ($value as $k => $v) {
// $new_arr[$key] = (array)$fal;
$fal->{$k} = $v;
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
}
$new_arr[$key] = (array)$fal;
}
return $new_arr;