<?php

function display_daily_item_return_header($trans_no, $status=true) {
    global $Refs;
    
    $type = ST_DAILY_ITEM_RETURN;
    
        if ($trans_no > 0) {
            $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
                $dii_header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $header['source_id']);
                    $dii_trans_no = $header['source_id'];
        } else {
            $dii_trans_no = $_POST['dii_trans_no'];
            $dii_header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $dii_trans_no);
        }
            $user_id = $_SESSION['wa_current_user']->user;
            $cap = can_access_project($dii_header['project_id'], $user_id);
                if ($cap['success'] !== true) {
                    display_error($cap["message"]);
                    set_focus('project_id');
                    display_footer_exit();
                    exit;
                } 

        div_start('nsit_header');
  
            start_outer_table(TABLESTYLE2, "width=65%"); // outer table
                start_table(TABLESTYLE2);  
                    table_section(1, "50%");

                        label_row("Issue Trans #:", get_inventory_trans_view_str(ST_DAILY_ITEM_ISSUE, $dii_header['trans_no']), "class='tableheader2'");  
                        label_row("Project:", $dii_header['project_description'], "class='tableheader2'");   
                        $_POST['project_id'] = $dii_header['project_id'];
                        hidden('project_id', $dii_header['project_id']);
                        
                        label_row("Date:", sql2date($dii_header['tran_date']), "class='tableheader2'");   
                        label_row("Issue Location:", $dii_header['location_name'], "class='tableheader2'");   
                        
                    table_section(2, "50%");
                        $movement_type = get_movement_type($dii_header['movement_type']);
                            label_row(_("Movement Type:"), $movement_type['name'], "class='tableheader2'");

                            if ($dii_header['linked_field_type'] == IMLF_EMPLOYEE) {
                                $gc_name = get_group_company_name($dii_header['group_company_code']);
                                    label_row(_("Group Company:"), $gc_name, "class='tableheader2'");
                            }
                            
                            hidden('linked_field_type', $dii_header['linked_field_type']);
                            $_POST['linked_field_type'] = $dii_header['linked_field_type'];
                            hidden('group_company_code', $dii_header['group_company_code']);
                            $_POST['group_company_code'] = $dii_header['group_company_code'];
                            
                        global $approval_statuses;
                        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $dii_trans_no."&source_type_id=".ST_DAILY_ITEM_ISSUE, "Approval Status");
                        label_row($s, $approval_statuses[$dii_header['approval_status']], "class='tableheader2'");

                end_table();
            end_outer_table(2); // outer table
            
            
            if ($trans_no > 0 && $status === true) {
                if (! isset($_POST['reference'])) {
                    $_POST['reference'] = $header['reference'];
                }

                if (! isset($_POST['comments'])){
                    $_POST['comments'] = $header['comments'];
                }        

                if (! isset($_POST['location'])) {
                    $_POST['location'] = $header['loc_code'];
                }

                if (! isset($_POST['tran_date'])) {
                    $_POST['tran_date'] = sql2date($header['tran_date']);
                }

                if (! isset($_POST['approval_status'])) {
                    $_POST['approval_status'] = $header['approval_status'];
                }

            } else {
                $_POST['reference'] = $Refs->get_next($type);            
                $_POST['approval_status'] = 0;
            }
            
            start_outer_table(TABLESTYLE2, "width=65%"); // outer table
                table_section(1, "40%");
                    if ($trans_no > 0 && $status === true) {
                        label_row("Return Trans #:", $header['trans_no']);  

                        $reference = $header['reference'];            
                        text_row("Reference:", 'reference', $reference, null, null);            
                    } else {            
                        text_row("Reference:", 'reference', $Refs->get_next($type), null, null);   
                    }
                    
                    if (! isset($_POST['location'])) {
                        $_POST['location'] = $dii_header['loc_code'];
                    }
                
                    locations_list_row(_("Return Location:"), 'location', null, '', false);

                table_section(2, "20%");
                    date_row("Date:", 'tran_date');

                table_section(3, "40%");
                    textarea_row("Comment:", 'comments', null, 35, 2); 
            end_outer_table(2); // outer table
    
        div_end();
}

