Thursday, September 8, 2016

API for Expire Instance relationship in Oracle install base

PROCEDURE csi_expire_relationship( p_object_id IN NUMBER)
IS
   l_relationship_rec                           csi_datastructures_pub.ii_relationship_rec;
   l_txn_rec                                    csi_datastructures_pub.transaction_rec;
   x_instance_id_lst                            csi_datastructures_pub.id_tbl;
   l_msg_index_out                              NUMBER;
   l_return_msg                                 VARCHAR2(4000);
   x_return_status                              VARCHAR2(2000);
   x_msg_count                                  NUMBER;
   x_msg_data                                   VARCHAR2(500);

   CURSOR rel_cur
   IS
      SELECT relationship_id
           , object_version_number
        FROM csi_ii_relationships
       WHERE object_id = p_object_id
         AND relationship_type_code = 'COMPONENT-OF'
         AND NVL(active_end_date, SYSDATE + 1) > SYSDATE;
BEGIN
   FOR rel_rec IN rel_cur
   LOOP
    l_relationship_rec.relationship_id := rel_rec.relationship_id;
    l_relationship_rec.object_version_number:= rel_rec.object_version_number;
    l_txn_rec.transaction_id              := NULL;
      l_txn_rec.transaction_date          := SYSDATE;
      l_txn_rec.source_transaction_date   := SYSDATE;
      l_txn_rec.transaction_type_id       := 1;

      fnd_msg_pub.initialize;

      csi_ii_relationships_pub.expire_relationship(
     p_api_version               => 1.0
   , p_commit                    => fnd_api.g_false
   , p_init_msg_list             => fnd_api.g_true
   , p_validation_level          => fnd_api.g_valid_level_full
   , p_relationship_rec          => l_relationship_rec
   , p_txn_rec                   => l_txn_rec
   , x_instance_id_lst           => x_instance_id_lst
   , x_return_status             => x_return_status
   , x_msg_count                 => x_msg_count
   , x_msg_data                  => x_msg_data);

      IF x_return_status = fnd_api.g_ret_sts_success
      THEN
         DBMS_OUTPUT.put_line( 'Expired relationship Successfully ');
         COMMIT;
      ELSE
         IF x_msg_count > 0
         THEN
            FOR i IN 1 .. x_msg_count
            LOOP
               fnd_msg_pub.get(
     p_msg_index                               => i
   , p_encoded                                 => fnd_api.g_false
   , p_data                                    => x_msg_data
   , p_msg_index_out                           => l_msg_index_out);

               IF l_return_msg IS NULL
               THEN
                  l_return_msg     :=
                        l_msg_index_out
                     || ':'
                     || x_msg_data;
               ELSE
                  l_return_msg  :=
                        l_return_msg
                     || '/'
                     || l_msg_index_out
                     || ':'
                     || x_msg_data;
               END IF;
            END LOOP;
         END IF;

         DBMS_OUTPUT.put_line('Expire instance relationship API failure : '                         || NVL(l_return_msg, x_msg_data));
      END IF;
   END LOOP;
END;

API to create Instance relationship in Oracle install Base

PROCEDURE csi_create_relationship(
   p_relationship_tbl IN     csi_datastructures_pub.ii_relationship_tbl
 , x_return_status                                 OUT VARCHAR2
 , x_return_msg                                    OUT VARCHAR2)
IS
   l_relationship_id        NUMBER;
   l_txn_rec                csi_datastructures_pub.transaction_rec;
   l_msg_index_out          NUMBER;
   x_msg_count              NUMBER;
   x_msg_data               VARCHAR2(500);
