Advertisement
eimkasp

Centas Export Example

Nov 23rd, 2020
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Invoice;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Response;
  8. use League\Csv\Reader;
  9.  
  10. class InvoiceController extends Controller {
  11.     //
  12.  
  13.     public function exportSettings() {
  14.  
  15.         return view('index');
  16.     }
  17.  
  18.     public function index(Request $request) {
  19.         header('Content-type: text/plain; charset=UTF-8');
  20.         $type = $request->input('type');
  21.         $monthFrom = $request->input('month');
  22.         $yearFrom = $request->input('year');
  23.         $yearTo = $request->input('year-to');
  24.         $monthTo = $request->input('month-to');
  25.  
  26.  
  27.         $invoices = Invoice::where( 'date', '>', "$yearFrom-$monthFrom-01 00:00:00" )->where( 'date', '<', "$yearTo-$monthTo-01 00:00:00" )->where('series', 'like', 'D%')->get();
  28.         $invoicesEgo = Invoice::where( 'date', '>', "$yearFrom-$monthFrom-01 00:00:00" )->where( 'date', '<', "$yearTo-$monthTo-01 00:00:00" )->where('series', 'like', 'E%')->get();
  29.  
  30.         $csvExporter = new \Laracsv\Export();
  31.  
  32.         if($type == 'ego') {
  33.             $invoices = $invoicesEgo;
  34.         }
  35.  
  36.         $csvExporter->beforeEach( function ( $invoice ) {
  37.             $invoice->csv_date = date( 'Y.m.d', strtotime( $invoice->check_date ) );
  38.  
  39.             // Remove double and single quotes from title
  40.             $invoice->for_title = str_replace( '"', "", $invoice->for_title );
  41.             $invoice->for_title = str_replace( "'", "", $invoice->for_title );
  42.             $invoice->for_title = str_replace( ",", "", $invoice->for_title );
  43.             $invoice->for_title = str_replace( "“", "", $invoice->for_title );
  44.             $invoice->for_title = str_replace( "”", "", $invoice->for_title );
  45.             $invoice->for_title = preg_replace( '/(^|;)"([^"]+)";/', '$1$2;', $invoice->for_title );
  46.             $a = $invoice->serial_number;
  47.             $invoice->csv_invoice = $invoice->series . " " . $a;
  48.             $invoice->total_price = $invoice->invoiceTotal();
  49.             $invoice->total_price = number_format( $invoice->invoiceTotal(), 2, ',', '' );
  50.             $invoice->vat_price   = $invoice->invoiceTotal() - ( $invoice->invoiceTotal() / 1.21 );
  51.             $invoice->vat_price   = number_format( $invoice->vat_price, 2, ',', '' );
  52.             $invoice->net_price   = $invoice->invoiceTotal() / 1.21;
  53.             $invoice->net_price   = number_format( $invoice->net_price, 2, ',', '' );
  54.             $invoice->extra       = 0.00;
  55.         } );
  56.         $csvExporter->getWriter()->setDelimiter( ';' );
  57.         $csvExporter->getWriter()->setOutputBOM(Reader::BOM_UTF8);
  58.         $csvExporter->build( $invoices,
  59.             [
  60.                 'for_title',
  61.                 'csv_date',
  62.                 'total_price',
  63.                 'csv_invoice',
  64.                 'csv_date',
  65.                 'vat_price',
  66.                 'net_price',
  67.                 'extra',
  68.                 'company_regcode',
  69.                 'company_vat'
  70.             ]
  71.             , [ 'header' => false ] );
  72.         $csvString = $csvExporter->getWriter()->getContent();
  73.         $csvString = str_replace( '"', '', $csvString );
  74. //      dd($csvString);
  75.  
  76.  
  77.         return response( (string) $csvString )
  78.             ->header( 'Content-Type', 'text/csv; charset=UTF-8' )
  79.             ->header( 'Content-Disposition', 'attachment; filename="screenshop-centas-export-' . $yearTo . '-' . $monthTo.'.csv"' );
  80.  
  81.  
  82.  
  83.  
  84.         $headers = array(
  85.             'Content-Type' => 'text/csv',
  86.         );
  87.  
  88.         return response( (string) $csvString )
  89.             ->header( 'Content-Type', 'text/csv; charset=UTF-8' )
  90.             ->header( 'Content-Disposition', 'attachment; filename="export-screenshop.csv"' );
  91.  
  92.         /*$response = response($csvString, 200, [
  93.             'Content-Type' => 'text/csv;charset=utf8',
  94.             'Content-Disposition' => 'attachment; filename="export-screenshop.csv"',
  95.             'charset' => 'utf-8',
  96.         ]);*/
  97.  
  98. //      return $response;
  99.  
  100.         $csvExporter->download();
  101.  
  102. //      return view( 'invoices', [ 'invoices' => $invoices ] );
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement