create or replace FUNCTION "PLANTA_F_CALC_PERIOD_FACTOR" 
(p_units_day NUMBER,
 p_calculation IN NUMBER default 0, 
 p_val IN NUMBER default 0, 
 p_unit IN VARCHAR default 'h', 
 p_factor_bu IN NUMBER default 1
)
RETURN NUMBER 
IS 
 v_result       number;
 value_hrs       number;
 calculationtype number;

 -- Default Values, in case parameters are NULL
 units_day      number := 8;
 calculation    number(1) := 0;
 val          number(8,2) := 0;
 unit       varchar2(2) := 'h';
 factor_bu     number(8,2) := 1;
BEGIN
/*
  Function calculates the "Available capacity for periods", on the basis of
  Factor Parameters from the resources Data Sheets.
  Default Factors in standard: BASE_LOAD, OVERTIME, OTHER
  Default calculations in standard: BASE_LOAD (-), OVERTIME (+), OTHER (-)
*/

  -- overwrite Default Values, if parameter values are provided
  if p_units_day is not null then units_day := p_units_day; end if;
  
  if p_calculation is not null then calculation := p_calculation; end if;  
  
  if p_val is not null then val := p_val; end if; 
  if p_unit is not null then unit := p_unit; end if;  
  if p_factor_bu is not null then factor_bu := p_factor_bu; end if; 
  
  --v_result := units_day;
  
  ------------------------------------------------------------------------------  
  
  if unit = 'h' or unit = 'T' then
  value_hrs := val * factor_bu;  
  else
  value_hrs := val / 100 * units_day; 
  end if;
  
  -- 1 and -1 are intentionally inverted
  if calculation = 0 then   -- Minus
  calculationtype := 1;
  else                      -- Plus
  calculationtype := -1;
  end if;
  
  --v_result := v_result + (value_hrs * calculationtype);
  v_result := (value_hrs * calculationtype);
  
return v_result;

END;