function display_daily_item_return_details($trans_no, $status=true) { 
    global $Ajax, $daily_item_types_array, $inventory_movement_type_linked_fields;
    $type = ST_DAILY_ITEM_RETURN;
    
    $units_array = build_all_item_units_array();
    
    display_heading("Details");
    
    if ($trans_no > 0) {
        $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
            $dii_trans_no = $header['source_id'];
    } else {
        $dii_trans_no = $_POST['dii_trans_no'];
    }
    
        $dii_header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $dii_trans_no);
            $linked_field_type = $dii_header['linked_field_type'];
            $link_f_name = $inventory_movement_type_linked_fields[$linked_field_type];
    
    $Ajax->activate('dir_details');
    div_start('dir_details');
    
        start_table(TABLESTYLE, "width=99%");

            $th = array(_("ID"), _("Rec"), _("Item Type"), _("Item"), _("Issued Unit"), _("Issued Qty"), _('Budget'), _('Item Cost'), _("$link_f_name"), 
                        _('Returned Qty'), _('Returning Unit'), _('Returning Qty'), _('Job'), _('Comment'), _(""));  
            table_header($th);

            $k = 0; //row colour counter
                $result = get_daily_item_issue_and_return_details($dii_trans_no, $trans_no);      
                    while ($row = db_fetch($result)) {
                        alt_table_row_color($k);
                            $trans_id = $row['trans_id'];
                            $rtn_trans_id = $row['rtn_trans_id'];
                            $stock_id = $row["stock_id"];

                            label_cell($trans_id, 'align=center');
                            label_cell($rtn_trans_id, 'align=center');
                            label_cell($daily_item_types_array[$row['daily_item_type']]);
                            if ($row['daily_item_type'] == NSIT_NON_STOCK_ITEM || $row['daily_item_type'] == NSIT_STOCK_ITEM) {
                                label_cell("$stock_id - ".$row["item_description"]);  
                            } else {
                                label_cell($row["item_description"]);
                            }
                            label_cell($row["unit_name"]);
                            label_cell(number_format($row["tran_qty"], 2), 'align=right');
                            if ($row['daily_item_type'] == NSIT_COMMENT) {
                                label_cell('No budget required');
                            } else {
                                label_cell($row["task_description"]);
                            }
                            $tran_unit_cost = $row["standard_cost"] * $row["conversion_factor"];
                                if ($tran_unit_cost > 0) {
                                    label_cell(number_format($tran_unit_cost, 2), 'align=right');
                                } else {
                                    label_cell('');
                                }
                            
                            $lf_name = get_movement_type_linked_field_values($_POST['linked_field_type'], $row['linked_field_id'], $_POST['group_company_code']);
                                label_cell($lf_name["value"]);  
                                
                            label_cell(number_format($row["returned_quantity"] / $row["conversion_factor"], 2), 'align=right');
                            
                            if (! isset($_POST["tran_unit_$trans_id"])) {
                                if ($rtn_trans_id > 0) {
                                    $_POST["tran_unit_$trans_id"] = $row['rtn_tran_unit'];
                                } else {
                                    $_POST["tran_unit_$trans_id"] = $row['tran_unit'];
                                }
                            } else {
                                if (! (strlen($_POST["tran_unit_$trans_id"]) > 0)) {
                                    if ($rtn_trans_id > 0) {
                                        $_POST["tran_unit_$trans_id"] = $row['rtn_tran_unit'];
                                    } else {
                                        $_POST["tran_unit_$trans_id"] = $row['tran_unit'];
                                    }
                                }
                            }
                            
                            if ($row['daily_item_type'] == NSIT_NON_STOCK_ITEM || $row['daily_item_type'] == NSIT_STOCK_ITEM) {
                                units_list_cells('', "tran_unit_$trans_id", $stock_id, null, '');
                            } else {
                                custom_values_list_cells('', "tran_unit_$trans_id", $units_array, null, '');
                            }
                            
                            if ($rtn_trans_id > 0) {
                                $conversion_factor = $row["rtn_conversion_factor"];
                            } else {
                                $conversion_factor = $row["conversion_factor"];
                            }
                                
                            get_convertion_factor2($stock_id, $_POST["tran_unit_$trans_id"], $conversion_factor);
                            
                            $dec = get_qty_dec($stock_id);
                            amount_cells("", "tran_qty_$trans_id", null, null, null, $dec);
                            
                            if ($rtn_trans_id > 0) {
                                if (! (isset($_POST["site_id_$trans_id"])) || ! ($_POST["site_id_$trans_id"] > 0)) {
                                    $_POST["site_id_$trans_id"] = $row['rtn_project_site_id'];
                                }
                                if (! (isset($_POST["line_comment_$trans_id"])) || ! (strlen($_POST["line_comment_$trans_id"]) > 0)) {
                                    $_POST["line_comment_$trans_id"] = $row['rtn_line_comment'];
                                }
                            }
                            
                            if (! isset($_POST["site_id_$trans_id"])) {
                                $_POST["site_id_$trans_id"] = $row['project_site_id'];
                            }
                            
                            project_sites_list_cells("", "site_id_$trans_id", $_POST['project_id'], null, '', false); 
                            text_cells_ex('', "line_comment_$trans_id", 50, 400, null, 'Line Comment');       

                            if ($rtn_trans_id > 0 && $status === true) {
                                check_cells('', "checked_$trans_id", true, false, false, 'align=center'); 
                            } else {
                                check_cells('', "checked_$trans_id", null, false, false, 'align=center'); 
                            }

                        end_row();        
                    }

        end_table(3);
    
    div_end();
}

