<?php

function dashboard_sales_pnl($from_month_display_order, $to_month_display_order, $entry_type_id=0) {
    global $systypes_array;
    
    $month_ranges_array = dashboard_get_month_ranges_array($from_month_display_order, $to_month_display_order);
    $table_width = dashboard_calculate_table_width($month_ranges_array);
    
    start_table(TABLESTYLE, "width$table_width%");
        
        $th = array("Type");
            foreach ($month_ranges_array as $month) {
                $th[] = $month['month_name'];
            }
            $th[] = "Total";

            table_header($th);
            
            $from_month = get_month_by_display_order($from_month_display_order);
            $to_month = get_month_by_display_order($to_month_display_order);
            
            $from_date = $from_month['month_begin_date'];
            $to_date = $to_month['month_end_date'];
            
            $k = $grand_total = 0; $total_balance_array = array();
            
            $result = dashboard_get_sales_pnl_types($from_date, $to_date, $entry_type_id);
                while ($row = db_fetch($result)) {
                    if (is_null($row['entry_type_id']) && is_null($row['grand_total'])) {
                        continue;
                    }
                    
                    $type = $row['trans_type'];

                    $line_totals = 0;
                    alt_table_row_color($k);
                        label_cell(text_bold($systypes_array[$row['trans_type']]));

                        foreach ($month_ranges_array as $month) {
                            $month_id = $month['month_id'];
                            $from_date = $month['month_begin'];
                            $to_date = $month['month_end'];
                            $parameters = "entry_type_id=$entry_type_id&from_date=$from_date&to_date=$to_date&type=$type";

                            $total = 0;
                            $total_row = dashboard_get_sales_pnl_period_total($from_date, $to_date, $entry_type_id, $type);
                                if (isset($total_row['total']) && $total_row['total'] > 0) {
                                    $total = $total_row['total'];
                                    
                                    if ($row['trans_type'] == ST_PAYROLL) {
                                        $str_count = get_trans_view_str($row['trans_type'], $total_row['trans_no'], number_format($total, 2));
                                    } else {
                                        $str_count = get_dashboard_view_str(DASH_SC_SALES_PNL, $parameters, number_format($total, 2));
                                    }
                                } else {
                                    $str_count = "";
                                }

                            label_cell($str_count, 'align=right');

                            $line_totals += $total;
                            
                            if ($row['trans_type'] != ST_POS_INVOICE) {
                                $total *= -1;
                            }

                            if (! isset($total_balance_array[$month_id])) {
                                $total_balance_array[$month_id] = $total;
                            } else {
                                $total_balance_array[$month_id] += $total;
                            }
                        }   

                        label_cell(number_format($line_totals, 2), 'align=right');
                    end_row();
                    
                    if ($row['trans_type'] != ST_POS_INVOICE) {
                        $line_totals *= -1;
                    }

                    $grand_total += $line_totals;
                }
                
                if (count($total_balance_array) > 0) {
                    start_row();
                        label_cell(text_bold('Balance'), 'align=right');
                        foreach ($total_balance_array as $value) {
                            label_cell(text_bold(number_format($value, 2)), 'align=right');
                        }
                        label_cell(text_bold(number_format($grand_total, 2)), 'align=right');
                    end_row();
                }
        
    end_table(2);
}

?>