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
|
<?php
/**
* BusinessAdmin: administrative software for small companies
* Copyright (C) 2015 Camil Staps (ViviSoft)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
require_once(__DIR__ . '/../index.php');
require_once(__DIR__ . '/../login.php');
require('./header.php');
?>
<div id="wrapper">
<?php require('./nav.php'); ?>
<div id="page-wrapper">
<div class="row">
<?php
//------------------------------------------------------------------------------
// Check for GET variables
//
// ?id=<id> View information of the offer with id <id>
// ?toggle_accept=<id> Toggle the accepted status of the offer with id <id>
// ?toggle_payment_eligibility=<id> Toggle the payment eligibility of the offer with id <id>
// ?generate_invoice=<id> Generate an invoice for the offer with id <id>
// ?trash_invoice=<id> Trash the invoice file
// ?refesh_payment=<id> Refresh the Braintree status of a payment
// ?delete=<id> Delete the offer with id <id>
//------------------------------------------------------------------------------
// Accept offer
if (isset($_GET['toggle_accept'])) {
$id = (int) $_GET['toggle_accept'];
try {
$offer = new Offer($_pdo, $id);
$offer->accepted = !$offer->accepted;
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The status of offer #{$offer->id} has been set to <i>".($offer->accepted ? "accepted" : "unaccepted")."</i>.</div>";
} catch (Exception $e) {
$alert = "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The acceptance status of offer {$id} could not be changed: {$e->getMessage()}.</div>";
}
}
// Toggle offer payment eligibility
if (isset($_GET['toggle_payment_eligibility'])) {
$id = (int) $_GET['toggle_payment_eligibility'];
try {
$offer = new Offer($_pdo, $id);
if ($offer->getPaymentEligibility()) {
$offer->payment_key = null;
} else {
$offer->payment_key = Offer::getRandomPaymentKey();
}
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer #{$offer->id} is now <i>".($offer->getPaymentEligibility() ? "eligible" : "ineligible")."</i> for online payment.</div>";
} catch (Exception $e) {
$alert = "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The payment eligibility of offer {$id} could not be changed: {$e->getMessage()}.</div>";
}
}
// Generate invoice
if (isset($_GET['generate_invoice'])) {
$id = (int) $_GET['generate_invoice'];
try {
$offer = new Offer($_pdo, $id);
$file = $offer->generateInvoice();
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The invoice for offer #{$offer->id} is generated: <a class='alert-link' href='{$file->getFilenameURI()}' target='_blank'>{$file->filename}</a></div>";
} catch (Exception $e) {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The invoice for offer #{$id} could not be generated: {$e->getMessage()}.</div>";
}
}
// Trash invoice
if (isset($_GET['trash_invoice'])) {
$id = (int) $_GET['trash_invoice'];
try {
$offer = new Offer($_pdo, $id);
$file = $offer->getInvoiceFile();
if ($file instanceof file && $file->delete()) {
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The invoice for offer #{$offer->id} is trashed.</div>";
} else {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The invoice for offer #{$id} could not be trashed.</div>";
}
} catch (Exception $e) {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The invoice for offer #{$id} could not be trashed: {$e->getMessage()}.</div>";
}
}
// Refresh payment
if (isset($_GET['refresh_payment'])) {
$id = (int) $_GET['refresh_payment'];
try {
$payment = new Payment($_pdo, $id);
$payment->refreshBraintreeStatus();
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The payment's status has been updated to <b>{$payment->braintree_status}</b>.</div>";
} catch (Exception $e) {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The payment could not be refreshed due to an exception: {$e->getMessage()}.</div>";
}
}
// Delete offer
if (isset($_GET['delete'])) {
$id = (int) $_GET['delete'];
try {
$offer = new Offer($_pdo, $id);
if ($offer->delete()) {
$alert = "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer #{$offer->id} has been removed.</div>";
} else {
$alert = "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer #{$offer->id} could not be removed. Perhaps it's already removed?</div>";
}
} catch (Exception $e) {
$alert = "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer with id {$id} could not be deleted: {$e->getMessage()}.</div>";
}
}
//------------------------------------------------------------------------------
// The header of the page
//------------------------------------------------------------------------------
$header = 'Offers';
// Whether or not to show an individual offer in the end (false if not, or the id if yes)
$show_individual = false;
// View offer
if (isset($_GET['id'])) {
$id = (int) $_GET['id'];
try {
$offer = new Offer($_pdo, $id);
$header = "<a href='".Constants::url_external."offers'>Offers</a> / #<a href='".Constants::url_external."offers?id={$offer->id}'>{$offer->id}</a>";
$show_individual = $id;
$accepted = $offer->accepted ?
['Accepted', 'btn-success'] :
['Not accepted', 'btn-default'];
$eligible = $offer->getPaymentEligibility() ?
['Eligible for online payment', 'btn-success'] :
['Not eligible for online payment', 'btn-default'];
$header .= "<span class='pull-right'>
<a title='{$accepted[0]}' href='?id={$offer->id}&toggle_accept={$offer->id}' class='btn {$accepted[1]} btn-circle fa fa-fw fa-check'></a>
<a title='{$eligible[0]}' href='?id={$offer->id}&toggle_payment_eligibility={$offer->id}' class='btn {$eligible[1]} btn-circle fa fa-fw fa-credit-card'></a>\t";
if (is_null($offer->getInvoiceFile())) {
$header .= "<a title='Generate invoice' href='?id={$offer->id}&generate_invoice={$offer->id}' class='btn btn-default btn-circle fa fa-fw fa-calculator'></a>";
} else {
$header .= "
<a title='View invoice' href='{$offer->getInvoiceFile()->getFilenameURI()}' target='_blank' class='btn btn-default btn-circle fa fa-eye'></a>
<a title='Regenerate invoice' href='?id={$offer->id}&generate_invoice={$offer->id}' class='btn btn-default btn-circle fa fa-refresh'></a>
<a title='Trash invoice' href='?id={$offer->id}&trash_invoice={$offer->id}' class='btn btn-default btn-circle fa fa-trash'></a>";
}
$header .= "
<a title='Send invoice' href='#' onclick='offerEmail({$offer->id})' class='btn btn-info btn-circle fa fa-fw fa-envelope'></a>
<a title='Delete' href='?delete={$offer->id}' class='btn btn-danger btn-circle fa fa-fw fa-times'></a>
</span>";
} catch (PDOException $e) {
$alert = "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer with id $id</i> could not be found.</div>";
} catch (Exception $e) {
$alert = "<div class='alert alert-warning alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer with id $id</i> could not be found.</div>";
}
}
// Show the header
echo "<div class='col-lg-12'><h1 class='page-header'>$header</h1></div>";
if (isset($alert)) echo "<div class='col-lg-12'>$alert</div>";
if ($show_individual !== false) {
$_id = $show_individual;
require('offers-view.php');
} else {
require('offers-overview.php');
}
?>
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<?php
require('./footer.php');
?>
|