Cancel AP Invoices Script

declare
   lv_boolean      boolean;
   lv_error_code   varchar2 (100);
   lv_debug_info   varchar2 (1000);
   lv_user_id      number;
 
v_boolean               boolean;
v_message_name          varchar2(1000);
v_invoice_amount        number;
v_base_amount           number;
v_temp_cancelled_amount number;
v_cancelled_by          varchar2(1000);
v_cancelled_amount      number;
v_cancelled_date        date;
v_last_update_date      date;
v_orig_prepay_amt       number;
v_pay_cur_inv_amt       number;
v_token                 varchar2(100);

   cursor c1
   is
      select invoice_id
        from ap_invoices_all
       where payment_status_flag <> 'Y';

begin
           lv_user_id := fnd_global.user_id;  --User ID should not anonymous


           fnd_global.apps_initialize (lv_user_id, 20639, 200);
           mo_global.set_policy_context ('S', 101);
           mo_global.init ('SQLAP');

      for i in c1
        loop
       
             begin
               lv_boolean :=
                  ap_cancel_pkg.is_invoice_cancellable (p_invoice_id         => i.invoice_id,
                                                        p_error_code         => lv_error_code,
                                                        p_debug_info         => lv_debug_info,
                                                        p_calling_sequence   => null);
                                                       
         

               if lv_boolean = true
               then
                  dbms_output.put_line ('Invoice ' || i.invoice_id || ' is cancellable'||lv_error_code);
                 
                   v_boolean :=
                           ap_cancel_pkg.ap_cancel_single_invoice(
                                               p_invoice_id                 => i.invoice_id,
                                               p_last_updated_by            => lv_user_id,
                                               p_last_update_login          => lv_user_id,
                                               p_accounting_date            =>sysdate,
                                               p_message_name               => v_message_name,
                                               p_invoice_amount             => v_invoice_amount,
                                               p_base_amount                => v_base_amount,
                                               p_temp_cancelled_amount      => v_temp_cancelled_amount,
                                               p_cancelled_by               => v_cancelled_by,
                                               p_cancelled_amount           => v_cancelled_amount,
                                               p_cancelled_date             => v_cancelled_date,
                                               p_last_update_date           => v_last_update_date,
                                               p_original_prepayment_amount => v_orig_prepay_amt,
                                               p_pay_curr_invoice_amount    => v_pay_cur_inv_amt,
                                               p_token                      => v_token,
                                               p_calling_sequence           => null
                                                 );
                                               
                                               
                                             if v_boolean then
                                                dbms_output.put_line ('Successfully Cancelled the Invoice' );
                                                commit;
                                             else
                                                dbms_output.put_line ('Failed to Cancel the Invoice' );
                                                rollback;
                                             end if;

                                                 dbms_output.put_line ('Invoice message_name'|| v_message_name);
                                           
               else
                  dbms_output.put_line ('Invoice '|| i.invoice_id|| ' is not cancellable due to :'|| lv_error_code);
               end if;
             
               exception
               when others
               then
                  dbms_output.put_line ('Invoice ' || i.invoice_id || ' is not cancellable :' || sqlerrm);
             
             end;
         
        end loop;
        commit;
exception
   when others
   then
      dbms_output.put_line ('Invoice is not cancellable :' || sqlerrm);
end;