/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $product = Product::find($id); $moduleId = 2; //product //get the image assiociates $prodFiles = Files::where('attachment_id', $product->id)->where('is_active', True)->where('module_id', $moduleId)->get(); //get product materials $prodMaterials = Product::find($id)->materials()->get(); $curMaterials = array(); $matcategids = array(); foreach ($prodMaterials as $pm) { //gets the current set materials $matExtObject = Material::where('is_active', true)->where('id', $pm->material_id)->first(); array_push($curMaterials, $matExtObject); array_push($matcategids, $matExtObject->material_categ_id); $matExtObject = null; } //get materialcategory from materials $materialCategs = MaterialCategory::whereIn('id', $matcategids)->get(); //all materials $allMaterials = Material::all(); //1. get subproducts from product $curSubProducts = array(); $curSubProductsNames = array(); $matPriceArray = ''; $subProducts = Product::find($id)->sub_products()->get(); foreach ($subProducts as $pm) { $subprodExtObject = SubProduct::where('is_active', true)->where('id', $pm->sub_product_id)->first(); array_push($curSubProducts, $subprodExtObject); array_push($curSubProductsNames, $subprodExtObject->sub_product_name); $subprodExtObject = null; } $currMaterialsDet = array(); foreach ($curSubProducts as $curSubProduct) { $prodMaterials = SubProduct::find($curSubProduct->id)->materials()->get(); foreach ($prodMaterials as $pm) { //gets the current set materials $matExtObject = Material::where('is_active', true)->where('id', $pm->material_id)->first(); array_push($currMaterialsDet, array('subprod_name' => $curSubProduct->sub_product_name, 'mat_sub_id' => $matExtObject->id, 'mat_sub_name' => $matExtObject->material_name, 'mat_sub_price' => $pm->mat_sub_price)); $matPriceArray[$matExtObject->material_name] = array(); $matExtObject = null; } } // echo '<pre>'; $count = 0; foreach ($curSubProducts as $cuuProd) { foreach ($currMaterialsDet as $matItem) { $matCount = SubProductMaterial::where('sub_product_id', $cuuProd->id)->where('material_id', $matItem['mat_sub_id'])->first(); if ($matCount) { //check if key value pairs exist first if (!empty($matPriceArray[$matItem['mat_sub_name']])) { $isExist = False; if (array_key_exists($count, $matPriceArray[$matItem['mat_sub_name']])) { if (array_key_exists($cuuProd->sub_product_name, $matPriceArray[$matItem['mat_sub_name']][$count])) { if ($count < count($matPriceArray)) { // print 'key: ' . key($matPriceArray[$matItem['mat_sub_name']][$count]) . ' productname: ' . $cuuProd->sub_product_name . '<br>'; if (key($matPriceArray[$matItem['mat_sub_name']][$count]) == $cuuProd->sub_product_name) { // print 'exist<br>'; // print 'price: ' . $matPriceArray[$matItem['mat_sub_name']][$count][$cuuProd->sub_product_name] . ' price actual: ' . $matCount->mat_sub_price . '<br>'; if ($matPriceArray[$matItem['mat_sub_name']][$count][$cuuProd->sub_product_name] == $matCount->mat_sub_price) { $isExist = True; continue; } } } else { break; } } } if (!$isExist) { array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => $matCount->mat_sub_price)); } } else { array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => $matCount->mat_sub_price)); } } else { //check if key value pairs exist first if (!empty($matPriceArray[$matItem['mat_sub_name']])) { $isExist = False; if (array_key_exists($count, $matPriceArray[$matItem['mat_sub_name']])) { if (array_key_exists($cuuProd->sub_product_name, $matPriceArray[$matItem['mat_sub_name']][$count])) { if ($count < count($matPriceArray)) { if (key($matPriceArray[$matItem['mat_sub_name']][$count]) == $cuuProd->sub_product_name) { if ($matPriceArray[$matItem['mat_sub_name']][$count][$cuuProd->sub_product_name] == ' ') { $isExist = True; continue; } } } else { break; } } } if (!$isExist) { array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => ' ')); } } else { array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => ' ')); } } } $count++; } return view('pages.product.show', ['product' => $product, 'curSubProducts' => $curSubProducts, 'matPriceArray' => $matPriceArray, 'prodFiles' => $prodFiles, 'curMaterials' => $curMaterials, 'materialCategs' => $materialCategs, 'allMaterials' => $allMaterials]); }
/** * API or adding materials being called from js */ public function removeMaterial(Request $request) { $prodId = $request->input('sub_product_id'); $matId = $request->input('material_id'); //delete material product SubProductMaterial::where('sub_product_id', $prodId)->where('material_id', $matId)->delete(); //get the product-materials $curMaterials = array(); $curMatIds = array(); $prodMaterials = SubProduct::find($prodId)->materials()->get(); foreach ($prodMaterials as $pm) { //gets the current set materials $matExtObject = Material::where('is_active', true)->where('id', $pm->material_id)->first(); array_push($curMaterials, $matExtObject); array_push($curMatIds, $pm->material_id); } //get the all current materials $allMaterials = Material::whereNotIn('id', $curMatIds)->where('is_active', True)->orderBy('material_name', 'asc')->get(); //return current and all materials return json_encode(array('curMaterials' => $curMaterials, 'allMaterials' => $allMaterials)); }