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