Generic

1. Implementation Types
  1. Fresh Implementation
  2. Rollout Projects – Country specific
  3. Upgradation Projects – Version specific – 12.1.1 to 12.1.3
  4. Support Projects
  5. Enhancement Project – Adding Modules or Modifies the functionality/ capability of existing design
2. Difference between Org ID, Organization ID and Operating Unit

Operating Unit – Location – India/US; Each operating unit will have an ID known as Org ID In India we have three inventories – AP, Karnataka, Orissa.
Each inventory organization will have an Organization ID

3. Concurrent program error (OPP Error)

If the program errors then pick the request id >> System Administrator >> Concurrent >> Manager >> Administer >> Click on Output Post Processor >> Click on Processes button >> Status Active: Click on Manager Log and search with request ID

4. LDT Commands

Login to WinScp and create folder where the files need to be downloaded. Next login to Putty and set environment path and the go the folder which was created above. Login to Putty: abc.xyz.net Enter Username and Password cd /u01/app/instancename/apps/apps_st/appl/ . ./APPSinstancename_servername.env cd /u01/app/instancename/apps/apps_st/appl/xxcustomtop/12.0.0/bin <FNDLOAD apps/pswd O Y DOWNLOAD $FND_TOP/patch/115/import/afscursp.lct file_name.ldt FND_RESPONSIBILITY RESP_KEY=”resp_short_name”> $XXCUSTOM_TOP/bin/xxab_deploy.sh give pswd enter deployment link — /u01/app/instancename/apps/apps_st/appl/xxcustomtop/12.0.0/bin lct = ‘loader control file’ ldt = ‘loader data file’

#Use below syntax in .sh file (XXAB_LDT.sh)

APPS_PASSWORD=$1

DB_INSTANCE=$2

$FND_TOP/bin/FNDLOAD apps/$APPS_PASSWORD@$DB_INSTANCE 0 Y UPLOAD $FND_TOP/patch/115/import/afscursp.lct XX_DOWNLOAD_FILE_NAME_CP.ldt - WARNING=YES UPLOAD_MODE=REPLACE CUSTOM_MODE=FORCE

Download & upload Concurrent Program: APPLICATION_SHORT_NAME=”PER” is of ‘Human Resources’ application

FNDLOAD apps/password O Y DOWNLOAD $FND_TOP/patch/115/import/afcpprog.lct XX_DOWNLOAD_FILE_NAME_CP.ldt PROGRAM APPLICATION_SHORT_NAME="PER" CONCURRENT_PROGRAM_NAME="CONCURRENT_PROGRAM_SHORT_NAME"

FNDLOAD apps/password 0 Y UPLOAD $FND_TOP/patch/115/import/afcpprog.lct XX_DOWNLOAD_FILE_NAME_CP.ldt - WARNING=YES UPLOAD_MODE=REPLACE CUSTOM_MODE=FORCE

Download & upload personalized form

FNDLOAD apps/<GIVE_PSWD_HERE> 0 Y DOWNLOAD $FND_TOP/patch/115/import/affrmcus.lct <GIVE_FILE_NAME_HERE>.ldt FND_FORM_CUSTOM_RULES function_name=<Give_Function_Name_Here>

FNDLOAD apps/<GIVE_PSWD_HERE> 0 Y UPLOAD $FND_TOP/patch/115/import/affrmcus.lct <Path_Name/Give_Download_File_Name_Here>.ldt
5. Loading shortcut file in Toad

First create a text file (.txt) with required syntaxes like the few given below: paaf=select * from hr.per_all_assignments_f papf=select * from hr.per_all_people_f hca=select * from apps.hz_cust_accounts_all where cust_account_id = hp=select * from apps.hz_parties hl=select * from apps.hz_locations Next navigation in Toad: View >> Toad Options >> Editor >> Behavior >> Auto Replace >> Import >> Ok

6. DUNS Number

Dun & Bradstreet provides a D-U-N-S number, a unique nine digit identification number, for each physical location of your business. BIR – Business Information Report

7. Types of GL Periods

Navigation: General Ledger Super User >> Setup >> Open/ Close >> Enter Ledger name like ‘Vision Operations (USA)’ >> Click on Find >> Select the period which you want to open and click Open Period button >> Enter Target Period which you want to open >> Click on Open >> Yes >> Ok >> Ensure the ‘Open Period’ program completed successfully >> Now you will find your period in open status. Also we can search in Receivables responsibility >> Control >> Accounting >> Open/ Close periods >> On status field open the period which you want to.

8. Testing Types

Order By – Smoke Testing >> Sanity Testing >> Regression Testing >> Unit Testing Smoke Testing – Functional testing of application Sanity Testing – S/W testing of application Regression Testing – Testing by functional team Unit Testing – Code testing by developer

9. Autonomous Transaction

9. Autonomous Transaction

10. To extend the session connectivity

10. To extend the session connectivity

11. Explain Plan

11. Explain Plan

12. Editing values in Toad

12. Editing values in Toad

13. Alter session in Toad

13. Alter session in Toad

14. Converting an excel/csv file into cap format or ending with ‘,’

14. Converting an excel/csv file into cap format or ending with ‘,’

15. OATS – Oracle Application Testing Suite

15. OATS – Oracle Application Testing Suite

16. PTD & YTD

16. PTD & YTD

17. Bank Information

17. Bank Information

18. Concurrent Program output in MS Excel format

18. Concurrent Program output in MS Excel format

19. WHO Columns

19. WHO Columns

20. FND_GLOBAL.APPS_INITIALIZE

20. FND_GLOBAL.APPS_INITIALIZE

21. ERRBUF & RETCODE

21. ERRBUF & RETCODE

22. Concurrent Program Phase and Status codes

22. Concurrent Program Phase and Status codes

23. CEMLI

23. CEMLI

24. Qualifier

24. Qualifier

25. Kill Session

25. Kill Session

26. Query to identify locked objects

26. Query to identify locked objects

27. Concurrent Program

27. Concurrent Program

28. Setting up the Priority of Concurrent Program

28. Setting up the Priority of Concurrent Program

29. Execute Procedure in Toad

29. Execute Procedure in Toad

30. Modules

30. Modules

31. Basic profile options to run any Responsibility

31. Basic profile options to run any Responsibility

32. Custom Top

32. Custom Top

33. Clear Cache

33. Clear Cache

34. To compare values in excel between two columns

34. To compare values in excel between two columns

35. Document types in Oracle EBS

35. Document types in Oracle EBS

36. FTP via Toad

36. FTP via Toad

37. Difference between APPS Schema and other Schemas

37. Difference between APPS Schema and other Schemas

38. Extended SQL and XSL Functions

38. Extended SQL and XSL Functions

39. Constraints

40. Synonym

40. Synonym

41. Synchronizing responsibilities

41. Synchronizing responsibilities

42. Adding a Function to Responsibility

42. Adding a Function to Responsibility

43. Value Sets

43. Value Sets

44. Pseudo Columns

44. Pseudo Columns

45. Set Operators

45. Set Operators

46. Difference Between 11i and R12

46. Difference Between 11i and R12

47. Instance access issue

47. Instance access issue

48. Request Set

Collection of multiple concurrent programs. We have option to select processing of programs either Sequentially (one after another) or parallel (all at once).

49. To run View – set below command
DECLARE
	lv_org_id VARCHAR2(10);
	ln_rowcount NUMBER;
	
	CURSOR c1 IS
	SELECT organization_id FROM hr_all_organization_units;
BEGIN
	OPEN c1;
	LOOP
		ln_rowcount := c1%rowcount;
		FETCH c1
		INTO lv_org_id;
		EXIT WHEN c1%notfound;
		--mo_global.set_policy_context('S',lv_org_id); --first check this and go with second
		--fnd_client_info.set_org_context(lv_org_id);
	END LOOP;
	dbms_output.put_line('Total records processed ' || ln_rowcount);
	CLOSE c1;
	COMMIT;
END;

Also change the language of DB. SQL Developer >> Tools >> Preferences >> Database >> NLS >> Language: AMERICAN or ALTER SESSION SET NLS_LANGUAGE = AMERICAN;

50. SDLC Flow

Requirement analysis and gathering >> to be designed (HLD and DLD) >> Configuration >> Development >> Unit Testing >> SIT >> UAT >> Cut Over

51. To get DFF values stored in which table n which column

Go to particular line which has DFF >> Help >> Diagnostics >> Examine

System Administrator >> Application >> Flexfiled >> Descriptive >> Enter Title as the value which is there above

Click on Global Data Elements >> Segments button

52. Enable DFF

Fetch the name of DFF >> Help Menu >> Diagnostics >> Examine >> Block Name = $DESCRIPTIVE_FLEXFIELD$ >> Field = XXX.DF >> Check value To enable this: Application Developer >> Flexfield >> Descriptive >> Segments >> F11 >> Enter Title with the value which we have above (exclude the value which is in ()) >> CTL + F11 >> Include in code >> Enter Segment values and attributes.

53. Concurrent Requests Inactive

System Administrator > Control L > Administer Concurrent Managers > Under name query (F11) for Standard Manager and see the pending requests.

54. Defining global parameters
gn_request_id NUMBER := fnd_global.conc_request_id;
gn_user_id NUMBER := fnd_global.user_id;
gn_login_id NUMBER := fnd_global.login_id;
gn_org_id NUMBER := fnd_global.org_id;
gn_created_by VARCHAR2(50) := fnd_profile.VALUE ('USER_ID');
gn_last_updated_by VARCHAR2(50) := fnd_profile.VALUE ('USER_ID');
55. FND_MESSAGES

Application Developer >> Application >> Messages >> F11 n query for relevant name

SELECT MESSAGE_TEXT FROM fnd_new_messages WHERE meaages_name = '' AND language_code = 'US';
56. Displaying dynamic value in FND Messages

The values which need to be displayed dynamically give &text in message text. The below function can be used to get dynamic text in message text. Here we have to get three dynamic texts. Say the text is like — This is a test messages to test &ln_amount with currency &lv_currency_code and possessing &ln_tax in fnd_messages.

FUNCTION fnd_messages_function (in_n_customer_trx_id IN NUMBER) 
 RETURN VARCHAR2 IS
	ln_tax VARCHAR2(200) := NULL;
	ln_amount VARCHAR2(200) := NULL;
	lv_currency_code VARCHAR2(200) := NULL;
	lv_sql_err VARCHAR2(200) := NULL;
	lv_org_name VARCHAR2(200) := NULL;
	lv_output VARCHAR2(2000) := NULL;
 BEGIN
	BEGIN
		ln_tax := 'write another function to get this value';
		ln_amount := 'write another function to get this value';
		lv_currency_code := 'write another function to get this value';
		lv_org_name := 'write another function to get this value';
		IF
			lv_org_name = 'Org Name'
		THEN
			fnd_message.clear;
			fnd_message.set_name('AR (give application name)','message_name');
			fnd_message.set_token('AMOUNT',ln_amount);
			fnd_message.set_token('CURRENCY',lv_currency_code);
			fnd_message.set_token('TAX',ln_tax);
			lv_output := fnd_message.get;
		END IF;
		
		return(lv_output);
	EXCEPTION
		WHEN OTHERS THEN
		lv_output := NULL;
		lv_sql_err := sqlerrm;
	END;
END;
57. NLS Language

NLS_LANGUAGE specifies the default language of the database. This language is used for messages, day and month names, symbols for AD, BC, a.m. and p.m. and the default sorting mechanism. This parameter also determines the default values of the parameters NLS_DATE_LANGUAGE and NLS_SORT.

ALTER SESSION SET NLS_LANGUAGE= 'AMERICAN' NLS_TERRITORY= 'AMERICA' NLS_CURRENCY= '$' NLS_ISO_CURRENCY= 'AMERICA' NLS_NUMERIC_CHARACTERS= '.,' NLS_CALENDAR= 'GREGORIAN' NLS_DATE_FORMAT= 'DD-MON-RR' NLS_DATE_LANGUAGE= 'AMERICAN' NLS_SORT= 'BINARY'

SELECT * FROM FND_LANGUAGES;

SELECT DECODE (parameter,
'NLS_CHARACTERSET', 'CHARACTER SET',
'NLS_LANGUAGE', 'LANGUAGE',
'NLS_TERRITORY', 'TERRITORY')
name,
VALUE
FROM v$nls_parameters
WHERE parameter IN ('NLS_CHARACTERSET', 'NLS_LANGUAGE', 'NLS_TERRITORY');
58. Mapping Rule

The rules that change the legacy system values into valid Oracle apps values is called Mapping Rule.

59. Optimizer

Oracle Optimizer has two modes of operation. 1. Cost based (CHOOSE) 2. Rule based (RULE)

60. Hints

Hints instructs the database engine on execution of SQL query (how to execute SQL query). Hint may instructs database engine to use or not to use Index.

61. Restore sql developer file

C:\Users%USERNAME%\AppData\Roaming\SQL Developer\SqlHistory

62. Query taking long time for first run and less time for second run

In case Query is taking long time for first run and less time during second run, Caching is main reason. In the second run instead of physical disc check it returns from Cache. To make the query run consistently during second run also, use below syntax. ALTER SYSTEM FLUSH BUFFER_CACHE; COMMIT;

63. Save output data from SQL Developer in excel

Right click in data >> Click on Export >> Change the format to csv >> Enter file name (Missing/ Incorrect path name leads to an error) >> Browse your location to download the file >> Next >> Finish

64. SQL Developer Installation

Install SQL Developer + Oracle Client 10g or 11g. Once Client 10g/ 11g is installed then we will be able to save TNS file in D:\app<name>\product\11.1.0\client_1\ etwork\admin folder

65. Agile and Scrum

