<?php
namespace Incoviba\Service\Contabilidad\Informe\Tesoreria\Input\Excel;

use stdClass;
use PhpOffice\PhpSpreadsheet;

class SaldosCuentas extends Sheet
{
    public function load(PhpSpreadsheet\Spreadsheet $excel): array
    {
        if (!$excel->sheetNameExists('Saldos cuentas')) {
            return [];
        }
        $sheet = $excel->getSheetByName('Saldos cuentas');
        $data = [];
        $data['anterior'] = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($this->getCalculatedValue($sheet, 'C4'));
        $data['actual'] = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($this->getCalculatedValue($sheet, 'C5'));
        $data['uf'] = $this->getCalculatedValue($sheet, 'F4');
        $data['usd'] = $this->getCalculatedValue($sheet, 'F5');

        $titleRow = 7;
        $data['sociedades'] = $this->getSociedades($sheet, $titleRow);

        $titleRow += count($data['sociedades']);
        while ($sheet->getCell("B{$titleRow}")->getValue() !== 'EMPRESA') {
            $titleRow ++;
        }
        $data['movimientos'] = $this->getMovimientos($sheet, $titleRow);

        return $data;
    }

    protected function getSociedades(PhpSpreadsheet\Worksheet\Worksheet $sheet, int $titleRow = 7, int $startColumn = 2): array
    {
        $totalsColumn = $startColumn;
        while ($sheet->getCell([$totalsColumn, $titleRow])->getValue() !== null && $sheet->getCell([$totalsColumn, $titleRow])->getValue() !== 'Total Cuentas') {
            $totalsColumn ++;
        }
        $rowIndex = $titleRow + 1;
        $sociedades = [];
        while ($rowIndex < 100 and $sheet->getCell("B{$rowIndex}")->getValue() !== 'TOTAL') {
            $sociedad = new stdClass();
            $sociedad->razon = $sheet->getCell("B{$rowIndex}")->getValue();
            $sociedad->cuentas = [];
            $cuentaColumnIndex = 3;
            $cuenta = new stdClass();
            while ($cuentaColumnIndex < $totalsColumn) {
                $cuenta->banco = $sheet->getCell([$cuentaColumnIndex ++, $rowIndex])->getValue() ?? '';
                $cuenta->numero = $sheet->getCell([$cuentaColumnIndex ++, $rowIndex])->getValue() ?? '';
                $cuenta->anterior = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);
                $cuenta->actual = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);
                $cuenta->diferencia = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);
                $cuenta->ffmm = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);
                $cuenta->deposito = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);
                $cuenta->saldo = $this->getCalculatedValue($sheet, $cuentaColumnIndex ++, $rowIndex);

                $sociedad->cuentas []= $cuenta;

                if ($sheet->getCell([$cuentaColumnIndex, $rowIndex])->getValue() === null) {
                    break;
                }
                $cuenta = new stdClass();
            }
            $sociedades []= $sociedad;
            $rowIndex ++;
        }
        return $sociedades;
    }
    protected function getMovimientos(PhpSpreadsheet\Worksheet\Worksheet $sheet, int $titleRow, int $startColumn = 2): array
    {
        $data = [];
        $titles = $this->getTableTitles($sheet, $titleRow, $startColumn, 15);

        $totalRow = $titleRow + 2;
        $data['total'] = $this->getCalculatedValue($sheet, "V{$totalRow}");
        $rowIndex = $titleRow + 4;
        $depositos = [];
        while ($sheet->getCell("B{$rowIndex}")->getValue() !== 'INGRESOS') {
            if ( $sheet->getCell("B{$rowIndex}")->getValue() !== null) {
                $depositos []= $this->getRow($sheet, $titles, $titleRow, $rowIndex, $startColumn);
            }
            $rowIndex ++;
        }
        $data['depositos'] = $depositos;

        $rowIndex ++;
        $ingresos = [];
        while ($sheet->getCell("B{$rowIndex}")->getValue() !== 'EGRESOS') {
            if ($sheet->getCell("B{$rowIndex}")->getValue() !== null) {
                $ingresos []= $this->getRow($sheet, $titles, $titleRow, $rowIndex, $startColumn);
            }
            $rowIndex ++;
        }
        $data['ingresos'] = $ingresos;

        $rowIndex ++;
        $egresos = [];
        while ($sheet->getCell("B{$rowIndex}")->getValue() !== 'TOTAL') {
            if ($sheet->getCell("B{$rowIndex}")->getValue() === null) {
                break;
            }
            $egresos [] = $this->getRow($sheet, $titles, $titleRow, $rowIndex, $startColumn);
            $rowIndex ++;
        }
        $data['egresos'] = $egresos;

        return $data;
    }
}
