/**
  * Gets modifiers of default combination from the entity.
  *
  * @param string $parent_entity - entity name
  * @param int $entity_id - ID of the entity
  * @return array(
  *  'price' => sum of price modificators and values included in the combination
  * ,'weight' => sum of weight modificators and values included in the combination
  * ,'shipping_cost' => sum of shipping cost modificators and values included in the combination
  * ,'handling_cost' => sum of handling cost modificators and values included in the combination
  * )
  */
 function getModifiersOfDefaultCombination($parent_entity, $entity_id)
 {
     $modifiers = $this->__getInnerVar("_MODIFIERS");
     if (!empty($modifiers)) {
         global $application;
         $tables = $this->getTables();
         $options_table = $tables['po_options']['columns'];
         $values_table = $tables['po_options_values']['columns'];
         $query = new DB_Select();
         $query->addSelectTable('po_options');
         $query->addSelectTable('po_options_values');
         foreach ($modifiers as $mod) {
             $query->addSelectField($query->fSum($values_table[$mod . '_modifier']), $mod);
         }
         $query->WhereValue($values_table['is_default'], DB_EQ, 'Y');
         $query->WhereAND();
         $query->WhereValue($options_table['parent_entity'], DB_EQ, $parent_entity);
         $query->WhereAND();
         $query->WhereValue($options_table['entity_id'], DB_EQ, $entity_id);
         $query->WhereAND();
         $query->WhereField($values_table['option_id'], DB_EQ, $options_table['option_id']);
         return array_shift(array_values($application->db->getDB_Result($query)));
     } else {
         return array();
     }
 }
 /**
  * @param int $category_id - ID
  * @param array $period = ('begin' => timestamp, 'end' => timestamp) -
  *
  * @param int $limit -                   (
  *                          ,    STAT_NO_LIMIT)
  * @param int $what_category = STAT_CATEGORY_THIS_ONLY ||
  * STAT_CATEGORY_RECURSIVE -
  *
  * @param int $what_products = STAT_PRODUCTS_ALL ||
  * STAT_PRODUCTS_EXISTS_ONLY -                                 ,
  *
  */
 function getProductsSellingStat($category_id, $period, $limit = STAT_NO_LIMIT, $what_category = STAT_CATEGORY_THIS_ONLY, $what_products = STAT_PRODUCTS_EXISTS_ONLY)
 {
     global $application;
     $tables = $this->getTables();
     $ps_table = $tables['stat_products_sold']['columns'];
     $categories_ids = array();
     if ($what_category == STAT_CATEGORY_RECURSIVE) {
         $categories = modApiFunc('Catalog', 'getSubcategoriesFullListWithParent', $category_id, false, false);
         foreach ($categories as $cat_info) {
             $categories_ids[] = $cat_info['id'];
         }
     } else {
         $categories_ids[] = $category_id;
     }
     $query = new DB_Select();
     $query->addSelectField($ps_table['product_id'], 'product_id');
     $query->addSelectField($query->fSum($ps_table['quantity']), 'sum_quantity');
     $query->addSelectTable('stat_products_sold');
     $query->WhereValue($ps_table['categories_ids'], DB_REGEXP, '[[.vertical-line.]]' . implode('|', $categories_ids) . '[[.vertical-line.]]');
     $query->WhereAND();
     $query->Where($ps_table['time'], DB_GTE, $period['begin']);
     $query->WhereAND();
     $query->Where($ps_table['time'], DB_LTE, $period['end']);
     if ($what_products == STAT_PRODUCTS_EXISTS_ONLY) {
         $catalog_tables = modApiStaticFunc('Catalog', 'getTables');
         $query->addSelectTable('products');
         $query->WhereAND();
         $query->WhereField($ps_table['product_id'], DB_EQ, $catalog_tables['products']['columns']['id']);
     }
     $query->SelectGroup('product_id');
     $query->SelectOrder('sum_quantity', 'DESC');
     if ($limit != STAT_NO_LIMIT) {
         $query->SelectLimit(0, $limit);
     }
     return $application->db->getDB_Result($query);
 }