<?php
$page_security = 'SA_PROJECT_BOQ_ITEM_GROUPS';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");



include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/common_functions.inc");


include_once($path_to_root . "/projects/includes/db/project_transaction_type_transactions_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");


include_once($path_to_root . "/admin/db/list_values_db.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}


simple_page_mode(true);

if (isset($_GET["from_date"]))
{
	$from_date = $_GET["from_date"];
}

if (isset($_GET["to_date"]))
{
	$to_date = $_GET["to_date"];
}

if (isset($_GET["project_id"]))
{
	$project_id = $_GET["project_id"];
}

if (isset($_GET["tran_type"]))
{
	$trans_type = $_GET["tran_type"];
}



page(_($help_context = "BOQ Group Details"), true, false, false, $js);

function display_subheading($project_id, $subheading)
{
    $project = get_project_record($project_id);
    $text = $subheading.' for project "'.$project['description'].'"';
    display_heading($text);
    
    br(1);
    
    start_table(TABLESTYLE, "width=70%");

$th = array(_("Date"), _("No"), _("Reference"), "Supplier / Employee / Package", "Amount");
     

table_header($th);

}

function display_subtotals($total_amount, $subheading)
{
    label_cell("Total for ".$subheading." :","align=right colspan=4");
    label_cell(text_bold(number_format($total_amount,2)),"align=right");
;
    
end_table();
}

function print_material_issues($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Material Issues";

        $invoices_result= get_material_issues($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Material Issues found";
            display_note($msg);
            return;
        }
                  
        display_subheading($project_id, $subheading);
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['im_date']),"align=center");
            $link = get_trans_view_str($type, $inv['im_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            $emp_name = '';
            if(strlen($inv['employee_code']) > 0){
                $emp_name = get_group_company_employee_formatted_long_name($inv['company_code'], $inv['employee_code']);
            }
            label_cell($emp_name);
            $amount = $inv["total_cost"];
            label_cell(number_format($amount,2), "align=right");
            //$rep->TextCol(5, 6, number_format($amount));
            
            end_row();
                  
            $total_amount += $amount;
            $total_settlement += $amount;
            
//            print_material_purchase_credit_notes($inv['trans_no'], ST_SUPPCREDIT, $inv['supp_name'], $total_amount, $total_settlement);
        
        }

        display_subtotals($total_amount, $subheading);
}

function print_material_returns($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Material Returns";

        
        $invoices_result= get_material_returns($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Material Returns found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
                  
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['rm_date']),"align=center");
            $link = get_trans_view_str($type, $inv['rm_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            $emp_name = '';
            if(strlen($inv['employee_code']) > 0){
                $emp_name = get_group_company_employee_formatted_long_name($inv['company_code'], $inv['employee_code']);
            }

            label_cell($emp_name);
            $amount = $inv["total_cost"];
            label_cell(number_format($amount,2), "align=right");

                  
            $total_amount += $amount;
            $total_settlement += $amount;
            
            end_row();
            

        }
        
        display_subtotals($total_amount, $subheading);
}

