-----------------------------------------------------------------------------
--
--  Logical unit: SubConCallOff
--  Component:    SUBCON
--
--  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 Release__ (
   info_       OUT           VARCHAR2,
   objid_      IN            VARCHAR2,
   objversion_ IN OUT NOCOPY VARCHAR2,
   attr_       IN OUT NOCOPY VARCHAR2,
   action_     IN            VARCHAR2 )
IS
   
   rec_ sub_con_call_off_tab%ROWTYPE;
BEGIN
   
   super(info_,objid_,objversion_,attr_,action_);
   rec_ := Lock_By_Id___(objid_, objversion_);
   
   C_Prepare_Validate(rec_.sub_con_no,rec_.call_off_seq);
   
END Release__;


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

PROCEDURE C_Prepare_Validate(
   sub_con_no_ IN VARCHAR2,
   call_off_seq_ IN NUMBER
   )
   
IS
   
   project_id_ VARCHAR2(200);
   category_ VARCHAR2(200);
   CURSOR get_activities IS
   SELECT sci.activity_seq, 
   SUB_CON_CO_LINES_API.Get_Tot_Cum_Co_Line_Value(sccl.sub_con_no,
                                             sccl.call_off_seq,
                                             sccl.call_off_line_no,
                                             SUB_CON_CALL_OFF_API.Get_Currency_Code(sccl.sub_con_no, 
                                                                                    sccl.call_off_seq)) call_off_value
   FROM sub_con_co_lines sccl,sub_con_co_items scci,sub_con_item sci
   WHERE sccl.sub_con_no = sub_con_no_ AND sccl.call_off_seq = call_off_seq_
   AND scci.sub_con_no = sccl.sub_con_no AND scci.call_off_seq = sccl.call_off_seq AND scci.call_off_line_no = sccl.call_off_line_no
   AND sci.sub_con_no = scci.sub_con_no AND sci.rev_seq = scci.rev_seq AND sci.line_no = scci.line_no AND sci.item_no = scci.item_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_;
   
   
BEGIN
   
   OPEN get_cat;
   FETCH get_cat INTO category_;
   CLOSE get_cat;
   
   FOR rec_ IN get_activities LOOP
      
      OPEN get_project(rec_.activity_seq);
      FETCH get_project INTO project_id_;
      CLOSE get_project;
      
      IF (Project_API.Get_C_Budg_On_Subcont_Db(project_id_) = 'TRUE') THEN
      
         C_Check_For_Budget(project_id_,rec_.activity_seq,rec_.call_off_value,category_);
      
      END IF;
      
   END LOOP;
   
   
END C_Prepare_Validate;


PROCEDURE C_Check_For_Budget(
   project_id_   IN VARCHAR2,
   activity_seq_ IN NUMBER,
   call_off_value_ IN NUMBER,
   control_category_ IN VARCHAR2
   )   
IS
   
   CURSOR get_activity_element IS
   SELECT committed,planned_committed,used
      FROM  activity_element_summary_cost
      WHERE activity_seq = activity_seq_ AND project_id = project_id_ AND control_category = control_category_;
   
   
   pl_committed_cost_ NUMBER;
   committed_cost_ NUMBER;
   used_cost_ NUMBER;
   budget_ NUMBER;
   comparison_ NUMBER;
   
   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_,pl_committed_cost_,used_cost_;
   CLOSE get_activity_element;
   

   OPEN get_control_budget;
   FETCH get_control_budget INTO budget_;
   CLOSE get_control_budget;

   comparison_ := nvl(committed_cost_,0) + nvl(pl_committed_cost_,0) + nvl(used_cost_,0);
   
   IF (budget_ < comparison_) THEN
      
      Error_SYS.record_general('SubConCallOff','ACTBUDGERROR: Call off amount exceeds active budget');      
   END IF;  
   
END C_Check_For_Budget;



