<?php

function display_profit_loss_details_selecting_parameters() {  
    start_table(TABLESTYLE_NOBORDER);
	$date = today();
	if (! isset($_POST['to_date'])) {
            $_POST['to_date'] = end_month($date);
        }
	if (! isset($_POST['from_date'])) {
            $_POST['from_date'] = add_days(end_month($date), -15);
        }        
        date_cells(_("From:"), 'from_date');
	date_cells(_("To:"), 'to_date');
	
	submit_cells('Show',_("Show"),'','', 'default');
    end_table(1);
}

function calculate_profit_loss_parent_children_total($parent, &$type_totals_array, $parents_children_array) { 
    $children_period_total = $children_fina_yer_total = $children_pre_period_total = $children_pre_fina_yer_total = 0;
    
        $parent_period_total = $type_totals_array[$parent]['period_total'];
        $parent_fina_yer_total = $type_totals_array[$parent]['fina_yer_total'];
        $parent_pre_period_total = $type_totals_array[$parent]['pre_period_total'];
        $parent_pre_fina_yer_total = $type_totals_array[$parent]['pre_fina_yer_total'];
    
        foreach ($parents_children_array[$parent] as $child) {
            $children_period_total += $type_totals_array[$child]['period_total'];
            $children_fina_yer_total += $type_totals_array[$child]['fina_yer_total'];
            $children_pre_period_total += $type_totals_array[$child]['pre_period_total'];
            $children_pre_fina_yer_total += $type_totals_array[$child]['pre_fina_yer_total'];
        }
        
            $type_totals_array[$parent]['period_total'] = $parent_period_total + $children_period_total;
            $type_totals_array[$parent]['fina_yer_total'] = $parent_fina_yer_total + $children_fina_yer_total;
            $type_totals_array[$parent]['pre_period_total'] = $parent_pre_period_total + $children_pre_period_total;
            $type_totals_array[$parent]['pre_fina_yer_total'] = $parent_pre_fina_yer_total + $children_pre_fina_yer_total;
}

function display_profit_loss_group_totals(&$type_totals_array, &$parents_children_array, $depth, $previous_depth, &$tmp_parents_array) { 
    if ($previous_depth > $depth) {
        krsort($tmp_parents_array);
            foreach ($tmp_parents_array as $parent) {
                
                $this_depth = $type_totals_array[$parent]['depth'];
                $group_name = $type_totals_array[$parent]['group_name'];
                
                if ($depth > $this_depth) {
//                    return;
                }
                
                calculate_profit_loss_parent_children_total($parent, $type_totals_array, $parents_children_array);
                
                $period_total = $type_totals_array[$parent]['period_total'];
                $fina_yer_total = $type_totals_array[$parent]['fina_yer_total'];
                $pre_period_total = $type_totals_array[$parent]['pre_period_total'];
                $pre_fina_yer_total = $type_totals_array[$parent]['pre_fina_yer_total'];
                
                start_row();
//                    label_cell(spaces($this_depth * 10).text_bold("$group_name - Group Total"), 'align=left colspan=1 style=color:red');
                    label_cell(spaces($this_depth * 10).text_bold("Group Total - $group_name"), 'align=right colspan=1');
                    amount_cell($period_total, true);
                    amount_cell($fina_yer_total, true);
                    amount_cell($pre_period_total, true);
                    amount_cell($pre_fina_yer_total, true);
                start_row();

                unset($tmp_parents_array[$parent]);
            }
    } else {
        return;
    }
}

