Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jeevansrivastava/127312ebb7442c26fb122a075951f1fb to your computer and use it in GitHub Desktop.

Select an option

Save jeevansrivastava/127312ebb7442c26fb122a075951f1fb to your computer and use it in GitHub Desktop.
compare logic
public function compare()
{
session(['orderid' => app('request')->input('id')]);
$order = Order::find(session('orderid'));
$subscribed = false;
$ordersObj = OrderDetail::with('product')->whereOrderId($order->id)->get();
$orders = $ordersObj->toArray();
$orderedProductsCategories = array_column(array_column($orders, 'product'), 'category_id');
if($order->status == 'order_placed') {
return redirect()->route('order.index')->with('error', \Lang::get('messages.can_not_be_changed'));
} else {
//1. is_active port
$port = Port::whereId($order->delivery_port)->whereIsActive(1)->first();
if(!is_null($port)) {
//1. get company id of order_by user
$companyId = CompanyEmployee::whereUserId($order->ordered_by)->first()->company_id;
//2. get company categories associated with this company
$companyCategoryIds = CompanyVendorCategory::whereTypeId($companyId)->whereType('company')
->pluck('category_id')->toArray();
//3. get subscribed vendors of this company
$subscribedVendorArray = CompanyVendor::whereCompanyId($companyId)->pluck('vendor_id')->toArray();
if (count($subscribedVendorArray) > 0) {
//3.1 if have subscribed vendor ids get them in array
$vendorCategoryIds = CompanyVendorCategory::whereIn('type_id', $subscribedVendorArray)
->whereType('vendor')->groupBy('type_id')->pluck('type_id');
//3.2 set subscribed to true
$subscribed = true;
} else {
//3.1 get vendors category ids
$vendorCategoryIds = CompanyVendorCategory::whereType('vendor')
->groupBy('type_id')->pluck('type_id');
}
//4. get vendor ids array
$vendor = [];
foreach ($vendorCategoryIds as $vendorId) {
$vendorCategories = CompanyVendorCategory::whereTypeId($vendorId)->whereType('vendor')
->pluck('category_id')->toArray();
if ($this->compareCategories($companyCategoryIds, $vendorCategories, $orderedProductsCategories)) {
array_push($vendor, $vendorId);
}
}
//5. if active port then get vendors that delivers the order on request port while creating order
$vendorIds = CompanyVendorPort::wherePortId($order->delivery_port)
->whereType('Vendor')
// ->whereIn('company_or_vendor', $vendor)
->pluck('company_or_vendor')
->toArray();
//6. get active vendors only if available
if (count($vendorIds) > 0) {
$vendorIds = Vendor::whereIn('id', $vendorIds)->whereIsActive(1)->pluck('name', 'id')->toArray();
if(count($vendorIds) > 0)
{
// Compare Vendor
$vendorIds= $this->compareVendor($vendorIds, $order->id,$order->delivery_port);
//7. limit vendors to 5
$vendorName = array_slice($vendorIds, 0, 3, true);
} else {
return redirect()->back()->with('error', \Lang::get('views.vendor_na_or_deactivated'));
}
} else {
return redirect()->back()->with('error', \Lang::get('views.vendor_na_or_deactivated'));
}
} else {
return redirect()->back()->with('error', \Lang::get('views.order_delivery_port'));
}
$orderAddress = Order::with('port')->whereId($order->id)->first();
if(\Auth::user()->hasRole('super-admin')){
$companyId = CompanyEmployee::whereUserId($order->ordered_by)->first()->company_id;
}else{
$companyId = CompanyEmployee::whereUserId(\Auth::User()->id)->first()->company_id;
}
$company = Company::whereId($companyId)->first();
$orders = $ordersObj;
return view('admin.order.compare', compact('vendorName', 'orders', 'orderAddress', 'company'
, 'subscribed', 'port'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment