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

$page_security = 'SA_SUBCONTRACTOR_CREDIT_NOTE_INQ';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/projects/includes/db/manual_project_invoice_db.inc");
//include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc");
//include_once($path_to_root . "/projects/includes/db/project_billing_inquiry_db.inc");

include_once($path_to_root . "/projects/includes/db/subcontractor_credit_note_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_credit_note_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_invoices_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_purchase_orders_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");





$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "View Subcontractor Credit Note"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------


if ($_GET['trans_no'] != "")
{
    $sccn_id = $_GET['trans_no'];
}


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

    display_heading("Subcontractor Credit Note #: " . $sccn_id);

    start_table(TABLESTYLE2, "width=60%");

    
    start_row();


    label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
    end_row();
    label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
    end_row();
    label_cells(_("Supplier"), $myrow['supplier_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Order Date"), sql2date($myrow['ord_date']), "class='tableheader2'");
    end_row();
    label_cells(_("Supplier Reference"), $myrow['supp_ref'], "class='tableheader2'");
    end_row();
    
    if($myrow['invoice_id'] > 0){
        $invoice_id = get_project_trans_view_str(ST_SUBCONTRACTOR_INVOICE, $myrow['invoice_id']);
    }
    else{
        $invoice_id = "Manual credit Note";
    }
    label_cells(_("Invoice No"), $invoice_id, "class='tableheader2'");
    end_row();


    global $approval_statuses;
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $sccn_id."&source_type_id=".ST_SUBCONTRACTOR_CREDIT_NOTE, "Approval Status");
    label_cells($s, $approval_statuses[$myrow['approval_status']], "class='tableheader2'");
    end_row();

    end_table();
}

//----------------------------------------------------------------------------------------------



function display_subcontractor_credit_note_summary($sccn_id, $myrow)
{
    display_heading("Summary");

    start_table(TABLESTYLE2, "width=40%");

    start_row();
    label_cells(_("Total Amount (without taxes)"), number_format($myrow['total'], 2), "class='tableheader2'");
    end_row();
    start_row();
    label_cells(_("Price Include Taxes"), get_yes_no_name($myrow['rate_include_taxes']), "class='tableheader2'");
    end_row();
    start_row();
    label_cells(_("Tax Group"), $myrow['tax_group_name'], "class='tableheader2'");
    end_row();


    $sccn_total_with_taxes = $myrow['total'];
    $tax_group_items = get_tax_group_items_as_array($myrow['tax_group_id']);
    foreach ($tax_group_items as $tax_group_item) {
        if($tax_group_item['rate'] > 0){
            $tax_type_amount = get_subcontractor_credit_note_tax_type_amount($sccn_id, $tax_group_item['tax_type_id']);
            start_row();
            label_cells($tax_group_item['tax_type_name'], number_format($tax_type_amount, 2), "class='tableheader2'");
            end_row();
            
            if ($tax_group_item['suspended'] == 0) {
                $sccn_total_with_taxes += $tax_type_amount;
            }
        }
    }

    start_row();
    label_cells(_("Credit Note Total"), text_bold(number_format($sccn_total_with_taxes, 2)), "class='tableheader2'");
    end_row();
    
    end_table();

}



