Laravel excel 3.1 add extra rows betweem iterations

老子叫甜甜 提交于 2019-12-02 15:52:42

问题


Sorry for my english. I have one to many relationship between two tables one is "alquileres" (rents) and the other is "cobros" (payments). I need to show the totals of each rental like this: what i need

But obtain this: result

This is the code of my export:

class ComprobantesExport implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize, WithEvents
{
use Exportable, RegistersEventListeners;

protected $alquileres;

public function __construct($alquileres = null)
{
    $this->alquileres = $alquileres;
}
/**
 * @return \Illuminate\Support\Collection
 */
public function collection()
{
    return $this->alquileres;
}

public function headings(): array
{
    return [
        '#',
        'Fecha',
        'Inquilino',
        'Propiedad',
        'Subpropiedad',
        'Atraso',
        'Monto'
    ];
}

public function map($alquiler): array
{
    return $alquiler->cobros->map(function ($cobro, $nro) use ($alquiler){
        return [
            $cobro->comprobante->id,
            $cobro->created_at->format("d/m/Y"),
            $alquiler->inquilino->full_name,
            $alquiler->propiedad->titulo,
            $alquiler->subpropiedad->titulo,
            'si',
            $alquiler->moneda->simbolo . $cobro->comprobante->monto,

        ];

    })->toArray();
}


/**
 * @return array
 */
public function registerEvents(): array
{
    return [
        BeforeExport::class => function(BeforeExport $event) {
            $event->writer->getProperties()->setCreator('Sistema de alquileres');
        },

    ];
}

}

My controller:

public function comprobantes()
{
    $alquileres = Alquiler::with('moneda', 'inquilino', 'propiedad', 'subpropiedad', 'cobros')
        ->get();

    return(new ComprobantesExport($alquileres))->download('comprobantes.xlsx');
}

Thanks.


回答1:


Solve it.

class ComprobantesExport implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize, WithEvents
{
use Exportable, RegistersEventListeners;

protected $alquileres;
protected $filas = [];
protected $total = 0;

public function __construct($alquileres = null)
{
    $this->alquileres = $alquileres;
}
/**
 * @return \Illuminate\Support\Collection
 */
public function collection()
{
    return $this->alquileres;
}

public function headings(): array
{
    return [
        '#',
        'Fecha',
        'Inquilino',
        'Propiedad',
        'Subpropiedad',
        'Atraso',
        'Monto'
    ];
}

public function map($alquiler): array
{
    $this->filas[] = $alquiler->cobros->count() + array_sum($this->filas) + 1 ;
    $this->limites[] = $alquiler->cobros->count();
    $this->total += $alquiler->cobros->sum("monto");
    return $alquiler->cobros->map(function ($cobro, $nro) use ($alquiler){

        return [
            $cobro->comprobante->id,
            $cobro->created_at->format("d/m/Y"),
            $alquiler->inquilino->full_name,
            $alquiler->propiedad->titulo,
            $alquiler->subpropiedad->titulo,
            'si',
            floatval($cobro->comprobante->monto),

        ];

    })->toArray();
}


/**
 * @return array
 */
public function registerEvents(): array
{
    $styleTitulos = [
        'font' => [
            'bold' => true,
            'size' => 12
        ]
    ];
    return [
        BeforeExport::class => function(BeforeExport $event) {
            $event->writer->getProperties()->setCreator('Sistema de alquileres');
        },
        AfterSheet::class => function(AfterSheet $event) use ($styleTitulos){
            $event->sheet->getStyle("A1:G1")->applyFromArray($styleTitulos);
            $event->sheet->setCellValue('A'. ($event->sheet->getHighestRow()+1),"Total");
            foreach ($this->filas as $index => $fila){
                $fila++;
                $event->sheet->insertNewRowBefore($fila, 1);
                $event->sheet->getStyle("A{$fila}:G{$fila}")->applyFromArray($styleTitulos)->getFill()
                    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFF0000');
                $event->sheet->setCellValue("A{$fila}","Subtotal Propiedad");
                $event->sheet->setCellValue("G{$fila}", "=SUM(G".($fila - $this->limites[$index]).":G".($fila - 1).")");
            }
            $event->sheet->getDelegate()->mergeCells("A{$event->sheet->getHighestRow()}:F{$event->sheet->getHighestRow()}");
            $event->sheet->setCellValue('G'. ($event->sheet->getHighestRow()), $this->total);
        }
    ];
}

}



来源:https://stackoverflow.com/questions/56104727/laravel-excel-3-1-add-extra-rows-betweem-iterations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!