<?php

class KataoSupplierPeer extends BaseKataoSupplierPeer {
    /**
     * KataoSupplierPeer::retrieveByKataoIndex()
     *
     * @param int $index
     * @return KataoSupplier
     */
    public static function retrieveByKataoIndex($index) {
        $criteria = new Criteria();
        $criteria->add(self::KATAO_INDEX, $index);
        return self::doSelectOne($criteria);
    }

    public static function getAllToSortArray($katao_period_id = false, $katao_node_id = false) {
        $return = array();
        $return['all_product'] = array();
        $return['some_product'] = array();
        $return['not_select'] = array();

        if ($katao_node_id && $katao_period_id) {
            if (null != $katao_node = KataoNodePeer::retrieveByPK($katao_node_id)) {
                if (null != $katao_period = KataoPeriodPeer::retrieveByPK($katao_period_id)) {
                    $criteria = new Criteria();
                    $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                    foreach ($katao_period->getKataoPeriodSuppliersJoinKataoSupplier($criteria) as/*(KataoPeriodSupplier)*/ $katao_period_supplier) {
                        $katao_supplier = $katao_period_supplier->getKataoSupplier();
                        $return['not_select'][$katao_supplier->getId()] = $katao_supplier->getName();
                        $return['all'][$katao_supplier->getId()] = $katao_supplier->getName();
                    }

                    foreach ($katao_node->getKataoSupplierNodesJoinKataoSupplier() as/*(KataoSupplierNode)*/ $katao_supplier_node) {
                        $katao_supplier = $katao_supplier_node->getKataoSupplier();
                        $return['not_select'][$katao_supplier->getId()] = $katao_supplier->getName();
                        $return['all'][$katao_supplier->getId()] = $katao_supplier->getName();
                    }

                    $criteria = new Criteria();
                    $criteria->add(KataoNodeSupplierPeer::KATAO_PERIOD_ID, $katao_period_id);
                    $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                    foreach ($katao_node->getKataoNodeSuppliersJoinKataoSupplier($criteria) as/*(KataoNodeSupplier)*/ $katao_node_supplier) {
                        $katao_supplier = $katao_node_supplier->getKataoSupplier();

                        $return[$katao_node_supplier->getIncludeAllProducts()?'all_product':'some_product'][$katao_supplier->getId()] = $katao_supplier->getName();
                        unset($return['not_select'][$katao_supplier->getId()]);
                    }
                }
            }
        } else {
            foreach (self::getAllGlobal(true) as $katao_supplier_id => $katao_supplier_name) {
                $return['not_select'][$katao_supplier_id] = $katao_supplier_name;
                $return['all'][$katao_supplier_id] = $katao_supplier_name;
            }

            if ($katao_period_id) {
                if (null != $katao_period = KataoPeriodPeer::retrieveByPK($katao_period_id)) {
                    $criteria = new Criteria();
                    $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                    foreach ($katao_period->getKataoPeriodSuppliersJoinKataoSupplier($criteria) as/*(KataoPeriodSupplier)*/ $katao_period_supplier) {
                        $katao_supplier = $katao_period_supplier->getKataoSupplier();

                        $return[$katao_period_supplier->getIncludeAllProducts()?'all_product':'some_product'][$katao_supplier->getId()] = $katao_supplier->getName();
                        unset($return['not_select'][$katao_supplier->getId()]);
                    }
                }
            }
        }

        return $return;
    }

