Advertisement
X1ucifer

Untitled

Nov 18th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 71.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Repositories\Admin\Product;
  4.  
  5. use App\Models\Campaign;
  6. use App\Models\CampaignProduct;
  7. use App\Models\Category;
  8. use App\Models\CompareProduct;
  9. use App\Models\Product;
  10. use App\Models\ProductLanguage;
  11. use App\Models\ProductStock;
  12. use App\Models\ProductView;
  13. use App\Models\Search;
  14. use App\Repositories\Interfaces\Admin\Product\ProductInterface;
  15. use App\Repositories\Interfaces\Admin\Product\ProductLanguageInterface;
  16. use App\Repositories\Interfaces\Admin\SellerInterface;
  17. use App\Traits\ImageTrait;
  18. use App\Traits\RandomStringTrait;
  19. use App\Traits\SlugTrait;
  20. use Carbon\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Str;
  23. use Sentinel;
  24. use Illuminate\Support\Facades\Log;
  25.  
  26.  
  27. class ProductRepository implements ProductInterface
  28. {
  29. use SlugTrait, ImageTrait, RandomStringTrait;
  30.  
  31. protected $productLang;
  32. protected $seller;
  33.  
  34. public function __construct(ProductLanguageInterface $productLang, SellerInterface $seller)
  35. {
  36. $this->productLang = $productLang;
  37. $this->seller = $seller;
  38. }
  39.  
  40. public function get($id)
  41. {
  42. return Product::CheckSellerSystem()->find($id);
  43. }
  44.  
  45. public function productFilter($request)
  46. {
  47.  
  48.  
  49. // $data = Product::with('productLanguages', 'createdBy')
  50. // ->get();
  51.  
  52. // $name = $data->getTranslation('name',apiLanguage($request->lang));
  53. // if(!empty($request["category_id"]) ){
  54. // $products = Product::with('stock','productLanguages')->where('category_id',$request["category_id"])->get();
  55. // }
  56.  
  57.  
  58. if($request["filterByname"] == 1){
  59.  
  60. if(!empty($request["category_id"]) ){
  61. $products = Product::with('stock','productLanguages')->where('slug', 'like', '%' . $request["title"] . '%')->where('category_id',$request["category_id"])->get();
  62. }else{
  63. $products = Product::with('stock','productLanguages')->where('slug', 'like', '%' . $request["title"] . '%')->get();
  64. }
  65.  
  66.  
  67. // $products = Product::with('stock','productLanguages')->where('slug', 'like', '%' . $request["title"] . '%')->get();
  68.  
  69. }elseif($request["filterByname"] == 0){
  70.  
  71. if(!empty($request["category_id"]) ){
  72. $products = Product::with('stock','productLanguages')->where('category_id',$request["category_id"])->get();
  73. }else{
  74. $products = Product::with('stock','productLanguages')->get();
  75. }
  76.  
  77. }elseif($request["filterByname"] == 2){
  78.  
  79. if(!empty($request["category_id"]) ){
  80. $products = Product::with('stock','productLanguages')->where('category_id',$request["category_id"])->get();
  81. }else{
  82. $products = Product::with('stock','productLanguages')->get();
  83. }
  84. }
  85.  
  86.  
  87.  
  88.  
  89. // elseif($request["filterByname"] == null){
  90. // $dataId = Category::where('parent_id',$request["category_id"])->where('ordering',$request["ordering"])
  91. // ->select('id')->get();
  92. // $products = Product::withAvg('reviews','rating')->withCount('reviews')->whereIn('category_id',$dataId)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  93. // ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  94. // ->get();
  95. // }
  96.  
  97. // if($request["mainFilter"] == 1){
  98.  
  99. // $category_ids = \App\Utility\CategoryUtility::getMyAllChildIds($request["category_id"]);
  100. // $category_ids[] = (int)$request["category_id"];
  101.  
  102.  
  103. // // $products = Product::withAvg('reviews','rating')->withCount('reviews')->whereIn('category_id',$category_ids)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  104. // // ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  105. // // ->get();
  106.  
  107. // if(empty($request["ordering"])){
  108.  
  109.  
  110. // $products = Product::withAvg('reviews','rating')->withCount('reviews')->whereIn('category_id',$category_ids)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  111. // ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  112. // ->get();
  113. // }else{
  114.  
  115.  
  116. // }
  117.  
  118. // }
  119.  
  120.  
  121.  
  122. // Log::info('Showing the user profile for user: '. json_encode($request["filterByname"] ));
  123.  
  124. return $products;
  125. }
  126.  
  127. public function productBydifference($category_id){
  128. return Product::where('category_id',$category_id)->get();
  129. }
  130.  
  131. public function getByLang($id, $lang)
  132. {
  133. if ($lang == null):
  134. $product_lang = ProductLanguage::with('product')
  135. ->where('lang', 'en')
  136. ->where('product_id', $id)
  137. ->when(settingHelper('seller_system') != 1, function ($q) {
  138. $q->whereHas('product', function ($qu) {
  139. $qu->where('user_id', 1);
  140. });
  141. })->first();
  142. else:
  143. $product_lang = ProductLanguage::with('product')
  144. ->where('lang', $lang)
  145. ->where('product_id', $id)
  146. ->when(settingHelper('seller_system') != 1, function ($q) {
  147. $q->whereHas('product', function ($qu) {
  148. $qu->where('user_id', 1);
  149. });
  150. })->first();
  151. if (blank($product_lang)):
  152. $product_lang = ProductLanguage::with('product')
  153. ->where('lang', 'en')
  154. ->where('product_id', $id)
  155. ->when(settingHelper('seller_system') != 1, function ($q) {
  156. $q->whereHas('product', function ($qu) {
  157. $qu->where('user_id', 1);
  158. });
  159. })->first();
  160. $product_lang['translation_null'] = 'not-found';
  161. endif;
  162. endif;
  163.  
  164. return $product_lang;
  165. }
  166.  
  167. public function all()
  168. {
  169. return Product::with('productLanguages', 'createdBy')
  170. ->when(!addon_is_activated('wholesale'), function ($q) {
  171. $q->where('is_wholesale', 0);
  172. })->CheckSellerSystem()->latest();
  173. }
  174.  
  175. public function paginate($request, $status, $limit, $product_for = null)
  176. {
  177. $products = Product::with('createdBy', 'productLanguages','user','stock')
  178. ->where('is_deleted', 0)
  179. ->CheckSellerSystem()
  180. ->whereHas('productLanguages', function ($q) use ($request) {
  181. $q->where('name', 'like', '%' . $request->q . '%');
  182. $q->orWhere('tags', 'like', '%' . $request->q . '%');
  183. $q->orwhere('description', 'like', '%' . $request->q . '%');
  184. $q->orwhere('short_description', 'like', '%' . $request->q . '%');
  185. $q->orwhere('meta_title', 'like', '%' . $request->q . '%');
  186. $q->orwhere('meta_description', 'like', '%' . $request->q . '%');
  187. $q->orwhere('meta_keywords', 'like', '%' . $request->q . '%');
  188. })
  189. ->when($status == 'trash' && $status != null, function ($query) {
  190. $query->onlyTrashed();
  191. })
  192. ->when($status != 'trash' && $status != 'pending' && $status != null, function ($query) use ($status) {
  193. $query->where('status', $status);
  194. })
  195. ->when($status == 'pending', function ($query) {
  196. $query->where('is_approved', 0);
  197. })
  198. ->when($request->sq != null, function ($q) use ($request) {
  199. $q->where('user_id', $request->sq);
  200. })
  201. ->when(!addon_is_activated('wholesale'), function ($q) {
  202. $q->where('is_wholesale', 0);
  203. })
  204. ->when($product_for != '', function ($q) use ($product_for) {
  205. $q->when($product_for == 'admin', function ($for) {
  206. $for->where('user_id', 1);
  207. });
  208. $q->when($product_for == 'seller', function ($for) {
  209. $for->where('user_id', '!=', 1);
  210. });
  211. $q->when($product_for == 'digital', function ($for) {
  212. $for->where('is_digital', 1);
  213. });
  214. $q->when($product_for == 'catalog', function ($for) {
  215. $for->where('is_catalog', 1);
  216. });
  217. $q->when($product_for == 'classified', function ($for) {
  218. $for->where('is_classified', 1);
  219. });
  220. $q->when($product_for == 'wholesale', function ($for) {
  221. $for->where('is_wholesale', 1);
  222. });
  223. })
  224. ->when($request->sl != null, function ($query) use ($request) {
  225. $seller = $this->seller->getSeller($request->sl);
  226. $query->where('user_id', $seller->user_id);
  227. })
  228. ->when($request->c != null, function ($q) use ($request) {
  229. $q->where('category_id', $request->c);
  230. });
  231. $sorting = $request->s;
  232. switch ($sorting) {
  233. case 'latest_on_top':
  234. $products->orderByDesc('id');
  235. break;
  236. case 'oldest_on_top':
  237. $products->orderBy('id');
  238. break;
  239. case 'sale_high':
  240. $products->orderByDesc('total_sale');
  241. break;
  242. case 'sale_low':
  243. $products->orderBy('total_sale');
  244. break;
  245. case 'rating_high':
  246. $products->orderByDesc('rating');
  247. break;
  248. case 'rating_low':
  249. $products->orderBy('rating');
  250. break;
  251. default:
  252. $products->orderBy('id', 'desc');
  253. break;
  254. }
  255.  
  256. Log::info("demo-->". json_encode( $products->paginate($limit)) );
  257.  
  258. return $products->paginate($limit);
  259. }
  260.  
  261. public function stockProduct($request)
  262. {
  263. $stocks = ProductStock::query();
  264.  
  265. if ($request->p && $request->q) {
  266. $stocks->whereHas('product.productLanguages', function ($p) use ($request) {
  267. $p->where('name', 'like', '%' . $request->p . '%');
  268. $p->orWhere('name', 'like', '%' . $request->q . '%');
  269. $p->orWhere('barcode', 'like', '%' . $request->p . '%');
  270. $p->orWhere('barcode', 'like', '%' . $request->q . '%');
  271. });
  272. }
  273. else{
  274. if ($request->q) {
  275. $stocks->whereHas('product.productLanguages', function ($q) use ($request) {
  276. $q->where('name', 'like', '%' . $request->q . '%');
  277. $q->orWhere('barcode', 'like', '%' . $request->q . '%');
  278. });
  279. }
  280. if ($request->p) {
  281. $stocks->whereHas('product.productLanguages', function ($p) use ($request) {
  282. $p->where('name', 'like', '%' . $request->p . '%');
  283. $p->orWhere('barcode', 'like', '%' . $request->p . '%');
  284. });
  285. }
  286. }
  287.  
  288. if ($request->category_id) {
  289. $category_ids = \App\Utility\CategoryUtility::getMyAllChildIds($request->category_id);
  290. $category_ids[] = (int)$request->category_id;
  291.  
  292. $stocks->whereHas('product', function ($q) use ($category_ids) {
  293. $q->whereIn('category_id', $category_ids);
  294. });
  295. }
  296.  
  297. if ($request->brand_id) {
  298. $stocks->whereHas('product', function ($q) use ($request) {
  299. $q->where('brand_id', $request->brand_id);
  300. });
  301. }
  302. return $stocks->with('product.productLanguages')
  303. ->whereHas('product',function ($query) use ($request){
  304. $query->when(Sentinel::getUser()->user_type == 'seller', function ($q){
  305. $q->where('user_id',authId())->UserCheck()->IsWholesale()->ProductPublished();
  306. });
  307. $query->when(Sentinel::getUser()->user_type == 'admin', function ($q){
  308. $q->where('user_id',1)->UserCheck()->IsWholesale()->ProductPublished();
  309. });
  310. $query->when(Sentinel::getUser()->user_type == 'staff', function ($q){
  311. $q->where('user_id',1)->UserCheck()->IsWholesale()->ProductPublished();
  312. });
  313.  
  314. })->where('current_stock', '>', '0')->latest()->paginate(16);
  315. }
  316.  
  317. public function store($request)
  318. {
  319.  
  320.  
  321. DB::BeginTransaction();
  322.  
  323.  
  324. try {
  325. $product = new Product();
  326.  
  327. Log::info('Showing: '. json_encode($request ));
  328.  
  329. $product->description_images = $this->saveMultipleImage($request->description_images,$product);
  330. if ($request->thumbnail):
  331. $files = $this->getImageWithRecommendedSize($request->thumbnail,190,230);
  332. if ($files):
  333. $product->thumbnail = $files;
  334. $product->thumbnail_id = $request->thumbnail;
  335. else:
  336. $product->thumbnail = [];
  337. endif;
  338. else:
  339. $product->thumbnail = [];
  340. endif;
  341. if ($request->images != ''):
  342. $selected_images = explode(',', $request->images);
  343. $images = array();
  344. foreach ($selected_images as $key=>$image):
  345. $files = $this->getImage($image);
  346. if($files):
  347. array_push($images, $files);
  348. else:
  349. unset($selected_images[$key]);
  350. endif;
  351. endforeach;
  352. $product->images = $images;
  353. $product->image_ids = implode(',',$selected_images);
  354. else:
  355. $product->images = [];
  356. endif;
  357.  
  358. // if ($request->childcat_ids != ''):
  359. // $cat_item = explode(',', $request->childcat_ids);
  360. // $cat_product = array();
  361.  
  362. // else:
  363. // $product->product_difference = [];
  364. // endif;
  365. $product->slug = $this->getSlug($request->name, $request->slug);
  366. $product->category_id = $request->category != '' ? $request->category : null;
  367. $product->product_difference = $request->childcat_ids ?? '';
  368. $product->brand_id = $request->brand != '' ? $request->brand : null;
  369. $product->created_by = authUser($request)->id;
  370. if (authUser($request)->user_type != 'seller'):
  371. $product->user_id = 1;
  372. $product->is_approved = 1;
  373. else:
  374. $product->user_id = authUser($request)->id;
  375. if (settingHelper('seller_product_auto_approve') == 1):
  376. $product->is_approved = 1;
  377. endif;
  378. endif;
  379. $product->status = $request->status;
  380. $product->minimum_order_quantity = $request->minimum_order_quantity != '' ? $request->minimum_order_quantity : 1;
  381. $product->barcode = $request->barcode != '' ? $request->barcode : $this->generate_random_string(16, 'upper');
  382.  
  383. if ($request->video_provider != '' && $request->video_url != ''):
  384. $product->video_provider = $request->video_provider;
  385. $product->video_url = $request->video_url;
  386. endif;
  387.  
  388. $product->price = priceFormatUpdate($request->price ,settingHelper('default_currency'));
  389.  
  390. if ($request->special_discount_type != ''):
  391. $product->special_discount_type = $request->special_discount_type;
  392. $product->special_discount = priceFormatUpdate($request->special_discount,settingHelper('default_currency'));
  393.  
  394. $dates = explode(" - ", $request->special_discount_period);
  395.  
  396. $product->special_discount_start = Carbon::createFromFormat('m-d-Y g:ia', $dates[0]);
  397. $product->special_discount_end = Carbon::createFromFormat('m-d-Y g:ia', $dates[1]);
  398. endif;
  399. if ($request->has_variant == 1):
  400. $product->has_variant = 1;
  401. $product->current_stock = 0;
  402. else:
  403. $product->current_stock = $request->current_stock != '' ? $request->current_stock : 0;
  404. endif;
  405.  
  406. $product->colors = $request->colors ?? [];
  407. $product->attribute_sets = $request->attribute_sets ?? [];
  408. if ($request->has('vat_taxes')):
  409. $product->vat_taxes = implode(',', $request->vat_taxes);
  410. endif;
  411.  
  412. if ($request->meta_image != ''):
  413. $files = $this->getImageWithRecommendedSize($request->meta_image, 1200, 630);
  414. if ($files):
  415. $product->meta_image = $files;
  416. $product->meta_image_id = $request->thumbnail;
  417. else:
  418. $product->meta_image = $product->thumbnail;
  419. $product->meta_image_id = $product->thumbnail_id;
  420. endif;
  421. else:
  422. $product->meta_image = $product->thumbnail;
  423. $product->meta_image_id = $product->thumbnail_id;
  424. endif;
  425.  
  426. if ($request->low_stock_to_notify != ''):
  427. $product->stock_notification = 1;
  428. $product->low_stock_to_notify = $request->low_stock_to_notify;
  429. endif;
  430. $product->stock_visibility = $request->stock_visibility ?? 'hide_stock';
  431. if ($request->has('cash_on_delivery')):
  432. $product->cash_on_delivery = 1;
  433. endif;
  434. if ($request->has('is_refundable')):
  435. $product->is_refundable = 1;
  436. endif;
  437. if ($request->has('is_catalog')):
  438. $product->is_catalog = 1;
  439. endif;
  440. if ($request->has('is_featured')):
  441. $product->is_featured = 1;
  442. endif;
  443. //digital product will not get delivered
  444. if ($request->has('is_digital')):
  445. $product->is_digital = 1;
  446. $product->minimum_order_quantity = 1;
  447. if ($request->product_file):
  448. $file = $this->getFile($request->product_file);
  449. if ($file):
  450. $product->product_file = $file;
  451. $product->product_file_id = $request->product_file;
  452. else:
  453. $product->product_file = [];
  454. endif;
  455. else:
  456. $product->product_file = [];
  457. endif;
  458. else:
  459. $product->estimated_shipping_days = $request->estimated_shipping_days != '' ? $request->estimated_shipping_days : 0;
  460. endif;
  461.  
  462. if ($request->has('todays_deal')):
  463. $product->todays_deal = $request->todays_deal;
  464. endif;
  465.  
  466. //cost
  467. if ($request->has('shipping_type')) :
  468. if ($request->shipping_type == 'free_shipping'):
  469. $product->shipping_fee = 0.00;
  470. elseif ($request->shipping_type == 'flat_rate'):
  471. $product->shipping_fee = $request->shipping_fee != '' ? priceFormatUpdate($request->shipping_fee,settingHelper('default_currency')) : 0.00;
  472. endif;
  473. $product->shipping_type = $request->shipping_type;
  474.  
  475. if ($request->has('shipping_fee_depend_on_quantity')):
  476. $product->shipping_fee_depend_on_quantity = $request->shipping_fee_depend_on_quantity;
  477. endif;
  478. endif;
  479.  
  480. if ($request->has('is_classified')):
  481. $product->is_classified = 1;
  482.  
  483. $contact_details['contact_name'] = $request->contact_name;
  484. $contact_details['phone_no'] = $request->phone_no;
  485. $contact_details['email'] = $request->email;
  486. $contact_details['address'] = $request->address;
  487. $contact_details['others'] = $request->others;
  488. $product->contact_info = $contact_details;
  489. endif;
  490.  
  491. $product->save();
  492.  
  493. $request['product_id'] = $product->id;
  494. if (!isset($request->lang)) :
  495. $request['lang'] = 'en';
  496. endif;
  497.  
  498. $this->productLang->store($request);
  499.  
  500. if ($request->campaign) :
  501. $campaign_product = new CampaignProduct();
  502. $campaign_product->campaign_id = $request->campaign;
  503. $campaign_product->product_id = $product->id;
  504. $campaign_product->discount = priceFormatUpdate($request->campaign_discount,settingHelper('default_currency'));
  505. $campaign_product->discount_type = $request->campaign_discount_type;
  506. $campaign_product->save();
  507.  
  508. $campaign = Campaign::find($request->campaign);
  509. $product->special_discount_type = $request->campaign_discount_type;
  510. $product->special_discount = priceFormatUpdate($request->campaign_discount,settingHelper('default_currency'));
  511. $product->special_discount_start = $campaign->start_date;
  512. $product->special_discount_end = $campaign->end_date;
  513. endif;
  514. if ($product->has_variant == 1 && $request->has('variant_name')):
  515. $total_stock = 0;
  516. foreach ($request->variant_name as $key => $variant):
  517. if ($request['variant_name'][$key]):
  518. $product_stock = new ProductStock();
  519. $product_stock->product_id = $product->id;
  520. $product_stock->name = $request['variant_name'][$key];
  521. $product_stock->variant_ids = $request['variant_ids'][$key];
  522. $product_stock->sku = $request['variant_sku'][$key];
  523. $product_stock->current_stock = $request['variant_stock'][$key];
  524. $product_stock->price = priceFormatUpdate($request['variant_price'][$key],settingHelper('default_currency'));
  525. if ($request['variant_image'][$key] != ''):
  526. $files = $this->getImage($request['variant_image'][$key]);
  527. if ($files):
  528. $product_stock->image = $files;
  529. $product_stock->image_id = $request['variant_image'][$key];
  530. else:
  531. $product_stock->image = [];
  532. endif;
  533. else:
  534. $product_stock->image = [];
  535. endif;
  536. $total_stock += $product_stock->current_stock;
  537. $product_stock->save();
  538. endif;
  539.  
  540. $selected_variants = array();
  541. $selected_variants_ids = array();
  542.  
  543. if ($request->has('attribute_sets')):
  544. foreach ($request->attribute_sets as $attribute_set):
  545. $attribute_values = 'attribute_values_' . $attribute_set;
  546. $values = array();
  547. if ($request->has($attribute_values)):
  548. foreach ($request[$attribute_values] as $value):
  549. array_push($values, $value);
  550. array_push($selected_variants_ids, $value);
  551. endforeach;
  552. $selected_variants[$attribute_set] = $values;
  553. endif;
  554. endforeach;
  555. endif;
  556. $product->selected_variants = $selected_variants;
  557. $product->selected_variants_ids = $selected_variants_ids;
  558. endforeach;
  559.  
  560. $product->current_stock = $total_stock;
  561. $product->save();
  562. else:
  563. $product_stock = new ProductStock();
  564. $product_stock->product_id = $product->id;
  565. $product_stock->sku = $request->sku;
  566. $product_stock->name = '';
  567. $product_stock->price = priceFormatUpdate($product->price,settingHelper('default_currency'));
  568. $product_stock->current_stock = $product->current_stock;
  569. $product_stock->save();
  570. endif;
  571.  
  572. DB::commit();
  573. return true;
  574. } catch (\Exception $e) {
  575. DB::rollback();
  576. dd($e);
  577. return false;
  578. }
  579.  
  580. }
  581.  
  582. public function update($request)
  583. {
  584. DB::BeginTransaction();
  585. try {
  586. $product = $this->get($request->id);
  587. $product->description_images = $this->saveMultipleImage($request->description_images,$product);
  588.  
  589. if ($request->thumbnail):
  590. $files = $this->getImageWithRecommendedSize($request->thumbnail, 190, 230);
  591. if ($files):
  592. $product->thumbnail = $files;
  593. $product->thumbnail_id = $request->thumbnail;
  594. else:
  595. $product->thumbnail = [];
  596. $product->thumbnail_id = null;
  597. endif;
  598. else:
  599. $product->thumbnail = [];
  600. $product->thumbnail_id = null;
  601. endif;
  602.  
  603. if ($request->images != ''):
  604. $selected_images = explode(',', $request->images);
  605. $images = array();
  606. foreach ($selected_images as $key => $image):
  607. $files = $this->getImage($image);
  608. if ($files):
  609. array_push($images, $files);
  610. else:
  611. unset($selected_images[$key]);
  612. endif;
  613. endforeach;
  614. $product->images = $images;
  615. $product->image_ids = implode(',', $selected_images);
  616. else:
  617. $product->images = [];
  618. $product->image_ids = null;
  619. endif;
  620.  
  621. $product->slug = $this->getSlug($request->name, $request->slug);
  622. $product->category_id = $request->category != '' ? $request->category : null;
  623. $product->brand_id = $request->brand != '' ? $request->brand : null;
  624. // $product->created_by = Sentinel::getUser()->id;
  625.  
  626. $product->minimum_order_quantity = $request->minimum_order_quantity != '' ? $request->minimum_order_quantity : 1;
  627. $product->barcode = $request->barcode != '' ? $request->barcode : $this->generate_random_string(16, 'upper');
  628.  
  629. if ($request->video_provider != '' && $request->video_url != ''):
  630. $product->video_provider = $request->video_provider;
  631. $product->video_url = $request->video_url;
  632. else:
  633. $product->video_provider = '';
  634. $product->video_url = '';
  635. endif;
  636.  
  637. $product->price = priceFormatUpdate($request->price,settingHelper('default_currency'));
  638.  
  639. if ($request->special_discount_type != ''):
  640. $product->special_discount_type = $request->special_discount_type;
  641. $product->special_discount = priceFormatUpdate($request->special_discount,settingHelper('default_currency'));
  642.  
  643. $dates = explode(" - ", $request->special_discount_period);
  644.  
  645. $product->special_discount_start = Carbon::createFromFormat('m-d-Y g:ia', $dates[0]);
  646. $product->special_discount_end = Carbon::createFromFormat('m-d-Y g:ia', $dates[1]);
  647. else :
  648. $product->special_discount_type = null;
  649. $product->special_discount = 0.00;
  650.  
  651. $product->special_discount_start = null;
  652. $product->special_discount_end = null;
  653. endif;
  654. if ($request->has_variant == 1):
  655. $product->has_variant = 1;
  656. $product->current_stock = 0;
  657. $product->colors = $request->colors ?? [];
  658. $product->attribute_sets = $request->attribute_sets ?? [];
  659. else:
  660. $product->has_variant = 0;
  661. $product->colors = [];
  662. $product->attribute_sets = [];
  663. $product->current_stock = $request->current_stock != '' ? $request->current_stock : 0;
  664. endif;
  665.  
  666.  
  667. if ($request->has('vat_taxes')):
  668. $product->vat_taxes = implode(',', $request->vat_taxes);
  669. else:
  670. $product->vat_taxes = '';
  671. endif;
  672.  
  673. if ($request->meta_image != ''):
  674. $files = $this->getImageWithRecommendedSize($request->meta_image, 1200, 630);
  675. if ($files):
  676. $product->meta_image = $files;
  677. $product->meta_image_id = $request->thumbnail;
  678. else:
  679. $product->meta_image = $product->thumbnail;
  680. $product->meta_image_id = $product->thumbnail_id;
  681. endif;
  682. else:
  683. $product->meta_image = $product->thumbnail;
  684. $product->meta_image_id = $product->thumbnail_id;
  685. endif;
  686.  
  687. if ($request->low_stock_to_notify != ''):
  688. $product->stock_notification = 1;
  689. $product->low_stock_to_notify = $request->low_stock_to_notify;
  690. endif;
  691. $product->stock_visibility = $request->stock_visibility ?? 'hide_stock';
  692. if ($request->has('cash_on_delivery')):
  693. $product->cash_on_delivery = 1;
  694. else:
  695. $product->cash_on_delivery = 0;
  696. endif;
  697. if ($request->has('is_refundable')):
  698. $product->is_refundable = 1;
  699. else:
  700. $product->is_refundable = 0;
  701. endif;
  702. if ($request->has('is_catalog')):
  703. $product->is_catalog = 1;
  704. $product->external_link = $request->external_link;
  705. else:
  706. $product->is_catalog = 0;
  707. endif;
  708. if ($request->has('is_featured')):
  709. $product->is_featured = 1;
  710. else:
  711. $product->is_featured = 0;
  712. endif;
  713. //digital product will not get delivered
  714. if ($request->has('is_digital')):
  715. $product->is_digital = 1;
  716. $product->minimum_order_quantity = 1;
  717. if ($request->product_file):
  718. $file = $this->getFile($request->product_file);
  719. if ($file):
  720. $product->product_file = $file;
  721. $product->product_file_id = $request->product_file;
  722. else:
  723. $product->product_file = [];
  724. endif;
  725. else:
  726. $product->product_file = [];
  727. endif;
  728. else:
  729. $product->is_digital = 0;
  730. $product->estimated_shipping_days = $request->estimated_shipping_days != '' ? $request->estimated_shipping_days : 0;
  731. endif;
  732.  
  733. if ($request->has('todays_deal')):
  734. $product->todays_deal = $request->todays_deal;
  735. else:
  736. $product->todays_deal = 0;
  737. endif;
  738.  
  739. //cost
  740. if ($request->has('shipping_type')) :
  741. if ($request->shipping_type == 'free_shipping'):
  742. $product->shipping_fee = 0.00;
  743. elseif ($request->shipping_type == 'flat_rate'):
  744. $product->shipping_fee = $request->shipping_fee != '' ? priceFormatUpdate($request->shipping_fee,settingHelper('default_currency')) : 0.00;
  745. endif;
  746. $product->shipping_type = $request->shipping_type;
  747.  
  748. if ($request->has('shipping_fee_depend_on_quantity')):
  749. $product->shipping_fee_depend_on_quantity = $request->shipping_fee_depend_on_quantity;
  750. endif;
  751. endif;
  752. if ($request->has('is_classified')):
  753. $product->is_classified = 1;
  754.  
  755. $contact_details['contact_name'] = $request->contact_name;
  756. $contact_details['phone_no'] = $request->phone_no;
  757. $contact_details['email'] = $request->email;
  758. $contact_details['address'] = $request->address;
  759. $contact_details['others'] = $request->others;
  760.  
  761. $product->contact_info = $contact_details;
  762. endif;
  763. $product->save();
  764.  
  765. $request['product_id'] = $product->id;
  766. if (!isset($request->lang)) :
  767. $request['lang'] = 'en';
  768. endif;
  769.  
  770. $this->productLang->store($request);
  771.  
  772. if ($request->campaign) :
  773. if ($product->campaign):
  774. $campaign_product = CampaignProduct::find($product->campaign->id);
  775. else :
  776. $campaign_product = new CampaignProduct();
  777. endif;
  778. $campaign_product->campaign_id = $request->campaign;
  779. $campaign_product->product_id = $product->id;
  780. $campaign_product->discount = priceFormatUpdate($request->campaign_discount,settingHelper('default_currency'));
  781. $campaign_product->discount_type = $request->campaign_discount_type;
  782. $campaign_product->save();
  783.  
  784. $campaign = Campaign::find($request->campaign);
  785. $product->special_discount_type = $request->campaign_discount_type;
  786. $product->special_discount = priceFormatUpdate($request->campaign_discount ,settingHelper('default_currency'));
  787. $product->special_discount_start = $campaign->start_date;
  788. $product->special_discount_end = $campaign->end_date;
  789. endif;
  790. foreach ($product->stock as $key => $stock):
  791. $stock->delete();
  792. endforeach;
  793. if ($product->has_variant == 1 && $request->has('variant_name')):
  794. $total_stock = 0;
  795. foreach ($request->variant_name as $key => $variant):
  796. if ($request['variant_name'][$key]):
  797. $product_stock = ProductStock::where('product_id', $product->id)->where('name', $request['variant_name'][$key])->first();
  798. if ($product_stock == ''):
  799. $product_stock = new ProductStock();
  800. endif;
  801. $product_stock->product_id = $product->id;
  802. $product_stock->variant_ids = $request['variant_ids'][$key];
  803. $product_stock->name = $request['variant_name'][$key];
  804. $product_stock->sku = $request['variant_sku'][$key];
  805. $product_stock->current_stock = $request['variant_stock'][$key];
  806. $product_stock->price = priceFormatUpdate($request['variant_price'][$key],settingHelper('default_currency'));
  807. if ($request['variant_image'][$key] != ''):
  808. $files = $this->getImage($request['variant_image'][$key]);
  809. if ($files):
  810. $product_stock->image = $files;
  811. $product_stock->image_id = $request['variant_image'][$key];
  812. else:
  813. $product_stock->image = [];
  814. $product_stock->image_id = null;
  815. endif;
  816. else:
  817. $product_stock->image = [];
  818. $product_stock->image_id = null;
  819. endif;
  820. $total_stock += $product_stock->current_stock;
  821. $product_stock->save();
  822. endif;
  823.  
  824. $selected_variants = array();
  825. $selected_variants_ids = array();
  826.  
  827. if ($request->has('attribute_sets')):
  828. foreach ($request->attribute_sets as $attribute_set):
  829. $attribute_values = 'attribute_values_' . $attribute_set;
  830. $values = array();
  831. if ($request->has($attribute_values)):
  832. foreach ($request[$attribute_values] as $value):
  833. array_push($values, $value);
  834. array_push($selected_variants_ids, $value);
  835. endforeach;
  836. $selected_variants[$attribute_set] = $values;
  837. endif;
  838. endforeach;
  839. endif;
  840. $product->selected_variants = $selected_variants;
  841. $product->selected_variants_ids = $selected_variants_ids;
  842. endforeach;
  843.  
  844. $product->current_stock = $total_stock;
  845. $product->save();
  846. else:
  847. $product_stock = $product->stock->first();
  848. if ($product_stock == ''):
  849. $product_stock = new ProductStock();
  850. endif;
  851. $product_stock->product_id = $product->id;
  852. $product_stock->sku = $request->sku;
  853. $product_stock->name = '';
  854. $product_stock->price = priceFormatUpdate($product->price ,settingHelper('default_currency'));
  855. $product_stock->image = [];
  856. $product_stock->image_id = null;
  857. $product_stock->current_stock = $product->current_stock;
  858. $product_stock->save();
  859. endif;
  860.  
  861. DB::commit();
  862. return true;
  863. } catch (\Exception $e) {
  864. DB::rollback();
  865. return false;
  866. }
  867. }
  868.  
  869. public function productView($request)
  870. {
  871. DB::BeginTransaction();
  872. try {
  873. $product = Product::where('slug', $request['product_slug'])->CheckSellerSystem()->first();
  874.  
  875. $user_id = authId();
  876. ProductView::where('product_id', $product->id)->delete();
  877.  
  878. $view = new ProductView();
  879. $view->product_id = $product->id;
  880. $view->user_id = $user_id;
  881. $view->save();
  882.  
  883. DB::commit();
  884. return true;
  885. } catch (\Exception $e) {
  886. DB::rollback();
  887. return false;
  888. }
  889. }
  890.  
  891. public function statusChange($request)
  892. {
  893. DB::beginTransaction();
  894. try {
  895. $product = $this->get($request['id']);
  896. if ($request['change_for'] != 'status') :
  897. $product[$request['change_for']] = $request['status'];
  898. else:
  899. $product->status = $request['status'] == '1' ? 'published' : 'unpublished';
  900. endif;
  901. $product->save();
  902.  
  903. DB::commit();
  904. return true;
  905. } catch (\Exception $e) {
  906. DB::rollback();
  907. return false;
  908. }
  909. }
  910.  
  911. public function variantStore($request, $product_id)
  912. {
  913.  
  914. }
  915.  
  916. public function restore($id)
  917. {
  918. DB::beginTransaction();
  919. try {
  920. $product = Product::withTrashed()->find($id);
  921. $product->status = 'unpublished';
  922. $product->is_deleted = 0;
  923. $product->save();
  924.  
  925. Product::withTrashed()->find($id)->restore();
  926. DB::commit();
  927. return true;
  928. } catch (\Exception $e) {
  929. DB::rollback();
  930. return false;
  931. }
  932. }
  933.  
  934. public function productStatusChange($status, $id)
  935. {
  936. DB::beginTransaction();
  937. try {
  938. $product = $this->get($id);
  939. if ($status == 'pending'):
  940. $product->is_approved = 0;
  941. elseif ('approve'):
  942. $product->is_approved = 1;
  943. endif;
  944. $product->save();
  945.  
  946. DB::commit();
  947. return true;
  948. } catch (\Exception $e) {
  949. DB::rollback();
  950. return false;
  951. }
  952. }
  953.  
  954. //for api
  955. public function todayDeals()
  956. {
  957. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->selectRaw('id,price,special_discount,minimum_order_quantity,current_stock,special_discount_type,special_discount_start,special_discount_end,rating,thumbnail,slug,reward,todays_deal,has_variant')
  958. ->ProductPublished()->where('todays_deal', 1)
  959. ->UserCheck()->IsWholesale()->IsStockOut()->latest()->take(16)->get();
  960. }
  961.  
  962. public function bestSelling()
  963. {
  964. return Product::with('category')->whereIn('category_id', [13, 19, 15, 17, 21, 23, 25 ])->get();
  965. }
  966.  
  967. public function offerEndingSoon($limit = null)
  968. {
  969. if (!$limit)
  970. {
  971. $limit = 8;
  972. }
  973. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->UserCheck()->IsWholesale()->IsStockOut()->where('is_wholesale',0)
  974. ->selectRaw('id,price,special_discount,is_wholesale,special_discount_type,minimum_order_quantity,current_stock,special_discount_start,special_discount_end,rating,thumbnail,slug,reward')
  975. ->where('special_discount', '>', 0)->where('special_discount_end', '!=', null)->ProductPublished()
  976. ->orderBy(DB::raw('ABS(DATEDIFF(products.special_discount_end, NOW()))'))->latest()->paginate($limit);
  977. }
  978.  
  979. public function productByOffer()
  980. {
  981. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('special_discount', '>', 0)->where('special_discount_end', '!=', null)->ProductPublished()
  982. ->orderBy(DB::raw('ABS(DATEDIFF(products.special_discount_end, NOW()))'))->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate(16);
  983. }
  984.  
  985. public function productByBestSelling($limit = null)
  986. {
  987. if (!$limit)
  988. {
  989. $limit = 16;
  990. }
  991.  
  992. // Product::with('category')->whereIn('category_id', [13, 19, 15, 17, 21, 23, 25 ])->withAvg('reviews','rating')->withCount('reviews')
  993. // ->selectRaw('id,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,total_sale,thumbnail,slug,reward,current_stock')
  994. // ->ProductPublished()->orderBy('total_sale', 'desc')->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($limit);
  995.  
  996. $selling = Product::with('category')->whereIn('category_id', [13, 19, 15, 17, 21, 23, 25 ])->get();
  997.  
  998. Log::info('data: '. json_encode($selling));
  999.  
  1000. return $selling;
  1001.  
  1002. }
  1003.  
  1004. public function productByGadget($slug)
  1005. {
  1006. $category = Category::where('slug', $slug)->first();
  1007. return Product::withAvg('reviews','rating')->withCount('reviews')->where('category_id', $category->id)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate(16);
  1008. }
  1009.  
  1010. public function viewedProduct()
  1011. {
  1012. $auth_id = authId();
  1013. $viewed_products = Product::withAvg('reviews','rating')->whereHas('viewedProducts', function ($query) use ($auth_id) {
  1014. $query->where('user_id', $auth_id)->groupBy('product_id')->latest();
  1015. })->selectRaw('id,thumbnail,slug,price,rating,current_stock,reward,minimum_order_quantity,special_discount,special_discount_start,special_discount_end')
  1016. ->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()->take(10)->get();
  1017.  
  1018. if (count($viewed_products) < 20) {
  1019. $left_product = 20 - count($viewed_products);
  1020.  
  1021. $products = Product::withAvg('reviews','rating')->whereHas('viewedProducts', function ($query) {
  1022. $query->groupBy('product_id')->latest();
  1023. })->selectRaw('id,thumbnail,slug,price,rating,current_stock,reward,minimum_order_quantity,special_discount,special_discount_start,special_discount_end')
  1024. ->ProductPublished()->whereNotIn('id', $viewed_products->pluck('id')->toArray())->UserCheck()->IsWholesale()->IsStockOut()->latest()->take($left_product)->get();
  1025.  
  1026. return $viewed_products->merge($products);
  1027. }
  1028. return $viewed_products;
  1029. }
  1030.  
  1031. public function productDetails($slug)
  1032. {
  1033. return Product::with(['brand:id', 'category:id', 'reviews', 'stock', 'userWishlist','userReview'])->withAvg('reviews','rating')
  1034. ->withCount('monthlyOrders')->withCount('reviews')->where('slug', $slug)->UserCheck()->IsWholesale()->IsStockOut()->first();
  1035. }
  1036.  
  1037. public function dailyDeals($paginate)
  1038. {
  1039. return Product::withAvg('reviews','rating')->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()
  1040. ->selectRaw('id,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,thumbnail,slug,reward,current_stock,created_at')
  1041. ->where('todays_deal', 1)->latest()->paginate($paginate);
  1042. }
  1043.  
  1044. public function productByCampaign($campaign_id, $paginate = null): \Illuminate\Contracts\Pagination\LengthAwarePaginator
  1045. {
  1046. if (!$paginate) {
  1047. $paginate = 12;
  1048. }
  1049.  
  1050. return Product::withAvg('reviews','rating')->join('campaign_products', 'products.id', 'campaign_products.product_id')
  1051. ->join('campaigns', 'campaign_products.campaign_id', 'campaigns.id')
  1052. ->join('users', 'products.user_id', 'users.id')->where('users.status', 1)
  1053. ->where('campaigns.id', $campaign_id)->where('products.status', 'published')->where('products.is_approved',1)
  1054. ->where('users.is_user_banned', 0)->IsWholesale()->IsStockOut()
  1055. ->select('products.id', 'campaigns.id as campaign_id', 'campaigns.slug as campaign_slug', 'current_stock', 'products.price', 'special_discount', 'special_discount_type', 'rating', 'text_color', 'start_date', 'end_date', 'products.special_discount_start', 'products.special_discount', 'products.special_discount_end', 'products.thumbnail', 'products.slug', 'reward')
  1056. ->orderBy('campaigns.created_at', 'desc')->paginate($paginate);
  1057. }
  1058.  
  1059. public function categoryProducts($slug): \Illuminate\Contracts\Pagination\LengthAwarePaginator
  1060. {
  1061. $category = Category::where('slug', $slug)->first();
  1062. return Product::withAvg('reviews','rating')->where('category_id', $category->id)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->paginate(8);
  1063. }
  1064.  
  1065. public function filterProduct($data): \Illuminate\Contracts\Pagination\LengthAwarePaginator
  1066. {
  1067.  
  1068. DB::connection()->enableQueryLog();
  1069. $price_range = $this->priceRange();
  1070. $products = Product::query();
  1071.  
  1072. if (array_key_exists('key', $data) && !empty($data['key'])) {
  1073. $products->whereHas('productLanguages', function ($query) use ($data) {
  1074. $query->where('name', 'like', '%' . $data['key'] . '%');
  1075. });
  1076. } else {
  1077. if ($data['route'] == 'product.by.brand') {
  1078. $products->whereHas('brand', function ($q) use ($data) {
  1079. $q->where('slug', $data['slug']);
  1080. });
  1081. }
  1082. if ($data['route'] == 'product.by.category' || $data['route'] == 'product.by.gadget') {
  1083.  
  1084. $category = Category::where('slug', $data['slug'])->first();
  1085. $category_ids = \App\Utility\CategoryUtility::getMyAllChildIds($category->id);
  1086. $category_ids[] = $category->id;
  1087. $all_nested_category = array();
  1088. if ($category) {
  1089. if (array_key_exists('child_category', $data) && count($data['child_category']) > 0) {
  1090. foreach ($data['child_category'] as $child) {
  1091. $nested_category = \App\Utility\CategoryUtility::getMyAllChildIds($child);
  1092. $nested_category[] = (int)$child;
  1093. $all_nested_category[] = $nested_category;
  1094. }
  1095. $all_nested_category[] = [(int)$category->id];
  1096. $products->whereIn('category_id', array_merge(...$all_nested_category));
  1097. } else {
  1098. $products->whereIn('category_id', $category_ids);
  1099. }
  1100. }
  1101. }
  1102. if ($data['route'] == 'product.by.offer') {
  1103. $products->where('special_discount', '>', 0)->where('special_discount_end', '!=', null);
  1104. }
  1105.  
  1106. if ($data['route'] == 'shop') {
  1107.  
  1108. $products->whereHas('sellerProfile', function ($q) use ($data) {
  1109. $q->where('slug', $data['slug']);
  1110. });
  1111. }
  1112.  
  1113. if (array_key_exists('category', $data) && count($data['category']) > 0) {
  1114. $all_nested_category = [];
  1115. foreach ($data['category'] as $child) {
  1116. $nested_category = \App\Utility\CategoryUtility::getMyAllChildIds($child);
  1117. $nested_category[] = (int)$child;
  1118. $all_nested_category[] = $nested_category;
  1119. }
  1120. $products->whereIn('category_id', array_merge(...$all_nested_category));
  1121. }
  1122. if (array_key_exists('brand', $data) && count($data['brand']) > 0) {
  1123. $products->whereIn('brand_id', $data['brand']);
  1124. }
  1125. if (array_key_exists('price', $data)) {
  1126. $price = json_decode($data['price']);
  1127.  
  1128. if ($price->min == 0 && $price->max == 0)
  1129. {
  1130. $max = $price_range['max'];
  1131. $min = $price_range['min'];
  1132. }
  1133. else{
  1134. $max = $price->max;
  1135. $min = $price->min;
  1136. }
  1137. if (!$min)
  1138. {
  1139. $min = 0;
  1140. }
  1141. if (!$max) {
  1142. $max = 0;
  1143. $products->where('price', '>', $min);
  1144. } else {
  1145. $products->whereBetween('price', [$min, $max]);
  1146. }
  1147. }
  1148. if (array_key_exists('color', $data) && count($data['color']) > 0) {
  1149.  
  1150. $products->where(function ($query) use ($data) {
  1151. foreach ($data['color'] as $color) {
  1152. $query->where('colors', 'like', "%\"{$color}\"%");
  1153. }
  1154. });
  1155. }
  1156. $length = array_key_exists('rating', $data) ? count($data['rating']) : 0;
  1157. if ($length > 0) {
  1158. sort($data['rating']);
  1159.  
  1160. if ($length > 1) {
  1161. $products->whereBetween('rating', [(int)$data['rating'][0], (int)$data['rating'][$length - 1]]);
  1162. } else {
  1163. $products->whereBetween('rating', [$data['rating'][0] - 0.5, (int)$data['rating'][0]]);
  1164. }
  1165. }
  1166. if (array_key_exists('attribute_value_id', $data) && count($data['attribute_value_id']) > 0) {
  1167. $products->where(function ($query) use ($data) {
  1168. foreach ($data['attribute_value_id'] as $attribute_value) {
  1169. $query->orWhere('selected_variants_ids', 'like', "%\"{$attribute_value}\"%");
  1170. }
  1171. });
  1172. }
  1173. if ($data['sort'] == 'newest') {
  1174. if ($data['route'] == 'product.by.selling') {
  1175. $products->orderByRaw("total_sale DESC, id DESC");
  1176. } else {
  1177. $products->latest();
  1178. }
  1179. } elseif ($data['sort'] == 'oldest') {
  1180. $products->oldest();
  1181. } elseif ($data['sort'] == 'top_rated') {
  1182. $products->orderBy('rating', 'desc');
  1183. } elseif ($data['sort'] == 'top_selling') {
  1184. $products->withCount('orders')->orderBy('orders_count', 'desc');
  1185. }
  1186. }
  1187.  
  1188. return $products->with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')
  1189. ->when($data['route'] == 'product.by.selling', function($q){
  1190. $q->orderByDesc('total_sale');
  1191. })
  1192. ->selectRaw('id,category_id,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,reward,current_stock,has_variant,minimum_order_quantity,todays_deal,total_sale')
  1193. ->ProductPublished()
  1194. ->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($data['paginate']);
  1195.  
  1196. }
  1197.  
  1198. protected function saveSearchQuery($data){
  1199. $oldQuery = Search::where('query',$data['key'])->first();
  1200. if(blank($oldQuery) || $oldQuery == null):
  1201. $searchQ = new Search();
  1202. $searchQ->query = $data['key'];
  1203. $searchQ->total_search = 1;
  1204. $searchQ->save();
  1205. else:
  1206. $oldQuery->total_search = $oldQuery->total_search+1;
  1207. $oldQuery->save();
  1208. endif;
  1209.  
  1210. }
  1211.  
  1212. public function relatedProducts($id, $take, $product_id)
  1213. {
  1214. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('category_id', $id)->where('id', '!=', $product_id)->ProductPublished()
  1215. ->UserCheck()->IsWholesale()->IsStockOut()->take($take)->get();
  1216. }
  1217.  
  1218. public function compareList(): int
  1219. {
  1220. if (authUser()) {
  1221. $count = CompareProduct::whereHas('product')->where('user_id', authId())->count();
  1222. } else {
  1223. $count = session()->get('compare_list') ? count(session()->get('compare_list')) : 0;
  1224. }
  1225. return $count;
  1226. }
  1227.  
  1228. public function addToCompare($id)
  1229. {
  1230. return CompareProduct::create([
  1231. 'user_id' => authId(),
  1232. 'product_id' => $id,
  1233. ]);
  1234. }
  1235.  
  1236. public function productByIds($ids)
  1237. {
  1238. return Product::with('brand:id', 'category:id','userWishlist')->withAvg('reviews','rating')->withCount('reviews')
  1239. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1240. ->whereIn('id', $ids)->UserCheck()->IsWholesale()->IsStockOut()->get();
  1241. }
  1242.  
  1243. public function compareProducts()
  1244. {
  1245. $product_ids = CompareProduct::where('user_id', authId())->pluck('product_id')->toArray();
  1246. return $this->productByIds($product_ids);
  1247. }
  1248.  
  1249. public function removeCompareProduct($id)
  1250. {
  1251. $compare = CompareProduct::where('user_id', authId())->where('product_id', $id)->first();
  1252. $product = $compare->product;
  1253. $compare->delete();
  1254. return $product;
  1255. }
  1256.  
  1257. public function bestSellingProduct($category_id)
  1258. {
  1259.  
  1260.  
  1261.  
  1262.  
  1263. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('category_id',$category_id)->ProductPublished()
  1264. ->UserCheck()->IsWholesale()->IsStockOut()->orderBy('total_sale','desc')->first();;
  1265.  
  1266.  
  1267. }
  1268.  
  1269. //seller wise products
  1270. public function featuredProducts($ids)
  1271. {
  1272. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->whereIn('id',$ids)->ProductPublished()->where('is_featured',1)
  1273. ->UserCheck()->IsWholesale()->IsStockOut()
  1274. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')->get();
  1275. }
  1276.  
  1277. public function newProducts($user_id)
  1278. {
  1279. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('user_id',$user_id)->ProductPublished()->latest()
  1280. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1281. ->UserCheck()->IsWholesale()->IsStockOut()->take(4)->get();
  1282. }
  1283.  
  1284. public function sellerBestSelling($user_id)
  1285. {
  1286. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('user_id',$user_id)->ProductPublished()->orderBy('total_sale','desc')
  1287. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1288. ->UserCheck()->IsWholesale()->IsStockOut()->take(12)->get();
  1289. }
  1290.  
  1291. public function sellerOfferEnding($user_id)
  1292. {
  1293. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('user_id',$user_id)->orderBy('total_sale','desc')->where('is_wholesale',0)
  1294. ->where('special_discount', '>', 0)->where('special_discount_end', '!=', null)->ProductPublished()
  1295. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1296. ->UserCheck()->IsWholesale()->IsStockOut()->take(12)->get();
  1297. }
  1298.  
  1299. public function sellerProducts($user_id): \Illuminate\Contracts\Pagination\LengthAwarePaginator
  1300. {
  1301. return Product::with('userWishlist')->withAvg('reviews','rating')->withCount('reviews')->where('user_id', $user_id)->ProductPublished()
  1302. ->orderBy('id', 'desc')
  1303. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1304. ->UserCheck()->IsWholesale()->IsStockOut()->paginate(12);
  1305. }
  1306.  
  1307. public function campaignProducts()
  1308. {
  1309. return $this->takeCampaignProducts(8, null, 0);
  1310.  
  1311. }
  1312.  
  1313. public function latestProducts()
  1314. {
  1315. return Product::withAvg('reviews','rating')->withCount('reviews')->ProductPublished()->latest()
  1316. ->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')
  1317. ->UserCheck()->IsWholesale()->IsStockOut()->take(6)->get();
  1318. }
  1319.  
  1320. public function productByFlashSale()
  1321. {
  1322. return $this->takeAllCampaignProducts(0, null, 0);
  1323. }
  1324.  
  1325. public function takeCampaignProducts($take, $id, $skip)
  1326. {
  1327. return Product::withAvg('reviews','rating')->withCount('reviews')->join('campaign_products', 'products.id', 'campaign_products.product_id')
  1328. ->join('campaigns', 'campaign_products.campaign_id', 'campaigns.id')
  1329. ->join('users', 'products.user_id', 'users.id')->where('users.status', 1)
  1330. ->where('products.status', 'published')->where('is_approved',1)->when($id,function($q) use ($id){
  1331. $q->where('campaigns.id', $id);
  1332. })
  1333. ->where('users.is_user_banned', 0)->IsWholesale()->IsStockOut()
  1334. ->select('products.id', 'campaigns.id as campaign_id','campaigns.slug as campaign_slug','current_stock', 'products.price', 'special_discount', 'special_discount_type', 'rating',
  1335. 'text_color', 'start_date', 'end_date', 'products.special_discount_start', 'products.special_discount','products.special_discount_end','products.thumbnail', 'products.slug','reward','minimum_order_quantity')
  1336. ->orderBy('campaigns.created_at', 'desc')->take($take)->get();
  1337. }
  1338.  
  1339. public function takeAllCampaignProducts($take, $id, $skip)
  1340. {
  1341. return Product::withAvg('reviews','rating')->withCount('reviews')->join('campaign_products', 'products.id', 'campaign_products.product_id')
  1342. ->join('campaigns', 'campaign_products.campaign_id', 'campaigns.id')
  1343. ->join('users', 'products.user_id', 'users.id')->where('users.status', 1)
  1344. ->where('products.status', 'published')->where('is_approved',1)->when($id,function($q) use ($id){
  1345. $q->where('campaigns.id', $id);
  1346. })->where('products.status', 'published')
  1347. ->where('users.is_user_banned', 0)->IsWholesale()->IsStockOut()
  1348. ->select('products.id', 'campaigns.id as campaign_id','campaigns.slug as campaign_slug','current_stock', 'products.price', 'special_discount', 'special_discount_type', 'rating', 'text_color', 'start_date', 'end_date', 'products.special_discount_start', 'products.special_discount','products.special_discount_end','products.thumbnail', 'products.slug','reward')
  1349. ->orderBy('campaigns.created_at', 'desc')->paginate(16);
  1350. }
  1351.  
  1352. public function priceRange(): array
  1353. {
  1354. $min = ProductStock::min('price');
  1355.  
  1356.  
  1357. $max = ProductStock::max('price');
  1358.  
  1359. return [
  1360. 'min' => $min,
  1361. 'max' => $max,
  1362. ];
  1363. }
  1364.  
  1365. public function searchProduct($key)
  1366. {
  1367. $this->saveSearchQuery(['key' => $key]);
  1368.  
  1369. return Product::withAvg('reviews','rating')->withCount('reviews')->whereHas('productLanguages', function ($query) use ($key) {
  1370. $query->where('name', 'like', '%' . $key . '%');
  1371. })->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')->ProductPublished()
  1372. ->UserCheck()->IsWholesale()->IsStockOut()->latest()->take(20)->get();
  1373. }
  1374. //api end
  1375.  
  1376. //mobileApi
  1377. public function getLatestProducts($limit,$data)
  1378. {
  1379.  
  1380.  
  1381.  
  1382. $data1 = Product::withAvg('reviews','rating','stock')->withCount('reviews')->ProductPublished()->when(array_key_exists('seller_id',$data),function ($query) use($data){
  1383. $query->whereHas('sellerProfile',function ($q) use($data){
  1384. $q->where('id',$data['seller_id']);
  1385. });
  1386. })->with('stock')->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1387. ->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($limit);
  1388.  
  1389.  
  1390. // $data2 = Product::withAvg('reviews','rating','stock')->withCount('reviews')->ProductPublished()->when(array_key_exists('seller_id',$data),function ($query) use($data){
  1391. // $query->whereHas('sellerProfile',function ($q) use($data){
  1392. // $q->where('id',$data['seller_id']);
  1393. // });
  1394. // })->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1395. // ->get();
  1396.  
  1397.  
  1398.  
  1399.  
  1400. // $d = ProductStock::where("product_id",1)->get();
  1401.  
  1402.  
  1403. Log::info('Showing the user profile for user: '. json_encode($data1));
  1404.  
  1405.  
  1406. return $data1;
  1407.  
  1408.  
  1409.  
  1410.  
  1411. }
  1412.  
  1413. public function getTopProducts($limit)
  1414. {
  1415. return Product::withAvg('reviews','rating')->withCount('reviews')->ProductPublished()
  1416. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1417. ->UserCheck()->IsWholesale()->IsStockOut()->orderBy('total_sale', 'desc')->paginate($limit);
  1418. }
  1419.  
  1420. public function bestRatedProducts($limit)
  1421. {
  1422. return Product::withAvg('reviews','rating')->withCount('reviews')->ProductPublished()
  1423. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1424. ->UserCheck()->IsWholesale()->IsStockOut()->orderBy('rating', 'desc')->paginate($limit);
  1425. }
  1426.  
  1427. public function flashDeals($limit)
  1428. {
  1429. return Product::withAvg('reviews','rating')->withCount('reviews')->join('campaign_products', 'products.id', 'campaign_products.product_id')
  1430. ->join('campaigns', 'campaign_products.campaign_id', 'campaigns.id')
  1431. ->join('users', 'products.user_id', 'users.id')->where('users.status', 1)
  1432. ->where('products.status', 'published')->where('is_approved',1)->where('campaigns.flash_sale',1)
  1433. ->where('users.is_user_banned', 0)->where('users.status',1)->IsWholesale()->IsStockOut()
  1434. ->select('products.id', 'current_stock', 'products.price', 'special_discount', 'special_discount_type', 'rating', 'products.special_discount_start', 'products.special_discount','products.special_discount_end',
  1435. 'products.thumbnail', 'products.slug','reward','minimum_order_quantity','reward','total_sale')
  1436. ->orderBy('campaigns.created_at', 'desc')->paginate($limit);
  1437. }
  1438.  
  1439. public function brandProducts($id, $limit)
  1440. {
  1441. return Product::withAvg('reviews','rating')->withCount('reviews')->where('brand_id',$id)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  1442. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1443. ->paginate($limit);
  1444. }
  1445.  
  1446. public function categoryByProducts($id, $limit)
  1447. {
  1448. $category_ids = \App\Utility\CategoryUtility::getMyAllChildIds($id);
  1449. $category_ids[] = (int)$id;
  1450.  
  1451.  
  1452.  
  1453. $data = Product::withAvg('reviews','rating')->withCount('reviews')->with('stock','productLanguages','brand','category')->whereIn('category_id',$category_ids)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  1454. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1455. ->get();
  1456.  
  1457. // Product::with('stock','productLanguages','brand')->whereIn('category_id',$category_ids)->get();
  1458.  
  1459.  
  1460. Log::info('Showing the user profile for user: '. json_encode($data));
  1461.  
  1462.  
  1463. return $data;
  1464. }
  1465.  
  1466. public function shopProducts($user_id, $limit)
  1467. {
  1468. return Product::withAvg('reviews','rating')->withCount('reviews')->where('user_id',$user_id)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  1469. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1470. ->paginate($limit);
  1471. }
  1472.  
  1473. public function search($key, $paginate)
  1474. {
  1475. if (!$paginate) {
  1476. $paginate = 12;
  1477. }
  1478.  
  1479. $this->saveSearchQuery(['key' => $key]);
  1480.  
  1481. return Product::withAvg('reviews','rating')->withCount('reviews')->whereHas('productLanguages', function ($query) use ($key) {
  1482. $query->where('name', 'like', '%' . $key . '%');
  1483. })->selectRaw('id,brand_id,category_id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,has_variant,reward,current_stock')->ProductPublished()
  1484. ->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($paginate);
  1485. }
  1486.  
  1487. public function wishlistsProduct($user_id,$limit)
  1488. {
  1489. return Product::withAvg('reviews','rating')->withCount('reviews')->whereHas('wishlists',function ($query) use($user_id){
  1490. $query->where('user_id',$user_id);
  1491. })->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()
  1492. ->selectRaw('id,status,price,special_discount,special_discount_type,special_discount_start,special_discount_end,rating,slug,thumbnail,minimum_order_quantity,reward,current_stock,total_sale,has_variant')
  1493. ->paginate($limit);
  1494. }
  1495.  
  1496. public function viewedProducts($user,$paginate)
  1497. {
  1498. if ($user)
  1499. {
  1500. $auth_id = $user->id;
  1501. $products = Product::withAvg('reviews','rating')->withCount('reviews')->whereHas('viewedProducts', function ($query) use ($auth_id) {
  1502. $query->where('user_id', $auth_id)->groupBy('product_id')->latest();
  1503. })->selectRaw('id,thumbnail,slug,price,rating,current_stock,reward,minimum_order_quantity,special_discount,special_discount_start,special_discount_end')
  1504. ->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($paginate);
  1505. }
  1506. else{
  1507. $products = Product::withAvg('reviews','rating')->withCount('reviews')->whereHas('viewedProducts', function ($query) {
  1508. $query->groupBy('product_id')->latest();
  1509. })->selectRaw('id,thumbnail,slug,price,rating,current_stock,reward,minimum_order_quantity,special_discount,special_discount_start,special_discount_end')
  1510. ->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()->paginate($paginate);
  1511.  
  1512. }
  1513. return $products;
  1514. }
  1515.  
  1516. public function adminProducts($id)
  1517. {
  1518. return Product::where('user_id',1)->where('id', '!=',$id)->ProductPublished()->UserCheck()->IsWholesale()->IsStockOut()->latest()->limit(4)->get();
  1519. }
  1520. }
  1521.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement