<?php

/**
 * Subclass for performing query and update operations on the 'katao_order' table.
 *
 * @package lib.model
 */
class KataoOrderPeer extends BaseKataoOrderPeer {
    public static function getAllStatuses() {
        return array(KataoOrder::STATUS_GENERATED => self::getStatusStr(KataoOrder::STATUS_GENERATED), KataoOrder::STATUS_ADJUSTED => self::getStatusStr(KataoOrder::STATUS_ADJUSTED), KataoOrder::STATUS_SENT => self::getStatusStr(KataoOrder::STATUS_SENT), KataoOrder::STATUS_RECEIVED => self::getStatusStr(KataoOrder::STATUS_RECEIVED), KataoOrder::STATUS_DELIVERED => self::getStatusStr(KataoOrder::STATUS_DELIVERED));
    }

    public static function getStatusStr($status) {
        switch ($status) {
            case KataoOrder::STATUS_GENERATED:
                return 'Générée';
            case KataoOrder::STATUS_ADJUSTED:
                return 'Ajustée';
            case KataoOrder::STATUS_SENT:
                return 'Envoyée';
            case KataoOrder::STATUS_RECEIVED:
                return 'Réceptionnée';
            case KataoOrder::STATUS_DELIVERED:
                return 'Distribuée';
        }
        return '';
    }

    public static function getLastDeliveryAddress($katao_node_id, $katao_supplier_id) {
        $return = '';

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::DELIVERY_ADDRESS);
        $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        $criteria->add(self::KATAO_SUPPLIER_ID, $katao_supplier_id);
        $criteria->add(self::DELIVERY_ADDRESS, null, Criteria::NOT_EQUAL);
        $criteria->add(self::DELIVERY_ADDRESS, '', Criteria::NOT_EQUAL);
        $criteria->addDescendingOrderByColumn(self::CREATED_AT);
        $criteria->setLimit(1);

        $rs = self::doSelectRS($criteria);
        if ($rs->next()) {
            $return = $rs->getString(1);
        }

        return $return;
    }

    public static function getPossibleOrdersForSupplierInvoice($katao_period_id, $katao_supplier_id) {
        $return = array();

        $criteria = new Criteria();
        $criteria->add(KataoOrderPeer::KATAO_PERIOD_ID, $katao_period_id);
        $criteria->add(KataoOrderPeer::KATAO_SUPPLIER_ID, $katao_supplier_id);
        $criteria->add(KataoOrderPeer::STATUS, KataoOrder::STATUS_SENT, Criteria::GREATER_EQUAL);
        $criteria->addAscendingOrderByColumn(KataoOrderPeer::ID);
        foreach (KataoOrderPeer::doSelect($criteria) as/*(KataoOrder)*/ $katao_order) {
            $has_been_delivered = true;
            foreach ($katao_order->getKataoOrderProducts() as/*(KataoOrderProduct)*/ $katao_order_product) {
                $has_been_delivered &= $katao_order_product->getHasBeenDelivered();
            }
            if ($has_been_delivered) {
                $return[$katao_order->getId()] = $katao_order;
            }
        }

        return $return;
    }
}