<?php

function check_asset_verification_count_detail_data($selected_id, $asset_verification_count_no) {
    $sch = get_asset_verification_count_header_record($asset_verification_count_no);
        if ($sch['status_id'] != SC_COUNTING){
            display_error(_("Status of this asset verification count does not allow updating its details."));
            set_focus('good_qty_'.$selected_id);
            return false;
        }
    
    if (! (isset($_POST['asset_id_'.$selected_id]) && strlen($_POST['asset_id_'.$selected_id]) > 0)) {
        display_error(_("Please select an asset."));
        set_focus('asset_id_'.$selected_id);
        return false;
    }
    
    if (isset($_POST['AddItem'])) {
        $sc_item = check_asset_verification_count_item_exists($asset_verification_count_no, $_POST['asset_id_'.$selected_id]);
            if (is_array($sc_item)) {
                display_error(_("Selected asset already entered in sheet number ".$sc_item['sheet_no']).'.');
                set_focus('asset_id_'.$selected_id);
                return false;
            }
    }

    if (! (input_num('line_sheet_no_'.$selected_id) > 0)) {
        display_error(_("Sheet number has to be a positive number."));
        set_focus('line_sheet_no_'.$selected_id);
        return false;
    }
    
    if (! (check_num('good_qty_'.$selected_id, 0))) {
        display_error(_("Good qunatity entered should be zero or higher."));
        set_focus('good_qty_'.$selected_id);
        return false;
    }
    
    if (! (check_num('damaged_qty_'.$selected_id, 0))) {
        display_error(_("Damaged qunatity entered should be zero or higher."));
        set_focus('damaged_qty_'.$selected_id);
        return false;
    }
    
    $asset_row = get_asset($_POST['asset_id_'.$selected_id]);
        if ($asset_row['multiple_items'] == 0) {
            $asset_counted_qty = input_num('good_qty_'.$selected_id) + input_num('damaged_qty_'.$selected_id);
                if ($asset_counted_qty > 1) {
                    display_error(_("Total counted quantity cannot be higher than one for single item assets."));
                    set_focus('good_qty_'.$selected_id);
                    return false;
                }
        }
    
	return true;
}

function can_delete_asset_verification_count_detail($selected_id, $asset_verification_count_no) {
    $sch = get_asset_verification_count_header_record($asset_verification_count_no);
        if ($sch['status_id'] != SC_COUNTING) {
            display_error(_("Status of this asset count does not allow updating its details."));
            return false;
        }
    
    $scd = get_asset_verification_count_detail($selected_id);
        if (! (in_array($scd['status_id'], array(0, SC_NEW, SC_COUNTING)))) {
            display_error(_("Status of this asset count detail item does not allow updating it."));
            return false;
        }
    
	return true;
}

function display_asset_verification_count_details($asset_verification_count_no, $selected_id) {
    $sheet_no = $_POST['sheet_no'];
    
    start_table(TABLESTYLE, "width=95%");

        $th = array(_("Asset"), _("Description"), _("Multiple Item"), _("Sheet No"), _("Good Qty"), _("Damaged Qty"), _("System Qty"), "", "");
    
        table_header($th);

            $j = 1;
            $k=0; //row colour counter

            $result = get_asset_verification_count_details($asset_verification_count_no, $sheet_no);

                while ($myrow = db_fetch($result)) {
                    if ($selected_id != $myrow["id"]) {
                        $dec = 0;
                        
                        alt_table_row_color($k);
                            label_cell($myrow["asset_code"].' ('.$myrow["asset_id"].')');
                            label_cell($myrow["description"]);
                            label_cell(get_yes_no_name($myrow["multiple_items"]), 'align=center');
                            label_cell($myrow["sheet_no"], "align=center");
                            label_cell(number_format($myrow["good_qty"], $dec), "align=right");
                            label_cell(number_format($myrow["damaged_qty"], $dec), "align=right");           
                            label_cell(number_format($myrow["system_qty"], $dec), "align=right");

                            edit_button_cell("Edit".$myrow["id"], _("Edit"));    
                            delete_button_cell("Delete".$myrow["id"], _("Delete"));
                        end_row();
                        
                        $j++;
                        If ($j == 25) {
                            $j = 1;
                            table_header($th);
                        }
                    } else {
                        start_row();
                            $m = "This will NOT update system quantity to the latest value. "
                                    . " \r\n If the asset has been counted again at a later time delete this line and re-enter. "
                                    . "That will update system quantity to the latest value.";
                            label_cell( "<font color='red'>".nl2br($m)."</font>","colspan=9 align=center");
                        end_row();
                        
                        display_asset_verification_count_details_edit_controls($selected_id);
                    }
                }

                if (! ($selected_id > 0)) {
                    display_asset_verification_count_details_edit_controls($selected_id);
                }
    
    end_table(2);

    hidden('selected_id', $selected_id);
}

function display_asset_verification_count_details_edit_controls($selected_id) {
    global $Ajax;
    
    if ($selected_id > 0) {
        $sc_detail = get_asset_verification_count_detail($selected_id);
    }
    
    $Ajax->activate('asset_ver_edit');
    div_start('asset_ver_edit');
    
        start_row();
            if($selected_id > 0){
                $asset_id = $sc_detail['asset_id'];

                label_cell($sc_detail['asset_code']." ($asset_id)");
                label_cell($sc_detail['asset_description']);
                label_cell(get_yes_no_name($sc_detail["multiple_items"]), 'align=center');
                $_POST['good_qty_'.$selected_id] = $sc_detail['good_qty'];
                $_POST['damaged_qty_'.$selected_id] = $sc_detail['damaged_qty'];
                $_POST['line_sheet_no_'.$selected_id] = $sc_detail['sheet_no'];

                hidden('asset_id_'.$selected_id, $sc_detail['asset_id']);

            } else {
                asset_list_cells(null, 'asset_id_'.$selected_id, null, false, true);
                $asset_id = $_POST['asset_id_'.$selected_id];

                $_POST['good_qty_'.$selected_id] = 0;
                $_POST['damaged_qty_'.$selected_id] = 0;

                if ($_POST['sheet_no'] > 0) {
                    $_POST['line_sheet_no_'.$selected_id] = $_POST['sheet_no'];
                } else {
                    $_POST['line_sheet_no_'.$selected_id] = 1;
                }
                
                $current_asset = get_asset($_POST['asset_id_'.$selected_id]);
                    if (is_array($current_asset)) {
                        label_cell(get_yes_no_name($current_asset["multiple_items"]), 'align=center');
                    } else {
                        label_cell('');
                    }
            }

            small_qty_cells(null, 'line_sheet_no_'.$selected_id, null, null, null, 0);
            amount_cells(null, 'good_qty_'.$selected_id, null, null, null, 0);
            amount_cells(null, 'damaged_qty_'.$selected_id, null, null, null, 0);

            label_cell('');

            if ($selected_id > 0) {
                button_cell('UpdateItem', _("Update"), _('Confirm changes'), ICON_UPDATE);
                button_cell('CancelItemChanges', _("Cancel"), _('Cancel changes'), ICON_CANCEL);
                hidden('LineNo', $selected_id);
                set_focus('qty');
            } else {
                submit_cells('AddItem', _("Add Item"), "colspan=2", _('Add new item to document'), true);
            }

        end_row();
    
    div_end();
}

function can_asset_verification_end_count($asset_verification_count_no) {
    $can_end_count = true;
    
    $dup_msg = ("Not posible to End counting due to below duplicate counted items: <br>");
        
        $result = get_asset_verification_count_duplicate_items($asset_verification_count_no);
            while ($row = db_fetch($result)) {
                $can_end_count = false;

                $itemq = get_asset_verification_count_duplicate_item_details($asset_verification_count_no, $row['asset_id']);
                    while ($item = db_fetch($itemq)) {
                        $dup_msg .= "Asset ".$item['asset_id']." - ".$item['asset_description']." in sheet No ".$item['sheet_no'].'. ';
                    }
            }
            
            if ($can_end_count === false) {
                display_error($dup_msg);
            }

    return $can_end_count;
}

function build_asset_verification_count_detail_array(&$sc_item, $selected_id, $asset_verification_count_no) {
    $sc_item['selected_id'] = $selected_id;
    
    $sc_item['asset_verification_count_no'] = $asset_verification_count_no;
    $sc_item['asset_id'] = $_POST['asset_id_'.$selected_id];
    
    $sc_item['good_qty'] = input_num('good_qty_'.$selected_id);
    $sc_item['damaged_qty'] = input_num('damaged_qty_'.$selected_id);
    
    $sc_item['line_sheet_no'] = $_POST['line_sheet_no_'.$selected_id];
    
    return true;
}

function save_asset_verification_count_detail(&$selected_id, $asset_verification_count_no) {
    if (! check_asset_verification_count_detail_data($selected_id, $asset_verification_count_no)) {
        return;
    }
    
    $sc_header = get_asset_verification_count_header($asset_verification_count_no);
        if ($sc_header['status_id'] != SC_COUNTING) {
            display_error("This asset count status does not allow updating details."); 
            return;
        }
    
    $sc_item = array();
    build_asset_verification_count_detail_array($sc_item, $selected_id, $asset_verification_count_no);
    
        $sc_item['header_status_id'] = $sc_header['status_id'];
        $sc_item['loc_code'] = $sc_header['loc_code'];
        $sc_item['tran_date'] = $sc_header['tran_date'];

    $msg = save_asset_verification_count_detail_item($sc_item);    
        if ($msg['success'] !== true) {        
            display_error($msg['message']);      
        } else {
            $selected_id = -1;
            display_notification($msg['message']);  
        }      
}

function save_asset_verification_count_detail_item($sc_item) {
    $selected_id = $sc_item['selected_id'];
    $asset_verification_count_no = $sc_item['asset_verification_count_no'];
    $asset_id = $sc_item['asset_id'];
    
    $good_qty = $sc_item['good_qty'];
    $damaged_qty = $sc_item['damaged_qty'];
    $line_sheet_no = $sc_item['line_sheet_no'];
    
    $status_id = $sc_item['header_status_id'];
    $loc_code = $sc_item['loc_code'];

    $asset = get_asset($asset_id);
    
    $current_value = $asset['current_value'];
            
        if ($selected_id > 0) {
            $n = update_asset_verification_count_detail($selected_id, $line_sheet_no, $good_qty, $damaged_qty, $status_id);
                if ($n == 1) {
                    return array("success" => true, "message" => "Selected asset verification count #$selected_id has been updated.");    
                } else { 
                    return array("success" => false, "message" => "Another user has updated selected asset verification count #$selected_id.");    
                } 
        } else {
            $qoh = get_asset_total_qty($asset_id, $loc_code); 
            
            add_asset_verification_count_detail($asset_verification_count_no, $line_sheet_no, $asset_id, $good_qty, $damaged_qty, $qoh, $current_value, 
                                                $status_id);

            return array("success" => true, "message" => "New asset verification counted detail has been added.");    
        }
}

function delete_asset_verification_count_detail_item(&$selected_id, $asset_verification_count_no) {
    if (! can_delete_asset_verification_count_detail($selected_id, $asset_verification_count_no)) {
        return;
    }
    
    $n = delete_asset_verification_count_detail($selected_id);
        if ($n == 1) {
            display_notification("Selected asset verification count detail item has been deleted.");    
        } else {
            display_error("Could not delete selected asset verification count detail item.");    
        }
}

?>