/
add_debt.php
157 lines (123 loc) · 5.61 KB
/
add_debt.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
<?php
include 'config.inc.php';
include 'users.class.php';
include 'debts.class.php';
include 'payments.class.php';
if (!isset($_COOKIE['user_id'])) {
header("Location: login.php");
exit();
}
$is_ajax = false;
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$is_ajax = true;
}
$user_id = $_COOKIE['user_id'];
$total = 0;
// Connect to database
$con = mysql_connect(DB_HOST, DB_USER, DB_PW);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(DB_NAME, $con);
// if the form has been submitted
if (isset($_POST['submit_check'])):
$paymentObj = new Payment();
$paymentObj->setUser_id($user_id);
$paymentObj->create();
$payment_id = mysql_insert_id();
$debtObj = new Debt();
$debtObj->setOwed_to($user_id);
$debtObj->setPayment_id($payment_id);
$total = 0;
for ($i = 0; $i < count($_POST["user_id"]); $i++) {
$debtObj->setOwed_by($_POST['user_id'][$i]);
$debt_total = $_POST['drinks_owed'][$i] + $_POST['food_owed'][$i];
$total += $debt_total;
$debtObj->setAmount($debt_total);
$debtObj->create();
//echo "<p>" . $_POST['user_id'][$i] . " owes " . $_COOKIE['user_id'] . " £" . $_POST['drinks_owed'][$i] . " for drinks and £" . $_POST['food_owed'][$i] . " for food for a total of £" . $total . "</p>";
}
$paymentObj->setId($payment_id);
$paymentObj->setTotal($total);
$paymentObj->setDate($_POST['date']);
$paymentObj->update();
header("Location: dashboard.php");
exit();
else:
// Get list of users for form
$user_arr = User::getAll();
// Built debt form input
$userObj = new User;
$debt_form = "<select name='user_id[]'>\n";
$debt_form .= "<option>Select a user</option>\n";
foreach ($user_arr as $user_id) {
$userObj->setId($user_id);
$userObj->load();
$debt_form .= "<option value='$user_id'>" . $userObj->getName() . "</option>\n";
}
$debt_form .= "</select>\n";
$debt_form .= "<div class=\"input-prepend \">
<span class=\"add-on\">£</span>
<input class=\"input-medium drinks\" id=\"drinks_owed1\" name=\"drinks_owed[]\" size=\"16\" type=\"number\" placeholder=\"Drinks\">
</div>\n";
$debt_form .= "<div class=\"input-prepend \">
<span class=\"add-on\">£</span>
<input class=\"input-medium food\" id=\"food_owed1\" name=\"food_owed[]\" size=\"16\" type=\"number\" placeholder=\"Food\">
</div>\n";
if (!$is_ajax) {
include 'header.php';
echo "<h3>Add Debt</h3>";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" id="modal-form" class="form-inline">
<div class="input-append date" id="dp3" data-date="<?php echo date('Y-m-d'); ?>" data-date-format="yyyy-mm-dd">
<input class="span2" size="16" type="text" name="date" readonly='' value="<?php echo date('Y-m-d'); ?>">
<span class="add-on"><i class="icon-th"></i></span>
</div>
<div id="input1" class="clonedInput">
<?php echo $debt_form; ?>
</div>
<button id="btnAdd" class="btn"><i class="icon-plus"></i> Add another attendee</button>
<button id="btnDel" class="btn"><i class="icon-trash"></i> Remove attendee</button>
<input type="hidden" name="submit_check" value="1"/>
<?php
if (!$is_ajax) {
echo "<input type=\"submit\" value=\"Submit\" class=\"btn btn-primary\" />";
}
?>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#dp3').datepicker();
$('#btnDel').attr('disabled','disabled');
$('#btnAdd').click(function(e) {
e.preventDefault();
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.find('input :first').attr('id', 'user_id' + newNum);
newElem.find('.drinks').attr('id', 'drinks_owed' + newNum).val('');
newElem.find('.food').attr('id', 'food_owed' + newNum).val('');
// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);
$('#btnDel').removeAttr('disabled');
if (newNum == 6)
$('#btnAdd').attr('disabled','disabled');
});
$('#btnDel').click(function(e) {
e.preventDefault();
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
$('#input' + num).remove(); // remove the last element
$('#btnAdd').removeAttr('disabled');
if (num-1 == 1)
$('#btnDel').attr('disabled','disabled');
});
});
</script>
<?php
mysql_close($con);
if (!$is_ajax) {
include 'footer.php';
}
endif;
?>