public function show($name)
 {
     $products = SubProduct::where('sub_product_name', $name)->get();
     $product = null;
     foreach ($products as $p) {
         $product = $p;
     }
     $moduleId = 6;
     //project
     //get the image assiociates
     $prodFiles = Files::where('attachment_id', $product->id)->where('is_active', True)->where('module_id', $moduleId)->get();
     //get all materials associated
     $curMaterials = array();
     $curMatIds = array();
     $prodMaterials = SubProduct::find($product->id)->materials()->get();
     $matcategids = array();
     foreach ($prodMaterials as $pm) {
         //gets the current set materials
         $matExtObject = Material::where('is_active', true)->where('id', $pm->material_id)->first();
         $matExtObject->price = $pm->mat_sub_price;
         array_push($curMaterials, $matExtObject);
         array_push($curMatIds, $pm->material_id);
         array_push($matcategids, $matExtObject->material_categ_id);
         $matExtObject = null;
     }
     //get materialcategory from materials
     $materialCategs = MaterialCategory::whereIn('id', $matcategids)->get();
     //all materials
     $allMaterials = Material::all();
     return view('pages.sub_product.show', ['product' => $product, 'prodFiles' => $prodFiles, 'curMaterials' => $curMaterials, 'materialCategs' => $materialCategs, 'allMaterials' => $allMaterials]);
 }
 /**
  * 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] == '&nbsp;') {
                                         $isExist = True;
                                         continue;
                                     }
                                 }
                             } else {
                                 break;
                             }
                         }
                     }
                     if (!$isExist) {
                         array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => '&nbsp;'));
                     }
                 } else {
                     array_push($matPriceArray[$matItem['mat_sub_name']], array($cuuProd->sub_product_name => '&nbsp;'));
                 }
             }
         }
         $count++;
     }
     return view('pages.product.show', ['product' => $product, 'curSubProducts' => $curSubProducts, 'matPriceArray' => $matPriceArray, 'prodFiles' => $prodFiles, 'curMaterials' => $curMaterials, 'materialCategs' => $materialCategs, 'allMaterials' => $allMaterials]);
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     $matObj = new Material();
     $material = $matObj->find($id);
     $materialCategories = MaterialCategory::all();
     $materialCategId = MaterialCategory::find($material->material_categ_id);
     $moduleId = 4;
     //material
     //get the image assiociates
     $matFiles = Files::where('attachment_id', $material->id)->where('is_active', True)->where('module_id', $moduleId)->get();
     return view('pages.admin.material.edit', ['material' => $material, 'materialCategories' => $materialCategories, 'materialCategId' => $materialCategId, 'matFiles' => $matFiles, 'moduleId' => $moduleId]);
 }