Wednesday, June 8, 2011

Inventory Period Close for All Organization through API


PROCEDURE inventory_close_api (
   errbuf          OUT      VARCHAR2,
   retcode         OUT      NUMBER,
   p_company       IN       VARCHAR2,
   p_fiscal_year   IN       NUMBER,
   p_period_num    IN       NUMBER
)
AS
   CURSOR ls_period_close (
      lc_company       VARCHAR2,
      ln_fiscal_year   NUMBER,
      ln_period_num    NUMBER
   )
   IS
      SELECT b.organization_code, b.organization_name, a.*
        FROM org_acct_periods a, org_organization_definitions b
       WHERE a.period_set_name = lc_company
         AND a.period_year = ln_fiscal_year
         AND a.period_num = ln_period_num
         AND a.open_flag = 'Y'
         AND a.organization_id = b.organization_id;

   lc_return_status    VARCHAR2 (1);
   lc_msg_data         VARCHAR2 (2000);
   lc_errbuf           VARCHAR2 (2000);
   ln_msg_count        NUMBER;
   p_closing_rowid     VARCHAR2 (100);
   x_wip_failed        BOOLEAN;
   x_close_failed      BOOLEAN;
   x_download_failed   BOOLEAN;
   x_req_id            NUMBER;
   x_return_status     VARCHAR2 (100);
   ln_user_id          NUMBER;
   ln_org_id           NUMBER;
   ln_resp_id          NUMBER;
   ln_resp_app_id      NUMBER;
BEGIN
   fnd_file.put_line (fnd_file.LOG,
                         '1st paramaeter - '
                      || p_company
                      || ' - 2nd Parameter - '
                      || p_fiscal_year
                      || ' - 3rd Parameter - '
                      || p_period_num
                     );
   fnd_file.put_line (fnd_file.output,
                         'WHSE'
                      || '       '
                      || 'PERIOD NUM'
                      || '      '
                      || 'PERIOD YR'
                     );
   ln_user_id := 1738;                  --:=APPS.FND_PROFILE.VALUE('USER_ID');
   ln_resp_id := apps.fnd_profile.VALUE ('RESP_ID');
   ln_resp_app_id := apps.fnd_profile.VALUE ('RESP_APPL_ID');
   ln_org_id := apps.fnd_profile.VALUE ('ORG_ID');

   BEGIN
      fnd_client_info.set_org_context (ln_org_id);
   END;

   BEGIN
      fnd_global.apps_initialize (1738, ln_resp_id, ln_resp_app_id);
   END;

   FOR ln_period_close IN ls_period_close (p_company,
                                           p_fiscal_year,
                                           p_period_num
                                          )
   LOOP
      EXIT WHEN ls_period_close%NOTFOUND;
      cst_accountingperiod_pub.close_period
               (p_api_version                 => 1.0,
                p_org_id                      => ln_period_close.organization_id,
                p_user_id                     => ln_period_close.created_by,
                p_login_id                    => ln_period_close.last_update_login,
                p_closing_acct_period_id      => ln_period_close.acct_period_id,
                p_period_close_date           => SYSDATE,
                p_schedule_close_date         => ln_period_close.schedule_close_date,
                p_closing_rowid               => p_closing_rowid,
                x_wip_failed                  => x_wip_failed,
                x_close_failed                => x_close_failed,
                x_download_failed             => x_download_failed,
                x_req_id                      => x_req_id,
                x_return_status               => x_return_status
               );
      fnd_file.put_line (fnd_file.output,
                            ln_period_close.organization_code
                         || '               '
                         || ln_period_close.period_num
                         || '       '
                         || ln_period_close.period_year
                        );

      IF lc_return_status <> 'S'
      THEN
         fnd_file.put_line (fnd_file.LOG, 'STATUS : ' || lc_return_status);
         COMMIT;

         IF ln_msg_count <= 1
         THEN
            lc_errbuf := lc_errbuf || lc_msg_data;
         ELSE
            FOR ln_i IN 1 .. ln_msg_count
            LOOP
               lc_errbuf := lc_errbuf || ' ' || fnd_msg_pub.get (ln_i, 'F');
               fnd_file.put_line (fnd_file.LOG, 'STATUS : ' || lc_errbuf);
            END LOOP;
         END IF;
      END IF;

      IF     ln_period_close.period_close_date IS NOT NULL
         AND ln_period_close.open_flag = 'Y'
      THEN
         fnd_file.put_line
                          (fnd_file.LOG,
                              'Inventory period still open for - '
                           || ln_period_close.organization_code
                           || 'as there are pending transactions for the month.'
                          );
      ELSE
         fnd_file.put_line (fnd_file.LOG,
                               'Inventory period closed for organization - '
                            || ln_period_close.organization_code
                           );
      END IF;
   END LOOP;

   COMMIT;
EXCEPTION
   WHEN OTHERS
   THEN
      fnd_file.put_line (fnd_file.LOG,
                         'Exception - Inventory Close API' || SQLERRM
                        );
END;

No comments:

Post a Comment