Payment terms related functions moved to separate file, common function for calculati...
[fa-stable.git] / admin / db / payment_terms_db.inc
1 <?php
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 ***********************************************************************/
12
13 function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
14 {
15         begin_transaction(__FUNCTION__, func_get_args());
16
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).")";
19
20         db_query($sql,"The payment term could not be added");
21
22         commit_transaction();
23 }
24
25 function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
26 {
27         begin_transaction(__FUNCTION__, func_get_args());
28
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);
35
36         db_query($sql,"The payment term could not be updated");
37
38         commit_transaction();
39 }
40
41 function delete_payment_terms($selected_id)
42 {
43         begin_transaction(__FUNCTION__, func_get_args());
44
45         $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
46         db_query($sql,"could not delete a payment terms");
47
48         commit_transaction();
49 }
50
51 function get_payment_terms($selected_id)
52 {
53         $sql = "SELECT * FROM ".TB_PREF."payment_terms t WHERE id=".db_escape($selected_id);
54
55         $result = db_query($sql,"could not get payment term");
56
57         return db_fetch($result);
58 }
59
60 function get_payment_terms_all($show_inactive)
61 {
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");
65 }
66
67 /*
68         Calculate due date using terms data provided either as table of payment data or payment terms id
69 */
70 function get_payment_due_date($terms_data, $date)
71 {
72         if (!is_array($terms_data))
73                 $terms_data = get_payment_terms($terms_data);
74
75         if (!is_date($date))
76                 $date = new_doc_date();
77         
78         if (!$terms_data)
79                 return $date;
80
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))
85                         $duedate = $end;
86         } elseif ($terms_data['type'] == PTT_DAYS)
87                 $duedate = add_days($date, $terms_data['days']);
88         else
89                 $duedate = $date;
90         return $duedate;
91 }