<?php
/**********************************************************************
***********************************************************************/

function build_invoice_items_array(&$items, $project_id, $pi_no)
{
    $loop_count = 1;
    $billing_items_top_q = get_top_level_invoicing_items($project_id, $pi_no);
    while ($billing_item_top = db_fetch($billing_items_top_q))
    {
        $line = array();
        $line['id'] = $billing_item_top['id']; 
        $line['type'] = $billing_item_top['type']; 
        $line['code'] = $billing_item_top['code']; 
        if($line['type'] == 0){
            $line['description'] = $billing_item_top['invoice_item_description']; 
        }
        else{
            $line['description'] = $billing_item_top['description']; 
        }
        $line['parent'] = "";
        $line['line_total'] = 0;
        $line['taxes'] = 0;
        $line['unit'] = "";
        $line['total_only'] = $billing_item_top['total_only'];
        
        $child_items_array = add_child_invoice_items_to_array($billing_item_top['id'], $line['description'], $pi_no, $loop_count);
        if($child_items_array){
            $line['children'] = $child_items_array;            
        }
        else{
            $line['children'] = array();
        }
        
        if($billing_item_top['type'] == 0){ //Valid billing item
            $pi_item = get_project_invoice_detail_item($pi_no, $billing_item_top['id'], $billing_item_top['invoice_line_id']);
            if(isset($pi_item['id']) &&  $pi_item['id'] > 0){
                $line['pi_line_id'] = $pi_item['id']; 
                $line['line_total'] = $pi_item['unit_price'] * (1 - $pi_item['discount']/100)* $pi_item['quantity'];
                $line['unit'] = $pi_item['unit'];
                $line['discount'] = $pi_item['discount'];
                $line['tax_group_name'] = $pi_item['tax_group_name'];
                $line['taxes'] = $pi_item['item_taxes'];
                $line['unit_price'] = $pi_item['unit_price']; 
                $line['list_price'] = $pi_item['list_price']; 
                $line['quantity'] = $pi_item['quantity']; 
            }
        }
        else{
            if($child_items_array){
                foreach ($child_items_array as $child_item){
                    if(isset($child_item['line_total'])){
                        $line['line_total'] += $child_item['line_total'];
                        $line['taxes'] += $child_item['taxes'];
                    }
                }            
            }
        }
        
        if($billing_item_top['type'] > 0 || ($billing_item_top['type'] == 0 && $billing_item_top['invoice_line_id'] > 0)){
            $items[] = $line;
        }
    }
    
}

function add_child_invoice_items_to_array($parent_id, $parent, $pi_no, &$loop_count)
{
    $loop_count++;
    if($loop_count > 20){
        //Stop infinite recursive loop
        return null;
    }
    $billing_items_array = array();
    $has_items =  false;
    
    $billing_items_top_q = get_child_invoicing_items($parent_id, $pi_no);
    while ($billing_item = db_fetch($billing_items_top_q))
    {
        $line = array();
        $line['id'] = $billing_item['id']; 
        $line['type'] = $billing_item['type']; 
        $line['code'] = $billing_item['code']; 
        if($line['type'] == 0){
            $line['description'] = $billing_item['invoice_item_description']; 
        }
        else{
            $line['description'] = $billing_item['description']; 
        }
        $line['parent'] = $parent;
        $line['line_total'] = 0;
        $line['taxes'] = 0;
        $line['unit'] = "";
        $line['total_only'] = $billing_item['total_only'];
        $line['has_items'] = false;

        if($billing_item['type'] == 0){ //Valid billing item
            $pi_item = get_project_invoice_detail_item($pi_no, $billing_item['id'], $billing_item['invoice_line_id']);
            if(isset($pi_item['id']) &&  $pi_item['id'] > 0){
                $line['has_items'] = true;
                $line['pi_line_id'] = $pi_item['id']; 
                $line['line_total'] = $pi_item['unit_price'] * (1 - $pi_item['discount']/100)* $pi_item['quantity'];
                $line['unit'] = $pi_item['unit'];
                $line['discount'] = $pi_item['discount'];
                $line['tax_group_name'] = $pi_item['tax_group_name'];
                $line['taxes'] = $pi_item['item_taxes'];
                $line['unit_price'] = $pi_item['unit_price']; 
                $line['list_price'] = $pi_item['list_price']; 
                $line['quantity'] = $pi_item['quantity']; 
            }
        }
        
        $child_items_array = add_child_invoice_items_to_array($billing_item['id'], $line['description'], $pi_no, $loop_count);
        if($child_items_array){
            foreach ($child_items_array as $child_item){
                if(isset($child_item['has_items']) && $child_item['has_items']){
                    $line['has_items'] = true;
                }
                $line['line_total'] += $child_item['line_total'];
                $line['taxes'] += $child_item['taxes'];
            }      
            $line['children'] = $child_items_array;
        }
        else{
            $line['children'] = array();
        }
                
        
        if($line['has_items']){
            $billing_items_array[] = $line;
            $has_items = true;
        }
    }
    
    foreach ($billing_items_array as $billing_item){
        if(isset($billing_item['has_items']) && $billing_item['has_items']){
            $has_items = true;
        }
    }
            
    $loop_count--;
    if($has_items){
        return $billing_items_array;
    }
    else{
        return null;
    }
}

