1 CREATE OR REPLACE FUNCTION determineDiscountDate(INTEGER, DATE) RETURNS DATE STABLE AS $$
2 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
3 -- See www.xtuple.com/CPAL for the full text of the software license.
6 pSourceDate ALIAS FOR $2;
12 SELECT terms_type, terms_discdays, terms_cutoffday INTO _p
14 WHERE (terms_id=pTermsid);
16 _discDate := pSourceDate;
18 -- Handle type D terms
19 ELSIF (_p.terms_type = 'D') THEN
20 _discDate := (pSourceDate + _p.terms_discdays);
22 -- Handle type P terms
23 ELSIF (_p.terms_type = 'P') THEN
24 IF (date_part('day', pSourceDate) <= _p.terms_cutoffday) THEN
25 _discDate := (DATE(date_trunc('month', pSourceDate)) + (_p.terms_discdays - 1));
27 _discDate := (DATE(date_trunc('month', pSourceDate)) + (_p.terms_discdays - 1) + INTERVAL '1 month');
30 -- Handle unknown terms
32 _discDate := pSourceDate;
38 $$ LANGUAGE 'plpgsql';