BEGIN
   fnd_msg_pub.initialize;
   l_relationship_id             := csi_ii_relationships_s.NEXTVAL;
   p_relationship_tbl( 1).relationship_id := l_relationship_id;
   p_relationship_tbl( 1).relationship_type_code := 'COMPONENT-OF';
   p_relationship_tbl( 1).object_id              := 4959909;
   p_relationship_tbl( 1).subject_id             := 4959910;
   p_relationship_tbl( 1).subject_has_child      := 'N';
   p_relationship_tbl( 1).position_reference     := NULL;
   p_relationship_tbl( 1).active_start_date      := SYSDATE;
   p_relationship_tbl( 1).active_end_date        := NULL;
   p_relationship_tbl( 1).display_order          := NULL;
   p_relationship_tbl( 1).mandatory_flag         := 'N';
   p_relationship_tbl( 1).context                := NULL;
   p_relationship_tbl( 1).attribute1             := NULL;
   p_relationship_tbl( 1).attribute2             := NULL;
   p_relationship_tbl( 1).attribute3             := NULL;
   p_relationship_tbl( 1).attribute4             := NULL;
   p_relationship_tbl( 1).attribute5             := NULL;
   p_relationship_tbl( 1).attribute6             := NULL;
   p_relationship_tbl( 1).attribute7             := NULL;
   p_relationship_tbl( 1).attribute8             := NULL;
   p_relationship_tbl( 1).attribute9             := NULL;
   p_relationship_tbl( 1).attribute10            := NULL;
   p_relationship_tbl( 1).attribute11            := NULL;
   p_relationship_tbl( 1).attribute12            := NULL;
   p_relationship_tbl( 1).attribute13            := NULL;
   p_relationship_tbl( 1).attribute14            := NULL;
   p_relationship_tbl( 1).attribute15            := NULL;
   p_relationship_tbl( 1).object_version_number  := 1;

   -- Setting the transaction record type
   l_txn_rec.transaction_date            := TRUNC( SYSDATE);
   l_txn_rec.source_transaction_date     := TRUNC( SYSDATE);
   l_txn_rec.transaction_type_id         := 1;
   l_txn_rec.object_version_number       := 1;
   --
   csi_ii_relationships_pub.create_relationship(
      p_api_version              => 1.0
    , p_commit                   => fnd_api.g_false
    , p_init_msg_list            => fnd_api.g_true
    , p_validation_level         => fnd_api.g_valid_level_full
    , p_relationship_tbl         => p_relationship_tbl
    , p_txn_rec                                 => l_txn_rec
    , x_return_status                           => x_return_status
    , x_msg_count                               => x_msg_count
    , x_msg_data                                => x_msg_data);

   IF x_return_status = fnd_api.g_ret_sts_success
   THEN
      DBMS_OUTPUT.put_line( 'Relationship created successfully ');
      COMMIT;
   ELSE
      IF x_msg_count > 0
      THEN
         FOR i IN 1 .. x_msg_count
         LOOP
            fnd_msg_pub.get(
            p_msg_index         => i
          , p_encoded           => fnd_api.g_false
          , p_data              => x_msg_data
          , p_msg_index_out     => l_msg_index_out);

            IF x_return_msg IS NULL
            THEN
               x_return_msg     :=
                     l_msg_index_out
                  || ':'
                  || x_msg_data;
            ELSE
               x_return_msg     :=
                     x_return_msg
                  || '/'
                  || l_msg_index_out
                  || ':'
                  || x_msg_data;
            END IF;
         END LOOP;
      END IF;

      DBMS_OUTPUT.put_line(   'Relationship creation failure : '
                           || x_return_msg);
   END IF;
END;

API to Expire Instance in Oracle Install Base

DECLARE
   l_record_status                              VARCHAR2(1);
   l_sv_status                                  VARCHAR2(2000);
   l_msg_data                                   VARCHAR2(2000);
   l_mesg                                       VARCHAR2(4000);
   l_mesg_len                                   NUMBER;
   l_mesg_count                                 NUMBER;


   SUBTYPE instance_rec IS csi_datastructures_pub.instance_rec;

   SUBTYPE transaction_rec IS csi_datastructures_pub.transaction_rec;

   SUBTYPE id_tbl IS csi_datastructures_pub.id_tbl;

   SUBTYPE instance_query_rec IS csi_datastructures_pub.instance_query_rec;

   SUBTYPE party_query_rec IS csi_datastructures_pub.party_query_rec;

   SUBTYPE party_account_query_rec IS csi_datastructures_pub.party_account_query_rec;

   SUBTYPE instance_header_tbl IS csi_datastructures_pub.instance_header_tbl;

   -- Get Item Instance parameters
   l_instance_query_rec                         instance_query_rec;
   l_party_query_rec                            party_query_rec;
   l_account_query_rec                          party_account_query_rec;
   l_instance_header_tbl                        instance_header_tbl;

   -- Expire Item Instance parameters
   l_instance_rec                               instance_rec;
   l_txn_rec                                    transaction_rec;
   l_instance_id_lst                            id_tbl;

   l_return_status       VARCHAR2(1) := okl_api.g_ret_sts_success;
   l_overall_status      VARCHAR2(1) := okl_api.g_ret_sts_success;

   l_api_name            CONSTANT VARCHAR2(30) := 'expire_item';
   l_api_version         CONSTANT NUMBER := 1;
   l_msg_count           NUMBER := fnd_api.g_miss_num;
BEGIN
   l_instance_query_rec.instance_id  := 5321932;

   csi_item_instance_pub.get_item_instances(
   p_api_version          => l_api_version
 , p_commit               => fnd_api.g_false
 , p_init_msg_list        => fnd_api.g_false
 , p_validation_level     => fnd_api.g_valid_level_full
 , p_instance_query_rec   => l_instance_query_rec
 , p_party_query_rec      => l_party_query_rec
 , p_account_query_rec    => l_account_query_rec
 , p_transaction_id       => NULL
 , p_resolve_id_columns   => fnd_api.g_false
 , p_active_instance_only => fnd_api.g_true
 , x_instance_header_tbl  => l_instance_header_tbl
 , x_return_status        => l_return_status
 , x_msg_count            => l_msg_count
 , x_msg_data             => l_msg_data);


l_instance_rec.instance_id:= l_instance_header_tbl( 1).instance_id;
l_instance_rec.object_version_number:=l_instance_header_tbl( 1).object_version_number;
   l_instance_rec.active_end_date   := SYSDATE;

   l_txn_rec.transaction_date         := SYSDATE;
   l_txn_rec.source_transaction_date  := SYSDATE;
   l_txn_rec.transaction_id           := NULL;
   l_txn_rec.transaction_type_id      := 1;

   -- **************************************
   -- Call Installed Base API to expire item
   -- **************************************

   csi_item_instance_pub.expire_item_instance(
   p_api_version                             => l_api_version
 , p_commit                                  => fnd_api.g_false
 , p_init_msg_list                           => fnd_api.g_false
 , p_validation_level                        => fnd_api.g_valid_level_full
 , p_instance_rec                            => l_instance_rec
 , p_expire_children                         => fnd_api.g_true
 , p_txn_rec                                 => l_txn_rec
 , x_instance_id_lst                         => l_instance_id_lst
 , x_return_status                           => l_return_status
 , x_msg_count                               => l_msg_count
 , x_msg_data                                => l_msg_data);

   IF l_return_status = 'S'
   THEN
      DBMS_OUTPUT.put_line(   'instance '
                           || l_instance_rec.instance_id
                           || ' expired successfully');
   -- **************************************
   -- Display errors encounted for the expiration
   -- **************************************
   ELSE
      l_mesg_count          := fnd_msg_pub.count_msg;

      IF l_mesg_count > 0
      THEN
         l_mesg             :=
               CHR( 10)
            || SUBSTR(fnd_msg_pub.get(fnd_msg_pub.g_first
                                    , fnd_api.g_false)
                    , 1
                    , 512);

         FOR i IN 1 .. (l_mesg_count - 1)
         LOOP
            l_mesg      :=
                  l_mesg
               || CHR( 10)
               || SUBSTR(fnd_msg_pub.get(fnd_msg_pub.g_next
                                       , fnd_api.g_false)
                       , 1
                       , 512);
         END LOOP;

         fnd_msg_pub.delete_msg();

         l_mesg_len           := LENGTH( l_mesg);

         FOR i IN 1 .. CEIL( l_mesg_len / 255)
         LOOP
            DBMS_OUTPUT.put_line(SUBSTR(l_mesg
                                      , ((i * 255) - 254)
                                      , 255));
         END LOOP;
      END IF;
   END IF;
