/**
  * Create and store Breeder profile data to database and
  * Associate User to Breeder user type as well
  *
  * @param  BreederProfileRequest $request
  * @return Redirect
  */
 public function storeProfile(BreederProfileRequest $request)
 {
     $user = $this->user;
     $breeder = Breeder::create($request->only(['officeAddress_addressLine1', 'officeAddress_addressLine2', 'officeAddress_province', 'officeAddress_zipCode', 'office_landline', 'office_mobile', 'website', 'produce', 'contactPerson_name', 'contactPerson_mobile']));
     $farmAddressArray = [];
     for ($i = 1; $i <= count($request->input('farmAddress.*.*')) / 8; $i++) {
         $farmAddress = new FarmAddress();
         $farmAddress->name = $request->input('farmAddress.' . $i . '.name');
         $farmAddress->addressLine1 = $request->input('farmAddress.' . $i . '.addressLine1');
         $farmAddress->addressLine2 = $request->input('farmAddress.' . $i . '.addressLine2');
         $farmAddress->province = $request->input('farmAddress.' . $i . '.province');
         $farmAddress->zipCode = $request->input('farmAddress.' . $i . '.zipCode');
         $farmAddress->farmType = $request->input('farmAddress.' . $i . '.farmType');
         $farmAddress->landline = $request->input('farmAddress.' . $i . '.landline');
         $farmAddress->mobile = $request->input('farmAddress.' . $i . '.mobile');
         array_push($farmAddressArray, $farmAddress);
     }
     $breeder->users()->save($user);
     $breeder->farmAddresses()->saveMany($farmAddressArray);
     $user->update_profile = 0;
     $user->save();
     return redirect()->route('breeder.edit')->with('message', 'Profile completed.');
 }
 /**
  * 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'));
     }
 }
 /**
  * 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'));
 }
 /**
  * Get the ratings of the Breeder.
  * Include overall, delivery,
  * transaction, and product
  * quality rating
  *
  * @param  Breeder  $breeder
  * @return Array
  */
 public function getRatings(Breeder $breeder)
 {
     $reviewDetails = [];
     $query = $breeder->reviews()->orderBy('created_at', 'desc')->get();
     $reviews = $query->take(3);
     $deliveryRating = $query->avg('rating_delivery');
     $transactionRating = $query->avg('rating_transaction');
     $productQualityRating = $query->avg('rating_productQuality');
     $overallRating = ($deliveryRating + $transactionRating + $productQualityRating) / 3;
     foreach ($reviews as $review) {
         $reviewDetail = [];
         $reviewDetail['customerName'] = Customer::find($review->customer_id)->users()->first()->name;
         $reviewDetail['comment'] = $review->comment;
         array_push($reviewDetails, $reviewDetail);
     }
     return ['overall' => round($overallRating, 2), 'delivery' => round($deliveryRating, 1), 'transaction' => round($transactionRating, 1), 'productQuality' => round($productQualityRating, 1), 'reviews' => $reviewDetails];
 }
 /**
  * Create an initial account for the breeder
  *
  * @param  \Illuminate\Http\Request  $request
  * @return View
  */
 public function createUser(Request $request)
 {
     $validator = $this->validator($request->all());
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $verCode = str_random('10');
     // create a verification code
     $password = $this->generatePassword();
     // generate the initial password for the breeder and save it to a variable to get the original password before encryption
     $user = $this->create($request->all(), $verCode, $password);
     // create a user instance
     $user->assignRole('breeder');
     // assign a breeder role to it
     $breeder = Breeder::create([])->users()->save($user);
     // create a breeder instance for that user
     // data to be passed in the email
     $data = ['email' => $request->input('email'), 'password' => $password];
     $adminID = Auth::user()->id;
     $adminName = Auth::user()->name;
     // create a log entry for the action done
     AdministratorLog::create(['admin_id' => $adminID, 'admin_name' => $adminName, 'user' => $data['email'], 'category' => 'Create', 'action' => 'Created user account for ' . $data['email']]);
     Mail::send('emails.credentials', ['email' => $request->input('email'), 'password' => $password], function ($message) use($data) {
         // send an email containing the credential of the user to the input email
         $message->to($data['email'])->subject('Breeder Credentials for Swine E-Commerce PH');
     });
     return Redirect::back()->withMessage('User Created!');
     // redirect to the page and display a toast notification that a user is created
 }