<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SALESTRANSVIEW';
$path_to_root = "../..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");

$js = "";
if($use_popup_windows)
    $js .= get_js_open_window(900, 600);

page(_($help_context = "View Customer Payment Return"), true, false, "", $js);

if(isset($_GET["trans_no"])){
    $trans_id = $_GET["trans_no"];
}
elseif(isset($_POST["trans_no"])){
    $trans_id = $_POST["trans_no"];
}

// 3 different queries to get the information - what a JOKE !!!!

function display_customer_payment_return_header($trans_id, &$myrow){
    
    $myrow = get_customer_trans($trans_id, ST_CUSTOMER_PAYMENT_RETURN);
    $branch = get_branch($myrow["branch_code"]);
    $src_so_no = get_customer_trans_order(ST_SALESINVOICE, $myrow["order_"]);
    $sales_order = get_sales_order_header($src_so_no, ST_SALESORDER);

    display_heading2(_("Customer Payment Return Details"));
        start_table(TABLESTYLE, "width=50%");
            start_row();
                label_cells("Customer", $myrow["DebtorName"], "class='tableheader2'");
                label_cells("Delivered To", $sales_order["deliver_to"], "class='tableheader2'");
            end_row();
            start_row();
                label_cells("Branch", $branch["br_name"], "class='tableheader2'");
                label_cells(_("Currency"), $sales_order["curr_code"], "class='tableheader2'");
            end_row();
            start_row();
                label_cells(_("Reference"), $myrow["reference"], "class='tableheader2'");
//                label_cells(_("Shipping Company"), $myrow["shipper_name"], "class='tableheader2'");
            end_row();
            start_row();
                label_cells(_("Customer Order Ref."), $sales_order["customer_ref"], "class='tableheader2'");        
                label_cells(_("Sales Type"), $myrow["sales_type"], "class='tableheader2'");
            end_row();
            start_row();
            end_row();
            start_row();
                label_cells(_("Customer Payment Return Date"), sql2date($myrow["tran_date"]), "class='tableheader2'", "nowrap");
                label_cells(_("Invoice No"), get_customer_trans_view_str(ST_SALESINVOICE,$myrow["order_"]), "class='tableheader2'");
            end_row();

            comments_display_row(ST_CUSTOMER_PAYMENT_RETURN, $trans_id);
        end_table();
}

function display_credit_notes($invoice_nos){
    start_table(TABLESTYLE);
        display_heading2(_("Credit Notes"));


        $th = array(_("#"), _("Ref"), _("Date"), _("Total"));
        table_header($th);

        $cn_numbers = array();
        $delivery_total = 0;

        if($result = get_sales_child_documents(ST_CUSTOMER_PAYMENT_RETURN, $invoice_nos)){
            $k = 0;
            while($del_row = db_fetch($result)){
                alt_table_row_color($k);
                    $cn_numbers[] = $del_row["trans_no"];
                    $this_total = $del_row["ov_freight"]+ $del_row["ov_amount"] + $del_row["ov_freight_tax"]  + $del_row["ov_gst"] ;
                    $delivery_total += $this_total;

                    label_cell(get_customer_trans_view_str($del_row["type"], $del_row["trans_no"]));
                    label_cell($del_row["reference"]);
                    label_cell(sql2date($del_row["tran_date"]));
                    amount_cell($this_total);
                end_row();
            }
        }
        label_row(null, price_format($delivery_total), " ", "colspan=4 align=right");
    end_table();
}

function display_customer_payment_return_details($trans_id, $myrow){
    $result = get_customer_trans_details(ST_CUSTOMER_PAYMENT_RETURN, $trans_id);
    start_table(TABLESTYLE, "width=95%");
        if(db_num_rows($result) > 0){
            $th = array(_("Id"), _("Item Code"), _("Item Description"), _("Quantity"), _("Unit"), _("Price"), _("Discount %"), _("Total"));
            table_header($th);

            $k = 0;	//row colour counter
            $sub_total = 0;
            while($myrow2 = db_fetch($result)){
                if($myrow2['quantity']==0) continue;
                alt_table_row_color($k);
                    $value = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec());
                    $sub_total += $value;

                    if($myrow2["discount_percent"] == 0){
                        $display_discount = "";
                    }else{
                        $display_discount = percent_format($myrow2["discount_percent"]*100) . "%";
                    }
                    label_cell($myrow2["id"], "align=center");
                    label_cell($myrow2["stock_id"]);
                    label_cell($myrow2["StockDescription"]);
                    qty_cell($myrow2["tran_qty"], false, get_qty_dec($myrow2["stock_id"]));
                    $tran_unit_name = get_unit_descr($myrow2['tran_unit']);
                    label_cell($tran_unit_name);
                    amount_cell($myrow2["unit_price"]*$myrow2["conversion_factor"]);
                    label_cell($display_discount, "nowrap align=right");
                    amount_cell($value);
                end_row();
            } //end while there are line items to print out
            
            $display_sub_tot = price_format($sub_total);
            label_row(_("Sub-total"), $display_sub_tot, "colspan=7 align=right", "nowrap align=right width=15%");
        }else
            display_note(_("There are no line items on this customer payment return."), 1, 2);

        $display_freight = price_format($myrow["ov_freight"]);
        label_row(_("Shipping"), $display_freight, "colspan=7 align=right", "nowrap align=right");
        $tax_items = get_trans_tax_details(ST_CUSTOMER_PAYMENT_RETURN, $trans_id);
        display_customer_trans_tax_details($tax_items, 6);
        $display_total = price_format($myrow["ov_freight"]+$myrow["ov_amount"]+$myrow["ov_freight_tax"]+$myrow["ov_gst"]);
        label_row(_("TOTAL VALUE"), $display_total, "colspan=7 align=right", "nowrap align=right");
    end_table(1);
}

    display_heading(sprintf(_("CUSTOMER PAYMENT RETURN #%d"),$trans_id));
    br(1);
//    start_outer_table(TABLESTYLE2, "width=50%");
//        table_section(1);        
//        echo "<td>";
            display_customer_payment_return_header($trans_id, $myrow);
//        echo "</td>";
//        table_section(2);
//        echo "<td>";
//            $invoice_nos = array();
//            $invoice_nos[] = $trans_id;
////            display_credit_notes($invoice_nos);
//        echo "</td>";
//    end_outer_table(1);

            br(1);
            
    display_customer_payment_return_details($trans_id, $myrow);
//    is_voided_display(ST_CUSTDELIVERY, $trans_id, _("This customer payment return has been voided."));

end_page(true, false, false, ST_CUSTOMER_PAYMENT_RETURN, $trans_id);

?>