/
payment_schedule.php
322 lines (301 loc) · 10.9 KB
/
payment_schedule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?
/**
*
* Copyright (C) 2003-2011 Cory Powers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
require_once('includes/common.php');
require_once('includes/SI_PaymentSchedule.php');
require_once('includes/SI_CompanyTransaction.php');
require_once('includes/SI_Project.php');
require_once('includes/SI_Task.php');
require_once('includes/SI_ItemCode.php');
checkLogin('accounting');
$title = '';
$ps = new SI_PaymentSchedule();
$project = new SI_Project();
$task = new SI_Task();
$item_code = new SI_ItemCode();
// Clean up amount
if(!empty($_POST['amount'])){
$_POST['amount'] = preg_replace('/[^0-9\.]/','', $_POST['amount']);
}
if($_REQUEST['mode'] == 'add'){
$title = "Add Scheduled Billing";
$project_id = intval($_REQUEST['project_id']);
$task_id = intval($_REQUEST['task_id']);
if($project_id == 0 && $task_id == 0){
fatal_error("You must provide a task_id or project_id for this scheduled payment!\n");
}
if($task_id > 0){
if($task->get($task_id) === FALSE){
$error_msg .= "Error getting task information!\n";
debug_message($task->getLastError());
}
if($project->get($task->project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}else{
if($project->get($project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}
if($_POST['save']){
if($_POST['recurrence'] == 'Monthly'){
$start_ts = getTSFromInput($_POST['start_ts']);
$end_ts = getTSFromInput($_POST['end_ts']);
$created_ps = SI_PaymentSchedule::generateScheduledPayments($_POST['project_id'], $_POST['task_id'], $_POST['recurrence'], $start_ts, $end_ts, $_POST['item_code_id'], $_POST['description'], $_POST['amount']);
if($created_ps === FALSE){
$error_msg .= "Error creating scheduled payments!\n";
}else{
//var_dump($created_ps);
goBack();
}
}else{
$_POST['due_ts'] = getTSFromInput($_POST['due_ts']);
$ps->updateFromAssocArray($_POST);
if($ps->add()){
goBack();
}else{
$error_msg .= "Error adding Payment Schedule!\n";
debug_message($ps->getLastError());
}
}
}
}else if($_REQUEST['mode'] == 'edit'){
$title = "Edit Payment Schedule";
if(empty($_REQUEST['id'])){
$error_msg .= "Error: No ID specified!\n";
}else{
if(!$ps->get($_REQUEST['id'])){
$error_msg .= "Error retrieving payment information!\n";
debug_message($ps->getLastError());
}
}
if($ps->task_id > 0){
if($task->get($ps->task_id) === FALSE){
$error_msg .= "Error getting task information!\n";
debug_message($task->getLastError());
}
if($project->get($task->project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}else{
if($project->get($ps->project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}
if($_POST['save']){
$_POST['due_ts'] = getTSFromInput($_POST['due_ts']);
$ps->updateFromAssocArray($_POST);
if($ps->update()){
goBack();
}else{
$error_msg .= "Error updating Payment Schedule!\n";
debug_message($ps->getLastError());
}
}
}else if($_REQUEST['mode'] == 'delete'){
$title = "Delete Payment Schedule";
if(!$ps->get($_REQUEST['id'])){
$error_msg .= "Error retrieving payment information!\n";
debug_message($ps->getLastError());
}
if($ps->task_id > 0){
if($task->get($ps->task_id) === FALSE){
$error_msg .= "Error getting task information!\n";
debug_message($task->getLastError());
}
if($project->get($task->project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}else{
if($project->get($ps->project_id) === FALSE){
$error_msg .= "Error getting project information!\n";
debug_message($project->getLastError());
}
}
if($_POST['confirm']){
if($_POST['confirm'] == "Yes"){
if($ps->delete($_REQUEST['id'])){
goBack();
}else{
$error_msg .= "Error deleting Payment Schedule!\n";
}
}else{
goBack();
}
}
}else{
fatal_error("Invalid mode ({$_REQUEST['mode']}) for this page!");
}
$company =& $project->getCompany();
$item_codes = $item_code->getCompanyPricedCodes($company->id);
if($item_codes === FALSE){
$error_msg .= "Could not get item codes for company!\n";
debug_message($item_code->getLastError());
}
?>
<? require('header.php'); ?>
<script>
function ItemCode(id, description, cost, price){
this.id = id;
this.description = description;
this.cost = cost;
this.price = price;
}
var item_prices = new Array();
<? for($i = 0; $i <= count($item_codes); $i++){
if(!empty($item_codes[$i]->id)){
print("item_prices[".$item_codes[$i]->id."] = new ItemCode(\"".$item_codes[$i]->id."\",\"".$item_codes[$i]->description."\", \"".$item_codes[$i]->cost."\", \"".$item_codes[$i]->price."\");\n");
}
} ?>
function updatePaymentSchedule(){
var oPrice = document.getElementById('amount');
var oDescription = document.getElementById('description');
var oCode = document.getElementById('item_code_id');
if(item_prices[oCode.options[oCode.selectedIndex].value]){
oPrice.value = item_prices[oCode.options[oCode.selectedIndex].value].price;
oDescription.value = item_prices[oCode.options[oCode.selectedIndex].value].description;
}else{
oPrice.value = '';
oDescription.value = '';
}
}
function switchRecurrence(value){
singleEl = document.getElementById('single_entry');
reccEl = document.getElementById('recurrence_settings');
if(value == 'Single'){
singleEl.style.display = 'block';
reccEl.style.display = 'none';
}else{
singleEl.style.display = 'none';
reccEl.style.display = 'block';
}
}
</script>
<form action="<?= $_SERVER['PHP_SELF'] ?>" METHOD="POST">
<input name="id" type="hidden" value="<?= $_REQUEST['id'] ?>">
<input name="project_id" type="hidden" value="<?= $project->id ?>">
<input name="task_id" type="hidden" value="<?= $task->id?>">
<input name="mode" type="hidden" value="<?= $_REQUEST['mode'] ?>">
<?if($_REQUEST['mode'] == "delete"){?>
<table border="0" cellspacing="0" cellpadding="0" class="form_table">
<tr>
<td class="form_header_cell"><?= $title ?></td>
</tr>
<tr>
<td class="form_field_cell">
<br>Are you sure you want to delete this scheduled billing?<br><br>
<span class="error">CAUTION:</span>This action is irreversible.<br><br>
</td>
</tr>
<tr>
<td class="form_footer_cell">
<div align="center">
<input type="submit" class="button" name="confirm" value="Yes">
<input type="submit" class="button" name="confirm" value="No">
</div>
</td>
</tr>
</table>
<? }else{ ?>
<div class="tableContainer">
<a href="javascript:;" class="tCollapse" onclick="toggleGrid(this)"><img src="images/arrow_down.jpg" alt="Hide table" /><?= $title ?></a><div>
<table border="0" cellspacing="0" cellpadding="0" class="form_table" width="450">
<tr>
<td class="form_field_header_cell" width="100">Project:</td>
<td class="form_field_cell"><?= $project->name?></td>
</tr>
<tr>
<td class="form_field_header_cell">Task:</td>
<td class="form_field_cell"><?= $task->name?></td>
</tr>
<tr>
<td class="form_field_header_cell">Item Code:</td>
<td class="form_field_cell">
<select name="item_code_id" id="item_code_id" class="input_text" onchange="updatePaymentSchedule()">
<?= SI_ItemCode::getSelectTags($ps->item_code_id) ?>
</select>
</td>
</tr>
<? if($_REQUEST['mode'] == 'add'){ ?>
<tr>
<td class="form_field_header_cell">Recurrence:</td>
<td class="form_field_cell">
<input type="radio" name="recurrence" onClick="switchRecurrence(this.value)" value="Single" CHECKED> Single
<input type="radio" name="recurrence" onClick="switchRecurrence(this.value)" value="Monthly"> Monthly
</td>
</tr>
<? } ?>
</table>
<div id="recurrence_settings" style="display: none">
<table border="0" cellspacing="0" cellpadding="0" class="form_table" width="450">
<tr>
<td class="form_field_header_cell" width="100">Start Date:</td>
<td class="form_field_cell">
<input type="text" class="input_text" name="start_ts" id="start_ts" SIZE="10" autocomplete="off" value="<?= date("n/j/Y")?>">
<a href="javascript:;" onclick="Uversa.SureInvoice.Calendar.show('start_ts')"><img width="16" height="16" border="0" src="images/dynCalendar.gif"/></a>
</td>
</tr>
<tr>
<td class="form_field_header_cell">End Date:</td>
<td class="form_field_cell">
<input type="text" class="input_text" name="end_ts" id="end_ts" SIZE="10" autocomplete="off" value="<?= date("n/j/Y") ?>">
<a href="javascript:;" onclick="Uversa.SureInvoice.Calendar.show('end_ts')"><img width="16" height="16" border="0" src="images/dynCalendar.gif"/></a>
</td>
</tr>
</table>
</div>
<div id="single_entry">
<table border="0" cellspacing="0" cellpadding="0" class="form_table" width="450">
<tr>
<td class="form_field_header_cell" width="100">Due Date:</td>
<td class="form_field_cell">
<input type="text" class="input_text" name="due_ts" id="due_ts" SIZE="10" autocomplete="off" value="<?= $ps->due_ts > 0 ? date("n/j/Y", $ps->due_ts) : '' ?>">
<a href="javascript:;" onclick="Uversa.SureInvoice.Calendar.show('due_ts')"><img width="16" height="16" border="0" src="images/dynCalendar.gif"/></a>
</td>
</tr>
</table>
</div>
<table border="0" cellspacing="0" cellpadding="0" class="form_table" width="450">
<tr>
<td class="form_field_header_cell" width="100">Amount:</td>
<td class="form_field_cell"><input name="amount" id="amount" class="input_text" size="10" type="text" value="<?= $ps->amount ?>"></td>
</tr>
<tr>
<td class="form_field_header_cell">Description:</td>
<td class="form_field_cell"><input name="description" id="description" class="input_text" size="35" type="text" value="<?= $ps->description ?>"></td>
</tr>
<tr>
<td colspan="2" class="form_field_cell">
<div align="right"><input type="submit" class="button" name="save" value="Save"></div>
</td>
</tr>
</table>
</div>
</div>
<? } //if mode==delete
?>
</form>
<? require('footer.php'); ?>