dbfdg can('manage product & service')) { $category = ProductServiceCategory::where('created_by', '=', \Auth::user()->creatorId())->where('type', '=', 'product & service')->get()->pluck('name', 'id'); $category->prepend('Select Category', ''); if (!empty($request->category)) { $productServices = ProductService::where('created_by', '=', \Auth::user()->creatorId())->where('category_id', $request->category)->with(['category', 'unit'])->get(); } else { $productServices = ProductService::where('created_by', '=', \Auth::user()->creatorId())->with(['category', 'unit'])->get(); } return view('productservice.index', compact('productServices', 'category')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function create() { if (\Auth::user()->can('create product & service')) { $customFields = CustomField::where('created_by', '=', \Auth::user()->creatorId())->where('module', '=', 'product')->get(); $category = ProductServiceCategory::where('created_by', '=', \Auth::user()->creatorId())->where('type', '=', 'product & service')->get()->pluck('name', 'id'); $unit = ProductServiceUnit::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $tax = Tax::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $incomeChartAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name, chart_of_accounts.id as id')) ->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type') ->where('chart_of_account_types.name', 'income') ->where('parent', '=', 0) ->where('chart_of_accounts.created_by', \Auth::user()->creatorId())->get() ->pluck('code_name', 'id'); $incomeChartAccounts->prepend('Select Account', 0); $incomeSubAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name,chart_of_accounts.id, chart_of_accounts.code, chart_of_account_parents.account')); $incomeSubAccounts->leftjoin('chart_of_account_parents', 'chart_of_accounts.parent', 'chart_of_account_parents.id'); $incomeSubAccounts->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type'); $incomeSubAccounts->where('chart_of_account_types.name', 'income'); $incomeSubAccounts->where('chart_of_accounts.parent', '!=', 0); $incomeSubAccounts->where('chart_of_accounts.created_by', \Auth::user()->creatorId()); $incomeSubAccounts = $incomeSubAccounts->get()->toArray(); $expenseChartAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name, chart_of_accounts.id as id')) ->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type') ->whereIn('chart_of_account_types.name', ['Expenses', 'Costs of Goods Sold']) ->where('chart_of_accounts.created_by', \Auth::user()->creatorId())->get() ->pluck('code_name', 'id'); $expenseChartAccounts->prepend('Select Account', ''); $expenseSubAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name,chart_of_accounts.id, chart_of_accounts.code, chart_of_account_parents.account')); $expenseSubAccounts->leftjoin('chart_of_account_parents', 'chart_of_accounts.parent', 'chart_of_account_parents.id'); $expenseSubAccounts->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type'); $expenseSubAccounts->whereIn('chart_of_account_types.name', ['Expenses', 'Costs of Goods Sold']); $expenseSubAccounts->where('chart_of_accounts.parent', '!=', 0); $expenseSubAccounts->where('chart_of_accounts.created_by', \Auth::user()->creatorId()); $expenseSubAccounts = $expenseSubAccounts->get()->toArray(); return view('productservice.create', compact('category', 'unit', 'tax', 'customFields', 'incomeChartAccounts', 'incomeSubAccounts', 'expenseChartAccounts', 'expenseSubAccounts')); } else { return response()->json(['error' => __('Permission denied.')], 401); } } public function store(Request $request) { if (\Auth::user()->can('create product & service')) { $rules = [ 'name' => 'required', 'sku' => [ 'required', Rule::unique('product_services')->where(function ($query) { return $query->where('created_by', \Auth::user()->id); }) ], 'sale_price' => 'required|numeric', 'purchase_price' => 'required|numeric', 'category_id' => 'required', 'unit_id' => 'required', 'type' => 'required', ]; $validator = \Validator::make($request->all(), $rules); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->route('productservice.index')->with('error', $messages->first()); } $productService = new ProductService(); $productService->name = $request->name; $productService->description = $request->description; $productService->sku = $request->sku; $productService->sale_price = $request->sale_price; $productService->purchase_price = $request->purchase_price; $productService->tax_id = !empty($request->tax_id) ? implode(',', $request->tax_id) : ''; $productService->unit_id = $request->unit_id; if (!empty($request->quantity)) { $productService->quantity = $request->quantity; } else { $productService->quantity = 0; } $productService->type = $request->type; $productService->sale_chartaccount_id = $request->sale_chartaccount_id; $productService->expense_chartaccount_id = $request->expense_chartaccount_id; $productService->category_id = $request->category_id; if (!empty($request->pro_image)) { //storage limit $image_size = $request->file('pro_image')->getSize(); $result = Utility::updateStorageLimit(\Auth::user()->creatorId(), $image_size); if ($result == 1) { if ($productService->pro_image) { $path = storage_path('uploads/pro_image' . $productService->pro_image); } $fileName = $request->pro_image->getClientOriginalName(); $productService->pro_image = $fileName; $dir = 'uploads/pro_image'; $path = Utility::upload_file($request, 'pro_image', $fileName, $dir, []); } } $productService->created_by = \Auth::user()->creatorId(); $productService->save(); CustomField::saveData($productService, $request->customField); return redirect()->route('productservice.index')->with('success', __('Product successfully created.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function show() { return redirect()->route('productservice.index'); } public function edit($id) { $productService = ProductService::find($id); if (\Auth::user()->can('edit product & service')) { if ($productService->created_by == \Auth::user()->creatorId()) { $category = ProductServiceCategory::where('created_by', '=', \Auth::user()->creatorId())->where('type', '=', 'product & service')->get()->pluck('name', 'id'); $unit = ProductServiceUnit::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $tax = Tax::where('created_by', '=', \Auth::user()->creatorId())->get()->pluck('name', 'id'); $productService->customField = CustomField::getData($productService, 'product'); $customFields = CustomField::where('created_by', '=', \Auth::user()->creatorId())->where('module', '=', 'product')->get(); $productService->tax_id = explode(',', $productService->tax_id); $incomeChartAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name, chart_of_accounts.id as id')) ->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type') ->where('chart_of_account_types.name', 'income') ->where('parent', '=', 0) ->where('chart_of_accounts.created_by', \Auth::user()->creatorId())->get() ->pluck('code_name', 'id'); $incomeChartAccounts->prepend('Select Account', 0); $incomeSubAccounts = ChartOfAccount::select('chart_of_accounts.id', 'chart_of_accounts.code', 'chart_of_accounts.name', 'chart_of_account_parents.account'); $incomeSubAccounts->leftjoin('chart_of_account_parents', 'chart_of_accounts.parent', 'chart_of_account_parents.id'); $incomeSubAccounts->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type'); $incomeSubAccounts->where('chart_of_account_types.name', 'income'); $incomeSubAccounts->where('chart_of_accounts.parent', '!=', 0); $incomeSubAccounts->where('chart_of_accounts.created_by', \Auth::user()->creatorId()); $incomeSubAccounts = $incomeSubAccounts->get()->toArray(); $expenseChartAccounts = ChartOfAccount::select(\DB::raw('CONCAT(chart_of_accounts.code, " - ", chart_of_accounts.name) AS code_name, chart_of_accounts.id as id')) ->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type') ->whereIn('chart_of_account_types.name', ['Expenses', 'Costs of Goods Sold']) ->where('chart_of_accounts.created_by', \Auth::user()->creatorId())->get() ->pluck('code_name', 'id'); $expenseChartAccounts->prepend('Select Account', ''); $expenseSubAccounts = ChartOfAccount::select('chart_of_accounts.id', 'chart_of_accounts.code', 'chart_of_accounts.name', 'chart_of_account_parents.account'); $expenseSubAccounts->leftjoin('chart_of_account_parents', 'chart_of_accounts.parent', 'chart_of_account_parents.id'); $expenseSubAccounts->leftjoin('chart_of_account_types', 'chart_of_account_types.id', 'chart_of_accounts.type'); $expenseSubAccounts->whereIn('chart_of_account_types.name', ['Expenses', 'Costs of Goods Sold']); $expenseSubAccounts->where('chart_of_accounts.parent', '!=', 0); $expenseSubAccounts->where('chart_of_accounts.created_by', \Auth::user()->creatorId()); $expenseSubAccounts = $expenseSubAccounts->get()->toArray(); return view('productservice.edit', compact('category', 'unit', 'tax', 'productService', 'customFields', 'incomeChartAccounts', 'expenseChartAccounts', 'incomeSubAccounts', 'expenseSubAccounts')); } else { return response()->json(['error' => __('Permission denied.')], 401); } } else { return response()->json(['error' => __('Permission denied.')], 401); } } public function update(Request $request, $id) { if (\Auth::user()->can('edit product & service')) { $productService = ProductService::find($id); if ($productService->created_by == \Auth::user()->creatorId()) { $rules = [ 'name' => 'required', 'sku' => 'required', Rule::unique('product_services')->ignore($productService->id), 'sale_price' => 'required|numeric', 'purchase_price' => 'required|numeric', 'category_id' => 'required', 'unit_id' => 'required', 'type' => 'required', ]; $validator = \Validator::make($request->all(), $rules); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->route('productservice.index')->with('error', $messages->first()); } $productService->name = $request->name; $productService->description = $request->description; $productService->sku = $request->sku; $productService->sale_price = $request->sale_price; $productService->purchase_price = $request->purchase_price; $productService->tax_id = !empty($request->tax_id) ? implode(',', $request->tax_id) : ''; $productService->unit_id = $request->unit_id; if (!empty($request->quantity)) { $productService->quantity = $request->quantity; } else { $productService->quantity = 0; } $productService->type = $request->type; $productService->sale_chartaccount_id = $request->sale_chartaccount_id; $productService->expense_chartaccount_id = $request->expense_chartaccount_id; $productService->category_id = $request->category_id; if (!empty($request->pro_image)) { //storage limit $file_path = '/uploads/pro_image/' . $productService->pro_image; $image_size = $request->file('pro_image')->getSize(); $result = Utility::updateStorageLimit(\Auth::user()->creatorId(), $image_size); if ($result == 1) { if ($productService->pro_image) { Utility::changeStorageLimit(\Auth::user()->creatorId(), $file_path); $path = storage_path('uploads/pro_image' . $productService->pro_image); // if(file_exists($path)) // { // \File::delete($path); // } } $fileName = $request->pro_image->getClientOriginalName(); $productService->pro_image = $fileName; $dir = 'uploads/pro_image'; $path = Utility::upload_file($request, 'pro_image', $fileName, $dir, []); } } $productService->created_by = \Auth::user()->creatorId(); $productService->save(); CustomField::saveData($productService, $request->customField); return redirect()->route('productservice.index')->with('success', __('Product successfully updated.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function destroy($id) { if (\Auth::user()->can('delete product & service')) { $productService = ProductService::find($id); if ($productService->created_by == \Auth::user()->creatorId()) { if (!empty($productService->pro_image)) { //storage limit $file_path = '/uploads/pro_image/' . $productService->pro_image; $result = Utility::changeStorageLimit(\Auth::user()->creatorId(), $file_path); } $productService->delete(); return redirect()->route('productservice.index')->with('success', __('Product successfully deleted.')); } else { return redirect()->back()->with('error', __('Permission denied.')); } } else { return redirect()->back()->with('error', __('Permission denied.')); } } public function export() { $name = 'product_service_' . date('Y-m-d i:h:s'); $data = Excel::download(new ProductServiceExport(), $name . '.xlsx'); return $data; } public function importFile() { return view('productservice.import'); } public function import(Request $request) { $rules = [ 'file' => 'required|mimes:csv,txt', ]; $validator = \Validator::make($request->all(), $rules); if ($validator->fails()) { $messages = $validator->getMessageBag(); return redirect()->back()->with('error', $messages->first()); } $products = (new ProductServiceImport)->toArray(request()->file('file'))[0]; $totalProduct = count($products) - 1; $errorArray = []; for ($i = 1; $i <= count($products) - 1; $i++) { $items = $products[$i]; $taxes = explode(';', $items[5]); $taxesData = []; foreach ($taxes as $tax) { $taxes = Tax::where('id', $tax)->first(); // $taxesData[] = $taxes->id; $taxesData[] = !empty($taxes->id) ? $taxes->id : 0; } $taxData = implode(',', $taxesData); // dd($taxData); if (!empty($productBySku)) { $productService = $productBySku; } else { $productService = new ProductService(); } $productService->name = $items[0]; $productService->sku = $items[1]; $productService->sale_price = $items[2]; $productService->purchase_price = $items[3]; $productService->quantity = $items[4]; $productService->tax_id = $items[5]; $productService->category_id = $items[6]; $productService->unit_id = $items[7]; $productService->type = $items[8]; $productService->description = $items[9]; $productService->created_by = \Auth::user()->creatorId(); if (empty($productService)) { $errorArray[] = $productService; } else { $productService->save(); } } $errorRecord = []; if (empty($errorArray)) { $data['status'] = 'success'; $data['msg'] = __('Record successfully imported'); } else { $data['status'] = 'error'; $data['msg'] = count($errorArray) . ' ' . __('Record imported fail out of' . ' ' . $totalProduct . ' ' . 'record'); foreach ($errorArray as $errorData) { $errorRecord[] = implode(',', $errorData); } \Session::put('errorArray', $errorRecord); } return redirect()->back()->with($data['status'], $data['msg']); } public function warehouseDetail($id) { $products = WarehouseProduct::with(['warehouse'])->where('product_id', '=', $id)->where('created_by', '=', \Auth::user()->creatorId())->get(); return view('productservice.detail', compact('products')); } public function searchProducts(Request $request) { $lastsegment = $request->session_key; if (Auth::user()->can('manage pos') && $request->ajax() && isset($lastsegment) && !empty($lastsegment)) { $output = ""; if($request->war_id == '0'){ $ids = WarehouseProduct::where('warehouse_id',1)->get()->pluck('product_id')->toArray(); if ($request->cat_id !== '' && $request->search == '') { if($request->cat_id == '0'){ $products = ProductService::getallproducts()->whereIn('product_services.id',$ids)->with(['unit'])->get(); }else{ $products = ProductService::getallproducts()->where('category_id', $request->cat_id)->whereIn('product_services.id',$ids)->with(['unit'])->get(); } } else { if($request->cat_id == '0'){ $products = ProductService::getallproducts()->where('product_services.'.$request->type, 'LIKE', "%{$request->search}%")->with(['unit'])->get(); }else{ $products = ProductService::getallproducts()->where('product_services.'.$request->type, 'LIKE', "%{$request->search}%")->orWhere('category_id', $request->cat_id)->with(['unit'])->get(); } } }else{ $ids = WarehouseProduct::where('warehouse_id',$request->war_id)->get()->pluck('product_id')->toArray(); if($request->cat_id == '0'){ $products = ProductService::getallproducts()->whereIn('product_services.id',$ids)->with(['unit'])->get(); }else{ $products = ProductService::getallproducts()->whereIn('product_services.id',$ids)->where('category_id', $request->cat_id)->with(['unit'])->get(); } } if (count($products)>0) { foreach ($products as $key => $product) { $quantity = $product->warehouseProduct($product->id, $request->war_id != 0 ? $request->war_id : 7); $unit = (!empty($product) && !empty($product->unit)) ? $product->unit->name : ''; if (!empty($product->pro_image)) { $image_url = ('uploads/pro_image') . '/' . $product->pro_image; } else { $image_url = ('uploads/pro_image') . '/default.png'; } if ($request->session_key == 'purchases') { $productprice = $product->purchase_price != 0 ? $product->purchase_price : 0; } else if ($request->session_key == 'pos') { $productprice = $product->sale_price != 0 ? $product->sale_price : 0; } else { $productprice = $product->sale_price != 0 ? $product->sale_price : $product->purchase_price; } $output .= '