function display_profit_loss_acc_group_accounts($type, $from_date_, $to_date_, $convert, $position, $depth, $parent, $fina_yer_begin_, $pre_from_date_, 
                                                $pre_to_date_, $pre_fina_yer_begin_, $pre_fina_yer_end_) {
    $path_to_root = "../..";
		
    $dimension = $dimension2 = $period_acc_bal_total = $fina_yer_acc_bal_total = $pre_period_acc_bal_total = $pre_fina_yer_acc_bal_total = 0;
    
    $depth += 1;
	
        $display_acc_total = false;
	//Get Accounts directly under this group/type
	$result = get_gl_accounts_with_position_depth(null, null, $type);	

            while ($account = db_fetch($result)) {
                $acc_code = $account["account_code"];
                $period_acc_bal = get_gl_trans_from_to($from_date_, $to_date_, $acc_code);
                $fina_yer_acc_bal = get_gl_trans_from_to($fina_yer_begin_, $to_date_, $acc_code);
                $pre_period_acc_bal = get_gl_trans_from_to($pre_from_date_, $pre_to_date_, $acc_code);
                $pre_fina_yer_acc_bal = get_gl_trans_from_to($pre_fina_yer_begin_, $pre_fina_yer_end_, $acc_code);
                
		if ($period_acc_bal  == 0 && $fina_yer_acc_bal == 0 && $pre_period_acc_bal  == 0 && $pre_fina_yer_acc_bal == 0) {
                    continue;
                }
		
                    $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?from_date=" 
                            . $from_date_ . "&to_date=" . $to_date_ . "&Dimension=" . $dimension . "&Dimension2=" . $dimension2 
                            . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
                            ." - ". $account['account_name'] ."</a>";
                    
                    start_row();
                        label_cell(spaces($depth * 10).$url);
                        $param = "account=$acc_code&from_date=$from_date_&to_date=$to_date_";
                        $view_str = get_gl_account_trans_str($param, number_format($period_acc_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                        
                        $param = "account=$acc_code&from_date=$fina_yer_begin_&to_date=$to_date_";
                        $view_str = get_gl_account_trans_str($param, number_format($fina_yer_acc_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                        
                        $param = "account=$acc_code&from_date=$pre_from_date_&to_date=$pre_to_date_";
                        $view_str = get_gl_account_trans_str($param, number_format($pre_period_acc_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                        
                        $param = "account=$acc_code&from_date=$pre_fina_yer_begin_&to_date=$pre_fina_yer_end_";
                        $view_str = get_gl_account_trans_str($param, number_format($pre_fina_yer_acc_bal * $convert, 2));
                        label_cell($view_str, 'align=right');
                    end_row();
			
		$period_acc_bal_total += $period_acc_bal * $convert;
		$fina_yer_acc_bal_total += $fina_yer_acc_bal * $convert;
		$pre_period_acc_bal_total += $pre_period_acc_bal * $convert;
		$pre_fina_yer_acc_bal_total += $pre_fina_yer_acc_bal * $convert;
                
                $display_acc_total = true;
            }
            
            if ($display_acc_total) {
                start_row();
                    label_cell(text_bold('Accounts Total'), 'align=right colspan=1');
                    amount_cell($period_acc_bal_total, true);
                    amount_cell($fina_yer_acc_bal_total, true);
                    amount_cell($pre_period_acc_bal_total, true);
                    amount_cell($pre_fina_yer_acc_bal_total, true);
                end_row();
            }
            
            return array('period_total' => $period_acc_bal_total, 'fina_yer_total' => $fina_yer_acc_bal_total, 
                         'pre_period_total' => $pre_period_acc_bal_total, 'pre_fina_yer_total' => $pre_fina_yer_acc_bal_total, 
                         'parent' => $parent, 'depth' => $depth, 'position' => $position);
}

function display_profit_and_loss_details($from_date_, $to_date_, $fina_yer_begin_, $fina_yer_end_) {
    global $Ajax;
    
    $pre_from_date_ = add_months($from_date_, -12);
    $pre_to_date_ = add_months($to_date_, -12);
    $pre_fina_yer_begin_ = add_months($fina_yer_begin_, -12);
    $pre_fina_yer_end_ = add_months($fina_yer_end_, -12);
    
    $Ajax->activate('details');
    div_start('details');
    start_table(TABLESTYLE, "width=70%");
    
        $type_totals_array = $parents_children_array = $class_totals_array = array();

        $clz_id = 0;
        $classes = get_account_classes(false, 0);
            while ($class = db_fetch($classes)) {
                $clz_id = $class['cid'];
                $class_name = $class["class_name"];
                
                $class_totals_array[$class_name] = array('clz_period_total' => 0, 'clz_fina_yer_total' => 0, 'clz_pre_period_total' => 0, 
                                                         'clz_pre_fina_yer_total' => 0);
                
                $clz_row = get_account_class($clz_id);
                $convert = get_class_type_convert($clz_row["ctype"]); 
                
                table_section_title($class_name, "5 style=background-color:#0ec9ba");
                
                    $th = array(_("Account Name/ Group"), _("This Period<br>($from_date_ -<br> $to_date_)"), 
                                _("This Financial Year<br>($fina_yer_begin_ -<br> $pre_to_date_)"), 
                                _("Previous Period<br>($pre_from_date_ -<br> $pre_to_date_)"), 
                                _("Previous Financial Year<br>($pre_fina_yer_begin_ -<br> $pre_fina_yer_end_)"));
                    table_header($th);

                    $tmp_parents_array = array();
                    $previous_depth = $k = 0;
                    
                    $result = get_account_class_account_types($clz_id, false);
                        while ($row = db_fetch($result)) {
                            $parent = $row["parent"];
                            $depth = $row["depth"];
                            $position = $row["position"];
                            
                            display_profit_loss_group_totals($type_totals_array, $parents_children_array, $depth, $previous_depth, $tmp_parents_array);
                            
                            alt_table_row_color($k);
                                label_cell(spaces(($depth) * 10).text_bold($row["name"]), 'colspan=5');
                            end_row();

                            $ar = display_profit_loss_acc_group_accounts($row["id"], $from_date_, $to_date_, $convert, $position, $depth, $parent, 
                                                                         $fina_yer_begin_, $pre_from_date_, $pre_to_date_, $pre_fina_yer_begin_, 
                                                                         $pre_fina_yer_end_);
                            
                            if ($parent > 0) {
                                $tmp_parents_array[$parent] = $parent;
                                $parents_children_array[$parent][$row["id"]] = $row["id"];
                            }
                            
                            $type_totals_array[$row["id"]] = array('period_total' => $ar['period_total'], 'fina_yer_total' => $ar['fina_yer_total'], 
                                                                   'pre_period_total' => $ar['pre_period_total'], 
                                                                   'pre_fina_yer_total' => $ar['pre_fina_yer_total'], 'depth' => $depth, 
                                                                   'position' => $position, 'parent' => $parent, 'group_name' => $row["name"]);
                            
                            $class_totals_array[$class_name]['clz_period_total'] += $ar['period_total'];
                            $class_totals_array[$class_name]['clz_fina_yer_total'] += $ar['fina_yer_total'];
                            $class_totals_array[$class_name]['clz_pre_period_total'] += $ar['pre_period_total'];
                            $class_totals_array[$class_name]['clz_pre_fina_yer_total'] += $ar['pre_fina_yer_total'];
                            
                            $previous_depth = $depth;
                        }
                        
                        if (count($tmp_parents_array) > 0) {
                            display_profit_loss_group_totals($type_totals_array, $parents_children_array, 0, $previous_depth, $tmp_parents_array);
                        }
                        
                        display_profit_and_loss_class_total($class_name, $class_totals_array);
            }

    end_table(2);
    div_end();
}

function display_profit_and_loss_class_total($class_name, &$class_totals_array) {
    start_row();
        label_cell(("Total $class_name"), 'align=right colspan=1 style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_period_total'], false, 'style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_fina_yer_total'], false, 'style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_pre_period_total'], false, 'style=color:blue');
        amount_cell($class_totals_array[$class_name]['clz_pre_fina_yer_total'], false, 'style=color:blue');
    end_row();
    
    if ($class_name == 'Costs') {
        $gp_period_total = $class_totals_array['Income']['clz_period_total'] - abs($class_totals_array[$class_name]['clz_period_total']);
        $gp_fina_yer_total = $class_totals_array['Income']['clz_fina_yer_total'] - abs($class_totals_array[$class_name]['clz_fina_yer_total']);
        $gp_pre_period_total = $class_totals_array['Income']['clz_pre_period_total'] - abs($class_totals_array[$class_name]['clz_pre_period_total']);
        $gp_pre_fina_yer_total = $class_totals_array['Income']['clz_pre_fina_yer_total'] - abs($class_totals_array[$class_name]['clz_pre_fina_yer_total']);
            
        start_row();
            label_cell(text_bold("Gross Profit"), 'align=right colspan=1 style=color:blue');
            amount_cell($gp_period_total, true, 'style=color:blue');
            amount_cell($gp_fina_yer_total, true, 'style=color:blue');
            amount_cell($gp_pre_period_total, true, 'style=color:blue');
            amount_cell($gp_pre_fina_yer_total, true, 'style=color:blue');
        end_row();
    }
    
    if ($class_name == 'Expenses') {
        $gp_period_total = $class_totals_array['Income']['clz_period_total'] - abs($class_totals_array['Costs']['clz_period_total']);
        $gp_fina_yer_total = $class_totals_array['Income']['clz_fina_yer_total'] - abs($class_totals_array['Costs']['clz_fina_yer_total']);
        $gp_pre_period_total = $class_totals_array['Income']['clz_pre_period_total'] - abs($class_totals_array['Costs']['clz_pre_period_total']);
        $gp_pre_fina_yer_total = $class_totals_array['Income']['clz_pre_fina_yer_total'] - abs($class_totals_array['Costs']['clz_pre_fina_yer_total']);
        
        $np_period_total = $gp_period_total - abs($class_totals_array[$class_name]['clz_period_total']);
        $np_fina_yer_total = $gp_fina_yer_total - abs($class_totals_array[$class_name]['clz_fina_yer_total']);
        $np_pre_period_total = $gp_pre_period_total - abs($class_totals_array[$class_name]['clz_pre_period_total']);
        $np_pre_fina_yer_total = $gp_pre_fina_yer_total - abs($class_totals_array[$class_name]['clz_pre_fina_yer_total']);
        
        start_row();
            label_cell(text_bold("Net Income/(Loss)"), 'align=right colspan=1 style=color:blue');
            amount_cell($np_period_total, true, 'style=color:blue');
            amount_cell($np_fina_yer_total, true, 'style=color:blue');
            amount_cell($np_pre_period_total, true, 'style=color:blue');
            amount_cell($np_pre_fina_yer_total, true, 'style=color:blue');
        end_row();
    }
}

?>