<?php

class StaticFichaDetalle
{
    protected $fichaLiquidacion;

    public function __construct($fichaLiquidacion)
    {
        $this->fichaLiquidacion = $fichaLiquidacion;
    }

    protected function dataProvider($data, $pagination = 10)
    {
        return new CArrayDataProvider($data, [
            'pagination' => [
                'pageSize' => $pagination,
            ],
        ]);
    }

    public function get($type = null)
    {
        switch ($type) {
            case 'aporte_asociado':
                list($header, $columns) = $this->aportesOrdinarios(
                    $this->fichaLiquidacion->find('asignaciones.conceptos.0.detalle', [])
                );

                $columna = 'aporte_patrono';
                return [
                    [
                        'title' => 'Aportes ordinarios',
                        'grid' => array_merge($header, [
                            'columns' => (new Warp($columns))->filter(function ($column) use ($columna) {
                                return $column['name'] !== $columna;
                            }),
                        ]),
                    ],
                ];
            case 'aporte_patrono':
                list($header, $columns) = $this->aportesOrdinarios(
                    $this->fichaLiquidacion->find('asignaciones.conceptos.1.detalle', [])
                );

                $columna = 'aporte_asociado';
                return [
                    [
                        'title' => 'Aportes ordinarios',
                        'grid' => array_merge($header, [
                            'columns' => (new Warp($columns))->filter(function ($column) use ($columna) {
                                return $column['name'] !== $columna;
                            }),
                        ]),
                    ],
                ];
            case 'aporte_extra_socio':
                return [
                    [
                        'title' => 'Aportes carga individual',
                        'grid' => $this->aportesVoluntarios(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.2.detalle_individual', [])
                        ),
                    ],
                    [
                        'title' => 'Aportes carga masiva',
                        'grid' => $this->aportesVoluntariosMasivo(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.2.c', [])
                        ),
                    ],
                ];
            case 'aporte_extra_patrono':
                return [
                    [
                        'title' => 'Aportes carga individual',
                        'grid' => $this->aportesVoluntarios(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.3.detalle_individual', [])
                        ),
                    ],
                    [
                        'title' => 'Aportes carga masiva',
                        'grid' => $this->aportesVoluntariosMasivo(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.3.detalle_individual', [])
                        ),
                    ],
                ];
            case 'asignacion_monto_asociado':
                return [
                    [
                        'title' => 'Ajustes de haberes',
                        'grid' => $this->ajustesDeHaberes(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.4.detalle', [])
                        ),
                    ],
                ];
            case 'asignacion_monto_patrono':
                return [
                    [
                        'title' => 'Ajustes de haberes',
                        'grid' => $this->ajustesDeHaberes(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.5.detalle', [])
                        ),
                    ],
                ];
            case 'dividendos':
                return [
                    [
                        'title' => 'Dividendos',
                        'grid' => $this->dividendos(
                            $this->fichaLiquidacion->find('asignaciones.conceptos.6.detalle', [])
                        ),
                    ],
                ];
            case 'descuento_monto_asociado':
                return [
                    [
                        'title' => 'Ajustes de haberes',
                        'grid' => $this->ajustesDeHaberes(
                            $this->fichaLiquidacion->find('retiro_deducciones.conceptos.0.detalle', [])
                        ),
                    ],
                ];
            case 'descuento_monto_patrono':
                return [
                    [
                        'title' => 'Ajustes de haberes',
                        'grid' => $this->ajustesDeHaberes(
                            $this->fichaLiquidacion->find('retiro_deducciones.conceptos.1.detalle', [])
                        ),
                    ],
                ];
            case 'retiro_parcial':
                return [
                    [
                        'title' => 'Retiros parciales',
                        'grid' => $this->retirosParciales(
                            $this->fichaLiquidacion->find('retiro_deducciones.conceptos.2.detalle', [])
                        ),
                    ],
                ];
            case 'prestamos_no_afectados':
                return [
                    [
                        'title' => 'Capital de cuotas no afectadas',
                        'grid' => $this->cuotasNoAfectadas(
                            $this->fichaLiquidacion->find('capital_cartera_prestamos.conceptos.0.detalle', [])
                        ),
                    ],
                ];
            case 'prestamos_afectados_capital':
                return [
                    [
                        'title' => 'Cuotas afectadas por prenomina',
                        'grid' => $this->cuotasAfectadas(
                            $this->fichaLiquidacion->find('capital_afectado_prenomina.conceptos.0.detalle', [])
                        ),
                    ],
                ];
            case 'prestamos_afectados_interes':
                return [
                    [
                        'title' => 'Cuotas afectadas por prenomina',
                        'grid' => $this->cuotasAfectadas(
                            $this->fichaLiquidacion->find('interes_afectado_prenomina.conceptos.0.detalle', [])
                        ),
                    ],
                ];
        }
    }

