<?php

function sc_calculate_sales_summary($calculation_id, $calculation_type, $month_id, $month_begin_date, $month_end_date)
{
    delete_monthly_summary_sales($month_id);

    $result = get_all_list_values("DEPARTMENTS");
    
    while ($department = db_fetch($result)) {    
        sc_calculate_monthly_department_sales_invoices_summary($month_id, $department['id'],  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date);
        
        sc_calculate_monthly_department_credit_notes_summary($month_id, $department['id'],  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date);
        
        sc_calculate_monthly_department_sales_invoice_receipts_summary($month_id, $department['id'],  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date);
        
        sc_calculate_monthly_department_sales_credit_note_payments_summary($month_id, $department['id'],  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date);
    }

}

function sc_calculate_monthly_department_sales_invoice_receipts_summary($month_id, $department_id,  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date){
    
    if($department_id == 1651){
        $ss= 2222;
    }
    $result = get_monthly_summary_department_invoice_receipts($department_id, $month_begin_date, $month_end_date);
    
    while ($project_sales = db_fetch($result))
    {
        add_to_monthly_summary_sales($month_id, $department_id, $project_sales['project_id'], 
                                    SCVT_SALES_INVOICE_RECEIPTS, $calculation_type, 
                                    $project_sales['receipts_total'], 0, 
                                    0, 0, 
                                    $calculation_id);
    }

}


function sc_calculate_monthly_department_sales_credit_note_payments_summary($month_id, $department_id,  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date){
    
    if($department_id == 1651){
        $ss= 2222;
    }
    $result = get_monthly_summary_department_credit_note_payments($department_id, $month_begin_date, $month_end_date);
    
    while ($project_sales = db_fetch($result))
    {
        add_to_monthly_summary_sales($month_id, $department_id, $project_sales['project_id'], 
                                    SCVT_SALES_CREDIT_NOTE_PAYMENTS, $calculation_type, 
                                    $project_sales['payments_total'], 0, 
                                    0, 0, 
                                    $calculation_id);
    }

}


function sc_calculate_monthly_department_sales_invoices_summary($month_id, $department_id,  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date){
    
    if($department_id == 1651){
        $ss= 2222;
    }
    $result = get_monthly_summary_department_invoices($department_id, $month_begin_date, $month_end_date);
    
    while ($project_sales = db_fetch($result))
    {
        add_to_monthly_summary_sales($month_id, $department_id, $project_sales['project_id'], 
                                    SCVT_SALES_INVOICES, $calculation_type, 
                                    $project_sales['inv_total'], $project_sales['inv_discount_total'], 
                                    $project_sales['inv_taxes_total'], $project_sales['inv_cost_total'], 
                                    $calculation_id);
    }
                                        
    
}


function sc_calculate_monthly_department_credit_notes_summary($month_id, $department_id,  
                                        $calculation_type, $calculation_id, $month_begin_date, $month_end_date){
    
    if($department_id == 1651){
        $ss= 2222;
    }
    $result = get_monthly_summary_department_credit_notes($department_id, $month_begin_date, $month_end_date);
    
    while ($project_sales = db_fetch($result))
    {
        add_to_monthly_summary_sales($month_id, $department_id, $project_sales['project_id'], 
                                    SCVT_SALES_CREDIT_NOTES, $calculation_type, 
                                    -$project_sales['cn_total'], -$project_sales['cn_discount_total'], 
                                    -$project_sales['cn_taxes_total'], -$project_sales['cn_cost_total'], 
                                    $calculation_id);
    }
                                        
    
}