Viewing file: PosController.php (7.35 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Item; use App\Models\Order; use App\Models\OrderDetails; use App\Models\Restaurant; use App\Models\User; use App\Models\UserPlan; use Illuminate\Http\Request;
class PosController extends Controller { public function index() { $auth = auth()->user(); if ($auth->type == 'user') { $userplan = UserPlan::where('user_id',$auth->restaurant_owner_id)->first(); if(isset($userplan) && $userplan->pos_system=='no'){ return abort('404'); } $data['restaurents'] = Restaurant::where('id', $auth->restaurant_id)->select('id', 'name')->get(); }else{ $currentPlan=isset($auth->current_plans[0])?$auth->current_plans[0]:''; if($currentPlan->pos_system=='no'){ return abort('404'); } $data['restaurents'] = auth()->user()->restaurants()->where('status', 'active')->select('id', 'name')->get(); } $data['customers'] = User::where('type', 'customer') ->where('status', 'approved') ->select('id', 'name')->get(); return view('pos.index', $data); }
public function get_items(Request $request){ $all_items = []; $auth = auth()->user();
if($auth->type == 'user'){ $items = Item:: where('restaurant_id', $auth->restaurant_id) ->when($request->item_name, function ($query, $item_name) { $keywords = explode(' ', $item_name); foreach ($keywords as $keyword) { $query->where('name', 'like', '%' . $keyword . '%'); } return $query; }) ->get();
}else{ $items = $auth->items() ->where('restaurant_id', $request->restaurant_id) ->when($request->item_name, function ($query, $item_name) { $keywords = explode(' ', $item_name); foreach ($keywords as $keyword) { $query->where('name', 'like', '%' . $keyword . '%'); } return $query; }) ->get(); } foreach ($items as $item) { $all_items[] = [ 'id' => $item->id, 'name' => $item->name, 'image' => $item->image, 'price' => $item->price, 'discount' => $item->discount, 'discount_type' => $item->discount_type, 'tax_amount' => $item->tax->amount, 'tax_type' => $item->tax->type, 'currency_symbol' => $item->restaurant->currency_symbol ]; } return response()->json(['data'=>$all_items,'status'=>'success']);
}
public function store(Request $request){ $request->validate([ 'items.*' => 'required', 'restaurant_id' => 'required', 'customer_id' => 'required', ]);
$restaurant = Restaurant::find($request->restaurant_id);
if (!$restaurant) return response()->json(['message' => 'Restaurant Not Found','status'=>'failed']);
$orderStatus = json_decode(get_settings('manage_place_order')); if (isset($orderStatus->admin_order_status) && $orderStatus->admin_order_status == 'disable' && isset($restaurant->order_status) && $restaurant->order_status == 'disable') { return response()->json(['message' => trans('You can not place order right now, please try again later'),'status'=>'failed']); } $auth = auth()->user(); $order = new Order(); $order->user_id = $auth ? $auth->id : null; $order->restaurant_id = $request->restaurant_id; $order->name = ($request->customer_id == 'walk_in_customer') ? 'walk_in_customer' : $request->customer_id; $order->payment_status = 'paid'; $order->type = 'pos'; $order->status = 'approved'; $order->order_number = substr(time(), -6) . rand(10, 99); $order->save();
$totalPrice = 0; $totalTax = 0; $orderDetailsData = []; $allPrice = 0; $total_price = 0; $total_discount = 0; $i = 0; foreach ($request['items'] as $key => $req_item) { $orderQuantity = $req_item['item_quantity']; $item = Item::where(['id' => $req_item['item_id'], 'restaurant_id' => $request->restaurant_id])->first(); $price = $item->price; $discountPrice = 0; if ($item) { if ($item->discount > 0) { if ($item->discount_type == 'flat') { $discountPrice = $item->discount; $price = $item->price - $discountPrice; } elseif ($item->discount_type == 'percent') { $discountPrice = ($item->price * $item->discount) / 100; $price = $item->price - $discountPrice; } } else { $price = $item->price; } $taxAmount = 0; if ($item->tax && $item->tax->type) {
$taxAmount = $item->tax->amount; if ($item->tax->type == 'percentage') { $taxAmount = ($taxAmount * $item->price) / 100; } } // $totalTax += $taxAmount * $orderQuantity; $totalTax = $taxAmount * $orderQuantity; $total_price = $item->price * $orderQuantity; $total_discount = $discountPrice * $orderQuantity; $allPrice = $total_price - $total_discount + $totalTax;
$orderDetailsData[$i]['order_id'] = $order->id; $orderDetailsData[$i]['item_id'] = $item->id; $orderDetailsData[$i]['price'] = $item->price; $orderDetailsData[$i]['quantity'] = $orderQuantity; $orderDetailsData[$i]['discount'] = $total_discount; $orderDetailsData[$i]['total'] = $allPrice; $orderDetailsData[$i]['tax_amount'] = $totalTax; $orderDetailsData[$i]['status'] = 'approved'; $orderDetailsData[$i]['created_at'] = now(); $orderDetailsData[$i]['updated_at'] = now(); $totalPrice += $allPrice; $i++; } } OrderDetails::insert($orderDetailsData); if($request->discount && $request->discount > 0){ $order->total_price = $totalPrice-$request->discount; $order->pos_discount = $request->discount; }else{ $order->total_price = $totalPrice; } $order->save(); return response()->json(['message' => 'A new order has been placed','status'=>'success','orderId'=>$order->id]);
}
public function details(Request $request){
$order = Order::with(['restaurant', 'user'])->find($request->orderId);
if (!$order) { return response()->json(['status' => 'error', 'message' => 'Order not found'], 404); }
$orderDetails = OrderDetails::with('item')->where('order_id', $order->id)->get();
return response()->json([ 'status' => 'success', 'restaurant' => $order->restaurant, 'order' => $order, 'item' => $orderDetails, ]); } }
|