<?php


//function display_paid_open_vehicle_allocations()
//{
//    global $Ajax;
//    
//    $Ajax->activate('vas');
//    div_start('vas');
//
//    $result = get_supplier_transport_invoice_vehicle_allocations();
//
//    display_heading2("Advance Paid Vehicle Allocations");
//    
//    start_table(TABLESTYLE, "width=95%");
//    $th = array(_('VA'),"VR", _("Project"), 
//                "Package" , _('From Date'),_('To Date'), 
//                "Route", "Passengers",  "Comment", "Supplier", "Payment", "");
//
//    table_header($th);
//    $k = $total = 0; //row colour counter
//
//    while ($myrow = db_fetch($result))
//    {
//        $prefix = "va_".$myrow["id"]."_";
//        
//        alt_table_row_color($k);
//
//        $a = get_trans_view_str(ST_VEHICLE_ALLOCATION, $myrow['id'],$myrow['id']);
//        label_cell($a, "align=center");
//        
//        if($myrow["vehicle_request_id"] > 0){
//            $r = get_trans_view_str(ST_VEHICLE_REQUEST, $myrow['vehicle_request_id'],$myrow['vehicle_request_id']);
//            label_cell($r, "align=center");
////            label_cell($myrow["vehicle_request_id"], "align=center");
//        }
//        else{
//            label_cell("");
//        }
//        
//        label_cell($myrow["project_description"]);
////        label_cell($myrow["task_description"]);
//        label_cell($myrow["package_description"]);
////        label_cell($myrow["cost"]);
//        label_cell(substr($myrow["from_date_time"], 0, 16));
//        label_cell(substr($myrow["to_date_time"], 0, 16));
//        label_cell($myrow["route"]);
//        label_cell($myrow["passengers"]);
//        
//        label_cell($myrow["comment"]);
//        label_cell($myrow["supp_name"]);
//        
//        $dp_link = get_petty_cash_trans_view_str(ST_PETTY_CASH_DIRECT_PAYMENTS, $myrow["payment_source_no"], $myrow["paid_amount"]);
//        label_cell($dp_link, "align=center");     
//        
//        check_cells('', $myrow["id"]."_".'selected_va_'.$myrow["id"]);
//        
//        $_POST[$prefix.'package_id'] = $myrow['package_id'];
////        vehicle_packages_list_cells(null, $prefix.'package_id');
//        hidden($prefix.'package_id', $_POST[$prefix.'package_id']);
//
//      
//        end_row();
//            
//        if(isset($_POST[$myrow["id"]."_".'selected_va_'.$myrow["id"]])){  
//            $total += $myrow['paid_amount'];
//        }
//        
////        $k--;
//        
//    }
//    //inactive_control_row($th);
//    start_row();    
//        label_cell("Total:", "colspan=9 align=right");
//        amount_cell($total, true); 
//    end_row();
//    
//    end_table(1);
//    div_end();
//    
//    return $total;
//}
//

function display_supplier_transport_invoice_vehicle_allocations($sti_no = -1)
{
    global $Ajax;
    
    $Ajax->activate('vas');
    div_start('vas');

    $result = get_supplier_transport_invoice_vehicle_allocations($sti_no);

    display_heading2("Advance Paid Vehicle Allocations");
    
    start_table(TABLESTYLE, "width=95%");
    $th = array(_('VA'),"VR", _("Project"), 
                "Package" , _('From Date'),_('To Date'), 
                "Route", "Passengers",  "Comment", "Supplier", "Payment", "");

    table_header($th);
    $k = $total = 0; //row colour counter

    while ($myrow = db_fetch($result))
    {
        $prefix = "va_".$myrow["id"]."_";
        
        alt_table_row_color($k);

        $a = get_trans_view_str(ST_VEHICLE_ALLOCATION, $myrow['id'],$myrow['id']);
        label_cell($a, "align=center");
        
        if($myrow["vehicle_request_id"] > 0){
            $r = get_trans_view_str(ST_VEHICLE_REQUEST, $myrow['vehicle_request_id'],$myrow['vehicle_request_id']);
            label_cell($r, "align=center");
//            label_cell($myrow["vehicle_request_id"], "align=center");
        }
        else{
            label_cell("");
        }
        
        label_cell($myrow["project_description"]);
//        label_cell($myrow["task_description"]);
        label_cell($myrow["package_description"]);
//        label_cell($myrow["cost"]);
        label_cell(substr($myrow["from_date_time"], 0, 16));
        label_cell(substr($myrow["to_date_time"], 0, 16));
        label_cell($myrow["route"]);
        label_cell($myrow["passengers"]);
        
        label_cell($myrow["comment"]);
        label_cell($myrow["supp_name"]);
        
        $dp_link = get_petty_cash_trans_view_str(ST_PETTY_CASH_DIRECT_PAYMENTS, $myrow["payment_source_no"], $myrow["paid_amount"]);
        label_cell($dp_link, "align=right");     
        
        if($sti_no > 0){
            delete_button_cell("Delete_sti_va".$myrow["id"], _("Remove"));
            $total += $myrow['paid_amount'];
        }
        else{
            check_cells('', $myrow["id"]."_".'selected_va_'.$myrow["id"]);
            
            if(isset($_POST[$myrow["id"]."_".'selected_va_'.$myrow["id"]])){  
                $total += $myrow['paid_amount'];
            }
        }
        
        $_POST[$prefix.'package_id'] = $myrow['package_id'];
//        vehicle_packages_list_cells(null, $prefix.'package_id');
        hidden($prefix.'package_id', $_POST[$prefix.'package_id']);

      
        end_row();
            
        
        
//        $k--;
        
    }
    //inactive_control_row($th);
    start_row();    
        label_cell("Advances Total:", "colspan=10 align=right");
        amount_cell($total, true); 
    end_row();
    
    end_table(1);
    div_end();
    
    return $total;
}



