From f1ed0f880893b4cd3e104866d3adc8518cab1dff Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Fri, 30 Jun 2023 13:32:56 +0700 Subject: [PATCH] bit fix paylater use --- .../Controllers/Admin/PaylaterController.php | 10 +++++++++- app/Models/PaylaterCustomer.php | 19 +++++++++++++++++++ app/Models/PaylaterHistory.php | 7 ++++++- resources/js/Customer/Paylater/Index.jsx | 4 +--- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Admin/PaylaterController.php b/app/Http/Controllers/Admin/PaylaterController.php index ab10828..9012568 100644 --- a/app/Http/Controllers/Admin/PaylaterController.php +++ b/app/Http/Controllers/Admin/PaylaterController.php @@ -7,6 +7,7 @@ use App\Models\Customer; use App\Models\PaylaterHistory; use App\Services\GeneralService; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; class PaylaterController extends Controller @@ -79,8 +80,15 @@ class PaylaterController extends Controller DB::beginTransaction(); $customer = Customer::find($request->customer_id); - $customer->paylater->update([ + $paylater = $customer->paylater; + if ($paylater->day_deadline_at != null) { + $paylater->day_deadline_at = Carbon::parse($paylater->day_deadline_at) + ->addDays($request->day_deadline - $paylater->day_deadline); + } + + $paylater->update([ 'day_deadline' => $request->day_deadline, + 'day_deadline_at' => $paylater->day_deadline_at, ]); $customer->partner()->updateOrCreate([ diff --git a/app/Models/PaylaterCustomer.php b/app/Models/PaylaterCustomer.php index 938c6d1..d4aec2c 100644 --- a/app/Models/PaylaterCustomer.php +++ b/app/Models/PaylaterCustomer.php @@ -2,6 +2,9 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Support\Carbon; + class PaylaterCustomer extends Model { protected $fillable = [ @@ -13,8 +16,24 @@ class PaylaterCustomer extends Model 'day_deadline_at', ]; + protected $appends = [ + 'paylater_description' + ]; + public function customer() { return $this->hasOne(Customer::class, 'customer_id', 'id'); } + + public function paylaterDescription(): Attribute + { + return Attribute::make(get: function () { + if ($this->day_deadline_at != null) { + $deadlineAt = Carbon::parse($this->day_deadline_at)->translatedFormat('d F Y'); + return "lunasi pinjaman kamu sebelum jatuh tempo pada {$deadlineAt}"; + } + + return "yuk gunakan terus saldo yang tersedia"; + }); + } } diff --git a/app/Models/PaylaterHistory.php b/app/Models/PaylaterHistory.php index 8406840..105c76a 100644 --- a/app/Models/PaylaterHistory.php +++ b/app/Models/PaylaterHistory.php @@ -50,9 +50,14 @@ class PaylaterHistory extends Model { $customer = Customer::find($this->customer_id); $paylater = $customer->paylater; + + if ($paylater->day_deadline_at == null) { + $paylater->day_deadline_at = now()->addDays($paylater->day_deadline); + } + $paylater->update([ 'usage' => $paylater->usage + $this->debit - $this->credit, - // TODO: add day dateline + 'day_deadline_at' => $paylater->day_deadline_at, ]); } diff --git a/resources/js/Customer/Paylater/Index.jsx b/resources/js/Customer/Paylater/Index.jsx index f18ac38..7b6afd4 100644 --- a/resources/js/Customer/Paylater/Index.jsx +++ b/resources/js/Customer/Paylater/Index.jsx @@ -63,9 +63,7 @@ export default function Index({ className="mx-auto px-4 mt-4 py-2 text-xs text-blue-800 rounded-lg bg-blue-50 flex flex-row space-x-2 w-full items-center" role="alert" > -
- lunasi pinjaman kamu sebelum jatuh tempo pada ... -
+
{user.paylater.paylater_description}