Advertisement
X1ucifer

Untitled

Nov 17th, 2022
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Admin\Product;
  4.  
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Requests\Admin\Product\ProductStoreRequest;
  7. use App\Models\ProductStock;
  8. use App\Repositories\Interfaces\Site\ReviewInterface;
  9. use App\Utility\VariantUtility;
  10. use App\Repositories\Admin\VatTaxRepository;
  11. use App\Repositories\Interfaces\Admin\LanguageInterface;
  12. use App\Repositories\Interfaces\Admin\Product\AttributeInterface;
  13. use App\Repositories\Interfaces\Admin\Product\BrandInterface;
  14. use App\Repositories\Interfaces\Admin\Product\CategoryInterface;
  15. use App\Repositories\Interfaces\Admin\Product\ColorInterface;
  16. use App\Repositories\Interfaces\Admin\Product\ProductInterface;
  17. use App\Repositories\Interfaces\Admin\SellerInterface;
  18. use Brian2694\Toastr\Facades\Toastr;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Validator;
  22. use Illuminate\Support\Facades\Log;
  23.  
  24.  
  25. class ProductController extends Controller
  26. {
  27. protected $products;
  28. protected $categories;
  29. protected $brands;
  30. protected $colors;
  31. protected $attributes;
  32. protected $vat_tax;
  33. protected $languages;
  34. protected $sellers;
  35.  
  36. public function __construct(ProductInterface $products,
  37. CategoryInterface $categories,
  38. BrandInterface $brands,
  39. ColorInterface $colors,
  40. AttributeInterface $attributes,
  41. VatTaxRepository $vat_tax,
  42. SellerInterface $sellers,
  43. LanguageInterface $languages){
  44. $this->products = $products;
  45. $this->categories = $categories;
  46. $this->brands = $brands;
  47. $this->colors = $colors;
  48. $this->attributes = $attributes;
  49. $this->vat_tax = $vat_tax;
  50. $this->languages = $languages;
  51. $this->sellers = $sellers;
  52. }
  53. public function index(Request $request, $status = null){
  54. try {
  55. $products = $this->products->paginate($request, $status ,get_pagination('pagination'),'');
  56.  
  57. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  58. $selected_seller = isset($request->sl) ? $this->sellers->getSeller($request->sl) : null;
  59.  
  60. // Log::info("demofromview-->". json_encode( $products) );
  61.  
  62.  
  63. return view('admin.products.products.index', compact('status','products','selected_category','selected_seller'));
  64. } catch (\Exception $e) {
  65. Toastr::error(__('Something went wrong, please try again'));
  66. return back();
  67. }
  68. }
  69. public function adminProducts(Request $request, $status = null){
  70. try {
  71. $products = $this->products->paginate($request, $status ,get_pagination('pagination'),'admin');
  72. $sellers = $this->sellers->all()->where('is_user_banned',0)->where('status',1)->get();
  73.  
  74. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  75.  
  76. return view('admin.products.products.admin-products', compact('status','products','selected_category','sellers'));
  77. } catch (\Exception $e) {
  78. Toastr::error(__('Something went wrong, please try again'));
  79. return back();
  80. }
  81. }
  82. public function sellerProducts(Request $request, $status = null){
  83. try {
  84. $products = $this->products->paginate($request, $status ,get_pagination('pagination'),'seller');
  85.  
  86. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  87. $selected_seller = isset($request->sl) ? $this->sellers->getSeller($request->sl) : null;
  88.  
  89. return view('admin.products.products.seller-products', compact('status','products','selected_category','selected_seller'));
  90. } catch (\Exception $e) {
  91. Toastr::error(__('Something went wrong, please try again'));
  92. return back();
  93. }
  94. }
  95. public function digitalProducts(Request $request, $status = null){
  96. try {
  97. $products = $this->products->paginate($request, $status ,get_pagination('pagination'),'digital');
  98.  
  99. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  100. $selected_seller = isset($request->sl) ? $this->sellers->getSeller($request->sl) : null;
  101.  
  102. return view('admin.products.products.digital-products', compact('status','products','selected_category','selected_seller'));
  103. } catch (\Exception $e) {
  104. Toastr::error(__('Something went wrong, please try again'));
  105. return back();
  106. }
  107. }
  108. public function catalogProducts(Request $request, $status = null){
  109. try {
  110. $products = $this->products->paginate($request, $status ,\Config::get('yrsetting.paginate'),'catalog');
  111.  
  112. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  113. $selected_seller = isset($request->sl) ? $this->sellers->getSeller($request->sl) : null;
  114.  
  115. return view('admin.products.products.catalog-products', compact('status','products','selected_category','selected_seller'));
  116. } catch (\Exception $e) {
  117. Toastr::error(__('Something went wrong, please try again'));
  118. return back();
  119. }
  120. }
  121.  
  122. public function classifiedProducts(Request $request, $status = null){
  123. try {
  124. $products = $this->products->paginate($request, $status ,\Config::get('yrsetting.paginate'),'classified');
  125. $selected_category = isset($request->c) ? $this->categories->get($request->c) : null;
  126. $selected_seller = isset($request->sl) ? $this->sellers->getSeller($request->sl) : null;
  127. return view('admin.products.products.classified-products', compact('status','products','selected_category','selected_seller'));
  128. } catch (\Exception $e) {
  129. Toastr::error(__('Something went wrong, please try again'));
  130. return back();
  131. }
  132. }
  133.  
  134. public function create(Request $request){
  135. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  136. $products = $this->products->productBydifference(19);
  137. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  138. $colors = $this->colors->all()->where('lang', 'en')->get();
  139. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  140. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  141. return view('admin.products.products.form',compact('categories','products','brands','colors','attributes','r'));
  142. }
  143. public function createDigitalProduct(Request $request){
  144. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  145. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  146. $colors = $this->colors->all()->where('lang', 'en')->get();
  147. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  148. $is_digital = 1;
  149. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  150. return view('admin.products.products.form',compact('categories','brands','colors','attributes','is_digital','r'));
  151. }
  152. public function createCatalogProduct(Request $request){
  153. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  154. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  155. $colors = $this->colors->all()->where('lang', 'en')->get();
  156. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  157. $is_catalog = 1;
  158. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  159. return view('admin.products.products.form',compact('categories','brands','colors','attributes','is_catalog','r'));
  160. }
  161. public function createClassifiedProduct(Request $request){
  162. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  163. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  164. $colors = $this->colors->all()->where('lang', 'en')->get();
  165. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  166. $is_classified = 1;
  167. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  168. return view('admin.products.products.form',compact('categories','brands','colors','attributes','is_classified','r'));
  169. }
  170.  
  171. public function edit($id, Request $request){
  172. try {
  173. // session()->forget('attributes');
  174. $languages = $this->languages->all()->orderBy('id', 'asc')->get();
  175.  
  176. $lang = $request->lang != '' ? $request->lang : \App::getLocale();
  177. if ($this->products->get($id) && $product_language = $this->products->getByLang($id, $lang)):
  178. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  179. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  180. $colors = $this->colors->all()->where('lang', 'en')->get();
  181. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  182. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  183. return view('admin.products.products.edit',compact('languages', 'lang','product_language','categories','brands','attributes','colors','r'));
  184.  
  185. else:
  186. Toastr::error(__('Not found'));
  187. return back();
  188. endif;
  189. } catch (\Exception $e){
  190. Toastr::error(__('Something went wrong, please try again'));
  191. return back();
  192. }
  193. }
  194.  
  195. public function statusChange(Request $request)
  196. {
  197. if (isDemoServer()):
  198. $response['message'] = __('This function is disabled in demo server.');
  199. $response['title'] = __('Ops..!');
  200. $response['status'] = 'error';
  201. return response()->json($response);
  202. endif;
  203.  
  204. if ($this->products->statusChange($request['data'])):
  205. $response['message'] = __('Updated Successfully');
  206. $response['title'] = __('Success');
  207. $response['status'] = 'success';
  208. return response()->json($response);
  209. else:
  210. $response['message'] = __('Something went wrong, please try again');
  211. $response['title'] = __('Ops..!');
  212. $response['status'] = 'error';
  213. return response()->json($response);
  214. endif;
  215. }
  216.  
  217. public function store(ProductStoreRequest $request)
  218. {
  219. if (isDemoServer()):
  220. Toastr::info(__('This function is disabled in demo server.'));
  221. return redirect()->back();
  222. endif;
  223.  
  224. if ($this->products->store($request)):
  225. Toastr::success(__('Created Successfully'));
  226. session()->forget('attributes');
  227. return redirect()->route('products');
  228. else:
  229. Toastr::error(__('Something went wrong, please try again'));
  230. return back()->withInput();
  231. endif;
  232. }
  233.  
  234. public function update(Request $request)
  235. {
  236. if (isDemoServer()):
  237. Toastr::info(__('This function is disabled in demo server.'));
  238. return redirect()->back();
  239. endif;
  240.  
  241. DB::beginTransaction();
  242. try{
  243.  
  244. session()->forget('attributes');
  245. if ($request->variant_ids && count($request->variant_ids))
  246. {
  247. ProductStock::where('product_id',$request->id)->delete();
  248. session()->put('attributes',count($request->variant_ids));
  249. }
  250.  
  251. $product = $this->products->get($request->id);
  252.  
  253. $validator = Validator::make($request->all(), [
  254. 'name' => 'required|max:190',
  255. 'slug' => 'nullable|nullable|max:190|unique:products,slug,'.$request->id,
  256. 'category' => 'required',
  257. 'price' => 'numeric|required',
  258. 'unit' => 'required',
  259. 'variant_sku.*' => 'required_if:has_variant,1|distinct|unique:product_stocks,sku',
  260. 'video_url' => 'required_with:video_provider',
  261. 'minimum_order_quantity' => 'numeric|min:1',
  262. 'low_stock_to_notify' => 'numeric|min:0',
  263. 'shipping_fee' => 'required_if:shipping_type,flat_rate',
  264. 'special_discount_period' => 'required_with:special_discount_type',
  265. 'special_discount' => 'required_with:special_discount_type',
  266. 'campaign_discount' => 'required_with:campaign',
  267. 'campaign_discount_type' => 'required_with:campaign'
  268. ]);
  269.  
  270. DB::commit();
  271.  
  272. if ($validator->fails()) {
  273. DB::rollBack();
  274. return back()->withInput()->withErrors($validator);
  275. }
  276.  
  277. if(!$request->has_variant)
  278. {
  279. $sku_validator = Validator::make($request->all(), [
  280. 'sku' => 'required_without_all:has_variant,is_classified|unique:product_stocks,sku,'.$product->stock()->first()->id,
  281. ]);
  282.  
  283. if ($sku_validator->fails()) {
  284. DB::rollBack();
  285. return back()->withInput()->withErrors($sku_validator);
  286. }
  287. }
  288.  
  289. } catch (\Exception $e) {
  290. DB::rollBack();
  291. return back()->withInput()->withErrors($validator);
  292. }
  293.  
  294. if (isDemoServer()):
  295. Toastr::info(__('This function is disabled in demo server.'));
  296. return redirect()->back();
  297. endif;
  298.  
  299. if ($this->products->update($request)):
  300. Toastr::success(__('Updated Successfully'));
  301. session()->forget('attributes');
  302. return redirect($request->r);
  303. else:
  304. Toastr::error(__('Something went wrong, please try again'));
  305. return back()->withInput();
  306. endif;
  307. }
  308.  
  309. public function variants(Request $request)
  310. {
  311. if ($request->has_variant == 1):
  312. $variants = array();
  313.  
  314. $product_price = $request->price;
  315. $colors = false;
  316.  
  317. if (!empty($request->colors)):
  318. array_push($variants, $request->colors);
  319. $colors = true;
  320. endif;
  321.  
  322. if ($request->has('attribute_sets')):
  323. foreach ($request->attribute_sets as $key => $attribute_set):
  324. $attribute_values = 'attribute_values_'.$attribute_set;
  325. $values = array();
  326. if ($request->has($attribute_values)):
  327. foreach ($request[$attribute_values] as $value):
  328. array_push($values, $value);
  329. endforeach;
  330. endif;
  331. if($request->has($attribute_values)):
  332. array_push($variants, $values);
  333. endif;
  334. endforeach;
  335. endif;
  336. $variants_data = VariantUtility::getVariants($variants);
  337. if (!empty($variants_data[0])):
  338. return view('admin.products.products.variants', compact('variants','variants_data','product_price','colors'));
  339. else:
  340. return view('admin.products.products.variants');
  341. endif;
  342. else:
  343. return '';
  344. endif;
  345. }
  346.  
  347.  
  348. public function variantsEdit(Request $request)
  349. {
  350. $product = $this->products->get($request->id);
  351. // dd($product);
  352. if ($request->has_variant == 1):
  353. $variants = array();
  354.  
  355. $product_price = $request->price;
  356. $colors = false;
  357.  
  358. if (!empty($request->colors)):
  359. array_push($variants, $request->colors);
  360. $colors = true;
  361. endif;
  362.  
  363. if ($request->has('attribute_sets')):
  364. foreach ($request->attribute_sets as $key => $attribute_set):
  365. $attribute_values = 'attribute_values_'.$attribute_set;
  366. $values = array();
  367. if ($request->has($attribute_values)):
  368. foreach ($request[$attribute_values] as $value):
  369. array_push($values, $value);
  370. endforeach;
  371. endif;
  372. if($request->has($attribute_values)):
  373. array_push($variants, $values);
  374. endif;
  375. endforeach;
  376. endif;
  377. $variants_data = VariantUtility::getVariants($variants);
  378. if (!empty($variants_data[0])):
  379. return view('admin.products.products.variants_edit', compact('variants','variants_data','product_price','product','colors'));
  380. else:
  381. return view('admin.products.products.variants_edit');
  382. endif;
  383. else:
  384. return '';
  385. endif;
  386. }
  387.  
  388. public function getAttributeValues(Request $request)
  389. {
  390. $attributes_sets = $request->attribute_sets;
  391.  
  392. if (!empty($attributes_sets)):
  393. $attributes = $this->attributes->all()->whereIn('attributes.id', $attributes_sets)->where('lang','en')->get();
  394. return view('admin.products.products.values', compact('attributes','request','attributes_sets'));
  395. else:
  396. return '';
  397. endif;
  398. }
  399.  
  400. public function restore($id){
  401. if ($this->products->restore($id)):
  402. Toastr::success(__('Updated successfully as unpublished'));
  403. return redirect()->back();
  404. else:
  405. Toastr::error(__('Something went wrong, please try again'));
  406. return back();
  407. endif;
  408. }
  409. public function productStatusChange($status,$id){
  410. if (isDemoServer()):
  411. Toastr::info(__('This function is disabled in demo server.'));
  412. return redirect()->back();
  413. endif;
  414.  
  415. if ($this->products->productStatusChange($status,$id)):
  416. Toastr::success(__('Updated successfully as '.$status));
  417. return redirect()->back();
  418. else:
  419. Toastr::error(__('Something went wrong, please try again'));
  420. return back();
  421. endif;
  422. }
  423.  
  424. public function cloneProduct(Request $request, $id){
  425. try {
  426. $languages = $this->languages->all()->orderBy('id', 'asc')->get();
  427.  
  428. $lang = $request->lang != '' ? $request->lang : \App::getLocale();
  429. if ($this->products->get($id) && $product_language = $this->products->getByLang($id, $lang)):
  430. $categories = $this->categories->allCategory()->where('parent_id', null)->where('status',1);
  431. $brands = $this->brands->all()->where('lang','en')->where('status',1)->get();
  432. $colors = $this->colors->all()->where('lang', 'en')->get();
  433. $attributes = $this->attributes->all()->where('lang', 'en')->get();
  434. $r = $request->r != ''? $request->r : $request->server('HTTP_REFERER');
  435. $clone = 1;
  436. if($product_language->product->is_wholesale != 1):
  437. return view('admin.products.products.edit',
  438. compact(
  439. 'languages',
  440. 'lang',
  441. 'product_language',
  442. 'categories','brands',
  443. 'attributes',
  444. 'colors',
  445. 'r',
  446. 'clone'
  447. ));
  448. else:
  449. return redirect()->route('wholesale.product.clone', [$id]);
  450. endif;
  451.  
  452. else:
  453. Toastr::error(__('Not found'));
  454. return back();
  455. endif;
  456. } catch (\Exception $e){
  457. Toastr::error(__('Something went wrong, please try again'));
  458. return back();
  459. }
  460. }
  461.  
  462. public function StoreCloneProduct(ProductStoreRequest $request){
  463. if (isDemoServer()):
  464. Toastr::info(__('This function is disabled in demo server.'));
  465. return redirect()->back();
  466. endif;
  467.  
  468. if ($this->products->store($request)):
  469. Toastr::success(__('Created Successfully'));
  470. return redirect($request->r);
  471. else:
  472. Toastr::error(__('Something went wrong, please try again'));
  473. return back()->withInput();
  474. endif;
  475. }
  476.  
  477. public function reviews(ReviewInterface $review){
  478. $reviews = $review->paginateReviews(get_pagination('pagination'));
  479. return view('admin.products.products.review',compact('reviews'));
  480. }
  481.  
  482.  
  483. public function reviewStatusChange(ReviewInterface $review,Request $request)
  484. {
  485. if (isDemoServer()):
  486. $response['message'] = __('This function is disabled in demo server.');
  487. $response['title'] = __('Ops..!');
  488. $response['status'] = 'error';
  489. return response()->json($response);
  490. endif;
  491.  
  492. if ($review->statusChange($request['data'])):
  493. $response['message'] = __('Updated Successfully');
  494. $response['title'] = __('Success');
  495. $response['status'] = 'success';
  496. return response()->json($response);
  497. else:
  498. $response['message'] = __('Something went wrong, please try again');
  499. $response['title'] = __('Ops..!');
  500. $response['status'] = 'error';
  501. return response()->json($response);
  502. endif;
  503. }
  504.  
  505. public function productImport(Request $request)
  506. {
  507. return view('admin.products.products.import-products');
  508. }
  509. }
  510.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement