7d74dd20d8675bd915394e7511596129a04516ae
[fa-stable.git] / sales / includes / db / recurrent_invoices_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_recurrent_invoice($description, $order_no, $debtor_no, $group_no, $days, $monthly,
14         $begin, $end)
15 {
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");
22 }
23
24 function update_recurrent_invoice($selected_id, $description, $order_no, $debtor_no, $group_no, $days, $monthly,
25         $begin, $end)
26 {
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).", 
32                 days=".$days.", 
33                 monthly=".$monthly.", 
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");
38 }
39
40 function update_last_sent_recurrent_invoice($id, $date)
41 {
42         $date = date2sql($date);
43         $sql = "UPDATE ".TB_PREF."recurrent_invoices SET last_sent='$date' WHERE id=".db_escape($id);
44
45         db_query($sql,"The recurrent invoice could not be updated");
46 }
47
48 function delete_recurrent_invoice($selected_id)
49 {
50         $sql="DELETE FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
51         db_query($sql,"could not delete recurrent invoice");
52 }
53
54 function get_recurrent_invoices($date=null)
55 {
56         // last_sent stores end date of last generated invoice _plus_ 1 (or is empty)
57         if ($date)
58         {
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";
63         } else
64                 $sql = "SELECT * ";
65
66         $sql .= " FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
67
68         return db_query($sql,"could not get recurrent invoices");
69 }
70
71 function get_recurrent_invoice($selected_id)
72 {
73         $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
74
75         $result = db_query($sql,"could not get recurrent invoice");
76         return db_fetch($result);
77 }
78
79 function check_recurrent_invoice_description($description, $id=null)
80 {
81         $sql = "SELECT count(*) FROM ".TB_PREF."recurrent_invoices WHERE description=".db_escape($description);
82
83         if (isset($id))
84                 $sql .= " AND id<>".db_escape($id);
85
86         $result = db_query($sql,"could not check recurrent invoice");
87         $row = db_fetch($result);
88         return !$row[0];
89 }
90
91 function recurrent_invoice_ready($selected_id, $date)
92 {
93         $date = date2sql($date);
94
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);
99
100         $result = db_query($sql,"could not get recurrent invoice");
101         $ret =  db_fetch($result);
102
103         return $ret['overdue'];
104 }
105 /*
106         Return number of invoices generated by recurrent invoice.
107 */
108 function recurrent_invoice_count($id)
109 {
110
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);
116
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);
122
123         $sql = "SELECT count(*) FROM ($sql1 UNION $sql2) a";
124         $result = db_fetch(db_query($sql, "cannot count recurrent invoices"));
125
126         return $result[0];
127 }
128
129 function check_recurrent_invoice_prices($id)
130 {
131         $errors = 0;
132         $inv = get_recurrent_invoice($id);
133
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);
139
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);
145
146         $sql = "SELECT distinct * FROM ($sql1 UNION $sql2) a";
147
148         $currs = db_query($sql, "cannot count recurrent invoices");
149
150         $template = get_sales_order_header($inv['order_no'], ST_SALESORDER);
151
152         while ($curr = db_fetch($currs))
153         {
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']))
157                         {
158                                 display_error(sprintf(_("Unknown %s price for '%s' in pricelist '%s'"), $curr['curr_code'], $line['stk_code'], $template['sales_type']));
159                                 $errors++;
160                         }
161         }
162         return $errors;
163 }
164
165 /*
166         Returns true when sales order is suitable to be pattern for recurrent invoice.
167 */
168 function check_sales_order_type($order_no)
169 {
170         $myrow = get_sales_order_header($order_no, ST_SALESORDER);
171
172         return !$myrow['prepaid'];
173 }