<?php
require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/facturestatcommon.class.php';
class FactureStorno extends FactureStatCommon
{
    /**
     * Get HUF, CARD factures to be storno
     * 
     * @param int       $entity         Entity ID (optional, default=0)
     * 
     * @return array Arrays of records or empty
     */
    public function facturesToBeStornoHufCard(string $fromDate, string $toDate, int $entity): array
    {
        return $this->facturesToBeStorno($fromDate, $toDate, $entity, FactureStatCommon::HUF, FactureStatCommon::CARD);
    }

    /**
     * Get HUF, CARD factures to be storno
     * 
     * @param int       $entity         Entity ID (optional, default=0)
     * 
     * @return array Arrays of records or empty
     */
    public function facturesToBeStornoEurCard(string $fromDate, string $toDate, int $entity): array
    {
        return $this->facturesToBeStorno($fromDate, $toDate, $entity, FactureStatCommon::EUR, FactureStatCommon::CARD);
    }

    /**
     * Get EUR, CASH factures to be storno
     * 
     * @param int       $entity         Entity ID (optional, default=0)
     * 
     * @return array Arrays of records or empty
     */
    public function facturesToBeStornoEurCash(string $fromDate, string $toDate, int $entity): array
    {
        return $this->facturesToBeStorno($fromDate, $toDate, $entity, FactureStatCommon::EUR, FactureStatCommon::CASH);
    }

    /**
     * Get HUF, CASH factures to be storno
     * 
     * @param int       $entity         Entity ID (optional, default=0)
     * 
     * @return array Arrays of records or empty
     */
    public function facturesToBeStornoHufCash(string $fromDate, string $toDate, int $entity): array
    {
        return $this->facturesToBeStorno($fromDate, $toDate, $entity, FactureStatCommon::HUF, FactureStatCommon::CASH);
    }
    /**
     * Get all factures to be storno
     * 
     * @param int       $entity         Entity ID
     * 
     * @return array Arrays of records or empty
     */
    public function allFacturesToBeStorno(int $entity): array
    {
        $result = [];

        $sqlEntity = $this->getEntitySql($entity);

        $sql = "
            SELECT f.*
            FROM ".MAIN_DB_PREFIX."facture AS f
            {$sqlEntity}
            ORDER BY f.rowid ASC
        ";

        $rows = $this->db->query($sql);
        if ($rows) {
            while ($row = $this->db->fetch_object($rows)) {
                $result[] = (array) $row;
            }
        }

        return $result;
    }

    /**
     * Get rows from DB
     * 
     * @param int       $entity         Entity ID (optional, default=0)
     * @param string    $currency       Currency (EUR or HUF)
     * @param string    $paymentCode    Payment code (optional, possible values LIQ or CB, default='')
     * 
     * @return array Arrays of records or empty
     */
    private function facturesToBeStorno(string $fromDate, string $toDate, int $entity, string $currency, string $paymentCode): array
    {
        $result = [];
        // entity condition
        $sqlEntity = $this->getEntitySql($entity);
        // payment code condition
        $sqlPaymentCode = (empty($paymentCode)) ? '' : $this->getPaymentCodeSql($paymentCode);

        $sql = "
            SELECT f.*
            FROM ".MAIN_DB_PREFIX."facture AS f
            LEFT JOIN ".MAIN_DB_PREFIX."facture_extrafields AS fe ON fe.fk_object=f.rowid
            WHERE fe.marked_for_storno=1
            AND f.date_closing BETWEEN '{$fromDate}' AND '{$toDate}'
            AND f.multicurrency_code ILIKE '{$currency}'
            AND NOT EXISTS (
                SELECT 1
                FROM llx_facture AS sf
                WHERE sf.fk_facture_source=f.rowid
            )
            {$sqlEntity}
            {$sqlPaymentCode}
            ORDER BY f.rowid ASC
        ";

        $rows = $this->db->query($sql);
        if ($rows) {
            while ($row = $this->db->fetch_object($rows)) {
                $result[] = (array) $row;
            }
        }

        return $result;
    }
}