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_recurrent_invoice($description, $order_no, $debtor_no, $group_no, $days, $monthly,
16 $sql = "INSERT INTO ".TB_PREF."recurrent_invoices (description, order_no, debtor_no,
17 group_no, days, monthly, begin, end, last_sent) VALUES (".db_escape($description) . ", "
18 .db_escape($order_no).", ".db_escape($debtor_no).", "
19 .db_escape($group_no).", ".$days.", ".$monthly.", '"
20 .date2sql($begin)."', '".date2sql($end)."', '0000-00-00')";
21 db_query($sql,"The recurrent invoice could not be added");
24 function update_recurrent_invoice($selected_id, $description, $order_no, $debtor_no, $group_no, $days, $monthly,
27 $sql = "UPDATE ".TB_PREF."recurrent_invoices SET
28 description=".db_escape($description).",
29 order_no=".db_escape($order_no).",
30 debtor_no=".db_escape($debtor_no).",
31 group_no=".db_escape($group_no).",
34 begin='".date2sql($begin)."',
35 end='".date2sql($end)."'
36 WHERE id = ".db_escape($selected_id);
37 db_query($sql,"The recurrent invoice could not be updated");
40 function update_last_sent_recurrent_invoice($id, $date)
42 $date = date2sql($date);
43 $sql = "UPDATE ".TB_PREF."recurrent_invoices SET last_sent='$date' WHERE id=".db_escape($id);
45 db_query($sql,"The recurrent invoice could not be updated");
48 function delete_recurrent_invoice($selected_id)
50 $sql="DELETE FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
51 db_query($sql,"could not delete recurrent invoice");
54 function get_recurrent_invoices($date=null)
56 // last_sent stores end date of last generated invoice _plus_ 1 (or is empty)
59 $date = date2sql($date);
60 // we can issue invoice after or at last day of covered period
61 $sql = "SELECT *, DATE_ADD(DATE_ADD(IF(`last_sent`='0000-00-00', `begin`, `last_sent`), INTERVAL `monthly` MONTH), INTERVAL `days` DAY) <= '$date'
62 AND last_sent < DATE_ADD(`end`, INTERVAL 1 DAY) as overdue";
66 $sql .= " FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
68 return db_query($sql,"could not get recurrent invoices");
71 function get_recurrent_invoice($selected_id)
73 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
75 $result = db_query($sql,"could not get recurrent invoice");
76 return db_fetch($result);
79 function check_recurrent_invoice_description($description, $id=null)
81 $sql = "SELECT count(*) FROM ".TB_PREF."recurrent_invoices WHERE description=".db_escape($description);
84 $sql .= " AND id<>".db_escape($id);
86 $result = db_query($sql,"could not check recurrent invoice");
87 $row = db_fetch($result);
91 function recurrent_invoice_ready($selected_id, $date)
93 $date = date2sql($date);
95 $sql = "SELECT DATE_ADD(DATE_ADD(IF(`last_sent`='0000-00-00', `begin`, `last_sent`), INTERVAL `monthly` MONTH), INTERVAL `days` DAY) <= '$date'
96 AND last_sent < DATE_ADD(`end`, INTERVAL 1 DAY) as overdue
97 FROM ".TB_PREF."recurrent_invoices WHERE
98 id=".db_escape($selected_id);
100 $result = db_query($sql,"could not get recurrent invoice");
101 $ret = db_fetch($result);
103 return $ret['overdue'];
106 Return number of invoices generated by recurrent invoice.
108 function recurrent_invoice_count($id)
111 $sql1 = "SELECT branch.*
112 FROM ".TB_PREF."recurrent_invoices rec
113 LEFT JOIN ".TB_PREF."cust_branch branch ON rec.group_no = branch.group_no AND NOT branch.inactive
114 LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = branch.debtor_no AND NOT debtor.inactive
115 WHERE rec.debtor_no=0 AND !ISNULL(debtor.debtor_no) AND rec.id=".db_escape($id);
117 $sql2 = "SELECT branch.*
118 FROM ".TB_PREF."recurrent_invoices rec
119 LEFT JOIN ".TB_PREF."cust_branch branch ON rec.group_no = branch.branch_code AND NOT branch.inactive
120 LEFT JOIN ".TB_PREF."debtors_master debtor ON rec.debtor_no = debtor.debtor_no AND NOT debtor.inactive
121 WHERE rec.debtor_no!=0 AND !ISNULL(debtor.debtor_no) AND rec.id=".db_escape($id);
123 $sql = "SELECT count(*) FROM ($sql1 UNION $sql2) a";
124 $result = db_fetch(db_query($sql, "cannot count recurrent invoices"));
129 function check_recurrent_invoice_prices($id)
132 $inv = get_recurrent_invoice($id);
134 $sql1 = "SELECT debtor.curr_code
135 FROM ".TB_PREF."recurrent_invoices rec
136 LEFT JOIN ".TB_PREF."cust_branch branch ON rec.group_no = branch.group_no AND NOT branch.inactive
137 LEFT JOIN ".TB_PREF."debtors_master debtor ON debtor.debtor_no = branch.debtor_no AND NOT debtor.inactive
138 WHERE rec.debtor_no=0 AND !ISNULL(debtor.debtor_no) AND rec.id=".db_escape($id);
140 $sql2 = "SELECT debtor.curr_code
141 FROM ".TB_PREF."recurrent_invoices rec
142 LEFT JOIN ".TB_PREF."cust_branch branch ON rec.group_no = branch.branch_code AND NOT branch.inactive
143 LEFT JOIN ".TB_PREF."debtors_master debtor ON rec.debtor_no = debtor.debtor_no AND NOT debtor.inactive
144 WHERE rec.debtor_no!=0 AND !ISNULL(debtor.debtor_no) AND rec.id=".db_escape($id);
146 $sql = "SELECT distinct * FROM ($sql1 UNION $sql2) a";
148 $currs = db_query($sql, "cannot count recurrent invoices");
150 $template = get_sales_order_header($inv['order_no'], ST_SALESORDER);
152 while ($curr = db_fetch($currs))
154 $details = get_sales_order_details($inv['order_no'], ST_SALESORDER);
155 while($line = db_fetch($details))
156 if ($curr['curr_code'] != $template['curr_code'] && !get_price($line['stk_code'], $curr['curr_code'], $template['sales_type_id']))
158 display_error(sprintf(_("Unknown %s price for '%s' in pricelist '%s'"), $curr['curr_code'], $line['stk_code'], $template['sales_type']));
166 Returns true when sales order is suitable to be pattern for recurrent invoice.
168 function check_sales_order_type($order_no)
170 $myrow = get_sales_order_header($order_no, ST_SALESORDER);
172 return !$myrow['prepaid'];