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


function display_manual_project_invoice_details_import_file_data(&$file_data, $validate = false, $import = false)
{
    start_table(TABLESTYLE, "width=98%");    
    $th = array("Project", "Customer", "Branch", "Reference", "PO No", "Order Date", 
                "Payment Terms", "Sales Person", "Payment Application", 
                "Payment Certificate Reference", "Payment Certificate Amount", "Total amount without taxes",
                "Rate Include Taxes", "Tax Group Name", "Tax type 1 name", "Tax type 1 amount", "Tax type 2 name", "Tax type 2 amount",
                "Tax type 3 name", "Tax type 3 amount", "Sequence", "Line type", "Details",
                "Date", "Units", "Rate", "Qty", "Amount", "Line Category", "Debit Account", "Credit Account","Comment", "Message");
              

    table_header($th);
    
    foreach ($file_data as &$line){    

        $mpi = array();
        
        if($validate || $import){
            validate_manual_project_invoice_detail_import_data($line, $mpi);
        }

        if($import && $line['success'] === true){            
            
//            add_other_values_to_mpi_array($line, $mpi);
            $mpi_no = -1;
            $msg = save_manual_project_invoice($mpi_no, -1, $mpi);
            if($msg['success'] === true){
                $line['success'] = true;
                $line['message'] = "imported. Invoice #".$mpi_no;
            }
            else{
                $line['success'] = false;
                $line['message'] = "<font color='red'>".$msg['message']."</font>";
            }

        }

        display_manual_project_invoice_details_import_data_array($line);   
    }
    
    end_table();
    
}
    

   
    