//function build_all_item_units_array() {
//    $units_array = array();
//    
//        $result = get_all_item_units_units();
//            while ($row = db_fetch($result)) {
//                $units_array[$row['unit_code']] = $row['unit_name'];
//            }
//            
//        return $units_array;
//}

function can_process_daily_item_return_header($trans_no) {
    global $Refs, $systypes_array;
    
    $type = ST_DAILY_ITEM_RETURN;
    
    if ($trans_no > 0) {
        $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
    } else {
        $dii_trans_no = $_POST['dii_trans_no'];
        $dii_header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $dii_trans_no);
            if (! ($dii_header['approval_status'] == AS_APPROVED)) {

                $set_focus = '';
                $message = ($systypes_array[ST_DAILY_ITEM_ISSUE]." record is not approved. Please approve it first.");
                return array("success" => false, "focus_to" => $set_focus, "message" => $message);
            }
    }
      
    if (isset($header) && is_array($header)) {
        if (! ($header['approval_status'] == AS_NEW || $header['approval_status'] == AS_WITH_THE_APPROVER || 
               $header['approval_status'] == AS_AWAITING_APPROVAL || $header['approval_status'] == AS_BACK_TO_ORIGINATOR)) {
        
            $set_focus = '';
            $message = ("This document status does not allow further updating.");
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);
        }
    } 
    
    if (! $Refs->is_valid($_POST['reference'])) {
        $message = ("Please enter a reference.");
        $set_focus = 'reference';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
    }    
 
    $ref_msg = reference_exists($_POST['reference'], $type, $trans_no);
        if ($ref_msg) {
            $message = $ref_msg;
            $set_focus = 'reference';
            return array("success" => false, "focus_to" => $set_focus, "message" => $message);       
        }
    
    if (! is_date($_POST['tran_date'])) {
        $message = "Date entered is not a valid date.";
        $set_focus = 'tran_date';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);        
    }
    
    if (!is_date_in_fiscalyear($_POST['tran_date'])) 
    {
        $message = (_("The entered date is not in fiscal year."));
        $set_focus = ('tran_date');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
    } 
    
    $msg = is_accounting_period_open($_POST['tran_date']);
    if ($msg['success'] !== true)
    {
        $message = ($msg['message']);
        $set_focus = ('tran_date');
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);      
    }
              
    if ($_POST['location'] == '') {
        $message = "Please select a location.";
        $set_focus = 'location';
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);
    }
    
    $row_state = 0;
    if ($trans_no > 0) {
        $row_state = $header['row_state'];
    }
    
    return array("success" => true, 'row_state' => $row_state, "message" => "");    
}

