<?php

class KataoProductPeer extends BaseKataoProductPeer {
    public static function getSelectedProducts($katao_suppliers_ids = array(), $katao_period_id = false, $katao_node_id = false) {
        $return = array();
        $return['list'] = self::getAllGroupedBySupplier($katao_suppliers_ids, $katao_period_id, $katao_node_id);
        $return['selected'] = array();

        if ($katao_node_id) {
            $criteria = new Criteria();
            $criteria->addSelectColumn(KataoNodeProductPeer::KATAO_PRODUCT_ID);
            $criteria->addJoin(KataoNodeProductPeer::KATAO_PRODUCT_ID, self::ID);
            $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
            $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
            $criteria->addAnd($criterion);
            $criteria->add(KataoNodeProductPeer::KATAO_NODE_ID, $katao_node_id);
            $rs = self::doSelectRS($criteria);
            while ($rs->next()) {
                $return['selected'][] = $rs->getInt(1);
            }
        } elseif ($katao_period_id) {
            $criteria = new Criteria();
            $criteria->addSelectColumn(KataoPeriodProductPeer::KATAO_PRODUCT_ID);
            $criteria->addJoin(KataoPeriodProductPeer::KATAO_PRODUCT_ID, self::ID);
            $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
            $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
            $criteria->addAnd($criterion);
            $criteria->add(KataoPeriodProductPeer::KATAO_PERIOD_ID, $katao_period_id);
            $rs = self::doSelectRS($criteria);
            while ($rs->next()) {
                $return['selected'][] = $rs->getInt(1);
            }
        }

        return $return;
    }

