Example #1
0
 /**
  * @return \Illuminate\Http\JsonResponse
  */
 public function stock()
 {
     $products = Product::all();
     $stock = Stock::orderBy('quality', 'DESC')->get();
     if ($products->count()) {
         $stock_count = 0;
         foreach ($stock as $stok) {
             $stock_product = Product::where('id', '=', $stok->product_id);
             if ($stock_product->count()) {
                 $stock_name = $stock_product->first()->name;
                 $stock_type = $stock_product->first()->type;
                 $stock[$stock_count]->stock_name = $stock_name;
                 $stock[$stock_count]->type = $stock_type;
             }
             $stock_count++;
         }
         $prod_count = 0;
         foreach ($products as $product) {
             $distributor = Distributor::where('id', '=', $product->distributor_id);
             if ($distributor->count()) {
                 $distributor = $distributor->first();
                 $distributor_name = $distributor->name;
                 $products[$prod_count]->distributor_name = $distributor_name;
             }
             $prod_count++;
         }
         return response()->json(['products' => $product, 'stock' => $stock]);
     } else {
         return response()->json(['products' => [], 'stock' => []]);
     }
 }
 /**
  * @param $dist_id
  * @param Request $request
  * @return \Illuminate\Http\JsonResponse
  */
 public function deleteDistributor($dist_id)
 {
     $dist = Distributor::where('id', $dist_id);
     $products = Product::where('distributor_id', '=', $dist_id);
     if ($products->count()) {
         return response()->json(['deleted' => false, 'products' => true]);
     } else {
         if ($dist->delete()) {
             return response()->json(['deleted' => true, 'products' => false]);
         }
         return response()->json(['deleted' => false]);
     }
 }
Example #3
0
 /**
  * @param Request $request
  * @return \Illuminate\Http\JsonResponse
  */
 public function updateProduct($id, Request $request)
 {
     $name = $request->input('name');
     $type = $request->input('type');
     $dist_id = $request->input('dist_id');
     $update = Product::where('id', $id)->update(['name' => $name, 'type' => $type, 'distributor_id' => $dist_id]);
     $dist_name = Distributor::where('id', '=', $dist_id)->get();
     if ($dist_name->count()) {
         $dist_name = $dist_name->first()->name;
     }
     if ($update) {
         return response()->json(['updated' => true, 'dist_name' => $dist_name]);
     }
 }