function print_material_purchased_invoices($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Material Purchased Invoices";

        $invoices_result= get_material_purchased_invoices($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Material Purchase invoices found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
                  
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['supp_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount);
                  
            $total_amount += $amount;
            $total_settlement += $inv["alloc"];
            
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}


function print_material_purchase_credit_notes($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Material Purchased Credit Notes";

        $invoices_result= get_material_purchased_credit_notes($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Material Purchased Credit Notes";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['supp_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount);
                  
            $total_amount += $amount;
            $total_settlement += -$inv["alloc"];
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}


function print_subcontractor_invoices($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Subcontractor Invoices";

        $invoices_result= get_subcontractor_invoices($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Subcontractor invoices found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['supp_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount); 

            $total_amount += $amount;
            $total_settlement += $inv["alloc"];
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}

function print_subcontractor_credit_notes($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Subcontractor Credit Notes";

        $invoices_result= get_subcontractor_credit_notes($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Subcontractor Credit Notes found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['supp_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount); 
            $allocation = -$inv["alloc"];
                  
            $total_amount += $amount;
            $total_settlement += $allocation;
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}

function print_transport_expenses($project_id, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;
        $subheading = "Transport Expenses (Package Based Costs)";
         
        $iou_result= get_vehicle_running_chart_entries($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($iou_result) > 0)){
            $msg = "No Transport Expenses found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $row = db_fetch($iou_result))
        {
            start_row();
            label_cell(sql2date($row['end_date_time']),"align=center");
            $link = get_trans_view_str(ST_VEHICLE_RUNNING_CHART, $row['id']);
            label_cell($link, "align=center");
            label_cell($row['route']);
            
            label_cell($row['package_name']);

            amount_decimal_cell($row["cost"]); 
            
            if($row["inv_no"] > 0 || $row["payment_source_no"] > 0){
                $paid_amount = $row["cost"];
            }
            else{
                $paid_amount = 0;
            }

            $total_amount += $row["cost"];
            $total_settlement += $paid_amount;
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}

function print_petty_cash_payments($project_id, $from_date, $to_date)
{
    global $systypes_array;
    
        $total_amount = $total_settlement = 0;
        $subheading = "Petty Cash Payments";
        
        $iou_result= get_petty_cash_payments($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($iou_result) > 0)){
            $msg = "No Petty Cash Payments found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $row = db_fetch($iou_result))
        {            
            $type_name = $systypes_array[$row['expense_source']];
            
            switch ($row['expense_source']) {
                case ST_SUPPLIER_TRANSPORT_INVOICES:
                    $tran_no = $row['expense_id'];
                    $amount = $row['expense_amount'];                    
                    break;
                case ST_VEHICLE_ALLOCATION:
                    $tran_no = $row['expense_id'];
                    $amount = $row['expense_amount'];  
                    break;
                case ST_VEHICLE_RUNNING_CHART:
                    $tran_no = $row['expense_id'];
                    $amount = $row['expense_amount'];
                    break;
                case ST_PETTY_CASH:
                    $tran_no = $row['expense_id'];
                    $amount = $row['expense_amount'];  
                    break;
                case ST_I_OWE_YOU:
                    $tran_no = $row['expense_id'];
                    $amount = $row['expense_amount'];  
                    break;

                default:
                    $tran_no = 0;
                    break;
            }
            
            start_row();
            label_cell(sql2date($row['tran_date']),"align=center");
            $link = get_trans_view_str(ST_I_OWE_YOU_PAYMENT, $row['id']);
            label_cell($link, "align=center");
            label_cell('');
            
            label_cell($type_name." (#".$tran_no." - Rs. ".$amount.")");

            amount_decimal_cell($row["payment_amount"]); 
            
            $total_amount += $row["payment_amount"];
            $total_settlement += $row["payment_amount"];
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}


function print_iou_advances($project_id, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;
        
        $subheading = "I Owe You Adavnces";

        $iou_result= get_iou_advances_details($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($iou_result) > 0)){
            $msg = "No I Owe You Adavnces found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);
        
        while( $row = db_fetch($iou_result))
        {              
            start_row();
            label_cell(sql2date($row['tran_date']),"align=center");
            
//            get_i_owe_you_advances_paid_total($iou_id);
            $link = get_trans_view_str(ST_I_OWE_YOU_ADVANCES_BATCH, $row['batch_no'], $row['id']);
            label_cell($link, "align=center");
            label_cell($row['reference']);
            
            $emp_name = '';
            if(strlen($row['employee_code']) > 0){
                $emp_name = get_group_company_employee_formatted_long_name($row['company_code'], $row['employee_code']);
            }

            label_cell($emp_name);
            $amount = $row["advance_amount"];
            label_cell(number_format($amount,2), "align=right");

            $total_amount += $amount;
            $total_settlement += $amount;
            
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}

function print_miscellaneous_project_costs($project_id, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;
        $type = ST_MISCELLANEOUS_PROJECT_COST;
        
        $subheading = "Miscellaneous Project Costs";
        
        $iou_result= get_miscellaneous_project_costs($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($iou_result) > 0)){
            $msg = "No Miscellaneous Project Cost entries found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);                  
        
        while( $row = db_fetch($iou_result))
        {            
            start_row();
            label_cell(sql2date($row['date']),"align=center");
            $link = get_trans_view_str($type, $row['id']);
            label_cell($link, "align=center");
            label_cell($row['reference']);
            label_cell($row['comment']);
                  
            $paid_amount = 0;
            $total_amount += $row["total"];
            $total_settlement += $paid_amount;
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}


function display_project_transaction_type_transactions($project_id, $type, $from_date, $to_date){

    switch ($type) {
        case ST_MATERIAL_ISSUE:
            print_material_issues($project_id, ST_MATERIAL_ISSUE, $from_date, $to_date); 
            break;
        case ST_MATERIAL_RETURN:
            print_material_returns($project_id, ST_MATERIAL_RETURN, $from_date, $to_date);  
            break;
        case ST_SUPPINVOICE:
            print_material_purchased_invoices($project_id, ST_SUPPINVOICE, $from_date, $to_date);  
            break;
        case ST_SUPPCREDIT:
            print_material_purchase_credit_notes($project_id, ST_SUPPCREDIT, $from_date, $to_date);
            break;
        case ST_SUBCONTRACTOR_INVOICE:
            print_subcontractor_invoices($project_id, ST_SUBCONTRACTOR_INVOICE, $from_date, $to_date);  
            break;
        case ST_SUBCONTRACTOR_CREDIT_NOTE:
            print_subcontractor_credit_notes($project_id, ST_SUBCONTRACTOR_CREDIT_NOTE, $from_date, $to_date);  
            break;
        case ST_VEHICLE_RUNNING_CHART:
            print_transport_expenses($project_id, $from_date, $to_date);  
            break;
        case ST_I_OWE_YOU_PAYMENT:
            print_petty_cash_payments($project_id, $from_date, $to_date); 
            break;
        case ST_I_OWE_YOU_ADVANCES:
            print_iou_advances($project_id, $from_date, $to_date); 
            break;
        case ST_MISCELLANEOUS_PROJECT_COST:
            print_miscellaneous_project_costs($project_id, $from_date, $to_date);  
            break;
        
        
        case "108P":
            print_project_invoices_pending($project_id, ST_PROJECT_INVOICE, $from_date, $to_date);   
            break; 
        case "128P":
            print_project_credit_notes_pending($project_id, ST_PROJECT_CREDIT_NOTE, $from_date, $to_date);  
            break;
        case ST_PROJECT_INVOICE:
            print_project_invoices_certified($project_id, ST_PROJECT_INVOICE, $from_date, $to_date);
            break;
        case ST_PROJECT_CREDIT_NOTE:
            print_project_credit_notes_certified($project_id, ST_PROJECT_CREDIT_NOTE, $from_date, $to_date);  
            break;

        default:
            break;
    }    
}
      
function print_project_credit_notes_pending($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Project Credit Notes - Pending (IPAs)";

        $invoices_result= get_project_credit_notes_pending($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Project Credit Notes found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);  
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['ord_date']),"align=center");
            $link = get_trans_view_str($type, $inv['order_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['cust_name']);
            $amount = $inv["total"] - $inv["taxes"];
            amount_decimal_cell($amount);
                  
            $total_amount += $amount;
            $total_settlement += 0;        
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}

function print_project_invoices_pending($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Project Invoices - Pending (IPAs)";

        $invoices_result= get_project_invoices_pending($project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No pending Project Invoices found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);   
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['ord_date']),"align=center");
            $link = get_trans_view_str($type, $inv['order_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['cust_name']);
            $amount = $inv["total"] - $inv["taxes"];
            amount_decimal_cell($amount);
                  
            $total_amount += $amount;
            $total_settlement += 0;        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
                  
}


function print_project_credit_notes_certified($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Project Credit Notes - Certified (IPCs)";

        $invoices_result= get_project_credit_notes_certified($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Project Credit Notes found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);   
        
        while( $inv = db_fetch($invoices_result))
        {
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['cust_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount);
            
            $allocation = -$inv["alloc"];
                  
            $total_amount += $amount;
            $total_settlement += $allocation;
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}



function print_project_invoices_certified($project_id, $type, $from_date, $to_date)
{
        $total_amount = $total_settlement = 0;

        $subheading = "Project Invoices - Certified (IPCs)";
        
        $invoices_result= get_project_invoices_certified($type, $project_id, $from_date, $to_date);
        
        if(!(db_num_rows($invoices_result) > 0)){
            $msg = "No Project Invoices found";
            display_note($msg);
            return;
        }
        
        display_subheading($project_id, $subheading);   
        
        while( $inv = db_fetch($invoices_result))
        {            
            start_row();
            label_cell(sql2date($inv['tran_date']),"align=center");
            $link = get_trans_view_str($type, $inv['trans_no']);
            label_cell($link, "align=center");
            label_cell($inv['reference']);
            
            label_cell($inv['cust_name']);
            $amount = $inv["ov_amount"] - $inv["ov_discount"] + $inv["ov_gst"];
            amount_decimal_cell($amount);
                  
            $total_amount += $amount;
            $total_settlement += $inv["alloc"];
        
            end_row();
        }
        
        display_subtotals($total_amount, $subheading);
}


start_form();

br(1);

$user_id = $_SESSION['wa_current_user']->user;
$r = can_access_project($project_id, $user_id);
if($r["success"] !== true){
    display_error($r["message"]);
    display_footer_exit();
    exit;
}

hidden('trans_no', $project_id);



$from_date = INFINITE_START_DATE;
$to_date = INFINITE_END_DATE;

display_project_transaction_type_transactions($project_id, $trans_type, $from_date, $to_date);

//submit_center_first('Recalculate_Poject_PA', _("Recalculate Payment Application Summaries"), '', 'default');

end_form();


end_page();
