You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.6 KiB
PHTML

<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\LocationProfile;
use App\Models\Sale;
use App\Models\Voucher;
1 year ago
use App\Services\GeneralService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
1 year ago
use Illuminate\Validation\Rule;
class CartController extends Controller
{
/**
* show list of item in cart
* has payed button
* show payment method -> deposit, poin, paylater
*/
public function index(Request $request)
{
$customer = $request->user('customer');
$carts = $customer->carts->load(['locationProfile.location']);
$total = $carts->sum(function ($item) {
return $item->quantity * $item->locationProfile->validate_price;
});
1 year ago
$checkAllowProcess = [
$customer->deposit_balance >= $total,
1 year ago
$customer->paylater_remain >= $total,
1 year ago
];
$allowProcess = in_array(true, $checkAllowProcess);
1 year ago
return inertia('Cart/Index', [
'carts' => $carts,
'total' => $total,
1 year ago
'allow_process' => $allowProcess,
1 year ago
'payments' => GeneralService::getCartEnablePayment($customer, $total),
]);
}
/**
* handle cart add, remove or sub
*/
public function store(Request $request, LocationProfile $profile)
{
$operator = $request->param ?? 'add'; //delete, sub, add
$customer = $request->user('customer');
1 year ago
if (! $customer->allow_transaction) {
1 year ago
$customer->carts()->delete();
1 year ago
1 year ago
return redirect()->back()
1 year ago
->with('message', ['type' => 'error', 'message' => 'akun anda dibekukan tidak dapat melakukan transaksi']);
1 year ago
}
$item = $customer->carts()->where(['entity_id' => $profile->id])->first();
if ($item !== null) {
if ($operator == 'delete') {
$item->delete();
session()->flash('message', ['type' => 'success', 'message' => 'voucher dihapus dari keranjang', 'cart' => 1]);
}
if ($operator == 'add') {
// bisa tambah filter stock vouchernya
$item->update([
1 year ago
'quantity' => $item->quantity + 1,
]);
}
if ($operator == 'sub') {
if ($item->quantity - 1 != 0) {
$item->update([
1 year ago
'quantity' => $item->quantity - 1,
]);
}
}
} else {
$customer->carts()->create([
'entity_id' => $profile->id,
1 year ago
'quantity' => 1,
]);
session()->flash('message', ['type' => 'success', 'message' => 'voucher ditambahkan ke keranjang', 'cart' => 1]);
}
if ($request->direct != '') {
return redirect()->route('cart.index');
}
}
/**
* find correct voucher , reject if cant be found
* create sale and item sale
* credit deposit
* redirect to show detail
*/
1 year ago
public function purchase(Request $request)
{
1 year ago
$request->validate([
'payed_with' => [
'required',
Rule::in([Sale::PAYED_WITH_DEPOSIT, Sale::PAYED_WITH_PAYLATER]),
],
]);
$customer = $request->user('customer');
$carts = $customer->carts->load(['locationProfile.location']);
1 year ago
// validate carts not empty
if ($carts->count() == 0) {
return redirect()->route('home.index')
->with('message', ['type' => 'error', 'message' => 'transaksi gagal, keranjang anda kosong']);
}
// validate voucher is available
foreach ($carts as $item) {
$batchCount = $item->locationProfile->count_unsold();
1 year ago
if ($batchCount < $item->quantity) {
$customer->carts()->delete();
return redirect()->route('home.index')
->with('message', ['type' => 'error', 'message' => 'transaksi gagal, voucher sedang tidak tersedia']);
}
}
1 year ago
// calculate total
$total = $carts->sum(function ($item) {
return $item->quantity * $item->locationProfile->validate_price;
});
DB::beginTransaction();
1 year ago
// create sale
$sale = $customer->sales()->create([
'date_time' => now(),
'amount' => $total,
1 year ago
'payed_with' => $request->payed_with,
]);
1 year ago
// create sale item by per voucher
foreach ($carts as $item) {
$vouchers = $item->locationProfile->shuffle_unsold($item->quantity);
1 year ago
foreach ($vouchers as $voucher) {
$sale->items()->create([
'entity_type' => $voucher::class,
'entity_id' => $voucher->id,
'price' => $voucher->validate_price,
'quantity' => 1,
'additional_info_json' => json_encode([
1 year ago
'voucher' => $voucher->load(['locationProfile.location']),
]),
]);
$voucher->update(['is_sold' => Voucher::SOLD]);
$voucher->check_stock_notification();
$voucher->create_bonus_poin($customer);
}
}
1 year ago
$sale->create_payment();
$sale->create_notification();
$sale->create_poin_reward();
$sale->create_poin_affilate();
1 year ago
1 year ago
// remove carts
$customer->carts()->delete();
DB::commit();
return redirect()->route('transactions.sale.show', $sale)
->with('message', ['type' => 'success', 'message' => 'pembelian berhasil']);
}
}