function can_process_daily_item_issue_lines($trans_no) {
    if ($trans_no > 0) {
        $header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_RETURN, $trans_no);
            $dii_trans_no = $header['source_id'];
    } else {
        $dii_trans_no = $_POST['dii_trans_no'];
    }
    
    $selected_lines_count = 0;
        $result = get_daily_item_issue_and_return_details($dii_trans_no, $trans_no);        
            while ($row = db_fetch($result)) {
                $trans_id = $row['trans_id'];
                
                if (isset($_POST["checked_$trans_id"]) && $_POST["checked_$trans_id"] == 1) {
                    
                    if ($row['daily_item_type'] == NSIT_NON_STOCK_ITEM || $row['daily_item_type'] == NSIT_STOCK_ITEM) {
                        if ($_POST["tran_unit_$trans_id"] == -1) {
                            $message = "Line #$trans_id tran unit cannot be empty.";
                            $set_focus = "tran_unit_$trans_id";
                            return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
                        }
                    } elseif ($row['daily_item_type'] == NSIT_COMMENT) {
                        if ($_POST["tran_unit_$trans_id"] == '0') {
                            $message = "Record #$trans_id tran unit cannot be empty.";
                            $set_focus = "tran_unit_$trans_id";
                            return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
                        }
                    }
                    
                    $tran_qty = input_num("tran_qty_$trans_id");

                    if (! ($tran_qty > 0)) {
                        $message = "Line #$trans_id quantity should be greater than zero.";
                        $set_focus = "tran_qty_$trans_id";
                        return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
                    }
                    
                    // check max returnable qty
                    $conversion_factor = $row["conversion_factor"];
                    if (! ($row['daily_item_type'] == NSIT_COMMENT)) {
                        get_convertion_factor2($row["stock_id"], $_POST["tran_unit_$trans_id"], $conversion_factor);
                    }

                    $returned_quantity = get_daily_item_issue_line_returned_total($trans_id , $row['rtn_trans_id']);
                        $balance_qty = ($row["qty"] - abs($returned_quantity));
                        
                        $max_returnable_qty = $balance_qty / $conversion_factor;
                        
                        if ($tran_qty > $max_returnable_qty) {
                            $message = "Line #$trans_id exceeds maximum returnable quantity $max_returnable_qty.";
                            $set_focus = "tran_qty_$trans_id";
                            return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
                        }
                        
                    $selected_lines_count++;
                }
            }
            
            if (! ($selected_lines_count > 0)) {
                $message = "You should check at least one line before returning.";
                $set_focus = "";
                return array("success" => false, "focus_to" => $set_focus, "message" => $message);  
            }

            return array("success" => true, "focus_to" => '', "message" => "");  
}

function save_daily_item_return_header(&$trans_no, $type) {
    global $systypes_array;
    
    if ($trans_no > 0) {
        $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
            $dii_hd = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $header['source_id']);
                $dii_trans_no = $header['source_id'];
    } else {
        $dii_trans_no = $_POST['dii_trans_no'];
        $dii_hd = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $dii_trans_no);
    }
    
        $trans_no = write_daily_item_issue_header_by_type_trans_no($type, $trans_no, $_POST['reference'], $dii_hd['project_id'], $dii_hd['movement_type'], 
                                                                   $dii_hd['linked_field_type'], '', $dii_hd['group_company_code'], $_POST['location'], 
                                                                   $_POST['comments'], $_POST['tran_date'], $dii_trans_no); 
    
            if ($trans_no > 0) {
                return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$type]." header #$trans_no has been saved.");
            } 
}

function save_daily_item_return_lines(&$trans_no) {    
    global $systypes_array;
    
    $type = ST_DAILY_ITEM_RETURN;
    
    $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
        $dii_trans_no = $header['source_id'];
        
        $result = get_daily_item_issue_and_return_details($dii_trans_no, $trans_no);      
            while ($row = db_fetch($result)) {
                $trans_id = $row['trans_id'];
                
                if (isset($_POST["checked_$trans_id"]) && $_POST["checked_$trans_id"] == 1) {
                    
                    $stock_id = $row['stock_id'];
                    $rtn_trans_id = $row["rtn_trans_id"];
                    $tran_unit = $_POST["tran_unit_$trans_id"];
                    $tran_qty = -input_num("tran_qty_$trans_id");
                    $site_id = $_POST["site_id_$trans_id"];
                    $line_comment = $_POST["line_comment_$trans_id"];
                    
                    if ($row['daily_item_type'] == NSIT_STOCK_ITEM) {
                        $standard_cost = get_standard_cost($stock_id);
                        $conversion_factor = 1;
                        
                        get_convertion_factor2($stock_id, $tran_unit, $conversion_factor);
                    } else {
                        $standard_cost = $row["standard_cost"]; 
                        $conversion_factor = $row["conversion_factor"];
                    }

                    $qty = $tran_qty * $conversion_factor;

                    $af_no = write_daily_item_issue_line($trans_no, $rtn_trans_id, $type, $_POST['location'], $_POST['tran_date'], $stock_id, 
                                                         $row['daily_item_type'], $row['item_description'], $tran_unit, $tran_qty, $qty, 
                                                         $conversion_factor, $standard_cost, $site_id, $line_comment, $row['linked_field_id'],
                                                         $row['task_id'], $trans_id); 

                        if (! ($af_no > 0)) {
                            $ln_ms = $systypes_array[$type]." line #$trans_id could not be saved.";
                            return array("success" => false, 'trans_no' => $trans_no, "message" => $ln_ms);
                        } 
        
                        update_daily_item_issue_line_returned_total($trans_id);
                }
            }

                $ln_ms = $systypes_array[$type]." lines have been saved.";
                
                return array("success" => true, 'trans_no' => $trans_no, "message" => $ln_ms);
}

function save_daily_item_return(&$trans_no) {
    $type = ST_DAILY_ITEM_RETURN;
    
    $hd_msg = can_process_daily_item_return_header($trans_no);   
        if ($hd_msg["success"] !== true) {          
            if (isset($hd_msg["focus_to"])) {
                set_focus($hd_msg["focus_to"]);            
            }
            
            return array("success" => false, "message" => $hd_msg["message"]);
        }
    
    $ln_msg = can_process_daily_item_issue_lines($trans_no);
        if ($ln_msg['success'] !== true) {
            if (isset($ln_msg["focus_to"])) {
                set_focus($ln_msg["focus_to"]);            
            }
            
            return array("success" => false, 'trans_no' => $trans_no, "message" => $ln_msg['message']);        
        }
       
        begin_transaction();

            $header_msg = save_daily_item_return_header($trans_no, $type);
                if ($header_msg["success"] !== true) {
                    cancel_transaction();
                    return array("success" => false, 'trans_no' => $trans_no, "message" => $header_msg['message']);
                }

            $line_msg = save_daily_item_return_lines($trans_no);
                if ($line_msg['success'] !== true) {
                    cancel_transaction();
                    return array("success" => false, 'trans_no' => $trans_no, "message" => $line_msg['message']);
                }
                
            $ap_msg = approve_daily_item_return($trans_no, true);
                if ($ap_msg['success'] !== true) {
                    cancel_transaction();
                    return array("success" => false, 'trans_no' => $trans_no, "message" => $ap_msg['message']);
                }

        commit_transaction();

        meta_forward($_SERVER['PHP_SELF'], "SavedID=$trans_no"); 
}

