123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Product;
- use App\Models\ProductStock;
- use App\Models\Category;
- use App\Models\ProductTranslation;
- use App\Models\Upload;
- use Auth;
- class DigitalProductController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- $sort_search = null;
- $products = Product::orderBy('created_at', 'desc');
- if ($request->has('search')){
- $sort_search = $request->search;
- $products = $products->where('name', 'like', '%'.$sort_search.'%');
- }
- $products = $products->where('digital', 1)->paginate(10);
- return view('backend.product.digital_products.index', compact('products','sort_search'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $categories = Category::where('parent_id', 0)
- ->where('digital', 1)
- ->with('childrenCategories')
- ->get();
- return view('backend.product.digital_products.create', compact('categories'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $product = new Product;
- $product->name = $request->name;
- $product->added_by = $request->added_by;
- $product->user_id = Auth::user()->id;
- $product->category_id = $request->category_id;
- $product->digital = 1;
- $product->photos = $request->photos;
- $product->thumbnail_img = $request->thumbnail_img;
- $tags = array();
- if($request->tags[0] != null){
- foreach (json_decode($request->tags[0]) as $key => $tag) {
- array_push($tags, $tag->value);
- }
- }
- $product->tags = implode(',', $tags);
- $product->description = $request->description;
- $product->unit_price = $request->unit_price;
- $product->purchase_price = $request->purchase_price;
- $product->tax = $request->tax;
- $product->tax_type = $request->tax_type;
- $product->discount = $request->discount;
- $product->discount_type = $request->discount_type;
- $product->meta_title = $request->meta_title;
- $product->meta_description = $request->meta_description;
- $product->meta_img = $request->meta_img;
- $product->file_name = $request->file;
- $product->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.rand(10000,99999);
- if($product->save()){
-
- $product_stock = new ProductStock;
- $product_stock->product_id = $product->id;
- $product_stock->variant = '';
- $product_stock->price = $request->unit_price;
- $product_stock->sku = '';
- $product_stock->qty = 0;
- $product_stock->save();
- // Product Translations
- $product_translation = ProductTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'product_id' => $product->id]);
- $product_translation->name = $request->name;
- $product_translation->description = $request->description;
- $product_translation->save();
- flash(translate('Digital Product has been inserted successfully'))->success();
- return redirect()->route('digitalproducts.index');
- }
- else{
- flash(translate('Something went wrong'))->error();
- return back();
- }
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Request $request, $id)
- {
- $lang = $request->lang;
- $product = Product::findOrFail($id);
- $categories = Category::where('parent_id', 0)
- ->where('digital', 1)
- ->with('childrenCategories')
- ->get();
- return view('backend.product.digital_products.edit', compact('product','lang', 'categories'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $product = Product::findOrFail($id);
- if($request->lang == env("DEFAULT_LANGUAGE")){
- $product->name = $request->name;
- $product->description = $request->description;
- }
- $product->user_id = Auth::user()->id;
- $product->category_id = $request->category_id;
- $product->digital = 1;
- $product->photos = $request->photos;
- $product->thumbnail_img = $request->thumbnail_img;
- $tags = array();
- if($request->tags[0] != null){
- foreach (json_decode($request->tags[0]) as $key => $tag) {
- array_push($tags, $tag->value);
- }
- }
- $product->tags = implode(',', $tags);
- $product->unit_price = $request->unit_price;
- $product->purchase_price = $request->purchase_price;
- $product->tax = $request->tax;
- $product->tax_type = $request->tax_type;
- $product->discount = $request->discount;
- $product->discount_type = $request->discount_type;
- $product->meta_title = $request->meta_title;
- $product->meta_description = $request->meta_description;
- $product->meta_img = $request->meta_img;
- $product->slug = strtolower($request->slug);
- $product->file_name = $request->file;
- // Delete From Product Stock
- foreach ($product->stocks as $key => $stock) {
- $stock->delete();
- }
-
- if($product->save()){
- // Insert Into Product Stock
- $product_stock = new ProductStock;
- $product_stock->product_id = $product->id;
- $product_stock->variant = '';
- $product_stock->price = $request->unit_price;
- $product_stock->sku = '';
- $product_stock->qty = 0;
- $product_stock->save();
-
- // Product Translations
- $product_translation = ProductTranslation::firstOrNew(['lang' => $request->lang, 'product_id' => $product->id]);
- $product_translation->name = $request->name;
- $product_translation->description = $request->description;
- $product_translation->save();
- flash(translate('Digital Product has been updated successfully'))->success();
- return back();
- }
- else{
- flash(translate('Something went wrong'))->error();
- return back();
- }
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $product = Product::findOrFail($id);
- $product->product_translations()->delete();
- $product->stocks()->delete();
- Product::destroy($id);
- flash(translate('Product has been deleted successfully'))->success();
- return redirect()->route('digitalproducts.index');
- }
- public function download(Request $request){
- $product = Product::findOrFail(decrypt($request->id));
- $downloadable = false;
- foreach (Auth::user()->orders as $key => $order) {
- foreach ($order->orderDetails as $key => $orderDetail) {
- if($orderDetail->product_id == $product->id && $orderDetail->payment_status == 'paid'){
- $downloadable = true;
- break;
- }
- }
- }
- if(Auth::user()->user_type == 'admin' || Auth::user()->id == $product->user_id || $downloadable){
- $upload = Upload::findOrFail($product->file_name);
- if (env('FILESYSTEM_DRIVER') == "s3") {
- return \Storage::disk('s3')->download($upload->file_name, $upload->file_original_name.".".$upload->extension);
- }
- else {
- if (file_exists(base_path('public/'.$upload->file_name))) {
- return response()->download(base_path('public/'.$upload->file_name));
- }
- }
- }
- else {
- abort(404);
- }
- }
- }
|