2015-10-13 17:44:01 +02:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Auth;
|
2016-01-18 09:30:42 +01:00
|
|
|
use View;
|
2015-10-13 17:44:01 +02:00
|
|
|
use DB;
|
2016-01-18 09:30:42 +01:00
|
|
|
use URL;
|
2015-10-13 17:44:01 +02:00
|
|
|
use Input;
|
|
|
|
use Utils;
|
2016-01-18 09:30:42 +01:00
|
|
|
use Request;
|
|
|
|
use Session;
|
2015-10-13 17:44:01 +02:00
|
|
|
use Datatable;
|
2016-01-18 09:30:42 +01:00
|
|
|
use App\Models\Gateway;
|
2015-10-13 17:44:01 +02:00
|
|
|
use App\Models\Invitation;
|
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\ActivityRepository;
|
2016-01-18 09:30:42 +01:00
|
|
|
use App\Events\InvoiceInvitationWasViewed;
|
|
|
|
use App\Events\QuoteInvitationWasViewed;
|
|
|
|
use App\Services\PaymentService;
|
2015-10-13 17:44:01 +02:00
|
|
|
|
|
|
|
class PublicClientController extends BaseController
|
|
|
|
{
|
|
|
|
private $invoiceRepo;
|
|
|
|
private $paymentRepo;
|
|
|
|
|
2016-01-18 09:30:42 +01:00
|
|
|
public function __construct(InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ActivityRepository $activityRepo, PaymentService $paymentService)
|
2015-10-13 17:44:01 +02:00
|
|
|
{
|
|
|
|
$this->invoiceRepo = $invoiceRepo;
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
2015-10-28 20:22:07 +01:00
|
|
|
$this->activityRepo = $activityRepo;
|
2016-01-18 09:30:42 +01:00
|
|
|
$this->paymentService = $paymentService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view($invitationKey)
|
|
|
|
{
|
|
|
|
if (!$invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
|
|
|
|
return response()->view('error', [
|
|
|
|
'error' => trans('texts.invoice_not_found'),
|
|
|
|
'hideHeader' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
if (!$account->checkSubdomain(Request::server('HTTP_HOST'))) {
|
|
|
|
return response()->view('error', [
|
|
|
|
'error' => trans('texts.invoice_not_found'),
|
|
|
|
'hideHeader' => true,
|
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Input::has('phantomjs') && !Session::has($invitationKey) && (!Auth::check() || Auth::user()->account_id != $invoice->account_id)) {
|
|
|
|
if ($invoice->is_quote) {
|
|
|
|
event(new QuoteInvitationWasViewed($invoice, $invitation));
|
|
|
|
} else {
|
|
|
|
event(new InvoiceInvitationWasViewed($invoice, $invitation));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::put($invitationKey, true); // track this invitation has been seen
|
|
|
|
Session::put('invitation_key', $invitationKey); // track current invitation
|
|
|
|
|
|
|
|
$account->loadLocalizationSettings($client);
|
|
|
|
|
|
|
|
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
|
|
|
|
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
|
|
|
|
$invoice->is_pro = $account->isPro();
|
|
|
|
$invoice->invoice_fonts = $account->getFontsData();
|
|
|
|
|
|
|
|
if ($invoice->invoice_design_id == CUSTOM_DESIGN) {
|
|
|
|
$invoice->invoice_design->javascript = $account->custom_design;
|
|
|
|
} else {
|
|
|
|
$invoice->invoice_design->javascript = $invoice->invoice_design->pdfmake;
|
|
|
|
}
|
|
|
|
$contact = $invitation->contact;
|
|
|
|
$contact->setVisible([
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'phone',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$paymentTypes = $this->getPaymentTypes($client, $invitation);
|
|
|
|
$paymentURL = '';
|
|
|
|
if (count($paymentTypes)) {
|
|
|
|
$paymentURL = $paymentTypes[0]['url'];
|
|
|
|
if (!$account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) {
|
|
|
|
$paymentURL = URL::to($paymentURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$showApprove = $invoice->quote_invoice_id ? false : true;
|
|
|
|
if ($invoice->due_date) {
|
|
|
|
$showApprove = time() < strtotime($invoice->due_date);
|
|
|
|
}
|
2016-01-20 09:41:57 +01:00
|
|
|
if ($invoice->invoice_status_id >= INVOICE_STATUS_APPROVED) {
|
|
|
|
$showApprove = false;
|
|
|
|
}
|
2016-01-18 09:30:42 +01:00
|
|
|
|
|
|
|
// Checkout.com requires first getting a payment token
|
|
|
|
$checkoutComToken = false;
|
|
|
|
$checkoutComKey = false;
|
|
|
|
if ($accountGateway = $account->getGatewayConfig(GATEWAY_CHECKOUT_COM)) {
|
|
|
|
if ($checkoutComToken = $this->paymentService->getCheckoutComToken($invitation)) {
|
|
|
|
$checkoutComKey = $accountGateway->getConfigField('publicApiKey');
|
|
|
|
$invitation->transaction_reference = $checkoutComToken;
|
|
|
|
$invitation->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'account' => $account,
|
|
|
|
'showApprove' => $showApprove,
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
|
|
|
'hideHeader' => $account->isNinjaAccount(),
|
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
|
|
|
'invoice' => $invoice->hidePrivateFields(),
|
|
|
|
'invitation' => $invitation,
|
|
|
|
'invoiceLabels' => $account->getInvoiceLabels(),
|
|
|
|
'contact' => $contact,
|
|
|
|
'paymentTypes' => $paymentTypes,
|
|
|
|
'paymentURL' => $paymentURL,
|
|
|
|
'checkoutComToken' => $checkoutComToken,
|
|
|
|
'checkoutComKey' => $checkoutComKey,
|
|
|
|
'phantomjs' => Input::has('phantomjs'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return View::make('invoices.view', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPaymentTypes($client, $invitation)
|
|
|
|
{
|
|
|
|
$paymentTypes = [];
|
|
|
|
$account = $client->account;
|
|
|
|
|
|
|
|
if ($client->getGatewayToken()) {
|
|
|
|
$paymentTypes[] = [
|
|
|
|
'url' => URL::to("payment/{$invitation->invitation_key}/token"), 'label' => trans('texts.use_card_on_file')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
foreach(Gateway::$paymentTypes as $type) {
|
|
|
|
if ($account->getGatewayByType($type)) {
|
|
|
|
$typeLink = strtolower(str_replace('PAYMENT_TYPE_', '', $type));
|
|
|
|
$url = URL::to("/payment/{$invitation->invitation_key}/{$typeLink}");
|
|
|
|
|
|
|
|
// PayPal doesn't allow being run in an iframe so we need to open in new tab
|
|
|
|
if ($type === PAYMENT_TYPE_PAYPAL && $account->iframe_url) {
|
|
|
|
$url = 'javascript:window.open("'.$url.'", "_blank")';
|
|
|
|
}
|
|
|
|
$paymentTypes[] = [
|
|
|
|
'url' => $url, 'label' => trans('texts.'.strtolower($type))
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $paymentTypes;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 16:50:01 +01:00
|
|
|
public function download($invitationKey)
|
|
|
|
{
|
|
|
|
if (!$invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
|
|
|
|
return response()->view('error', [
|
|
|
|
'error' => trans('texts.invoice_not_found'),
|
|
|
|
'hideHeader' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$pdfString = $invoice->getPDFString();
|
|
|
|
|
2016-02-17 20:13:55 +01:00
|
|
|
header('Content-Type: application/pdf');
|
|
|
|
header('Content-Length: ' . strlen($pdfString));
|
|
|
|
header('Content-disposition: attachment; filename="' . $invoice->getFileName() . '"');
|
|
|
|
header('Cache-Control: public, must-revalidate, max-age=0');
|
|
|
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
|
|
|
|
|
|
|
return $pdfString;
|
2016-02-17 16:50:01 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 17:44:01 +02:00
|
|
|
public function dashboard()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return $this->returnError();
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'color' => $color,
|
|
|
|
'account' => $account,
|
|
|
|
'client' => $client,
|
2015-10-14 22:56:17 +02:00
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-12-30 19:11:51 +01:00
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
2015-10-13 17:44:01 +02:00
|
|
|
];
|
2016-01-06 21:46:11 +01:00
|
|
|
|
2015-10-13 17:44:01 +02:00
|
|
|
return response()->view('invited.dashboard', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activityDatatable()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
2015-11-16 13:30:44 +01:00
|
|
|
$query = $this->activityRepo->findByClientId($invoice->client_id);
|
2015-10-28 20:22:07 +01:00
|
|
|
$query->where('activities.adjustment', '!=', 0);
|
2015-10-13 17:44:01 +02:00
|
|
|
|
|
|
|
return Datatable::query($query)
|
|
|
|
->addColumn('activities.id', function ($model) { return Utils::timestampToDateTimeString(strtotime($model->created_at)); })
|
2015-12-31 12:31:50 +01:00
|
|
|
->addColumn('activity_type_id', function ($model) {
|
2015-10-28 20:22:07 +01:00
|
|
|
$data = [
|
|
|
|
'client' => Utils::getClientDisplayName($model),
|
|
|
|
'user' => $model->is_system ? ('<i>' . trans('texts.system') . '</i>') : ($model->user_first_name . ' ' . $model->user_last_name),
|
|
|
|
'invoice' => trans('texts.invoice') . ' ' . $model->invoice,
|
|
|
|
'contact' => Utils::getClientDisplayName($model),
|
|
|
|
'payment' => trans('texts.payment') . ($model->payment ? ' ' . $model->payment : ''),
|
|
|
|
];
|
|
|
|
|
|
|
|
return trans("texts.activity_{$model->activity_type_id}", $data);
|
|
|
|
})
|
2015-12-07 14:34:55 +01:00
|
|
|
->addColumn('balance', function ($model) { return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id); })
|
2015-12-14 22:05:17 +01:00
|
|
|
->addColumn('adjustment', function ($model) { return $model->adjustment != 0 ? Utils::wrapAdjustment($model->adjustment, $model->currency_id, $model->country_id) : ''; })
|
2015-10-13 17:44:01 +02:00
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invoiceIndex()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return $this->returnError();
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'color' => $color,
|
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-12-30 19:11:51 +01:00
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
2015-10-13 17:44:01 +02:00
|
|
|
'title' => trans('texts.invoices'),
|
|
|
|
'entityType' => ENTITY_INVOICE,
|
|
|
|
'columns' => Utils::trans(['invoice_number', 'invoice_date', 'invoice_total', 'balance_due', 'due_date']),
|
|
|
|
];
|
|
|
|
|
|
|
|
return response()->view('public_list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invoiceDatatable()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
2015-12-27 12:08:58 +01:00
|
|
|
return '';
|
2015-10-20 19:12:34 +02:00
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
|
|
|
|
return $this->invoiceRepo->getClientDatatable($invitation->contact_id, ENTITY_INVOICE, Input::get('sSearch'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function paymentIndex()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return $this->returnError();
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'color' => $color,
|
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-12-30 19:11:51 +01:00
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
2015-10-13 17:44:01 +02:00
|
|
|
'entityType' => ENTITY_PAYMENT,
|
|
|
|
'title' => trans('texts.payments'),
|
|
|
|
'columns' => Utils::trans(['invoice', 'transaction_reference', 'method', 'payment_amount', 'payment_date'])
|
|
|
|
];
|
|
|
|
|
|
|
|
return response()->view('public_list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentDatatable()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$payments = $this->paymentRepo->findForContact($invitation->contact->id, Input::get('sSearch'));
|
|
|
|
|
|
|
|
return Datatable::query($payments)
|
|
|
|
->addColumn('invoice_number', function ($model) { return $model->invitation_key ? link_to('/view/'.$model->invitation_key, $model->invoice_number) : $model->invoice_number; })
|
|
|
|
->addColumn('transaction_reference', function ($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; })
|
|
|
|
->addColumn('payment_type', function ($model) { return $model->payment_type ? $model->payment_type : ($model->account_gateway_id ? '<i>Online payment</i>' : ''); })
|
2015-12-07 14:34:55 +01:00
|
|
|
->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id); })
|
2015-10-13 17:44:01 +02:00
|
|
|
->addColumn('payment_date', function ($model) { return Utils::dateToString($model->payment_date); })
|
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quoteIndex()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return $this->returnError();
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'color' => $color,
|
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-12-30 19:11:51 +01:00
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
2015-10-13 17:44:01 +02:00
|
|
|
'title' => trans('texts.quotes'),
|
|
|
|
'entityType' => ENTITY_QUOTE,
|
|
|
|
'columns' => Utils::trans(['quote_number', 'quote_date', 'quote_total', 'due_date']),
|
|
|
|
];
|
|
|
|
|
|
|
|
return response()->view('public_list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function quoteDatatable()
|
|
|
|
{
|
2015-10-20 19:12:34 +02:00
|
|
|
if (!$invitation = $this->getInvitation()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-13 17:44:01 +02:00
|
|
|
|
|
|
|
return $this->invoiceRepo->getClientDatatable($invitation->contact_id, ENTITY_QUOTE, Input::get('sSearch'));
|
|
|
|
}
|
|
|
|
|
2015-10-20 19:12:34 +02:00
|
|
|
private function returnError()
|
|
|
|
{
|
|
|
|
return response()->view('error', [
|
|
|
|
'error' => trans('texts.invoice_not_found'),
|
|
|
|
'hideHeader' => true,
|
2015-12-30 19:11:51 +01:00
|
|
|
'clientViewCSS' => $account->clientViewCSS(),
|
2016-01-07 08:08:30 +01:00
|
|
|
'clientFontUrl' => $account->getFontsUrl(),
|
2015-10-20 19:12:34 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-10-13 17:44:01 +02:00
|
|
|
private function getInvitation()
|
|
|
|
{
|
|
|
|
$invitationKey = session('invitation_key');
|
|
|
|
|
|
|
|
if (!$invitationKey) {
|
2015-10-20 19:12:34 +02:00
|
|
|
return false;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$invitation = Invitation::where('invitation_key', '=', $invitationKey)->first();
|
|
|
|
|
|
|
|
if (!$invitation || $invitation->is_deleted) {
|
2015-10-20 19:12:34 +02:00
|
|
|
return false;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
|
|
|
if (!$invoice || $invoice->is_deleted) {
|
2015-10-20 19:12:34 +02:00
|
|
|
return false;
|
2015-10-13 17:44:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $invitation;
|
|
|
|
}
|
|
|
|
|
2016-01-20 09:41:57 +01:00
|
|
|
}
|