store 4
This commit is contained in:
@@ -17,6 +17,7 @@ use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
use App\Models\Price;
|
||||
use App\Http\Controllers\Admin\ProductController;
|
||||
use App\Http\Controllers\Admin\GiftCardController;
|
||||
use App\Services\ConfigService;
|
||||
|
||||
|
||||
|
||||
@@ -172,11 +173,9 @@ class CartController extends Controller
|
||||
->keyBy('id');
|
||||
|
||||
$numbers = collect($data)
|
||||
->mapWithKeys(function ($item) {
|
||||
return [
|
||||
$item['product_id'] => $item['number'] ?? 1
|
||||
];
|
||||
});
|
||||
->filter(fn ($item) => is_array($item) && isset($item['product_id']))
|
||||
->groupBy('product_id')
|
||||
->map(fn ($group) => $group->sum(fn ($item) => (int) ($item['number'] ?? 1)));
|
||||
foreach ($products as $product) {
|
||||
if(($product->inventory - $numbers[$product->id]) < 0){
|
||||
return response()->json(['message' => "موجودی محصول {$product->title} کافی نیست!"], 422);
|
||||
@@ -202,6 +201,14 @@ class CartController extends Controller
|
||||
$wallet = Wallet::where('owner',auth('customer')->id())->whereNull('fund_id')->whereNull('product_id')
|
||||
->first();
|
||||
|
||||
if (!$wallet) {
|
||||
return response()->json(['message' => 'کیف پول یافت نشد!'], 404);
|
||||
}
|
||||
|
||||
if (app(ConfigService::class)->isStore()) {
|
||||
return $this->checkoutStore($data, $cart, $wallet);
|
||||
}
|
||||
|
||||
$priceRow = Price::latest()->first();
|
||||
$goldPrice = $priceRow->price;
|
||||
$data['price'] = $goldPrice;
|
||||
@@ -325,4 +332,138 @@ class CartController extends Controller
|
||||
'message' => 'true',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* تسویه سبد فروشگاهی — قیمت نقدی هر محصول، بدون فرمول آبشده/گرم.
|
||||
*/
|
||||
private function checkoutStore(array $payload, Cart $cart, $wallet)
|
||||
{
|
||||
$customerId = (int) auth('customer')->id();
|
||||
$priceRow = Price::latest()->first();
|
||||
$goldPrice = $priceRow ? (float) $priceRow->price : 0;
|
||||
|
||||
$requestLines = collect($payload)
|
||||
->filter(fn ($row) => is_array($row) && isset($row['product_id']))
|
||||
->groupBy('product_id')
|
||||
->map(fn ($group) => $group->last());
|
||||
|
||||
$wallet_interface = new WalletInterface($customerId, auth('customer')->user()->type);
|
||||
|
||||
$total_price = 0;
|
||||
$total_amount = 0;
|
||||
$total_fee = 0;
|
||||
|
||||
foreach ($cart->items as $item) {
|
||||
$product = Product::find($item->product_id);
|
||||
if (!$product) {
|
||||
throw new UnprocessableEntityHttpException('محصول یافت نشد.');
|
||||
}
|
||||
if (!$product->open_buy) {
|
||||
throw new UnprocessableEntityHttpException("خرید {$product->title} در این لحظه مقدور نیست.");
|
||||
}
|
||||
|
||||
$line = $requestLines->get($item->product_id);
|
||||
$qty = (int) $item->quantity;
|
||||
|
||||
$unitPrice = (float) ($line['price'] ?? 0);
|
||||
if ($unitPrice <= 0) {
|
||||
$unitPrice = (float) (($product->price ?? 0) + ($product->price_profit ?? 0));
|
||||
}
|
||||
if ($unitPrice <= 0 && (float) $product->amount > 0 && $goldPrice > 0) {
|
||||
$unitPrice = (float) $product->amount * $goldPrice;
|
||||
}
|
||||
if ($unitPrice <= 0) {
|
||||
throw new UnprocessableEntityHttpException("قیمت محصول {$product->title} مشخص نیست.");
|
||||
}
|
||||
|
||||
$cashFee = (float) ($product->cash_fee_buy ?? 0) * $qty;
|
||||
$lineTotal = ($unitPrice * $qty) + $cashFee;
|
||||
|
||||
$total_price += $lineTotal;
|
||||
$total_amount += (float) $product->amount * $qty;
|
||||
$total_fee += $cashFee;
|
||||
|
||||
$item->update([
|
||||
'price' => $lineTotal,
|
||||
'gold_price' => $goldPrice,
|
||||
]);
|
||||
}
|
||||
|
||||
$orderData = [
|
||||
'customer_id' => $customerId,
|
||||
'price_id' => $priceRow?->id,
|
||||
'price' => $total_amount > 0 ? (int) round($total_price / $total_amount) : 0,
|
||||
'price_original' => $goldPrice,
|
||||
'amount' => $total_amount,
|
||||
'service_charge_percentage' => 0,
|
||||
'service_charge' => 0,
|
||||
'service_charge_price' => 0,
|
||||
'fee' => $total_fee,
|
||||
'total_fee' => $total_fee,
|
||||
'total_price_original' => $total_amount * $goldPrice,
|
||||
'total_price' => $total_price,
|
||||
'payment_amount' => $total_price,
|
||||
'sts' => 1,
|
||||
'direction' => 1,
|
||||
'pay_at' => now(),
|
||||
];
|
||||
|
||||
if (!empty($payload[0]['discount_code'])) {
|
||||
$discount = new GiftCardController();
|
||||
$discount = $discount->useDiscount($payload[0]['discount_code'], $customerId);
|
||||
if (!empty($discount['amount'])) {
|
||||
$orderData['payment_amount'] = $orderData['total_price'] =
|
||||
$orderData['total_price'] - ($orderData['total_price'] * (float) $discount['amount']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($wallet->cash < $orderData['total_price']) {
|
||||
throw new UnprocessableEntityHttpException('موجودی کیف پول شما کافی نیست.');
|
||||
}
|
||||
|
||||
$order = Order::create($orderData);
|
||||
$cart->order_id = $order->id;
|
||||
$cart->save();
|
||||
|
||||
new TransactionInterface($wallet_interface, [
|
||||
'cash' => $orderData['payment_amount'],
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'order_id' => $order->id,
|
||||
'start_at' => now(),
|
||||
'title' => 'سبد خرید',
|
||||
], 12, false, false);
|
||||
|
||||
foreach ($cart->items as $item) {
|
||||
$wallet_interface_product = new WalletInterface(
|
||||
$customerId,
|
||||
auth('customer')->user()->type,
|
||||
null,
|
||||
$item->product_id
|
||||
);
|
||||
$wallet_interface_product->wallet->gold = (int) $wallet_interface_product->wallet->gold;
|
||||
$wallet_interface_product->wallet->save();
|
||||
|
||||
new TransactionInterface($wallet_interface_product, [
|
||||
'gold' => $item->quantity,
|
||||
'product_id' => $item->product_id,
|
||||
'expire_at' => now(),
|
||||
'active' => true,
|
||||
'order_id' => $order->id,
|
||||
'start_at' => now(),
|
||||
], 1, true, false);
|
||||
}
|
||||
|
||||
$order->sts = 2;
|
||||
$order->payment_sts = 4;
|
||||
$order->save();
|
||||
|
||||
foreach ($cart->items as $item) {
|
||||
Product::where('id', $item->product_id)->decrement('inventory', $item->quantity);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'true',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user