Update from usntable branch.
[fa-stable.git] / sales / manage / recurrent_invoices.php
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 $page_security = 'SA_SRECURRENT';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15 include($path_to_root . "/includes/ui.inc");
16
17 $js = "";
18 if ($use_popup_windows)
19         $js .= get_js_open_window(900, 600);
20 if ($use_date_picker)
21         $js .= get_js_date_picker();
22
23 page(_($help_context = "Recurrent Invoices"), false, false, "", $js);
24
25 simple_page_mode(true);
26
27 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
28 {
29
30         $input_error = 0;
31
32         if (strlen($_POST['description']) == 0) 
33         {
34                 $input_error = 1;
35                 display_error(_("The area description cannot be empty."));
36                 set_focus('description');
37         }
38
39         if ($input_error != 1)
40         {
41         if ($selected_id != -1) 
42         {
43                 $sql = "UPDATE ".TB_PREF."recurrent_invoices SET 
44                         description=".db_escape($_POST['description']).", 
45                         order_no=".db_escape($_POST['order_no']).", 
46                         debtor_no=".db_escape($_POST['debtor_no']).", 
47                         group_no=".db_escape($_POST['group_no']).", 
48                         days=".input_num('days', 0).", 
49                         monthly=".input_num('monthly', 0).", 
50                         begin='".date2sql($_POST['begin'])."', 
51                         end='".date2sql($_POST['end'])."' 
52                         WHERE id = ".db_escape($selected_id);
53                         $note = _('Selected recurrent invoice has been updated');
54         } 
55         else 
56         {
57                 $sql = "INSERT INTO ".TB_PREF."recurrent_invoices (description, order_no, debtor_no,
58                         group_no, days, monthly, begin, end, last_sent) VALUES (".db_escape($_POST['description']) . ", "
59                         .db_escape($_POST['order_no']).", ".db_escape($_POST['debtor_no']).", "
60                         .db_escape($_POST['group_no']).", ".input_num('days', 0).", ".input_num('monthly', 0).", '"
61                         .date2sql($_POST['begin'])."', '".date2sql($_POST['end'])."', '".date2sql(Add_Years($_POST['begin'], -5))."')";
62                         $note = _('New recurrent invoice has been added');
63         }
64     
65         db_query($sql,"The recurrent invoice could not be updated or added");
66                 display_notification($note);            
67                 $Mode = 'RESET';
68         }
69
70
71 if ($Mode == 'Delete')
72 {
73
74         $cancel_delete = 0;
75
76         if ($cancel_delete == 0) 
77         {
78                 $sql="DELETE FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
79                 db_query($sql,"could not delete recurrent invoice");
80
81                 display_notification(_('Selected recurrent invoice has been deleted'));
82         } //end if Delete area
83         $Mode = 'RESET';
84
85
86 if ($Mode == 'RESET')
87 {
88         $selected_id = -1;
89         unset($_POST);
90 }
91 //-------------------------------------------------------------------------------------------------
92 function get_sales_group_name($group_no)
93 {
94         $sql = "SELECT description FROM ".TB_PREF."groups WHERE id = ".db_escape($group_no);
95         $result = db_query($sql, "could not get group");
96         $row = db_fetch($result);
97         return $row[0];
98 }
99
100 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
101 $result = db_query($sql,"could not get recurrent invoices");
102
103 start_form();
104 start_table("$table_style width=70%");
105 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"", "");
106 table_header($th);
107 $k = 0;
108 while ($myrow = db_fetch($result)) 
109 {
110         $begin = sql2date($myrow["begin"]);
111         $end = sql2date($myrow["end"]);
112         $last_sent = sql2date($myrow["last_sent"]);
113         
114         alt_table_row_color($k);
115                 
116         label_cell($myrow["description"]);
117         label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
118         if ($myrow["debtor_no"] == 0)
119         {
120                 label_cell("");
121                 label_cell(get_sales_group_name($myrow["group_no"]));
122         }       
123         else
124         {
125                 label_cell(get_customer_name($myrow["debtor_no"]));
126                 label_cell(get_branch_name($myrow['group_no']));
127         }       
128         label_cell($myrow["days"]);
129         label_cell($myrow['monthly']);
130         label_cell($begin);
131         label_cell($end);
132         label_cell($last_sent);
133         edit_button_cell("Edit".$myrow["id"], _("Edit"));
134         delete_button_cell("Delete".$myrow["id"], _("Delete"));
135         end_row();
136 }
137 end_table();
138
139 end_form();
140 echo '<br>';
141
142 //-------------------------------------------------------------------------------------------------
143
144 start_form();
145
146 start_table($table_style2);
147
148 if ($selected_id != -1) 
149 {
150         if ($Mode == 'Edit') {
151                 //editing an existing area
152                 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($selected_id);
153
154                 $result = db_query($sql,"could not get recurrent invoice");
155                 $myrow = db_fetch($result);
156
157                 $_POST['description']  = $myrow["description"];
158                 $_POST['order_no']  = $myrow["order_no"];
159                 $_POST['debtor_no']  = $myrow["debtor_no"];
160                 $_POST['group_no']  = $myrow["group_no"];
161                 $_POST['days']  = $myrow["days"];
162                 $_POST['monthly']  = $myrow["monthly"];
163                 $_POST['begin']  = sql2date($myrow["begin"]);
164                 $_POST['end']  = sql2date($myrow["end"]);
165         } 
166         hidden("selected_id", $selected_id);
167 }
168
169
170 text_row_ex(_("Description:"), 'description', 50); 
171
172 templates_list_row(_("Template:"), 'order_no');
173
174 customer_list_row(_("Customer:"), 'debtor_no', null, " ", true);
175
176 if ($_POST['debtor_no'] > 0)
177         customer_branches_list_row(_("Branch:"), $_POST['debtor_no'], 'group_no', null, false);
178 else    
179         sales_groups_list_row(_("Sales Group:"), 'group_no', null, " ");
180
181 small_amount_row(_("Days:"), 'days', 0, null, null, 0);
182
183 small_amount_row(_("Monthly:"), 'monthly', 0, null, null, 0);
184
185 date_row(_("Begin:"), 'begin');
186
187 date_row(_("End:"), 'end', null, null, 0, 0, 5);
188
189 end_table(1);
190
191 submit_add_or_update_center($selected_id == -1, '', 'both');
192
193 end_form();
194
195 end_page();
196 ?>