2010-04-28 06:30:29 +07:00
|
|
|
CREATE OR REPLACE FUNCTION paymenttermduedate (in paymentterm_id numeric, in docdate timestamptz) RETURNS timestamptz AS
|
2009-03-25 12:02:55 +07:00
|
|
|
$BODY$
|
2008-11-01 09:07:34 +07:00
|
|
|
/*************************************************************************
|
|
|
|
* The contents of this file are subject to the Compiere License. You may
|
|
|
|
* obtain a copy of the License at http://www.compiere.org/license.html
|
|
|
|
* Software is on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
|
|
|
|
* express or implied. See the License for details. Code: Compiere ERP+CRM
|
|
|
|
* Copyright (C) 1999-2001 Jorg Janke, ComPiere, Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* converted to postgreSQL by Karsten Thiemann (Schaeffer AG),
|
|
|
|
* kthiemann@adempiere.org
|
|
|
|
*************************************************************************
|
|
|
|
* Title: Get Due timestamp with time zone
|
|
|
|
* Description:
|
|
|
|
* Returns the due timestamp with time zone
|
|
|
|
* Test:
|
|
|
|
* select paymenttermDueDate(106, now()) from Test; => now()+30 days
|
|
|
|
************************************************************************/
|
|
|
|
DECLARE
|
|
|
|
Days NUMERIC := 0;
|
|
|
|
DueDate timestamp with time zone := TRUNC(DocDate);
|
|
|
|
--
|
|
|
|
FirstDay timestamp with time zone;
|
|
|
|
NoDays NUMERIC;
|
|
|
|
p RECORD;
|
|
|
|
BEGIN
|
|
|
|
FOR p IN
|
|
|
|
SELECT *
|
|
|
|
FROM C_PaymentTerm
|
|
|
|
WHERE C_PaymentTerm_ID = PaymentTerm_ID
|
|
|
|
LOOP -- for convineance only
|
|
|
|
-- Due 15th of following month
|
|
|
|
IF (p.IsDueFixed = 'Y') THEN
|
|
|
|
FirstDay := TRUNC(DocDate, 'MM');
|
2010-04-28 06:30:29 +07:00
|
|
|
NoDays := EXTRACT(day FROM TRUNC(DocDate) - FirstDay);
|
2008-11-01 09:07:34 +07:00
|
|
|
DueDate := FirstDay + (p.FixMonthDay-1); -- starting on 1st
|
|
|
|
DueDate := ADD_MONTHS(DueDate, p.FixMonthOffset);
|
|
|
|
IF (NoDays > p.FixMonthCutoff) THEN
|
|
|
|
DueDate := ADD_MONTHS(DueDate, 1);
|
|
|
|
END IF;
|
|
|
|
ELSE
|
|
|
|
DueDate := TRUNC(DocDate) + p.NetDays;
|
|
|
|
END IF;
|
|
|
|
END LOOP;
|
|
|
|
RETURN DueDate;
|
|
|
|
END;
|
2009-03-25 12:02:55 +07:00
|
|
|
$BODY$
|
2014-05-31 00:11:20 +07:00
|
|
|
LANGUAGE 'plpgsql' STABLE
|
2009-03-25 12:02:55 +07:00
|
|
|
;
|
2014-05-31 00:11:20 +07:00
|
|
|
|