END;
/

Sunday, September 4, 2016

API to swap Service contract for Newly created Install Base with Old install base

DECLARE
   p_transaction_type            VARCHAR2(50);
   p_instance_id                 NUMBER;
   p_new_instance_id             NUMBER;
   p_vld_org_id                  NUMBER;
   p_quantity                    NUMBER;
   p_party_account_id1           NUMBER;
   p_party_account_id2           NUMBER;
   p_transaction_date            DATE;
   p_source_transaction_date     DATE;
   p_transaction_id              NUMBER;
   p_grp_call_contracts          VARCHAR2(1);
   p_txn_type_id                 NUMBER;
   p_system_id                   NUMBER;
   p_order_line_id               NUMBER;
   p_call_from_bom_expl          VARCHAR2(1);
   lp_oks_txn_inst_tbl           oks_ibint_pub.txn_instance_tbl;
   lx_return_status              VARCHAR2(1);
   lx_msg_count                  NUMBER;
   lx_msg_data                   VARCHAR2(500);
   l_msg_index_out               NUMBER;
   l_return_msg                  VARCHAR2(32600);
   k_rpl_tbl                     oks_extwarprgm_pvt.contract_tbl;

   CURSOR get_k_for_rpl_csr
   IS
        SELECT tmp.old_customer_product_id instance_id
             , tmp.termination_date
             , tmp.installation_date
             , tmp.transaction_date
             , tmp.old_customer_acct_id
             , tmp.new_customer_acct_id
             , tmp.system_id
             , tmp.old_quantity
             , tmp.new_quantity
             , tmp.new_customer_product_id
             , ki.cle_id subline_id
             , ki.dnz_chr_id
             , kh.start_date hdr_sdt
             , kh.end_date hdr_edt
             , kh.sts_code hdr_sts
             , kl.cle_id
             , kl.price_negotiated
             , kl.start_date
             , kl.end_date
             , kl.sts_code prod_sts
             , kl.cust_acct_id
             , tl.start_date srv_sdt
             , tl.end_date srv_edt
             , kh.sts_code
             , kh.contract_number
             , ki.number_of_items
             , tl.price_negotiated
             , kl.date_terminated
             , tmp.old_inventory_item_id
             , kh.authoring_org_id
             , kh.inv_organization_id
             , kl.lse_id
             , kh.scs_code
             , tmp.new_customer_product_id
             , kis.object1_id1
             , tl.currency_code
             , tmp.old_unit_of_measure
             , kl.line_renewal_type_code
             , tmp.raise_credit
             , NULL
             , okl.tax_amount
             , kl.price_unit
             , kl.name
             , kl.item_description
             , kl.upg_orig_system_ref
             , kl.upg_orig_system_ref_id
             , tmp.new_inventory_item_id
             , tmp.return_reason_code
             , tmp.order_line_id
             , okl.price_uom
             , okl.toplvl_uom_code
             , okl.toplvl_price_qty
          FROM okc_k_items ki
             , okc_k_headers_all_b kh
             , okc_k_lines_v kl
             , okc_statuses_b st
             , oks_instance_temp tmp
             , okc_k_lines_b tl
             , okc_k_items kis
             , oks_k_lines_b okl
         WHERE tmp.rpl = 'Y'
           AND ki.object1_id1 = TO_CHAR( tmp.old_customer_product_id)
           AND ki.jtot_object1_code = 'OKX_CUSTPROD'
           AND ki.dnz_chr_id = kh.id
           AND kh.scs_code IN ('WARRANTY', 'SERVICE', 'SUBSCRIPTION')
           AND ki.cle_id = kl.id
           AND tl.id = kl.cle_id
           AND kis.cle_id = tl.id
           AND kis.dnz_chr_id = tl.dnz_chr_id
           AND kl.sts_code = st.code
           AND st.ste_code NOT IN ('TERMINATED', 'CANCELLED', 'HOLD')
           AND kl.date_terminated IS NULL
           AND okl.cle_id = kl.id
           AND kh.template_yn = 'N'
           AND ((TRUNC( tmp.transaction_date) <= TRUNC( kl.end_date)
             AND TRUNC( tmp.transaction_date) >= TRUNC( kl.start_date))
             OR (TRUNC( tmp.transaction_date) <= TRUNC( kl.start_date)))
      ORDER BY tmp.old_customer_product_id
             , kh.creation_date;
