From 9204a05654904ee4cd320cd94f20bf5ffce87fc1 Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 15 May 2022 00:24:05 +1000 Subject: [PATCH] Added insertPricing & allPricing function Added insertPricing & allPricing function --- app/Http/Controllers/DomainsController.php | 11 +-------- app/Http/Controllers/ResellerController.php | 11 +-------- app/Http/Controllers/ServerController.php | 13 ++--------- app/Http/Controllers/SharedController.php | 11 +-------- app/Models/Pricing.php | 25 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 41 deletions(-) diff --git a/app/Http/Controllers/DomainsController.php b/app/Http/Controllers/DomainsController.php index ff8c364..3823f7e 100644 --- a/app/Http/Controllers/DomainsController.php +++ b/app/Http/Controllers/DomainsController.php @@ -63,16 +63,7 @@ class DomainsController extends Controller $as_usd = $pricing->convertToUSD($request->price, $request->currency); - Pricing::create([ - 'service_id' => $domain_id, - 'service_type' => 4, - 'currency' => $request->currency, - 'price' => $request->price, - 'term' => $request->payment_term, - 'as_usd' => $as_usd, - 'usd_per_month' => $pricing->costAsPerMonth($as_usd, $request->payment_term), - 'next_due_date' => $request->next_due_date, - ]); + $pricing->insertPricing(4, $domain_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Domains::create([ 'id' => $domain_id, diff --git a/app/Http/Controllers/ResellerController.php b/app/Http/Controllers/ResellerController.php index 60f1b9a..190cf08 100644 --- a/app/Http/Controllers/ResellerController.php +++ b/app/Http/Controllers/ResellerController.php @@ -66,16 +66,7 @@ class ResellerController extends Controller $as_usd = $pricing->convertToUSD($request->price, $request->currency); - Pricing::create([ - 'service_id' => $reseller_id, - 'service_type' => 3, - 'currency' => $request->currency, - 'price' => $request->price, - 'term' => $request->payment_term, - 'as_usd' => $as_usd, - 'usd_per_month' => $pricing->costAsPerMonth($as_usd, $request->payment_term), - 'next_due_date' => $request->next_due_date, - ]); + $pricing->insertPricing(3, $reseller_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); if (!is_null($request->dedicated_ip)) { IPs::insertIP($reseller_id, $request->dedicated_ip); diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index 11aacb7..20deb31 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -110,16 +110,7 @@ class ServerController extends Controller $as_usd = $pricing->convertToUSD($request->price, $request->currency); - Pricing::create([ - 'service_id' => $server_id, - 'service_type' => 1, - 'currency' => $request->currency, - 'price' => $request->price, - 'term' => $request->payment_term, - 'as_usd' => $as_usd, - 'usd_per_month' => $pricing->costAsPerMonth($as_usd, $request->payment_term), - 'next_due_date' => $request->next_due_date, - ]); + $pricing->insertPricing(1, $server_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); if (!is_null($request->ip1)) { IPs::insertIP($server_id, $request->ip1); @@ -204,7 +195,7 @@ class ServerController extends Controller ->where('s.id', '=', $server->id) ->get(['s.*', 'p.*']); - return view('servers.edit', compact(['server','ip_addresses'])); + return view('servers.edit', compact(['server', 'ip_addresses'])); } public function update(Request $request, Server $server) diff --git a/app/Http/Controllers/SharedController.php b/app/Http/Controllers/SharedController.php index 4cdb060..92c82e2 100644 --- a/app/Http/Controllers/SharedController.php +++ b/app/Http/Controllers/SharedController.php @@ -64,16 +64,7 @@ class SharedController extends Controller $as_usd = $pricing->convertToUSD($request->price, $request->currency); - Pricing::create([ - 'service_id' => $shared_id, - 'service_type' => 2, - 'currency' => $request->currency, - 'price' => $request->price, - 'term' => $request->payment_term, - 'as_usd' => $as_usd, - 'usd_per_month' => $pricing->costAsPerMonth($as_usd, $request->payment_term), - 'next_due_date' => $request->next_due_date, - ]); + $pricing->insertPricing(2, $shared_id, $request->currency, $request->price, $request->payment_term, $as_usd, $request->next_due_date); Labels::deleteLabelsAssignedTo($shared_id); diff --git a/app/Models/Pricing.php b/app/Models/Pricing.php index 15efbdb..d089654 100644 --- a/app/Models/Pricing.php +++ b/app/Models/Pricing.php @@ -4,7 +4,9 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Session; class Pricing extends Model { @@ -75,4 +77,27 @@ class Pricing extends Model { DB::table('pricings')->where('service_id', '=', $id)->delete(); } + + public function insertPricing(int $type, string $server_id, string $currency, float $price, int $term, float $as_usd, string $next_due_date, int $is_active = 1) + { + return self::create([ + 'service_type' => $type, + 'service_id' => $server_id, + 'currency' => $currency, + 'price' => $price, + 'term' => $term, + 'as_usd' => $as_usd, + 'usd_per_month' => $this->costAsPerMonth($as_usd, $term), + 'next_due_date' => $next_due_date, + 'active' => ($is_active) ? 1 : 0 + ]); + } + + public static function allPricing() + { + return Cache::remember('all_pricing', now()->addWeek(1), function () { + return DB::table('pricings') + ->get(); + }); + } }