Server : Apache System : Linux host44.registrar-servers.com 4.18.0-513.18.1.lve.2.el8.x86_64 #1 SMP Sat Mar 30 15:36:11 UTC 2024 x86_64 User : vapecompany ( 2719) PHP Version : 7.4.33 Disable Function : NONE Directory : /home/vapecompany/demo.vapecompany.com.bd/app/Http/Controllers/Frontend/ |
Upload File : |
<?php namespace App\Http\Controllers\Frontend; use App\Http\Controllers\Controller; use App\Models\Barcode; use App\Models\Chalan; use App\Models\ChalanDetails; use App\Models\Customer; use App\Models\Emplyoee; use App\Models\MCode; use App\Models\Notification; use App\Models\Product; use Carbon\Carbon; use Gloudemans\Shoppingcart\Facades\Cart; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CheckOutController extends Controller { public function index() { $cart = Cart::content(); $cartQty = Cart::count(); if ($cartQty < 1) { return redirect()->route('cart'); } $cartTotal = Cart::total(); return view('frontend.pages.checkout', compact('cart', 'cartQty', 'cartTotal')); } public function store(Request $request) { $code = MCode::where('chalan_time', date("Y") . date('m'))->first(); $chalan_n = 1; if ($code) { $chalan_n = str_pad($code->chanlan_code, 4, 0, STR_PAD_LEFT); } else { $chalan_n = str_pad($chalan_n, 4, 0, STR_PAD_LEFT); } try { DB::beginTransaction(); $total_price = 0; $total_quantity = 0; $requisition_date = date("dmY"); foreach ($request->product_id as $k => $v) { $product = Product::where('product_id', $v)->first(); $total_quantity += $request->qty[$k]; $total_price += ($product->sale_price * $request->qty[$k]); } $latestChalanByInvoiceNumber = Chalan::orderBy('invoice_number')->latest()->take(1)->first(); $customer = Customer::where('user_id', auth()->id())->first(); $chalan = new Chalan; $chalan->invoice_number = $latestChalanByInvoiceNumber->invoice_number; $chalan->customer_id = $customer->customer_id; $chalan->chalan_number = date("Y") . date('m') . $chalan_n; $chalan->chalan_date = Carbon::create($requisition_date[2] . '-' . $requisition_date[1] . '-' . $requisition_date[0], 'Asia/Dhaka'); $chalan->remarks = $request->remarks ? $request->remarks : 'Order From Website'; $chalan->delivery_mode = 2; $chalan->chalan_status = 1; $chalan->total_quantity = $total_quantity; $chalan->total_amount = $total_price; $chalan->sales_user_id = 1; $chalan->courier_id = 1; $chalan->save(); $uid = $chalan->chalan_id; $i = 0; $iemis = json_decode($request->barcode, true); foreach ($request->product_id as $k => $v) { $product = Product::where('product_id', $request->product_id[$i])->first(); $details = new ChalanDetails; $details->chalan_id = $uid; $details->product_id = $v; $details->chalan_date = Carbon::create($requisition_date[2] . '-' . $requisition_date[1] . '-' . $requisition_date[0], 'Asia/Dhaka'); $details->product_quantity = $request->qty[$i]; $details->product_id = $request->product_id[$i]; $details->product_name = $product->product_name; $details->product_unit_price = $product->sale_price; $details->product_price = $request->qty[$i] * $product->sale_price; $details->save(); $ch_details = $details->chalan_details_id; $barcode = Barcode::where('product_id', $request->product_id[$i])->first(); if (!empty($barcode)) { if ($barcode->quantity < $request->qty[$i]) { DB::rollBack(); $msg = $product->product_name . ' Quantity is higher than stock'; return redirect()->route('home')->with('error', $msg); } } else { return redirect()->route('home')->with('error', 'Product barcode not found'); } $barcode->quantity = $barcode->quantity - $request->qty[$i]; $barcode->save(); $i++; } if ($code) { $code->chanlan_code = intval($code->chanlan_code) + 1; $code->save(); } else { $code = MCode::first(); $code->chalan_time = date("Y") . date('m'); $code->chanlan_code = 2; $code->save(); } $employees = Emplyoee::where('user_type', 2)->orwhere('user_type', 1)->get(); foreach ($employees as $employee) { $notification = new Notification; $notification->message = "New Chalan is Created"; $notification->notefication_date = Carbon::now('Asia/dhaka'); $notification->target_url = $uid; $notification->type = 2; $notification->r_status = 1; $notification->user_id = $employee->user_id; $notification->department_id = $employee->department_id; $notification->save(); } DB::commit(); Cart::destroy(); return redirect()->route('home')->with('success', 'Successfully done'); } catch (\PDOException $e) { // Woopsy DB::rollBack(); return redirect()->route('home')->with('error', 'Something went wrong, please try again later!'); } return back(); } }