function display_vehicle_running_chart_grouping_details($sti_no, $advances_total)
{
    global $Ajax, $petty_cash_float_transaction_types;   
    
    $Ajax->activate('leaves');
    div_start('leaves');
      
    display_heading2("Unbilled Running Chart Entries");
    
    start_table(TABLESTYLE, "width=95%");        
    

    $_POST['supplier_charge'] = get_post('supplier_charge');
    
    if($sti_no > 0){
        $result = get_supplier_transport_invoice_vehicle_running_chart($sti_no);
    }
    else{
        $result = get_vehicle_running_chart($_POST['from_date'], $_POST['to_date'],
                                        $_POST['search_supplier_id'], $_POST['vehicle_id'], $_POST['plate_no'], $_POST['batch_no']);
    }

    $th = array(_("VRC#"), _("Batch No"), _("Project"), 
                _("Vehicle"), _("Plate No:"), _("Driver"), 
                _("Cost"), _("Supplier"), _("bill reference"), _("Supplier Charge"), "");              
           
    table_header($th);

    $j = 0;
    $total = 0;
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);

        $vrc_link = get_transport_trans_view_str(ST_VEHICLE_RUNNING_CHART, $myrow["id"]);
        label_cells('', $vrc_link,'', "align=center");  
        
        $vrcb_link = get_transport_trans_view_str(ST_VEHICLE_RUNNING_CHART, $myrow["batch_no"]);
        label_cells('', $vrcb_link, '',"align=center");     
        label_cells('', $myrow["project_description"]);                 
//        label_cells('', $myrow["package_description"],'');        
        label_cells('', $myrow["vehicle_description"],'');           
           
        label_cells('', $myrow["plate_no"],'', "align=right");
        label_cells('', $myrow["driver_name"],'');
        label_cells('', number_format($myrow["cost"],2),'');   
        label_cells('', $myrow["supp_name"]); 
        
        if($sti_no > 0 && !isset($_POST[$myrow["id"]."_vrc_".'bill_reference'])){
            $_POST[$myrow["id"]."_vrc_".'bill_reference'] = $myrow['bill_reference'];
        }
        text_cells(null, $myrow["id"]."_vrc_".'bill_reference', null, 30, 50); 
        
        if($sti_no > 0 && !isset($_POST[$myrow["id"]."_vrc_".'supplier_charge'])){
            $_POST[$myrow["id"]."_vrc_".'supplier_charge'] = $myrow['supplier_charge'];
        }
        amount_cells(null, $myrow["id"]."_vrc_".'supplier_charge');         
        
        
        if($sti_no > 0){
            delete_button_cell("Delete_sti_vrc".$myrow["id"], _("Remove"));
            $total += input_num($myrow["id"]."_vrc_".'supplier_charge');
        }
        else{
            check_cells('', $myrow["id"]."_".'selected_vrc_'.$myrow["id"]);
            
            if(isset($_POST[$myrow["id"]."_".'selected_vrc_'.$myrow["id"]])){  
                $total += input_num($myrow["id"]."_vrc_".'supplier_charge');
            }
        }
          
        end_row();
        $j++; 
    }
    
    hidden('STINo', $sti_no);
    
    if($sti_no > 0){
        $sti = get_supplier_transport_invoice_record($sti_no);
        if(!isset($_POST["other_charges_text"])){
            $_POST["other_charges_text"] = $sti["other_charges_text"];
        }
        if(!isset($_POST["other_charges"])){
            $_POST["other_charges"] = $sti["other_charges"];
        }
    }
    
    alt_table_row_color($k);
    
    label_cell("(Other Charges)", "colspan=8 align=right");
    text_cells(null, 'other_charges_text', null, 30, 200);
    amount_cells(null, 'other_charges'); 
    label_cell(null);
    
    end_row();
    
    $total += input_num('other_charges');
    
    
    start_row();    
        label_cell("Charges Total:", "colspan=9 align=right");
        amount_cell($total); 
    end_row();
    
