ProductController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Http\Controllers\Seller;
  3. use App\Http\Requests\ProductRequest;
  4. use App\Models\SellerSpreadPackagePayment;
  5. use Illuminate\Http\Request;
  6. use App\Models\AttributeValue;
  7. use App\Models\Cart;
  8. use App\Models\Category;
  9. use App\Models\Product;
  10. use App\Models\ProductTax;
  11. use App\Models\User;
  12. use App\Models\ProductTranslation;
  13. use Carbon\Carbon;
  14. use Combinations;
  15. use Artisan;
  16. use Auth;
  17. use Str;
  18. use App\Services\ProductService;
  19. use App\Services\ProductTaxService;
  20. use App\Services\ProductFlashDealService;
  21. use App\Services\ProductStockService;
  22. use function collect;
  23. use function count;
  24. use function date;
  25. use function dd;
  26. use function flash;
  27. use function print_r;
  28. use function redirect;
  29. use function seller_package_validity_check;
  30. use function time;
  31. use function translate;
  32. class ProductController extends Controller
  33. {
  34. protected $productService;
  35. protected $productTaxService;
  36. protected $productFlashDealService;
  37. protected $productStockService;
  38. public function __construct(
  39. ProductService $productService,
  40. ProductTaxService $productTaxService,
  41. ProductFlashDealService $productFlashDealService,
  42. ProductStockService $productStockService
  43. ) {
  44. $this->productService = $productService;
  45. $this->productTaxService = $productTaxService;
  46. $this->productFlashDealService = $productFlashDealService;
  47. $this->productStockService = $productStockService;
  48. }
  49. public function product_seller_review_modal(Request $request){
  50. $product = Product::where('id',$request->product_id)->first();
  51. $customers = User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get();
  52. return view('frontend.user.product_seller_review_modal', compact('product','customers'));
  53. }
  54. public function index(Request $request)
  55. {
  56. $search = null;
  57. $products = Product::where('user_id', Auth::user()->id)->where('digital', 0)->orderBy('created_at', 'desc');
  58. if ($request->has('search')) {
  59. $search = $request->search;
  60. $products = $products->where('name', 'like', '%' . $search . '%');
  61. }
  62. $products = $products->paginate(10);
  63. // dd($products);
  64. $seller_spread_packages_payments = collect(SellerSpreadPackagePayment::with(['products', 'seller_spread_package'])->where('user_id', Auth::user()->id)->where('expire_at', '>', time())->get())->toArray();
  65. foreach ( $seller_spread_packages_payments as $key=>$seller_spread_packages_payment )
  66. {
  67. if (count($seller_spread_packages_payment['products']) >= $seller_spread_packages_payment['product_spread_limit']) {
  68. unset($seller_spread_packages_payments[$key]);
  69. }
  70. }
  71. return view('seller.product.products.index', compact('products', 'search', 'seller_spread_packages_payments'));
  72. }
  73. public function create(Request $request)
  74. {
  75. if (addon_is_activated('seller_subscription')) {
  76. if (seller_package_validity_check()) {
  77. $categories = Category::where('parent_id', 0)
  78. ->where('digital', 0)
  79. ->with('childrenCategories')
  80. ->get();
  81. return view('seller.product.products.create', compact('categories'));
  82. } else {
  83. flash(translate('Please upgrade your package.'))->warning();
  84. return back();
  85. }
  86. }
  87. $categories = Category::where('parent_id', 0)
  88. ->where('digital', 0)
  89. ->with('childrenCategories')
  90. ->get();
  91. return view('seller.product.products.create', compact('categories'));
  92. }
  93. public function store(ProductRequest $request)
  94. {
  95. if (addon_is_activated('seller_subscription')) {
  96. if (!seller_package_validity_check()) {
  97. flash(translate('Please upgrade your package.'))->warning();
  98. return redirect()->route('seller.products');
  99. }
  100. }
  101. $product = $this->productService->store($request->except([
  102. '_token', 'sku', 'choice', 'tax_id', 'tax', 'tax_type', 'flash_deal_id', 'flash_discount', 'flash_discount_type'
  103. ]));
  104. $request->merge(['product_id' => $product->id]);
  105. //VAT & Tax
  106. if($request->tax_id) {
  107. $this->productTaxService->store($request->only([
  108. 'tax_id', 'tax', 'tax_type', 'product_id'
  109. ]));
  110. }
  111. //Product Stock
  112. $this->productStockService->store($request->only([
  113. 'colors_active', 'colors', 'choice_no', 'unit_price', 'sku', 'current_stock', 'product_id'
  114. ]), $product);
  115. // Product Translations
  116. $request->merge(['lang' => env('DEFAULT_LANGUAGE')]);
  117. ProductTranslation::create($request->only([
  118. 'lang', 'name', 'unit', 'description', 'product_id'
  119. ]));
  120. flash(translate('Product has been inserted successfully'))->success();
  121. Artisan::call('view:clear');
  122. Artisan::call('cache:clear');
  123. return redirect()->route('seller.products');
  124. }
  125. public function edit(Request $request, $id)
  126. {
  127. $product = Product::findOrFail($id);
  128. if (Auth::user()->id != $product->user_id) {
  129. flash(translate('This product is not yours.'))->warning();
  130. return back();
  131. }
  132. $lang = $request->lang;
  133. $tags = json_decode($product->tags);
  134. $categories = Category::where('parent_id', 0)
  135. ->where('digital', 0)
  136. ->with('childrenCategories')
  137. ->get();
  138. return view('seller.product.products.edit', compact('product', 'categories', 'tags', 'lang'));
  139. }
  140. public function update(ProductRequest $request, Product $product)
  141. {
  142. //Product
  143. $product = $this->productService->update($request->except([
  144. '_token', 'sku', 'choice', 'tax_id', 'tax', 'tax_type', 'flash_deal_id', 'flash_discount', 'flash_discount_type'
  145. ]), $product);
  146. //Product Stock
  147. foreach ($product->stocks as $key => $stock) {
  148. $stock->delete();
  149. }
  150. $request->merge(['product_id' => $product->id]);
  151. $this->productStockService->store($request->only([
  152. 'colors_active', 'colors', 'choice_no', 'unit_price', 'sku', 'current_stock', 'product_id'
  153. ]), $product);
  154. //VAT & Tax
  155. if ($request->tax_id) {
  156. ProductTax::where('product_id', $product->id)->delete();
  157. $request->merge(['product_id' => $product->id]);
  158. $this->productTaxService->store($request->only([
  159. 'tax_id', 'tax', 'tax_type', 'product_id'
  160. ]));
  161. }
  162. // Product Translations
  163. ProductTranslation::where('lang', $request->lang)
  164. ->where('product_id', $request->product_id)
  165. ->updateOrInsert($request->only([
  166. 'lang', 'name', 'unit', 'description', 'product_id'
  167. ]));
  168. flash(translate('Product has been updated successfully'))->success();
  169. Artisan::call('view:clear');
  170. Artisan::call('cache:clear');
  171. return back();
  172. }
  173. public function sku_combination(Request $request)
  174. {
  175. $options = array();
  176. if ($request->has('colors_active') && $request->has('colors') && count($request->colors) > 0) {
  177. $colors_active = 1;
  178. array_push($options, $request->colors);
  179. } else {
  180. $colors_active = 0;
  181. }
  182. $unit_price = $request->unit_price;
  183. $product_name = $request->name;
  184. if ($request->has('choice_no')) {
  185. foreach ($request->choice_no as $key => $no) {
  186. $name = 'choice_options_' . $no;
  187. $data = array();
  188. foreach ($request[$name] as $key => $item) {
  189. array_push($data, $item);
  190. }
  191. array_push($options, $data);
  192. }
  193. }
  194. $combinations = Combinations::makeCombinations($options);
  195. return view('backend.product.products.sku_combinations', compact('combinations', 'unit_price', 'colors_active', 'product_name'));
  196. }
  197. public function sku_combination_edit(Request $request)
  198. {
  199. $product = Product::findOrFail($request->id);
  200. $options = array();
  201. if ($request->has('colors_active') && $request->has('colors') && count($request->colors) > 0) {
  202. $colors_active = 1;
  203. array_push($options, $request->colors);
  204. } else {
  205. $colors_active = 0;
  206. }
  207. $product_name = $request->name;
  208. $unit_price = $request->unit_price;
  209. if ($request->has('choice_no')) {
  210. foreach ($request->choice_no as $key => $no) {
  211. $name = 'choice_options_' . $no;
  212. $data = array();
  213. foreach ($request[$name] as $key => $item) {
  214. array_push($data, $item);
  215. }
  216. array_push($options, $data);
  217. }
  218. }
  219. $combinations = Combinations::makeCombinations($options);
  220. return view('backend.product.products.sku_combinations_edit', compact('combinations', 'unit_price', 'colors_active', 'product_name', 'product'));
  221. }
  222. public function add_more_choice_option(Request $request)
  223. {
  224. $all_attribute_values = AttributeValue::with('attribute')->where('attribute_id', $request->attribute_id)->get();
  225. $html = '';
  226. foreach ($all_attribute_values as $row) {
  227. $html .= '<option value="' . $row->value . '">' . $row->value . '</option>';
  228. }
  229. echo json_encode($html);
  230. }
  231. public function updatePublished(Request $request)
  232. {
  233. $product = Product::findOrFail($request->id);
  234. $product->published = $request->status;
  235. if (addon_is_activated('seller_subscription') && $request->status == 1) {
  236. $shop = $product->user->shop;
  237. if (
  238. $shop->package_invalid_at == null
  239. || Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) < 0
  240. || $shop->product_upload_limit <= $shop->user->products()->where('published', 1)->count()
  241. ) {
  242. return 2;
  243. }
  244. }
  245. $product->save();
  246. return 1;
  247. }
  248. public function updateFeatured(Request $request)
  249. {
  250. $product = Product::findOrFail($request->id);
  251. $product->seller_featured = $request->status;
  252. if ($product->save()) {
  253. Artisan::call('view:clear');
  254. Artisan::call('cache:clear');
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. public function updateSellerSpreadPackage(Request $request)
  260. {
  261. $seller_spread_packages_payment = SellerSpreadPackagePayment::find($request->seller_spread_package_payment_id);
  262. if (!$seller_spread_packages_payment) {
  263. flash('推广包不存在')->success();
  264. return redirect()->route('seller.products');
  265. }elseif ($seller_spread_packages_payment->approval!=1){
  266. flash('推广包不存在')->success();
  267. return redirect()->route('seller.products');
  268. }
  269. elseif ($seller_spread_packages_payment->expire_at < time()) {
  270. flash('推广包已过期')->success();
  271. return redirect()->route('seller.products');
  272. }
  273. elseif ($seller_spread_packages_payment->product_spread_limit <= count($seller_spread_packages_payment->products) ) {
  274. flash('推广包已使用完')->success();
  275. return redirect()->route('seller.products');
  276. }
  277. $product = Product::findOrFail($request->product_id);
  278. $product->seller_spread_package_payment_id = $seller_spread_packages_payment->id;
  279. if ($product->save()) {
  280. Artisan::call('view:clear');
  281. Artisan::call('cache:clear');
  282. flash(translate('Product has been spreaded successfully'))->success();
  283. return redirect()->route('seller.products');
  284. }
  285. flash(translate('Error'))->error();
  286. return redirect()->route('seller.products');
  287. }
  288. public function duplicate($id)
  289. {
  290. $product = Product::find($id);
  291. if (Auth::user()->id != $product->user_id) {
  292. dd($product->user_id);
  293. flash(translate('This product is not yours.'))->warning();
  294. return back();
  295. }
  296. // dd(seller_package_validity_check());
  297. // if (addon_is_activated('seller_subscription')) {
  298. if (!seller_package_validity_check()) {
  299. flash(translate('Please upgrade your package.'))->warning();
  300. return back();
  301. }
  302. // }
  303. if (Auth::user()->id == $product->user_id) {
  304. $product_new = $product->replicate();
  305. $product_new->slug = $product_new->slug . '-' . Str::random(5);
  306. $product_new->save();
  307. //Product Stock
  308. $this->productStockService->product_duplicate_store($product->stocks, $product_new);
  309. //VAT & Tax
  310. $this->productTaxService->product_duplicate_store($product->taxes, $product_new);
  311. flash(translate('Product has been duplicated successfully'))->success();
  312. return redirect()->route('seller.products');
  313. } else {
  314. flash(translate('This product is not yours.'))->warning();
  315. return back();
  316. }
  317. }
  318. public function destroy($id)
  319. {
  320. $product = Product::findOrFail($id);
  321. if (Auth::user()->id != $product->user_id) {
  322. flash(translate('This product is not yours.'))->warning();
  323. return back();
  324. }
  325. $product->product_translations()->delete();
  326. $product->stocks()->delete();
  327. $product->taxes()->delete();
  328. if (Product::destroy($id)) {
  329. Cart::where('product_id', $id)->delete();
  330. flash(translate('Product has been deleted successfully'))->success();
  331. Artisan::call('view:clear');
  332. Artisan::call('cache:clear');
  333. return back();
  334. } else {
  335. flash(translate('Something went wrong'))->error();
  336. return back();
  337. }
  338. }
  339. }