SET SQLBLANKLINES ON
SET DEFINE OFF

-- IDEMPIERE-5059 Import the database view definition into dictionary
-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Dunning_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Dunning_Line_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Invoice_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Invoice_LineTax_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Order_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Order_LineTax_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_PaySelection_Check_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_PaySelection_Remittance_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Project_Details_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_Project_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_RfQResponseLineQty_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_RfQResponseLine_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW C_RfQResponse_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW DD_Order_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW Fact_Acct_Balance
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW M_InOutConfirm_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW M_InOut_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW M_InOut_LineConfirm_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW M_InOut_Line_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Order_BOM_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Order_BOMLine_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Order_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Order_Node_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Order_Workflow_Header_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW PP_Product_BOMLine_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_Fact_Adaxa
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_Fact_Simple
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_PP_Order_Receipt_Issue
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_PP_Order_Storage
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_RequestUpdates_Only
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_Requestupdates_v
;

-- Nov 28, 2021, 11:19:13 PM CET
DROP VIEW RV_RequestUpdates
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW C_Project_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Project_ID, Value, ProjectName, Description, Note, IsSummary, ProjectCategory, Org_Location_ID, TaxID, C_ProjectType_ID, ProjectTypeName, C_Phase_ID, ProjectPhaseName, SalesRep_ID, SalesRep_Name, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, PaymentTerm, PaymentTermNote, POReference, C_Currency_ID, M_PriceList_Version_ID, C_Campaign_ID, PlannedAmt, PlannedQty, PlannedMarginAmt, InvoicedAmt, InvoicedQty, ProjectBalanceAmt, IsCommitment, CommittedAmt, CommittedQty, DateContract, DateFinish, IsCommitCeiling, M_Warehouse_ID, Logo_ID, AD_User_ID, C_BPartner_Location_ID, CopyFrom, C_Project_C_PaymentTerm_ID, GenerateTo, c_project_processed, c_project_processing, ProjectLineLevel, ProjInvoiceRule, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused,
c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, C_ProjectType_AD_Org_ID, c_projecttype_description, c_projecttype_help, c_projecttype_isactive, c_projecttype_projectcategory, C_Phase_AD_Org_ID, C_Phase_C_Phase_ID, C_Phase_C_ProjectType_ID, c_phase_description, c_phase_help, c_phase_isactive, C_Phase_M_Product_ID, c_phase_seqno, StandardQty, SalesRep_AD_Org_ID, SalesRep_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bp_acqusitioncost, salesrep_bp_actuallifevalue, salesrep_bp_ad_language, SalesRep_BP_AD_OrgBP_ID, SalesRep_BP_AD_Org_ID, SalesRep_BP_BPartner_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Dunning_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_C_InvoiceSched_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bp_created, SalesRep_BP_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bp_deliveryrule, salesrep_bp_deliveryviarule, salesrep_bp_description, salesrep_bp_dunninggrace, salesrep_bp_duns, salesrep_bp_firstsale, salesrep_bp_flatdiscount, salesrep_bp_freightcostrule, salesrep_bp_invoicerule, salesrep_bp_isactive, salesrep_bp_iscustomer, salesrep_bp_isdiscountprinted, salesrep_bp_isemployee, salesrep_bp_ismanufacturer, salesrep_bp_isonetime, salesrep_bp_ispotaxexempt, salesrep_bp_isprospect, salesrep_bp_issalesrep,
salesrep_bp_issummary, salesrep_bp_istaxexempt, salesrep_bp_isvendor, SalesRep_BP_Logo_ID, SalesRep_BP_M_DiscountSchm_ID, SalesRep_BP_M_PriceList_ID, salesrep_bp_naics, salesrep_bp_name2, salesrep_bp_numberemployees, salesrep_bp_paymentrule, salesrep_bp_paymentrulepo, SalesRep_BP_PO_DiscountSchm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bp_poreference, salesrep_bp_potentiallifevalue, salesrep_bp_rating, salesrep_bp_referenceno, SalesRep_BP_SalesRep_ID, salesrep_bp_salesvolume, salesrep_bp_sendemail, salesrep_bp_shareofcustomer, salesrep_bp_shelflifeminpct, salesrep_bp_so_creditlimit, salesrep_bp_socreditstatus, salesrep_bp_so_creditused, salesrep_bp_so_description, salesrep_bp_taxid, salesrep_bp_totalopenbalance, salesrep_bp_updated, SalesRep_BP_UpdatedBy, salesrep_bp_url, salesrep_bp_value, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, AD_User_AD_Org_ID, AD_User_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, C_User_Greeting_C_Greeting_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDefault,
IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value) AS SELECT p.ad_client_id AS AD_Client_ID, p.ad_org_id AS AD_Org_ID, p.isactive AS IsActive, p.created AS Created, p.createdby AS CreatedBy, p.updated AS Updated, p.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, p.c_project_id AS C_Project_ID, p.value AS Value, p.name AS ProjectName, p.description AS Description, p.note AS Note, p.issummary AS IsSummary, p.projectcategory AS ProjectCategory, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, p.c_projecttype_id AS C_ProjectType_ID, pjt.name AS ProjectTypeName, p.c_phase_id AS C_Phase_ID, pjp.name AS ProjectPhaseName, p.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, p.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, pt.name AS PaymentTerm, pt.documentnote AS PaymentTermNote, p.poreference AS POReference, p.c_currency_id AS C_Currency_ID, p.m_pricelist_version_id AS M_PriceList_Version_ID, p.c_campaign_id AS C_Campaign_ID, p.plannedamt AS PlannedAmt, p.plannedqty AS PlannedQty, p.plannedmarginamt AS PlannedMarginAmt, p.invoicedamt AS InvoicedAmt, p.invoicedqty AS InvoicedQty, p.projectbalanceamt AS ProjectBalanceAmt, p.iscommitment AS IsCommitment, p.committedamt AS CommittedAmt, p.committedqty AS CommittedQty, p.datecontract AS DateContract, p.datefinish AS DateFinish, p.iscommitceiling AS IsCommitCeiling, p.m_warehouse_id AS M_Warehouse_ID, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, p.ad_user_id AS AD_User_ID, p.c_bpartner_location_id AS C_BPartner_Location_ID, p.copyfrom AS CopyFrom, p.c_paymentterm_id AS C_Project_C_PaymentTerm_ID,
p.generateto AS GenerateTo, p.processed AS c_project_processed, p.processing AS c_project_processing, p.projectlinelevel AS ProjectLineLevel, p.projinvoicerule AS ProjInvoiceRule, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating,
bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, pjt.ad_org_id AS C_ProjectType_AD_Org_ID, pjt.description AS c_projecttype_description, pjt.help AS c_projecttype_help, pjt.isactive AS c_projecttype_isactive, pjt.projectcategory AS c_projecttype_projectcategory, pjp.ad_org_id AS C_Phase_AD_Org_ID, pjp.c_phase_id AS C_Phase_C_Phase_ID, pjp.c_projecttype_id AS C_Phase_C_ProjectType_ID, pjp.description AS c_phase_description, pjp.help AS c_phase_help, pjp.isactive AS c_phase_isactive, pjp.m_product_id AS C_Phase_M_Product_ID, pjp.seqno AS c_phase_seqno, pjp.standardqty AS StandardQty, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created,
u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bp_acqusitioncost, ubp.actuallifetimevalue AS salesrep_bp_actuallifevalue, ubp.ad_language AS salesrep_bp_ad_language, ubp.ad_orgbp_id AS SalesRep_BP_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BP_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BP_BPartner_Parent_ID, ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID, ubp.c_dunning_id AS SalesRep_BP_C_Dunning_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_C_InvoiceSched_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bp_created, ubp.createdby AS SalesRep_BP_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bp_deliveryrule, ubp.deliveryviarule AS salesrep_bp_deliveryviarule, ubp.description AS salesrep_bp_description, ubp.dunninggrace AS salesrep_bp_dunninggrace, ubp.duns AS salesrep_bp_duns, ubp.firstsale AS salesrep_bp_firstsale, ubp.flatdiscount AS salesrep_bp_flatdiscount, ubp.freightcostrule AS salesrep_bp_freightcostrule, ubp.invoicerule AS salesrep_bp_invoicerule, ubp.isactive AS salesrep_bp_isactive, ubp.iscustomer AS salesrep_bp_iscustomer, ubp.isdiscountprinted AS salesrep_bp_isdiscountprinted, ubp.isemployee AS salesrep_bp_isemployee, ubp.ismanufacturer AS salesrep_bp_ismanufacturer, ubp.isonetime AS salesrep_bp_isonetime, ubp.ispotaxexempt AS salesrep_bp_ispotaxexempt, ubp.isprospect AS salesrep_bp_isprospect, ubp.issalesrep AS salesrep_bp_issalesrep, ubp.issummary AS salesrep_bp_issummary,
ubp.istaxexempt AS salesrep_bp_istaxexempt, ubp.isvendor AS salesrep_bp_isvendor, ubp.logo_id AS SalesRep_BP_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_M_DiscountSchm_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bp_naics, ubp.name2 AS salesrep_bp_name2, ubp.numberemployees AS salesrep_bp_numberemployees, ubp.paymentrule AS salesrep_bp_paymentrule, ubp.paymentrulepo AS salesrep_bp_paymentrulepo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountSchm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bp_poreference, ubp.potentiallifetimevalue AS salesrep_bp_potentiallifevalue, ubp.rating AS salesrep_bp_rating, ubp.referenceno AS salesrep_bp_referenceno, ubp.salesrep_id AS SalesRep_BP_SalesRep_ID, ubp.salesvolume AS salesrep_bp_salesvolume, ubp.sendemail AS salesrep_bp_sendemail, ubp.shareofcustomer AS salesrep_bp_shareofcustomer, ubp.shelflifeminpct AS salesrep_bp_shelflifeminpct, ubp.so_creditlimit AS salesrep_bp_so_creditlimit, ubp.socreditstatus AS salesrep_bp_socreditstatus, ubp.so_creditused AS salesrep_bp_so_creditused, ubp.so_description AS salesrep_bp_so_description, ubp.taxid AS salesrep_bp_taxid, ubp.totalopenbalance AS salesrep_bp_totalopenbalance, ubp.updated AS salesrep_bp_updated, ubp.updatedby AS SalesRep_BP_UpdatedBy, ubp.url AS salesrep_bp_url, ubp.value AS salesrep_bp_value, bpg.ad_org_id AS C_Greeting_AD_Org_ID, bpg.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpg.name AS c_greeting_name, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description,
bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcg.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcg.c_greeting_id AS C_User_Greeting_C_Greeting_ID, bpcg.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcg.name AS c_user_greeting_name, bpl.ad_org_id AS C_BP_Location_AD_Org_ID, bpl.c_bpartner_id AS C_BP_Location_C_BPartner_ID, bpl.created AS c_bp_location_created, bpl.createdby AS C_BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bp_location_fax, bpl.isactive AS c_bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bp_location_name, bpl.phone AS c_bp_location_phone, bpl.phone2 AS c_bp_location_phone2, bpl.updated AS c_bp_location_updated, bpl.updatedby AS C_BP_Location_UpdatedBy, pt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_C_PaymentTerm_ID, pt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, pt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value FROM c_project p
LEFT JOIN c_bpartner bp ON p.c_bpartner_id = bp.c_bpartner_id
JOIN ad_orginfo oi ON p.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON p.ad_client_id = ci.ad_client_id
LEFT JOIN c_projecttype pjt ON p.c_projecttype_id = pjt.c_projecttype_id
LEFT JOIN c_phase pjp ON p.c_phase_id = pjp.c_phase_id
LEFT JOIN ad_user u ON p.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN ad_user bpc ON p.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_bpartner_location bpl ON p.c_bpartner_location_id = bpl.c_bpartner_location_id
LEFT JOIN c_paymentterm pt ON p.c_paymentterm_id = pt.c_paymentterm_id
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW C_Project_Details_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Project_ID, C_ProjectLine_ID, Line, PlannedQty, PlannedPrice, PlannedAmt, PlannedMarginAmt, CommittedAmt, M_Product_ID, Name, Description, DocumentNote, UPC, SKU, ProductValue, M_Product_Category_ID, InvoicedAmt, InvoicedQty, CommittedQty, C_Order_ID, C_OrderPO_ID, C_ProjectIssue_ID, C_ProjectPhase_ID, C_ProjectTask_ID, DoPricing, IsPrinted, c_projectline_processed, C_Project_AD_Org_ID, AD_User_ID, C_BPartner_ID, C_BPartner_Location_ID, C_BPartnerSR_ID, C_Campaign_ID, C_Currency_ID, c_project_committedamt, c_project_committedqty, c_project_copyfrom, C_PaymentTerm_ID, C_Phase_ID, C_ProjectType_ID, c_project_created, C_Project_CreatedBy, DateContract, DateFinish, c_project_description, GenerateTo, c_project_invoicedamt, c_project_invoicedqty, c_project_isactive, IsCommitCeiling, IsCommitment, c_project_issummary, M_PriceList_Version_ID, M_Warehouse_ID, c_project_name, c_project_note, c_project_plannedamt, c_project_plannedmarginamt, c_project_plannedqty, POReference, c_project_processed, c_project_processing, ProjectBalanceAmt, ProjectCategory, ProjectLineLevel, ProjInvoiceRule, C_Project_SalesRep_ID, c_project_updated, C_Project_UpdatedBy, c_project_value, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_AttributeSet_ID, M_AttributeSetInstance_ID, M_FreightCategory_ID, M_Locator_ID, M_Product_M_Prod_Category_ID, M_Product_M_Product_ID, m_product_processing, ProductType,
R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight) AS SELECT pl.ad_client_id AS AD_Client_ID, pl.ad_org_id AS AD_Org_ID, pl.isactive AS IsActive, pl.created AS Created, pl.createdby AS CreatedBy, pl.updated AS Updated, pl.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, pj.c_project_id AS C_Project_ID, pl.c_projectline_id AS C_ProjectLine_ID, pl.line AS Line, pl.plannedqty AS PlannedQty, pl.plannedprice AS PlannedPrice, pl.plannedamt AS PlannedAmt, pl.plannedmarginamt AS PlannedMarginAmt, pl.committedamt AS CommittedAmt, pl.m_product_id AS M_Product_ID, COALESCE(p.name, pl.description) AS Name, CASE WHEN p.name IS NOT NULL THEN pl.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, pl.m_product_category_id AS M_Product_Category_ID, pl.invoicedamt AS InvoicedAmt, pl.invoicedqty AS InvoicedQty, pl.committedqty AS CommittedQty, pl.c_order_id AS C_Order_ID, pl.c_orderpo_id AS C_OrderPO_ID, pl.c_projectissue_id AS C_ProjectIssue_ID, pl.c_projectphase_id AS C_ProjectPhase_ID, pl.c_projecttask_id AS C_ProjectTask_ID, pl.dopricing AS DoPricing, pl.isprinted AS IsPrinted, pl.processed AS c_projectline_processed, pj.ad_org_id AS C_Project_AD_Org_ID, pj.ad_user_id AS AD_User_ID, pj.c_bpartner_id AS C_BPartner_ID, pj.c_bpartner_location_id AS C_BPartner_Location_ID, pj.c_bpartnersr_id AS C_BPartnerSR_ID, pj.c_campaign_id AS C_Campaign_ID, pj.c_currency_id AS C_Currency_ID, pj.committedamt AS c_project_committedamt, pj.committedqty AS c_project_committedqty, pj.copyfrom AS c_project_copyfrom, pj.c_paymentterm_id AS C_PaymentTerm_ID, pj.c_phase_id AS C_Phase_ID, pj.c_projecttype_id AS C_ProjectType_ID, pj.created AS c_project_created, pj.createdby AS C_Project_CreatedBy, pj.datecontract AS DateContract,
pj.datefinish AS DateFinish, pj.description AS c_project_description, pj.generateto AS GenerateTo, pj.invoicedamt AS c_project_invoicedamt, pj.invoicedqty AS c_project_invoicedqty, pj.isactive AS c_project_isactive, pj.iscommitceiling AS IsCommitCeiling, pj.iscommitment AS IsCommitment, pj.issummary AS c_project_issummary, pj.m_pricelist_version_id AS M_PriceList_Version_ID, pj.m_warehouse_id AS M_Warehouse_ID, pj.name AS c_project_name, pj.note AS c_project_note, pj.plannedamt AS c_project_plannedamt, pj.plannedmarginamt AS c_project_plannedmarginamt, pj.plannedqty AS c_project_plannedqty, pj.poreference AS POReference, pj.processed AS c_project_processed, pj.processing AS c_project_processing, pj.projectbalanceamt AS ProjectBalanceAmt, pj.projectcategory AS ProjectCategory, pj.projectlinelevel AS ProjectLineLevel, pj.projinvoicerule AS ProjInvoiceRule, pj.salesrep_id AS C_Project_SalesRep_ID, pj.updated AS c_project_updated, pj.updatedby AS C_Project_UpdatedBy, pj.value AS c_project_value, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked,
p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.m_product_id AS M_Product_M_Product_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight FROM c_projectline pl
JOIN c_project pj ON pl.c_project_id = pj.c_project_id
LEFT JOIN m_product p ON pl.m_product_id = p.m_product_id WHERE pl.isprinted = 'Y'
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW RV_Asset_Delivery(A_Asset_Delivery_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, A_Asset_ID, A_Asset_Group_ID, M_Product_ID, GuaranteeDate, AssetServiceDate, C_BPartner_ID, AD_User_ID, MovementDate, SerNo, Lot, VersionNo, M_InOutLine_ID, EMail, MessageID, DeliveryConfirmation, URL, Remote_Addr, Remote_Host, Referrer, Description) AS SELECT ad.a_asset_delivery_id AS A_Asset_Delivery_ID, ad.ad_client_id AS AD_Client_ID, ad.ad_org_id AS AD_Org_ID, ad.isactive AS IsActive, ad.created AS Created, ad.createdby AS CreatedBy, ad.updated AS Updated, ad.updatedby AS UpdatedBy, a.a_asset_id AS A_Asset_ID, a.a_asset_group_id AS A_Asset_Group_ID, a.m_product_id AS M_Product_ID, a.guaranteedate AS GuaranteeDate, a.assetservicedate AS AssetServiceDate, a.c_bpartner_id AS C_BPartner_ID, ad.ad_user_id AS AD_User_ID, ad.movementdate AS MovementDate, ad.serno AS SerNo, ad.lot AS Lot, ad.versionno AS VersionNo, ad.m_inoutline_id AS M_InOutLine_ID, ad.email AS EMail, ad.messageid AS MessageID, ad.deliveryconfirmation AS DeliveryConfirmation, ad.url AS URL, ad.remote_addr AS Remote_Addr, ad.remote_host AS Remote_Host, ad.referrer AS Referrer, ad.description AS Description FROM a_asset_delivery ad
JOIN a_asset a ON a.a_asset_id = ad.a_asset_id
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW RV_Asset_Customer(A_Asset_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, Value, Name, Description, Help, A_Asset_Group_ID, M_Product_ID, SerNo, Lot, VersionNo, GuaranteeDate, AssetServiceDate, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, DeliveryCount) AS SELECT a.a_asset_id AS A_Asset_ID, a.ad_client_id AS AD_Client_ID, a.ad_org_id AS AD_Org_ID, a.isactive AS IsActive, a.created AS Created, a.createdby AS CreatedBy, a.updated AS Updated, a.updatedby AS UpdatedBy, a.value AS Value, a.name AS Name, a.description AS Description, a.help AS Help, a.a_asset_group_id AS A_Asset_Group_ID, a.m_product_id AS M_Product_ID, a.serno AS SerNo, a.lot AS Lot, a.versionno AS VersionNo, a.guaranteedate AS GuaranteeDate, a.assetservicedate AS AssetServiceDate, a.c_bpartner_id AS C_BPartner_ID, a.c_bpartner_location_id AS C_BPartner_Location_ID, a.ad_user_id AS AD_User_ID, ( SELECT count(*) AS count FROM a_asset_delivery ad WHERE a.a_asset_id = ad.a_asset_id) AS DeliveryCount FROM a_asset a WHERE a.c_bpartner_id IS NOT NULL
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW RV_Transaction(M_Transaction_ID, AD_Client_ID, AD_Org_ID, MovementType, MovementDate, MovementQty, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, M_Product_ID, Value, Name, Description, UPC, SKU, C_UOM_ID, M_Product_Category_ID, Classification, Weight, Volume, VersionNo, M_Locator_ID, M_Warehouse_ID, X, Y, Z, M_InventoryLine_ID, M_Inventory_ID, M_MovementLine_ID, M_Movement_ID, M_InOutLine_ID, M_InOut_ID, M_ProductionLine_ID, M_ProductionPlan_ID, M_Production_ID, C_ProjectIssue_ID, C_Project_ID, Line, m_transaction_created, M_Transaction_CreatedBy, m_transaction_isactive, PP_Cost_Collector_ID, m_transaction_updated, M_Transction_UpdatedBy, M_Locator_AD_Org_ID, m_locator_isactive, IsDefault, PriorityNo, m_locator_value, M_Product_AD_Org_ID, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, DescriptionURL, m_product_discontinued, m_product_discontinuedat, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, M_InventoryLine_AD_Org_ID, M_InventoryLine_C_Charge_ID, m_inventoryline_created, M_InventoryLine_CreatedBy, m_inventoryline_description, InventoryType, m_inventoryline_isactive, M_InventoryLine_M_ASI_ID, M_InventoryLine_M_Locator_ID, M_InventoryLine_M_Product_ID, m_inventoryline_processed, QtyBook, QtyCount, QtyCsv, QtyInternalUse, M_InventoryLine_RevLine_ID, m_inventoryline_updated,
M_InventoryLine_UpdatedBy, M_MovementLine_AD_Org_ID, m_movementline_confirmedqty, m_movementline_created, M_MovementLine_CreatedBy, DD_OrderLine_ID, m_movementline_description, m_movementline_isactive, M_MovementLine_M_ASI_ID, M_AttributeSetInstanceTo_ID, M_MovementLine_M_Locator_ID, M_LocatorTo_ID, m_movementline_movementqty, M_MovementLine_M_Product_ID, m_movementline_processed, M_MovementLine_ReversalLn_ID, m_movementline_scrappedqty, m_movementline_targetqty, m_movementline_updated, M_MovementLine_UpdatedBy, M_InOutLine_AD_Org_ID, M_InOutLine_AD_OrgTrx_ID, M_InOutLine_C_Activity_ID, M_InOutLine_C_Campaign_ID, M_InOutLine_C_Charge_ID, m_inoutline_confirmedqty, M_InOutLine_C_OrderLine_ID, M_InOutLine_C_Project_ID, C_ProjectPhase_ID, C_ProjectTask_ID, m_inoutline_created, M_InOutLine_CreatedBy, M_InOutLine_C_UOM_ID, m_inoutline_description, m_inoutline_isactive, IsDescription, IsInvoiced, m_inoutline_line, M_InOutLine_M_ASI_ID, M_InOutLine_M_InOut_ID, m_inoutline_movementqty, M_InOutLine_M_Product_ID, M_InOutLine_M_RMALine_ID, m_inoutline_pickedqty, m_inoutline_processed, m_inoutline_qtyentered, Ref_InOutLine_ID, M_InOutLine_ReversalLine_ID, m_inoutline_scrappedqty, m_inoutline_targetqty, m_inoutline_updated, M_InOutLine_UpdatedBy, M_InOutLine_User1_ID, M_InOutLine_User2_ID, M_ProductionLine_AD_Org_ID, m_productionline_created, M_ProductionLine_CreatedBy, m_productionline_description, m_productionline_isactive, M_ProductionLine_M_ASI_ID, M_ProductionLine_M_Locator_ID, m_productionline_movementqty, M_ProductionLine_M_Product_ID, M_ProductionL_M_ProductionL_ID, m_productionline_processed, m_productionline_updated, M_ProductionLine_UpdatedBy, M_ProductionPlan_AD_Org_ID, m_productionplan_created, M_ProductionPlan_CreatedBy, m_productionplan_description, m_productionplan_isactive, m_productionplan_line, M_ProductionPlan_M_Locator_ID, M_ProductionPlan_M_Product_ID, m_productionplan_processed, m_productionplan_productionqt, m_productionplan_updated,
M_ProductionPlan_UpdatedBy, C_PrjctIssue_AD_Org_ID, C_PrjctIssue_C_ProjectIssue_ID, c_prjctissue_created, C_PrjctIssue_CreatedBy, c_prjctissue_description, c_prjctissue_isactive, C_PrjctIssue_M_ASI_ID, C_PrjctIssue_M_InOutLine_ID, C_PrjctIssue_M_Locator_ID, c_prjctissue_movementdate, c_prjctissue_movementqty, C_PrjctIssue_M_Product_ID, c_prjctissue_posted, c_prjctissue_processed, c_prjctissue_processedon, c_prjctissue_processing, C_PrjctIssue_S_TimeExpLine_ID, c_prjctissue_updated, C_PrjctIssue_UpdatedBy) AS SELECT t.m_transaction_id AS M_Transaction_ID, t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.movementtype AS MovementType, t.movementdate AS MovementDate, t.movementqty AS MovementQty, t.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, t.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.description AS Description, p.upc AS UPC, p.sku AS SKU, p.c_uom_id AS C_UOM_ID, p.m_product_category_id AS M_Product_Category_ID, p.classification AS Classification, p.weight AS Weight, p.volume AS Volume, p.versionno AS VersionNo, t.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, t.m_inventoryline_id AS M_InventoryLine_ID, il.m_inventory_id AS M_Inventory_ID, t.m_movementline_id AS M_MovementLine_ID, ml.m_movement_id AS M_Movement_ID, t.m_inoutline_id AS M_InOutLine_ID, iol.m_inout_id AS M_InOut_ID, t.m_productionline_id AS M_ProductionLine_ID, prdl.m_productionplan_id AS M_ProductionPlan_ID, prdp.m_production_id AS M_Production_ID, t.c_projectissue_id AS C_ProjectIssue_ID, pjl.c_project_id AS C_Project_ID, COALESCE(il.line, ml.line, iol.line, prdl.line, pjl.line) AS Line, t.created AS m_transaction_created, t.createdby AS M_Transaction_CreatedBy, t.isactive AS m_transaction_isactive, t.pp_cost_collector_id AS PP_Cost_Collector_ID,
t.updated AS m_transaction_updated, t.updatedby AS M_Transction_UpdatedBy, l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, p.ad_org_id AS M_Product_AD_Org_ID, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued, p.discontinuedat AS m_product_discontinuedat, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, il.ad_org_id AS M_InventoryLine_AD_Org_ID,
il.c_charge_id AS M_InventoryLine_C_Charge_ID, il.created AS m_inventoryline_created, il.createdby AS M_InventoryLine_CreatedBy, il.description AS m_inventoryline_description, il.inventorytype AS InventoryType, il.isactive AS m_inventoryline_isactive, il.m_attributesetinstance_id AS M_InventoryLine_M_ASI_ID, il.m_locator_id AS M_InventoryLine_M_Locator_ID, il.m_product_id AS M_InventoryLine_M_Product_ID, il.processed AS m_inventoryline_processed, il.qtybook AS QtyBook, il.qtycount AS QtyCount, il.qtycsv AS QtyCsv, il.qtyinternaluse AS QtyInternalUse, il.reversalline_id AS M_InventoryLine_RevLine_ID, il.updated AS m_inventoryline_updated, il.updatedby AS M_InventoryLine_UpdatedBy, ml.ad_org_id AS M_MovementLine_AD_Org_ID, ml.confirmedqty AS m_movementline_confirmedqty, ml.created AS m_movementline_created, ml.createdby AS M_MovementLine_CreatedBy, ml.dd_orderline_id AS DD_OrderLine_ID, ml.description AS m_movementline_description, ml.isactive AS m_movementline_isactive, ml.m_attributesetinstance_id AS M_MovementLine_M_ASI_ID, ml.m_attributesetinstanceto_id AS M_AttributeSetInstanceTo_ID, ml.m_locator_id AS M_MovementLine_M_Locator_ID, ml.m_locatorto_id AS M_LocatorTo_ID, ml.movementqty AS m_movementline_movementqty, ml.m_product_id AS M_MovementLine_M_Product_ID, ml.processed AS m_movementline_processed, ml.reversalline_id AS M_MovementLine_ReversalLn_ID, ml.scrappedqty AS m_movementline_scrappedqty, ml.targetqty AS m_movementline_targetqty, ml.updated AS m_movementline_updated, ml.updatedby AS M_MovementLine_UpdatedBy, iol.ad_org_id AS M_InOutLine_AD_Org_ID, iol.ad_orgtrx_id AS M_InOutLine_AD_OrgTrx_ID, iol.c_activity_id AS M_InOutLine_C_Activity_ID, iol.c_campaign_id AS M_InOutLine_C_Campaign_ID, iol.c_charge_id AS M_InOutLine_C_Charge_ID, iol.confirmedqty AS m_inoutline_confirmedqty, iol.c_orderline_id AS M_InOutLine_C_OrderLine_ID, iol.c_project_id AS M_InOutLine_C_Project_ID, iol.c_projectphase_id AS C_ProjectPhase_ID, iol.c_projecttask_id AS C_ProjectTask_ID,
iol.created AS m_inoutline_created, iol.createdby AS M_InOutLine_CreatedBy, iol.c_uom_id AS M_InOutLine_C_UOM_ID, iol.description AS m_inoutline_description, iol.isactive AS m_inoutline_isactive, iol.isdescription AS IsDescription, iol.isinvoiced AS IsInvoiced, iol.line AS m_inoutline_line, iol.m_attributesetinstance_id AS M_InOutLine_M_ASI_ID, iol.m_inout_id AS M_InOutLine_M_InOut_ID, iol.movementqty AS m_inoutline_movementqty, iol.m_product_id AS M_InOutLine_M_Product_ID, iol.m_rmaline_id AS M_InOutLine_M_RMALine_ID, iol.pickedqty AS m_inoutline_pickedqty, iol.processed AS m_inoutline_processed, iol.qtyentered AS m_inoutline_qtyentered, iol.ref_inoutline_id AS Ref_InOutLine_ID, iol.reversalline_id AS M_InOutLine_ReversalLine_ID, iol.scrappedqty AS m_inoutline_scrappedqty, iol.targetqty AS m_inoutline_targetqty, iol.updated AS m_inoutline_updated, iol.updatedby AS M_InOutLine_UpdatedBy, iol.user1_id AS M_InOutLine_User1_ID, iol.user2_id AS M_InOutLine_User2_ID, prdl.ad_org_id AS M_ProductionLine_AD_Org_ID, prdl.created AS m_productionline_created, prdl.createdby AS M_ProductionLine_CreatedBy, prdl.description AS m_productionline_description, prdl.isactive AS m_productionline_isactive, prdl.m_attributesetinstance_id AS M_ProductionLine_M_ASI_ID, prdl.m_locator_id AS M_ProductionLine_M_Locator_ID, prdl.movementqty AS m_productionline_movementqty, prdl.m_product_id AS M_ProductionLine_M_Product_ID, prdl.m_productionline_id AS M_ProductionL_M_ProductionL_ID, prdl.processed AS m_productionline_processed, prdl.updated AS m_productionline_updated, prdl.updatedby AS M_ProductionLine_UpdatedBy, prdp.ad_org_id AS M_ProductionPlan_AD_Org_ID, prdp.created AS m_productionplan_created, prdp.createdby AS M_ProductionPlan_CreatedBy, prdp.description AS m_productionplan_description, prdp.isactive AS m_productionplan_isactive, prdp.line AS m_productionplan_line, prdp.m_locator_id AS M_ProductionPlan_M_Locator_ID, prdp.m_product_id AS M_ProductionPlan_M_Product_ID,
prdp.processed AS m_productionplan_processed, prdp.productionqty AS m_productionplan_productionqt, prdp.updated AS m_productionplan_updated, prdp.updatedby AS M_ProductionPlan_UpdatedBy, pjl.ad_org_id AS C_PrjctIssue_AD_Org_ID, pjl.c_projectissue_id AS C_PrjctIssue_C_ProjectIssue_ID, pjl.created AS c_prjctissue_created, pjl.createdby AS C_PrjctIssue_CreatedBy, pjl.description AS c_prjctissue_description, pjl.isactive AS c_prjctissue_isactive, pjl.m_attributesetinstance_id AS C_PrjctIssue_M_ASI_ID, pjl.m_inoutline_id AS C_PrjctIssue_M_InOutLine_ID, pjl.m_locator_id AS C_PrjctIssue_M_Locator_ID, pjl.movementdate AS c_prjctissue_movementdate, pjl.movementqty AS c_prjctissue_movementqty, pjl.m_product_id AS C_PrjctIssue_M_Product_ID, pjl.posted AS c_prjctissue_posted, pjl.processed AS c_prjctissue_processed, pjl.processedon AS c_prjctissue_processedon, pjl.processing AS c_prjctissue_processing, pjl.s_timeexpenseline_id AS C_PrjctIssue_S_TimeExpLine_ID, pjl.updated AS c_prjctissue_updated, pjl.updatedby AS C_PrjctIssue_UpdatedBy FROM m_transaction t
JOIN m_locator l ON t.m_locator_id = l.m_locator_id
JOIN m_product p ON t.m_product_id = p.m_product_id
LEFT JOIN m_attributesetinstance asi ON t.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_inventoryline il ON t.m_inventoryline_id = il.m_inventoryline_id
LEFT JOIN m_movementline ml ON t.m_movementline_id = ml.m_movementline_id
LEFT JOIN m_inoutline iol ON t.m_inoutline_id = iol.m_inoutline_id
LEFT JOIN m_productionline prdl ON t.m_productionline_id = prdl.m_productionline_id
LEFT JOIN m_productionplan prdp ON prdl.m_productionplan_id = prdp.m_productionplan_id
LEFT JOIN c_projectissue pjl ON t.c_projectissue_id = pjl.c_projectissue_id
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW RV_Storage(AD_Client_ID, AD_Org_ID, M_Product_ID, Value, Name, Description, UPC, SKU, C_UOM_ID, M_Product_Category_ID, Classification, Weight, Volume, VersionNo, GuaranteeDays, GuaranteeDaysMin, M_Locator_ID, M_Warehouse_ID, X, Y, Z, QtyOnHand, QtyReserved, QtyAvailable, QtyOrdered, DateLastInventory, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, ShelfLifeDays, GoodForDays, ShelfLifeRemainingPct, m_storage_isactive, m_storage_updated, M_Storage_UpdatedBy, M_Locator_AD_Org_ID, m_locator_isactive, IsDefault, PriorityNo, m_locator_value, M_Product_AD_Org_ID, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, DescriptionURL, m_product_discontinued, m_product_discontinuedat, DocumentNote, Group1, Group2, Help, ImageURL, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy) AS SELECT s.ad_client_id AS AD_Client_ID, s.ad_org_id AS AD_Org_ID, s.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.description AS Description, p.upc AS UPC, p.sku AS SKU, p.c_uom_id AS C_UOM_ID, p.m_product_category_id AS M_Product_Category_ID, p.classification AS Classification, p.weight AS Weight, p.volume AS Volume, p.versionno AS VersionNo, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, s.m_locator_id AS M_Locator_ID,
l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, s.qtyonhand AS QtyOnHand, s.qtyreserved AS QtyReserved, CASE WHEN COALESCE(lt.isavailableforreservation, 'Y') = 'Y' THEN s.qtyonhand ELSE 0 END - s.qtyreserved AS QtyAvailable, s.qtyordered AS QtyOrdered, s.datelastinventory AS DateLastInventory, s.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, daysbetween(asi.guaranteedate, getdate()) AS ShelfLifeDays, daysbetween(asi.guaranteedate, getdate()) - p.guaranteedaysmin AS GoodForDays, CASE WHEN COALESCE(p.guaranteedays, 0) > 0 THEN round(daysbetween(asi.guaranteedate, getdate()) / p.guaranteedays * 100, 0) ELSE NULL END AS ShelfLifeRemainingPct, s.isactive AS m_storage_isactive, s.updated AS m_storage_updated, s.updatedby AS M_Storage_UpdatedBy, l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, p.ad_org_id AS M_Product_AD_Org_ID, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued, p.discontinuedat AS m_product_discontinuedat, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified,
p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM m_storage s
JOIN m_locator l ON s.m_locator_id = l.m_locator_id
LEFT JOIN m_locatortype lt ON l.m_locatortype_id = lt.m_locatortype_id
JOIN m_product p ON s.m_product_id = p.m_product_id
LEFT JOIN m_attributesetinstance asi ON s.m_attributesetinstance_id = asi.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:13 PM CET
CREATE OR REPLACE VIEW RV_Click_Month(AD_Client_ID, AD_Org_ID, Name, Description, TargetURL, C_BPartner_ID, Created, Counter) AS SELECT cc.ad_client_id AS AD_Client_ID, cc.ad_org_id AS AD_Org_ID, cc.name AS Name, cc.description AS Description, cc.targeturl AS TargetURL, cc.c_bpartner_id AS C_BPartner_ID, firstof(c.created, 'MM') AS Created, count(*) AS Counter FROM w_clickcount cc
JOIN w_click c ON cc.w_clickcount_id = c.w_clickcount_id WHERE cc.isactive = 'Y' GROUP BY cc.ad_client_id, cc.ad_org_id, cc.name, cc.description, cc.targeturl, cc.c_bpartner_id, (firstof(c.created, 'MM'))
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_UnPosted(AD_Client_ID, AD_Org_ID, Created, CreatedBy, Updated, UpdatedBy, IsActive, DocumentNo, DateDoc, DateAcct, AD_Table_ID, Record_ID, IsSOTrx, Posted, Processing, Processed, DocStatus, ProcessedOn) AS SELECT gl_journal.ad_client_id AS AD_Client_ID, gl_journal.ad_org_id AS AD_Org_ID, gl_journal.created AS Created, gl_journal.createdby AS CreatedBy, gl_journal.updated AS Updated, gl_journal.updatedby AS UpdatedBy, gl_journal.isactive AS IsActive, gl_journal.documentno AS DocumentNo, gl_journal.datedoc AS DateDoc, gl_journal.dateacct AS DateAcct, 224 AS AD_Table_ID, gl_journal.gl_journal_id AS Record_ID, 'N' AS IsSOTrx, gl_journal.posted AS Posted, gl_journal.processing AS Processing, gl_journal.processed AS Processed, gl_journal.docstatus AS DocStatus, gl_journal.processedon AS ProcessedOn FROM gl_journal WHERE gl_journal.posted <> 'Y' AND gl_journal.docstatus <> 'VO' UNION SELECT pi.ad_client_id AS AD_Client_ID, pi.ad_org_id AS AD_Org_ID, pi.created AS Created, pi.createdby AS CreatedBy, pi.updated AS Updated, pi.updatedby AS UpdatedBy, pi.isactive AS IsActive, (p.name || '_') || pi.line AS DocumentNo, pi.movementdate AS DateDoc, pi.movementdate AS DateAcct, 623 AS AD_Table_ID, pi.c_projectissue_id AS Record_ID, 'N' AS IsSOTrx, pi.posted AS Posted, pi.processing AS Processing, pi.processed AS Processed, 'CO' AS DocStatus, pi.processedon AS ProcessedOn FROM c_projectissue pi
JOIN c_project p ON pi.c_project_id = p.c_project_id WHERE pi.posted <> 'Y' UNION SELECT c_invoice.ad_client_id AS AD_Client_ID, c_invoice.ad_org_id AS AD_Org_ID, c_invoice.created AS Created, c_invoice.createdby AS CreatedBy, c_invoice.updated AS Updated, c_invoice.updatedby AS UpdatedBy, c_invoice.isactive AS IsActive, c_invoice.documentno AS DocumentNo, c_invoice.dateinvoiced AS DateDoc, c_invoice.dateacct AS DateAcct, 318 AS AD_Table_ID, c_invoice.c_invoice_id AS Record_ID, c_invoice.issotrx AS IsSOTrx, c_invoice.posted AS Posted, c_invoice.processing AS Processing, c_invoice.processed AS Processed, c_invoice.docstatus AS DocStatus, c_invoice.processedon AS ProcessedOn FROM c_invoice WHERE c_invoice.posted <> 'Y' AND c_invoice.docstatus <> 'VO' UNION SELECT m_inout.ad_client_id AS AD_Client_ID, m_inout.ad_org_id AS AD_Org_ID, m_inout.created AS Created, m_inout.createdby AS CreatedBy, m_inout.updated AS Updated, m_inout.updatedby AS UpdatedBy, m_inout.isactive AS IsActive, m_inout.documentno AS DocumentNo, m_inout.movementdate AS DateDoc, m_inout.dateacct AS DateAcct, 319 AS AD_Table_ID, m_inout.m_inout_id AS Record_ID, m_inout.issotrx AS IsSOTrx, m_inout.posted AS Posted, m_inout.processing AS Processing, m_inout.processed AS Processed, m_inout.docstatus AS DocStatus, m_inout.processedon AS ProcessedOn FROM m_inout WHERE m_inout.posted <> 'Y' AND m_inout.docstatus <> 'VO' UNION SELECT m_inventory.ad_client_id AS AD_Client_ID, m_inventory.ad_org_id AS AD_Org_ID, m_inventory.created AS Created, m_inventory.createdby AS CreatedBy, m_inventory.updated AS Updated, m_inventory.updatedby AS UpdatedBy, m_inventory.isactive AS IsActive, m_inventory.documentno AS DocumentNo, m_inventory.movementdate AS DateDoc, m_inventory.movementdate AS DateAcct, 321 AS AD_Table_ID, m_inventory.m_inventory_id AS Record_ID, 'N' AS IsSOTrx, m_inventory.posted AS Posted, m_inventory.processing AS Processing, m_inventory.processed AS Processed, m_inventory.docstatus AS DocStatus,
m_inventory.processedon AS ProcessedOn FROM m_inventory WHERE m_inventory.posted <> 'Y' AND m_inventory.docstatus <> 'VO' UNION SELECT m_movement.ad_client_id AS AD_Client_ID, m_movement.ad_org_id AS AD_Org_ID, m_movement.created AS Created, m_movement.createdby AS CreatedBy, m_movement.updated AS Updated, m_movement.updatedby AS UpdatedBy, m_movement.isactive AS IsActive, m_movement.documentno AS DocumentNo, m_movement.movementdate AS DateDoc, m_movement.movementdate AS DateAcct, 323 AS AD_Table_ID, m_movement.m_movement_id AS Record_ID, 'N' AS IsSOTrx, m_movement.posted AS Posted, m_movement.processing AS Processing, m_movement.processed AS Processed, m_movement.docstatus AS DocStatus, m_movement.processedon AS ProcessedOn FROM m_movement WHERE m_movement.posted <> 'Y' AND m_movement.docstatus <> 'VO' UNION SELECT m_production.ad_client_id AS AD_Client_ID, m_production.ad_org_id AS AD_Org_ID, m_production.created AS Created, m_production.createdby AS CreatedBy, m_production.updated AS Updated, m_production.updatedby AS UpdatedBy, m_production.isactive AS IsActive, m_production.documentno AS DocumentNo, m_production.movementdate AS DateDoc, m_production.movementdate AS DateAcct, 325 AS AD_Table_ID, m_production.m_production_id AS Record_ID, 'N' AS IsSOTrx, m_production.posted AS Posted, m_production.processing AS Processing, m_production.processed AS Processed, m_production.docstatus AS DocStatus, m_production.processedon AS ProcessedOn FROM m_production WHERE m_production.posted <> 'Y' AND m_production.docstatus <> 'VO' UNION SELECT c_cash.ad_client_id AS AD_Client_ID, c_cash.ad_org_id AS AD_Org_ID, c_cash.created AS Created, c_cash.createdby AS CreatedBy, c_cash.updated AS Updated, c_cash.updatedby AS UpdatedBy, c_cash.isactive AS IsActive, c_cash.name AS DocumentNo, c_cash.statementdate AS DateDoc, c_cash.dateacct AS DateAcct, 407 AS AD_Table_ID, c_cash.c_cash_id AS Record_ID, 'N' AS IsSOTrx, c_cash.posted AS Posted, c_cash.processing AS Processing,
c_cash.processed AS Processed, c_cash.docstatus AS DocStatus, c_cash.processedon AS ProcessedOn FROM c_cash WHERE c_cash.posted <> 'Y' AND c_cash.docstatus <> 'VO' UNION SELECT c_payment.ad_client_id AS AD_Client_ID, c_payment.ad_org_id AS AD_Org_ID, c_payment.created AS Created, c_payment.createdby AS CreatedBy, c_payment.updated AS Updated, c_payment.updatedby AS UpdatedBy, c_payment.isactive AS IsActive, c_payment.documentno AS DocumentNo, c_payment.datetrx AS DateDoc, c_payment.dateacct AS DateAcct, 335 AS AD_Table_ID, c_payment.c_payment_id AS Record_ID, 'N' AS IsSOTrx, c_payment.posted AS Posted, c_payment.processing AS Processing, c_payment.processed AS Processed, c_payment.docstatus AS DocStatus, c_payment.processedon AS ProcessedOn FROM c_payment WHERE c_payment.posted <> 'Y' AND c_payment.docstatus <> 'VO' UNION SELECT c_allocationhdr.ad_client_id AS AD_Client_ID, c_allocationhdr.ad_org_id AS AD_Org_ID, c_allocationhdr.created AS Created, c_allocationhdr.createdby AS CreatedBy, c_allocationhdr.updated AS Updated, c_allocationhdr.updatedby AS UpdatedBy, c_allocationhdr.isactive AS IsActive, c_allocationhdr.documentno AS DocumentNo, c_allocationhdr.datetrx AS DateDoc, c_allocationhdr.dateacct AS DateAcct, 735 AS AD_Table_ID, c_allocationhdr.c_allocationhdr_id AS Record_ID, 'N' AS IsSOTrx, c_allocationhdr.posted AS Posted, c_allocationhdr.processing AS Processing, c_allocationhdr.processed AS Processed, c_allocationhdr.docstatus AS DocStatus, c_allocationhdr.processedon AS ProcessedOn FROM c_allocationhdr WHERE c_allocationhdr.posted <> 'Y' AND c_allocationhdr.docstatus <> 'VO' UNION SELECT c_bankstatement.ad_client_id AS AD_Client_ID, c_bankstatement.ad_org_id AS AD_Org_ID, c_bankstatement.created AS Created, c_bankstatement.createdby AS CreatedBy, c_bankstatement.updated AS Updated, c_bankstatement.updatedby AS UpdatedBy, c_bankstatement.isactive AS IsActive, c_bankstatement.name AS DocumentNo, c_bankstatement.statementdate AS DateDoc,
c_bankstatement.statementdate AS DateAcct, 392 AS AD_Table_ID, c_bankstatement.c_bankstatement_id AS Record_ID, 'N' AS IsSOTrx, c_bankstatement.posted AS Posted, c_bankstatement.processing AS Processing, c_bankstatement.processed AS Processed, c_bankstatement.docstatus AS DocStatus, c_bankstatement.processedon AS ProcessedOn FROM c_bankstatement WHERE c_bankstatement.posted <> 'Y' AND c_bankstatement.docstatus <> 'VO' UNION SELECT m_matchinv.ad_client_id AS AD_Client_ID, m_matchinv.ad_org_id AS AD_Org_ID, m_matchinv.created AS Created, m_matchinv.createdby AS CreatedBy, m_matchinv.updated AS Updated, m_matchinv.updatedby AS UpdatedBy, m_matchinv.isactive AS IsActive, m_matchinv.documentno AS DocumentNo, m_matchinv.datetrx AS DateDoc, m_matchinv.dateacct AS DateAcct, 472 AS AD_Table_ID, m_matchinv.m_matchinv_id AS Record_ID, 'N' AS IsSOTrx, m_matchinv.posted AS Posted, m_matchinv.processing AS Processing, m_matchinv.processed AS Processed, 'CO' AS DocStatus, m_matchinv.processedon AS ProcessedOn FROM m_matchinv WHERE m_matchinv.posted <> 'Y' UNION SELECT m_matchpo.ad_client_id AS AD_Client_ID, m_matchpo.ad_org_id AS AD_Org_ID, m_matchpo.created AS Created, m_matchpo.createdby AS CreatedBy, m_matchpo.updated AS Updated, m_matchpo.updatedby AS UpdatedBy, m_matchpo.isactive AS IsActive, m_matchpo.documentno AS DocumentNo, m_matchpo.datetrx AS DateDoc, m_matchpo.dateacct AS DateAcct, 473 AS AD_Table_ID, m_matchpo.m_matchpo_id AS Record_ID, 'N' AS IsSOTrx, m_matchpo.posted AS Posted, m_matchpo.processing AS Processing, m_matchpo.processed AS Processed, 'CO' AS DocStatus, m_matchpo.processedon AS ProcessedOn FROM m_matchpo WHERE m_matchpo.posted <> 'Y' UNION SELECT c_order.ad_client_id AS AD_Client_ID, c_order.ad_org_id AS AD_Org_ID, c_order.created AS Created, c_order.createdby AS CreatedBy, c_order.updated AS Updated, c_order.updatedby AS UpdatedBy, c_order.isactive AS IsActive, c_order.documentno AS DocumentNo, c_order.dateordered AS DateDoc,
c_order.dateacct AS DateAcct, 259 AS AD_Table_ID, c_order.c_order_id AS Record_ID, c_order.issotrx AS IsSOTrx, c_order.posted AS Posted, c_order.processing AS Processing, c_order.processed AS Processed, c_order.docstatus AS DocStatus, c_order.processedon AS ProcessedOn FROM c_order WHERE c_order.posted <> 'Y' AND c_order.docstatus <> 'VO' UNION SELECT m_requisition.ad_client_id AS AD_Client_ID, m_requisition.ad_org_id AS AD_Org_ID, m_requisition.created AS Created, m_requisition.createdby AS CreatedBy, m_requisition.updated AS Updated, m_requisition.updatedby AS UpdatedBy, m_requisition.isactive AS IsActive, m_requisition.documentno AS DocumentNo, m_requisition.datedoc AS DateDoc, m_requisition.datedoc AS DateAcct, 702 AS AD_Table_ID, m_requisition.m_requisition_id AS Record_ID, 'N' AS IsSOTrx, m_requisition.posted AS Posted, m_requisition.processing AS Processing, m_requisition.processed AS Processed, m_requisition.docstatus AS DocStatus, m_requisition.processedon AS ProcessedOn FROM m_requisition WHERE m_requisition.posted <> 'Y' AND m_requisition.docstatus <> 'VO'
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_ProjectCycle(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Cycle_ID, CycleName, C_Currency_ID, C_CycleStep_ID, CycleStepName, SeqNo, RelativeWeight, C_Phase_ID, ProjectPhaseName, C_ProjectType_ID, ProjectTypeName, ProjectValue, ProjectName, Description, Note, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, POReference, SalesRep_ID, M_Warehouse_ID, ProjectCategory, DateContract, DateFinish, IsCommitment, IsCommitCeiling, CommittedQty, CommittedAmt, PlannedQty, PlannedAmt, PlannedMarginAmt, InvoicedAmt, InvoicedQty, ProjectBalanceAmt) AS SELECT p.ad_client_id AS AD_Client_ID, p.ad_org_id AS AD_Org_ID, p.isactive AS IsActive, p.created AS Created, p.createdby AS CreatedBy, p.updated AS Updated, p.updatedby AS UpdatedBy, c.c_cycle_id AS C_Cycle_ID, c.name AS CycleName, c.c_currency_id AS C_Currency_ID, cs.c_cyclestep_id AS C_CycleStep_ID, cs.name AS CycleStepName, cs.seqno AS SeqNo, cs.relativeweight AS RelativeWeight, pp.c_phase_id AS C_Phase_ID, pp.name AS ProjectPhaseName, pt.c_projecttype_id AS C_ProjectType_ID, pt.name AS ProjectTypeName, p.value AS ProjectValue, p.name AS ProjectName, p.description AS Description, p.note AS Note, p.c_bpartner_id AS C_BPartner_ID, p.c_bpartner_location_id AS C_BPartner_Location_ID, p.ad_user_id AS AD_User_ID, p.poreference AS POReference, p.salesrep_id AS SalesRep_ID, p.m_warehouse_id AS M_Warehouse_ID, p.projectcategory AS ProjectCategory, p.datecontract AS DateContract, p.datefinish AS DateFinish, p.iscommitment AS IsCommitment, p.iscommitceiling AS IsCommitCeiling, p.committedqty * cs.relativeweight AS CommittedQty, currencyconvert(p.committedamt, p.c_currency_id, c.c_currency_id, getdate(), 0, p.ad_client_id, p.ad_org_id) * cs.relativeweight AS CommittedAmt, p.plannedqty * cs.relativeweight AS PlannedQty, currencyconvert(p.plannedamt, p.c_currency_id, c.c_currency_id, getdate(), 0, p.ad_client_id, p.ad_org_id) * cs.relativeweight AS PlannedAmt,
currencyconvert(p.plannedmarginamt, p.c_currency_id, c.c_currency_id, getdate(), 0, p.ad_client_id, p.ad_org_id) * cs.relativeweight AS PlannedMarginAmt, currencyconvert(p.invoicedamt, p.c_currency_id, c.c_currency_id, getdate(), 0, p.ad_client_id, p.ad_org_id) * cs.relativeweight AS InvoicedAmt, p.invoicedqty * cs.relativeweight AS InvoicedQty, currencyconvert(p.projectbalanceamt, p.c_currency_id, c.c_currency_id, getdate(), 0, p.ad_client_id, p.ad_org_id) * cs.relativeweight AS ProjectBalanceAmt FROM c_cycle c
JOIN c_cyclestep cs ON c.c_cycle_id = cs.c_cycle_id
JOIN c_cyclephase cp ON cs.c_cyclestep_id = cp.c_cyclestep_id
JOIN c_phase pp ON cp.c_phase_id = pp.c_phase_id
JOIN c_project p ON cp.c_phase_id = p.c_phase_id
JOIN c_projecttype pt ON p.c_projecttype_id = pt.c_projecttype_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_OpenItem(AD_Org_ID, AD_Client_ID, DocumentNo, C_Invoice_ID, C_Order_ID, C_BPartner_ID, IsSOTrx, DateInvoiced, DateAcct, NetDays, DueDate, DaysDue, DiscountDate, DiscountAmt, GrandTotal, PaidAmt, OpenAmt, C_Currency_ID, C_ConversionType_ID, C_PaymentTerm_ID, IsPayScheduleValid, C_InvoicePaySchedule_ID, InvoiceCollectionType, C_Campaign_ID, C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, AD_User_ID, C_BPartner_Location_ID, C_Charge_ID, C_DocType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, ChargeAmt, C_Payment_ID, Created, CreatedBy, DateOrdered, DatePrinted, Description, DocAction, DocStatus, DunningGrace, GenerateTo, IsActive, IsApproved, IsDiscountPrinted, IsInDispute, IsPaid, IsPrinted, IsSelfService, IsTaxIncluded, IsTransferred, M_PriceList_ID, M_RMA_ID, PaymentRule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SalesRep_ID, SendEMail, TotalLines, Updated, UpdatedBy, User1_ID, User2_ID) AS SELECT i.ad_org_id AS AD_Org_ID, i.ad_client_id AS AD_Client_ID, i.documentno AS DocumentNo, i.c_invoice_id AS C_Invoice_ID, i.c_order_id AS C_Order_ID, i.c_bpartner_id AS C_BPartner_ID, i.issotrx AS IsSOTrx, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, p.netdays AS NetDays, paymenttermduedate(i.c_paymentterm_id, i.dateinvoiced) AS DueDate, paymenttermduedays(i.c_paymentterm_id, i.dateinvoiced, getdate()) AS DaysDue, adddays(i.dateinvoiced, p.discountdays) AS DiscountDate, currencyround(i.grandtotal * p.discount / 100, i.c_currency_id, 'N') AS DiscountAmt, i.grandtotal AS GrandTotal, invoicepaid(i.c_invoice_id, i.c_currency_id, 1) AS PaidAmt, invoiceopen(i.c_invoice_id, 0) AS OpenAmt, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.ispayschedulevalid AS IsPayScheduleValid, NULL AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID,
i.c_activity_id AS C_Activity_ID, i.c_invoice_ad_orgtrx_id AS AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_charge_id AS C_Charge_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_payment_id AS C_Payment_ID, i.created AS Created, i.createdby AS CreatedBy, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS Description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.isactive AS IsActive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispaid AS IsPaid, i.isprinted AS IsPrinted, i.c_invoice_isselfservice AS IsSelfService, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS Updated, i.updatedby AS UpdatedBy, i.user1_id AS User1_ID, i.user2_id AS User2_ID FROM rv_c_invoice i
JOIN c_paymentterm p ON i.c_paymentterm_id = p.c_paymentterm_id WHERE invoiceopen(i.c_invoice_id, 0) <> 0 AND i.ispayschedulevalid <> 'Y' AND (i.docstatus IN ('CO', 'CL')) UNION SELECT i.ad_org_id AS AD_Org_ID, i.ad_client_id AS AD_Client_ID, i.documentno AS DocumentNo, i.c_invoice_id AS C_Invoice_ID, i.c_order_id AS C_Order_ID, i.c_bpartner_id AS C_BPartner_ID, i.issotrx AS IsSOTrx, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, daysbetween(ips.duedate, i.dateinvoiced) AS NetDays, ips.duedate AS DueDate, daysbetween(getdate(), ips.duedate) AS DaysDue, ips.discountdate AS DiscountDate, ips.discountamt AS DiscountAmt, ips.dueamt AS GrandTotal, invoicepaid(i.c_invoice_id, i.c_currency_id, 1) AS PaidAmt, invoiceopen(i.c_invoice_id, ips.c_invoicepayschedule_id) AS OpenAmt, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.ispayschedulevalid AS IsPayScheduleValid, ips.c_invoicepayschedule_id AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.c_invoice_ad_orgtrx_id AS AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_charge_id AS C_Charge_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_payment_id AS C_Payment_ID, i.created AS Created, i.createdby AS CreatedBy, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS Description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.isactive AS IsActive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispaid AS IsPaid, i.isprinted AS IsPrinted, i.c_invoice_isselfservice AS IsSelfService, i.istaxincluded AS IsTaxIncluded,
i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS Updated, i.updatedby AS UpdatedBy, i.user1_id AS User1_ID, i.user2_id AS User2_ID FROM rv_c_invoice i
JOIN c_invoicepayschedule ips ON i.c_invoice_id = ips.c_invoice_id WHERE invoiceopen(i.c_invoice_id, ips.c_invoicepayschedule_id) <> 0 AND i.ispayschedulevalid = 'Y' AND (i.docstatus IN ('CO', 'CL')) AND ips.isvalid = 'Y'
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_OrderDetail(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Order_ID, DocStatus, DocAction, C_DocType_ID, IsApproved, IsCreditApproved, SalesRep_ID, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, IsDropShip, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, POReference, C_Currency_ID, IsSOTrx, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_OrderLine_ID, DateOrdered, DatePromised, M_Product_ID, M_Warehouse_ID, M_AttributeSetInstance_ID, ProductAttribute, M_AttributeSet_ID, M_Lot_ID, GuaranteeDate, Lot, SerNo, C_UOM_ID, QtyEntered, QtyOrdered, QtyReserved, QtyDelivered, QtyInvoiced, PriceActual, PriceEntered, QtyToDeliver, QtyToInvoice, NetAmtToInvoice, QtyLostSales, AmtLostSales, Discount, Margin, MarginAmt, C_Order_AD_Org_ID, C_Order_AD_OrgTrx_ID, AmountRefunded, AmountTendered, C_Order_C_Activity_ID, C_Order_C_BPartner_ID, C_Order_C_BPartner_Loc_ID, C_Order_C_Compaign_ID, C_CashLine_ID, C_CashPlanLine_ID, C_Order_C_Charge_ID, C_ConversionType_ID, C_DocTypeTarget_ID, ChargeAmt, CopyFrom, C_Payment_ID, C_PaymentTerm_ID, C_POS_ID, C_Order_C_Project_ID, c_order_created, C_Order_CreatedBy, DateAcct, c_order_dateordered, DatePrinted, c_order_datepromised, DeliveryRule, DeliveryViaRule, c_order_description, DocumentNo, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, c_order_freightamt, FreightCostRule, GrandTotal, InvoiceRule, c_order_isactive, IsDelivered, IsDiscountPrinted, IsInvoiced, IsPayScheduleValid, IsPrinted, IsSelected, IsSelfService, IsTaxIncluded, IsTransferred, Link_Order_ID, M_FreightCategory_ID, M_PriceList_ID, C_Order_M_Shipper_ID, C_Order_M_Warehouse_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, PaymentRule, Posted, PriorityRule, c_order_processed, ProcessedOn, PromotionCode, Ref_Order_ID, SendEMail, TotalLines, c_order_updated, C_Order_UpdatedBy, C_Order_User1_ID, C_Order_User2_ID, Volume, Weight, C_OrderLine_AD_OrgTrx_ID,
C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_Tax_ID, DateDelivered, DateInvoiced, c_orderline_description, c_orderline_discount, c_orderline_freightamt, IsDescription, Line, LineNetAmt, Link_OrderLine_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, PriceCost, PriceLimit, PriceList, c_orderline_processed, Ref_OrderLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, C_OrderLine_User1_ID, C_OrderLine_User2_ID, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy) AS SELECT l.ad_client_id AS AD_Client_ID, l.ad_org_id AS AD_Org_ID, l.isactive AS IsActive, l.created AS Created, l.createdby AS CreatedBy, l.updated AS Updated, l.updatedby AS UpdatedBy, o.c_order_id AS C_Order_ID, o.docstatus AS DocStatus, o.docaction AS DocAction, o.c_doctype_id AS C_DocType_ID, o.isapproved AS IsApproved, o.iscreditapproved AS IsCreditApproved, o.salesrep_id AS SalesRep_ID, o.bill_bpartner_id AS Bill_BPartner_ID, o.bill_location_id AS Bill_Location_ID, o.bill_user_id AS Bill_User_ID, o.isdropship AS IsDropShip, l.c_bpartner_id AS C_BPartner_ID, l.c_bpartner_location_id AS C_BPartner_Location_ID, o.ad_user_id AS AD_User_ID, o.poreference AS POReference, o.c_currency_id AS C_Currency_ID, o.issotrx AS IsSOTrx, l.c_campaign_id AS C_Campaign_ID, l.c_project_id AS C_Project_ID, l.c_activity_id AS C_Activity_ID, l.c_projectphase_id AS C_ProjectPhase_ID, l.c_projecttask_id AS C_ProjectTask_ID, l.c_orderline_id AS C_OrderLine_ID, l.dateordered AS DateOrdered, l.datepromised AS DatePromised, l.m_product_id AS M_Product_ID, l.m_warehouse_id AS M_Warehouse_ID, l.m_attributesetinstance_id AS M_AttributeSetInstance_ID, productattribute(l.m_attributesetinstance_id) AS ProductAttribute, pasi.m_attributeset_id AS M_AttributeSet_ID, pasi.m_lot_id AS M_Lot_ID, pasi.guaranteedate AS GuaranteeDate, pasi.lot AS Lot, pasi.serno AS SerNo, l.c_uom_id AS C_UOM_ID, l.qtyentered AS QtyEntered, l.qtyordered AS QtyOrdered,
l.qtyreserved AS QtyReserved, l.qtydelivered AS QtyDelivered, l.qtyinvoiced AS QtyInvoiced, l.priceactual AS PriceActual, l.priceentered AS PriceEntered, l.qtyordered - l.qtydelivered AS QtyToDeliver, l.qtyordered - l.qtyinvoiced AS QtyToInvoice, (l.qtyordered - l.qtyinvoiced) * l.priceactual AS NetAmtToInvoice, l.qtylostsales AS QtyLostSales, l.qtylostsales * l.priceactual AS AmtLostSales, CASE WHEN l.pricelist = 0 THEN 0 ELSE currencyround((l.pricelist - l.priceactual) / l.pricelist * 100, o.c_currency_id, 'N') END AS Discount, CASE WHEN l.pricelimit = 0 THEN 0 ELSE currencyround((l.priceactual - l.pricelimit) / l.pricelimit * 100, o.c_currency_id, 'N') END AS Margin, CASE WHEN l.pricelimit = 0 THEN 0 ELSE (l.priceactual - l.pricelimit) * l.qtydelivered END AS MarginAmt, o.ad_org_id AS C_Order_AD_Org_ID, o.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, o.amountrefunded AS AmountRefunded, o.amounttendered AS AmountTendered, o.c_activity_id AS C_Order_C_Activity_ID, o.c_bpartner_id AS C_Order_C_BPartner_ID, o.c_bpartner_location_id AS C_Order_C_BPartner_Loc_ID, o.c_campaign_id AS C_Order_C_Compaign_ID, o.c_cashline_id AS C_CashLine_ID, o.c_cashplanline_id AS C_CashPlanLine_ID, o.c_charge_id AS C_Order_C_Charge_ID, o.c_conversiontype_id AS C_ConversionType_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.chargeamt AS ChargeAmt, o.copyfrom AS CopyFrom, o.c_payment_id AS C_Payment_ID, o.c_paymentterm_id AS C_PaymentTerm_ID, o.c_pos_id AS C_POS_ID, o.c_project_id AS C_Order_C_Project_ID, o.created AS c_order_created, o.createdby AS C_Order_CreatedBy, o.dateacct AS DateAcct, o.dateordered AS c_order_dateordered, o.dateprinted AS DatePrinted, o.datepromised AS c_order_datepromised, o.deliveryrule AS DeliveryRule, o.deliveryviarule AS DeliveryViaRule, o.description AS c_order_description, o.documentno AS DocumentNo, o.dropship_bpartner_id AS DropShip_BPartner_ID, o.dropship_location_id AS DropShip_Location_ID, o.dropship_user_id AS DropShip_User_ID,
o.freightamt AS c_order_freightamt, o.freightcostrule AS FreightCostRule, o.grandtotal AS GrandTotal, o.invoicerule AS InvoiceRule, o.isactive AS c_order_isactive, o.isdelivered AS IsDelivered, o.isdiscountprinted AS IsDiscountPrinted, o.isinvoiced AS IsInvoiced, o.ispayschedulevalid AS IsPayScheduleValid, o.isprinted AS IsPrinted, o.isselected AS IsSelected, o.isselfservice AS IsSelfService, o.istaxincluded AS IsTaxIncluded, o.istransferred AS IsTransferred, o.link_order_id AS Link_Order_ID, o.m_freightcategory_id AS M_FreightCategory_ID, o.m_pricelist_id AS M_PriceList_ID, o.m_shipper_id AS C_Order_M_Shipper_ID, o.m_warehouse_id AS C_Order_M_Warehouse_ID, o.ordertype AS OrderType, o.pay_bpartner_id AS Pay_BPartner_ID, o.pay_location_id AS Pay_Location_ID, o.paymentrule AS PaymentRule, o.posted AS Posted, o.priorityrule AS PriorityRule, o.processed AS c_order_processed, o.processedon AS ProcessedOn, o.promotioncode AS PromotionCode, o.ref_order_id AS Ref_Order_ID, o.sendemail AS SendEMail, o.totallines AS TotalLines, o.updated AS c_order_updated, o.updatedby AS C_Order_UpdatedBy, o.user1_id AS C_Order_User1_ID, o.user2_id AS C_Order_User2_ID, o.volume AS Volume, o.weight AS Weight, l.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, l.c_charge_id AS C_OrderLine_C_Charge_ID, l.c_currency_id AS C_OrderLine_C_Currency_ID, l.c_tax_id AS C_Tax_ID, l.datedelivered AS DateDelivered, l.dateinvoiced AS DateInvoiced, l.description AS c_orderline_description, l.discount AS c_orderline_discount, l.freightamt AS c_orderline_freightamt, l.isdescription AS IsDescription, l.line AS Line, l.linenetamt AS LineNetAmt, l.link_orderline_id AS Link_OrderLine_ID, l.m_promotion_id AS M_Promotion_ID, l.m_shipper_id AS C_OrderLine_M_Shipper_ID, l.pricecost AS PriceCost, l.pricelimit AS PriceLimit, l.pricelist AS PriceList, l.processed AS c_orderline_processed, l.ref_orderline_id AS Ref_OrderLine_ID, l.rramt AS RRAmt, l.rrstartdate AS RRStartDate,
l.s_resourceassignment_id AS S_ResourceAssignment_ID, l.user1_id AS C_OrderLine_User1_ID, l.user2_id AS C_OrderLine_User2_ID, pasi.ad_org_id AS M_ASI_AD_Org_ID, pasi.created AS m_asi_created, pasi.createdby AS M_ASI_CreatedBy, pasi.description AS M_ASI_Description, pasi.isactive AS m_asi_isactive, pasi.updated AS m_asi_updated, pasi.updatedby AS M_ASI_UpdatedBy FROM c_order o
JOIN c_orderline l ON o.c_order_id = l.c_order_id
LEFT JOIN m_attributesetinstance pasi ON l.m_attributesetinstance_id = pasi.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_M_Transaction_Sum(AD_Client_ID, AD_Org_ID, MovementType, M_Warehouse_ID, M_Locator_ID, M_Product_ID, MovementDate, MovementQty) AS SELECT t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.movementtype AS MovementType, l.m_warehouse_id AS M_Warehouse_ID, t.m_locator_id AS M_Locator_ID, t.m_product_id AS M_Product_ID, t.movementdate AS MovementDate, sum(t.movementqty) AS MovementQty FROM m_transaction t,
m_locator l WHERE t.m_locator_id = l.m_locator_id GROUP BY t.ad_client_id, t.ad_org_id, t.movementtype, l.m_warehouse_id, t.m_locator_id, t.m_product_id, t.movementdate
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW M_InOut_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOut_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, C_Order_ID, MovementDate, MovementType, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Description, POReference, DateOrdered, Volume, Weight, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, Logo_ID, AD_OrgTrx_ID, AD_User_ID, C_Activity_ID, C_BPartner_Location_ID, C_Campaign_ID, C_Charge_ID, ChargeAmt, C_Invoice_ID, C_Project_ID, CreateConfirm, CreateFrom, CreatePackage, DateAcct, DatePrinted, DateReceived, DocAction, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, FreightAmt, FreightCostRule, GenerateTo, IsApproved, IsDropShip, IsInDispute, m_inout_isintransit, IsPrinted, M_RMA_ID, NoPackages, PickDate, Posted, Processed, ProcessedOn, Processing, Ref_InOut_ID, Reversal_ID, M_InOut_SalesRep_ID, SendEMail, ShipDate, TrackingNo, User1_ID, User2_ID, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID,
C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, M_Warehouse_AD_Org_ID, m_warehouse_description, m_warehouse__isactive, IsDisallowNegativeInv, m_warehouse_isintransit, M_WarehouseSource_ID, m_warehouse_name, ReplenishmentClass, Separator, m_warehouse_value) AS
SELECT io.ad_client_id AS AD_Client_ID, io.ad_org_id AS AD_Org_ID, io.isactive AS IsActive, io.created AS Created, io.createdby AS CreatedBy, io.updated AS Updated, io.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, io.m_inout_id AS M_InOut_ID, io.issotrx AS IsSOTrx, io.documentno AS DocumentNo, io.docstatus AS DocStatus, io.c_doctype_id AS C_DocType_ID, io.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, io.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, io.c_order_id AS C_Order_ID, io.movementdate AS MovementDate, io.movementtype AS MovementType, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, io.description AS Description, io.poreference AS POReference, io.dateordered AS DateOrdered, io.volume AS Volume, io.weight AS Weight, io.m_shipper_id AS M_Shipper_ID, io.deliveryrule AS DeliveryRule, io.deliveryviarule AS DeliveryViaRule, io.priorityrule AS PriorityRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, io.ad_orgtrx_id AS AD_OrgTrx_ID, io.ad_user_id AS AD_User_ID, io.c_activity_id AS C_Activity_ID, io.c_bpartner_location_id AS C_BPartner_Location_ID, io.c_campaign_id AS C_Campaign_ID, io.c_charge_id AS C_Charge_ID, io.chargeamt AS ChargeAmt, io.c_invoice_id AS C_Invoice_ID, io.c_project_id AS C_Project_ID, io.createconfirm AS CreateConfirm, io.createfrom AS CreateFrom, io.createpackage AS CreatePackage, io.dateacct AS DateAcct, io.dateprinted AS DatePrinted, io.datereceived AS DateReceived, io.docaction AS DocAction, io.dropship_bpartner_id AS DropShip_BPartner_ID,
io.dropship_location_id AS DropShip_Location_ID, io.dropship_user_id AS DropShip_User_ID, io.freightamt AS FreightAmt, io.freightcostrule AS FreightCostRule, io.generateto AS GenerateTo, io.isapproved AS IsApproved, io.isdropship AS IsDropShip, io.isindispute AS IsInDispute, io.isintransit AS m_inout_isintransit, io.isprinted AS IsPrinted, io.m_rma_id AS M_RMA_ID, io.nopackages AS NoPackages, io.pickdate AS PickDate, io.posted AS Posted, io.processed AS Processed, io.processedon AS ProcessedOn, io.processing AS Processing, io.ref_inout_id AS Ref_InOut_ID, io.reversal_id AS Reversal_ID, io.salesrep_id AS M_InOut_SalesRep_ID, io.sendemail AS SendEMail, io.shipdate AS ShipDate, io.trackingno AS TrackingNo, io.user1_id AS User1_ID, io.user2_id AS User2_ID, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary,
bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, bpg.ad_org_id AS C_Greeting_AD_Org_ID, bpg.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpg.name AS c_greeting_name, bpl.ad_org_id AS C_BP_Location_AD_Org_ID, bpl.c_bpartner_id AS C_BP_Location_C_BPartner_ID, bpl.created AS c_bp_location_created, bpl.createdby AS C_BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bp_location_fax, bpl.isactive AS c_bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bp_location_name, bpl.phone AS c_bp_location_phone, bpl.phone2 AS c_bp_location_phone2, bpl.updated AS c_bp_location_updated, bpl.updatedby AS C_BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID,
l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcg.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcg.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcg.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, wh.ad_org_id AS M_Warehouse_AD_Org_ID, wh.description AS m_warehouse_description, wh.isactive AS m_warehouse__isactive, wh.isdisallownegativeinv AS IsDisallowNegativeInv,
wh.isintransit AS m_warehouse_isintransit, wh.m_warehousesource_id AS M_WarehouseSource_ID, wh.name AS m_warehouse_name, wh.replenishmentclass AS ReplenishmentClass, wh.separator AS Separator, wh.value AS m_warehouse_value FROM m_inout io
JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
JOIN c_bpartner bp ON io.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN c_bpartner_location bpl ON io.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON io.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
JOIN ad_orginfo oi ON io.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON io.ad_client_id = ci.ad_client_id
JOIN m_warehouse wh ON io.m_warehouse_id = wh.m_warehouse_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_BPartnerOpen(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_BPartner_ID, C_Currency_ID, Amt, OpenAmt, DateDoc, DaysDue, C_Campaign_ID, C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, C_Charge_ID, C_ConversionType_ID, C_DocType_ID, ChargeAmt, C_Invoice_ID, C_Order_ID, C_Payment_ID, DateAcct, Description, DocStatus, DocumentNo, IsApproved, IsSelfService, Posted, ProcessedOn, Reversal_ID) AS SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.c_bpartner_id AS C_BPartner_ID, i.c_currency_id AS C_Currency_ID, i.grandtotal * i.multiplierap AS Amt, invoiceopen(i.c_invoice_id, i.c_invoicepayschedule_id) * i.multiplierap AS OpenAmt, i.dateinvoiced AS DateDoc, COALESCE(daysbetween(getdate(), ips.duedate), paymenttermduedays(i.c_paymentterm_id, i.dateinvoiced, getdate())) AS DaysDue, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.c_charge_id AS C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_doctype_id AS C_DocType_ID, i.chargeamt AS ChargeAmt, i.c_invoice_id AS C_Invoice_ID, i.c_order_id AS C_Order_ID, i.c_payment_id AS C_Payment_ID, i.dateacct AS DateAcct, i.description AS Description, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.isapproved AS IsApproved, i.isselfservice AS IsSelfService, i.posted AS Posted, i.processedon AS ProcessedOn, i.reversal_id AS Reversal_ID FROM c_invoice_v i
LEFT JOIN c_invoicepayschedule ips ON i.c_invoicepayschedule_id = ips.c_invoicepayschedule_id WHERE i.ispaid = 'N' AND (i.docstatus IN ('CO', 'CL')) UNION SELECT p.ad_client_id AS AD_Client_ID, p.ad_org_id AS AD_Org_ID, p.isactive AS IsActive, p.created AS Created, p.createdby AS CreatedBy, p.updated AS Updated, p.updatedby AS UpdatedBy, p.c_bpartner_id AS C_BPartner_ID, p.c_currency_id AS C_Currency_ID, p.payamt * p.multiplierap * '-1' AS Amt, paymentavailable(p.c_payment_id) * p.multiplierap * '-1' AS OpenAmt, p.datetrx AS DateDoc, NULL AS DaysDue, p.c_campaign_id AS C_Campaign_ID, p.c_project_id AS C_Project_ID, p.c_activity_id AS C_Activity_ID, p.ad_orgtrx_id AS AD_OrgTrx_ID, p.c_charge_id AS C_Charge_ID, p.c_conversiontype_id AS C_ConversionType_ID, p.c_doctype_id AS C_DocType_ID, p.chargeamt AS ChargeAmt, p.c_invoice_id AS C_Invoice_ID, p.c_order_id AS C_Order_ID, p.c_payment_id AS C_Payment_ID, p.dateacct AS DateAcct, p.description AS Description, p.docstatus AS DocStatus, p.documentno AS DocumentNo, p.isapproved AS IsApproved, p.isselfservice AS IsSelfService, p.posted AS Posted, p.processedon AS ProcessedOn, p.reversal_id AS Reversal_ID FROM c_payment_v p WHERE p.isallocated = 'N' AND p.c_bpartner_id IS NOT NULL AND (p.docstatus IN ('CO', 'CL'))
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW C_Invoice_LineTax_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Invoice_ID, C_InvoiceLine_ID, C_Tax_ID, TaxAmt, LineTotalAmt, TaxIndicator, Line, M_Product_ID, QtyInvoiced, QtyEntered, UOMSymbol, Name, Description, DocumentNote, UPC, SKU, ProductValue, ResourceDescription, PriceList, PriceEnteredList, Discount, PriceActual, PriceEntered, LineNetAmt, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_InvoiceLine_AD_OrgTrx_ID, A_Processed, C_Charge_ID, C_OrderLine_ID, C_UOM_ID, IsDescription, c_invoiceline_isprinted, M_InOutLine_ID, M_RMALine_ID, PriceLimit, c_invoiceline_processed, Ref_InvoiceLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, C_InvoiceLine_User1_ID, C_InvoiceLine_User2_ID, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, c_uom_isdefault, c_uom_name, StdPrecision, UOMType, X12DE355, C_Invoice_AD_Org_ID, C_Invoice_AD_OrgTrx_ID, C_Invoice_AD_User_ID, C_Invoice_C_Activity_ID, C_Invoice_C_BPartner_ID, C_BPartner_Location_ID, C_Invoice_C_Campaign_ID, C_Invoice_C_Charge_ID, C_ConversionType_ID, C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, c_invoice_chargeamt, C_Order_ID, C_Payment_ID, C_PaymentTerm_ID, C_Invoice_C_Project_ID, c_invoice_created, C_Invoice_CreatedBy, DateAcct, DateInvoiced, DateOrdered, DatePrinted, c_invoice_description, DocAction, DocStatus, DocumentNo, DunningGrace, GenerateTo, GrandTotal, InvoiceCollectionType, c_invoice_isactive, IsApproved, IsDiscountPrinted, IsInDispute, IsPaid, IsPayScheduleValid, c_invoice_isprinted, c_invoice_isselfservice, IsSOTrx, c_invoice_istaxincluded, IsTransferred, M_PriceList_ID, M_RMA_ID, PaymentRule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SalesRep_ID, SendEMail, TotalLines, c_invoice_updated, C_Invoice_UpdatedBy,
C_Invoice_User1_ID, C_Invoice_User2_ID, C_Tax_AD_Org_ID, AD_Rule_ID, C_Country_ID, C_Region_ID, C_Tax_C_TaxCategory_ID, c_tax_description, c_tax_isactive, c_tax_isdefault, IsDocumentLevel, IsSalesTax, c_tax_issummary, IsTaxExempt, c_tax_name, Parent_Tax_ID, Rate, RequiresTaxCertificate, SOPOType, To_Country_ID, To_Region_ID, ValidFrom, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, M_Product_C_TaxCategory_ID, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Locator_ID, M_Product_M_Prod_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, M_Product_S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, C_Charge_AD_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded, C_BP_Product_AD_Org_ID, C_BP_Product_C_BPartner_ID, c_bp_product_created, C_BP_Product_CreatedBy, c_bp_product_description, c_bp_product_isactive, IsManufacturer, Manufacturer, QualityRating, ShelfLifeMinDays, ShelfLifeMinPct, c_bp_product_updated, C_BP_Product_UpdatedBy, VendorCategory, S_RAssignment_AD_Org_ID, AssignDateFrom, AssignDateTo, s_rassignment_created, s_rassignment_createby, s_rassignment_isactive, IsConfirmed, s_rassignment_name, s_rassignment_qty, S_RAssignment_S_Resource_ID, s_rassignment_updated, S_RAssignment_UpdatedBy, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy,
M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, il.c_tax_id AS C_Tax_ID, il.taxamt AS TaxAmt, il.linetotalamt AS LineTotalAmt, t.taxindicator AS TaxIndicator, il.line AS Line, p.m_product_id AS M_Product_ID, CASE WHEN il.qtyinvoiced <> 0 OR il.m_product_id IS NOT NULL THEN il.qtyinvoiced ELSE NULL END AS QtyInvoiced, CASE WHEN il.qtyentered <> 0 OR il.m_product_id IS NOT NULL THEN il.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN il.qtyentered <> 0 OR il.m_product_id IS NOT NULL THEN uom.uomsymbol ELSE NULL END AS UOMSymbol, COALESCE(c.name, (p.name || COALESCE(productattribute(il.m_attributesetinstance_id), '')), il.description) AS Name, CASE WHEN COALESCE(c.name, p.name) IS NOT NULL THEN il.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, COALESCE(pp.vendorproductno, p.value) AS ProductValue, ra.description AS ResourceDescription, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist <> 0 THEN il.pricelist ELSE NULL END AS PriceList, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist <> 0 AND il.qtyentered <> 0 THEN il.pricelist * il.qtyinvoiced / il.qtyentered ELSE NULL END AS PriceEnteredList, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist > il.priceactual AND il.pricelist <> 0 THEN (il.pricelist - il.priceactual) / il.pricelist * 100 ELSE NULL END AS Discount, CASE WHEN il.priceactual <> 0 OR il.m_product_id IS NOT NULL THEN il.priceactual ELSE NULL END AS PriceActual, CASE WHEN il.priceentered <> 0 OR il.m_product_id IS NOT NULL THEN il.priceentered ELSE NULL END AS PriceEntered,
CASE WHEN il.linenetamt <> 0 OR il.m_product_id IS NOT NULL THEN il.linenetamt ELSE NULL END AS LineNetAmt, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, p.description AS ProductDescription, p.imageurl AS ImageURL, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID, il.ad_orgtrx_id AS C_InvoiceLine_AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_charge_id AS C_Charge_ID, il.c_orderline_id AS C_OrderLine_ID, il.c_uom_id AS C_UOM_ID, il.isdescription AS IsDescription, il.isprinted AS c_invoiceline_isprinted, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS M_RMALine_ID, il.pricelimit AS PriceLimit, il.processed AS c_invoiceline_processed, il.ref_invoiceline_id AS Ref_InvoiceLine_ID, il.rramt AS RRAmt, il.rrstartdate AS RRStartDate, il.s_resourceassignment_id AS S_ResourceAssignment_ID, il.user1_id AS C_InvoiceLine_User1_ID, il.user2_id AS C_InvoiceLine_User2_ID, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.ad_user_id AS C_Invoice_AD_User_ID, i.c_activity_id AS C_Invoice_C_Activity_ID, i.c_bpartner_id AS C_Invoice_C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_campaign_id AS C_Invoice_C_Campaign_ID, i.c_charge_id AS C_Invoice_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID,
i.chargeamt AS c_invoice_chargeamt, i.c_order_id AS C_Order_ID, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_project_id AS C_Invoice_C_Project_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateacct AS DateAcct, i.dateinvoiced AS DateInvoiced, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS c_invoice_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.grandtotal AS GrandTotal, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispaid AS IsPaid, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS c_invoice_isprinted, i.isselfservice AS c_invoice_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS c_invoice_istaxincluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS C_Invoice_User1_ID, i.user2_id AS C_Invoice_User2_ID, t.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, t.description AS c_tax_description, t.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS IsTaxExempt, t.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate, t.requirestaxcertificate AS RequiresTaxCertificate,
t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS To_Region_ID, t.validfrom AS ValidFrom, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, c.ad_org_id AS C_Charge_AD_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID,
c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, c.description AS c_charge_description, c.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded, pp.ad_org_id AS C_BP_Product_AD_Org_ID, pp.c_bpartner_id AS C_BP_Product_C_BPartner_ID, pp.created AS c_bp_product_created, pp.createdby AS C_BP_Product_CreatedBy, pp.description AS c_bp_product_description, pp.isactive AS c_bp_product_isactive, pp.ismanufacturer AS IsManufacturer, pp.manufacturer AS Manufacturer, pp.qualityrating AS QualityRating, pp.shelflifemindays AS ShelfLifeMinDays, pp.shelflifeminpct AS ShelfLifeMinPct, pp.updated AS c_bp_product_updated, pp.updatedby AS C_BP_Product_UpdatedBy, pp.vendorcategory AS VendorCategory, ra.ad_org_id AS S_RAssignment_AD_Org_ID, ra.assigndatefrom AS AssignDateFrom, ra.assigndateto AS AssignDateTo, ra.created AS s_rassignment_created, ra.createdby AS s_rassignment_createby, ra.isactive AS s_rassignment_isactive, ra.isconfirmed AS IsConfirmed, ra.name AS s_rassignment_name, ra.qty AS s_rassignment_qty, ra.s_resource_id AS S_RAssignment_S_Resource_ID, ra.updated AS s_rassignment_updated, ra.updatedby AS S_RAssignment_UpdatedBy, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM c_invoiceline il
JOIN c_uom uom ON il.c_uom_id = uom.c_uom_id
JOIN c_invoice i ON il.c_invoice_id = i.c_invoice_id
LEFT JOIN c_tax t ON il.c_tax_id = t.c_tax_id
LEFT JOIN m_product p ON il.m_product_id = p.m_product_id
LEFT JOIN c_charge c ON il.c_charge_id = c.c_charge_id
LEFT JOIN c_bpartner_product pp ON il.m_product_id = pp.m_product_id AND i.c_bpartner_id = pp.c_bpartner_id
LEFT JOIN s_resourceassignment ra ON il.s_resourceassignment_id = ra.s_resourceassignment_id
LEFT JOIN m_attributesetinstance asi ON il.m_attributesetinstance_id = asi.m_attributesetinstance_id UNION SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, il.c_tax_id AS C_Tax_ID, il.taxamt AS TaxAmt, il.linetotalamt AS LineTotalAmt, t.taxindicator AS TaxIndicator, il.line + bl.line / 100 AS Line, p.m_product_id AS M_Product_ID, CASE WHEN bl.isqtypercentage = 'N' THEN il.qtyinvoiced * bl.qtybom ELSE il.qtyinvoiced * (bl.qtybatch / 100) END AS QtyInvoiced, CASE WHEN bl.isqtypercentage = 'N' THEN il.qtyentered * bl.qtybom ELSE il.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uom.uomsymbol AS UOMSymbol, p.name AS Name, b.description AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, p.description AS ProductDescription, p.imageurl AS ImageURL, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID, il.ad_orgtrx_id AS C_InvoiceLine_AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_charge_id AS C_Charge_ID, il.c_orderline_id AS C_OrderLine_ID, il.c_uom_id AS C_UOM_ID, il.isdescription AS IsDescription, il.isprinted AS c_invoiceline_isprinted, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS M_RMALine_ID, il.pricelimit AS PriceLimit,
il.processed AS c_invoiceline_processed, il.ref_invoiceline_id AS Ref_InvoiceLine_ID, il.rramt AS RRAmt, il.rrstartdate AS RRStartDate, il.s_resourceassignment_id AS S_ResourceAssignment_ID, il.user1_id AS C_InvoiceLine_User1_ID, il.user2_id AS C_InvoiceLine_User2_ID, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID,
NULL AS C_Invoice_User2_ID, t.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, t.description AS c_tax_description, t.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS IsTaxExempt, t.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate, t.requirestaxcertificate AS RequiresTaxCertificate, t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS To_Region_ID, t.validfrom AS ValidFrom, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing,
p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM pp_product_bom b
JOIN c_invoiceline il ON b.m_product_id = il.m_product_id
JOIN m_product bp ON bp.m_product_id = il.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.isinvoiceprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON bl.m_product_id = p.m_product_id
JOIN c_uom uom ON p.c_uom_id = uom.c_uom_id
LEFT JOIN c_tax t ON il.c_tax_id = t.c_tax_id
LEFT JOIN m_attributesetinstance asi ON il.m_attributesetinstance_id = asi.m_attributesetinstance_id UNION SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, NULL AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, NULL AS TaxIndicator, il.line AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, il.description AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID,
NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL,
NULL AS Discontinued, NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID,
NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoiceline il WHERE il.c_uom_id IS NULL UNION SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, i.c_invoice_id AS C_Invoice_ID, NULL AS C_InvoiceLine_ID, NULL AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, NULL AS TaxIndicator, 999998 AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, NULL AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID,
NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created,
NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL, NULL AS Discontinued, NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby,
NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoice i UNION SELECT it.ad_client_id AS AD_Client_ID, it.ad_org_id AS AD_Org_ID, it.isactive AS IsActive, it.created AS Created, it.createdby AS CreatedBy, it.updated AS Updated, it.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, it.c_invoice_id AS C_Invoice_ID, NULL AS C_InvoiceLine_ID, it.c_tax_id AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, t.taxindicator AS TaxIndicator, 999999 AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, t.name AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, CASE WHEN it.istaxincluded = 'Y' THEN it.taxamt ELSE it.taxbaseamt END AS PriceActual, CASE WHEN it.istaxincluded = 'Y' THEN it.taxamt ELSE it.taxbaseamt END AS PriceEntered, CASE WHEN it.istaxincluded = 'Y' THEN NULL ELSE it.taxamt END AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate,
NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel,
NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL, NULL AS Discontinued, NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive,
NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoicetax it
JOIN c_tax t ON it.c_tax_id = t.c_tax_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW C_Order_LineTax_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Order_ID, C_OrderLine_ID, C_Tax_ID, TaxIndicator, C_BPartner_ID, C_BPartner_Location_ID, BPName, C_Location_ID, Line, M_Product_ID, VendorProductNo, QtyOrdered, QtyEntered, UOMSymbol, Name, Description, DocumentNote, UPC, SKU, ProductValue, ResourceDescription, PriceList, PriceEnteredList, Discount, PriceActual, PriceEntered, LineNetAmt, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_OrderLine_AD_OrgTrx_ID, C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_UOM_ID, DateDelivered, DateInvoiced, DateOrdered, c_orderline_datepromised, c_orderline_freightamt, IsDescription, Link_OrderLine_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, C_OrderLine_M_Warehouse_ID, PriceCost, PriceLimit, c_orderline_processed, QtyDelivered, QtyInvoiced, QtyLostSales, QtyReserved, Ref_OrderLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, C_OrderLine_User1_ID, C_OrderLine_User2_ID, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, IsDefault, c_uom_name, StdPrecision, UOMType, X12DE355, C_Order_AD_Org_ID, C_Order_AD_OrgTrx_ID, AD_User_ID, AmountRefunded, AmountTendered, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, C_Order_C_Activity_ID, C_Order_C_BPartner_ID, C_Order_C_Campaign_ID, C_CashLine_ID, C_CashPlanLine_ID, C_Order_C_Charge_ID, C_ConversionType_ID, C_Order_C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, ChargeAmt, CopyFrom, C_Payment_ID, C_PaymentTerm_ID, C_POS_ID, C_Order_C_Project_ID, c_order_created, C_Order_CreatedBy, DateAcct, DatePrinted, c_order_datepromised, DeliveryRule, DeliveryViaRule, c_order_description, DocAction, DocStatus, DocumentNo, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, c_order_freightamt, FreightCostRule, GrandTotal, InvoiceRule, c_order_isactive, IsApproved, IsCreditApproved, IsDelivered, c_order_isdiscountprinted,
c_order_isdropship, IsInvoiced, IsPayScheduleValid, IsPrinted, IsSelected, c_order_isselfservice, IsSOTrx, IsTaxIncluded, IsTransferred, Link_Order_ID, C_Order_M_FreightCategory_ID, C_Order_M_PriceList_ID, C_Order_M_Shipper_ID, C_Order_M_Warehouse_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, c_order_paymentrule, POReference, Posted, PriorityRule, c_order_processed, ProcessedOn, PromotionCode, Ref_Order_ID, SalesRep_ID, c_order_sendemail, TotalLines, c_order_updated, C_Order_UpdatedBy, C_Order_User1_ID, C_Order_User2_ID, c_order_volume, c_order_weight, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_Product_C_UOM_ID, DescriptionURL, m_product_discontinued, m_product_discontinuedat, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, m_product_isdropship, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_AttributeSet_ID, M_AttributeSetInstance_ID, M_Product_M_FreightCategory_ID, M_Locator_ID, M_Product_Product_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, m_product_volume, m_product_weight, M_Product_PO_AD_Org_ID, M_Product_PO_C_BPartner_ID, M_Product_PO_C_Currency_ID, CostPerOrder, m_product_po_created, M_Product_PO_CreatedBy, M_Product_PO_C_UOM_ID, DeliveryTime_Actual, DeliveryTime_Promised, m_product_po_discontinued, m_product_po_discontinuedat, m_product_po_isactive, IsCurrentVendor, m_product_po_manufacturer, M_Product_PO_M_Product_ID, Order_Min, Order_Pack, PriceEffective, PriceLastInv, PriceLastPO, m_product_po_pricelist, PricePO, m_product_po_qualityrating, RoyaltyAmt,
m_product_po_upc, m_product_po_updated, M_Product_PO_UpdatedBy, m_product_po_vendorcategory, S_ResourceAssignment_AD_Org_ID, AssignDateFrom, AssignDateTo, s_resourceassignment_created, s_resourceassignment_createby, s_resourceassignment_isactive, IsConfirmed, s_resourceassignment_name, s_resourceassignment_qty, s_resourceassignment_s_resour, s_resourceassignment_updated, S_ResourceAssignment_UpdatedBy, C_Charge_C_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded, C_BPartner_Product_AD_Org_ID, C_BP_Product_C_BPartner_ID, c_bpartner_product_created, C_BPartner_Product_CreatedBy, c_bpartner_product_description, c_bpartner_product_isactive, IsManufacturer, c_bpartner_product_manufactur, c_bpartner_product_qualityrat, ShelfLifeMinDays, c_bpartner_product_shelflifem, c_bpartner_product_updated, C_BPartner_Product_UpdatedBy, c_bpartner_product_vendorcate, AcqusitionCost, ActualLifeTimeValue, c_bpartner_ad_language, AD_OrgBP_ID, C_BPartner_AD_Org_ID, BPartner_Parent_ID, C_BP_Group_ID, C_Dunning_ID, C_Greeting_ID, C_InvoiceSchedule_ID, C_BPartner_C_PaymentTerm_ID, c_bpartner_created, C_BPartner_CreatedBy, C_BPartner_C_TaxGroup_ID, c_bpartner_deliveryrule, c_bpartner_deliveryviarule, c_bpartner_description, c_bpartner_dunninggrace, DUNS, c_bpartner_firstsale, c_bpartner_flatdiscount, c_bpartner_freightcostrule, c_bpartner_invoicerule, c_bpartner_isactive, IsCustomer, c_bpartner_isdiscountprinted, c_bpartner_isemployee, c_bpartner_ismanufacturer, IsOneTime, IsPOTaxExempt, IsProspect, IsSalesRep, c_bpartner_issummary, c_bpartner_istaxexempt, IsVendor, Logo_ID, M_DiscountSchema_ID, C_BPartner_M_PriceList_ID, NAICS, c_bpartner_name, c_bpartner_name2, NumberEmployees, c_bpartner_paymentrule, PaymentRulePO, PO_DiscountSchema_ID, PO_PaymentTerm_ID, PO_PriceList_ID, c_bpartner_poreference, PotentialLifeTimeValue, c_bpartner_rating,
c_bpartner_referenceno, C_BPartner_SalesRep_ID, SalesVolume, c_bpartner_sendemail, ShareOfCustomer, c_bpartner_shelflifeminpct, SO_CreditLimit, SOCreditStatus, SO_CreditUsed, SO_Description, c_bpartner_taxid, TotalOpenBalance, c_bpartner_updated, C_BPartner_UpdatedBy, c_bpartner_url, c_bpartner_value, C_BPartner_Location_AD_Org_ID, c_bpartner_location_c_bpartne, c_bpartner_location_created, C_BPartner_Location_CreatedBy, C_SalesRegion_ID, c_bpartner_location_fax, c_bpartner_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bpartner_location_name, c_bpartner_location_phone, c_bpartner_location_phone2, c_bpartner_location_updated, C_BPartner_Location_UpdatedBy, C_Tax_AD_Org_ID, AD_Rule_ID, C_Country_ID, C_Region_ID, C_Tax_C_TaxCategory_ID, c_tax_description, c_tax_isactive, c_tax_isdefault, IsDocumentLevel, IsSalesTax, c_tax_issummary, c_tax_istaxexempt, c_tax_name, Parent_Tax_ID, Rate, RequiresTaxCertificate, SOPOType, To_Country_ID, C_Tax_To_Region_ID, ValidFrom) AS SELECT ol.ad_client_id AS AD_Client_ID, ol.ad_org_id AS AD_Org_ID, ol.isactive AS IsActive, ol.created AS Created, ol.createdby AS CreatedBy, ol.updated AS Updated, ol.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, ol.c_order_id AS C_Order_ID, ol.c_orderline_id AS C_OrderLine_ID, ol.c_tax_id AS C_Tax_ID, t.taxindicator AS TaxIndicator, ol.c_bpartner_id AS C_BPartner_ID, ol.c_bpartner_location_id AS C_BPartner_Location_ID, bp.name AS BPName, bpl.c_location_id AS C_Location_ID, ol.line AS Line, p.m_product_id AS M_Product_ID, po.vendorproductno AS VendorProductNo, CASE WHEN ol.qtyordered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.qtyordered ELSE NULL END AS QtyOrdered, CASE WHEN ol.qtyentered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN ol.qtyentered <> 0 OR ol.m_product_id IS NOT NULL THEN uom.uomsymbol ELSE NULL END AS UOMSymbol,
COALESCE(c.name, (p.name || productattribute(ol.m_attributesetinstance_id)), ol.description) AS Name, CASE WHEN COALESCE(c.name, p.name) IS NOT NULL THEN ol.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, COALESCE(pp.vendorproductno, p.value) AS ProductValue, ra.description AS ResourceDescription, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist <> 0 THEN ol.pricelist ELSE NULL END AS PriceList, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist <> 0 AND ol.qtyentered <> 0 THEN ol.pricelist * ol.qtyordered / ol.qtyentered ELSE NULL END AS PriceEnteredList, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist > ol.priceactual AND ol.pricelist <> 0 THEN (ol.pricelist - ol.priceactual) / ol.pricelist * 100 ELSE NULL END AS Discount, CASE WHEN ol.priceactual <> 0 OR ol.m_product_id IS NOT NULL THEN ol.priceactual ELSE NULL END AS PriceActual, CASE WHEN ol.priceentered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.priceentered ELSE NULL END AS PriceEntered, CASE WHEN ol.linenetamt <> 0 OR ol.m_product_id IS NOT NULL THEN ol.linenetamt ELSE NULL END AS LineNetAmt, p.description AS ProductDescription, p.imageurl AS ImageURL, ol.c_campaign_id AS C_Campaign_ID, ol.c_project_id AS C_Project_ID, ol.c_activity_id AS C_Activity_ID, ol.c_projectphase_id AS C_ProjectPhase_ID, ol.c_projecttask_id AS C_ProjectTask_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_uom_id AS C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.freightamt AS c_orderline_freightamt, ol.isdescription AS IsDescription, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_M_Warehouse_ID, ol.pricecost AS PriceCost, ol.pricelimit AS PriceLimit,
ol.processed AS c_orderline_processed, ol.qtydelivered AS QtyDelivered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS IsDefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Order_AD_Org_ID, i.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.amountrefunded AS AmountRefunded, i.amounttendered AS AmountTendered, i.bill_bpartner_id AS Bill_BPartner_ID, i.bill_location_id AS Bill_Location_ID, i.bill_user_id AS Bill_User_ID, i.c_activity_id AS C_Order_C_Activity_ID, i.c_bpartner_id AS C_Order_C_BPartner_ID, i.c_campaign_id AS C_Order_C_Campaign_ID, i.c_cashline_id AS C_CashLine_ID, i.c_cashplanline_id AS C_CashPlanLine_ID, i.c_charge_id AS C_Order_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Order_C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.chargeamt AS ChargeAmt, i.copyfrom AS CopyFrom, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_pos_id AS C_POS_ID, i.c_project_id AS C_Order_C_Project_ID, i.created AS c_order_created, i.createdby AS C_Order_CreatedBy, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.datepromised AS c_order_datepromised, i.deliveryrule AS DeliveryRule, i.deliveryviarule AS DeliveryViaRule, i.description AS c_order_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.dropship_bpartner_id AS DropShip_BPartner_ID, i.dropship_location_id AS DropShip_Location_ID,
i.dropship_user_id AS DropShip_User_ID, i.freightamt AS c_order_freightamt, i.freightcostrule AS FreightCostRule, i.grandtotal AS GrandTotal, i.invoicerule AS InvoiceRule, i.isactive AS c_order_isactive, i.isapproved AS IsApproved, i.iscreditapproved AS IsCreditApproved, i.isdelivered AS IsDelivered, i.isdiscountprinted AS c_order_isdiscountprinted, i.isdropship AS c_order_isdropship, i.isinvoiced AS IsInvoiced, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselected AS IsSelected, i.isselfservice AS c_order_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.link_order_id AS Link_Order_ID, i.m_freightcategory_id AS C_Order_M_FreightCategory_ID, i.m_pricelist_id AS C_Order_M_PriceList_ID, i.m_shipper_id AS C_Order_M_Shipper_ID, i.m_warehouse_id AS C_Order_M_Warehouse_ID, i.ordertype AS OrderType, i.pay_bpartner_id AS Pay_BPartner_ID, i.pay_location_id AS Pay_Location_ID, i.paymentrule AS c_order_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.priorityrule AS PriorityRule, i.processed AS c_order_processed, i.processedon AS ProcessedOn, i.promotioncode AS PromotionCode, i.ref_order_id AS Ref_Order_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS c_order_sendemail, i.totallines AS TotalLines, i.updated AS c_order_updated, i.updatedby AS C_Order_UpdatedBy, i.user1_id AS C_Order_User1_ID, i.user2_id AS C_Order_User2_ID, i.volume AS c_order_volume, i.weight AS c_order_weight, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued, p.discontinuedat AS m_product_discontinuedat, p.group1 AS Group1,
p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS m_product_isdropship, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_Product_M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_Product_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS m_product_volume, p.weight AS m_product_weight, po.ad_org_id AS M_Product_PO_AD_Org_ID, po.c_bpartner_id AS M_Product_PO_C_BPartner_ID, po.c_currency_id AS M_Product_PO_C_Currency_ID, po.costperorder AS CostPerOrder, po.created AS m_product_po_created, po.createdby AS M_Product_PO_CreatedBy, po.c_uom_id AS M_Product_PO_C_UOM_ID, po.deliverytime_actual AS DeliveryTime_Actual, po.deliverytime_promised AS DeliveryTime_Promised, po.discontinued AS m_product_po_discontinued, po.discontinuedat AS m_product_po_discontinuedat, po.isactive AS m_product_po_isactive, po.iscurrentvendor AS IsCurrentVendor, po.manufacturer AS m_product_po_manufacturer,
po.m_product_id AS M_Product_PO_M_Product_ID, po.order_min AS Order_Min, po.order_pack AS Order_Pack, po.priceeffective AS PriceEffective, po.pricelastinv AS PriceLastInv, po.pricelastpo AS PriceLastPO, po.pricelist AS m_product_po_pricelist, po.pricepo AS PricePO, po.qualityrating AS m_product_po_qualityrating, po.royaltyamt AS RoyaltyAmt, po.upc AS m_product_po_upc, po.updated AS m_product_po_updated, po.updatedby AS M_Product_PO_UpdatedBy, po.vendorcategory AS m_product_po_vendorcategory, ra.ad_org_id AS S_ResourceAssignment_AD_Org_ID, ra.assigndatefrom AS AssignDateFrom, ra.assigndateto AS AssignDateTo, ra.created AS s_resourceassignment_created, ra.createdby AS s_resourceassignment_createby, ra.isactive AS s_resourceassignment_isactive, ra.isconfirmed AS IsConfirmed, ra.name AS s_resourceassignment_name, ra.qty AS s_resourceassignment_qty, ra.s_resource_id AS s_resourceassignment_s_resour, ra.updated AS s_resourceassignment_updated, ra.updatedby AS S_ResourceAssignment_UpdatedBy, c.ad_org_id AS C_Charge_C_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID, c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, c.description AS c_charge_description, c.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded, pp.ad_org_id AS C_BPartner_Product_AD_Org_ID, pp.c_bpartner_id AS C_BP_Product_C_BPartner_ID, pp.created AS c_bpartner_product_created, pp.createdby AS C_BPartner_Product_CreatedBy, pp.description AS c_bpartner_product_description, pp.isactive AS c_bpartner_product_isactive, pp.ismanufacturer AS IsManufacturer, pp.manufacturer AS c_bpartner_product_manufactur, pp.qualityrating AS c_bpartner_product_qualityrat, pp.shelflifemindays AS ShelfLifeMinDays, pp.shelflifeminpct AS c_bpartner_product_shelflifem, pp.updated AS c_bpartner_product_updated, pp.updatedby AS C_BPartner_Product_UpdatedBy,
pp.vendorcategory AS c_bpartner_product_vendorcate, bp.acqusitioncost AS AcqusitionCost, bp.actuallifetimevalue AS ActualLifeTimeValue, bp.ad_language AS c_bpartner_ad_language, bp.ad_orgbp_id AS AD_OrgBP_ID, bp.ad_org_id AS C_BPartner_AD_Org_ID, bp.bpartner_parent_id AS BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_Group_ID, bp.c_dunning_id AS C_Dunning_ID, bp.c_greeting_id AS C_Greeting_ID, bp.c_invoiceschedule_id AS C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BPartner_C_PaymentTerm_ID, bp.created AS c_bpartner_created, bp.createdby AS C_BPartner_CreatedBy, bp.c_taxgroup_id AS C_BPartner_C_TaxGroup_ID, bp.deliveryrule AS c_bpartner_deliveryrule, bp.deliveryviarule AS c_bpartner_deliveryviarule, bp.description AS c_bpartner_description, bp.dunninggrace AS c_bpartner_dunninggrace, bp.duns AS DUNS, bp.firstsale AS c_bpartner_firstsale, bp.flatdiscount AS c_bpartner_flatdiscount, bp.freightcostrule AS c_bpartner_freightcostrule, bp.invoicerule AS c_bpartner_invoicerule, bp.isactive AS c_bpartner_isactive, bp.iscustomer AS IsCustomer, bp.isdiscountprinted AS c_bpartner_isdiscountprinted, bp.isemployee AS c_bpartner_isemployee, bp.ismanufacturer AS c_bpartner_ismanufacturer, bp.isonetime AS IsOneTime, bp.ispotaxexempt AS IsPOTaxExempt, bp.isprospect AS IsProspect, bp.issalesrep AS IsSalesRep, bp.issummary AS c_bpartner_issummary, bp.istaxexempt AS c_bpartner_istaxexempt, bp.isvendor AS IsVendor, bp.logo_id AS Logo_ID, bp.m_discountschema_id AS M_DiscountSchema_ID, bp.m_pricelist_id AS C_BPartner_M_PriceList_ID, bp.naics AS NAICS, bp.name AS c_bpartner_name, bp.name2 AS c_bpartner_name2, bp.numberemployees AS NumberEmployees, bp.paymentrule AS c_bpartner_paymentrule, bp.paymentrulepo AS PaymentRulePO, bp.po_discountschema_id AS PO_DiscountSchema_ID, bp.po_paymentterm_id AS PO_PaymentTerm_ID, bp.po_pricelist_id AS PO_PriceList_ID, bp.poreference AS c_bpartner_poreference, bp.potentiallifetimevalue AS PotentialLifeTimeValue, bp.rating AS c_bpartner_rating,
bp.referenceno AS c_bpartner_referenceno, bp.salesrep_id AS C_BPartner_SalesRep_ID, bp.salesvolume AS SalesVolume, bp.sendemail AS c_bpartner_sendemail, bp.shareofcustomer AS ShareOfCustomer, bp.shelflifeminpct AS c_bpartner_shelflifeminpct, bp.so_creditlimit AS SO_CreditLimit, bp.socreditstatus AS SOCreditStatus, bp.so_creditused AS SO_CreditUsed, bp.so_description AS SO_Description, bp.taxid AS c_bpartner_taxid, bp.totalopenbalance AS TotalOpenBalance, bp.updated AS c_bpartner_updated, bp.updatedby AS C_BPartner_UpdatedBy, bp.url AS c_bpartner_url, bp.value AS c_bpartner_value, bpl.ad_org_id AS C_BPartner_Location_AD_Org_ID, bpl.c_bpartner_id AS c_bpartner_location_c_bpartne, bpl.created AS c_bpartner_location_created, bpl.createdby AS C_BPartner_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bpartner_location_fax, bpl.isactive AS c_bpartner_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bpartner_location_name, bpl.phone AS c_bpartner_location_phone, bpl.phone2 AS c_bpartner_location_phone2, bpl.updated AS c_bpartner_location_updated, bpl.updatedby AS C_BPartner_Location_UpdatedBy, t.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, t.description AS c_tax_description, t.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS c_tax_istaxexempt, t.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate, t.requirestaxcertificate AS RequiresTaxCertificate, t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS C_Tax_To_Region_ID, t.validfrom AS ValidFrom FROM c_orderline ol
JOIN c_uom uom ON ol.c_uom_id = uom.c_uom_id
JOIN c_order i ON ol.c_order_id = i.c_order_id
LEFT JOIN m_product p ON ol.m_product_id = p.m_product_id
LEFT JOIN m_product_po po ON p.m_product_id = po.m_product_id AND i.c_bpartner_id = po.c_bpartner_id
LEFT JOIN s_resourceassignment ra ON ol.s_resourceassignment_id = ra.s_resourceassignment_id
LEFT JOIN c_charge c ON ol.c_charge_id = c.c_charge_id
LEFT JOIN c_bpartner_product pp ON ol.m_product_id = pp.m_product_id AND i.c_bpartner_id = pp.c_bpartner_id
JOIN c_bpartner bp ON ol.c_bpartner_id = bp.c_bpartner_id
JOIN c_bpartner_location bpl ON ol.c_bpartner_location_id = bpl.c_bpartner_location_id
LEFT JOIN c_tax t ON ol.c_tax_id = t.c_tax_id UNION SELECT ol.ad_client_id AS AD_Client_ID, ol.ad_org_id AS AD_Org_ID, ol.isactive AS IsActive, ol.created AS Created, ol.createdby AS CreatedBy, ol.updated AS Updated, ol.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, ol.c_order_id AS C_Order_ID, ol.c_orderline_id AS C_OrderLine_ID, ol.c_tax_id AS C_Tax_ID, NULL AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, ol.line + bl.line / 100 AS Line, p.m_product_id AS M_Product_ID, po.vendorproductno AS VendorProductNo, CASE WHEN bl.isqtypercentage = 'N' THEN ol.qtyordered * bl.qtybom ELSE ol.qtyordered * (bl.qtybatch / 100) END AS QtyOrdered, CASE WHEN bl.isqtypercentage = 'N' THEN ol.qtyentered * bl.qtybom ELSE ol.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uom.uomsymbol AS UOMSymbol, p.name AS Name, bl.description AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, p.description AS ProductDescription, p.imageurl AS ImageURL, ol.c_campaign_id AS C_Campaign_ID, ol.c_project_id AS C_Project_ID, ol.c_activity_id AS C_Activity_ID, ol.c_projectphase_id AS C_ProjectPhase_ID, ol.c_projecttask_id AS C_ProjectTask_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_uom_id AS C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.freightamt AS c_orderline_freightamt, ol.isdescription AS IsDescription, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_M_Warehouse_ID,
ol.pricecost AS PriceCost, ol.pricelimit AS PriceLimit, ol.processed AS c_orderline_processed, ol.qtydelivered AS QtyDelivered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS IsDefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Order_AD_Org_ID, i.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.amountrefunded AS AmountRefunded, i.amounttendered AS AmountTendered, i.bill_bpartner_id AS Bill_BPartner_ID, i.bill_location_id AS Bill_Location_ID, i.bill_user_id AS Bill_User_ID, i.c_activity_id AS C_Order_C_Activity_ID, i.c_bpartner_id AS C_Order_C_BPartner_ID, i.c_campaign_id AS C_Order_C_Campaign_ID, i.c_cashline_id AS C_CashLine_ID, i.c_cashplanline_id AS C_CashPlanLine_ID, i.c_charge_id AS C_Order_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Order_C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.chargeamt AS ChargeAmt, i.copyfrom AS CopyFrom, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_pos_id AS C_POS_ID, i.c_project_id AS C_Order_C_Project_ID, i.created AS c_order_created, i.createdby AS C_Order_CreatedBy, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.datepromised AS c_order_datepromised, i.deliveryrule AS DeliveryRule, i.deliveryviarule AS DeliveryViaRule, i.description AS c_order_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.dropship_bpartner_id AS DropShip_BPartner_ID,
i.dropship_location_id AS DropShip_Location_ID, i.dropship_user_id AS DropShip_User_ID, i.freightamt AS c_order_freightamt, i.freightcostrule AS FreightCostRule, i.grandtotal AS GrandTotal, i.invoicerule AS InvoiceRule, i.isactive AS c_order_isactive, i.isapproved AS IsApproved, i.iscreditapproved AS IsCreditApproved, i.isdelivered AS IsDelivered, i.isdiscountprinted AS c_order_isdiscountprinted, i.isdropship AS c_order_isdropship, i.isinvoiced AS IsInvoiced, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselected AS IsSelected, i.isselfservice AS c_order_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.link_order_id AS Link_Order_ID, i.m_freightcategory_id AS C_Order_M_FreightCategory_ID, i.m_pricelist_id AS C_Order_M_PriceList_ID, i.m_shipper_id AS C_Order_M_Shipper_ID, i.m_warehouse_id AS C_Order_M_Warehouse_ID, i.ordertype AS OrderType, i.pay_bpartner_id AS Pay_BPartner_ID, i.pay_location_id AS Pay_Location_ID, i.paymentrule AS c_order_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.priorityrule AS PriorityRule, i.processed AS c_order_processed, i.processedon AS ProcessedOn, i.promotioncode AS PromotionCode, i.ref_order_id AS Ref_Order_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS c_order_sendemail, i.totallines AS TotalLines, i.updated AS c_order_updated, i.updatedby AS C_Order_UpdatedBy, i.user1_id AS C_Order_User1_ID, i.user2_id AS C_Order_User2_ID, i.volume AS c_order_volume, i.weight AS c_order_weight, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued,
p.discontinuedat AS m_product_discontinuedat, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS m_product_isdropship, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_Product_M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_Product_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS m_product_volume, p.weight AS m_product_weight, po.ad_org_id AS M_Product_PO_AD_Org_ID, po.c_bpartner_id AS M_Product_PO_C_BPartner_ID, po.c_currency_id AS M_Product_PO_C_Currency_ID, po.costperorder AS CostPerOrder, po.created AS m_product_po_created, po.createdby AS M_Product_PO_CreatedBy, po.c_uom_id AS M_Product_PO_C_UOM_ID, po.deliverytime_actual AS DeliveryTime_Actual, po.deliverytime_promised AS DeliveryTime_Promised, po.discontinued AS m_product_po_discontinued, po.discontinuedat AS m_product_po_discontinuedat, po.isactive AS m_product_po_isactive, po.iscurrentvendor AS IsCurrentVendor,
po.manufacturer AS m_product_po_manufacturer, po.m_product_id AS M_Product_PO_M_Product_ID, po.order_min AS Order_Min, po.order_pack AS Order_Pack, po.priceeffective AS PriceEffective, po.pricelastinv AS PriceLastInv, po.pricelastpo AS PriceLastPO, po.pricelist AS m_product_po_pricelist, po.pricepo AS PricePO, po.qualityrating AS m_product_po_qualityrating, po.royaltyamt AS RoyaltyAmt, po.upc AS m_product_po_upc, po.updated AS m_product_po_updated, po.updatedby AS M_Product_PO_UpdatedBy, po.vendorcategory AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_description, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID, NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID,
NULL AS C_BPartner_C_PaymentTerm_ID, NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule, NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount, NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule, NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name, NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2,
NULL AS c_bpartner_location_updated, NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS c_tax_istaxexempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM pp_product_bom b
JOIN c_orderline ol ON b.m_product_id = ol.m_product_id
JOIN c_order i ON ol.c_order_id = i.c_order_id
JOIN m_product bp ON bp.m_product_id = ol.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.isinvoiceprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON p.m_product_id = bl.m_product_id
LEFT JOIN m_product_po po ON p.m_product_id = po.m_product_id AND i.c_bpartner_id = po.c_bpartner_id
JOIN c_uom uom ON p.c_uom_id = uom.c_uom_id UNION SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, i.c_order_id AS C_Order_ID, NULL AS C_OrderLine_ID, NULL AS C_Tax_ID, NULL AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, NULL AS Line, NULL AS M_Product_ID, NULL AS VendorProductNo, NULL AS QtyOrdered, NULL AS QtyEntered, NULL AS UOMSymbol, NULL AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_freightamt, NULL AS IsDescription, NULL AS Link_OrderLine_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_M_Warehouse_ID, NULL AS PriceCost, NULL AS PriceLimit, NULL AS c_orderline_processed, NULL AS QtyDelivered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_OrderLine_User1_ID, NULL AS C_OrderLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS IsDefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Order_AD_Org_ID, NULL AS C_Order_AD_OrgTrx_ID,
NULL AS AD_User_ID, NULL AS AmountRefunded, NULL AS AmountTendered, NULL AS Bill_BPartner_ID, NULL AS Bill_Location_ID, NULL AS Bill_User_ID, NULL AS C_Order_C_Activity_ID, NULL AS C_Order_C_BPartner_ID, NULL AS C_Order_C_Campaign_ID, NULL AS C_CashLine_ID, NULL AS C_CashPlanLine_ID, NULL AS C_Order_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Order_C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS ChargeAmt, NULL AS CopyFrom, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_POS_ID, NULL AS C_Order_C_Project_ID, NULL AS c_order_created, NULL AS C_Order_CreatedBy, NULL AS DateAcct, NULL AS DatePrinted, NULL AS c_order_datepromised, NULL AS DeliveryRule, NULL AS DeliveryViaRule, NULL AS c_order_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DropShip_BPartner_ID, NULL AS DropShip_Location_ID, NULL AS DropShip_User_ID, NULL AS c_order_freightamt, NULL AS FreightCostRule, NULL AS GrandTotal, NULL AS InvoiceRule, NULL AS c_order_isactive, NULL AS IsApproved, NULL AS IsCreditApproved, NULL AS IsDelivered, NULL AS c_order_isdiscountprinted, NULL AS c_order_isdropship, NULL AS IsInvoiced, NULL AS IsPayScheduleValid, NULL AS IsPrinted, NULL AS IsSelected, NULL AS c_order_isselfservice, NULL AS IsSOTrx, NULL AS IsTaxIncluded, NULL AS IsTransferred, NULL AS Link_Order_ID, NULL AS C_Order_M_FreightCategory_ID, NULL AS C_Order_M_PriceList_ID, NULL AS C_Order_M_Shipper_ID, NULL AS C_Order_M_Warehouse_ID, NULL AS OrderType, NULL AS Pay_BPartner_ID, NULL AS Pay_Location_ID, NULL AS c_order_paymentrule, NULL AS POReference, NULL AS Posted, NULL AS PriorityRule, NULL AS c_order_processed, NULL AS ProcessedOn, NULL AS PromotionCode, NULL AS Ref_Order_ID, NULL AS SalesRep_ID, NULL AS c_order_sendemail, NULL AS TotalLines, NULL AS c_order_updated, NULL AS C_Order_UpdatedBy, NULL AS C_Order_User1_ID, NULL AS C_Order_User2_ID, NULL AS c_order_volume, NULL AS c_order_weight, NULL AS M_Product_AD_Org_ID,
NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS C_TaxCategory_ID, NULL AS M_Product_C_UOM_ID, NULL AS DescriptionURL, NULL AS m_product_discontinued, NULL AS m_product_discontinuedat, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS m_product_isdropship, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_AttributeSet_ID, NULL AS M_AttributeSetInstance_ID, NULL AS M_Product_M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_Product_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS m_product_volume, NULL AS m_product_weight, NULL AS M_Product_PO_AD_Org_ID, NULL AS M_Product_PO_C_BPartner_ID, NULL AS M_Product_PO_C_Currency_ID, NULL AS CostPerOrder, NULL AS m_product_po_created, NULL AS M_Product_PO_CreatedBy, NULL AS M_Product_PO_C_UOM_ID, NULL AS DeliveryTime_Actual, NULL AS DeliveryTime_Promised, NULL AS m_product_po_discontinued, NULL AS m_product_po_discontinuedat, NULL AS m_product_po_isactive, NULL AS IsCurrentVendor, NULL AS m_product_po_manufacturer, NULL AS M_Product_PO_M_Product_ID, NULL AS Order_Min, NULL AS Order_Pack, NULL AS PriceEffective, NULL AS PriceLastInv, NULL AS PriceLastPO, NULL AS m_product_po_pricelist, NULL AS PricePO, NULL AS m_product_po_qualityrating, NULL AS RoyaltyAmt,
NULL AS m_product_po_upc, NULL AS m_product_po_updated, NULL AS M_Product_PO_UpdatedBy, NULL AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_description, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID, NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID, NULL AS C_BPartner_C_PaymentTerm_ID, NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule, NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount, NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule,
NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name, NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2, NULL AS c_bpartner_location_updated, NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS c_tax_istaxexempt, NULL AS c_tax_name,
NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM c_order i UNION SELECT ot.ad_client_id AS AD_Client_ID, ot.ad_org_id AS AD_Org_ID, ot.isactive AS IsActive, ot.created AS Created, ot.createdby AS CreatedBy, ot.updated AS Updated, ot.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, ot.c_order_id AS C_Order_ID, NULL AS C_OrderLine_ID, ot.c_tax_id AS C_Tax_ID, t.taxindicator AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, NULL AS Line, NULL AS M_Product_ID, NULL AS VendorProductNo, NULL AS QtyOrdered, NULL AS QtyEntered, NULL AS UOMSymbol, t.name AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, CASE WHEN ot.istaxincluded = 'Y' THEN ot.taxamt ELSE ot.taxbaseamt END AS PriceActual, CASE WHEN ot.istaxincluded = 'Y' THEN ot.taxamt ELSE ot.taxbaseamt END AS PriceEntered, CASE WHEN ot.istaxincluded = 'Y' THEN NULL ELSE ot.taxamt END AS LineNetAmt, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_freightamt, NULL AS IsDescription, NULL AS Link_OrderLine_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_M_Warehouse_ID, NULL AS PriceCost, NULL AS PriceLimit, NULL AS c_orderline_processed, NULL AS QtyDelivered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate,
NULL AS S_ResourceAssignment_ID, NULL AS C_OrderLine_User1_ID, NULL AS C_OrderLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS IsDefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Order_AD_Org_ID, NULL AS C_Order_AD_OrgTrx_ID, NULL AS AD_User_ID, NULL AS AmountRefunded, NULL AS AmountTendered, NULL AS Bill_BPartner_ID, NULL AS Bill_Location_ID, NULL AS Bill_User_ID, NULL AS C_Order_C_Activity_ID, NULL AS C_Order_C_BPartner_ID, NULL AS C_Order_C_Campaign_ID, NULL AS C_CashLine_ID, NULL AS C_CashPlanLine_ID, NULL AS C_Order_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Order_C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS ChargeAmt, NULL AS CopyFrom, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_POS_ID, NULL AS C_Order_C_Project_ID, NULL AS c_order_created, NULL AS C_Order_CreatedBy, NULL AS DateAcct, NULL AS DatePrinted, NULL AS c_order_datepromised, NULL AS DeliveryRule, NULL AS DeliveryViaRule, NULL AS c_order_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DropShip_BPartner_ID, NULL AS DropShip_Location_ID, NULL AS DropShip_User_ID, NULL AS c_order_freightamt, NULL AS FreightCostRule, NULL AS GrandTotal, NULL AS InvoiceRule, NULL AS c_order_isactive, NULL AS IsApproved, NULL AS IsCreditApproved, NULL AS IsDelivered, NULL AS c_order_isdiscountprinted, NULL AS c_order_isdropship, NULL AS IsInvoiced, NULL AS IsPayScheduleValid, NULL AS IsPrinted, NULL AS IsSelected, NULL AS c_order_isselfservice, NULL AS IsSOTrx, NULL AS IsTaxIncluded, NULL AS IsTransferred, NULL AS Link_Order_ID, NULL AS C_Order_M_FreightCategory_ID, NULL AS C_Order_M_PriceList_ID, NULL AS C_Order_M_Shipper_ID, NULL AS C_Order_M_Warehouse_ID, NULL AS OrderType, NULL AS Pay_BPartner_ID, NULL AS Pay_Location_ID, NULL AS c_order_paymentrule, NULL AS POReference, NULL AS Posted, NULL AS PriorityRule,
NULL AS c_order_processed, NULL AS ProcessedOn, NULL AS PromotionCode, NULL AS Ref_Order_ID, NULL AS SalesRep_ID, NULL AS c_order_sendemail, NULL AS TotalLines, NULL AS c_order_updated, NULL AS C_Order_UpdatedBy, NULL AS C_Order_User1_ID, NULL AS C_Order_User2_ID, NULL AS c_order_volume, NULL AS c_order_weight, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS C_TaxCategory_ID, NULL AS M_Product_C_UOM_ID, NULL AS DescriptionURL, NULL AS m_product_discontinued, NULL AS m_product_discontinuedat, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS m_product_isdropship, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_AttributeSet_ID, NULL AS M_AttributeSetInstance_ID, NULL AS M_Product_M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_Product_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS m_product_volume, NULL AS m_product_weight, NULL AS M_Product_PO_AD_Org_ID, NULL AS M_Product_PO_C_BPartner_ID, NULL AS M_Product_PO_C_Currency_ID, NULL AS CostPerOrder, NULL AS m_product_po_created, NULL AS M_Product_PO_CreatedBy, NULL AS M_Product_PO_C_UOM_ID, NULL AS DeliveryTime_Actual, NULL AS DeliveryTime_Promised, NULL AS m_product_po_discontinued,
NULL AS m_product_po_discontinuedat, NULL AS m_product_po_isactive, NULL AS IsCurrentVendor, NULL AS m_product_po_manufacturer, NULL AS M_Product_PO_M_Product_ID, NULL AS Order_Min, NULL AS Order_Pack, NULL AS PriceEffective, NULL AS PriceLastInv, NULL AS PriceLastPO, NULL AS m_product_po_pricelist, NULL AS PricePO, NULL AS m_product_po_qualityrating, NULL AS RoyaltyAmt, NULL AS m_product_po_upc, NULL AS m_product_po_updated, NULL AS M_Product_PO_UpdatedBy, NULL AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_description, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID, NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID, NULL AS C_BPartner_C_PaymentTerm_ID,
NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule, NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount, NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule, NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name, NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2, NULL AS c_bpartner_location_updated,
NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS c_tax_istaxexempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM c_ordertax ot
JOIN c_tax t ON ot.c_tax_id = t.c_tax_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_M_Requisition(M_Requisition_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, DocumentNo, Description, Help, AD_User_ID, M_PriceList_ID, M_Warehouse_ID, IsApproved, PriorityRule, DateRequired, TotalLines, DocAction, DocStatus, Processed, M_RequisitionLine_ID, Line, Qty, QtyOrdered, M_Product_ID, LineDescription, PriceActual, LineNetAmt, C_Charge_ID) AS SELECT r.m_requisition_id AS M_Requisition_ID, r.ad_client_id AS AD_Client_ID, r.ad_org_id AS AD_Org_ID, r.isactive AS IsActive, r.created AS Created, r.createdby AS CreatedBy, r.updated AS Updated, r.updatedby AS UpdatedBy, r.documentno AS DocumentNo, r.description AS Description, r.help AS Help, r.ad_user_id AS AD_User_ID, r.m_pricelist_id AS M_PriceList_ID, r.m_warehouse_id AS M_Warehouse_ID, r.isapproved AS IsApproved, r.priorityrule AS PriorityRule, r.daterequired AS DateRequired, r.totallines AS TotalLines, r.docaction AS DocAction, r.docstatus AS DocStatus, r.processed AS Processed, l.m_requisitionline_id AS M_RequisitionLine_ID, l.line AS Line, l.qty AS Qty, CASE WHEN l.c_orderline_id IS NOT NULL THEN l.qty ELSE 0 END AS QtyOrdered, l.m_product_id AS M_Product_ID, l.description AS LineDescription, l.priceactual AS PriceActual, l.linenetamt AS LineNetAmt, l.c_charge_id AS C_Charge_ID FROM m_requisition r
JOIN m_requisitionline l ON r.m_requisition_id = l.m_requisition_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW C_Dunning_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_DunningRun_ID, C_DunningRunEntry_ID, DunningDate, PrintName, DocumentNote, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, SalesRep_ID, SalesRep_Name, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, Postal, Amt, Qty, Note, Logo_ID, C_Dunning_ID, C_DunningRun_C_DunningLevel_ID, c_dunningrun_description, Processed, Processing, SendIt, C_DunningRunEntry_AD_Org_ID, AD_User_ID, C_BPartner_Location_ID, C_Currency_ID, C_DunRunEntry_C_DunLevel_ID, c_dunningrunentry_created, C_DunningRunEntry_CreatedBy, c_dunningrunentry_isactive, c_dunningrunentry_processed, c_dunningrunentry_updated, C_DunningRunEntry_UpdatedBy, C_DunningLevel_AD_Org_ID, C_DunningLevel_C_Dunning_ID, ChargeFee, ChargeInterest, C_PaymentTerm_ID, DaysAfterDue, DaysBetweenDunning, c_dunninglevel_description, Dunning_PrintFormat_ID, FeeAmt, InterestPercent, InvoiceCollectionType, c_dunninglevel_isactive, IsSetCreditStop, IsSetPaymentTerm, IsShowAllDue, IsShowNotDue, IsStatement, c_dunninglevel_name, c_bpartner_acqusitioncost, c_bpartner_actuallifetimevalue, c_bpartner_ad_language, C_BPartner_AD_OrgBP_ID, C_BPartner_AD_Org_ID, C_BPartner_BPartner_Parent_ID, C_BPartner_C_BP_Group_ID, C_BPartner_C_Greeting_ID, C_BPartner_InvoiceSchedule_ID, C_BPartner_C_PaymentTerm_ID, c_bpartner_created, C_BPartner_CreatedBy, C_BPartner_C_TaxGroup_ID, c_bpartner_deliveryrule, c_bpartner_deliveryviarule, c_bpartner_description, c_bpartner_dunninggrace, c_bpartner_firstsale, c_bpartner_flatdiscount, c_bpartner_freightcostrule, c_bpartner_invoicerule, c_bpartner_isactive, c_bpartner_iscustomer, c_bpartner_isdiscountprinted, c_bpartner_isemployee, c_bpartner_ismanufacturer, c_bpartner_isonetime, c_bpartner_ispotaxexempt, c_bpartner_isprospect, c_bpartner_issalesrep, c_bpartner_issummary, c_bpartner_istaxexempt, c_bpartner_isvendor,
C_BPartner_Logo_ID, C_BPartner_M_DiscountSchema_ID, C_BPartner_M_PriceList_ID, c_bpartner_naics, c_bpartner_numberemployees, c_bpartner_paymentrule, c_bpartner_paymentrulepo, C_BPartnerPO_DiscountSchema_ID, C_BPartner_PO_PaymentTerm_ID, C_BPartner_PO_PriceList_ID, c_bpartner_poreference, c_bpartner_potentiallifetimev, c_bpartner_rating, C_BPartner_SalesRep_ID, c_bpartner_salesvolume, c_bpartner_sendemail, c_bpartner_shareofcustomer, c_bpartner_shelflifeminpct, c_bpartner_so_creditlimit, c_bpartner_socreditstatus, c_bpartner_so_creditused, c_bpartner_so_description, c_bpartner_totalopenbalance, c_bpartner_updated, C_BPartner_UpdatedBy, c_bpartner_url, C_BPartner_Greeting_AD_Org_ID, c_bpartner_greeting_isactive, c_bpartner_greeting_isfirstna, c_bpartner_greeting_name, C_BPartner_Location_AD_Org_ID, c_bpartner_location_created, C_BPartner_Location_CreatedBy, C_SalesRegion_ID, c_bpartner_location_fax, c_bpartner_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bpartner_location_name, c_bpartner_location_phone, c_bpartner_location_phone2, c_bpartner_location_updated, C_BPartner_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, Postal_Add, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_name, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfirstnameon, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID,
ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bpartner_acqusitionc, salesrep_bpartner_actuallifet, salesrep_bpartner_ad_language, SalesRep_BPartner_AD_OrgBP_ID, SalesRep_BPartner_AD_Org_ID, SalesRep_BPartner_BP_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_InvoiceSchedule_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bpartner_created, SalesRep_BPartner_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bpartner_deliveryrule, salesrep_bpartner_deliveryvia, salesrep_bpartner_description, salesrep_bpartner_dunninggrace, salesrep_bpartner_firstsale, salesrep_bpartner_flatdiscount, salesrep_bpartner_freightcost, salesrep_bpartner_invoicerule, salesrep_bpartner_isactive, salesrep_bpartner_iscustomer, salesrep_bpartner_isdiscountp, salesrep_bpartner_isemployee, salesrep_bpartner_ismanufactu, salesrep_bpartner_isonetime, salesrep_bpartner_ispotaxexem, salesrep_bpartner_isprospect, salesrep_bpartner_issalesrep, salesrep_bpartner_issummary, salesrep_bpartner_istaxexempt, salesrep_bpartner_isvendor, SalesRep_BPartner_Logo_ID, SalesRep_BP_MDiscountSchema_ID, SalesRep_BP_M_PriceList_ID, salesrep_bpartner_naics, salesrep_bpartner_numberemplo, salesrep_bpartner_paymentrule, salesrep_bpartner_paymentrupo, SalesRep_BP_PO_DiscountScm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bpartner_poreference, salesrep_bpartner_potentialli, salesrep_bpartner_rating,
SalesRep_BPartner_SalesRep_ID, salesrep_bpartner_salesvolume, salesrep_bpartner_sendemail, salesrep_bpartner_shareofcust, salesrep_bpartner_shelflifemi, salesrep_bpartner_so_creditli, salesrep_bpartner_socreditsta, salesrep_bpartner_so_creditus, salesrep_bpartner_so_descript, salesrep_bpartner_totalopenba, salesrep_bpartner_updated, SalesRep_BPartner_UpdatedBy, salesrep_bpartner_url) AS SELECT dr.ad_client_id AS AD_Client_ID, dr.ad_org_id AS AD_Org_ID, dr.isactive AS IsActive, dr.created AS Created, dr.createdby AS CreatedBy, dr.updated AS Updated, dr.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, dr.c_dunningrun_id AS C_DunningRun_ID, dre.c_dunningrunentry_id AS C_DunningRunEntry_ID, dr.dunningdate AS DunningDate, dl.printname AS PrintName, dl.note AS DocumentNote, dre.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, dre.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, l.postal || l.postal_add AS Postal, dre.amt AS Amt, dre.qty AS Qty, dre.note AS Note, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, dr.c_dunning_id AS C_Dunning_ID, dr.c_dunninglevel_id AS C_DunningRun_C_DunningLevel_ID, dr.description AS c_dunningrun_description, dr.processed AS Processed, dr.processing AS Processing, dr.sendit AS SendIt, dre.ad_org_id AS C_DunningRunEntry_AD_Org_ID, dre.ad_user_id AS AD_User_ID, dre.c_bpartner_location_id AS C_BPartner_Location_ID, dre.c_currency_id AS C_Currency_ID, dre.c_dunninglevel_id AS C_DunRunEntry_C_DunLevel_ID, dre.created AS c_dunningrunentry_created, dre.createdby AS C_DunningRunEntry_CreatedBy, dre.isactive AS c_dunningrunentry_isactive,
dre.processed AS c_dunningrunentry_processed, dre.updated AS c_dunningrunentry_updated, dre.updatedby AS C_DunningRunEntry_UpdatedBy, dl.ad_org_id AS C_DunningLevel_AD_Org_ID, dl.c_dunning_id AS C_DunningLevel_C_Dunning_ID, dl.chargefee AS ChargeFee, dl.chargeinterest AS ChargeInterest, dl.c_paymentterm_id AS C_PaymentTerm_ID, dl.daysafterdue AS DaysAfterDue, dl.daysbetweendunning AS DaysBetweenDunning, dl.description AS c_dunninglevel_description, dl.dunning_printformat_id AS Dunning_PrintFormat_ID, dl.feeamt AS FeeAmt, dl.interestpercent AS InterestPercent, dl.invoicecollectiontype AS InvoiceCollectionType, dl.isactive AS c_dunninglevel_isactive, dl.issetcreditstop AS IsSetCreditStop, dl.issetpaymentterm AS IsSetPaymentTerm, dl.isshowalldue AS IsShowAllDue, dl.isshownotdue AS IsShowNotDue, dl.isstatement AS IsStatement, dl.name AS c_dunninglevel_name, bp.acqusitioncost AS c_bpartner_acqusitioncost, bp.actuallifetimevalue AS c_bpartner_actuallifetimevalue, bp.ad_language AS c_bpartner_ad_language, bp.ad_orgbp_id AS C_BPartner_AD_OrgBP_ID, bp.ad_org_id AS C_BPartner_AD_Org_ID, bp.bpartner_parent_id AS C_BPartner_BPartner_Parent_ID, bp.c_bp_group_id AS C_BPartner_C_BP_Group_ID, bp.c_greeting_id AS C_BPartner_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BPartner_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BPartner_C_PaymentTerm_ID, bp.created AS c_bpartner_created, bp.createdby AS C_BPartner_CreatedBy, bp.c_taxgroup_id AS C_BPartner_C_TaxGroup_ID, bp.deliveryrule AS c_bpartner_deliveryrule, bp.deliveryviarule AS c_bpartner_deliveryviarule, bp.description AS c_bpartner_description, bp.dunninggrace AS c_bpartner_dunninggrace, bp.firstsale AS c_bpartner_firstsale, bp.flatdiscount AS c_bpartner_flatdiscount, bp.freightcostrule AS c_bpartner_freightcostrule, bp.invoicerule AS c_bpartner_invoicerule, bp.isactive AS c_bpartner_isactive, bp.iscustomer AS c_bpartner_iscustomer, bp.isdiscountprinted AS c_bpartner_isdiscountprinted, bp.isemployee AS c_bpartner_isemployee,
bp.ismanufacturer AS c_bpartner_ismanufacturer, bp.isonetime AS c_bpartner_isonetime, bp.ispotaxexempt AS c_bpartner_ispotaxexempt, bp.isprospect AS c_bpartner_isprospect, bp.issalesrep AS c_bpartner_issalesrep, bp.issummary AS c_bpartner_issummary, bp.istaxexempt AS c_bpartner_istaxexempt, bp.isvendor AS c_bpartner_isvendor, bp.logo_id AS C_BPartner_Logo_ID, bp.m_discountschema_id AS C_BPartner_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BPartner_M_PriceList_ID, bp.naics AS c_bpartner_naics, bp.numberemployees AS c_bpartner_numberemployees, bp.paymentrule AS c_bpartner_paymentrule, bp.paymentrulepo AS c_bpartner_paymentrulepo, bp.po_discountschema_id AS C_BPartnerPO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BPartner_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BPartner_PO_PriceList_ID, bp.poreference AS c_bpartner_poreference, bp.potentiallifetimevalue AS c_bpartner_potentiallifetimev, bp.rating AS c_bpartner_rating, bp.salesrep_id AS C_BPartner_SalesRep_ID, bp.salesvolume AS c_bpartner_salesvolume, bp.sendemail AS c_bpartner_sendemail, bp.shareofcustomer AS c_bpartner_shareofcustomer, bp.shelflifeminpct AS c_bpartner_shelflifeminpct, bp.so_creditlimit AS c_bpartner_so_creditlimit, bp.socreditstatus AS c_bpartner_socreditstatus, bp.so_creditused AS c_bpartner_so_creditused, bp.so_description AS c_bpartner_so_description, bp.totalopenbalance AS c_bpartner_totalopenbalance, bp.updated AS c_bpartner_updated, bp.updatedby AS C_BPartner_UpdatedBy, bp.url AS c_bpartner_url, bpg.ad_org_id AS C_BPartner_Greeting_AD_Org_ID, bpg.isactive AS c_bpartner_greeting_isactive, bpg.isfirstnameonly AS c_bpartner_greeting_isfirstna, bpg.name AS c_bpartner_greeting_name, bpl.ad_org_id AS C_BPartner_Location_AD_Org_ID, bpl.created AS c_bpartner_location_created, bpl.createdby AS C_BPartner_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bpartner_location_fax, bpl.isactive AS c_bpartner_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN,
bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bpartner_location_name, bpl.phone AS c_bpartner_location_phone, bpl.phone2 AS c_bpartner_location_phone2, bpl.updated AS c_bpartner_location_updated, bpl.updatedby AS C_BPartner_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.postal_add AS Postal_Add, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.name AS ad_user_name, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcg.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcg.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfirstnameon, bpcg.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email,
oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bpartner_acqusitionc, ubp.actuallifetimevalue AS salesrep_bpartner_actuallifet, ubp.ad_language AS salesrep_bpartner_ad_language, ubp.ad_orgbp_id AS SalesRep_BPartner_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BPartner_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BPartner_BP_Parent_ID, ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_InvoiceSchedule_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bpartner_created, ubp.createdby AS SalesRep_BPartner_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bpartner_deliveryrule, ubp.deliveryviarule AS salesrep_bpartner_deliveryvia, ubp.description AS salesrep_bpartner_description, ubp.dunninggrace AS salesrep_bpartner_dunninggrace, ubp.firstsale AS salesrep_bpartner_firstsale,
ubp.flatdiscount AS salesrep_bpartner_flatdiscount, ubp.freightcostrule AS salesrep_bpartner_freightcost, ubp.invoicerule AS salesrep_bpartner_invoicerule, ubp.isactive AS salesrep_bpartner_isactive, ubp.iscustomer AS salesrep_bpartner_iscustomer, ubp.isdiscountprinted AS salesrep_bpartner_isdiscountp, ubp.isemployee AS salesrep_bpartner_isemployee, ubp.ismanufacturer AS salesrep_bpartner_ismanufactu, ubp.isonetime AS salesrep_bpartner_isonetime, ubp.ispotaxexempt AS salesrep_bpartner_ispotaxexem, ubp.isprospect AS salesrep_bpartner_isprospect, ubp.issalesrep AS salesrep_bpartner_issalesrep, ubp.issummary AS salesrep_bpartner_issummary, ubp.istaxexempt AS salesrep_bpartner_istaxexempt, ubp.isvendor AS salesrep_bpartner_isvendor, ubp.logo_id AS SalesRep_BPartner_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_MDiscountSchema_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bpartner_naics, ubp.numberemployees AS salesrep_bpartner_numberemplo, ubp.paymentrule AS salesrep_bpartner_paymentrule, ubp.paymentrulepo AS salesrep_bpartner_paymentrupo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountScm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bpartner_poreference, ubp.potentiallifetimevalue AS salesrep_bpartner_potentialli, ubp.rating AS salesrep_bpartner_rating, ubp.salesrep_id AS SalesRep_BPartner_SalesRep_ID, ubp.salesvolume AS salesrep_bpartner_salesvolume, ubp.sendemail AS salesrep_bpartner_sendemail, ubp.shareofcustomer AS salesrep_bpartner_shareofcust, ubp.shelflifeminpct AS salesrep_bpartner_shelflifemi, ubp.so_creditlimit AS salesrep_bpartner_so_creditli, ubp.socreditstatus AS salesrep_bpartner_socreditsta, ubp.so_creditused AS salesrep_bpartner_so_creditus, ubp.so_description AS salesrep_bpartner_so_descript, ubp.totalopenbalance AS salesrep_bpartner_totalopenba, ubp.updated AS salesrep_bpartner_updated,
ubp.updatedby AS SalesRep_BPartner_UpdatedBy, ubp.url AS salesrep_bpartner_url FROM c_dunningrun dr
JOIN c_dunningrunentry dre ON dr.c_dunningrun_id = dre.c_dunningrun_id
JOIN c_dunninglevel dl ON dre.c_dunninglevel_id = dl.c_dunninglevel_id
JOIN c_bpartner bp ON dre.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN c_bpartner_location bpl ON dre.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON dre.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
JOIN ad_orginfo oi ON dr.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON dr.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON dre.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_Payment(C_Payment_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, DocumentNo, DateTrx, IsReceipt, C_DocType_ID, TrxType, C_BankAccount_ID, C_BPartner_ID, C_Invoice_ID, C_BP_BankAccount_ID, C_PaymentBatch_ID, TenderType, CreditCardType, CreditCardNumber, CreditCardVV, CreditCardExpMM, CreditCardExpYY, Micr, RoutingNo, AccountNo, CheckNo, A_Name, A_Street, A_City, A_State, A_Zip, A_Ident_DL, A_Ident_SSN, A_EMail, VoiceAuthCode, Orig_TrxID, PONum, C_Currency_ID, C_ConversionType_ID, PayAmt, DiscountAmt, WriteOffAmt, TaxAmt, OverUnderAmt, MultiplierAP, AllocatedAmt, AvailableAmt, IsOverUnderPayment, IsApproved, R_PnRef, R_Result, R_RespMsg, R_AuthCode, R_AvsAddr, R_AvsZip, R_Info, Processing, OProcessing, DocStatus, DocAction, IsPrepayment, C_Charge_ID, IsReconciled, IsAllocated, IsOnline, Processed, Posted, C_Campaign_ID, C_Project_ID, C_Activity_ID, A_Country, AD_OrgTrx_ID, ChargeAmt, C_Order_ID, DateAcct, Description, IsDelayedCapture, IsSelfService, ProcessedOn, R_AuthCode_DC, R_CVV2Match, Ref_Payment_ID, Reversal_ID, R_PnRef_DC, Swipe, User1_ID, User2_ID) AS SELECT c_payment.c_payment_id AS C_Payment_ID, c_payment.ad_client_id AS AD_Client_ID, c_payment.ad_org_id AS AD_Org_ID, c_payment.isactive AS IsActive, c_payment.created AS Created, c_payment.createdby AS CreatedBy, c_payment.updated AS Updated, c_payment.updatedby AS UpdatedBy, c_payment.documentno AS DocumentNo, c_payment.datetrx AS DateTrx, c_payment.isreceipt AS IsReceipt, c_payment.c_doctype_id AS C_DocType_ID, c_payment.trxtype AS TrxType, c_payment.c_bankaccount_id AS C_BankAccount_ID, c_payment.c_bpartner_id AS C_BPartner_ID, c_payment.c_invoice_id AS C_Invoice_ID, c_payment.c_bp_bankaccount_id AS C_BP_BankAccount_ID, c_payment.c_paymentbatch_id AS C_PaymentBatch_ID, c_payment.tendertype AS TenderType, c_payment.creditcardtype AS CreditCardType, c_payment.creditcardnumber AS CreditCardNumber, c_payment.creditcardvv AS CreditCardVV,
c_payment.creditcardexpmm AS CreditCardExpMM, c_payment.creditcardexpyy AS CreditCardExpYY, c_payment.micr AS Micr, c_payment.routingno AS RoutingNo, c_payment.accountno AS AccountNo, c_payment.checkno AS CheckNo, c_payment.a_name AS A_Name, c_payment.a_street AS A_Street, c_payment.a_city AS A_City, c_payment.a_state AS A_State, c_payment.a_zip AS A_Zip, c_payment.a_ident_dl AS A_Ident_DL, c_payment.a_ident_ssn AS A_Ident_SSN, c_payment.a_email AS A_EMail, c_payment.voiceauthcode AS VoiceAuthCode, c_payment.orig_trxid AS Orig_TrxID, c_payment.ponum AS PONum, c_payment.c_currency_id AS C_Currency_ID, c_payment.c_conversiontype_id AS C_ConversionType_ID, CASE c_payment.isreceipt WHEN 'Y' THEN c_payment.payamt ELSE c_payment.payamt * '-1' END AS PayAmt, CASE c_payment.isreceipt WHEN 'Y' THEN c_payment.discountamt ELSE c_payment.discountamt * '-1' END AS DiscountAmt, CASE c_payment.isreceipt WHEN 'Y' THEN c_payment.writeoffamt ELSE c_payment.writeoffamt * '-1' END AS WriteOffAmt, CASE c_payment.isreceipt WHEN 'Y' THEN c_payment.taxamt ELSE c_payment.taxamt * '-1' END AS TaxAmt, CASE c_payment.isreceipt WHEN 'Y' THEN c_payment.overunderamt ELSE c_payment.overunderamt * '-1' END AS OverUnderAmt, CASE c_payment.isreceipt WHEN 'Y' THEN 1 ELSE -1 END AS MultiplierAP, paymentallocated(c_payment.c_payment_id, c_payment.c_currency_id) AS AllocatedAmt, paymentavailable(c_payment.c_payment_id) AS AvailableAmt, c_payment.isoverunderpayment AS IsOverUnderPayment, c_payment.isapproved AS IsApproved, c_payment.r_pnref AS R_PnRef, c_payment.r_result AS R_Result, c_payment.r_respmsg AS R_RespMsg, c_payment.r_authcode AS R_AuthCode, c_payment.r_avsaddr AS R_AvsAddr, c_payment.r_avszip AS R_AvsZip, c_payment.r_info AS R_Info, c_payment.processing AS Processing, c_payment.oprocessing AS OProcessing, c_payment.docstatus AS DocStatus, c_payment.docaction AS DocAction, c_payment.isprepayment AS IsPrepayment, c_payment.c_charge_id AS C_Charge_ID, c_payment.isreconciled AS IsReconciled,
c_payment.isallocated AS IsAllocated, c_payment.isonline AS IsOnline, c_payment.processed AS Processed, c_payment.posted AS Posted, c_payment.c_campaign_id AS C_Campaign_ID, c_payment.c_project_id AS C_Project_ID, c_payment.c_activity_id AS C_Activity_ID, c_payment.a_country AS A_Country, c_payment.ad_orgtrx_id AS AD_OrgTrx_ID, c_payment.chargeamt AS ChargeAmt, c_payment.c_order_id AS C_Order_ID, c_payment.dateacct AS DateAcct, c_payment.description AS Description, c_payment.isdelayedcapture AS IsDelayedCapture, c_payment.isselfservice AS IsSelfService, c_payment.processedon AS ProcessedOn, c_payment.r_authcode_dc AS R_AuthCode_DC, c_payment.r_cvv2match AS R_CVV2Match, c_payment.ref_payment_id AS Ref_Payment_ID, c_payment.reversal_id AS Reversal_ID, c_payment.r_pnref_dc AS R_PnRef_DC, c_payment.swipe AS Swipe, c_payment.user1_id AS User1_ID, c_payment.user2_id AS User2_ID FROM c_payment
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_CommissionRunDetail(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_CommissionRun_ID, DocumentNo, Description, StartDate, GrandTotal, Processed, C_Commission_ID, Commission_BPartner_ID, C_CommissionAmt_ID, CommissionConvertedAmt, CommissionQty, CommissionAmt, C_CommissionDetail_ID, Reference, C_OrderLine_ID, C_InvoiceLine_ID, Info, C_Currency_ID, ActualAmt, ConvertedAmt, ActualQty, InvoiceDocumentNo, DateDoc, M_Product_ID, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, C_DocType_ID, c_commisionrun_processing, C_Commission_AD_Org_ID, C_Commission_C_Currency_ID, c_commission_created, C_Commission_CreatedBy, CreateFrom, DateLastRun, c_commission_description, DocBasisType, FrequencyType, c_commission_isactive, ListDetails, c_commission_name, c_commission_updated, C_Commission_UpdatedBy, C_CommissionAmt_AD_Org_ID, C_CommissionLine_ID, c_commmissionamt_created, C_CommmissionAmt_CreatedBy, c_commmissionamt_isactive, c_commmissionamt_updated, C_CommssionAmt_UpdatedBy, C_CommissionDetail_AD_Org_ID, c_commissiondetail_created, C_CommissionDetail_CreatedBy, c_commossiondetail_isactive, c_commissiondetail_updated, C_CommissionDetail_UpdatedBy, C_OrderLine_AD_Org_ID, C_OrderLine_AD_OrgTrx_ID, C_OrderLine_C_Activity_ID, C_OrderLine_C_BPartner_ID, C_OrderLine_C_BP_Location_ID, C_OrderLine_C_Campaign_ID, C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_OrderLine_C_Order_ID, C_OrderLine_C_Project_ID, C_OrderLine_C_ProjectPhase_ID, C_OrderLine_C_ProjectTask_ID, c_orderline_created, C_OrderLine_CreatedBy, C_OrderLine_C_Tax_ID, C_OrderLine_C_UOM_ID, DateDelivered, DateInvoiced, DateOrdered, DatePromised, c_orderline_description, Discount, FreightAmt, c_orderline_isactive, IsDescription, c_orderline_line, c_orderline_linenetamt, Link_OrderLine_ID, C_OrderLine_M_ASI_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, C_OrderLine_M_Warehouse_ID, c_orderline_priceactual, PriceCost, c_orderline_priceentered, c_orderline_pricelist,
c_orderline_processed, QtyDelivered, QtyEntered, c_orderline_qtyinvoiced, QtyLostSales, QtyOrdered, QtyReserved, C_OrderLine_Ref_OrderLine_ID, c_orderline_rramt, c_orderline_rrstartsate, C_OrderLine_S_RAssignment_ID, c_orderline_updated, C_OrderLine_UpdatedBy, C_OrderLine_User1_ID, C_OrderLine_User2_ID, C_InviceLine_AD_Org_ID, C_InvoiceLine_AD_OrgTrx_ID, A_Processed, C_InvoiceLine_C_Activity_ID, C_InvoiceLine_C_Campaign_ID, C_InvoiceLine_C_Charge_ID, C_Invoice_ID, C_InvoiceLine_C_OrderLine_ID, C_InvoiceLine_C_Project_ID, C_InvoiceLine_C_ProjPhase_ID, C_InvoiceLine_C_ProjectTask_ID, c_invoiceline_created, C_InvoiceLine_CreatedBy, C_InvoiceLine_C_Tax_ID, C_InvoiceLine_C_UOM_ID, c_invoiceline_description, c_invoiceline_isactive, c_invoiceline_isdescription, IsPrinted, c_invoiceline_line, c_invoiceline_linenetamt, c_invoiceline_linetotalamt, C_InvoiceLine_M_ASI_ID, M_InOutLine_ID, C_InvoiceLine_M_RMALine_ID, c_invoiceline_priceactual, c_invoiceline_priceentered, c_invoiceline_pricelimit, c_invoiceline_pricelist, c_invoiceline_processed, c_invoiceline_qtyentered, c_invoiceline_qtyinvoiced, C_InvoiceLine_Ref_InvLine_ID, c_invoiceline_rramt, c_invoiceline_rrstartdate, C_InvoiceLine_S_RAssignment_ID, c_invoiceline_taxamt, c_invoiceline_updated, C_InvoiceLine_UpdatedBy, C_InvoiceLine_User1_ID, C_InvoiceLine_User2_ID, C_Order_AD_Org_ID, C_Order_AD_OrgTrx_ID, C_Order_AD_User_ID, AmountRefunded, AmountTendered, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, C_Activity_ID, C_Order_C_Campaign_ID, C_CashLine_ID, C_CashPlanLine_ID, C_Charge_ID, C_ConversionType_ID, C_Order_C_Currency_ID, C_DocTypeTarget_ID, c_order_chargeamt, c_order_copyfrom, C_Order_C_Order_ID, C_Order_C_Payment_ID, C_Order_PaymentTerm_ID, C_POS_ID, C_Order_C_Project_ID, c_order_created, C_Order_CreatedBy, c_order_dateacct, c_order_dateprinted, c_order_datepromised, c_order_deliveryrule, c_order_deliveryviarule, c_order_description, c_order_docaction, c_order_docstatus, c_order_documentno,
DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, c_order_freightamt, c_order_freightcostrule, c_order_grandtotal, InvoiceRule, c_order_isactive, IsApproved, IsCreditApproved, IsDelivered, IsDiscountPrinted, IsDropShip, IsInvoiced, IsPayScheduleValid, c_order_isprinted, c_order_isselected, c_order_isselfservice, c_order_issotrx, c_order_istaxincluded, IsTransferred, Link_Order_ID, M_FreightCategory_ID, M_PriceList_ID, C_Order_M_Shipper_ID, C_Order_M_Warehouse_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, PaymentRule, POReference, c_order_posted, PriorityRule, c_order_processed, c_order_processedon, PromotionCode, C_Order_Ref_Order_ID, c_order_sendemail, c_order_totallines, C_Order_User1_ID, C_Order_User2_ID, C_Invoice_AD_Org_ID, C_Invoice_AD_OrgTrx_ID, C_Invoice_C_Activity_ID, C_Invoice_C_Campaign_ID, C_Invoice_C_Charge_ID, C_Invoice_C_ConversionType_ID, C_Invoice_C_Currency_ID, C_Invoice_C_DocTypeTarget_ID, C_DunningLevel_ID, c_invoice_chargeamt, C_Invoice_C_Invoice_ID, C_Invoice_C_Order_ID, C_Invoice_C_Payment_ID, C_Invoice_C_PaymentTerm_ID, C_Invoice_C_Project_ID, c_invoice_created, C_Invoice_CreatedBy, c_invoice_dateacct, c_invoice_dateordered, c_invoice_dateprinted, c_invoice_description, c_invoice_docaction, c_invoice_docstatus, c_invoice_dunninggrace, GenerateTo, c_invoice_grandtotal, InvoiceCollectionType, c_invoice_isactive, c_invoice_isapproved, c_invoice_isdiscountprinted, c_invoice_isindispute, c_invoice_ispayschedulevalid, c_invoice_isprinted, c_invoice_isselfservice, c_invoice_issotrx, c_invoice_istaxincluded, c_invoice_istransferred, C_Invoice_M_PriceList_ID, C_Invoice_M_RMA_ID, c_invoice_paymentrule, c_invoice_poreference, c_invoice_posted, c_invoice_processedon, c_invoice_processing, Ref_Invoice_ID, Reversal_ID, C_Invoice_SalesRep_ID, c_invoice_sendemail, c_invoice_totallines, c_invoice_updated, C_Invoice_UpdatedBy, C_Invoice_User1_ID, C_Invoice_User2_ID) AS SELECT cr.ad_client_id AS AD_Client_ID, cr.ad_org_id AS AD_Org_ID,
cr.isactive AS IsActive, cr.created AS Created, cr.createdby AS CreatedBy, cr.updated AS Updated, cr.updatedby AS UpdatedBy, cr.c_commissionrun_id AS C_CommissionRun_ID, cr.documentno AS DocumentNo, cr.description AS Description, cr.startdate AS StartDate, cr.grandtotal AS GrandTotal, cr.processed AS Processed, c.c_commission_id AS C_Commission_ID, c.c_bpartner_id AS Commission_BPartner_ID, ca.c_commissionamt_id AS C_CommissionAmt_ID, ca.convertedamt AS CommissionConvertedAmt, ca.actualqty AS CommissionQty, ca.commissionamt AS CommissionAmt, cd.c_commissiondetail_id AS C_CommissionDetail_ID, cd.reference AS Reference, cd.c_orderline_id AS C_OrderLine_ID, cd.c_invoiceline_id AS C_InvoiceLine_ID, cd.info AS Info, cd.c_currency_id AS C_Currency_ID, cd.actualamt AS ActualAmt, cd.convertedamt AS ConvertedAmt, cd.actualqty AS ActualQty, i.documentno AS InvoiceDocumentNo, COALESCE(i.dateinvoiced, o.dateordered) AS DateDoc, COALESCE(il.m_product_id, ol.m_product_id) AS M_Product_ID, COALESCE(i.c_bpartner_id, o.c_bpartner_id) AS C_BPartner_ID, COALESCE(i.c_bpartner_location_id, o.c_bpartner_location_id) AS C_BPartner_Location_ID, COALESCE(i.ad_user_id, o.ad_user_id) AS AD_User_ID, COALESCE(i.c_doctype_id, o.c_doctype_id) AS C_DocType_ID, cr.processing AS c_commisionrun_processing, c.ad_org_id AS C_Commission_AD_Org_ID, c.c_currency_id AS C_Commission_C_Currency_ID, c.created AS c_commission_created, c.createdby AS C_Commission_CreatedBy, c.createfrom AS CreateFrom, c.datelastrun AS DateLastRun, c.description AS c_commission_description, c.docbasistype AS DocBasisType, c.frequencytype AS FrequencyType, c.isactive AS c_commission_isactive, c.listdetails AS ListDetails, c.name AS c_commission_name, c.updated AS c_commission_updated, c.updatedby AS C_Commission_UpdatedBy, ca.ad_org_id AS C_CommissionAmt_AD_Org_ID, ca.c_commissionline_id AS C_CommissionLine_ID, ca.created AS c_commmissionamt_created, ca.createdby AS C_CommmissionAmt_CreatedBy,
ca.isactive AS c_commmissionamt_isactive, ca.updated AS c_commmissionamt_updated, ca.updatedby AS C_CommssionAmt_UpdatedBy, cd.ad_org_id AS C_CommissionDetail_AD_Org_ID, cd.created AS c_commissiondetail_created, cd.createdby AS C_CommissionDetail_CreatedBy, cd.isactive AS c_commossiondetail_isactive, cd.updated AS c_commissiondetail_updated, cd.updatedby AS C_CommissionDetail_UpdatedBy, ol.ad_org_id AS C_OrderLine_AD_Org_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_activity_id AS C_OrderLine_C_Activity_ID, ol.c_bpartner_id AS C_OrderLine_C_BPartner_ID, ol.c_bpartner_location_id AS C_OrderLine_C_BP_Location_ID, ol.c_campaign_id AS C_OrderLine_C_Campaign_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_order_id AS C_OrderLine_C_Order_ID, ol.c_project_id AS C_OrderLine_C_Project_ID, ol.c_projectphase_id AS C_OrderLine_C_ProjectPhase_ID, ol.c_projecttask_id AS C_OrderLine_C_ProjectTask_ID, ol.created AS c_orderline_created, ol.createdby AS C_OrderLine_CreatedBy, ol.c_tax_id AS C_OrderLine_C_Tax_ID, ol.c_uom_id AS C_OrderLine_C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS DatePromised, ol.description AS c_orderline_description, ol.discount AS Discount, ol.freightamt AS FreightAmt, ol.isactive AS c_orderline_isactive, ol.isdescription AS IsDescription, ol.line AS c_orderline_line, ol.linenetamt AS c_orderline_linenetamt, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_attributesetinstance_id AS C_OrderLine_M_ASI_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_M_Warehouse_ID, ol.priceactual AS c_orderline_priceactual, ol.pricecost AS PriceCost, ol.priceentered AS c_orderline_priceentered, ol.pricelist AS c_orderline_pricelist, ol.processed AS c_orderline_processed, ol.qtydelivered AS QtyDelivered, ol.qtyentered AS QtyEntered,
ol.qtyinvoiced AS c_orderline_qtyinvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyordered AS QtyOrdered, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS C_OrderLine_Ref_OrderLine_ID, ol.rramt AS c_orderline_rramt, ol.rrstartdate AS c_orderline_rrstartsate, ol.s_resourceassignment_id AS C_OrderLine_S_RAssignment_ID, ol.updated AS c_orderline_updated, ol.updatedby AS C_OrderLine_UpdatedBy, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, il.ad_org_id AS C_InviceLine_AD_Org_ID, il.ad_orgtrx_id AS C_InvoiceLine_AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_activity_id AS C_InvoiceLine_C_Activity_ID, il.c_campaign_id AS C_InvoiceLine_C_Campaign_ID, il.c_charge_id AS C_InvoiceLine_C_Charge_ID, il.c_invoice_id AS C_Invoice_ID, il.c_orderline_id AS C_InvoiceLine_C_OrderLine_ID, il.c_project_id AS C_InvoiceLine_C_Project_ID, il.c_projectphase_id AS C_InvoiceLine_C_ProjPhase_ID, il.c_projecttask_id AS C_InvoiceLine_C_ProjectTask_ID, il.created AS c_invoiceline_created, il.createdby AS C_InvoiceLine_CreatedBy, il.c_tax_id AS C_InvoiceLine_C_Tax_ID, il.c_uom_id AS C_InvoiceLine_C_UOM_ID, il.description AS c_invoiceline_description, il.isactive AS c_invoiceline_isactive, il.isdescription AS c_invoiceline_isdescription, il.isprinted AS IsPrinted, il.line AS c_invoiceline_line, il.linenetamt AS c_invoiceline_linenetamt, il.linetotalamt AS c_invoiceline_linetotalamt, il.m_attributesetinstance_id AS C_InvoiceLine_M_ASI_ID, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS C_InvoiceLine_M_RMALine_ID, il.priceactual AS c_invoiceline_priceactual, il.priceentered AS c_invoiceline_priceentered, il.pricelimit AS c_invoiceline_pricelimit, il.pricelist AS c_invoiceline_pricelist, il.processed AS c_invoiceline_processed, il.qtyentered AS c_invoiceline_qtyentered, il.qtyinvoiced AS c_invoiceline_qtyinvoiced, il.ref_invoiceline_id AS C_InvoiceLine_Ref_InvLine_ID, il.rramt AS c_invoiceline_rramt, il.rrstartdate AS c_invoiceline_rrstartdate,
il.s_resourceassignment_id AS C_InvoiceLine_S_RAssignment_ID, il.taxamt AS c_invoiceline_taxamt, il.updated AS c_invoiceline_updated, il.updatedby AS C_InvoiceLine_UpdatedBy, il.user1_id AS C_InvoiceLine_User1_ID, il.user2_id AS C_InvoiceLine_User2_ID, o.ad_org_id AS C_Order_AD_Org_ID, o.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, o.ad_user_id AS C_Order_AD_User_ID, o.amountrefunded AS AmountRefunded, o.amounttendered AS AmountTendered, o.bill_bpartner_id AS Bill_BPartner_ID, o.bill_location_id AS Bill_Location_ID, o.bill_user_id AS Bill_User_ID, o.c_activity_id AS C_Activity_ID, o.c_campaign_id AS C_Order_C_Campaign_ID, o.c_cashline_id AS C_CashLine_ID, o.c_cashplanline_id AS C_CashPlanLine_ID, o.c_charge_id AS C_Charge_ID, o.c_conversiontype_id AS C_ConversionType_ID, o.c_currency_id AS C_Order_C_Currency_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.chargeamt AS c_order_chargeamt, o.copyfrom AS c_order_copyfrom, o.c_order_id AS C_Order_C_Order_ID, o.c_payment_id AS C_Order_C_Payment_ID, o.c_paymentterm_id AS C_Order_PaymentTerm_ID, o.c_pos_id AS C_POS_ID, o.c_project_id AS C_Order_C_Project_ID, o.created AS c_order_created, o.createdby AS C_Order_CreatedBy, o.dateacct AS c_order_dateacct, o.dateprinted AS c_order_dateprinted, o.datepromised AS c_order_datepromised, o.deliveryrule AS c_order_deliveryrule, o.deliveryviarule AS c_order_deliveryviarule, o.description AS c_order_description, o.docaction AS c_order_docaction, o.docstatus AS c_order_docstatus, o.documentno AS c_order_documentno, o.dropship_bpartner_id AS DropShip_BPartner_ID, o.dropship_location_id AS DropShip_Location_ID, o.dropship_user_id AS DropShip_User_ID, o.freightamt AS c_order_freightamt, o.freightcostrule AS c_order_freightcostrule, o.grandtotal AS c_order_grandtotal, o.invoicerule AS InvoiceRule, o.isactive AS c_order_isactive, o.isapproved AS IsApproved, o.iscreditapproved AS IsCreditApproved, o.isdelivered AS IsDelivered, o.isdiscountprinted AS IsDiscountPrinted, o.isdropship AS IsDropShip,
o.isinvoiced AS IsInvoiced, o.ispayschedulevalid AS IsPayScheduleValid, o.isprinted AS c_order_isprinted, o.isselected AS c_order_isselected, o.isselfservice AS c_order_isselfservice, o.issotrx AS c_order_issotrx, o.istaxincluded AS c_order_istaxincluded, o.istransferred AS IsTransferred, o.link_order_id AS Link_Order_ID, o.m_freightcategory_id AS M_FreightCategory_ID, o.m_pricelist_id AS M_PriceList_ID, o.m_shipper_id AS C_Order_M_Shipper_ID, o.m_warehouse_id AS C_Order_M_Warehouse_ID, o.ordertype AS OrderType, o.pay_bpartner_id AS Pay_BPartner_ID, o.pay_location_id AS Pay_Location_ID, o.paymentrule AS PaymentRule, o.poreference AS POReference, o.posted AS c_order_posted, o.priorityrule AS PriorityRule, o.processed AS c_order_processed, o.processedon AS c_order_processedon, o.promotioncode AS PromotionCode, o.ref_order_id AS C_Order_Ref_Order_ID, o.sendemail AS c_order_sendemail, o.totallines AS c_order_totallines, o.user1_id AS C_Order_User1_ID, o.user2_id AS C_Order_User2_ID, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.c_activity_id AS C_Invoice_C_Activity_ID, i.c_campaign_id AS C_Invoice_C_Campaign_ID, i.c_charge_id AS C_Invoice_C_Charge_ID, i.c_conversiontype_id AS C_Invoice_C_ConversionType_ID, i.c_currency_id AS C_Invoice_C_Currency_ID, i.c_doctypetarget_id AS C_Invoice_C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS c_invoice_chargeamt, i.c_invoice_id AS C_Invoice_C_Invoice_ID, i.c_order_id AS C_Invoice_C_Order_ID, i.c_payment_id AS C_Invoice_C_Payment_ID, i.c_paymentterm_id AS C_Invoice_C_PaymentTerm_ID, i.c_project_id AS C_Invoice_C_Project_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateacct AS c_invoice_dateacct, i.dateordered AS c_invoice_dateordered, i.dateprinted AS c_invoice_dateprinted, i.description AS c_invoice_description, i.docaction AS c_invoice_docaction, i.docstatus AS c_invoice_docstatus, i.dunninggrace AS c_invoice_dunninggrace,
i.generateto AS GenerateTo, i.grandtotal AS c_invoice_grandtotal, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS c_invoice_isapproved, i.isdiscountprinted AS c_invoice_isdiscountprinted, i.isindispute AS c_invoice_isindispute, i.ispayschedulevalid AS c_invoice_ispayschedulevalid, i.isprinted AS c_invoice_isprinted, i.isselfservice AS c_invoice_isselfservice, i.issotrx AS c_invoice_issotrx, i.istaxincluded AS c_invoice_istaxincluded, i.istransferred AS c_invoice_istransferred, i.m_pricelist_id AS C_Invoice_M_PriceList_ID, i.m_rma_id AS C_Invoice_M_RMA_ID, i.paymentrule AS c_invoice_paymentrule, i.poreference AS c_invoice_poreference, i.posted AS c_invoice_posted, i.processedon AS c_invoice_processedon, i.processing AS c_invoice_processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS C_Invoice_SalesRep_ID, i.sendemail AS c_invoice_sendemail, i.totallines AS c_invoice_totallines, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS C_Invoice_User1_ID, i.user2_id AS C_Invoice_User2_ID FROM c_commissionrun cr
JOIN c_commission c ON cr.c_commission_id = c.c_commission_id
JOIN c_commissionamt ca ON cr.c_commissionrun_id = ca.c_commissionrun_id
JOIN c_commissiondetail cd ON ca.c_commissionamt_id = cd.c_commissionamt_id
LEFT JOIN c_orderline ol ON cd.c_orderline_id = ol.c_orderline_id
LEFT JOIN c_invoiceline il ON cd.c_invoiceline_id = il.c_invoiceline_id
LEFT JOIN c_order o ON ol.c_order_id = o.c_order_id
LEFT JOIN c_invoice i ON il.c_invoice_id = i.c_invoice_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_Cost(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Product_ID, Value, Name, UPC, IsBOM, ProductType, M_Product_Category_ID, M_CostType_ID, M_CostElement_ID, CostElementType, CostingMethod, IsCalculated, C_AcctSchema_ID, C_Currency_ID, CurrentCostPrice, FutureCostPrice, Description, CurrentCostPriceLL, FutureCostPriceLL, IsCostFrozen, CumulatedAmt, CumulatedQty, CurrentQty, M_AttributeSetInstance_ID, Percent, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, m_product_m_attributesetinsta, M_FreightCategory_ID, M_Locator_ID, m_product_processing, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, M_CostElement_AD_Org_ID, m_costelement_description, m_costelement_isactive, m_costelement_name, C_AcctSchema_AD_Org_ID, c_acctschema_description, c_acctschema_name) AS SELECT c.ad_client_id AS AD_Client_ID, c.ad_org_id AS AD_Org_ID, c.isactive AS IsActive, c.created AS Created, c.createdby AS CreatedBy, c.updated AS Updated, c.updatedby AS UpdatedBy, p.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.upc AS UPC, p.isbom AS IsBOM, p.producttype AS ProductType, p.m_product_category_id AS M_Product_Category_ID, c.m_costtype_id AS M_CostType_ID, ce.m_costelement_id AS M_CostElement_ID, ce.costelementtype AS CostElementType,
ce.costingmethod AS CostingMethod, ce.iscalculated AS IsCalculated, acct.c_acctschema_id AS C_AcctSchema_ID, acct.c_currency_id AS C_Currency_ID, c.currentcostprice AS CurrentCostPrice, c.futurecostprice AS FutureCostPrice, c.description AS Description, c.currentcostpricell AS CurrentCostPriceLL, c.futurecostpricell AS FutureCostPriceLL, c.iscostfrozen AS IsCostFrozen, c.cumulatedamt AS CumulatedAmt, c.cumulatedqty AS CumulatedQty, c.currentqty AS CurrentQty, c.m_attributesetinstance_id AS M_AttributeSetInstance_ID, c.percent AS Percent, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS m_product_m_attributesetinsta, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.processing AS m_product_processing, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID,
p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.sku AS SKU, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, ce.ad_org_id AS M_CostElement_AD_Org_ID, ce.description AS m_costelement_description, ce.isactive AS m_costelement_isactive, ce.name AS m_costelement_name, acct.ad_org_id AS C_AcctSchema_AD_Org_ID, acct.description AS c_acctschema_description, acct.name AS c_acctschema_name FROM m_cost c
JOIN m_product p ON c.m_product_id = p.m_product_id
JOIN m_costelement ce ON c.m_costelement_id = ce.m_costelement_id
JOIN c_acctschema acct ON c.c_acctschema_id = acct.c_acctschema_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_C_InvoiceLine(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_InvoiceLine_ID, C_Invoice_ID, SalesRep_ID, C_BPartner_ID, C_BP_Group_ID, M_Product_ID, M_Product_Category_ID, DateInvoiced, DateAcct, IsSOTrx, C_DocType_ID, DocStatus, IsPaid, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, QtyInvoiced, QtyEntered, M_AttributeSetInstance_ID, ProductAttribute, M_AttributeSet_ID, M_Lot_ID, GuaranteeDate, Lot, SerNo, PriceList, PriceActual, PriceLimit, PriceEntered, Discount, Margin, MarginAmt, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineOverLimitAmt, AD_OrgTrx_ID, A_Processed, C_Charge_ID, C_OrderLine_ID, C_Tax_ID, C_InvoiceLine_C_UOM_ID, c_invoiceline_description, IsDescription, IsPrinted, Line, c_invoiceline_linenetamt, LineTotalAmt, M_InOutLine_ID, M_RMALine_ID, Processed, Ref_InvoiceLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, TaxAmt, User1_ID, User2_ID, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_ProductLine_C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_FreightCategory_ID, M_Locator_ID, M_Product_M_Product_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, M_ASI_AD_Org_ID, m_attributesetinstance_created, M_ASI_CreatedBy, M_ASI_Description, m_attributesetinstance_isacti,
m_attributesetinstance_serno, m_attributesetinstance_updated, M_ASI_UpdatedBy, M_Product_Category_Parent_ID) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, il.c_invoiceline_id AS C_InvoiceLine_ID, i.c_invoice_id AS C_Invoice_ID, i.salesrep_id AS SalesRep_ID, i.c_bpartner_id AS C_BPartner_ID, i.c_bp_group_id AS C_BP_Group_ID, il.m_product_id AS M_Product_ID, p.m_product_category_id AS M_Product_Category_ID, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, i.issotrx AS IsSOTrx, i.c_doctype_id AS C_DocType_ID, i.docstatus AS DocStatus, i.ispaid AS IsPaid, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID, il.qtyinvoiced * i.multiplier AS QtyInvoiced, il.qtyentered * i.multiplier AS QtyEntered, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, productattribute(il.m_attributesetinstance_id) AS ProductAttribute, pasi.m_attributeset_id AS M_AttributeSet_ID, pasi.m_lot_id AS M_Lot_ID, pasi.guaranteedate AS GuaranteeDate, pasi.lot AS Lot, pasi.serno AS SerNo, il.pricelist AS PriceList, il.priceactual AS PriceActual, il.pricelimit AS PriceLimit, il.priceentered AS PriceEntered, CASE WHEN il.pricelist = 0 THEN 0 ELSE currencyround((il.pricelist - il.priceactual) / il.pricelist * 100, i.c_currency_id, 'N') END AS Discount, CASE WHEN il.pricelimit = 0 THEN 0 ELSE currencyround((il.priceactual - il.pricelimit) / il.pricelimit * 100, i.c_currency_id, 'N') END AS Margin, CASE WHEN il.pricelimit = 0 THEN 0 ELSE (il.priceactual - il.pricelimit) * il.qtyinvoiced * i.multiplier END AS MarginAmt, currencyround(i.multiplier * il.linenetamt, i.c_currency_id, 'N') AS LineNetAmt, currencyround(i.multiplier * il.pricelist * il.qtyinvoiced, i.c_currency_id, 'N') AS LineListAmt,
CASE WHEN COALESCE(il.pricelimit, 0) = 0 THEN currencyround(i.multiplier * il.linenetamt, i.c_currency_id, 'N') ELSE currencyround(i.multiplier * il.pricelimit * il.qtyinvoiced, i.c_currency_id, 'N') END AS LineLimitAmt, currencyround(i.multiplier * (il.pricelist * il.qtyinvoiced - il.linenetamt), i.c_currency_id, 'N') AS LineDiscountAmt, CASE WHEN COALESCE(il.pricelimit, 0) = 0 THEN 0 ELSE currencyround(i.multiplier * (il.linenetamt - il.pricelimit * il.qtyinvoiced), i.c_currency_id, 'N') END AS LineOverLimitAmt, il.ad_orgtrx_id AS AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_charge_id AS C_Charge_ID, il.c_orderline_id AS C_OrderLine_ID, il.c_tax_id AS C_Tax_ID, il.c_uom_id AS C_InvoiceLine_C_UOM_ID, il.description AS c_invoiceline_description, il.isdescription AS IsDescription, il.isprinted AS IsPrinted, il.line AS Line, il.linenetamt AS c_invoiceline_linenetamt, il.linetotalamt AS LineTotalAmt, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS M_RMALine_ID, il.processed AS Processed, il.ref_invoiceline_id AS Ref_InvoiceLine_ID, il.rramt AS RRAmt, il.rrstartdate AS RRStartDate, il.s_resourceassignment_id AS S_ResourceAssignment_ID, il.taxamt AS TaxAmt, il.user1_id AS User1_ID, il.user2_id AS User2_ID, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_ProductLine_C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip,
p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_id AS M_Product_M_Product_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.sku AS SKU, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, pasi.ad_org_id AS M_ASI_AD_Org_ID, pasi.created AS m_attributesetinstance_created, pasi.createdby AS M_ASI_CreatedBy, pasi.description AS M_ASI_Description, pasi.isactive AS m_attributesetinstance_isacti, pasi.serno AS m_attributesetinstance_serno, pasi.updated AS m_attributesetinstance_updated, pasi.updatedby AS M_ASI_UpdatedBy, pc.m_product_category_parent_id AS M_Product_Category_Parent_ID FROM rv_c_invoice i
JOIN c_invoiceline il ON i.c_invoice_id = il.c_invoice_id
LEFT JOIN m_product p ON il.m_product_id = p.m_product_id
LEFT JOIN m_product_category pc ON p.m_product_category_id = pc.m_product_category_id
LEFT JOIN m_attributesetinstance pasi ON il.m_attributesetinstance_id = pasi.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:14 PM CET
CREATE OR REPLACE VIEW RV_Asset_SumMonth(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, A_Asset_ID, A_Asset_Group_ID, M_Product_ID, Value, Name, Description, Help, GuaranteeDate, AssetServiceDate, C_BPartner_ID, AD_User_ID, SerNo, Lot, VersionNo, MovementDate, DeliveryCount) AS SELECT a.ad_client_id AS AD_Client_ID, a.ad_org_id AS AD_Org_ID, a.isactive AS IsActive, a.created AS Created, a.createdby AS CreatedBy, a.updated AS Updated, a.updatedby AS UpdatedBy, a.a_asset_id AS A_Asset_ID, a.a_asset_group_id AS A_Asset_Group_ID, a.m_product_id AS M_Product_ID, a.value AS Value, a.name AS Name, a.description AS Description, a.help AS Help, a.guaranteedate AS GuaranteeDate, a.assetservicedate AS AssetServiceDate, a.c_bpartner_id AS C_BPartner_ID, a.ad_user_id AS AD_User_ID, a.serno AS SerNo, a.lot AS Lot, a.versionno AS VersionNo, firstof(ad.movementdate, 'MM') AS MovementDate, count(*) AS DeliveryCount FROM a_asset a
JOIN a_asset_delivery ad ON a.a_asset_id = ad.a_asset_id GROUP BY a.ad_client_id, a.ad_org_id, a.isactive, a.created, a.createdby, a.updated, a.updatedby, a.a_asset_id, a.a_asset_group_id, a.m_product_id, a.value, a.name, a.description, a.help, a.guaranteedate, a.assetservicedate, a.c_bpartner_id, a.ad_user_id, a.serno, a.lot, a.versionno, (firstof(ad.movementdate, 'MM'))
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_RequestUpdates(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, R_Request_ID, AD_User_ID, IsSelfService, R_Group_ID, R_RequestType_ID, R_Category_ID) AS SELECT r_requestupdates.ad_client_id AS AD_Client_ID, r_requestupdates.ad_org_id AS AD_Org_ID, r_requestupdates.isactive AS IsActive, r_requestupdates.created AS Created, r_requestupdates.createdby AS CreatedBy, r_requestupdates.updated AS Updated, r_requestupdates.updatedby AS UpdatedBy, r_requestupdates.r_request_id AS R_Request_ID, r_requestupdates.ad_user_id AS AD_User_ID, r_requestupdates.isselfservice AS IsSelfService, NULLIF(1,1) AS R_Group_ID, NULLIF(1,1) AS R_RequestType_ID, NULLIF(1,1) AS R_Category_ID FROM r_requestupdates UNION SELECT u.ad_client_id AS AD_Client_ID, u.ad_org_id AS AD_Org_ID, u.isactive AS IsActive, u.created AS Created, u.createdby AS CreatedBy, u.updated AS Updated, u.updatedby AS UpdatedBy, r.r_request_id AS R_Request_ID, u.ad_user_id AS AD_User_ID, u.isselfservice AS IsSelfService, r.r_group_id AS R_Group_ID, NULL AS R_RequestType_ID, NULL AS R_Category_ID FROM r_groupupdates u
JOIN r_request r ON u.r_group_id = r.r_group_id UNION SELECT u.ad_client_id AS AD_Client_ID, u.ad_org_id AS AD_Org_ID, u.isactive AS IsActive, u.created AS Created, u.createdby AS CreatedBy, u.updated AS Updated, u.updatedby AS UpdatedBy, r.r_request_id AS R_Request_ID, u.ad_user_id AS AD_User_ID, u.isselfservice AS IsSelfService, NULL AS R_Group_ID, r.r_requesttype_id AS R_RequestType_ID, NULL AS R_Category_ID FROM r_requesttypeupdates u
JOIN r_request r ON u.r_requesttype_id = r.r_requesttype_id UNION SELECT u.ad_client_id AS AD_Client_ID, u.ad_org_id AS AD_Org_ID, u.isactive AS IsActive, u.created AS Created, u.createdby AS CreatedBy, u.updated AS Updated, u.updatedby AS UpdatedBy, r.r_request_id AS R_Request_ID, u.ad_user_id AS AD_User_ID, u.isselfservice AS IsSelfService, NULL AS R_Group_ID, NULL AS R_RequestType_ID, r.r_category_id AS R_Category_ID FROM r_categoryupdates u
JOIN r_request r ON u.r_category_id = r.r_category_id UNION SELECT r_request.ad_client_id AS AD_Client_ID, r_request.ad_org_id AS AD_Org_ID, r_request.isactive AS IsActive, r_request.created AS Created, r_request.createdby AS CreatedBy, r_request.updated AS Updated, r_request.updatedby AS UpdatedBy, r_request.r_request_id AS R_Request_ID, r_request.ad_user_id AS AD_User_ID, r_request.isselfservice AS IsSelfService, NULL AS R_Group_ID, NULL AS R_RequestType_ID, NULL AS R_Category_ID FROM r_request WHERE r_request.ad_user_id IS NOT NULL UNION SELECT u.ad_client_id AS AD_Client_ID, u.ad_org_id AS AD_Org_ID, u.isactive AS IsActive, u.created AS Created, u.createdby AS CreatedBy, u.updated AS Updated, u.updatedby AS UpdatedBy, r.r_request_id AS R_Request_ID, u.ad_user_id AS AD_User_ID, NULL AS IsSelfService, NULL AS R_Group_ID, NULL AS R_RequestType_ID, r.r_category_id AS R_Category_ID FROM ad_user u
JOIN r_request r ON u.ad_user_id = r.salesrep_id UNION SELECT r.ad_client_id AS AD_Client_ID, r.ad_org_id AS AD_Org_ID, u.isactive AS IsActive, r.created AS Created, r.createdby AS CreatedBy, r.updated AS Updated, r.updatedby AS UpdatedBy, r.r_request_id AS R_Request_ID, u.ad_user_id AS AD_User_ID, NULL AS IsSelfService, NULL AS R_Group_ID, NULL AS R_RequestType_ID, NULL AS R_Category_ID FROM r_request r
JOIN ad_user_roles u ON u.ad_role_id = r.ad_role_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_Day(AD_Client_ID, AD_Org_ID, SalesRep_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.salesrep_id AS SalesRep_ID, firstof(il.dateinvoiced, 'DD') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.salesrep_id, (firstof(il.dateinvoiced, 'DD')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_Month(AD_Client_ID, AD_Org_ID, SalesRep_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.salesrep_id AS SalesRep_ID, firstof(il.dateinvoiced, 'MM') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.salesrep_id, (firstof(il.dateinvoiced, 'MM')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_ProdWeek(AD_Client_ID, AD_Org_ID, M_Product_Category_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.m_product_category_id AS M_Product_Category_ID, firstof(il.dateinvoiced, 'DY') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.m_product_category_id, (firstof(il.dateinvoiced, 'DY')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_VendorMonth(AD_Client_ID, AD_Org_ID, C_BPartner_ID, M_Product_Category_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, po.c_bpartner_id AS C_BPartner_ID, il.m_product_category_id AS M_Product_Category_ID, firstof(il.dateinvoiced, 'MM') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id
JOIN m_product_po po ON il.m_product_id = po.m_product_id WHERE il.issotrx = 'Y' GROUP BY il.ad_client_id, il.ad_org_id, po.c_bpartner_id, il.m_product_category_id, (firstof(il.dateinvoiced, 'MM')), i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_Week(AD_Client_ID, AD_Org_ID, SalesRep_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.salesrep_id AS SalesRep_ID, firstof(il.dateinvoiced, 'DY') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.salesrep_id, (firstof(il.dateinvoiced, 'DY')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_CustomerProdQtr(AD_Client_ID, AD_Org_ID, C_BPartner_ID, M_Product_Category_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.c_bpartner_id AS C_BPartner_ID, il.m_product_category_id AS M_Product_Category_ID, firstof(il.dateinvoiced, 'Q') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.c_bpartner_id, il.m_product_category_id, (firstof(il.dateinvoiced, 'Q')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_CustomerVendQtr(AD_Client_ID, AD_Org_ID, C_BPartner_ID, Vendor_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.c_bpartner_id AS C_BPartner_ID, po.c_bpartner_id AS Vendor_ID, firstof(il.dateinvoiced, 'Q') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id
JOIN m_product_po po ON il.m_product_id = po.m_product_id WHERE il.issotrx = 'Y' GROUP BY il.ad_client_id, il.ad_org_id, il.c_bpartner_id, po.c_bpartner_id, (firstof(il.dateinvoiced, 'Q')), i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice(C_Invoice_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, IsSOTrx, DocumentNo, DocStatus, DocAction, IsPrinted, IsDiscountPrinted, Processing, Processed, IsTransferred, IsPaid, C_DocType_ID, C_DocTypeTarget_ID, C_Order_ID, Description, IsApproved, SalesRep_ID, DateInvoiced, DatePrinted, DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, C_BP_Group_ID, POReference, DateOrdered, C_Currency_ID, C_ConversionType_ID, PaymentRule, C_PaymentTerm_ID, M_PriceList_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, IsPayScheduleValid, InvoiceCollectionType, C_Country_ID, C_Region_ID, Postal, City, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, Multiplier, C_Invoice_AD_OrgTrx_ID, C_Invoice_C_ConversionType_ID, C_DunningLevel_ID, C_Payment_ID, c_invoice_dateordered, DunningGrace, GenerateTo, IsInDispute, c_invoice_ispayschedulevalid, c_invoice_isselfservice, IsTaxIncluded, M_RMA_ID, Posted, ProcessedOn, Ref_Invoice_ID, Reversal_ID, SendEMail, User1_ID, User2_ID, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_duns, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_naics, c_bp_name, c_bp_name2, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, c_bp_referenceno,
C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, TaxID, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, c_bp_value, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, C_BP_Location_C_Location_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, c_location_created, C_Location_CreatedBy, c_location_isactive, Postal_Add, RegionName, c_location_updated, C_Location_UpdatedBy) AS SELECT i.c_invoice_id AS C_Invoice_ID, i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.docaction AS DocAction, i.isprinted AS IsPrinted, i.isdiscountprinted AS IsDiscountPrinted, i.processing AS Processing, i.processed AS Processed, i.istransferred AS IsTransferred, i.ispaid AS IsPaid, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_order_id AS C_Order_ID, i.description AS Description, i.isapproved AS IsApproved, i.salesrep_id AS SalesRep_ID, i.dateinvoiced AS DateInvoiced, i.dateprinted AS DatePrinted, i.dateacct AS DateAcct, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.ad_user_id AS AD_User_ID, b.c_bp_group_id AS C_BP_Group_ID, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.paymentrule AS PaymentRule, i.c_paymentterm_id AS C_PaymentTerm_ID, i.m_pricelist_id AS M_PriceList_ID,
i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.ispayschedulevalid AS IsPayScheduleValid, i.invoicecollectiontype AS InvoiceCollectionType, loc.c_country_id AS C_Country_ID, loc.c_region_id AS C_Region_ID, loc.postal AS Postal, loc.city AS City, i.c_charge_id AS C_Charge_ID, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.chargeamt * '-1' ELSE i.chargeamt END AS ChargeAmt, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.totallines * '-1' ELSE i.totallines END AS TotalLines, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.grandtotal * '-1' ELSE i.grandtotal END AS GrandTotal, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN -1 ELSE 1 END AS Multiplier, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.c_conversiontype_id AS C_Invoice_C_ConversionType_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.c_payment_id AS C_Payment_ID, i.dateordered AS c_invoice_dateordered, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.isindispute AS IsInDispute, i.ispayschedulevalid AS c_invoice_ispayschedulevalid, i.isselfservice AS c_invoice_isselfservice, i.istaxincluded AS IsTaxIncluded, i.m_rma_id AS M_RMA_ID, i.posted AS Posted, i.processedon AS ProcessedOn, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.user1_id AS User1_ID, i.user2_id AS User2_ID, b.acqusitioncost AS c_bp_acqusitioncost, b.actuallifetimevalue AS c_bp_actuallifetimevalue, b.ad_language AS c_bp_ad_language, b.ad_orgbp_id AS C_BP_AD_OrgBP_ID, b.ad_org_id AS C_BP_AD_Org_ID, b.bpartner_parent_id AS C_BP_BPartner_Parent_ID, b.c_dunning_id AS C_BP_C_Dunning_ID, b.c_greeting_id AS C_BP_C_Greeting_ID, b.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, b.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, b.created AS c_bp_created, b.createdby AS C_BP_CreatedBy, b.c_taxgroup_id AS C_BP_C_TaxGroup_ID, b.deliveryrule AS c_bp_deliveryrule, b.deliveryviarule AS c_bp_deliveryviarule, b.description AS c_bp_description,
b.dunninggrace AS c_bp_dunninggrace, b.duns AS c_bp_duns, b.firstsale AS c_bp_firstsale, b.flatdiscount AS c_bp_flatdiscount, b.freightcostrule AS c_bp_freightcostrule, b.invoicerule AS c_bp_invoicerule, b.isactive AS c_bp_isactive, b.iscustomer AS c_bp_iscustomer, b.isdiscountprinted AS c_bp_isdiscountprinted, b.isemployee AS c_bp_isemployee, b.ismanufacturer AS c_bp_ismanufacturer, b.isonetime AS c_bp_isonetime, b.ispotaxexempt AS c_bp_ispotaxexempt, b.isprospect AS c_bp_isprospect, b.issalesrep AS c_bp_issalesrep, b.issummary AS c_bp_issummary, b.istaxexempt AS c_bp_istaxexempt, b.isvendor AS c_bp_isvendor, b.logo_id AS C_BP_Logo_ID, b.m_discountschema_id AS C_BP_M_DiscountSchema_ID, b.m_pricelist_id AS C_BP_M_PriceList_ID, b.naics AS c_bp_naics, b.name AS c_bp_name, b.name2 AS c_bp_name2, b.numberemployees AS c_bp_numberemployees, b.paymentrule AS c_bp_paymentrule, b.paymentrulepo AS c_bp_paymentrulepo, b.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, b.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, b.po_pricelist_id AS C_BP_PO_PriceList_ID, b.poreference AS c_bp_poreference, b.potentiallifetimevalue AS c_bp_potentiallifetimevalue, b.rating AS c_bp_rating, b.referenceno AS c_bp_referenceno, b.salesrep_id AS C_BP_SalesRep_ID, b.salesvolume AS c_bp_salesvolume, b.sendemail AS c_bp_sendemail, b.shareofcustomer AS c_bp_shareofcustomer, b.shelflifeminpct AS c_bp_shelflifeminpct, b.so_creditlimit AS c_bp_so_creditlimit, b.socreditstatus AS c_bp_socreditstatus, b.so_creditused AS c_bp_so_creditused, b.so_description AS c_bp_so_description, b.taxid AS TaxID, b.totalopenbalance AS c_bp_totalopenbalance, b.updated AS c_bp_updated, b.updatedby AS C_BP_UpdatedBy, b.url AS c_bp_url, b.value AS c_bp_value, bpl.ad_org_id AS C_BP_Location_AD_Org_ID, bpl.c_bpartner_id AS C_BP_Location_C_BPartner_ID, bpl.c_location_id AS C_BP_Location_C_Location_ID, bpl.created AS c_bp_location_created, bpl.createdby AS C_BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID,
bpl.fax AS c_bp_location_fax, bpl.isactive AS c_bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bp_location_name, bpl.phone AS c_bp_location_phone, bpl.phone2 AS c_bp_location_phone2, bpl.updated AS c_bp_location_updated, bpl.updatedby AS C_BP_Location_UpdatedBy, loc.address1 AS Address1, loc.address2 AS Address2, loc.address3 AS Address3, loc.address4 AS Address4, loc.ad_org_id AS C_Location_AD_Org_ID, loc.c_city_id AS C_City_ID, loc.created AS c_location_created, loc.createdby AS C_Location_CreatedBy, loc.isactive AS c_location_isactive, loc.postal_add AS Postal_Add, loc.regionname AS RegionName, loc.updated AS c_location_updated, loc.updatedby AS C_Location_UpdatedBy FROM c_invoice i
JOIN c_doctype d ON i.c_doctype_id = d.c_doctype_id
JOIN c_bpartner b ON i.c_bpartner_id = b.c_bpartner_id
JOIN c_bpartner_location bpl ON i.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location loc ON bpl.c_location_id = loc.c_location_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_InOutLineConfirm(M_InOutConfirm_ID, M_InOutLineConfirm_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, TargetQty, ConfirmedQty, DifferenceQty, ScrappedQty, Description, Processed, M_InOut_ID, DocumentNo, ConfirmType, IsApproved, IsCancelled, C_BPartner_ID, C_BPartner_Location_ID, M_Warehouse_ID, C_Order_ID, IsSOTrx, M_InOutLine_ID, M_Product_ID, M_AttributeSetInstance_ID, M_Locator_ID) AS SELECT cl.m_inoutconfirm_id AS M_InOutConfirm_ID, cl.m_inoutlineconfirm_id AS M_InOutLineConfirm_ID, cl.ad_client_id AS AD_Client_ID, cl.ad_org_id AS AD_Org_ID, cl.isactive AS IsActive, cl.created AS Created, cl.createdby AS CreatedBy, cl.updated AS Updated, cl.updatedby AS UpdatedBy, cl.targetqty AS TargetQty, cl.confirmedqty AS ConfirmedQty, cl.differenceqty AS DifferenceQty, cl.scrappedqty AS ScrappedQty, cl.description AS Description, cl.processed AS Processed, c.m_inout_id AS M_InOut_ID, c.documentno AS DocumentNo, c.confirmtype AS ConfirmType, c.isapproved AS IsApproved, c.iscancelled AS IsCancelled, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.m_warehouse_id AS M_Warehouse_ID, i.c_order_id AS C_Order_ID, i.issotrx AS IsSOTrx, cl.m_inoutline_id AS M_InOutLine_ID, il.m_product_id AS M_Product_ID, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, il.m_locator_id AS M_Locator_ID FROM m_inoutlineconfirm cl
JOIN m_inoutconfirm c ON cl.m_inoutconfirm_id = c.m_inoutconfirm_id
JOIN m_inout i ON c.m_inout_id = i.m_inout_id
JOIN m_inoutline il ON cl.m_inoutline_id = il.m_inoutline_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW T_InvoiceGL_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Invoice_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_Order_ID, Description, SalesRep_ID, DateInvoiced, DateAcct, C_PaymentTerm_ID, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, IsSelfService, C_Currency_ID, C_ConversionType_ID, GrandTotal, IsTaxIncluded, C_Campaign_ID, C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, User1_ID, User2_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, Fact_Acct_ID, C_AcctSchema_ID, Account_ID, C_Period_ID, GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType, AmtSourceDr, AmtSourceCr, AmtAcctDr, AmtAcctCr, C_UOM_ID, Qty, AD_PInstance_ID, APAR, OpenAmt, Percent, AmtRevalDr, AmtRevalCr, DateReval, C_ConversionTypeReval_ID, AmtSourceBalance, AmtAcctBalance, C_DocTypeReval_ID, AmtRevalDrDiff, AmtRevalCrDiff, IsAllCurrencies, AmtAcctOpenDr, AmtAcctOpenCr, AmtAcctOpenBalance, T_InvoiceGL_AD_Org_ID, T_InvoiceGL_C_Invoice_ID, t_invoicegl_created, T_InvoiceGL_CreatedBy, t_invoicegl_grandtotal, t_invoicegl_isactive, t_invoicegl_updated, T_InvoiceGL_UpdatedBy, C_Invoice_C_Charge_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, ChargeAmt, C_Payment_ID, DateOrdered, DatePrinted, DocAction, DunningGrace, GenerateTo, InvoiceCollectionType, IsApproved, IsDiscountPrinted, IsInDispute, IsPaid, IsPayScheduleValid, IsPrinted, IsTransferred, M_PriceList_ID, M_RMA_ID, PaymentRule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SendEMail, TotalLines, A_Asset_ID, Fact_Acct_AD_Org_ID, Fact_Acct_AD_OrgTrx_ID, Fact_Acct_AD_Table_ID, Fact_Acct_C_Activity_ID, Fact_Acct_C_BPartner_ID, Fact_Acct_C_Campaign_ID, Fact_Acct_C_Currency_ID, C_SubAcct_ID, fact_acct_dateacct, DateTrx, fact_acct_description, fact_acct_isactive, Line_ID, Fact_Acct_M_Product_ID, Record_ID, fact_acct_updated, Fact_Acct_UpdatedBy, Fact_Acct_User1_ID, Fact_Acct_User2_ID, UserElement1_ID, UserElement2_ID) AS SELECT i.ad_client_id AS AD_Client_ID,
i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.c_invoice_id AS C_Invoice_ID, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.c_doctype_id AS C_DocType_ID, i.c_order_id AS C_Order_ID, i.description AS Description, i.salesrep_id AS SalesRep_ID, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.ad_user_id AS AD_User_ID, i.isselfservice AS IsSelfService, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.grandtotal AS GrandTotal, i.istaxincluded AS IsTaxIncluded, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.user1_id AS User1_ID, i.user2_id AS User2_ID, fa.c_locfrom_id AS C_LocFrom_ID, fa.c_locto_id AS C_LocTo_ID, fa.c_salesregion_id AS C_SalesRegion_ID, fa.fact_acct_id AS Fact_Acct_ID, fa.c_acctschema_id AS C_AcctSchema_ID, fa.account_id AS Account_ID, fa.c_period_id AS C_Period_ID, fa.gl_category_id AS GL_Category_ID, fa.gl_budget_id AS GL_Budget_ID, fa.c_tax_id AS C_Tax_ID, fa.m_locator_id AS M_Locator_ID, fa.postingtype AS PostingType, fa.amtsourcedr AS AmtSourceDr, fa.amtsourcecr AS AmtSourceCr, fa.amtacctdr AS AmtAcctDr, fa.amtacctcr AS AmtAcctCr, fa.c_uom_id AS C_UOM_ID, fa.qty AS Qty, gl.ad_pinstance_id AS AD_PInstance_ID, gl.apar AS APAR, gl.openamt AS OpenAmt, gl.percent AS Percent, gl.amtrevaldr AS AmtRevalDr, gl.amtrevalcr AS AmtRevalCr, gl.datereval AS DateReval, gl.c_conversiontypereval_id AS C_ConversionTypeReval_ID, gl.amtsourcebalance AS AmtSourceBalance, gl.amtacctbalance AS AmtAcctBalance, gl.c_doctypereval_id AS C_DocTypeReval_ID, gl.amtrevaldrdiff AS AmtRevalDrDiff, gl.amtrevalcrdiff AS AmtRevalCrDiff, gl.isallcurrencies AS IsAllCurrencies,
fa.amtacctdr * gl.percent / 100 AS AmtAcctOpenDr, fa.amtacctcr * gl.percent / 100 AS AmtAcctOpenCr, (fa.amtacctdr - fa.amtacctcr) * gl.percent / 100 AS AmtAcctOpenBalance, gl.ad_org_id AS T_InvoiceGL_AD_Org_ID, gl.c_invoice_id AS T_InvoiceGL_C_Invoice_ID, gl.created AS t_invoicegl_created, gl.createdby AS T_InvoiceGL_CreatedBy, gl.grandtotal AS t_invoicegl_grandtotal, gl.isactive AS t_invoicegl_isactive, gl.updated AS t_invoicegl_updated, gl.updatedby AS T_InvoiceGL_UpdatedBy, i.c_charge_id AS C_Invoice_C_Charge_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_payment_id AS C_Payment_ID, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.docaction AS DocAction, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispaid AS IsPaid, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, fa.a_asset_id AS A_Asset_ID, fa.ad_org_id AS Fact_Acct_AD_Org_ID, fa.ad_orgtrx_id AS Fact_Acct_AD_OrgTrx_ID, fa.ad_table_id AS Fact_Acct_AD_Table_ID, fa.c_activity_id AS Fact_Acct_C_Activity_ID, fa.c_bpartner_id AS Fact_Acct_C_BPartner_ID, fa.c_campaign_id AS Fact_Acct_C_Campaign_ID, fa.c_currency_id AS Fact_Acct_C_Currency_ID, fa.c_subacct_id AS C_SubAcct_ID, fa.dateacct AS fact_acct_dateacct, fa.datetrx AS DateTrx, fa.description AS fact_acct_description, fa.isactive AS fact_acct_isactive, fa.line_id AS Line_ID, fa.m_product_id AS Fact_Acct_M_Product_ID, fa.record_id AS Record_ID,
fa.updated AS fact_acct_updated, fa.updatedby AS Fact_Acct_UpdatedBy, fa.user1_id AS Fact_Acct_User1_ID, fa.user2_id AS Fact_Acct_User2_ID, fa.userelement1_id AS UserElement1_ID, fa.userelement2_id AS UserElement2_ID FROM t_invoicegl gl
JOIN c_invoice i ON gl.c_invoice_id = i.c_invoice_id
JOIN fact_acct fa ON gl.fact_acct_id = fa.fact_acct_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_InOutLineMA_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, M_AttributeSetInstance_ID, MovementQty, M_Locator_ID) AS SELECT m.ad_client_id AS AD_Client_ID, m.ad_org_id AS AD_Org_ID, m.isactive AS IsActive, m.created AS Created, m.createdby AS CreatedBy, m.updated AS Updated, m.updatedby AS UpdatedBy, l.m_inout_id AS M_InOut_ID, m.m_inoutline_id AS M_InOutLine_ID, l.line AS Line, l.m_product_id AS M_Product_ID, m.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m.movementqty AS MovementQty, l.m_locator_id AS M_Locator_ID FROM m_inoutlinema m
JOIN m_inoutline l ON m.m_inoutline_id = l.m_inoutline_id UNION SELECT m_inoutline.ad_client_id AS AD_Client_ID, m_inoutline.ad_org_id AS AD_Org_ID, m_inoutline.isactive AS IsActive, m_inoutline.created AS Created, m_inoutline.createdby AS CreatedBy, m_inoutline.updated AS Updated, m_inoutline.updatedby AS UpdatedBy, m_inoutline.m_inout_id AS M_InOut_ID, m_inoutline.m_inoutline_id AS M_InOutLine_ID, m_inoutline.line AS Line, m_inoutline.m_product_id AS M_Product_ID, m_inoutline.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m_inoutline.movementqty AS MovementQty, m_inoutline.m_locator_id AS M_Locator_ID FROM m_inoutline
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_MovementLineMA_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Movement_ID, M_MovementLine_ID, Line, M_Product_ID, M_AttributeSetInstance_ID, MovementQty, M_Locator_ID, M_LocatorTo_ID) AS SELECT m.ad_client_id AS AD_Client_ID, m.ad_org_id AS AD_Org_ID, m.isactive AS IsActive, m.created AS Created, m.createdby AS CreatedBy, m.updated AS Updated, m.updatedby AS UpdatedBy, l.m_movement_id AS M_Movement_ID, m.m_movementline_id AS M_MovementLine_ID, l.line AS Line, l.m_product_id AS M_Product_ID, m.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m.movementqty AS MovementQty, l.m_locator_id AS M_Locator_ID, l.m_locatorto_id AS M_LocatorTo_ID FROM m_movementlinema m
JOIN m_movementline l ON m.m_movementline_id = l.m_movementline_id UNION SELECT m_movementline.ad_client_id AS AD_Client_ID, m_movementline.ad_org_id AS AD_Org_ID, m_movementline.isactive AS IsActive, m_movementline.created AS Created, m_movementline.createdby AS CreatedBy, m_movementline.updated AS Updated, m_movementline.updatedby AS UpdatedBy, m_movementline.m_movement_id AS M_Movement_ID, m_movementline.m_movementline_id AS M_MovementLine_ID, m_movementline.line AS Line, m_movementline.m_product_id AS M_Product_ID, m_movementline.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m_movementline.movementqty AS MovementQty, m_movementline.m_locator_id AS M_Locator_ID, m_movementline.m_locatorto_id AS M_LocatorTo_ID FROM m_movementline
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_M_Transaction(AD_Client_ID, AD_Org_ID, MovementDate, MovementQty, M_Product_ID, M_Locator_ID, M_AttributeSetInstance_ID, M_Product_Category_ID, Value, C_BPartner_ID, PricePO, PriceLastPO, PriceList, C_ProjectIssue_ID, m_transaction_created, M_Transaction_CreatedBy, m_transaction_isactive, M_InOutLine_ID, MovementType, M_ProductionLine_ID, M_Transaction_ID, PP_Cost_Collector_ID, m_transation_updated, M_Transation_UpdatedBy, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_Product_C_UOM_ID, m_product_description, DescriptionURL, m_product_discontinued, m_product_discontinuedat, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, m_product_name, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_upc, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, M_Product_PO_AD_Org_ID, M_Product_PO_C_Currency_ID, CostPerOrder, m_product_po_created, M_Product_PO_CreatedBy, M_Product_PO_C_UOM_ID, DeliveryTime_Actual, DeliveryTime_Promised, m_product_po_discontinued, m_product_po_discontinuedat, m_product_po_isactive, IsCurrentVendor, Manufacturer, M_Product_PO_M_Product_ID, Order_Min, Order_Pack, PriceEffective, PriceLastInv, QualityRating, RoyaltyAmt, m_product_po_upc, m_product_po_updated, M_Product_PO_UpdatedBy, VendorCategory, VendorProductNo) AS SELECT t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.movementdate AS MovementDate,
t.movementqty AS MovementQty, t.m_product_id AS M_Product_ID, t.m_locator_id AS M_Locator_ID, t.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_product_category_id AS M_Product_Category_ID, p.value AS Value, po.c_bpartner_id AS C_BPartner_ID, po.pricepo AS PricePO, po.pricelastpo AS PriceLastPO, po.pricelist AS PriceList, t.c_projectissue_id AS C_ProjectIssue_ID, t.created AS m_transaction_created, t.createdby AS M_Transaction_CreatedBy, t.isactive AS m_transaction_isactive, t.m_inoutline_id AS M_InOutLine_ID, t.movementtype AS MovementType, t.m_productionline_id AS M_ProductionLine_ID, t.m_transaction_id AS M_Transaction_ID, t.pp_cost_collector_id AS PP_Cost_Collector_ID, t.updated AS m_transation_updated, t.updatedby AS M_Transation_UpdatedBy, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued, p.discontinuedat AS m_product_discontinuedat, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID,
p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.name AS m_product_name, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.sku AS SKU, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.upc AS m_product_upc, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, po.ad_org_id AS M_Product_PO_AD_Org_ID, po.c_currency_id AS M_Product_PO_C_Currency_ID, po.costperorder AS CostPerOrder, po.created AS m_product_po_created, po.createdby AS M_Product_PO_CreatedBy, po.c_uom_id AS M_Product_PO_C_UOM_ID, po.deliverytime_actual AS DeliveryTime_Actual, po.deliverytime_promised AS DeliveryTime_Promised, po.discontinued AS m_product_po_discontinued, po.discontinuedat AS m_product_po_discontinuedat, po.isactive AS m_product_po_isactive, po.iscurrentvendor AS IsCurrentVendor, po.manufacturer AS Manufacturer, po.m_product_id AS M_Product_PO_M_Product_ID, po.order_min AS Order_Min, po.order_pack AS Order_Pack, po.priceeffective AS PriceEffective, po.pricelastinv AS PriceLastInv, po.qualityrating AS QualityRating, po.royaltyamt AS RoyaltyAmt, po.upc AS m_product_po_upc, po.updated AS m_product_po_updated, po.updatedby AS M_Product_PO_UpdatedBy, po.vendorcategory AS VendorCategory, po.vendorproductno AS VendorProductNo FROM m_transaction t
JOIN m_product p ON t.m_product_id = p.m_product_id
JOIN m_product_po po ON t.m_product_id = po.m_product_id WHERE po.iscurrentvendor = 'Y'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_RfQ_UnAnswered(AD_Client_ID, AD_Org_ID, C_RfQ_ID, Name, Description, Help, SalesRep_ID, C_RfQ_Topic_ID, QuoteType, IsQuoteTotalAmt, IsQuoteAllQty, C_Currency_ID, DateResponse, IsRfQResponseAccepted, DateWorkStart, DeliveryDays, DateWorkComplete, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID) AS SELECT q.ad_client_id AS AD_Client_ID, q.ad_org_id AS AD_Org_ID, q.c_rfq_id AS C_RfQ_ID, q.name AS Name, q.description AS Description, q.help AS Help, q.salesrep_id AS SalesRep_ID, q.c_rfq_topic_id AS C_RfQ_Topic_ID, q.quotetype AS QuoteType, q.isquotetotalamt AS IsQuoteTotalAmt, q.isquoteallqty AS IsQuoteAllQty, q.c_currency_id AS C_Currency_ID, q.dateresponse AS DateResponse, q.isrfqresponseaccepted AS IsRfQResponseAccepted, q.dateworkstart AS DateWorkStart, q.deliverydays AS DeliveryDays, q.dateworkcomplete AS DateWorkComplete, r.c_bpartner_id AS C_BPartner_ID, r.c_bpartner_location_id AS C_BPartner_Location_ID, r.ad_user_id AS AD_User_ID FROM c_rfq q
JOIN c_rfqresponse r ON q.c_rfq_id = r.c_rfq_id WHERE r.iscomplete = 'N' AND q.processed = 'N'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_RfQResponse(AD_Client_ID, AD_Org_ID, C_RfQ_ID, C_RfQ_Topic_ID, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, C_RfQResponse_ID, C_Currency_ID, DateResponse, DateWorkStart, DeliveryDays, DateWorkComplete, Price, Ranking, IsSelfService, Description, Help, M_Product_ID, M_AttributeSetInstance_ID, Line, LineDateWorkStart, LineDeliveryDays, LineDateWorkComplete, LineDescription, LineHelp, C_UOM_ID, Qty, BenchmarkPrice, BenchmarkDifference, QtyPrice, Discount, QtyRanking) AS SELECT q.ad_client_id AS AD_Client_ID, q.ad_org_id AS AD_Org_ID, q.c_rfq_id AS C_RfQ_ID, q.c_rfq_topic_id AS C_RfQ_Topic_ID, r.c_bpartner_id AS C_BPartner_ID, r.c_bpartner_location_id AS C_BPartner_Location_ID, r.ad_user_id AS AD_User_ID, r.c_rfqresponse_id AS C_RfQResponse_ID, r.c_currency_id AS C_Currency_ID, r.dateresponse AS DateResponse, r.dateworkstart AS DateWorkStart, r.deliverydays AS DeliveryDays, r.dateworkcomplete AS DateWorkComplete, r.price AS Price, r.ranking AS Ranking, r.isselfservice AS IsSelfService, r.description AS Description, r.help AS Help, ql.m_product_id AS M_Product_ID, ql.m_attributesetinstance_id AS M_AttributeSetInstance_ID, ql.line AS Line, rl.dateworkstart AS LineDateWorkStart, rl.deliverydays AS LineDeliveryDays, rl.dateworkcomplete AS LineDateWorkComplete, rl.description AS LineDescription, rl.help AS LineHelp, qlq.c_uom_id AS C_UOM_ID, qlq.qty AS Qty, qlq.benchmarkprice AS BenchmarkPrice, rlq.price - qlq.benchmarkprice AS BenchmarkDifference, rlq.price AS QtyPrice, rlq.discount AS Discount, rlq.ranking AS QtyRanking FROM c_rfq q
JOIN c_rfqline ql ON q.c_rfq_id = ql.c_rfq_id
JOIN c_rfqlineqty qlq ON ql.c_rfqline_id = qlq.c_rfqline_id
JOIN c_rfqresponse r ON q.c_rfq_id = r.c_rfq_id
JOIN c_rfqresponseline rl ON r.c_rfqresponse_id = rl.c_rfqresponse_id AND ql.c_rfqline_id = rl.c_rfqline_id
JOIN c_rfqresponselineqty rlq ON rl.c_rfqresponseline_id = rlq.c_rfqresponseline_id AND qlq.c_rfqlineqty_id = rlq.c_rfqlineqty_id WHERE r.iscomplete = 'Y' AND q.processed = 'N'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_InOutConfirm_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOutConfirm_ID, DocumentNo, ConfirmType, IsApproved, IsCancelled, Description, M_InOut_ID, ShipDescription, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, SalesRep_ID, C_DocType_ID, DocumentType, C_Order_ID, DateOrdered, MovementDate, MovementType, M_Warehouse_ID, POReference, DeliveryRule, FreightCostRule, DeliveryViaRule, M_Shipper_ID, PriorityRule, Processed) AS SELECT ioc.ad_client_id AS AD_Client_ID, ioc.ad_org_id AS AD_Org_ID, ioc.isactive AS IsActive, ioc.created AS Created, ioc.createdby AS CreatedBy, ioc.updated AS Updated, ioc.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, ioc.m_inoutconfirm_id AS M_InOutConfirm_ID, ioc.documentno AS DocumentNo, ioc.confirmtype AS ConfirmType, ioc.isapproved AS IsApproved, ioc.iscancelled AS IsCancelled, ioc.description AS Description, io.m_inout_id AS M_InOut_ID, io.description AS ShipDescription, io.c_bpartner_id AS C_BPartner_ID, io.c_bpartner_location_id AS C_BPartner_Location_ID, io.ad_user_id AS AD_User_ID, io.salesrep_id AS SalesRep_ID, io.c_doctype_id AS C_DocType_ID, dt.printname AS DocumentType, io.c_order_id AS C_Order_ID, io.dateordered AS DateOrdered, io.movementdate AS MovementDate, io.movementtype AS MovementType, io.m_warehouse_id AS M_Warehouse_ID, io.poreference AS POReference, io.deliveryrule AS DeliveryRule, io.freightcostrule AS FreightCostRule, io.deliveryviarule AS DeliveryViaRule, io.m_shipper_id AS M_Shipper_ID, io.priorityrule AS PriorityRule, ioc.processed AS Processed FROM m_inoutconfirm ioc
JOIN m_inout io ON ioc.m_inout_id = io.m_inout_id
JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_Allocation(C_AllocationHdr_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, DocumentNo, Description, DateTrx, DateAcct, C_Currency_ID, ApprovalAmt, IsManual, DocStatus, DocAction, Processed, C_AllocationLine_ID, C_Invoice_ID, C_BPartner_ID, C_Order_ID, C_Payment_ID, C_CashLine_ID, Amount, DiscountAmt, WriteOffAmt, OverUnderAmt, IsApproved, Posted, C_AllocationLine_AD_Org_ID, c_allocationline_created, C_AllocationLine_CreatedBy, c_allocationline_datetrx, c_allocationline_isactive, c_allocationline_ismanual, c_allocationline_updated, C_AllocationLine_UpdatedBy) AS SELECT h.c_allocationhdr_id AS C_AllocationHdr_ID, h.ad_client_id AS AD_Client_ID, h.ad_org_id AS AD_Org_ID, h.isactive AS IsActive, h.created AS Created, h.createdby AS CreatedBy, h.updated AS Updated, h.updatedby AS UpdatedBy, h.documentno AS DocumentNo, h.description AS Description, h.datetrx AS DateTrx, h.dateacct AS DateAcct, h.c_currency_id AS C_Currency_ID, h.approvalamt AS ApprovalAmt, h.ismanual AS IsManual, h.docstatus AS DocStatus, h.docaction AS DocAction, h.processed AS Processed, l.c_allocationline_id AS C_AllocationLine_ID, l.c_invoice_id AS C_Invoice_ID, l.c_bpartner_id AS C_BPartner_ID, l.c_order_id AS C_Order_ID, l.c_payment_id AS C_Payment_ID, l.c_cashline_id AS C_CashLine_ID, l.amount AS Amount, l.discountamt AS DiscountAmt, l.writeoffamt AS WriteOffAmt, l.overunderamt AS OverUnderAmt, h.isapproved AS IsApproved, h.posted AS Posted, l.ad_org_id AS C_AllocationLine_AD_Org_ID, l.created AS c_allocationline_created, l.createdby AS C_AllocationLine_CreatedBy, l.datetrx AS c_allocationline_datetrx, l.isactive AS c_allocationline_isactive, l.ismanual AS c_allocationline_ismanual, l.updated AS c_allocationline_updated, l.updatedby AS C_AllocationLine_UpdatedBy FROM c_allocationhdr h
JOIN c_allocationline l ON h.c_allocationhdr_id = l.c_allocationhdr_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_CostSummary(AD_Client_ID, AD_Org_ID, IsActive, CreatedBy, Created, UpdatedBy, Updated, M_Product_ID, Value, Name, UPC, IsBOM, ProductType, M_Product_Category_ID, M_CostType_ID, C_AcctSchema_ID, C_Currency_ID, CurrentCostPrice, FutureCostPrice) AS SELECT c.ad_client_id AS AD_Client_ID, c.ad_org_id AS AD_Org_ID, c.isactive AS IsActive, 0 AS CreatedBy, getdate() AS Created, 0 AS UpdatedBy, getdate() AS Updated, p.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.upc AS UPC, p.isbom AS IsBOM, p.producttype AS ProductType, p.m_product_category_id AS M_Product_Category_ID, c.m_costtype_id AS M_CostType_ID, acct.c_acctschema_id AS C_AcctSchema_ID, acct.c_currency_id AS C_Currency_ID, sum(c.currentcostprice) AS CurrentCostPrice, sum(c.futurecostprice) AS FutureCostPrice FROM m_cost c
JOIN m_product p ON c.m_product_id = p.m_product_id
JOIN c_acctschema acct ON c.c_acctschema_id = acct.c_acctschema_id WHERE acct.m_costtype_id = c.m_costtype_id GROUP BY c.ad_client_id, c.ad_org_id, c.isactive, p.m_product_id, p.value, p.name, p.upc, p.isbom, p.producttype, p.m_product_category_id, c.m_costtype_id, acct.c_acctschema_id, acct.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_CostDetail(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Product_ID, Value, Name, UPC, IsBOM, ProductType, M_Product_Category_ID, M_InOutLine_ID, C_InvoiceLine_ID, M_AttributeSetInstance_ID, M_AttributeSet_ID, Lot, SerNo, C_AcctSchema_ID, C_Currency_ID, Amt, Qty, Description, Processed, C_OrderLine_ID, C_ProjectIssue_ID, CumulatedAmt, CumulatedQty, CurrentCostPrice, CurrentQty, DeltaAmt, DeltaQty, IsSOTrx, M_CostDetail_ID, M_CostElement_ID, M_InventoryLine_ID, M_MovementLine_ID, M_ProductionLine_ID, PP_Cost_Collector_ID, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Locator_ID, m_product_processing, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, C_AcctSchema_AD_Org_ID, c_acctschema_description, c_acctschema_name, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, GuaranteeDate, m_asi_isactive, M_Lot_ID, m_asi_updated, M_ASI_UpdatedBy) AS SELECT c.ad_client_id AS AD_Client_ID, c.ad_org_id AS AD_Org_ID, c.isactive AS IsActive, c.created AS Created, c.createdby AS CreatedBy, c.updated AS Updated, c.updatedby AS UpdatedBy, p.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.upc AS UPC, p.isbom AS IsBOM, p.producttype AS ProductType,
p.m_product_category_id AS M_Product_Category_ID, c.m_inoutline_id AS M_InOutLine_ID, c.c_invoiceline_id AS C_InvoiceLine_ID, asi.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.lot AS Lot, asi.serno AS SerNo, acct.c_acctschema_id AS C_AcctSchema_ID, acct.c_currency_id AS C_Currency_ID, c.amt AS Amt, c.qty AS Qty, c.description AS Description, c.processed AS Processed, c.c_orderline_id AS C_OrderLine_ID, c.c_projectissue_id AS C_ProjectIssue_ID, c.cumulatedamt AS CumulatedAmt, c.cumulatedqty AS CumulatedQty, c.currentcostprice AS CurrentCostPrice, c.currentqty AS CurrentQty, c.deltaamt AS DeltaAmt, c.deltaqty AS DeltaQty, c.issotrx AS IsSOTrx, c.m_costdetail_id AS M_CostDetail_ID, c.m_costelement_id AS M_CostElement_ID, c.m_inventoryline_id AS M_InventoryLine_ID, c.m_movementline_id AS M_MovementLine_ID, c.m_productionline_id AS M_ProductionLine_ID, c.pp_cost_collector_id AS PP_Cost_Collector_ID, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary,
p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.processing AS m_product_processing, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.sku AS SKU, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, acct.ad_org_id AS C_AcctSchema_AD_Org_ID, acct.description AS c_acctschema_description, acct.name AS c_acctschema_name, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.guaranteedate AS GuaranteeDate, asi.isactive AS m_asi_isactive, asi.m_lot_id AS M_Lot_ID, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM m_costdetail c
JOIN m_product p ON c.m_product_id = p.m_product_id
JOIN c_acctschema acct ON c.c_acctschema_id = acct.c_acctschema_id
JOIN m_attributesetinstance asi ON c.m_attributesetinstance_id = asi.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_ProductQtr(AD_Client_ID, AD_Org_ID, M_Product_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.m_product_id AS M_Product_ID, firstof(il.dateinvoiced, 'Q') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.m_product_id, (firstof(il.dateinvoiced, 'Q')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_ProductMonth(AD_Client_ID, AD_Org_ID, M_Product_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.m_product_id AS M_Product_ID, firstof(il.dateinvoiced, 'MM') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.m_product_id, (firstof(il.dateinvoiced, 'MM')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_RfQResponseLine_v(C_RfQResponse_ID, C_RfQResponseLine_ID, C_RfQLine_ID, C_RfQResponseLineQty_ID, C_RfQLineQty_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Line, M_Product_ID, M_AttributeSetInstance_ID, Name, Description, DocumentNote, UPC, SKU, ProductValue, Help, DateWorkStart, DeliveryDays, C_UOM_ID, UOMSymbol, BenchmarkPrice, Qty, Price, Discount) AS SELECT rrl.c_rfqresponse_id AS C_RfQResponse_ID, rrl.c_rfqresponseline_id AS C_RfQResponseLine_ID, rrl.c_rfqline_id AS C_RfQLine_ID, rq.c_rfqresponselineqty_id AS C_RfQResponseLineQty_ID, rq.c_rfqlineqty_id AS C_RfQLineQty_ID, rrl.ad_client_id AS AD_Client_ID, rrl.ad_org_id AS AD_Org_ID, rrl.isactive AS IsActive, rrl.created AS Created, rrl.createdby AS CreatedBy, rrl.updated AS Updated, rrl.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, rl.line AS Line, rl.m_product_id AS M_Product_ID, rl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, COALESCE(p.name || productattribute(rl.m_attributesetinstance_id), rl.description) AS Name, CASE WHEN p.name IS NOT NULL THEN rl.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, rl.help AS Help, rl.dateworkstart AS DateWorkStart, rl.deliverydays AS DeliveryDays, q.c_uom_id AS C_UOM_ID, uom.uomsymbol AS UOMSymbol, q.benchmarkprice AS BenchmarkPrice, q.qty AS Qty, rq.price AS Price, rq.discount AS Discount FROM c_rfqresponselineqty rq
JOIN c_rfqlineqty q ON rq.c_rfqlineqty_id = q.c_rfqlineqty_id
JOIN c_uom uom ON q.c_uom_id = uom.c_uom_id
JOIN c_rfqresponseline rrl ON rq.c_rfqresponseline_id = rrl.c_rfqresponseline_id
JOIN c_rfqline rl ON rrl.c_rfqline_id = rl.c_rfqline_id
LEFT JOIN m_product p ON rl.m_product_id = p.m_product_id WHERE rq.isactive = 'Y' AND q.isactive = 'Y' AND rrl.isactive = 'Y' AND rl.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_RfQResponse_v(C_RfQResponse_ID, C_RfQ_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Org_Location_ID, TaxID, Name, Description, Help, C_Currency_ID, ISO_Code, DateResponse, DateWorkStart, DeliveryDays, C_BPartner_ID, BPName, BPName2, C_BPartner_Location_ID, C_Location_ID, AD_User_ID, Title, Phone, ContactName) AS SELECT rr.c_rfqresponse_id AS C_RfQResponse_ID, rr.c_rfq_id AS C_RfQ_ID, rr.ad_client_id AS AD_Client_ID, rr.ad_org_id AS AD_Org_ID, rr.isactive AS IsActive, rr.created AS Created, rr.createdby AS CreatedBy, rr.updated AS Updated, rr.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, r.name AS Name, r.description AS Description, r.help AS Help, r.c_currency_id AS C_Currency_ID, c.iso_code AS ISO_Code, r.dateresponse AS DateResponse, r.dateworkstart AS DateWorkStart, r.deliverydays AS DeliveryDays, rr.c_bpartner_id AS C_BPartner_ID, bp.name AS BPName, bp.name2 AS BPName2, rr.c_bpartner_location_id AS C_BPartner_Location_ID, bpl.c_location_id AS C_Location_ID, rr.ad_user_id AS AD_User_ID, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName FROM c_rfqresponse rr
JOIN c_rfq r ON rr.c_rfq_id = r.c_rfq_id
JOIN ad_orginfo oi ON rr.ad_org_id = oi.ad_org_id
JOIN c_currency c ON r.c_currency_id = c.c_currency_id
JOIN c_bpartner bp ON rr.c_bpartner_id = bp.c_bpartner_id
JOIN c_bpartner_location bpl ON rr.c_bpartner_location_id = bpl.c_bpartner_location_id
LEFT JOIN ad_user bpc ON rr.ad_user_id = bpc.ad_user_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_RfQResponseLineQty_v(C_RfQResponseLine_ID, C_RfQResponseLineQty_ID, C_RfQLineQty_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_UOM_ID, UOMSymbol, Qty, Price, Discount) AS SELECT rq.c_rfqresponseline_id AS C_RfQResponseLine_ID, rq.c_rfqresponselineqty_id AS C_RfQResponseLineQty_ID, rq.c_rfqlineqty_id AS C_RfQLineQty_ID, rq.ad_client_id AS AD_Client_ID, rq.ad_org_id AS AD_Org_ID, rq.isactive AS IsActive, rq.created AS Created, rq.createdby AS CreatedBy, rq.updated AS Updated, rq.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, q.c_uom_id AS C_UOM_ID, uom.uomsymbol AS UOMSymbol, q.qty AS Qty, rq.price AS Price, rq.discount AS Discount FROM c_rfqresponselineqty rq
JOIN c_rfqlineqty q ON rq.c_rfqlineqty_id = q.c_rfqlineqty_id
JOIN c_uom uom ON q.c_uom_id = uom.c_uom_id WHERE rq.isactive = 'Y' AND q.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_Invoice_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Invoice_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, DocumentType, DocumentTypeNote, C_Order_ID, SalesRep_ID, SalesRep_Name, DateInvoiced, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, Postal, Description, POReference, DateOrdered, C_Currency_ID, PaymentTerm, PaymentTermNote, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsTaxIncluded, C_Campaign_ID, C_Project_ID, C_Activity_ID, IsPaid, Logo_ID, C_Invoice_AD_OrgTrx_ID, AD_User_ID, C_BPartner_Location_ID, C_ConversionType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, C_Payment_ID, DateAcct, DatePrinted, DocAction, DunningGrace, GenerateTo, InvoiceCollectionType, IsApproved, IsDiscountPrinted, IsInDispute, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, IsTransferred, M_RMA_ID, PaymentRule, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SendEMail, User1_ID, User2_ID, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDefault, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value, bp_acqusitioncost, bp_actuallifetimevalue, bp_ad_language, BP_AD_OrgBP_ID, BP_AD_Org_ID, BP_BPartner_Parent_ID, BP_C_BP_Group_ID, BP_C_Dunning_ID, BP_C_Greeting_ID, BP_C_InvoiceSchedule_ID, BP_C_PaymentTerm_ID, bp_created, BP_CreatedBy, BP_C_TaxGroup_ID, bp_deliveryrule, bp_deliveryviarule, bp_description, bp_dunninggrace, bp_firstsale, bp_flatdiscount, bp_freightcostrule, bp_invoicerule, bp_isactive, bp_iscustomer, bp_isdiscountprinted, bp_isemployee, bp_ismanufacturer, bp_isonetime, bp_ispotaxexempt, bp_isprospect, bp_issalesrep,
bp_issummary, bp_istaxexempt, bp_isvendor, BP_Logo_ID, BP_M_DiscountSchema_ID, BP_M_PriceList_ID, bp_naics, bp_numberemployees, bp_paymentrule, bp_paymentrulepo, BP_PO_DiscountSchema_ID, BP_PO_PaymentTerm_ID, BP_PO_PriceList_ID, bp_poreference, bp_potentiallifetimevalue, bp_rating, BP_SalesRep_ID, bp_salesvolume, bp_sendemail, bp_shareofcustomer, bp_shelflifeminpct, bp_so_creditlimit, bp_socreditstatus, bp_so_creditused, bp_so_description, bp_totalopenbalance, bp_updated, BP_UpdatedBy, bp_url, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, BP_Location_AD_Org_ID, BP_Location_C_BPartner_ID, bp_location_created, BP_Location_CreatedBy, C_SalesRegion_ID, bp_location_fax, bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, bp_location_name, bp_location_phone, bp_location_phone2, bp_location_updated, BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, user_greeting_isactive, user_greeting_isfirstnameonly, user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID,
SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, bpartner_acqusitioncost, bpartner_actuallifetimevalue, bpartner_ad_language, BPartner_AD_OrgBP_ID, BPartner_AD_Org_ID, BPartner_BPartner_Parent_ID, BPartner_C_BP_Group_ID, BPartner_C_Dunning_ID, BPartner_C_Greeting_ID, BPartner_C_InvoiceSchedule_ID, BPartner_C_PaymentTerm_ID, bpartner_created, BPartner_CreatedBy, BPartner_C_TaxGroup_ID, bpartner_deliveryrule, bpartner_deliveryviarule, bpartner_description, bpartner_dunninggrace, bpartner_duns, bpartner_firstsale, bpartner_flatdiscount, bpartner_freightcostrule, bpartner_invoicerule, bpartner_isactive, bpartner_iscustomer, bpartner_isdiscountprinted, bpartner_isemployee, bpartner_ismanufacturer, bpartner_isonetime, bpartner_ispotaxexempt, bpartner_isprospect, bpartner_issalesrep, bpartner_issummary, bpartner_istaxexempt, bpartner_isvendor, BPartner_Logo_ID, BPartner_M_DiscountSchema_ID, BPartner_M_PriceList_ID, bpartner_naics, bpartner_name2, bpartner_numberemployees, bpartner_paymentrule, bpartner_paymentrulepo, BPartner_PO_DiscountSchema_ID, BPartner_PO_PaymentTerm_ID, BPartner_PO_PriceList_ID, bpartner_poreference, bpartner_potentiallifetimeval, bpartner_rating, bpartner_referenceno, BPartner_SalesRep_ID, bpartner_salesvolume, bpartner_sendemail, bpartner_shareofcustomer, bpartner_shelflifeminpct, bpartner_so_creditlimit, bpartner_socreditstatus, bpartner_so_creditused, bpartner_so_description, bpartner_taxid, bpartner_totalopenbalance, bpartner_updated, BPartner_UpdatedBy, bpartner_url, BPartner_Value, CurSymbol, cur_description) AS SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated,
i.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, i.c_invoice_id AS C_Invoice_ID, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.c_doctype_id AS C_DocType_ID, i.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, i.c_order_id AS C_Order_ID, i.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, i.dateinvoiced AS DateInvoiced, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, l.postal || l.postal_add AS Postal, i.description AS Description, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, pt.name AS PaymentTerm, pt.documentnote AS PaymentTermNote, i.c_charge_id AS C_Charge_ID, i.chargeamt AS ChargeAmt, i.totallines AS TotalLines, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, i.m_pricelist_id AS M_PriceList_ID, i.istaxincluded AS IsTaxIncluded, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.ispaid AS IsPaid, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.c_payment_id AS C_Payment_ID, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.docaction AS DocAction, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted,
i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.istransferred AS IsTransferred, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.user1_id AS User1_ID, i.user2_id AS User2_ID, pt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_ID, pt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, pt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value, bp.acqusitioncost AS bp_acqusitioncost, bp.actuallifetimevalue AS bp_actuallifetimevalue, bp.ad_language AS bp_ad_language, bp.ad_orgbp_id AS BP_AD_OrgBP_ID, bp.ad_org_id AS BP_AD_Org_ID, bp.bpartner_parent_id AS BP_BPartner_Parent_ID, bp.c_bp_group_id AS BP_C_BP_Group_ID, bp.c_dunning_id AS BP_C_Dunning_ID, bp.c_greeting_id AS BP_C_Greeting_ID, bp.c_invoiceschedule_id AS BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS BP_C_PaymentTerm_ID, bp.created AS bp_created, bp.createdby AS BP_CreatedBy, bp.c_taxgroup_id AS BP_C_TaxGroup_ID, bp.deliveryrule AS bp_deliveryrule, bp.deliveryviarule AS bp_deliveryviarule, bp.description AS bp_description, bp.dunninggrace AS bp_dunninggrace, bp.firstsale AS bp_firstsale, bp.flatdiscount AS bp_flatdiscount, bp.freightcostrule AS bp_freightcostrule, bp.invoicerule AS bp_invoicerule, bp.isactive AS bp_isactive,
bp.iscustomer AS bp_iscustomer, bp.isdiscountprinted AS bp_isdiscountprinted, bp.isemployee AS bp_isemployee, bp.ismanufacturer AS bp_ismanufacturer, bp.isonetime AS bp_isonetime, bp.ispotaxexempt AS bp_ispotaxexempt, bp.isprospect AS bp_isprospect, bp.issalesrep AS bp_issalesrep, bp.issummary AS bp_issummary, bp.istaxexempt AS bp_istaxexempt, bp.isvendor AS bp_isvendor, bp.logo_id AS BP_Logo_ID, bp.m_discountschema_id AS BP_M_DiscountSchema_ID, bp.m_pricelist_id AS BP_M_PriceList_ID, bp.naics AS bp_naics, bp.numberemployees AS bp_numberemployees, bp.paymentrule AS bp_paymentrule, bp.paymentrulepo AS bp_paymentrulepo, bp.po_discountschema_id AS BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS BP_PO_PriceList_ID, bp.poreference AS bp_poreference, bp.potentiallifetimevalue AS bp_potentiallifetimevalue, bp.rating AS bp_rating, bp.salesrep_id AS BP_SalesRep_ID, bp.salesvolume AS bp_salesvolume, bp.sendemail AS bp_sendemail, bp.shareofcustomer AS bp_shareofcustomer, bp.shelflifeminpct AS bp_shelflifeminpct, bp.so_creditlimit AS bp_so_creditlimit, bp.socreditstatus AS bp_socreditstatus, bp.so_creditused AS bp_so_creditused, bp.so_description AS bp_so_description, bp.totalopenbalance AS bp_totalopenbalance, bp.updated AS bp_updated, bp.updatedby AS BP_UpdatedBy, bp.url AS bp_url, bpg.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpg.name AS c_greeting_name, bpl.ad_org_id AS BP_Location_AD_Org_ID, bpl.c_bpartner_id AS BP_Location_C_BPartner_ID, bpl.created AS bp_location_created, bpl.createdby AS BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS bp_location_fax, bpl.isactive AS bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS bp_location_name, bpl.phone AS bp_location_phone, bpl.phone2 AS bp_location_phone2, bpl.updated AS bp_location_updated,
bpl.updatedby AS BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcg.isactive AS user_greeting_isactive, bpcg.isfirstnameonly AS user_greeting_isfirstnameonly, bpcg.name AS user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy,
u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS bpartner_acqusitioncost, ubp.actuallifetimevalue AS bpartner_actuallifetimevalue, ubp.ad_language AS bpartner_ad_language, ubp.ad_orgbp_id AS BPartner_AD_OrgBP_ID, ubp.ad_org_id AS BPartner_AD_Org_ID, ubp.bpartner_parent_id AS BPartner_BPartner_Parent_ID, ubp.c_bp_group_id AS BPartner_C_BP_Group_ID, ubp.c_dunning_id AS BPartner_C_Dunning_ID, ubp.c_greeting_id AS BPartner_C_Greeting_ID, ubp.c_invoiceschedule_id AS BPartner_C_InvoiceSchedule_ID, ubp.c_paymentterm_id AS BPartner_C_PaymentTerm_ID, ubp.created AS bpartner_created, ubp.createdby AS BPartner_CreatedBy, ubp.c_taxgroup_id AS BPartner_C_TaxGroup_ID, ubp.deliveryrule AS bpartner_deliveryrule, ubp.deliveryviarule AS bpartner_deliveryviarule, ubp.description AS bpartner_description, ubp.dunninggrace AS bpartner_dunninggrace, ubp.duns AS bpartner_duns, ubp.firstsale AS bpartner_firstsale, ubp.flatdiscount AS bpartner_flatdiscount, ubp.freightcostrule AS bpartner_freightcostrule, ubp.invoicerule AS bpartner_invoicerule, ubp.isactive AS bpartner_isactive, ubp.iscustomer AS bpartner_iscustomer, ubp.isdiscountprinted AS bpartner_isdiscountprinted, ubp.isemployee AS bpartner_isemployee, ubp.ismanufacturer AS bpartner_ismanufacturer,
ubp.isonetime AS bpartner_isonetime, ubp.ispotaxexempt AS bpartner_ispotaxexempt, ubp.isprospect AS bpartner_isprospect, ubp.issalesrep AS bpartner_issalesrep, ubp.issummary AS bpartner_issummary, ubp.istaxexempt AS bpartner_istaxexempt, ubp.isvendor AS bpartner_isvendor, ubp.logo_id AS BPartner_Logo_ID, ubp.m_discountschema_id AS BPartner_M_DiscountSchema_ID, ubp.m_pricelist_id AS BPartner_M_PriceList_ID, ubp.naics AS bpartner_naics, ubp.name2 AS bpartner_name2, ubp.numberemployees AS bpartner_numberemployees, ubp.paymentrule AS bpartner_paymentrule, ubp.paymentrulepo AS bpartner_paymentrulepo, ubp.po_discountschema_id AS BPartner_PO_DiscountSchema_ID, ubp.po_paymentterm_id AS BPartner_PO_PaymentTerm_ID, ubp.po_pricelist_id AS BPartner_PO_PriceList_ID, ubp.poreference AS bpartner_poreference, ubp.potentiallifetimevalue AS bpartner_potentiallifetimeval, ubp.rating AS bpartner_rating, ubp.referenceno AS bpartner_referenceno, ubp.salesrep_id AS BPartner_SalesRep_ID, ubp.salesvolume AS bpartner_salesvolume, ubp.sendemail AS bpartner_sendemail, ubp.shareofcustomer AS bpartner_shareofcustomer, ubp.shelflifeminpct AS bpartner_shelflifeminpct, ubp.so_creditlimit AS bpartner_so_creditlimit, ubp.socreditstatus AS bpartner_socreditstatus, ubp.so_creditused AS bpartner_so_creditused, ubp.so_description AS bpartner_so_description, ubp.taxid AS bpartner_taxid, ubp.totalopenbalance AS bpartner_totalopenbalance, ubp.updated AS bpartner_updated, ubp.updatedby AS BPartner_UpdatedBy, ubp.url AS bpartner_url, ubp.value AS BPartner_Value, cur.cursymbol AS CurSymbol, cur.description AS cur_description FROM c_invoice i
JOIN c_doctype dt ON i.c_doctype_id = dt.c_doctype_id
JOIN c_paymentterm pt ON i.c_paymentterm_id = pt.c_paymentterm_id
JOIN c_bpartner bp ON i.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN c_bpartner_location bpl ON i.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON i.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
JOIN ad_orginfo oi ON i.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON i.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON i.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
LEFT JOIN c_currency cur ON i.c_currency_id = cur.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_Cash_Detail(C_Cash_ID, C_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_CashBook_ID, Name, StatementDate, DateAcct, Processed, Posted, Line, Description, CashType, C_Currency_ID, Amount, ConvertedAmt, C_BankAccount_ID, C_Invoice_ID, C_Charge_ID) AS SELECT cl.c_cash_id AS C_Cash_ID, cl.c_cashline_id AS C_CashLine_ID, c.ad_client_id AS AD_Client_ID, c.ad_org_id AS AD_Org_ID, cl.isactive AS IsActive, cl.created AS Created, cl.createdby AS CreatedBy, cl.updated AS Updated, cl.updatedby AS UpdatedBy, c.c_cashbook_id AS C_CashBook_ID, c.name AS Name, c.statementdate AS StatementDate, c.dateacct AS DateAcct, c.processed AS Processed, c.posted AS Posted, cl.line AS Line, cl.description AS Description, cl.cashtype AS CashType, cl.c_currency_id AS C_Currency_ID, cl.amount AS Amount, currencyconvert(cl.amount, cl.c_currency_id, cb.c_currency_id, c.statementdate, 0, c.ad_client_id, c.ad_org_id) AS ConvertedAmt, cl.c_bankaccount_id AS C_BankAccount_ID, cl.c_invoice_id AS C_Invoice_ID, cl.c_charge_id AS C_Charge_ID FROM c_cash c
JOIN c_cashline cl ON c.c_cash_id = cl.c_cash_id
JOIN c_cashbook cb ON c.c_cashbook_id = cb.c_cashbook_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_WarehousePrice(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Product_ID, M_PriceList_Version_ID, M_Warehouse_ID, Value, Name, UPC, SKU, C_UOM_ID, UOMSymbol, PriceList, PriceStd, Margin, PriceLimit, WarehouseName, QtyAvailable, QtyOnHand, QtyReserved, QtyOrdered, IsInstanceAttribute) AS SELECT w.ad_client_id AS AD_Client_ID, w.ad_org_id AS AD_Org_ID, CASE WHEN p.discontinued = 'N' THEN 'Y' ELSE 'N' END AS IsActive, pr.created AS Created, pr.createdby AS CreatedBy, pr.updated AS Updated, pr.updatedby AS UpdatedBy, p.m_product_id AS M_Product_ID, pr.m_pricelist_version_id AS M_PriceList_Version_ID, w.m_warehouse_id AS M_Warehouse_ID, p.value AS Value, p.name AS Name, p.upc AS UPC, p.sku AS SKU, uom.c_uom_id AS C_UOM_ID, uom.uomsymbol AS UOMSymbol, bompricelist(p.m_product_id, pr.m_pricelist_version_id) AS PriceList, bompricestd(p.m_product_id, pr.m_pricelist_version_id) AS PriceStd, bompricestd(p.m_product_id, pr.m_pricelist_version_id) - bompricelimit(p.m_product_id, pr.m_pricelist_version_id) AS Margin, bompricelimit(p.m_product_id, pr.m_pricelist_version_id) AS PriceLimit, w.name AS WarehouseName, bomqtyavailable(p.m_product_id, w.m_warehouse_id, 0) AS QtyAvailable, bomqtyonhand(p.m_product_id, w.m_warehouse_id, 0) AS QtyOnHand, bomqtyreserved(p.m_product_id, w.m_warehouse_id, 0) AS QtyReserved, bomqtyordered(p.m_product_id, w.m_warehouse_id, 0) AS QtyOrdered, COALESCE(pa.isinstanceattribute, 'N') AS IsInstanceAttribute FROM m_product p
JOIN m_productprice pr ON p.m_product_id = pr.m_product_id
JOIN c_uom uom ON p.c_uom_id = uom.c_uom_id
LEFT JOIN m_attributeset pa ON p.m_attributeset_id = pa.m_attributeset_id
JOIN m_warehouse w ON p.ad_client_id = w.ad_client_id WHERE p.issummary = 'N' AND p.isactive = 'Y' AND pr.isactive = 'Y' AND w.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_Fact_Acct_Day(AD_Client_ID, AD_Org_ID, C_AcctSchema_ID, Account_ID, DateAcct, C_Period_ID, GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType, C_Currency_ID, AmtSourceDr, AmtSourceCr, AmtSource, AmtAcctDr, AmtAcctCr, AmtAcct, Rate, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, A_Asset_ID) AS SELECT fact_acct.ad_client_id AS AD_Client_ID, fact_acct.ad_org_id AS AD_Org_ID, fact_acct.c_acctschema_id AS C_AcctSchema_ID, fact_acct.account_id AS Account_ID, firstof(fact_acct.dateacct, 'DD') AS DateAcct, fact_acct.c_period_id AS C_Period_ID, fact_acct.gl_category_id AS GL_Category_ID, fact_acct.gl_budget_id AS GL_Budget_ID, fact_acct.c_tax_id AS C_Tax_ID, fact_acct.m_locator_id AS M_Locator_ID, fact_acct.postingtype AS PostingType, fact_acct.c_currency_id AS C_Currency_ID, sum(fact_acct.amtsourcedr) AS AmtSourceDr, sum(fact_acct.amtsourcecr) AS AmtSourceCr, sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) AS AmtSource, sum(fact_acct.amtacctdr) AS AmtAcctDr, sum(fact_acct.amtacctcr) AS AmtAcctCr, sum(fact_acct.amtacctdr - fact_acct.amtacctcr) AS AmtAcct, CASE WHEN sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) = 0 THEN 0 ELSE sum(fact_acct.amtacctdr - fact_acct.amtacctcr) / sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) END AS Rate, fact_acct.m_product_id AS M_Product_ID, fact_acct.c_bpartner_id AS C_BPartner_ID, fact_acct.ad_orgtrx_id AS AD_OrgTrx_ID, fact_acct.c_locfrom_id AS C_LocFrom_ID, fact_acct.c_locto_id AS C_LocTo_ID, fact_acct.c_salesregion_id AS C_SalesRegion_ID, fact_acct.c_project_id AS C_Project_ID, fact_acct.c_campaign_id AS C_Campaign_ID, fact_acct.c_activity_id AS C_Activity_ID, fact_acct.user1_id AS User1_ID, fact_acct.user2_id AS User2_ID, fact_acct.a_asset_id AS A_Asset_ID FROM fact_acct GROUP BY fact_acct.ad_client_id, fact_acct.ad_org_id, fact_acct.c_acctschema_id, fact_acct.account_id,
(firstof(fact_acct.dateacct, 'DD')), fact_acct.c_period_id, fact_acct.gl_category_id, fact_acct.gl_budget_id, fact_acct.c_tax_id, fact_acct.m_locator_id, fact_acct.postingtype, fact_acct.c_currency_id, fact_acct.m_product_id, fact_acct.c_bpartner_id, fact_acct.ad_orgtrx_id, fact_acct.c_locfrom_id, fact_acct.c_locto_id, fact_acct.c_salesregion_id, fact_acct.c_project_id, fact_acct.c_campaign_id, fact_acct.c_activity_id, fact_acct.user1_id, fact_acct.user2_id, fact_acct.a_asset_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_Fact_Acct(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, Fact_Acct_ID, C_AcctSchema_ID, Account_ID, DateTrx, DateAcct, C_Period_ID, AD_Table_ID, Record_ID, Line_ID, GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType, C_Currency_ID, AmtSourceDr, AmtSourceCr, AmtSource, AmtAcctDr, AmtAcctCr, AmtAcct, Rate, C_UOM_ID, Qty, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, A_Asset_ID, Description, OrgValue, OrgName, AccountValue, Name, AccountType, BPartnerValue, BPName, C_BP_Group_ID, ProductValue, ProductName, UPC, M_Product_Category_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_SubAcct_ID, UserElement1_ID, UserElement2_ID, ad_org_description, ad_org_isactive, C_ElementValue_AD_Org_ID, C_BankAccount_ID, C_ElementValue_C_Currency_ID, C_Element_ID, C_ElementValue_ID, c_elementvalue_description, c_elementvalue_isactive, IsBankAccount, IsForeignCurrency, c_elementvalue_issummary, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, DUNS, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, NAICS, c_bp_name2, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, ReferenceNo, C_BP_SalesRep_ID,
c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, TaxID, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_Product_C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight) AS SELECT f.ad_client_id AS AD_Client_ID, f.ad_org_id AS AD_Org_ID, f.isactive AS IsActive, f.created AS Created, f.createdby AS CreatedBy, f.updated AS Updated, f.updatedby AS UpdatedBy, f.fact_acct_id AS Fact_Acct_ID, f.c_acctschema_id AS C_AcctSchema_ID, f.account_id AS Account_ID, f.datetrx AS DateTrx, f.dateacct AS DateAcct, f.c_period_id AS C_Period_ID, f.ad_table_id AS AD_Table_ID, f.record_id AS Record_ID, f.line_id AS Line_ID, f.gl_category_id AS GL_Category_ID, f.gl_budget_id AS GL_Budget_ID, f.c_tax_id AS C_Tax_ID, f.m_locator_id AS M_Locator_ID, f.postingtype AS PostingType, f.c_currency_id AS C_Currency_ID, f.amtsourcedr AS AmtSourceDr, f.amtsourcecr AS AmtSourceCr, f.amtsourcedr - f.amtsourcecr AS AmtSource, f.amtacctdr AS AmtAcctDr, f.amtacctcr AS AmtAcctCr, f.amtacctdr - f.amtacctcr AS AmtAcct,
CASE WHEN (f.amtsourcedr - f.amtsourcecr) = 0 THEN 0 ELSE (f.amtacctdr - f.amtacctcr) / (f.amtsourcedr - f.amtsourcecr) END AS Rate, f.c_uom_id AS C_UOM_ID, f.qty AS Qty, f.m_product_id AS M_Product_ID, f.c_bpartner_id AS C_BPartner_ID, f.ad_orgtrx_id AS AD_OrgTrx_ID, f.c_locfrom_id AS C_LocFrom_ID, f.c_locto_id AS C_LocTo_ID, f.c_salesregion_id AS C_SalesRegion_ID, f.c_project_id AS C_Project_ID, f.c_campaign_id AS C_Campaign_ID, f.c_activity_id AS C_Activity_ID, f.user1_id AS User1_ID, f.user2_id AS User2_ID, f.a_asset_id AS A_Asset_ID, f.description AS Description, o.value AS OrgValue, o.name AS OrgName, ev.value AS AccountValue, ev.name AS Name, ev.accounttype AS AccountType, bp.value AS BPartnerValue, bp.name AS BPName, bp.c_bp_group_id AS C_BP_Group_ID, p.value AS ProductValue, p.name AS ProductName, p.upc AS UPC, p.m_product_category_id AS M_Product_Category_ID, f.c_projectphase_id AS C_ProjectPhase_ID, f.c_projecttask_id AS C_ProjectTask_ID, f.c_subacct_id AS C_SubAcct_ID, f.userelement1_id AS UserElement1_ID, f.userelement2_id AS UserElement2_ID, o.description AS ad_org_description, o.isactive AS ad_org_isactive, ev.ad_org_id AS C_ElementValue_AD_Org_ID, ev.c_bankaccount_id AS C_BankAccount_ID, ev.c_currency_id AS C_ElementValue_C_Currency_ID, ev.c_element_id AS C_Element_ID, ev.c_elementvalue_id AS C_ElementValue_ID, ev.description AS c_elementvalue_description, ev.isactive AS c_elementvalue_isactive, ev.isbankaccount AS IsBankAccount, ev.isforeigncurrency AS IsForeignCurrency, ev.issummary AS c_elementvalue_issummary, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID,
bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.duns AS DUNS, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.naics AS NAICS, bp.name2 AS c_bp_name2, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.referenceno AS ReferenceNo, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.taxid AS TaxID, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, p.ad_org_id AS M_Product_AD_Org_ID,
p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.documentnote AS DocumentNote, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, p.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.sku AS SKU, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight FROM fact_acct f
JOIN ad_org o ON f.ad_org_id = o.ad_org_id
JOIN c_elementvalue ev ON f.account_id = ev.c_elementvalue_id
LEFT JOIN c_bpartner bp ON f.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN m_product p ON f.m_product_id = p.m_product_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_PaySelection_Check_v(AD_Client_ID, AD_Org_ID, AD_Language, C_PaySelection_ID, C_PaySelectionCheck_ID, Org_Location_ID, TaxID, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, BPGreeting, Name, Name2, C_Location_ID, ReferenceNo, POReference, PayDate, PayAmt, AmtInWords, Qty, PaymentRule, DocumentNo, Logo_ID, DocumentType, DocumentTypeNote, Description, BankAccountDescription, C_BP_BankAccount_ID, C_Payment_ID, c_payselectioncheck_created, C_PaySelectionCheck_CreatedBy, c_payselectioncheck_discamt, c_payselectioncheck_isactive, IsGeneratedDraft, IsPrinted, IsReceipt, Processed, c_payselectioncheck_updated, C_PaySelectionCheck_UpdatedBy, C_PaySelection_AD_Org_ID, C_PaySel_C_BankAccount_ID, c_payselection_created, C_PaySelection_CreatedBy, c_payselection_description, c_payselection_isactive, c_payselection_isapproved, c_payselection_name, c_payselection_processed, TotalAmt, c_payselection_updated, C_PaySelection_UpdatedBy, A_City, A_Country, C_Payment_AD_Org_ID, A_EMail, A_Ident_DL, A_Name, A_State, A_Street, A_Zip, C_Activity_ID, C_Payment_C_BPartner_ID, C_Campaign_ID, C_Payment_C_Charge_ID, C_ConversionType_ID, C_Currency_ID, c_payment_chargeamt, CheckNo, C_Payment_C_Invoice_ID, C_Payment_C_Order_ID, C_PaymentBatch_ID, C_Project_ID, c_payment_created, C_Payment_CreatedBy, DateAcct, DateTrx, c_payment_discountamt, DocStatus, c_payment_documentno, c_payment_isactive, IsAllocated, IsApproved, IsDelayedCapture, IsOnline, IsOverUnderPayment, IsPrepayment, IsReconciled, c_payment_isselfservice, Micr, Orig_TrxID, OverUnderAmt, c_payment_payamt, PONum, c_payment_posted, c_payment_processed, c_payment_processedon, R_AuthCode, R_AuthCode_DC, R_AvsAddr, R_AvsZip, R_CVV2Match, Ref_Payment_ID, C_Payment_Reversal_ID, R_Info, RoutingNo, R_PnRef, R_PnRef_DC, R_RespMsg, R_Result, Swipe, TaxAmt, TenderType, TrxType, c_payment_updated, C_Payment_UpdatedBy, User1_ID, User2_ID, VoiceAuthCode, WriteOffAmt, c_bp_acqusitioncost, c_bp_actuallifetimevalue,
c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy) AS SELECT psc.ad_client_id AS AD_Client_ID, psc.ad_org_id AS AD_Org_ID, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, psc.c_payselection_id AS C_PaySelection_ID, psc.c_payselectioncheck_id AS C_PaySelectionCheck_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, p.c_doctype_id AS C_DocType_ID, bp.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2,
bpartnerremitlocation(bp.c_bpartner_id) AS C_Location_ID, bp.referenceno AS ReferenceNo, bp.poreference AS POReference, ps.paydate AS PayDate, psc.payamt AS PayAmt, psc.payamt AS AmtInWords, psc.qty AS Qty, psc.paymentrule AS PaymentRule, psc.documentno AS DocumentNo, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, p.description AS Description, ba.description AS BankAccountDescription, psc.c_bp_bankaccount_id AS C_BP_BankAccount_ID, psc.c_payment_id AS C_Payment_ID, psc.created AS c_payselectioncheck_created, psc.createdby AS C_PaySelectionCheck_CreatedBy, psc.discountamt AS c_payselectioncheck_discamt, psc.isactive AS c_payselectioncheck_isactive, psc.isgenerateddraft AS IsGeneratedDraft, psc.isprinted AS IsPrinted, psc.isreceipt AS IsReceipt, psc.processed AS Processed, psc.updated AS c_payselectioncheck_updated, psc.updatedby AS C_PaySelectionCheck_UpdatedBy, ps.ad_org_id AS C_PaySelection_AD_Org_ID, ps.c_bankaccount_id AS C_PaySel_C_BankAccount_ID, ps.created AS c_payselection_created, ps.createdby AS C_PaySelection_CreatedBy, ps.description AS c_payselection_description, ps.isactive AS c_payselection_isactive, ps.isapproved AS c_payselection_isapproved, ps.name AS c_payselection_name, ps.processed AS c_payselection_processed, ps.totalamt AS TotalAmt, ps.updated AS c_payselection_updated, ps.updatedby AS C_PaySelection_UpdatedBy, p.a_city AS A_City, p.a_country AS A_Country, p.ad_org_id AS C_Payment_AD_Org_ID, p.a_email AS A_EMail, p.a_ident_dl AS A_Ident_DL, p.a_name AS A_Name, p.a_state AS A_State, p.a_street AS A_Street, p.a_zip AS A_Zip, p.c_activity_id AS C_Activity_ID, p.c_bpartner_id AS C_Payment_C_BPartner_ID, p.c_campaign_id AS C_Campaign_ID, p.c_charge_id AS C_Payment_C_Charge_ID, p.c_conversiontype_id AS C_ConversionType_ID, p.c_currency_id AS C_Currency_ID, p.chargeamt AS c_payment_chargeamt, p.checkno AS CheckNo, p.c_invoice_id AS C_Payment_C_Invoice_ID,
p.c_order_id AS C_Payment_C_Order_ID, p.c_paymentbatch_id AS C_PaymentBatch_ID, p.c_project_id AS C_Project_ID, p.created AS c_payment_created, p.createdby AS C_Payment_CreatedBy, p.dateacct AS DateAcct, p.datetrx AS DateTrx, p.discountamt AS c_payment_discountamt, p.docstatus AS DocStatus, p.documentno AS c_payment_documentno, p.isactive AS c_payment_isactive, p.isallocated AS IsAllocated, p.isapproved AS IsApproved, p.isdelayedcapture AS IsDelayedCapture, p.isonline AS IsOnline, p.isoverunderpayment AS IsOverUnderPayment, p.isprepayment AS IsPrepayment, p.isreconciled AS IsReconciled, p.isselfservice AS c_payment_isselfservice, p.micr AS Micr, p.orig_trxid AS Orig_TrxID, p.overunderamt AS OverUnderAmt, p.payamt AS c_payment_payamt, p.ponum AS PONum, p.posted AS c_payment_posted, p.processed AS c_payment_processed, p.processedon AS c_payment_processedon, p.r_authcode AS R_AuthCode, p.r_authcode_dc AS R_AuthCode_DC, p.r_avsaddr AS R_AvsAddr, p.r_avszip AS R_AvsZip, p.r_cvv2match AS R_CVV2Match, p.ref_payment_id AS Ref_Payment_ID, p.reversal_id AS C_Payment_Reversal_ID, p.r_info AS R_Info, p.routingno AS RoutingNo, p.r_pnref AS R_PnRef, p.r_pnref_dc AS R_PnRef_DC, p.r_respmsg AS R_RespMsg, p.r_result AS R_Result, p.swipe AS Swipe, p.taxamt AS TaxAmt, p.tendertype AS TenderType, p.trxtype AS TrxType, p.updated AS c_payment_updated, p.updatedby AS C_Payment_UpdatedBy, p.user1_id AS User1_ID, p.user2_id AS User2_ID, p.voiceauthcode AS VoiceAuthCode, p.writeoffamt AS WriteOffAmt, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created,
bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, bpg.ad_org_id AS C_Greeting_AD_Org_ID, bpg.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpg.name AS c_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID,
oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy FROM c_payselectioncheck psc
JOIN c_payselection ps ON psc.c_payselection_id = ps.c_payselection_id
LEFT JOIN c_payment p ON psc.c_payment_id = p.c_payment_id
LEFT JOIN c_bankaccount ba ON p.c_bankaccount_id = ba.c_bankaccount_id
LEFT JOIN c_doctype dt ON p.c_doctype_id = dt.c_doctype_id
JOIN c_bpartner bp ON psc.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN ad_orginfo oi ON psc.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON psc.ad_client_id = ci.ad_client_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_InOut_Line_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, MovementQty, QtyEntered, UOMSymbol, QtyOrdered, QtyDelivered, QtyBackOrdered, Name, Description, DocumentNote, UPC, SKU, ProductValue, M_Locator_ID, M_Warehouse_ID, X, Y, Z, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, AD_OrgTrx_ID, C_Charge_ID, ConfirmedQty, C_OrderLine_ID, M_InOutLine_C_UOM_ID, m_inoutline_isdescription, IsInvoiced, M_RMALine_ID, PickedQty, m_inoutline_processed, Ref_InOutLine_ID, ReversalLine_ID, ScrappedQty, TargetQty, M_InOutLine_User1_ID, M_InOutLine_User2_ID, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, M_Product_C_TaxCategory_ID, M_Product_C_UOM_ID, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, M_Product_M_Prod_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, M_Product_S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, c_uom_isdefault, c_uom_name, StdPrecision, UOMType, X12DE355, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy, M_Locator_AD_Org_ID, m_locator_isactive,
IsDefault, PriorityNo, m_locator_value, C_OrderLine_AD_Org_ID, C_OrderLine_AD_OrgTrx_ID, C_OrderLine_C_Activity_ID, C_OrderLine_C_BPartner_ID, C_OrderLine_C_BP_Location_ID, C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_Order_ID, C_OrderLine_C_Project_ID, C_OrderLine_C_ProjectPhase_ID, C_OrderLine_C_ProjectTask_ID, c_orderline_created, C_OrderLine_CreatedBy, C_OrderLine_C_Tax_ID, C_OrderLine_C_UOM_ID, DateDelivered, DateInvoiced, DateOrdered, c_orderline_datepromised, c_orderline_description, Discount, c_orderline_freightamt, c_orderline_isactive, c_orderline_isdescription, c_orderline_line, LineNetAmt, Link_OrderLine_ID, C_OrderLine_M_ASI_ID, C_OrderLine_M_Product_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, C_OrderLine_Warehouse_ID, PriceActual, PriceCost, PriceEntered, PriceLimit, PriceList, c_orderline_processed, c_orderline_qtyentered, QtyInvoiced, QtyLostSales, QtyReserved, Ref_OrderLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, c_orderline_updated, C_OrderLine_UpdatedBy, C_OrderLine_User1_ID, C_OrderLine_User2_ID, C_Charge_C_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded) AS SELECT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.isactive AS IsActive, iol.created AS Created, iol.createdby AS CreatedBy, iol.updated AS Updated, iol.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line AS Line, p.m_product_id AS M_Product_ID, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN iol.movementqty ELSE NULL END AS MovementQty, CASE WHEN iol.qtyentered <> 0 OR iol.m_product_id IS NOT NULL THEN iol.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN uom.uomsymbol ELSE NULL END AS UOMSymbol, ol.qtyordered AS QtyOrdered,
ol.qtydelivered AS QtyDelivered, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN ol.qtyordered - ol.qtydelivered ELSE NULL END AS QtyBackOrdered, COALESCE(p.name || productattribute(iol.m_attributesetinstance_id), c.name, iol.description) AS Name, CASE WHEN COALESCE(c.name, p.name) IS NOT NULL THEN iol.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, p.description AS ProductDescription, p.imageurl AS ImageURL, iol.c_campaign_id AS C_Campaign_ID, iol.c_project_id AS C_Project_ID, iol.c_activity_id AS C_Activity_ID, iol.c_projectphase_id AS C_ProjectPhase_ID, iol.c_projecttask_id AS C_ProjectTask_ID, iol.ad_orgtrx_id AS AD_OrgTrx_ID, iol.c_charge_id AS C_Charge_ID, iol.confirmedqty AS ConfirmedQty, iol.c_orderline_id AS C_OrderLine_ID, iol.c_uom_id AS M_InOutLine_C_UOM_ID, iol.isdescription AS m_inoutline_isdescription, iol.isinvoiced AS IsInvoiced, iol.m_rmaline_id AS M_RMALine_ID, iol.pickedqty AS PickedQty, iol.processed AS m_inoutline_processed, iol.ref_inoutline_id AS Ref_InOutLine_ID, iol.reversalline_id AS ReversalLine_ID, iol.scrappedqty AS ScrappedQty, iol.targetqty AS TargetQty, iol.user1_id AS M_InOutLine_User1_ID, iol.user2_id AS M_InOutLine_User2_ID, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL,
p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy,
l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, ol.ad_org_id AS C_OrderLine_AD_Org_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_activity_id AS C_OrderLine_C_Activity_ID, ol.c_bpartner_id AS C_OrderLine_C_BPartner_ID, ol.c_bpartner_location_id AS C_OrderLine_C_BP_Location_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_order_id AS C_Order_ID, ol.c_project_id AS C_OrderLine_C_Project_ID, ol.c_projectphase_id AS C_OrderLine_C_ProjectPhase_ID, ol.c_projecttask_id AS C_OrderLine_C_ProjectTask_ID, ol.created AS c_orderline_created, ol.createdby AS C_OrderLine_CreatedBy, ol.c_tax_id AS C_OrderLine_C_Tax_ID, ol.c_uom_id AS C_OrderLine_C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.description AS c_orderline_description, ol.discount AS Discount, ol.freightamt AS c_orderline_freightamt, ol.isactive AS c_orderline_isactive, ol.isdescription AS c_orderline_isdescription, ol.line AS c_orderline_line, ol.linenetamt AS LineNetAmt, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_attributesetinstance_id AS C_OrderLine_M_ASI_ID, ol.m_product_id AS C_OrderLine_M_Product_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_Warehouse_ID, ol.priceactual AS PriceActual, ol.pricecost AS PriceCost, ol.priceentered AS PriceEntered, ol.pricelimit AS PriceLimit, ol.pricelist AS PriceList, ol.processed AS c_orderline_processed, ol.qtyentered AS c_orderline_qtyentered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.updated AS c_orderline_updated,
ol.updatedby AS C_OrderLine_UpdatedBy, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, c.ad_org_id AS C_Charge_C_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID, c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, c.description AS c_charge_description, c.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded FROM m_inoutline iol
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id
LEFT JOIN m_product p ON iol.m_product_id = p.m_product_id
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
LEFT JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
LEFT JOIN c_charge c ON iol.c_charge_id = c.c_charge_id UNION SELECT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.isactive AS IsActive, iol.created AS Created, iol.createdby AS CreatedBy, iol.updated AS Updated, iol.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line + bl.line / 100 AS Line, p.m_product_id AS M_Product_ID, CASE WHEN bl.isqtypercentage = 'N' THEN iol.movementqty * bl.qtybom ELSE iol.movementqty * (bl.qtybatch / 100) END AS MovementQty, CASE WHEN bl.isqtypercentage = 'N' THEN iol.qtyentered * bl.qtybom ELSE iol.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uom.uomsymbol AS UOMSymbol, NULL AS QtyOrdered, NULL AS QtyDelivered, NULL AS QtyBackOrdered, p.name AS Name, b.description AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, p.description AS ProductDescription, p.imageurl AS ImageURL, iol.c_campaign_id AS C_Campaign_ID, iol.c_project_id AS C_Project_ID, iol.c_activity_id AS C_Activity_ID, iol.c_projectphase_id AS C_ProjectPhase_ID, iol.c_projecttask_id AS C_ProjectTask_ID, iol.ad_orgtrx_id AS AD_OrgTrx_ID, iol.c_charge_id AS C_Charge_ID, iol.confirmedqty AS ConfirmedQty, iol.c_orderline_id AS C_OrderLine_ID, iol.c_uom_id AS M_InOutLine_C_UOM_ID, iol.isdescription AS m_inoutline_isdescription, iol.isinvoiced AS IsInvoiced, iol.m_rmaline_id AS M_RMALine_ID, iol.pickedqty AS PickedQty, iol.processed AS m_inoutline_processed, iol.ref_inoutline_id AS Ref_InOutLine_ID, iol.reversalline_id AS ReversalLine_ID, iol.scrappedqty AS ScrappedQty, iol.targetqty AS TargetQty,
iol.user1_id AS M_InOutLine_User1_ID, iol.user2_id AS M_InOutLine_User2_ID, p.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, p.created AS m_product_created, p.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, uom.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision,
uom.description AS c_uom_description, uom.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uom.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy, l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, NULL AS C_OrderLine_AD_Org_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Activity_ID, NULL AS C_OrderLine_C_BPartner_ID, NULL AS C_OrderLine_C_BP_Location_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_Order_ID, NULL AS C_OrderLine_C_Project_ID, NULL AS C_OrderLine_C_ProjectPhase_ID, NULL AS C_OrderLine_C_ProjectTask_ID, NULL AS c_orderline_created, NULL AS C_OrderLine_CreatedBy, NULL AS C_OrderLine_C_Tax_ID, NULL AS C_OrderLine_C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_description, NULL AS Discount, NULL AS c_orderline_freightamt, NULL AS c_orderline_isactive, NULL AS c_orderline_isdescription, NULL AS c_orderline_line, NULL AS LineNetAmt, NULL AS Link_OrderLine_ID, NULL AS C_OrderLine_M_ASI_ID, NULL AS C_OrderLine_M_Product_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_Warehouse_ID, NULL AS PriceActual, NULL AS PriceCost, NULL AS PriceEntered, NULL AS PriceLimit, NULL AS PriceList, NULL AS c_orderline_processed, NULL AS c_orderline_qtyentered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS c_orderline_updated, NULL AS C_OrderLine_UpdatedBy, NULL AS C_OrderLine_User1_ID,
NULL AS C_OrderLine_User2_ID, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded FROM pp_product_bom b
JOIN m_inoutline iol ON b.m_product_id = iol.m_product_id
JOIN m_product bp ON bp.m_product_id = iol.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.ispicklistprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON bl.m_product_id = p.m_product_id
JOIN c_uom uom ON p.c_uom_id = uom.c_uom_id
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_BPartner(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_BPartner_ID, Value, Name, Name2, Description, IsSummary, C_BP_Group_ID, IsOneTime, IsProspect, IsVendor, IsCustomer, IsEmployee, IsSalesRep, ReferenceNo, DUNS, URL, AD_Language, TaxID, IsTaxExempt, C_InvoiceSchedule_ID, Rating, SalesVolume, NumberEmployees, NAICS, FirstSale, AcqusitionCost, PotentialLifeTimeValue, ActualLifeTimeValue, ShareOfCustomer, PaymentRule, SO_CreditLimit, SO_CreditUsed, SO_CreditAvailable, C_PaymentTerm_ID, M_PriceList_ID, M_DiscountSchema_ID, C_Dunning_ID, IsDiscountPrinted, SO_Description, POReference, PaymentRulePO, PO_PriceList_ID, PO_DiscountSchema_ID, PO_PaymentTerm_ID, DocumentCopies, C_Greeting_ID, InvoiceRule, DeliveryRule, FreightCostRule, DeliveryViaRule, SalesRep_ID, SendEMail, BPartner_Parent_ID, Invoice_PrintFormat_ID, SOCreditStatus, ShelfLifeMinPct, AD_OrgBP_ID, FlatDiscount, TotalOpenBalance, AD_User_ID, ContactName, ContactDescription, EMail, Supervisor_ID, EMailUser, BPContactGreeting, Title, Comments, Phone, Phone2, Fax, LastContact, LastResult, Birthday, AD_OrgTrx_ID, EMailVerify, LDAPUser, EMailVerifyDate, NotificationType, C_BPartner_Location_ID, Postal, City, Address1, Address2, Address3, C_Region_ID, RegionName, C_Country_ID, CountryName, C_BP_C_TaxGroup_ID, c_bp_dunninggrace, c_bp_ismanufacturer, c_bp_ispotaxexempt, C_BP_Logo_ID, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, C_BP_Location_C_Location_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_BP_Location_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, c_bp_location_isbillto, c_bp_location_isdn, c_bp_location_ispayfrom, c_bp_location_isremitto, c_bp_location_isshipto, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, ad_user_created, AD_User_CreatedBy, ad_user_isactive, ad_user_updated,
AD_User_UpdatedBy, ad_user_value, Address4, C_Location_AD_Org_ID, C_City_ID, C_Location_ID, c_location_created, C_Location_CreatedBy, c_location_isactive, Postal_Add, c_location_updated, C_Location_UpdatedBy, C_Region_AD_Org_ID, C_Region_C_Country_ID, c_region_description, c_region_isactive, IsDefault, c_country_ad_language, C_Country_C_Currency_ID, CountryCode, c_country_description, c_country_isactive) AS SELECT bp.ad_client_id AS AD_Client_ID, bp.ad_org_id AS AD_Org_ID, bp.isactive AS IsActive, bp.created AS Created, bp.createdby AS CreatedBy, bp.updated AS Updated, bp.updatedby AS UpdatedBy, bp.c_bpartner_id AS C_BPartner_ID, bp.value AS Value, bp.name AS Name, bp.name2 AS Name2, bp.description AS Description, bp.issummary AS IsSummary, bp.c_bp_group_id AS C_BP_Group_ID, bp.isonetime AS IsOneTime, bp.isprospect AS IsProspect, bp.isvendor AS IsVendor, bp.iscustomer AS IsCustomer, bp.isemployee AS IsEmployee, bp.issalesrep AS IsSalesRep, bp.referenceno AS ReferenceNo, bp.duns AS DUNS, bp.url AS URL, bp.ad_language AS AD_Language, bp.taxid AS TaxID, bp.istaxexempt AS IsTaxExempt, bp.c_invoiceschedule_id AS C_InvoiceSchedule_ID, bp.rating AS Rating, bp.salesvolume AS SalesVolume, bp.numberemployees AS NumberEmployees, bp.naics AS NAICS, bp.firstsale AS FirstSale, bp.acqusitioncost AS AcqusitionCost, bp.potentiallifetimevalue AS PotentialLifeTimeValue, bp.actuallifetimevalue AS ActualLifeTimeValue, bp.shareofcustomer AS ShareOfCustomer, bp.paymentrule AS PaymentRule, bp.so_creditlimit AS SO_CreditLimit, bp.so_creditused AS SO_CreditUsed, bp.so_creditused - bp.so_creditlimit AS SO_CreditAvailable, bp.c_paymentterm_id AS C_PaymentTerm_ID, bp.m_pricelist_id AS M_PriceList_ID, bp.m_discountschema_id AS M_DiscountSchema_ID, bp.c_dunning_id AS C_Dunning_ID, bp.isdiscountprinted AS IsDiscountPrinted, bp.so_description AS SO_Description, bp.poreference AS POReference, bp.paymentrulepo AS PaymentRulePO, bp.po_pricelist_id AS PO_PriceList_ID,
bp.po_discountschema_id AS PO_DiscountSchema_ID, bp.po_paymentterm_id AS PO_PaymentTerm_ID, bp.documentcopies AS DocumentCopies, bp.c_greeting_id AS C_Greeting_ID, bp.invoicerule AS InvoiceRule, bp.deliveryrule AS DeliveryRule, bp.freightcostrule AS FreightCostRule, bp.deliveryviarule AS DeliveryViaRule, bp.salesrep_id AS SalesRep_ID, bp.sendemail AS SendEMail, bp.bpartner_parent_id AS BPartner_Parent_ID, bp.invoice_printformat_id AS Invoice_PrintFormat_ID, bp.socreditstatus AS SOCreditStatus, bp.shelflifeminpct AS ShelfLifeMinPct, bp.ad_orgbp_id AS AD_OrgBP_ID, bp.flatdiscount AS FlatDiscount, bp.totalopenbalance AS TotalOpenBalance, c.ad_user_id AS AD_User_ID, c.name AS ContactName, c.description AS ContactDescription, c.email AS EMail, c.supervisor_id AS Supervisor_ID, c.emailuser AS EMailUser, c.c_greeting_id AS BPContactGreeting, c.title AS Title, c.comments AS Comments, c.phone AS Phone, c.phone2 AS Phone2, c.fax AS Fax, c.lastcontact AS LastContact, c.lastresult AS LastResult, c.birthday AS Birthday, c.ad_orgtrx_id AS AD_OrgTrx_ID, c.emailverify AS EMailVerify, c.ldapuser AS LDAPUser, c.emailverifydate AS EMailVerifyDate, c.notificationtype AS NotificationType, l.c_bpartner_location_id AS C_BPartner_Location_ID, a.postal AS Postal, a.city AS City, a.address1 AS Address1, a.address2 AS Address2, a.address3 AS Address3, a.c_region_id AS C_Region_ID, COALESCE(r.name, a.regionname) AS RegionName, a.c_country_id AS C_Country_ID, cc.name AS CountryName, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.dunninggrace AS c_bp_dunninggrace, bp.ismanufacturer AS c_bp_ismanufacturer, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.logo_id AS C_BP_Logo_ID, l.ad_org_id AS C_BP_Location_AD_Org_ID, l.c_bpartner_id AS C_BP_Location_C_BPartner_ID, l.c_location_id AS C_BP_Location_C_Location_ID, l.created AS c_bp_location_created, l.createdby AS C_BP_Location_CreatedBy, l.c_salesregion_id AS C_BP_Location_SalesRegion_ID, l.fax AS c_bp_location_fax, l.isactive AS c_bp_location_isactive,
l.isbillto AS c_bp_location_isbillto, l.isdn AS c_bp_location_isdn, l.ispayfrom AS c_bp_location_ispayfrom, l.isremitto AS c_bp_location_isremitto, l.isshipto AS c_bp_location_isshipto, l.name AS c_bp_location_name, l.phone AS c_bp_location_phone, l.phone2 AS c_bp_location_phone2, l.updated AS c_bp_location_updated, l.updatedby AS C_BP_Location_UpdatedBy, c.ad_org_id AS AD_User_AD_Org_ID, c.c_bpartner_id AS AD_User_C_BPartner_ID, c.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, c.created AS ad_user_created, c.createdby AS AD_User_CreatedBy, c.isactive AS ad_user_isactive, c.updated AS ad_user_updated, c.updatedby AS AD_User_UpdatedBy, c.value AS ad_user_value, a.address4 AS Address4, a.ad_org_id AS C_Location_AD_Org_ID, a.c_city_id AS C_City_ID, a.c_location_id AS C_Location_ID, a.created AS c_location_created, a.createdby AS C_Location_CreatedBy, a.isactive AS c_location_isactive, a.postal_add AS Postal_Add, a.updated AS c_location_updated, a.updatedby AS C_Location_UpdatedBy, r.ad_org_id AS C_Region_AD_Org_ID, r.c_country_id AS C_Region_C_Country_ID, r.description AS c_region_description, r.isactive AS c_region_isactive, r.isdefault AS IsDefault, cc.ad_language AS c_country_ad_language, cc.c_currency_id AS C_Country_C_Currency_ID, cc.countrycode AS CountryCode, cc.description AS c_country_description, cc.isactive AS c_country_isactive FROM c_bpartner bp
LEFT JOIN c_bpartner_location l ON bp.c_bpartner_id = l.c_bpartner_id AND l.isactive = 'Y'
LEFT JOIN ad_user c ON bp.c_bpartner_id = c.c_bpartner_id AND (c.c_bpartner_location_id IS NULL OR c.c_bpartner_location_id = l.c_bpartner_location_id) AND c.isactive = 'Y'
LEFT JOIN c_location a ON l.c_location_id = a.c_location_id
LEFT JOIN c_region r ON a.c_region_id = r.c_region_id
JOIN c_country cc ON a.c_country_id = cc.c_country_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW C_Dunning_Line_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_DunningRunLine_ID, C_DunningRunEntry_ID, Amt, ConvertedAmt, DaysDue, TimesDunned, InterestAmt, FeeAmt, TotalAmt, C_Invoice_ID, IsSOTrx, DocumentNo, DocStatus, DateTrx, C_DocType_ID, DocumentType, Description, C_Currency_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, User1_ID, User2_ID, DateAcct, C_ConversionType_ID, AD_OrgTrx_ID, POReference, DateOrdered, DateInvoiced, IsInDispute, PaymentTerm, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsPaid, IsAllocated, TenderType, DiscountAmt, C_InvoicePaySchedule_ID, C_Payment_ID, c_dunningrunline_isindispute, OpenAmt, c_dunningrunline_processed, C_Invoice_AD_Org_ID, AD_User_ID, C_Invoice_C_BPartner_ID, C_BPartner_Location_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, C_Invoice_C_Order_ID, C_Invoice_C_Payment_ID, c_invoice_created, C_Invoice_CreatedBy, DatePrinted, DocAction, DunningGrace, GenerateTo, InvoiceCollectionType, c_invoice_isactive, c_invoice_isapproved, IsDiscountPrinted, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, IsTaxIncluded, IsTransferred, M_RMA_ID, PaymentRule, c_invoice_posted, c_invoice_processedon, Processing, Ref_Invoice_ID, C_Invoice_Reversal_ID, SalesRep_ID, SendEMail, c_invoice_updated, C_Invoice_UpdatedBy, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, DocumentNote, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value, A_City, A_Country, C_Payment_AD_Org_ID, A_EMail, A_Ident_DL, A_Name, A_State, A_Street, A_Zip, C_Payment_C_BPartner_ID, C_Payment_C_Charge_ID, c_payment_chargeamt, CheckNo, C_Payment_C_Invoice_ID, C_Payment_C_Order_ID, C_PaymentBatch_ID, c_payment_created, C_Payment_CreatedBy, c_payment_isactive, IsApproved,
IsDelayedCapture, IsOnline, IsOverUnderPayment, IsPrepayment, IsReconciled, c_payment_isselfservice, Micr, Orig_TrxID, OverUnderAmt, PayAmt, PONum, c_payment_posted, c_payment_processed, c_payment_processedon, R_AuthCode, R_AuthCode_DC, R_AvsAddr, R_AvsZip, R_CVV2Match, Ref_Payment_ID, C_Payment_Reversal_ID, R_Info, RoutingNo, R_PnRef, R_PnRef_DC, R_RespMsg, R_Result, Swipe, TaxAmt, TrxType, c_payment_updated, C_Payment_UpdatedBy, VoiceAuthCode, WriteOffAmt) AS SELECT drl.ad_client_id AS AD_Client_ID, drl.ad_org_id AS AD_Org_ID, drl.isactive AS IsActive, drl.created AS Created, drl.createdby AS CreatedBy, drl.updated AS Updated, drl.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, drl.c_dunningrunline_id AS C_DunningRunLine_ID, drl.c_dunningrunentry_id AS C_DunningRunEntry_ID, drl.amt AS Amt, drl.convertedamt AS ConvertedAmt, drl.daysdue AS DaysDue, drl.timesdunned AS TimesDunned, drl.interestamt AS InterestAmt, drl.feeamt AS FeeAmt, drl.totalamt AS TotalAmt, drl.c_invoice_id AS C_Invoice_ID, COALESCE(i.issotrx, p.isreceipt) AS IsSOTrx, COALESCE(i.documentno, p.documentno) AS DocumentNo, COALESCE(i.docstatus, p.docstatus) AS DocStatus, COALESCE(i.dateinvoiced, p.datetrx) AS DateTrx, COALESCE(i.c_doctype_id, p.c_doctype_id) AS C_DocType_ID, COALESCE(dt.printname, dtp.printname) AS DocumentType, COALESCE(i.description, p.description) AS Description, COALESCE(i.c_currency_id, p.c_currency_id) AS C_Currency_ID, COALESCE(i.c_campaign_id, p.c_campaign_id) AS C_Campaign_ID, COALESCE(i.c_project_id, p.c_project_id) AS C_Project_ID, COALESCE(i.c_activity_id, p.c_activity_id) AS C_Activity_ID, COALESCE(i.user1_id, p.user1_id) AS User1_ID, COALESCE(i.user2_id, p.user2_id) AS User2_ID, COALESCE(i.dateacct, p.dateacct) AS DateAcct, COALESCE(i.c_conversiontype_id, p.c_conversiontype_id) AS C_ConversionType_ID, COALESCE(i.ad_orgtrx_id, p.ad_orgtrx_id) AS AD_OrgTrx_ID, i.poreference AS POReference, i.dateordered AS DateOrdered,
i.dateinvoiced AS DateInvoiced, i.isindispute AS IsInDispute, pt.name AS PaymentTerm, i.c_charge_id AS C_Charge_ID, i.chargeamt AS ChargeAmt, i.totallines AS TotalLines, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, i.m_pricelist_id AS M_PriceList_ID, i.ispaid AS IsPaid, p.isallocated AS IsAllocated, p.tendertype AS TenderType, p.discountamt AS DiscountAmt, drl.c_invoicepayschedule_id AS C_InvoicePaySchedule_ID, drl.c_payment_id AS C_Payment_ID, drl.isindispute AS c_dunningrunline_isindispute, drl.openamt AS OpenAmt, drl.processed AS c_dunningrunline_processed, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_id AS C_Invoice_C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.c_order_id AS C_Invoice_C_Order_ID, i.c_payment_id AS C_Invoice_C_Payment_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateprinted AS DatePrinted, i.docaction AS DocAction, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS c_invoice_isapproved, i.isdiscountprinted AS IsDiscountPrinted, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.posted AS c_invoice_posted, i.processedon AS c_invoice_processedon, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS C_Invoice_Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, pt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_ID, pt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2,
pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.documentnote AS DocumentNote, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, pt.isactive AS c_paymentterm_isactive, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value, p.a_city AS A_City, p.a_country AS A_Country, p.ad_org_id AS C_Payment_AD_Org_ID, p.a_email AS A_EMail, p.a_ident_dl AS A_Ident_DL, p.a_name AS A_Name, p.a_state AS A_State, p.a_street AS A_Street, p.a_zip AS A_Zip, p.c_bpartner_id AS C_Payment_C_BPartner_ID, p.c_charge_id AS C_Payment_C_Charge_ID, p.chargeamt AS c_payment_chargeamt, p.checkno AS CheckNo, p.c_invoice_id AS C_Payment_C_Invoice_ID, p.c_order_id AS C_Payment_C_Order_ID, p.c_paymentbatch_id AS C_PaymentBatch_ID, p.created AS c_payment_created, p.createdby AS C_Payment_CreatedBy, p.isactive AS c_payment_isactive, p.isapproved AS IsApproved, p.isdelayedcapture AS IsDelayedCapture, p.isonline AS IsOnline, p.isoverunderpayment AS IsOverUnderPayment, p.isprepayment AS IsPrepayment, p.isreconciled AS IsReconciled, p.isselfservice AS c_payment_isselfservice, p.micr AS Micr, p.orig_trxid AS Orig_TrxID, p.overunderamt AS OverUnderAmt, p.payamt AS PayAmt, p.ponum AS PONum, p.posted AS c_payment_posted, p.processed AS c_payment_processed, p.processedon AS c_payment_processedon, p.r_authcode AS R_AuthCode, p.r_authcode_dc AS R_AuthCode_DC, p.r_avsaddr AS R_AvsAddr, p.r_avszip AS R_AvsZip, p.r_cvv2match AS R_CVV2Match, p.ref_payment_id AS Ref_Payment_ID, p.reversal_id AS C_Payment_Reversal_ID, p.r_info AS R_Info, p.routingno AS RoutingNo, p.r_pnref AS R_PnRef, p.r_pnref_dc AS R_PnRef_DC, p.r_respmsg AS R_RespMsg, p.r_result AS R_Result, p.swipe AS Swipe, p.taxamt AS TaxAmt, p.trxtype AS TrxType, p.updated AS c_payment_updated,
p.updatedby AS C_Payment_UpdatedBy, p.voiceauthcode AS VoiceAuthCode, p.writeoffamt AS WriteOffAmt FROM c_dunningrunline drl
LEFT JOIN c_invoice i ON drl.c_invoice_id = i.c_invoice_id
LEFT JOIN c_doctype dt ON i.c_doctype_id = dt.c_doctype_id
LEFT JOIN c_paymentterm pt ON i.c_paymentterm_id = pt.c_paymentterm_id
LEFT JOIN c_payment p ON drl.c_payment_id = p.c_payment_id
LEFT JOIN c_doctype dtp ON p.c_doctype_id = dtp.c_doctype_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_Transaction_v(M_Transaction_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, MovementType, M_Locator_ID, M_Product_ID, MovementDate, MovementQty, M_InventoryLine_ID, M_Inventory_ID, M_MovementLine_ID, M_Movement_ID, M_InOutLine_ID, M_InOut_ID, M_ProductionLine_ID, M_Production_ID, C_ProjectIssue_ID, C_Project_ID, M_AttributeSetInstance_ID) AS SELECT t.m_transaction_id AS M_Transaction_ID, t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.isactive AS IsActive, t.created AS Created, t.createdby AS CreatedBy, t.updated AS Updated, t.updatedby AS UpdatedBy, t.movementtype AS MovementType, t.m_locator_id AS M_Locator_ID, t.m_product_id AS M_Product_ID, t.movementdate AS MovementDate, t.movementqty AS MovementQty, t.m_inventoryline_id AS M_InventoryLine_ID, i.m_inventory_id AS M_Inventory_ID, t.m_movementline_id AS M_MovementLine_ID, m.m_movement_id AS M_Movement_ID, t.m_inoutline_id AS M_InOutLine_ID, io.m_inout_id AS M_InOut_ID, t.m_productionline_id AS M_ProductionLine_ID, pp.m_production_id AS M_Production_ID, t.c_projectissue_id AS C_ProjectIssue_ID, pi.c_project_id AS C_Project_ID, t.m_attributesetinstance_id AS M_AttributeSetInstance_ID FROM m_transaction t
LEFT JOIN m_inoutline io ON t.m_inoutline_id = io.m_inoutline_id AND t.m_attributesetinstance_id = io.m_attributesetinstance_id
LEFT JOIN m_movementline m ON t.m_movementline_id = m.m_movementline_id AND t.m_attributesetinstance_id = m.m_attributesetinstance_id
LEFT JOIN m_inventoryline i ON t.m_inventoryline_id = i.m_inventoryline_id AND t.m_attributesetinstance_id = i.m_attributesetinstance_id
LEFT JOIN c_projectissue pi ON t.c_projectissue_id = pi.c_projectissue_id AND t.m_attributesetinstance_id = pi.m_attributesetinstance_id
LEFT JOIN m_productionline pl ON t.m_productionline_id = pl.m_productionline_id AND t.m_attributesetinstance_id = pl.m_attributesetinstance_id
LEFT JOIN m_productionplan pp ON pl.m_productionplan_id = pp.m_productionplan_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_Product_Stock_V(IsActive, Created, CreatedBy, Updated, UpdatedBy, Value, Help, QtyAvailable, QtyOnHand, QtyReserved, Description, warehouse, M_Warehouse_ID, AD_Client_ID, AD_Org_ID, DocumentNote, M_Product_ID) AS SELECT ms.isactive AS IsActive, ms.created AS Created, ms.createdby AS CreatedBy, ms.updated AS Updated, ms.updatedby AS UpdatedBy, mp.value AS Value, mp.help AS Help, CASE WHEN COALESCE(lt.isavailableforreservation, 'Y') = 'Y' THEN ms.qtyonhand ELSE 0 END - ms.qtyreserved AS QtyAvailable, ms.qtyonhand AS QtyOnHand, ms.qtyreserved AS QtyReserved, mp.description AS Description, mw.name AS warehouse, mw.m_warehouse_id AS M_Warehouse_ID, mw.ad_client_id AS AD_Client_ID, mw.ad_org_id AS AD_Org_ID, mp.documentnote AS DocumentNote, mp.m_product_id AS M_Product_ID FROM m_storage ms
JOIN m_product mp ON ms.m_product_id = mp.m_product_id
JOIN m_locator ml ON ms.m_locator_id = ml.m_locator_id
LEFT JOIN m_locatortype lt ON ml.m_locatortype_id = lt.m_locatortype_id
JOIN m_warehouse mw ON ml.m_warehouse_id = mw.m_warehouse_id ORDER BY mw.name
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW M_Product_SubstituteRelated_V(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Product_ID, Substitute_ID, rowtype, Name, QtyAvailable, QtyOnHand, QtyReserved, PriceStd, M_PriceList_Version_ID, M_Warehouse_ID, OrgName) AS SELECT s.ad_client_id AS AD_Client_ID, s.ad_org_id AS AD_Org_ID, s.isactive AS IsActive, s.created AS Created, s.createdby AS CreatedBy, s.updated AS Updated, s.updatedby AS UpdatedBy, s.m_product_id AS M_Product_ID, s.substitute_id AS Substitute_ID, 'S' AS rowtype, mp.name AS Name, sum( CASE WHEN COALESCE(lt.isavailableforreservation, 'Y') = 'Y' THEN ms.qtyonhand ELSE 0 END - ms.qtyreserved) AS QtyAvailable, sum(ms.qtyonhand) AS QtyOnHand, sum(ms.qtyreserved) AS QtyReserved, currencyround(max(mpr.pricestd), mpl.c_currency_id, 'N') AS PriceStd, mpr.m_pricelist_version_id AS M_PriceList_Version_ID, mw.m_warehouse_id AS M_Warehouse_ID, org.name AS OrgName FROM m_substitute s
JOIN m_storage ms ON ms.m_product_id = s.substitute_id
JOIN m_product mp ON ms.m_product_id = mp.m_product_id
JOIN m_locator ml ON ms.m_locator_id = ml.m_locator_id
LEFT JOIN m_locatortype lt ON ml.m_locatortype_id = lt.m_locatortype_id
JOIN m_warehouse mw ON ml.m_warehouse_id = mw.m_warehouse_id
JOIN m_productprice mpr ON ms.m_product_id = mpr.m_product_id
JOIN m_pricelist_version mplv ON mplv.m_pricelist_version_id = mpr.m_pricelist_version_id
JOIN m_pricelist mpl ON mplv.m_pricelist_id = mpl.m_pricelist_id
JOIN ad_org org ON org.ad_org_id = mw.ad_org_id GROUP BY s.ad_client_id, s.ad_org_id, s.isactive, s.created, s.createdby, s.updated, s.updatedby, s.m_product_id, s.substitute_id, mw.m_warehouse_id, mpr.m_pricelist_version_id, org.name, mp.name, mpl.c_currency_id UNION SELECT r.ad_client_id AS AD_Client_ID, r.ad_org_id AS AD_Org_ID, r.isactive AS IsActive, r.created AS Created, r.createdby AS CreatedBy, r.updated AS Updated, r.updatedby AS UpdatedBy, r.m_product_id AS M_Product_ID, r.relatedproduct_id AS Substitute_ID, 'R' AS rowtype, mp.name AS Name, sum( CASE WHEN COALESCE(lt.isavailableforreservation, 'Y') = 'Y' THEN ms.qtyonhand ELSE 0 END - ms.qtyreserved) AS QtyAvailable, sum(ms.qtyonhand) AS QtyOnHand, sum(ms.qtyreserved) AS QtyReserved, currencyround(max(mpr.pricestd), mpl.c_currency_id, 'N') AS PriceStd, mpr.m_pricelist_version_id AS M_PriceList_Version_ID, mw.m_warehouse_id AS M_Warehouse_ID, org.name AS OrgName FROM m_relatedproduct r
JOIN m_storage ms ON ms.m_product_id = r.relatedproduct_id
JOIN m_product mp ON ms.m_product_id = mp.m_product_id
JOIN m_locator ml ON ms.m_locator_id = ml.m_locator_id
LEFT JOIN m_locatortype lt ON ml.m_locatortype_id = lt.m_locatortype_id
JOIN m_warehouse mw ON ml.m_warehouse_id = mw.m_warehouse_id
JOIN m_productprice mpr ON ms.m_product_id = mpr.m_product_id
JOIN m_pricelist_version mplv ON mplv.m_pricelist_version_id = mpr.m_pricelist_version_id
JOIN m_pricelist mpl ON mplv.m_pricelist_id = mpl.m_pricelist_id
JOIN ad_org org ON org.ad_org_id = mw.ad_org_id GROUP BY r.ad_client_id, r.ad_org_id, r.isactive, r.created, r.createdby, r.updated, r.updatedby, r.m_product_id, r.relatedproduct_id, mw.m_warehouse_id, mpr.m_pricelist_version_id, org.name, mp.name, mpl.c_currency_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_PP_Order_Storage(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, Updated, Created, IsActive, PP_Order_BOM_ID, PP_Order_BOMLine_ID, PP_Order_ID, IsCritical, M_Product_ID, Name, C_UOM_ID, QtyOnHand, QtyRequiered, QtyBatchSize, QtyReserved, QtyAvailable, M_Warehouse_ID, QtyBOM, IsQtyPercentage, QtyBatch, M_AttributeSetInstance_ID, M_Locator_ID, X, Y, Z) AS SELECT obl.ad_client_id AS AD_Client_ID, obl.ad_org_id AS AD_Org_ID, obl.createdby AS CreatedBy, obl.updatedby AS UpdatedBy, obl.updated AS Updated, obl.created AS Created, obl.isactive AS IsActive, obl.pp_order_bom_id AS PP_Order_BOM_ID, obl.pp_order_bomline_id AS PP_Order_BOMLine_ID, obl.pp_order_id AS PP_Order_ID, obl.iscritical AS IsCritical, obl.m_product_id AS M_Product_ID, ( SELECT p.name FROM m_product p WHERE p.m_product_id = o.m_product_id) AS Name, obl.c_uom_id AS C_UOM_ID, s.qtyonhand AS QtyOnHand, round(obl.qtyrequiered, 4) AS QtyRequiered, CASE WHEN o.qtybatchs = 0 THEN 1 ELSE round(obl.qtyrequiered / o.qtybatchs, 4) END AS QtyBatchSize, round(bomqtyreserved(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyReserved, round(bomqtyavailable(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyAvailable, obl.m_warehouse_id AS M_Warehouse_ID, obl.qtybom AS QtyBOM, obl.isqtypercentage AS IsQtyPercentage, round(obl.qtybatch, 4) AS QtyBatch, obl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, l.m_locator_id AS M_Locator_ID, l.x AS X, l.y AS Y, l.z AS Z FROM pp_order_bomline obl
JOIN pp_order o ON o.pp_order_id = obl.pp_order_id
LEFT JOIN m_storage s ON s.m_product_id = obl.m_product_id AND s.qtyonhand <> 0 AND obl.m_warehouse_id = (SELECT ld.m_warehouse_id FROM m_locator ld WHERE s.m_locator_id = ld.m_locator_id)
LEFT JOIN m_locator l ON l.m_locator_id = s.m_locator_id ORDER BY obl.m_product_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_PP_Product_BOMLine(SeqNo, LevelNo, Levels, AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, Updated, Created, AD_PInstance_ID, Implosion, M_Product_ID, IsActive, PP_Product_BOM_ID, PP_Product_BOMLine_ID, Description, IsCritical, ComponentType, TM_Product_ID, C_UOM_ID, IssueMethod, Line, M_AttributeSetInstance_ID, Scrap, ValidFrom, ValidTo, QtyBOM, QtyBatch, IsQtyPercentage) AS SELECT t.seqno AS SeqNo, t.levelno AS LevelNo, t.levels AS Levels, t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.createdby AS CreatedBy, t.updatedby AS UpdatedBy, t.updated AS Updated, t.created AS Created, t.ad_pinstance_id AS AD_PInstance_ID, t.implosion AS Implosion, t.sel_product_id AS M_Product_ID, bl.isactive AS IsActive, bl.pp_product_bom_id AS PP_Product_BOM_ID, bl.pp_product_bomline_id AS PP_Product_BOMLine_ID, bl.description AS Description, bl.iscritical AS IsCritical, bl.componenttype AS ComponentType, t.m_product_id AS TM_Product_ID, bl.c_uom_id AS C_UOM_ID, bl.issuemethod AS IssueMethod, bl.line AS Line, bl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, bl.scrap AS Scrap, bl.validfrom AS ValidFrom, bl.validto AS ValidTo, bl.qtybom AS QtyBOM, bl.qtybatch AS QtyBatch, bl.isqtypercentage AS IsQtyPercentage FROM t_bomline t
LEFT JOIN pp_product_bomline bl ON t.pp_product_bomline_id = bl.pp_product_bomline_id
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW RV_PP_Order_Transactions(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, UpdatedBy, Updated, DocumentNo, M_Product_ID, M_Locator_ID, MovementDate, PP_Order_ID, QtyDelivered, QtyScrap, QtyDeliveredLine, QtyIssueShouldBe, QtyScrapLine, QtyIssueScrapShouldBe, createdbyissue, updatedbyissue, QtyToDeliver, DifferenceQty, IsSOTrx, DateOrdered) AS SELECT DISTINCT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updatedby AS UpdatedBy, o.updated AS Updated, o.documentno AS DocumentNo, ol.m_product_id AS M_Product_ID, mt.m_locator_id AS M_Locator_ID, mt.movementdate AS MovementDate, o.pp_order_id AS PP_Order_ID, o.qtydelivered AS QtyDelivered, o.qtyscrap AS QtyScrap, ol.qtydelivered AS QtyDeliveredLine, o.qtydelivered * ol.qtybatch / 100 AS QtyIssueShouldBe, ol.qtyscrap AS QtyScrapLine, o.qtyscrap * ol.qtybatch / 100 AS QtyIssueScrapShouldBe, mt.createdby AS createdbyissue, mt.updatedby AS updatedbyissue, ( SELECT sum(t.movementqty) AS sum FROM m_transaction t WHERE t.pp_cost_collector_id = cc.pp_cost_collector_id) AS QtyToDeliver, (o.qtydelivered + o.qtyscrap) * ol.qtybatch / 100 + (( SELECT sum(t.movementqty) AS sum FROM m_transaction t WHERE t.pp_cost_collector_id = cc.pp_cost_collector_id)) AS DifferenceQty, o.issotrx AS IsSOTrx, o.dateordered AS DateOrdered FROM pp_order o
JOIN pp_order_bomline ol ON ol.pp_order_id = o.pp_order_id
JOIN pp_cost_collector cc ON cc.pp_order_bomline_id = ol.pp_order_bomline_id
LEFT JOIN m_transaction mt ON mt.pp_cost_collector_id = cc.pp_cost_collector_id ORDER BY o.ad_client_id, o.ad_org_id, o.isactive, o.created, o.createdby, o.updatedby, o.updated, o.documentno, ol.m_product_id, mt.m_locator_id, mt.movementdate, o.pp_order_id, o.qtydelivered, o.qtyscrap, ol.qtydelivered, (o.qtydelivered * ol.qtybatch / 100), ol.qtyscrap, (o.qtyscrap * ol.qtybatch / 100), mt.createdby, mt.updatedby, QtyToDeliver, DifferenceQty, o.issotrx, o.dateordered
;

-- Nov 28, 2021, 11:19:15 PM CET
CREATE OR REPLACE VIEW DD_Order_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, DD_Order_ID, C_Order_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, SalesRep_ID, SalesRep_Name, DateOrdered, DatePromised, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Description, POReference, C_Charge_ID, ChargeAmt, Volume, Weight, C_Campaign_ID, C_Project_ID, C_Activity_ID, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, o.dd_order_id AS DD_Order_ID, o.c_order_id AS C_Order_ID, o.issotrx AS IsSOTrx, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, o.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, o.description AS Description, o.poreference AS POReference, o.c_charge_id AS C_Charge_ID, o.chargeamt AS ChargeAmt, o.volume AS Volume, o.weight AS Weight,
o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.m_shipper_id AS M_Shipper_ID, o.deliveryrule AS DeliveryRule, o.deliveryviarule AS DeliveryViaRule, o.priorityrule AS PriorityRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM dd_order o
JOIN c_doctype dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN c_bpartner bp ON o.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN c_bpartner_location bpl ON o.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON o.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_PP_Order_BOMLine(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, Updated, Created, IsActive, PP_Order_BOM_ID, PP_Order_BOMLine_ID, PP_Order_ID, IsCritical, ComponentType, M_Product_ID, C_UOM_ID, QtyRequiered, QtyReserved, QtyAvailable, QtyOnHand, M_Warehouse_ID, QtyBOM, IsQtyPercentage, QtyBatch, QtyBatchSize) AS SELECT obl.ad_client_id AS AD_Client_ID, obl.ad_org_id AS AD_Org_ID, obl.createdby AS CreatedBy, obl.updatedby AS UpdatedBy, obl.updated AS Updated, obl.created AS Created, obl.isactive AS IsActive, obl.pp_order_bom_id AS PP_Order_BOM_ID, obl.pp_order_bomline_id AS PP_Order_BOMLine_ID, obl.pp_order_id AS PP_Order_ID, obl.iscritical AS IsCritical, obl.componenttype AS ComponentType, obl.m_product_id AS M_Product_ID, obl.c_uom_id AS C_UOM_ID, round(obl.qtyrequiered, 4) AS QtyRequiered, round(bomqtyreserved(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyReserved, round(bomqtyavailable(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyAvailable, round(bomqtyonhand(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyOnHand, obl.m_warehouse_id AS M_Warehouse_ID, round(obl.qtybom, 4) AS QtyBOM, obl.isqtypercentage AS IsQtyPercentage, round(obl.qtybatch, 4) AS QtyBatch, CASE WHEN o.qtybatchs = 0 THEN 1 ELSE round(obl.qtyrequiered / o.qtybatchs, 4) END AS QtyBatchSize FROM pp_order_bomline obl
JOIN pp_order o ON o.pp_order_id = obl.pp_order_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW Fact_Acct_Balance(AD_Client_ID, AD_Org_ID, C_AcctSchema_ID, DateAcct, Account_ID, PostingType, M_Product_ID, C_BPartner_ID, C_Project_ID, AD_OrgTrx_ID, C_SalesRegion_ID, C_Activity_ID, C_Campaign_ID, C_LocTo_ID, C_LocFrom_ID, User1_ID, User2_ID, GL_Budget_ID, AmtAcctDr, AmtAcctCr, Qty, CreatedBy, Created, UpdatedBy, Updated, IsActive, C_SubAcct_ID, UserElement1_ID, UserElement2_ID, C_ProjectPhase_ID, C_ProjectTask_ID) AS SELECT a.ad_client_id AS AD_Client_ID, a.ad_org_id AS AD_Org_ID, a.c_acctschema_id AS C_AcctSchema_ID, trunc(a.dateacct) AS DateAcct, a.account_id AS Account_ID, a.postingtype AS PostingType, a.m_product_id AS M_Product_ID, a.c_bpartner_id AS C_BPartner_ID, a.c_project_id AS C_Project_ID, a.ad_orgtrx_id AS AD_OrgTrx_ID, a.c_salesregion_id AS C_SalesRegion_ID, a.c_activity_id AS C_Activity_ID, a.c_campaign_id AS C_Campaign_ID, a.c_locto_id AS C_LocTo_ID, a.c_locfrom_id AS C_LocFrom_ID, a.user1_id AS User1_ID, a.user2_id AS User2_ID, a.gl_budget_id AS GL_Budget_ID, COALESCE(sum(a.amtacctdr), 0) AS AmtAcctDr, COALESCE(sum(a.amtacctcr), 0) AS AmtAcctCr, COALESCE(sum(a.qty), 0) AS Qty, max(a.createdby) AS CreatedBy, max(a.created) AS Created, max(a.updatedby) AS UpdatedBy, max(a.updated) AS Updated, max(a.isactive) AS IsActive, max(a.c_subacct_id) AS C_SubAcct_ID, a.userelement1_id AS UserElement1_ID, a.userelement2_id AS UserElement2_ID, max(a.c_projectphase_id) AS C_ProjectPhase_ID, max(a.c_projecttask_id) AS C_ProjectTask_ID FROM fact_acct a GROUP BY a.ad_client_id, a.ad_org_id, a.c_acctschema_id, (trunc(a.dateacct)), a.account_id, a.postingtype, a.m_product_id, a.c_bpartner_id, a.c_project_id, a.ad_orgtrx_id, a.c_salesregion_id, a.c_activity_id, a.c_campaign_id, a.c_locto_id, a.c_locfrom_id, a.user1_id, a.user2_id, a.userelement1_id, a.userelement2_id, a.gl_budget_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_DD_OrderDetail(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, DD_Order_ID, C_Order_ID, DocStatus, DocAction, C_DocType_ID, IsApproved, SalesRep_ID, IsDropShip, C_BPartner_ID, C_BP_Group_ID, AD_User_ID, POReference, IsSOTrx, C_Campaign_ID, C_Project_ID, C_Activity_ID, DD_OrderLine_ID, DateOrdered, DatePromised, M_Product_ID, M_Locator_ID, M_LocatorTo_ID, M_AttributeSetInstance_ID, ProductAttribute, M_AttributeSetInstanceTo_ID, ProductAttributeTo, M_AttributeSet_ID, M_Lot_ID, GuaranteeDate, Lot, SerNo, C_UOM_ID, QtyEntered, QtyOrdered, QtyReserved, QtyDelivered, ConfirmedQty, QtyInTransit, TargetQty, QtyToDeliver, Description) AS SELECT l.ad_client_id AS AD_Client_ID, l.ad_org_id AS AD_Org_ID, l.isactive AS IsActive, l.created AS Created, l.createdby AS CreatedBy, l.updated AS Updated, l.updatedby AS UpdatedBy, o.dd_order_id AS DD_Order_ID, o.c_order_id AS C_Order_ID, o.docstatus AS DocStatus, o.docaction AS DocAction, o.c_doctype_id AS C_DocType_ID, o.isapproved AS IsApproved, o.salesrep_id AS SalesRep_ID, o.isdropship AS IsDropShip, o.c_bpartner_id AS C_BPartner_ID, bp.c_bp_group_id AS C_BP_Group_ID, o.ad_user_id AS AD_User_ID, o.poreference AS POReference, o.issotrx AS IsSOTrx, l.c_campaign_id AS C_Campaign_ID, l.c_project_id AS C_Project_ID, l.c_activity_id AS C_Activity_ID, l.dd_orderline_id AS DD_OrderLine_ID, l.dateordered AS DateOrdered, l.datepromised AS DatePromised, l.m_product_id AS M_Product_ID, l.m_locator_id AS M_Locator_ID, l.m_locatorto_id AS M_LocatorTo_ID, l.m_attributesetinstance_id AS M_AttributeSetInstance_ID, productattribute(l.m_attributesetinstance_id) AS ProductAttribute, l.m_attributesetinstanceto_id AS M_AttributeSetInstanceTo_ID, productattribute(l.m_attributesetinstanceto_id) AS ProductAttributeTo, pasi.m_attributeset_id AS M_AttributeSet_ID, pasi.m_lot_id AS M_Lot_ID, pasi.guaranteedate AS GuaranteeDate, pasi.lot AS Lot, pasi.serno AS SerNo, l.c_uom_id AS C_UOM_ID,
l.qtyentered AS QtyEntered, l.qtyordered AS QtyOrdered, l.qtyreserved AS QtyReserved, l.qtydelivered AS QtyDelivered, l.confirmedqty AS ConfirmedQty, l.qtyintransit AS QtyInTransit, l.targetqty AS TargetQty, l.qtyordered - l.qtydelivered AS QtyToDeliver, l.description AS Description FROM dd_order o
JOIN dd_orderline l ON l.dd_order_id = o.dd_order_id
JOIN c_bpartner bp ON bp.c_bpartner_id = o.c_bpartner_id
LEFT JOIN m_attributesetinstance pasi ON l.m_attributesetinstance_id = pasi.m_attributesetinstance_id
LEFT JOIN m_attributesetinstance pasito ON l.m_attributesetinstanceto_id = pasito.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_M_Forecast(AD_Client_ID, AD_Org_ID, M_Forecast_ID, Name, C_Calendar_ID, C_Year_ID, C_Period_ID, DatePromised, M_Product_ID, SalesRep_ID, C_UOM_ID, Qty, QtyCalculated, PriceList, PriceStd, PriceLimit, TotalAmt, M_Product_Category_ID, Classification, Group1, Group2) AS SELECT f.ad_client_id AS AD_Client_ID, fl.ad_org_id AS AD_Org_ID, f.m_forecast_id AS M_Forecast_ID, f.name AS Name, f.c_calendar_id AS C_Calendar_ID, f.c_year_id AS C_Year_ID, fl.c_period_id AS C_Period_ID, fl.datepromised AS DatePromised, fl.m_product_id AS M_Product_ID, fl.salesrep_id AS SalesRep_ID, p.c_uom_id AS C_UOM_ID, fl.qty AS Qty, fl.qtycalculated AS QtyCalculated, ( SELECT pp.pricelist FROM m_pricelist_version plv JOIN m_productprice pp ON pp.m_pricelist_version_id = plv.m_pricelist_version_id WHERE pp.m_product_id = fl.m_product_id AND plv.m_pricelist_id = pl.m_pricelist_id) AS PriceList, ( SELECT pp.pricestd FROM m_pricelist_version plv JOIN m_productprice pp ON pp.m_pricelist_version_id = plv.m_pricelist_version_id WHERE pp.m_product_id = fl.m_product_id AND plv.m_pricelist_id = pl.m_pricelist_id) AS PriceStd, ( SELECT pp.pricelimit FROM m_pricelist_version plv JOIN m_productprice pp ON pp.m_pricelist_version_id = plv.m_pricelist_version_id WHERE pp.m_product_id = fl.m_product_id AND plv.m_pricelist_id = pl.m_pricelist_id) AS PriceLimit, (( SELECT pp.pricestd FROM m_pricelist_version plv JOIN m_productprice pp ON pp.m_pricelist_version_id = plv.m_pricelist_version_id WHERE pp.m_product_id = fl.m_product_id AND plv.m_pricelist_id = pl.m_pricelist_id)) * fl.qty AS TotalAmt, p.m_product_category_id AS M_Product_Category_ID, p.classification AS Classification, p.group1 AS Group1, p.group2 AS Group2 FROM m_forecast f
JOIN m_forecastline fl ON f.m_forecast_id = fl.m_forecast_id
JOIN m_product p ON p.m_product_id = fl.m_product_id
JOIN m_pricelist pl ON pl.m_pricelist_id = f.m_pricelist_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_M_Forecast_Period(AD_Client_ID, AD_Org_ID, M_Forecast_ID, Name, C_Calendar_ID, C_Year_ID, C_Period_ID, M_Product_ID, C_UOM_ID, Qty, QtyCalculated, TotalAmt) AS SELECT f.ad_client_id AS AD_Client_ID, f.ad_org_id AS AD_Org_ID, f.m_forecast_id AS M_Forecast_ID, max(f.name) AS Name, f.c_calendar_id AS C_Calendar_ID, f.c_year_id AS C_Year_ID, f.c_period_id AS C_Period_ID, f.m_product_id AS M_Product_ID, f.c_uom_id AS C_UOM_ID, sum(f.qty) AS Qty, sum(f.qtycalculated) AS QtyCalculated, sum(f.totalamt) AS TotalAmt FROM rv_m_forecast f GROUP BY f.ad_client_id, f.ad_org_id, f.m_forecast_id, f.m_product_id, f.c_uom_id, f.c_calendar_id, f.c_year_id, f.c_period_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_PP_Order_Workflow(AD_Client_ID, AD_Org_ID, Created, CreatedBy, IsActive, Updated, UpdatedBy, PP_Order_Workflow_ID, Name, PP_Order_ID, DocStatus, Value, S_Resource_ID, DurationRequiered, DurationReal, Duration, MovingTime, WaitingTime, SetupTime, QueuingTime, QtyDelivered, QtyReject, QtyScrap, DateStartSchedule, DateFinishSchedule, C_BPartner_ID, Description, IsMilestone, IsSubcontracting) AS SELECT n.ad_client_id AS AD_Client_ID, n.ad_org_id AS AD_Org_ID, n.created AS Created, n.createdby AS CreatedBy, n.isactive AS IsActive, n.updated AS Updated, n.updatedby AS UpdatedBy, owf.pp_order_workflow_id AS PP_Order_Workflow_ID, n.name AS Name, n.pp_order_id AS PP_Order_ID, n.docstatus AS DocStatus, n.value AS Value, n.s_resource_id AS S_Resource_ID, n.durationrequiered AS DurationRequiered, n.durationreal AS DurationReal, n.durationrequiered - n.durationreal AS Duration, n.movingtime AS MovingTime, n.waitingtime AS WaitingTime, n.setuptime AS SetupTime, n.queuingtime AS QueuingTime, n.qtydelivered AS QtyDelivered, n.qtyreject AS QtyReject, n.qtyscrap AS QtyScrap, n.datestartschedule AS DateStartSchedule, n.datefinishschedule AS DateFinishSchedule, n.c_bpartner_id AS C_BPartner_ID, n.description AS Description, n.ismilestone AS IsMilestone, n.issubcontracting AS IsSubcontracting FROM pp_order_workflow owf
JOIN pp_order_node n ON n.pp_order_workflow_id = owf.pp_order_workflow_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW T_BOMLine_Costs(SeqNo, LevelNo, Levels, AD_Client_ID, C_AcctSchema_ID, AD_Org_ID, CreatedBy, UpdatedBy, Updated, Created, AD_PInstance_ID, Implosion, M_Product_ID, M_CostElement_ID, CurrentCostPrice, CurrentCostPriceLL, FutureCostPrice, FutureCostPriceLL, IsCostFrozen, QtyBOM, Cost, CostStandard, M_CostType_ID, CostingMethod, IsActive, PP_Product_BOM_ID, PP_Product_BOMLine_ID, Description, IsCritical, ComponentType, TM_Product_ID, C_UOM_ID, IssueMethod, Line, M_AttributeSetInstance_ID, Scrap, ValidFrom, ValidTo, IsQtyPercentage) AS SELECT t.seqno AS SeqNo, t.levelno AS LevelNo, t.levels AS Levels, t.ad_client_id AS AD_Client_ID, t.c_acctschema_id AS C_AcctSchema_ID, t.ad_org_id AS AD_Org_ID, t.createdby AS CreatedBy, t.updatedby AS UpdatedBy, t.updated AS Updated, t.created AS Created, t.ad_pinstance_id AS AD_PInstance_ID, t.implosion AS Implosion, t.sel_product_id AS M_Product_ID, t.m_costelement_id AS M_CostElement_ID, t.currentcostprice AS CurrentCostPrice, t.currentcostpricell AS CurrentCostPriceLL, t.futurecostprice AS FutureCostPrice, t.futurecostpricell AS FutureCostPriceLL, t.iscostfrozen AS IsCostFrozen, t.qtybom AS QtyBOM, (t.currentcostprice + t.currentcostpricell) * t.qtybom AS Cost, (t.futurecostprice + t.futurecostpricell) * t.qtybom AS CostStandard, t.m_costtype_id AS M_CostType_ID, t.costingmethod AS CostingMethod, bl.isactive AS IsActive, bl.pp_product_bom_id AS PP_Product_BOM_ID, bl.pp_product_bomline_id AS PP_Product_BOMLine_ID, bl.description AS Description, bl.iscritical AS IsCritical, bl.componenttype AS ComponentType, t.m_product_id AS TM_Product_ID, bl.c_uom_id AS C_UOM_ID, bl.issuemethod AS IssueMethod, bl.line AS Line, bl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, bl.scrap AS Scrap, bl.validfrom AS ValidFrom, bl.validto AS ValidTo, bl.isqtypercentage AS IsQtyPercentage FROM t_bomline t
LEFT JOIN pp_product_bomline bl ON t.pp_product_bomline_id = bl.pp_product_bomline_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW PP_Order_BOM_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocumentNo, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, C_UOM_ID, S_Resource_ID, PP_Product_BOM_ID, AD_Workflow_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchSize, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, Yield, C_Campaign_ID, C_Project_ID, C_Activity_ID, BOMType, BOMUse, Description, Help, M_AttributeSetInstance_ID, M_Product_ID, Name, Revision, ValidFrom, ValidTo, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, o.pp_order_id AS PP_Order_ID, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, d.printname AS DocumentType, d.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.c_uom_id AS C_UOM_ID, o.s_resource_id AS S_Resource_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID, o.priorityrule AS PriorityRule, o.qtybatchsize AS QtyBatchSize, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered, o.qtyordered AS QtyOrdered,
o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.yield AS Yield, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, ob.bomtype AS BOMType, ob.bomuse AS BOMUse, ob.description AS Description, ob.help AS Help, ob.m_attributesetinstance_id AS M_AttributeSetInstance_ID, ob.m_product_id AS M_Product_ID, ob.name AS Name, ob.revision AS Revision, ob.validfrom AS ValidFrom, ob.validto AS ValidTo, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN c_doctype d ON o.c_doctype_id = d.c_doctype_id
JOIN pp_order_bom ob ON ob.pp_order_id = o.pp_order_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_Unprocessed(AD_Client_ID, AD_Org_ID, Created, CreatedBy, Updated, UpdatedBy, IsActive, DocumentNo, DateDoc, DateAcct, AD_Table_ID, Record_ID, IsSOTrx, Posted, Processing, Processed, DocStatus) AS SELECT gl_journal.ad_client_id AS AD_Client_ID, gl_journal.ad_org_id AS AD_Org_ID, gl_journal.created AS Created, gl_journal.createdby AS CreatedBy, gl_journal.updated AS Updated, gl_journal.updatedby AS UpdatedBy, gl_journal.isactive AS IsActive, gl_journal.documentno AS DocumentNo, gl_journal.datedoc AS DateDoc, gl_journal.dateacct AS DateAcct, 224 AS AD_Table_ID, gl_journal.gl_journal_id AS Record_ID, 'N' AS IsSOTrx, gl_journal.posted AS Posted, gl_journal.processing AS Processing, gl_journal.processed AS Processed, gl_journal.docstatus AS DocStatus FROM gl_journal WHERE gl_journal.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT c_invoice.ad_client_id AS AD_Client_ID, c_invoice.ad_org_id AS AD_Org_ID, c_invoice.created AS Created, c_invoice.createdby AS CreatedBy, c_invoice.updated AS Updated, c_invoice.updatedby AS UpdatedBy, c_invoice.isactive AS IsActive, c_invoice.documentno AS DocumentNo, c_invoice.dateinvoiced AS DateDoc, c_invoice.dateacct AS DateAcct, 318 AS AD_Table_ID, c_invoice.c_invoice_id AS Record_ID, c_invoice.issotrx AS IsSOTrx, c_invoice.posted AS Posted, c_invoice.processing AS Processing, c_invoice.processed AS Processed, c_invoice.docstatus AS DocStatus FROM c_invoice WHERE c_invoice.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT m_inout.ad_client_id AS AD_Client_ID, m_inout.ad_org_id AS AD_Org_ID, m_inout.created AS Created, m_inout.createdby AS CreatedBy, m_inout.updated AS Updated, m_inout.updatedby AS UpdatedBy, m_inout.isactive AS IsActive, m_inout.documentno AS DocumentNo, m_inout.movementdate AS DateDoc, m_inout.dateacct AS DateAcct, 319 AS AD_Table_ID, m_inout.m_inout_id AS Record_ID, m_inout.issotrx AS IsSOTrx, m_inout.posted AS Posted, m_inout.processing AS Processing, m_inout.processed AS Processed,
m_inout.docstatus AS DocStatus FROM m_inout WHERE m_inout.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT m_inventory.ad_client_id AS AD_Client_ID, m_inventory.ad_org_id AS AD_Org_ID, m_inventory.created AS Created, m_inventory.createdby AS CreatedBy, m_inventory.updated AS Updated, m_inventory.updatedby AS UpdatedBy, m_inventory.isactive AS IsActive, m_inventory.documentno AS DocumentNo, m_inventory.movementdate AS DateDoc, m_inventory.movementdate AS DateAcct, 321 AS AD_Table_ID, m_inventory.m_inventory_id AS Record_ID, 'N' AS IsSOTrx, m_inventory.posted AS Posted, m_inventory.processing AS Processing, m_inventory.processed AS Processed, m_inventory.docstatus AS DocStatus FROM m_inventory WHERE m_inventory.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT m_movement.ad_client_id AS AD_Client_ID, m_movement.ad_org_id AS AD_Org_ID, m_movement.created AS Created, m_movement.createdby AS CreatedBy, m_movement.updated AS Updated, m_movement.updatedby AS UpdatedBy, m_movement.isactive AS IsActive, m_movement.documentno AS DocumentNo, m_movement.movementdate AS DateDoc, m_movement.movementdate AS DateAcct, 323 AS AD_Table_ID, m_movement.m_movement_id AS Record_ID, 'N' AS IsSOTrx, m_movement.posted AS Posted, m_movement.processing AS Processing, m_movement.processed AS Processed, m_movement.docstatus AS DocStatus FROM m_movement WHERE m_movement.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT c_cash.ad_client_id AS AD_Client_ID, c_cash.ad_org_id AS AD_Org_ID, c_cash.created AS Created, c_cash.createdby AS CreatedBy, c_cash.updated AS Updated, c_cash.updatedby AS UpdatedBy, c_cash.isactive AS IsActive, c_cash.name AS DocumentNo, c_cash.statementdate AS DateDoc, c_cash.dateacct AS DateAcct, 407 AS AD_Table_ID, c_cash.c_cash_id AS Record_ID, 'N' AS IsSOTrx, c_cash.posted AS Posted, c_cash.processing AS Processing, c_cash.processed AS Processed, c_cash.docstatus AS DocStatus FROM c_cash WHERE c_cash.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION
SELECT c_payment.ad_client_id AS AD_Client_ID, c_payment.ad_org_id AS AD_Org_ID, c_payment.created AS Created, c_payment.createdby AS CreatedBy, c_payment.updated AS Updated, c_payment.updatedby AS UpdatedBy, c_payment.isactive AS IsActive, c_payment.documentno AS DocumentNo, c_payment.datetrx AS DateDoc, c_payment.datetrx AS DateAcct, 335 AS AD_Table_ID, c_payment.c_payment_id AS Record_ID, 'N' AS IsSOTrx, c_payment.posted AS Posted, c_payment.processing AS Processing, c_payment.processed AS Processed, c_payment.docstatus AS DocStatus FROM c_payment WHERE c_payment.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT c_allocationhdr.ad_client_id AS AD_Client_ID, c_allocationhdr.ad_org_id AS AD_Org_ID, c_allocationhdr.created AS Created, c_allocationhdr.createdby AS CreatedBy, c_allocationhdr.updated AS Updated, c_allocationhdr.updatedby AS UpdatedBy, c_allocationhdr.isactive AS IsActive, c_allocationhdr.documentno AS DocumentNo, c_allocationhdr.datetrx AS DateDoc, c_allocationhdr.datetrx AS DateAcct, 735 AS AD_Table_ID, c_allocationhdr.c_allocationhdr_id AS Record_ID, 'N' AS IsSOTrx, c_allocationhdr.posted AS Posted, c_allocationhdr.processing AS Processing, c_allocationhdr.processed AS Processed, c_allocationhdr.docstatus AS DocStatus FROM c_allocationhdr WHERE c_allocationhdr.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT c_bankstatement.ad_client_id AS AD_Client_ID, c_bankstatement.ad_org_id AS AD_Org_ID, c_bankstatement.created AS Created, c_bankstatement.createdby AS CreatedBy, c_bankstatement.updated AS Updated, c_bankstatement.updatedby AS UpdatedBy, c_bankstatement.isactive AS IsActive, c_bankstatement.name AS DocumentNo, c_bankstatement.statementdate AS DateDoc, c_bankstatement.statementdate AS DateAcct, 392 AS AD_Table_ID, c_bankstatement.c_bankstatement_id AS Record_ID, 'N' AS IsSOTrx, c_bankstatement.posted AS Posted, c_bankstatement.processing AS Processing, c_bankstatement.processed AS Processed, c_bankstatement.docstatus AS DocStatus
FROM c_bankstatement WHERE c_bankstatement.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT c_order.ad_client_id AS AD_Client_ID, c_order.ad_org_id AS AD_Org_ID, c_order.created AS Created, c_order.createdby AS CreatedBy, c_order.updated AS Updated, c_order.updatedby AS UpdatedBy, c_order.isactive AS IsActive, c_order.documentno AS DocumentNo, c_order.dateordered AS DateDoc, c_order.dateacct AS DateAcct, 259 AS AD_Table_ID, c_order.c_order_id AS Record_ID, c_order.issotrx AS IsSOTrx, c_order.posted AS Posted, c_order.processing AS Processing, c_order.processed AS Processed, c_order.docstatus AS DocStatus FROM c_order WHERE c_order.docstatus NOT IN ('CO', 'CL', 'VO', 'RE') UNION SELECT m_requisition.ad_client_id AS AD_Client_ID, m_requisition.ad_org_id AS AD_Org_ID, m_requisition.created AS Created, m_requisition.createdby AS CreatedBy, m_requisition.updated AS Updated, m_requisition.updatedby AS UpdatedBy, m_requisition.isactive AS IsActive, m_requisition.documentno AS DocumentNo, m_requisition.datedoc AS DateDoc, m_requisition.datedoc AS DateAcct, 702 AS AD_Table_ID, m_requisition.m_requisition_id AS Record_ID, 'N' AS IsSOTrx, m_requisition.posted AS Posted, m_requisition.processing AS Processing, m_requisition.processed AS Processed, m_requisition.docstatus AS DocStatus FROM m_requisition WHERE m_requisition.docstatus NOT IN ('CO', 'CL', 'VO', 'RE')
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW PP_Order_BOMLine_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Description, Feature, M_Product_ID, BackflushGroup, C_UOM_ID, ComponentType, DateDelivered, Forecast, Help, IsCritical, IssueMethod, LeadTimeOffset, Line, M_AttributeSetInstance_ID, M_ChangeNotice_ID, M_Locator_ID, M_Warehouse_ID, PP_Order_BOM_ID, PP_Order_BOMLine_ID, PP_Order_ID, QtyDelivered, QtyPost, QtyReject, QtyScrap, Scrap, ValidFrom, ValidTo, Assay, AD_User_ID, QtyRequiered, QtyReserved, QtyAvailable, QtyOnHand, QtyBOM, IsQtyPercentage, QtyBatch, QtyBatchSize) AS SELECT obl.ad_client_id AS AD_Client_ID, obl.ad_org_id AS AD_Org_ID, obl.isactive AS IsActive, obl.created AS Created, obl.createdby AS CreatedBy, obl.updated AS Updated, obl.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, obl.description AS Description, obl.feature AS Feature, obl.m_product_id AS M_Product_ID, obl.backflushgroup AS BackflushGroup, obl.c_uom_id AS C_UOM_ID, obl.componenttype AS ComponentType, obl.datedelivered AS DateDelivered, obl.forecast AS Forecast, obl.help AS Help, obl.iscritical AS IsCritical, obl.issuemethod AS IssueMethod, obl.leadtimeoffset AS LeadTimeOffset, obl.line AS Line, obl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, obl.m_changenotice_id AS M_ChangeNotice_ID, obl.m_locator_id AS M_Locator_ID, obl.m_warehouse_id AS M_Warehouse_ID, obl.pp_order_bom_id AS PP_Order_BOM_ID, obl.pp_order_bomline_id AS PP_Order_BOMLine_ID, obl.pp_order_id AS PP_Order_ID, obl.qtydelivered AS QtyDelivered, obl.qtypost AS QtyPost, obl.qtyreject AS QtyReject, obl.qtyscrap AS QtyScrap, obl.scrap AS Scrap, obl.validfrom AS ValidFrom, obl.validto AS ValidTo, obl.assay AS Assay, obl.ad_user_id AS AD_User_ID, round(obl.qtyrequiered, 4) AS QtyRequiered, round(bomqtyreserved(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyReserved, round(bomqtyavailable(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyAvailable,
round(bomqtyonhand(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyOnHand, round(obl.qtybom, 4) AS QtyBOM, obl.isqtypercentage AS IsQtyPercentage, round(obl.qtybatch, 4) AS QtyBatch, CASE WHEN o.qtybatchs = 0 THEN 1 ELSE round(obl.qtyrequiered / o.qtybatchs, 4) END AS QtyBatchSize FROM pp_order_bomline obl
JOIN pp_order o ON o.pp_order_id = obl.pp_order_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW PP_Order_Node_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Name, C_BPartner_ID, Cost, DateFinish, DateFinishSchedule, DateStart, DateStartSchedule, Description, DocAction, DocStatus, Duration, DurationReal, DurationRequiered, Help, IsMilestone, IsSubcontracting, MovingTime, OverlapUnits, PP_Order_ID, PP_Order_Workflow_ID, PP_Order_Node_ID, Priority, QtyDelivered, QtyRequiered, QtyScrap, QueuingTime, S_Resource_ID, SetupTime, SetupTimeReal, UnitsCycles, ValidFrom, ValidTo, Value, WaitingTime, WorkingTime, Yield) AS SELECT onode.ad_client_id AS AD_Client_ID, onode.ad_org_id AS AD_Org_ID, onode.isactive AS IsActive, onode.created AS Created, onode.createdby AS CreatedBy, onode.updated AS Updated, onode.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, onode.name AS Name, onode.c_bpartner_id AS C_BPartner_ID, onode.cost AS Cost, onode.datefinish AS DateFinish, onode.datefinishschedule AS DateFinishSchedule, onode.datestart AS DateStart, onode.datestartschedule AS DateStartSchedule, onode.description AS Description, onode.docaction AS DocAction, onode.docstatus AS DocStatus, onode.duration AS Duration, onode.durationreal AS DurationReal, onode.durationrequiered AS DurationRequiered, onode.help AS Help, onode.ismilestone AS IsMilestone, onode.issubcontracting AS IsSubcontracting, onode.movingtime AS MovingTime, onode.overlapunits AS OverlapUnits, onode.pp_order_id AS PP_Order_ID, onode.pp_order_workflow_id AS PP_Order_Workflow_ID, onode.pp_order_node_id AS PP_Order_Node_ID, onode.priority AS Priority, onode.qtydelivered AS QtyDelivered, onode.qtyrequiered AS QtyRequiered, onode.qtyscrap AS QtyScrap, onode.queuingtime AS QueuingTime, onode.s_resource_id AS S_Resource_ID, onode.setuptime AS SetupTime, onode.setuptimereal AS SetupTimeReal, onode.unitscycles AS UnitsCycles, onode.validfrom AS ValidFrom, onode.validto AS ValidTo, onode.value AS Value,
onode.waitingtime AS WaitingTime, onode.workingtime AS WorkingTime, onode.yield AS Yield FROM pp_order_node onode
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW PP_Order_Workflow_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, C_UOM_ID, S_Resource_ID, PP_Product_BOM_ID, AD_Workflow_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, C_Campaign_ID, C_Project_ID, C_Activity_ID, Name, Description, Help, Author, Cost, DocumentNo, Duration, DurationUnit, Version, ValidFrom, ValidTo, MovingTime, OverlapUnits, PublishStatus, QueuingTime, SetupTime, UnitsCycles, WaitingTime, WorkflowType, WorkingTime, Yield, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, o.pp_order_id AS PP_Order_ID, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, d.printname AS DocumentType, d.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.c_uom_id AS C_UOM_ID, o.s_resource_id AS S_Resource_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID, o.priorityrule AS PriorityRule, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered,
o.qtyordered AS QtyOrdered, o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, ow.name AS Name, ow.description AS Description, ow.help AS Help, ow.author AS Author, ow.cost AS Cost, ow.documentno AS DocumentNo, ow.duration AS Duration, ow.durationunit AS DurationUnit, ow.version AS Version, ow.validfrom AS ValidFrom, ow.validto AS ValidTo, ow.movingtime AS MovingTime, ow.overlapunits AS OverlapUnits, ow.publishstatus AS PublishStatus, ow.queuingtime AS QueuingTime, ow.setuptime AS SetupTime, ow.unitscycles AS UnitsCycles, ow.waitingtime AS WaitingTime, ow.workflowtype AS WorkflowType, ow.workingtime AS WorkingTime, ow.yield AS Yield, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN pp_order_workflow ow ON ow.pp_order_id = o.pp_order_id
JOIN c_doctype d ON o.c_doctype_id = d.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW PP_Order_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocumentNo, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, Description, M_Product_ID, M_AttributeSetInstance_ID, C_UOM_ID, S_Resource_ID, PP_Product_BOM_ID, AD_Workflow_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchSize, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, Yield, C_Campaign_ID, C_Project_ID, C_Activity_ID, User1_ID, User2_ID, AD_OrgTrx_ID, C_DocTypeTarget_ID, ScheduleType, IsApproved, DocAction, Posted, IsPrinted, OrderType, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, o.pp_order_id AS PP_Order_ID, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.description AS Description, o.m_product_id AS M_Product_ID, o.m_attributesetinstance_id AS M_AttributeSetInstance_ID, o.c_uom_id AS C_UOM_ID, o.s_resource_id AS S_Resource_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID,
o.priorityrule AS PriorityRule, o.qtybatchsize AS QtyBatchSize, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered, o.qtyordered AS QtyOrdered, o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.yield AS Yield, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.user1_id AS User1_ID, o.user2_id AS User2_ID, o.ad_orgtrx_id AS AD_OrgTrx_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.scheduletype AS ScheduleType, o.isapproved AS IsApproved, o.docaction AS DocAction, o.posted AS Posted, o.isprinted AS IsPrinted, o.ordertype AS OrderType, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN c_doctype dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_Storage_Per_Product(AD_Client_ID, AD_Org_ID, M_Product_ID, Value, Name, Description, UPC, SKU, C_UOM_ID, M_Product_Category_ID, Classification, Weight, Volume, VersionNo, GuaranteeDays, GuaranteeDaysMin, SumQtyOnHand) AS SELECT strg.ad_client_id AS AD_Client_ID, strg.ad_org_id AS AD_Org_ID, p.m_product_id AS M_Product_ID, p.value AS Value, p.name AS Name, p.description AS Description, p.upc AS UPC, p.sku AS SKU, p.c_uom_id AS C_UOM_ID, p.m_product_category_id AS M_Product_Category_ID, p.classification AS Classification, p.weight AS Weight, p.volume AS Volume, p.versionno AS VersionNo, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, strg.sumqtyonhand AS SumQtyOnHand FROM m_product p
JOIN ( SELECT rv_storage.m_product_id, rv_storage.m_locator_id, sum(rv_storage.qtyonhand) AS sumqtyonhand, rv_storage.ad_client_id, rv_storage.ad_org_id FROM rv_storage GROUP BY rv_storage.m_product_id, rv_storage.m_locator_id, rv_storage.ad_client_id, rv_storage.ad_org_id) strg ON p.m_product_id = strg.m_product_id
JOIN m_locator l ON strg.m_locator_id = l.m_locator_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_RequestUpdates_Only(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, R_Request_ID, AD_User_ID) AS SELECT min(rv_requestupdates.ad_client_id) AS AD_Client_ID, min(rv_requestupdates.ad_org_id) AS AD_Org_ID, 'Y' AS IsActive, getdate() AS Created, 0 AS CreatedBy, getdate() AS Updated, 0 AS UpdatedBy, rv_requestupdates.r_request_id AS R_Request_ID, rv_requestupdates.ad_user_id AS AD_User_ID FROM rv_requestupdates WHERE rv_requestupdates.isactive = 'Y' GROUP BY rv_requestupdates.r_request_id, rv_requestupdates.ad_user_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_C_Invoice_ProdMonth(AD_Client_ID, AD_Org_ID, M_Product_Category_ID, DateInvoiced, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineDiscount, LineOverLimitAmt, LineOverLimit, QtyInvoiced, IsSOTrx) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.m_product_category_id AS M_Product_Category_ID, firstof(il.dateinvoiced, 'MM') AS DateInvoiced, sum(il.linenetamt) AS LineNetAmt, sum(il.linelistamt) AS LineListAmt, sum(il.linelimitamt) AS LineLimitAmt, sum(il.linediscountamt) AS LineDiscountAmt, CASE WHEN sum(il.linelistamt) = 0 THEN 0 ELSE currencyround((sum(il.linelistamt) - sum(il.linenetamt)) / sum(il.linelistamt) * 100, i.c_currency_id, 'N') END AS LineDiscount, sum(il.lineoverlimitamt) AS LineOverLimitAmt, CASE WHEN sum(il.linenetamt) = 0 THEN 0 ELSE 100 - currencyround((sum(il.linenetamt) - sum(il.lineoverlimitamt)) / sum(il.linenetamt) * 100, i.c_currency_id, 'N') END AS LineOverLimit, sum(il.qtyinvoiced) AS QtyInvoiced, il.issotrx AS IsSOTrx FROM rv_c_invoiceline il
JOIN c_invoice i ON i.c_invoice_id = il.c_invoice_id GROUP BY il.ad_client_id, il.ad_org_id, il.m_product_category_id, (firstof(il.dateinvoiced, 'MM')), il.issotrx, i.c_currency_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_ProjectLineIssue(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Project_ID, M_Product_ID, C_ProjectLine_ID, Line, Description, PlannedQty, PlannedPrice, PlannedAmt, PlannedMarginAmt, CommittedQty, C_ProjectIssue_ID, M_Locator_ID, MovementQty, MovementDate, IssueLine, IssueDescription, M_InOutLine_ID, S_TimeExpenseLine_ID, C_AcctSchema_ID, Account_ID, AmtSourceDr, AmtSourceCr, AmtAcctDr, AmtAcctCr, LineMargin) AS SELECT COALESCE(l.ad_client_id, i.ad_client_id) AS AD_Client_ID, COALESCE(l.ad_org_id, i.ad_org_id) AS AD_Org_ID, COALESCE(l.isactive, i.isactive) AS IsActive, COALESCE(l.created, i.created) AS Created, COALESCE(l.createdby, i.createdby) AS CreatedBy, COALESCE(l.updated, i.updated) AS Updated, COALESCE(l.updatedby, i.updatedby) AS UpdatedBy, COALESCE(l.c_project_id, i.c_project_id) AS C_Project_ID, COALESCE(l.m_product_id, i.m_product_id) AS M_Product_ID, l.c_projectline_id AS C_ProjectLine_ID, l.line AS Line, l.description AS Description, l.plannedqty AS PlannedQty, l.plannedprice AS PlannedPrice, l.plannedamt AS PlannedAmt, l.plannedmarginamt AS PlannedMarginAmt, l.committedqty AS CommittedQty, i.c_projectissue_id AS C_ProjectIssue_ID, i.m_locator_id AS M_Locator_ID, i.movementqty AS MovementQty, i.movementdate AS MovementDate, i.line AS IssueLine, i.description AS IssueDescription, i.m_inoutline_id AS M_InOutLine_ID, i.s_timeexpenseline_id AS S_TimeExpenseLine_ID, fa.c_acctschema_id AS C_AcctSchema_ID, fa.account_id AS Account_ID, fa.amtsourcedr AS AmtSourceDr, fa.amtsourcecr AS AmtSourceCr, fa.amtacctdr AS AmtAcctDr, fa.amtacctcr AS AmtAcctCr, l.plannedamt - fa.amtsourcedr + fa.amtsourcecr AS LineMargin FROM c_projectline l
FULL JOIN c_projectissue i ON i.c_project_id = l.c_project_id AND i.c_projectissue_id = l.c_projectissue_id
LEFT JOIN fact_acct fa ON fa.ad_table_id = 623 AND fa.record_id = i.c_projectissue_id AND fa.m_locator_id IS NULL
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_Click_Unprocessed(W_Click_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, TargetURL, Referrer, Remote_Host, Remote_Addr, UserAgent, AcceptLanguage, Processed, W_ClickCount_ID, AD_User_ID, EMail) AS SELECT w_click.w_click_id AS W_Click_ID, w_click.ad_client_id AS AD_Client_ID, w_click.ad_org_id AS AD_Org_ID, w_click.isactive AS IsActive, w_click.created AS Created, w_click.createdby AS CreatedBy, w_click.updated AS Updated, w_click.updatedby AS UpdatedBy, w_click.targeturl AS TargetURL, w_click.referrer AS Referrer, w_click.remote_host AS Remote_Host, w_click.remote_addr AS Remote_Addr, w_click.useragent AS UserAgent, w_click.acceptlanguage AS AcceptLanguage, w_click.processed AS Processed, w_click.w_clickcount_id AS W_ClickCount_ID, w_click.ad_user_id AS AD_User_ID, w_click.email AS EMail FROM w_click WHERE w_click.w_clickcount_id IS NULL OR w_click.processed = 'N'
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW C_PaySelection_Remittance_v(AD_Client_ID, AD_Org_ID, AD_Language, C_PaySelection_ID, C_PaySelectionLine_ID, C_PaySelectionCheck_ID, PaymentRule, Line, OpenAmt, PayAmt, DiscountAmt, DifferenceAmt, C_BPartner_ID, DocumentNo, DateInvoiced, GrandTotal, AmtInWords, C_Invoice_ID, c_payselectionline_created, C_PaySelectionLine_CreatedBy, c_payselectionline_description, c_payselectionline_isactive, IsManual, c_payselectionline_issotrx, Processed, c_payselectionline_updated, C_PaySelectionLine_UpdatedBy, C_Invoice_AD_Org_ID, AD_OrgTrx_ID, AD_User_ID, C_Activity_ID, C_BPartner_Location_ID, C_Campaign_ID, C_Charge_ID, C_ConversionType_ID, C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, ChargeAmt, C_Payment_ID, C_PaymentTerm_ID, C_Project_ID, c_invoice_created, C_Invoice_CreatedBy, DateAcct, DateOrdered, DatePrinted, c_invoice_description, DocAction, DocStatus, DunningGrace, GenerateTo, InvoiceCollectionType, c_invoice_isactive, IsApproved, IsDiscountPrinted, IsInDispute, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, c_invoice_issotrx, IsTaxIncluded, IsTransferred, M_PriceList_ID, M_RMA_ID, c_invoice_paymentrule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SendEMail, TotalLines, c_invoice_updated, C_Invoice_UpdatedBy, User1_ID, User2_ID) AS SELECT psl.ad_client_id AS AD_Client_ID, psl.ad_org_id AS AD_Org_ID, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, psl.c_payselection_id AS C_PaySelection_ID, psl.c_payselectionline_id AS C_PaySelectionLine_ID, psl.c_payselectioncheck_id AS C_PaySelectionCheck_ID, psl.paymentrule AS PaymentRule, psl.line AS Line, psl.openamt AS OpenAmt, psl.payamt AS PayAmt, psl.discountamt AS DiscountAmt, psl.differenceamt AS DifferenceAmt, i.c_bpartner_id AS C_BPartner_ID, i.documentno AS DocumentNo, i.dateinvoiced AS DateInvoiced, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, psl.c_invoice_id AS C_Invoice_ID, psl.created AS c_payselectionline_created,
psl.createdby AS C_PaySelectionLine_CreatedBy, psl.description AS c_payselectionline_description, psl.isactive AS c_payselectionline_isactive, psl.ismanual AS IsManual, psl.issotrx AS c_payselectionline_issotrx, psl.processed AS Processed, psl.updated AS c_payselectionline_updated, psl.updatedby AS C_PaySelectionLine_UpdatedBy, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_activity_id AS C_Activity_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_campaign_id AS C_Campaign_ID, i.c_charge_id AS C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_project_id AS C_Project_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateacct AS DateAcct, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS c_invoice_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.issotrx AS c_invoice_issotrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS c_invoice_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS c_invoice_updated,
i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS User1_ID, i.user2_id AS User2_ID FROM c_payselectionline psl
JOIN c_invoice i ON psl.c_invoice_id = i.c_invoice_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_InOutConfirm(M_InOutConfirm_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_InOut_ID, DocumentNo, ConfirmType, IsApproved, IsCancelled, Description, Processing, Processed, C_BPartner_ID, C_BPartner_Location_ID, M_Warehouse_ID, C_Order_ID, IsSOTrx) AS SELECT c.m_inoutconfirm_id AS M_InOutConfirm_ID, c.ad_client_id AS AD_Client_ID, c.ad_org_id AS AD_Org_ID, c.isactive AS IsActive, c.created AS Created, c.createdby AS CreatedBy, c.updated AS Updated, c.updatedby AS UpdatedBy, c.m_inout_id AS M_InOut_ID, c.documentno AS DocumentNo, c.confirmtype AS ConfirmType, c.isapproved AS IsApproved, c.iscancelled AS IsCancelled, c.description AS Description, c.processing AS Processing, c.processed AS Processed, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.m_warehouse_id AS M_Warehouse_ID, i.c_order_id AS C_Order_ID, i.issotrx AS IsSOTrx FROM m_inoutconfirm c
JOIN m_inout i ON c.m_inout_id = i.m_inout_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_PrintFormatDetail(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_PrintFormat_ID, Name, Description, IsTableBased, IsForm, AD_Table_ID, AD_ReportView_ID, AD_PrintPaper_ID, Default_AD_PrintColor_ID, Default_AD_PrintFont_ID, IsStandardHeaderFooter, AD_PrintTableFormat_ID, HeaderMargin, FooterMargin, PrinterName, IsDefault, AD_PrintFormatItem_ID, ItemName, PrintName, PrintNameSuffix, IsPrinted, PrintAreaType, SeqNo, PrintFormatType, AD_Column_ID, AD_PrintFormatChild_ID, ImageIsAttached, ImageURL, IsRelativePosition, IsNextLine, XSpace, YSpace, XPosition, YPosition, MaxWidth, IsHeightOneLine, MaxHeight, IsFixedWidth, IsSetNLPosition, IsSuppressNull, BelowColumn, FieldAlignmentType, LineAlignmentType, AD_PrintColor_ID, AD_PrintFont_ID, IsOrderBy, SortNo, IsGroupBy, IsPageBreak, IsNextPage, IsSummarized, IsAveraged, IsCounted, IsMinCalc, IsMaxCalc, IsVarianceCalc, IsDeviationCalc, IsRunningTotal, RunningTotalLines, AD_PrintGraph_ID) AS SELECT f.ad_client_id AS AD_Client_ID, f.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, f.ad_printformat_id AS AD_PrintFormat_ID, f.name AS Name, f.description AS Description, f.istablebased AS IsTableBased, f.isform AS IsForm, f.ad_table_id AS AD_Table_ID, f.ad_reportview_id AS AD_ReportView_ID, f.ad_printpaper_id AS AD_PrintPaper_ID, f.ad_printcolor_id AS Default_AD_PrintColor_ID, f.ad_printfont_id AS Default_AD_PrintFont_ID, f.isstandardheaderfooter AS IsStandardHeaderFooter, f.ad_printtableformat_id AS AD_PrintTableFormat_ID, f.headermargin AS HeaderMargin, f.footermargin AS FooterMargin, f.printername AS PrinterName, f.isdefault AS IsDefault, i.ad_printformatitem_id AS AD_PrintFormatItem_ID, i.name AS ItemName, i.printname AS PrintName, i.printnamesuffix AS PrintNameSuffix, i.isprinted AS IsPrinted, i.printareatype AS PrintAreaType, i.seqno AS SeqNo,
i.printformattype AS PrintFormatType, i.ad_column_id AS AD_Column_ID, i.ad_printformatchild_id AS AD_PrintFormatChild_ID, i.imageisattached AS ImageIsAttached, i.imageurl AS ImageURL, i.isrelativeposition AS IsRelativePosition, i.isnextline AS IsNextLine, i.xspace AS XSpace, i.yspace AS YSpace, i.xposition AS XPosition, i.yposition AS YPosition, i.maxwidth AS MaxWidth, i.isheightoneline AS IsHeightOneLine, i.maxheight AS MaxHeight, i.isfixedwidth AS IsFixedWidth, i.issetnlposition AS IsSetNLPosition, i.issuppressnull AS IsSuppressNull, i.belowcolumn AS BelowColumn, i.fieldalignmenttype AS FieldAlignmentType, i.linealignmenttype AS LineAlignmentType, i.ad_printcolor_id AS AD_PrintColor_ID, i.ad_printfont_id AS AD_PrintFont_ID, i.isorderby AS IsOrderBy, i.sortno AS SortNo, i.isgroupby AS IsGroupBy, i.ispagebreak AS IsPageBreak, i.isnextpage AS IsNextPage, i.issummarized AS IsSummarized, i.isaveraged AS IsAveraged, i.iscounted AS IsCounted, i.ismincalc AS IsMinCalc, i.ismaxcalc AS IsMaxCalc, i.isvariancecalc AS IsVarianceCalc, i.isdeviationcalc AS IsDeviationCalc, i.isrunningtotal AS IsRunningTotal, i.runningtotallines AS RunningTotalLines, i.ad_printgraph_id AS AD_PrintGraph_ID FROM ad_printformat f
JOIN ad_printformatitem i ON f.ad_printformat_id = i.ad_printformat_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_InOutDetails(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_InOut_ID, IsSOTrx, DocumentNo, DocAction, DocStatus, Posted, Processed, C_DocType_ID, Description, C_Order_ID, DateOrdered, MovementType, MovementDate, DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, SalesRep_ID, M_Warehouse_ID, POReference, DeliveryRule, FreightCostRule, FreightAmt, DeliveryViaRule, M_Shipper_ID, PriorityRule, DatePrinted, NoPackages, PickDate, ShipDate, TrackingNo, AD_OrgTrx_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, DateReceived, IsApproved, IsInDispute, M_InOutLine_ID, Line, LineDescription, C_OrderLine_ID, M_Locator_ID, M_Product_ID, C_UOM_ID, M_AttributeSetInstance_ID, ProductAttribute, M_AttributeSet_ID, M_Lot_ID, GuaranteeDate, Lot, SerNo, MovementQty, QtyEntered, IsDescription, ConfirmedQty, PickedQty, ScrappedQty, TargetQty, LocatorValue, X, Y, Z, M_InOut_C_Charge_ID, ChargeAmt, M_InOut_C_Invoice_ID, CreateConfirm, m_inout_created, M_InOut_CreatedBy, CreateFrom, CreatePackage, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, GenerateTo, m_inout_isactive, IsDropShip, IsInTransit, IsPrinted, M_RMA_ID, ProcessedOn, Processing, Ref_InOut_ID, Reversal_ID, SendEMail, m_inout_updated, M_InOut_UpdatedBy, Volume, Weight, M_InOutLine_AD_Org_ID, M_InOutLine_AD_OrgTrx_ID, M_InOutLine_C_Activity_ID, M_InOutLine_C_Campaign_ID, M_InOutLine_C_Charge_ID, M_InOutLine_C_Project_ID, C_ProjectPhase_ID, C_ProjectTask_ID, IsInvoiced, M_RMALine_ID, m_inoutline_processed, Ref_InOutLine_ID, ReversalLine_ID, M_InOutLine_User1_ID, M_InOutLine_User2_ID, M_Locator_AD_Org_ID, m_locator_isactive, IsDefault, M_Locator_M_Warehouse_ID, PriorityNo, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy) AS SELECT h.ad_client_id AS AD_Client_ID, h.ad_org_id AS AD_Org_ID, l.isactive AS IsActive, l.created AS Created, l.createdby AS CreatedBy,
l.updated AS Updated, l.updatedby AS UpdatedBy, h.m_inout_id AS M_InOut_ID, h.issotrx AS IsSOTrx, h.documentno AS DocumentNo, h.docaction AS DocAction, h.docstatus AS DocStatus, h.posted AS Posted, h.processed AS Processed, h.c_doctype_id AS C_DocType_ID, h.description AS Description, h.c_order_id AS C_Order_ID, h.dateordered AS DateOrdered, h.movementtype AS MovementType, h.movementdate AS MovementDate, h.dateacct AS DateAcct, h.c_bpartner_id AS C_BPartner_ID, h.c_bpartner_location_id AS C_BPartner_Location_ID, h.ad_user_id AS AD_User_ID, h.salesrep_id AS SalesRep_ID, h.m_warehouse_id AS M_Warehouse_ID, h.poreference AS POReference, h.deliveryrule AS DeliveryRule, h.freightcostrule AS FreightCostRule, h.freightamt AS FreightAmt, h.deliveryviarule AS DeliveryViaRule, h.m_shipper_id AS M_Shipper_ID, h.priorityrule AS PriorityRule, h.dateprinted AS DatePrinted, h.nopackages AS NoPackages, h.pickdate AS PickDate, h.shipdate AS ShipDate, h.trackingno AS TrackingNo, h.ad_orgtrx_id AS AD_OrgTrx_ID, h.c_project_id AS C_Project_ID, h.c_campaign_id AS C_Campaign_ID, h.c_activity_id AS C_Activity_ID, h.user1_id AS User1_ID, h.user2_id AS User2_ID, h.datereceived AS DateReceived, h.isapproved AS IsApproved, h.isindispute AS IsInDispute, l.m_inoutline_id AS M_InOutLine_ID, l.line AS Line, l.description AS LineDescription, l.c_orderline_id AS C_OrderLine_ID, l.m_locator_id AS M_Locator_ID, l.m_product_id AS M_Product_ID, l.c_uom_id AS C_UOM_ID, l.m_attributesetinstance_id AS M_AttributeSetInstance_ID, productattribute(l.m_attributesetinstance_id) AS ProductAttribute, pasi.m_attributeset_id AS M_AttributeSet_ID, pasi.m_lot_id AS M_Lot_ID, pasi.guaranteedate AS GuaranteeDate, pasi.lot AS Lot, pasi.serno AS SerNo, l.movementqty AS MovementQty, l.qtyentered AS QtyEntered, l.isdescription AS IsDescription, l.confirmedqty AS ConfirmedQty, l.pickedqty AS PickedQty, l.scrappedqty AS ScrappedQty, l.targetqty AS TargetQty, loc.value AS LocatorValue, loc.x AS X, loc.y AS Y, loc.z AS Z,
h.c_charge_id AS M_InOut_C_Charge_ID, h.chargeamt AS ChargeAmt, h.c_invoice_id AS M_InOut_C_Invoice_ID, h.createconfirm AS CreateConfirm, h.created AS m_inout_created, h.createdby AS M_InOut_CreatedBy, h.createfrom AS CreateFrom, h.createpackage AS CreatePackage, h.dropship_bpartner_id AS DropShip_BPartner_ID, h.dropship_location_id AS DropShip_Location_ID, h.dropship_user_id AS DropShip_User_ID, h.generateto AS GenerateTo, h.isactive AS m_inout_isactive, h.isdropship AS IsDropShip, h.isintransit AS IsInTransit, h.isprinted AS IsPrinted, h.m_rma_id AS M_RMA_ID, h.processedon AS ProcessedOn, h.processing AS Processing, h.ref_inout_id AS Ref_InOut_ID, h.reversal_id AS Reversal_ID, h.sendemail AS SendEMail, h.updated AS m_inout_updated, h.updatedby AS M_InOut_UpdatedBy, h.volume AS Volume, h.weight AS Weight, l.ad_org_id AS M_InOutLine_AD_Org_ID, l.ad_orgtrx_id AS M_InOutLine_AD_OrgTrx_ID, l.c_activity_id AS M_InOutLine_C_Activity_ID, l.c_campaign_id AS M_InOutLine_C_Campaign_ID, l.c_charge_id AS M_InOutLine_C_Charge_ID, l.c_project_id AS M_InOutLine_C_Project_ID, l.c_projectphase_id AS C_ProjectPhase_ID, l.c_projecttask_id AS C_ProjectTask_ID, l.isinvoiced AS IsInvoiced, l.m_rmaline_id AS M_RMALine_ID, l.processed AS m_inoutline_processed, l.ref_inoutline_id AS Ref_InOutLine_ID, l.reversalline_id AS ReversalLine_ID, l.user1_id AS M_InOutLine_User1_ID, l.user2_id AS M_InOutLine_User2_ID, loc.ad_org_id AS M_Locator_AD_Org_ID, loc.isactive AS m_locator_isactive, loc.isdefault AS IsDefault, loc.m_warehouse_id AS M_Locator_M_Warehouse_ID, loc.priorityno AS PriorityNo, pasi.ad_org_id AS M_ASI_AD_Org_ID, pasi.created AS m_asi_created, pasi.createdby AS M_ASI_CreatedBy, pasi.description AS M_ASI_Description, pasi.isactive AS m_asi_isactive, pasi.updated AS m_asi_updated, pasi.updatedby AS M_ASI_UpdatedBy FROM m_inout h
JOIN m_inoutline l ON h.m_inout_id = l.m_inout_id
LEFT JOIN m_locator loc ON l.m_locator_id = loc.m_locator_id
LEFT JOIN m_attributesetinstance pasi ON l.m_attributesetinstance_id = pasi.m_attributesetinstance_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW C_Order_Header_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Order_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, SalesRep_ID, SalesRep_Name, DateOrdered, DatePromised, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, Bill_BPValue, Bill_BPTaxID, Bill_Name, Bill_Name2, Bill_Title, Bill_Phone, Bill_ContactName, Bill_C_Location_ID, Description, POReference, C_Currency_ID, PaymentTerm, PaymentTermNote, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsTaxIncluded, Volume, Weight, C_Campaign_ID, C_Project_ID, C_Activity_ID, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, InvoiceRule, Logo_ID, AD_OrgTrx_ID, AD_User_ID, AmountRefunded, AmountTendered, C_BPartner_Location_ID, C_CashLine_ID, C_CashPlanLine_ID, C_ConversionType_ID, C_DocTypeTarget_ID, CopyFrom, C_Payment_ID, C_PaymentTerm_ID, C_POS_ID, DateAcct, DatePrinted, DocAction, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, FreightAmt, FreightCostRule, IsApproved, IsCreditApproved, IsDelivered, IsDiscountPrinted, IsDropShip, IsInvoiced, IsPayScheduleValid, IsPrinted, IsSelected, IsSelfService, IsTransferred, Link_Order_ID, M_FreightCategory_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, PaymentRule, Posted, Processed, ProcessedOn, PromotionCode, Ref_Order_ID, SendEMail, User1_ID, User2_ID, M_Warehouse_AD_Org_ID, m_warehouse_description, m_warehouse_isactive, IsDisallowNegativeInv, IsInTransit, M_WarehouseSource_ID, m_warehouse_name, ReplenishmentClass, Separator, m_warehouse_value, C_PaymentTerm_AD_Org_ID, AfterDelivery, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset,
GraceDays, c_paymentterm_isactive, IsDefault, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, m_paymentterm_value, bp_acqusitioncost, bp_actuallifetimevalue, bp_ad_language, BP_AD_OrgBP_ID, BP_AD_Org_ID, BP_BPartner_Parent_ID, BP_C_BP_Group_ID, BP_C_Dunning_ID, BP_C_Greeting_ID, BP_C_InvoiceSchedule_ID, BP_C_PaymentTerm_ID, bp_created, BP_CreatedBy, BP_C_TaxGroup_ID, bp_deliveryrule, bp_deliveryviarule, bp_description, bp_dunninggrace, bp_firstsale, bp_flatdiscount, bp_freightcostrule, bp_invoicerule, bp_isactive, bp_iscustomer, bp_isdiscountprinted, bp_isemployee, bp_ismanufacturer, bp_isonetime, bp_ispotaxexempt, bp_isprospect, bp_issalesrep, bp_issummary, bp_istaxexempt, bp_isvendor, BP_Logo_ID, BP_M_DiscountSchema_ID, BP_M_PriceList_ID, bp_numberemployees, bp_paymentrule, bp_paymentrulepo, BP_PO_DiscountSchema_ID, BP_PO_PaymentTerm_ID, BP_PO_PriceList_ID, bp_poreference, bp_potentiallifetimevalue, bp_rating, BP_SalesRep_ID, bp_salesvolume, bp_sendemail, bp_shareofcustomer, bp_shelflifeminpct, bp_so_creditlimit, bp_socreditstatus, bp_so_creditused, bp_so_description, bp_totalopenbalance, bp_updated, BP_UpdatedBy, bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, BP_Location_AD_Org_ID, BP_Location_C_BPartner_ID, bp_location_created, BP_Location_CreatedBy, BP_Location_C_SalesRegion_ID, bp_location_fax, bp_location_isactive, bp_location_isbillto, bp_location_isdn, bp_location_ispayfrom, bp_location_isremitto, bp_location_isshipto, bp_location_name, bp_location_phone, bp_location_phone2, bp_location_updated, BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments,
ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bp_acqusitioncost, salesrep_bp_actuallifetimeval, salesrep_bp_ad_language, SalesRep_BP_AD_OrgBP_ID, SalesRep_BP_AD_Org_ID, SalesRep_BP_BPartner_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Dunning_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_C_InvoiceSched_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bp_created, SalesRep_BP_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bp_deliveryrule, salesrep_bp_deliveryviarule, salesrep_bp_description, salesrep_bp_dunninggrace, salesrep_bp_duns, salesrep_bp_firstsale, salesrep_bp_flatdiscount, salesrep_bp_freightcostrule, salesrep_bp_invoicerule, salesrep_bp_isactive, salesrep_bp_iscustomer, salesrep_bp_isdiscountprinted, salesrep_bp_isemployee, salesrep_bp_ismanufacturer, salesrep_bp_isonetime, salesrep_bp_ispotaxexempt, salesrep_bp_isprospect, salesrep_bp_issalesrep, salesrep_bp_issummary, salesrep_bp_istaxexempt,
salesrep_bp_isvendor, SalesRep_BP_Logo_ID, SalesRep_BP_M_DiscountSchm_ID, SalesRep_BP_M_PriceList_ID, salesrep_bp_naics, salesrep_bp_name2, salesrep_bp_numberemployees, salesrep_bp_paymentrule, salesrep_bp_paymentrulepo, SalesRep_BP_PO_DiscountSchm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bp_poreference, salesrep_bp_potentiallifetime, salesrep_bp_rating, salesrep_bp_referenceno, SalesRep_BP_SalesRep_ID, salesrep_bp_salesvolume, salesrep_bp_sendemail, salesrep_bp_shareofcustomer, salesrep_bp_shelflifeminpct, salesrep_bp_so_creditlimit, salesrep_bp_socreditstatus, salesrep_bp_so_creditused, salesrep_bp_so_description, salesrep_bp_taxid, salesrep_bp_totalopenbalance, salesrep_bp_updated, SalesRep_BP_UpdatedBy, salesrep_bp_url, salesrep_bp_value, bill_bp_acqusitioncost, bill_bp_actuallifetimevalue, bill_bp_ad_language, Bill_BP_AD_OrgBP_ID, Bill_BP_AD_Org_ID, Bill_BP_BPartner_Parent_ID, Bill_BP_C_BP_Group_ID, Bill_BP_C_Dunning_ID, Bill_BP_C_Greeting_ID, Bill_BP_C_InvoiceSchedule_ID, Bill_BP_C_PaymentTerm_ID, bill_bp_created, Bill_BP_CreatedBy, Bill_BP_C_TaxGroup_ID, bill_bp_deliveryrule, bill_bp_deliveryviarule, bill_bp_description, bill_bp_dunninggrace, bill_bp_duns, bill_bp_firstsale, bill_bp_flatdiscount, bill_bp_freightcostrule, bill_bp_invoicerule, bill_bp_isactive, bill_bp_iscustomer, bill_bp_isdiscountprinted, bill_bp_isemployee, bill_bp_ismanufacturer, bill_bp_isonetime, bill_bp_ispotaxexempt, bill_bp_isprospect, bill_bp_issalesrep, bill_bp_issummary, bill_bp_istaxexempt, bill_bp_isvendor, Bill_BP_Logo_ID, Bill_BP_M_DiscountSchema_ID, Bill_BP_M_PriceList_ID, bill_bp_naics, bill_bp_numberemployees, bill_bp_paymentrule, bill_bp_paymentrulepo, Bill_BP_PO_DiscountSchema_ID, Bill_BP_PO_PaymentTerm_ID, Bill_BP_PO_PriceList_ID, bill_bp_poreference, bill_bp_potentiallifetimevalue, bill_bp_rating, bill_bp_referenceno, Bill_BP_SalesRep_ID, bill_bp_salesvolume, bill_bp_sendemail, bill_bp_shareofcustomer, bill_bp_shelflifeminpct,
bill_bp_so_creditlimit, bill_bp_socreditstatus, bill_bp_so_creditused, bill_bp_so_description, bill_bp_totalopenbalance, bill_bp_updated, Bill_BP_UpdatedBy, bill_bp_url, Bill_BP_Location_AD_Org_ID, Bill_BP_Location_C_BPartner_ID, bill_bp_location_created, Bill_BP_Location_CreatedBy, Bill_BP_Location_C_SalesReg_ID, bill_bp_location_fax, bill_bp_location_isactive, bill_bp_location_isbillto, bill_bp_location_isdn, bill_bp_location_ispayfrom, bill_bp_location_isremitto, bill_bp_location_isshipto, bill_bp_location_name, bill_bp_location_phone, bill_bp_location_phone2, bill_bp_location_updated, Bill_BP_Location_UpdatedBy, Bill_User_AD_Org_ID, Bill_User_AD_OrgTrx_ID, bill_user_ad_birthday, Bill_User_C_BPartner_ID, Bill_User_C_BP_Location_ID, Bill_User_C_Greeting_ID, bill_user_comments, bill_user_created, Bill_User_CreatedBy, bill_user_description, bill_user_email, bill_user_fax, bill_user_isactive, bill_user_lastcontact, bill_user_lastresult, bill_user_phone2, Bill_User_Supervisor_ID, bill_user_updated, Bill_User_UpdatedBy, bill_user_value, CurSymbol, cur_description) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, o.c_order_id AS C_Order_ID, o.issotrx AS IsSOTrx, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, o.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2,
bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, o.bill_bpartner_id AS Bill_BPartner_ID, o.bill_location_id AS Bill_Location_ID, o.bill_user_id AS Bill_User_ID, bbp.value AS Bill_BPValue, bbp.taxid AS Bill_BPTaxID, bbp.name AS Bill_Name, bbp.name2 AS Bill_Name2, bbpc.title AS Bill_Title, bbpc.phone AS Bill_Phone, NULLIF(bbpc.name, bbp.name) AS Bill_ContactName, bbpl.c_location_id AS Bill_C_Location_ID, o.description AS Description, o.poreference AS POReference, o.c_currency_id AS C_Currency_ID, pt.name AS PaymentTerm, pt.documentnote AS PaymentTermNote, o.c_charge_id AS C_Charge_ID, o.chargeamt AS ChargeAmt, o.totallines AS TotalLines, o.grandtotal AS GrandTotal, o.grandtotal AS AmtInWords, o.m_pricelist_id AS M_PriceList_ID, o.istaxincluded AS IsTaxIncluded, o.volume AS Volume, o.weight AS Weight, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.m_shipper_id AS M_Shipper_ID, o.deliveryrule AS DeliveryRule, o.deliveryviarule AS DeliveryViaRule, o.priorityrule AS PriorityRule, o.invoicerule AS InvoiceRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, o.ad_orgtrx_id AS AD_OrgTrx_ID, o.ad_user_id AS AD_User_ID, o.amountrefunded AS AmountRefunded, o.amounttendered AS AmountTendered, o.c_bpartner_location_id AS C_BPartner_Location_ID, o.c_cashline_id AS C_CashLine_ID, o.c_cashplanline_id AS C_CashPlanLine_ID, o.c_conversiontype_id AS C_ConversionType_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.copyfrom AS CopyFrom, o.c_payment_id AS C_Payment_ID, o.c_paymentterm_id AS C_PaymentTerm_ID, o.c_pos_id AS C_POS_ID, o.dateacct AS DateAcct, o.dateprinted AS DatePrinted, o.docaction AS DocAction, o.dropship_bpartner_id AS DropShip_BPartner_ID, o.dropship_location_id AS DropShip_Location_ID, o.dropship_user_id AS DropShip_User_ID,
o.freightamt AS FreightAmt, o.freightcostrule AS FreightCostRule, o.isapproved AS IsApproved, o.iscreditapproved AS IsCreditApproved, o.isdelivered AS IsDelivered, o.isdiscountprinted AS IsDiscountPrinted, o.isdropship AS IsDropShip, o.isinvoiced AS IsInvoiced, o.ispayschedulevalid AS IsPayScheduleValid, o.isprinted AS IsPrinted, o.isselected AS IsSelected, o.isselfservice AS IsSelfService, o.istransferred AS IsTransferred, o.link_order_id AS Link_Order_ID, o.m_freightcategory_id AS M_FreightCategory_ID, o.ordertype AS OrderType, o.pay_bpartner_id AS Pay_BPartner_ID, o.pay_location_id AS Pay_Location_ID, o.paymentrule AS PaymentRule, o.posted AS Posted, o.processed AS Processed, o.processedon AS ProcessedOn, o.promotioncode AS PromotionCode, o.ref_order_id AS Ref_Order_ID, o.sendemail AS SendEMail, o.user1_id AS User1_ID, o.user2_id AS User2_ID, wh.ad_org_id AS M_Warehouse_AD_Org_ID, wh.description AS m_warehouse_description, wh.isactive AS m_warehouse_isactive, wh.isdisallownegativeinv AS IsDisallowNegativeInv, wh.isintransit AS IsInTransit, wh.m_warehousesource_id AS M_WarehouseSource_ID, wh.name AS m_warehouse_name, wh.replenishmentclass AS ReplenishmentClass, wh.separator AS Separator, wh.value AS m_warehouse_value, pt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, pt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS m_paymentterm_value, bp.acqusitioncost AS bp_acqusitioncost, bp.actuallifetimevalue AS bp_actuallifetimevalue,
bp.ad_language AS bp_ad_language, bp.ad_orgbp_id AS BP_AD_OrgBP_ID, bp.ad_org_id AS BP_AD_Org_ID, bp.bpartner_parent_id AS BP_BPartner_Parent_ID, bp.c_bp_group_id AS BP_C_BP_Group_ID, bp.c_dunning_id AS BP_C_Dunning_ID, bp.c_greeting_id AS BP_C_Greeting_ID, bp.c_invoiceschedule_id AS BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS BP_C_PaymentTerm_ID, bp.created AS bp_created, bp.createdby AS BP_CreatedBy, bp.c_taxgroup_id AS BP_C_TaxGroup_ID, bp.deliveryrule AS bp_deliveryrule, bp.deliveryviarule AS bp_deliveryviarule, bp.description AS bp_description, bp.dunninggrace AS bp_dunninggrace, bp.firstsale AS bp_firstsale, bp.flatdiscount AS bp_flatdiscount, bp.freightcostrule AS bp_freightcostrule, bp.invoicerule AS bp_invoicerule, bp.isactive AS bp_isactive, bp.iscustomer AS bp_iscustomer, bp.isdiscountprinted AS bp_isdiscountprinted, bp.isemployee AS bp_isemployee, bp.ismanufacturer AS bp_ismanufacturer, bp.isonetime AS bp_isonetime, bp.ispotaxexempt AS bp_ispotaxexempt, bp.isprospect AS bp_isprospect, bp.issalesrep AS bp_issalesrep, bp.issummary AS bp_issummary, bp.istaxexempt AS bp_istaxexempt, bp.isvendor AS bp_isvendor, bp.logo_id AS BP_Logo_ID, bp.m_discountschema_id AS BP_M_DiscountSchema_ID, bp.m_pricelist_id AS BP_M_PriceList_ID, bp.numberemployees AS bp_numberemployees, bp.paymentrule AS bp_paymentrule, bp.paymentrulepo AS bp_paymentrulepo, bp.po_discountschema_id AS BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS BP_PO_PriceList_ID, bp.poreference AS bp_poreference, bp.potentiallifetimevalue AS bp_potentiallifetimevalue, bp.rating AS bp_rating, bp.salesrep_id AS BP_SalesRep_ID, bp.salesvolume AS bp_salesvolume, bp.sendemail AS bp_sendemail, bp.shareofcustomer AS bp_shareofcustomer, bp.shelflifeminpct AS bp_shelflifeminpct, bp.so_creditlimit AS bp_so_creditlimit, bp.socreditstatus AS bp_socreditstatus, bp.so_creditused AS bp_so_creditused, bp.so_description AS bp_so_description,
bp.totalopenbalance AS bp_totalopenbalance, bp.updated AS bp_updated, bp.updatedby AS BP_UpdatedBy, bp.url AS bp_url, bpg.ad_org_id AS C_Greeting_AD_Org_ID, bpg.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpg.name AS c_greeting_name, bpl.ad_org_id AS BP_Location_AD_Org_ID, bpl.c_bpartner_id AS BP_Location_C_BPartner_ID, bpl.created AS bp_location_created, bpl.createdby AS BP_Location_CreatedBy, bpl.c_salesregion_id AS BP_Location_C_SalesRegion_ID, bpl.fax AS bp_location_fax, bpl.isactive AS bp_location_isactive, bpl.isbillto AS bp_location_isbillto, bpl.isdn AS bp_location_isdn, bpl.ispayfrom AS bp_location_ispayfrom, bpl.isremitto AS bp_location_isremitto, bpl.isshipto AS bp_location_isshipto, bpl.name AS bp_location_name, bpl.phone AS bp_location_phone, bpl.phone2 AS bp_location_phone2, bpl.updated AS bp_location_updated, bpl.updatedby AS BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID,
bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcg.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcg.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcg.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bp_acqusitioncost, ubp.actuallifetimevalue AS salesrep_bp_actuallifetimeval, ubp.ad_language AS salesrep_bp_ad_language, ubp.ad_orgbp_id AS SalesRep_BP_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BP_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BP_BPartner_Parent_ID,
ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID, ubp.c_dunning_id AS SalesRep_BP_C_Dunning_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_C_InvoiceSched_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bp_created, ubp.createdby AS SalesRep_BP_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bp_deliveryrule, ubp.deliveryviarule AS salesrep_bp_deliveryviarule, ubp.description AS salesrep_bp_description, ubp.dunninggrace AS salesrep_bp_dunninggrace, ubp.duns AS salesrep_bp_duns, ubp.firstsale AS salesrep_bp_firstsale, ubp.flatdiscount AS salesrep_bp_flatdiscount, ubp.freightcostrule AS salesrep_bp_freightcostrule, ubp.invoicerule AS salesrep_bp_invoicerule, ubp.isactive AS salesrep_bp_isactive, ubp.iscustomer AS salesrep_bp_iscustomer, ubp.isdiscountprinted AS salesrep_bp_isdiscountprinted, ubp.isemployee AS salesrep_bp_isemployee, ubp.ismanufacturer AS salesrep_bp_ismanufacturer, ubp.isonetime AS salesrep_bp_isonetime, ubp.ispotaxexempt AS salesrep_bp_ispotaxexempt, ubp.isprospect AS salesrep_bp_isprospect, ubp.issalesrep AS salesrep_bp_issalesrep, ubp.issummary AS salesrep_bp_issummary, ubp.istaxexempt AS salesrep_bp_istaxexempt, ubp.isvendor AS salesrep_bp_isvendor, ubp.logo_id AS SalesRep_BP_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_M_DiscountSchm_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bp_naics, ubp.name2 AS salesrep_bp_name2, ubp.numberemployees AS salesrep_bp_numberemployees, ubp.paymentrule AS salesrep_bp_paymentrule, ubp.paymentrulepo AS salesrep_bp_paymentrulepo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountSchm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bp_poreference, ubp.potentiallifetimevalue AS salesrep_bp_potentiallifetime, ubp.rating AS salesrep_bp_rating,
ubp.referenceno AS salesrep_bp_referenceno, ubp.salesrep_id AS SalesRep_BP_SalesRep_ID, ubp.salesvolume AS salesrep_bp_salesvolume, ubp.sendemail AS salesrep_bp_sendemail, ubp.shareofcustomer AS salesrep_bp_shareofcustomer, ubp.shelflifeminpct AS salesrep_bp_shelflifeminpct, ubp.so_creditlimit AS salesrep_bp_so_creditlimit, ubp.socreditstatus AS salesrep_bp_socreditstatus, ubp.so_creditused AS salesrep_bp_so_creditused, ubp.so_description AS salesrep_bp_so_description, ubp.taxid AS salesrep_bp_taxid, ubp.totalopenbalance AS salesrep_bp_totalopenbalance, ubp.updated AS salesrep_bp_updated, ubp.updatedby AS SalesRep_BP_UpdatedBy, ubp.url AS salesrep_bp_url, ubp.value AS salesrep_bp_value, bbp.acqusitioncost AS bill_bp_acqusitioncost, bbp.actuallifetimevalue AS bill_bp_actuallifetimevalue, bbp.ad_language AS bill_bp_ad_language, bbp.ad_orgbp_id AS Bill_BP_AD_OrgBP_ID, bbp.ad_org_id AS Bill_BP_AD_Org_ID, bbp.bpartner_parent_id AS Bill_BP_BPartner_Parent_ID, bbp.c_bp_group_id AS Bill_BP_C_BP_Group_ID, bbp.c_dunning_id AS Bill_BP_C_Dunning_ID, bbp.c_greeting_id AS Bill_BP_C_Greeting_ID, bbp.c_invoiceschedule_id AS Bill_BP_C_InvoiceSchedule_ID, bbp.c_paymentterm_id AS Bill_BP_C_PaymentTerm_ID, bbp.created AS bill_bp_created, bbp.createdby AS Bill_BP_CreatedBy, bbp.c_taxgroup_id AS Bill_BP_C_TaxGroup_ID, bbp.deliveryrule AS bill_bp_deliveryrule, bbp.deliveryviarule AS bill_bp_deliveryviarule, bbp.description AS bill_bp_description, bbp.dunninggrace AS bill_bp_dunninggrace, bbp.duns AS bill_bp_duns, bbp.firstsale AS bill_bp_firstsale, bbp.flatdiscount AS bill_bp_flatdiscount, bbp.freightcostrule AS bill_bp_freightcostrule, bbp.invoicerule AS bill_bp_invoicerule, bbp.isactive AS bill_bp_isactive, bbp.iscustomer AS bill_bp_iscustomer, bbp.isdiscountprinted AS bill_bp_isdiscountprinted, bbp.isemployee AS bill_bp_isemployee, bbp.ismanufacturer AS bill_bp_ismanufacturer, bbp.isonetime AS bill_bp_isonetime, bbp.ispotaxexempt AS bill_bp_ispotaxexempt,
bbp.isprospect AS bill_bp_isprospect, bbp.issalesrep AS bill_bp_issalesrep, bbp.issummary AS bill_bp_issummary, bbp.istaxexempt AS bill_bp_istaxexempt, bbp.isvendor AS bill_bp_isvendor, bbp.logo_id AS Bill_BP_Logo_ID, bbp.m_discountschema_id AS Bill_BP_M_DiscountSchema_ID, bbp.m_pricelist_id AS Bill_BP_M_PriceList_ID, bbp.naics AS bill_bp_naics, bbp.numberemployees AS bill_bp_numberemployees, bbp.paymentrule AS bill_bp_paymentrule, bbp.paymentrulepo AS bill_bp_paymentrulepo, bbp.po_discountschema_id AS Bill_BP_PO_DiscountSchema_ID, bbp.po_paymentterm_id AS Bill_BP_PO_PaymentTerm_ID, bbp.po_pricelist_id AS Bill_BP_PO_PriceList_ID, bbp.poreference AS bill_bp_poreference, bbp.potentiallifetimevalue AS bill_bp_potentiallifetimevalue, bbp.rating AS bill_bp_rating, bbp.referenceno AS bill_bp_referenceno, bbp.salesrep_id AS Bill_BP_SalesRep_ID, bbp.salesvolume AS bill_bp_salesvolume, bbp.sendemail AS bill_bp_sendemail, bbp.shareofcustomer AS bill_bp_shareofcustomer, bbp.shelflifeminpct AS bill_bp_shelflifeminpct, bbp.so_creditlimit AS bill_bp_so_creditlimit, bbp.socreditstatus AS bill_bp_socreditstatus, bbp.so_creditused AS bill_bp_so_creditused, bbp.so_description AS bill_bp_so_description, bbp.totalopenbalance AS bill_bp_totalopenbalance, bbp.updated AS bill_bp_updated, bbp.updatedby AS Bill_BP_UpdatedBy, bbp.url AS bill_bp_url, bbpl.ad_org_id AS Bill_BP_Location_AD_Org_ID, bbpl.c_bpartner_id AS Bill_BP_Location_C_BPartner_ID, bbpl.created AS bill_bp_location_created, bbpl.createdby AS Bill_BP_Location_CreatedBy, bbpl.c_salesregion_id AS Bill_BP_Location_C_SalesReg_ID, bbpl.fax AS bill_bp_location_fax, bbpl.isactive AS bill_bp_location_isactive, bbpl.isbillto AS bill_bp_location_isbillto, bbpl.isdn AS bill_bp_location_isdn, bbpl.ispayfrom AS bill_bp_location_ispayfrom, bbpl.isremitto AS bill_bp_location_isremitto, bbpl.isshipto AS bill_bp_location_isshipto, bbpl.name AS bill_bp_location_name, bbpl.phone AS bill_bp_location_phone, bbpl.phone2 AS bill_bp_location_phone2,
bbpl.updated AS bill_bp_location_updated, bbpl.updatedby AS Bill_BP_Location_UpdatedBy, bbpc.ad_org_id AS Bill_User_AD_Org_ID, bbpc.ad_orgtrx_id AS Bill_User_AD_OrgTrx_ID, bbpc.birthday AS bill_user_ad_birthday, bbpc.c_bpartner_id AS Bill_User_C_BPartner_ID, bbpc.c_bpartner_location_id AS Bill_User_C_BP_Location_ID, bbpc.c_greeting_id AS Bill_User_C_Greeting_ID, bbpc.comments AS bill_user_comments, bbpc.created AS bill_user_created, bbpc.createdby AS Bill_User_CreatedBy, bbpc.description AS bill_user_description, bbpc.email AS bill_user_email, bbpc.fax AS bill_user_fax, bbpc.isactive AS bill_user_isactive, bbpc.lastcontact AS bill_user_lastcontact, bbpc.lastresult AS bill_user_lastresult, bbpc.phone2 AS bill_user_phone2, bbpc.supervisor_id AS Bill_User_Supervisor_ID, bbpc.updated AS bill_user_updated, bbpc.updatedby AS Bill_User_UpdatedBy, bbpc.value AS bill_user_value, cur.cursymbol AS CurSymbol, cur.description AS cur_description FROM c_order o
JOIN c_doctype dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN c_paymentterm pt ON o.c_paymentterm_id = pt.c_paymentterm_id
JOIN c_bpartner bp ON o.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
JOIN c_bpartner_location bpl ON o.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON o.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
JOIN c_bpartner bbp ON o.bill_bpartner_id = bbp.c_bpartner_id
JOIN c_bpartner_location bbpl ON o.bill_location_id = bbpl.c_bpartner_location_id
LEFT JOIN ad_user bbpc ON o.bill_user_id = bbpc.ad_user_id
LEFT JOIN c_currency cur ON o.c_currency_id = cur.c_currency_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW M_InOut_LineConfirm_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOutLineConfirm_ID, M_InOutConfirm_ID, TargetQty, ConfirmedQty, DifferenceQty, ScrappedQty, Description, Processed, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, MovementQty, UOMSymbol, QtyBackOrdered, Name, ShipDescription, DocumentNote, UPC, SKU, ProductValue, M_Locator_ID, M_Warehouse_ID, X, Y, Z, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate) AS SELECT iolc.ad_client_id AS AD_Client_ID, iolc.ad_org_id AS AD_Org_ID, iolc.isactive AS IsActive, iolc.created AS Created, iolc.createdby AS CreatedBy, iolc.updated AS Updated, iolc.updatedby AS UpdatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, iolc.m_inoutlineconfirm_id AS M_InOutLineConfirm_ID, iolc.m_inoutconfirm_id AS M_InOutConfirm_ID, iolc.targetqty AS TargetQty, iolc.confirmedqty AS ConfirmedQty, iolc.differenceqty AS DifferenceQty, iolc.scrappedqty AS ScrappedQty, iolc.description AS Description, iolc.processed AS Processed, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line AS Line, p.m_product_id AS M_Product_ID, iol.movementqty AS MovementQty, uom.uomsymbol AS UOMSymbol, ol.qtyordered - ol.qtydelivered AS QtyBackOrdered, COALESCE(p.name, iol.description) AS Name, CASE WHEN p.name IS NOT NULL THEN iol.description ELSE NULL END AS ShipDescription, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate FROM m_inoutlineconfirm iolc
JOIN m_inoutline iol ON iolc.m_inoutline_id = iol.m_inoutline_id
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id
LEFT JOIN m_product p ON iol.m_product_id = p.m_product_id
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
LEFT JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_C_InvoiceTax(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Tax_ID, C_Invoice_ID, C_DocType_ID, C_BPartner_ID, TaxID, IsTaxExempt, DateAcct, DateInvoiced, IsSOTrx, DocumentNo, IsPaid, C_Currency_ID, TaxBaseAmt, TaxAmt, TaxLineTotal, Multiplier, C_InvoiceTax_AD_Org_ID, c_invoicetax_isactive, c_invoicetax_istaxincluded, c_invoicetax_processed, C_Invoice_AD_OrgTrx_ID, AD_User_ID, C_BPartner_Location_ID, C_Activity_ID, C_Campaign_ID, C_Charge_ID, C_ConversionType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, ChargeAmt, C_Order_ID, C_Payment_ID, C_Invoice_C_PaymentTerm_ID, c_invoice_created, C_Invoice_CreatedBy, DatePrinted, c_invoice_description, DocAction, DocStatus, DunningGrace, GenerateTo, InvoiceCollectionType, IsApproved, IsDiscountPrinted, IsInDispute, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, c_invoice_istaxincluded, IsTransferred, M_PriceList_ID, M_RMA_ID, PaymentRule, c_invoice_poreference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SalesRep_ID, SendEMail, TotalLines, c_invoice_updated, C_Invoice_UpdatedBy, User1_ID, User2_ID, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, DUNS, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, IsVendor, C_BP_Logo_ID, M_DiscountSchema_ID, C_BP_M_PriceList_ID, NAICS, c_bp_name, c_bp_name2, NumberEmployees, c_bp_paymentrule, c_bp_paymentrulepo, PO_DiscountSchema_ID, PO_PaymentTerm_ID, PO_PriceList_ID, c_bp_poreference,
PotentialLifeTimeValue, c_bp_rating, C_BP_SalesRep_ID, SalesVolume, c_bp_sendemail, ShareOfCustomer, ShelfLifeMinPct, SO_CreditLimit, SOCreditStatus, SO_CreditUsed, SO_Description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, c_bp_value) AS SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, t.created AS Created, t.createdby AS CreatedBy, t.updated AS Updated, t.updatedby AS UpdatedBy, t.c_tax_id AS C_Tax_ID, i.c_invoice_id AS C_Invoice_ID, i.c_doctype_id AS C_DocType_ID, i.c_bpartner_id AS C_BPartner_ID, bp.taxid AS TaxID, bp.istaxexempt AS IsTaxExempt, i.dateacct AS DateAcct, i.dateinvoiced AS DateInvoiced, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.ispaid AS IsPaid, i.c_currency_id AS C_Currency_ID, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN t.taxbaseamt * '-1' ELSE t.taxbaseamt END AS TaxBaseAmt, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN t.taxamt * '-1' ELSE t.taxamt END AS TaxAmt, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN (t.taxbaseamt + t.taxamt) * '-1' ELSE t.taxbaseamt + t.taxamt END AS TaxLineTotal, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN -1 ELSE 1 END AS Multiplier, t.ad_org_id AS C_InvoiceTax_AD_Org_ID, t.isactive AS c_invoicetax_isactive, t.istaxincluded AS c_invoicetax_istaxincluded, t.processed AS c_invoicetax_processed, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_activity_id AS C_Activity_ID, i.c_campaign_id AS C_Campaign_ID, i.c_charge_id AS C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_order_id AS C_Order_ID, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_Invoice_C_PaymentTerm_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateprinted AS DatePrinted, i.description AS c_invoice_description, i.docaction AS DocAction,
i.docstatus AS DocStatus, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.istaxincluded AS c_invoice_istaxincluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS c_invoice_poreference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS User1_ID, i.user2_id AS User2_ID, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.duns AS DUNS, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer,
bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.isvendor AS IsVendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.naics AS NAICS, bp.name AS c_bp_name, bp.name2 AS c_bp_name2, bp.numberemployees AS NumberEmployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS PO_DiscountSchema_ID, bp.po_paymentterm_id AS PO_PaymentTerm_ID, bp.po_pricelist_id AS PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS PotentialLifeTimeValue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS SalesVolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS ShareOfCustomer, bp.shelflifeminpct AS ShelfLifeMinPct, bp.so_creditlimit AS SO_CreditLimit, bp.socreditstatus AS SOCreditStatus, bp.so_creditused AS SO_CreditUsed, bp.so_description AS SO_Description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, bp.value AS c_bp_value FROM c_invoicetax t
JOIN c_invoice i ON t.c_invoice_id = i.c_invoice_id
JOIN c_doctype d ON i.c_doctype_id = d.c_doctype_id
JOIN c_bpartner bp ON i.c_bpartner_id = bp.c_bpartner_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_Fact_Acct_Period(AD_Client_ID, AD_Org_ID, C_AcctSchema_ID, Account_ID, C_Period_ID, GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType, C_Currency_ID, AmtSourceDr, AmtSourceCr, AmtSource, AmtAcctDr, AmtAcctCr, AmtAcct, Rate, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, A_Asset_ID) AS SELECT fact_acct.ad_client_id AS AD_Client_ID, fact_acct.ad_org_id AS AD_Org_ID, fact_acct.c_acctschema_id AS C_AcctSchema_ID, fact_acct.account_id AS Account_ID, fact_acct.c_period_id AS C_Period_ID, fact_acct.gl_category_id AS GL_Category_ID, fact_acct.gl_budget_id AS GL_Budget_ID, fact_acct.c_tax_id AS C_Tax_ID, fact_acct.m_locator_id AS M_Locator_ID, fact_acct.postingtype AS PostingType, fact_acct.c_currency_id AS C_Currency_ID, sum(fact_acct.amtsourcedr) AS AmtSourceDr, sum(fact_acct.amtsourcecr) AS AmtSourceCr, sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) AS AmtSource, sum(fact_acct.amtacctdr) AS AmtAcctDr, sum(fact_acct.amtacctcr) AS AmtAcctCr, sum(fact_acct.amtacctdr - fact_acct.amtacctcr) AS AmtAcct, CASE WHEN sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) = 0 THEN 0 ELSE sum(fact_acct.amtacctdr - fact_acct.amtacctcr) / sum(fact_acct.amtsourcedr - fact_acct.amtsourcecr) END AS Rate, fact_acct.m_product_id AS M_Product_ID, fact_acct.c_bpartner_id AS C_BPartner_ID, fact_acct.ad_orgtrx_id AS AD_OrgTrx_ID, fact_acct.c_locfrom_id AS C_LocFrom_ID, fact_acct.c_locto_id AS C_LocTo_ID, fact_acct.c_salesregion_id AS C_SalesRegion_ID, fact_acct.c_project_id AS C_Project_ID, fact_acct.c_campaign_id AS C_Campaign_ID, fact_acct.c_activity_id AS C_Activity_ID, fact_acct.user1_id AS User1_ID, fact_acct.user2_id AS User2_ID, fact_acct.a_asset_id AS A_Asset_ID FROM fact_acct GROUP BY fact_acct.ad_client_id, fact_acct.ad_org_id, fact_acct.c_acctschema_id, fact_acct.account_id, fact_acct.c_period_id, fact_acct.gl_category_id,
fact_acct.gl_budget_id, fact_acct.c_tax_id, fact_acct.m_locator_id, fact_acct.postingtype, fact_acct.c_currency_id, fact_acct.m_product_id, fact_acct.c_bpartner_id, fact_acct.ad_orgtrx_id, fact_acct.c_locfrom_id, fact_acct.c_locto_id, fact_acct.c_salesregion_id, fact_acct.c_project_id, fact_acct.c_campaign_id, fact_acct.c_activity_id, fact_acct.user1_id, fact_acct.user2_id, fact_acct.a_asset_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_PP_Operation_Activity(AD_Client_ID, AD_Org_ID, Created, CreatedBy, IsActive, Updated, UpdatedBy, PP_Order_ID, DocStatus, Value, S_Resource_ID, DurationRequiered, DurationReal, Duration, QtyDelivered, QtyReject, QtyScrap, DateStartSchedule, DateFinishSchedule) AS SELECT n.ad_client_id AS AD_Client_ID, n.ad_org_id AS AD_Org_ID, n.created AS Created, n.createdby AS CreatedBy, n.isactive AS IsActive, n.updated AS Updated, n.updatedby AS UpdatedBy, n.pp_order_id AS PP_Order_ID, n.docstatus AS DocStatus, n.value AS Value, n.s_resource_id AS S_Resource_ID, n.durationrequiered AS DurationRequiered, n.durationreal AS DurationReal, n.durationrequiered - n.durationreal AS Duration, n.qtydelivered AS QtyDelivered, n.qtyreject AS QtyReject, n.qtyscrap AS QtyScrap, n.datestartschedule AS DateStartSchedule, n.datefinishschedule AS DateFinishSchedule FROM pp_order_node n
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_PP_MRP(PP_MRP_ID, AD_Client_ID, AD_Org_ID, Created, CreatedBy, IsActive, IsAvailable, Updated, UpdatedBy, IsMPS, Name, Description, C_Order_ID, C_OrderLine_ID, DateOrdered, DateConfirm, DatePromised, DateStartSchedule, DateFinishSchedule, DateStart, DateSimulation, DocStatus, M_Forecast_ID, M_ForecastLine_ID, Value, M_Product_ID, M_Requisition_ID, M_RequisitionLine_ID, M_Warehouse_ID, PP_Order_ID, PP_Order_BOMLine_ID, DD_Order_ID, DD_OrderLine_ID, Qty, S_Resource_ID, Planner_ID, Priority, OrderType, TypeMRP, LowLevel, C_BPartner_ID, Version, DocumentNo) AS SELECT mrp.pp_mrp_id AS PP_MRP_ID, mrp.ad_client_id AS AD_Client_ID, mrp.ad_org_id AS AD_Org_ID, mrp.created AS Created, mrp.createdby AS CreatedBy, mrp.isactive AS IsActive, mrp.isavailable AS IsAvailable, mrp.updated AS Updated, mrp.updatedby AS UpdatedBy, pp.ismps AS IsMPS, mrp.name AS Name, mrp.description AS Description, mrp.c_order_id AS C_Order_ID, mrp.c_orderline_id AS C_OrderLine_ID, mrp.dateordered AS DateOrdered, mrp.dateconfirm AS DateConfirm, mrp.datepromised AS DatePromised, mrp.datestartschedule AS DateStartSchedule, mrp.datefinishschedule AS DateFinishSchedule, mrp.datestart AS DateStart, mrp.datesimulation AS DateSimulation, mrp.docstatus AS DocStatus, mrp.m_forecast_id AS M_Forecast_ID, mrp.m_forecastline_id AS M_ForecastLine_ID, mrp.value AS Value, mrp.m_product_id AS M_Product_ID, mrp.m_requisition_id AS M_Requisition_ID, mrp.m_requisitionline_id AS M_RequisitionLine_ID, mrp.m_warehouse_id AS M_Warehouse_ID, mrp.pp_order_id AS PP_Order_ID, mrp.pp_order_bomline_id AS PP_Order_BOMLine_ID, mrp.dd_order_id AS DD_Order_ID, mrp.dd_orderline_id AS DD_OrderLine_ID, mrp.qty AS Qty, mrp.s_resource_id AS S_Resource_ID, mrp.planner_id AS Planner_ID, mrp.priority AS Priority, mrp.ordertype AS OrderType, mrp.typemrp AS TypeMRP, p.lowlevel AS LowLevel, mrp.c_bpartner_id AS C_BPartner_ID, mrp.version AS Version, documentno(mrp.pp_mrp_id) AS DocumentNo FROM pp_mrp mrp
JOIN m_product p ON mrp.m_product_id = p.m_product_id
LEFT JOIN pp_product_planning pp ON pp.m_product_id = mrp.m_product_id AND mrp.m_warehouse_id = pp.m_warehouse_id WHERE mrp.qty <> 0 UNION SELECT 0 AS PP_MRP_ID, pp.ad_client_id AS AD_Client_ID, pp.ad_org_id AS AD_Org_ID, pp.created AS Created, pp.createdby AS CreatedBy, pp.isactive AS IsActive, 'Y' AS IsAvailable, pp.updated AS Updated, pp.updatedby AS UpdatedBy, pp.ismps AS IsMPS, NULL AS Name, NULL AS Description, NULL AS C_Order_ID, NULL AS C_OrderLine_ID, getdate() AS DateOrdered, getdate() AS DateConfirm, getdate() AS DatePromised, getdate() AS DateStartSchedule, getdate() AS DateFinishSchedule, getdate() AS DateStart, getdate() AS DateSimulation, 'CO' AS DocStatus, NULL AS M_Forecast_ID, NULL AS M_ForecastLine_ID, NULL AS Value, pp.m_product_id AS M_Product_ID, NULL AS M_Requisition_ID, NULL AS M_RequisitionLine_ID, pp.m_warehouse_id AS M_Warehouse_ID, NULL AS PP_Order_ID, NULL AS PP_Order_BOMLine_ID, NULL AS DD_Order_ID, NULL AS DD_OrderLine_ID, pp.safetystock - bomqtyonhand(pp.m_product_id, pp.m_warehouse_id, 0) AS Qty, pp.s_resource_id AS S_Resource_ID, NULL AS Planner_ID, NULL AS Priority, 'STK' AS OrderType, 'D' AS TypeMRP, p.lowlevel AS LowLevel, NULL AS C_BPartner_ID, NULL AS Version, 'Safety Strock' AS DocumentNo FROM pp_product_planning pp
JOIN m_product p ON pp.m_product_id = p.m_product_id WHERE bomqtyonhand(pp.m_product_id, pp.m_warehouse_id, 0) < pp.safetystock
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW M_CostMovement_v(AD_Client_ID, AD_Org_ID, C_AcctSchema_ID, M_CostHistory_ID, M_CostType_ID, M_CostElement_ID, M_AttributeSetInstance_ID, M_Product_ID, OldQty, NewQty, OldCostPrice, NewCostPrice, OldCQty, NewCQty, OldCAmt, NewCAmt, Qty, Amt, DeltaQty, DeltaAmt, C_OrderLine_ID, M_InOutLine_ID, C_InvoiceLine_ID, M_MovementLine_ID, M_InventoryLine_ID, M_ProductionLine_ID, C_ProjectIssue_ID, M_MatchInv_ID, M_CostDetail_ID, Description, Created, CreatedBy, Updated, UpdatedBy, IsActive) AS SELECT a.ad_client_id AS AD_Client_ID, a.ad_org_id AS AD_Org_ID, b.c_acctschema_id AS C_AcctSchema_ID, a.m_costhistory_id AS M_CostHistory_ID, a.m_costtype_id AS M_CostType_ID, a.m_costelement_id AS M_CostElement_ID, a.m_attributesetinstance_id AS M_AttributeSetInstance_ID, b.m_product_id AS M_Product_ID, a.oldqty AS OldQty, a.newqty AS NewQty, a.oldcostprice AS OldCostPrice, a.newcostprice AS NewCostPrice, a.oldcqty AS OldCQty, a.newcqty AS NewCQty, a.oldcamt AS OldCAmt, a.newcamt AS NewCAmt, b.qty AS Qty, b.amt AS Amt, b.deltaqty AS DeltaQty, b.deltaamt AS DeltaAmt, b.c_orderline_id AS C_OrderLine_ID, b.m_inoutline_id AS M_InOutLine_ID, b.c_invoiceline_id AS C_InvoiceLine_ID, b.m_movementline_id AS M_MovementLine_ID, b.m_inventoryline_id AS M_InventoryLine_ID, b.m_productionline_id AS M_ProductionLine_ID, b.c_projectissue_id AS C_ProjectIssue_ID, b.m_matchinv_id AS M_MatchInv_ID, a.m_costdetail_id AS M_CostDetail_ID, b.description AS Description, a.created AS Created, a.createdby AS CreatedBy, a.updated AS Updated, a.updatedby AS UpdatedBy, a.isactive AS IsActive FROM m_costhistory a
JOIN m_costdetail b ON a.m_costdetail_id = b.m_costdetail_id ORDER BY a.m_costhistory_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW T_RV_Reconciliation(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, Fact_Acct_ID, C_AcctSchema_ID, Account_ID, DateTrx, DateAcct, C_Period_ID, AD_Table_ID, Record_ID, Line_ID, GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType, C_Currency_ID, AmtSourceDr, AmtSourceCr, AmtSource, AmtAcctDr, AmtAcctCr, AmtAcct, Rate, C_UOM_ID, Qty, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, A_Asset_ID, Description, OrgValue, OrgName, AccountValue, Name, AccountType, BPartnerValue, BPName, C_BP_Group_ID, ProductValue, ProductName, UPC, M_Product_Category_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_SubAcct_ID, UserElement1_ID, UserElement2_ID, ad_org_description, ad_org_isactive, C_ElementValue_AD_Org_ID, C_BankAccount_ID, C_ElementValue_C_Currency_ID, C_Element_ID, C_ElementValue_ID, c_elementvalue_description, c_elementvalue_isactive, IsBankAccount, IsForeignCurrency, c_elementvalue_issummary, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, DUNS, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, NAICS, c_bp_name2, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, ReferenceNo, C_BP_SalesRep_ID,
c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, TaxID, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_Product_C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, DocumentNote, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, SKU, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, AD_PInstance_ID, MatchCode) AS SELECT f.ad_client_id AS AD_Client_ID, f.ad_org_id AS AD_Org_ID, f.isactive AS IsActive, f.created AS Created, f.createdby AS CreatedBy, f.updated AS Updated, f.updatedby AS UpdatedBy, f.fact_acct_id AS Fact_Acct_ID, f.c_acctschema_id AS C_AcctSchema_ID, f.account_id AS Account_ID, f.datetrx AS DateTrx, f.dateacct AS DateAcct, f.c_period_id AS C_Period_ID, f.ad_table_id AS AD_Table_ID, f.record_id AS Record_ID, f.line_id AS Line_ID, f.gl_category_id AS GL_Category_ID, f.gl_budget_id AS GL_Budget_ID, f.c_tax_id AS C_Tax_ID, f.m_locator_id AS M_Locator_ID, f.postingtype AS PostingType, f.c_currency_id AS C_Currency_ID, f.amtsourcedr AS AmtSourceDr, f.amtsourcecr AS AmtSourceCr, f.amtsource AS AmtSource, f.amtacctdr AS AmtAcctDr, f.amtacctcr AS AmtAcctCr, f.amtacct AS AmtAcct, f.rate AS Rate, f.c_uom_id AS C_UOM_ID, f.qty AS Qty,
f.m_product_id AS M_Product_ID, f.c_bpartner_id AS C_BPartner_ID, f.ad_orgtrx_id AS AD_OrgTrx_ID, f.c_locfrom_id AS C_LocFrom_ID, f.c_locto_id AS C_LocTo_ID, f.c_salesregion_id AS C_SalesRegion_ID, f.c_project_id AS C_Project_ID, f.c_campaign_id AS C_Campaign_ID, f.c_activity_id AS C_Activity_ID, f.user1_id AS User1_ID, f.user2_id AS User2_ID, f.a_asset_id AS A_Asset_ID, f.description AS Description, f.orgvalue AS OrgValue, f.orgname AS OrgName, f.accountvalue AS AccountValue, f.name AS Name, f.accounttype AS AccountType, f.bpartnervalue AS BPartnerValue, f.bpname AS BPName, f.c_bp_group_id AS C_BP_Group_ID, f.productvalue AS ProductValue, f.productname AS ProductName, f.upc AS UPC, f.m_product_category_id AS M_Product_Category_ID, f.c_projectphase_id AS C_ProjectPhase_ID, f.c_projecttask_id AS C_ProjectTask_ID, f.c_subacct_id AS C_SubAcct_ID, f.userelement1_id AS UserElement1_ID, f.userelement2_id AS UserElement2_ID, f.ad_org_description AS ad_org_description, f.ad_org_isactive AS ad_org_isactive, f.c_elementvalue_ad_org_id AS C_ElementValue_AD_Org_ID, f.c_bankaccount_id AS C_BankAccount_ID, f.c_elementvalue_c_currency_id AS C_ElementValue_C_Currency_ID, f.c_element_id AS C_Element_ID, f.c_elementvalue_id AS C_ElementValue_ID, f.c_elementvalue_description AS c_elementvalue_description, f.c_elementvalue_isactive AS c_elementvalue_isactive, f.isbankaccount AS IsBankAccount, f.isforeigncurrency AS IsForeignCurrency, f.c_elementvalue_issummary AS c_elementvalue_issummary, f.c_bp_acqusitioncost AS c_bp_acqusitioncost, f.c_bp_actuallifetimevalue AS c_bp_actuallifetimevalue, f.c_bp_ad_language AS c_bp_ad_language, f.c_bp_ad_orgbp_id AS C_BP_AD_OrgBP_ID, f.c_bp_ad_org_id AS C_BP_AD_Org_ID, f.c_bp_bpartner_parent_id AS C_BP_BPartner_Parent_ID, f.c_bp_c_dunning_id AS C_BP_C_Dunning_ID, f.c_bp_c_greeting_id AS C_BP_C_Greeting_ID, f.c_bp_c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, f.c_bp_c_paymentterm_id AS C_BP_C_PaymentTerm_ID, f.c_bp_created AS c_bp_created,
f.c_bp_createdby AS C_BP_CreatedBy, f.c_bp_c_taxgroup_id AS C_BP_C_TaxGroup_ID, f.c_bp_deliveryrule AS c_bp_deliveryrule, f.c_bp_deliveryviarule AS c_bp_deliveryviarule, f.c_bp_description AS c_bp_description, f.c_bp_dunninggrace AS c_bp_dunninggrace, f.duns AS DUNS, f.c_bp_firstsale AS c_bp_firstsale, f.c_bp_flatdiscount AS c_bp_flatdiscount, f.c_bp_freightcostrule AS c_bp_freightcostrule, f.c_bp_invoicerule AS c_bp_invoicerule, f.c_bp_isactive AS c_bp_isactive, f.c_bp_iscustomer AS c_bp_iscustomer, f.c_bp_isdiscountprinted AS c_bp_isdiscountprinted, f.c_bp_isemployee AS c_bp_isemployee, f.c_bp_ismanufacturer AS c_bp_ismanufacturer, f.c_bp_isonetime AS c_bp_isonetime, f.c_bp_ispotaxexempt AS c_bp_ispotaxexempt, f.c_bp_isprospect AS c_bp_isprospect, f.c_bp_issalesrep AS c_bp_issalesrep, f.c_bp_issummary AS c_bp_issummary, f.c_bp_istaxexempt AS c_bp_istaxexempt, f.c_bp_isvendor AS c_bp_isvendor, f.c_bp_logo_id AS C_BP_Logo_ID, f.c_bp_m_discountschema_id AS C_BP_M_DiscountSchema_ID, f.c_bp_m_pricelist_id AS C_BP_M_PriceList_ID, f.naics AS NAICS, f.c_bp_name2 AS c_bp_name2, f.c_bp_numberemployees AS c_bp_numberemployees, f.c_bp_paymentrule AS c_bp_paymentrule, f.c_bp_paymentrulepo AS c_bp_paymentrulepo, f.c_bp_po_discountschema_id AS C_BP_PO_DiscountSchema_ID, f.c_bp_po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, f.c_bp_po_pricelist_id AS C_BP_PO_PriceList_ID, f.c_bp_poreference AS c_bp_poreference, f.c_bp_potentiallifetimevalue AS c_bp_potentiallifetimevalue, f.c_bp_rating AS c_bp_rating, f.referenceno AS ReferenceNo, f.c_bp_salesrep_id AS C_BP_SalesRep_ID, f.c_bp_salesvolume AS c_bp_salesvolume, f.c_bp_sendemail AS c_bp_sendemail, f.c_bp_shareofcustomer AS c_bp_shareofcustomer, f.c_bp_shelflifeminpct AS c_bp_shelflifeminpct, f.c_bp_so_creditlimit AS c_bp_so_creditlimit, f.c_bp_socreditstatus AS c_bp_socreditstatus, f.c_bp_so_creditused AS c_bp_so_creditused, f.c_bp_so_description AS c_bp_so_description, f.taxid AS TaxID,
f.c_bp_totalopenbalance AS c_bp_totalopenbalance, f.c_bp_updated AS c_bp_updated, f.c_bp_updatedby AS C_BP_UpdatedBy, f.c_bp_url AS c_bp_url, f.m_product_ad_org_id AS M_Product_AD_Org_ID, f.classification AS Classification, f.m_product_copyfrom AS m_product_copyfrom, f.m_product_created AS m_product_created, f.m_product_createdby AS M_Product_CreatedBy, f.c_revenuerecognition_id AS C_RevenueRecognition_ID, f.c_subscriptiontype_id AS C_SubscriptionType_ID, f.c_taxcategory_id AS C_TaxCategory_ID, f.m_product_c_uom_id AS M_Product_C_UOM_ID, f.m_product_description AS m_product_description, f.descriptionurl AS DescriptionURL, f.discontinued AS Discontinued, f.discontinuedat AS DiscontinuedAt, f.documentnote AS DocumentNote, f.group1 AS Group1, f.group2 AS Group2, f.guaranteedays AS GuaranteeDays, f.guaranteedaysmin AS GuaranteeDaysMin, f.help AS Help, f.imageurl AS ImageURL, f.m_product_isactive AS m_product_isactive, f.isdropship AS IsDropShip, f.isexcludeautodelivery AS IsExcludeAutoDelivery, f.isinvoiceprintdetails AS IsInvoicePrintDetails, f.ispicklistprintdetails AS IsPickListPrintDetails, f.ispurchased AS IsPurchased, f.isselfservice AS IsSelfService, f.issold AS IsSold, f.isstocked AS IsStocked, f.m_product_issummary AS m_product_issummary, f.isverified AS IsVerified, f.iswebstorefeatured AS IsWebStoreFeatured, f.lowlevel AS LowLevel, f.m_product_m_attributeset_id AS M_Product_M_AttributeSet_ID, f.m_product_m_asi_id AS M_Product_M_ASI_ID, f.m_freightcategory_id AS M_FreightCategory_ID, f.m_product_m_locator_id AS M_Product_M_Locator_ID, f.m_product_processing AS m_product_processing, f.producttype AS ProductType, f.r_mailtext_id AS R_MailText_ID, f.m_product_salesrep_id AS M_Product_SalesRep_ID, f.s_expensetype_id AS S_ExpenseType_ID, f.shelfdepth AS ShelfDepth, f.shelfheight AS ShelfHeight, f.shelfwidth AS ShelfWidth, f.sku AS SKU, f.s_resource_id AS S_Resource_ID, f.unitsperpack AS UnitsPerPack, f.unitsperpallet AS UnitsPerPallet,
f.m_product_updated AS m_product_updated, f.m_product_updatedby AS M_Product_UpdatedBy, f.versionno AS VersionNo, f.volume AS Volume, f.weight AS Weight, t.ad_pinstance_id AS AD_PInstance_ID, t.matchcode AS MatchCode FROM rv_fact_acct f
JOIN t_reconciliation t ON f.fact_acct_id = t.fact_acct_id
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW M_Storage(M_Product_ID, M_Locator_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, QtyOnHand, QtyReserved, QtyOrdered, DateLastInventory, M_AttributeSetInstance_ID, M_Storage_UU, DateMaterialPolicy) AS SELECT s.m_product_id AS M_Product_ID, s.m_locator_id AS M_Locator_ID, s.ad_client_id AS AD_Client_ID, s.ad_org_id AS AD_Org_ID, s.isactive AS IsActive, s.created AS Created, s.createdby AS CreatedBy, s.updated AS Updated, s.updatedby AS UpdatedBy, s.qtyonhand AS QtyOnHand, 0 AS QtyReserved, 0 AS QtyOrdered, s.datelastinventory AS DateLastInventory, s.m_attributesetinstance_id AS M_AttributeSetInstance_ID, s.m_storageonhand_uu AS M_Storage_UU, s.datematerialpolicy AS DateMaterialPolicy FROM m_storageonhand s UNION  ALL SELECT sr.m_product_id AS M_Product_ID, w.m_reservelocator_id AS M_Locator_ID, sr.ad_client_id AS AD_Client_ID, sr.ad_org_id AS AD_Org_ID, sr.isactive AS IsActive, sr.created AS Created, sr.createdby AS CreatedBy, sr.updated AS Updated, sr.updatedby AS UpdatedBy, 0 AS QtyOnHand, sr.qty AS QtyReserved, 0 AS QtyOrdered, sr.datelastinventory AS DateLastInventory, sr.m_attributesetinstance_id AS M_AttributeSetInstance_ID, sr.m_storagereservation_uu AS M_Storage_UU, NULL AS DateMaterialPolicy FROM m_storagereservation sr
JOIN m_warehouse w ON sr.m_warehouse_id = w.m_warehouse_id WHERE sr.issotrx = 'Y' AND sr.qty <> 0 UNION  ALL SELECT so.m_product_id AS M_Product_ID, w.m_reservelocator_id AS M_Locator_ID, so.ad_client_id AS AD_Client_ID, so.ad_org_id AS AD_Org_ID, so.isactive AS IsActive, so.created AS Created, so.createdby AS CreatedBy, so.updated AS Updated, so.updatedby AS UpdatedBy, 0 AS QtyOnHand, 0 AS QtyReserved, so.qty AS QtyOrdered, so.datelastinventory AS DateLastInventory, so.m_attributesetinstance_id AS M_AttributeSetInstance_ID, so.m_storagereservation_uu AS M_Storage_UU, NULL AS DateMaterialPolicy FROM m_storagereservation so
JOIN m_warehouse w ON so.m_warehouse_id = w.m_warehouse_id WHERE so.issotrx = 'N' AND so.qty <> 0
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_T_1099Extract(AD_PInstance_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_BPartner_ID, Value, Name, TaxID, Address, Address1, Address2, Address3, Address4, CityStateZip, City, Region, Zip, Country, Cut_Year, Cut_Date, AmtBucket01, AmtBucket02, AmtBucket03, AmtBucket04, AmtBucket05, AmtBucket06, AmtBucket07, AmtBucket08, AmtBucket09, AmtBucket10, AmtBucket11, AmtBucket12, AmtBucket13, AmtBucket14, AmtBucket15, AmtBucket16) AS SELECT t.ad_pinstance_id AS AD_PInstance_ID, t.ad_client_id AS AD_Client_ID, t.ad_org_id AS AD_Org_ID, t.isactive AS IsActive, t.created AS Created, t.createdby AS CreatedBy, t.updated AS Updated, t.updatedby AS UpdatedBy, t.c_bpartner_id AS C_BPartner_ID, t.value AS Value, t.name AS Name, t.taxid AS TaxID, (((((l.address1 || ' ') || l.address2) || ' ') || l.address3) || ' ') || l.address4 AS Address, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, (((l.city || ', ') || r.name) || ' ') || l.postal AS CityStateZip, l.city AS City, r.name AS Region, l.postal AS Zip, ctry.name AS Country, t.cut_year AS Cut_Year, t.cut_date AS Cut_Date, COALESCE(t.amtbucket01, 0) AS AmtBucket01, COALESCE(t.amtbucket02, 0) AS AmtBucket02, COALESCE(t.amtbucket03, 0) AS AmtBucket03, COALESCE(t.amtbucket04, 0) AS AmtBucket04, COALESCE(t.amtbucket05, 0) AS AmtBucket05, COALESCE(t.amtbucket06, 0) AS AmtBucket06, COALESCE(t.amtbucket07, 0) AS AmtBucket07, COALESCE(t.amtbucket08, 0) AS AmtBucket08, COALESCE(t.amtbucket09, 0) AS AmtBucket09, COALESCE(t.amtbucket10, 0) AS AmtBucket10, COALESCE(t.amtbucket11, 0) AS AmtBucket11, COALESCE(t.amtbucket12, 0) AS AmtBucket12, COALESCE(t.amtbucket13, 0) AS AmtBucket13, COALESCE(t.amtbucket14, 0) AS AmtBucket14, COALESCE(t.amtbucket15, 0) AS AmtBucket15, COALESCE(t.amtbucket16, 0) AS AmtBucket16 FROM t_1099extract t
LEFT JOIN c_location l ON t.c_location_id = l.c_location_id
LEFT JOIN c_region r ON r.c_region_id = l.c_region_id
LEFT JOIN c_country ctry ON ctry.c_country_id = l.c_country_id WHERE t.ad_pinstance_id = (( SELECT max(t_1099extract.ad_pinstance_id) AS max
           FROM t_1099extract))
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_M_Product_WhereUsed_V(AD_Client_ID, AD_Org_ID, M_Product_ID, SelectedProductName, SelectedProductValue, SelectedProductDescription, PP_Product_BOM_ID, Value, Name, Description, QtyBOM, Line) AS SELECT bom.ad_client_id AS AD_Client_ID, bom.ad_org_id AS AD_Org_ID, p2.m_product_id AS M_Product_ID, p2.name AS SelectedProductName, p2.value AS SelectedProductValue, p2.description AS SelectedProductDescription, b.pp_product_bom_id AS PP_Product_BOM_ID, p.value AS Value, p.name AS Name, p.description AS Description, b.qtybom AS QtyBOM, b.line AS Line FROM m_product p2
LEFT JOIN pp_product_bomline b ON p2.m_product_id = b.m_product_id
LEFT JOIN pp_product_bom bom ON b.pp_product_bom_id = bom.pp_product_bom_id
LEFT JOIN m_product p ON bom.m_product_id = p.m_product_id WHERE p.isactive = 'Y' AND b.isactive = 'Y' AND p2.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW RV_Fact_Simple(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, C_Project_ID, C_AcctSchema_ID, Account_ID, AccountValue, AccountType, DateAcct, FinYear_Mth, AmtAcct, FinYear, "case") AS SELECT rv_fact_acct.ad_client_id AS AD_Client_ID, rv_fact_acct.ad_org_id AS AD_Org_ID, rv_fact_acct.isactive AS IsActive, rv_fact_acct.created AS Created, rv_fact_acct.createdby AS CreatedBy, rv_fact_acct.updated AS Updated, rv_fact_acct.updatedby AS UpdatedBy, rv_fact_acct.c_project_id AS C_Project_ID, rv_fact_acct.c_acctschema_id AS C_AcctSchema_ID, rv_fact_acct.account_id AS Account_ID, rv_fact_acct.accountvalue AS AccountValue, rv_fact_acct.accounttype AS AccountType, rv_fact_acct.dateacct AS DateAcct, to_char(rv_fact_acct.dateacct, 'YYYY-MM') AS FinYear_Mth, sum(rv_fact_acct.amtacctdr - rv_fact_acct.amtacctcr) AS AmtAcct, to_char(rv_fact_acct.dateacct, '"FY"YYYY') AS FinYear, CASE rv_fact_acct.accounttype WHEN 'A' THEN 'B' WHEN 'E' THEN 'P' WHEN 'L' THEN 'B' WHEN 'M' THEN 'B' WHEN 'O' THEN 'B' WHEN 'R' THEN 'P' ELSE '9. Unknown' END AS "case" FROM rv_fact_acct WHERE rv_fact_acct.postingtype = 'A' GROUP BY rv_fact_acct.ad_client_id, rv_fact_acct.ad_org_id, rv_fact_acct.isactive, rv_fact_acct.created, rv_fact_acct.createdby, rv_fact_acct.updated, rv_fact_acct.updatedby, rv_fact_acct.c_project_id, rv_fact_acct.c_acctschema_id, rv_fact_acct.account_id, rv_fact_acct.accountvalue, rv_fact_acct.accounttype, rv_fact_acct.dateacct, rv_fact_acct.amtacct
;

-- Nov 28, 2021, 11:19:16 PM CET
CREATE OR REPLACE VIEW AD_ChangeLog_v(AD_Session_ID, AD_ChangeLog_ID, TableName, Record_ID, ColumnName, OldValue, NewValue, Name, Created) AS SELECT l.ad_session_id AS AD_Session_ID, l.ad_changelog_id AS AD_ChangeLog_ID, t.tablename AS TableName, l.record_id AS Record_ID, c.columnname AS ColumnName, l.oldvalue AS OldValue, l.newvalue AS NewValue, u.name AS Name, l.created AS Created FROM ad_changelog l
JOIN ad_table t ON l.ad_table_id = t.ad_table_id
JOIN ad_column c ON l.ad_column_id = c.ad_column_id
JOIN ad_user u ON l.createdby = u.ad_user_id ORDER BY l.ad_session_id, l.ad_changelog_id, t.tablename, l.record_id, c.columnname
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Field_v(AD_Window_ID, AD_Tab_ID, AD_Field_ID, AD_Table_ID, AD_Column_ID, Name, Description, Help, IsDisplayed, DisplayLogic, DisplayLength, SeqNo, SortNo, IsSameLine, IsHeading, IsFieldOnly, IsReadOnly, isencryptedfield, ObscureType, ColumnName, ColumnSQL, FieldLength, VFormat, DefaultValue, IsKey, IsParent, IsMandatory, IsIdentifier, IsTranslated, AD_Reference_Value_ID, Callout, AD_Reference_ID, AD_Val_Rule_ID, AD_Process_ID, IsAlwaysUpdateable, ReadOnlyLogic, MandatoryLogic, IsUpdateable, isencryptedcolumn, IsSelectionColumn, TableName, ValueMin, ValueMax, fieldgroup, validationcode, Included_Tab_ID, FieldGroupType, IsCollapsedByDefault, InfoFactoryClass, IsAutocomplete, IsAllowCopy, IsDisplayedGrid, SeqNoGrid, SeqNoSelection, XPosition, ColumnSpan, NumLines, IsToolbarButton, FormatPattern, IsAdvancedField, IsDefaultFocus, AD_Chart_ID, AD_LabelStyle_ID, AD_FieldStyle_ID, PA_DashboardContent_ID, Placeholder, IsHtml, IsQuickForm, AD_Val_Rule_Lookup_ID, validationcodelookup) AS SELECT t.ad_window_id AS AD_Window_ID, f.ad_tab_id AS AD_Tab_ID, f.ad_field_id AS AD_Field_ID, tbl.ad_table_id AS AD_Table_ID, f.ad_column_id AS AD_Column_ID, f.name AS Name, f.description AS Description, f.help AS Help, f.isdisplayed AS IsDisplayed, f.displaylogic AS DisplayLogic, f.displaylength AS DisplayLength, f.seqno AS SeqNo, f.sortno AS SortNo, f.issameline AS IsSameLine, f.isheading AS IsHeading, f.isfieldonly AS IsFieldOnly, f.isreadonly AS IsReadOnly, f.isencrypted AS isencryptedfield, f.obscuretype AS ObscureType, c.columnname AS ColumnName, COALESCE(f.columnsql, c.columnsql) AS ColumnSQL, c.fieldlength AS FieldLength, COALESCE(f.vformat, c.vformat) AS VFormat, COALESCE(f.defaultvalue, c.defaultvalue) AS DefaultValue, c.iskey AS IsKey, c.isparent AS IsParent, COALESCE(f.ismandatory, c.ismandatory) AS IsMandatory, c.isidentifier AS IsIdentifier, c.istranslated AS IsTranslated,
COALESCE(f.ad_reference_value_id, c.ad_reference_value_id) AS AD_Reference_Value_ID, c.callout AS Callout, COALESCE(f.ad_reference_id, c.ad_reference_id) AS AD_Reference_ID, COALESCE(f.ad_val_rule_id, c.ad_val_rule_id) AS AD_Val_Rule_ID, c.ad_process_id AS AD_Process_ID, COALESCE(f.isalwaysupdateable, c.isalwaysupdateable) AS IsAlwaysUpdateable, COALESCE(f.readonlylogic, c.readonlylogic) AS ReadOnlyLogic, COALESCE(f.mandatorylogic, c.mandatorylogic) AS MandatoryLogic, COALESCE(f.isupdateable, c.isupdateable) AS IsUpdateable, c.isencrypted AS isencryptedcolumn, COALESCE(f.isselectioncolumn, c.isselectioncolumn) AS IsSelectionColumn, tbl.tablename AS TableName, c.valuemin AS ValueMin, c.valuemax AS ValueMax, fg.name AS fieldgroup, vr.code AS validationcode, f.included_tab_id AS Included_Tab_ID, fg.fieldgrouptype AS FieldGroupType, fg.iscollapsedbydefault AS IsCollapsedByDefault, COALESCE(f.infofactoryclass, c.infofactoryclass) AS InfoFactoryClass, c.isautocomplete AS IsAutocomplete, COALESCE(f.isallowcopy, c.isallowcopy) AS IsAllowCopy, f.isdisplayedgrid AS IsDisplayedGrid, f.seqnogrid AS SeqNoGrid, c.seqnoselection AS SeqNoSelection, f.xposition AS XPosition, f.columnspan AS ColumnSpan, f.numlines AS NumLines, COALESCE(f.istoolbarbutton, c.istoolbarbutton) AS IsToolbarButton, c.formatpattern AS FormatPattern, f.isadvancedfield AS IsAdvancedField, f.isdefaultfocus AS IsDefaultFocus, c.ad_chart_id AS AD_Chart_ID, f.ad_labelstyle_id AS AD_LabelStyle_ID, f.ad_fieldstyle_id AS AD_FieldStyle_ID, c.pa_dashboardcontent_id AS PA_DashboardContent_ID, COALESCE(f.placeholder, c.placeholder) AS Placeholder, c.ishtml AS IsHtml, f.isquickform AS IsQuickForm, COALESCE(f.ad_val_rule_lookup_id, c.ad_val_rule_lookup_id) AS AD_Val_Rule_Lookup_ID, vrf.code AS validationcodelookup FROM ad_field f
JOIN ad_tab t ON f.ad_tab_id = t.ad_tab_id
LEFT JOIN ad_fieldgroup fg ON f.ad_fieldgroup_id = fg.ad_fieldgroup_id
LEFT JOIN ad_column c ON f.ad_column_id = c.ad_column_id
JOIN ad_table tbl ON c.ad_table_id = tbl.ad_table_id
LEFT JOIN ad_val_rule vr ON vr.ad_val_rule_id = COALESCE(f.ad_val_rule_id, c.ad_val_rule_id)
LEFT JOIN ad_val_rule vrf ON vrf.ad_val_rule_id = COALESCE(f.ad_val_rule_lookup_id, c.ad_val_rule_lookup_id) WHERE f.isactive = 'Y' AND c.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Field_vt(AD_Language, AD_Window_ID, AD_Tab_ID, AD_Field_ID, AD_Table_ID, AD_Column_ID, Name, Description, Help, IsDisplayed, DisplayLogic, DisplayLength, SeqNo, SortNo, IsSameLine, IsHeading, IsFieldOnly, IsReadOnly, isencryptedfield, ObscureType, ColumnName, ColumnSQL, FieldLength, VFormat, DefaultValue, IsKey, IsParent, IsMandatory, IsIdentifier, IsTranslated, AD_Reference_Value_ID, Callout, AD_Reference_ID, AD_Val_Rule_ID, AD_Process_ID, IsAlwaysUpdateable, ReadOnlyLogic, MandatoryLogic, IsUpdateable, isencryptedcolumn, IsSelectionColumn, TableName, ValueMin, ValueMax, fieldgroup, validationcode, Included_Tab_ID, FieldGroupType, IsCollapsedByDefault, InfoFactoryClass, IsAutocomplete, IsAllowCopy, IsDisplayedGrid, SeqNoGrid, SeqNoSelection, XPosition, ColumnSpan, NumLines, IsToolbarButton, FormatPattern, IsAdvancedField, IsDefaultFocus, AD_Chart_ID, AD_LabelStyle_ID, AD_FieldStyle_ID, PA_DashboardContent_ID, Placeholder, IsHtml, IsQuickForm, AD_Val_Rule_Lookup_ID, validationcodelookup) AS SELECT trl.ad_language AS AD_Language, t.ad_window_id AS AD_Window_ID, f.ad_tab_id AS AD_Tab_ID, f.ad_field_id AS AD_Field_ID, tbl.ad_table_id AS AD_Table_ID, f.ad_column_id AS AD_Column_ID, trl.name AS Name, trl.description AS Description, trl.help AS Help, f.isdisplayed AS IsDisplayed, f.displaylogic AS DisplayLogic, f.displaylength AS DisplayLength, f.seqno AS SeqNo, f.sortno AS SortNo, f.issameline AS IsSameLine, f.isheading AS IsHeading, f.isfieldonly AS IsFieldOnly, f.isreadonly AS IsReadOnly, f.isencrypted AS isencryptedfield, f.obscuretype AS ObscureType, c.columnname AS ColumnName, COALESCE(f.columnsql, c.columnsql) AS ColumnSQL, c.fieldlength AS FieldLength, COALESCE(f.vformat, c.vformat) AS VFormat, COALESCE(f.defaultvalue, c.defaultvalue) AS DefaultValue, c.iskey AS IsKey, c.isparent AS IsParent, COALESCE(f.ismandatory, c.ismandatory) AS IsMandatory, c.isidentifier AS IsIdentifier, c.istranslated AS IsTranslated,
COALESCE(f.ad_reference_value_id, c.ad_reference_value_id) AS AD_Reference_Value_ID, c.callout AS Callout, COALESCE(f.ad_reference_id, c.ad_reference_id) AS AD_Reference_ID, COALESCE(f.ad_val_rule_id, c.ad_val_rule_id) AS AD_Val_Rule_ID, c.ad_process_id AS AD_Process_ID, COALESCE(f.isalwaysupdateable, c.isalwaysupdateable) AS IsAlwaysUpdateable, COALESCE(f.readonlylogic, c.readonlylogic) AS ReadOnlyLogic, COALESCE(f.mandatorylogic, c.mandatorylogic) AS MandatoryLogic, COALESCE(f.isupdateable, c.isupdateable) AS IsUpdateable, c.isencrypted AS isencryptedcolumn, COALESCE(f.isselectioncolumn, c.isselectioncolumn) AS IsSelectionColumn, tbl.tablename AS TableName, c.valuemin AS ValueMin, c.valuemax AS ValueMax, fgt.name AS fieldgroup, vr.code AS validationcode, f.included_tab_id AS Included_Tab_ID, fg.fieldgrouptype AS FieldGroupType, fg.iscollapsedbydefault AS IsCollapsedByDefault, COALESCE(f.infofactoryclass, c.infofactoryclass) AS InfoFactoryClass, c.isautocomplete AS IsAutocomplete, COALESCE(f.isallowcopy, c.isallowcopy) AS IsAllowCopy, f.isdisplayedgrid AS IsDisplayedGrid, f.seqnogrid AS SeqNoGrid, c.seqnoselection AS SeqNoSelection, f.xposition AS XPosition, f.columnspan AS ColumnSpan, f.numlines AS NumLines, COALESCE(f.istoolbarbutton, c.istoolbarbutton) AS IsToolbarButton, c.formatpattern AS FormatPattern, f.isadvancedfield AS IsAdvancedField, f.isdefaultfocus AS IsDefaultFocus, c.ad_chart_id AS AD_Chart_ID, f.ad_labelstyle_id AS AD_LabelStyle_ID, f.ad_fieldstyle_id AS AD_FieldStyle_ID, c.pa_dashboardcontent_id AS PA_DashboardContent_ID, COALESCE(trl.placeholder, ct.placeholder) AS Placeholder, c.ishtml AS IsHtml, f.isquickform AS IsQuickForm, COALESCE(f.ad_val_rule_lookup_id, c.ad_val_rule_lookup_id) AS AD_Val_Rule_Lookup_ID, vrf.code AS validationcodelookup FROM ad_field f
JOIN ad_field_trl trl ON f.ad_field_id = trl.ad_field_id
JOIN ad_tab t ON f.ad_tab_id = t.ad_tab_id
LEFT JOIN ad_fieldgroup fg ON f.ad_fieldgroup_id = fg.ad_fieldgroup_id
LEFT JOIN ad_fieldgroup_trl fgt ON f.ad_fieldgroup_id = fgt.ad_fieldgroup_id AND trl.ad_language = fgt.ad_language
LEFT JOIN ad_column c ON f.ad_column_id = c.ad_column_id
LEFT JOIN ad_column_trl ct ON f.ad_column_id = ct.ad_column_id AND trl.ad_language = ct.ad_language
JOIN ad_table tbl ON c.ad_table_id = tbl.ad_table_id
LEFT JOIN ad_val_rule vr ON vr.ad_val_rule_id = COALESCE(f.ad_val_rule_id, c.ad_val_rule_id)
LEFT JOIN ad_val_rule vrf ON vrf.ad_val_rule_id = COALESCE(f.ad_val_rule_lookup_id, c.ad_val_rule_lookup_id) WHERE f.isactive = 'Y' AND c.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Org_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, Value, Name, Description, IsSummary, C_Location_ID, DUNS, TaxID, Supervisor_ID, Parent_Org_ID, AD_OrgType_ID, M_Warehouse_ID, C_BPartner_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, o.value AS Value, o.name AS Name, o.description AS Description, o.issummary AS IsSummary, i.c_location_id AS C_Location_ID, i.duns AS DUNS, i.taxid AS TaxID, i.supervisor_id AS Supervisor_ID, i.parent_org_id AS Parent_Org_ID, i.ad_orgtype_id AS AD_OrgType_ID, i.m_warehouse_id AS M_Warehouse_ID, bp.c_bpartner_id AS C_BPartner_ID FROM ad_org o
JOIN ad_orginfo i ON o.ad_org_id = i.ad_org_id
LEFT JOIN c_bpartner bp ON o.ad_org_id = bp.ad_orgbp_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Tab_v(AD_Tab_ID, AD_Window_ID, AD_Table_ID, Name, Description, Help, SeqNo, IsSingleRow, HasTree, IsInfoTab, ReplicationType, TableName, AccessLevel, IsSecurityEnabled, IsDeleteable, IsHighVolume, IsView, hasassociation, IsTranslationTab, IsReadOnly, AD_Image_ID, TabLevel, WhereClause, OrderByClause, CommitWarning, ReadOnlyLogic, DisplayLogic, AD_Column_ID, AD_Process_ID, IsSortTab, IsInsertRecord, IsAdvancedTab, AD_ColumnSortOrder_ID, AD_ColumnSortYesNo_ID, Included_Tab_ID, Parent_Column_ID, AD_Tab_UU, AD_Table_UU, TreeDisplayedOn, MaxQueryRecords, IsAllowAdvancedLookup, IsLookupOnlySelection, AD_TabType) AS SELECT t.ad_tab_id AS AD_Tab_ID, t.ad_window_id AS AD_Window_ID, t.ad_table_id AS AD_Table_ID, t.name AS Name, t.description AS Description, t.help AS Help, t.seqno AS SeqNo, t.issinglerow AS IsSingleRow, t.hastree AS HasTree, t.isinfotab AS IsInfoTab, tbl.replicationtype AS ReplicationType, tbl.tablename AS TableName, tbl.accesslevel AS AccessLevel, tbl.issecurityenabled AS IsSecurityEnabled, tbl.isdeleteable AS IsDeleteable, tbl.ishighvolume AS IsHighVolume, tbl.isview AS IsView, CAST('N' AS char(1)) AS hasassociation, t.istranslationtab AS IsTranslationTab, t.isreadonly AS IsReadOnly, t.ad_image_id AS AD_Image_ID, t.tablevel AS TabLevel, t.whereclause AS WhereClause, t.orderbyclause AS OrderByClause, t.commitwarning AS CommitWarning, t.readonlylogic AS ReadOnlyLogic, t.displaylogic AS DisplayLogic, t.ad_column_id AS AD_Column_ID, t.ad_process_id AS AD_Process_ID, t.issorttab AS IsSortTab, t.isinsertrecord AS IsInsertRecord, t.isadvancedtab AS IsAdvancedTab, t.ad_columnsortorder_id AS AD_ColumnSortOrder_ID, t.ad_columnsortyesno_id AS AD_ColumnSortYesNo_ID, t.included_tab_id AS Included_Tab_ID, t.parent_column_id AS Parent_Column_ID, t.ad_tab_uu AS AD_Tab_UU, tbl.ad_table_uu AS AD_Table_UU, t.treedisplayedon AS TreeDisplayedOn, t.maxqueryrecords AS MaxQueryRecords, t.isallowadvancedlookup AS IsAllowAdvancedLookup,
t.islookuponlyselection AS IsLookupOnlySelection, t.ad_tabtype AS AD_TabType FROM ad_tab t
JOIN ad_table tbl ON t.ad_table_id = tbl.ad_table_id WHERE t.isactive = 'Y' AND tbl.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Tab_vt(AD_Language, AD_Tab_ID, AD_Window_ID, AD_Table_ID, Name, Description, Help, SeqNo, IsSingleRow, HasTree, IsInfoTab, ReplicationType, TableName, AccessLevel, IsSecurityEnabled, IsDeleteable, IsHighVolume, IsView, hasassociation, IsTranslationTab, IsReadOnly, AD_Image_ID, TabLevel, WhereClause, OrderByClause, CommitWarning, ReadOnlyLogic, DisplayLogic, AD_Column_ID, AD_Process_ID, IsSortTab, IsInsertRecord, IsAdvancedTab, AD_ColumnSortOrder_ID, AD_ColumnSortYesNo_ID, Included_Tab_ID, Parent_Column_ID, AD_Tab_UU, AD_Table_UU, TreeDisplayedOn, MaxQueryRecords, IsAllowAdvancedLookup, IsLookupOnlySelection, AD_TabType) AS SELECT trl.ad_language AS AD_Language, t.ad_tab_id AS AD_Tab_ID, t.ad_window_id AS AD_Window_ID, t.ad_table_id AS AD_Table_ID, trl.name AS Name, trl.description AS Description, trl.help AS Help, t.seqno AS SeqNo, t.issinglerow AS IsSingleRow, t.hastree AS HasTree, t.isinfotab AS IsInfoTab, tbl.replicationtype AS ReplicationType, tbl.tablename AS TableName, tbl.accesslevel AS AccessLevel, tbl.issecurityenabled AS IsSecurityEnabled, tbl.isdeleteable AS IsDeleteable, tbl.ishighvolume AS IsHighVolume, tbl.isview AS IsView, CAST('N' AS char(1)) AS hasassociation, t.istranslationtab AS IsTranslationTab, t.isreadonly AS IsReadOnly, t.ad_image_id AS AD_Image_ID, t.tablevel AS TabLevel, t.whereclause AS WhereClause, t.orderbyclause AS OrderByClause, trl.commitwarning AS CommitWarning, t.readonlylogic AS ReadOnlyLogic, t.displaylogic AS DisplayLogic, t.ad_column_id AS AD_Column_ID, t.ad_process_id AS AD_Process_ID, t.issorttab AS IsSortTab, t.isinsertrecord AS IsInsertRecord, t.isadvancedtab AS IsAdvancedTab, t.ad_columnsortorder_id AS AD_ColumnSortOrder_ID, t.ad_columnsortyesno_id AS AD_ColumnSortYesNo_ID, t.included_tab_id AS Included_Tab_ID, t.parent_column_id AS Parent_Column_ID, t.ad_tab_uu AS AD_Tab_UU, tbl.ad_table_uu AS AD_Table_UU, t.treedisplayedon AS TreeDisplayedOn, t.maxqueryrecords AS MaxQueryRecords,
t.isallowadvancedlookup AS IsAllowAdvancedLookup, t.islookuponlyselection AS IsLookupOnlySelection, t.ad_tabtype AS AD_TabType FROM ad_tab t
JOIN ad_table tbl ON t.ad_table_id = tbl.ad_table_id
JOIN ad_tab_trl trl ON t.ad_tab_id = trl.ad_tab_id WHERE t.isactive = 'Y' AND tbl.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_User_Roles_v(Name, roleName) AS SELECT u.name AS Name, r.name AS roleName FROM ad_user_roles ur
JOIN ad_user u ON ur.ad_user_id = u.ad_user_id
JOIN ad_role r ON ur.ad_role_id = r.ad_role_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW AD_Window_vt(AD_Language, AD_Window_ID, Name, Description, Help, WindowType, AD_Color_ID, AD_Image_ID, IsActive, WinWidth, WinHeight, IsSOTrx, AD_Window_UU) AS SELECT trl.ad_language AS AD_Language, bt.ad_window_id AS AD_Window_ID, trl.name AS Name, trl.description AS Description, trl.help AS Help, bt.windowtype AS WindowType, bt.ad_color_id AS AD_Color_ID, bt.ad_image_id AS AD_Image_ID, bt.isactive AS IsActive, bt.winwidth AS WinWidth, bt.winheight AS WinHeight, bt.issotrx AS IsSOTrx, bt.ad_window_uu AS AD_Window_UU FROM ad_window bt
JOIN ad_window_trl trl ON bt.ad_window_id = trl.ad_window_id WHERE bt.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Dunning_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_DunningRun_ID, C_DunningRunEntry_ID, DunningDate, PrintName, DocumentNote, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, SalesRep_ID, SalesRep_Name, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, Postal, Amt, Qty, Note, Logo_ID, C_Dunning_ID, C_DunningRun_C_DunningLevel_ID, c_dunningrun_description, Processed, Processing, SendIt, C_DunningRunEntry_AD_Org_ID, AD_User_ID, C_BPartner_Location_ID, C_Currency_ID, C_DunRunEntry_C_DunLevel_ID, c_dunningrunentry_created, C_DunningRunEntry_CreatedBy, c_dunningrunentry_isactive, c_dunningrunentry_processed, c_dunningrunentry_updated, C_DunningRunEntry_UpdatedBy, C_DunningLevel_AD_Org_ID, C_DunningLevel_C_Dunning_ID, ChargeFee, ChargeInterest, C_PaymentTerm_ID, DaysAfterDue, DaysBetweenDunning, c_dunninglevel_description, Dunning_PrintFormat_ID, FeeAmt, InterestPercent, InvoiceCollectionType, c_dunninglevel_isactive, IsSetCreditStop, IsSetPaymentTerm, IsShowAllDue, IsShowNotDue, IsStatement, c_dunninglevel_name, c_bpartner_acqusitioncost, c_bpartner_actuallifetimevalue, c_bpartner_ad_language, C_BPartner_AD_OrgBP_ID, C_BPartner_AD_Org_ID, C_BPartner_BPartner_Parent_ID, C_BPartner_C_BP_Group_ID, C_BPartner_C_Greeting_ID, C_BPartner_InvoiceSchedule_ID, C_BPartner_C_PaymentTerm_ID, c_bpartner_created, C_BPartner_CreatedBy, C_BPartner_C_TaxGroup_ID, c_bpartner_deliveryrule, c_bpartner_deliveryviarule, c_bpartner_description, c_bpartner_dunninggrace, c_bpartner_firstsale, c_bpartner_flatdiscount, c_bpartner_freightcostrule, c_bpartner_invoicerule, c_bpartner_isactive, c_bpartner_iscustomer, c_bpartner_isdiscountprinted, c_bpartner_isemployee, c_bpartner_ismanufacturer, c_bpartner_isonetime, c_bpartner_ispotaxexempt, c_bpartner_isprospect, c_bpartner_issalesrep, c_bpartner_issummary, c_bpartner_istaxexempt, c_bpartner_isvendor,
C_BPartner_Logo_ID, C_BPartner_M_DiscountSchema_ID, C_BPartner_M_PriceList_ID, c_bpartner_naics, c_bpartner_numberemployees, c_bpartner_paymentrule, c_bpartner_paymentrulepo, C_BPartnerPO_DiscountSchema_ID, C_BPartner_PO_PaymentTerm_ID, C_BPartner_PO_PriceList_ID, c_bpartner_poreference, c_bpartner_potentiallifetimev, c_bpartner_rating, C_BPartner_SalesRep_ID, c_bpartner_salesvolume, c_bpartner_sendemail, c_bpartner_shareofcustomer, c_bpartner_shelflifeminpct, c_bpartner_so_creditlimit, c_bpartner_socreditstatus, c_bpartner_so_creditused, c_bpartner_so_description, c_bpartner_totalopenbalance, c_bpartner_updated, C_BPartner_UpdatedBy, c_bpartner_url, C_BPartner_Greeting_AD_Org_ID, c_bpartner_greeting_isactive, c_bpartner_greeting_isfirstna, c_bpartner_greeting_name, C_BPartner_Location_AD_Org_ID, c_bpartner_location_created, C_BPartner_Location_CreatedBy, C_SalesRegion_ID, c_bpartner_location_fax, c_bpartner_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bpartner_location_name, c_bpartner_location_phone, c_bpartner_location_phone2, c_bpartner_location_updated, C_BPartner_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, Postal_Add, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_name, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfirstnameon, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID,
ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bpartner_acqusitionc, salesrep_bpartner_actuallifet, salesrep_bpartner_ad_language, SalesRep_BPartner_AD_OrgBP_ID, SalesRep_BPartner_AD_Org_ID, SalesRep_BPartner_BP_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_InvoiceSchedule_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bpartner_created, SalesRep_BPartner_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bpartner_deliveryrule, salesrep_bpartner_deliveryvia, salesrep_bpartner_description, salesrep_bpartner_dunninggrace, salesrep_bpartner_firstsale, salesrep_bpartner_flatdiscount, salesrep_bpartner_freightcost, salesrep_bpartner_invoicerule, salesrep_bpartner_isactive, salesrep_bpartner_iscustomer, salesrep_bpartner_isdiscountp, salesrep_bpartner_isemployee, salesrep_bpartner_ismanufactu, salesrep_bpartner_isonetime, salesrep_bpartner_ispotaxexem, salesrep_bpartner_isprospect, salesrep_bpartner_issalesrep, salesrep_bpartner_issummary, salesrep_bpartner_istaxexempt, salesrep_bpartner_isvendor, SalesRep_BPartner_Logo_ID, SalesRep_BP_MDiscountSchema_ID, SalesRep_BP_M_PriceList_ID, salesrep_bpartner_naics, salesrep_bpartner_numberemplo, salesrep_bpartner_paymentrule, salesrep_bpartner_paymentrupo, SalesRep_BP_PO_DiscountScm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bpartner_poreference, salesrep_bpartner_potentialli, salesrep_bpartner_rating,
SalesRep_BPartner_SalesRep_ID, salesrep_bpartner_salesvolume, salesrep_bpartner_sendemail, salesrep_bpartner_shareofcust, salesrep_bpartner_shelflifemi, salesrep_bpartner_so_creditli, salesrep_bpartner_socreditsta, salesrep_bpartner_so_creditus, salesrep_bpartner_so_descript, salesrep_bpartner_totalopenba, salesrep_bpartner_updated, SalesRep_BPartner_UpdatedBy, salesrep_bpartner_url) AS SELECT dr.ad_client_id AS AD_Client_ID, dr.ad_org_id AS AD_Org_ID, dr.isactive AS IsActive, dr.created AS Created, dr.createdby AS CreatedBy, dr.updated AS Updated, dr.updatedby AS UpdatedBy, dlt.ad_language AS AD_Language, dr.c_dunningrun_id AS C_DunningRun_ID, dre.c_dunningrunentry_id AS C_DunningRunEntry_ID, dr.dunningdate AS DunningDate, dlt.printname AS PrintName, dlt.note AS DocumentNote, dre.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, dre.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcgt.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, l.postal || l.postal_add AS Postal, dre.amt AS Amt, dre.qty AS Qty, dre.note AS Note, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, dr.c_dunning_id AS C_Dunning_ID, dr.c_dunninglevel_id AS C_DunningRun_C_DunningLevel_ID, dr.description AS c_dunningrun_description, dr.processed AS Processed, dr.processing AS Processing, dr.sendit AS SendIt, dre.ad_org_id AS C_DunningRunEntry_AD_Org_ID, dre.ad_user_id AS AD_User_ID, dre.c_bpartner_location_id AS C_BPartner_Location_ID, dre.c_currency_id AS C_Currency_ID, dre.c_dunninglevel_id AS C_DunRunEntry_C_DunLevel_ID, dre.created AS c_dunningrunentry_created, dre.createdby AS C_DunningRunEntry_CreatedBy, dre.isactive AS c_dunningrunentry_isactive,
dre.processed AS c_dunningrunentry_processed, dre.updated AS c_dunningrunentry_updated, dre.updatedby AS C_DunningRunEntry_UpdatedBy, dl.ad_org_id AS C_DunningLevel_AD_Org_ID, dl.c_dunning_id AS C_DunningLevel_C_Dunning_ID, dl.chargefee AS ChargeFee, dl.chargeinterest AS ChargeInterest, dl.c_paymentterm_id AS C_PaymentTerm_ID, dl.daysafterdue AS DaysAfterDue, dl.daysbetweendunning AS DaysBetweenDunning, dl.description AS c_dunninglevel_description, dl.dunning_printformat_id AS Dunning_PrintFormat_ID, dl.feeamt AS FeeAmt, dl.interestpercent AS InterestPercent, dl.invoicecollectiontype AS InvoiceCollectionType, dl.isactive AS c_dunninglevel_isactive, dl.issetcreditstop AS IsSetCreditStop, dl.issetpaymentterm AS IsSetPaymentTerm, dl.isshowalldue AS IsShowAllDue, dl.isshownotdue AS IsShowNotDue, dl.isstatement AS IsStatement, dl.name AS c_dunninglevel_name, bp.acqusitioncost AS c_bpartner_acqusitioncost, bp.actuallifetimevalue AS c_bpartner_actuallifetimevalue, bp.ad_language AS c_bpartner_ad_language, bp.ad_orgbp_id AS C_BPartner_AD_OrgBP_ID, bp.ad_org_id AS C_BPartner_AD_Org_ID, bp.bpartner_parent_id AS C_BPartner_BPartner_Parent_ID, bp.c_bp_group_id AS C_BPartner_C_BP_Group_ID, bp.c_greeting_id AS C_BPartner_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BPartner_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BPartner_C_PaymentTerm_ID, bp.created AS c_bpartner_created, bp.createdby AS C_BPartner_CreatedBy, bp.c_taxgroup_id AS C_BPartner_C_TaxGroup_ID, bp.deliveryrule AS c_bpartner_deliveryrule, bp.deliveryviarule AS c_bpartner_deliveryviarule, bp.description AS c_bpartner_description, bp.dunninggrace AS c_bpartner_dunninggrace, bp.firstsale AS c_bpartner_firstsale, bp.flatdiscount AS c_bpartner_flatdiscount, bp.freightcostrule AS c_bpartner_freightcostrule, bp.invoicerule AS c_bpartner_invoicerule, bp.isactive AS c_bpartner_isactive, bp.iscustomer AS c_bpartner_iscustomer, bp.isdiscountprinted AS c_bpartner_isdiscountprinted, bp.isemployee AS c_bpartner_isemployee,
bp.ismanufacturer AS c_bpartner_ismanufacturer, bp.isonetime AS c_bpartner_isonetime, bp.ispotaxexempt AS c_bpartner_ispotaxexempt, bp.isprospect AS c_bpartner_isprospect, bp.issalesrep AS c_bpartner_issalesrep, bp.issummary AS c_bpartner_issummary, bp.istaxexempt AS c_bpartner_istaxexempt, bp.isvendor AS c_bpartner_isvendor, bp.logo_id AS C_BPartner_Logo_ID, bp.m_discountschema_id AS C_BPartner_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BPartner_M_PriceList_ID, bp.naics AS c_bpartner_naics, bp.numberemployees AS c_bpartner_numberemployees, bp.paymentrule AS c_bpartner_paymentrule, bp.paymentrulepo AS c_bpartner_paymentrulepo, bp.po_discountschema_id AS C_BPartnerPO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BPartner_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BPartner_PO_PriceList_ID, bp.poreference AS c_bpartner_poreference, bp.potentiallifetimevalue AS c_bpartner_potentiallifetimev, bp.rating AS c_bpartner_rating, bp.salesrep_id AS C_BPartner_SalesRep_ID, bp.salesvolume AS c_bpartner_salesvolume, bp.sendemail AS c_bpartner_sendemail, bp.shareofcustomer AS c_bpartner_shareofcustomer, bp.shelflifeminpct AS c_bpartner_shelflifeminpct, bp.so_creditlimit AS c_bpartner_so_creditlimit, bp.socreditstatus AS c_bpartner_socreditstatus, bp.so_creditused AS c_bpartner_so_creditused, bp.so_description AS c_bpartner_so_description, bp.totalopenbalance AS c_bpartner_totalopenbalance, bp.updated AS c_bpartner_updated, bp.updatedby AS C_BPartner_UpdatedBy, bp.url AS c_bpartner_url, bpgt.ad_org_id AS C_BPartner_Greeting_AD_Org_ID, bpgt.isactive AS c_bpartner_greeting_isactive, bpg.isfirstnameonly AS c_bpartner_greeting_isfirstna, bpgt.name AS c_bpartner_greeting_name, bpl.ad_org_id AS C_BPartner_Location_AD_Org_ID, bpl.created AS c_bpartner_location_created, bpl.createdby AS C_BPartner_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bpartner_location_fax, bpl.isactive AS c_bpartner_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN,
bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bpartner_location_name, bpl.phone AS c_bpartner_location_phone, bpl.phone2 AS c_bpartner_location_phone2, bpl.updated AS c_bpartner_location_updated, bpl.updatedby AS C_BPartner_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.postal_add AS Postal_Add, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.name AS ad_user_name, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcgt.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcgt.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfirstnameon, bpcgt.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns,
oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bpartner_acqusitionc, ubp.actuallifetimevalue AS salesrep_bpartner_actuallifet, ubp.ad_language AS salesrep_bpartner_ad_language, ubp.ad_orgbp_id AS SalesRep_BPartner_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BPartner_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BPartner_BP_Parent_ID, ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_InvoiceSchedule_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bpartner_created, ubp.createdby AS SalesRep_BPartner_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bpartner_deliveryrule, ubp.deliveryviarule AS salesrep_bpartner_deliveryvia, ubp.description AS salesrep_bpartner_description, ubp.dunninggrace AS salesrep_bpartner_dunninggrace,
ubp.firstsale AS salesrep_bpartner_firstsale, ubp.flatdiscount AS salesrep_bpartner_flatdiscount, ubp.freightcostrule AS salesrep_bpartner_freightcost, ubp.invoicerule AS salesrep_bpartner_invoicerule, ubp.isactive AS salesrep_bpartner_isactive, ubp.iscustomer AS salesrep_bpartner_iscustomer, ubp.isdiscountprinted AS salesrep_bpartner_isdiscountp, ubp.isemployee AS salesrep_bpartner_isemployee, ubp.ismanufacturer AS salesrep_bpartner_ismanufactu, ubp.isonetime AS salesrep_bpartner_isonetime, ubp.ispotaxexempt AS salesrep_bpartner_ispotaxexem, ubp.isprospect AS salesrep_bpartner_isprospect, ubp.issalesrep AS salesrep_bpartner_issalesrep, ubp.issummary AS salesrep_bpartner_issummary, ubp.istaxexempt AS salesrep_bpartner_istaxexempt, ubp.isvendor AS salesrep_bpartner_isvendor, ubp.logo_id AS SalesRep_BPartner_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_MDiscountSchema_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bpartner_naics, ubp.numberemployees AS salesrep_bpartner_numberemplo, ubp.paymentrule AS salesrep_bpartner_paymentrule, ubp.paymentrulepo AS salesrep_bpartner_paymentrupo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountScm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bpartner_poreference, ubp.potentiallifetimevalue AS salesrep_bpartner_potentialli, ubp.rating AS salesrep_bpartner_rating, ubp.salesrep_id AS SalesRep_BPartner_SalesRep_ID, ubp.salesvolume AS salesrep_bpartner_salesvolume, ubp.sendemail AS salesrep_bpartner_sendemail, ubp.shareofcustomer AS salesrep_bpartner_shareofcust, ubp.shelflifeminpct AS salesrep_bpartner_shelflifemi, ubp.so_creditlimit AS salesrep_bpartner_so_creditli, ubp.socreditstatus AS salesrep_bpartner_socreditsta, ubp.so_creditused AS salesrep_bpartner_so_creditus, ubp.so_description AS salesrep_bpartner_so_descript, ubp.totalopenbalance AS salesrep_bpartner_totalopenba,
ubp.updated AS salesrep_bpartner_updated, ubp.updatedby AS SalesRep_BPartner_UpdatedBy, ubp.url AS salesrep_bpartner_url FROM c_dunningrun dr
JOIN c_dunningrunentry dre ON dr.c_dunningrun_id = dre.c_dunningrun_id
JOIN c_dunninglevel dl ON dre.c_dunninglevel_id = dl.c_dunninglevel_id
JOIN c_dunninglevel_trl dlt ON dl.c_dunninglevel_id = dlt.c_dunninglevel_id
JOIN c_bpartner bp ON dre.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id AND dlt.ad_language = bpgt.ad_language
JOIN c_bpartner_location bpl ON dre.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON dre.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_greeting_trl bpcgt ON bpc.c_greeting_id = bpcgt.c_greeting_id AND dlt.ad_language = bpcgt.ad_language
JOIN ad_orginfo oi ON dr.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON dr.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON dre.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Dunning_Line_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_DunningRunLine_ID, C_DunningRunEntry_ID, Amt, ConvertedAmt, DaysDue, TimesDunned, InterestAmt, FeeAmt, TotalAmt, C_Invoice_ID, IsSOTrx, DocumentNo, DocStatus, DateTrx, C_DocType_ID, DocumentType, Description, C_Currency_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, User1_ID, User2_ID, DateAcct, C_ConversionType_ID, AD_OrgTrx_ID, POReference, DateOrdered, DateInvoiced, IsInDispute, PaymentTerm, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsPaid, IsAllocated, TenderType, DiscountAmt, C_InvoicePaySchedule_ID, C_Payment_ID, c_dunningrunline_isindispute, OpenAmt, c_dunningrunline_processed, C_Invoice_AD_Org_ID, AD_User_ID, C_Invoice_C_BPartner_ID, C_BPartner_Location_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, C_Invoice_C_Order_ID, C_Invoice_C_Payment_ID, c_invoice_created, C_Invoice_CreatedBy, DatePrinted, DocAction, DunningGrace, GenerateTo, InvoiceCollectionType, c_invoice_isactive, c_invoice_isapproved, IsDiscountPrinted, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, IsTaxIncluded, IsTransferred, M_RMA_ID, PaymentRule, c_invoice_posted, c_invoice_processedon, Processing, Ref_Invoice_ID, C_Invoice_Reversal_ID, SalesRep_ID, SendEMail, c_invoice_updated, C_Invoice_UpdatedBy, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, DocumentNote, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value, A_City, A_Country, C_Payment_AD_Org_ID, A_EMail, A_Ident_DL, A_Name, A_State, A_Street, A_Zip, C_Payment_C_BPartner_ID, C_Payment_C_Charge_ID, c_payment_chargeamt, CheckNo, C_Payment_C_Invoice_ID, C_Payment_C_Order_ID, C_PaymentBatch_ID, c_payment_created, C_Payment_CreatedBy, c_payment_isactive, IsApproved,
IsDelayedCapture, IsOnline, IsOverUnderPayment, IsPrepayment, IsReconciled, c_payment_isselfservice, Micr, Orig_TrxID, OverUnderAmt, PayAmt, PONum, c_payment_posted, c_payment_processed, c_payment_processedon, R_AuthCode, R_AuthCode_DC, R_AvsAddr, R_AvsZip, R_CVV2Match, Ref_Payment_ID, C_Payment_Reversal_ID, R_Info, RoutingNo, R_PnRef, R_PnRef_DC, R_RespMsg, R_Result, Swipe, TaxAmt, TrxType, c_payment_updated, C_Payment_UpdatedBy, VoiceAuthCode, WriteOffAmt) AS SELECT drl.ad_client_id AS AD_Client_ID, drl.ad_org_id AS AD_Org_ID, drl.isactive AS IsActive, drl.created AS Created, drl.createdby AS CreatedBy, drl.updated AS Updated, drl.updatedby AS UpdatedBy, COALESCE(dt.ad_language, dtp.ad_language) AS AD_Language, drl.c_dunningrunline_id AS C_DunningRunLine_ID, drl.c_dunningrunentry_id AS C_DunningRunEntry_ID, drl.amt AS Amt, drl.convertedamt AS ConvertedAmt, drl.daysdue AS DaysDue, drl.timesdunned AS TimesDunned, drl.interestamt AS InterestAmt, drl.feeamt AS FeeAmt, drl.totalamt AS TotalAmt, drl.c_invoice_id AS C_Invoice_ID, COALESCE(i.issotrx, p.isreceipt) AS IsSOTrx, COALESCE(i.documentno, p.documentno) AS DocumentNo, COALESCE(i.docstatus, p.docstatus) AS DocStatus, COALESCE(i.dateinvoiced, p.datetrx) AS DateTrx, COALESCE(i.c_doctype_id, p.c_doctype_id) AS C_DocType_ID, COALESCE(dt.printname, dtp.printname) AS DocumentType, COALESCE(i.description, p.description) AS Description, COALESCE(i.c_currency_id, p.c_currency_id) AS C_Currency_ID, COALESCE(i.c_campaign_id, p.c_campaign_id) AS C_Campaign_ID, COALESCE(i.c_project_id, p.c_project_id) AS C_Project_ID, COALESCE(i.c_activity_id, p.c_activity_id) AS C_Activity_ID, COALESCE(i.user1_id, p.user1_id) AS User1_ID, COALESCE(i.user2_id, p.user2_id) AS User2_ID, COALESCE(i.dateacct, p.dateacct) AS DateAcct, COALESCE(i.c_conversiontype_id, p.c_conversiontype_id) AS C_ConversionType_ID, COALESCE(i.ad_orgtrx_id, p.ad_orgtrx_id) AS AD_OrgTrx_ID, i.poreference AS POReference, i.dateordered AS DateOrdered,
i.dateinvoiced AS DateInvoiced, i.isindispute AS IsInDispute, ptt.name AS PaymentTerm, i.c_charge_id AS C_Charge_ID, i.chargeamt AS ChargeAmt, i.totallines AS TotalLines, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, i.m_pricelist_id AS M_PriceList_ID, i.ispaid AS IsPaid, p.isallocated AS IsAllocated, p.tendertype AS TenderType, p.discountamt AS DiscountAmt, drl.c_invoicepayschedule_id AS C_InvoicePaySchedule_ID, drl.c_payment_id AS C_Payment_ID, drl.isindispute AS c_dunningrunline_isindispute, drl.openamt AS OpenAmt, drl.processed AS c_dunningrunline_processed, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_id AS C_Invoice_C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.c_order_id AS C_Invoice_C_Order_ID, i.c_payment_id AS C_Invoice_C_Payment_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateprinted AS DatePrinted, i.docaction AS DocAction, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS c_invoice_isapproved, i.isdiscountprinted AS IsDiscountPrinted, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.posted AS c_invoice_posted, i.processedon AS c_invoice_processedon, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS C_Invoice_Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, ptt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_ID, ptt.description AS c_paymentterm_description, pt.discount AS Discount,
pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, ptt.documentnote AS DocumentNote, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, ptt.isactive AS c_paymentterm_isactive, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value, p.a_city AS A_City, p.a_country AS A_Country, p.ad_org_id AS C_Payment_AD_Org_ID, p.a_email AS A_EMail, p.a_ident_dl AS A_Ident_DL, p.a_name AS A_Name, p.a_state AS A_State, p.a_street AS A_Street, p.a_zip AS A_Zip, p.c_bpartner_id AS C_Payment_C_BPartner_ID, p.c_charge_id AS C_Payment_C_Charge_ID, p.chargeamt AS c_payment_chargeamt, p.checkno AS CheckNo, p.c_invoice_id AS C_Payment_C_Invoice_ID, p.c_order_id AS C_Payment_C_Order_ID, p.c_paymentbatch_id AS C_PaymentBatch_ID, p.created AS c_payment_created, p.createdby AS C_Payment_CreatedBy, p.isactive AS c_payment_isactive, p.isapproved AS IsApproved, p.isdelayedcapture AS IsDelayedCapture, p.isonline AS IsOnline, p.isoverunderpayment AS IsOverUnderPayment, p.isprepayment AS IsPrepayment, p.isreconciled AS IsReconciled, p.isselfservice AS c_payment_isselfservice, p.micr AS Micr, p.orig_trxid AS Orig_TrxID, p.overunderamt AS OverUnderAmt, p.payamt AS PayAmt, p.ponum AS PONum, p.posted AS c_payment_posted, p.processed AS c_payment_processed, p.processedon AS c_payment_processedon, p.r_authcode AS R_AuthCode, p.r_authcode_dc AS R_AuthCode_DC, p.r_avsaddr AS R_AvsAddr, p.r_avszip AS R_AvsZip, p.r_cvv2match AS R_CVV2Match, p.ref_payment_id AS Ref_Payment_ID, p.reversal_id AS C_Payment_Reversal_ID, p.r_info AS R_Info, p.routingno AS RoutingNo, p.r_pnref AS R_PnRef, p.r_pnref_dc AS R_PnRef_DC, p.r_respmsg AS R_RespMsg, p.r_result AS R_Result, p.swipe AS Swipe, p.taxamt AS TaxAmt, p.trxtype AS TrxType,
p.updated AS c_payment_updated, p.updatedby AS C_Payment_UpdatedBy, p.voiceauthcode AS VoiceAuthCode, p.writeoffamt AS WriteOffAmt FROM c_dunningrunline drl
LEFT JOIN c_invoice i ON drl.c_invoice_id = i.c_invoice_id
LEFT JOIN c_doctype_trl dt ON i.c_doctype_id = dt.c_doctype_id
LEFT JOIN c_paymentterm pt ON i.c_paymentterm_id = pt.c_paymentterm_id
LEFT JOIN c_paymentterm_trl ptt ON i.c_paymentterm_id = ptt.c_paymentterm_id AND ptt.ad_language = dt.ad_language
LEFT JOIN c_payment p ON drl.c_payment_id = p.c_payment_id
LEFT JOIN c_doctype_trl dtp ON p.c_doctype_id = dtp.c_doctype_id WHERE COALESCE(dt.ad_language, dtp.ad_language) = COALESCE(dtp.ad_language, dt.ad_language)
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Invoice_Candidate_v(AD_Client_ID, AD_Org_ID, C_BPartner_ID, C_Order_ID, DocumentNo, DateOrdered, C_DocType_ID, TotalLines) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.c_bpartner_id AS C_BPartner_ID, o.c_order_id AS C_Order_ID, o.documentno AS DocumentNo, o.dateordered AS DateOrdered, o.c_doctype_id AS C_DocType_ID, sum((l.qtyordered - l.qtyinvoiced) * l.priceactual) AS TotalLines FROM c_order o
JOIN c_orderline l ON o.c_order_id = l.c_order_id
JOIN c_bpartner bp ON o.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_invoiceschedule si ON bp.c_invoiceschedule_id = si.c_invoiceschedule_id WHERE (o.docstatus IN ('CO', 'CL', 'IP')) AND (o.c_doctype_id IN ( SELECT c_doctype.c_doctype_id
           FROM c_doctype
          WHERE c_doctype.docbasetype = 'SOO' AND (c_doctype.docsubtypeso NOT IN ('ON', 'OB', 'WR')))) AND l.qtyordered <> l.qtyinvoiced AND (o.invoicerule = 'I' OR o.invoicerule = 'O' AND NOT (EXISTS ( SELECT 1
           FROM c_orderline zz1
          WHERE zz1.c_order_id = o.c_order_id AND zz1.qtyordered <> zz1.qtydelivered)) OR o.invoicerule = 'D' AND l.qtyinvoiced <> l.qtydelivered OR o.invoicerule = 'S' AND bp.c_invoiceschedule_id IS NULL OR o.invoicerule = 'S' AND bp.c_invoiceschedule_id IS NOT NULL AND (si.invoicefrequency IS NULL OR si.invoicefrequency = 'D' OR si.invoicefrequency = 'W' OR si.invoicefrequency = 'T' AND (trunc(o.dateordered) <= (firstof(getdate(), 'MM') + si.invoicedaycutoff - 1) AND trunc(getdate()) >= (firstof(o.dateordered, 'MM') + si.invoiceday - 1) OR trunc(o.dateordered) <= (firstof(getdate(), 'MM') + si.invoicedaycutoff + 14) AND trunc(getdate()) >= (firstof(o.dateordered, 'MM') + si.invoiceday + 14)) OR si.invoicefrequency = 'M' AND trunc(o.dateordered) <= (firstof(getdate(), 'MM') + si.invoicedaycutoff - 1) AND trunc(getdate()) >= (firstof(o.dateordered, 'MM') + si.invoiceday - 1))) GROUP BY o.ad_client_id, o.ad_org_id, o.c_bpartner_id, o.c_order_id, o.documentno, o.dateordered, o.c_doctype_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Invoice_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Invoice_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, DocumentType, DocumentTypeNote, C_Order_ID, SalesRep_ID, SalesRep_Name, DateInvoiced, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, Postal, Description, POReference, DateOrdered, C_Currency_ID, PaymentTerm, PaymentTermNote, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsTaxIncluded, C_Campaign_ID, C_Project_ID, C_Activity_ID, IsPaid, Logo_ID, C_Invoice_AD_OrgTrx_ID, AD_User_ID, C_BPartner_Location_ID, C_ConversionType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, C_Payment_ID, DateAcct, DatePrinted, DocAction, DunningGrace, GenerateTo, InvoiceCollectionType, IsApproved, IsDiscountPrinted, IsInDispute, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, IsTransferred, M_RMA_ID, PaymentRule, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SendEMail, User1_ID, User2_ID, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDefault, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value, bp_acqusitioncost, bp_actuallifetimevalue, bp_ad_language, BP_AD_OrgBP_ID, BP_AD_Org_ID, BP_BPartner_Parent_ID, BP_C_BP_Group_ID, BP_C_Dunning_ID, BP_C_Greeting_ID, BP_C_InvoiceSchedule_ID, BP_C_PaymentTerm_ID, bp_created, BP_CreatedBy, BP_C_TaxGroup_ID, bp_deliveryrule, bp_deliveryviarule, bp_description, bp_dunninggrace, bp_firstsale, bp_flatdiscount, bp_freightcostrule, bp_invoicerule, bp_isactive, bp_iscustomer, bp_isdiscountprinted, bp_isemployee, bp_ismanufacturer, bp_isonetime, bp_ispotaxexempt, bp_isprospect, bp_issalesrep,
bp_issummary, bp_istaxexempt, bp_isvendor, BP_Logo_ID, BP_M_DiscountSchema_ID, BP_M_PriceList_ID, bp_naics, bp_numberemployees, bp_paymentrule, bp_paymentrulepo, BP_PO_DiscountSchema_ID, BP_PO_PaymentTerm_ID, BP_PO_PriceList_ID, bp_poreference, bp_potentiallifetimevalue, bp_rating, BP_SalesRep_ID, bp_salesvolume, bp_sendemail, bp_shareofcustomer, bp_shelflifeminpct, bp_so_creditlimit, bp_socreditstatus, bp_so_creditused, bp_so_description, bp_totalopenbalance, bp_updated, BP_UpdatedBy, bp_url, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, BP_Location_AD_Org_ID, BP_Location_C_BPartner_ID, bp_location_created, BP_Location_CreatedBy, C_SalesRegion_ID, bp_location_fax, bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, bp_location_name, bp_location_phone, bp_location_phone2, bp_location_updated, BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, user_greeting_isactive, user_greeting_isfirstnameonly, user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID,
SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, bpartner_acqusitioncost, bpartner_actuallifetimevalue, bpartner_ad_language, BPartner_AD_OrgBP_ID, BPartner_AD_Org_ID, BPartner_BPartner_Parent_ID, BPartner_C_BP_Group_ID, BPartner_C_Dunning_ID, BPartner_C_Greeting_ID, BPartner_C_InvoiceSchedule_ID, BPartner_C_PaymentTerm_ID, bpartner_created, BPartner_CreatedBy, BPartner_C_TaxGroup_ID, bpartner_deliveryrule, bpartner_deliveryviarule, bpartner_description, bpartner_dunninggrace, bpartner_duns, bpartner_firstsale, bpartner_flatdiscount, bpartner_freightcostrule, bpartner_invoicerule, bpartner_isactive, bpartner_iscustomer, bpartner_isdiscountprinted, bpartner_isemployee, bpartner_ismanufacturer, bpartner_isonetime, bpartner_ispotaxexempt, bpartner_isprospect, bpartner_issalesrep, bpartner_issummary, bpartner_istaxexempt, bpartner_isvendor, BPartner_Logo_ID, BPartner_M_DiscountSchema_ID, BPartner_M_PriceList_ID, bpartner_naics, bpartner_name2, bpartner_numberemployees, bpartner_paymentrule, bpartner_paymentrulepo, BPartner_PO_DiscountSchema_ID, BPartner_PO_PaymentTerm_ID, BPartner_PO_PriceList_ID, bpartner_poreference, bpartner_potentiallifetimeval, bpartner_rating, bpartner_referenceno, BPartner_SalesRep_ID, bpartner_salesvolume, bpartner_sendemail, bpartner_shareofcustomer, bpartner_shelflifeminpct, bpartner_so_creditlimit, bpartner_socreditstatus, bpartner_so_creditused, bpartner_so_description, bpartner_taxid, bpartner_totalopenbalance, bpartner_updated, BPartner_UpdatedBy, bpartner_url, BPartner_Value, CurSymbol, cur_description) AS SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated,
i.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, i.c_invoice_id AS C_Invoice_ID, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.c_doctype_id AS C_DocType_ID, i.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, i.c_order_id AS C_Order_ID, i.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, i.dateinvoiced AS DateInvoiced, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcgt.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, l.postal || l.postal_add AS Postal, i.description AS Description, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, ptt.name AS PaymentTerm, ptt.documentnote AS PaymentTermNote, i.c_charge_id AS C_Charge_ID, i.chargeamt AS ChargeAmt, i.totallines AS TotalLines, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, i.m_pricelist_id AS M_PriceList_ID, i.istaxincluded AS IsTaxIncluded, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.ispaid AS IsPaid, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.c_payment_id AS C_Payment_ID, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.docaction AS DocAction, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute,
i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.istransferred AS IsTransferred, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.user1_id AS User1_ID, i.user2_id AS User2_ID, ptt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_ID, ptt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, ptt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value, bp.acqusitioncost AS bp_acqusitioncost, bp.actuallifetimevalue AS bp_actuallifetimevalue, bp.ad_language AS bp_ad_language, bp.ad_orgbp_id AS BP_AD_OrgBP_ID, bp.ad_org_id AS BP_AD_Org_ID, bp.bpartner_parent_id AS BP_BPartner_Parent_ID, bp.c_bp_group_id AS BP_C_BP_Group_ID, bp.c_dunning_id AS BP_C_Dunning_ID, bp.c_greeting_id AS BP_C_Greeting_ID, bp.c_invoiceschedule_id AS BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS BP_C_PaymentTerm_ID, bp.created AS bp_created, bp.createdby AS BP_CreatedBy, bp.c_taxgroup_id AS BP_C_TaxGroup_ID, bp.deliveryrule AS bp_deliveryrule, bp.deliveryviarule AS bp_deliveryviarule, bp.description AS bp_description, bp.dunninggrace AS bp_dunninggrace, bp.firstsale AS bp_firstsale, bp.flatdiscount AS bp_flatdiscount, bp.freightcostrule AS bp_freightcostrule, bp.invoicerule AS bp_invoicerule, bp.isactive AS bp_isactive,
bp.iscustomer AS bp_iscustomer, bp.isdiscountprinted AS bp_isdiscountprinted, bp.isemployee AS bp_isemployee, bp.ismanufacturer AS bp_ismanufacturer, bp.isonetime AS bp_isonetime, bp.ispotaxexempt AS bp_ispotaxexempt, bp.isprospect AS bp_isprospect, bp.issalesrep AS bp_issalesrep, bp.issummary AS bp_issummary, bp.istaxexempt AS bp_istaxexempt, bp.isvendor AS bp_isvendor, bp.logo_id AS BP_Logo_ID, bp.m_discountschema_id AS BP_M_DiscountSchema_ID, bp.m_pricelist_id AS BP_M_PriceList_ID, bp.naics AS bp_naics, bp.numberemployees AS bp_numberemployees, bp.paymentrule AS bp_paymentrule, bp.paymentrulepo AS bp_paymentrulepo, bp.po_discountschema_id AS BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS BP_PO_PriceList_ID, bp.poreference AS bp_poreference, bp.potentiallifetimevalue AS bp_potentiallifetimevalue, bp.rating AS bp_rating, bp.salesrep_id AS BP_SalesRep_ID, bp.salesvolume AS bp_salesvolume, bp.sendemail AS bp_sendemail, bp.shareofcustomer AS bp_shareofcustomer, bp.shelflifeminpct AS bp_shelflifeminpct, bp.so_creditlimit AS bp_so_creditlimit, bp.socreditstatus AS bp_socreditstatus, bp.so_creditused AS bp_so_creditused, bp.so_description AS bp_so_description, bp.totalopenbalance AS bp_totalopenbalance, bp.updated AS bp_updated, bp.updatedby AS BP_UpdatedBy, bp.url AS bp_url, bpgt.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpgt.name AS c_greeting_name, bpl.ad_org_id AS BP_Location_AD_Org_ID, bpl.c_bpartner_id AS BP_Location_C_BPartner_ID, bpl.created AS bp_location_created, bpl.createdby AS BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS bp_location_fax, bpl.isactive AS bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS bp_location_name, bpl.phone AS bp_location_phone, bpl.phone2 AS bp_location_phone2, bpl.updated AS bp_location_updated,
bpl.updatedby AS BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcgt.isactive AS user_greeting_isactive, bpcg.isfirstnameonly AS user_greeting_isfirstnameonly, bpcgt.name AS user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy,
u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS bpartner_acqusitioncost, ubp.actuallifetimevalue AS bpartner_actuallifetimevalue, ubp.ad_language AS bpartner_ad_language, ubp.ad_orgbp_id AS BPartner_AD_OrgBP_ID, ubp.ad_org_id AS BPartner_AD_Org_ID, ubp.bpartner_parent_id AS BPartner_BPartner_Parent_ID, ubp.c_bp_group_id AS BPartner_C_BP_Group_ID, ubp.c_dunning_id AS BPartner_C_Dunning_ID, ubp.c_greeting_id AS BPartner_C_Greeting_ID, ubp.c_invoiceschedule_id AS BPartner_C_InvoiceSchedule_ID, ubp.c_paymentterm_id AS BPartner_C_PaymentTerm_ID, ubp.created AS bpartner_created, ubp.createdby AS BPartner_CreatedBy, ubp.c_taxgroup_id AS BPartner_C_TaxGroup_ID, ubp.deliveryrule AS bpartner_deliveryrule, ubp.deliveryviarule AS bpartner_deliveryviarule, ubp.description AS bpartner_description, ubp.dunninggrace AS bpartner_dunninggrace, ubp.duns AS bpartner_duns, ubp.firstsale AS bpartner_firstsale, ubp.flatdiscount AS bpartner_flatdiscount, ubp.freightcostrule AS bpartner_freightcostrule, ubp.invoicerule AS bpartner_invoicerule, ubp.isactive AS bpartner_isactive, ubp.iscustomer AS bpartner_iscustomer, ubp.isdiscountprinted AS bpartner_isdiscountprinted, ubp.isemployee AS bpartner_isemployee, ubp.ismanufacturer AS bpartner_ismanufacturer,
ubp.isonetime AS bpartner_isonetime, ubp.ispotaxexempt AS bpartner_ispotaxexempt, ubp.isprospect AS bpartner_isprospect, ubp.issalesrep AS bpartner_issalesrep, ubp.issummary AS bpartner_issummary, ubp.istaxexempt AS bpartner_istaxexempt, ubp.isvendor AS bpartner_isvendor, ubp.logo_id AS BPartner_Logo_ID, ubp.m_discountschema_id AS BPartner_M_DiscountSchema_ID, ubp.m_pricelist_id AS BPartner_M_PriceList_ID, ubp.naics AS bpartner_naics, ubp.name2 AS bpartner_name2, ubp.numberemployees AS bpartner_numberemployees, ubp.paymentrule AS bpartner_paymentrule, ubp.paymentrulepo AS bpartner_paymentrulepo, ubp.po_discountschema_id AS BPartner_PO_DiscountSchema_ID, ubp.po_paymentterm_id AS BPartner_PO_PaymentTerm_ID, ubp.po_pricelist_id AS BPartner_PO_PriceList_ID, ubp.poreference AS bpartner_poreference, ubp.potentiallifetimevalue AS bpartner_potentiallifetimeval, ubp.rating AS bpartner_rating, ubp.referenceno AS bpartner_referenceno, ubp.salesrep_id AS BPartner_SalesRep_ID, ubp.salesvolume AS bpartner_salesvolume, ubp.sendemail AS bpartner_sendemail, ubp.shareofcustomer AS bpartner_shareofcustomer, ubp.shelflifeminpct AS bpartner_shelflifeminpct, ubp.so_creditlimit AS bpartner_so_creditlimit, ubp.socreditstatus AS bpartner_socreditstatus, ubp.so_creditused AS bpartner_so_creditused, ubp.so_description AS bpartner_so_description, ubp.taxid AS bpartner_taxid, ubp.totalopenbalance AS bpartner_totalopenbalance, ubp.updated AS bpartner_updated, ubp.updatedby AS BPartner_UpdatedBy, ubp.url AS bpartner_url, ubp.value AS BPartner_Value, cur.cursymbol AS CurSymbol, cur.description AS cur_description FROM c_invoice i
JOIN c_doctype_trl dt ON i.c_doctype_id = dt.c_doctype_id
JOIN c_paymentterm pt ON i.c_paymentterm_id = pt.c_paymentterm_id
JOIN c_paymentterm_trl ptt ON i.c_paymentterm_id = ptt.c_paymentterm_id AND dt.ad_language = ptt.ad_language
JOIN c_bpartner bp ON i.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id AND dt.ad_language = bpgt.ad_language
JOIN c_bpartner_location bpl ON i.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON i.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_greeting_trl bpcgt ON bpc.c_greeting_id = bpcgt.c_greeting_id AND dt.ad_language = bpcgt.ad_language
JOIN ad_orginfo oi ON i.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON i.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON i.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
LEFT JOIN c_currency_trl cur ON i.c_currency_id = cur.c_currency_id AND dt.ad_language = cur.ad_language
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Invoice_LineTax_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Invoice_ID, C_InvoiceLine_ID, C_Tax_ID, TaxAmt, LineTotalAmt, TaxIndicator, Line, M_Product_ID, QtyInvoiced, QtyEntered, UOMSymbol, Name, Description, DocumentNote, UPC, SKU, ProductValue, ResourceDescription, PriceList, PriceEnteredList, Discount, PriceActual, PriceEntered, LineNetAmt, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_InvoiceLine_AD_OrgTrx_ID, A_Processed, C_Charge_ID, C_OrderLine_ID, C_UOM_ID, IsDescription, c_invoiceline_isprinted, M_InOutLine_ID, M_RMALine_ID, PriceLimit, c_invoiceline_processed, Ref_InvoiceLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, C_InvoiceLine_User1_ID, C_InvoiceLine_User2_ID, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, c_uom_isdefault, c_uom_name, StdPrecision, UOMType, X12DE355, C_Invoice_AD_Org_ID, C_Invoice_AD_OrgTrx_ID, C_Invoice_AD_User_ID, C_Invoice_C_Activity_ID, C_Invoice_C_BPartner_ID, C_BPartner_Location_ID, C_Invoice_C_Campaign_ID, C_Invoice_C_Charge_ID, C_ConversionType_ID, C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, c_invoice_chargeamt, C_Order_ID, C_Payment_ID, C_PaymentTerm_ID, C_Invoice_C_Project_ID, c_invoice_created, C_Invoice_CreatedBy, DateAcct, DateInvoiced, DateOrdered, DatePrinted, c_invoice_description, DocAction, DocStatus, DocumentNo, DunningGrace, GenerateTo, GrandTotal, InvoiceCollectionType, c_invoice_isactive, IsApproved, IsDiscountPrinted, IsInDispute, IsPaid, IsPayScheduleValid, c_invoice_isprinted, c_invoice_isselfservice, IsSOTrx, c_invoice_istaxincluded, IsTransferred, M_PriceList_ID, M_RMA_ID, PaymentRule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SalesRep_ID, SendEMail, TotalLines, c_invoice_updated, C_Invoice_UpdatedBy,
C_Invoice_User1_ID, C_Invoice_User2_ID, C_Tax_AD_Org_ID, AD_Rule_ID, C_Country_ID, C_Region_ID, C_Tax_C_TaxCategory_ID, c_tax_description, c_tax_isactive, c_tax_isdefault, IsDocumentLevel, IsSalesTax, c_tax_issummary, IsTaxExempt, c_tax_name, Parent_Tax_ID, Rate, RequiresTaxCertificate, SOPOType, To_Country_ID, To_Region_ID, ValidFrom, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, M_Product_C_TaxCategory_ID, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Locator_ID, M_Product_M_Prod_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, M_Product_S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, C_Charge_AD_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded, C_BP_Product_AD_Org_ID, C_BP_Product_C_BPartner_ID, c_bp_product_created, C_BP_Product_CreatedBy, c_bp_product_description, c_bp_product_isactive, IsManufacturer, Manufacturer, QualityRating, ShelfLifeMinDays, ShelfLifeMinPct, c_bp_product_updated, C_BP_Product_UpdatedBy, VendorCategory, S_RAssignment_AD_Org_ID, AssignDateFrom, AssignDateTo, s_rassignment_created, s_rassignment_createby, s_rassignment_isactive, IsConfirmed, s_rassignment_name, s_rassignment_qty, S_RAssignment_S_Resource_ID, s_rassignment_updated, S_RAssignment_UpdatedBy, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy,
M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, il.c_tax_id AS C_Tax_ID, il.taxamt AS TaxAmt, il.linetotalamt AS LineTotalAmt, tt.taxindicator AS TaxIndicator, il.line AS Line, pt.m_product_id AS M_Product_ID, CASE WHEN il.qtyinvoiced <> 0 OR il.m_product_id IS NOT NULL THEN il.qtyinvoiced ELSE NULL END AS QtyInvoiced, CASE WHEN il.qtyentered <> 0 OR il.m_product_id IS NOT NULL THEN il.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN il.qtyentered <> 0 OR il.m_product_id IS NOT NULL THEN uomt.uomsymbol ELSE NULL END AS UOMSymbol, COALESCE(ct.name, (pt.name || COALESCE(productattribute(il.m_attributesetinstance_id), '')), il.description) AS Name, CASE WHEN COALESCE(ct.name, pt.name) IS NOT NULL THEN il.description ELSE NULL END AS Description, pt.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, COALESCE(pp.vendorproductno, p.value) AS ProductValue, ra.description AS ResourceDescription, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist <> 0 THEN il.pricelist ELSE NULL END AS PriceList, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist <> 0 AND il.qtyentered <> 0 THEN il.pricelist * il.qtyinvoiced / il.qtyentered ELSE NULL END AS PriceEnteredList, CASE WHEN i.isdiscountprinted = 'Y' AND il.pricelist > il.priceactual AND il.pricelist <> 0 THEN (il.pricelist - il.priceactual) / il.pricelist * 100 ELSE NULL END AS Discount, CASE WHEN il.priceactual <> 0 OR il.m_product_id IS NOT NULL THEN il.priceactual ELSE NULL END AS PriceActual, CASE WHEN il.priceentered <> 0 OR il.m_product_id IS NOT NULL THEN il.priceentered ELSE NULL END AS PriceEntered,
CASE WHEN il.linenetamt <> 0 OR il.m_product_id IS NOT NULL THEN il.linenetamt ELSE NULL END AS LineNetAmt, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, pt.description AS ProductDescription, p.imageurl AS ImageURL, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID, il.ad_orgtrx_id AS C_InvoiceLine_AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_charge_id AS C_Charge_ID, il.c_orderline_id AS C_OrderLine_ID, il.c_uom_id AS C_UOM_ID, il.isdescription AS IsDescription, il.isprinted AS c_invoiceline_isprinted, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS M_RMALine_ID, il.pricelimit AS PriceLimit, il.processed AS c_invoiceline_processed, il.ref_invoiceline_id AS Ref_InvoiceLine_ID, il.rramt AS RRAmt, il.rrstartdate AS RRStartDate, il.s_resourceassignment_id AS S_ResourceAssignment_ID, il.user1_id AS C_InvoiceLine_User1_ID, il.user2_id AS C_InvoiceLine_User2_ID, uomt.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uomt.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_orgtrx_id AS C_Invoice_AD_OrgTrx_ID, i.ad_user_id AS C_Invoice_AD_User_ID, i.c_activity_id AS C_Invoice_C_Activity_ID, i.c_bpartner_id AS C_Invoice_C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_campaign_id AS C_Invoice_C_Campaign_ID, i.c_charge_id AS C_Invoice_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID,
i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS c_invoice_chargeamt, i.c_order_id AS C_Order_ID, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_project_id AS C_Invoice_C_Project_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateacct AS DateAcct, i.dateinvoiced AS DateInvoiced, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS c_invoice_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.grandtotal AS GrandTotal, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispaid AS IsPaid, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS c_invoice_isprinted, i.isselfservice AS c_invoice_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS c_invoice_istaxincluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS PaymentRule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS c_invoice_updated, i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS C_Invoice_User1_ID, i.user2_id AS C_Invoice_User2_ID, tt.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, tt.description AS c_tax_description, tt.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS IsTaxExempt, tt.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate,
t.requirestaxcertificate AS RequiresTaxCertificate, t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS To_Region_ID, t.validfrom AS ValidFrom, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, pt.updated AS m_product_updated, pt.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight,
ct.ad_org_id AS C_Charge_AD_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID, c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, ct.description AS c_charge_description, ct.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded, pp.ad_org_id AS C_BP_Product_AD_Org_ID, pp.c_bpartner_id AS C_BP_Product_C_BPartner_ID, pp.created AS c_bp_product_created, pp.createdby AS C_BP_Product_CreatedBy, pp.description AS c_bp_product_description, pp.isactive AS c_bp_product_isactive, pp.ismanufacturer AS IsManufacturer, pp.manufacturer AS Manufacturer, pp.qualityrating AS QualityRating, pp.shelflifemindays AS ShelfLifeMinDays, pp.shelflifeminpct AS ShelfLifeMinPct, pp.updated AS c_bp_product_updated, pp.updatedby AS C_BP_Product_UpdatedBy, pp.vendorcategory AS VendorCategory, ra.ad_org_id AS S_RAssignment_AD_Org_ID, ra.assigndatefrom AS AssignDateFrom, ra.assigndateto AS AssignDateTo, ra.created AS s_rassignment_created, ra.createdby AS s_rassignment_createby, ra.isactive AS s_rassignment_isactive, ra.isconfirmed AS IsConfirmed, ra.name AS s_rassignment_name, ra.qty AS s_rassignment_qty, ra.s_resource_id AS S_RAssignment_S_Resource_ID, ra.updated AS s_rassignment_updated, ra.updatedby AS S_RAssignment_UpdatedBy, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM c_invoiceline il
JOIN c_uom uom ON il.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON il.c_uom_id = uomt.c_uom_id
JOIN c_invoice i ON il.c_invoice_id = i.c_invoice_id
LEFT JOIN c_tax t ON il.c_tax_id = t.c_tax_id
LEFT JOIN c_tax_trl tt ON il.c_tax_id = tt.c_tax_id AND uomt.ad_language = tt.ad_language
LEFT JOIN m_product p ON il.m_product_id = p.m_product_id
LEFT JOIN c_charge c ON il.c_charge_id = c.c_charge_id
LEFT JOIN c_charge_trl ct ON il.c_charge_id = ct.c_charge_id AND uomt.ad_language = ct.ad_language
LEFT JOIN c_bpartner_product pp ON il.m_product_id = pp.m_product_id AND i.c_bpartner_id = pp.c_bpartner_id
LEFT JOIN m_product_trl pt ON il.m_product_id = pt.m_product_id AND uomt.ad_language = pt.ad_language
LEFT JOIN s_resourceassignment ra ON il.s_resourceassignment_id = ra.s_resourceassignment_id
LEFT JOIN m_attributesetinstance asi ON il.m_attributesetinstance_id = asi.m_attributesetinstance_id UNION SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, il.c_tax_id AS C_Tax_ID, il.taxamt AS TaxAmt, il.linetotalamt AS LineTotalAmt, tt.taxindicator AS TaxIndicator, il.line + bl.line / 100 AS Line, pt.m_product_id AS M_Product_ID, CASE WHEN bl.isqtypercentage = 'N' THEN il.qtyinvoiced * bl.qtybom ELSE il.qtyinvoiced * (bl.qtybatch / 100) END AS QtyInvoiced, CASE WHEN bl.isqtypercentage = 'N' THEN il.qtyentered * bl.qtybom ELSE il.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uomt.uomsymbol AS UOMSymbol, COALESCE(pt.name, p.name) AS Name, b.description AS Description, COALESCE(pt.documentnote, p.documentnote) AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, il.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, pt.description AS ProductDescription, p.imageurl AS ImageURL, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID, il.ad_orgtrx_id AS C_InvoiceLine_AD_OrgTrx_ID, il.a_processed AS A_Processed, il.c_charge_id AS C_Charge_ID, il.c_orderline_id AS C_OrderLine_ID, il.c_uom_id AS C_UOM_ID, il.isdescription AS IsDescription, il.isprinted AS c_invoiceline_isprinted, il.m_inoutline_id AS M_InOutLine_ID, il.m_rmaline_id AS M_RMALine_ID, il.pricelimit AS PriceLimit,
il.processed AS c_invoiceline_processed, il.ref_invoiceline_id AS Ref_InvoiceLine_ID, il.rramt AS RRAmt, il.rrstartdate AS RRStartDate, il.s_resourceassignment_id AS S_ResourceAssignment_ID, il.user1_id AS C_InvoiceLine_User1_ID, il.user2_id AS C_InvoiceLine_User2_ID, uomt.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uomt.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID,
NULL AS C_Invoice_User2_ID, tt.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, tt.description AS c_tax_description, tt.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS IsTaxExempt, tt.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate, t.requirestaxcertificate AS RequiresTaxCertificate, t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS To_Region_ID, t.validfrom AS ValidFrom, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing,
p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy FROM pp_product_bom b
JOIN c_invoiceline il ON b.m_product_id = il.m_product_id
JOIN m_product bp ON bp.m_product_id = il.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.isinvoiceprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON bl.m_product_id = p.m_product_id
JOIN c_uom uom ON p.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON p.c_uom_id = uomt.c_uom_id
JOIN m_product_trl pt ON bl.m_product_id = pt.m_product_id AND uomt.ad_language = pt.ad_language
LEFT JOIN c_tax t ON il.c_tax_id = t.c_tax_id
LEFT JOIN c_tax_trl tt ON il.c_tax_id = tt.c_tax_id AND uomt.ad_language = tt.ad_language
LEFT JOIN m_attributesetinstance asi ON il.m_attributesetinstance_id = asi.m_attributesetinstance_id UNION SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.isactive AS IsActive, il.created AS Created, il.createdby AS CreatedBy, il.updated AS Updated, il.updatedby AS UpdatedBy, l.ad_language AS AD_Language, il.c_invoice_id AS C_Invoice_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, NULL AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, NULL AS TaxIndicator, il.line AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, il.description AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID,
NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL,
NULL AS Discontinued, NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID,
NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoiceline il,
ad_language l WHERE il.c_uom_id IS NULL AND l.isbaselanguage = 'N' AND l.issystemlanguage = 'Y' UNION SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, l.ad_language AS AD_Language, i.c_invoice_id AS C_Invoice_ID, NULL AS C_InvoiceLine_ID, NULL AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, NULL AS TaxIndicator, 999998 AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, NULL AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID, NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID,
NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL, NULL AS Discontinued,
NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby, NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated,
NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoice i,
ad_language l WHERE l.isbaselanguage = 'N' AND l.issystemlanguage = 'Y' UNION SELECT it.ad_client_id AS AD_Client_ID, it.ad_org_id AS AD_Org_ID, it.isactive AS IsActive, it.created AS Created, it.createdby AS CreatedBy, it.updated AS Updated, it.updatedby AS UpdatedBy, tt.ad_language AS AD_Language, it.c_invoice_id AS C_Invoice_ID, NULL AS C_InvoiceLine_ID, it.c_tax_id AS C_Tax_ID, NULL AS TaxAmt, NULL AS LineTotalAmt, tt.taxindicator AS TaxIndicator, 999999 AS Line, NULL AS M_Product_ID, NULL AS QtyInvoiced, NULL AS QtyEntered, NULL AS UOMSymbol, tt.name AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, CASE WHEN it.istaxincluded = 'Y' THEN it.taxamt ELSE it.taxbaseamt END AS PriceActual, CASE WHEN it.istaxincluded = 'Y' THEN it.taxamt ELSE it.taxbaseamt END AS PriceEntered, CASE WHEN it.istaxincluded = 'Y' THEN NULL ELSE it.taxamt END AS LineNetAmt, NULL AS M_AttributeSetInstance_ID, NULL AS M_AttributeSet_ID, NULL AS SerNo, NULL AS Lot, NULL AS M_Lot_ID, NULL AS GuaranteeDate, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_InvoiceLine_AD_OrgTrx_ID, NULL AS A_Processed, NULL AS C_Charge_ID, NULL AS C_OrderLine_ID, NULL AS C_UOM_ID, NULL AS IsDescription, NULL AS c_invoiceline_isprinted, NULL AS M_InOutLine_ID, NULL AS M_RMALine_ID, NULL AS PriceLimit, NULL AS c_invoiceline_processed, NULL AS Ref_InvoiceLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_InvoiceLine_User1_ID, NULL AS C_InvoiceLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS c_uom_isdefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Invoice_AD_Org_ID,
NULL AS C_Invoice_AD_OrgTrx_ID, NULL AS C_Invoice_AD_User_ID, NULL AS C_Invoice_C_Activity_ID, NULL AS C_Invoice_C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS C_Invoice_C_Campaign_ID, NULL AS C_Invoice_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS C_DunningLevel_ID, NULL AS c_invoice_chargeamt, NULL AS C_Order_ID, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_Invoice_C_Project_ID, NULL AS c_invoice_created, NULL AS C_Invoice_CreatedBy, NULL AS DateAcct, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS DatePrinted, NULL AS c_invoice_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DunningGrace, NULL AS GenerateTo, NULL AS GrandTotal, NULL AS InvoiceCollectionType, NULL AS c_invoice_isactive, NULL AS IsApproved, NULL AS IsDiscountPrinted, NULL AS IsInDispute, NULL AS IsPaid, NULL AS IsPayScheduleValid, NULL AS c_invoice_isprinted, NULL AS c_invoice_isselfservice, NULL AS IsSOTrx, NULL AS c_invoice_istaxincluded, NULL AS IsTransferred, NULL AS M_PriceList_ID, NULL AS M_RMA_ID, NULL AS PaymentRule, NULL AS POReference, NULL AS Posted, NULL AS ProcessedOn, NULL AS Processing, NULL AS Ref_Invoice_ID, NULL AS Reversal_ID, NULL AS SalesRep_ID, NULL AS SendEMail, NULL AS TotalLines, NULL AS c_invoice_updated, NULL AS C_Invoice_UpdatedBy, NULL AS C_Invoice_User1_ID, NULL AS C_Invoice_User2_ID, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS IsTaxExempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS To_Region_ID, NULL AS ValidFrom, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created,
NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS M_Product_C_TaxCategory_ID, NULL AS DescriptionURL, NULL AS Discontinued, NULL AS DiscontinuedAt, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS IsDropShip, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_Product_M_AttributeSet_ID, NULL AS M_Product_M_ASI_ID, NULL AS M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_M_Prod_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS M_Product_S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS Volume, NULL AS Weight, NULL AS C_Charge_AD_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BP_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bp_product_created, NULL AS C_BP_Product_CreatedBy, NULL AS c_bp_product_description, NULL AS c_bp_product_isactive, NULL AS IsManufacturer, NULL AS Manufacturer, NULL AS QualityRating, NULL AS ShelfLifeMinDays, NULL AS ShelfLifeMinPct, NULL AS c_bp_product_updated, NULL AS C_BP_Product_UpdatedBy, NULL AS VendorCategory, NULL AS S_RAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_rassignment_created, NULL AS s_rassignment_createby,
NULL AS s_rassignment_isactive, NULL AS IsConfirmed, NULL AS s_rassignment_name, NULL AS s_rassignment_qty, NULL AS S_RAssignment_S_Resource_ID, NULL AS s_rassignment_updated, NULL AS S_RAssignment_UpdatedBy, NULL AS M_ASI_AD_Org_ID, NULL AS m_asi_created, NULL AS M_ASI_CreatedBy, NULL AS M_ASI_Description, NULL AS m_asi_isactive, NULL AS m_asi_updated, NULL AS M_ASI_UpdatedBy FROM c_invoicetax it
JOIN c_tax_trl tt ON it.c_tax_id = tt.c_tax_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_InvoiceLine_v(AD_Client_ID, AD_Org_ID, C_InvoiceLine_ID, C_Invoice_ID, SalesRep_ID, C_BPartner_ID, M_Product_ID, DocumentNo, DateInvoiced, DateAcct, IsSOTrx, DocStatus, LineNetAmt, LineListAmt, LineLimitAmt, LineDiscountAmt, LineOverLimitAmt, QtyInvoiced, QtyEntered, Line, C_OrderLine_ID, C_UOM_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID) AS SELECT il.ad_client_id AS AD_Client_ID, il.ad_org_id AS AD_Org_ID, il.c_invoiceline_id AS C_InvoiceLine_ID, i.c_invoice_id AS C_Invoice_ID, i.salesrep_id AS SalesRep_ID, i.c_bpartner_id AS C_BPartner_ID, il.m_product_id AS M_Product_ID, i.documentno AS DocumentNo, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, i.issotrx AS IsSOTrx, i.docstatus AS DocStatus, currencyround(i.multiplier * il.linenetamt, i.c_currency_id, 'N') AS LineNetAmt, currencyround(i.multiplier * il.pricelist * il.qtyinvoiced, i.c_currency_id, 'N') AS LineListAmt, CASE WHEN COALESCE(il.pricelimit, 0) = 0 THEN currencyround(i.multiplier * il.linenetamt, i.c_currency_id, 'N') ELSE currencyround(i.multiplier * il.pricelimit * il.qtyinvoiced, i.c_currency_id, 'N') END AS LineLimitAmt, currencyround(i.multiplier * il.pricelist * il.qtyinvoiced - il.linenetamt, i.c_currency_id, 'N') AS LineDiscountAmt, CASE WHEN COALESCE(il.pricelimit, 0) = 0 THEN 0 ELSE currencyround(i.multiplier * il.linenetamt - il.pricelimit * il.qtyinvoiced, i.c_currency_id, 'N') END AS LineOverLimitAmt, il.qtyinvoiced AS QtyInvoiced, il.qtyentered AS QtyEntered, il.line AS Line, il.c_orderline_id AS C_OrderLine_ID, il.c_uom_id AS C_UOM_ID, il.c_campaign_id AS C_Campaign_ID, il.c_project_id AS C_Project_ID, il.c_activity_id AS C_Activity_ID, il.c_projectphase_id AS C_ProjectPhase_ID, il.c_projecttask_id AS C_ProjectTask_ID FROM c_invoice_v i,
c_invoiceline il WHERE i.c_invoice_id = il.c_invoice_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Invoice_v(C_Invoice_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, IsSOTrx, DocumentNo, DocStatus, DocAction, Processing, Processed, C_DocType_ID, C_DocTypeTarget_ID, C_Order_ID, Description, IsApproved, IsTransferred, SalesRep_ID, DateInvoiced, DatePrinted, DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, POReference, DateOrdered, C_Currency_ID, C_ConversionType_ID, PaymentRule, C_PaymentTerm_ID, C_Charge_ID, M_PriceList_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, IsPrinted, IsDiscountPrinted, IsPaid, IsInDispute, IsPayScheduleValid, C_InvoicePaySchedule_ID, InvoiceCollectionType, DunningGrace, ChargeAmt, TotalLines, GrandTotal, Multiplier, MultiplierAP, DocBaseType, DueDate, AD_OrgTrx_ID, C_Payment_ID, IsSelfService, Posted, ProcessedOn, Reversal_ID) AS SELECT i.c_invoice_id AS C_Invoice_ID, i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.docaction AS DocAction, i.processing AS Processing, i.processed AS Processed, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_order_id AS C_Order_ID, i.description AS Description, i.isapproved AS IsApproved, i.istransferred AS IsTransferred, i.salesrep_id AS SalesRep_ID, i.dateinvoiced AS DateInvoiced, i.dateprinted AS DatePrinted, i.dateacct AS DateAcct, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.ad_user_id AS AD_User_ID, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.paymentrule AS PaymentRule, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_charge_id AS C_Charge_ID, i.m_pricelist_id AS M_PriceList_ID, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID,
i.c_activity_id AS C_Activity_ID, i.isprinted AS IsPrinted, i.isdiscountprinted AS IsDiscountPrinted, i.ispaid AS IsPaid, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, NULL AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.dunninggrace AS DunningGrace, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.chargeamt * '-1' ELSE i.chargeamt END AS ChargeAmt, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.totallines * '-1' ELSE i.totallines END AS TotalLines, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.grandtotal * '-1' ELSE i.grandtotal END AS GrandTotal, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN -1.0 ELSE 1.0 END AS Multiplier, CASE WHEN charat(d.docbasetype, 2) = 'P' THEN -1.0 ELSE 1.0 END AS MultiplierAP, d.docbasetype AS DocBaseType, paymenttermduedate(i.c_paymentterm_id, i.dateinvoiced) AS DueDate, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.c_payment_id AS C_Payment_ID, i.isselfservice AS IsSelfService, i.posted AS Posted, i.processedon AS ProcessedOn, i.reversal_id AS Reversal_ID FROM c_invoice i
JOIN c_doctype d ON i.c_doctype_id = d.c_doctype_id WHERE i.ispayschedulevalid <> 'Y' UNION SELECT i.c_invoice_id AS C_Invoice_ID, i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.docaction AS DocAction, i.processing AS Processing, i.processed AS Processed, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_order_id AS C_Order_ID, i.description AS Description, i.isapproved AS IsApproved, i.istransferred AS IsTransferred, i.salesrep_id AS SalesRep_ID, i.dateinvoiced AS DateInvoiced, i.dateprinted AS DatePrinted, i.dateacct AS DateAcct, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.ad_user_id AS AD_User_ID, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.paymentrule AS PaymentRule, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_charge_id AS C_Charge_ID, i.m_pricelist_id AS M_PriceList_ID, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.isprinted AS IsPrinted, i.isdiscountprinted AS IsDiscountPrinted, i.ispaid AS IsPaid, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, ips.c_invoicepayschedule_id AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.dunninggrace AS DunningGrace, NULL AS ChargeAmt, NULL AS TotalLines, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN ips.dueamt * '-1' ELSE ips.dueamt END AS GrandTotal, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN -1 ELSE 1 END AS Multiplier, CASE WHEN charat(d.docbasetype, 2) = 'P' THEN -1 ELSE 1 END AS MultiplierAP, d.docbasetype AS DocBaseType, ips.duedate AS DueDate, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.c_payment_id AS C_Payment_ID,
i.isselfservice AS IsSelfService, i.posted AS Posted, i.processedon AS ProcessedOn, i.reversal_id AS Reversal_ID FROM c_invoice i
JOIN c_doctype d ON i.c_doctype_id = d.c_doctype_id
JOIN c_invoicepayschedule ips ON i.c_invoice_id = ips.c_invoice_id WHERE i.ispayschedulevalid = 'Y' AND ips.isvalid = 'Y'
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Invoice_v1(C_Invoice_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, IsSOTrx, DocumentNo, DocStatus, DocAction, Processing, Processed, C_DocType_ID, C_DocTypeTarget_ID, C_Order_ID, Description, IsApproved, IsTransferred, SalesRep_ID, DateInvoiced, DatePrinted, DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, POReference, DateOrdered, C_Currency_ID, C_ConversionType_ID, PaymentRule, C_PaymentTerm_ID, C_Charge_ID, M_PriceList_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, IsPrinted, IsDiscountPrinted, IsPaid, IsInDispute, IsPayScheduleValid, C_InvoicePaySchedule_ID, InvoiceCollectionType, ChargeAmt, TotalLines, GrandTotal, Multiplier, MultiplierAP, DocBaseType) AS SELECT i.c_invoice_id AS C_Invoice_ID, i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, i.issotrx AS IsSOTrx, i.documentno AS DocumentNo, i.docstatus AS DocStatus, i.docaction AS DocAction, i.processing AS Processing, i.processed AS Processed, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_order_id AS C_Order_ID, i.description AS Description, i.isapproved AS IsApproved, i.istransferred AS IsTransferred, i.salesrep_id AS SalesRep_ID, i.dateinvoiced AS DateInvoiced, i.dateprinted AS DatePrinted, i.dateacct AS DateAcct, i.c_bpartner_id AS C_BPartner_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.ad_user_id AS AD_User_ID, i.poreference AS POReference, i.dateordered AS DateOrdered, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.paymentrule AS PaymentRule, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_charge_id AS C_Charge_ID, i.m_pricelist_id AS M_PriceList_ID, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID, i.isprinted AS IsPrinted, i.isdiscountprinted AS IsDiscountPrinted,
i.ispaid AS IsPaid, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, NULLIF(1.0,1.0) AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.chargeamt * '-1' ELSE i.chargeamt END AS ChargeAmt, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.totallines * '-1' ELSE i.totallines END AS TotalLines, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN i.grandtotal * '-1' ELSE i.grandtotal END AS GrandTotal, CASE WHEN charat(d.docbasetype, 3) = 'C' THEN -1 ELSE 1 END AS Multiplier, CASE WHEN charat(d.docbasetype, 2) = 'P' THEN -1 ELSE 1 END AS MultiplierAP, d.docbasetype AS DocBaseType FROM c_invoice i
JOIN c_doctype d ON i.c_doctype_id = d.c_doctype_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Order_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Order_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, SalesRep_ID, SalesRep_Name, DateOrdered, DatePromised, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, Bill_BPValue, Bill_BPTaxID, Bill_Name, Bill_Name2, Bill_Title, Bill_Phone, Bill_ContactName, Bill_C_Location_ID, Description, POReference, C_Currency_ID, PaymentTerm, PaymentTermNote, C_Charge_ID, ChargeAmt, TotalLines, GrandTotal, AmtInWords, M_PriceList_ID, IsTaxIncluded, Volume, Weight, C_Campaign_ID, C_Project_ID, C_Activity_ID, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, InvoiceRule, Logo_ID, AD_OrgTrx_ID, AD_User_ID, AmountRefunded, AmountTendered, C_BPartner_Location_ID, C_CashLine_ID, C_CashPlanLine_ID, C_ConversionType_ID, C_DocTypeTarget_ID, CopyFrom, C_Payment_ID, C_PaymentTerm_ID, C_POS_ID, DateAcct, DatePrinted, DocAction, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, FreightAmt, FreightCostRule, IsApproved, IsCreditApproved, IsDelivered, IsDiscountPrinted, IsDropShip, IsInvoiced, IsPayScheduleValid, IsPrinted, IsSelected, IsSelfService, IsTransferred, Link_Order_ID, M_FreightCategory_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, PaymentRule, Posted, Processed, ProcessedOn, PromotionCode, Ref_Order_ID, SendEMail, User1_ID, User2_ID, M_Warehouse_AD_Org_ID, m_warehouse_description, m_warehouse_isactive, IsDisallowNegativeInv, IsInTransit, M_WarehouseSource_ID, m_warehouse_name, ReplenishmentClass, Separator, m_warehouse_value, C_PaymentTerm_AD_Org_ID, AfterDelivery, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset,
GraceDays, c_paymentterm_isactive, IsDefault, IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, m_paymentterm_value, bp_acqusitioncost, bp_actuallifetimevalue, bp_ad_language, BP_AD_OrgBP_ID, BP_AD_Org_ID, BP_BPartner_Parent_ID, BP_C_BP_Group_ID, BP_C_Dunning_ID, BP_C_Greeting_ID, BP_C_InvoiceSchedule_ID, BP_C_PaymentTerm_ID, bp_created, BP_CreatedBy, BP_C_TaxGroup_ID, bp_deliveryrule, bp_deliveryviarule, bp_description, bp_dunninggrace, bp_firstsale, bp_flatdiscount, bp_freightcostrule, bp_invoicerule, bp_isactive, bp_iscustomer, bp_isdiscountprinted, bp_isemployee, bp_ismanufacturer, bp_isonetime, bp_ispotaxexempt, bp_isprospect, bp_issalesrep, bp_issummary, bp_istaxexempt, bp_isvendor, BP_Logo_ID, BP_M_DiscountSchema_ID, BP_M_PriceList_ID, bp_numberemployees, bp_paymentrule, bp_paymentrulepo, BP_PO_DiscountSchema_ID, BP_PO_PaymentTerm_ID, BP_PO_PriceList_ID, bp_poreference, bp_potentiallifetimevalue, bp_rating, BP_SalesRep_ID, bp_salesvolume, bp_sendemail, bp_shareofcustomer, bp_shelflifeminpct, bp_so_creditlimit, bp_socreditstatus, bp_so_creditused, bp_so_description, bp_totalopenbalance, bp_updated, BP_UpdatedBy, bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, BP_Location_AD_Org_ID, BP_Location_C_BPartner_ID, bp_location_created, BP_Location_CreatedBy, BP_Location_C_SalesRegion_ID, bp_location_fax, bp_location_isactive, bp_location_isbillto, bp_location_isdn, bp_location_ispayfrom, bp_location_isremitto, bp_location_isshipto, bp_location_name, bp_location_phone, bp_location_phone2, bp_location_updated, BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments,
ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, SalesRep_AD_Org_ID, SalesRep_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bp_acqusitioncost, salesrep_bp_actuallifetimeval, salesrep_bp_ad_language, SalesRep_BP_AD_OrgBP_ID, SalesRep_BP_AD_Org_ID, SalesRep_BP_BPartner_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Dunning_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_C_InvoiceSched_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bp_created, SalesRep_BP_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bp_deliveryrule, salesrep_bp_deliveryviarule, salesrep_bp_description, salesrep_bp_dunninggrace, salesrep_bp_duns, salesrep_bp_firstsale, salesrep_bp_flatdiscount, salesrep_bp_freightcostrule, salesrep_bp_invoicerule, salesrep_bp_isactive, salesrep_bp_iscustomer, salesrep_bp_isdiscountprinted, salesrep_bp_isemployee, salesrep_bp_ismanufacturer, salesrep_bp_isonetime, salesrep_bp_ispotaxexempt, salesrep_bp_isprospect, salesrep_bp_issalesrep, salesrep_bp_issummary, salesrep_bp_istaxexempt,
salesrep_bp_isvendor, SalesRep_BP_Logo_ID, SalesRep_BP_M_DiscountSchm_ID, SalesRep_BP_M_PriceList_ID, salesrep_bp_naics, salesrep_bp_name2, salesrep_bp_numberemployees, salesrep_bp_paymentrule, salesrep_bp_paymentrulepo, SalesRep_BP_PO_DiscountSchm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bp_poreference, salesrep_bp_potentiallifetime, salesrep_bp_rating, salesrep_bp_referenceno, SalesRep_BP_SalesRep_ID, salesrep_bp_salesvolume, salesrep_bp_sendemail, salesrep_bp_shareofcustomer, salesrep_bp_shelflifeminpct, salesrep_bp_so_creditlimit, salesrep_bp_socreditstatus, salesrep_bp_so_creditused, salesrep_bp_so_description, salesrep_bp_taxid, salesrep_bp_totalopenbalance, salesrep_bp_updated, SalesRep_BP_UpdatedBy, salesrep_bp_url, salesrep_bp_value, bill_bp_acqusitioncost, bill_bp_actuallifetimevalue, bill_bp_ad_language, Bill_BP_AD_OrgBP_ID, Bill_BP_AD_Org_ID, Bill_BP_BPartner_Parent_ID, Bill_BP_C_BP_Group_ID, Bill_BP_C_Dunning_ID, Bill_BP_C_Greeting_ID, Bill_BP_C_InvoiceSchedule_ID, Bill_BP_C_PaymentTerm_ID, bill_bp_created, Bill_BP_CreatedBy, Bill_BP_C_TaxGroup_ID, bill_bp_deliveryrule, bill_bp_deliveryviarule, bill_bp_description, bill_bp_dunninggrace, bill_bp_duns, bill_bp_firstsale, bill_bp_flatdiscount, bill_bp_freightcostrule, bill_bp_invoicerule, bill_bp_isactive, bill_bp_iscustomer, bill_bp_isdiscountprinted, bill_bp_isemployee, bill_bp_ismanufacturer, bill_bp_isonetime, bill_bp_ispotaxexempt, bill_bp_isprospect, bill_bp_issalesrep, bill_bp_issummary, bill_bp_istaxexempt, bill_bp_isvendor, Bill_BP_Logo_ID, Bill_BP_M_DiscountSchema_ID, Bill_BP_M_PriceList_ID, bill_bp_naics, bill_bp_numberemployees, bill_bp_paymentrule, bill_bp_paymentrulepo, Bill_BP_PO_DiscountSchema_ID, Bill_BP_PO_PaymentTerm_ID, Bill_BP_PO_PriceList_ID, bill_bp_poreference, bill_bp_potentiallifetimevalue, bill_bp_rating, bill_bp_referenceno, Bill_BP_SalesRep_ID, bill_bp_salesvolume, bill_bp_sendemail, bill_bp_shareofcustomer, bill_bp_shelflifeminpct,
bill_bp_so_creditlimit, bill_bp_socreditstatus, bill_bp_so_creditused, bill_bp_so_description, bill_bp_totalopenbalance, bill_bp_updated, Bill_BP_UpdatedBy, bill_bp_url, Bill_BP_Location_AD_Org_ID, Bill_BP_Location_C_BPartner_ID, bill_bp_location_created, Bill_BP_Location_CreatedBy, Bill_BP_Location_C_SalesReg_ID, bill_bp_location_fax, bill_bp_location_isactive, bill_bp_location_isbillto, bill_bp_location_isdn, bill_bp_location_ispayfrom, bill_bp_location_isremitto, bill_bp_location_isshipto, bill_bp_location_name, bill_bp_location_phone, bill_bp_location_phone2, bill_bp_location_updated, Bill_BP_Location_UpdatedBy, Bill_User_AD_Org_ID, Bill_User_AD_OrgTrx_ID, bill_user_ad_birthday, Bill_User_C_BPartner_ID, Bill_User_C_BP_Location_ID, Bill_User_C_Greeting_ID, bill_user_comments, bill_user_created, Bill_User_CreatedBy, bill_user_description, bill_user_email, bill_user_fax, bill_user_isactive, bill_user_lastcontact, bill_user_lastresult, bill_user_phone2, Bill_User_Supervisor_ID, bill_user_updated, Bill_User_UpdatedBy, bill_user_value, CurSymbol, cur_description) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, o.c_order_id AS C_Order_ID, o.issotrx AS IsSOTrx, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, o.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcgt.greeting AS BPContactGreeting,
bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, o.bill_bpartner_id AS Bill_BPartner_ID, o.bill_location_id AS Bill_Location_ID, o.bill_user_id AS Bill_User_ID, bbp.value AS Bill_BPValue, bbp.taxid AS Bill_BPTaxID, bbp.name AS Bill_Name, bbp.name2 AS Bill_Name2, bbpc.title AS Bill_Title, bbpc.phone AS Bill_Phone, NULLIF(bbpc.name, bbp.name) AS Bill_ContactName, bbpl.c_location_id AS Bill_C_Location_ID, o.description AS Description, o.poreference AS POReference, o.c_currency_id AS C_Currency_ID, ptt.name AS PaymentTerm, ptt.documentnote AS PaymentTermNote, o.c_charge_id AS C_Charge_ID, o.chargeamt AS ChargeAmt, o.totallines AS TotalLines, o.grandtotal AS GrandTotal, o.grandtotal AS AmtInWords, o.m_pricelist_id AS M_PriceList_ID, o.istaxincluded AS IsTaxIncluded, o.volume AS Volume, o.weight AS Weight, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.m_shipper_id AS M_Shipper_ID, o.deliveryrule AS DeliveryRule, o.deliveryviarule AS DeliveryViaRule, o.priorityrule AS PriorityRule, o.invoicerule AS InvoiceRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, o.ad_orgtrx_id AS AD_OrgTrx_ID, o.ad_user_id AS AD_User_ID, o.amountrefunded AS AmountRefunded, o.amounttendered AS AmountTendered, o.c_bpartner_location_id AS C_BPartner_Location_ID, o.c_cashline_id AS C_CashLine_ID, o.c_cashplanline_id AS C_CashPlanLine_ID, o.c_conversiontype_id AS C_ConversionType_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.copyfrom AS CopyFrom, o.c_payment_id AS C_Payment_ID, o.c_paymentterm_id AS C_PaymentTerm_ID, o.c_pos_id AS C_POS_ID, o.dateacct AS DateAcct, o.dateprinted AS DatePrinted, o.docaction AS DocAction, o.dropship_bpartner_id AS DropShip_BPartner_ID, o.dropship_location_id AS DropShip_Location_ID, o.dropship_user_id AS DropShip_User_ID, o.freightamt AS FreightAmt,
o.freightcostrule AS FreightCostRule, o.isapproved AS IsApproved, o.iscreditapproved AS IsCreditApproved, o.isdelivered AS IsDelivered, o.isdiscountprinted AS IsDiscountPrinted, o.isdropship AS IsDropShip, o.isinvoiced AS IsInvoiced, o.ispayschedulevalid AS IsPayScheduleValid, o.isprinted AS IsPrinted, o.isselected AS IsSelected, o.isselfservice AS IsSelfService, o.istransferred AS IsTransferred, o.link_order_id AS Link_Order_ID, o.m_freightcategory_id AS M_FreightCategory_ID, o.ordertype AS OrderType, o.pay_bpartner_id AS Pay_BPartner_ID, o.pay_location_id AS Pay_Location_ID, o.paymentrule AS PaymentRule, o.posted AS Posted, o.processed AS Processed, o.processedon AS ProcessedOn, o.promotioncode AS PromotionCode, o.ref_order_id AS Ref_Order_ID, o.sendemail AS SendEMail, o.user1_id AS User1_ID, o.user2_id AS User2_ID, wh.ad_org_id AS M_Warehouse_AD_Org_ID, wh.description AS m_warehouse_description, wh.isactive AS m_warehouse_isactive, wh.isdisallownegativeinv AS IsDisallowNegativeInv, wh.isintransit AS IsInTransit, wh.m_warehousesource_id AS M_WarehouseSource_ID, wh.name AS m_warehouse_name, wh.replenishmentclass AS ReplenishmentClass, wh.separator AS Separator, wh.value AS m_warehouse_value, ptt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, ptt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, ptt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS m_paymentterm_value, bp.acqusitioncost AS bp_acqusitioncost, bp.actuallifetimevalue AS bp_actuallifetimevalue, bp.ad_language AS bp_ad_language,
bp.ad_orgbp_id AS BP_AD_OrgBP_ID, bp.ad_org_id AS BP_AD_Org_ID, bp.bpartner_parent_id AS BP_BPartner_Parent_ID, bp.c_bp_group_id AS BP_C_BP_Group_ID, bp.c_dunning_id AS BP_C_Dunning_ID, bp.c_greeting_id AS BP_C_Greeting_ID, bp.c_invoiceschedule_id AS BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS BP_C_PaymentTerm_ID, bp.created AS bp_created, bp.createdby AS BP_CreatedBy, bp.c_taxgroup_id AS BP_C_TaxGroup_ID, bp.deliveryrule AS bp_deliveryrule, bp.deliveryviarule AS bp_deliveryviarule, bp.description AS bp_description, bp.dunninggrace AS bp_dunninggrace, bp.firstsale AS bp_firstsale, bp.flatdiscount AS bp_flatdiscount, bp.freightcostrule AS bp_freightcostrule, bp.invoicerule AS bp_invoicerule, bp.isactive AS bp_isactive, bp.iscustomer AS bp_iscustomer, bp.isdiscountprinted AS bp_isdiscountprinted, bp.isemployee AS bp_isemployee, bp.ismanufacturer AS bp_ismanufacturer, bp.isonetime AS bp_isonetime, bp.ispotaxexempt AS bp_ispotaxexempt, bp.isprospect AS bp_isprospect, bp.issalesrep AS bp_issalesrep, bp.issummary AS bp_issummary, bp.istaxexempt AS bp_istaxexempt, bp.isvendor AS bp_isvendor, bp.logo_id AS BP_Logo_ID, bp.m_discountschema_id AS BP_M_DiscountSchema_ID, bp.m_pricelist_id AS BP_M_PriceList_ID, bp.numberemployees AS bp_numberemployees, bp.paymentrule AS bp_paymentrule, bp.paymentrulepo AS bp_paymentrulepo, bp.po_discountschema_id AS BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS BP_PO_PriceList_ID, bp.poreference AS bp_poreference, bp.potentiallifetimevalue AS bp_potentiallifetimevalue, bp.rating AS bp_rating, bp.salesrep_id AS BP_SalesRep_ID, bp.salesvolume AS bp_salesvolume, bp.sendemail AS bp_sendemail, bp.shareofcustomer AS bp_shareofcustomer, bp.shelflifeminpct AS bp_shelflifeminpct, bp.so_creditlimit AS bp_so_creditlimit, bp.socreditstatus AS bp_socreditstatus, bp.so_creditused AS bp_so_creditused, bp.so_description AS bp_so_description, bp.totalopenbalance AS bp_totalopenbalance, bp.updated AS bp_updated,
bp.updatedby AS BP_UpdatedBy, bp.url AS bp_url, bpgt.ad_org_id AS C_Greeting_AD_Org_ID, bpgt.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpgt.name AS c_greeting_name, bpl.ad_org_id AS BP_Location_AD_Org_ID, bpl.c_bpartner_id AS BP_Location_C_BPartner_ID, bpl.created AS bp_location_created, bpl.createdby AS BP_Location_CreatedBy, bpl.c_salesregion_id AS BP_Location_C_SalesRegion_ID, bpl.fax AS bp_location_fax, bpl.isactive AS bp_location_isactive, bpl.isbillto AS bp_location_isbillto, bpl.isdn AS bp_location_isdn, bpl.ispayfrom AS bp_location_ispayfrom, bpl.isremitto AS bp_location_isremitto, bpl.isshipto AS bp_location_isshipto, bpl.name AS bp_location_name, bpl.phone AS bp_location_phone, bpl.phone2 AS bp_location_phone2, bpl.updated AS bp_location_updated, bpl.updatedby AS BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID, l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy,
bpc.value AS ad_user_value, bpcgt.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcgt.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcgt.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy, u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bp_acqusitioncost, ubp.actuallifetimevalue AS salesrep_bp_actuallifetimeval, ubp.ad_language AS salesrep_bp_ad_language, ubp.ad_orgbp_id AS SalesRep_BP_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BP_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BP_BPartner_Parent_ID, ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID,
ubp.c_dunning_id AS SalesRep_BP_C_Dunning_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_C_InvoiceSched_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bp_created, ubp.createdby AS SalesRep_BP_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bp_deliveryrule, ubp.deliveryviarule AS salesrep_bp_deliveryviarule, ubp.description AS salesrep_bp_description, ubp.dunninggrace AS salesrep_bp_dunninggrace, ubp.duns AS salesrep_bp_duns, ubp.firstsale AS salesrep_bp_firstsale, ubp.flatdiscount AS salesrep_bp_flatdiscount, ubp.freightcostrule AS salesrep_bp_freightcostrule, ubp.invoicerule AS salesrep_bp_invoicerule, ubp.isactive AS salesrep_bp_isactive, ubp.iscustomer AS salesrep_bp_iscustomer, ubp.isdiscountprinted AS salesrep_bp_isdiscountprinted, ubp.isemployee AS salesrep_bp_isemployee, ubp.ismanufacturer AS salesrep_bp_ismanufacturer, ubp.isonetime AS salesrep_bp_isonetime, ubp.ispotaxexempt AS salesrep_bp_ispotaxexempt, ubp.isprospect AS salesrep_bp_isprospect, ubp.issalesrep AS salesrep_bp_issalesrep, ubp.issummary AS salesrep_bp_issummary, ubp.istaxexempt AS salesrep_bp_istaxexempt, ubp.isvendor AS salesrep_bp_isvendor, ubp.logo_id AS SalesRep_BP_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_M_DiscountSchm_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bp_naics, ubp.name2 AS salesrep_bp_name2, ubp.numberemployees AS salesrep_bp_numberemployees, ubp.paymentrule AS salesrep_bp_paymentrule, ubp.paymentrulepo AS salesrep_bp_paymentrulepo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountSchm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bp_poreference, ubp.potentiallifetimevalue AS salesrep_bp_potentiallifetime, ubp.rating AS salesrep_bp_rating, ubp.referenceno AS salesrep_bp_referenceno, ubp.salesrep_id AS SalesRep_BP_SalesRep_ID,
ubp.salesvolume AS salesrep_bp_salesvolume, ubp.sendemail AS salesrep_bp_sendemail, ubp.shareofcustomer AS salesrep_bp_shareofcustomer, ubp.shelflifeminpct AS salesrep_bp_shelflifeminpct, ubp.so_creditlimit AS salesrep_bp_so_creditlimit, ubp.socreditstatus AS salesrep_bp_socreditstatus, ubp.so_creditused AS salesrep_bp_so_creditused, ubp.so_description AS salesrep_bp_so_description, ubp.taxid AS salesrep_bp_taxid, ubp.totalopenbalance AS salesrep_bp_totalopenbalance, ubp.updated AS salesrep_bp_updated, ubp.updatedby AS SalesRep_BP_UpdatedBy, ubp.url AS salesrep_bp_url, ubp.value AS salesrep_bp_value, bbp.acqusitioncost AS bill_bp_acqusitioncost, bbp.actuallifetimevalue AS bill_bp_actuallifetimevalue, bbp.ad_language AS bill_bp_ad_language, bbp.ad_orgbp_id AS Bill_BP_AD_OrgBP_ID, bbp.ad_org_id AS Bill_BP_AD_Org_ID, bbp.bpartner_parent_id AS Bill_BP_BPartner_Parent_ID, bbp.c_bp_group_id AS Bill_BP_C_BP_Group_ID, bbp.c_dunning_id AS Bill_BP_C_Dunning_ID, bbp.c_greeting_id AS Bill_BP_C_Greeting_ID, bbp.c_invoiceschedule_id AS Bill_BP_C_InvoiceSchedule_ID, bbp.c_paymentterm_id AS Bill_BP_C_PaymentTerm_ID, bbp.created AS bill_bp_created, bbp.createdby AS Bill_BP_CreatedBy, bbp.c_taxgroup_id AS Bill_BP_C_TaxGroup_ID, bbp.deliveryrule AS bill_bp_deliveryrule, bbp.deliveryviarule AS bill_bp_deliveryviarule, bbp.description AS bill_bp_description, bbp.dunninggrace AS bill_bp_dunninggrace, bbp.duns AS bill_bp_duns, bbp.firstsale AS bill_bp_firstsale, bbp.flatdiscount AS bill_bp_flatdiscount, bbp.freightcostrule AS bill_bp_freightcostrule, bbp.invoicerule AS bill_bp_invoicerule, bbp.isactive AS bill_bp_isactive, bbp.iscustomer AS bill_bp_iscustomer, bbp.isdiscountprinted AS bill_bp_isdiscountprinted, bbp.isemployee AS bill_bp_isemployee, bbp.ismanufacturer AS bill_bp_ismanufacturer, bbp.isonetime AS bill_bp_isonetime, bbp.ispotaxexempt AS bill_bp_ispotaxexempt, bbp.isprospect AS bill_bp_isprospect, bbp.issalesrep AS bill_bp_issalesrep, bbp.issummary AS bill_bp_issummary,
bbp.istaxexempt AS bill_bp_istaxexempt, bbp.isvendor AS bill_bp_isvendor, bbp.logo_id AS Bill_BP_Logo_ID, bbp.m_discountschema_id AS Bill_BP_M_DiscountSchema_ID, bbp.m_pricelist_id AS Bill_BP_M_PriceList_ID, bbp.naics AS bill_bp_naics, bbp.numberemployees AS bill_bp_numberemployees, bbp.paymentrule AS bill_bp_paymentrule, bbp.paymentrulepo AS bill_bp_paymentrulepo, bbp.po_discountschema_id AS Bill_BP_PO_DiscountSchema_ID, bbp.po_paymentterm_id AS Bill_BP_PO_PaymentTerm_ID, bbp.po_pricelist_id AS Bill_BP_PO_PriceList_ID, bbp.poreference AS bill_bp_poreference, bbp.potentiallifetimevalue AS bill_bp_potentiallifetimevalue, bbp.rating AS bill_bp_rating, bbp.referenceno AS bill_bp_referenceno, bbp.salesrep_id AS Bill_BP_SalesRep_ID, bbp.salesvolume AS bill_bp_salesvolume, bbp.sendemail AS bill_bp_sendemail, bbp.shareofcustomer AS bill_bp_shareofcustomer, bbp.shelflifeminpct AS bill_bp_shelflifeminpct, bbp.so_creditlimit AS bill_bp_so_creditlimit, bbp.socreditstatus AS bill_bp_socreditstatus, bbp.so_creditused AS bill_bp_so_creditused, bbp.so_description AS bill_bp_so_description, bbp.totalopenbalance AS bill_bp_totalopenbalance, bbp.updated AS bill_bp_updated, bbp.updatedby AS Bill_BP_UpdatedBy, bbp.url AS bill_bp_url, bbpl.ad_org_id AS Bill_BP_Location_AD_Org_ID, bbpl.c_bpartner_id AS Bill_BP_Location_C_BPartner_ID, bbpl.created AS bill_bp_location_created, bbpl.createdby AS Bill_BP_Location_CreatedBy, bbpl.c_salesregion_id AS Bill_BP_Location_C_SalesReg_ID, bbpl.fax AS bill_bp_location_fax, bbpl.isactive AS bill_bp_location_isactive, bbpl.isbillto AS bill_bp_location_isbillto, bbpl.isdn AS bill_bp_location_isdn, bbpl.ispayfrom AS bill_bp_location_ispayfrom, bbpl.isremitto AS bill_bp_location_isremitto, bbpl.isshipto AS bill_bp_location_isshipto, bbpl.name AS bill_bp_location_name, bbpl.phone AS bill_bp_location_phone, bbpl.phone2 AS bill_bp_location_phone2, bbpl.updated AS bill_bp_location_updated, bbpl.updatedby AS Bill_BP_Location_UpdatedBy,
bbpc.ad_org_id AS Bill_User_AD_Org_ID, bbpc.ad_orgtrx_id AS Bill_User_AD_OrgTrx_ID, bbpc.birthday AS bill_user_ad_birthday, bbpc.c_bpartner_id AS Bill_User_C_BPartner_ID, bbpc.c_bpartner_location_id AS Bill_User_C_BP_Location_ID, bbpc.c_greeting_id AS Bill_User_C_Greeting_ID, bbpc.comments AS bill_user_comments, bbpc.created AS bill_user_created, bbpc.createdby AS Bill_User_CreatedBy, bbpc.description AS bill_user_description, bbpc.email AS bill_user_email, bbpc.fax AS bill_user_fax, bbpc.isactive AS bill_user_isactive, bbpc.lastcontact AS bill_user_lastcontact, bbpc.lastresult AS bill_user_lastresult, bbpc.phone2 AS bill_user_phone2, bbpc.supervisor_id AS Bill_User_Supervisor_ID, bbpc.updated AS bill_user_updated, bbpc.updatedby AS Bill_User_UpdatedBy, bbpc.value AS bill_user_value, cur.cursymbol AS CurSymbol, cur.description AS cur_description FROM c_order o
JOIN c_doctype_trl dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN c_paymentterm pt ON o.c_paymentterm_id = pt.c_paymentterm_id
JOIN c_paymentterm_trl ptt ON o.c_paymentterm_id = ptt.c_paymentterm_id AND dt.ad_language = ptt.ad_language
JOIN c_bpartner bp ON o.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id AND dt.ad_language = bpgt.ad_language
JOIN c_bpartner_location bpl ON o.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON o.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_greeting_trl bpcgt ON bpc.c_greeting_id = bpcgt.c_greeting_id AND dt.ad_language = bpcgt.ad_language
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
JOIN c_bpartner bbp ON o.bill_bpartner_id = bbp.c_bpartner_id
JOIN c_bpartner_location bbpl ON o.bill_location_id = bbpl.c_bpartner_location_id
LEFT JOIN ad_user bbpc ON o.bill_user_id = bbpc.ad_user_id
LEFT JOIN c_currency_trl cur ON o.c_currency_id = cur.c_currency_id AND dt.ad_language = cur.ad_language
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_Order_LineTax_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Order_ID, C_OrderLine_ID, C_Tax_ID, TaxIndicator, C_BPartner_ID, C_BPartner_Location_ID, BPName, C_Location_ID, Line, M_Product_ID, VendorProductNo, QtyOrdered, QtyEntered, UOMSymbol, Name, Description, DocumentNote, UPC, SKU, ProductValue, ResourceDescription, PriceList, PriceEnteredList, Discount, PriceActual, PriceEntered, LineNetAmt, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, C_OrderLine_AD_OrgTrx_ID, C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_UOM_ID, DateDelivered, DateInvoiced, DateOrdered, c_orderline_datepromised, c_orderline_freightamt, IsDescription, Link_OrderLine_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, C_OrderLine_M_Warehouse_ID, PriceCost, PriceLimit, c_orderline_processed, QtyDelivered, QtyInvoiced, QtyLostSales, QtyReserved, Ref_OrderLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, C_OrderLine_User1_ID, C_OrderLine_User2_ID, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, IsDefault, c_uom_name, StdPrecision, UOMType, X12DE355, C_Order_AD_Org_ID, C_Order_AD_OrgTrx_ID, AD_User_ID, AmountRefunded, AmountTendered, Bill_BPartner_ID, Bill_Location_ID, Bill_User_ID, C_Order_C_Activity_ID, C_Order_C_BPartner_ID, C_Order_C_Campaign_ID, C_CashLine_ID, C_CashPlanLine_ID, C_Order_C_Charge_ID, C_ConversionType_ID, C_Order_C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, ChargeAmt, CopyFrom, C_Payment_ID, C_PaymentTerm_ID, C_POS_ID, C_Order_C_Project_ID, c_order_created, C_Order_CreatedBy, DateAcct, DatePrinted, c_order_datepromised, DeliveryRule, DeliveryViaRule, c_order_description, DocAction, DocStatus, DocumentNo, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, c_order_freightamt, FreightCostRule, GrandTotal, InvoiceRule, c_order_isactive, IsApproved, IsCreditApproved, IsDelivered, c_order_isdiscountprinted,
c_order_isdropship, IsInvoiced, IsPayScheduleValid, IsPrinted, IsSelected, c_order_isselfservice, IsSOTrx, IsTaxIncluded, IsTransferred, Link_Order_ID, C_Order_M_FreightCategory_ID, C_Order_M_PriceList_ID, C_Order_M_Shipper_ID, C_Order_M_Warehouse_ID, OrderType, Pay_BPartner_ID, Pay_Location_ID, c_order_paymentrule, POReference, Posted, PriorityRule, c_order_processed, ProcessedOn, PromotionCode, Ref_Order_ID, SalesRep_ID, c_order_sendemail, TotalLines, c_order_updated, C_Order_UpdatedBy, C_Order_User1_ID, C_Order_User2_ID, c_order_volume, c_order_weight, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, M_Product_C_UOM_ID, DescriptionURL, m_product_discontinued, m_product_discontinuedat, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, m_product_isdropship, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_AttributeSet_ID, M_AttributeSetInstance_ID, M_Product_M_FreightCategory_ID, M_Locator_ID, M_Product_Product_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, m_product_volume, m_product_weight, M_Product_PO_AD_Org_ID, M_Product_PO_C_BPartner_ID, M_Product_PO_C_Currency_ID, CostPerOrder, m_product_po_created, M_Product_PO_CreatedBy, M_Product_PO_C_UOM_ID, DeliveryTime_Actual, DeliveryTime_Promised, m_product_po_discontinued, m_product_po_discontinuedat, m_product_po_isactive, IsCurrentVendor, m_product_po_manufacturer, M_Product_PO_M_Product_ID, Order_Min, Order_Pack, PriceEffective, PriceLastInv, PriceLastPO, m_product_po_pricelist, PricePO, m_product_po_qualityrating, RoyaltyAmt,
m_product_po_upc, m_product_po_updated, M_Product_PO_UpdatedBy, m_product_po_vendorcategory, S_ResourceAssignment_AD_Org_ID, AssignDateFrom, AssignDateTo, s_resourceassignment_created, s_resourceassignment_createby, s_resourceassignment_isactive, IsConfirmed, s_resourceassignment_name, s_resourceassignment_qty, s_resourceassignment_s_resour, s_resourceassignment_updated, S_ResourceAssignment_UpdatedBy, C_Charge_C_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded, C_BPartner_Product_AD_Org_ID, C_BP_Product_C_BPartner_ID, c_bpartner_product_created, C_BPartner_Product_CreatedBy, c_bpartner_product_descriptio, c_bpartner_product_isactive, IsManufacturer, c_bpartner_product_manufactur, c_bpartner_product_qualityrat, ShelfLifeMinDays, c_bpartner_product_shelflifem, c_bpartner_product_updated, C_BPartner_Product_UpdatedBy, c_bpartner_product_vendorcate, AcqusitionCost, ActualLifeTimeValue, c_bpartner_ad_language, AD_OrgBP_ID, C_BPartner_AD_Org_ID, BPartner_Parent_ID, C_BP_Group_ID, C_Dunning_ID, C_Greeting_ID, C_InvoiceSchedule_ID, C_BPartner_C_PaymentTerm_ID, c_bpartner_created, C_BPartner_CreatedBy, C_BPartner_C_TaxGroup_ID, c_bpartner_deliveryrule, c_bpartner_deliveryviarule, c_bpartner_description, c_bpartner_dunninggrace, DUNS, c_bpartner_firstsale, c_bpartner_flatdiscount, c_bpartner_freightcostrule, c_bpartner_invoicerule, c_bpartner_isactive, IsCustomer, c_bpartner_isdiscountprinted, c_bpartner_isemployee, c_bpartner_ismanufacturer, IsOneTime, IsPOTaxExempt, IsProspect, IsSalesRep, c_bpartner_issummary, c_bpartner_istaxexempt, IsVendor, Logo_ID, M_DiscountSchema_ID, C_BPartner_M_PriceList_ID, NAICS, c_bpartner_name, c_bpartner_name2, NumberEmployees, c_bpartner_paymentrule, PaymentRulePO, PO_DiscountSchema_ID, PO_PaymentTerm_ID, PO_PriceList_ID, c_bpartner_poreference, PotentialLifeTimeValue, c_bpartner_rating,
c_bpartner_referenceno, C_BPartner_SalesRep_ID, SalesVolume, c_bpartner_sendemail, ShareOfCustomer, c_bpartner_shelflifeminpct, SO_CreditLimit, SOCreditStatus, SO_CreditUsed, SO_Description, c_bpartner_taxid, TotalOpenBalance, c_bpartner_updated, C_BPartner_UpdatedBy, c_bpartner_url, c_bpartner_value, C_BPartner_Location_AD_Org_ID, c_bpartner_location_c_bpartne, c_bpartner_location_created, C_BPartner_Location_CreatedBy, C_SalesRegion_ID, c_bpartner_location_fax, c_bpartner_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bpartner_location_name, c_bpartner_location_phone, c_bpartner_location_phone2, c_bpartner_location_updated, C_BPartner_Location_UpdatedBy, C_Tax_AD_Org_ID, AD_Rule_ID, C_Country_ID, C_Region_ID, C_Tax_C_TaxCategory_ID, c_tax_description, c_tax_isactive, c_tax_isdefault, IsDocumentLevel, IsSalesTax, c_tax_issummary, c_tax_istaxexempt, c_tax_name, Parent_Tax_ID, Rate, RequiresTaxCertificate, SOPOType, To_Country_ID, C_Tax_To_Region_ID, ValidFrom) AS SELECT ol.ad_client_id AS AD_Client_ID, ol.ad_org_id AS AD_Org_ID, ol.isactive AS IsActive, ol.created AS Created, ol.createdby AS CreatedBy, ol.updated AS Updated, ol.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, ol.c_order_id AS C_Order_ID, ol.c_orderline_id AS C_OrderLine_ID, ol.c_tax_id AS C_Tax_ID, tt.taxindicator AS TaxIndicator, ol.c_bpartner_id AS C_BPartner_ID, ol.c_bpartner_location_id AS C_BPartner_Location_ID, bp.name AS BPName, bpl.c_location_id AS C_Location_ID, ol.line AS Line, pt.m_product_id AS M_Product_ID, po.vendorproductno AS VendorProductNo, CASE WHEN ol.qtyordered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.qtyordered ELSE NULL END AS QtyOrdered, CASE WHEN ol.qtyentered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN ol.qtyentered <> 0 OR ol.m_product_id IS NOT NULL THEN uomt.uomsymbol ELSE NULL END AS UOMSymbol,
COALESCE(ct.name, (COALESCE(pt.name, p.name) || productattribute(ol.m_attributesetinstance_id)), ol.description) AS Name, CASE WHEN COALESCE(ct.name, pt.name, p.name) IS NOT NULL THEN ol.description ELSE NULL END AS Description, COALESCE(pt.documentnote, p.documentnote) AS DocumentNote, p.upc AS UPC, p.sku AS SKU, COALESCE(pp.vendorproductno, p.value) AS ProductValue, ra.description AS ResourceDescription, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist <> 0 THEN ol.pricelist ELSE NULL END AS PriceList, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist <> 0 AND ol.qtyentered <> 0 THEN ol.pricelist * ol.qtyordered / ol.qtyentered ELSE NULL END AS PriceEnteredList, CASE WHEN i.isdiscountprinted = 'Y' AND ol.pricelist > ol.priceactual AND ol.pricelist <> 0 THEN (ol.pricelist - ol.priceactual) / ol.pricelist * 100 ELSE NULL END AS Discount, CASE WHEN ol.priceactual <> 0 OR ol.m_product_id IS NOT NULL THEN ol.priceactual ELSE NULL END AS PriceActual, CASE WHEN ol.priceentered <> 0 OR ol.m_product_id IS NOT NULL THEN ol.priceentered ELSE NULL END AS PriceEntered, CASE WHEN ol.linenetamt <> 0 OR ol.m_product_id IS NOT NULL THEN ol.linenetamt ELSE NULL END AS LineNetAmt, pt.description AS ProductDescription, p.imageurl AS ImageURL, ol.c_campaign_id AS C_Campaign_ID, ol.c_project_id AS C_Project_ID, ol.c_activity_id AS C_Activity_ID, ol.c_projectphase_id AS C_ProjectPhase_ID, ol.c_projecttask_id AS C_ProjectTask_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_uom_id AS C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.freightamt AS c_orderline_freightamt, ol.isdescription AS IsDescription, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_M_Warehouse_ID,
ol.pricecost AS PriceCost, ol.pricelimit AS PriceLimit, ol.processed AS c_orderline_processed, ol.qtydelivered AS QtyDelivered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, uomt.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uomt.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS IsDefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Order_AD_Org_ID, i.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.amountrefunded AS AmountRefunded, i.amounttendered AS AmountTendered, i.bill_bpartner_id AS Bill_BPartner_ID, i.bill_location_id AS Bill_Location_ID, i.bill_user_id AS Bill_User_ID, i.c_activity_id AS C_Order_C_Activity_ID, i.c_bpartner_id AS C_Order_C_BPartner_ID, i.c_campaign_id AS C_Order_C_Campaign_ID, i.c_cashline_id AS C_CashLine_ID, i.c_cashplanline_id AS C_CashPlanLine_ID, i.c_charge_id AS C_Order_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Order_C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.chargeamt AS ChargeAmt, i.copyfrom AS CopyFrom, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_pos_id AS C_POS_ID, i.c_project_id AS C_Order_C_Project_ID, i.created AS c_order_created, i.createdby AS C_Order_CreatedBy, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.datepromised AS c_order_datepromised, i.deliveryrule AS DeliveryRule, i.deliveryviarule AS DeliveryViaRule, i.description AS c_order_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo, i.dropship_bpartner_id AS DropShip_BPartner_ID,
i.dropship_location_id AS DropShip_Location_ID, i.dropship_user_id AS DropShip_User_ID, i.freightamt AS c_order_freightamt, i.freightcostrule AS FreightCostRule, i.grandtotal AS GrandTotal, i.invoicerule AS InvoiceRule, i.isactive AS c_order_isactive, i.isapproved AS IsApproved, i.iscreditapproved AS IsCreditApproved, i.isdelivered AS IsDelivered, i.isdiscountprinted AS c_order_isdiscountprinted, i.isdropship AS c_order_isdropship, i.isinvoiced AS IsInvoiced, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselected AS IsSelected, i.isselfservice AS c_order_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.link_order_id AS Link_Order_ID, i.m_freightcategory_id AS C_Order_M_FreightCategory_ID, i.m_pricelist_id AS C_Order_M_PriceList_ID, i.m_shipper_id AS C_Order_M_Shipper_ID, i.m_warehouse_id AS C_Order_M_Warehouse_ID, i.ordertype AS OrderType, i.pay_bpartner_id AS Pay_BPartner_ID, i.pay_location_id AS Pay_Location_ID, i.paymentrule AS c_order_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.priorityrule AS PriorityRule, i.processed AS c_order_processed, i.processedon AS ProcessedOn, i.promotioncode AS PromotionCode, i.ref_order_id AS Ref_Order_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS c_order_sendemail, i.totallines AS TotalLines, i.updated AS c_order_updated, i.updatedby AS C_Order_UpdatedBy, i.user1_id AS C_Order_User1_ID, i.user2_id AS C_Order_User2_ID, i.volume AS c_order_volume, i.weight AS c_order_weight, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS m_product_discontinued,
p.discontinuedat AS m_product_discontinuedat, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS m_product_isdropship, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_Product_M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_Product_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, pt.updated AS m_product_updated, pt.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS m_product_volume, p.weight AS m_product_weight, po.ad_org_id AS M_Product_PO_AD_Org_ID, po.c_bpartner_id AS M_Product_PO_C_BPartner_ID, po.c_currency_id AS M_Product_PO_C_Currency_ID, po.costperorder AS CostPerOrder, po.created AS m_product_po_created, po.createdby AS M_Product_PO_CreatedBy, po.c_uom_id AS M_Product_PO_C_UOM_ID, po.deliverytime_actual AS DeliveryTime_Actual, po.deliverytime_promised AS DeliveryTime_Promised, po.discontinued AS m_product_po_discontinued, po.discontinuedat AS m_product_po_discontinuedat, po.isactive AS m_product_po_isactive, po.iscurrentvendor AS IsCurrentVendor,
po.manufacturer AS m_product_po_manufacturer, po.m_product_id AS M_Product_PO_M_Product_ID, po.order_min AS Order_Min, po.order_pack AS Order_Pack, po.priceeffective AS PriceEffective, po.pricelastinv AS PriceLastInv, po.pricelastpo AS PriceLastPO, po.pricelist AS m_product_po_pricelist, po.pricepo AS PricePO, po.qualityrating AS m_product_po_qualityrating, po.royaltyamt AS RoyaltyAmt, po.upc AS m_product_po_upc, po.updated AS m_product_po_updated, po.updatedby AS M_Product_PO_UpdatedBy, po.vendorcategory AS m_product_po_vendorcategory, ra.ad_org_id AS S_ResourceAssignment_AD_Org_ID, ra.assigndatefrom AS AssignDateFrom, ra.assigndateto AS AssignDateTo, ra.created AS s_resourceassignment_created, ra.createdby AS s_resourceassignment_createby, ra.isactive AS s_resourceassignment_isactive, ra.isconfirmed AS IsConfirmed, ra.name AS s_resourceassignment_name, ra.qty AS s_resourceassignment_qty, ra.s_resource_id AS s_resourceassignment_s_resour, ra.updated AS s_resourceassignment_updated, ra.updatedby AS S_ResourceAssignment_UpdatedBy, ct.ad_org_id AS C_Charge_C_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID, c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, c.description AS c_charge_description, ct.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded, pp.ad_org_id AS C_BPartner_Product_AD_Org_ID, pp.c_bpartner_id AS C_BP_Product_C_BPartner_ID, pp.created AS c_bpartner_product_created, pp.createdby AS C_BPartner_Product_CreatedBy, pp.description AS c_bpartner_product_descriptio, pp.isactive AS c_bpartner_product_isactive, pp.ismanufacturer AS IsManufacturer, pp.manufacturer AS c_bpartner_product_manufactur, pp.qualityrating AS c_bpartner_product_qualityrat, pp.shelflifemindays AS ShelfLifeMinDays, pp.shelflifeminpct AS c_bpartner_product_shelflifem, pp.updated AS c_bpartner_product_updated,
pp.updatedby AS C_BPartner_Product_UpdatedBy, pp.vendorcategory AS c_bpartner_product_vendorcate, bp.acqusitioncost AS AcqusitionCost, bp.actuallifetimevalue AS ActualLifeTimeValue, bp.ad_language AS c_bpartner_ad_language, bp.ad_orgbp_id AS AD_OrgBP_ID, bp.ad_org_id AS C_BPartner_AD_Org_ID, bp.bpartner_parent_id AS BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_Group_ID, bp.c_dunning_id AS C_Dunning_ID, bp.c_greeting_id AS C_Greeting_ID, bp.c_invoiceschedule_id AS C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BPartner_C_PaymentTerm_ID, bp.created AS c_bpartner_created, bp.createdby AS C_BPartner_CreatedBy, bp.c_taxgroup_id AS C_BPartner_C_TaxGroup_ID, bp.deliveryrule AS c_bpartner_deliveryrule, bp.deliveryviarule AS c_bpartner_deliveryviarule, bp.description AS c_bpartner_description, bp.dunninggrace AS c_bpartner_dunninggrace, bp.duns AS DUNS, bp.firstsale AS c_bpartner_firstsale, bp.flatdiscount AS c_bpartner_flatdiscount, bp.freightcostrule AS c_bpartner_freightcostrule, bp.invoicerule AS c_bpartner_invoicerule, bp.isactive AS c_bpartner_isactive, bp.iscustomer AS IsCustomer, bp.isdiscountprinted AS c_bpartner_isdiscountprinted, bp.isemployee AS c_bpartner_isemployee, bp.ismanufacturer AS c_bpartner_ismanufacturer, bp.isonetime AS IsOneTime, bp.ispotaxexempt AS IsPOTaxExempt, bp.isprospect AS IsProspect, bp.issalesrep AS IsSalesRep, bp.issummary AS c_bpartner_issummary, bp.istaxexempt AS c_bpartner_istaxexempt, bp.isvendor AS IsVendor, bp.logo_id AS Logo_ID, bp.m_discountschema_id AS M_DiscountSchema_ID, bp.m_pricelist_id AS C_BPartner_M_PriceList_ID, bp.naics AS NAICS, bp.name AS c_bpartner_name, bp.name2 AS c_bpartner_name2, bp.numberemployees AS NumberEmployees, bp.paymentrule AS c_bpartner_paymentrule, bp.paymentrulepo AS PaymentRulePO, bp.po_discountschema_id AS PO_DiscountSchema_ID, bp.po_paymentterm_id AS PO_PaymentTerm_ID, bp.po_pricelist_id AS PO_PriceList_ID, bp.poreference AS c_bpartner_poreference, bp.potentiallifetimevalue AS PotentialLifeTimeValue,
bp.rating AS c_bpartner_rating, bp.referenceno AS c_bpartner_referenceno, bp.salesrep_id AS C_BPartner_SalesRep_ID, bp.salesvolume AS SalesVolume, bp.sendemail AS c_bpartner_sendemail, bp.shareofcustomer AS ShareOfCustomer, bp.shelflifeminpct AS c_bpartner_shelflifeminpct, bp.so_creditlimit AS SO_CreditLimit, bp.socreditstatus AS SOCreditStatus, bp.so_creditused AS SO_CreditUsed, bp.so_description AS SO_Description, bp.taxid AS c_bpartner_taxid, bp.totalopenbalance AS TotalOpenBalance, bp.updated AS c_bpartner_updated, bp.updatedby AS C_BPartner_UpdatedBy, bp.url AS c_bpartner_url, bp.value AS c_bpartner_value, bpl.ad_org_id AS C_BPartner_Location_AD_Org_ID, bpl.c_bpartner_id AS c_bpartner_location_c_bpartne, bpl.created AS c_bpartner_location_created, bpl.createdby AS C_BPartner_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bpartner_location_fax, bpl.isactive AS c_bpartner_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bpartner_location_name, bpl.phone AS c_bpartner_location_phone, bpl.phone2 AS c_bpartner_location_phone2, bpl.updated AS c_bpartner_location_updated, bpl.updatedby AS C_BPartner_Location_UpdatedBy, tt.ad_org_id AS C_Tax_AD_Org_ID, t.ad_rule_id AS AD_Rule_ID, t.c_country_id AS C_Country_ID, t.c_region_id AS C_Region_ID, t.c_taxcategory_id AS C_Tax_C_TaxCategory_ID, tt.description AS c_tax_description, tt.isactive AS c_tax_isactive, t.isdefault AS c_tax_isdefault, t.isdocumentlevel AS IsDocumentLevel, t.issalestax AS IsSalesTax, t.issummary AS c_tax_issummary, t.istaxexempt AS c_tax_istaxexempt, tt.name AS c_tax_name, t.parent_tax_id AS Parent_Tax_ID, t.rate AS Rate, t.requirestaxcertificate AS RequiresTaxCertificate, t.sopotype AS SOPOType, t.to_country_id AS To_Country_ID, t.to_region_id AS C_Tax_To_Region_ID, t.validfrom AS ValidFrom FROM c_orderline ol
JOIN c_uom uom ON ol.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON ol.c_uom_id = uomt.c_uom_id
JOIN c_order i ON ol.c_order_id = i.c_order_id
LEFT JOIN m_product p ON ol.m_product_id = p.m_product_id
LEFT JOIN m_product_trl pt ON ol.m_product_id = pt.m_product_id AND uomt.ad_language = pt.ad_language
LEFT JOIN m_product_po po ON p.m_product_id = po.m_product_id AND i.c_bpartner_id = po.c_bpartner_id
LEFT JOIN s_resourceassignment ra ON ol.s_resourceassignment_id = ra.s_resourceassignment_id
LEFT JOIN c_charge c ON ol.c_charge_id = c.c_charge_id
LEFT JOIN c_charge_trl ct ON ol.c_charge_id = ct.c_charge_id AND uomt.ad_language = ct.ad_language
LEFT JOIN c_bpartner_product pp ON ol.m_product_id = pp.m_product_id AND i.c_bpartner_id = pp.c_bpartner_id
JOIN c_bpartner bp ON ol.c_bpartner_id = bp.c_bpartner_id
JOIN c_bpartner_location bpl ON ol.c_bpartner_location_id = bpl.c_bpartner_location_id
LEFT JOIN c_tax t ON ol.c_tax_id = t.c_tax_id
LEFT JOIN c_tax_trl tt ON ol.c_tax_id = tt.c_tax_id AND uomt.ad_language = tt.ad_language UNION SELECT ol.ad_client_id AS AD_Client_ID, ol.ad_org_id AS AD_Org_ID, ol.isactive AS IsActive, ol.created AS Created, ol.createdby AS CreatedBy, ol.updated AS Updated, ol.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, ol.c_order_id AS C_Order_ID, ol.c_orderline_id AS C_OrderLine_ID, ol.c_tax_id AS C_Tax_ID, NULL AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, ol.line + bl.line / 100 AS Line, pt.m_product_id AS M_Product_ID, po.vendorproductno AS VendorProductNo, CASE WHEN bl.isqtypercentage = 'N' THEN ol.qtyordered * bl.qtybom ELSE ol.qtyordered * (bl.qtybatch / 100) END AS QtyOrdered, CASE WHEN bl.isqtypercentage = 'N' THEN ol.qtyentered * bl.qtybom ELSE ol.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uomt.uomsymbol AS UOMSymbol, pt.name AS Name, bl.description AS Description, pt.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, pt.description AS ProductDescription, p.imageurl AS ImageURL, ol.c_campaign_id AS C_Campaign_ID, ol.c_project_id AS C_Project_ID, ol.c_activity_id AS C_Activity_ID, ol.c_projectphase_id AS C_ProjectPhase_ID, ol.c_projecttask_id AS C_ProjectTask_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_uom_id AS C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.freightamt AS c_orderline_freightamt, ol.isdescription AS IsDescription, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID,
ol.m_warehouse_id AS C_OrderLine_M_Warehouse_ID, ol.pricecost AS PriceCost, ol.pricelimit AS PriceLimit, ol.processed AS c_orderline_processed, ol.qtydelivered AS QtyDelivered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, uomt.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uom.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS IsDefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, i.ad_org_id AS C_Order_AD_Org_ID, i.ad_orgtrx_id AS C_Order_AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.amountrefunded AS AmountRefunded, i.amounttendered AS AmountTendered, i.bill_bpartner_id AS Bill_BPartner_ID, i.bill_location_id AS Bill_Location_ID, i.bill_user_id AS Bill_User_ID, i.c_activity_id AS C_Order_C_Activity_ID, i.c_bpartner_id AS C_Order_C_BPartner_ID, i.c_campaign_id AS C_Order_C_Campaign_ID, i.c_cashline_id AS C_CashLine_ID, i.c_cashplanline_id AS C_CashPlanLine_ID, i.c_charge_id AS C_Order_C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Order_C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.chargeamt AS ChargeAmt, i.copyfrom AS CopyFrom, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_pos_id AS C_POS_ID, i.c_project_id AS C_Order_C_Project_ID, i.created AS c_order_created, i.createdby AS C_Order_CreatedBy, i.dateacct AS DateAcct, i.dateprinted AS DatePrinted, i.datepromised AS c_order_datepromised, i.deliveryrule AS DeliveryRule, i.deliveryviarule AS DeliveryViaRule, i.description AS c_order_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.documentno AS DocumentNo,
i.dropship_bpartner_id AS DropShip_BPartner_ID, i.dropship_location_id AS DropShip_Location_ID, i.dropship_user_id AS DropShip_User_ID, i.freightamt AS c_order_freightamt, i.freightcostrule AS FreightCostRule, i.grandtotal AS GrandTotal, i.invoicerule AS InvoiceRule, i.isactive AS c_order_isactive, i.isapproved AS IsApproved, i.iscreditapproved AS IsCreditApproved, i.isdelivered AS IsDelivered, i.isdiscountprinted AS c_order_isdiscountprinted, i.isdropship AS c_order_isdropship, i.isinvoiced AS IsInvoiced, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselected AS IsSelected, i.isselfservice AS c_order_isselfservice, i.issotrx AS IsSOTrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.link_order_id AS Link_Order_ID, i.m_freightcategory_id AS C_Order_M_FreightCategory_ID, i.m_pricelist_id AS C_Order_M_PriceList_ID, i.m_shipper_id AS C_Order_M_Shipper_ID, i.m_warehouse_id AS C_Order_M_Warehouse_ID, i.ordertype AS OrderType, i.pay_bpartner_id AS Pay_BPartner_ID, i.pay_location_id AS Pay_Location_ID, i.paymentrule AS c_order_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.priorityrule AS PriorityRule, i.processed AS c_order_processed, i.processedon AS ProcessedOn, i.promotioncode AS PromotionCode, i.ref_order_id AS Ref_Order_ID, i.salesrep_id AS SalesRep_ID, i.sendemail AS c_order_sendemail, i.totallines AS TotalLines, i.updated AS c_order_updated, i.updatedby AS C_Order_UpdatedBy, i.user1_id AS C_Order_User1_ID, i.user2_id AS C_Order_User2_ID, i.volume AS c_order_volume, i.weight AS c_order_weight, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL,
p.discontinued AS m_product_discontinued, p.discontinuedat AS m_product_discontinuedat, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS m_product_isdropship, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_Product_M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_Product_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS m_product_volume, p.weight AS m_product_weight, po.ad_org_id AS M_Product_PO_AD_Org_ID, po.c_bpartner_id AS M_Product_PO_C_BPartner_ID, po.c_currency_id AS M_Product_PO_C_Currency_ID, po.costperorder AS CostPerOrder, po.created AS m_product_po_created, po.createdby AS M_Product_PO_CreatedBy, po.c_uom_id AS M_Product_PO_C_UOM_ID, po.deliverytime_actual AS DeliveryTime_Actual, po.deliverytime_promised AS DeliveryTime_Promised, po.discontinued AS m_product_po_discontinued, po.discontinuedat AS m_product_po_discontinuedat, po.isactive AS m_product_po_isactive,
po.iscurrentvendor AS IsCurrentVendor, po.manufacturer AS m_product_po_manufacturer, po.m_product_id AS M_Product_PO_M_Product_ID, po.order_min AS Order_Min, po.order_pack AS Order_Pack, po.priceeffective AS PriceEffective, po.pricelastinv AS PriceLastInv, po.pricelastpo AS PriceLastPO, po.pricelist AS m_product_po_pricelist, po.pricepo AS PricePO, po.qualityrating AS m_product_po_qualityrating, po.royaltyamt AS RoyaltyAmt, po.upc AS m_product_po_upc, po.updated AS m_product_po_updated, po.updatedby AS M_Product_PO_UpdatedBy, po.vendorcategory AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_descriptio, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID,
NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID, NULL AS C_BPartner_C_PaymentTerm_ID, NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule, NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount, NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule, NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name,
NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2, NULL AS c_bpartner_location_updated, NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS c_tax_istaxexempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM pp_product_bom b
JOIN c_orderline ol ON b.m_product_id = ol.m_product_id
JOIN c_order i ON ol.c_order_id = i.c_order_id
JOIN m_product bp ON bp.m_product_id = ol.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.isinvoiceprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON p.m_product_id = bl.m_product_id
LEFT JOIN m_product_po po ON p.m_product_id = po.m_product_id AND i.c_bpartner_id = po.c_bpartner_id
JOIN c_uom uom ON ol.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON p.c_uom_id = uomt.c_uom_id
JOIN m_product_trl pt ON pt.m_product_id = bl.m_product_id AND uomt.ad_language = pt.ad_language UNION SELECT i.ad_client_id AS AD_Client_ID, i.ad_org_id AS AD_Org_ID, i.isactive AS IsActive, i.created AS Created, i.createdby AS CreatedBy, i.updated AS Updated, i.updatedby AS UpdatedBy, l.ad_language AS AD_Language, i.c_order_id AS C_Order_ID, NULL AS C_OrderLine_ID, NULL AS C_Tax_ID, NULL AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, NULL AS Line, NULL AS M_Product_ID, NULL AS VendorProductNo, NULL AS QtyOrdered, NULL AS QtyEntered, NULL AS UOMSymbol, NULL AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, NULL AS PriceActual, NULL AS PriceEntered, NULL AS LineNetAmt, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_freightamt, NULL AS IsDescription, NULL AS Link_OrderLine_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_M_Warehouse_ID, NULL AS PriceCost, NULL AS PriceLimit, NULL AS c_orderline_processed, NULL AS QtyDelivered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_OrderLine_User1_ID, NULL AS C_OrderLine_User2_ID, NULL AS C_UOM_AD_Org_ID, NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS IsDefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Order_AD_Org_ID,
NULL AS C_Order_AD_OrgTrx_ID, NULL AS AD_User_ID, NULL AS AmountRefunded, NULL AS AmountTendered, NULL AS Bill_BPartner_ID, NULL AS Bill_Location_ID, NULL AS Bill_User_ID, NULL AS C_Order_C_Activity_ID, NULL AS C_Order_C_BPartner_ID, NULL AS C_Order_C_Campaign_ID, NULL AS C_CashLine_ID, NULL AS C_CashPlanLine_ID, NULL AS C_Order_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Order_C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS ChargeAmt, NULL AS CopyFrom, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_POS_ID, NULL AS C_Order_C_Project_ID, NULL AS c_order_created, NULL AS C_Order_CreatedBy, NULL AS DateAcct, NULL AS DatePrinted, NULL AS c_order_datepromised, NULL AS DeliveryRule, NULL AS DeliveryViaRule, NULL AS c_order_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DropShip_BPartner_ID, NULL AS DropShip_Location_ID, NULL AS DropShip_User_ID, NULL AS c_order_freightamt, NULL AS FreightCostRule, NULL AS GrandTotal, NULL AS InvoiceRule, NULL AS c_order_isactive, NULL AS IsApproved, NULL AS IsCreditApproved, NULL AS IsDelivered, NULL AS c_order_isdiscountprinted, NULL AS c_order_isdropship, NULL AS IsInvoiced, NULL AS IsPayScheduleValid, NULL AS IsPrinted, NULL AS IsSelected, NULL AS c_order_isselfservice, NULL AS IsSOTrx, NULL AS IsTaxIncluded, NULL AS IsTransferred, NULL AS Link_Order_ID, NULL AS C_Order_M_FreightCategory_ID, NULL AS C_Order_M_PriceList_ID, NULL AS C_Order_M_Shipper_ID, NULL AS C_Order_M_Warehouse_ID, NULL AS OrderType, NULL AS Pay_BPartner_ID, NULL AS Pay_Location_ID, NULL AS c_order_paymentrule, NULL AS POReference, NULL AS Posted, NULL AS PriorityRule, NULL AS c_order_processed, NULL AS ProcessedOn, NULL AS PromotionCode, NULL AS Ref_Order_ID, NULL AS SalesRep_ID, NULL AS c_order_sendemail, NULL AS TotalLines, NULL AS c_order_updated, NULL AS C_Order_UpdatedBy, NULL AS C_Order_User1_ID, NULL AS C_Order_User2_ID, NULL AS c_order_volume, NULL AS c_order_weight,
NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS C_TaxCategory_ID, NULL AS M_Product_C_UOM_ID, NULL AS DescriptionURL, NULL AS m_product_discontinued, NULL AS m_product_discontinuedat, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS m_product_isdropship, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_AttributeSet_ID, NULL AS M_AttributeSetInstance_ID, NULL AS M_Product_M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_Product_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS m_product_volume, NULL AS m_product_weight, NULL AS M_Product_PO_AD_Org_ID, NULL AS M_Product_PO_C_BPartner_ID, NULL AS M_Product_PO_C_Currency_ID, NULL AS CostPerOrder, NULL AS m_product_po_created, NULL AS M_Product_PO_CreatedBy, NULL AS M_Product_PO_C_UOM_ID, NULL AS DeliveryTime_Actual, NULL AS DeliveryTime_Promised, NULL AS m_product_po_discontinued, NULL AS m_product_po_discontinuedat, NULL AS m_product_po_isactive, NULL AS IsCurrentVendor, NULL AS m_product_po_manufacturer, NULL AS M_Product_PO_M_Product_ID, NULL AS Order_Min, NULL AS Order_Pack, NULL AS PriceEffective, NULL AS PriceLastInv, NULL AS PriceLastPO, NULL AS m_product_po_pricelist, NULL AS PricePO,
NULL AS m_product_po_qualityrating, NULL AS RoyaltyAmt, NULL AS m_product_po_upc, NULL AS m_product_po_updated, NULL AS M_Product_PO_UpdatedBy, NULL AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_descriptio, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID, NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID, NULL AS C_BPartner_C_PaymentTerm_ID, NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule, NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount,
NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule, NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name, NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2, NULL AS c_bpartner_location_updated, NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID, NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary,
NULL AS c_tax_istaxexempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM c_order i,
ad_language l WHERE l.isbaselanguage = 'N' AND l.issystemlanguage = 'Y' UNION SELECT ot.ad_client_id AS AD_Client_ID, ot.ad_org_id AS AD_Org_ID, ot.isactive AS IsActive, ot.created AS Created, ot.createdby AS CreatedBy, ot.updated AS Updated, ot.updatedby AS UpdatedBy, tt.ad_language AS AD_Language, ot.c_order_id AS C_Order_ID, NULL AS C_OrderLine_ID, ot.c_tax_id AS C_Tax_ID, tt.taxindicator AS TaxIndicator, NULL AS C_BPartner_ID, NULL AS C_BPartner_Location_ID, NULL AS BPName, NULL AS C_Location_ID, NULL AS Line, NULL AS M_Product_ID, NULL AS VendorProductNo, NULL AS QtyOrdered, NULL AS QtyEntered, NULL AS UOMSymbol, tt.name AS Name, NULL AS Description, NULL AS DocumentNote, NULL AS UPC, NULL AS SKU, NULL AS ProductValue, NULL AS ResourceDescription, NULL AS PriceList, NULL AS PriceEnteredList, NULL AS Discount, CASE WHEN ot.istaxincluded = 'Y' THEN ot.taxamt ELSE ot.taxbaseamt END AS PriceActual, CASE WHEN ot.istaxincluded = 'Y' THEN ot.taxamt ELSE ot.taxbaseamt END AS PriceEntered, CASE WHEN ot.istaxincluded = 'Y' THEN NULL ELSE ot.taxamt END AS LineNetAmt, NULL AS ProductDescription, NULL AS ImageURL, NULL AS C_Campaign_ID, NULL AS C_Project_ID, NULL AS C_Activity_ID, NULL AS C_ProjectPhase_ID, NULL AS C_ProjectTask_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_freightamt, NULL AS IsDescription, NULL AS Link_OrderLine_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_M_Warehouse_ID, NULL AS PriceCost, NULL AS PriceLimit, NULL AS c_orderline_processed, NULL AS QtyDelivered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS C_OrderLine_User1_ID, NULL AS C_OrderLine_User2_ID, NULL AS C_UOM_AD_Org_ID,
NULL AS CostingPrecision, NULL AS c_uom_description, NULL AS c_uom_isactive, NULL AS IsDefault, NULL AS c_uom_name, NULL AS StdPrecision, NULL AS UOMType, NULL AS X12DE355, NULL AS C_Order_AD_Org_ID, NULL AS C_Order_AD_OrgTrx_ID, NULL AS AD_User_ID, NULL AS AmountRefunded, NULL AS AmountTendered, NULL AS Bill_BPartner_ID, NULL AS Bill_Location_ID, NULL AS Bill_User_ID, NULL AS C_Order_C_Activity_ID, NULL AS C_Order_C_BPartner_ID, NULL AS C_Order_C_Campaign_ID, NULL AS C_CashLine_ID, NULL AS C_CashPlanLine_ID, NULL AS C_Order_C_Charge_ID, NULL AS C_ConversionType_ID, NULL AS C_Order_C_Currency_ID, NULL AS C_DocType_ID, NULL AS C_DocTypeTarget_ID, NULL AS ChargeAmt, NULL AS CopyFrom, NULL AS C_Payment_ID, NULL AS C_PaymentTerm_ID, NULL AS C_POS_ID, NULL AS C_Order_C_Project_ID, NULL AS c_order_created, NULL AS C_Order_CreatedBy, NULL AS DateAcct, NULL AS DatePrinted, NULL AS c_order_datepromised, NULL AS DeliveryRule, NULL AS DeliveryViaRule, NULL AS c_order_description, NULL AS DocAction, NULL AS DocStatus, NULL AS DocumentNo, NULL AS DropShip_BPartner_ID, NULL AS DropShip_Location_ID, NULL AS DropShip_User_ID, NULL AS c_order_freightamt, NULL AS FreightCostRule, NULL AS GrandTotal, NULL AS InvoiceRule, NULL AS c_order_isactive, NULL AS IsApproved, NULL AS IsCreditApproved, NULL AS IsDelivered, NULL AS c_order_isdiscountprinted, NULL AS c_order_isdropship, NULL AS IsInvoiced, NULL AS IsPayScheduleValid, NULL AS IsPrinted, NULL AS IsSelected, NULL AS c_order_isselfservice, NULL AS IsSOTrx, NULL AS IsTaxIncluded, NULL AS IsTransferred, NULL AS Link_Order_ID, NULL AS C_Order_M_FreightCategory_ID, NULL AS C_Order_M_PriceList_ID, NULL AS C_Order_M_Shipper_ID, NULL AS C_Order_M_Warehouse_ID, NULL AS OrderType, NULL AS Pay_BPartner_ID, NULL AS Pay_Location_ID, NULL AS c_order_paymentrule, NULL AS POReference, NULL AS Posted, NULL AS PriorityRule, NULL AS c_order_processed, NULL AS ProcessedOn, NULL AS PromotionCode, NULL AS Ref_Order_ID, NULL AS SalesRep_ID,
NULL AS c_order_sendemail, NULL AS TotalLines, NULL AS c_order_updated, NULL AS C_Order_UpdatedBy, NULL AS C_Order_User1_ID, NULL AS C_Order_User2_ID, NULL AS c_order_volume, NULL AS c_order_weight, NULL AS M_Product_AD_Org_ID, NULL AS Classification, NULL AS m_product_copyfrom, NULL AS m_product_created, NULL AS M_Product_CreatedBy, NULL AS C_RevenueRecognition_ID, NULL AS C_SubscriptionType_ID, NULL AS C_TaxCategory_ID, NULL AS M_Product_C_UOM_ID, NULL AS DescriptionURL, NULL AS m_product_discontinued, NULL AS m_product_discontinuedat, NULL AS Group1, NULL AS Group2, NULL AS GuaranteeDays, NULL AS GuaranteeDaysMin, NULL AS Help, NULL AS m_product_isactive, NULL AS IsBOM, NULL AS m_product_isdropship, NULL AS IsExcludeAutoDelivery, NULL AS IsInvoicePrintDetails, NULL AS IsPickListPrintDetails, NULL AS IsPurchased, NULL AS m_product_isselfservice, NULL AS IsSold, NULL AS IsStocked, NULL AS m_product_issummary, NULL AS IsVerified, NULL AS IsWebStoreFeatured, NULL AS LowLevel, NULL AS M_AttributeSet_ID, NULL AS M_AttributeSetInstance_ID, NULL AS M_Product_M_FreightCategory_ID, NULL AS M_Locator_ID, NULL AS M_Product_Product_Category_ID, NULL AS m_product_processing, NULL AS ProductType, NULL AS R_MailText_ID, NULL AS M_Product_SalesRep_ID, NULL AS S_ExpenseType_ID, NULL AS ShelfDepth, NULL AS ShelfHeight, NULL AS ShelfWidth, NULL AS S_Resource_ID, NULL AS UnitsPerPack, NULL AS UnitsPerPallet, NULL AS m_product_updated, NULL AS M_Product_UpdatedBy, NULL AS VersionNo, NULL AS m_product_volume, NULL AS m_product_weight, NULL AS M_Product_PO_AD_Org_ID, NULL AS M_Product_PO_C_BPartner_ID, NULL AS M_Product_PO_C_Currency_ID, NULL AS CostPerOrder, NULL AS m_product_po_created, NULL AS M_Product_PO_CreatedBy, NULL AS M_Product_PO_C_UOM_ID, NULL AS DeliveryTime_Actual, NULL AS DeliveryTime_Promised, NULL AS m_product_po_discontinued, NULL AS m_product_po_discontinuedat, NULL AS m_product_po_isactive, NULL AS IsCurrentVendor, NULL AS m_product_po_manufacturer,
NULL AS M_Product_PO_M_Product_ID, NULL AS Order_Min, NULL AS Order_Pack, NULL AS PriceEffective, NULL AS PriceLastInv, NULL AS PriceLastPO, NULL AS m_product_po_pricelist, NULL AS PricePO, NULL AS m_product_po_qualityrating, NULL AS RoyaltyAmt, NULL AS m_product_po_upc, NULL AS m_product_po_updated, NULL AS M_Product_PO_UpdatedBy, NULL AS m_product_po_vendorcategory, NULL AS S_ResourceAssignment_AD_Org_ID, NULL AS AssignDateFrom, NULL AS AssignDateTo, NULL AS s_resourceassignment_created, NULL AS s_resourceassignment_createby, NULL AS s_resourceassignment_isactive, NULL AS IsConfirmed, NULL AS s_resourceassignment_name, NULL AS s_resourceassignment_qty, NULL AS s_resourceassignment_s_resour, NULL AS s_resourceassignment_updated, NULL AS S_ResourceAssignment_UpdatedBy, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded, NULL AS C_BPartner_Product_AD_Org_ID, NULL AS C_BP_Product_C_BPartner_ID, NULL AS c_bpartner_product_created, NULL AS C_BPartner_Product_CreatedBy, NULL AS c_bpartner_product_descriptio, NULL AS c_bpartner_product_isactive, NULL AS IsManufacturer, NULL AS c_bpartner_product_manufactur, NULL AS c_bpartner_product_qualityrat, NULL AS ShelfLifeMinDays, NULL AS c_bpartner_product_shelflifem, NULL AS c_bpartner_product_updated, NULL AS C_BPartner_Product_UpdatedBy, NULL AS c_bpartner_product_vendorcate, NULL AS AcqusitionCost, NULL AS ActualLifeTimeValue, NULL AS c_bpartner_ad_language, NULL AS AD_OrgBP_ID, NULL AS C_BPartner_AD_Org_ID, NULL AS BPartner_Parent_ID, NULL AS C_BP_Group_ID, NULL AS C_Dunning_ID, NULL AS C_Greeting_ID, NULL AS C_InvoiceSchedule_ID, NULL AS C_BPartner_C_PaymentTerm_ID, NULL AS c_bpartner_created, NULL AS C_BPartner_CreatedBy, NULL AS C_BPartner_C_TaxGroup_ID, NULL AS c_bpartner_deliveryrule,
NULL AS c_bpartner_deliveryviarule, NULL AS c_bpartner_description, NULL AS c_bpartner_dunninggrace, NULL AS DUNS, NULL AS c_bpartner_firstsale, NULL AS c_bpartner_flatdiscount, NULL AS c_bpartner_freightcostrule, NULL AS c_bpartner_invoicerule, NULL AS c_bpartner_isactive, NULL AS IsCustomer, NULL AS c_bpartner_isdiscountprinted, NULL AS c_bpartner_isemployee, NULL AS c_bpartner_ismanufacturer, NULL AS IsOneTime, NULL AS IsPOTaxExempt, NULL AS IsProspect, NULL AS IsSalesRep, NULL AS c_bpartner_issummary, NULL AS c_bpartner_istaxexempt, NULL AS IsVendor, NULL AS Logo_ID, NULL AS M_DiscountSchema_ID, NULL AS C_BPartner_M_PriceList_ID, NULL AS NAICS, NULL AS c_bpartner_name, NULL AS c_bpartner_name2, NULL AS NumberEmployees, NULL AS c_bpartner_paymentrule, NULL AS PaymentRulePO, NULL AS PO_DiscountSchema_ID, NULL AS PO_PaymentTerm_ID, NULL AS PO_PriceList_ID, NULL AS c_bpartner_poreference, NULL AS PotentialLifeTimeValue, NULL AS c_bpartner_rating, NULL AS c_bpartner_referenceno, NULL AS C_BPartner_SalesRep_ID, NULL AS SalesVolume, NULL AS c_bpartner_sendemail, NULL AS ShareOfCustomer, NULL AS c_bpartner_shelflifeminpct, NULL AS SO_CreditLimit, NULL AS SOCreditStatus, NULL AS SO_CreditUsed, NULL AS SO_Description, NULL AS c_bpartner_taxid, NULL AS TotalOpenBalance, NULL AS c_bpartner_updated, NULL AS C_BPartner_UpdatedBy, NULL AS c_bpartner_url, NULL AS c_bpartner_value, NULL AS C_BPartner_Location_AD_Org_ID, NULL AS c_bpartner_location_c_bpartne, NULL AS c_bpartner_location_created, NULL AS C_BPartner_Location_CreatedBy, NULL AS C_SalesRegion_ID, NULL AS c_bpartner_location_fax, NULL AS c_bpartner_location_isactive, NULL AS IsBillTo, NULL AS ISDN, NULL AS IsPayFrom, NULL AS IsRemitTo, NULL AS IsShipTo, NULL AS c_bpartner_location_name, NULL AS c_bpartner_location_phone, NULL AS c_bpartner_location_phone2, NULL AS c_bpartner_location_updated, NULL AS C_BPartner_Location_UpdatedBy, NULL AS C_Tax_AD_Org_ID, NULL AS AD_Rule_ID, NULL AS C_Country_ID, NULL AS C_Region_ID,
NULL AS C_Tax_C_TaxCategory_ID, NULL AS c_tax_description, NULL AS c_tax_isactive, NULL AS c_tax_isdefault, NULL AS IsDocumentLevel, NULL AS IsSalesTax, NULL AS c_tax_issummary, NULL AS c_tax_istaxexempt, NULL AS c_tax_name, NULL AS Parent_Tax_ID, NULL AS Rate, NULL AS RequiresTaxCertificate, NULL AS SOPOType, NULL AS To_Country_ID, NULL AS C_Tax_To_Region_ID, NULL AS ValidFrom FROM c_ordertax ot
JOIN c_tax_trl tt ON ot.c_tax_id = tt.c_tax_id
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_PaySelection_Check_vt(AD_Client_ID, AD_Org_ID, AD_Language, C_PaySelection_ID, C_PaySelectionCheck_ID, Org_Location_ID, TaxID, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, BPGreeting, Name, Name2, C_Location_ID, ReferenceNo, POReference, PayDate, PayAmt, AmtInWords, Qty, PaymentRule, DocumentNo, Logo_ID, DocumentType, DocumentTypeNote, Description, BankAccountDescription, C_BP_BankAccount_ID, C_Payment_ID, c_payselectioncheck_created, C_PaySelectionCheck_CreatedBy, c_payselectioncheck_discamt, c_payselectioncheck_isactive, IsGeneratedDraft, IsPrinted, IsReceipt, Processed, c_payselectioncheck_updated, C_PaySelectionCheck_UpdatedBy, C_PaySelection_AD_Org_ID, C_PaySel_C_BankAccount_ID, c_payselection_created, C_PaySelection_CreatedBy, c_payselection_description, c_payselection_isactive, c_payselection_isapproved, c_payselection_name, c_payselection_processed, TotalAmt, c_payselection_updated, C_PaySelection_UpdatedBy, A_City, A_Country, C_Payment_AD_Org_ID, A_EMail, A_Ident_DL, A_Name, A_State, A_Street, A_Zip, C_Activity_ID, C_Payment_C_BPartner_ID, C_Campaign_ID, C_Payment_C_Charge_ID, C_ConversionType_ID, C_Currency_ID, c_payment_chargeamt, CheckNo, C_Payment_C_Invoice_ID, C_Payment_C_Order_ID, C_PaymentBatch_ID, C_Project_ID, c_payment_created, C_Payment_CreatedBy, DateAcct, DateTrx, c_payment_discountamt, DocStatus, c_payment_documentno, c_payment_isactive, IsAllocated, IsApproved, IsDelayedCapture, IsOnline, IsOverUnderPayment, IsPrepayment, IsReconciled, c_payment_isselfservice, Micr, Orig_TrxID, OverUnderAmt, c_payment_payamt, PONum, c_payment_posted, c_payment_processed, c_payment_processedon, R_AuthCode, R_AuthCode_DC, R_AvsAddr, R_AvsZip, R_CVV2Match, Ref_Payment_ID, C_Payment_Reversal_ID, R_Info, RoutingNo, R_PnRef, R_PnRef_DC, R_RespMsg, R_Result, Swipe, TaxAmt, TenderType, TrxType, c_payment_updated, C_Payment_UpdatedBy, User1_ID, User2_ID, VoiceAuthCode, WriteOffAmt, c_bp_acqusitioncost, c_bp_actuallifetimevalue,
c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy) AS SELECT psc.ad_client_id AS AD_Client_ID, psc.ad_org_id AS AD_Org_ID, l.ad_language AS AD_Language, psc.c_payselection_id AS C_PaySelection_ID, psc.c_payselectioncheck_id AS C_PaySelectionCheck_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, p.c_doctype_id AS C_DocType_ID, bp.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2,
bpartnerremitlocation(bp.c_bpartner_id) AS C_Location_ID, bp.referenceno AS ReferenceNo, bp.poreference AS POReference, ps.paydate AS PayDate, psc.payamt AS PayAmt, psc.payamt AS AmtInWords, psc.qty AS Qty, psc.paymentrule AS PaymentRule, psc.documentno AS DocumentNo, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, p.description AS Description, ba.description AS BankAccountDescription, psc.c_bp_bankaccount_id AS C_BP_BankAccount_ID, psc.c_payment_id AS C_Payment_ID, psc.created AS c_payselectioncheck_created, psc.createdby AS C_PaySelectionCheck_CreatedBy, psc.discountamt AS c_payselectioncheck_discamt, psc.isactive AS c_payselectioncheck_isactive, psc.isgenerateddraft AS IsGeneratedDraft, psc.isprinted AS IsPrinted, psc.isreceipt AS IsReceipt, psc.processed AS Processed, psc.updated AS c_payselectioncheck_updated, psc.updatedby AS C_PaySelectionCheck_UpdatedBy, ps.ad_org_id AS C_PaySelection_AD_Org_ID, ps.c_bankaccount_id AS C_PaySel_C_BankAccount_ID, ps.created AS c_payselection_created, ps.createdby AS C_PaySelection_CreatedBy, ps.description AS c_payselection_description, ps.isactive AS c_payselection_isactive, ps.isapproved AS c_payselection_isapproved, ps.name AS c_payselection_name, ps.processed AS c_payselection_processed, ps.totalamt AS TotalAmt, ps.updated AS c_payselection_updated, ps.updatedby AS C_PaySelection_UpdatedBy, p.a_city AS A_City, p.a_country AS A_Country, p.ad_org_id AS C_Payment_AD_Org_ID, p.a_email AS A_EMail, p.a_ident_dl AS A_Ident_DL, p.a_name AS A_Name, p.a_state AS A_State, p.a_street AS A_Street, p.a_zip AS A_Zip, p.c_activity_id AS C_Activity_ID, p.c_bpartner_id AS C_Payment_C_BPartner_ID, p.c_campaign_id AS C_Campaign_ID, p.c_charge_id AS C_Payment_C_Charge_ID, p.c_conversiontype_id AS C_ConversionType_ID, p.c_currency_id AS C_Currency_ID, p.chargeamt AS c_payment_chargeamt, p.checkno AS CheckNo, p.c_invoice_id AS C_Payment_C_Invoice_ID,
p.c_order_id AS C_Payment_C_Order_ID, p.c_paymentbatch_id AS C_PaymentBatch_ID, p.c_project_id AS C_Project_ID, p.created AS c_payment_created, p.createdby AS C_Payment_CreatedBy, p.dateacct AS DateAcct, p.datetrx AS DateTrx, p.discountamt AS c_payment_discountamt, p.docstatus AS DocStatus, p.documentno AS c_payment_documentno, p.isactive AS c_payment_isactive, p.isallocated AS IsAllocated, p.isapproved AS IsApproved, p.isdelayedcapture AS IsDelayedCapture, p.isonline AS IsOnline, p.isoverunderpayment AS IsOverUnderPayment, p.isprepayment AS IsPrepayment, p.isreconciled AS IsReconciled, p.isselfservice AS c_payment_isselfservice, p.micr AS Micr, p.orig_trxid AS Orig_TrxID, p.overunderamt AS OverUnderAmt, p.payamt AS c_payment_payamt, p.ponum AS PONum, p.posted AS c_payment_posted, p.processed AS c_payment_processed, p.processedon AS c_payment_processedon, p.r_authcode AS R_AuthCode, p.r_authcode_dc AS R_AuthCode_DC, p.r_avsaddr AS R_AvsAddr, p.r_avszip AS R_AvsZip, p.r_cvv2match AS R_CVV2Match, p.ref_payment_id AS Ref_Payment_ID, p.reversal_id AS C_Payment_Reversal_ID, p.r_info AS R_Info, p.routingno AS RoutingNo, p.r_pnref AS R_PnRef, p.r_pnref_dc AS R_PnRef_DC, p.r_respmsg AS R_RespMsg, p.r_result AS R_Result, p.swipe AS Swipe, p.taxamt AS TaxAmt, p.tendertype AS TenderType, p.trxtype AS TrxType, p.updated AS c_payment_updated, p.updatedby AS C_Payment_UpdatedBy, p.user1_id AS User1_ID, p.user2_id AS User2_ID, p.voiceauthcode AS VoiceAuthCode, p.writeoffamt AS WriteOffAmt, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created,
bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, bpgt.ad_org_id AS C_Greeting_AD_Org_ID, bpgt.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpgt.name AS c_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID,
oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy FROM c_payselectioncheck psc
JOIN c_payselection ps ON psc.c_payselection_id = ps.c_payselection_id
LEFT JOIN c_payment p ON psc.c_payment_id = p.c_payment_id
LEFT JOIN c_bankaccount ba ON p.c_bankaccount_id = ba.c_bankaccount_id
LEFT JOIN c_doctype_trl dt ON p.c_doctype_id = dt.c_doctype_id
JOIN c_bpartner bp ON psc.c_bpartner_id = bp.c_bpartner_id
JOIN ad_orginfo oi ON psc.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON psc.ad_client_id = ci.ad_client_id
LEFT JOIN ad_language l ON l.issystemlanguage = 'Y'
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id AND bpgt.ad_language = l.ad_language
;

-- Nov 28, 2021, 11:19:17 PM CET
CREATE OR REPLACE VIEW C_PaySelection_Remittance_vt(AD_Client_ID, AD_Org_ID, AD_Language, C_PaySelection_ID, C_PaySelectionLine_ID, C_PaySelectionCheck_ID, PaymentRule, Line, OpenAmt, PayAmt, DiscountAmt, DifferenceAmt, C_BPartner_ID, DocumentNo, DateInvoiced, GrandTotal, AmtInWords, C_Invoice_ID, c_payselectionline_created, C_PaySelectionLine_CreatedBy, c_payselectionline_description, c_payselectionline_isactive, IsManual, c_payselectionline_issotrx, Processed, c_payselectionline_updated, C_PaySelectionLine_UpdatedBy, C_Invoice_AD_Org_ID, AD_OrgTrx_ID, AD_User_ID, C_Activity_ID, C_BPartner_Location_ID, C_Campaign_ID, C_Charge_ID, C_ConversionType_ID, C_Currency_ID, C_DocType_ID, C_DocTypeTarget_ID, C_DunningLevel_ID, ChargeAmt, C_Payment_ID, C_PaymentTerm_ID, C_Project_ID, c_invoice_created, C_Invoice_CreatedBy, DateAcct, DateOrdered, DatePrinted, c_invoice_description, DocAction, DocStatus, DunningGrace, GenerateTo, InvoiceCollectionType, c_invoice_isactive, IsApproved, IsDiscountPrinted, IsInDispute, IsPayScheduleValid, IsPrinted, c_invoice_isselfservice, c_invoice_issotrx, IsTaxIncluded, IsTransferred, M_PriceList_ID, M_RMA_ID, c_invoice_paymentrule, POReference, Posted, ProcessedOn, Processing, Ref_Invoice_ID, Reversal_ID, SendEMail, TotalLines, c_invoice_updated, C_Invoice_UpdatedBy, User1_ID, User2_ID) AS SELECT psl.ad_client_id AS AD_Client_ID, psl.ad_org_id AS AD_Org_ID, l.ad_language AS AD_Language, psl.c_payselection_id AS C_PaySelection_ID, psl.c_payselectionline_id AS C_PaySelectionLine_ID, psl.c_payselectioncheck_id AS C_PaySelectionCheck_ID, psl.paymentrule AS PaymentRule, psl.line AS Line, psl.openamt AS OpenAmt, psl.payamt AS PayAmt, psl.discountamt AS DiscountAmt, psl.differenceamt AS DifferenceAmt, i.c_bpartner_id AS C_BPartner_ID, i.documentno AS DocumentNo, i.dateinvoiced AS DateInvoiced, i.grandtotal AS GrandTotal, i.grandtotal AS AmtInWords, psl.c_invoice_id AS C_Invoice_ID, psl.created AS c_payselectionline_created,
psl.createdby AS C_PaySelectionLine_CreatedBy, psl.description AS c_payselectionline_description, psl.isactive AS c_payselectionline_isactive, psl.ismanual AS IsManual, psl.issotrx AS c_payselectionline_issotrx, psl.processed AS Processed, psl.updated AS c_payselectionline_updated, psl.updatedby AS C_PaySelectionLine_UpdatedBy, i.ad_org_id AS C_Invoice_AD_Org_ID, i.ad_orgtrx_id AS AD_OrgTrx_ID, i.ad_user_id AS AD_User_ID, i.c_activity_id AS C_Activity_ID, i.c_bpartner_location_id AS C_BPartner_Location_ID, i.c_campaign_id AS C_Campaign_ID, i.c_charge_id AS C_Charge_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_currency_id AS C_Currency_ID, i.c_doctype_id AS C_DocType_ID, i.c_doctypetarget_id AS C_DocTypeTarget_ID, i.c_dunninglevel_id AS C_DunningLevel_ID, i.chargeamt AS ChargeAmt, i.c_payment_id AS C_Payment_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.c_project_id AS C_Project_ID, i.created AS c_invoice_created, i.createdby AS C_Invoice_CreatedBy, i.dateacct AS DateAcct, i.dateordered AS DateOrdered, i.dateprinted AS DatePrinted, i.description AS c_invoice_description, i.docaction AS DocAction, i.docstatus AS DocStatus, i.dunninggrace AS DunningGrace, i.generateto AS GenerateTo, i.invoicecollectiontype AS InvoiceCollectionType, i.isactive AS c_invoice_isactive, i.isapproved AS IsApproved, i.isdiscountprinted AS IsDiscountPrinted, i.isindispute AS IsInDispute, i.ispayschedulevalid AS IsPayScheduleValid, i.isprinted AS IsPrinted, i.isselfservice AS c_invoice_isselfservice, i.issotrx AS c_invoice_issotrx, i.istaxincluded AS IsTaxIncluded, i.istransferred AS IsTransferred, i.m_pricelist_id AS M_PriceList_ID, i.m_rma_id AS M_RMA_ID, i.paymentrule AS c_invoice_paymentrule, i.poreference AS POReference, i.posted AS Posted, i.processedon AS ProcessedOn, i.processing AS Processing, i.ref_invoice_id AS Ref_Invoice_ID, i.reversal_id AS Reversal_ID, i.sendemail AS SendEMail, i.totallines AS TotalLines, i.updated AS c_invoice_updated,
i.updatedby AS C_Invoice_UpdatedBy, i.user1_id AS User1_ID, i.user2_id AS User2_ID FROM c_payselectionline psl
JOIN c_invoice i ON psl.c_invoice_id = i.c_invoice_id
JOIN ad_language l ON l.issystemlanguage = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW C_Project_Details_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Project_ID, C_ProjectLine_ID, Line, PlannedQty, PlannedPrice, PlannedAmt, PlannedMarginAmt, CommittedAmt, M_Product_ID, Name, Description, DocumentNote, UPC, SKU, ProductValue, M_Product_Category_ID, InvoicedAmt, InvoicedQty, CommittedQty, C_Order_ID, C_OrderPO_ID, C_ProjectIssue_ID, C_ProjectPhase_ID, C_ProjectTask_ID, DoPricing, IsPrinted, c_projectline_processed, C_Project_AD_Org_ID, AD_User_ID, C_BPartner_ID, C_BPartner_Location_ID, C_BPartnerSR_ID, C_Campaign_ID, C_Currency_ID, c_project_committedamt, c_project_committedqty, c_project_copyfrom, C_PaymentTerm_ID, C_Phase_ID, C_ProjectType_ID, c_project_created, C_Project_CreatedBy, DateContract, DateFinish, c_project_description, GenerateTo, c_project_invoicedamt, c_project_invoicedqty, c_project_isactive, IsCommitCeiling, IsCommitment, c_project_issummary, M_PriceList_Version_ID, M_Warehouse_ID, c_project_name, c_project_note, c_project_plannedamt, c_project_plannedmarginamt, c_project_plannedqty, POReference, c_project_processed, c_project_processing, ProjectBalanceAmt, ProjectCategory, ProjectLineLevel, ProjInvoiceRule, C_Project_SalesRep_ID, c_project_updated, C_Project_UpdatedBy, c_project_value, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, C_TaxCategory_ID, C_UOM_ID, m_product_description, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, ImageURL, m_product_isactive, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, IsSelfService, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_AttributeSet_ID, M_AttributeSetInstance_ID, M_FreightCategory_ID, M_Locator_ID, M_Product_M_Prod_Category_ID, M_Product_M_Product_ID, m_product_processing, ProductType,
R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight) AS SELECT pl.ad_client_id AS AD_Client_ID, pl.ad_org_id AS AD_Org_ID, pl.isactive AS IsActive, pl.created AS Created, pl.createdby AS CreatedBy, pl.updated AS Updated, pl.updatedby AS UpdatedBy, l.ad_language AS AD_Language, pj.c_project_id AS C_Project_ID, pl.c_projectline_id AS C_ProjectLine_ID, pl.line AS Line, pl.plannedqty AS PlannedQty, pl.plannedprice AS PlannedPrice, pl.plannedamt AS PlannedAmt, pl.plannedmarginamt AS PlannedMarginAmt, pl.committedamt AS CommittedAmt, pl.m_product_id AS M_Product_ID, COALESCE(pt.name, pl.description) AS Name, CASE WHEN pt.name IS NOT NULL THEN pl.description ELSE NULL END AS Description, pt.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, pl.m_product_category_id AS M_Product_Category_ID, pl.invoicedamt AS InvoicedAmt, pl.invoicedqty AS InvoicedQty, pl.committedqty AS CommittedQty, pl.c_order_id AS C_Order_ID, pl.c_orderpo_id AS C_OrderPO_ID, pl.c_projectissue_id AS C_ProjectIssue_ID, pl.c_projectphase_id AS C_ProjectPhase_ID, pl.c_projecttask_id AS C_ProjectTask_ID, pl.dopricing AS DoPricing, pl.isprinted AS IsPrinted, pl.processed AS c_projectline_processed, pj.ad_org_id AS C_Project_AD_Org_ID, pj.ad_user_id AS AD_User_ID, pj.c_bpartner_id AS C_BPartner_ID, pj.c_bpartner_location_id AS C_BPartner_Location_ID, pj.c_bpartnersr_id AS C_BPartnerSR_ID, pj.c_campaign_id AS C_Campaign_ID, pj.c_currency_id AS C_Currency_ID, pj.committedamt AS c_project_committedamt, pj.committedqty AS c_project_committedqty, pj.copyfrom AS c_project_copyfrom, pj.c_paymentterm_id AS C_PaymentTerm_ID, pj.c_phase_id AS C_Phase_ID, pj.c_projecttype_id AS C_ProjectType_ID, pj.created AS c_project_created, pj.createdby AS C_Project_CreatedBy, pj.datecontract AS DateContract, pj.datefinish AS DateFinish,
pj.description AS c_project_description, pj.generateto AS GenerateTo, pj.invoicedamt AS c_project_invoicedamt, pj.invoicedqty AS c_project_invoicedqty, pj.isactive AS c_project_isactive, pj.iscommitceiling AS IsCommitCeiling, pj.iscommitment AS IsCommitment, pj.issummary AS c_project_issummary, pj.m_pricelist_version_id AS M_PriceList_Version_ID, pj.m_warehouse_id AS M_Warehouse_ID, pj.name AS c_project_name, pj.note AS c_project_note, pj.plannedamt AS c_project_plannedamt, pj.plannedmarginamt AS c_project_plannedmarginamt, pj.plannedqty AS c_project_plannedqty, pj.poreference AS POReference, pj.processed AS c_project_processed, pj.processing AS c_project_processing, pj.projectbalanceamt AS ProjectBalanceAmt, pj.projectcategory AS ProjectCategory, pj.projectlinelevel AS ProjectLineLevel, pj.projinvoicerule AS ProjInvoiceRule, pj.salesrep_id AS C_Project_SalesRep_ID, pj.updated AS c_project_updated, pj.updatedby AS C_Project_UpdatedBy, pj.value AS c_project_value, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS C_TaxCategory_ID, p.c_uom_id AS C_UOM_ID, pt.description AS m_product_description, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, p.imageurl AS ImageURL, pt.isactive AS m_product_isactive, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS IsSelfService, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary,
p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_AttributeSet_ID, p.m_attributesetinstance_id AS M_AttributeSetInstance_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.m_product_id AS M_Product_M_Product_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight FROM c_projectline pl
JOIN c_project pj ON pl.c_project_id = pj.c_project_id
LEFT JOIN m_product p ON pl.m_product_id = p.m_product_id
LEFT JOIN m_product_trl pt ON pl.m_product_id = pt.m_product_id
JOIN ad_language l ON l.issystemlanguage = 'Y' WHERE pl.isprinted = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW C_Project_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_Project_ID, Value, ProjectName, Description, Note, IsSummary, ProjectCategory, Org_Location_ID, TaxID, C_ProjectType_ID, ProjectTypeName, C_Phase_ID, ProjectPhaseName, SalesRep_ID, SalesRep_Name, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, ReferenceNo, PaymentTerm, PaymentTermNote, POReference, C_Currency_ID, M_PriceList_Version_ID, C_Campaign_ID, PlannedAmt, PlannedQty, PlannedMarginAmt, InvoicedAmt, InvoicedQty, ProjectBalanceAmt, IsCommitment, CommittedAmt, CommittedQty, DateContract, DateFinish, IsCommitCeiling, M_Warehouse_ID, Logo_ID, AD_User_ID, C_BPartner_Location_ID, CopyFrom, C_Project_C_PaymentTerm_ID, GenerateTo, c_project_processed, c_project_processing, ProjectLineLevel, ProjInvoiceRule, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID, C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused,
c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, C_ProjectType_AD_Org_ID, c_projecttype_description, c_projecttype_help, c_projecttype_isactive, c_projecttype_projectcategory, C_Phase_AD_Org_ID, C_Phase_C_Phase_ID, C_Phase_C_ProjectType_ID, c_phase_description, c_phase_help, c_phase_isactive, C_Phase_M_Product_ID, c_phase_seqno, StandardQty, SalesRep_AD_Org_ID, SalesRep_AD_OrgTrx_ID, salesrep_ad_birthday, SalesRep_C_BPartner_ID, SalesRep_C_BP_Location_ID, SalesRep_C_Greeting_ID, salesrep_comments, salesrep_created, SalesRep_CreatedBy, salesrep_description, salesrep_email, salesrep_fax, salesrep_isactive, salesrep_lastcontact, salesrep_lastresult, salesrep_phone, salesrep_phone2, SalesRep_Supervisor_ID, salesrep_title, salesrep_updated, SalesRep_UpdatedBy, salesrep_value, salesrep_bp_acqusitioncost, salesrep_bp_actuallifevalue, salesrep_bp_ad_language, SalesRep_BP_AD_OrgBP_ID, SalesRep_BP_AD_Org_ID, SalesRep_BP_BPartner_Parent_ID, SalesRep_BP_C_BP_Group_ID, SalesRep_BP_C_Dunning_ID, SalesRep_BP_C_Greeting_ID, SalesRep_BP_C_InvoiceSched_ID, SalesRep_BP_C_PaymentTerm_ID, salesrep_bp_created, SalesRep_BP_CreatedBy, SalesRep_BP_C_TaxGroup_ID, salesrep_bp_deliveryrule, salesrep_bp_deliveryviarule, salesrep_bp_description, salesrep_bp_dunninggrace, salesrep_bp_duns, salesrep_bp_firstsale, salesrep_bp_flatdiscount, salesrep_bp_freightcostrule, salesrep_bp_invoicerule, salesrep_bp_isactive, salesrep_bp_iscustomer, salesrep_bp_isdiscountprinted, salesrep_bp_isemployee, salesrep_bp_ismanufacturer, salesrep_bp_isonetime, salesrep_bp_ispotaxexempt, salesrep_bp_isprospect, salesrep_bp_issalesrep,
salesrep_bp_issummary, salesrep_bp_istaxexempt, salesrep_bp_isvendor, SalesRep_BP_Logo_ID, SalesRep_BP_M_DiscountSchm_ID, SalesRep_BP_M_PriceList_ID, salesrep_bp_naics, salesrep_bp_name2, salesrep_bp_numberemployees, salesrep_bp_paymentrule, salesrep_bp_paymentrulepo, SalesRep_BP_PO_DiscountSchm_ID, SalesRep_BP_PO_PaymentTerm_ID, SalesRep_BP_PO_PriceList_ID, salesrep_bp_poreference, salesrep_bp_potentiallifevalue, salesrep_bp_rating, salesrep_bp_referenceno, SalesRep_BP_SalesRep_ID, salesrep_bp_salesvolume, salesrep_bp_sendemail, salesrep_bp_shareofcustomer, salesrep_bp_shelflifeminpct, salesrep_bp_so_creditlimit, salesrep_bp_socreditstatus, salesrep_bp_so_creditused, salesrep_bp_so_description, salesrep_bp_taxid, salesrep_bp_totalopenbalance, salesrep_bp_updated, SalesRep_BP_UpdatedBy, salesrep_bp_url, salesrep_bp_value, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, AD_User_AD_Org_ID, AD_User_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, C_User_Greeting_C_Greeting_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, C_PaymentTerm_AD_Org_ID, AfterDelivery, C_PaymentTerm_C_PaymentTerm_ID, c_paymentterm_description, Discount, Discount2, DiscountDays, DiscountDays2, FixMonthCutoff, FixMonthDay, FixMonthOffset, GraceDays, c_paymentterm_isactive, IsDefault,
IsDueFixed, IsNextBusinessDay, IsValid, NetDay, NetDays, PaymentTermUsage, c_paymentterm_value) AS SELECT p.ad_client_id AS AD_Client_ID, p.ad_org_id AS AD_Org_ID, p.isactive AS IsActive, p.created AS Created, p.createdby AS CreatedBy, p.updated AS Updated, p.updatedby AS UpdatedBy, ptt.ad_language AS AD_Language, p.c_project_id AS C_Project_ID, p.value AS Value, p.name AS ProjectName, p.description AS Description, p.note AS Note, p.issummary AS IsSummary, p.projectcategory AS ProjectCategory, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, p.c_projecttype_id AS C_ProjectType_ID, pjt.name AS ProjectTypeName, p.c_phase_id AS C_Phase_ID, pjp.name AS ProjectPhaseName, p.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, p.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcgt.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, bp.referenceno AS ReferenceNo, ptt.name AS PaymentTerm, ptt.documentnote AS PaymentTermNote, p.poreference AS POReference, p.c_currency_id AS C_Currency_ID, p.m_pricelist_version_id AS M_PriceList_Version_ID, p.c_campaign_id AS C_Campaign_ID, p.plannedamt AS PlannedAmt, p.plannedqty AS PlannedQty, p.plannedmarginamt AS PlannedMarginAmt, p.invoicedamt AS InvoicedAmt, p.invoicedqty AS InvoicedQty, p.projectbalanceamt AS ProjectBalanceAmt, p.iscommitment AS IsCommitment, p.committedamt AS CommittedAmt, p.committedqty AS CommittedQty, p.datecontract AS DateContract, p.datefinish AS DateFinish, p.iscommitceiling AS IsCommitCeiling, p.m_warehouse_id AS M_Warehouse_ID, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, p.ad_user_id AS AD_User_ID, p.c_bpartner_location_id AS C_BPartner_Location_ID, p.copyfrom AS CopyFrom, p.c_paymentterm_id AS C_Project_C_PaymentTerm_ID, p.generateto AS GenerateTo,
p.processed AS c_project_processed, p.processing AS c_project_processing, p.projectlinelevel AS ProjectLineLevel, p.projinvoicerule AS ProjInvoiceRule, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary, bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID,
bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, pjt.ad_org_id AS C_ProjectType_AD_Org_ID, pjt.description AS c_projecttype_description, pjt.help AS c_projecttype_help, pjt.isactive AS c_projecttype_isactive, pjt.projectcategory AS c_projecttype_projectcategory, pjp.ad_org_id AS C_Phase_AD_Org_ID, pjp.c_phase_id AS C_Phase_C_Phase_ID, pjp.c_projecttype_id AS C_Phase_C_ProjectType_ID, pjp.description AS c_phase_description, pjp.help AS c_phase_help, pjp.isactive AS c_phase_isactive, pjp.m_product_id AS C_Phase_M_Product_ID, pjp.seqno AS c_phase_seqno, pjp.standardqty AS StandardQty, u.ad_org_id AS SalesRep_AD_Org_ID, u.ad_orgtrx_id AS SalesRep_AD_OrgTrx_ID, u.birthday AS salesrep_ad_birthday, u.c_bpartner_id AS SalesRep_C_BPartner_ID, u.c_bpartner_location_id AS SalesRep_C_BP_Location_ID, u.c_greeting_id AS SalesRep_C_Greeting_ID, u.comments AS salesrep_comments, u.created AS salesrep_created, u.createdby AS SalesRep_CreatedBy,
u.description AS salesrep_description, u.email AS salesrep_email, u.fax AS salesrep_fax, u.isactive AS salesrep_isactive, u.lastcontact AS salesrep_lastcontact, u.lastresult AS salesrep_lastresult, u.phone AS salesrep_phone, u.phone2 AS salesrep_phone2, u.supervisor_id AS SalesRep_Supervisor_ID, u.title AS salesrep_title, u.updated AS salesrep_updated, u.updatedby AS SalesRep_UpdatedBy, u.value AS salesrep_value, ubp.acqusitioncost AS salesrep_bp_acqusitioncost, ubp.actuallifetimevalue AS salesrep_bp_actuallifevalue, ubp.ad_language AS salesrep_bp_ad_language, ubp.ad_orgbp_id AS SalesRep_BP_AD_OrgBP_ID, ubp.ad_org_id AS SalesRep_BP_AD_Org_ID, ubp.bpartner_parent_id AS SalesRep_BP_BPartner_Parent_ID, ubp.c_bp_group_id AS SalesRep_BP_C_BP_Group_ID, ubp.c_dunning_id AS SalesRep_BP_C_Dunning_ID, ubp.c_greeting_id AS SalesRep_BP_C_Greeting_ID, ubp.c_invoiceschedule_id AS SalesRep_BP_C_InvoiceSched_ID, ubp.c_paymentterm_id AS SalesRep_BP_C_PaymentTerm_ID, ubp.created AS salesrep_bp_created, ubp.createdby AS SalesRep_BP_CreatedBy, ubp.c_taxgroup_id AS SalesRep_BP_C_TaxGroup_ID, ubp.deliveryrule AS salesrep_bp_deliveryrule, ubp.deliveryviarule AS salesrep_bp_deliveryviarule, ubp.description AS salesrep_bp_description, ubp.dunninggrace AS salesrep_bp_dunninggrace, ubp.duns AS salesrep_bp_duns, ubp.firstsale AS salesrep_bp_firstsale, ubp.flatdiscount AS salesrep_bp_flatdiscount, ubp.freightcostrule AS salesrep_bp_freightcostrule, ubp.invoicerule AS salesrep_bp_invoicerule, ubp.isactive AS salesrep_bp_isactive, ubp.iscustomer AS salesrep_bp_iscustomer, ubp.isdiscountprinted AS salesrep_bp_isdiscountprinted, ubp.isemployee AS salesrep_bp_isemployee, ubp.ismanufacturer AS salesrep_bp_ismanufacturer, ubp.isonetime AS salesrep_bp_isonetime, ubp.ispotaxexempt AS salesrep_bp_ispotaxexempt, ubp.isprospect AS salesrep_bp_isprospect, ubp.issalesrep AS salesrep_bp_issalesrep, ubp.issummary AS salesrep_bp_issummary, ubp.istaxexempt AS salesrep_bp_istaxexempt,
ubp.isvendor AS salesrep_bp_isvendor, ubp.logo_id AS SalesRep_BP_Logo_ID, ubp.m_discountschema_id AS SalesRep_BP_M_DiscountSchm_ID, ubp.m_pricelist_id AS SalesRep_BP_M_PriceList_ID, ubp.naics AS salesrep_bp_naics, ubp.name2 AS salesrep_bp_name2, ubp.numberemployees AS salesrep_bp_numberemployees, ubp.paymentrule AS salesrep_bp_paymentrule, ubp.paymentrulepo AS salesrep_bp_paymentrulepo, ubp.po_discountschema_id AS SalesRep_BP_PO_DiscountSchm_ID, ubp.po_paymentterm_id AS SalesRep_BP_PO_PaymentTerm_ID, ubp.po_pricelist_id AS SalesRep_BP_PO_PriceList_ID, ubp.poreference AS salesrep_bp_poreference, ubp.potentiallifetimevalue AS salesrep_bp_potentiallifevalue, ubp.rating AS salesrep_bp_rating, ubp.referenceno AS salesrep_bp_referenceno, ubp.salesrep_id AS SalesRep_BP_SalesRep_ID, ubp.salesvolume AS salesrep_bp_salesvolume, ubp.sendemail AS salesrep_bp_sendemail, ubp.shareofcustomer AS salesrep_bp_shareofcustomer, ubp.shelflifeminpct AS salesrep_bp_shelflifeminpct, ubp.so_creditlimit AS salesrep_bp_so_creditlimit, ubp.socreditstatus AS salesrep_bp_socreditstatus, ubp.so_creditused AS salesrep_bp_so_creditused, ubp.so_description AS salesrep_bp_so_description, ubp.taxid AS salesrep_bp_taxid, ubp.totalopenbalance AS salesrep_bp_totalopenbalance, ubp.updated AS salesrep_bp_updated, ubp.updatedby AS SalesRep_BP_UpdatedBy, ubp.url AS salesrep_bp_url, ubp.value AS salesrep_bp_value, bpgt.ad_org_id AS C_Greeting_AD_Org_ID, bpgt.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpgt.name AS c_greeting_name, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax,
bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcgt.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcgt.c_greeting_id AS C_User_Greeting_C_Greeting_ID, bpcgt.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcgt.name AS c_user_greeting_name, bpl.ad_org_id AS C_BP_Location_AD_Org_ID, bpl.c_bpartner_id AS C_BP_Location_C_BPartner_ID, bpl.created AS c_bp_location_created, bpl.createdby AS C_BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bp_location_fax, bpl.isactive AS c_bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bp_location_name, bpl.phone AS c_bp_location_phone, bpl.phone2 AS c_bp_location_phone2, bpl.updated AS c_bp_location_updated, bpl.updatedby AS C_BP_Location_UpdatedBy, ptt.ad_org_id AS C_PaymentTerm_AD_Org_ID, pt.afterdelivery AS AfterDelivery, pt.c_paymentterm_id AS C_PaymentTerm_C_PaymentTerm_ID, ptt.description AS c_paymentterm_description, pt.discount AS Discount, pt.discount2 AS Discount2, pt.discountdays AS DiscountDays, pt.discountdays2 AS DiscountDays2, pt.fixmonthcutoff AS FixMonthCutoff, pt.fixmonthday AS FixMonthDay, pt.fixmonthoffset AS FixMonthOffset, pt.gracedays AS GraceDays, ptt.isactive AS c_paymentterm_isactive, pt.isdefault AS IsDefault, pt.isduefixed AS IsDueFixed, pt.isnextbusinessday AS IsNextBusinessDay, pt.isvalid AS IsValid, pt.netday AS NetDay, pt.netdays AS NetDays, pt.paymenttermusage AS PaymentTermUsage, pt.value AS c_paymentterm_value FROM c_project p
LEFT JOIN c_bpartner bp ON p.c_bpartner_id = bp.c_bpartner_id
JOIN ad_orginfo oi ON p.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON p.ad_client_id = ci.ad_client_id
LEFT JOIN c_paymentterm pt ON p.c_paymentterm_id = pt.c_paymentterm_id
LEFT JOIN c_paymentterm_trl ptt ON p.c_paymentterm_id = ptt.c_paymentterm_id
LEFT JOIN c_projecttype pjt ON p.c_projecttype_id = pjt.c_projecttype_id
LEFT JOIN c_phase pjp ON p.c_phase_id = pjp.c_phase_id
LEFT JOIN ad_user u ON p.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id
LEFT JOIN ad_user bpc ON p.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_greeting_trl bpcgt ON bpc.c_greeting_id = bpcgt.c_greeting_id
LEFT JOIN c_bpartner_location bpl ON p.c_bpartner_location_id = bpl.c_bpartner_location_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW C_RfQResponseLineQty_vt(C_RfQResponseLine_ID, C_RfQResponseLineQty_ID, C_RfQLineQty_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, C_UOM_ID, UOMSymbol, Qty, Price, Discount) AS SELECT rq.c_rfqresponseline_id AS C_RfQResponseLine_ID, rq.c_rfqresponselineqty_id AS C_RfQResponseLineQty_ID, rq.c_rfqlineqty_id AS C_RfQLineQty_ID, rq.ad_client_id AS AD_Client_ID, rq.ad_org_id AS AD_Org_ID, rq.isactive AS IsActive, rq.created AS Created, rq.createdby AS CreatedBy, rq.updated AS Updated, rq.updatedby AS UpdatedBy, l.ad_language AS AD_Language, q.c_uom_id AS C_UOM_ID, uom.uomsymbol AS UOMSymbol, q.qty AS Qty, rq.price AS Price, rq.discount AS Discount FROM c_rfqresponselineqty rq
JOIN c_rfqlineqty q ON rq.c_rfqlineqty_id = q.c_rfqlineqty_id
JOIN c_uom uom ON q.c_uom_id = uom.c_uom_id
JOIN ad_language l ON l.issystemlanguage = 'Y' WHERE rq.isactive = 'Y' AND q.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW C_RfQResponseLine_vt(C_RfQResponse_ID, C_RfQResponseLine_ID, C_RfQLine_ID, C_RfQResponseLineQty_ID, C_RfQLineQty_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Line, M_Product_ID, M_AttributeSetInstance_ID, Name, Description, DocumentNote, UPC, SKU, ProductValue, Help, DateWorkStart, DeliveryDays, C_UOM_ID, UOMSymbol, Qty, Price, Discount) AS SELECT rrl.c_rfqresponse_id AS C_RfQResponse_ID, rrl.c_rfqresponseline_id AS C_RfQResponseLine_ID, rrl.c_rfqline_id AS C_RfQLine_ID, rq.c_rfqresponselineqty_id AS C_RfQResponseLineQty_ID, rq.c_rfqlineqty_id AS C_RfQLineQty_ID, rrl.ad_client_id AS AD_Client_ID, rrl.ad_org_id AS AD_Org_ID, rrl.isactive AS IsActive, rrl.created AS Created, rrl.createdby AS CreatedBy, rrl.updated AS Updated, rrl.updatedby AS UpdatedBy, l.ad_language AS AD_Language, rl.line AS Line, rl.m_product_id AS M_Product_ID, rl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, COALESCE(p.name || productattribute(rl.m_attributesetinstance_id), rl.description) AS Name, CASE WHEN p.name IS NOT NULL THEN rl.description ELSE NULL END AS Description, p.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, rl.help AS Help, rl.dateworkstart AS DateWorkStart, rl.deliverydays AS DeliveryDays, q.c_uom_id AS C_UOM_ID, uom.uomsymbol AS UOMSymbol, q.qty AS Qty, rq.price AS Price, rq.discount AS Discount FROM c_rfqresponselineqty rq
JOIN c_rfqlineqty q ON rq.c_rfqlineqty_id = q.c_rfqlineqty_id
JOIN c_uom uom ON q.c_uom_id = uom.c_uom_id
JOIN c_rfqresponseline rrl ON rq.c_rfqresponseline_id = rrl.c_rfqresponseline_id
JOIN c_rfqline rl ON rrl.c_rfqline_id = rl.c_rfqline_id
LEFT JOIN m_product p ON rl.m_product_id = p.m_product_id
JOIN ad_language l ON l.issystemlanguage = 'Y' WHERE rq.isactive = 'Y' AND q.isactive = 'Y' AND rrl.isactive = 'Y' AND rl.isactive = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW C_RfQResponse_vt(C_RfQResponse_ID, C_RfQ_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Org_Location_ID, TaxID, Name, Description, Help, C_Currency_ID, ISO_Code, DateResponse, DateWorkStart, DeliveryDays, C_BPartner_ID, BPName, BPName2, C_BPartner_Location_ID, C_Location_ID, AD_User_ID, Title, Phone, ContactName) AS SELECT rr.c_rfqresponse_id AS C_RfQResponse_ID, rr.c_rfq_id AS C_RfQ_ID, rr.ad_client_id AS AD_Client_ID, rr.ad_org_id AS AD_Org_ID, rr.isactive AS IsActive, rr.created AS Created, rr.createdby AS CreatedBy, rr.updated AS Updated, rr.updatedby AS UpdatedBy, l.ad_language AS AD_Language, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, r.name AS Name, r.description AS Description, r.help AS Help, r.c_currency_id AS C_Currency_ID, c.iso_code AS ISO_Code, r.dateresponse AS DateResponse, r.dateworkstart AS DateWorkStart, r.deliverydays AS DeliveryDays, rr.c_bpartner_id AS C_BPartner_ID, bp.name AS BPName, bp.name2 AS BPName2, rr.c_bpartner_location_id AS C_BPartner_Location_ID, bpl.c_location_id AS C_Location_ID, rr.ad_user_id AS AD_User_ID, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName FROM c_rfqresponse rr
JOIN c_rfq r ON rr.c_rfq_id = r.c_rfq_id
JOIN ad_orginfo oi ON rr.ad_org_id = oi.ad_org_id
JOIN c_currency c ON r.c_currency_id = c.c_currency_id
JOIN c_bpartner bp ON rr.c_bpartner_id = bp.c_bpartner_id
JOIN c_bpartner_location bpl ON rr.c_bpartner_location_id = bpl.c_bpartner_location_id
LEFT JOIN ad_user bpc ON rr.ad_user_id = bpc.ad_user_id
JOIN ad_language l ON l.issystemlanguage = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW DD_Order_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, DD_Order_ID, C_Order_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, SalesRep_ID, SalesRep_Name, DateOrdered, DatePromised, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Description, POReference, C_Charge_ID, ChargeAmt, Volume, Weight, C_Campaign_ID, C_Project_ID, C_Activity_ID, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, o.dd_order_id AS DD_Order_ID, o.c_order_id AS C_Order_ID, o.issotrx AS IsSOTrx, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, o.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.salesrep_id AS SalesRep_ID, COALESCE(ubp.name, u.name) AS SalesRep_Name, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, bpg.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcg.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, o.description AS Description, o.poreference AS POReference, o.c_charge_id AS C_Charge_ID, o.chargeamt AS ChargeAmt, o.volume AS Volume, o.weight AS Weight, o.c_campaign_id AS C_Campaign_ID,
o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.m_shipper_id AS M_Shipper_ID, o.deliveryrule AS DeliveryRule, o.deliveryviarule AS DeliveryViaRule, o.priorityrule AS PriorityRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM dd_order o
JOIN c_doctype_trl dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN c_bpartner bp ON o.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting_trl bpg ON bp.c_greeting_id = bpg.c_greeting_id AND dt.ad_language = bpg.ad_language
JOIN c_bpartner_location bpl ON o.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON o.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting_trl bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id AND dt.ad_language = bpcg.ad_language
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.salesrep_id = u.ad_user_id
LEFT JOIN c_bpartner ubp ON u.c_bpartner_id = ubp.c_bpartner_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW GL_JournalLine_Acct_v(GL_JournalLine_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, GL_Journal_ID, Line, IsGenerated, Description, AmtSourceDr, AmtSourceCr, C_Currency_ID, C_ConversionType_ID, CurrencyRate, DateAcct, AmtAcctDr, AmtAcctCr, C_UOM_ID, Qty, C_ValidCombination_ID, C_AcctSchema_ID, Account_ID, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, User1_ID, User2_ID, IsFullyQualified, C_Activity_ID) AS SELECT gl.gl_journalline_id AS GL_JournalLine_ID, gl.ad_client_id AS AD_Client_ID, gl.ad_org_id AS AD_Org_ID, gl.isactive AS IsActive, gl.created AS Created, gl.createdby AS CreatedBy, gl.updated AS Updated, gl.updatedby AS UpdatedBy, gl.gl_journal_id AS GL_Journal_ID, gl.line AS Line, gl.isgenerated AS IsGenerated, gl.description AS Description, gl.amtsourcedr AS AmtSourceDr, gl.amtsourcecr AS AmtSourceCr, gl.c_currency_id AS C_Currency_ID, gl.c_conversiontype_id AS C_ConversionType_ID, gl.currencyrate AS CurrencyRate, gl.dateacct AS DateAcct, gl.amtacctdr AS AmtAcctDr, gl.amtacctcr AS AmtAcctCr, gl.c_uom_id AS C_UOM_ID, gl.qty AS Qty, gl.c_validcombination_id AS C_ValidCombination_ID, vc.c_acctschema_id AS C_AcctSchema_ID, vc.account_id AS Account_ID, vc.m_product_id AS M_Product_ID, vc.c_bpartner_id AS C_BPartner_ID, vc.ad_orgtrx_id AS AD_OrgTrx_ID, vc.c_locfrom_id AS C_LocFrom_ID, vc.c_locto_id AS C_LocTo_ID, vc.c_salesregion_id AS C_SalesRegion_ID, vc.c_project_id AS C_Project_ID, vc.c_campaign_id AS C_Campaign_ID, vc.user1_id AS User1_ID, vc.user2_id AS User2_ID, vc.isfullyqualified AS IsFullyQualified, vc.c_activity_id AS C_Activity_ID FROM gl_journalline gl,
c_validcombination vc WHERE gl.c_validcombination_id = vc.c_validcombination_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOut_Candidate_v(AD_Client_ID, AD_Org_ID, C_BPartner_ID, C_Order_ID, DocumentNo, DateOrdered, C_DocType_ID, POReference, Description, SalesRep_ID, M_Warehouse_ID, TotalLines) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.c_bpartner_id AS C_BPartner_ID, o.c_order_id AS C_Order_ID, o.documentno AS DocumentNo, o.dateordered AS DateOrdered, o.c_doctype_id AS C_DocType_ID, o.poreference AS POReference, o.description AS Description, o.salesrep_id AS SalesRep_ID, l.m_warehouse_id AS M_Warehouse_ID, sum((l.qtyordered - l.qtydelivered) * l.priceactual) AS TotalLines FROM c_order o
JOIN c_orderline l ON o.c_order_id = l.c_order_id WHERE o.docstatus = 'CO' AND o.isdelivered = 'N' AND (o.c_doctype_id IN ( SELECT c_doctype.c_doctype_id
           FROM c_doctype
          WHERE c_doctype.docbasetype = 'SOO' AND (c_doctype.docsubtypeso NOT IN ('ON', 'OB', 'WR')))) AND o.deliveryrule <> 'M' AND (l.m_product_id IS NULL OR (EXISTS ( SELECT 1
           FROM m_product p
          WHERE l.m_product_id = p.m_product_id AND p.isexcludeautodelivery = 'N'))) AND l.qtyordered <> l.qtydelivered AND (l.m_product_id IS NOT NULL OR l.c_charge_id IS NOT NULL) AND NOT (EXISTS ( SELECT 1
           FROM m_inoutline iol
             JOIN m_inout io ON iol.m_inout_id = io.m_inout_id
          WHERE iol.c_orderline_id = l.c_orderline_id AND (io.docstatus IN ('DR', 'IN', 'IP', 'WC')))) GROUP BY o.ad_client_id, o.ad_org_id, o.c_bpartner_id, o.c_order_id, o.documentno, o.dateordered, o.c_doctype_id, o.poreference, o.description, o.salesrep_id, l.m_warehouse_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOutConfirm_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOutConfirm_ID, DocumentNo, ConfirmType, IsApproved, IsCancelled, Description, M_InOut_ID, ShipDescription, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, SalesRep_ID, C_DocType_ID, DocumentType, C_Order_ID, DateOrdered, MovementDate, MovementType, M_Warehouse_ID, POReference, DeliveryRule, FreightCostRule, DeliveryViaRule, M_Shipper_ID, PriorityRule, Processed) AS SELECT ioc.ad_client_id AS AD_Client_ID, ioc.ad_org_id AS AD_Org_ID, ioc.isactive AS IsActive, ioc.created AS Created, ioc.createdby AS CreatedBy, ioc.updated AS Updated, ioc.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, ioc.m_inoutconfirm_id AS M_InOutConfirm_ID, ioc.documentno AS DocumentNo, ioc.confirmtype AS ConfirmType, ioc.isapproved AS IsApproved, ioc.iscancelled AS IsCancelled, ioc.description AS Description, io.m_inout_id AS M_InOut_ID, io.description AS ShipDescription, io.c_bpartner_id AS C_BPartner_ID, io.c_bpartner_location_id AS C_BPartner_Location_ID, io.ad_user_id AS AD_User_ID, io.salesrep_id AS SalesRep_ID, io.c_doctype_id AS C_DocType_ID, dt.printname AS DocumentType, io.c_order_id AS C_Order_ID, io.dateordered AS DateOrdered, io.movementdate AS MovementDate, io.movementtype AS MovementType, io.m_warehouse_id AS M_Warehouse_ID, io.poreference AS POReference, io.deliveryrule AS DeliveryRule, io.freightcostrule AS FreightCostRule, io.deliveryviarule AS DeliveryViaRule, io.m_shipper_id AS M_Shipper_ID, io.priorityrule AS PriorityRule, ioc.processed AS Processed FROM m_inoutconfirm ioc
JOIN m_inout io ON ioc.m_inout_id = io.m_inout_id
JOIN c_doctype_trl dt ON io.c_doctype_id = dt.c_doctype_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOut_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOut_ID, IsSOTrx, DocumentNo, DocStatus, C_DocType_ID, C_BPartner_ID, BPValue, BPTaxID, NAICS, DUNS, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, C_Order_ID, MovementDate, MovementType, BPGreeting, Name, Name2, BPContactGreeting, Title, Phone, ContactName, C_Location_ID, Postal, ReferenceNo, Description, POReference, DateOrdered, Volume, Weight, M_Shipper_ID, DeliveryRule, DeliveryViaRule, PriorityRule, Logo_ID, AD_OrgTrx_ID, AD_User_ID, C_Activity_ID, C_BPartner_Location_ID, C_Campaign_ID, C_Charge_ID, ChargeAmt, C_Invoice_ID, C_Project_ID, CreateConfirm, CreateFrom, CreatePackage, DateAcct, DatePrinted, DateReceived, DocAction, DropShip_BPartner_ID, DropShip_Location_ID, DropShip_User_ID, FreightAmt, FreightCostRule, GenerateTo, IsApproved, IsDropShip, IsInDispute, m_inout_isintransit, IsPrinted, M_RMA_ID, NoPackages, PickDate, Posted, Processed, ProcessedOn, Processing, Ref_InOut_ID, Reversal_ID, M_InOut_SalesRep_ID, SendEMail, ShipDate, TrackingNo, User1_ID, User2_ID, c_bp_acqusitioncost, c_bp_actuallifetimevalue, c_bp_ad_language, C_BP_AD_OrgBP_ID, C_BP_AD_Org_ID, C_BP_BPartner_Parent_ID, C_BP_C_BP_Group_ID, C_BP_C_Dunning_ID, C_BP_C_Greeting_ID, C_BP_C_InvoiceSchedule_ID, C_BP_C_PaymentTerm_ID, c_bp_created, C_BP_CreatedBy, C_BP_C_TaxGroup_ID, c_bp_deliveryrule, c_bp_deliveryviarule, c_bp_description, c_bp_dunninggrace, c_bp_firstsale, c_bp_flatdiscount, c_bp_freightcostrule, c_bp_invoicerule, c_bp_isactive, c_bp_iscustomer, c_bp_isdiscountprinted, c_bp_isemployee, c_bp_ismanufacturer, c_bp_isonetime, c_bp_ispotaxexempt, c_bp_isprospect, c_bp_issalesrep, c_bp_issummary, c_bp_istaxexempt, c_bp_isvendor, C_BP_Logo_ID, C_BP_M_DiscountSchema_ID, C_BP_M_PriceList_ID, c_bp_numberemployees, c_bp_paymentrule, c_bp_paymentrulepo, C_BP_PO_DiscountSchema_ID, C_BP_PO_PaymentTerm_ID,
C_BP_PO_PriceList_ID, c_bp_poreference, c_bp_potentiallifetimevalue, c_bp_rating, C_BP_SalesRep_ID, c_bp_salesvolume, c_bp_sendemail, c_bp_shareofcustomer, c_bp_shelflifeminpct, c_bp_so_creditlimit, c_bp_socreditstatus, c_bp_so_creditused, c_bp_so_description, c_bp_totalopenbalance, c_bp_updated, C_BP_UpdatedBy, c_bp_url, C_Greeting_AD_Org_ID, c_greeting_isactive, IsFirstNameOnly, c_greeting_name, C_BP_Location_AD_Org_ID, C_BP_Location_C_BPartner_ID, c_bp_location_created, C_BP_Location_CreatedBy, C_SalesRegion_ID, c_bp_location_fax, c_bp_location_isactive, IsBillTo, ISDN, IsPayFrom, IsRemitTo, IsShipTo, c_bp_location_name, c_bp_location_phone, c_bp_location_phone2, c_bp_location_updated, C_BP_Location_UpdatedBy, Address1, Address2, Address3, Address4, C_Location_AD_Org_ID, C_City_ID, C_Country_ID, City, c_location_created, C_Location_CreatedBy, C_Region_ID, c_location_isactive, RegionName, c_location_updated, C_Location_UpdatedBy, AD_User_AD_Org_ID, AD_User_AD_AD_OrgTrx_ID, ad_user_ad_birthday, AD_User_C_BPartner_ID, AD_User_C_BPartner_Location_ID, AD_User_C_Greeting_ID, ad_user_comments, ad_user_created, AD_User_CreatedBy, ad_user_description, ad_user_email, ad_user_fax, ad_user_isactive, ad_user_lastcontact, ad_user_lastresult, ad_user_phone2, AD_User_Supervisor_ID, ad_user_updated, AD_User_UpdatedBy, ad_user_value, C_User_Greeting_AD_Org_ID, c_user_greeting_isactive, c_user_greeting_isfnameonly, c_user_greeting_name, AD_OrgInfo_AD_Org_ID, AD_OrgType_ID, C_Calendar_ID, ad_orginfo_created, AD_OrgInfo_CreatedBy, DropShip_Warehouse_ID, ad_orginfo_duns, ad_orginfo_email, ad_orginfo_fax, ad_orginfo_isactive, AD_OrgInfo_M_Warehouse_ID, Parent_Org_ID, ad_orginfo_phone, ad_orginfo_phone2, ReceiptFooterMsg, Supervisor_ID, ad_orginfo_updated, AD_OrgInfo_UpdatedBy, M_Warehouse_AD_Org_ID, m_warehouse_description, m_warehouse__isactive, IsDisallowNegativeInv, m_warehouse_isintransit, M_WarehouseSource_ID, m_warehouse_name, ReplenishmentClass, Separator, m_warehouse_value) AS
SELECT io.ad_client_id AS AD_Client_ID, io.ad_org_id AS AD_Org_ID, io.isactive AS IsActive, io.created AS Created, io.createdby AS CreatedBy, io.updated AS Updated, io.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, io.m_inout_id AS M_InOut_ID, io.issotrx AS IsSOTrx, io.documentno AS DocumentNo, io.docstatus AS DocStatus, io.c_doctype_id AS C_DocType_ID, io.c_bpartner_id AS C_BPartner_ID, bp.value AS BPValue, bp.taxid AS BPTaxID, bp.naics AS NAICS, bp.duns AS DUNS, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, io.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, io.c_order_id AS C_Order_ID, io.movementdate AS MovementDate, io.movementtype AS MovementType, bpgt.greeting AS BPGreeting, bp.name AS Name, bp.name2 AS Name2, bpcgt.greeting AS BPContactGreeting, bpc.title AS Title, bpc.phone AS Phone, NULLIF(bpc.name, bp.name) AS ContactName, bpl.c_location_id AS C_Location_ID, l.postal || l.postal_add AS Postal, bp.referenceno AS ReferenceNo, io.description AS Description, io.poreference AS POReference, io.dateordered AS DateOrdered, io.volume AS Volume, io.weight AS Weight, io.m_shipper_id AS M_Shipper_ID, io.deliveryrule AS DeliveryRule, io.deliveryviarule AS DeliveryViaRule, io.priorityrule AS PriorityRule, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID, io.ad_orgtrx_id AS AD_OrgTrx_ID, io.ad_user_id AS AD_User_ID, io.c_activity_id AS C_Activity_ID, io.c_bpartner_location_id AS C_BPartner_Location_ID, io.c_campaign_id AS C_Campaign_ID, io.c_charge_id AS C_Charge_ID, io.chargeamt AS ChargeAmt, io.c_invoice_id AS C_Invoice_ID, io.c_project_id AS C_Project_ID, io.createconfirm AS CreateConfirm, io.createfrom AS CreateFrom, io.createpackage AS CreatePackage, io.dateacct AS DateAcct, io.dateprinted AS DatePrinted, io.datereceived AS DateReceived, io.docaction AS DocAction, io.dropship_bpartner_id AS DropShip_BPartner_ID,
io.dropship_location_id AS DropShip_Location_ID, io.dropship_user_id AS DropShip_User_ID, io.freightamt AS FreightAmt, io.freightcostrule AS FreightCostRule, io.generateto AS GenerateTo, io.isapproved AS IsApproved, io.isdropship AS IsDropShip, io.isindispute AS IsInDispute, io.isintransit AS m_inout_isintransit, io.isprinted AS IsPrinted, io.m_rma_id AS M_RMA_ID, io.nopackages AS NoPackages, io.pickdate AS PickDate, io.posted AS Posted, io.processed AS Processed, io.processedon AS ProcessedOn, io.processing AS Processing, io.ref_inout_id AS Ref_InOut_ID, io.reversal_id AS Reversal_ID, io.salesrep_id AS M_InOut_SalesRep_ID, io.sendemail AS SendEMail, io.shipdate AS ShipDate, io.trackingno AS TrackingNo, io.user1_id AS User1_ID, io.user2_id AS User2_ID, bp.acqusitioncost AS c_bp_acqusitioncost, bp.actuallifetimevalue AS c_bp_actuallifetimevalue, bp.ad_language AS c_bp_ad_language, bp.ad_orgbp_id AS C_BP_AD_OrgBP_ID, bp.ad_org_id AS C_BP_AD_Org_ID, bp.bpartner_parent_id AS C_BP_BPartner_Parent_ID, bp.c_bp_group_id AS C_BP_C_BP_Group_ID, bp.c_dunning_id AS C_BP_C_Dunning_ID, bp.c_greeting_id AS C_BP_C_Greeting_ID, bp.c_invoiceschedule_id AS C_BP_C_InvoiceSchedule_ID, bp.c_paymentterm_id AS C_BP_C_PaymentTerm_ID, bp.created AS c_bp_created, bp.createdby AS C_BP_CreatedBy, bp.c_taxgroup_id AS C_BP_C_TaxGroup_ID, bp.deliveryrule AS c_bp_deliveryrule, bp.deliveryviarule AS c_bp_deliveryviarule, bp.description AS c_bp_description, bp.dunninggrace AS c_bp_dunninggrace, bp.firstsale AS c_bp_firstsale, bp.flatdiscount AS c_bp_flatdiscount, bp.freightcostrule AS c_bp_freightcostrule, bp.invoicerule AS c_bp_invoicerule, bp.isactive AS c_bp_isactive, bp.iscustomer AS c_bp_iscustomer, bp.isdiscountprinted AS c_bp_isdiscountprinted, bp.isemployee AS c_bp_isemployee, bp.ismanufacturer AS c_bp_ismanufacturer, bp.isonetime AS c_bp_isonetime, bp.ispotaxexempt AS c_bp_ispotaxexempt, bp.isprospect AS c_bp_isprospect, bp.issalesrep AS c_bp_issalesrep, bp.issummary AS c_bp_issummary,
bp.istaxexempt AS c_bp_istaxexempt, bp.isvendor AS c_bp_isvendor, bp.logo_id AS C_BP_Logo_ID, bp.m_discountschema_id AS C_BP_M_DiscountSchema_ID, bp.m_pricelist_id AS C_BP_M_PriceList_ID, bp.numberemployees AS c_bp_numberemployees, bp.paymentrule AS c_bp_paymentrule, bp.paymentrulepo AS c_bp_paymentrulepo, bp.po_discountschema_id AS C_BP_PO_DiscountSchema_ID, bp.po_paymentterm_id AS C_BP_PO_PaymentTerm_ID, bp.po_pricelist_id AS C_BP_PO_PriceList_ID, bp.poreference AS c_bp_poreference, bp.potentiallifetimevalue AS c_bp_potentiallifetimevalue, bp.rating AS c_bp_rating, bp.salesrep_id AS C_BP_SalesRep_ID, bp.salesvolume AS c_bp_salesvolume, bp.sendemail AS c_bp_sendemail, bp.shareofcustomer AS c_bp_shareofcustomer, bp.shelflifeminpct AS c_bp_shelflifeminpct, bp.so_creditlimit AS c_bp_so_creditlimit, bp.socreditstatus AS c_bp_socreditstatus, bp.so_creditused AS c_bp_so_creditused, bp.so_description AS c_bp_so_description, bp.totalopenbalance AS c_bp_totalopenbalance, bp.updated AS c_bp_updated, bp.updatedby AS C_BP_UpdatedBy, bp.url AS c_bp_url, bpgt.ad_org_id AS C_Greeting_AD_Org_ID, bpgt.isactive AS c_greeting_isactive, bpg.isfirstnameonly AS IsFirstNameOnly, bpgt.name AS c_greeting_name, bpl.ad_org_id AS C_BP_Location_AD_Org_ID, bpl.c_bpartner_id AS C_BP_Location_C_BPartner_ID, bpl.created AS c_bp_location_created, bpl.createdby AS C_BP_Location_CreatedBy, bpl.c_salesregion_id AS C_SalesRegion_ID, bpl.fax AS c_bp_location_fax, bpl.isactive AS c_bp_location_isactive, bpl.isbillto AS IsBillTo, bpl.isdn AS ISDN, bpl.ispayfrom AS IsPayFrom, bpl.isremitto AS IsRemitTo, bpl.isshipto AS IsShipTo, bpl.name AS c_bp_location_name, bpl.phone AS c_bp_location_phone, bpl.phone2 AS c_bp_location_phone2, bpl.updated AS c_bp_location_updated, bpl.updatedby AS C_BP_Location_UpdatedBy, l.address1 AS Address1, l.address2 AS Address2, l.address3 AS Address3, l.address4 AS Address4, l.ad_org_id AS C_Location_AD_Org_ID, l.c_city_id AS C_City_ID, l.c_country_id AS C_Country_ID,
l.city AS City, l.created AS c_location_created, l.createdby AS C_Location_CreatedBy, l.c_region_id AS C_Region_ID, l.isactive AS c_location_isactive, l.regionname AS RegionName, l.updated AS c_location_updated, l.updatedby AS C_Location_UpdatedBy, bpc.ad_org_id AS AD_User_AD_Org_ID, bpc.ad_orgtrx_id AS AD_User_AD_AD_OrgTrx_ID, bpc.birthday AS ad_user_ad_birthday, bpc.c_bpartner_id AS AD_User_C_BPartner_ID, bpc.c_bpartner_location_id AS AD_User_C_BPartner_Location_ID, bpc.c_greeting_id AS AD_User_C_Greeting_ID, bpc.comments AS ad_user_comments, bpc.created AS ad_user_created, bpc.createdby AS AD_User_CreatedBy, bpc.description AS ad_user_description, bpc.email AS ad_user_email, bpc.fax AS ad_user_fax, bpc.isactive AS ad_user_isactive, bpc.lastcontact AS ad_user_lastcontact, bpc.lastresult AS ad_user_lastresult, bpc.phone2 AS ad_user_phone2, bpc.supervisor_id AS AD_User_Supervisor_ID, bpc.updated AS ad_user_updated, bpc.updatedby AS AD_User_UpdatedBy, bpc.value AS ad_user_value, bpcgt.ad_org_id AS C_User_Greeting_AD_Org_ID, bpcgt.isactive AS c_user_greeting_isactive, bpcg.isfirstnameonly AS c_user_greeting_isfnameonly, bpcgt.name AS c_user_greeting_name, oi.ad_org_id AS AD_OrgInfo_AD_Org_ID, oi.ad_orgtype_id AS AD_OrgType_ID, oi.c_calendar_id AS C_Calendar_ID, oi.created AS ad_orginfo_created, oi.createdby AS AD_OrgInfo_CreatedBy, oi.dropship_warehouse_id AS DropShip_Warehouse_ID, oi.duns AS ad_orginfo_duns, oi.email AS ad_orginfo_email, oi.fax AS ad_orginfo_fax, oi.isactive AS ad_orginfo_isactive, oi.m_warehouse_id AS AD_OrgInfo_M_Warehouse_ID, oi.parent_org_id AS Parent_Org_ID, oi.phone AS ad_orginfo_phone, oi.phone2 AS ad_orginfo_phone2, oi.receiptfootermsg AS ReceiptFooterMsg, oi.supervisor_id AS Supervisor_ID, oi.updated AS ad_orginfo_updated, oi.updatedby AS AD_OrgInfo_UpdatedBy, wh.ad_org_id AS M_Warehouse_AD_Org_ID, wh.description AS m_warehouse_description, wh.isactive AS m_warehouse__isactive, wh.isdisallownegativeinv AS IsDisallowNegativeInv,
wh.isintransit AS m_warehouse_isintransit, wh.m_warehousesource_id AS M_WarehouseSource_ID, wh.name AS m_warehouse_name, wh.replenishmentclass AS ReplenishmentClass, wh.separator AS Separator, wh.value AS m_warehouse_value FROM m_inout io
JOIN c_doctype_trl dt ON io.c_doctype_id = dt.c_doctype_id
JOIN c_bpartner bp ON io.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN c_greeting bpg ON bp.c_greeting_id = bpg.c_greeting_id
LEFT JOIN c_greeting_trl bpgt ON bp.c_greeting_id = bpgt.c_greeting_id AND dt.ad_language = bpgt.ad_language
JOIN c_bpartner_location bpl ON io.c_bpartner_location_id = bpl.c_bpartner_location_id
JOIN c_location l ON bpl.c_location_id = l.c_location_id
LEFT JOIN ad_user bpc ON io.ad_user_id = bpc.ad_user_id
LEFT JOIN c_greeting bpcg ON bpc.c_greeting_id = bpcg.c_greeting_id
LEFT JOIN c_greeting_trl bpcgt ON bpc.c_greeting_id = bpcgt.c_greeting_id AND dt.ad_language = bpcgt.ad_language
JOIN ad_orginfo oi ON io.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON io.ad_client_id = ci.ad_client_id
JOIN m_warehouse wh ON io.m_warehouse_id = wh.m_warehouse_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOut_LineConfirm_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOutLineConfirm_ID, M_InOutConfirm_ID, TargetQty, ConfirmedQty, DifferenceQty, ScrappedQty, Description, Processed, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, MovementQty, UOMSymbol, QtyBackOrdered, Name, ShipDescription, DocumentNote, UPC, SKU, ProductValue, M_Locator_ID, M_Warehouse_ID, X, Y, Z, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate) AS SELECT iolc.ad_client_id AS AD_Client_ID, iolc.ad_org_id AS AD_Org_ID, iolc.isactive AS IsActive, iolc.created AS Created, iolc.createdby AS CreatedBy, iolc.updated AS Updated, iolc.updatedby AS UpdatedBy, uom.ad_language AS AD_Language, iolc.m_inoutlineconfirm_id AS M_InOutLineConfirm_ID, iolc.m_inoutconfirm_id AS M_InOutConfirm_ID, iolc.targetqty AS TargetQty, iolc.confirmedqty AS ConfirmedQty, iolc.differenceqty AS DifferenceQty, iolc.scrappedqty AS ScrappedQty, iolc.description AS Description, iolc.processed AS Processed, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line AS Line, p.m_product_id AS M_Product_ID, iol.movementqty AS MovementQty, uom.uomsymbol AS UOMSymbol, ol.qtyordered - ol.qtydelivered AS QtyBackOrdered, COALESCE(COALESCE(pt.name, p.name), iol.description) AS Name, CASE WHEN COALESCE(pt.name, p.name) IS NOT NULL THEN iol.description ELSE NULL END AS ShipDescription, COALESCE(pt.documentnote, p.documentnote) AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate FROM m_inoutlineconfirm iolc
JOIN m_inoutline iol ON iolc.m_inoutline_id = iol.m_inoutline_id
JOIN c_uom_trl uom ON iol.c_uom_id = uom.c_uom_id
LEFT JOIN m_product p ON iol.m_product_id = p.m_product_id
LEFT JOIN m_product_trl pt ON iol.m_product_id = pt.m_product_id AND uom.ad_language = pt.ad_language
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
LEFT JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOutLineMA_Returned(M_InOutLine_ID, M_AttributeSetInstance_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, MovementQty, M_InOutLineMA_UU, DateMaterialPolicy, IsAutoGenerated, returnedqty) AS SELECT sma.m_inoutline_id AS M_InOutLine_ID, sma.m_attributesetinstance_id AS M_AttributeSetInstance_ID, sma.ad_client_id AS AD_Client_ID, sma.ad_org_id AS AD_Org_ID, sma.isactive AS IsActive, sma.created AS Created, sma.createdby AS CreatedBy, sma.updated AS Updated, sma.updatedby AS UpdatedBy, sma.movementqty AS MovementQty, sma.m_inoutlinema_uu AS M_InOutLineMA_UU, sma.datematerialpolicy AS DateMaterialPolicy, sma.isautogenerated AS IsAutoGenerated, sum(crma.movementqty) AS returnedqty FROM m_inoutlinema sma
JOIN m_inoutline sl ON sma.m_inoutline_id = sl.m_inoutline_id
JOIN m_inout sio ON sio.m_inout_id = sl.m_inout_id
LEFT JOIN m_rmaline rl ON rl.m_inoutline_id = sl.m_inoutline_id
LEFT JOIN m_inoutline crl ON rl.m_rmaline_id = crl.m_rmaline_id
LEFT JOIN m_inout cr ON cr.m_inout_id = crl.m_inout_id AND NOT (cr.docstatus IN ('CO', 'RE'))
LEFT JOIN m_inoutlinema crma ON crma.m_inoutline_id = crl.m_inoutline_id AND sma.m_attributesetinstance_id = crma.m_attributesetinstance_id GROUP BY sma.m_attributesetinstance_id, sma.m_inoutline_id, sma.ad_client_id, sma.ad_org_id, sma.isactive, sma.created, sma.createdby, sma.updated, sma.updatedby, sma.movementqty, sma.m_inoutlinema_uu, sma.datematerialpolicy, sma.isautogenerated
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOutLineMA_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, M_AttributeSetInstance_ID, MovementQty, M_Locator_ID) AS SELECT m.ad_client_id AS AD_Client_ID, m.ad_org_id AS AD_Org_ID, m.isactive AS IsActive, m.created AS Created, m.createdby AS CreatedBy, m.updated AS Updated, m.updatedby AS UpdatedBy, l.m_inout_id AS M_InOut_ID, m.m_inoutline_id AS M_InOutLine_ID, l.line AS Line, l.m_product_id AS M_Product_ID, m.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m.movementqty AS MovementQty, l.m_locator_id AS M_Locator_ID FROM m_inoutlinema m
JOIN m_inoutline l ON m.m_inoutline_id = l.m_inoutline_id UNION SELECT m_inoutline.ad_client_id AS AD_Client_ID, m_inoutline.ad_org_id AS AD_Org_ID, m_inoutline.isactive AS IsActive, m_inoutline.created AS Created, m_inoutline.createdby AS CreatedBy, m_inoutline.updated AS Updated, m_inoutline.updatedby AS UpdatedBy, m_inoutline.m_inout_id AS M_InOut_ID, m_inoutline.m_inoutline_id AS M_InOutLine_ID, m_inoutline.line AS Line, m_inoutline.m_product_id AS M_Product_ID, m_inoutline.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m_inoutline.movementqty AS MovementQty, m_inoutline.m_locator_id AS M_Locator_ID FROM m_inoutline
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_InOut_Line_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, M_InOut_ID, M_InOutLine_ID, Line, M_Product_ID, MovementQty, QtyEntered, UOMSymbol, QtyOrdered, QtyDelivered, QtyBackOrdered, Name, Description, DocumentNote, UPC, SKU, ProductValue, M_Locator_ID, M_Warehouse_ID, X, Y, Z, M_AttributeSetInstance_ID, M_AttributeSet_ID, SerNo, Lot, M_Lot_ID, GuaranteeDate, ProductDescription, ImageURL, C_Campaign_ID, C_Project_ID, C_Activity_ID, C_ProjectPhase_ID, C_ProjectTask_ID, AD_OrgTrx_ID, C_Charge_ID, ConfirmedQty, C_OrderLine_ID, M_InOutLine_C_UOM_ID, m_inoutline_isdescription, IsInvoiced, M_RMALine_ID, PickedQty, m_inoutline_processed, Ref_InOutLine_ID, ReversalLine_ID, ScrappedQty, TargetQty, M_InOutLine_User1_ID, M_InOutLine_User2_ID, M_Product_AD_Org_ID, Classification, m_product_copyfrom, m_product_created, M_Product_CreatedBy, C_RevenueRecognition_ID, C_SubscriptionType_ID, M_Product_C_TaxCategory_ID, M_Product_C_UOM_ID, DescriptionURL, Discontinued, DiscontinuedAt, Group1, Group2, GuaranteeDays, GuaranteeDaysMin, Help, m_product_isactive, IsBOM, IsDropShip, IsExcludeAutoDelivery, IsInvoicePrintDetails, IsPickListPrintDetails, IsPurchased, m_product_isselfservice, IsSold, IsStocked, m_product_issummary, IsVerified, IsWebStoreFeatured, LowLevel, M_Product_M_AttributeSet_ID, M_Product_M_ASI_ID, M_FreightCategory_ID, M_Product_M_Locator_ID, M_Product_M_Prod_Category_ID, m_product_processing, ProductType, R_MailText_ID, M_Product_SalesRep_ID, S_ExpenseType_ID, ShelfDepth, ShelfHeight, ShelfWidth, M_Product_S_Resource_ID, UnitsPerPack, UnitsPerPallet, m_product_updated, M_Product_UpdatedBy, VersionNo, Volume, Weight, C_UOM_AD_Org_ID, CostingPrecision, c_uom_description, c_uom_isactive, c_uom_isdefault, c_uom_name, StdPrecision, UOMType, X12DE355, M_ASI_AD_Org_ID, m_asi_created, M_ASI_CreatedBy, M_ASI_Description, m_asi_isactive, m_asi_updated, M_ASI_UpdatedBy, M_Locator_AD_Org_ID, m_locator_isactive,
IsDefault, PriorityNo, m_locator_value, C_OrderLine_AD_Org_ID, C_OrderLine_AD_OrgTrx_ID, C_OrderLine_C_Activity_ID, C_OrderLine_C_BPartner_ID, C_OrderLine_C_BP_Location_ID, C_OrderLine_C_Charge_ID, C_OrderLine_C_Currency_ID, C_Order_ID, C_OrderLine_C_Project_ID, C_OrderLine_C_ProjectPhase_ID, C_OrderLine_C_ProjectTask_ID, c_orderline_created, C_OrderLine_CreatedBy, C_OrderLine_C_Tax_ID, C_OrderLine_C_UOM_ID, DateDelivered, DateInvoiced, DateOrdered, c_orderline_datepromised, c_orderline_description, Discount, c_orderline_freightamt, c_orderline_isactive, c_orderline_isdescription, c_orderline_line, LineNetAmt, Link_OrderLine_ID, C_OrderLine_M_ASI_ID, C_OrderLine_M_Product_ID, M_Promotion_ID, C_OrderLine_M_Shipper_ID, C_OrderLine_Warehouse_ID, PriceActual, PriceCost, PriceEntered, PriceLimit, PriceList, c_orderline_processed, c_orderline_qtyentered, QtyInvoiced, QtyLostSales, QtyReserved, Ref_OrderLine_ID, RRAmt, RRStartDate, S_ResourceAssignment_ID, c_orderline_updated, C_OrderLine_UpdatedBy, C_OrderLine_User1_ID, C_OrderLine_User2_ID, C_Charge_C_Org_ID, C_Charge_C_BPartner_ID, C_ChargeType_ID, c_charge_chargeamt, C_Charge_C_TaxCategory_ID, c_charge_description, c_charge_isactive, IsSameCurrency, IsSameTax, c_charge_istaxincluded) AS SELECT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.isactive AS IsActive, iol.created AS Created, iol.createdby AS CreatedBy, iol.updated AS Updated, iol.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line AS Line, pt.m_product_id AS M_Product_ID, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN iol.movementqty ELSE NULL END AS MovementQty, CASE WHEN iol.qtyentered <> 0 OR iol.m_product_id IS NOT NULL THEN iol.qtyentered ELSE NULL END AS QtyEntered, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN uomt.uomsymbol ELSE NULL END AS UOMSymbol, ol.qtyordered AS QtyOrdered,
ol.qtydelivered AS QtyDelivered, CASE WHEN iol.movementqty <> 0 OR iol.m_product_id IS NOT NULL THEN ol.qtyordered - ol.qtydelivered ELSE NULL END AS QtyBackOrdered, COALESCE(pt.name || productattribute(iol.m_attributesetinstance_id), ct.name, iol.description) AS Name, CASE WHEN COALESCE(ct.name, pt.name) IS NOT NULL THEN iol.description ELSE NULL END AS Description, pt.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, pt.description AS ProductDescription, p.imageurl AS ImageURL, iol.c_campaign_id AS C_Campaign_ID, iol.c_project_id AS C_Project_ID, iol.c_activity_id AS C_Activity_ID, iol.c_projectphase_id AS C_ProjectPhase_ID, iol.c_projecttask_id AS C_ProjectTask_ID, iol.ad_orgtrx_id AS AD_OrgTrx_ID, iol.c_charge_id AS C_Charge_ID, iol.confirmedqty AS ConfirmedQty, iol.c_orderline_id AS C_OrderLine_ID, iol.c_uom_id AS M_InOutLine_C_UOM_ID, iol.isdescription AS m_inoutline_isdescription, iol.isinvoiced AS IsInvoiced, iol.m_rmaline_id AS M_RMALine_ID, iol.pickedqty AS PickedQty, iol.processed AS m_inoutline_processed, iol.ref_inoutline_id AS Ref_InOutLine_ID, iol.reversalline_id AS ReversalLine_ID, iol.scrappedqty AS ScrappedQty, iol.targetqty AS TargetQty, iol.user1_id AS M_InOutLine_User1_ID, iol.user2_id AS M_InOutLine_User2_ID, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL,
p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, p.updated AS m_product_updated, p.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, uomt.ad_org_id AS C_UOM_AD_Org_ID, uom.costingprecision AS CostingPrecision, uomt.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy,
l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, ol.ad_org_id AS C_OrderLine_AD_Org_ID, ol.ad_orgtrx_id AS C_OrderLine_AD_OrgTrx_ID, ol.c_activity_id AS C_OrderLine_C_Activity_ID, ol.c_bpartner_id AS C_OrderLine_C_BPartner_ID, ol.c_bpartner_location_id AS C_OrderLine_C_BP_Location_ID, ol.c_charge_id AS C_OrderLine_C_Charge_ID, ol.c_currency_id AS C_OrderLine_C_Currency_ID, ol.c_order_id AS C_Order_ID, ol.c_project_id AS C_OrderLine_C_Project_ID, ol.c_projectphase_id AS C_OrderLine_C_ProjectPhase_ID, ol.c_projecttask_id AS C_OrderLine_C_ProjectTask_ID, ol.created AS c_orderline_created, ol.createdby AS C_OrderLine_CreatedBy, ol.c_tax_id AS C_OrderLine_C_Tax_ID, ol.c_uom_id AS C_OrderLine_C_UOM_ID, ol.datedelivered AS DateDelivered, ol.dateinvoiced AS DateInvoiced, ol.dateordered AS DateOrdered, ol.datepromised AS c_orderline_datepromised, ol.description AS c_orderline_description, ol.discount AS Discount, ol.freightamt AS c_orderline_freightamt, ol.isactive AS c_orderline_isactive, ol.isdescription AS c_orderline_isdescription, ol.line AS c_orderline_line, ol.linenetamt AS LineNetAmt, ol.link_orderline_id AS Link_OrderLine_ID, ol.m_attributesetinstance_id AS C_OrderLine_M_ASI_ID, ol.m_product_id AS C_OrderLine_M_Product_ID, ol.m_promotion_id AS M_Promotion_ID, ol.m_shipper_id AS C_OrderLine_M_Shipper_ID, ol.m_warehouse_id AS C_OrderLine_Warehouse_ID, ol.priceactual AS PriceActual, ol.pricecost AS PriceCost, ol.priceentered AS PriceEntered, ol.pricelimit AS PriceLimit, ol.pricelist AS PriceList, ol.processed AS c_orderline_processed, ol.qtyentered AS c_orderline_qtyentered, ol.qtyinvoiced AS QtyInvoiced, ol.qtylostsales AS QtyLostSales, ol.qtyreserved AS QtyReserved, ol.ref_orderline_id AS Ref_OrderLine_ID, ol.rramt AS RRAmt, ol.rrstartdate AS RRStartDate, ol.s_resourceassignment_id AS S_ResourceAssignment_ID, ol.updated AS c_orderline_updated,
ol.updatedby AS C_OrderLine_UpdatedBy, ol.user1_id AS C_OrderLine_User1_ID, ol.user2_id AS C_OrderLine_User2_ID, ct.ad_org_id AS C_Charge_C_Org_ID, c.c_bpartner_id AS C_Charge_C_BPartner_ID, c.c_chargetype_id AS C_ChargeType_ID, c.chargeamt AS c_charge_chargeamt, c.c_taxcategory_id AS C_Charge_C_TaxCategory_ID, ct.description AS c_charge_description, ct.isactive AS c_charge_isactive, c.issamecurrency AS IsSameCurrency, c.issametax AS IsSameTax, c.istaxincluded AS c_charge_istaxincluded FROM m_inoutline iol
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON iol.c_uom_id = uomt.c_uom_id
LEFT JOIN m_product p ON iol.m_product_id = p.m_product_id
LEFT JOIN m_product_trl pt ON iol.m_product_id = pt.m_product_id AND uomt.ad_language = pt.ad_language
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
LEFT JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
LEFT JOIN c_charge c ON iol.c_charge_id = c.c_charge_id
LEFT JOIN c_charge_trl ct ON iol.c_charge_id = ct.c_charge_id AND uomt.ad_language = ct.ad_language UNION SELECT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.isactive AS IsActive, iol.created AS Created, iol.createdby AS CreatedBy, iol.updated AS Updated, iol.updatedby AS UpdatedBy, uomt.ad_language AS AD_Language, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line + bl.line / 100 AS Line, pt.m_product_id AS M_Product_ID, CASE WHEN bl.isqtypercentage = 'N' THEN iol.movementqty * bl.qtybom ELSE iol.movementqty * (bl.qtybatch / 100) END AS MovementQty, CASE WHEN bl.isqtypercentage = 'N' THEN iol.qtyentered * bl.qtybom ELSE iol.qtyentered * (bl.qtybatch / 100) END AS QtyEntered, uomt.uomsymbol AS UOMSymbol, NULL AS QtyOrdered, NULL AS QtyDelivered, NULL AS QtyBackOrdered, pt.name AS Name, b.description AS Description, pt.documentnote AS DocumentNote, p.upc AS UPC, p.sku AS SKU, p.value AS ProductValue, iol.m_locator_id AS M_Locator_ID, l.m_warehouse_id AS M_Warehouse_ID, l.x AS X, l.y AS Y, l.z AS Z, iol.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.m_attributeset_id AS M_AttributeSet_ID, asi.serno AS SerNo, asi.lot AS Lot, asi.m_lot_id AS M_Lot_ID, asi.guaranteedate AS GuaranteeDate, pt.description AS ProductDescription, p.imageurl AS ImageURL, iol.c_campaign_id AS C_Campaign_ID, iol.c_project_id AS C_Project_ID, iol.c_activity_id AS C_Activity_ID, iol.c_projectphase_id AS C_ProjectPhase_ID, iol.c_projecttask_id AS C_ProjectTask_ID, iol.ad_orgtrx_id AS AD_OrgTrx_ID, iol.c_charge_id AS C_Charge_ID, iol.confirmedqty AS ConfirmedQty, iol.c_orderline_id AS C_OrderLine_ID, iol.c_uom_id AS M_InOutLine_C_UOM_ID, iol.isdescription AS m_inoutline_isdescription, iol.isinvoiced AS IsInvoiced, iol.m_rmaline_id AS M_RMALine_ID, iol.pickedqty AS PickedQty, iol.processed AS m_inoutline_processed, iol.ref_inoutline_id AS Ref_InOutLine_ID, iol.reversalline_id AS ReversalLine_ID, iol.scrappedqty AS ScrappedQty,
iol.targetqty AS TargetQty, iol.user1_id AS M_InOutLine_User1_ID, iol.user2_id AS M_InOutLine_User2_ID, pt.ad_org_id AS M_Product_AD_Org_ID, p.classification AS Classification, p.copyfrom AS m_product_copyfrom, pt.created AS m_product_created, pt.createdby AS M_Product_CreatedBy, p.c_revenuerecognition_id AS C_RevenueRecognition_ID, p.c_subscriptiontype_id AS C_SubscriptionType_ID, p.c_taxcategory_id AS M_Product_C_TaxCategory_ID, p.c_uom_id AS M_Product_C_UOM_ID, p.descriptionurl AS DescriptionURL, p.discontinued AS Discontinued, p.discontinuedat AS DiscontinuedAt, p.group1 AS Group1, p.group2 AS Group2, p.guaranteedays AS GuaranteeDays, p.guaranteedaysmin AS GuaranteeDaysMin, p.help AS Help, pt.isactive AS m_product_isactive, p.isbom AS IsBOM, p.isdropship AS IsDropShip, p.isexcludeautodelivery AS IsExcludeAutoDelivery, p.isinvoiceprintdetails AS IsInvoicePrintDetails, p.ispicklistprintdetails AS IsPickListPrintDetails, p.ispurchased AS IsPurchased, p.isselfservice AS m_product_isselfservice, p.issold AS IsSold, p.isstocked AS IsStocked, p.issummary AS m_product_issummary, p.isverified AS IsVerified, p.iswebstorefeatured AS IsWebStoreFeatured, p.lowlevel AS LowLevel, p.m_attributeset_id AS M_Product_M_AttributeSet_ID, p.m_attributesetinstance_id AS M_Product_M_ASI_ID, p.m_freightcategory_id AS M_FreightCategory_ID, p.m_locator_id AS M_Product_M_Locator_ID, p.m_product_category_id AS M_Product_M_Prod_Category_ID, p.processing AS m_product_processing, p.producttype AS ProductType, p.r_mailtext_id AS R_MailText_ID, p.salesrep_id AS M_Product_SalesRep_ID, p.s_expensetype_id AS S_ExpenseType_ID, p.shelfdepth AS ShelfDepth, p.shelfheight AS ShelfHeight, p.shelfwidth AS ShelfWidth, p.s_resource_id AS M_Product_S_Resource_ID, p.unitsperpack AS UnitsPerPack, p.unitsperpallet AS UnitsPerPallet, pt.updated AS m_product_updated, pt.updatedby AS M_Product_UpdatedBy, p.versionno AS VersionNo, p.volume AS Volume, p.weight AS Weight, uomt.ad_org_id AS C_UOM_AD_Org_ID,
uom.costingprecision AS CostingPrecision, uomt.description AS c_uom_description, uomt.isactive AS c_uom_isactive, uom.isdefault AS c_uom_isdefault, uomt.name AS c_uom_name, uom.stdprecision AS StdPrecision, uom.uomtype AS UOMType, uom.x12de355 AS X12DE355, asi.ad_org_id AS M_ASI_AD_Org_ID, asi.created AS m_asi_created, asi.createdby AS M_ASI_CreatedBy, asi.description AS M_ASI_Description, asi.isactive AS m_asi_isactive, asi.updated AS m_asi_updated, asi.updatedby AS M_ASI_UpdatedBy, l.ad_org_id AS M_Locator_AD_Org_ID, l.isactive AS m_locator_isactive, l.isdefault AS IsDefault, l.priorityno AS PriorityNo, l.value AS m_locator_value, NULL AS C_OrderLine_AD_Org_ID, NULL AS C_OrderLine_AD_OrgTrx_ID, NULL AS C_OrderLine_C_Activity_ID, NULL AS C_OrderLine_C_BPartner_ID, NULL AS C_OrderLine_C_BP_Location_ID, NULL AS C_OrderLine_C_Charge_ID, NULL AS C_OrderLine_C_Currency_ID, NULL AS C_Order_ID, NULL AS C_OrderLine_C_Project_ID, NULL AS C_OrderLine_C_ProjectPhase_ID, NULL AS C_OrderLine_C_ProjectTask_ID, NULL AS c_orderline_created, NULL AS C_OrderLine_CreatedBy, NULL AS C_OrderLine_C_Tax_ID, NULL AS C_OrderLine_C_UOM_ID, NULL AS DateDelivered, NULL AS DateInvoiced, NULL AS DateOrdered, NULL AS c_orderline_datepromised, NULL AS c_orderline_description, NULL AS Discount, NULL AS c_orderline_freightamt, NULL AS c_orderline_isactive, NULL AS c_orderline_isdescription, NULL AS c_orderline_line, NULL AS LineNetAmt, NULL AS Link_OrderLine_ID, NULL AS C_OrderLine_M_ASI_ID, NULL AS C_OrderLine_M_Product_ID, NULL AS M_Promotion_ID, NULL AS C_OrderLine_M_Shipper_ID, NULL AS C_OrderLine_Warehouse_ID, NULL AS PriceActual, NULL AS PriceCost, NULL AS PriceEntered, NULL AS PriceLimit, NULL AS PriceList, NULL AS c_orderline_processed, NULL AS c_orderline_qtyentered, NULL AS QtyInvoiced, NULL AS QtyLostSales, NULL AS QtyReserved, NULL AS Ref_OrderLine_ID, NULL AS RRAmt, NULL AS RRStartDate, NULL AS S_ResourceAssignment_ID, NULL AS c_orderline_updated, NULL AS C_OrderLine_UpdatedBy,
NULL AS C_OrderLine_User1_ID, NULL AS C_OrderLine_User2_ID, NULL AS C_Charge_C_Org_ID, NULL AS C_Charge_C_BPartner_ID, NULL AS C_ChargeType_ID, NULL AS c_charge_chargeamt, NULL AS C_Charge_C_TaxCategory_ID, NULL AS c_charge_description, NULL AS c_charge_isactive, NULL AS IsSameCurrency, NULL AS IsSameTax, NULL AS c_charge_istaxincluded FROM pp_product_bom b
JOIN m_inoutline iol ON b.m_product_id = iol.m_product_id
JOIN m_product bp ON bp.m_product_id = iol.m_product_id AND bp.isbom = 'Y' AND bp.isverified = 'Y' AND bp.ispicklistprintdetails = 'Y'
JOIN pp_product_bomline bl ON bl.pp_product_bom_id = b.pp_product_bom_id
JOIN m_product p ON bl.m_product_id = p.m_product_id
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id
JOIN c_uom_trl uomt ON p.c_uom_id = uomt.c_uom_id
JOIN m_product_trl pt ON bl.m_product_id = pt.m_product_id AND uomt.ad_language = pt.ad_language
LEFT JOIN m_attributesetinstance asi ON iol.m_attributesetinstance_id = asi.m_attributesetinstance_id
LEFT JOIN m_locator l ON iol.m_locator_id = l.m_locator_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_Movement_Candidate_v(AD_Client_ID, AD_Org_ID, C_BPartner_ID, DD_Order_ID, DocumentNo, DateOrdered, C_DocType_ID, POReference, Description, SalesRep_ID, M_Locator_ID, M_LocatorTo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.c_bpartner_id AS C_BPartner_ID, o.dd_order_id AS DD_Order_ID, o.documentno AS DocumentNo, o.dateordered AS DateOrdered, o.c_doctype_id AS C_DocType_ID, o.poreference AS POReference, o.description AS Description, o.salesrep_id AS SalesRep_ID, l.m_locator_id AS M_Locator_ID, l.m_locatorto_id AS M_LocatorTo_ID FROM dd_order o
JOIN dd_orderline l ON o.dd_order_id = l.dd_order_id WHERE o.docstatus = 'CO' AND o.isdelivered = 'N' AND (o.c_doctype_id IN ( SELECT c_doctype.c_doctype_id
           FROM c_doctype
          WHERE c_doctype.docbasetype = 'DOO')) AND o.deliveryrule <> 'M' AND (l.m_product_id IS NULL OR (EXISTS ( SELECT 1
           FROM m_product p
          WHERE l.m_product_id = p.m_product_id AND p.isexcludeautodelivery = 'N'))) AND l.qtyordered <> l.qtydelivered AND l.confirmedqty > 0 AND o.isdropship = 'N' AND (l.m_product_id IS NOT NULL OR l.c_charge_id IS NOT NULL) AND NOT (EXISTS ( SELECT 1
           FROM m_movementline iol
             JOIN m_movement io ON iol.m_movement_id = io.m_movement_id
          WHERE iol.dd_orderline_id = l.dd_orderline_id AND (io.docstatus IN ('IP', 'WC')))) GROUP BY o.ad_client_id, o.ad_org_id, o.c_bpartner_id, o.dd_order_id, o.documentno, o.dateordered, o.c_doctype_id, o.poreference, o.description, o.salesrep_id, l.m_locator_id, l.m_locatorto_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_MovementLineMA_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, M_Movement_ID, M_MovementLine_ID, Line, M_Product_ID, M_AttributeSetInstance_ID, MovementQty, M_Locator_ID, M_LocatorTo_ID) AS SELECT m.ad_client_id AS AD_Client_ID, m.ad_org_id AS AD_Org_ID, m.isactive AS IsActive, m.created AS Created, m.createdby AS CreatedBy, m.updated AS Updated, m.updatedby AS UpdatedBy, l.m_movement_id AS M_Movement_ID, m.m_movementline_id AS M_MovementLine_ID, l.line AS Line, l.m_product_id AS M_Product_ID, m.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m.movementqty AS MovementQty, l.m_locator_id AS M_Locator_ID, l.m_locatorto_id AS M_LocatorTo_ID FROM m_movementlinema m
JOIN m_movementline l ON m.m_movementline_id = l.m_movementline_id UNION SELECT m_movementline.ad_client_id AS AD_Client_ID, m_movementline.ad_org_id AS AD_Org_ID, m_movementline.isactive AS IsActive, m_movementline.created AS Created, m_movementline.createdby AS CreatedBy, m_movementline.updated AS Updated, m_movementline.updatedby AS UpdatedBy, m_movementline.m_movement_id AS M_Movement_ID, m_movementline.m_movementline_id AS M_MovementLine_ID, m_movementline.line AS Line, m_movementline.m_product_id AS M_Product_ID, m_movementline.m_attributesetinstance_id AS M_AttributeSetInstance_ID, m_movementline.movementqty AS MovementQty, m_movementline.m_locator_id AS M_Locator_ID, m_movementline.m_locatorto_id AS M_LocatorTo_ID FROM m_movementline
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW M_PackageLines_Avail_v(AD_Client_ID, AD_Org_ID, M_InOut_ID, M_InOutLine_ID, Line, Qty, M_Product_ID, ProductName, uom) AS SELECT DISTINCT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line AS Line, iol.qtyentered - COALESCE(( SELECT sum(m_packageline.qty) AS sum FROM m_packageline WHERE m_packageline.m_inoutline_id = iol.m_inoutline_id), 0) AS Qty, iol.m_product_id AS M_Product_ID, p.name AS ProductName, uom.name AS uom FROM m_inoutline iol
LEFT JOIN m_packageline mpl ON iol.m_inoutline_id = mpl.m_inoutline_id
JOIN m_product p ON iol.m_product_id = p.m_product_id
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id
JOIN ad_clientinfo ci ON ci.ad_client_id = iol.ad_client_id WHERE p.ispicklistprintdetails = 'N' AND iol.m_product_id <> COALESCE(ci.m_productfreight_id, 0) AND (iol.qtyentered - COALESCE(( SELECT sum(m_packageline.qty) AS sum
           FROM m_packageline
          WHERE m_packageline.m_inoutline_id = iol.m_inoutline_id), 0)) <> 0 UNION SELECT DISTINCT iol.ad_client_id AS AD_Client_ID, iol.ad_org_id AS AD_Org_ID, iol.m_inout_id AS M_InOut_ID, iol.m_inoutline_id AS M_InOutLine_ID, iol.line + pbom.line / 100 AS Line, iol.qtyentered * pbom.bomqty - COALESCE(( SELECT sum(m_packageline.qty) AS sum FROM m_packageline WHERE m_packageline.m_product_id = p2.m_product_id AND (m_packageline.m_package_id IN ( SELECT m_package.m_package_id FROM m_package WHERE m_package.m_inout_id = iol.m_inout_id))), 0) AS Qty, pbom.m_productbom_id AS M_Product_ID, p2.name AS ProductName, uom.name AS uom FROM m_inoutline iol
LEFT JOIN m_packageline mpl ON iol.m_inoutline_id = mpl.m_inoutline_id
JOIN m_product p ON iol.m_product_id = p.m_product_id
JOIN m_product_bom_old pbom ON p.m_product_id = pbom.m_product_id
JOIN m_product p2 ON pbom.m_productbom_id = p2.m_product_id
JOIN c_uom uom ON iol.c_uom_id = uom.c_uom_id WHERE p.isbom = 'Y' AND p.isverified = 'Y' AND p.ispicklistprintdetails = 'Y' AND (iol.qtyentered * pbom.bomqty - COALESCE(( SELECT sum(m_packageline.qty) AS sum
           FROM m_packageline
          WHERE m_packageline.m_product_id = p2.m_product_id AND (m_packageline.m_package_id IN ( SELECT m_package.m_package_id
                   FROM m_package
                  WHERE m_package.m_inout_id = iol.m_inout_id))), 0)) <> 0 ORDER BY 1, 2, 3, 5
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Order_BOM_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocumentNo, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, C_UOM_ID, S_Resource_ID, PP_Product_BOM_ID, AD_Workflow_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchSize, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, Yield, C_Campaign_ID, C_Project_ID, C_Activity_ID, BOMType, BOMUse, Description, Help, M_AttributeSetInstance_ID, M_Product_ID, Name, Revision, ValidFrom, ValidTo, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, obt.ad_language AS AD_Language, o.pp_order_id AS PP_Order_ID, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.c_uom_id AS C_UOM_ID, o.s_resource_id AS S_Resource_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID, o.priorityrule AS PriorityRule, o.qtybatchsize AS QtyBatchSize, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered, o.qtyordered AS QtyOrdered, o.dateconfirm AS DateConfirm,
o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.yield AS Yield, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, ob.bomtype AS BOMType, ob.bomuse AS BOMUse, obt.description AS Description, obt.help AS Help, ob.m_attributesetinstance_id AS M_AttributeSetInstance_ID, ob.m_product_id AS M_Product_ID, obt.name AS Name, ob.revision AS Revision, ob.validfrom AS ValidFrom, ob.validto AS ValidTo, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN c_doctype_trl dt ON o.c_doctype_id = dt.c_doctype_id
JOIN pp_order_bom ob ON ob.pp_order_id = o.pp_order_id
JOIN pp_order_bom_trl obt ON obt.pp_order_bom_id = ob.pp_order_bom_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Order_BOMLine_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Description, Feature, M_Product_ID, BackflushGroup, C_UOM_ID, ComponentType, DateDelivered, Forecast, Help, IsCritical, IssueMethod, LeadTimeOffset, Line, M_AttributeSetInstance_ID, M_ChangeNotice_ID, M_Locator_ID, M_Warehouse_ID, PP_Order_BOM_ID, PP_Order_BOMLine_ID, PP_Order_ID, QtyDelivered, QtyPost, QtyReject, QtyScrap, Scrap, ValidFrom, ValidTo, Assay, AD_User_ID, QtyBatchs, QtyRequiered, QtyReserved, QtyAvailable, QtyOnHand, QtyBOM, IsQtyPercentage, QtyBatch, QtyBatchSize) AS SELECT obl.ad_client_id AS AD_Client_ID, obl.ad_org_id AS AD_Org_ID, obl.isactive AS IsActive, obl.created AS Created, obl.createdby AS CreatedBy, obl.updated AS Updated, obl.updatedby AS UpdatedBy, oblt.ad_language AS AD_Language, oblt.description AS Description, obl.feature AS Feature, obl.m_product_id AS M_Product_ID, obl.backflushgroup AS BackflushGroup, obl.c_uom_id AS C_UOM_ID, obl.componenttype AS ComponentType, obl.datedelivered AS DateDelivered, obl.forecast AS Forecast, oblt.help AS Help, obl.iscritical AS IsCritical, obl.issuemethod AS IssueMethod, obl.leadtimeoffset AS LeadTimeOffset, obl.line AS Line, obl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, obl.m_changenotice_id AS M_ChangeNotice_ID, obl.m_locator_id AS M_Locator_ID, obl.m_warehouse_id AS M_Warehouse_ID, obl.pp_order_bom_id AS PP_Order_BOM_ID, obl.pp_order_bomline_id AS PP_Order_BOMLine_ID, obl.pp_order_id AS PP_Order_ID, obl.qtydelivered AS QtyDelivered, obl.qtypost AS QtyPost, obl.qtyreject AS QtyReject, obl.qtyscrap AS QtyScrap, obl.scrap AS Scrap, obl.validfrom AS ValidFrom, obl.validto AS ValidTo, obl.assay AS Assay, obl.ad_user_id AS AD_User_ID, o.qtybatchs AS QtyBatchs, round(obl.qtyrequiered, 4) AS QtyRequiered, round(bomqtyreserved(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyReserved,
round(bomqtyavailable(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyAvailable, round(bomqtyonhand(obl.m_product_id, obl.m_warehouse_id, 0), 4) AS QtyOnHand, round(obl.qtybom, 4) AS QtyBOM, obl.isqtypercentage AS IsQtyPercentage, round(obl.qtybatch, 4) AS QtyBatch, CASE WHEN o.qtybatchs = 0 THEN 1 ELSE round(obl.qtyrequiered / o.qtybatchs, 4) END AS QtyBatchSize FROM pp_order_bomline obl
JOIN pp_order o ON o.pp_order_id = obl.pp_order_id
LEFT JOIN pp_order_bomline_trl oblt ON oblt.pp_order_bomline_id = obl.pp_order_bomline_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Order_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocumentNo, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, Description, M_Product_ID, M_AttributeSetInstance_ID, C_UOM_ID, S_Resource_ID, PP_Product_BOM_ID, AD_Workflow_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchSize, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, Yield, C_Campaign_ID, C_Project_ID, C_Activity_ID, User1_ID, User2_ID, AD_OrgTrx_ID, C_DocTypeTarget_ID, ScheduleType, IsApproved, DocAction, Posted, IsPrinted, OrderType, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, dt.ad_language AS AD_Language, o.pp_order_id AS PP_Order_ID, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.description AS Description, o.m_product_id AS M_Product_ID, o.m_attributesetinstance_id AS M_AttributeSetInstance_ID, o.c_uom_id AS C_UOM_ID, o.s_resource_id AS S_Resource_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID, o.priorityrule AS PriorityRule,
o.qtybatchsize AS QtyBatchSize, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered, o.qtyordered AS QtyOrdered, o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.yield AS Yield, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, o.user1_id AS User1_ID, o.user2_id AS User2_ID, o.ad_orgtrx_id AS AD_OrgTrx_ID, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.scheduletype AS ScheduleType, o.isapproved AS IsApproved, o.docaction AS DocAction, o.posted AS Posted, o.isprinted AS IsPrinted, o.ordertype AS OrderType, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN c_doctype_trl dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Order_Node_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, Name, C_BPartner_ID, Cost, DateFinish, DateFinishSchedule, DateStart, DateStartSchedule, Description, DocAction, DocStatus, Duration, DurationReal, DurationRequiered, Help, IsMilestone, IsSubcontracting, MovingTime, OverlapUnits, PP_Order_ID, PP_Order_Workflow_ID, PP_Order_Node_ID, Priority, QtyDelivered, QtyRequiered, QtyScrap, QueuingTime, S_Resource_ID, SetupTime, SetupTimeReal, UnitsCycles, ValidFrom, ValidTo, Value, WaitingTime, WorkingTime, Yield) AS SELECT onode.ad_client_id AS AD_Client_ID, onode.ad_org_id AS AD_Org_ID, onode.isactive AS IsActive, onode.created AS Created, onode.createdby AS CreatedBy, onode.updated AS Updated, onode.updatedby AS UpdatedBy, ont.ad_language AS AD_Language, ont.name AS Name, onode.c_bpartner_id AS C_BPartner_ID, onode.cost AS Cost, onode.datefinish AS DateFinish, onode.datefinishschedule AS DateFinishSchedule, onode.datestart AS DateStart, onode.datestartschedule AS DateStartSchedule, ont.description AS Description, onode.docaction AS DocAction, onode.docstatus AS DocStatus, onode.duration AS Duration, onode.durationreal AS DurationReal, onode.durationrequiered AS DurationRequiered, ont.help AS Help, onode.ismilestone AS IsMilestone, onode.issubcontracting AS IsSubcontracting, onode.movingtime AS MovingTime, onode.overlapunits AS OverlapUnits, onode.pp_order_id AS PP_Order_ID, onode.pp_order_workflow_id AS PP_Order_Workflow_ID, onode.pp_order_node_id AS PP_Order_Node_ID, onode.priority AS Priority, onode.qtydelivered AS QtyDelivered, onode.qtyrequiered AS QtyRequiered, onode.qtyscrap AS QtyScrap, onode.queuingtime AS QueuingTime, onode.s_resource_id AS S_Resource_ID, onode.setuptime AS SetupTime, onode.setuptimereal AS SetupTimeReal, onode.unitscycles AS UnitsCycles, onode.validfrom AS ValidFrom, onode.validto AS ValidTo, onode.value AS Value, onode.waitingtime AS WaitingTime,
onode.workingtime AS WorkingTime, onode.yield AS Yield FROM pp_order_node onode
LEFT JOIN pp_order_node_trl ont ON ont.pp_order_node_id = onode.pp_order_node_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Order_Workflow_Header_vt(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, AD_Language, PP_Order_ID, DocStatus, C_DocType_ID, Org_Location_ID, TaxID, M_Warehouse_ID, Warehouse_Location_ID, DocumentType, DocumentTypeNote, Planner_ID, SalesRep_Name, DateStart, DateStartSchedule, FloatAfter, FloatBefored, Line, Lot, SerNo, C_UOM_ID, PP_Product_BOM_ID, Assay, C_OrderLine_ID, PriorityRule, QtyBatchSize, QtyBatchs, QtyDelivered, QtyEntered, QtyOrdered, DateConfirm, DateDelivered, DateFinish, DateFinishSchedule, DateOrdered, DatePromised, QtyReject, QtyReserved, QtyScrap, S_Resource_ID, C_Campaign_ID, C_Project_ID, C_Activity_ID, Name, Description, Help, Author, Cost, DocumentNo, Duration, DurationUnit, Version, ValidFrom, ValidTo, MovingTime, OverlapUnits, AD_Workflow_ID, PublishStatus, QueuingTime, SetupTime, UnitsCycles, WaitingTime, WorkflowType, WorkingTime, Yield, Logo_ID) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, owt.ad_language AS AD_Language, o.pp_order_id AS PP_Order_ID, o.docstatus AS DocStatus, o.c_doctype_id AS C_DocType_ID, oi.c_location_id AS Org_Location_ID, oi.taxid AS TaxID, o.m_warehouse_id AS M_Warehouse_ID, wh.c_location_id AS Warehouse_Location_ID, dt.printname AS DocumentType, dt.documentnote AS DocumentTypeNote, o.planner_id AS Planner_ID, u.name AS SalesRep_Name, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.floatafter AS FloatAfter, o.floatbefored AS FloatBefored, o.line AS Line, o.lot AS Lot, o.serno AS SerNo, o.c_uom_id AS C_UOM_ID, o.pp_product_bom_id AS PP_Product_BOM_ID, o.assay AS Assay, o.c_orderline_id AS C_OrderLine_ID, o.priorityrule AS PriorityRule, o.qtybatchsize AS QtyBatchSize, o.qtybatchs AS QtyBatchs, o.qtydelivered AS QtyDelivered, o.qtyentered AS QtyEntered, o.qtyordered AS QtyOrdered,
o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.qtyreject AS QtyReject, o.qtyreserved AS QtyReserved, o.qtyscrap AS QtyScrap, o.s_resource_id AS S_Resource_ID, o.c_campaign_id AS C_Campaign_ID, o.c_project_id AS C_Project_ID, o.c_activity_id AS C_Activity_ID, owt.name AS Name, owt.description AS Description, owt.help AS Help, ow.author AS Author, ow.cost AS Cost, ow.documentno AS DocumentNo, ow.duration AS Duration, ow.durationunit AS DurationUnit, ow.version AS Version, ow.validfrom AS ValidFrom, ow.validto AS ValidTo, ow.movingtime AS MovingTime, ow.overlapunits AS OverlapUnits, ow.ad_workflow_id AS AD_Workflow_ID, ow.publishstatus AS PublishStatus, ow.queuingtime AS QueuingTime, ow.setuptime AS SetupTime, ow.unitscycles AS UnitsCycles, ow.waitingtime AS WaitingTime, ow.workflowtype AS WorkflowType, ow.workingtime AS WorkingTime, ow.yield AS Yield, COALESCE(oi.logo_id, ci.logo_id) AS Logo_ID FROM pp_order o
JOIN pp_order_workflow ow ON ow.pp_order_id = o.pp_order_id
JOIN pp_order_workflow_trl owt ON owt.pp_order_workflow_id = ow.pp_order_workflow_id
JOIN c_doctype dt ON o.c_doctype_id = dt.c_doctype_id
JOIN m_warehouse wh ON o.m_warehouse_id = wh.m_warehouse_id
JOIN ad_orginfo oi ON o.ad_org_id = oi.ad_org_id
JOIN ad_clientinfo ci ON o.ad_client_id = ci.ad_client_id
LEFT JOIN ad_user u ON o.planner_id = u.ad_user_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Product_BOMLine_v(Feature, AD_Org_ID, Assay, BackflushGroup, C_UOM_ID, ComponentType, Created, CreatedBy, AD_Language, Description, Forecast, Help, IsActive, IsCritical, IsQtyPercentage, IssueMethod, LeadTimeOffset, Line, M_AttributeSetInstance_ID, M_ChangeNotice_ID, M_Product_ID, PP_Product_BOMLine_ID, PP_Product_BOM_ID, QtyBOM, QtyBatch, Scrap, Updated, UpdatedBy, ValidFrom, AD_Client_ID, ValidTo) AS SELECT bl.feature AS Feature, bl.ad_org_id AS AD_Org_ID, bl.assay AS Assay, bl.backflushgroup AS BackflushGroup, bl.c_uom_id AS C_UOM_ID, bl.componenttype AS ComponentType, bl.created AS Created, bl.createdby AS CreatedBy, CAST('en_US' AS CHARACTER VARYING(6)) AS AD_Language, blt.description AS Description, bl.forecast AS Forecast, blt.help AS Help, bl.isactive AS IsActive, bl.iscritical AS IsCritical, bl.isqtypercentage AS IsQtyPercentage, bl.issuemethod AS IssueMethod, bl.leadtimeoffset AS LeadTimeOffset, bl.line AS Line, bl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, bl.m_changenotice_id AS M_ChangeNotice_ID, bl.m_product_id AS M_Product_ID, bl.pp_product_bomline_id AS PP_Product_BOMLine_ID, bl.pp_product_bom_id AS PP_Product_BOM_ID, bl.qtybom AS QtyBOM, bl.qtybatch AS QtyBatch, bl.scrap AS Scrap, bl.updated AS Updated, bl.updatedby AS UpdatedBy, bl.validfrom AS ValidFrom, bl.ad_client_id AS AD_Client_ID, bl.validto AS ValidTo FROM pp_product_bomline bl
JOIN pp_product_bomline_trl blt ON blt.pp_product_bomline_id = bl.pp_product_bomline_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW PP_Product_BOMLine_vt(Feature, AD_Org_ID, Assay, BackflushGroup, C_UOM_ID, ComponentType, Created, CreatedBy, AD_Language, Description, Forecast, Help, IsActive, IsCritical, IsQtyPercentage, IssueMethod, LeadTimeOffset, Line, M_AttributeSetInstance_ID, M_ChangeNotice_ID, M_Product_ID, PP_Product_BOMLine_ID, PP_Product_BOM_ID, QtyBOM, QtyBatch, Scrap, Updated, UpdatedBy, ValidFrom, AD_Client_ID, ValidTo) AS SELECT bl.feature AS Feature, bl.ad_org_id AS AD_Org_ID, bl.assay AS Assay, bl.backflushgroup AS BackflushGroup, bl.c_uom_id AS C_UOM_ID, bl.componenttype AS ComponentType, bl.created AS Created, bl.createdby AS CreatedBy, blt.ad_language AS AD_Language, blt.description AS Description, bl.forecast AS Forecast, blt.help AS Help, bl.isactive AS IsActive, bl.iscritical AS IsCritical, bl.isqtypercentage AS IsQtyPercentage, bl.issuemethod AS IssueMethod, bl.leadtimeoffset AS LeadTimeOffset, bl.line AS Line, bl.m_attributesetinstance_id AS M_AttributeSetInstance_ID, bl.m_changenotice_id AS M_ChangeNotice_ID, bl.m_product_id AS M_Product_ID, bl.pp_product_bomline_id AS PP_Product_BOMLine_ID, bl.pp_product_bom_id AS PP_Product_BOM_ID, bl.qtybom AS QtyBOM, bl.qtybatch AS QtyBatch, bl.scrap AS Scrap, bl.updated AS Updated, bl.updatedby AS UpdatedBy, bl.validfrom AS ValidFrom, bl.ad_client_id AS AD_Client_ID, bl.validto AS ValidTo FROM pp_product_bomline bl
JOIN pp_product_bomline_trl blt ON blt.pp_product_bomline_id = bl.pp_product_bomline_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW R_Request_v(R_Request_ID, AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, DocumentNo, R_RequestType_ID, R_Group_ID, R_Category_ID, R_Status_ID, R_Resolution_ID, R_RequestRelated_ID, Priority, PriorityUser, DueType, Summary, ConfidentialType, IsEscalated, IsSelfService, SalesRep_ID, AD_Role_ID, DateLastAction, DateLastAlert, LastResult, Processed, IsInvoiced, C_BPartner_ID, AD_User_ID, C_Campaign_ID, C_Order_ID, C_Invoice_ID, C_Payment_ID, M_Product_ID, C_Project_ID, A_Asset_ID, M_InOut_ID, M_RMA_ID, AD_Table_ID, Record_ID, RequestAmt, R_MailText_ID, Result, ConfidentialTypeEntry, R_StandardResponse_ID, NextAction, DateNextAction, StartTime, EndTime, QtySpent, QtyInvoiced, M_ProductSpent_ID, C_Activity_ID, StartDate, CloseDate, C_InvoiceRequest_ID, M_ChangeRequest_ID, TaskStatus, QtyPlan, DateCompletePlan, DateStartPlan, M_FixChangeNotice_ID) AS SELECT r_request.r_request_id AS R_Request_ID, r_request.ad_client_id AS AD_Client_ID, r_request.ad_org_id AS AD_Org_ID, r_request.isactive AS IsActive, r_request.created AS Created, r_request.createdby AS CreatedBy, r_request.updated AS Updated, r_request.updatedby AS UpdatedBy, r_request.documentno AS DocumentNo, r_request.r_requesttype_id AS R_RequestType_ID, r_request.r_group_id AS R_Group_ID, r_request.r_category_id AS R_Category_ID, r_request.r_status_id AS R_Status_ID, r_request.r_resolution_id AS R_Resolution_ID, r_request.r_requestrelated_id AS R_RequestRelated_ID, r_request.priority AS Priority, r_request.priorityuser AS PriorityUser, r_request.duetype AS DueType, r_request.summary AS Summary, r_request.confidentialtype AS ConfidentialType, r_request.isescalated AS IsEscalated, r_request.isselfservice AS IsSelfService, r_request.salesrep_id AS SalesRep_ID, r_request.ad_role_id AS AD_Role_ID, r_request.datelastaction AS DateLastAction, r_request.datelastalert AS DateLastAlert, r_request.lastresult AS LastResult, r_request.processed AS Processed,
r_request.isinvoiced AS IsInvoiced, r_request.c_bpartner_id AS C_BPartner_ID, r_request.ad_user_id AS AD_User_ID, r_request.c_campaign_id AS C_Campaign_ID, r_request.c_order_id AS C_Order_ID, r_request.c_invoice_id AS C_Invoice_ID, r_request.c_payment_id AS C_Payment_ID, r_request.m_product_id AS M_Product_ID, r_request.c_project_id AS C_Project_ID, r_request.a_asset_id AS A_Asset_ID, r_request.m_inout_id AS M_InOut_ID, r_request.m_rma_id AS M_RMA_ID, r_request.ad_table_id AS AD_Table_ID, r_request.record_id AS Record_ID, r_request.requestamt AS RequestAmt, r_request.r_mailtext_id AS R_MailText_ID, r_request.result AS Result, r_request.confidentialtypeentry AS ConfidentialTypeEntry, r_request.r_standardresponse_id AS R_StandardResponse_ID, r_request.nextaction AS NextAction, r_request.datenextaction AS DateNextAction, r_request.starttime AS StartTime, r_request.endtime AS EndTime, r_request.qtyspent AS QtySpent, r_request.qtyinvoiced AS QtyInvoiced, r_request.m_productspent_id AS M_ProductSpent_ID, r_request.c_activity_id AS C_Activity_ID, r_request.startdate AS StartDate, r_request.closedate AS CloseDate, r_request.c_invoicerequest_id AS C_InvoiceRequest_ID, r_request.m_changerequest_id AS M_ChangeRequest_ID, r_request.taskstatus AS TaskStatus, r_request.qtyplan AS QtyPlan, r_request.datecompleteplan AS DateCompletePlan, r_request.datestartplan AS DateStartPlan, r_request.m_fixchangenotice_id AS M_FixChangeNotice_ID FROM r_request WHERE r_request.isactive = 'Y' AND r_request.processed = 'N' AND getdate() > r_request.datenextaction
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW RV_OpenItemToDate(AD_Org_ID, AD_Client_ID, DocumentNo, C_Invoice_ID, C_Order_ID, C_BPartner_ID, IsSOTrx, DateInvoiced, DateAcct, NetDays, DueDate, DaysDue, DiscountDate, DiscountAmt, GrandTotal, C_Currency_ID, C_ConversionType_ID, C_PaymentTerm_ID, IsPayScheduleValid, C_InvoicePaySchedule_ID, InvoiceCollectionType, C_Campaign_ID, C_Project_ID, C_Activity_ID) AS SELECT i.ad_org_id AS AD_Org_ID, i.ad_client_id AS AD_Client_ID, i.documentno AS DocumentNo, i.c_invoice_id AS C_Invoice_ID, i.c_order_id AS C_Order_ID, i.c_bpartner_id AS C_BPartner_ID, i.issotrx AS IsSOTrx, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, p.netdays AS NetDays, paymenttermduedate(i.c_paymentterm_id, i.dateinvoiced) AS DueDate, paymenttermduedays(i.c_paymentterm_id, i.dateinvoiced, getdate()) AS DaysDue, adddays(i.dateinvoiced, p.discountdays) AS DiscountDate, currencyround(i.grandtotal * p.discount / 100, i.c_currency_id, 'N') AS DiscountAmt, i.grandtotal AS GrandTotal, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.ispayschedulevalid AS IsPayScheduleValid, NULL AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID FROM rv_c_invoice i
JOIN c_paymentterm p ON i.c_paymentterm_id = p.c_paymentterm_id WHERE i.ispayschedulevalid <> 'Y' AND (i.docstatus IN ('CO', 'CL')) UNION SELECT i.ad_org_id AS AD_Org_ID, i.ad_client_id AS AD_Client_ID, i.documentno AS DocumentNo, i.c_invoice_id AS C_Invoice_ID, i.c_order_id AS C_Order_ID, i.c_bpartner_id AS C_BPartner_ID, i.issotrx AS IsSOTrx, i.dateinvoiced AS DateInvoiced, i.dateacct AS DateAcct, daysbetween(ips.duedate, i.dateinvoiced) AS NetDays, ips.duedate AS DueDate, daysbetween(getdate(), ips.duedate) AS DaysDue, ips.discountdate AS DiscountDate, ips.discountamt AS DiscountAmt, ips.dueamt AS GrandTotal, i.c_currency_id AS C_Currency_ID, i.c_conversiontype_id AS C_ConversionType_ID, i.c_paymentterm_id AS C_PaymentTerm_ID, i.ispayschedulevalid AS IsPayScheduleValid, ips.c_invoicepayschedule_id AS C_InvoicePaySchedule_ID, i.invoicecollectiontype AS InvoiceCollectionType, i.c_campaign_id AS C_Campaign_ID, i.c_project_id AS C_Project_ID, i.c_activity_id AS C_Activity_ID FROM rv_c_invoice i
JOIN c_invoicepayschedule ips ON i.c_invoice_id = ips.c_invoice_id WHERE i.ispayschedulevalid = 'Y' AND (i.docstatus IN ('CO', 'CL')) AND ips.isvalid = 'Y'
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW RV_PP_Order(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, PP_Order_ID, DocumentNo, DocStatus, M_Warehouse_ID, M_Product_ID, QtyEntered, QtyReject, QtyScrap, QtyBatchs, QtyBatchSize, DateOrdered, DatePromised, DateStart, DateStartSchedule, DateFinish, DateFinishSchedule, DateConfirm, DateDelivered, Lot, PP_Product_BOM_ID, AD_Workflow_ID, Weight, C_DocTypeTarget_ID, M_AttributeSetInstance_ID, Planner_ID, PriorityRule, AD_OrgTrx_ID, User1_ID, User2_ID, C_DocType_ID, Line, Description, S_Resource_ID, FloatBefored, FloatAfter, C_UOM_ID, QtyOrdered, QtyDelivered, Yield, C_Project_ID, C_Campaign_ID, C_Activity_ID, IsApproved, IsPrinted, IsSelected, Processed, Assay, IsQtyPercentage, OrderType, IsSOTrx, ScheduleType, SerNo) AS SELECT o.ad_client_id AS AD_Client_ID, o.ad_org_id AS AD_Org_ID, o.isactive AS IsActive, o.created AS Created, o.createdby AS CreatedBy, o.updated AS Updated, o.updatedby AS UpdatedBy, o.pp_order_id AS PP_Order_ID, o.documentno AS DocumentNo, o.docstatus AS DocStatus, o.m_warehouse_id AS M_Warehouse_ID, o.m_product_id AS M_Product_ID, o.qtyentered AS QtyEntered, o.qtyreject AS QtyReject, o.qtyscrap AS QtyScrap, o.qtybatchs AS QtyBatchs, o.qtybatchsize AS QtyBatchSize, o.dateordered AS DateOrdered, o.datepromised AS DatePromised, o.datestart AS DateStart, o.datestartschedule AS DateStartSchedule, o.datefinish AS DateFinish, o.datefinishschedule AS DateFinishSchedule, o.dateconfirm AS DateConfirm, o.datedelivered AS DateDelivered, o.lot AS Lot, o.pp_product_bom_id AS PP_Product_BOM_ID, o.ad_workflow_id AS AD_Workflow_ID, ( SELECT p.weight FROM m_product p WHERE p.m_product_id = o.m_product_id) AS Weight, o.c_doctypetarget_id AS C_DocTypeTarget_ID, o.m_attributesetinstance_id AS M_AttributeSetInstance_ID, o.planner_id AS Planner_ID, o.priorityrule AS PriorityRule, o.ad_orgtrx_id AS AD_OrgTrx_ID, o.user1_id AS User1_ID, o.user2_id AS User2_ID, o.c_doctype_id AS C_DocType_ID, o.line AS Line,
o.description AS Description, o.s_resource_id AS S_Resource_ID, o.floatbefored AS FloatBefored, o.floatafter AS FloatAfter, o.c_uom_id AS C_UOM_ID, o.qtyordered AS QtyOrdered, o.qtydelivered AS QtyDelivered, o.yield AS Yield, o.c_project_id AS C_Project_ID, o.c_campaign_id AS C_Campaign_ID, o.c_activity_id AS C_Activity_ID, o.isapproved AS IsApproved, o.isprinted AS IsPrinted, o.isselected AS IsSelected, o.processed AS Processed, o.assay AS Assay, o.isqtypercentage AS IsQtyPercentage, o.ordertype AS OrderType, o.issotrx AS IsSOTrx, o.scheduletype AS ScheduleType, o.serno AS SerNo FROM pp_order o
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW RV_PP_Order_Receipt_Issue(PP_Order_BOMLine_ID, IsCritical, Value, M_Product_ID, ProductName, M_AttributeSetInstance_ID, instancename, C_UOM_ID, UOMName, QtyRequiered, qtyreserved_order, QtyOnHand, qtyreserved_storage, QtyAvailable, M_Locator_ID, M_Warehouse_ID, WarehouseName, QtyBOM, IsQtyPercentage, QtyBatch, ComponentType, qtyopen, PP_Order_ID) AS SELECT obl.pp_order_bomline_id AS PP_Order_BOMLine_ID, obl.iscritical AS IsCritical, p.value AS Value, obl.m_product_id AS M_Product_ID, mos.name AS ProductName, mos.m_attributesetinstance_id AS M_AttributeSetInstance_ID, asi.description AS instancename, mos.c_uom_id AS C_UOM_ID, u.name AS UOMName, obl.qtyrequiered AS QtyRequiered, obl.qtyreserved AS qtyreserved_order, mos.qtyonhand AS QtyOnHand, mos.qtyreserved AS qtyreserved_storage, mos.qtyavailable AS QtyAvailable, mos.m_locator_id AS M_Locator_ID, mos.m_warehouse_id AS M_Warehouse_ID, w.name AS WarehouseName, mos.qtybom AS QtyBOM, mos.isqtypercentage AS IsQtyPercentage, mos.qtybatch AS QtyBatch, obl.componenttype AS ComponentType, mos.qtyrequiered - obl.qtydelivered AS qtyopen, obl.pp_order_id AS PP_Order_ID FROM rv_pp_order_storage mos
JOIN pp_order_bomline obl ON mos.pp_order_bomline_id = obl.pp_order_bomline_id
JOIN m_attributesetinstance asi ON mos.m_attributesetinstance_id = asi.m_attributesetinstance_id
JOIN c_uom u ON mos.c_uom_id = u.c_uom_id
JOIN m_product p ON mos.m_product_id = p.m_product_id
JOIN m_warehouse w ON mos.m_warehouse_id = w.m_warehouse_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW RV_RequestUpdates_v(AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, R_Request_ID, AD_User_ID) AS SELECT min(rv_requestupdates.ad_client_id) AS AD_Client_ID, min(rv_requestupdates.ad_org_id) AS AD_Org_ID, CAST('Y' AS CHAR(1)) AS IsActive, getdate() AS Created, 0 AS CreatedBy, getdate() AS Updated, 0 AS UpdatedBy, rv_requestupdates.r_request_id AS R_Request_ID, rv_requestupdates.ad_user_id AS AD_User_ID FROM rv_requestupdates WHERE rv_requestupdates.isactive = 'Y' GROUP BY rv_requestupdates.r_request_id, rv_requestupdates.ad_user_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW X_PackageLineWeight(AD_Client_ID, M_Package_ID, Qty, M_InOutLine_ID, M_InOut_ID, M_Product_ID, Weight, lineweight, M_PackageMPS_ID) AS SELECT pl.ad_client_id AS AD_Client_ID, pl.m_package_id AS M_Package_ID, pl.qty AS Qty, pl.m_inoutline_id AS M_InOutLine_ID, iol.m_inout_id AS M_InOut_ID, iol.m_product_id AS M_Product_ID, p.weight AS Weight, pl.qty * p.weight AS lineweight, pl.m_packagemps_id AS M_PackageMPS_ID FROM m_packageline pl
JOIN m_inoutline iol ON pl.m_inoutline_id = iol.m_inoutline_id
JOIN m_product p ON iol.m_product_id = p.m_product_id
;

-- Nov 28, 2021, 11:19:18 PM CET
CREATE OR REPLACE VIEW RV_Fact_Adaxa(AD_Client_ID, AD_Org_ID, ClientName, DateAcct, BSDate, BSMth, FYear, FYMth, Table_Name, Record_ID, BudName, TaxName, Act_Bud, Currency, AmtSce, AmtAcct, pl_act, bs_act, pl_bud, UOMName, Qty, ProjectName, Description, OrgName, AC_Code, AC_Name, Account_Name, BSTypeName, PLTypeName, AC_Group, BSGroup, PLGroup, PLBS, Na_Eq, BPCode, BPName, BPartnerCodeAndName, BPGroupName, RepName, ProdCode, ProdName, Product, ProdCategory) AS SELECT f.ad_client_id AS AD_Client_ID, f.ad_org_id AS AD_Org_ID, ( SELECT cli.name FROM ad_client cli WHERE cli.ad_client_id = f.ad_client_id) AS ClientName, f.dateacct AS DateAcct, CASE WHEN f.dateacct < (( SELECT clinfo.fy_startdate FROM ad_clientinfo clinfo WHERE clinfo.ad_client_id = f.ad_client_id)) THEN ( SELECT clinfo.fy_startdate - 1 FROM ad_clientinfo clinfo WHERE clinfo.ad_client_id = f.ad_client_id) ELSE f.dateacct END AS BSDate, CASE WHEN f.dateacct < (( SELECT clinfo.fy_startdate FROM ad_clientinfo clinfo WHERE clinfo.ad_client_id = f.ad_client_id)) THEN ( SELECT to_char(clinfo.fy_startdate - 1, 'YYYY-MM') AS to_char FROM ad_clientinfo clinfo WHERE clinfo.ad_client_id = f.ad_client_id) ELSE to_char(f.dateacct, 'YYYY-MM') END AS BSMth, to_char(f.dateacct + 184, '"FY"YYYY') AS FYear, to_char(f.dateacct, 'YYYY-MM') AS FYMth, ( SELECT t.name FROM ad_table t WHERE t.ad_table_id = f.ad_table_id) AS Table_Name, f.record_id AS Record_ID, ( SELECT gl.name FROM gl_budget gl WHERE gl.gl_budget_id = f.gl_budget_id) AS BudName, ( SELECT tax.name FROM c_tax tax WHERE tax.c_tax_id = f.c_tax_id) AS TaxName, CASE f.postingtype WHEN 'A' THEN 'Actual' WHEN 'B' THEN 'Budget' WHEN 'E' THEN 'Encumbrance' WHEN 'S' THEN 'Statistic' ELSE 'Other' END AS Act_Bud, ( SELECT c.iso_code FROM c_currency c WHERE c.c_currency_id = f.c_currency_id) AS Currency, f.amtsourcedr - f.amtsourcecr AS AmtSce, f.amtacctdr - f.amtacctcr AS AmtAcct, CASE WHEN f.postingtype = 'B' THEN 0 ELSE f.amtacctcr - f.amtacctdr END AS pl_act,
CASE WHEN f.postingtype = 'B' THEN 0 WHEN ev.accounttype = 'A' THEN f.amtacctdr - f.amtacctcr WHEN ev.accounttype = 'L' THEN f.amtacctdr - f.amtacctcr WHEN ev.accounttype = 'O' THEN f.amtacctcr - f.amtacctdr WHEN ev.accounttype = 'E' THEN f.amtacctcr - f.amtacctdr WHEN ev.accounttype = 'R' THEN f.amtacctcr - f.amtacctdr ELSE 0 END AS bs_act, CASE WHEN f.postingtype = 'A' THEN 0 WHEN ev.accounttype = 'E' THEN f.amtacctcr - f.amtacctdr WHEN ev.accounttype = 'R' THEN f.amtacctcr - f.amtacctdr ELSE 0 END AS pl_bud, ( SELECT um.uomsymbol FROM c_uom um WHERE um.c_uom_id = f.c_uom_id) AS UOMName, f.qty AS Qty, ( SELECT prj.name FROM c_project prj WHERE prj.c_project_id = f.c_project_id) AS ProjectName, f.description AS Description, o.name AS OrgName, ev.value AS AC_Code, ev.name AS AC_Name, (ev.value || '-') || ev.name AS Account_Name, CASE ev.accounttype WHEN 'A' THEN '1 Asset' WHEN 'E' THEN '3 Equity' WHEN 'L' THEN '2 Liability' WHEN 'M' THEN '9 Memo' WHEN 'O' THEN '3 Equity' WHEN 'R' THEN '3 Equity' ELSE '9. Unknown' END AS BSTypeName, CASE ev.accounttype WHEN 'A' THEN '9 BSheet' WHEN 'E' THEN '6 Expense' WHEN 'L' THEN '9 BSheet' WHEN 'M' THEN '9 Memo' WHEN 'O' THEN '9 BSheet' WHEN 'R' THEN '4 Revenue' ELSE '9. Unknown' END AS PLTypeName, ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 1) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) AS AC_Group,
CASE ev.accounttype WHEN 'A' THEN ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 3) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) WHEN 'E' THEN '330-Current P and L Accounts Balance' WHEN 'L' THEN ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 3) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) WHEN 'O' THEN ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 3) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) WHEN 'R' THEN '330-Current P and L Accounts Balance' ELSE 'Unknown Account Type' END AS BSGroup, CASE ev.accounttype WHEN 'A' THEN '999-Balance Sheet Item' WHEN 'E' THEN ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 3) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) WHEN 'L' THEN '999-Balance Sheet Item' WHEN 'O' THEN '999-Balance Sheet Item' WHEN 'R' THEN ( SELECT (ss.value || '-') || ss.name FROM c_elementvalue ss WHERE ss.value = substr(ev.value, 1, 3) AND ss.issummary = 'Y' AND ss.ad_client_id = f.ad_client_id) ELSE 'Unknown Account Type' END AS PLGroup, CASE ev.accounttype WHEN 'A' THEN 'BS' WHEN 'E' THEN 'PL' WHEN 'L' THEN 'BS' WHEN 'M' THEN 'Unknown Account Type' WHEN 'O' THEN 'BS' WHEN 'R' THEN 'PL' ELSE 'Unknown Account Type' END AS PLBS, CASE ev.accounttype WHEN 'A' THEN '1 NetAssets' WHEN 'E' THEN '2 Equity' WHEN 'L' THEN '1 NetAssets' WHEN 'M' THEN '9 Memo' WHEN 'O' THEN '2 Equity' WHEN 'R' THEN '2 Equity' ELSE 'Unknown Account Type' END AS Na_Eq, bp.value AS BPCode, bp.name AS BPName, CASE WHEN bp.name IS NULL THEN NULL ELSE (bp.value || '-') || bp.name END AS BPartnerCodeAndName, ( SELECT bpg.name FROM c_bp_group bpg WHERE bpg.c_bp_group_id = bp.c_bp_group_id) AS BPGroupName, ( SELECT ad_user.name FROM ad_user WHERE ad_user.ad_user_id = bp.salesrep_id) AS RepName, p.value AS ProdCode,
p.name AS ProdName, CASE WHEN p.name IS NULL THEN NULL ELSE (p.value || '-') || p.name END AS Product, ( SELECT pc.name FROM m_product_category pc WHERE pc.m_product_category_id = p.m_product_category_id) AS ProdCategory FROM fact_acct f
JOIN ad_org o ON f.ad_org_id = o.ad_org_id
JOIN c_elementvalue ev ON f.account_id = ev.c_elementvalue_id
LEFT JOIN c_bpartner bp ON f.c_bpartner_id = bp.c_bpartner_id
LEFT JOIN m_product p ON f.m_product_id = p.m_product_id
;

SELECT register_migration_script('202111282319_IDEMPIERE-5059.sql') FROM dual
;

