<?php

/**
 * Subclass for performing query and update operations on the 'katao_invoice' table.
 *
 * @package lib.model
 */
class KataoInvoicePeer extends BaseKataoInvoicePeer {
    public static function getAllStatuses() {
        return array(KataoInvoice::STATUS_GENERATED => self::getStatusStr(KataoInvoice::STATUS_GENERATED), KataoInvoice::STATUS_DELIVERED => self::getStatusStr(KataoInvoice::STATUS_DELIVERED), KataoInvoice::STATUS_VALIDATED => self::getStatusStr(KataoInvoice::STATUS_VALIDATED), KataoInvoice::STATUS_SENT => self::getStatusStr(KataoInvoice::STATUS_SENT));
    }

    public static function getStatusStr($status) {
        switch ($status) {
            case KataoInvoice::STATUS_GENERATED:
                return 'Générée';
            case KataoInvoice::STATUS_DELIVERED:
                return 'Délivrée';
            case KataoInvoice::STATUS_VALIDATED:
                return 'Validée';
            case KataoInvoice::STATUS_SENT:
                return 'Envoyée';
        }
        return '';
    }

    /**
     * KataoInvoicePeer::countAll()
     *
     * @param int $katao_node_id
     * @return int
     */
    public static function countAll($katao_node_id) {
        $criteria = new Criteria();
        if ($katao_node_id) {
            $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        }
        return self::doCount($criteria);
    }

    /**
     * KataoInvoicePeer::sumAll()
     *
     * @param int $katao_node_id
     * @param int $year
     * @return float
     */
    public static function sumAll($katao_node_id = false, $year = false) {
        $return = 0;

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoInvoiceProductPeer::KATAO_INVOICE_ID);
        $criteria->addAsColumn('sum_products', sprintf('SUM(%s * %s)', KataoInvoiceProductPeer::PRODUCT_PRICE_EURO, KataoInvoiceProductPeer::QUANTITY));
        $criteria->addJoin(KataoInvoiceProductPeer::KATAO_INVOICE_ID, self::ID);
        if ($katao_node_id) {
            $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        }
        if ($year) {
            $criteria->add(self::CREATED_AT, sprintf('DATE_FORMAT(%s, "%%Y") = %d', self::CREATED_AT, $year), Criteria::CUSTOM);
        }
        $criteria->addGroupByColumn(KataoInvoiceProductPeer::KATAO_INVOICE_ID);
    	$criteria->setDistinct();

        $rs = KataoInvoiceProductPeer::doSelectRS($criteria);
        while ($rs->next()) {
            $return += $rs->getFloat(2);
        }

        return $return;
    }

    /**
     * KataoInvoicePeer::addAll()
     *
     * @param int $katao_node_id
     * @param int $year
     * @return float
     */
    public static function addAll($katao_node_id = false, $year = false) {
        $return = 0;

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoInvoiceProductPeer::KATAO_INVOICE_ID);
        $criteria->addAsColumn('sum_quantities', sprintf('SUM(%s)', KataoInvoiceProductPeer::QUANTITY));
        $criteria->addJoin(KataoInvoiceProductPeer::KATAO_INVOICE_ID, self::ID);
        if ($katao_node_id) {
            $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        }
        if ($year) {
            $criteria->add(self::CREATED_AT, sprintf('DATE_FORMAT(%s, "%%Y") = %d', self::CREATED_AT, $year), Criteria::CUSTOM);
        }
        $criteria->addGroupByColumn(KataoInvoiceProductPeer::KATAO_INVOICE_ID);
    	$criteria->setDistinct();

        $rs = KataoInvoiceProductPeer::doSelectRS($criteria);
        while ($rs->next()) {
            $return += $rs->getInt(2);
        }

        return $return;
    }
}