Ejemplo n.º 1
0
 public function edit(Request $request)
 {
     \DB::beginTransaction();
     //var_dump($request->all()); die();
     $oProd = Product::find($request->input('product_id'));
     //si viene el prod y ademas es prod con variantes
     if (!empty($oProd) && $oProd->hasVariants == 1) {
         $variant = $this->variantRepo->findV($request->input('id'));
         if ($request->input('autogenerado') === true) {
             $sku = \DB::table('variants')->max('sku');
             if (!empty($sku)) {
                 $sku = $sku + 1;
             } else {
                 $sku = 1000;
                 //inicializar el sku;
             }
             $request->merge(array('sku' => $sku));
         } else {
         }
         $request->merge(array('user_id' => Auth()->user()->id));
         $managerVar = new VariantManager($variant, $request->except('stock', 'detAtr', 'presentation_base_object', 'presentations'));
         $managerVar->save();
         $oProd->quantVar = $oProd->quantVar + 1;
         $oProd->save();
         //================================ VARIANTES==============================//
         //$variant->presentation()->detach();
         foreach ($request->input('presentations') as $presentation) {
             $presentation['variant_id'] = $variant->id;
             $presentation['presentation_id'] = $presentation['id'];
             $oPres = new DetPresRepo();
             //$oStock = $stockRepo->getModel()->where('variant_id',$stock['variant_id'])->where('warehouse_id',$stock['warehouse_id'])->first();
             $obj = $oPres->getModel()->where('variant_id', $presentation['variant_id'])->where('presentation_id', $presentation['presentation_id'])->first();
             if (!isset($obj->id)) {
                 $presManager = new DetPresManager($oPres->getModel(), $presentation);
                 $presManager->save();
             } else {
                 $presManager = new DetPresManager($obj, $presentation);
                 $presManager->save();
             }
             //$presManager = new DetPresManager($oPres->getModel(),$presentation);
             //$presManager->save();
         }
         $variant->atributes()->detach();
         foreach ($request->input('detAtr') as $detAtr) {
             if (!empty($detAtr['descripcion'])) {
                 $detAtr['variant_id'] = $variant->id;
                 $oDetAtr = new DetAtrRepo();
                 $detAtrManager = new DetAtrManager($oDetAtr->getModel(), $detAtr);
                 $detAtrManager->save();
             }
         }
         if ($request->input('track') == 1) {
             //if (empty($variant->warehouse())) {
             //var_dump( $variant->stock ); die();
             //$variant->warehouse()->detach();
             foreach ($request->input('stock') as $stock) {
                 if (isset($stock['stockActual']) && $stock['stockActual'] == null && $stock['stockActual'] == '') {
                     $stock['stockActual'] = 0;
                 }
                 if (isset($stock['stockMin']) && $stock['stockMin'] == null && $stock['stockMin'] == '') {
                     $stock['stockMin'] = 0;
                 }
                 if (isset($stock['stockMinSoles']) && $stock['stockMinSoles'] == null && $stock['stockMinSoles'] == '') {
                     $stock['stockMinSoles'] = 0;
                 }
                 $stock['variant_id'] = $variant->id;
                 $oStock = new StockRepo();
                 //var_dump($stock['variant_id']);
                 //var_dump($stock['warehouse_id']);
                 $obj = $oStock->getModel()->where('variant_id', $stock['variant_id'])->where('warehouse_id', $stock['warehouse_id'])->first();
                 if (!isset($obj->id)) {
                     $stockManager = new StockManager($oStock->getModel(), $stock);
                     $stockManager->save();
                 } else {
                     $stockManager = new StockManager($obj, $stock);
                     $stockManager->save();
                 }
                 //print_r($obj->id); die();
             }
             //}
         }
         \DB::commit();
         return response()->json(['estado' => true, 'nombres' => $variant->nombre]);
     } else {
         return response()->json(['estado' => 'Prod sin variantes']);
     }
     //================================./VARIANTES==============================//
 }