BEGIN
   --   lp_oks_txn_inst_tbl( 1).old_customer_product_id          := 4367640;
   apps.csi_item_instance_pvt.call_to_contracts(
  p_transaction_type                        => 'RPL'
, p_instance_id                             => 4367640
, p_new_instance_id                         => 5302910
, p_vld_org_id                              => 103
, p_quantity                                => NULL
, p_party_account_id1                       => NULL
, p_party_account_id2                       => NULL
, p_transaction_date                        => SYSDATE
, p_source_transaction_date                 => SYSDATE
, p_grp_call_contracts                      => fnd_api.g_false
, p_oks_txn_inst_tbl                        => lp_oks_txn_inst_tbl
, x_return_status                           => lx_return_status
, x_msg_count                               => lx_msg_count
, x_msg_data                                => lx_msg_data);

   IF lx_return_status = fnd_api.g_ret_sts_success
   THEN
      FORALL i IN lp_oks_txn_inst_tbl.FIRST .. lp_oks_txn_inst_tbl.LAST
         INSERT INTO oks_instance_temp
              VALUES lp_oks_txn_inst_tbl( i);

      fnd_global.apps_initialize(12247
                               , 50305
                               , 515);

      OPEN get_k_for_rpl_csr;

      FETCH get_k_for_rpl_csr BULK COLLECT INTO k_rpl_tbl;

      CLOSE get_k_for_rpl_csr;

      apps.oks_extwarprgm_pvt.update_contract_ibreplace(
           k_rpl_tbl
         , lx_return_status
         , lx_msg_count
         , lx_msg_data);

      IF lx_return_status = fnd_api.g_ret_sts_success
      THEN
         DBMS_OUTPUT.put_line( 'Swapped syccessfully.');
         COMMIT;
      ELSE
         IF lx_msg_count > 0
         THEN
            FOR i IN 1 .. lx_msg_count
            LOOP
               fnd_msg_pub.get(p_msg_index    => i
                             , p_encoded      => fnd_api.g_false
                             , p_data         => lx_msg_data
                             , p_msg_index_out=> l_msg_index_out);

               IF l_return_msg IS NULL
               THEN
                  l_return_msg                :=
                        l_msg_index_out
                     || ':'
                     || lx_msg_data;
               ELSE
                  l_return_msg                :=
                        l_return_msg
                     || '/'
                     || l_msg_index_out
                     || ':'
                     || lx_msg_data;
               END IF;
            END LOOP;
         END IF;

         DBMS_OUTPUT.put_line(   'Swaping of Service contract for IB failed : '
                              || NVL(lx_msg_data, l_return_msg));
      END IF;
   ELSE
      IF lx_msg_count > 0
      THEN
         FOR i IN 1 .. lx_msg_count
         LOOP
            fnd_msg_pub.get(p_msg_index    => i
                          , p_encoded      => fnd_api.g_false
                          , p_data         => lx_msg_data
                          , p_msg_index_out=> l_msg_index_out);

            IF l_return_msg IS NULL
            THEN
               l_return_msg                :=
                     l_msg_index_out
                  || ':'
                  || lx_msg_data;
            ELSE
               l_return_msg                :=
                     l_return_msg
                  || '/'
                  || l_msg_index_out
                  || ':'
                  || lx_msg_data;
            END IF;
         END LOOP;
      END IF;

      DBMS_OUTPUT.put_line(   'call to contract failure : '
                           || NVL(lx_msg_data, l_return_msg));
   END IF;
END;