CREATE OR REPLACE PACKAGE mip_helper_special_cases IS

  -- Author  : HARDYA
  -- Created : 21/01/2008 11:40:25
  -- Purpose : helper package to support special cases not provided through mip_'tablename'_helper.

  PROCEDURE table_enquiries(p_rec              IN enquiries%ROWTYPE
                           ,p_mandatory_checks IN OUT mip_mandatory.t_mandatory_checks);

END mip_helper_special_cases;
/
CREATE OR REPLACE PACKAGE BODY mip_helper_special_cases IS

  PROCEDURE table_enquiries(p_rec              IN enquiries%ROWTYPE
                           ,p_mandatory_checks IN OUT mip_mandatory.t_mandatory_checks) IS
    l_bypass_other bypass_reasons.description%TYPE;
  BEGIN
    -- O-M1
    IF p_rec.install_building IS NULL
       AND p_rec.install_sub_building IS NULL THEN
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'INSTALL_BUILDING'
                             ,p_error_message    => 'At least one of Building or Sub-Building must be completed.');
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'INSTALL_SUB_BUILDING'
                             ,p_error_message    => 'At least one of Building or Sub-Building must be completed.');
    END IF;
  
    -- O-M2
    IF p_rec.enty_code IN ('STD INSTALL', 'STD EXCHANGE', 'CHANGE CAPACITY') THEN
      IF (p_rec.required_mesc_code IS NULL OR
         p_rec.required_mesc_code = 'OTHER') THEN
        IF p_rec.qmax IS NULL THEN
          mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                                 ,p_field_name       => 'QMAX'
                                 ,p_error_message    => 'At least one of Qmax or Meter Size must be completed.');
        END IF;
      END IF;
    END IF;
  
    -- O-M3
    IF p_rec.enty_code IN
       ('INSTALL', 'OFMAT', 'REMOVE', 'ADVERSARIAL', 'ALTERATION',
        'CHANGE CAPACITY', 'ADDON', 'OTHER')
       AND p_rec.required_svcp_code = 'IP'
       AND (p_rec.required_ip_details IS NULL) THEN
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'REQUIRED_SVCP_CODE'
                             ,p_error_message    => 'Required IP Details must be completed when a Service Pressure of ''IP'' is requested.');
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'REQUIRED_IP_DETAILS'
                             ,p_error_message    => 'Required IP Details must be completed when a Service Pressure of ''IP'' is requested.');
    END IF;
  
    -- O-M4
    IF p_rec.enty_code IN ('INSTALL', 'STD INSTALL')
       AND (p_rec.mprn IS NULL AND p_rec.mprn_alt IS NULL) THEN
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'MPRN'
                             ,p_error_message    => 'Either MPRN or Additional Information must be completed.');
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'MPRN_ALT'
                             ,p_error_message    => 'Either MPRN or Additional Information must be completed.');
    END IF;
  
    -- O-M5
    -- Tripartite Agreement and AMR -- this is a specific example of the Tripartite and addons
    -- This is handled through the mip_tripartite.addon functionality
  
    -- O-M6
    IF p_rec.enty_code IN ('OFMAT', 'EXCHANGE', 'STD EXCHANGE', 'REMOVE',
        'STD REMOVE', 'ADVERSARIAL', 'CHANGE CAPACITY', 'ALTERATION')
       AND nvl(p_rec.existing_metr_code, 'OTHER') = 'OTHER'
       AND (p_rec.existing_mesc_code IS NULL OR
       p_rec.existing_mety_code IS NULL) THEN
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'EXISTING_MESC_CODE'
                             ,p_error_message    => 'Either Existing Meter Model OR Existing Meter Size and Meter Type information must be completed.');
      mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                             ,p_field_name       => 'EXISTING_METY_CODE'
                             ,p_error_message    => 'Either Existing Meter Model OR Existing Meter Size and Meter Type information must be completed.');
    END IF;
  
    -- O-M7
    IF p_rec.enty_code IN
       ('INSTALL', 'STD INSTALL', 'EXCHANGE', 'STD EXCHANGE', 'ALTERATION',
        'CHANGE CAPACITY', 'ADDON', 'OTHER') THEN
      SELECT description
        INTO l_bypass_other
        FROM bypass_reasons
       WHERE code = '7';
      IF (p_rec.bypass_required = l_bypass_other AND
         p_rec.bypass_other IS NULL) THEN
        mip_mandatory.add_error(p_mandatory_checks => p_mandatory_checks
                               ,p_field_name       => 'BYPASS_OTHER'
                               ,p_error_message    => '''Other'' reason must be completed.');
      END IF;
    END IF; 
  END table_enquiries;
BEGIN
  -- Initialization
  NULL;
END mip_helper_special_cases;
/