    public static function getAllGroupedBySupplier($katao_suppliers_ids = array(), $katao_period_id = false, $katao_node_id = false) {
        $return = array();

        if (0 < count($katao_suppliers_ids)) {
            if ($katao_node_id && $katao_period_id) {
                $criteria = new Criteria();
                $criteria->addSelectColumn(KataoPeriodSupplierPeer::KATAO_SUPPLIER_ID);
                $criteria->addSelectColumn(KataoPeriodSupplierPeer::INCLUDE_ALL_PRODUCTS);
                $criteria->add(KataoPeriodSupplierPeer::KATAO_PERIOD_ID, $katao_period_id);
                $criteria->add(KataoPeriodSupplierPeer::KATAO_SUPPLIER_ID, $katao_suppliers_ids, Criteria::IN);
                $rs = self::doSelectRS($criteria);

                $suppliers_with_all_products = array();
                $suppliers_with_some_products = array();
                while ($rs->next()) {
                    if ($rs->getInt(2)) {
                        $suppliers_with_all_products[] = $rs->getInt(1);
                    } else {
                        $suppliers_with_some_products[] = $rs->getInt(1);
                    }
                }

                $criteria = new Criteria();
                $criteria->addSelectColumn(KataoSupplierNodePeer::KATAO_SUPPLIER_ID);
                $criteria->add(KataoSupplierNodePeer::KATAO_SUPPLIER_ID, $katao_suppliers_ids, Criteria::IN);
                $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node_id);
                $rs = self::doSelectRS($criteria);

                while ($rs->next()) {
                    $suppliers_with_all_products[] = $rs->getInt(1);
                }

                if (0 < count($suppliers_with_some_products)) {
                    /* retrieve all selected products for the current period */
                    $criteria = new Criteria();
                    $criteria->addSelectColumn(KataoSupplierPeer::NAME);
                    $criteria->addSelectColumn(self::ID);
                    $criteria->addSelectColumn(self::NAME);
                    $criteria->addJoin(self::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
                    $criteria->addJoin(self::ID, KataoPeriodProductPeer::KATAO_PRODUCT_ID);
                    $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
                    $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
                    $criteria->addAnd($criterion);
                    $criteria->add(KataoSupplierPeer::ID, $suppliers_with_some_products, Criteria::IN);
                    $criteria->add(KataoPeriodProductPeer::KATAO_PERIOD_ID, $katao_period_id);
                    $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                    $criteria->addAscendingOrderByColumn(self::NAME);

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

                if (0 < count($suppliers_with_all_products)) {
                    /* retrieve all products from suppliers with all products for the current period */
                    $criteria = new Criteria();
                    $criteria->addSelectColumn(KataoSupplierPeer::NAME);
                    $criteria->addSelectColumn(self::ID);
                    $criteria->addSelectColumn(self::NAME);
                    $criteria->addJoin(self::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
                    $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
                    $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
                    $criteria->addAnd($criterion);
                    $criteria->add(KataoSupplierPeer::ID, $suppliers_with_all_products, Criteria::IN);
                    $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                    $criteria->addAscendingOrderByColumn(self::NAME);

                    $rs = self::doSelectRS($criteria);
                    while ($rs->next()) {
                        $return[$rs->getString(1)][$rs->getInt(2)] = $rs->getString(3);
                    }
                }
            } else {
                $criteria = new Criteria();
                $criteria->addSelectColumn(KataoSupplierPeer::NAME);
                $criteria->addSelectColumn(self::ID);
                $criteria->addSelectColumn(self::NAME);
                $criteria->addJoin(self::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
                $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
                $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
                $criteria->addAnd($criterion);
                $criteria->add(KataoSupplierPeer::ID, $katao_suppliers_ids, Criteria::IN);
                $criteria->addAscendingOrderByColumn(KataoSupplierPeer::NAME);
                $criteria->addAscendingOrderByColumn(self::NAME);

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

        return $return;
    }

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

    /**
     * KataoProductPeer::getPagerForAllByProductFamilyUrlIdentifier()
     *
     * @param string $url_identifier
     * @param integer $page
     * @return
     */
    public static function getPagerForAllByProductFamilyUrlIdentifier($url_identifier, $page = 1) {
        $sf_user = sfContext::getInstance()->getUser();

        $criteria = new Criteria();
        $criteria->addJoin(self::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->add(KataoProductFamilyPeer::URL_IDENTIFIER, $url_identifier);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $pager = new sfPropelPager('KataoProduct', sfConfig::get('app_katao_pager'));
        $pager->setCriteria($criteria);
        $pager->setPage($page);
        $pager->init();
        return $pager;
    }

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

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

        $criteria = new Criteria();
        $criteria->addSelectColumn(self::NAME);
        $criteria->addSelectColumn(self::URL_IDENTIFIER);
        $criteria->addSelectColumn(KataoProductFamilyPeer::NAME);
        $criteria->addSelectColumn(KataoProductFamilyPeer::URL_IDENTIFIER);
        $criteria->addSelectColumn(KataoProductCategoryPeer::NAME);
        $criteria->addSelectColumn(KataoProductCategoryPeer::URL_IDENTIFIER);
        $criteria->addJoin(self::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
        $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->addAscendingOrderByColumn(KataoProductCategoryPeer::SORT_ORDER);
        $criteria->addAscendingOrderByColumn(KataoProductFamilyPeer::NAME);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $rs = self::doSelectRS($criteria);
        while ($rs->next()) {
            $katao_product_category_name = $rs->getString(5);
            $katao_product_category_url = $rs->getString(6);
            $katao_product_family_name = $rs->getString(3);
            $katao_product_family_url = $rs->getString(4);
            $katao_product_name = $rs->getString(1);
            $katao_product_url = $rs->getString(2);

            $return[$katao_product_category_url]['name'] = $katao_product_category_name;
            $return[$katao_product_category_url]['families'][$katao_product_family_url]['name'] = $katao_product_family_name;
            $return[$katao_product_category_url]['families'][$katao_product_family_url]['products'][$katao_product_url] = $katao_product_name;
        }

        return $return;
    }

    /**
     * KataoProductPeer::getPagerForAllBySupplierUrlIdentifier()
     *
     * @param string $url_identifier
     * @param integer $page
     * @return sfPropelPager
     */
    public static function getPagerForAllBySupplierUrlIdentifier($url_identifier, $page = 1) {
        $criteria = new Criteria();
        $criteria->addJoin(self::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->add(KataoSupplierPeer::URL_IDENTIFIER, $url_identifier);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $pager = new sfPropelPager('KataoProduct', sfConfig::get('app_katao_pager'));
        $pager->setCriteria($criteria);
        $pager->setPage($page);
        $pager->init();
        return $pager;
    }

    /**
     * KataoProductPeer::getPagerForAllByProductCategoryUrlIdentifier()
     *
     * @param string $url_identifier
     * @param integer $page
     * @return sfPropelPager
     */
    public static function getPagerForAllByProductCategoryUrlIdentifier($url_identifier, $page = 1) {
        $sf_user = sfContext::getInstance()->getUser();

        $criteria = new Criteria();
        $criteria->addJoin(self::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
        $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->add(KataoProductCategoryPeer::URL_IDENTIFIER, $url_identifier);
        $criteria->addAscendingOrderByColumn(self::NAME);

        $pager = new sfPropelPager('KataoProduct', sfConfig::get('app_katao_pager'));
        $pager->setCriteria($criteria);
        $pager->setPage($page);
        $pager->init();
        return $pager;
    }

    /**
     * KataoProductPeer::getPagerForSearch()
     *
     * @param string $search
     * @param integer $page
     * @return sfPropelPager
     */
    public static function getPagerForSearch($search, $page = 1) {
        $sf_user = sfContext::getInstance()->getUser();

        $criteria = new Criteria();
        $criteria->addJoin(self::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
        $criteria->addJoin(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, KataoProductCategoryPeer::ID);
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);

        $search_like = sprintf('%%%s%%', $search);
        $criterion = $criteria->getNewCriterion(self::REFERENCE, $search_like, Criteria::LIKE);
        $criterion->addOr($criteria->getNewCriterion(self::NAME, $search_like, Criteria::LIKE));
        $criterion->addOr($criteria->getNewCriterion(self::DESCRIPTION, $search_like, Criteria::LIKE));
        $criterion->addOr($criteria->getNewCriterion(KataoProductFamilyPeer::NAME, $search_like, Criteria::LIKE));
        $criterion->addOr($criteria->getNewCriterion(KataoProductCategoryPeer::NAME, $search_like, Criteria::LIKE));
        $criteria->addAnd($criterion);

        $criteria->addAscendingOrderByColumn(self::NAME);

        $pager = new sfPropelPager('KataoProduct', sfConfig::get('app_katao_pager'));
        $pager->setCriteria($criteria);
        $pager->setPage($page);
        $pager->init();
        return $pager;
    }

    public static function getAll() {
        $criteria = new Criteria();
        $sf_user = sfContext::getInstance()->getUser();
        if ($sf_user->isJustSupplier()) {
            $criteria->add(self::KATAO_SUPPLIER_ID, $sf_user->getInstance()->getKataoSupplierId());
        } elseif ($sf_user->isJustReferer()) {
            $criteria->addJoin(self::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
            $criteria->add(KataoSupplierPeer::REFERER_ID, $sf_user->getInstance()->getKataoMemberId());
        }
        $criteria->addAscendingOrderByColumn(self::NAME);
        return self::doSelect($criteria);
    }

    /**
     * KataoProductPeer::getPagerForAll()
     *
     * @return sfPropelPager
     */
    public static function getPagerForAll($category_id = false, $supplier_id = false) {
        $criteria = new Criteria();
        $criteria->add(self::ID, sfContext::getInstance()->getUser()->getAvailableProducts(), Criteria::IN);
        $criterion = $criteria->getNewCriterion(self::IS_ARCHIVED, null);
        $criterion->addOr($criteria->getNewCriterion(self::IS_ARCHIVED, false));
        $criteria->addAnd($criterion);
        $criteria->addAscendingOrderByColumn(self::NAME);

        if ($category_id) {
            $criteria->addJoin(self::KATAO_PRODUCT_FAMILY_ID, KataoProductFamilyPeer::ID);
            $criteria->add(KataoProductFamilyPeer::KATAO_PRODUCT_CATEGORY_ID, $category_id);
        }
        if ($supplier_id) {
            $criteria->add(self::KATAO_SUPPLIER_ID, $supplier_id);
        }

        $pager = new sfPropelPager('KataoProduct', 10000);
        $pager->setCriteria($criteria);
        $pager->setPage(1);
        $pager->init();
        return $pager;
    }
}