Agile = 4 values and 12 principles
Scrum = Framework in Agile
Other frameworks in Agile are XP, Crystal, DSDM, FDD
Scrum = Iteration duration is upto 1 calendar month XP = Iteration duration is upto 1 to 3 weeks DSDM = Iteration duration is upto 2 to 4 weeks In Scrum, 3 Roles, 5 Events, 3 Artifacts 3 Roles: Product Owner, Scrum Master, Development Team…all together we call as Scrum Team. The maximum size of Scrum team is 11 members. PO and Scrum master + 3 to 9 developers. There is no Manager in Scrum team. There are only three roles. The management activities are distributed among them. 5 Events: Event1: Sprint Event2: Sprint planning. Event3: Daily scrum Event4: Sprint Review Event5: Sprint Retrospective Backlog Refinement: Preparing for upcoming sprints. 3 Artifcats: 1. Product Backlog — Product backlog is never frozen (base-lined). Business can keep adding and refining the backlog. 2. Sprint Backlog — This is subset of product backlog and the teams plan, but for short term only. 3. Increment

66. FSG (Financial Statement Generator)

66. FSG (Financial Statement Generator)

67. Remove blank spaces from notepad++
  • Go to Search -> Replace
  • Select “Regular expression” under Search mode.
  • For leading spaces use ^\s* and for trailing spaces use \s+$ for “Find what” and leave “Replace with” blank.
  • Click Replace All
68. Inactive – No Manager

The concurrent manager defined to process this request is not active in the current work shift. The request will not process within this work shift. Contact your system administrator to verify that the concurrent manager work shifts are defined correctly.
Navigation: System Administrator -> Concurrent -> Managers -> Administer Search for the Managers with pending requests and confirm your requests in running in this manager by clicking on requests button.
Now go to System Administrator -> Concurrent -> Managers -> Define Query for this Manager and go to shifts. Here you can change the shift to Standard and the manager will then run for 24 hr.

69. Oracle Discoverer
  • Discoverer Desktop (Only for execution — Running & seeing the report)
  • Discoverer Administrator (Developer Tool)
  • Discoverer Plus (Web Browser)
70. API to delete Concurrent program definition and executable
CLEAR SCREEN;

SET SERVEROUTPUT ON;

DECLARE
    v_conc_program_id         NUMBER;
    v_conc_program_name       VARCHAR2(200);
    v_conc_prog_short_name    VARCHAR2(200);
    v_ccp_application_id      NUMBER;
    v_ccp_application_name    VARCHAR2(200);
    v_conc_program_count      NUMBER;
    v_conc_executable_id      NUMBER;
    v_conc_executable_name    VARCHAR2(200);
    v_conc_exec_short_name    VARCHAR2(200);
    v_cce_application_id      NUMBER;
    v_cce_application_name    VARCHAR2(200);
    v_conc_executable_count   NUMBER;
    TYPE tab_ccp_name IS
        TABLE OF VARCHAR2(200) INDEX BY BINARY_INTEGER;
    vtab_ccp_name             tab_ccp_name;
BEGIN
    vtab_ccp_name(1) := 'XX Supplier Conversion';   
  --vtab_ccp_name(2) := 'Program Name';
  --vtab_ccp_name(3) := 'Program Name';
  --vtab_ccp_name(4) := 'Program Name';
    v_conc_program_count := 0;
    v_conc_executable_count := 0;
    FOR i IN 1..vtab_ccp_name.count LOOP
        BEGIN
            SELECT
                concurrent_program_id,
                concurrent_program_name,
                application_id,
                executable_id
            INTO
                v_conc_program_id,v_conc_prog_short_name,v_ccp_application_id,v_conc_executable_id
            FROM
                apps.fnd_concurrent_programs_vl
            WHERE
                user_concurrent_program_name = vtab_ccp_name(i);

            SELECT
                application_name
            INTO
                v_ccp_application_name
            FROM
                apps.fnd_application_vl
            WHERE
                application_id = v_ccp_application_id;

        EXCEPTION
            WHEN OTHERS THEN
                dbms_output.put_line('Caught in Exception while fetching the details of concurrent program : '
                || vtab_ccp_name(i)
                || ' : '
                || sqlerrm);
        END;

        IF
            v_conc_executable_id IS NOT NULL
        THEN
            BEGIN
                SELECT
                    executable_name,
                    application_id
                INTO
                    v_conc_exec_short_name,v_cce_application_id
                FROM
                    apps.fnd_executables
                WHERE
                    executable_id = v_conc_executable_id;

                SELECT
                    application_name
                INTO
                    v_cce_application_name
                FROM
                    apps.fnd_application_vl
                WHERE
                    application_id = v_cce_application_id;

            EXCEPTION
                WHEN OTHERS THEN
                    dbms_output.put_line('Caught in Exception while fetching the details of concurrent program Executable : '
                    || sqlerrm);
            END;

        ELSE
            dbms_output.put_line('Executable not existing for concurrent program : '
            || vtab_ccp_name(i) );
        END IF;

        IF
            v_conc_program_id > 0
        THEN
        /*-- API to Delete Concurrent Program ---- */
            BEGIN
                apps.fnd_program.delete_program(program_short_name => v_conc_prog_short_name,application => v_ccp_application_name);
                COMMIT;
                dbms_output.put_line('Concurrent Program has been deleted : '
                || vtab_ccp_name(i) );
                v_conc_program_count := v_conc_program_count + 1;
            EXCEPTION
                WHEN OTHERS THEN
                    dbms_output.put_line('Caught in Exception while deleting the Concurrent Program : '
                    || vtab_ccp_name(i)
                    || ' : '
                    || sqlerrm);
            END;
        
        /*-- API to Delete Concurrent Program Executable ---- */

            BEGIN
                apps.fnd_program.delete_executable(executable_short_name => v_conc_exec_short_name,application => v_cce_application_name);
                COMMIT;
                dbms_output.put_line('Concurrent Program Executable has been deleted : '
                || v_conc_exec_short_name);
                v_conc_executable_count := v_conc_executable_count + 1;
            EXCEPTION
                WHEN OTHERS THEN
                    dbms_output.put_line('Caught in Exception while deleting the Concurrent Program Executable : '
                    || v_conc_exec_short_name
                    || ' : '
                    || sqlerrm);
            END;

        ELSE
            dbms_output.put_line('Concurrent program Not Existing, Please create  : '
            || vtab_ccp_name(i) );
        END IF;

        dbms_output.put_line('Total Count of deleted Concurrent Program/s  : '
        || v_conc_program_count);
        dbms_output.put_line('Total Count of deleted Concurrent Program Executable  : '
        || v_conc_executable_count);
    END LOOP;

EXCEPTION
    WHEN OTHERS THEN
        dbms_output.put_line('Caught in Main Exception :'
        || sqlerrm);
END;
/
71. Database Schemas Found in EBS

https://docs.oracle.com/cd/E26401_01/doc.122/e22952/T156458T659606.html

72. Custom Top

#${APPLCSF} = /u01/oraout/instance_name Custom Top: /u01/app/instance_name/apps/apps_st/appl/custom_schema/12.0.0 Shell script/ Host file/ .sh file: /u01/app/instance_name/apps/apps_st/appl/xx/12.0.0/bin

73. Open & Closed Periods
  • Purchasing Period: Navigation: Purchasing Super User >Setup >Financials >Accounting >Control Purchasing Periods Enter the Fiscal year and click Go button. (If it prompts for selecting Operating Unit, select it and click Go button.) Then open the periods by changing the Period Status and click on Save button.
  • Payables Period: Navigation: Payables Super User or Payable Manager >Accounting >Control Payables Periods Open the periods by changing the Period Status and click on Save button.
  • General Ledger Period: Navigation: General Ledger Super User >Setup >Open/Close First find the required Ledger Then open the required periods by clicking “Open Periods” button, select the required period as Target Period and click Open button.
  • Inventory Period: Navigation: Inventory Super User >Accounting Close Cycle >Inventory Accounting Periods Then select the period to be opened and click on Change Status button.

74. e-Invoice Germany
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Invoice
	xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
	xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
	xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
	xmlns:cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2"
	xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
	xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
	xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"
	xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2"
	xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2"
	xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2"
	xmlns:udt="urn:oasis:names:specification:bdndr:schema:xsd:UnqualifiedDataTypes-1"
	xmlns:vrbl="urn:vertexinc:vrbl:ExtensionComponent:1"
	xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
	<ext:UBLExtensions>
		<ext:UBLExtension>
			<ext:ExtensionContent>
				<vrbl:InvoiceExtension>
					<vrbl:RoutingDetails>
						<vrbl:Sender>info@lieferant.com</vrbl:Sender>
						<vrbl:Receiver>DE727081482</vrbl:Receiver>
					</vrbl:RoutingDetails>
				</vrbl:InvoiceExtension>
			</ext:ExtensionContent>
		</ext:UBLExtension>
	</ext:UBLExtensions>
	<cbc:CustomizationID>urn:vertexinc:vrbl:billing:1#Invoice#VRBL-Invoice-DE-PeppolBIS-3p0</cbc:CustomizationID>
	<cbc:ProfileID>urn:vertexinc:vrbl:billing:1</cbc:ProfileID>
	<cbc:ID>6063636771001</cbc:ID>
	<cbc:IssueDate>2026-08-27</cbc:IssueDate>
	<cbc:DueDate>2026-09-16</cbc:DueDate>
	<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
	<cbc:Note>Bank: LBBW Stuttgart IBAN: DE75512108001245126199 BIC / Swift-Code: SOLADEST600;Lieferant GmbH & Co. KG - 98765 Lieferantenstadt - T +49 (0)7891 11-0 - F +49(0)7891 11-1000 - info@lieferant.com - www.lieferant.com Hausanschrift: Lieferanten-Strasse 12-17 - 98765 Lieferantenstadt - Sitz Lieferantenstadt, Amtsgericht Stuttgart HRA 123456 Geschäftsführer: ;Wenn nicht anders angegeben entspricht das Leistungsdatum dem Rechnungsdatum.;Haben Sie Fragen zur Rechnung? Gerne hilft Ihnen Ihr zuständiger Lieferant Verkäufer weiter. Die Lieferung erfolgte zu unseren bekannten Verkaufs- und Lieferbedingungen. Bitte beachten Sie hierzu unsere allgemeinen Geschäftsbedingungen unter www.lieferant.de/agb.;Zahlungsavis/Aufstellung/Auflistung zur Zahlung bitte an: E-mail: zahlungseingang@lieferant.com oder Fax +49 7891 11-59333;ZUGFeRD vers 2.5.0 Extended;Collective Invoice</cbc:Note>
	<cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
	<cbc:BuyerReference>BUYERREFERENCE BT-10</cbc:BuyerReference>
	<cac:AccountingSupplierParty>
		<cac:Party>
			<cbc:EndpointID schemeID="VRBL:DE:EM">info@lieferant.com</cbc:EndpointID>
			<cac:PartyIdentification>
				<cbc:ID schemeID="0088">0001231000000</cbc:ID>
			</cac:PartyIdentification>
			<cac:PostalAddress>
				<cbc:StreetName>Lieferanten-Strasse.12-17</cbc:StreetName>
				<cbc:CityName>Lieferantenstadt</cbc:CityName>
				<cbc:PostalZone>98765</cbc:PostalZone>
				<cac:Country>
					<cbc:IdentificationCode>DE</cbc:IdentificationCode>
				</cac:Country>
			</cac:PostalAddress>
			<cac:PartyTaxScheme>
				<cbc:CompanyID>DE727081482</cbc:CompanyID>
				<TaxScheme
					xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
					<cbc:ID>VAT</cbc:ID>
				</TaxScheme>
			</cac:PartyTaxScheme>
			<cac:PartyTaxScheme>
				<cbc:CompanyID>88888/00072</cbc:CompanyID>
				<TaxScheme
					xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
					<cbc:ID>FC</cbc:ID>
				</TaxScheme>
			</cac:PartyTaxScheme>
			<cac:PartyLegalEntity>
				<cbc:RegistrationName>Lieferant GmbH & Co. KG</cbc:RegistrationName>
			</cac:PartyLegalEntity>
			<cac:Contact>
				<cbc:Name>Tim Mustermann</cbc:Name>
				<cbc:Telephone>0170 66677788</cbc:Telephone>
				<cbc:ElectronicMail>tim.Mustermann@lieferant.com</cbc:ElectronicMail>
			</cac:Contact>
		</cac:Party>
	</cac:AccountingSupplierParty>
	<cac:AccountingCustomerParty>
		<cac:Party>
			<cbc:EndpointID schemeID="9930">DE727081482</cbc:EndpointID>
			<cac:PartyIdentification>
				<cbc:ID>9900880077</cbc:ID>
			</cac:PartyIdentification>
			<cac:PostalAddress>
				<cbc:StreetName>Musterstrasse 44</cbc:StreetName>
				<cbc:CityName>Musterstadt</cbc:CityName>
				<cbc:PostalZone>40789</cbc:PostalZone>
				<cac:Country>
					<cbc:IdentificationCode>DE</cbc:IdentificationCode>
				</cac:Country>
			</cac:PostalAddress>
			<cac:PartyTaxScheme>
				<cbc:CompanyID>DE727081482</cbc:CompanyID>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:PartyTaxScheme>
			<cac:PartyLegalEntity>
				<cbc:RegistrationName>Musterkunde GmbH & Co Name 2 Musterkunde</cbc:RegistrationName>
			</cac:PartyLegalEntity>
			<cac:Contact>
				<cbc:Name>Herr Test Monteur</cbc:Name>
				<cbc:Telephone>02173 9364</cbc:Telephone>
				<cbc:ElectronicMail>mike.maier@lieferant.com</cbc:ElectronicMail>
			</cac:Contact>
		</cac:Party>
	</cac:AccountingCustomerParty>
	<cac:Delivery>
		<cbc:ActualDeliveryDate>2026-08-27</cbc:ActualDeliveryDate>
		<cac:DeliveryLocation>
			<cac:Address>
				<cbc:StreetName>Musterstrasse 44</cbc:StreetName>
				<cbc:CityName>Musterstadt</cbc:CityName>
				<cbc:PostalZone>40789</cbc:PostalZone>
				<cac:Country>
					<cbc:IdentificationCode>DE</cbc:IdentificationCode>
				</cac:Country>
			</cac:Address>
		</cac:DeliveryLocation>
		<cac:DeliveryParty>
			<cac:PartyName>
				<cbc:Name>Musterkunde GmbH & Co Name 2 Musterkunde</cbc:Name>
			</cac:PartyName>
			<cac:PartyTaxScheme>
				<cbc:CompanyID>DE106008386</cbc:CompanyID>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:PartyTaxScheme>
		</cac:DeliveryParty>
	</cac:Delivery>
	<cac:PaymentMeans>
		<cbc:PaymentMeansCode name="Bezahlung per SEPA Überweisung">58</cbc:PaymentMeansCode>
		<cbc:PaymentID>Kundennummer:. 9900880077 Rechnungsnummer:. 6063636771001</cbc:PaymentID>
		<cac:PayeeFinancialAccount>
			<cbc:ID>DE75512108001245126199</cbc:ID>
			<cac:FinancialInstitutionBranch>
				<cbc:ID>SOLADEST600</cbc:ID>
			</cac:FinancialInstitutionBranch>
		</cac:PayeeFinancialAccount>
	</cac:PaymentMeans>
	<cac:PaymentTerms>
		<cbc:Note>Bis zum 16.09.2026 ohne Abzug; Bis zum 06.09.2026 erhalten Sie 2,000 % Skonto</cbc:Note>
		<cbc:SettlementDiscountPercent>2</cbc:SettlementDiscountPercent>
		<SettlementPeriod
			xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
			<cbc:DurationMeasure unitCode="D">10</cbc:DurationMeasure>
		</SettlementPeriod>
	</cac:PaymentTerms>
	<cac:TaxTotal>
		<cbc:TaxAmount currencyID="EUR">100.83</cbc:TaxAmount>
		<cac:TaxSubtotal>
			<cbc:TaxableAmount currencyID="EUR">516.75</cbc:TaxableAmount>
			<cbc:TaxAmount currencyID="EUR">98.18</cbc:TaxAmount>
			<cac:TaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:TaxCategory>
		</cac:TaxSubtotal>
		<cac:TaxSubtotal>
			<cbc:TaxableAmount currencyID="EUR">37.8</cbc:TaxableAmount>
			<cbc:TaxAmount currencyID="EUR">2.65</cbc:TaxAmount>
			<cac:TaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>7</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:TaxCategory>
		</cac:TaxSubtotal>
	</cac:TaxTotal>
	<cac:LegalMonetaryTotal>
		<cbc:LineExtensionAmount currencyID="EUR">554.55</cbc:LineExtensionAmount>
		<cbc:TaxExclusiveAmount currencyID="EUR">554.55</cbc:TaxExclusiveAmount>
		<cbc:TaxInclusiveAmount currencyID="EUR">655.38</cbc:TaxInclusiveAmount>
		<cbc:AllowanceTotalAmount currencyID="EUR">0</cbc:AllowanceTotalAmount>
		<cbc:PayableAmount currencyID="EUR">655.38</cbc:PayableAmount>
	</cac:LegalMonetaryTotal>
	<cac:InvoiceLine>
		<cbc:ID>000001</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">25</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">51.5</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Gewindestange</cbc:Description>
			<cbc:Name>GWDSTG-DIN976-A-4.8 M4</cbc:Name>
			<cac:SellersItemIdentification>
				<cbc:ID>9999999 25</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7711231873598</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">2.06</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">1</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000002</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">150</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">73.94</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Müllsack, -beutel</cbc:Description>
			<cbc:Name>MUELLSACK-EXTRASTARK</cbc:Name>
			<cac:BuyersItemIdentification>
				<cbc:ID>KD-MAT POS 1 BT-156</cbc:ID>
			</cac:BuyersItemIdentification>
			<cac:SellersItemIdentification>
				<cbc:ID>88888888888 150</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7748539263943</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">49.29</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">100</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000003</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">400</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">131.04</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Schraube Rahmen</cbc:Description>
			<cbc:Name>Schraube spezial 10 x 100</cbc:Name>
			<cac:SellersItemIdentification>
				<cbc:ID>77777777777 200</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7738898142591</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">32.76</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">100</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000004</cbc:ID>
		<cbc:Note>Test</cbc:Note>
		<cbc:InvoicedQuantity unitCode="H87">500</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">45.8</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Sechskantmutter</cbc:Description>
			<cbc:Name>MUTTER-6KT-DIN934-M6</cbc:Name>
			<cac:BuyersItemIdentification>
				<cbc:ID>KD-MAT POS 1 BT-156</cbc:ID>
			</cac:BuyersItemIdentification>
			<cac:SellersItemIdentification>
				<cbc:ID>6666666 100</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7711231333337</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">9.16</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">100</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000005</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">500</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">66.6</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Sechskantmutter</cbc:Description>
			<cbc:Name>MUTTER-6KT-DIN934-M8</cbc:Name>
			<cac:BuyersItemIdentification>
				<cbc:ID>KD-MAT POS 2 BT-156</cbc:ID>
			</cac:BuyersItemIdentification>
			<cac:SellersItemIdentification>
				<cbc:ID>5555555 100</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7711231333498</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">13.32</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">100</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000006</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">300</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">147.87</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Müllsack, -beutel</cbc:Description>
			<cbc:Name>MUELLSACK-EXTRASTARK</cbc:Name>
			<cac:BuyersItemIdentification>
				<cbc:ID>KD-MAT POS 1 BT-156</cbc:ID>
			</cac:BuyersItemIdentification>
			<cac:SellersItemIdentification>
				<cbc:ID>88888888888 150</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7748539263943</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>19</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">49.29</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">100</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
	<cac:InvoiceLine>
		<cbc:ID>000007</cbc:ID>
		<cbc:InvoicedQuantity unitCode="H87">2</cbc:InvoicedQuantity>
		<cbc:LineExtensionAmount currencyID="EUR">37.8</cbc:LineExtensionAmount>
		<cac:InvoicePeriod>
			<cbc:EndDate>2026-08-27</cbc:EndDate>
		</cac:InvoicePeriod>
		<cac:Item>
			<cbc:Description>Kaffee</cbc:Description>
			<cbc:Name>KAFFEE-1KG</cbc:Name>
			<cac:SellersItemIdentification>
				<cbc:ID>44444444444 10</cbc:ID>
			</cac:SellersItemIdentification>
			<cac:StandardItemIdentification>
				<cbc:ID schemeID="0160">7765233128651</cbc:ID>
			</cac:StandardItemIdentification>
			<cac:ClassifiedTaxCategory>
				<cbc:ID>S</cbc:ID>
				<cbc:Percent>7</cbc:Percent>
				<cac:TaxScheme>
					<cbc:ID>VAT</cbc:ID>
				</cac:TaxScheme>
			</cac:ClassifiedTaxCategory>
		</cac:Item>
		<cac:Price>
			<cbc:PriceAmount currencyID="EUR">18.9</cbc:PriceAmount>
			<cbc:BaseQuantity unitCode="H87">1</cbc:BaseQuantity>
		</cac:Price>
	</cac:InvoiceLine>
</Invoice>
SELECT XMLELEMENT(
    "Invoice",
    XMLATTRIBUTES(
        'urn:pagero:CommonAggregateComponents:1.0' AS "xmlns:cac",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS "xmlns:cbc",
        'urn:pagero:ExtensionComponent:1.0' AS "xmlns:puf",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS "xmlns:ext",
        'urn:pagero:PageroUniversalFormat:Invoice:1.0' AS "xmlns"
    ),
    XMLELEMENT(
        "cbc:CustomizationID",
        'urn:pagero.com:puf:billing:2.0'
    ),
    XMLELEMENT(
        "cbc:ProfileID",
        'urn:pagero.com:puf:billing:1.0'
    ),
    XMLELEMENT(
        "cbc:ID",
        trx.trx_number
    ),
    XMLELEMENT(
        "cbc:IssueDate",
        to_char(trx.trx_date, 'YYYY-MM-DD')
    ),
    XMLELEMENT(
        "cbc:DueDate",
        to_char(trx.due_date, 'YYYY-MM-DD')
    ),

         ----------------------------------------------------------
         -- EXPORT FLAG
         ----------------------------------------------------------

    XMLELEMENT(
        "cbc:InvoiceTypeCode",
        XMLATTRIBUTES(
            '00000001' AS "name"
        ),
        '380'
    ),
    XMLELEMENT(
        "cbc:DocumentCurrencyCode",
        trx.invoice_currency_code
    ),

         ----------------------------------------------------------
         -- Customs Reference
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:AdditionalDocumentReference",
        XMLELEMENT(
            "cbc:ID",
            XMLATTRIBUTES(
                'ED' AS "schemeID"
            ),
            --nvl(export_reference, 'EXPORT')
            'EXPORT'
        )
    ),

         ----------------------------------------------------------
         -- Supplier
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:AccountingSupplierParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES(
                    '0235' AS "schemeID"
                ),
                trx.attribute6
            ),
            XMLELEMENT(
                "cac:PartyIdentification",
                XMLELEMENT(
                    "cbc:ID",
                    XMLATTRIBUTES(
                        'AE:LRA' AS "schemeID"
                    ),
                    trx.attribute11
                )
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT(
                    "cbc:StreetName",
                    trx.attribute2
                ),
                XMLELEMENT(
                    "cbc:CityName",
                    trx.attribute3
                ),
                XMLELEMENT(
                    "cbc:CountrySubentity",
                    trx.attribute4
                ),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT(
                        "cbc:IdentificationCode",
                        trx.attribute5
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT(
                    "cbc:CompanyID",
                    trx.attribute7
                ),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT(
                        "cbc:ID",
                        'VAT'
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT(
                    "cbc:RegistrationName",
                    trx.description
                ),
                XMLELEMENT(
                    "cbc:CompanyID",
                    XMLATTRIBUTES(
                        'AE:TL' AS "schemeID"
                    ),
                    trx.attribute9
                )
            )
        )
    ),

         ----------------------------------------------------------
         -- CUSTOMER EXPORT
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:AccountingCustomerParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES(
                    '0235' AS "schemeID"
                ),
                 -- '9900000099'
                trx.global_attribute2
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT(
                    "cbc:StreetName",
                    trx.customer_address
                ),
                XMLELEMENT(
                    "cbc:CityName",
                    trx.customer_city
                ),
                XMLELEMENT(
                    "cbc:CountrySubentity",
                     --trx.customer_state
                    trx.global_attribute1
                ),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT(
                        "cbc:IdentificationCode",
                       -- trx.customer_country_code)
                        --l_customer_country_code
                        'DE'
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT(
                    "cbc:RegistrationName",
                    trx.customer_name_xml
                )
            )
        )
    ),

         ----------------------------------------------------------
         -- DELIVERY
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:Delivery",
        XMLELEMENT(
            "cbc:ActualDeliveryDate",
            to_char(trx.trx_date, 'YYYY-MM-DD')
        ),
        XMLELEMENT(
            "cac:DeliveryLocation",
            XMLELEMENT(
                "cac:Address",
                XMLELEMENT(
                    "cbc:StreetName",
                    trx.customer_address
                ),
                XMLELEMENT(
                    "cbc:CityName",
                    trx.customer_city
                ),
                XMLELEMENT(
                    "cbc:CountrySubentity",
                    trx.customer_state
                ),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT(
                        "cbc:IdentificationCode",
                        --l_customer_country_code
                        'DE'
                    )
                )
            )
        ),
        XMLELEMENT(
            "cac:DeliveryParty",
            XMLELEMENT(
                "cac:PartyName",
                XMLELEMENT(
                    "cbc:Name",
                    trx.customer_name_xml
                )
            )
        )
    ),

         ----------------------------------------------------------
         -- INCOTERM
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:DeliveryTerms",
        XMLELEMENT(
            "cbc:ID",
            --nvl(incoterm, 'FOB')
            'FOB'
        )
    ),

         ----------------------------------------------------------
         -- PAYMENT
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:PaymentMeans",
        XMLELEMENT(
            "cbc:PaymentMeansCode",
            '30'
        ),
        XMLELEMENT(
            "cac:PayeeFinancialAccount",
            XMLELEMENT(
                "cbc:ID",
                trx.attribute1
            ),
            XMLELEMENT(
                "cac:FinancialInstitutionBranch",
                XMLELEMENT(
                    "cbc:ID",
                    trx.attribute15
                )
            )
        )
    ),

         ----------------------------------------------------------
         -- ZERO RATED TAX
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:TaxTotal",
        XMLELEMENT(
            "cbc:TaxAmount",
            XMLATTRIBUTES(
                'AED' AS "currencyID"
            ),
            0
        ),
        XMLELEMENT(
            "cac:TaxSubtotal",
            XMLELEMENT(
                "cbc:TaxableAmount",
                XMLATTRIBUTES(
                    'AED' AS "currencyID"
                ),
                trx.net_amount
            ),
            XMLELEMENT(
                "cbc:TaxAmount",
                XMLATTRIBUTES(
                    'AED' AS "currencyID"
                ),
                0
            ),
            XMLELEMENT(
                "cac:TaxCategory",
                XMLELEMENT(
                    "cbc:ID",
                    'Z'
                ),
                XMLELEMENT(
                    "cbc:Percent",
                    '0.00'
                ),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT(
                        "cbc:ID",
                        'VAT'
                    )
                )
            )
        )
    ),

         ----------------------------------------------------------
         -- MONETARY TOTAL
         ----------------------------------------------------------

    XMLELEMENT(
        "cac:LegalMonetaryTotal",
        XMLELEMENT(
            "cbc:LineExtensionAmount",
            XMLATTRIBUTES(
                'AED' AS "currencyID"
            ),
            trx.net_amount
        ),
        XMLELEMENT(
            "cbc:TaxExclusiveAmount",
            XMLATTRIBUTES(
                'AED' AS "currencyID"
            ),
            trx.net_amount
        ),
        XMLELEMENT(
            "cbc:TaxInclusiveAmount",
            XMLATTRIBUTES(
                'AED' AS "currencyID"
            ),
            trx.net_amount
        ),
        XMLELEMENT(
            "cbc:PayableAmount",
            XMLATTRIBUTES(
                'AED' AS "currencyID"
            ),
            trx.net_amount
        )
    ),

         ----------------------------------------------------------
         -- INVOICE LINES
         ----------------------------------------------------------

    (SELECT XMLAGG(XMLELEMENT(
          "cac:InvoiceLine",
         XMLELEMENT(
              "cbc:ID",
              l.line_number
          ),
         XMLELEMENT(
              "cbc:InvoicedQuantity",
         XMLATTRIBUTES(
                  'EA' AS "unitCode"
              ),
              l.quantity_invoiced
          ),
         XMLELEMENT(
              "cbc:LineExtensionAmount",
         XMLATTRIBUTES(
                  'AED' AS "currencyID"
              ),
              l.extended_amount
          ),
         XMLELEMENT(
              "cac:TaxTotal",
         XMLELEMENT(
                  "cbc:TaxAmount",
         XMLATTRIBUTES(
                      'AED' AS "currencyID"
                  ),
         0
              )
          ),
         XMLELEMENT(
              "cac:Item",
         XMLELEMENT(
                  "cbc:Description",
                  l.description
              ),
         XMLELEMENT(
                  "cbc:Name",
                  --item_name
                  l.description
              ),
         XMLELEMENT(
                  "cac:CommodityClassification",
         XMLELEMENT(
                      "cbc:CommodityCode",
         'G'
                  ),
         XMLELEMENT(
                      "cbc:ItemClassificationCode",
         XMLATTRIBUTES(
                          'HS' AS "listID"
                      ),
         '3304.99.00'
                  )
              ),
         XMLELEMENT(
                  "cac:ClassifiedTaxCategory",
         XMLELEMENT(
                      "cbc:ID",
         'Z'
                  ),
         XMLELEMENT(
                      "cbc:Percent",
         '0.00'
                  ),
         XMLELEMENT(
                      "cac:TaxScheme",
         XMLELEMENT(
                          "cbc:ID",
         'VAT'
                      )
                  )
              )
          ),
         XMLELEMENT(
              "cac:Price",
         XMLELEMENT(
                  "cbc:PriceAmount",
         XMLATTRIBUTES(
                      'AED' AS "currencyID"
                  ),
         round(nvl(l.extended_amount, 0) / decode(
                      nvl(l.quantity_invoiced, 0),
                      0,
                      1,
                      l.quantity_invoiced
                  ),
               2)
              ),
         XMLELEMENT(
                  "cbc:BaseQuantity",
         XMLATTRIBUTES(
                      'EA' AS "unitCode"
                  ),
         1
              ),
         XMLELEMENT(
                  "cac:AllowanceCharge",
         XMLELEMENT(
                      "cbc:ChargeIndicator",
         'false'
                  ),
         XMLELEMENT(
                      "cbc:Amount",
         XMLATTRIBUTES(
                          'AED' AS "currencyID"
                      ),
         0
                  ),
         XMLELEMENT(
                      "cbc:BaseAmount",
         XMLATTRIBUTES(
                          'AED' AS "currencyID"
                      ),
         round(nvl(l.extended_amount, 0) / decode(
                          nvl(l.quantity_invoiced, 0),
                          0,
                          1,
                          l.quantity_invoiced
                      ),
               2)
                  )
              )
          )
      ))
        FROM ra_customer_trx_lines_all l
      WHERE l.customer_trx_id = trx.customer_trx_id
        AND l.line_type = 'LINE'
    )
)
  --INTO l_xml
  FROM ( SELECT rcta.customer_trx_id,
                rcta.trx_number,
                rcta.trx_date,
                apsa.due_date,
                rcta.invoice_currency_code,
                hp.party_name customer_name_xml,
                hl.address2   customer_address,
                hl.city       customer_city,
                hl.state      customer_state,

             --SUM(NVL(zl.taxable_amt,0)) taxable_amount,
             --SUM(NVL(zl.tax_amt,0)) tax_amount,

                ( SELECT SUM(nvl(line.extended_amount, 0))
                      FROM ra_customer_trx_lines_all line
                   WHERE line.customer_trx_id = rcta.customer_trx_id
                     AND line.line_type = 'LINE'
                )             net_amount,
                ( SELECT SUM(nvl(line.extended_amount, 0))
                      FROM ra_customer_trx_lines_all line
                   WHERE line.customer_trx_id = rcta.customer_trx_id
                     AND line.line_type = 'LINE'
             --) + SUM(NVL(zl.tax_amt,0)) gross_amount,
                )             gross_amount,
                flv.description,
                flv.attribute1,
                flv.attribute2,
                flv.attribute3,
                flv.attribute4,
                flv.attribute5,
                flv.attribute6,
                flv.attribute7,
                flv.attribute8,
                flv.attribute9,
                flv.attribute11,
                flv.attribute15,
                hcasa.global_attribute1,
                hcasa.global_attribute2,
                hcasa.global_attribute3,
                hcasa.global_attribute6
                FROM ra_customer_trx_all      rcta,
                     ar_payment_schedules_all apsa,
                     hz_cust_accounts         hca,
                     hz_parties               hp,
                     hz_cust_site_uses_all    hcsu,
                     hz_cust_acct_sites_all   hcasa,
                     hz_party_sites           hps,
                     hz_locations             hl,
           --zx_lines zl,
                     fnd_lookup_values        flv
        WHERE 1 = 1
          --AND rcta.customer_trx_id = p_invoice_id
          AND apsa.customer_trx_id (+)        = rcta.customer_trx_id
          AND hca.cust_account_id             = rcta.bill_to_customer_id
          AND hp.party_id                     = hca.party_id
          AND hcsu.site_use_id                = rcta.bill_to_site_use_id
          AND hcasa.cust_acct_site_id         = hcsu.cust_acct_site_id
          AND hps.party_site_id               = hcasa.party_site_id
          AND hl.location_id                  = hps.location_id
     -- AND zl.trx_id (+) = rcta.customer_trx_id
	 -- AND zl.internal_organization_id = rcta.org_id
          AND upper(rcta.attribute1)          = upper(flv.description)
          AND flv.lookup_type                 = 'HYT_AR_UAE_EINVOICE'
          AND flv.language                    = 'US'
          AND flv.enabled_flag                = 'Y'
          AND nvl(flv.end_date_active, sysdate) >= sysdate
          AND hcasa.global_attribute_category = 'JL.AE.ARXCUDCI.Additional'
        GROUP BY rcta.customer_trx_id,
                 rcta.trx_number,
                 rcta.trx_date,
                 apsa.due_date,
                 rcta.invoice_currency_code,
                 hp.party_name,
                 hl.address2,
                 hl.city,
                 hl.state,
                 flv.description,
                 flv.attribute2,
                 flv.attribute3,
                 flv.attribute4,
                 flv.attribute5,
                 flv.attribute6,
                 flv.attribute7,
                 flv.attribute8,
                 flv.attribute9,
                 flv.attribute11,
                 flv.description,
                 flv.attribute1,
                 flv.attribute15,
                 hcasa.global_attribute1,
                 hcasa.global_attribute2,
                 hcasa.global_attribute3,
                 hcasa.global_attribute6
       ) trx;