    public static function getAll() {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::REFERER_ID);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = array('name' => $rs->getString(2), 'url' => $rs->getString(3), 'referer' => $rs->getInt(4));
        }

        return $return;
    }

    public static function getAllSimple() {
        $return = array();
        foreach (self::getAll() as $id => $data) {
            $return[$id] = $data['name'];
        }
        return $return;
    }

    public static function getAllGlobal($only_active = false) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addJoin(self::ID, KataoProductPeer::KATAO_SUPPLIER_ID);
        $criteria->add(self::IS_GLOBAL, true);
        $criterion = $criteria->getNewCriterion(KataoProductPeer::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(KataoProductPeer::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        if ($only_active) {
            $criteria->addJoin(self::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
            $criteria->add(KataoUserPeer::STATUS, KataoUser::STATUS_ACTIVE);
        }
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = $rs->getString(2);
        }

        return $return;
    }

    /**
     * KataoSupplierPeer::retrieveByUrlIdentifier()
     *
     * @param string $url_identifier
     * @return KataoSupplier
     */
    public static function retrieveByUrlIdentifier($url_identifier) {
        $criteria = new Criteria();
        $criteria->add(self::URL_IDENTIFIER, $url_identifier);
        return self::doSelectOne($criteria);
    }

    /**
     * KataoSupplierPeer::countByAccountingCode()
     *
     * @param string $accounting_code
     * @return boolean
     */
    public static function countByAccountingCode($accounting_code) {
        $criteria = new Criteria();
        $criteria->add(self::ACCOUNTING_CODE, $accounting_code);
        return self::doCount($criteria);
    }

    public static function getAllVisible($force_all_retrieval = false) {
        $suppliers = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::ORDER_MIN_AMOUNT);
        $criteria->addSelectColumn(KataoUserPeer::STATUS);
        $criteria->add(self::IS_GLOBAL, true);
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $suppliers[$rs->getString(2)] = array('id' => $rs->getInt(1), 'url' => $rs->getString(3), 'order_min_amount' => $rs->getFloat(4), 'status' => $rs->getInt(5));
        }
				$katao_node = null;


	    	if (sfContext::getInstance()->getUser()->hasAttribute('katao_period_instance')) {
	    		$katao_node = sfContext::getInstance()->getUser()->getActiveNode();
	    	}


        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::ORDER_MIN_AMOUNT);
        $criteria->addSelectColumn(KataoUserPeer::STATUS);
        $criteria->addJoin(self::ID, KataoSupplierNodePeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
        $criteria->add(self::IS_GLOBAL, false);
        if (!$force_all_retrieval && !is_null($katao_node)) {
            $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node->getId());
        }
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $suppliers[$rs->getString(2)] = array('id' => $rs->getInt(1), 'url' => $rs->getString(3), 'order_min_amount' => $rs->getFloat(4), 'status' => $rs->getInt(5));
        }

        ksort($suppliers);

        $return = array();
        foreach ($suppliers as $supplier_name => $supplier_data) {
            $supplier_data['name'] = $supplier_name;
            $return[$supplier_data['id']] = $supplier_data;
        }

        return $return;
    }

    public static function getAllVisibleByCategory($force_all_retrieval = false) {
        $suppliers = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::ORDER_MIN_AMOUNT);
        $criteria->addSelectColumn(KataoUserPeer::STATUS);
        $criteria->addSelectColumn(KataoProductCategoryPeer::NAME);
        $criteria->add(self::IS_GLOBAL, true);
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(self::ID, KataoSupplierProductCategoryPeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(KataoSupplierProductCategoryPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->addAscendingOrderByColumn(KataoProductCategoryPeer::SORT_ORDER);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $suppliers[$rs->getString(6)][$rs->getString(2)] = array('id' => $rs->getInt(1), 'url' => $rs->getString(3), 'order_min_amount' => $rs->getFloat(4), 'status' => $rs->getInt(5));
        }

        $katao_node = sfContext::getInstance()->getUser()->getActiveNode();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::ORDER_MIN_AMOUNT);
        $criteria->addSelectColumn(KataoUserPeer::STATUS);
        $criteria->addSelectColumn(KataoProductCategoryPeer::NAME);
        $criteria->addJoin(self::ID, KataoSupplierNodePeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(self::ID, KataoUserPeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(self::ID, KataoSupplierProductCategoryPeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(KataoSupplierProductCategoryPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->add(self::IS_GLOBAL, false);
        if (!$force_all_retrieval && !is_null($katao_node)) {
            $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node->getId());
        }
        $criteria->addAscendingOrderByColumn(KataoProductCategoryPeer::SORT_ORDER);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $suppliers[$rs->getString(6)][$rs->getString(2)] = array('id' => $rs->getInt(1), 'url' => $rs->getString(3), 'order_min_amount' => $rs->getFloat(4), 'status' => $rs->getInt(5));
        }

        $return = array();
        foreach ($suppliers as $supplier_category => $category_suppliers) {
            foreach ($category_suppliers as $supplier_name => $supplier_data) {
                $supplier_data['name'] = $supplier_name;
                $return[$supplier_category][$supplier_data['id']] = $supplier_data;
            }
        }

        return $return;
    }

    public static function getAllByRefererId($referer_id) {
        $return = array();
        foreach (self::getAll() as $id => $data) {
            if (!empty($data['referer']) && $referer_id == $data['referer']) {
                $return[$id] = $data['name'];
            }
        }
        return $return;
    }

    public static function getAllLocalToNode($katao_node_id) {
        $return = array();

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addJoin(self::ID, KataoSupplierNodePeer::KATAO_SUPPLIER_ID);
        $criteria->add(self::IS_GLOBAL, true, Criteria::NOT_EQUAL);
        $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node_id);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $return[$rs->getInt(1)] = array('name' => $rs->getString(2));
        }

        return $return;
    }

    /**
     * KataoSupplierPeer::retrieveBySiretNumber()
     *
     * @param string $siret_number
     * @param int $exclude
     * @return KataoSupplier
     */
    public static function retrieveBySiretNumber($siret_number, $exclude = false) {
        $criteria = new Criteria();
        $criteria->add(self::SIRET_NUMBER, $siret_number);
        $criteria->add(self::ID, $exclude, Criteria::NOT_EQUAL);
        return self::doSelectOne($criteria);
    }

    public static function getAllJoinCart() {
        $suppliers = array();

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

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::ID);
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(self::ORDER_MIN_AMOUNT);
        $criteria->addJoin(self::ID, KataoProductPeer::KATAO_SUPPLIER_ID);
        $criteria->addJoin(KataoProductPeer::ID, KataoCartProductPeer::KATAO_PRODUCT_ID);
        $criteria->addJoin(KataoCartProductPeer::KATAO_CART_ID, KataoCartPeer::ID);
        $criteria->add(KataoCartPeer::KATAO_PERIOD_ID, $sf_user->getActivePeriod()->getId());
        $criteria->add(KataoCartPeer::KATAO_NODE_ID, $sf_user->getActiveNode()->getId());
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $suppliers[$rs->getString(2)] = array('id' => $rs->getInt(1), 'url' => $rs->getString(3), 'order_min_amount' => $rs->getFloat(4));
        }

        ksort($suppliers);

        $return = array();
        foreach ($suppliers as $supplier_name => $supplier_data) {
            $supplier_data['name'] = $supplier_name;
            $return[$supplier_data['id']] = $supplier_data;
        }

        return $return;
    }
}