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

//$page_security = 'SA_APPROVE_SALARY_AJUSTMENTS';
//if (!@$_GET['popup'])
//	$path_to_root = "..";
//else	
//	$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/ui.inc");
//include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
//include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
//include_once($path_to_root . "/projects/includes/db/work_done_entry_details_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();
//}
//
//
//page(_($help_context = "Work Done Entry Details"), false, false, "", $js);


//-------------------------------------------------------------------------------------------
$project_id = -1;

if(isset($_GET['ProjectId']))
{
    $_POST['ProjectId'] = $_GET['ProjectId'];
}
if(isset($_POST['ProjectId']))
{
    $project_id = $_POST['ProjectId'] ;
}

$boq_group_id = -1;

if(isset($_GET['BOQGroupId']))
{
    $_POST['BOQGroupId'] = $_GET['BOQGroupId'];
}
if(isset($_POST['BOQGroupId']))
{
    $boq_group_id = $_POST['BOQGroupId'] ;
}


function display_work_done_entry_details($project_id, $boq_group_id, $entry_date, $project_site_id)
{
    global $Ajax; 
    $Ajax->activate('work_done_details');
    
    if(!$project_site_id >= 0){
        $project_site_id = 0;
    }
    
    div_start('work_done_details');
//  display_heading('Expenses');
    
    $entry_date = date2sql($_POST['entry_date']);
    
    start_table(TABLESTYLE, "width=90%");
//    $result_a = get_group_boq_items($boq_group_id);
    $result_a = get_project_group_boq_items($project_id, $boq_group_id);
    
    
    $th = array(_("ID"),_("Code"), _("Description"), "Billing Type",
                _("Completed Quantity"), _("Unit"), "Last Entry Date",  _("Quantity"),_("Comments"), "");
   
    table_header($th);

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

    while ($boq_item = db_fetch($result_a))
    {
        alt_table_row_color($k);
        
//        label_cell($boq_item["id"], "align=right");
//        label_cells(get_petty_cash_trans_view_str(ST_pro, $iou_line["id"]));
        label_cell(get_project_trans_view_str(ST_PROJECT_BOQ_ITEM, $boq_item["id"]), "align=center");
        label_cell($boq_item["code"]);
        label_cell($boq_item["description"]);

        if($boq_item["billing_type"] == 1){
            label_cell("Lump Sum");
        }else{
            label_cell("Measure and Pay");
        }
        
//        label_cell($boq_item["completed_qty"], "align=right");
        label_cell(get_project_trans_view_str(ST_PROJECT_WORK_DONE, $boq_item["completed_qty"]), "align=right");
        label_cell($boq_item["unit"]);      
        
        if($boq_item["last_entry_date"] == "0000-00-00"){
            label_cell("");
        }else{
            label_cell(sql2date($boq_item['last_entry_date']), "align=center");
        }
       
//        mmddyyyy=sql2date(yyyymmdd);
        $work_done_detail = get_work_done_detail($boq_item["id"], $entry_date, $project_site_id);
        if($work_done_detail){
            $_POST[$boq_item["id"]."_tran_date"] = sql2date($work_done_detail['tran_date']);
            $_POST[$boq_item["id"]."_quantity"] = $work_done_detail["quantity"];
            $_POST[$boq_item["id"]."_comment"] = $work_done_detail["comment"];
        }
        else{
            $_POST[$boq_item["id"]."_tran_date"] = sql2date($entry_date);
            $_POST[$boq_item["id"]."_quantity"] = 0.00;
            $_POST[$boq_item["id"]."_comment"] = "";
        }
        
//      $_POST[$boq_item["id"]."_tran_date"] = sql2date($_POST[$boq_item["id"]."_tran_date"]);
//        date_cells(null, $boq_item["id"]."_tran_date");
        amount_cells(null,$boq_item["id"]."_quantity");
        
        text_cells(null, $boq_item["id"]."_comment");
            
        hidden($boq_item["id"]."_"."boq_item_id", $boq_item["id"]);

        check_cells(null, $boq_item["id"]."_check_item");
    }
    
    hidden('ProjectId', $project_id);
    hidden('BOQGroupId', $boq_group_id);
    
    end_table(1);
    div_end();
}


function can_process($boq_item_id)
{
    $tran_date = $_POST['entry_date'];  
    $site_id = $_POST['project_site_id'];  
    
    $boq_item = get_project_boq_item_details($boq_item_id);
//    $last_entry_date = sql2date($boq_item['last_entry_date']);     
    $description = $boq_item['description'];
    $code = $boq_item['code'];
    
    if(!(check_num2(input_num($boq_item_id."_quantity"), 0.001))){   
        $message = "[".$code."] ".$description." - quantity entered should be positive and valid amount."; 
        $set_focus = $boq_item["id"]."_quantity";
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);                    
    }
        
    $site_wd = get_last_site_work_done_detail_record($boq_item_id, $site_id);
    if($site_wd){
        $last_entry_date = sql2date($site_wd['tran_date']);
    }
    else{
        $last_entry_date = "";
    }
    
    if(date1_greater_date2($last_entry_date, $tran_date)){   
        $message = "[".$code."]".$description." - There is an already entered workdone detail for ".$last_entry_date."."; 
        $set_focus = $boq_item["id"]."_quantity";
        return array("success" => false, "focus_to" => $set_focus, "message" => $message);                    
    }     

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


function delete_work_done_entry_details($boq_group_id)
{   
    $delete_count = 0;
    
    $site_id = $_POST['project_site_id'];
    if(!($site_id > 0)){
        $site_id = 0;
    }
    
    $batch_q = get_group_boq_items($boq_group_id);

    while ($boq_item = db_fetch($batch_q))
    {
        if(isset($_POST[$boq_item["id"]."_check_item"]) && $_POST[$boq_item["id"]."_check_item"] == 1){  
                    
            $boq_item_id = $_POST[$boq_item["id"]."_"."boq_item_id"];
            $tran_date =  $_POST['entry_date'];
                
            delete_work_done_details($boq_item_id, $site_id, $tran_date);     
            
            unset($_POST[$boq_item["id"]."_check_item"]);
            $delete_count++;
        }
        else{
            continue;
        }
    }    
    
    return $delete_count;
      
}


function save_work_done_entry_details($project_id, $boq_group_id)
{   
//    $batch_q = get_group_boq_items($boq_group_id);
    $batch_q = get_project_group_boq_items($project_id, $boq_group_id);

    while ($boq_item = db_fetch($batch_q))
    {
        if(isset($_POST[$boq_item["id"]."_check_item"]) && $_POST[$boq_item["id"]."_check_item"] == 1){  
                $r = can_process($boq_item["id"]);            
                if($r["success"] !== true){
                    return $r;
                }
                    
                $boq_item_id = $_POST[$boq_item["id"]."_"."boq_item_id"];
                $site_id = $_POST['project_site_id'];
                if(!($site_id > 0)){
                    $site_id = 0;
                }
                $tran_date =  $_POST['entry_date'];
                $quantity = input_num($boq_item["id"].'_'.'quantity');
                $comment =  $_POST[$boq_item["id"].'_'.'comment']; 
                
            $n = write_work_done_entry_detail($boq_item_id, $site_id, $tran_date, $quantity, $comment);     
            
            unset($_POST[$boq_item["id"]."_check_item"]);
        }
        else{
            continue;
        }
    }    
        return $r;
}


if(isset($_POST['Update_workdone_entry_details'])){
    $wde = save_work_done_entry_details($project_id, $boq_group_id);
    if($wde["success"] !== true){
        if(isset($wde["message"])){            
             display_error($wde["message"]);                
        }      
    }    
}

if(isset($_POST['Delete_workdone_entry_details'])){
    $n = delete_work_done_entry_details($boq_group_id);
    if($n == 0){
       display_error($wde["No items selected"]);    
    }
    else{
        display_note("All previous workdone entries up to ".$_POST["entry_date"]." of the selected items deleted.");     
    }    
}

//global $Ajax;
//
//start_form();
//br();
//
//if($boq_group_id > 0){
//    $boq_item_group = get_project_boq_item_group_record($boq_group_id);
//    display_heading2("BOQ Items of ".$boq_item_group['code']."-". $boq_item_group['description']);
//}
//else{
//    display_heading2("BOQ Items with no Group");
//}
//
//br();
//
//$Ajax->activate('work_done_filtering_controls');
//
//div_start('work_done_filtering_controls');
//
//start_table(TABLESTYLE, "width=30%");
//date_row("Entry date :", "entry_date", null, null, 0, 0, 0, null, true);
//project_sites_list_row("Job :", "project_site_id", $project_id, null, "Select a job" , true);
//end_table();
//  
//div_end();
//br();
//    
//display_work_done_entry_details($project_id, $boq_group_id, $_POST['entry_date'], $_POST['project_site_id']);
//
////submit_center_first('Update_workdone_entry_details', _("Update"), '', 'default');
////submit_center_first('Delete_workdone_entry_details', _("Delete"), '', 'default');
//  submit_center_first('Update_workdone_entry_details', _("Update"), '', 'default');
//  submit('Delete_workdone_entry_details', 'Delete', true, '', 'default');
//
//br();
//
//if (!@$_GET['popup'])
//{
//	end_form();
//	end_page(@$_GET['popup'], false, false);
//}
//



?>