function approve_daily_item_return($trans_no) {
    global $systypes_array;
    
    $type = ST_DAILY_ITEM_RETURN;

        $ap_msg = add_daily_item_retrun_lines($trans_no, $type, $_POST['tran_date']);
            if ($ap_msg["success"] !== true) {          
                return $ap_msg;
            } 

        $af_row = update_daily_item_return_header_approval_status($trans_no, AS_NEW, AS_APPROVED);
            if (! ($af_row > 0)) {          
                return array("success" => false, 'trans_no' => $trans_no, "message" => "Could not be updated approval status.");
            } 
                
        // budget update 
            $dii_details_array = array();
            $has_to_update_budget = false;
            
            $result = get_daily_item_issue_details($trans_no, $type);      
                while ($row = db_fetch($result)) {
                    if ($row['daily_item_type'] != NSIT_COMMENT) {
                        $qty = abs($row['qty']);
                        $b['row_id'] = $row['trans_id'];
                        $b['stock_id'] = $row['stock_id'];
                        $b['task_id'] = $row['task_id'];
                        $b['requested_qty'] = $qty;
                        $b['high_value_item'] = 0;
                        
                        $standard_cost = $row['standard_cost'];
                        $b['reference_cost'] = $standard_cost; 
                        $b['standard_cost'] = $standard_cost;
                        $b['charging_cost'] = $standard_cost;
                        $b['total_issued_qty'] = $qty;
                        $b['qty'] = $qty;

                        $dii_details_array[] = $b;
                        
                        $has_to_update_budget = true;
                    }
                }
                
                if ($has_to_update_budget === false) {
                    return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$type].' has been approved.');
                }

            $mi_tasks_summary = calculate_task_remaining_totals($dii_details_array);
            $created_by = $_SESSION['wa_current_user']->user;
    
            $mtrec_msg = remove_costs_from_tasks($type, $trans_no, $mi_tasks_summary, $created_by);
                if ($mtrec_msg['success'] !== true) {
                    return array("success" => $mtrec_msg['success'], "tran_no" => $trans_no, "message" => $mtrec_msg['message']);
                }
                
        return array("success" => true, 'trans_no' => $trans_no, "message" => $systypes_array[$type].' has been approved.');
}

function add_daily_item_retrun_lines($trans_no, $type, $tran_date_) {

    $returnh = get_daily_item_issue_header_record_type_trans_no(ST_DAILY_ITEM_RETURN, $trans_no);
    $result = get_daily_item_issue_details($trans_no, $type);      
        while ($row = db_fetch($result)) {
            $qty = abs($row["qty"]);
            $tran_qty = abs($row["tran_qty"]);
            
            if ($row['daily_item_type'] == NSIT_STOCK_ITEM) {
                add_stock_adjustment_item($trans_no, $row["stock_id"], $row["loc_code"], $tran_date_, $row["reference"], $qty, $row["standard_cost"], '', 
                                          $row["tran_unit"], $tran_qty, $row["conversion_factor"], $type, $row["task_id"], $row["project_site_id"], 
                                          $row["trans_id"],$returnh['project_id']);
            } else {
                add_non_stock_move($type, $row["stock_id"], $trans_no, $row["loc_code"], $tran_date_, $row["reference"], $qty, $row["standard_cost"], 
                                   $row["person_id"], $row["visible"], $row["price"], $row["discount_percent"], $row["tran_unit"], $tran_qty, 
                                   $row["conversion_factor"], $row["task_id"], $row["project_site_id"], $row["trans_id"], $returnh['project_id']);
            }
        }  
        
            return array("success" => true, 'trans_no' => $trans_no, "message" => "");
}

