src/org/openbravo/erpCommon/ad_reports/ReportInvoiceDiscount_data.xsql
author RM packaging bot <staff.rm@openbravo.com>
Thu, 12 Dec 2019 21:42:11 +0000
changeset 36861 954d2b7a66fb
parent 36388 4134f50a66ff
permissions -rw-r--r--
CI: update AD_MODULE to version 36860
<?xml version="1.0" encoding="UTF-8" ?>
<!--
 *************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
 * Version 1.1  with a permitted attribution clause; you may not  use this
 * file except in compliance with the License. You  may  obtain  a copy of
 * the License at http://www.openbravo.com/legal/license.html 
 * Software distributed under the License  is  distributed  on  an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific  language  governing  rights  and  limitations
 * under the License. 
 * The Original Code is Openbravo ERP. 
 * The Initial Developer of the Original Code is Openbravo SLU 
 * All portions are Copyright (C) 2001-2019 Openbravo SLU 
 * All Rights Reserved. 
 * Contributor(s):  ______________________________________.
 ************************************************************************
-->





<SqlClass name="ReportInvoiceDiscountData" package="org.openbravo.erpCommon.ad_reports">
  <SqlClassComment></SqlClassComment>
  <SqlMethod name="select" type="preparedStatement" return="scrollable">
    <SqlMethodComment></SqlMethodComment>
    <Sql>
    <![CDATA[
	SELECT C_BPARTNER_ID as ID, NAME, PRODUCTNAME, QTY,UOM, AUMQTY, AUM, ROUND(C_DIVIDE(TOTALLINE,QTY),3) AS PRICEACTUAL, TOTALLINE, 
	ROUND(C_DIVIDE(TOTALLINEDISCOUNT,QTY),3) AS REALPRICE, TOTALLINEDISCOUNT,        
	(ROUND(C_DIVIDE((ROUND(C_DIVIDE(TOTALLINE,QTY),3)-ROUND(C_DIVIDE(TOTALLINEDISCOUNT,QTY),3)),ROUND(C_DIVIDE(TOTALLINE,QTY),3)),2))*100 AS DISCOUNT,        
	(CASE (ROUND(C_DIVIDE((ROUND(C_DIVIDE(TOTALLINE,QTY),3)-ROUND(C_DIVIDE(TOTALLINEDISCOUNT,QTY),3)),ROUND(C_DIVIDE(TOTALLINE,QTY),3)),2))*100 WHEN 0 THEN '' ELSE '' END)  AS CLASS_DESIGN,        
	WEIGHT,	TRCURRENCYID AS TRANSCURRENCYID, TRDATE AS TRANSDATE,	TRCLIENTID AS TRANSCLIENTID, TRORGID AS TRANSORGID,
	C_CURRENCY_SYMBOL(?, 0, 'Y') AS CONVSYM, C_CURRENCY_ISOSYM(?) AS CONVISOSYM
	FROM (SELECT C_DOCTYPE.DOCBASETYPE,C_BPARTNER.C_BPARTNER_ID, C_BPARTNER.NAME, M_PRODUCT.NAME AS PRODUCTNAME, C_UOM.UOMSYMBOL AS UOM, CASE WHEN C_DOCTYPE.DOCBASETYPE='ARC' THEN SUM(C_INVOICELINE.QTYINVOICED)*-1 ELSE SUM(C_INVOICELINE.QTYINVOICED) END AS QTY, 
	CASE WHEN C_INVOICELINE.C_AUM IS NULL THEN NULL ELSE (SELECT u.UOMSYMBOL FROM C_UOM u WHERE u.C_UOM_ID = C_INVOICELINE.C_AUM) END AS AUM,
	CASE WHEN C_DOCTYPE.DOCBASETYPE='ARC' THEN SUM(C_INVOICELINE.AUMQTY)*-1 ELSE SUM(C_INVOICELINE.AUMQTY) END AS AUMQTY,
	C_CURRENCY_CONVERT(ROUND(CASE WHEN C_DOCTYPE.DOCBASETYPE='ARC' THEN SUM(LINENETAMT)*-1 ELSE SUM(LINENETAMT) END,2), C_INVOICE.C_CURRENCY_ID, ?,  TO_DATE(COALESCE(C_INVOICE.DATEINVOICED, NOW())), NULL, C_INVOICELINE.AD_CLIENT_ID, C_INVOICELINE.AD_ORG_ID) AS TOTALLINE,         
	C_CURRENCY_CONVERT(ROUND(SUM(CASE WHEN C_DOCTYPE.DOCBASETYPE='ARC' THEN LINENETAMT*-1*(1-COALESCE(DISCOUNTPERUNIT,0)) ELSE LINENETAMT*(1-COALESCE(DISCOUNTPERUNIT,0)) END),2), C_INVOICE.C_CURRENCY_ID, ?,  TO_DATE(COALESCE(C_INVOICE.DATEINVOICED, NOW())), NULL, C_INVOICELINE.AD_CLIENT_ID, C_INVOICELINE.AD_ORG_ID) AS TOTALLINEDISCOUNT, 
	SUM(C_INVOICELINE.QTYINVOICED)*COALESCE(M_PRODUCT.WEIGHT,0) AS WEIGHT,
	C_INVOICE.C_CURRENCY_ID AS TRCURRENCYID,
	TO_DATE(COALESCE(C_INVOICE.DATEINVOICED, NOW())) AS TRDATE,
	C_INVOICELINE.AD_CLIENT_ID AS TRCLIENTID, 
	C_INVOICELINE.AD_ORG_ID AS TRORGID      
	FROM C_INVOICELINE left join          
	(SELECT C_INVOICE_ID, DISCOUNTLINES/(TOTALLINES+DISCOUNTLINES) AS DISCOUNTPERUNIT           
	FROM (SELECT I.C_INVOICE_ID, 
	C_CURRENCY_CONVERT(I.TOTALLINES, I.C_CURRENCY_ID, ?,  TO_DATE(COALESCE(I.DATEINVOICED, NOW())), NULL, I.AD_CLIENT_ID, I.AD_ORG_ID) AS TOTALLINES, 
	C_CURRENCY_CONVERT(-SUM(LINENETAMT), I.C_CURRENCY_ID, ?,  TO_DATE(COALESCE(I.DATEINVOICED, NOW())), NULL, IL.AD_CLIENT_ID, IL.AD_ORG_ID) AS DISCOUNTLINES
	FROM C_INVOICE I, C_INVOICELINE IL           
	WHERE I.C_INVOICE_ID = IL.C_INVOICE_ID AND IL.C_INVOICE_DISCOUNT_ID IS NOT NULL AND I.AD_CLIENT_ID IN ('1') AND I.AD_ORG_ID IN ('1')                 
	GROUP BY I.C_INVOICE_ID, I.TOTALLINES, I.C_CURRENCY_ID, I.DATEINVOICED, I.AD_CLIENT_ID, I.AD_ORG_ID, IL.AD_CLIENT_ID, IL.AD_ORG_ID) BB) A on C_INVOICELINE.C_INVOICE_ID = A.C_INVOICE_ID
    left join C_INVOICE on C_INVOICELINE.C_INVOICE_ID = C_INVOICE.C_INVOICE_ID
    LEFT Join C_DOCTYPE on C_INVOICE.C_DOCTYPE_ID=C_DOCTYPE.C_DOCTYPE_ID,
    M_PRODUCT, C_BPARTNER,  C_UOM
    WHERE  C_INVOICELINE.C_INVOICE_DISCOUNT_ID IS NULL
    AND C_INVOICELINE.M_PRODUCT_ID = M_PRODUCT.M_PRODUCT_ID
    AND M_PRODUCT.C_UOM_ID = C_UOM.C_UOM_ID
    AND C_INVOICE.C_BPARTNER_ID = C_BPARTNER.C_BPARTNER_ID
    AND C_INVOICE.ISSOTRX='Y'
    AND C_INVOICE.AD_CLIENT_ID IN ('1')
    AND C_INVOICE.AD_ORG_ID IN ('1')
    AND PRICEACTUAL<>0
    AND C_INVOICE.DATEINVOICED >= to_date(?)
    AND C_INVOICE.DATEINVOICED <= to_date(?)
    AND 1=1
    GROUP BY C_BPARTNER.C_BPARTNER_ID, C_BPARTNER.NAME, M_PRODUCT.NAME, C_UOM.UOMSYMBOL, C_INVOICELINE.C_AUM, C_INVOICELINE.AUMQTY, M_PRODUCT.WEIGHT, 
    C_INVOICE.C_CURRENCY_ID, C_INVOICE.DATEINVOICED, C_INVOICELINE.AD_CLIENT_ID, C_INVOICELINE.AD_ORG_ID,C_DOCTYPE.DOCBASETYPE) AA
    WHERE QTY<>0
    AND 2=2
    ORDER BY NAME
      ]]></Sql>
    <Parameter name="cCurrencyConv"/>
	<Parameter name="cCurrencyConv"/>
	<Parameter name="cCurrencyConv"/>
	<Parameter name="cCurrencyConv"/>
	<Parameter name="cCurrencyConv"/>
	<Parameter name="cCurrencyConv"/>
    <Parameter name="adUserClient" type="replace" optional="true" after="I.AD_CLIENT_ID IN (" text="'1'"/>
    <Parameter name="adUserOrg" type="replace" optional="true" after="I.AD_ORG_ID IN (" text="'1'"/>
    <Parameter name="adUserClient" type="replace" optional="true" after="C_INVOICE.AD_CLIENT_ID IN (" text="'1'"/>
    <Parameter name="adUserOrg" type="replace" optional="true" after="C_INVOICE.AD_ORG_ID IN (" text="'1'"/>
    <Parameter name="datefrom"/>
    <Parameter name="dateto"/>
    <Parameter name="parBPartnerId" optional="true" type="argument" after="AND 1=1"><![CDATA[ AND C_BPARTNER.C_BPARTNER_ID IN]]></Parameter>
    <Parameter name="discount" optional="true" type="none" after="AND 2=2"><![CDATA[ AND (ROUND((ROUND(TOTALLINE/QTY,3)-ROUND(TOTALLINEDISCOUNT/QTY,3))/CASE WHEN ROUND(TOTALLINE/QTY,3) = 0 THEN 1 ELSE ROUND(TOTALLINE/QTY,3) END,2))*100 <>0]]></Parameter>
  </SqlMethod>
</SqlClass>