function display_subcontractor_credit_note_details($sccn_id)
{
    global $subconractor_credit_note_item_types;    

    $result = view_subcontractor_credit_note_details($sccn_id);

    start_table(TABLESTYLE, "width=95%");
    $th = array(_('Seq.'), "Line Type",_('Details'), _('Date'),
                _('Unit'), _('Quantity'),_('Rate'), _('Amount'), 
                _('Category'));            

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

    display_heading("Details");

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

            label_cell($myrow["sequence"], "align=right");
            label_cell($subconractor_credit_note_item_types[$myrow["item_type"]]);

            switch ($myrow["item_type"]) {
                case SCCNIT_BOQ_ITEM:
                    $description = $myrow["boq_item_code"]." - ".$myrow["boq_item_description"];
                    break;
                case SCCNIT_INV_LINE:
                    $scil = get_subcontractor_invoice_line($myrow["invoice_line_id"]);
                    switch ($scil["item_type"]) {
                        case SCIIT_BOQ_ITEM:
                            $boqi = get_project_boq_item_record($scil["item_id"]);
                            $description = $boqi["code"]." - ".$boqi["description"];
                            break;
                        case SCIIT_PO_LINE:
                            $po_line = get_sc_purchase_order_line_record($scil["po_line_id"]);
                            if($po_line["item_type"] == 1){
                                $boqi = get_project_boq_item_record($po_line["boq_item_id"]);
                                $description = $boqi["code"]." - ".$boqi["description"];
                            }
                            else{
                                $description = $po_line["description"];
                            }                            
                            break;
                        default : //SCIIT_TEXT
                            $description = $scil["description"];
                            break;
                    }
                    break;
                default :
                    $description = $myrow["description"];
                    break;
            }
            
            
            switch ($myrow["line_type"]) {
                case PILT_MAIN_HEADER:
                case PILT_MAIN_SUMMARY:
                    label_cell("<b>".$description."</b>");
                    break;
                case PILT_MAIN_ITEM:
                    label_cell(spaces(10).$description);
                    break;
                case PILT_SUB_HEADER:
                case PILT_SUB_SUMMARY:
                    label_cell(spaces(10)."<b>".$description."</b>");
                    break;
                case PILT_SUB_ITEM:
                    label_cell(spaces(20).$description);
                    break;
                case PILT_DETAIL:
                default:
                    label_cell(spaces(30).$description);
                    break;
                case PILT_FINAL_TOTAL:
                    label_cell("<b>".$description."</b>");
                    break;
            }

    //      label_cell($myrow["unit"]);  

          switch ($myrow["line_type"]) {
                case PILT_MAIN_HEADER:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    break;
                case PILT_MAIN_SUMMARY:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    amount_cell($myrow["amount"], true);
                    break;
                case PILT_MAIN_ITEM:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit_name"]);
                    label_cell(number_format($myrow["quantity"],3), "align=right");
                    label_cell(number_format($myrow["rate"],2), "align=right");
                    label_cell($myrow["amount"]. spaces(10), "align=right");
    //                amount_cell($myrow["amount"]);
                    break;
                case PILT_SUB_HEADER:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    break;
                case PILT_SUB_SUMMARY:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("<b>".$myrow["amount"]."</b>". spaces(10), "align=right");
                    break;
                case PILT_SUB_ITEM:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit_name"]);
                    label_cell(number_format($myrow["quantity"],3), "align=right");
                    label_cell(number_format($myrow["rate"],2), "align=right");
                    label_cell($myrow["amount"]. spaces(10), "align=right");
                    break;
                case PILT_DETAIL:
                default:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit"]);
                    label_cell(number_format($myrow["quantity"],3), "align=right");
                    label_cell(number_format($myrow["rate"],2), "align=right");
                    label_cell($myrow["amount"]. spaces(10), "align=right");
                    break;
                case PILT_FINAL_TOTAL:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    amount_cell($myrow["amount"], true);
                    break;
            }

            label_cell($myrow["category_name"]);
            
            end_row();  

            
            if($myrow['task_id'] > 0){
                $details = "<b>Budget:</b> ".$myrow["task_code"]." - ".$myrow["task_description"];
            }
            else{
                if(strlen($myrow["debit_account"]) > 0 || strlen($myrow["credit_account"]) > 0){
                    $details = "<b>Debit A/C:</b> ".$myrow["debit_account"]." - ".$myrow["debit_account_name"];
                    $details .= spaces(5)."<b>Credit A/C:</b> ".$myrow["credit_account"]." - ".$myrow["credit_account_name"];
                }
                else{
                    $details = "";
                }
            }
        
            if(strlen($details) > 0){
                alt_table_row_color($k);
                label_cell('');
                label_cell($details, "colspan=8 align=right");
                end_row();
            }
        
        
//          label_cell("[".$subcontractor_invoice_line_types[$myrow['line_type']]."]"); 
        
            
            if(strlen($myrow["line_comment"]) > 0){
                alt_table_row_color($k);
                label_cell('');
                label_cell("<b>Comment:</b> ".$myrow["line_comment"], 'colspan=8 align=right');
                end_row();
            }
    }


    end_table();

}


$sccnh = view_subcontractor_credit_note_header($sccn_id);

display_subcontractor_credit_note_header($sccn_id, $sccnh);

echo "<br>";

display_subcontractor_credit_note_summary($sccn_id, $sccnh);

br();

display_subcontractor_credit_note_details($sccn_id);

br(2);

$final_total = $sccnh['total'] + $sccnh['taxes'];
display_allocations_from(PT_SUPPLIER, $sccnh['supplier_id'], ST_SUBCONTRACTOR_CREDIT_NOTE, $sccn_id, $final_total);

end_page(true, false, false, ST_SUBCONTRACTOR_CREDIT_NOTE, $sccn_id);


?>
