diff --git a/app/Http/Controllers/Customer/PoinExchangeController.php b/app/Http/Controllers/Customer/PoinExchangeController.php
index 79f666f..bcc06b3 100644
--- a/app/Http/Controllers/Customer/PoinExchangeController.php
+++ b/app/Http/Controllers/Customer/PoinExchangeController.php
@@ -5,8 +5,10 @@ namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\Location;
+use App\Models\LocationProfile;
use App\Models\Sale;
use App\Models\Voucher;
+use App\Services\GeneralService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -14,53 +16,55 @@ class PoinExchangeController extends Controller
{
public function index(Request $request)
{
- $favorite = $request->favorite ?? 1;
$customer = $request->user('customer');
+
$flocations = $customer->locationFavorites;
$slocations = [];
+
+ $favorite = $request->favorite ?? 0;
+ if ($request->favorite == '') {
+ $favorite = $customer->locationFavorites->count() > 0 ? 1 : 0;
+ }
+
$locations = Location::orderBy('name', 'asc')->get();
- $vouchers = Voucher::with(['locationProfile.location'])
- ->whereHas('locationProfile', function ($q) {
- $q->where('price_poin', '!=', 0)
- ->orWhereHas('prices', function ($q) {
- return $q->where('price_poin', '!=', 0);
- });
+ $profiles = LocationProfile::with(['location'])
+ ->whereHas('vouchers', function ($q) {
+ $q->where('is_sold', Voucher::UNSOLD);
+ })
+ ->where(function ($q) {
+ $q->where('price_poin', '!=', 0)->orWhereHas('prices', function ($q) {
+ return $q->where('price_poin', '!=', 0);
+ });
})
- ->where('is_sold', Voucher::UNSOLD)
- ->groupBy('location_profile_id')
->orderBy('updated_at', 'desc');
- if ($request->location_ids != '') {
- $vouchers->whereHas('locationProfile', function ($q) use ($request) {
- return $q->whereIn('location_id', $request->location_ids);
- });
-
- $slocations = Location::whereIn('id', $request->location_ids)->get();
+ if ($favorite == 0) {
+ if ($request->location_ids != '') {
+ $profiles->whereIn('location_id', $request->location_ids);
+ $profiles = $profiles->paginate(20);
- $vouchers = tap($vouchers->paginate(20))->setHidden(['username', 'password']);
+ $slocations = Location::whereIn('id', $request->location_ids)->get();
+ }
}
- if ($request->location_ids == '' && $flocations->count() > 0) {
- $favorite = 1;
- $vouchers->whereHas('locationProfile', function ($q) use ($flocations) {
- return $q->whereIn('location_id', $flocations->pluck('id')->toArray());
- });
- $vouchers = tap($vouchers->paginate(20))->setHidden(['username', 'password']);
+ if ($favorite == 1) {
+ $profiles->whereIn('location_id', $flocations->pluck('id')->toArray());
+ $profiles = $profiles->paginate(20);
}
- return inertia('Poin/Exchange', [
+ return inertia('PoinExchange/Index', [
'locations' => $locations,
- 'vouchers' => $vouchers,
+ 'profiles' => $profiles,
'_slocations' => $slocations,
'_flocations' => $flocations,
'_favorite' => $favorite
]);
}
- public function exchange(Voucher $voucher)
+ public function exchange(LocationProfile $profile)
{
- $batchCount = $voucher->count_unsold();
+ $batchCount = $profile->count_unsold();
if ($batchCount < 1) {
return redirect()->route('customer.poin.exchange')
->with('message', ['type' => 'error', 'message' => 'transaksi gagal, voucher sedang tidak tersedia']);
@@ -68,43 +72,45 @@ class PoinExchangeController extends Controller
$customer = Customer::find(auth()->id());
- if ($customer->poin_balance < $voucher->price_poin) {
+ if ($customer->poin_balance < $profile->validate_price_poin) {
return redirect()->route('customer.poin.exchange')
->with('message', ['type' => 'error', 'message' => 'koin kamu tidak cukup untuk ditukar voucher ini']);
}
DB::beginTransaction();
$sale = $customer->sales()->create([
- 'code' => 'Tukar poin ' . str()->upper(str()->random(5)), //TODO changes this
+ 'code' => GeneralService::generateExchangePoinCode(),
'date_time' => now(),
- 'amount' => 0,
+ 'amount' => $profile->validate_price_poin,
'payed_with' => Sale::PAYED_WITH_POIN,
]);
- $voucher = $voucher->shuffle_unsold(1);
- $sale->items()->create([
- 'entity_type' => $voucher::class,
- 'entity_id' => $voucher->id,
- 'price' => $voucher->price_poin,
- 'quantity' => 1,
- 'additional_info_json' => json_encode([
- 'id' => $voucher->id,
+ $vouchers = $profile->shuffle_unsold(1);
+ foreach ($vouchers as $voucher) {
+ $sale->items()->create([
+ 'entity_type' => $voucher::class,
+ 'entity_id' => $voucher->id,
+ 'price' => $voucher->validate_price_poin,
'quantity' => 1,
- 'voucher' => $voucher->load(['location']),
- ]),
- ]);
+ 'additional_info_json' => json_encode([
+ 'voucher' => $voucher->load(['locationProfile.location'])
+ ]),
+ ]);
- $voucher->update(['is_sold' => Voucher::SOLD]);
- $voucher->check_stock_notification();
+ $voucher->update(['is_sold' => Voucher::SOLD]);
+ $voucher->check_stock_notification();
- $sale->create_notification();
+ $poin = $customer->poins()->create([
+ 'credit' => $voucher->validate_price_poin,
+ 'description' => $sale->code,
+ 'narration' => 'Penukaran Voucher Poin'
+ ]);
- $poin = $customer->poins()->create([
- 'credit' => $voucher->price_poin,
- 'description' => $sale->code,
- ]);
+ $poin->update_customer_balance();
+ }
+
+ $sale->create_notification();
- $poin->update_customer_balance();
DB::commit();
return redirect()->route('transactions.sale.show', $sale)
diff --git a/app/Models/LocationProfile.php b/app/Models/LocationProfile.php
index 9648061..c56637c 100644
--- a/app/Models/LocationProfile.php
+++ b/app/Models/LocationProfile.php
@@ -42,6 +42,8 @@ class LocationProfile extends Model
'validate_price',
'validate_display_price',
'validate_discount',
+ 'validate_price_poin',
+ 'validate_bonus_poin',
];
protected static function booted(): void
@@ -158,6 +160,38 @@ class LocationProfile extends Model
});
}
+ public function validateBonusPoin(): Attribute
+ {
+ return Attribute::make(get: function () {
+ if ($this->prices->count() > 0) {
+ $price = $this->prices;
+ if (auth()->guard('customer')->check()) {
+ $customer = self::getInstance()['customer'];
+ return $price->where('customer_level_id', $customer->customer_level_id)
+ ->value('bonus_poin');
+ }
+ return $price->max('bonus_poin');
+ }
+ return $this->bonus_poin;
+ });
+ }
+
+ public function validatePricePoin(): Attribute
+ {
+ return Attribute::make(get: function () {
+ if ($this->prices->count() > 0) {
+ $price = $this->prices;
+ if (auth()->guard('customer')->check()) {
+ $customer = self::getInstance()['customer'];
+ return $price->where('customer_level_id', $customer->customer_level_id)
+ ->value('price_poin');
+ }
+ return $price->max('price_poin');
+ }
+ return $this->price_poin;
+ });
+ }
+
public function shuffle_unsold($limit)
{
$vouchers = Voucher::where([
diff --git a/app/Services/GeneralService.php b/app/Services/GeneralService.php
index d322eee..136cd1a 100644
--- a/app/Services/GeneralService.php
+++ b/app/Services/GeneralService.php
@@ -160,7 +160,9 @@ class GeneralService
public static function generateSaleVoucherCode()
{
- $code = Sale::whereDate('created_at', now())->count() + 1;
+ $code = Sale::whereDate('created_at', '=', now())
+ ->where('payed_with', '!=', Sale::PAYED_WITH_POIN)
+ ->count() + 1;
return 'Invoice #VCR' . now()->format('dmy') . GeneralService::formatNumberCode($code);
}
@@ -172,6 +174,15 @@ class GeneralService
return 'Invoice #BPN' . now()->format('dmy') . GeneralService::formatNumberCode($code);
}
+ public static function generateExchangePoinCode()
+ {
+ $code = Sale::whereDate('created_at', '=', now())
+ ->where('payed_with', '=', Sale::PAYED_WITH_POIN)
+ ->count() + 1;
+
+ return 'Invoice #PVC' . now()->format('dmy') . GeneralService::formatNumberCode($code);
+ }
+
public static function formatNumberCode($number)
{
if ($number < 10) {
@@ -185,12 +196,12 @@ class GeneralService
}
return $number;
}
-
+
public static function isAllowAffilate($key)
{
$isAllow = false;
$levels = json_decode(Setting::getByKey('AFFILATE_ALLOWED_LEVELS'));
- foreach($levels as $level) {
+ foreach ($levels as $level) {
if ($key == $level->key) {
$isAllow = true;
}
diff --git a/resources/js/Customer/Poin/Index.jsx b/resources/js/Customer/Poin/Index.jsx
index 4382e03..669d572 100644
--- a/resources/js/Customer/Poin/Index.jsx
+++ b/resources/js/Customer/Poin/Index.jsx
@@ -77,7 +77,7 @@ export default function Index(props) {