diff --git a/app/Models/Client.php b/app/Models/Client.php index 754119dc6a2c..823df416b65d 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -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; } }); diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 50cffcfd250b..90afed56db87 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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; diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php new file mode 100644 index 000000000000..cf79e31b372c --- /dev/null +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -0,0 +1,61 @@ +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; + } + }); + } +} diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 45e1e80d6779..c1999ec42edb 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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'); diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index 6162dee9b983..c40fca791e84 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -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; diff --git a/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php b/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php index f2c1e6de5817..b5802eebe75c 100644 --- a/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php +++ b/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php @@ -103,15 +103,16 @@ trait CompanyGatewayFeesAndLimitsSaver $new_arr = []; foreach ($fees_and_limits as $key => $value) { - $fal = new FeesAndLimits; + $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; diff --git a/tests/Unit/Migration/FeesAndLimitsTest.php b/tests/Unit/Migration/FeesAndLimitsTest.php index 79a84a1c2bde..0df50f9623f4 100644 --- a/tests/Unit/Migration/FeesAndLimitsTest.php +++ b/tests/Unit/Migration/FeesAndLimitsTest.php @@ -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)); } @@ -40,15 +43,16 @@ class FeesAndLimitsTest extends TestCase $new_arr = []; foreach ($fees_and_limits as $key => $value) { - $fal = new FeesAndLimits; + $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;