<?php

/**
 * Subclass for performing query and update operations on the 'katao_cart' table.
 *
 * @package lib.model
 */
class KataoCartPeer extends BaseKataoCartPeer {
    /**
     * KataoCartPeer::retrieveByUserAndPeriodAndNode()
     *
     * @param int $katao_user_id
     * @param int $katao_period_id
     * @param int $katao_node_id
     * @param bool $ignore_status
     * @return KataoCart
     */
    public static function retrieveByUserAndPeriodAndNode($katao_user_id, $katao_period_id, $katao_node_id, $ignore_status = false) {
        $criteria = new Criteria();
        $criteria->add(self::KATAO_USER_ID, $katao_user_id);
        $criteria->add(self::KATAO_PERIOD_ID, $katao_period_id);
        $criteria->add(self::KATAO_NODE_ID, $katao_node_id);
        if (!$ignore_status) {
            $criteria->add(self::STATUS, array(KataoCart::STATUS_IN_PROGRESS, KataoCart::STATUS_VALIDATED), Criteria::IN);
        }
        if (null == $return = self::doSelectOne($criteria)) {
            $return = new KataoCart();
            $return->setKataoUserId($katao_user_id);
            $return->setKataoPeriodId($katao_period_id);
            $return->setKataoNodeId($katao_node_id);
            if (KataoPeriodPeer::getActivePeriod()->getOrderEndedAt('Y-m-d') >= date('Y-m-d')) {
                $return->save();
            }
        }
        return $return;
    }

    public static function getAllStatuses() {
        return array(KataoCart::STATUS_IN_PROGRESS => self::getStatusStr(KataoCart::STATUS_IN_PROGRESS), KataoCart::STATUS_VALIDATED => self::getStatusStr(KataoCart::STATUS_VALIDATED), KataoCart::STATUS_ARCHIVED => self::getStatusStr(KataoCart::STATUS_ARCHIVED));
    }

    public static function getStatusStr($status) {
        switch ($status) {
            case KataoCart::STATUS_IN_PROGRESS:
                return 'En cours';
            case KataoCart::STATUS_VALIDATED:
                return 'Validée';
            case KataoCart::STATUS_ARCHIVED:
                return 'Terminée';
        }
        return '';
    }

    public static function getAmountStats($katao_period_id = null) {
        $return = array();

        foreach (KataoNodePeer::getAllArray() as $katao_node_id => $katao_node_city) {
            $return[$katao_node_city]['unvalidated'] = 0;
            $return[$katao_node_city]['validated'] = 0;
        }

        if(!KataoPeriodPeer::getActivePeriod()){
		return false;
	}

	$criteria = new Criteria();
        $criteria->add(self::KATAO_PERIOD_ID, !is_null($katao_period_id)?$katao_period_id:KataoPeriodPeer::getActivePeriod()->getId());
        foreach (self::doSelectJoinKataoNode($criteria) as/*(KataoCart)*/ $katao_cart) {
            $katao_node_city = $katao_cart->getKataoNode()->getCity();

            $status = 'validated';
            if (KataoCart::STATUS_IN_PROGRESS == $katao_cart->getStatus()) {
                $status = 'unvalidated';
            }

            $criteria2 = new Criteria();
            if (!is_null($katao_supplier_id)) {
                $criteria2->addJoin(KataoCartProductPeer::KATAO_PRODUCT_ID, KataoProductPeer::ID);
                $criteria2->add(KataoProductPeer::KATAO_SUPPLIER_ID, $katao_supplier_id);
            }
            foreach ($katao_cart->getKataoCartProducts($criteria2) as/*(KataoCartProduct)*/ $katao_cart_product) {
                $quantity_asked = $katao_cart_product->getQuantity();
                $quantity_adjusted = $katao_cart_product->gethasBeenAdjusted()?$katao_cart_product->getQuantityAdjusted():CONST_N_A;
                $quantity_delivered = $katao_cart_product->getKataoOrderProduct()?($katao_cart_product->getKataoOrderProduct()->getHasBeenDelivered()?$katao_cart_product->getQuantityDelivered():CONST_N_A):CONST_N_A;

                $quantity = $quantity_delivered;
                if (CONST_N_A === $quantity) {
                    $quantity = $quantity_adjusted;
                }
                if (CONST_N_A === $quantity) {
                    $quantity = $quantity_asked;
                }

                $price = $katao_cart_product->getProductPriceWithTaxes();

                $return[$katao_node_city][$status] += $quantity * $price;
            }
        }

        return $return;
    }

    public static function sumForSupplier($katao_supplier_id) {
        $return = 0;

        $sf_user =/*(myUser)*/ sfContext::getInstance()->getUser();

        $criteria = new Criteria();
        $criteria->addJoin(KataoCartProductPeer::KATAO_CART_ID, self::ID);
        $criteria->addJoin(KataoCartProductPeer::KATAO_PRODUCT_ID, KataoProductPeer::ID);
        $criteria->add(self::KATAO_PERIOD_ID, $sf_user->getActivePeriod()->getId());
        $criteria->add(self::KATAO_NODE_ID, $sf_user->getActiveNode()->getId());
        $criteria->add(KataoProductPeer::KATAO_SUPPLIER_ID, $katao_supplier_id);
        foreach (KataoCartProductPeer::doSelect($criteria) as/*(KataoCartProduct)*/ $katao_cart_product) {
            $quantity_asked = $katao_cart_product->getQuantity();
            $quantity_adjusted = $katao_cart_product->gethasBeenAdjusted()?$katao_cart_product->getQuantityAdjusted():CONST_N_A;
            $quantity_delivered = $katao_cart_product->getKataoOrderProduct()?($katao_cart_product->getKataoOrderProduct()->getHasBeenDelivered()?$katao_cart_product->getQuantityDelivered():CONST_N_A):CONST_N_A;

            $quantity = $quantity_delivered;
            if (CONST_N_A === $quantity) {
                $quantity = $quantity_adjusted;
            }
            if (CONST_N_A === $quantity) {
                $quantity = $quantity_asked;
            }

            $price = $katao_cart_product->getProductPriceWithTaxes();

            $return += $quantity * $price;
        }

        return $return;
    }
}