    private function aportesOrdinarios($data)
    {
        $model = new AporteOrdinario('search');

        return [
            [
                'id' => 'aporte-ordinario-grid',
                'type' => 'striped bordered condensed',
                'enableSorting' => false,
                'dataProvider' => $this->dataProvider($data),
            ],
            [
                [
                    'header' => 'Nombre archivo',
                    'name' => 'nombre_archivo',
                    'value' => '$data["nombre_archivo"]',
                ],
                [
                    'header' => 'Fecha de aporte',
                    'name' => 'fecha_aporte',
                    'value' => '$data["fecha_aporte"]',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Sueldo base',
                    'name' => 'sueldo_base',
                    'value' => '$data["sueldo_base"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Aporte patrono',
                    'name' => 'aporte_patrono',
                    'value' => '$data["aporte_patrono"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Aporte asociado',
                    'name' => 'aporte_asociado',
                    'value' => '$data["aporte_asociado"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Total aporte',
                    'name' => 'total_aporte',
                    'value' => '$data["total_aporte"]',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Aporte cargado',
                    'name' => 'aporte_pagado',
                    'value' => '$data["aporte_pagado"]',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Aporte por pagar',
                    'name' => 'aporte_x_pagar',
                    'value' => '$data["aporte_x_pagar"]',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Tipo de nómina',
                    'name' => 'tipo_nomina',
                    'value' => '$data["tipo_nomina"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'estatus_aporte',
                    'value' => '$data["estatus_aporte"]',
                    'type' => 'raw',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
            ],
        ];
    }

    private function aportesVoluntarios($data)
    {
        return [
            'id' => 'aporte-voluntario-individual-grid',
            'type' => 'striped bordered condensed',
            'dataProvider' => $this->dataProvider($data),
            'columns' => [
                [
                    'header' => 'Código',
                    'name' => 'codigo',
                    'value' => '$data["codigo"]',
                ],
                [
                    'header' => 'Monto',
                    'name' => 'monto',
                    'value' => '$data["monto"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Aportante',
                    'name' => 'id_aportante',
                    'value' => '$data["aportante"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Fecha de pago',
                    'name' => 'fecha_pago',
                    'value' => '$data["fecha_pago"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Tipo de operación',
                    'name' => 'opcion_retiro',
                    'header' => 'Tipo Operación',
                    'value' => '$data["opcion_retiro"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Fecha de registro',
                    'name' => 'fecha_registro',
                    'value' => '$data["fecha_registro"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'estatus',
                    'value' => '$data["nombre_estatus"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
            ],
        ];
    }

    private function aportesVoluntariosMasivo($data)
    {
        return [
            'id' => 'aporte-voluntario-masivo-grid',
            'type' => 'striped bordered condensed',
            'dataProvider' => $this->dataProvider($data),
            'columns' => [
                [
                    'header' => 'Aportante',
                    'name' => 'id_aportante',
                    'value' => '$data["aportante"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Tipo de operación',
                    'name' => 'opcion_retiro',
                    'value' => '$data["opcion_retiro"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Monto cargado',
                    'name' => 'monto_cargado',
                    'value' => '$data["monto_cargado"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                    'type' => 'number',
                ],
                [
                    'header' => 'Monto retirado',
                    'name' => 'monto_retirado',
                    'value' => '$data["monto_retirado"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                    'type' => 'number',
                ],
                [
                    'header' => 'Comisión retira',
                    'name' => 'monto_comision_retira',
                    'value' => '$data["monto_comision_retira"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                    'type' => 'number',
                ],
                [
                    'header' => 'Monto capitalizado',
                    'name' => 'monto_capitalizado',
                    'value' => '$data["monto_capitalizado"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                    'type' => 'number',
                ],
                [
                    'header' => 'Comisión capitalización',
                    'name' => 'monto_comision_capitaliza',
                    'value' => '$data["monto_comision_capitaliza"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                    'type' => 'number',
                ],
                [
                    'header' => 'Fecha pago',
                    'name' => 'fecha_pagos',
                    'value' => '$data["fecha_pagos"]',
                    'class' => 'SYDateColumn',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'nombre_estatus',
                    'value' => '$data["nombre_estatus"]',
                    'type' => 'raw',
                ],
            ],
        ];
    }

    private function ajustesDeHaberes($data)
    {
        return [
            'id' => 'ajuste-haberes-grid',
            'type' => 'striped bordered condensed',
            'dataProvider' => $this->dataProvider($data),
            'columns' => [
                [
                    'header' => 'Número de operación',
                    'name' => 'numero_operacion',
                    'value' => '$data["numero_operacion"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Fecha de operación',
                    'name' => 'fecha_operacion',
                    'value' => '$data["fecha_operacion"]',
                    'class' => 'SYDateColumn',
                ],
                [
                    'header' => 'Concepto de ajuste',
                    'name' => 'id_concepto_ajuste',
                    'value' => '$data["concepto_ajuste"]',
                ],
                [
                    'header' => 'Tipo de ajuste',
                    'name' => 'id_tipo_ajuste',
                    'value' => '$data["tipo_ajuste"]',
                ],
                [
                    'header' => 'Tipo de movimiento',
                    'name' => 'id_tipo_movimiento',
                    'value' => '$data["tipo_movimiento"]',
                ],
                [
                    'header' => 'Monto asociado',
                    'name' => 'monto_asociado',
                    'value' => '$data["monto_asociado"]',
                    'type' => 'number',
                ],
                [
                    'header' => 'Monto patrono',
                    'name' => 'monto_patrono',
                    'value' => '$data["monto_patrono"]',
                    'type' => 'number',
                ],
                [
                    'header' => 'Fecha de estatus',
                    'name' => 'fecha_estatus',
                    'value' => '$data["fecha_estatus"]',
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'id_estatus',
                    'value' => '$data["estatus"]',
                ],
            ],
        ];
    }

    private function dividendos($datos)
    {
        return [
            'id' => 'dividendos-grid',
            'type' => 'striped bordered condensed',
            'dataProvider' => $this->dataProvider($datos),
            'columns' => [
                [
                    'header' => 'Año',
                    'name' => 'ano',
                    'value' => '$data["ano"]',
                ],
                [
                    'header' => 'Monto',
                    'name' => 'dividendo',
                    'value' => '$data["dividendo"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Fecha de registro',
                    'name' => 'fecha_registro',
                    'value' => '$data["fecha_registro"]',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'activo',
                    'value' => '$data["activo"]',
                ],
            ],
        ];
    }

    private function retirosParciales($data)
    {
        return [
            'id' => 'retiro-parcial-grid',
            'type' => 'striped bordered condensed',
            'dataProvider' => $this->dataProvider($data),
            'columns' => [
                [
                    'header' => 'Fecha solicitud',
                    'name' => 'fecha_registro',
                    'value' => '$data["fecha_registro"]',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Monto aportes',
                    'name' => 'monto_total_disponible',
                    'value' => '$data["monto_total_disponible"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right'],
                ],
                [
                    'header' => 'Monto solicitado',
                    'name' => 'monto_solicitado',
                    'header' => 'Monto Solicitado',
                    'value' => '$data["monto_solicitado"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right', 'class' => 'td_monto_solicitado_rp'],
                ],
                [
                    'header' => 'Monto aprobado',
                    'name' => 'monto_aprobado',
                    'header' => 'Monto Aprobado',
                    'value' => '$data["monto_aprobado"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right', 'class' => 'td_monto_aprobado_rp'],
                ],
                [
                    'header' => 'Fecha estatus',
                    'name' => 'fecha_estatus',
                    'value' => '$data["fecha_estatus"]',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'idEstatus',
                    'value' => '$data["estatus"]',
                    'htmlOptions' => ['style' => 'text-align: center'],
                ],
            ],
        ];
    }

    private function cuotasNoAfectadas($datos)
    {
        return [
            'id' => 'detalle-comprobante-grid',
            'enableSorting' => false,
            'dataProvider' => $this->dataProvider($datos),
            'columns' => [
                [
                    'header' => 'Número cuota',
                    'name' => 'numero_cuota',
                    'value' => '$data["numero_cuota"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Fecha de vencimiento',
                    'name' => 'fecha_vencimiento',
                    'value' => '! empty($data["fecha_vencimiento"]) ? $data["fecha_vencimiento"] : null',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center;'],
                ],
                [
                    'header' => 'Monto capital',
                    'name' => 'monto_capital',
                    'value' => '$data["monto_capital"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Monto interes',
                    'name' => 'monto_interes',
                    'value' => '$data["monto_interes"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Monto de la cuota',
                    'name' => 'monto_cuota',
                    'value' => '$data["monto_cuota"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Monto pagado',
                    'name' => 'monto_pagado',
                    'value' => '$data["monto_pagado"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
            ],
        ];
    }

    private function cuotasAfectadas($datos)
    {
        return [
            'id' => 'detalle-comprobante-grid',
            'enableSorting' => false,
            'dataProvider' => $this->dataProvider($datos),
            'columns' => [
                [
                    'header' => 'Número',
                    'name' => 'numero_cuota',
                    'value' => '$data["numero_cuota"]',
                    'htmlOptions' => ['style' => 'width: 1%; text-align: right;'],
                ],
                [
                    'header' => 'Nombre de archivo',
                    'name' => 'nombre_archivo',
                    'value' => '$data["nombre_archivo"]',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Fecha de vencimiento',
                    'name' => 'fecha_vencimiento',
                    'value' => '$data["fecha_vencimiento"] ? $data["fecha_vencimiento"] : ""',
                    'type' => 'date',
                    'htmlOptions' => ['style' => 'text-align: center;'],
                ],
                [
                    'header' => 'Capital',
                    'name' => 'monto_capital',
                    'value' => '$data["monto_capital"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Capital pendiente',
                    'name' => 'monto_capital_pendiente',
                    'value' => '$data["monto_capital_pendiente"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Interes',
                    'name' => 'monto_interes',
                    'value' => '$data["monto_interes"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Interes pendiente',
                    'name' => 'monto_interes_pendiente',
                    'value' => '$data["monto_interes_pendiente"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Monto de la cuota',
                    'name' => 'monto_cuota',
                    'value' => '$data["monto_cuota"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Monto pagado',
                    'name' => 'monto_pagado',
                    'value' => '$data["monto_pagado"]',
                    'type' => 'number',
                    'htmlOptions' => ['style' => 'text-align: right;'],
                ],
                [
                    'header' => 'Estatus',
                    'name' => 'nombre_estatus',
                    'value' => '$data["nombre_estatus"]',
                    'htmlOptions' => ['style' => 'text-align: center;'],
                ],
            ],
        ];
    }
}
