/**
  * Get all of the products of a given Breeder
  *
  * @param  Breeder      $breeder
  * @return Collection
  */
 public function forBreeder(Breeder $breeder)
 {
     $products = $breeder->products()->where('status', 'requested')->get();
     $reservations = $breeder->reservations()->get();
     $items = [];
     // Include all "requested" products
     foreach ($products as $product) {
         if ($product->quantity == 0) {
             continue;
         }
         $itemDetail = [];
         $itemDetail['uuid'] = (string) Uuid::uuid4();
         $itemDetail['id'] = $product->id;
         $itemDetail['reservation_id'] = 0;
         $itemDetail['img_path'] = '/images/product/' . Image::find($product->primary_img_id)->name;
         $itemDetail['breeder_id'] = $product->breeder_id;
         $itemDetail['farm_province'] = FarmAddress::find($product->farm_from_id)->province;
         $itemDetail['name'] = $product->name;
         $itemDetail['type'] = $product->type;
         $itemDetail['age'] = $this->computeAge($product->birthdate);
         $itemDetail['breed'] = $this->transformBreedSyntax(Breed::find($product->breed_id)->name);
         $itemDetail['quantity'] = $product->quantity;
         $itemDetail['adg'] = $product->adg;
         $itemDetail['fcr'] = $product->fcr;
         $itemDetail['bft'] = $product->backfat_thickness;
         $itemDetail['status'] = $product->status;
         $itemDetail['customer_id'] = 0;
         $itemDetail['customer_name'] = '';
         $itemDetail['date_needed'] = '';
         $itemDetail['special_request'] = '';
         array_push($items, (object) $itemDetail);
     }
     // Include "reserved" / "paid" / "on_delivery" / "sold" products
     foreach ($reservations as $reservation) {
         $product = Product::find($reservation->product_id);
         $itemDetail = [];
         $itemDetail['uuid'] = (string) Uuid::uuid4();
         $itemDetail['id'] = $product->id;
         $itemDetail['reservation_id'] = $reservation->id;
         $itemDetail['img_path'] = '/images/product/' . Image::find($product->primary_img_id)->name;
         $itemDetail['breeder_id'] = $product->breeder_id;
         $itemDetail['farm_province'] = FarmAddress::find($product->farm_from_id)->province;
         $itemDetail['name'] = $product->name;
         $itemDetail['type'] = $product->type;
         $itemDetail['age'] = $this->computeAge($product->birthdate);
         $itemDetail['breed'] = $this->transformBreedSyntax(Breed::find($product->breed_id)->name);
         $itemDetail['quantity'] = $reservation->quantity;
         $itemDetail['adg'] = $product->adg;
         $itemDetail['fcr'] = $product->fcr;
         $itemDetail['bft'] = $product->backfat_thickness;
         $itemDetail['status'] = $reservation->order_status;
         $itemDetail['customer_id'] = $reservation->customer_id;
         $itemDetail['customer_name'] = Customer::find($reservation->customer_id)->users()->first()->name;
         $itemDetail['date_needed'] = $this->transformDateSyntax($reservation->date_needed);
         $itemDetail['special_request'] = $reservation->special_request;
         array_push($items, (object) $itemDetail);
     }
     // dd($items);
     return collect($items)->toJson();
 }
 /**
  * Get items in the Swine Cart
  * [!]AJAX
  *
  * @param  Request $request
  * @return JSON/Array
  */
 public function getSwineCartItems(Request $request)
 {
     if ($request->ajax()) {
         $customer = $this->user->userable;
         $swineCartItems = $customer->swineCartItems()->where('if_requested', 0)->get();
         $items = [];
         foreach ($swineCartItems as $item) {
             $itemDetail = [];
             $product = Product::find($item->product_id);
             $itemDetail['item_id'] = $item->id;
             $itemDetail['product_id'] = $item->product_id;
             $itemDetail['product_name'] = $product->name;
             $itemDetail['product_type'] = $product->type;
             $itemDetail['product_breed'] = Breed::find($product->breed_id)->name;
             $itemDetail['img_path'] = '/images/product/' . Image::find($product->primary_img_id)->name;
             $itemDetail['breeder'] = Breeder::find($product->breeder_id)->users()->first()->name;
             $itemDetail['token'] = csrf_token();
             array_push($items, $itemDetail);
         }
         $itemsCollection = collect($items);
         return $itemsCollection->toJson();
     } else {
         $customer = $this->user->userable;
         $swineCartItems = $customer->swineCartItems()->where('if_rated', 0)->get();
         $products = [];
         $log = $customer->transactionLogs()->get();
         $history = [];
         foreach ($swineCartItems as $item) {
             $itemDetail = [];
             $product = Product::find($item->product_id);
             $reviews = Breeder::find($product->breeder_id)->reviews()->get();
             $itemDetail['request_status'] = $item->if_requested;
             $itemDetail['request_quantity'] = $item->quantity;
             $itemDetail['status'] = $item->reservation_id ? ProductReservation::find($item->reservation_id)->order_status : $product->status;
             $itemDetail['staus'] = $product->status;
             $itemDetail['item_id'] = $item->id;
             $itemDetail['customer_id'] = $customer->id;
             $itemDetail['breeder_id'] = $product->breeder_id;
             $itemDetail['breeder'] = Breeder::find($product->breeder_id)->users()->first()->name;
             $itemDetail['product_id'] = $item->product_id;
             $itemDetail['product_province'] = FarmAddress::find($product->farm_from_id)->province;
             $itemDetail['product_name'] = $product->name;
             $itemDetail['product_type'] = $product->type;
             $itemDetail['product_quantity'] = $product->quantity;
             $itemDetail['product_breed'] = $this->transformBreedSyntax(Breed::find($product->breed_id)->name);
             $itemDetail['product_birthdate'] = $this->transformDateSyntax($product->birthdate);
             $itemDetail['product_age'] = $this->computeAge($product->birthdate);
             $itemDetail['product_adg'] = $product->adg;
             $itemDetail['product_fcr'] = $product->fcr;
             $itemDetail['product_backfat_thickness'] = $product->backfat_thickness;
             $itemDetail['other_details'] = $product->other_details;
             $itemDetail['avg_delivery'] = $reviews->avg('rating_delivery');
             $itemDetail['avg_transaction'] = $reviews->avg('rating_transaction');
             $itemDetail['avg_productQuality'] = $reviews->avg('rating_productQuality');
             if ($item->date_needed == '0000-00-00') {
                 $itemDetail['date_needed'] = '';
             } else {
                 $itemDetail['date_needed'] = $this->transformDateSyntax($item->date_needed);
             }
             $itemDetail['special_request'] = $item->special_request;
             $itemDetail['img_path'] = '/images/product/' . Image::find($product->primary_img_id)->name;
             if ($item->transactionLog) {
                 $itemDetail['status_transactions'] = json_decode($item->transactionLog->status_transactions, true);
             } else {
                 $itemDetail['status_transactions'] = ["requested" => '', "reserved" => '', "on_delivery" => '', "paid" => '', "sold" => '', "rated" => ''];
             }
             array_push($products, (object) $itemDetail);
         }
         $products = collect($products);
         $history = collect($history);
         $token = csrf_token();
         $customerId = $customer->id;
         return view('user.customer.swineCart', compact('products', 'history', 'token', 'customerId'));
     }
 }
 /**
  * Delete Breeder's farm information instance
  * AJAX
  *
  * @param  Request $request
  * @return String / View
  */
 public function deleteFarm(Request $request)
 {
     $farmAddress = FarmAddress::find($request->id);
     $farmAddress->delete();
     if ($request->ajax()) {
         return "OK";
     } else {
         return redirect()->route('breeder.edit');
     }
 }
 /**
  * View Details of a Product
  *
  * @param  Product  $product
  * @return View
  */
 public function customerViewProductDetail(Product $product)
 {
     // $product = Product::find($productId);
     $product->img_path = '/images/product/' . Image::find($product->primary_img_id)->name;
     $product->breeder = Breeder::find($product->breeder_id)->users->first()->name;
     $product->birthdate = $this->transformBirthdateSyntax($product->birthdate);
     $product->age = $this->computeAge($product->birthdate);
     $product->type = ucfirst($product->type);
     $product->breed = $this->transformBreedSyntax(Breed::find($product->breed_id)->name);
     $product->farm_province = FarmAddress::find($product->farm_from_id)->province;
     $product->other_details = $this->transformOtherDetailsSyntax($product->other_details);
     $product->imageCollection = $product->images()->where('id', '!=', $product->primary_img_id)->get();
     $product->videoCollection = $product->videos;
     return view('user.customer.viewProductDetail', compact('product'));
 }