SELECT XMLELEMENT(
    "Invoice",
    XMLATTRIBUTES(
        'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS "xmlns",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns:cac",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS "xmlns:cbc",
        'urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2' AS "xmlns:cct",
        'http://w3.org' AS "xmlns:ds",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS "xmlns:ext",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2' AS "xmlns:n0",
        'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2' AS "xmlns:qdt",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2' AS "xmlns:sac",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2' AS "xmlns:sbc",
        'urn:oasis:names:specification:bdndr:schema:xsd:UnqualifiedDataTypes-1' AS "xmlns:udt",
        'urn:vertexinc:vrbl:ExtensionComponent:1' AS "xmlns:vrbl",
        'http://etsi.org' AS "xmlns:xades"
    ),

    ----------------------------------------------------------
    -- VERTEX ROUTING EXTENSION (ext:UBLExtensions)
    ----------------------------------------------------------
    XMLELEMENT(
        "ext:UBLExtensions",
        XMLELEMENT(
            "ext:UBLExtension",
            XMLELEMENT(
                "ext:ExtensionContent",
                XMLELEMENT(
                    "vrbl:InvoiceExtension",
                    XMLELEMENT(
                        "vrbl:RoutingDetails",
                        XMLELEMENT("vrbl:Sender", NVL(trx.seller_email, 'NULL')), -- TODO: Verify Vertex Sender ID with Business
                        XMLELEMENT("vrbl:Receiver", NVL(trx.routing_receiver, 'NULL')) -- TODO: Verify Peppol vs SMTP routing code with Business
                    )
                )
            )
        )
    ),

    ----------------------------------------------------------
    -- INVOICE HEADER DETAILS
    ----------------------------------------------------------
    XMLELEMENT("cbc:CustomizationID", 'urn:vertexinc:vrbl:billing:1#Invoice#VRBL-Invoice-DE-PeppolBIS-3p0'), -- TODO: Verify B2B vs DE-XRechnung-3p0 (B2G)
    XMLELEMENT("cbc:ProfileID", 'urn:vertexinc:vrbl:billing:1'),
    XMLELEMENT("cbc:ID", trx.trx_number),
    XMLELEMENT("cbc:IssueDate", TO_CHAR(trx.trx_date, 'YYYY-MM-DD')),
    XMLELEMENT("cbc:DueDate", TO_CHAR(trx.due_date, 'YYYY-MM-DD')),
    XMLELEMENT("cbc:InvoiceTypeCode", DECODE(trx.trx_type, 'CM', '381', '380')), -- TODO: Verify if Corrected Invoices (384) are used
    XMLELEMENT("cbc:Note", NVL(trx.invoice_note, 'NULL')), -- TODO: Verify statutory German legal footer text & comments source
    XMLELEMENT("cbc:DocumentCurrencyCode", trx.invoice_currency_code),
    XMLELEMENT("cbc:BuyerReference", NVL(trx.buyer_reference, 'NULL')), -- TODO: Verify source field for BT-10 / Leitweg-ID

    ----------------------------------------------------------
    -- SUPPLIER / SELLER (cac:AccountingSupplierParty)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:AccountingSupplierParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES('VRBL:DE:EM' AS "schemeID"), -- TODO: Verify Seller Endpoint schemeID ('VRBL:DE:EM' vs '0088' vs '9930')
                NVL(trx.seller_endpoint_id, 'NULL')
            ),
            XMLELEMENT(
                "cac:PartyIdentification",
                XMLELEMENT(
                    "cbc:ID",
                    XMLATTRIBUTES('0088' AS "schemeID"), -- TODO: Verify Seller Party ID / GLN schemeID
                    NVL(trx.seller_gln, 'NULL')
                )
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT("cbc:StreetName", NVL(trx.seller_street, 'NULL')),
                XMLELEMENT("cbc:CityName", NVL(trx.seller_city, 'NULL')),
                XMLELEMENT("cbc:PostalZone", NVL(trx.seller_postal_code, 'NULL')),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT("cbc:IdentificationCode", NVL(trx.seller_country, 'DE'))
                )
            ),
            -- Seller VAT ID (Scheme = VAT)
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT("cbc:CompanyID", NVL(trx.seller_vat_id, 'NULL')),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT("cbc:ID", 'VAT')
                )
            ),
            -- Seller Local Tax Number / Steuernummer (Scheme = FC)
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT("cbc:CompanyID", NVL(trx.seller_tax_num_fc, 'NULL')), -- TODO: Verify German Steuernummer source
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT("cbc:ID", 'FC')
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT("cbc:RegistrationName", NVL(trx.seller_legal_name, 'NULL'))
            ),
            XMLELEMENT(
                "cac:Contact",
                XMLELEMENT("cbc:Name", NVL(trx.seller_contact_name, 'NULL')), -- TODO: Verify AR Contact Name source
                XMLELEMENT("cbc:Telephone", NVL(trx.seller_contact_phone, 'NULL')), -- TODO: Verify AR Contact Phone source
                XMLELEMENT("cbc:ElectronicMail", NVL(trx.seller_contact_email, 'NULL')) -- TODO: Verify AR Contact Email source
            )
        )
    ),

    ----------------------------------------------------------
    -- CUSTOMER / BUYER (cac:AccountingCustomerParty)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:AccountingCustomerParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES('9930' AS "schemeID"), -- TODO: Verify Customer Endpoint schemeID ('9930' DE VAT vs '0204' Leitweg-ID vs 'EM')
                NVL(trx.customer_endpoint_id, 'NULL') -- TODO: Verify Customer Peppol Endpoint ID source
            ),
            XMLELEMENT(
                "cac:PartyIdentification",
                XMLELEMENT("cbc:ID", trx.customer_account_number)
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT("cbc:StreetName", trx.customer_address),
                XMLELEMENT("cbc:CityName", trx.customer_city),
                XMLELEMENT("cbc:PostalZone", trx.customer_postal_code),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT("cbc:IdentificationCode", trx.customer_country_code)
                )
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT("cbc:CompanyID", NVL(trx.customer_vat_id, 'NULL')), -- TODO: Verify Customer Bill-To VAT ID source
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT("cbc:ID", 'VAT')
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT("cbc:RegistrationName", trx.customer_name_xml)
            ),
            XMLELEMENT(
                "cac:Contact",
                XMLELEMENT("cbc:Name", NVL(trx.customer_contact_name, 'NULL')), -- TODO: Verify Bill-To Contact Name source
                XMLELEMENT("cbc:Telephone", NVL(trx.customer_contact_phone, 'NULL')), -- TODO: Verify Bill-To Contact Phone source
                XMLELEMENT("cbc:ElectronicMail", NVL(trx.customer_contact_email, 'NULL')) -- TODO: Verify Bill-To Contact Email source
            )
        )
    ),

    ----------------------------------------------------------
    -- DELIVERY / SHIP-TO (cac:Delivery)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:Delivery",
        XMLELEMENT("cbc:ActualDeliveryDate", TO_CHAR(NVL(trx.ship_date_actual, trx.trx_date), 'YYYY-MM-DD')),
        XMLELEMENT(
            "cac:DeliveryLocation",
            XMLELEMENT(
                "cac:Address",
                XMLELEMENT("cbc:StreetName", trx.ship_to_address),
                XMLELEMENT("cbc:CityName", trx.ship_to_city),
                XMLELEMENT("cbc:PostalZone", trx.ship_to_postal_code),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT("cbc:IdentificationCode", trx.ship_to_country_code)
                )
            )
        ),
        XMLELEMENT(
            "cac:DeliveryParty",
            XMLELEMENT(
                "cac:PartyName",
                XMLELEMENT("cbc:Name", trx.ship_to_customer_name)
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT("cbc:CompanyID", NVL(trx.ship_to_vat_id, 'NULL')), -- TODO: Verify if Ship-To VAT ID is required
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT("cbc:ID", 'VAT')
                )
            )
        )
    ),

    ----------------------------------------------------------
    -- PAYMENT MEANS (cac:PaymentMeans)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:PaymentMeans",
        XMLELEMENT(
            "cbc:PaymentMeansCode",
            XMLATTRIBUTES(NVL(trx.payment_means_name, 'Bezahlung per SEPA Überweisung') AS "name"),
            NVL(trx.payment_means_code, '58') -- TODO: Verify Receipt Method to UNCL 4461 mapping ('58' SEPA vs '30')
        ),
        XMLELEMENT(
            "cbc:PaymentID",
            'Kundennummer:. ' || trx.customer_account_number || ' Rechnungsnummer:. ' || trx.trx_number
        ),
        XMLELEMENT(
            "cac:PayeeFinancialAccount",
XMLELEMENT("cbc:ID", NVL(trx.seller_iban, 'NULL')), -- TODO: Verify Remit-To Bank IBAN source
XMLELEMENT(
"cac:FinancialInstitutionBranch",
XMLELEMENT("cbc:ID", NVL(trx.seller_bic, 'NULL')) -- TODO: Verify Remit-To Bank BIC/SWIFT source
)
)
),
----------------------------------------------------------
-- PAYMENT TERMS & SKONTO (cac:PaymentTerms)
----------------------------------------------------------
XMLELEMENT(
"cac:PaymentTerms",
XMLELEMENT("cbc:Note", NVL(trx.payment_terms_note, 'NULL')), -- TODO: Verify Skonto text formatting rule
XMLELEMENT("cbc:SettlementDiscountPercent", NVL(TO_CHAR(trx.skonto_percent), 'NULL')), -- TODO: Verify RA_TERMS_LINES_DISCOUNTS mapping
XMLELEMENT(
"SettlementPeriod",
XMLATTRIBUTES('urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns"),
XMLELEMENT(
"cbc:DurationMeasure",
XMLATTRIBUTES('D' AS "unitCode"),
NVL(TO_CHAR(trx.skonto_days), 'NULL') -- TODO: Verify discount days source
)
)
),
----------------------------------------------------------
-- TAX TOTAL & SUBTOTALS BY RATE (cac:TaxTotal)
----------------------------------------------------------
XMLELEMENT(
"cac:TaxTotal",
XMLELEMENT(
"cbc:TaxAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
trx.tax_amount
),
(
SELECT XMLAGG(
XMLELEMENT(
"cac:TaxSubtotal",
XMLELEMENT(
"cbc:TaxableAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
SUM(NVL(zl.taxable_amt, 0))
),
XMLELEMENT(
"cbc:TaxAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
SUM(NVL(zl.tax_amt, 0))
),
XMLELEMENT(
"cac:TaxCategory",
XMLELEMENT("cbc:ID", DECODE(NVL(zl.tax_rate, 0), 0, 'E', 'S')), -- TODO: Verify eBTax status to UNCL 5305 ('S','E','AE','K')
XMLELEMENT("cbc:Percent", NVL(zl.tax_rate, 0)),
XMLELEMENT(
"cac:TaxScheme",
XMLELEMENT("cbc:ID", 'VAT')
)
)
)
)
FROM zx_lines zl
WHERE zl.trx_id = trx.customer_trx_id
AND zl.application_id = 222
AND zl.entity_code = 'TRANSACTIONS'
AND zl.event_class_code IN ('INVOICE', 'CREDIT_MEMO', 'DEBIT_MEMO')
AND NVL(zl.cancel_flag, 'N') = 'N'
GROUP BY zl.tax_rate
)
),
----------------------------------------------------------
-- LEGAL MONETARY TOTAL (cac:LegalMonetaryTotal)
----------------------------------------------------------
XMLELEMENT(
"cac:LegalMonetaryTotal",
XMLELEMENT(
"cbc:LineExtensionAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
trx.net_amount
),
XMLELEMENT(
"cbc:TaxExclusiveAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
trx.net_amount
),
XMLELEMENT(
"cbc:TaxInclusiveAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
trx.gross_amount
),
XMLELEMENT(
"cbc:AllowanceTotalAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
0 -- TODO: Verify if header-level allowances exist
),
XMLELEMENT(
"cbc:PayableAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
trx.gross_amount
)
),
----------------------------------------------------------
-- INVOICE LINES (cac:InvoiceLine)
----------------------------------------------------------
(
SELECT XMLAGG(
XMLELEMENT(
"cac:InvoiceLine",
XMLELEMENT("cbc:ID", LPAD(l.line_number, 6, '0')),
-- Optional Line Note (present on Line 4 in sample XML)
CASE
WHEN l.attribute1 IS NOT NULL THEN XMLELEMENT("cbc:Note", l.attribute1) -- TODO: Verify line note source
ELSE NULL
END,
XMLELEMENT(
"cbc:InvoicedQuantity",
XMLATTRIBUTES(
DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode" -- TODO: Verify complete UOM to UN/ECE Rec 20 lookup
),
NVL(l.quantity_invoiced, l.quantity_credited)
),
XMLELEMENT(
"cbc:LineExtensionAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
l.extended_amount
),
XMLELEMENT(
"cac:InvoicePeriod",
XMLELEMENT("cbc:EndDate", TO_CHAR(NVL(l.rule_end_date, NVL(trx.ship_date_actual, trx.trx_date)), 'YYYY-MM-DD'))
),
XMLELEMENT(
"cac:Item",
XMLELEMENT("cbc:Description", l.description),
XMLELEMENT("cbc:Name", NVL(msib.segment1, l.description)),
-- Buyer Item Identification (BT-156)
XMLELEMENT(
"cac:BuyersItemIdentification",
XMLELEMENT("cbc:ID", NVL(mci.customer_item_number, 'NULL')) -- TODO: Verify Customer Item Number source
),
-- Seller Item Identification (BT-155)
XMLELEMENT(
"cac:SellersItemIdentification",
XMLELEMENT("cbc:ID", NVL(msib.segment1, 'NULL'))
),
-- Standard Item Identification / GTIN-EAN (BT-157)
XMLELEMENT(
"cac:StandardItemIdentification",
XMLELEMENT(
"cbc:ID",
XMLATTRIBUTES('0160' AS "schemeID"),
NVL(mcr.cross_reference, 'NULL') -- TODO: Verify GTIN/EAN barcode cross-reference type
)
),
-- Line Tax Category
XMLELEMENT(
"cac:ClassifiedTaxCategory",
XMLELEMENT("cbc:ID", DECODE(NVL(line_tax.tax_rate, 0), 0, 'E', 'S')),
XMLELEMENT("cbc:Percent", NVL(line_tax.tax_rate, 0)),
XMLELEMENT(
"cac:TaxScheme",
XMLELEMENT("cbc:ID", 'VAT')
)
)
),
XMLELEMENT(
"cac:Price",
XMLELEMENT(
"cbc:PriceAmount",
XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
NVL(l.unit_selling_price, 0)
),
XMLELEMENT(
"cbc:BaseQuantity",
XMLATTRIBUTES(
DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode"
),
1 -- TODO: Verify if BaseQuantity is always 1 or 100/1000
)
)
)
ORDER BY l.line_number
)
FROM ra_customer_trx_lines_all l,
mtl_system_items_b        msib,
mtl_customer_items        mci,
mtl_cross_references      mcr,
(
SELECT trx_line_id, MAX(tax_rate) tax_rate
FROM zx_lines
WHERE trx_id = trx.customer_trx_id
AND application_id = 222
AND entity_code = 'TRANSACTIONS'
AND NVL(cancel_flag, 'N') = 'N'
GROUP BY trx_line_id
) line_tax
WHERE l.customer_trx_id          = trx.customer_trx_id
AND l.line_type                = 'LINE'
AND msib.inventory_item_id (+) = l.inventory_item_id
AND msib.organization_id (+)   = l.warehouse_id
AND mci.customer_item_id (+)   = l.customer_item_id
AND mcr.inventory_item_id (+)  = l.inventory_item_id
AND mcr.cross_reference_type (+) = 'GTIN' -- TODO: Verify Cross Reference Type name in Oracle Inventory
AND line_tax.trx_line_id (+)   = l.customer_trx_line_id
)
) AS invoice_xml
FROM (
SELECT rcta.customer_trx_id,
rcta.trx_number,
rcta.trx_date,
rcta.ship_date_actual,
apsa.due_date,
rcta.invoice_currency_code,
rctta.type                      trx_type,
rcta.comments                   invoice_note,
rcta.purchase_order             buyer_reference, -- TODO: Verify if BT-10 is PURCHASE_ORDER or ATTRIBUTE1
-- Routing & Seller Details (Placeholder / Lookup Columns)
'NULL'                          seller_email,
'NULL'                          routing_receiver,
'NULL'                          seller_endpoint_id,
'NULL'                          seller_gln,
'NULL'                          seller_street,
'NULL'                          seller_city,
'NULL'                          seller_postal_code,
'DE'                            seller_country,
'NULL'                          seller_vat_id,
'NULL'                          seller_tax_num_fc,
'NULL'                          seller_legal_name,
'NULL'                          seller_contact_name,
'NULL'                          seller_contact_phone,
'NULL'                          seller_contact_email,
-- Bill-To Customer Details
hca.account_number              customer_account_number,
hp.party_name                   customer_name_xml,
hl.address1                     customer_address,
hl.city                         customer_city,
hl.postal_code                  customer_postal_code,
hl.country                      customer_country_code,
hcsu.tax_reference              customer_vat_id, -- TODO: Verify if VAT ID is on HCSU, HP, or ZX_REGISTRATIONS
'NULL'                          customer_endpoint_id,
'NULL'                          customer_contact_name,
'NULL'                          customer_contact_phone,
'NULL'                          customer_contact_email,
-- Ship-To Customer Details
NVL(hp_ship.party_name, hp.party_name)       ship_to_customer_name,
NVL(hl_ship.address1, hl.address1)           ship_to_address,
NVL(hl_ship.city, hl.city)                   ship_to_city,
NVL(hl_ship.postal_code, hl.postal_code)     ship_to_postal_code,
NVL(hl_ship.country, hl.country)             ship_to_country_code,
hcsu_ship.tax_reference                      ship_to_vat_id,
-- Payment & Terms Details
arm.name                        payment_means_name,
'58'                            payment_means_code,
'NULL'                          seller_iban,
'NULL'                          seller_bic,
rtt.description                 payment_terms_note,
NULL                            skonto_percent,
NULL                            skonto_days,
-- Monetary Totals
(
SELECT NVL(SUM(NVL(line.extended_amount, 0)), 0)
FROM ra_customer_trx_lines_all line
WHERE line.customer_trx_id = rcta.customer_trx_id
AND line.line_type = 'LINE'
) net_amount,
(
SELECT NVL(SUM(NVL(zl.tax_amt, 0)), 0)
FROM zx_lines zl
WHERE zl.trx_id = rcta.customer_trx_id
AND zl.application_id = 222
AND zl.entity_code = 'TRANSACTIONS'
AND NVL(zl.cancel_flag, 'N') = 'N'
) tax_amount,
(
SELECT NVL(SUM(NVL(line.extended_amount, 0)), 0)
FROM ra_customer_trx_lines_all line
WHERE line.customer_trx_id = rcta.customer_trx_id
AND line.line_type IN ('LINE', 'TAX', 'FREIGHT')
) gross_amount
FROM ra_customer_trx_all      rcta,
ra_cust_trx_types_all    rctta,
ar_payment_schedules_all apsa,
ar_receipt_methods       arm,
ra_terms_tl              rtt,
hz_cust_accounts         hca,
hz_parties               hp,
hz_cust_site_uses_all    hcsu,
hz_cust_acct_sites_all   hcasa,
hz_party_sites           hps,
hz_locations             hl,
hz_cust_site_uses_all    hcsu_ship,
hz_cust_acct_sites_all   hcasa_ship,
hz_party_sites           hps_ship,
hz_locations             hl_ship,
hz_cust_accounts         hca_ship,
hz_parties               hp_ship
WHERE 1 = 1
-- AND rcta.customer_trx_id = :p_invoice_id
AND rcta.complete_flag              = 'Y'
AND rctta.cust_trx_type_id          = rcta.cust_trx_type_id
AND rctta.org_id                    = rcta.org_id
AND apsa.customer_trx_id (+)        = rcta.customer_trx_id
AND arm.receipt_method_id (+)       = rcta.receipt_method_id
AND rtt.term_id (+)                 = rcta.term_id
AND rtt.language (+)                = 'D' -- German terms text ('US' for English)
-- Bill-To Joins
AND hca.cust_account_id             = rcta.bill_to_customer_id
AND hp.party_id                     = hca.party_id
AND hcsu.site_use_id                = rcta.bill_to_site_use_id
AND hcasa.cust_acct_site_id         = hcsu.cust_acct_site_id
AND hps.party_site_id               = hcasa.party_site_id
AND hl.location_id                  = hps.location_id
-- Ship-To Joins (Outer joined in case Ship-To is blank on AR Invoice)
AND hcsu_ship.site_use_id (+)       = rcta.ship_to_site_use_id
AND hcasa_ship.cust_acct_site_id (+) = hcsu_ship.cust_acct_site_id
AND hps_ship.party_site_id (+)      = hcasa_ship.party_site_id
AND hl_ship.location_id (+)         = hps_ship.location_id
AND hca_ship.cust_account_id (+)    = rcta.ship_to_customer_id
AND hp_ship.party_id (+)            = hca_ship.party_id
) trx;
SELECT XMLELEMENT(
    "Invoice",
    XMLATTRIBUTES(
        'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS "xmlns",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns:cac",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS "xmlns:cbc",
        'urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2' AS "xmlns:cct",
        'http://w3.org' AS "xmlns:ds",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS "xmlns:ext",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2' AS "xmlns:n0",
        'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2' AS "xmlns:qdt",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2' AS "xmlns:sac",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2' AS "xmlns:sbc",
        'urn:oasis:names:specification:bdndr:schema:xsd:UnqualifiedDataTypes-1' AS "xmlns:udt",
        'urn:vertexinc:vrbl:ExtensionComponent:1' AS "xmlns:vrbl",
        'http://etsi.org' AS "xmlns:xades"
    ),

    ----------------------------------------------------------
    -- VERTEX ROUTING DETAILS (ext:UBLExtensions)
    ----------------------------------------------------------
    XMLELEMENT(
        "ext:UBLExtensions",
        XMLELEMENT(
            "ext:UBLExtension",
            XMLELEMENT(
                "ext:ExtensionContent",
                XMLELEMENT(
                    "vrbl:InvoiceExtension",
                    XMLELEMENT(
                        "vrbl:RoutingDetails",
                        XMLELEMENT(
                            "vrbl:Sender",
                            NVL(trx.attribute6, 'NULL') -- TODO: Verify Vertex Technical Sender ID (flv.attribute6)
                        ),
                        XMLELEMENT(
                            "vrbl:Receiver",
                            NVL(trx.global_attribute2, 'NULL') -- TODO: Verify Vertex Technical Receiver ID (hcasa.global_attribute2)
                        )
                    )
                )
            )
        )
    ),

    ----------------------------------------------------------
    -- HEADER DETAILS
    ----------------------------------------------------------
    XMLELEMENT(
        "cbc:CustomizationID",
        'urn:vertexinc:vrbl:billing:1#Invoice#VRBL-Invoice-DE-PeppolBIS-3p0' -- TODO: Verify B2B vs DE-XRechnung-3p0 (B2G)
    ),
    XMLELEMENT(
        "cbc:ProfileID",
        'urn:vertexinc:vrbl:billing:1'
    ),
    XMLELEMENT(
        "cbc:ID",
        trx.trx_number
    ),
    XMLELEMENT(
        "cbc:IssueDate",
        TO_CHAR(trx.trx_date, 'YYYY-MM-DD')
    ),
    XMLELEMENT(
        "cbc:DueDate",
        TO_CHAR(trx.due_date, 'YYYY-MM-DD')
    ),
    XMLELEMENT(
        "cbc:InvoiceTypeCode",
        '380' -- TODO: Verify Credit Memo ('381') / Corrected Invoice ('384') logic
    ),
    XMLELEMENT(
        "cbc:Note",
        NVL(trx.attribute8, 'NULL') -- TODO: Verify German statutory legal footer & invoice note source (flv.attribute8 / rcta.comments)
    ),
    XMLELEMENT(
        "cbc:DocumentCurrencyCode",
        trx.invoice_currency_code
    ),
    XMLELEMENT(
        "cbc:BuyerReference",
        NVL(trx.global_attribute3, 'NULL') -- TODO: Verify BT-10 BuyerReference / Leitweg-ID source
    ),

    ----------------------------------------------------------
    -- SUPPLIER / SELLER (cac:AccountingSupplierParty)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:AccountingSupplierParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES(
                    'VRBL:DE:EM' AS "schemeID" -- TODO: Verify Seller Endpoint schemeID ('VRBL:DE:EM' vs '0088')
                ),
                NVL(trx.attribute6, 'NULL') -- TODO: Verify Seller Electronic Address (flv.attribute6)
            ),
            XMLELEMENT(
                "cac:PartyIdentification",
                XMLELEMENT(
                    "cbc:ID",
                    XMLATTRIBUTES(
                        '0088' AS "schemeID"
                    ),
                    NVL(trx.attribute11, 'NULL') -- TODO: Verify Seller GLN / Party ID (flv.attribute11)
                )
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT(
                    "cbc:StreetName",
                    NVL(trx.attribute2, 'NULL') -- TODO: Verify Seller Street (flv.attribute2)
                ),
                XMLELEMENT(
                    "cbc:CityName",
                    NVL(trx.attribute3, 'NULL') -- TODO: Verify Seller City (flv.attribute3)
                ),
                XMLELEMENT(
                    "cbc:PostalZone",
                    NVL(trx.attribute4, 'NULL') -- TODO: Verify Seller Postal Code (flv.attribute4)
                ),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT(
                        "cbc:IdentificationCode",
                        NVL(trx.attribute5, 'DE') -- TODO: Verify Seller Country Code (flv.attribute5)
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT(
                    "cbc:CompanyID",
                    NVL(trx.attribute7, 'NULL') -- TODO: Verify Seller VAT ID (flv.attribute7)
                ),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT(
                        "cbc:ID",
                        'VAT'
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT(
                    "cbc:CompanyID",
                    NVL(trx.attribute9, 'NULL') -- TODO: Verify Seller Local Tax Number / Steuernummer (flv.attribute9)
                ),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT(
                        "cbc:ID",
                        'FC'
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT(
                    "cbc:RegistrationName",
                    NVL(trx.description, 'NULL') -- TODO: Verify Seller Legal Entity Name (flv.description)
                )
            ),
            XMLELEMENT(
                "cac:Contact",
                XMLELEMENT(
                    "cbc:Name",
                    'NULL' -- TODO: Verify Seller Contact Name source
                ),
                XMLELEMENT(
                    "cbc:Telephone",
                    'NULL' -- TODO: Verify Seller Contact Telephone source
                ),
                XMLELEMENT(
                    "cbc:ElectronicMail",
                    'NULL' -- TODO: Verify Seller Contact Email source
                )
            )
        )
    ),

    ----------------------------------------------------------
    -- CUSTOMER / BUYER (cac:AccountingCustomerParty)
    ----------------------------------------------------------
    XMLELEMENT(
        "cac:AccountingCustomerParty",
        XMLELEMENT(
            "cac:Party",
            XMLELEMENT(
                "cbc:EndpointID",
                XMLATTRIBUTES(
                    '9930' AS "schemeID" -- TODO: Verify Buyer Endpoint schemeID ('9930' DE VAT vs '0204' Leitweg-ID)
                ),
                NVL(trx.global_attribute2, 'NULL') -- TODO: Verify Customer Endpoint ID (hcasa.global_attribute2)
            ),
            XMLELEMENT(
                "cac:PartyIdentification",
                XMLELEMENT(
                    "cbc:ID",
                    trx.account_number
                )
            ),
            XMLELEMENT(
                "cac:PostalAddress",
                XMLELEMENT(
                    "cbc:StreetName",
                    trx.customer_address
                ),
                XMLELEMENT(
                    "cbc:CityName",
                    trx.customer_city
                ),
                XMLELEMENT(
                    "cbc:PostalZone",
                    trx.customer_postal_code
                ),
                XMLELEMENT(
                    "cac:Country",
                    XMLELEMENT(
                        "cbc:IdentificationCode",
                        NVL(trx.customer_country_code, 'DE')
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyTaxScheme",
                XMLELEMENT(
                    "cbc:CompanyID",
                    NVL(trx.global_attribute6, 'NULL') -- TODO: Verify Customer VAT ID source (hcsu.tax_reference / hcasa.global_attribute6)
                ),
                XMLELEMENT(
                    "cac:TaxScheme",
                    XMLELEMENT(
                        "cbc:ID",
                        'VAT'
                    )
                )
            ),
            XMLELEMENT(
                "cac:PartyLegalEntity",
                XMLELEMENT(
                    "cbc:RegistrationName",
                    trx.customer_name_xml
                )
            ),
            XMLELEMENT(
                "cac:Contact",
                XMLELEMENT(
                    "cbc:Name",
                    'NULL' -- TODO: Verify Customer Contact Name source
                ),
                XMLELEMENT(
                    "cbc:Telephone",
                    'NULL' -- TODO: Verify Customer Contact Phone source
                ),
                XMLELEMENT(
                    "cbc:ElectronicMail",
                    'NULL' -- TODO: Verify Customer Contact Email source
                )
            )
        )
    ),

    ----------------------------------------------------------
-- DELIVERY (cac:Delivery)
----------------------------------------------------------
XMLELEMENT(
"cac:Delivery",
XMLELEMENT(
"cbc:ActualDeliveryDate",
TO_CHAR(trx.trx_date, 'YYYY-MM-DD') -- TODO: Verify if SHIP_DATE_ACTUAL should be used instead of TRX_DATE
),
XMLELEMENT(
"cac:DeliveryLocation",
XMLELEMENT(
"cac:Address",
XMLELEMENT(
"cbc:StreetName",
trx.customer_address
),
XMLELEMENT(
"cbc:CityName",
trx.customer_city
),
XMLELEMENT(
"cbc:PostalZone",
trx.customer_postal_code
),
XMLELEMENT(
"cac:Country",
XMLELEMENT(
"cbc:IdentificationCode",
NVL(trx.customer_country_code, 'DE')
)
)
)
),
XMLELEMENT(
"cac:DeliveryParty",
XMLELEMENT(
"cac:PartyName",
XMLELEMENT(
"cbc:Name",
trx.customer_name_xml
)
),
XMLELEMENT(
"cac:PartyTaxScheme",
XMLELEMENT(
"cbc:CompanyID",
NVL(trx.global_attribute6, 'NULL') -- TODO: Verify Delivery Party VAT ID source
),
XMLELEMENT(
"cac:TaxScheme",
XMLELEMENT(
"cbc:ID",
'VAT'
)
)
)
)
),
----------------------------------------------------------
-- PAYMENT MEANS (cac:PaymentMeans)
----------------------------------------------------------
XMLELEMENT(
"cac:PaymentMeans",
XMLELEMENT(
"cbc:PaymentMeansCode",
XMLATTRIBUTES(
'Bezahlung per SEPA Überweisung' AS "name" -- TODO: Verify Payment Means Name
),
'58' -- TODO: Verify Payment Means Code ('58' SEPA vs '30')
),
XMLELEMENT(
"cbc:PaymentID",
'Kundennummer:. ' || trx.account_number || ' Rechnungsnummer:. ' || trx.trx_number
),
XMLELEMENT(
"cac:PayeeFinancialAccount",
XMLELEMENT(
"cbc:ID",
NVL(trx.attribute1, 'NULL') -- TODO: Verify Remit-To Bank IBAN (flv.attribute1)
),
XMLELEMENT(
"cac:FinancialInstitutionBranch",
XMLELEMENT(
"cbc:ID",
NVL(trx.attribute15, 'NULL') -- TODO: Verify Remit-To Bank BIC / SWIFT (flv.attribute15)
)
)
)
),
----------------------------------------------------------
-- PAYMENT TERMS / SKONTO (cac:PaymentTerms)
----------------------------------------------------------
XMLELEMENT(
"cac:PaymentTerms",
XMLELEMENT(
"cbc:Note",
'NULL' -- TODO: Verify Skonto Payment Terms text note source
),
XMLELEMENT(
"cbc:SettlementDiscountPercent",
'NULL' -- TODO: Verify Skonto discount % source
),
XMLELEMENT(
"SettlementPeriod",
XMLATTRIBUTES(
'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns"
),
XMLELEMENT(
"cbc:DurationMeasure",
XMLATTRIBUTES(
'D' AS "unitCode"
),
'NULL' -- TODO: Verify Skonto discount days source
)
)
),
----------------------------------------------------------
-- TAX TOTAL & SUBTOTALS (cac:TaxTotal)
----------------------------------------------------------
XMLELEMENT(
"cac:TaxTotal",
XMLELEMENT(
"cbc:TaxAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
trx.tax_amount
),
(
SELECT XMLAGG(
XMLELEMENT(
"cac:TaxSubtotal",
XMLELEMENT(
"cbc:TaxableAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
SUM(NVL(zl.taxable_amt, 0))
),
XMLELEMENT(
"cbc:TaxAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
SUM(NVL(zl.tax_amt, 0))
),
XMLELEMENT(
"cac:TaxCategory",
XMLELEMENT(
"cbc:ID",
DECODE(NVL(zl.tax_rate, 0), 0, 'E', 'S') -- TODO: Verify eBTax to UNCL 5305 mapping ('S','E','AE','K')
),
XMLELEMENT(
"cbc:Percent",
NVL(zl.tax_rate, 0)
),
XMLELEMENT(
"cac:TaxScheme",
XMLELEMENT(
"cbc:ID",
'VAT'
)
)
)
)
)
FROM zx_lines zl
WHERE zl.trx_id = trx.customer_trx_id
AND zl.application_id = 222
AND zl.entity_code = 'TRANSACTIONS'
AND NVL(zl.cancel_flag, 'N') = 'N'
GROUP BY zl.tax_rate
)
),
----------------------------------------------------------
-- LEGAL MONETARY TOTAL (cac:LegalMonetaryTotal)
----------------------------------------------------------
XMLELEMENT(
"cac:LegalMonetaryTotal",
XMLELEMENT(
"cbc:LineExtensionAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
trx.net_amount
),
XMLELEMENT(
"cbc:TaxExclusiveAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
trx.net_amount
),
XMLELEMENT(
"cbc:TaxInclusiveAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
trx.gross_amount
),
XMLELEMENT(
"cbc:AllowanceTotalAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
0 -- TODO: Verify if header-level allowances apply
),
XMLELEMENT(
"cbc:PayableAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
trx.gross_amount
)
),
----------------------------------------------------------
-- INVOICE LINES (cac:InvoiceLine)
----------------------------------------------------------
(
SELECT XMLAGG(
XMLELEMENT(
"cac:InvoiceLine",
XMLELEMENT(
"cbc:ID",
LPAD(l.line_number, 6, '0')
),
CASE
WHEN l.attribute1 IS NOT NULL THEN
XMLELEMENT("cbc:Note", l.attribute1) -- TODO: Verify line note source (l.attribute1)
ELSE NULL
END,
XMLELEMENT(
"cbc:InvoicedQuantity",
XMLATTRIBUTES(
DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode" -- TODO: Verify UOM to UN/ECE Rec 20 mapping
),
l.quantity_invoiced
),
XMLELEMENT(
"cbc:LineExtensionAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
l.extended_amount
),
XMLELEMENT(
"cac:InvoicePeriod",
XMLELEMENT(
"cbc:EndDate",
TO_CHAR(trx.trx_date, 'YYYY-MM-DD') -- TODO: Verify line delivery/period end date source
)
),
XMLELEMENT(
"cac:Item",
XMLELEMENT(
"cbc:Description",
l.description
),
XMLELEMENT(
"cbc:Name",
l.description -- TODO: Verify if Item Code/Name differs from Description
),
XMLELEMENT(
"cac:BuyersItemIdentification",
XMLELEMENT(
"cbc:ID",
'NULL' -- TODO: Verify Buyer Item ID source (e.g., l.attributeX)
)
),
XMLELEMENT(
"cac:SellersItemIdentification",
XMLELEMENT(
"cbc:ID",
'NULL' -- TODO: Verify Seller Item ID source (e.g., l.attributeX)
)
),
XMLELEMENT(
"cac:StandardItemIdentification",
XMLELEMENT(
"cbc:ID",
XMLATTRIBUTES(
'0160' AS "schemeID"
),
'NULL' -- TODO: Verify Standard Item GTIN/EAN barcode source
)
),
XMLELEMENT(
"cac:ClassifiedTaxCategory",
XMLELEMENT(
"cbc:ID",
DECODE(NVL(line_tax.tax_rate, 0), 0, 'E', 'S') -- TODO: Verify line tax category code
),
XMLELEMENT(
"cbc:Percent",
NVL(line_tax.tax_rate, 0)
),
XMLELEMENT(
"cac:TaxScheme",
XMLELEMENT(
"cbc:ID",
'VAT'
)
)
)
),
XMLELEMENT(
"cac:Price",
XMLELEMENT(
"cbc:PriceAmount",
XMLATTRIBUTES(
trx.invoice_currency_code AS "currencyID"
),
ROUND(
NVL(l.extended_amount, 0) / DECODE(NVL(l.quantity_invoiced, 0), 0, 1, l.quantity_invoiced),
2
)
),
XMLELEMENT(
"cbc:BaseQuantity",
XMLATTRIBUTES(
DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode"
),
1 -- TODO: Verify if BaseQuantity is always 1 or 100
)
)
)
ORDER BY l.line_number
)
FROM ra_customer_trx_lines_all l,
(
SELECT zl.trx_line_id,
MAX(zl.tax_rate) tax_rate
FROM zx_lines zl
WHERE zl.trx_id = trx.customer_trx_id
AND zl.application_id = 222
AND zl.entity_code = 'TRANSACTIONS'
AND NVL(zl.cancel_flag, 'N') = 'N'
GROUP BY zl.trx_line_id
) line_tax
WHERE l.customer_trx_id        = trx.customer_trx_id
AND l.line_type              = 'LINE'
AND line_tax.trx_line_id (+) = l.customer_trx_line_id
)
) AS invoice_xml
FROM (
SELECT rcta.customer_trx_id,
rcta.trx_number,
rcta.trx_date,
apsa.due_date,
rcta.invoice_currency_code,
hca.account_number,
hp.party_name       customer_name_xml,
hl.address1         customer_address, -- TODO: Verify ADDRESS1 vs ADDRESS2 for German StreetName
hl.city             customer_city,
hl.postal_code      customer_postal_code,
hl.country          customer_country_code,
(
SELECT NVL(SUM(NVL(line.extended_amount, 0)), 0)
FROM ra_customer_trx_lines_all line
WHERE line.customer_trx_id = rcta.customer_trx_id
AND line.line_type = 'LINE'
) net_amount,
(
SELECT NVL(SUM(NVL(zl.tax_amt, 0)), 0)
FROM zx_lines zl
WHERE zl.trx_id = rcta.customer_trx_id
AND zl.application_id = 222
AND zl.entity_code = 'TRANSACTIONS'
AND NVL(zl.cancel_flag, 'N') = 'N'
) tax_amount,
(
SELECT NVL(SUM(NVL(line.extended_amount, 0)), 0)
FROM ra_customer_trx_lines_all line
WHERE line.customer_trx_id = rcta.customer_trx_id
AND line.line_type IN ('LINE', 'TAX', 'FREIGHT')
) gross_amount,
flv.description,
flv.attribute1,
flv.attribute2,
flv.attribute3,
flv.attribute4,
flv.attribute5,
flv.attribute6,
flv.attribute7,
flv.attribute8,
flv.attribute9,
flv.attribute11,
flv.attribute15,
hcasa.global_attribute1,
hcasa.global_attribute2,
hcasa.global_attribute3,
hcasa.global_attribute6
FROM ra_customer_trx_all      rcta,
ar_payment_schedules_all apsa,
hz_cust_accounts         hca,
hz_parties               hp,
hz_cust_site_uses_all    hcsu,
hz_cust_acct_sites_all   hcasa,
hz_party_sites           hps,
hz_locations             hl,
fnd_lookup_values        flv
WHERE 1 = 1
-- AND rcta.customer_trx_id = p_invoice_id
AND apsa.customer_trx_id (+)           = rcta.customer_trx_id
AND hca.cust_account_id                = rcta.bill_to_customer_id
AND hp.party_id                        = hca.party_id
AND hcsu.site_use_id                   = rcta.bill_to_site_use_id
AND hcasa.cust_acct_site_id            = hcsu.cust_acct_site_id
AND hps.party_site_id                  = hcasa.party_site_id
AND hl.location_id                     = hps.location_id
AND UPPER(rcta.attribute1)             = UPPER(flv.description) -- TODO: Verify join key between RCTA and German Lookup
AND flv.lookup_type                    = 'HYT_AR_DE_EINVOICE' -- TODO: Verify German FND Lookup Type name
AND flv.language                       = 'US'
AND flv.enabled_flag                   = 'Y'
AND NVL(flv.end_date_active, SYSDATE) >= SYSDATE
AND hcasa.global_attribute_category    = 'JL.DE.ARXCUDCI.Additional' -- TODO: Verify German Customer Site GDF Category
GROUP BY rcta.customer_trx_id,
rcta.trx_number,
rcta.trx_date,
apsa.due_date,
rcta.invoice_currency_code,
hca.account_number,
hp.party_name,
hl.address1,
hl.city,
hl.postal_code,
hl.country,
flv.description,
flv.attribute1,
flv.attribute2,
flv.attribute3,
flv.attribute4,
flv.attribute5,
flv.attribute6,
flv.attribute7,
flv.attribute8,
flv.attribute9,
flv.attribute11,
flv.attribute15,
hcasa.global_attribute1,
hcasa.global_attribute2,
hcasa.global_attribute3,
hcasa.global_attribute6
) trx;
SELECT XMLELEMENT(
    "Invoice",
    XMLATTRIBUTES(
        'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS "xmlns",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns:cac",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' AS "xmlns:cbc",
        'urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2' AS "xmlns:cct",
        'http://www.w3.org/2000/09/xmldsig#' AS "xmlns:ds",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' AS "xmlns:ext",
        'urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2' AS "xmlns:n0",
        'urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2' AS "xmlns:qdt",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2' AS "xmlns:sac",
        'urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2' AS "xmlns:sbc",
        'urn:oasis:names:specification:bdndr:schema:xsd:UnqualifiedDataTypes-1' AS "xmlns:udt",
        'urn:vertexinc:vrbl:ExtensionComponent:1' AS "xmlns:vrbl",
        'http://uri.etsi.org/01903/v1.3.2#' AS "xmlns:xades"
    ),
    ----------------------------------------------------------
    -- ext:UBLExtensions
    ----------------------------------------------------------
    XMLELEMENT("ext:UBLExtensions",XMLELEMENT("ext:UBLExtension",XMLELEMENT("ext:ExtensionContent",XMLELEMENT("vrbl:InvoiceExtension",XMLELEMENT("vrbl:RoutingDetails",
                        XMLELEMENT("vrbl:Sender", 'info@lieferant.com'), -- TODO: Verify Sender details 
                        XMLELEMENT("vrbl:Receiver", 'DE727081482') -- TODO: Verify Receiver details
                    ))))),
    ----------------------------------------------------------
    -- INVOICE HEADER DETAILS
    ----------------------------------------------------------
    XMLELEMENT("cbc:CustomizationID", 'urn:vertexinc:vrbl:billing:1#Invoice#VRBL-Invoice-DE-PeppolBIS-3p0'), -- TODO: Verify CustomizationID
    XMLELEMENT("cbc:ProfileID", 'urn:vertexinc:vrbl:billing:1'), -- TODO: Verify ProfileID
    XMLELEMENT("cbc:ID", trx.trx_number),
    XMLELEMENT("cbc:IssueDate", to_char(trx.trx_date, 'YYYY-MM-DD')),
    XMLELEMENT("cbc:DueDate", to_char(trx.due_date, 'YYYY-MM-DD')),
    XMLELEMENT("cbc:InvoiceTypeCode",XMLATTRIBUTES('00000001' AS "name"),'380'), -- TODO: Verify InvoiceTypeCode
    XMLELEMENT("cbc:Note",nvl(trx.comments, NULL)),
    XMLELEMENT("cbc:DocumentCurrencyCode",trx.invoice_currency_code),
    XMLELEMENT("cbc:BuyerReference",'BUYERREFERENCE BT-10'), -- TODO: Verify BuyerReference (Is this Purchase Order?)
    ----------------------------------------------------------
    -- SUPPLIER (cac:AccountingSupplierParty)
    ----------------------------------------------------------
    XMLELEMENT("cac:AccountingSupplierParty",
            XMLELEMENT("cac:Party",XMLELEMENT("cbc:EndpointID",XMLATTRIBUTES('VRBL:DE:EM' AS "schemeID"),nvl(trx.attribute6, NULL)),
            XMLELEMENT("cac:PartyIdentification",XMLELEMENT("cbc:ID",XMLATTRIBUTES('0088' AS "schemeID"),nvl(trx.attribute11, NULL))),
            XMLELEMENT("cac:PostalAddress",XMLELEMENT("cbc:StreetName",nvl(trx.attribute2, NULL)),
            XMLELEMENT("cbc:CityName",nvl(trx.attribute3, NULL)),
            XMLELEMENT("cbc:PostalZone",'PostalZone'), --TODO: Verify PostalZone
            XMLELEMENT("cac:Country",XMLELEMENT("cbc:IdentificationCode",nvl(trx.attribute5, NULL)))),
            XMLELEMENT("cac:PartyTaxScheme",XMLELEMENT("cbc:CompanyID",nvl(trx.attribute7, NULL)),
            XMLELEMENT("TaxScheme",XMLATTRIBUTES('urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns"),XMLELEMENT("cbc:ID",'VAT'))),
            XMLELEMENT("cac:PartyTaxScheme",XMLELEMENT("cbc:CompanyID",'88888/00072'), -- TODO: Verify 
            XMLELEMENT("TaxScheme",XMLATTRIBUTES('urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns"),XMLELEMENT("cbc:ID",'FC'))),
            XMLELEMENT("cac:PartyLegalEntity",XMLELEMENT("cbc:RegistrationName",nvl(trx.description, NULL))),
            XMLELEMENT("cac:Contact",XMLELEMENT("cbc:Name",'Tim Mustermann'), -- TODO: Verify AR Contact Name
            XMLELEMENT("cbc:Telephone",'0170 66677788'), -- TODO: Verify AR Contact Phone
            XMLELEMENT("cbc:ElectronicMail",'tim.Mustermann@lieferant.com')))), -- TODO: Verify AR Contact Email
    ----------------------------------------------------------
    -- CUSTOMER (cac:AccountingCustomerParty)
    ----------------------------------------------------------
    XMLELEMENT("cac:AccountingCustomerParty",
            XMLELEMENT("cac:Party",XMLELEMENT("cbc:EndpointID",XMLATTRIBUTES('9930' AS "schemeID"),nvl(trx.global_attribute2, NULL)), -- TODO: Verify Customer Endpoint ID source
            XMLELEMENT("cac:PartyIdentification",XMLELEMENT("cbc:ID",'9900880077')), --TODO: Verify PartyIdentification
            XMLELEMENT("cac:PostalAddress",XMLELEMENT("cbc:StreetName",nvl(trx.customer_address, NULL)),
            XMLELEMENT("cbc:CityName",nvl(trx.customer_city, NULL)),
            XMLELEMENT("cbc:PostalZone",trx.customer_postal_code),
            XMLELEMENT("cac:Country",XMLELEMENT("cbc:IdentificationCode",trx.customer_country_code))),
            XMLELEMENT("cac:PartyTaxScheme",XMLELEMENT("cbc:CompanyID",'DE727081482'), -- TODO: Verify VAT ID
            XMLELEMENT("cac:TaxScheme",XMLELEMENT("cbc:ID",'VAT'))),
            XMLELEMENT("cac:PartyLegalEntity",XMLELEMENT("cbc:RegistrationName",trx.customer_name_xml)),
            XMLELEMENT("cac:Contact",XMLELEMENT("cbc:Name",'Herr Test Monteur'), -- TODO: Verify Bill-To Contact Name
            XMLELEMENT("cbc:Telephone",'02173 9364'), -- TODO: Verify Bill-To Contact Phone
            XMLELEMENT("cbc:ElectronicMail",'mike.maier@lieferant.com')))), -- TODO: Verify Bill-To Contact Email
    ----------------------------------------------------------
    -- DELIVERY (cac:Delivery)
    ----------------------------------------------------------
    XMLELEMENT("cac:Delivery",
            XMLELEMENT("cbc:ActualDeliveryDate",to_char(trx.trx_date, 'YYYY-MM-DD')),
            XMLELEMENT("cac:DeliveryLocation",XMLELEMENT("cac:Address",XMLELEMENT("cbc:StreetName",trx.customer_address),
            XMLELEMENT("cbc:CityName",trx.customer_city),
            XMLELEMENT("cbc:PostalZone",'40789'), -- TODO: Verify PostalZone
            XMLELEMENT("cac:Country",XMLELEMENT("cbc:IdentificationCode",'DE')))),
            XMLELEMENT("cac:DeliveryParty",XMLELEMENT("cac:PartyName",XMLELEMENT("cbc:Name",trx.customer_name_xml)),
            XMLELEMENT("cac:PartyTaxScheme",XMLELEMENT("cbc:CompanyID",'DE106008386'), -- TODO: Verify VAT ID
            XMLELEMENT("cac:TaxScheme",XMLELEMENT("cbc:ID",'VAT'))))),
    ----------------------------------------------------------
    -- PAYMENT MEANS (cac:PaymentMeans)
    ----------------------------------------------------------
    XMLELEMENT("cac:PaymentMeans",
        XMLELEMENT("cbc:PaymentMeansCode",XMLATTRIBUTES('Bezahlung per SEPA Überweisung' AS "name"),'58'), -- TODO: Verify
        XMLELEMENT("cbc:PaymentID",'Kundennummer:. 9900880077 Rechnungsnummer:. 6063636771001'), --TODO: Verify
        XMLELEMENT("cac:PayeeFinancialAccount",XMLELEMENT("cbc:ID",nvl(trx.attribute1, NULL)), -- TODO: Verify
        XMLELEMENT("cac:FinancialInstitutionBranch",XMLELEMENT("cbc:ID",nvl(trx.attribute15, NULL))))), -- TODO: Verify
    ----------------------------------------------------------
    -- PAYMENT TERMS (cac:PaymentTerms)
    ----------------------------------------------------------
    XMLELEMENT("cac:PaymentTerms",XMLELEMENT("cbc:Note",NULL), -- TODO: Verify Payment Terms
    XMLELEMENT("cbc:SettlementDiscountPercent",NULL), -- TODO: Verify discount
    XMLELEMENT("SettlementPeriod",XMLATTRIBUTES('urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' AS "xmlns"),
    XMLELEMENT("cbc:DurationMeasure",XMLATTRIBUTES('D' AS "unitCode"),NULL))), -- TODO: Verify discount days
    ----------------------------------------------------------
    -- TAX TOTAL (cac:TaxTotal)
    ----------------------------------------------------------
    XMLELEMENT("cac:TaxTotal",
        XMLELEMENT("cbc:TaxAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),trx.tax_amount),
            (SELECT XMLAGG( XMLELEMENT("cac:TaxSubtotal",
                    XMLELEMENT("cbc:TaxableAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),SUM(NVL(zl.taxable_amt, 0))),
                    XMLELEMENT("cbc:TaxAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),SUM(NVL(zl.tax_amt, 0))),
                    XMLELEMENT("cac:TaxCategory",XMLELEMENT("cbc:ID",DECODE(NVL(zl.tax_rate, 0), 0, 'E', 'S')),
                    XMLELEMENT("cbc:Percent",NVL(zl.tax_rate, 0)),
                    XMLELEMENT("cac:TaxScheme",XMLELEMENT("cbc:ID",'VAT')))))
            FROM zx_lines zl
            WHERE zl.trx_id = trx.customer_trx_id
            AND zl.application_id = 222
            AND zl.entity_code = 'TRANSACTIONS'
            AND NVL(zl.cancel_flag, 'N') = 'N'
            GROUP BY zl.tax_rate)),
    ----------------------------------------------------------
    -- LEGAL MONETARY TOTAL (cac:LegalMonetaryTotal)
    ----------------------------------------------------------
    XMLELEMENT("cac:LegalMonetaryTotal",
            XMLELEMENT("cbc:LineExtensionAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),trx.net_amount),
            XMLELEMENT("cbc:TaxExclusiveAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),trx.net_amount),
            XMLELEMENT("cbc:TaxInclusiveAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),trx.gross_amount),
            XMLELEMENT("cbc:AllowanceTotalAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),0),
            XMLELEMENT("cbc:PayableAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),trx.gross_amount)),
    ----------------------------------------------------------
    -- INVOICE LINES (cac:InvoiceLine)
    ----------------------------------------------------------
    (SELECT XMLAGG(XMLELEMENT("cac:InvoiceLine",XMLELEMENT("cbc:ID",LPAD(l.line_number, 6, '0')),
            CASE WHEN l.attribute1 IS NOT NULL THEN XMLELEMENT("cbc:Note", l.attribute1) ELSE NULL END,
            XMLELEMENT("cbc:InvoicedQuantity",XMLATTRIBUTES(DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode"),l.quantity_invoiced),
            XMLELEMENT("cbc:LineExtensionAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),l.extended_amount),
            XMLELEMENT("cac:InvoicePeriod",XMLELEMENT("cbc:EndDate",TO_CHAR(trx.trx_date, 'YYYY-MM-DD'))),
            XMLELEMENT("cac:Item",XMLELEMENT("cbc:Description",l.description),
            XMLELEMENT("cbc:Name",l.description),
            XMLELEMENT("cac:BuyersItemIdentification",XMLELEMENT("cbc:ID",NULL)),
            XMLELEMENT("cac:SellersItemIdentification",XMLELEMENT("cbc:ID",NULL)),
            XMLELEMENT("cac:StandardItemIdentification",XMLELEMENT("cbc:ID",XMLATTRIBUTES('0160' AS "schemeID"),NULL)),
            XMLELEMENT("cac:ClassifiedTaxCategory",XMLELEMENT("cbc:ID",DECODE(NVL(line_tax.tax_rate, 0), 0, 'E', 'S')),
            XMLELEMENT("cbc:Percent",NVL(line_tax.tax_rate, 0)),
            XMLELEMENT("cac:TaxScheme",XMLELEMENT("cbc:ID",'VAT')))),
            XMLELEMENT("cac:Price",XMLELEMENT("cbc:PriceAmount",XMLATTRIBUTES(trx.invoice_currency_code AS "currencyID"),
                    ROUND(NVL(l.extended_amount, 0) / DECODE(NVL(l.quantity_invoiced, 0), 0, 1, l.quantity_invoiced),2)),
            XMLELEMENT("cbc:BaseQuantity",XMLATTRIBUTES(DECODE(l.uom_code, 'EA', 'H87', 'PCS', 'H87', NVL(l.uom_code, 'H87')) AS "unitCode"),1)))
        ORDER BY l.line_number)
    FROM ra_customer_trx_lines_all l,
    (SELECT zl.trx_line_id,MAX(zl.tax_rate) tax_rate
        FROM zx_lines zl
        WHERE zl.trx_id = trx.customer_trx_id
        AND zl.application_id = 222
        AND zl.entity_code = 'TRANSACTIONS'
        AND NVL(zl.cancel_flag, 'N') = 'N'
        GROUP BY zl.trx_line_id) line_tax
    WHERE l.customer_trx_id        = trx.customer_trx_id
    AND l.line_type              = 'LINE'
    AND line_tax.trx_line_id (+) = l.customer_trx_line_id)            
) AS invoice_xml
  FROM ( SELECT rcta.customer_trx_id,
                rcta.trx_number,
                rcta.trx_date,
                rcta.comments,
                apsa.due_date,
                rcta.invoice_currency_code,
                hca.account_number,
                hp.party_name  customer_name_xml,
                hl.address1    customer_address,
                hl.city        customer_city,
                hl.postal_code customer_postal_code,
                hl.country     customer_country_code,
                ( SELECT nvl(sum(nvl(line.extended_amount, 0)),0)
                      FROM ra_customer_trx_lines_all line
                   WHERE line.customer_trx_id = rcta.customer_trx_id
                     AND line.line_type = 'LINE') net_amount,
                ( SELECT nvl(sum(nvl(zl.tax_amt, 0)),0)
                      FROM zx_lines zl
                   WHERE zl.trx_id = rcta.customer_trx_id
                     AND zl.application_id        = 222
                     AND zl.entity_code           = 'TRANSACTIONS'
                     AND nvl(zl.cancel_flag, 'N') = 'N') tax_amount,
                ( SELECT nvl(sum(nvl(line.extended_amount, 0)),0)
                      FROM ra_customer_trx_lines_all line
                   WHERE line.customer_trx_id = rcta.customer_trx_id
                     AND line.line_type IN ( 'LINE', 'TAX', 'FREIGHT' )) gross_amount,
                flv.description,
                flv.attribute1,
                flv.attribute2,
                flv.attribute3,
                flv.attribute4,
                flv.attribute5,
                flv.attribute6,
                flv.attribute7,
                flv.attribute8,
                flv.attribute9,
                flv.attribute11,
                flv.attribute15,
                hcasa.global_attribute1,
                hcasa.global_attribute2,
                hcasa.global_attribute3,
                hcasa.global_attribute6
                FROM ra_customer_trx_all      rcta,
                     ar_payment_schedules_all apsa,
                     hz_cust_accounts         hca,
                     hz_parties               hp,
                     hz_cust_site_uses_all    hcsu,
                     hz_cust_acct_sites_all   hcasa,
                     hz_party_sites           hps,
                     hz_locations             hl,
                     fnd_lookup_values        flv
        WHERE 1 = 1
          -- AND rcta.customer_trx_id = p_invoice_id
          AND rcta.trx_number          = '20015661'
          AND apsa.customer_trx_id (+) = rcta.customer_trx_id
          AND hca.cust_account_id      = rcta.bill_to_customer_id
          AND hp.party_id              = hca.party_id
          AND hcsu.site_use_id         = rcta.bill_to_site_use_id
          AND hcasa.cust_acct_site_id  = hcsu.cust_acct_site_id
          AND hps.party_site_id        = hcasa.party_site_id
          AND hl.location_id           = hps.location_id
          AND upper(rcta.attribute1)   = upper(flv.description(+)) -- TODO: Verify join key between RCTA and German Lookup
          AND flv.lookup_type (+)      = 'HYT_AR_DE_EINVOICE' -- TODO: Verify German FND Lookup Type name
          AND flv.language (+)         = 'US'
          AND flv.enabled_flag (+)     = 'Y'
          AND nvl(flv.end_date_active(+),sysdate) >= sysdate
          --AND hcasa.global_attribute_category = 'JL.DE.ARXCUDCI.Additional' -- TODO: Verify
        GROUP BY rcta.customer_trx_id,
                 rcta.trx_number,
                 rcta.trx_date,
                 rcta.comments,
                 apsa.due_date,
                 rcta.invoice_currency_code,
                 hca.account_number,
                 hp.party_name,
                 hl.address1,
                 hl.city,
                 hl.postal_code,
                 hl.country,
                 flv.description,
                 flv.attribute1,
                 flv.attribute2,
                 flv.attribute3,
                 flv.attribute4,
                 flv.attribute5,
                 flv.attribute6,
                 flv.attribute7,
                 flv.attribute8,
                 flv.attribute9,
                 flv.attribute11,
                 flv.attribute15,
                 hcasa.global_attribute1,
                 hcasa.global_attribute2,
                 hcasa.global_attribute3,
                 hcasa.global_attribute6
       ) trx;

Leave a Reply

Your email address will not be published. Required fields are marked *