//    start_row();    
//        label_cell("Advances Total:", "colspan=9 align=right");
//        amount_cell(-$advances_total); 
//    end_row();
    
    
    start_row();    
        label_cell("Sub Total (Charges - Advances):", "colspan=9 align=right");
        amount_cell($total - $advances_total, true); 
    end_row();
    
    
    if($sti_no > 0){
        $sti = get_supplier_transport_invoice_record($sti_no);  
        $supplier = get_supplier($sti['supplier_id']);
        $tax_group_id = $supplier['tax_group_id'];
        $tax_included = $sti['tax_included'];
    }
    else{
        if($_POST['supplier_id'] > 0){
            $supplier = get_supplier($_POST['supplier_id']);
            $tax_group_id = $supplier['tax_group_id'];
            $tax_included = $supplier['tax_included'];
        }
        else{
            $tax_included = 0;
        }
    }
        
    if($_POST['supplier_id'] > 0){
        if($sti_no > 0){
            $taxes = get_supplier_transport_invoice_tax_details($sti_no);
        }
        else{
            $taxes = calculate_common_item_taxes($total - $advances_total, $tax_group_id, $tax_included);
        }
        
        display_edit_tax_items($taxes, 9, $tax_included, 2, false);
    }
    else{
        $taxes = array();
    }
    
    $taxes_total = 0;
    
    if($tax_included){
        $final_total = $total - $advances_total;
    }
    else{
        foreach ($taxes as $tax_line){
            $taxes_total += $tax_line['Value'];
        }
        
        $final_total = $total - $advances_total + $taxes_total;
    }
    
    start_row();    
        label_cell("Total:", "colspan=9 align=right");
        amount_cell($final_total, true); 
    end_row();
    
    
    end_table();
    div_end();     
}

function display_sti_header($sti_no)
{
    global $approval_statuses, $Refs;    
//    start_outer_table(TABLESTYLE2, "width=50%"); // outer table
    start_table(TABLESTYLE2);  

        if($sti_no > 0){
            $sti = get_supplier_transport_invoice_record($sti_no);    
            $user_id = $_SESSION['wa_current_user']->user;
            $cap = can_access_project($sti['project_id'], $user_id);
            if($cap['success'] !== true) 
            {
                display_error($cap["message"]);
                set_focus('project_id');
                display_footer_exit();
                exit;
            }   
            
            if(!isset($_POST['reference'])){
                $_POST['reference'] = $sti['reference'];
            }
            if(!isset($_POST['supp_inv_ref'])){
                $_POST['supp_inv_ref'] = $sti['supp_inv_ref'];
            }
            if(!isset($_POST['tran_date'])){
                $_POST['tran_date'] = sql2date($sti['tran_date']);
            }
            if(!isset($_POST['payment_terms'])){
                $_POST['payment_terms'] = $sti['payment_terms'];
            }
            if(!isset($_POST['due_date'])){
                $_POST['due_date'] = sql2date($sti['due_date']);
            }
            if(!isset($_POST['comments'])){
                $_POST['comments'] = $sti['comments'];
            }
            if(!isset($_POST['project_id'])){
                $_POST['project_id'] = $sti['project_id'];
            }
            if(!isset($_POST['task_id'])){
                $_POST['task_id'] = $sti['task_id'];
            }
            
            $_POST['approval_status'] = $sti['approval_status'];
                    
            ref_row("Reference", 'reference');
            $supplier = get_supplier($sti['supplier_id']);  
            label_row("Supplier:", $supplier['supp_name']);
            $_POST['supplier_id'] = $sti['supplier_id'];
            hidden('supplier_id', $sti['supplier_id']);
        }
        else{
            if(!isset($_POST['reference'])){
                $_POST['reference'] = $Refs->get_next(ST_SUPPLIER_TRANSPORT_INVOICES);
            }
            ref_row("Reference", 'reference');
            supplier_list_row("Supplier:", 'supplier_id', null, false, true);
            $supplier = get_supplier($_POST['supplier_id']);  
            $_POST['payment_terms'] = $supplier['payment_terms'];
            $_POST['approval_status'] = 0;
        }

        text_row("Supp. Inv. Ref:", 'supp_inv_ref', null, null, null);              
        date_row("Invoice Date:", 'tran_date');     
        
        start_row();
        sale_payment_list_cells("Payment Terms:", 'payment_terms', null, null);  
        end_row(); 
        
        if(!(isset($_POST['due_date'])) || list_updated('payment_terms')){
            $due_date = get_duedate_from_payment_terms($_POST['tran_date'], $_POST['payment_terms']);
            $_POST['due_date'] = $due_date;
        }
        date_row("Due Date:", 'due_date'); 
        
        if($sti_no > 0){
            $project = get_project_record($_POST['project_id']);
            label_row("Project:", $project['description']);
            hidden("project_id", $project['project_id']);
        }
        else{
            project_list_row("Project:", 'project_id', null, '', true);
        }
        tasks_list_row("Budget:", 'task_id', $_POST['project_id'], null, '');
        
        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $sti_no."&source_type_id=".ST_SUPPLIER_TRANSPORT_INVOICES, "Approval Status");
        label_row($s,$approval_statuses[$_POST['approval_status']]);
        textarea_row("Comments :", "comments", '', 35, 3);
           

    end_table(1);

//    end_outer_table(1); // outer table
//    hidden('PSONo', $pso_no);  

}
