DigitalProductController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Product;
  5. use App\Models\ProductStock;
  6. use App\Models\Category;
  7. use App\Models\ProductTranslation;
  8. use App\Models\Upload;
  9. use Auth;
  10. class DigitalProductController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. $sort_search = null;
  20. $products = Product::orderBy('created_at', 'desc');
  21. if ($request->has('search')){
  22. $sort_search = $request->search;
  23. $products = $products->where('name', 'like', '%'.$sort_search.'%');
  24. }
  25. $products = $products->where('digital', 1)->paginate(10);
  26. return view('backend.product.digital_products.index', compact('products','sort_search'));
  27. }
  28. /**
  29. * Show the form for creating a new resource.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function create()
  34. {
  35. $categories = Category::where('parent_id', 0)
  36. ->where('digital', 1)
  37. ->with('childrenCategories')
  38. ->get();
  39. return view('backend.product.digital_products.create', compact('categories'));
  40. }
  41. /**
  42. * Store a newly created resource in storage.
  43. *
  44. * @param \Illuminate\Http\Request $request
  45. * @return \Illuminate\Http\Response
  46. */
  47. public function store(Request $request)
  48. {
  49. $product = new Product;
  50. $product->name = $request->name;
  51. $product->added_by = $request->added_by;
  52. $product->user_id = Auth::user()->id;
  53. $product->category_id = $request->category_id;
  54. $product->digital = 1;
  55. $product->photos = $request->photos;
  56. $product->thumbnail_img = $request->thumbnail_img;
  57. $tags = array();
  58. if($request->tags[0] != null){
  59. foreach (json_decode($request->tags[0]) as $key => $tag) {
  60. array_push($tags, $tag->value);
  61. }
  62. }
  63. $product->tags = implode(',', $tags);
  64. $product->description = $request->description;
  65. $product->unit_price = $request->unit_price;
  66. $product->purchase_price = $request->purchase_price;
  67. $product->tax = $request->tax;
  68. $product->tax_type = $request->tax_type;
  69. $product->discount = $request->discount;
  70. $product->discount_type = $request->discount_type;
  71. $product->meta_title = $request->meta_title;
  72. $product->meta_description = $request->meta_description;
  73. $product->meta_img = $request->meta_img;
  74. $product->file_name = $request->file;
  75. $product->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.rand(10000,99999);
  76. if($product->save()){
  77. $product_stock = new ProductStock;
  78. $product_stock->product_id = $product->id;
  79. $product_stock->variant = '';
  80. $product_stock->price = $request->unit_price;
  81. $product_stock->sku = '';
  82. $product_stock->qty = 0;
  83. $product_stock->save();
  84. // Product Translations
  85. $product_translation = ProductTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'product_id' => $product->id]);
  86. $product_translation->name = $request->name;
  87. $product_translation->description = $request->description;
  88. $product_translation->save();
  89. flash(translate('Digital Product has been inserted successfully'))->success();
  90. return redirect()->route('digitalproducts.index');
  91. }
  92. else{
  93. flash(translate('Something went wrong'))->error();
  94. return back();
  95. }
  96. }
  97. /**
  98. * Display the specified resource.
  99. *
  100. * @param int $id
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function show($id)
  104. {
  105. //
  106. }
  107. /**
  108. * Show the form for editing the specified resource.
  109. *
  110. * @param int $id
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function edit(Request $request, $id)
  114. {
  115. $lang = $request->lang;
  116. $product = Product::findOrFail($id);
  117. $categories = Category::where('parent_id', 0)
  118. ->where('digital', 1)
  119. ->with('childrenCategories')
  120. ->get();
  121. return view('backend.product.digital_products.edit', compact('product','lang', 'categories'));
  122. }
  123. /**
  124. * Update the specified resource in storage.
  125. *
  126. * @param \Illuminate\Http\Request $request
  127. * @param int $id
  128. * @return \Illuminate\Http\Response
  129. */
  130. public function update(Request $request, $id)
  131. {
  132. $product = Product::findOrFail($id);
  133. if($request->lang == env("DEFAULT_LANGUAGE")){
  134. $product->name = $request->name;
  135. $product->description = $request->description;
  136. }
  137. $product->user_id = Auth::user()->id;
  138. $product->category_id = $request->category_id;
  139. $product->digital = 1;
  140. $product->photos = $request->photos;
  141. $product->thumbnail_img = $request->thumbnail_img;
  142. $tags = array();
  143. if($request->tags[0] != null){
  144. foreach (json_decode($request->tags[0]) as $key => $tag) {
  145. array_push($tags, $tag->value);
  146. }
  147. }
  148. $product->tags = implode(',', $tags);
  149. $product->unit_price = $request->unit_price;
  150. $product->purchase_price = $request->purchase_price;
  151. $product->tax = $request->tax;
  152. $product->tax_type = $request->tax_type;
  153. $product->discount = $request->discount;
  154. $product->discount_type = $request->discount_type;
  155. $product->meta_title = $request->meta_title;
  156. $product->meta_description = $request->meta_description;
  157. $product->meta_img = $request->meta_img;
  158. $product->slug = strtolower($request->slug);
  159. $product->file_name = $request->file;
  160. // Delete From Product Stock
  161. foreach ($product->stocks as $key => $stock) {
  162. $stock->delete();
  163. }
  164. if($product->save()){
  165. // Insert Into Product Stock
  166. $product_stock = new ProductStock;
  167. $product_stock->product_id = $product->id;
  168. $product_stock->variant = '';
  169. $product_stock->price = $request->unit_price;
  170. $product_stock->sku = '';
  171. $product_stock->qty = 0;
  172. $product_stock->save();
  173. // Product Translations
  174. $product_translation = ProductTranslation::firstOrNew(['lang' => $request->lang, 'product_id' => $product->id]);
  175. $product_translation->name = $request->name;
  176. $product_translation->description = $request->description;
  177. $product_translation->save();
  178. flash(translate('Digital Product has been updated successfully'))->success();
  179. return back();
  180. }
  181. else{
  182. flash(translate('Something went wrong'))->error();
  183. return back();
  184. }
  185. }
  186. /**
  187. * Remove the specified resource from storage.
  188. *
  189. * @param int $id
  190. * @return \Illuminate\Http\Response
  191. */
  192. public function destroy($id)
  193. {
  194. $product = Product::findOrFail($id);
  195. $product->product_translations()->delete();
  196. $product->stocks()->delete();
  197. Product::destroy($id);
  198. flash(translate('Product has been deleted successfully'))->success();
  199. return redirect()->route('digitalproducts.index');
  200. }
  201. public function download(Request $request){
  202. $product = Product::findOrFail(decrypt($request->id));
  203. $downloadable = false;
  204. foreach (Auth::user()->orders as $key => $order) {
  205. foreach ($order->orderDetails as $key => $orderDetail) {
  206. if($orderDetail->product_id == $product->id && $orderDetail->payment_status == 'paid'){
  207. $downloadable = true;
  208. break;
  209. }
  210. }
  211. }
  212. if(Auth::user()->user_type == 'admin' || Auth::user()->id == $product->user_id || $downloadable){
  213. $upload = Upload::findOrFail($product->file_name);
  214. if (env('FILESYSTEM_DRIVER') == "s3") {
  215. return \Storage::disk('s3')->download($upload->file_name, $upload->file_original_name.".".$upload->extension);
  216. }
  217. else {
  218. if (file_exists(base_path('public/'.$upload->file_name))) {
  219. return response()->download(base_path('public/'.$upload->file_name));
  220. }
  221. }
  222. }
  223. else {
  224. abort(404);
  225. }
  226. }
  227. }