aboutsummaryrefslogtreecommitdiff
path: root/include/offers.php
blob: 2aa150d870d6c7e9070e36d1f39e05aec6f1b46b (plain) (blame)
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
<?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('./index.php');
require_once('./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>
			// ?generate_invoice=<id> 	Generate an invoice for the offer with id <id>
			// ?trash_invoice=<id>			Trash the invoice file
			// ?delete=<id>       			Delete the offer with id <id>
			//------------------------------------------------------------------------------

			// 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> / #{$offer->id}";
					$show_individual = $id;
				} 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>";

			// Accept offer
			if (isset($_GET['toggle_accept'])) {
				echo "<div class='col-lg-12'>";
				$id = (int) $_GET['toggle_accept'];
				try {
					$offer = new Offer($_pdo, $id);
					$offer->accepted = !$offer->accepted;
					echo "<div class='alert alert-success alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The status offer #{$offer->id} has been set to <i>".($offer->accepted ? "accepted" : "unaccepted")."</i>.</div>";
				} catch (PDOException $e) {
					echo "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The status of the offer could not be changed due to a PDO error.</div>";
				} catch (Exception $e) {
					echo "<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 found.</div>";
				}

				echo "</div>";
			}

			// Generate invoice
			if (isset($_GET['generate_invoice'])) {
				echo "<div class='col-lg-12'>";
				$id = (int) $_GET['generate_invoice'];
				try {
					$offer = new Offer($_pdo, $id);
					$file = $offer->generateInvoice();
					echo "<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 (PDOException $e) {
					echo "<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 #{$offer->id} could not be generated due to a PDO error.</div>";
				} catch (Exception $e) {
					echo "<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.</div>";
				}
				echo "</div>";
			}

			// Trash invoice
			if (isset($_GET['trash_invoice'])) {
				echo "<div class='col-lg-12'>";
				$id = (int) $_GET['trash_invoice'];
				try {
					$offer = new Offer($_pdo, $id);
					$file = $offer->getInvoiceFile();
					if ($file instanceof file && $file->delete()) {
						echo "<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 {
						echo "<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 (PDOException $e) {
					echo "<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 #{$offer->id} could not be trashed due to a PDO error.</div>";
				} catch (Exception $e) {
						echo "<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>";
				}
				echo "</div>";
			}

			// Delete offer
			if (isset($_GET['delete'])) {
				echo "<div class='col-lg-12'>";
				$id = (int) $_GET['delete'];
				try {
					$offer = new Offer($_pdo, $id);
					if ($offer->delete()) {
						echo "<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 {
						echo "<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 (PDOException $e) {
					echo "<div class='alert alert-danger alert-dismissable'><button type='button' class='close fa fa-times' data-dismiss='alert' aria-hidden='true'></button>The offer could not be removed due to a PDO error.</div>";
				} catch (Exception $e) {
					echo "<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 found.</div>";
				}

				echo "</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');
?>