<?php

function get_summary_sales_value_total($month_id, $value_types_in)
{
    $sql = "SELECT SUM(order_total) order_total, SUM(discounts_total) discounts_total, SUM(taxes_total) taxes_total, SUM(total_cost) costs_total 
            FROM ".TB_PREF."monthly_summary_sales  
            WHERE month_id = ". db_escape($month_id)."  AND  
                    value_type IN (".$value_types_in.")";
    
    $result = db_query($sql);
    return db_fetch($result);
}


function get_summary_sales_month_range_departments($company_codes_in, $salary_components_in, $from_month_display_order, $to_month_display_order)
{
    $sql = "SELECT DISTINCT s.department_code 
            FROM ".TB_PREF."monthly_summary_sales s   
                JOIN ".TB_PREF."months m   
                    ON p.month_id = m.id 
            WHERE m.display_order >= ". db_escape($from_month_display_order)."  AND 
                    m.display_order <= ". db_escape($to_month_display_order)."  AND 
                    company_code IN (".$company_codes_in.") AND 
                    value_type IN (".$salary_components_in.")";
    
    return db_query($sql);
    
}

function get_summary_sales_month_range_company_values($value_types_in, $from_month_display_order, $to_month_display_order)
{
    $sql = "SELECT SUM(s.order_total) order_total, SUM(s.discounts_total) discounts_total, SUM(s.taxes_total) taxes_total    
            FROM ".TB_PREF."monthly_summary_sales s                 
                JOIN ".TB_PREF."months m   
                    ON s.month_id = m.id 
            WHERE m.display_order >= ". db_escape($from_month_display_order)."  AND 
                    m.display_order <= ". db_escape($to_month_display_order)."  AND   
                    s.value_type IN (".$value_types_in.") 
            ORDER BY order_total DESC ";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

function get_summary_sales_month_range_department_total($department_ids_in, $value_types_in, $from_month_display_order, $to_month_display_order)
{
    $sql = "SELECT SUM(s.order_total) order_total, SUM(s.discounts_total) discounts_total, SUM(s.taxes_total) taxes_total    
            FROM ".TB_PREF."monthly_summary_sales s                 
                JOIN ".TB_PREF."months m   
                    ON s.month_id = m.id 
            WHERE m.display_order >= ". db_escape($from_month_display_order)."  AND 
                    m.display_order <= ". db_escape($to_month_display_order)."  AND  
                    s.department_id IN (".$department_ids_in.") AND 
                    s.value_type IN (".$value_types_in.") 
            ORDER BY order_total DESC ";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}



function get_summary_sales_month_range_project_values($project_ids_in, $value_types_in, $from_month_display_order, $to_month_display_order)
{
    $sql = "SELECT SUM(s.order_total) order_total, SUM(s.discounts_total) discounts_total, SUM(s.taxes_total) taxes_total    
            FROM ".TB_PREF."monthly_summary_sales s                 
                JOIN ".TB_PREF."months m   
                    ON s.month_id = m.id 
            WHERE m.display_order >= ". db_escape($from_month_display_order)."  AND 
                    m.display_order <= ". db_escape($to_month_display_order)."  AND  
                    s.project_id IN (".$project_ids_in.") AND 
                    s.value_type IN (".$value_types_in.")";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}