Merged changes from stable branch up to 2.3.23.
[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 include_once($path_to_root . "/sales/includes/sales_db.inc");
17
18 $js = "";
19 if ($SysPrefs->use_popup_windows)
20         $js .= get_js_open_window(900, 600);
21 if (user_use_date_picker())
22         $js .= get_js_date_picker();
23
24 page(_($help_context = "Recurrent Invoices"), false, false, "", $js);
25
26 simple_page_mode(true);
27
28 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
29 {
30
31         $input_error = 0;
32
33         if (strlen($_POST['order_no']) == 0) 
34         {
35                 $input_error = 1;
36                 display_error(_("No order has been selected as a template for recurrent invoicing."));
37                 set_focus('order_no');
38         }
39         if (strlen($_POST['description']) == 0) 
40         {
41                 $input_error = 1;
42                 display_error(_("The invoice description cannot be empty."));
43                 set_focus('description');
44         }
45         if (!is_date($_POST['begin'])) {
46                 $input_error = 1;
47                 display_error(_("The entered date is invalid."));
48                 set_focus('begin');
49         }
50         if (!is_date($_POST['end'])) {
51                 $input_error = 1;
52                 display_error(_("The entered date is invalid."));
53                 set_focus('end');
54         }
55         if (isset($_POST['last_sent']) && !is_date($_POST['last_sent'])) {
56                 $input_error = 1;
57                 display_error(_("The entered date is invalid."));
58                 set_focus('last_sent');
59         }
60
61         if ($input_error != 1)
62         {
63         if ($selected_id != -1) 
64         {
65                 update_recurrent_invoice($selected_id, $_POST['description'], $_POST['order_no'], input_num('debtor_no'), 
66                         input_num('group_no'), input_num('days', 0), input_num('monthly', 0), $_POST['begin'], $_POST['end']);
67                 if (isset($_POST['last_sent'])) 
68                                 update_last_sent_recurrent_invoice($selected_id, $_POST['last_sent']);
69                         $note = _('Selected recurrent invoice has been updated');
70         } 
71         else 
72         {
73                 add_recurrent_invoice($_POST['description'], $_POST['order_no'], input_num('debtor_no'), input_num('group_no'),
74                         input_num('days', 0), input_num('monthly', 0), $_POST['begin'], $_POST['end']);
75                         $note = _('New recurrent invoice has been added');
76         }
77     
78                 display_notification($note);            
79                 $Mode = 'RESET';
80         }
81
82
83 if ($Mode == 'Delete')
84 {
85
86         $cancel_delete = 0;
87
88         if ($cancel_delete == 0) 
89         {
90                 delete_recurrent_invoice($selected_id);
91
92                 display_notification(_('Selected recurrent invoice has been deleted'));
93         } //end if Delete area
94         $Mode = 'RESET';
95
96
97 if ($Mode == 'RESET')
98 {
99         $selected_id = -1;
100         unset($_POST);
101 }
102 //-------------------------------------------------------------------------------------------------
103
104 $result = get_recurrent_invoices();
105
106 start_form();
107 start_table(TABLESTYLE, "width=70%");
108 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"", "");
109 table_header($th);
110 $k = 0;
111 while ($myrow = db_fetch($result)) 
112 {
113         $begin = sql2date($myrow["begin"]);
114         $end = sql2date($myrow["end"]);
115         $last_sent = $myrow["last_sent"] == '0000-00-00' ? '' : sql2date($myrow["last_sent"]);
116         
117         alt_table_row_color($k);
118                 
119         label_cell($myrow["description"]);
120         label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
121         if ($myrow["debtor_no"] == 0)
122         {
123                 label_cell("");
124                 label_cell(get_sales_group_name($myrow["group_no"]));
125         }       
126         else
127         {
128                 label_cell(get_customer_name($myrow["debtor_no"]));
129                 label_cell(get_branch_name($myrow['group_no']));
130         }       
131         label_cell($myrow["days"]);
132         label_cell($myrow['monthly']);
133         label_cell($begin);
134         label_cell($end);
135         label_cell($last_sent);
136         edit_button_cell("Edit".$myrow["id"], _("Edit"));
137         delete_button_cell("Delete".$myrow["id"], _("Delete"));
138         end_row();
139 }
140 end_table();
141
142 end_form();
143 echo '<br>';
144
145 //-------------------------------------------------------------------------------------------------
146
147 start_form();
148
149 start_table(TABLESTYLE2);
150
151 if ($selected_id != -1) 
152 {
153         if ($Mode == 'Edit') {
154                 //editing an existing area
155                 $myrow = get_recurrent_invoice($selected_id);
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                 $_POST['last_sent']  = ($myrow['last_sent']=="0000-00-00"?"":sql2date($myrow["last_sent"]));
166         } 
167         hidden("selected_id", $selected_id);
168 }
169
170
171 text_row_ex(_("Description:"), 'description', 50); 
172
173 templates_list_row(_("Template:"), 'order_no');
174
175 customer_list_row(_("Customer:"), 'debtor_no', null, " ", true);
176
177 if ($_POST['debtor_no'] > 0)
178         customer_branches_list_row(_("Branch:"), $_POST['debtor_no'], 'group_no', null, false);
179 else    
180         sales_groups_list_row(_("Sales Group:"), 'group_no', null, " ");
181
182 small_amount_row(_("Days:"), 'days', 0, null, null, 0);
183
184 small_amount_row(_("Monthly:"), 'monthly', 0, null, null, 0);
185
186 date_row(_("Begin:"), 'begin');
187
188 date_row(_("End:"), 'end', null, null, 0, 0, 5);
189
190 if ($selected_id != -1 && $_POST['last_sent'] != "")
191         date_row(_("Last Created"), 'last_sent');
192
193 end_table(1);
194
195 submit_add_or_update_center($selected_id == -1, '', 'both');
196
197 end_form();
198
199 end_page();