function validate_manual_project_invoice_detail_import_data(&$line, &$mpi)
{
    $success = true;
        
    $msg = get_project_id_by_name($line['project_name']);
    if($msg['success'] !== true){
        $success = false;
        $m = "Unknown project";
        $line['project_name'] = $line['project_name']."<font color='red'>".$m."</font>";
        $mpi['project_id'] = 0;
    }
    else{
        $mpi['project_id'] = $msg['message'];
    }
    
        
    $msg = get_customer_id_by_name($line['customer_name']);
    if($msg['success'] !== true){
        $success = false;
        $m = "Unknown customer";
        $line['customer_name'] = $line['customer_name']."<font color='red'>".$m."</font>";
        $mpi['customer_id'] = 0;
    }
    else{
        $mpi['customer_id'] = $msg['message'];
    }
        
    
    $msg = get_branch_id_by_name($line['branch_name'], $mpi['customer_id']);
    if($msg['success'] !== true){
        $success = false;
        $m = " Unknown customer branch";
        $line['branch_name'] = $line['branch_name']."<font color='red'>".$m."</font>";
    }
    else{
        $mpi['branch_code'] = $msg['message'];
        $mpi['tax_group_id'] = $msg['tax_group_id'];
    }
    
    $mpi['reference'] = $line['reference'];
    
    
    $msg = get_payment_term_id_by_name($line['payment_terms']);
    if($msg['success'] !== true){
        $success = false;
        $m = "Unknown payment term";
        $line['payment_terms'] = $line['payment_terms']."<font color='red'>".$m."</font>";
    }
    else{
        $mpi['payment_terms'] = $msg['message'];
    }
    
     
    if(!is_date($line['ord_date'])){   
        $success = false;
        $m = "Order date is invalid.";
        $line['ord_date'] = $line['ord_date']."<font color='red'>".$m."</font>";
    }
    else{
        $mpi['ord_date'] = $line['ord_date'];
    }
    
            
    $msg = get_sales_person_id_by_name($line['salesman_name']);
    if($msg['success'] !== true){
        $success = false;
        $m = "Unknown salesman";
        $line['salesman_name'] = $line['salesman_name']."<font color='red'>".$m."</font>";
    }
    else{
        $mpi['salesman_code'] = $msg['message'];
    }
    
        
    $mpi['total'] = input_num2($line['total']);
    
    
    if(strlen($line['rate_include_taxes']) > 0 &&($line['rate_include_taxes'] == 1 ||strtoupper($line['rate_include_taxes']) == "YES")){   
        $mpi['rate_include_taxes'] = 1;
    }
    elseif(strlen($line['rate_include_taxes']) > 0 &&($line['rate_include_taxes'] == 0 || strtoupper($line['rate_include_taxes']) == "No")){   
        $mpi['rate_include_taxes'] = 0;
    }
    else{
        $success = false;
        $m = "Unknown value";
        $line['rate_include_taxes'] = $line['rate_include_taxes']."<font color='red'>".$m."</font>";
    }
    
    if($line['payment_application_id'] > 0){
        $msg = check_payment_application_id($line['payment_application_id'], $mpi['project_id']);
        if($msg['success'] !== true){
            $success = false;
            $m = $msg['message'];
            $line['payment_application_id'] = $line['payment_application_id']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['payment_application_id'] = $msg['message'];
        }
    }
    else{
        $mpi['payment_application_id'] = 0;
    }
    
    
    $mpi['payment_certificate_ref'] = $line['payment_certificate_ref'];
      
    $mpi['payment_certificate_amount'] = $line['payment_certificate_amount'];
      
    $msg = get_tax_group_id_by_name($line['tax_group_name']);
    if($msg['success'] !== true){
        $success = false;
        $m = " Unknown tax group name";
        $line['tax_group_name'] = $line['tax_group_name']."<font color='red'>".$m."</font>";
    }
    else{
        $mpi['tax_group_id'] = $msg['message'];
    }
    
    if(strlen($line['tax_type_1_name']) > 0){ 
        $msg = get_tax_type_id_by_name($line['tax_type_1_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = " Unknown tax type 1 name";
            $line['tax_type_1_name'] = $line['tax_type_1_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi[$msg['message'].'_mpi_tax'] = input_num2($line['tax_type_1_amount']);
        }
    }else{
//        $mpi['1_mpi_tax'] = 0;
    }   
        
           
    if(strlen($line['tax_type_2_name']) > 0){  
        $msg = get_tax_type_id_by_name($line['tax_type_2_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = " Unknown tax type 2 name";
            $line['tax_type_2_name'] = $line['tax_type_2_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi[$msg['message'].'_mpi_tax'] = input_num2($line['tax_type_2_amount']);
        } 
    }else{
//        $mpi['2_mpi_tax'] = 0;
    }
    
    if(strlen($line['tax_type_3_name']) > 0){    
        $msg = get_tax_type_id_by_name($line['tax_type_3_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = " Unknown tax type 3 name";
            $line['tax_type_3_name'] = $line['tax_type_3_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi[$msg['message'].'_mpi_tax'] = input_num2($line['tax_type_3_amount']);
        }
    }else{
//        $mpi['3_mpi_tax'] = 0;
    }
    
    if($line['project_sales_order_id'] > 0){
        $msg = check_customer_project_sales_order_id($line['project_sales_order_id'], $mpi['customer_id']);     
        if($msg['success'] !== true){
            $success = false;
            $m = $msg['message'];
            $line['project_sales_order_id'] = $line['project_sales_order_id']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['project_sales_order_id'] = $msg['message'];
        }  
    }
    else{
        $mpi['project_sales_order_id'] = 0;
    }
    
    
    $mpi['sequence'] = $line['sequence'];
    $mpi['description'] = $line['description'];
    $mpi['completed_date'] = $line['completed_date'];
    $mpi['amount'] = input_num2($line['amount']);
    $mpi['comments'] = $line['comments'];
    $mpi['line_type'] = 0;    
    $mpi['rate'] = input_num2($line['rate']);
    $mpi['quantity'] = input_num2($line['quantity']);
    $mpi['line_comment'] = $line['comments'];
    $mpi['debit_account'] = ''; 
    $mpi['credit_account'] = '';
        
    if(strlen($line['unit']) > 0){   
        $msg = get_unit_id_by_name($line['unit']);
        if($msg['success'] !== true){
            $success = false;
            $m = "Unknown unit";
            $line['unit'] = $line['unit']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['unit'] = $msg['message'];
            $mpi['amount'] = $mpi['rate'] * $mpi['quantity'];
        }  
    }
    else{
        $mpi['rate'] = 0;
        $mpi['quantity'] = 0;
    }
      

    if(strlen($line['category_name']) > 0){
        $msg = get_line_category_id_by_name($line['category_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = "Unknown line category";
            $line['category_name'] = $line['category_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['category_id'] = $msg['message'];
        }  
    }
    else{
        $mpi['category_id'] = 0;
    }
    
    if(strlen($line['debit_account_name'] > 1)){
        $msg = get_debit_account_id_by_name($line['debit_account_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = "Unknown debit account";
            $line['debit_account_name'] = $line['debit_account_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['debit_account'] = $msg['message'];
        }  
    }
    else{
        $mpi['debit_account'] = '';
    }
    
         
    if(strlen($line['credit_account_name'] > 1)){
        $msg = get_credit_account_id_by_name($line['credit_account_name']);
        if($msg['success'] !== true){
            $success = false;
            $m = "Unknown credit account";
            $line['credit_account_name'] = $line['credit_account_name']."<font color='red'>".$m."</font>";
        }
        else{
            $mpi['credit_account_code'] = $msg['message'];
        }  
    }
    else{
        $mpi['credit_account_code'] = '';
    }
        
                      
    if($success === true){
        $line['success'] = true;
        $line['message'] = "Can import";
    }
    else{
        $line['success'] = false;
        $line['message'] .= "<font color='red'>Can not import</font>";
    }
}


function display_manual_project_invoice_details_import_data_array($row)
{
    $j = $k = 0;
  
    if($row == ''){
        return;
    }

    //header
    label_cell($row['project_name']);
    label_cell($row['customer_name']);
    label_cell($row['branch_name']);
    label_cell($row['reference']);
    label_cell($row['project_sales_order_id']);

    label_cell($row['ord_date']);
    label_cell($row['payment_terms']);
    label_cell($row['salesman_name']);
    label_cell($row['payment_application_id']);
    label_cell($row['payment_certificate_ref']);
    label_cell($row['payment_certificate_amount']);
        
    //taxes 
    label_cell($row['total']);
    label_cell($row['rate_include_taxes']);
    label_cell($row['tax_group_name']);
    label_cell($row['tax_type_1_name']);
    label_cell($row['tax_type_1_amount']);
    label_cell($row['tax_type_2_name']);
    label_cell($row['tax_type_2_amount']);
    label_cell($row['tax_type_3_name']);
    label_cell($row['tax_type_3_amount']);

    //details
    label_cell($row['sequence']);
    label_cell($row['line_type']);
    label_cell($row['description']);
    label_cell($row['completed_date']);
    label_cell($row['unit']);
    label_cell($row['rate']);
    label_cell($row['quantity']);
    label_cell($row['amount']);
    label_cell($row['category_name']);
    label_cell($row['debit_account_name']);
    label_cell($row['credit_account_name']);
    label_cell($row['comments']);      
    
    label_cell($row['message']);

    end_row();

}



function read_manual_project_invoice_details_import_file_data(&$file_data, $line)
{
    $l = $line;

    //header
    $project_name = str_replace('"', '', $l[0]);
    $customer_name = str_replace('"', '', $l[1]);
    $branch_name = str_replace('"', '', $l[2]);                    
    $reference = str_replace('"', '', $l[3]);
    $project_sales_order_id = str_replace('"', '', $l[4]);
    
    $ord_date = str_replace('"', '', $l[5]);
    $payment_terms = str_replace('"', '', $l[6]);        
    $salesman_code = str_replace('"', '', $l[7]);
    $payment_application_id = str_replace('"', '', $l[8]);
    $payment_certificate_ref = str_replace('"', '', $l[9]);
    $payment_certificate_amount = str_replace('"', '', $l[10]);
    
    //taxes
    $total = str_replace('"', '', $l[11]);     
    $rate_include_taxes = str_replace('"', '', $l[12]);  
    $tax_group_name = str_replace('"', '', $l[13]); 
    $tax_type_1_name = str_replace('"', '', $l[14]);
    $tax_type_1_amount = str_replace('"', '', $l[15]);
    $tax_type_2_name = str_replace('"', '', $l[16]);
    $tax_type_2_amount = str_replace('"', '', $l[17]);
    $tax_type_3_name = str_replace('"', '', $l[18]);
    $tax_type_3_amount = str_replace('"', '', $l[19]);
       
    //details
    $sequence = str_replace('"', '', $l[20]);        
    $line_type = str_replace('"', '', $l[21]);
    $description = str_replace('"', '', $l[22]);
    $completed_date = str_replace('"', '', $l[23]);
    $unit = str_replace('"', '', $l[24]);
    $rate = str_replace('"', '', $l[25]);
    $quantity = str_replace('"', '', $l[26]);
    $amount = str_replace('"', '', $l[27]);
    $category_name = str_replace('"', '', $l[28]);
    $debit_account_name = str_replace('"', '', $l[29]);
    $credit_account_name = str_replace('"', '', $l[30]);
    $comments = str_replace('"', '', $l[31]);

        add_to_import_manual_project_invoice_details_file_data_line($file_data, $project_name, $customer_name, $branch_name,
               $reference, $project_sales_order_id, $ord_date, $payment_terms, $salesman_code,
               $payment_application_id, $payment_certificate_ref, $payment_certificate_amount, $total, $rate_include_taxes, $tax_group_name,
               $tax_type_1_name, $tax_type_1_amount, $tax_type_2_name, $tax_type_2_amount, $tax_type_3_name, $tax_type_3_amount, 
               $sequence, $line_type, $description, $completed_date,
               $unit, $rate, $quantity, $amount, $category_name, $debit_account_name, $credit_account_name, $comments );

}

function add_to_import_manual_project_invoice_details_file_data_line(&$file_data, $project_name, $customer_name, $branch_name,
               $reference, $project_sales_order_id, $ord_date, $payment_terms, $salesman_code,
               $payment_application_id, $payment_certificate_ref, $payment_certificate_amount, $total, $rate_include_taxes, $tax_group_name,
               $tax_type_1_name, $tax_type_1_amount, $tax_type_2_name, $tax_type_2_amount, $tax_type_3_name, $tax_type_3_amount,
               $sequence, $line_type, $description, $completed_date,
               $unit, $rate, $quantity, $amount, $category_name, $debit_account_name, $credit_account_name, $comments )
{
    $file_data_line = array("project_name" => $project_name , 
                        "customer_name" => $customer_name, 
                        "branch_name" => $branch_name, 
                        "reference" => $reference, 
                        "project_sales_order_id" => $project_sales_order_id,
                        "ord_date" => $ord_date, 
                        "payment_terms" => $payment_terms, 
                        "salesman_name" => $salesman_code,
                        "payment_application_id" => $payment_application_id, 
                        "payment_certificate_ref" => $payment_certificate_ref,
                        "payment_certificate_amount" => $payment_certificate_amount, 
                        "total" => $total,  
                        "rate_include_taxes" => $rate_include_taxes,
                        "tax_group_name" => $tax_group_name,
                        "tax_type_1_name" => $tax_type_1_name, 
                        "tax_type_1_amount" => $tax_type_1_amount,
                        "tax_type_2_name" => $tax_type_2_name,
                        "tax_type_2_amount" => $tax_type_2_amount,
                        "tax_type_3_name" => $tax_type_3_name,      
                        "tax_type_3_amount" => $tax_type_3_amount,                     
                        "sequence" => $sequence,
                        "line_type" => $line_type, 
                        "description" => $description, 
                        "completed_date" => $completed_date, 
                        "unit" => $unit, 
                        "rate" => $rate, 
                        "quantity" => $quantity, 
                        "amount" => $amount,
                        "category_name" => $category_name, 
                        "debit_account_name" => $debit_account_name, 
                        "credit_account_name" => $credit_account_name,
                        "comments" => $comments,                        
                        "message" => "");
    
    $file_data[] = $file_data_line;
}



?>
