2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
15 begin_transaction(__FUNCTION__, func_get_args());
17 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms, type, days, early_discount, early_days)
18 VALUES (" .db_escape($terms) . "," . db_escape($type) . ", ".db_escape($days). ", ".db_escape($early_discount). ", ".db_escape($early_days).")";
20 db_query($sql,"The payment term could not be added");
25 function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
27 begin_transaction(__FUNCTION__, func_get_args());
29 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
30 type=".db_escape($type).",
31 days=" . db_escape($days).",
32 early_discount=".db_escape($early_discount).",
33 early_days=".db_escape($early_days).
34 " WHERE id = " .db_escape($selected_id);
36 db_query($sql,"The payment term could not be updated");
41 function delete_payment_terms($selected_id)
43 begin_transaction(__FUNCTION__, func_get_args());
45 $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
46 db_query($sql,"could not delete a payment terms");
51 function get_payment_terms($selected_id)
53 $sql = "SELECT * FROM ".TB_PREF."payment_terms t WHERE id=".db_escape($selected_id);
55 $result = db_query($sql,"could not get payment term");
57 return db_fetch($result);
60 function get_payment_terms_all($show_inactive)
62 $sql = "SELECT * FROM ".TB_PREF."payment_terms";
63 if (!$show_inactive) $sql .= " WHERE !inactive";
64 return db_query($sql,"could not get payment terms");
68 Calculate due date using terms data provided either as table of payment data or payment terms id
70 function get_payment_due_date($terms_data, $date)
72 if (!is_array($terms_data))
73 $terms_data = get_payment_terms($terms_data);
76 $date = new_doc_date();
81 if ($terms_data['type'] == PTT_FOLLOWING) {
82 $end = end_month(add_months(begin_month($date), 1));
83 $duedate = add_days(end_month($date), $terms_data['days']);
84 if (date1_greater_date2($duedate, $end))
86 } elseif ($terms_data['type'] == PTT_DAYS)
87 $duedate = add_days($date, $terms_data['days']);