function display_project_invoice_lines(&$rep, $pi_header, $item){
    $SubTotal = 0;
    $dec = 2;
    $sign = 1;
    
    if($item['type'] > 0){
        display_project_invoice_sub_headings($rep, $pi_header, $item);
    }
    else{
        if ($item["discount"]==0){
            $DisplayDiscount ="";
        }
        else{
            $DisplayDiscount = number_format2($item["discount"],2) . "%";
        }

        start_row();
        label_cell($item['id']);
        label_cell($item['type'] == 0 ? "Item" : "Group");
        label_cell($item['code']." - ".$item['description'], "align=right");
        if($item['total_only'] == 0){
            amount_decimal_cell($item['quantity']); 
            label_cell($item['unit']);
            amount_decimal_cell($item['list_price']);   
            amount_decimal_cell($item['unit_price']);    
            amount_decimal_cell($item['discount']);   
            amount_decimal_cell($item['taxes']);
        }
        else{
            label_cell('', "colspan=6"); 
        }
        
        amount_decimal_cell($item['line_total']);
//        label_cell($DisplayNet, "align=right");
        label_cell($item['tax_group_name']);
        
        edit_button_cell("Edit".$item["pi_line_id"], _("Edit"));    
        delete_button_cell("Delete".$item["pi_line_id"], _("Delete"));  
        
        end_row();
    }
    
    foreach ($item['children'] as $child_item){
        display_project_invoice_lines($rep, $pi_header, $child_item);
        if($child_item['type'] > 0){
            display_project_invoice_sub_totals($rep, $pi_header, $child_item);
        }
    }

}


function display_project_invoice_sub_totals(&$rep, $pi_header, $item)
{

    if($pi_header['rate_include_taxes']){
        $sub_total = $item['line_total'];
    }
    else{
        $sub_total = $item['line_total'] + $item['taxes'];
    }
    
    start_row();
    label_cell($item['id']);
    label_cell($item['type'] == 0 ? "Item" : "Group");
    label_cell("<b>".$item['description']." - Sub Total:</b>", "colspan=7");
    amount_cell($sub_total,true);

    end_row();
}


function display_project_invoice_sub_headings(&$rep, $pi_no, $item)
{
//    $rep->NewLine();

    start_row();
    label_cell($item["id"]);
    label_cell($item['type'] == 0 ? "Item" : "Group"); 
    label_cell("<b>".$item["description"]."</b>", "colspan=11");
//    label_cell($item["code"]);
    end_row();
}

function get_project_invoice_total_from_array($pi_header, $items)
{
    $pi_total = 0;
    
    foreach ($items as $item)                
    {
        if($pi_header['rate_include_taxes']){
            $line_total = $item['line_total'];
        }
        else{
            $line_total = $item['line_total'] + $item['taxes'];
        }
    
        $pi_total += $line_total;
    }
    
    return $pi_total;
}

?>