Ejemplo n.º 2
0
 public function edit(Request $request)
 {
     \DB::beginTransaction();
     //$customer = $this->customerRepo->find($request->id);
     //$manager = new CustomerManager($customer,$request->except('fechaNac'));
     //$manager->save();
     $product = $this->productRepo->find($request->id);
     //$detPres = $this->detPres->getModel();
     if ($request->input('estado') == 1) {
     } else {
         $request->merge(array('estado' => '0'));
     }
     if ($request->input('hasVariants') == 1) {
     } else {
         $request->merge(array('hasVariants' => '0'));
     }
     if ($request->input('track') == 1) {
     } else {
         $request->merge(array('track' => '0'));
     }
     $request->merge(array('user_id' => Auth()->user()->id));
     //var_dump($request->all());die();
     $managerPro = new ProductManager($product, $request->except('sku', 'suppPri', 'markup', 'price', 'track'));
     //================================PROD CON VARIANTES==============================//
     if ($request->input('hasVariants') === true) {
         $managerPro->save();
         $request->merge(array('product_id' => $product->id));
         $product->quantVar = 0;
         //cantidad de variantes igual a 0;
         $product->save();
         //$managerVar = new VariantManager($variant,$request->only('sku','suppPri','markup','price','track','product_id'));
         //$managerVar->save();
         //================================./PROD CON VARIANTES==============================//
         //================================PROD SIN VARIANTES==============================//
     } elseif ($request->input('hasVariants') === '0') {
         $managerPro->save();
         $request->merge(array('product_id' => $product->id));
         if ($request->input('autogenerado') === true) {
             $sku = \DB::table('variants')->max('sku');
             if (!empty($sku)) {
                 $sku = $sku + 1;
             } else {
                 $sku = 1000;
                 //inicializar el sku;
             }
             $request->merge(array('sku' => $sku));
         } else {
         }
         $product->quantVar = 0;
         //aunq presenta una fila en la tabla variantes por defecto
         $product->save();
         $variant = $this->variantRepo->getModel()->where('product_id', $product->id)->first();
         $managerVar = new VariantManager($variant, $request->only('sku', 'suppPri', 'markup', 'price', 'track', 'codigo', 'product_id', 'user_id'));
         $managerVar->save();
         //var_dump($request->input('presentations')); die();
         //$variant->presentation()->detach();
         foreach ($request->input('presentations') as $presentation) {
             //var_dump('o'); die();
             $presentation['variant_id'] = $variant->id;
             $presentation['presentation_id'] = $presentation['id'];
             /*$detpresRepo = new DetPresRepo();
               //$oPres = $detpresRepo->getModel()->where('presentation_id',$presentation['presentation_id'])->where('variant_id',$presentation['variant_id'])->first();
               $oPres = $detpresRepo->getModel();
               $presManager = new DetPresManager($oPres,$presentation);
               $presManager->save();*/
             $oPres = new DetPresRepo();
             //$oStock = $stockRepo->getModel()->where('variant_id',$stock['variant_id'])->where('warehouse_id',$stock['warehouse_id'])->first();
             $obj = $oPres->getModel()->where('variant_id', $presentation['variant_id'])->where('presentation_id', $presentation['presentation_id'])->first();
             if (!isset($obj->id)) {
                 $presManager = new DetPresManager($oPres->getModel(), $presentation);
                 $presManager->save();
             } else {
                 $presManager = new DetPresManager($obj, $presentation);
                 $presManager->save();
             }
         }
         if ($request->input('track') == 1) {
             //$variant->warehouse()->detach();
             foreach ($request->input('stock') as $stock) {
                 if (isset($stock['stockActual']) && $stock['stockActual'] == null) {
                     $stock['stockActual'] = 0;
                 }
                 if (isset($stock['stockMin']) && $stock['stockMin'] == null) {
                     $stock['stockMin'] = 0;
                 }
                 if (isset($stock['stockMinSoles']) && $stock['stockMinSoles'] == null) {
                     $stock['stockMinSoles'] = 0;
                 }
                 $stock['variant_id'] = $variant->id;
                 $stockRepo = new StockRepo();
                 //$oStock = $stockRepo->getModel()->where('variant_id',$stock['variant_id'])->where('warehouse_id',$stock['warehouse_id'])->first();
                 $obj = $stockRepo->getModel()->where('variant_id', $stock['variant_id'])->where('warehouse_id', $stock['warehouse_id'])->first();
                 if (!isset($obj->id)) {
                     $stockManager = new StockManager($stockRepo->getModel(), $stock);
                     $stockManager->save();
                 } else {
                     $stockManager = new StockManager($obj, $stock);
                     $stockManager->save();
                 }
                 /*$oStock = $stockRepo->getModel();
                   $stockManager = new StockManager($oStock, $stock);
                   $stockManager->save();
                   */
             }
         }
     }
     //================================./PROD SIN VARIANTES==============================//
     //================================ADD IMAGE TO PROD==============================//
     if ($request->has('image') and substr($request->input('image'), 5, 5) === 'image') {
         $image = $request->input('image');
         $mime = $this->get_string_between($image, '/', ';');
         $image = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $image));
         Image::make($image)->resize(200, 200)->save('images/products/' . $product->id . '.' . $mime);
         $product->image = '/images/products/' . $product->id . '.' . $mime;
         $product->save();
     }
     //================================./ADD IMAGE TO PROD==============================//
     \DB::commit();
     return response()->json(['estado' => true, 'nombres' => $product->nombre]);
 }