Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Invoice;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Response;
- use League\Csv\Reader;
- class InvoiceController extends Controller {
- //
- public function exportSettings() {
- return view('index');
- }
- public function index(Request $request) {
- header('Content-type: text/plain; charset=UTF-8');
- $type = $request->input('type');
- $monthFrom = $request->input('month');
- $yearFrom = $request->input('year');
- $yearTo = $request->input('year-to');
- $monthTo = $request->input('month-to');
- $invoices = Invoice::where( 'date', '>', "$yearFrom-$monthFrom-01 00:00:00" )->where( 'date', '<', "$yearTo-$monthTo-01 00:00:00" )->where('series', 'like', 'D%')->get();
- $invoicesEgo = Invoice::where( 'date', '>', "$yearFrom-$monthFrom-01 00:00:00" )->where( 'date', '<', "$yearTo-$monthTo-01 00:00:00" )->where('series', 'like', 'E%')->get();
- $csvExporter = new \Laracsv\Export();
- if($type == 'ego') {
- $invoices = $invoicesEgo;
- }
- $csvExporter->beforeEach( function ( $invoice ) {
- $invoice->csv_date = date( 'Y.m.d', strtotime( $invoice->check_date ) );
- // Remove double and single quotes from title
- $invoice->for_title = str_replace( '"', "", $invoice->for_title );
- $invoice->for_title = str_replace( "'", "", $invoice->for_title );
- $invoice->for_title = str_replace( ",", "", $invoice->for_title );
- $invoice->for_title = str_replace( "“", "", $invoice->for_title );
- $invoice->for_title = str_replace( "”", "", $invoice->for_title );
- $invoice->for_title = preg_replace( '/(^|;)"([^"]+)";/', '$1$2;', $invoice->for_title );
- $a = $invoice->serial_number;
- $invoice->csv_invoice = $invoice->series . " " . $a;
- $invoice->total_price = $invoice->invoiceTotal();
- $invoice->total_price = number_format( $invoice->invoiceTotal(), 2, ',', '' );
- $invoice->vat_price = $invoice->invoiceTotal() - ( $invoice->invoiceTotal() / 1.21 );
- $invoice->vat_price = number_format( $invoice->vat_price, 2, ',', '' );
- $invoice->net_price = $invoice->invoiceTotal() / 1.21;
- $invoice->net_price = number_format( $invoice->net_price, 2, ',', '' );
- $invoice->extra = 0.00;
- } );
- $csvExporter->getWriter()->setDelimiter( ';' );
- $csvExporter->getWriter()->setOutputBOM(Reader::BOM_UTF8);
- $csvExporter->build( $invoices,
- [
- 'for_title',
- 'csv_date',
- 'total_price',
- 'csv_invoice',
- 'csv_date',
- 'vat_price',
- 'net_price',
- 'extra',
- 'company_regcode',
- 'company_vat'
- ]
- , [ 'header' => false ] );
- $csvString = $csvExporter->getWriter()->getContent();
- $csvString = str_replace( '"', '', $csvString );
- // dd($csvString);
- return response( (string) $csvString )
- ->header( 'Content-Type', 'text/csv; charset=UTF-8' )
- ->header( 'Content-Disposition', 'attachment; filename="screenshop-centas-export-' . $yearTo . '-' . $monthTo.'.csv"' );
- $headers = array(
- 'Content-Type' => 'text/csv',
- );
- return response( (string) $csvString )
- ->header( 'Content-Type', 'text/csv; charset=UTF-8' )
- ->header( 'Content-Disposition', 'attachment; filename="export-screenshop.csv"' );
- /*$response = response($csvString, 200, [
- 'Content-Type' => 'text/csv;charset=utf8',
- 'Content-Disposition' => 'attachment; filename="export-screenshop.csv"',
- 'charset' => 'utf-8',
- ]);*/
- // return $response;
- $csvExporter->download();
- // return view( 'invoices', [ 'invoices' => $invoices ] );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement