-----------------------------------------------------------------------------
--
--  Logical unit: SubvalValuation
--  Component:    SUBVAL
--
--  IFS Developer Studio Template Version 3.0
--
--  Date    Sign    History
--  ------  ------  ---------------------------------------------------------
-----------------------------------------------------------------------------

layer Cust;

-------------------- PUBLIC DECLARATIONS ------------------------------------


-------------------- PRIVATE DECLARATIONS -----------------------------------


-------------------- LU SPECIFIC IMPLEMENTATION METHODS ---------------------


-------------------- LU SPECIFIC PRIVATE METHODS ----------------------------


-------------------- LU SPECIFIC PROTECTED METHODS --------------------------


-------------------- LU SPECIFIC PUBLIC METHODS -----------------------------
@Override
PROCEDURE Receive_Application (
   info_             OUT VARCHAR2,
   sub_con_no_       IN  VARCHAR2,
   valuation_no_     IN  NUMBER,
   company_          IN  VARCHAR2,
   voucher_date_     IN  DATE,
   user_group_       IN  VARCHAR2,
   app_voucher_type_ IN  VARCHAR2 )
   
   
IS
   
   CURSOR get_value IS
   SELECT gen_valuation_work_value FROM Subval_Valuation_Summary
   WHERE sub_con_no = sub_con_no_ AND valuation_no = valuation_no_;
   
   afp_value_ NUMBER;
  
   
BEGIN
   
   super(info_,sub_con_no_,valuation_no_,company_,voucher_date_,user_group_,app_voucher_type_);
   
   
   OPEN get_value;
   FETCH get_value INTO afp_value_;
   CLOSE get_value;
   
   --C_Validate(sub_con_no_,valuation_no_, afp_value_);
   --Test(sub_con_no_,valuation_no_);
   
END Receive_Application;


-------------------- LU CUST NEW METHODS -------------------------------------

PROCEDURE Test (
   sub_con_no_   IN VARCHAR2,
   valuation_no_ IN NUMBER
   )
   
IS
   
   project_id_ VARCHAR2(200);
   log_ VARCHAR2(200);
   comp_log_ VARCHAR2(32000);
   
   
   CURSOR get_afp_lines IS
  select t.activity_seq,t.item_no, t.application_tot_value from subval_valuation_line_Item t
  where sub_con_no = sub_con_no_ and valuation_no = valuation_no_;
   
   
   CURSOR get_project(activity_seq_ VARCHAR2) IS
   SELECT project_id FROM sub_con_project WHERE sub_con_no = sub_con_no_ AND activity_seq = activity_seq_;

   CURSOR get_proj_cons(project_id_ VARCHAR2, activity_seq_ NUMBER) IS
   select rownum, t.* from project_connection_details t
where t.project_id = project_id_
and t.activity_seq = activity_seq_
and t.proj_lu_name = 'Activity Estimate';
   
BEGIN
   
   FOR rec_ IN get_afp_lines LOOP
      
      OPEN get_project(rec_.activity_seq);
      FETCH get_project INTO project_id_;
      CLOSE get_project;
      
      FOR cons_ IN get_proj_cons(project_id_,rec_.activity_seq) LOOP         
         IF (rec_.item_no = cons_.rownum) THEN
            error_sys.record_general('Debug','Debug 2');
            C_Check_For_Budget(project_id_,rec_.activity_seq,rec_.application_tot_value,cons_.control_category,log_);
            comp_log_ := comp_log_ || log_;         
         END IF;
      END LOOP;    

   END LOOP;
   error_sys.record_general('DEBUG','Debug :P1',comp_log_);
END Test;

PROCEDURE C_Check_For_Budget(
   project_id_   IN VARCHAR2,
   activity_seq_ IN NUMBER,
   afp_value_ IN NUMBER,
   control_category_ IN VARCHAR2,
   log_ OUT VARCHAR2
   )   
IS
  
    committed_cost_ NUMBER;
   used_cost_ NUMBER;
   budget_ NUMBER;
   comparison_ NUMBER;
   
   CURSOR get_activity_element IS
   SELECT committed,used
      FROM  activity_element_summary_cost
      WHERE activity_seq = activity_seq_ AND project_id = project_id_ AND control_category = control_category_;
      
      CURSOR get_budget IS
      SELECT pfi.budget_base_curr FROM project_forecast pf,project_forecast_item_all pfi
WHERE pf.project_id = project_id_
and pf.active_budget_db = 'TRUE'
and pf.project_id = pfi.project_id 
and pf.forecast_id = pfi.forecast_id
and pfi.activity_seq = activity_seq_
and pfi.project_cost_element = control_category_;
      
   CURSOR get_control_budget IS
   select Proj_Budget_Control_Rules_API.Get_Control_Total(t.combination_id) budget from proj_budget_control_rules t
   where project_id = project_id_ AND activity_seq = activity_seq_ AND project_cost_element = control_category_;
   
  
   
BEGIN
   
   
   
   OPEN get_activity_element;
   FETCH get_activity_element INTO committed_cost_,used_cost_;
   CLOSE get_activity_element;
   
--   OPEN get_budget;
--   FETCH get_budget INTO budget_;
--   CLOSE get_budget;
   
   OPEN get_control_budget;
   FETCH get_control_budget INTO budget_;
   CLOSE get_control_budget;
   
   comparison_ := nvl(used_cost_,0) + nvl(committed_cost_,0);
   
   IF (nvl(committed_cost_,0) < afp_value_) THEN
      --comparison_ := ((afp_value_ - nvl(committed_cost_,0)) + nvl(committed_cost_,0)) + nvl(used_cost_,0);
      comparison_ := nvl(used_cost_,0);
      
   END IF;
   
   IF (nvl(committed_cost_,0) > afp_value_) THEN
      comparison_ := (nvl(committed_cost_,0) - afp_value_) + (afp_value_ + nvl(used_cost_,0));
      
   END IF;   
   
   --comparison_ := call_off_value_;
   --error_sys.record_general('DEBUG','Debug budget - :P1',budget_ || ' com - ' || comparison_ );
   log_ := 'budget - ' || budget_ || ' comp - ' || comparison_;
   IF (budget_ < comparison_) THEN
       log_ := 'budget - ' || budget_ || ' comp - ' || comparison_;
      --error_sys.record_general('SubConCallOff','ACTBUDGERROR: AFP amount exceeds active budget');      
   END IF;  
   
END C_Check_For_Budget;

@DynamicComponentDependency PROJ
PROCEDURE C_Validate (
   sub_con_no_   IN VARCHAR2,
   valuation_no_ IN NUMBER,
   afp_value_ IN NUMBER
   ) 
   
IS
   
  CURSOR get_afp_lines IS
  select t.activity_seq,t.application_tot_value from subval_valuation_line_Item t
  where sub_con_no = sub_con_no_ and valuation_no = valuation_no_;
   
  CURSOR get_project_details IS
  SELECT project_id
  FROM sub_con_project WHERE sub_con_no = sub_con_no_;
  
  CURSOR get_project(activity_seq_ VARCHAR2) IS
   SELECT project_id FROM sub_con_project WHERE sub_con_no = sub_con_no_ AND activity_seq = activity_seq_;
  
  CURSOR get_cat IS
  select t.CATEGORY1_NAME from sub_contract t
where sub_con_no = sub_con_no_;
  
    
  project_id_ VARCHAR2(200);
  control_category_ VARCHAR2(200);
  log_ VARCHAR2(200);
  log_temp_ VARCHAR2(400);
   
BEGIN
  
  OPEN get_cat;
   FETCH get_cat INTO control_category_;
   CLOSE get_cat;
  
   FOR rec_ IN get_afp_lines LOOP     

      OPEN get_project(rec_.activity_seq);
      FETCH get_project INTO project_id_;
      CLOSE get_project;
      
  --error_sys.record_general('DEBUG','Debug project - :P1',project_id_ || ' activity - ' || rec_.activity_seq );
  
      IF (Project_API.Get_C_Budg_On_Subcont_Db(project_id_) = 'TRUE') THEN
  
         C_Check_For_Budget(project_id_,rec_.activity_seq,rec_.application_tot_value,control_category_,log_);
         --C_Check_Budget(project_id_,rec_.activity_seq,0,log_);
         --log_temp_ := log_temp_  || ' | ' || log_;
      END IF;
   
  
  END LOOP;

  --error_sys.record_general('DEBUG','Debug log - :P1',log_temp_);
  
END C_Validate;


PROCEDURE C_Check_Budget(
   project_id_   IN VARCHAR2,
   activity_seq_ IN NUMBER,
   call_off_value_ IN NUMBER,
   check_log_ OUT VARCHAR2
   )   
IS
  
   budget_ NUMBER;
   comparison_ NUMBER;
   
   log_ VARCHAR2(200);
   
   CURSOR get_activity_element IS
   SELECT committed,used,control_category
      FROM  activity_element_summary_cost
      WHERE activity_seq = activity_seq_ AND project_id = project_id_;
      
   CURSOR get_control_budget(control_category_ VARCHAR2) IS
   select Proj_Budget_Control_Rules_API.Get_Control_Total(t.combination_id) budget from proj_budget_control_rules t
   where project_id = project_id_ AND activity_seq = activity_seq_ AND project_cost_element = control_category_;
   
  
   
BEGIN
   
   
   FOR rec_ IN get_activity_element LOOP
   
      OPEN get_control_budget(rec_.control_category);
      FETCH get_control_budget INTO budget_;
      CLOSE get_control_budget;
   
      comparison_ := nvl(rec_.used,0) + nvl(rec_.committed,0);
      --comparison_ := call_off_value_;
    log_ := log_ || 'cc - ' || rec_.control_category || ' ( budget - ' || budget_ || 'comparison - ' || comparison_ || ') '; 
   --error_sys.record_general('DEBUG','Debug budget - :P1',budget_ || ' com - ' || comparison_ );
   
      IF (budget_ < comparison_) THEN
         error_sys.record_general('SubConCallOff','ACTBUDGERROR: AFP amount exceeds active budget');      
      END IF;  
   
   END LOOP;
   
   check_log_ := log_;
   --error_sys.record_general('DEBUG','Debug - :P1',log_);
END C_Check_Budget;


