Laravel 7 pdf, This page isn’t working and is currently unable to handle this request. HTTP ERROR 500

Viewed 317

My Laravel 7 application PDF was working perfectly on localhost but on the internet server it gives me this error -> This page isn’t working and is currently unable to handle this request. HTTP ERROR 500. Each time I download it back to the local server it will still work like always, and kept on giving me the same errors on the internet server. please how can I solve this?

Controller file

namespace App\Http\Controllers\Backend;

use App\Models\Branch;
use App\Models\Product;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Barryvdh\DomPDF\Facade as PDF;
use Auth;

class StockReportController extends Controller
{

public function stockReportPdf(Request $request){
        if (!Auth::user()->can('view_stock')) {
            return redirect('home')->with(denied());
        } // end permission checking

        $products = Product::orderBy('title', 'ASC')->get();

        $random_string = str_random(10);
        $pdf = PDF::loadView('backend.pdf.reports.stock.all-branch', compact('products', 'request'))->setPaper('a4');

        if ($request->action_type == 'download'){
            return $pdf->download('stock-report-' . Carbon::now()->format(get_option('app_date_format')) . '-'. $random_string . '.pdf');
        }elseif($request->action_type == 'print'){
            @unlink('pdf/reports/stock/' . 'stock-report.pdf');
            $pdf->save('pdf/reports/stock/' . 'stock-report.pdf');
            return redirect('pdf/reports/stock/' . 'stock-report.pdf');
        }else{

            $headers = [
                'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
                , 'Content-type' => 'text/csv'
                , 'Content-Disposition' => 'attachment; filename=galleries.csv'
                , 'Expires' => '0'
                , 'Pragma' => 'public'
            ];

            $filename = 'download.csv';
            $handle = fopen($filename, 'w');
            fputcsv($handle, [
                __('pages.sl'),
                __('pages.product'),
                __('pages.purchase') . ' '. __('pages.quantity'),
                __('pages.sells') . ' '. __('pages.quantity'),
                __('pages.current_stock_quantity'),
                __('pages.current_stock_value'),
            ]);

            $products = Product::orderBy('title', 'ASC')->get();
            foreach ($products as $key => $product) {

                if (Auth::user()->can('access_to_all_branch')) {
                   $purchaseQuantity = $product->purchaseProducts->sum('quantity');
                    $sellQuantity = $product->sellProducts->sum('quantity');
                    $current_stock_qty = $product->purchaseProducts->sum('quantity') - $product->sellProducts->sum('quantity');
                } else {
                    $purchaseQuantity = $product->purchaseProducts->where('branch_id', auth()->user()->employee->branch_id)->sum('quantity');
                    $sellQuantity = $product->sellProducts->where('branch_id', auth()->user()->employee->branch_id)->sum('quantity');
                    $current_stock_qty =  productAvailableTransactionStockQty($product->id);
                }

                $product_tax = $product->sell_price * $product->tax->value / 100;
                $current_stock_amount = $current_stock_qty * ($product->sell_price + $product_tax);


                fputcsv($handle, [
                    $key + 1,
                    $product->title . '|'. $product->sku,
                    $purchaseQuantity,
                    $sellQuantity,
                    $current_stock_qty,
                    get_option('app_currency') . number_format($current_stock_amount,2),
                ]);
            }

            fclose($handle);
            return response()->download($filename, 'stock-report-' . Carbon::now()->toDateString() . '.csv', $headers);

        }
    }

    private function branchRequisitionFromQty($request, $product){
        $branch_requisitions_from = \App\Models\Requisition::where('requisition_from', $request->branch_id)
            ->where('status', 2)
            ->select('id')
            ->distinct()
            ->get();

        return $branch_requisitions_from_qty = \App\Models\RequisitionProduct::whereIn('requisition_id', $branch_requisitions_from)
            ->where('product_id', $product->id)
            ->sum('quantity');
    }

    private function branchRequisitionToQty($request, $product){
        $branch_requisitions_to = \App\Models\Requisition::where('requisition_to', $request->branch_id)
            ->where('status', 2)
            ->select('id')
            ->distinct()
            ->get();

      return  $branch_requisitions_to_qty = \App\Models\RequisitionProduct::whereIn('requisition_id', $branch_requisitions_to)
            ->where('product_id', $product->id)
            ->select('id')
            ->sum('quantity');
    }
}

Blade Template file

<div class="card-header p-0">
     <div class="btn-group btn-group-justified nav-buttons" role="group" aria-label="Basic example">
          <a href="{{url('report/stock-report')}}" class="btn btn-outline-primary {{ active_if_full_match('report/stock-report') }}"><i class="fas fa-money-check"></i> Stock Summary </a>
                    </div>

         <div class="btn-group btn-group-sm filter-pdf-btn" role="group">
         <form action="{{url('report/stock-report-pdf')}}" method="get">
         <input type="hidden" name="action_type" value="download">
         <button type="submit" class="btn btn-secondary rounded-0 btn-sm pl-2 pr-2"><i class="fas fa-file-download mr-2"></i> {{__('pages.pdf')}} </button>
         </form>

         <form action="{{url('report/stock-report-pdf')}}" method="get" target="_blank">
         <input type="hidden" name="action_type" value="print">
         <button type="submit" class="btn btn-warning btn-sm rounded-0 pl-2 pr-2"><i class="fa fa-print mr-2"></i> {{__('pages.print')}} </button>
         </form>

         <form action="{{url('report/stock-report-pdf')}}" method="get">
         <input type="hidden" name="action_type" value="csv">
         <button type="submit" class="btn btn-secondary btn-sm rounded-0 pl-2 pr-2"><i class="fa fa-file-csv mr-2"></i> {{__('pages.csv')}} </button>
         </form>
     </div>
</div>
   
0 Answers
Related