dbfdg can('manage pos')) { session()->forget('pos'); $customers = Customer::where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'name'); $customers->prepend('Walk-in-customer', ''); $warehouses = warehouse::select('*', \DB::raw("CONCAT(name) AS name"))->where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $user = Auth::user(); $details = [ 'pos_id' => $user->posNumberFormat($this->invoicePosNumber()), 'customer' => $customers != null ? $customers->toArray() : [], 'user' => $user != null ? $user->toArray() : [], 'date' => date('Y-m-d'), 'pay' => 'show', ]; if ($id != 0) { $quotation = Quotation::find($id); $customerId = $quotation->customer_id; $customerId = Customer::find($customerId); $customer = $customerId->name; $warehouseId = $quotation->warehouse_id; $quotationProduct = QuotationProduct::where('quotation_id', $id)->get(); foreach ($quotationProduct as $value) { $products = Quotation::quotationProduct($value); } } else { $customer = ''; $warehouseId = ''; } return view('pos.index', compact('customers', 'warehouses', 'details', 'customer', 'warehouseId', 'id')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { $sess = session()->get('pos'); if (Auth::user()->can('manage pos') && isset($sess) && !empty($sess) && count($sess) > 0) { $user = Auth::user(); $settings = Utility::settings(); $customer = Customer::where('name', '=', $request->vc_name)->where('created_by', $user->creatorId())->first(); $warehouse = warehouse::where('id', '=', $request->warehouse_name)->where('created_by', $user->creatorId())->first(); $details = [ 'pos_id' => $user->posNumberFormat($this->invoicePosNumber()), 'customer' => $customer != null ? $customer->toArray() : [], 'warehouse' => $warehouse != null ? $warehouse->toArray() : [], 'user' => $user != null ? $user->toArray() : [], 'date' => date('Y-m-d'), 'pay' => 'show', ]; if (!empty($details['customer'])) { $warehousedetails = '' . ucfirst($details['warehouse']['name']) . '

'; $details['customer']['billing_state'] = $details['customer']['billing_state'] != '' ? ", " . $details['customer']['billing_state'] : ''; $details['customer']['shipping_state'] = $details['customer']['shipping_state'] != '' ? ", " . $details['customer']['shipping_state'] : ''; $customerdetails = '
' . ucfirst($details['customer']['name']) . '

' . $details['customer']['billing_phone'] . '

' . '

' . $details['customer']['billing_address'] . '

' . '

' . $details['customer']['billing_city'] . $details['customer']['billing_state'] . '

' . '

' . $details['customer']['billing_country'] . '

' . '

' . $details['customer']['billing_zip'] . '

'; $shippdetails = '
' . ucfirst($details['customer']['name']) . '' . '

' . $details['customer']['shipping_phone'] . '

' . '

' . $details['customer']['shipping_address'] . '

' . '

' . $details['customer']['shipping_city'] . $details['customer']['shipping_state'] . '

' . '

' . $details['customer']['shipping_country'] . '

' . '

' . $details['customer']['shipping_zip'] . '

'; } else { $customerdetails = '

' . __('Walk-in Customer') . '

'; $warehousedetails = '' . ucfirst($details['warehouse']['name']) . '

'; $shippdetails = '-'; } $settings['company_telephone'] = $settings['company_telephone'] != '' ? ", " . $settings['company_telephone'] : ''; $settings['company_state'] = $settings['company_state'] != '' ? ", " . $settings['company_state'] : ''; $userdetails = '

' . ucfirst($details['user']['name']) . '

' . '

' . $settings['company_name'] . $settings['company_telephone'] . '

' . '

' . $settings['company_address'] . '

' . '

' . $settings['company_city'] . $settings['company_state'] . '

' . '

' . $settings['company_country'] . '

' . '

' . $settings['company_zipcode'] . '

'; $details['customer']['details'] = $customerdetails; $details['warehouse']['details'] = $warehousedetails; // $details['customer']['shippdetails'] = $shippdetails; $details['user']['details'] = $userdetails; $mainsubtotal = 0; $sales = []; foreach ($sess as $key => $value) { $subtotal = $value['price'] * $value['quantity']; $tax = ($subtotal * $value['tax']) / 100; $sales['data'][$key]['name'] = $value['name']; $sales['data'][$key]['quantity'] = $value['quantity']; $sales['data'][$key]['price'] = Auth::user()->priceFormat($value['price']); $sales['data'][$key]['tax'] = $value['tax'] . '%'; $sales['data'][$key]['product_tax'] = $value['product_tax']; $sales['data'][$key]['tax_amount'] = Auth::user()->priceFormat($tax); $sales['data'][$key]['subtotal'] = Auth::user()->priceFormat($value['subtotal']); $mainsubtotal += $value['subtotal']; } $discount = !empty($request->discount) ? $request->discount : 0; $sales['discount'] = Auth::user()->priceFormat($discount); $total = $mainsubtotal - $discount; $sales['sub_total'] = Auth::user()->priceFormat($mainsubtotal); $sales['total'] = Auth::user()->priceFormat($total); return view('pos.show', compact('sales', 'details')); } else { return response()->json( [ 'error' => __('Add some products to cart!'), ], '404' ); } } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $discount = $request->discount; if (Auth::user()->can('manage pos')) { if($request->quotation_id != 0) { $quotation = Quotation::where('id', $request->quotation_id)->first(); $quotation->is_converted = 1; $quotation->save(); } $user_id = Auth::user()->creatorId(); $customer_id = Customer::customer_id($request->vc_name); $warehouse_id = warehouse::warehouse_id($request->warehouse_name); $pos_id = $this->invoicePosNumber(); $sales = session()->get('pos'); if (isset($sales) && !empty($sales) && count($sales) > 0) { $result = DB::table('pos')->where('pos_id', $pos_id)->where('created_by', $user_id)->get(); if (count($result) > 0) { return response()->json( [ 'code' => 200, 'success' => __('Payment is already completed!'), ] ); } else { $pos = new Pos(); $pos->pos_id = $pos_id; $pos->customer_id = $customer_id; $pos->warehouse_id = $request->warehouse_name; $pos->pos_date = date('Y-m-d'); $pos->created_by = $user_id; $pos->save(); if($request->quotation_id != 0) { $quotation->converted_pos_id = $pos->id; $quotation->save(); } foreach ($sales as $key => $value) { $product_id = $value['id']; $product = ProductService::whereId($product_id)->where('created_by', $user_id)->first(); $original_quantity = ($product == null) ? 0 : (int) $product->quantity; $product_quantity = $original_quantity - $value['quantity']; if ($product != null && !empty($product)) { ProductService::where('id', $product_id)->update(['quantity' => $product_quantity]); } $tax_id = ProductService::tax_id($product_id); $positems = new PosProduct(); $positems->pos_id = $pos->id; $positems->product_id = $product_id; $positems->price = $value['price']; $positems->quantity = $value['quantity']; $positems->tax = $tax_id; $positems->discount = $discount; $positems->save(); Utility::warehouse_quantity('minus', $positems->quantity, $positems->product_id, $request->warehouse_name); //Product Stock Report $type = 'pos'; $type_id = $pos->id; StockReport::where('type', '=', 'pos')->where('type_id', '=', $pos->id)->delete(); $description = $positems->quantity . ' ' . __(' quantity sold in pos') . ' ' . \Auth::user()->posNumberFormat($pos->pos_id); Utility::addProductStock($positems->product_id, $positems->quantity, $type, $description, $type_id); } $posPayment = new PosPayment(); $posPayment->pos_id = $pos->id; $posPayment->date = $request->date; $mainsubtotal = 0; $sales = []; $sess = session()->get('pos'); foreach ($sess as $key => $value) { $subtotal = $value['price'] * $value['quantity']; $tax = ($subtotal * $value['tax']) / 100; $sales['data'][$key]['price'] = Auth::user()->priceFormat($value['price']); $sales['data'][$key]['tax'] = $value['tax'] . '%'; $sales['data'][$key]['tax_amount'] = Auth::user()->priceFormat($tax); $sales['data'][$key]['subtotal'] = Auth::user()->priceFormat($value['subtotal']); $mainsubtotal += $value['subtotal']; } $amount = $mainsubtotal; $posPayment->amount = $amount; $total = $mainsubtotal - $discount; $posPayment->discount = $discount; $posPayment->discount_amount = $total; $posPayment->save(); session()->forget('pos'); return response()->json( [ 'code' => 200, 'success' => __('Payment completed successfully!'), ] ); } } else { return response()->json( [ 'code' => 404, 'success' => __('Items not found!'), ] ); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function show($ids) { if (\Auth::user()->can('show pos') || \Auth::user()->type == 'company') { try { $id = Crypt::decrypt($ids); } catch (\Throwable $th) { return redirect()->back()->with('error', __('Pos Not Found.')); } $id = Crypt::decrypt($ids); $pos = Pos::find($id); if ($pos->created_by == \Auth::user()->creatorId()) { $posPayment = PosPayment::where('pos_id', $pos->id)->first(); $customer = $pos->customer; $iteams = $pos->items; return view('pos.view', compact('pos', 'customer', 'iteams', 'posPayment')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function invoicePosNumber() { if (Auth::user()->can('manage pos')) { $latest = Pos::where('created_by', '=', \Auth::user()->creatorId())->latest()->first(); return $latest ? $latest->pos_id + 1 : 1; } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function report() { if (\Auth::user()->can('manage pos')) { $posPayments = Pos::where('created_by', '=', \Auth::user()->creatorId())->with(['customer', 'warehouse'])->get(); return view('pos.report', compact('posPayments')); } else { return redirect()->back()->with('error', __('Permission Denied.')); } } public function barcode() { if (\Auth::user()->can('manage pos')) { $productServices = ProductService::where('created_by', '=', \Auth::user()->creatorId())->get(); $barcode = [ 'barcodeType' => Auth::user()->barcodeType(), 'barcodeFormat' => Auth::user()->barcodeFormat(), ]; return view('pos.barcode', compact('productServices', 'barcode')); } else { return redirect()->back()->with('error', __('Permission Denied.')); } } public function setting() { if (\Auth::user()->can('manage pos')) { $settings = Utility::settings(); return view('pos.setting', compact('settings')); } else { return redirect()->back()->with('error', 'Permission denied.'); } } 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() { if (\Auth::user()->can('manage pos')) { $warehouses = warehouse::select('*', \DB::raw("CONCAT(name) AS name"))->where('created_by', \Auth::user()->creatorId())->get()->pluck('name', 'id'); return view('pos.print', compact('warehouses')); } else { return redirect()->back()->with('error', __('Permission Denied.')); } } public function getproduct(Request $request) { // dd($request->all()); if ($request->warehouse_id == 0) { $productServices = WarehouseProduct::where('product_id', '=', $request->warehouse_id)->where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id')->toArray(); } else { $productServicesId = WarehouseProduct::where('created_by', '=', \Auth::user()->creatorId())->where('warehouse_id', $request->warehouse_id)->get()->pluck('product_id')->toArray(); $productServices = ProductService::whereIn('id', $productServicesId)->get()->pluck('name', 'id')->toArray(); } return response()->json($productServices); } public function receipt(Request $request) { if (!empty($request->product_id)) { $productServices = ProductService::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('pos.receipt', compact('productServices', 'barcode', 'quantity')); } public function cartdiscount(Request $request) { if ($request->discount) { $sess = session()->get('pos'); $subtotal = !empty($sess) ? array_sum(array_column($sess, 'subtotal')) : 0; $discount = $request->discount; $total = $subtotal - $discount; $total = \Auth::user()->priceFormat($total); } else { $sess = session()->get('pos'); $subtotal = !empty($sess) ? array_sum(array_column($sess, 'subtotal')) : 0; $discount = 0; $total = $subtotal - $discount; $total = \Auth::user()->priceFormat($total); } return response()->json(['total' => $total], '200'); } public function pos($pos_id) { $settings = Utility::settings(); $posId = Crypt::decrypt($pos_id); $pos = Pos::where('id', $posId)->first(); $posPayment = PosPayment::where('pos_id', $pos->id)->first(); $data = DB::table('settings'); $data = $data->where('created_by', '=', $pos->created_by); $data1 = $data->get(); foreach ($data1 as $row) { $settings[$row->name] = $row->value; } $customer = $pos->customer; $totalTaxPrice = 0; $totalQuantity = 0; $totalRate = 0; $totalDiscount = 0; $taxesData = []; $items = []; foreach ($pos->items as $product) { $item = new \stdClass(); $item->name = !empty($product->product()) ? $product->product()->name : ''; $item->quantity = $product->quantity; $item->tax = $product->tax; $item->discount = $product->discount; $item->price = $product->price; $item->description = $product->description; $totalQuantity += $item->quantity; $totalRate += $item->price; $totalDiscount += $item->discount; $taxes = Utility::tax($product->tax); $itemTaxes = []; if (!empty($item->tax)) { foreach ($taxes as $tax) { $taxPrice = Utility::taxRate($tax->rate, $item->price, $item->quantity); $totalTaxPrice += $taxPrice; $itemTax['name'] = $tax->name; $itemTax['rate'] = $tax->rate . '%'; $itemTax['price'] = Utility::priceFormat($settings, $taxPrice); $itemTaxes[] = $itemTax; if (array_key_exists($tax->name, $taxesData)) { $taxesData[$tax->name] = $taxesData[$tax->name] + $taxPrice; } else { $taxesData[$tax->name] = $taxPrice; } } $item->itemTax = $itemTaxes; } else { $item->itemTax = []; } $items[] = $item; } $pos->itemData = $items; $pos->totalTaxPrice = $totalTaxPrice; $pos->totalQuantity = $totalQuantity; $pos->totalRate = $totalRate; $pos->totalDiscount = $totalDiscount; $pos->taxesData = $taxesData; $logo = asset(Storage::url('uploads/logo/')); $company_logo = Utility::getValByName('company_logo_dark'); $pos_logo = Utility::getValByName('pos_logo'); if (isset($pos_logo) && !empty($pos_logo)) { $img = Utility::get_file('pos_logo/') . $pos_logo; } else { $img = asset($logo . '/' . (isset($company_logo) && !empty($company_logo) ? $company_logo : 'logo-dark.png')); } if ($pos) { $color = '#' . $settings['pos_color']; $font_color = Utility::getFontColor($color); return view('pos.templates.' . $settings['pos_template'], compact('pos', 'posPayment', 'color', 'settings', 'customer', 'img', 'font_color')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function previewPos($template, $color) { $objUser = \Auth::user(); $settings = Utility::settings(); $pos = new Pos(); $posPayment = new posPayment(); $posPayment->amount = 360; $posPayment->discount = 100; $customer = new \stdClass(); $customer->email = ''; $customer->shipping_name = ''; $customer->shipping_country = ''; $customer->shipping_state = ''; $customer->shipping_city = ''; $customer->shipping_phone = ''; $customer->shipping_zip = ''; $customer->shipping_address = '
'; $customer->billing_name = ''; $customer->billing_country = ''; $customer->billing_state = ''; $customer->billing_city = ''; $customer->billing_phone = ''; $customer->billing_zip = ''; $customer->billing_address = '
'; $totalTaxPrice = 0; $taxesData = []; $items = []; for ($i = 1; $i <= 3; $i++) { $item = new \stdClass(); $item->name = 'Item ' . $i; $item->quantity = 1; $item->tax = 5; $item->discount = 50; $item->price = 100; $taxes = [ 'Tax 1', 'Tax 2', ]; $itemTaxes = []; foreach ($taxes as $k => $tax) { $taxPrice = 10; $totalTaxPrice += $taxPrice; $itemTax['name'] = 'Tax ' . $k; $itemTax['rate'] = '10 %'; $itemTax['price'] = '$10'; $itemTaxes[] = $itemTax; if (array_key_exists('Tax ' . $k, $taxesData)) { $taxesData['Tax ' . $k] = $taxesData['Tax 1'] + $taxPrice; } else { $taxesData['Tax ' . $k] = $taxPrice; } } $item->itemTax = $itemTaxes; $items[] = $item; } $pos->pos_id = 1; $pos->issue_date = date('Y-m-d H:i:s'); $pos->itemData = $items; $pos->totalTaxPrice = 60; $pos->totalQuantity = 3; $pos->totalRate = 300; $pos->totalDiscount = 10; $pos->taxesData = $taxesData; $pos->created_by = $objUser->creatorId(); $preview = 1; $color = '#' . $color; $font_color = Utility::getFontColor($color); $logo = asset(Storage::url('uploads/logo/')); $company_logo = Utility::getValByName('company_logo_dark'); $settings_data = \App\Models\Utility::settingsById($pos->created_by); $pos_logo = $settings_data['pos_logo']; if (isset($pos_logo) && !empty($pos_logo)) { $img = Utility::get_file('pos_logo/') . $pos_logo; } else { $img = asset($logo . '/' . (isset($company_logo) && !empty($company_logo) ? $company_logo : 'logo-dark.png')); } return view('pos.templates.' . $template, compact('pos', 'preview', 'color', 'img', 'settings', 'customer', 'font_color', 'posPayment')); } public function savePosTemplateSettings(Request $request) { $post = $request->all(); unset($post['_token']); if (isset($post['pos_template']) && (!isset($post['pos_color']) || empty($post['pos_color']))) { $post['pos_color'] = "ffffff"; } if ($request->pos_logo) { $dir = 'pos_logo/'; $pos_logo = \Auth::user()->id . '_pos_logo.png'; $validation = [ 'mimes:' . 'png', 'max:' . '20480', ]; $path = Utility::upload_file($request, 'pos_logo', $pos_logo, $dir, $validation); if ($path['flag'] == 0) { return redirect()->back()->with('error', __($path['msg'])); } $post['pos_logo'] = $pos_logo; } foreach ($post as $key => $data) { \DB::insert( 'insert into settings (`value`, `name`,`created_by`) values (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`) ', [ $data, $key, \Auth::user()->creatorId(), ] ); } return redirect()->back()->with('success', __('POS Setting updated successfully')); } //for thermal print public function printView(Request $request) { $sess = session()->get('pos'); $user = Auth::user(); $settings = Utility::settings(); $customer = Customer::where('name', '=', $request->vc_name)->where('created_by', $user->creatorId())->first(); $warehouse = warehouse::where('id', '=', $request->warehouse_name)->where('created_by', $user->creatorId())->first(); $details = [ 'pos_id' => $user->posNumberFormat($this->invoicePosNumber()), 'customer' => $customer != null ? $customer->toArray() : [], 'warehouse' => $warehouse != null ? $warehouse->toArray() : [], 'user' => $user != null ? $user->toArray() : [], 'date' => date('Y-m-d'), 'pay' => 'show', ]; if (!empty($details['customer'])) { $warehousedetails = '' . ucfirst($details['warehouse']['name']) . '

'; $details['customer']['billing_state'] = $details['customer']['billing_state'] != '' ? ", " . $details['customer']['billing_state'] : ''; $details['customer']['shipping_state'] = $details['customer']['shipping_state'] != '' ? ", " . $details['customer']['shipping_state'] : ''; $customerdetails = '
' . ucfirst($details['customer']['name']) . '

' . $details['customer']['billing_phone'] . '

' . '

' . $details['customer']['billing_address'] . '

' . '

' . $details['customer']['billing_city'] . $details['customer']['billing_state'] . '

' . '

' . $details['customer']['billing_country'] . '

' . '

' . $details['customer']['billing_zip'] . '

'; $shippdetails = '
' . ucfirst($details['customer']['name']) . '' . '

' . $details['customer']['shipping_phone'] . '

' . '

' . $details['customer']['shipping_address'] . '

' . '

' . $details['customer']['shipping_city'] . $details['customer']['shipping_state'] . '

' . '

' . $details['customer']['shipping_country'] . '

' . '

' . $details['customer']['shipping_zip'] . '

'; } else { $customerdetails = '

' . __('Walk-in Customer') . '

'; $warehousedetails = '' . ucfirst($details['warehouse']['name']) . '

'; $shippdetails = '-'; } $settings['company_telephone'] = $settings['company_telephone'] != '' ? ", " . $settings['company_telephone'] : ''; $settings['company_state'] = $settings['company_state'] != '' ? ", " . $settings['company_state'] : ''; $userdetails = '

' . ucfirst($details['user']['name']) . '

' . '

' . $settings['company_name'] . $settings['company_telephone'] . '

' . '

' . $settings['company_address'] . '

' . '

' . $settings['company_city'] . $settings['company_state'] . '

' . '

' . $settings['company_country'] . '

' . '

' . $settings['company_zipcode'] . '

'; $details['customer']['details'] = $customerdetails; $details['warehouse']['details'] = $warehousedetails; // $details['customer']['shippdetails'] = $shippdetails; $details['user']['details'] = $userdetails; $mainsubtotal = 0; $sales = []; foreach ($sess as $key => $value) { $subtotal = $value['price'] * $value['quantity']; $tax = ($subtotal * $value['tax']) / 100; $sales['data'][$key]['name'] = $value['name']; $sales['data'][$key]['quantity'] = $value['quantity']; $sales['data'][$key]['price'] = Auth::user()->priceFormat($value['price']); $sales['data'][$key]['tax'] = $value['tax'] . '%'; $sales['data'][$key]['product_tax'] = $value['product_tax']; $sales['data'][$key]['tax_amount'] = Auth::user()->priceFormat($tax); $sales['data'][$key]['subtotal'] = Auth::user()->priceFormat($value['subtotal']); $mainsubtotal += $value['subtotal']; } $discount = !empty($request->discount) ? $request->discount : 0; $sales['discount'] = Auth::user()->priceFormat($discount); $total = $mainsubtotal - $discount; $sales['sub_total'] = Auth::user()->priceFormat($mainsubtotal); $sales['total'] = Auth::user()->priceFormat($total); //for barcode $productServices = ProductService::where('created_by', '=', \Auth::user()->creatorId())->get(); $barcode = [ 'barcodeType' => Auth::user()->barcodeType(), 'barcodeFormat' => Auth::user()->barcodeFormat(), ]; return view('pos.printview', compact('details', 'sales', 'customer', 'productServices', 'barcode')); } }