//------------------------------------------------------------------------------
// Accept offer
if (isset($_GET['toggle_accept'])) {
$id = (int) $_GET['toggle_accept'];
try {
$offer = new Offer($_pdo, $id);
$offer->accepted = !$offer->accepted;
$alert = " The status of offer #{$offer->id} has been set to ".($offer->accepted ? "accepted" : "unaccepted")." .
";
} catch (Exception $e) {
$alert = " The acceptance status of offer {$id} could not be changed: {$e->getMessage()}.
";
}
}
// 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 = " The offer #{$offer->id} is now ".($offer->getPaymentEligibility() ? "eligible" : "ineligible")." for online payment.
";
} catch (Exception $e) {
$alert = " The payment eligibility of offer {$id} could not be changed: {$e->getMessage()}.
";
}
}
// Generate invoice
if (isset($_GET['generate_invoice'])) {
$id = (int) $_GET['generate_invoice'];
try {
$offer = new Offer($_pdo, $id);
$file = $offer->generateInvoice();
$alert = "";
} catch (Exception $e) {
$alert = " The invoice for offer #{$id} could not be generated: {$e->getMessage()}.
";
}
}
// 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 = " The invoice for offer #{$offer->id} is trashed.
";
} else {
$alert = " The invoice for offer #{$id} could not be trashed.
";
}
} catch (Exception $e) {
$alert = " The invoice for offer #{$id} could not be trashed: {$e->getMessage()}.
";
}
}
// Refresh payment
if (isset($_GET['refresh_payment'])) {
$id = (int) $_GET['refresh_payment'];
try {
$payment = new Payment($_pdo, $id);
$payment->refreshBraintreeStatus();
$alert = " The payment's status has been updated to {$payment->braintree_status} .
";
} catch (Exception $e) {
$alert = " The payment could not be refreshed due to an exception: {$e->getMessage()}.
";
}
}
// Delete offer
if (isset($_GET['delete'])) {
$id = (int) $_GET['delete'];
try {
$offer = new Offer($_pdo, $id);
if ($offer->delete()) {
$alert = " The offer #{$offer->id} has been removed.
";
} else {
$alert = " The offer #{$offer->id} could not be removed. Perhaps it's already removed?
";
}
} catch (Exception $e) {
$alert = " The offer with id {$id} could not be deleted: {$e->getMessage()}.
";
}
}
//------------------------------------------------------------------------------
// 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 = "Offers / #{$offer->id} ";
$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 .= "
\t";
if (is_null($offer->getInvoiceFile())) {
$header .= " ";
} else {
$header .= "
";
}
$header .= "
";
} catch (PDOException $e) {
$alert = " The offer with id $id could not be found.
";
} catch (Exception $e) {
$alert = " The offer with id $id could not be found.
";
}
}
// Show the header
echo "
";
if (isset($alert)) echo "$alert
";
if ($show_individual !== false) {
$_id = $show_individual;
require('offers-view.php');
} else {
require('offers-overview.php');
}
?>