DigitalProductController.php 9.1 KB

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