function view_daily_item_return_header($type, $trans_no) {
    global $document_approval_statuses;
    
    $row = get_daily_item_issue_header_type_trans_no($type, $trans_no);
    
        start_outer_table(TABLESTYLE2, "width=60%");

            table_section(1, "50%");
                label_row("Issue Trans #:", get_inventory_trans_view_str(ST_DAILY_ITEM_ISSUE, $row['source_id']), "class='tableheader2'");
                label_row(_("Reference:"), $row['reference'], "class='tableheader2'");
                label_row(_("Porject:"), $row['project_description'], "class='tableheader2'");
                label_row(_("Date:"), sql2date($row['tran_date']), "class='tableheader2'");

            table_section(2, "50%");
                label_row(_("Location:"), $row['location_name'], "class='tableheader2'");
                label_row(_("Movement Type:"), $row['movement_type_name'], "class='tableheader2'");
                label_row(_("Approval Status:"), $document_approval_statuses[$row['approval_status']], "class='tableheader2'");

        end_outer_table(2);

        if (strlen($row['comments']) > 0) {
            start_table(TABLESTYLE2);
                label_row(_("Comment:"), nl2br($row['comments']), "class='tableheader2'");
            end_table(3);
        }
}

function view_daily_item_return_details($type, $trans_no) {
    global $daily_item_types_array, $inventory_movement_type_linked_fields;
    
    $header = get_daily_item_issue_header_type_trans_no($type, $trans_no);
        $dii_trans_no = $header['source_id'];
    
        $dii_header = get_daily_item_issue_header_type_trans_no(ST_DAILY_ITEM_ISSUE, $dii_trans_no);
            $linked_field_type = $dii_header['linked_field_type'];
            $link_f_name = $inventory_movement_type_linked_fields[$linked_field_type];
    
    start_table(TABLESTYLE, "width=98%");

        $th = array(_("ID"), _("Issue ID"), _("Item Type"), _("Description"), _("Unit"), _("Qty"), _('Budget'), _('Cost'), _("$link_f_name"), _('Job'), 
                    _('Comment'));  
        table_header($th);

        $k = 0; //row colour counter
            $result = get_daily_item_issue_details($trans_no, $type);      
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

                        label_cell($row['trans_id'], 'align=center');
                        label_cell($row['source_line_id'], 'align=center');
                        label_cell($daily_item_types_array[$row['daily_item_type']]);
                        if ($row['daily_item_type'] == NSIT_NON_STOCK_ITEM || $row['daily_item_type'] == NSIT_STOCK_ITEM) {
                            label_cell($row["stock_id"]." - ".$row["item_description"]);  
                        } else {
                            label_cell($row["item_description"]);
                        }
                        label_cell($row["unit_name"]);
                        label_cell(number_format(abs($row["tran_qty"]), 2), 'align=right');
                        
                        if ($row['daily_item_type'] == NSIT_COMMENT) {
                            label_cell('No budget required');
                        } else {
                            label_cell($row["task_description"]);
                        }
                        $tran_unit_cost = $row["standard_cost"] * $row["conversion_factor"];
                            if ($tran_unit_cost > 0) {
                                label_cell(number_format($tran_unit_cost, 2), 'align=right');
                            } else {
                                label_cell('');
                            }
                            
                        $lf_name = get_movement_type_linked_field_values($header['linked_field_type'], $row['linked_field_id'], $header['group_company_code']);
                            label_cell($lf_name["value"]); 
                        
                        label_cell($row["site_description"]);          
                        label_cell(nl2br($row["line_comment"]));          

                    end_row();        
                }

    end_table(3);
}

?>