1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Controllers/ClientPortalController.php

889 lines
31 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-10-13 17:44:01 +02:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Events\InvoiceInvitationWasViewed;
use App\Events\QuoteInvitationWasViewed;
use App\Models\Contact;
use App\Models\Document;
use App\Models\Gateway;
2015-10-13 17:44:01 +02:00
use App\Models\Invitation;
2016-05-15 22:27:56 +02:00
use App\Models\PaymentMethod;
2015-10-28 20:22:07 +01:00
use App\Ninja\Repositories\ActivityRepository;
2016-09-23 11:02:48 +02:00
use App\Ninja\Repositories\CreditRepository;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\DocumentRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository;
use App\Services\PaymentService;
2017-01-30 20:40:43 +01:00
use Auth;
use Barracuda\ArchiveStream\ZipArchive;
2017-01-30 20:40:43 +01:00
use Cache;
use Datatable;
use Exception;
use Input;
use Redirect;
use Request;
use Response;
use Session;
use URL;
use Utils;
use Validator;
use View;
2015-10-13 17:44:01 +02:00
class ClientPortalController extends BaseController
2015-10-13 17:44:01 +02:00
{
private $invoiceRepo;
private $paymentRepo;
2016-03-23 23:40:42 +01:00
private $documentRepo;
2015-10-13 17:44:01 +02:00
2016-09-23 11:02:48 +02:00
public function __construct(InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ActivityRepository $activityRepo, DocumentRepository $documentRepo, PaymentService $paymentService, CreditRepository $creditRepo)
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-03-24 16:58:30 +01:00
$this->documentRepo = $documentRepo;
$this->paymentService = $paymentService;
2016-09-23 11:02:48 +02:00
$this->creditRepo = $creditRepo;
}
public function view($invitationKey)
{
2017-02-17 08:48:44 +01:00
if (request()->silent) {
session(['silent' => true]);
return redirect(request()->url());
}
2017-01-30 20:40:43 +01:00
if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
return $this->returnError();
}
$invoice = $invitation->invoice;
$client = $invoice->client;
$account = $invoice->account;
2017-01-30 20:40:43 +01:00
if (! $account->checkSubdomain(Request::server('HTTP_HOST'))) {
return response()->view('error', [
'error' => trans('texts.invoice_not_found'),
]);
}
2016-12-20 09:50:23 +01:00
$account->loadLocalizationSettings($client);
if (! Input::has('phantomjs') && ! session('silent') && ! Session::has($invitation->invitation_key)
2017-01-30 20:40:43 +01:00
&& (! Auth::check() || Auth::user()->account_id != $invoice->account_id)) {
2016-05-26 16:56:54 +02:00
if ($invoice->isType(INVOICE_TYPE_QUOTE)) {
event(new QuoteInvitationWasViewed($invoice, $invitation));
} else {
event(new InvoiceInvitationWasViewed($invoice, $invitation));
}
}
Session::put($invitation->invitation_key, true); // track this invitation has been seen
2017-01-30 20:40:43 +01:00
Session::put('contact_key', $invitation->contact->contact_key); // track current contact
$invoice->invoice_date = Utils::fromSqlDate($invoice->invoice_date);
$invoice->due_date = Utils::fromSqlDate($invoice->due_date);
$invoice->features = [
'customize_invoice_design' => $account->hasFeature(FEATURE_CUSTOMIZE_INVOICE_DESIGN),
'remove_created_by' => $account->hasFeature(FEATURE_REMOVE_CREATED_BY),
'invoice_settings' => $account->hasFeature(FEATURE_INVOICE_SETTINGS),
];
$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',
]);
2017-02-17 15:28:42 +01:00
// translate the country names
2016-12-20 14:09:41 +01:00
if ($invoice->client->country) {
$invoice->client->country->name = trans('texts.country_' . $invoice->client->country->name);
}
2017-02-17 15:28:42 +01:00
if ($invoice->account->country) {
$invoice->account->country->name = trans('texts.country_' . $invoice->account->country->name);
}
2016-12-20 14:09:41 +01:00
$data = [];
2016-06-20 16:14:43 +02:00
$paymentTypes = $this->getPaymentTypes($account, $client, $invitation);
$paymentURL = '';
2016-04-29 23:50:21 +02:00
if (count($paymentTypes) == 1) {
$paymentURL = $paymentTypes[0]['url'];
2016-09-26 11:33:30 +02:00
if ($paymentTypes[0]['gatewayTypeId'] == GATEWAY_TYPE_CUSTOM) {
// do nothing
2017-01-30 20:40:43 +01:00
} elseif (! $account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) {
$paymentURL = URL::to($paymentURL);
}
}
2017-01-30 17:05:31 +01:00
if ($wepayGateway = $account->getGatewayConfig(GATEWAY_WEPAY)) {
$data['enableWePayACH'] = $wepayGateway->getAchEnabled();
2016-05-07 04:33:03 +02:00
}
$showApprove = $invoice->quote_invoice_id ? false : true;
if ($invoice->due_date) {
$showApprove = time() < strtotime($invoice->due_date);
}
if ($invoice->invoice_status_id >= INVOICE_STATUS_APPROVED) {
$showApprove = false;
}
$data += [
'account' => $account,
'showApprove' => $showApprove,
'showBreadcrumbs' => false,
'invoice' => $invoice->hidePrivateFields(),
'invitation' => $invitation,
'invoiceLabels' => $account->getInvoiceLabels(),
'contact' => $contact,
'paymentTypes' => $paymentTypes,
'paymentURL' => $paymentURL,
'phantomjs' => Input::has('phantomjs'),
];
2016-06-21 11:40:10 +02:00
if ($paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_CREDIT_CARD)) {
$data += [
'transactionToken' => $paymentDriver->createTransactionToken(),
'partialView' => $paymentDriver->partialView(),
2016-06-22 11:22:38 +02:00
'accountGateway' => $paymentDriver->accountGateway,
2016-06-21 11:40:10 +02:00
];
}
2016-09-26 11:33:30 +02:00
if ($accountGateway = $account->getGatewayByType(GATEWAY_TYPE_CUSTOM)) {
$data += [
'customGatewayName' => $accountGateway->getConfigField('name'),
'customGatewayText' => $accountGateway->getConfigField('text'),
];
}
2016-06-21 11:40:10 +02:00
2017-01-30 17:05:31 +01:00
if ($account->hasFeature(FEATURE_DOCUMENTS) && $this->canCreateZip()) {
$zipDocs = $this->getInvoiceZipDocuments($invoice, $size);
2017-01-30 17:05:31 +01:00
if (count($zipDocs) > 1) {
$data['documentsZipURL'] = URL::to("client/documents/{$invitation->invitation_key}");
$data['documentsZipSize'] = $size;
}
}
return View::make('invoices.view', $data);
}
2016-06-20 16:14:43 +02:00
private function getPaymentTypes($account, $client, $invitation)
{
2016-06-20 16:14:43 +02:00
$links = [];
2016-06-20 16:14:43 +02:00
foreach ($account->account_gateways as $accountGateway) {
$paymentDriver = $accountGateway->paymentDriver($invitation);
$links = array_merge($links, $paymentDriver->tokenLinks());
$links = array_merge($links, $paymentDriver->paymentLinks());
}
2016-04-29 23:50:21 +02:00
2016-06-20 16:14:43 +02:00
return $links;
2015-10-13 17:44:01 +02:00
}
2016-02-17 16:50:01 +01:00
public function download($invitationKey)
{
2017-01-30 20:40:43 +01:00
if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
2016-02-17 16:50:01 +01:00
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
}
2016-11-04 14:34:15 +01:00
public function sign($invitationKey)
{
2017-01-30 20:40:43 +01:00
if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
2016-11-04 14:34:15 +01:00
return RESULT_FAILURE;
}
if ($signature = Input::get('signature')) {
$invitation->signature_base64 = $signature;
$invitation->signature_date = date_create();
$invitation->save();
}
session(['authorized:' . $invitation->invitation_key => true]);
2016-11-04 14:34:15 +01:00
return RESULT_SUCCESS;
}
public function dashboard($contactKey = false)
2015-10-13 17:44:01 +02:00
{
if (request()->silent) {
session(['silent' => true]);
return redirect(request()->url());
}
if ($contactKey) {
2017-01-30 20:40:43 +01:00
if (! $contact = Contact::where('contact_key', '=', $contactKey)->first()) {
return $this->returnError();
}
2017-01-30 20:40:43 +01:00
Session::put('contact_key', $contactKey); // track current contact
} elseif (! $contact = $this->getContact()) {
2015-10-20 19:12:34 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
$account = $client->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($client);
2015-10-13 17:44:01 +02:00
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
$customer = false;
2015-10-13 17:44:01 +02:00
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
2017-01-30 20:40:43 +01:00
} elseif (! $account->enable_client_portal_dashboard) {
return redirect()->to('/client/invoices/');
}
if ($paymentDriver = $account->paymentDriver(false, GATEWAY_TYPE_TOKEN)) {
$customer = $paymentDriver->customer($client->id);
}
2016-06-20 16:14:43 +02:00
2015-10-13 17:44:01 +02:00
$data = [
'color' => $color,
2016-05-25 21:04:58 +02:00
'contact' => $contact,
2015-10-13 17:44:01 +02:00
'account' => $account,
'client' => $client,
'gateway' => $account->getTokenGateway(),
2016-06-20 16:14:43 +02:00
'paymentMethods' => $customer ? $customer->payment_methods : false,
'transactionToken' => $paymentDriver ? $paymentDriver->createTransactionToken() : false,
2015-10-13 17:44:01 +02:00
];
2016-05-07 04:33:03 +02:00
2015-10-13 17:44:01 +02:00
return response()->view('invited.dashboard', $data);
}
public function activityDatatable()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-05-24 23:02:28 +02:00
return $this->returnError();
2015-10-20 19:12:34 +02:00
}
2015-10-13 17:44:01 +02:00
2016-05-24 23:02:28 +02:00
$client = $contact->client;
$query = $this->activityRepo->findByClientId($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)
2017-01-30 17:05:31 +01:00
->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->account_name),
'invoice' => $model->invoice,
2015-10-28 20:22:07 +01:00
'contact' => Utils::getClientDisplayName($model),
'payment' => $model->payment ? ' ' . $model->payment : '',
2016-04-23 22:40:19 +02:00
'credit' => $model->payment_amount ? Utils::formatMoney($model->credit, $model->currency_id, $model->country_id) : '',
'payment_amount' => $model->payment_amount ? Utils::formatMoney($model->payment_amount, $model->currency_id, $model->country_id) : null,
'adjustment' => $model->adjustment ? Utils::formatMoney($model->adjustment, $model->currency_id, $model->country_id) : null,
2015-10-28 20:22:07 +01:00
];
return trans("texts.activity_{$model->activity_type_id}", $data);
2017-01-30 17:05:31 +01:00
})
->addColumn('balance', function ($model) {
return Utils::formatMoney($model->balance, $model->currency_id, $model->country_id);
})
->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 recurringInvoiceIndex()
2015-10-13 17:44:01 +02:00
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2015-10-20 19:12:34 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
}
2015-10-13 17:44:01 +02:00
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
2015-10-13 17:44:01 +02:00
$data = [
'color' => $color,
2016-04-29 23:50:21 +02:00
'account' => $account,
2016-05-24 23:02:28 +02:00
'client' => $contact->client,
'title' => trans('texts.recurring_invoices'),
'entityType' => ENTITY_RECURRING_INVOICE,
'columns' => Utils::trans(['frequency', 'start_date', 'end_date', 'invoice_total', 'auto_bill']),
];
return response()->view('public_list', $data);
}
public function invoiceIndex()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
}
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
$data = [
'color' => $color,
'account' => $account,
2016-05-24 23:02:28 +02:00
'client' => $contact->client,
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()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
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
2016-05-24 23:02:28 +02:00
return $this->invoiceRepo->getClientDatatable($contact->id, ENTITY_INVOICE, Input::get('sSearch'));
2015-10-13 17:44:01 +02:00
}
public function recurringInvoiceDatatable()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
return '';
}
2016-05-24 23:02:28 +02:00
return $this->invoiceRepo->getClientRecurringDatatable($contact->id);
}
2015-10-13 17:44:01 +02:00
public function paymentIndex()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2015-10-20 19:12:34 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
}
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
2016-10-30 19:12:02 +01:00
2015-10-13 17:44:01 +02:00
$data = [
'color' => $color,
2016-04-29 23:50:21 +02:00
'account' => $account,
2015-10-13 17:44:01 +02:00
'entityType' => ENTITY_PAYMENT,
'title' => trans('texts.payments'),
2017-01-30 20:40:43 +01:00
'columns' => Utils::trans(['invoice', 'transaction_reference', 'method', 'payment_amount', 'payment_date', 'status']),
2015-10-13 17:44:01 +02:00
];
return response()->view('public_list', $data);
}
public function paymentDatatable()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-05-24 23:02:28 +02:00
return $this->returnError();
2015-10-20 19:12:34 +02:00
}
2016-05-24 23:02:28 +02:00
$payments = $this->paymentRepo->findForContact($contact->id, Input::get('sSearch'));
2015-10-13 17:44:01 +02:00
return Datatable::query($payments)
2017-01-30 17:05:31 +01:00
->addColumn('invoice_number', function ($model) {
return $model->invitation_key ? link_to('/view/'.$model->invitation_key, $model->invoice_number)->toHtml() : $model->invoice_number;
})
->addColumn('transaction_reference', function ($model) {
return $model->transaction_reference ? $model->transaction_reference : '<i>'.trans('texts.manual_entry').'</i>';
})
->addColumn('payment_type', function ($model) {
2017-01-30 20:40:43 +01:00
return ($model->payment_type && ! $model->last4) ? $model->payment_type : ($model->account_gateway_id ? '<i>Online payment</i>' : '');
2017-01-30 17:05:31 +01:00
})
->addColumn('amount', function ($model) {
return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id);
})
->addColumn('payment_date', function ($model) {
return Utils::dateToString($model->payment_date);
})
->addColumn('status', function ($model) {
return $this->getPaymentStatusLabel($model);
})
->orderColumns('invoice_number', 'transaction_reference', 'payment_type', 'amount', 'payment_date')
2015-10-13 17:44:01 +02:00
->make();
}
2016-04-23 22:40:19 +02:00
private function getPaymentStatusLabel($model)
{
$label = trans('texts.status_' . strtolower($model->payment_status_name));
2016-04-23 22:40:19 +02:00
$class = 'default';
switch ($model->payment_status_id) {
case PAYMENT_STATUS_PENDING:
$class = 'info';
break;
case PAYMENT_STATUS_COMPLETED:
$class = 'success';
break;
case PAYMENT_STATUS_FAILED:
$class = 'danger';
break;
case PAYMENT_STATUS_PARTIALLY_REFUNDED:
$label = trans('texts.status_partially_refunded_amount', [
'amount' => Utils::formatMoney($model->refunded, $model->currency_id, $model->country_id),
]);
$class = 'primary';
break;
case PAYMENT_STATUS_REFUNDED:
$class = 'default';
break;
}
2017-01-30 20:40:43 +01:00
2016-04-23 22:40:19 +02:00
return "<h4><div class=\"label label-{$class}\">$label</div></h4>";
}
2015-10-13 17:44:01 +02:00
public function quoteIndex()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2015-10-20 19:12:34 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
}
2015-10-13 17:44:01 +02:00
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
2016-10-30 19:12:02 +01:00
2015-10-13 17:44:01 +02:00
$data = [
'color' => $color,
2016-04-29 23:50:21 +02:00
'account' => $account,
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()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2015-10-20 19:12:34 +02:00
return false;
}
2015-10-13 17:44:01 +02:00
2016-05-24 23:02:28 +02:00
return $this->invoiceRepo->getClientDatatable($contact->id, ENTITY_QUOTE, Input::get('sSearch'));
2015-10-13 17:44:01 +02:00
}
2016-09-23 11:02:48 +02:00
public function creditIndex()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-09-23 11:02:48 +02:00
return $this->returnError();
}
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2016-09-23 11:02:48 +02:00
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
2016-09-23 11:02:48 +02:00
return $this->returnError();
}
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
2016-10-30 19:12:02 +01:00
2016-09-23 11:02:48 +02:00
$data = [
'color' => $color,
'account' => $account,
'title' => trans('texts.credits'),
'entityType' => ENTITY_CREDIT,
'columns' => Utils::trans(['credit_date', 'credit_amount', 'credit_balance']),
];
return response()->view('public_list', $data);
}
public function creditDatatable()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-09-23 11:02:48 +02:00
return false;
}
return $this->creditRepo->getClientDatatable($contact->client_id);
}
2016-03-24 16:58:30 +01:00
public function documentIndex()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-03-24 16:58:30 +01:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$account = $contact->account;
2016-10-30 19:12:02 +01:00
$account->loadLocalizationSettings($contact->client);
2017-01-30 20:40:43 +01:00
if (! $account->enable_client_portal) {
return $this->returnError();
}
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
2016-10-30 19:12:02 +01:00
2016-03-24 16:58:30 +01:00
$data = [
'color' => $color,
2016-04-29 23:50:21 +02:00
'account' => $account,
2016-03-24 16:58:30 +01:00
'title' => trans('texts.documents'),
'entityType' => ENTITY_DOCUMENT,
'columns' => Utils::trans(['invoice_number', 'name', 'document_date', 'document_size']),
];
return response()->view('public_list', $data);
}
public function documentDatatable()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-03-24 16:58:30 +01:00
return false;
}
2016-05-24 23:02:28 +02:00
return $this->documentRepo->getClientDatatable($contact->id, ENTITY_DOCUMENT, Input::get('sSearch'));
2016-03-24 16:58:30 +01:00
}
private function returnError($error = false)
2015-10-20 19:12:34 +02:00
{
return response()->view('error', [
'error' => $error ?: trans('texts.invoice_not_found'),
2015-10-20 19:12:34 +02:00
'hideHeader' => true,
'account' => $this->getContact()->account,
2015-10-20 19:12:34 +02:00
]);
}
2017-01-30 17:05:31 +01:00
private function getContact()
{
2016-05-24 23:02:28 +02:00
$contactKey = session('contact_key');
2015-10-13 17:44:01 +02:00
2017-01-30 20:40:43 +01:00
if (! $contactKey) {
2015-10-20 19:12:34 +02:00
return false;
2015-10-13 17:44:01 +02:00
}
2016-05-24 23:02:28 +02:00
$contact = Contact::where('contact_key', '=', $contactKey)->first();
2015-10-13 17:44:01 +02:00
2017-01-30 20:40:43 +01:00
if (! $contact || $contact->is_deleted) {
2015-10-20 19:12:34 +02:00
return false;
2015-10-13 17:44:01 +02:00
}
2016-05-24 23:02:28 +02:00
return $contact;
2015-10-13 17:44:01 +02:00
}
2017-01-30 17:05:31 +01:00
public function getDocumentVFSJS($publicId, $name)
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-03-23 23:40:42 +01:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$document = Document::scope($publicId, $contact->account_id)->first();
2017-01-30 20:40:43 +01:00
if (! $document->isPDFEmbeddable()) {
return Response::view('error', ['error' => 'Image does not exist!'], 404);
2016-03-23 23:40:42 +01:00
}
2016-03-23 23:40:42 +01:00
$authorized = false;
2017-01-30 17:05:31 +01:00
if ($document->expense && $document->expense->client_id == $contact->client_id) {
2016-03-23 23:40:42 +01:00
$authorized = true;
2017-01-30 20:40:43 +01:00
} elseif ($document->invoice && $document->invoice->client_id == $contact->client_id) {
2016-03-23 23:40:42 +01:00
$authorized = true;
}
2017-01-30 20:40:43 +01:00
if (! $authorized) {
return Response::view('error', ['error' => 'Not authorized'], 403);
}
2017-01-30 20:40:43 +01:00
if (substr($name, -3) == '.js') {
2016-03-23 23:40:42 +01:00
$name = substr($name, 0, -3);
}
2017-01-30 20:40:43 +01:00
$content = $document->preview ? $document->getRawPreview() : $document->getRaw();
2016-03-23 23:40:42 +01:00
$content = 'ninjaAddVFSDoc('.json_encode(intval($publicId).'/'.strval($name)).',"'.base64_encode($content).'")';
$response = Response::make($content, 200);
$response->header('content-type', 'text/javascript');
$response->header('cache-control', 'max-age=31536000');
2016-03-23 23:40:42 +01:00
return $response;
}
2017-01-30 17:05:31 +01:00
protected function canCreateZip()
{
return function_exists('gmp_init');
}
2017-01-30 20:40:43 +01:00
protected function getInvoiceZipDocuments($invoice, &$size = 0)
2017-01-30 17:05:31 +01:00
{
$documents = $invoice->documents;
2017-01-30 17:05:31 +01:00
foreach ($invoice->expenses as $expense) {
$documents = $documents->merge($expense->documents);
}
$documents = $documents->sortBy('size');
$size = 0;
$maxSize = MAX_ZIP_DOCUMENTS_SIZE * 1000;
$toZip = [];
2017-01-30 17:05:31 +01:00
foreach ($documents as $document) {
if ($size + $document->size > $maxSize) {
break;
}
2017-01-30 20:40:43 +01:00
if (! empty($toZip[$document->name])) {
// This name is taken
2017-01-30 17:05:31 +01:00
if ($toZip[$document->name]->hash != $document->hash) {
// 2 different files with the same name
$nameInfo = pathinfo($document->name);
2017-01-30 20:40:43 +01:00
for ($i = 1; ; $i++) {
$name = $nameInfo['filename'].' ('.$i.').'.$nameInfo['extension'];
2017-01-30 17:05:31 +01:00
if (empty($toZip[$name])) {
$toZip[$name] = $document;
$size += $document->size;
break;
2017-01-30 17:05:31 +01:00
} elseif ($toZip[$name]->hash == $document->hash) {
// We're not adding this after all
break;
}
}
}
2017-01-30 17:05:31 +01:00
} else {
$toZip[$document->name] = $document;
$size += $document->size;
}
}
return $toZip;
}
2017-01-30 17:05:31 +01:00
public function getInvoiceDocumentsZip($invitationKey)
{
2017-01-30 20:40:43 +01:00
if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
return $this->returnError();
}
2017-01-30 20:40:43 +01:00
Session::put('contact_key', $invitation->contact->contact_key); // track current contact
$invoice = $invitation->invoice;
$toZip = $this->getInvoiceZipDocuments($invoice);
2017-01-30 20:40:43 +01:00
if (! count($toZip)) {
return Response::view('error', ['error' => 'No documents small enough'], 404);
}
$zip = new ZipArchive($invitation->account->name.' Invoice '.$invoice->invoice_number.'.zip');
2017-01-30 20:40:43 +01:00
2017-01-30 17:05:31 +01:00
return Response::stream(function () use ($toZip, $zip) {
2017-01-30 20:40:43 +01:00
foreach ($toZip as $name => $document) {
$fileStream = $document->getStream();
2017-01-30 17:05:31 +01:00
if ($fileStream) {
2017-01-30 20:40:43 +01:00
$zip->init_file_stream_transfer($name, $document->size, ['time' => $document->created_at->timestamp]);
2017-01-30 17:05:31 +01:00
while ($buffer = fread($fileStream, 256000)) {
$zip->stream_file_part($buffer);
}
fclose($fileStream);
$zip->complete_file_stream();
2017-01-30 17:05:31 +01:00
} else {
$zip->add_file($name, $document->getRaw());
}
}
$zip->finish();
}, 200);
}
2017-01-30 17:05:31 +01:00
public function getDocument($invitationKey, $publicId)
{
2017-01-30 20:40:43 +01:00
if (! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
2016-03-24 02:25:33 +01:00
return $this->returnError();
}
2017-01-30 20:40:43 +01:00
Session::put('contact_key', $invitation->contact->contact_key); // track current contact
2016-03-24 02:25:33 +01:00
$clientId = $invitation->invoice->client_id;
$document = Document::scope($publicId, $invitation->account_id)->firstOrFail();
2016-03-24 02:25:33 +01:00
$authorized = false;
2017-01-30 17:05:31 +01:00
if ($document->expense && $document->expense->client_id == $invitation->invoice->client_id) {
2016-03-24 02:25:33 +01:00
$authorized = true;
2017-01-30 17:05:31 +01:00
} elseif ($document->invoice && $document->invoice->client_id == $invitation->invoice->client_id) {
2016-03-24 02:25:33 +01:00
$authorized = true;
}
2017-01-30 20:40:43 +01:00
if (! $authorized) {
return Response::view('error', ['error' => 'Not authorized'], 403);
}
2016-03-24 02:25:33 +01:00
return DocumentController::getDownloadResponse($document);
}
2015-10-13 17:44:01 +02:00
2016-04-29 23:50:21 +02:00
public function paymentMethods()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-04-29 23:50:21 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
2016-04-29 23:50:21 +02:00
$account = $client->account;
2016-06-20 16:14:43 +02:00
$paymentDriver = $account->paymentDriver(false, GATEWAY_TYPE_TOKEN);
$customer = $paymentDriver->customer($client->id);
2016-04-29 23:50:21 +02:00
$data = [
2016-04-29 23:50:21 +02:00
'account' => $account,
2016-05-25 21:04:58 +02:00
'contact' => $contact,
2016-04-29 23:50:21 +02:00
'color' => $account->primary_color ? $account->primary_color : '#0b4d78',
'client' => $client,
2016-06-20 16:14:43 +02:00
'paymentMethods' => $customer ? $customer->payment_methods : false,
2016-04-29 23:50:21 +02:00
'gateway' => $account->getTokenGateway(),
2016-06-20 16:14:43 +02:00
'title' => trans('texts.payment_methods'),
'transactionToken' => $paymentDriver->createTransactionToken(),
];
2016-04-29 23:50:21 +02:00
return response()->view('payments.paymentmethods', $data);
}
public function verifyPaymentMethod()
{
2016-05-11 00:46:32 +02:00
$publicId = Input::get('source_id');
2016-04-29 23:50:21 +02:00
$amount1 = Input::get('verification1');
$amount2 = Input::get('verification2');
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-04-29 23:50:21 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
2016-06-20 16:14:43 +02:00
$account = $client->account;
$paymentDriver = $account->paymentDriver(null, GATEWAY_TYPE_BANK_TRANSFER);
$result = $paymentDriver->verifyBankAccount($client, $publicId, $amount1, $amount2);
2016-04-29 23:50:21 +02:00
if (is_string($result)) {
Session::flash('error', $result);
} else {
Session::flash('message', trans('texts.payment_method_verified'));
}
2017-01-30 20:40:43 +01:00
return redirect()->to($account->enable_client_portal_dashboard ? '/client/dashboard' : '/client/payment_methods/');
2016-04-29 23:50:21 +02:00
}
2016-05-11 00:46:32 +02:00
public function removePaymentMethod($publicId)
2016-04-29 23:50:21 +02:00
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-04-29 23:50:21 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
2016-06-20 16:14:43 +02:00
$account = $contact->account;
2016-05-25 21:04:58 +02:00
2016-06-20 16:14:43 +02:00
$paymentDriver = $account->paymentDriver(false, GATEWAY_TYPE_TOKEN);
$paymentMethod = PaymentMethod::clientId($client->id)
->wherePublicId($publicId)
->firstOrFail();
2016-05-06 17:21:06 +02:00
2016-06-20 16:14:43 +02:00
try {
$paymentDriver->removePaymentMethod($paymentMethod);
Session::flash('message', trans('texts.payment_method_removed'));
} catch (Exception $exception) {
Session::flash('error', $exception->getMessage());
2016-04-29 23:50:21 +02:00
}
2017-01-30 20:40:43 +01:00
return redirect()->to($client->account->enable_client_portal_dashboard ? '/client/dashboard' : '/client/payment_methods/');
2016-04-29 23:50:21 +02:00
}
2016-06-21 11:40:10 +02:00
2017-01-30 17:05:31 +01:00
public function setDefaultPaymentMethod()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
2016-04-29 23:50:21 +02:00
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
2016-06-20 16:14:43 +02:00
$account = $client->account;
2016-05-24 23:02:28 +02:00
$validator = Validator::make(Input::all(), ['source' => 'required']);
2016-04-29 23:50:21 +02:00
if ($validator->fails()) {
2017-01-30 20:40:43 +01:00
return Redirect::to($client->account->enable_client_portal_dashboard ? '/client/dashboard' : '/client/payment_methods/');
2016-04-29 23:50:21 +02:00
}
2016-06-20 16:14:43 +02:00
$paymentDriver = $account->paymentDriver(false, GATEWAY_TYPE_TOKEN);
$paymentMethod = PaymentMethod::clientId($client->id)
->wherePublicId(Input::get('source'))
->firstOrFail();
2016-04-29 23:50:21 +02:00
2016-06-20 16:14:43 +02:00
$customer = $paymentDriver->customer($client->id);
$customer->default_payment_method_id = $paymentMethod->id;
$customer->save();
Session::flash('message', trans('texts.payment_method_set_as_default'));
2016-04-29 23:50:21 +02:00
2017-01-30 20:40:43 +01:00
return redirect()->to($client->account->enable_client_portal_dashboard ? '/client/dashboard' : '/client/payment_methods/');
2016-04-29 23:50:21 +02:00
}
2016-04-30 23:54:56 +02:00
private function paymentMethodError($type, $error, $accountGateway = false, $exception = false)
{
$message = '';
if ($accountGateway && $accountGateway->gateway) {
$message = $accountGateway->gateway->name . ': ';
}
$message .= $error ?: trans('texts.payment_method_error');
Session::flash('error', $message);
Utils::logError("Payment Method Error [{$type}]: " . ($exception ? Utils::getErrorString($exception) : $message), 'PHP', true);
}
2017-01-30 17:05:31 +01:00
public function setAutoBill()
{
2017-01-30 20:40:43 +01:00
if (! $contact = $this->getContact()) {
return $this->returnError();
}
2016-05-24 23:02:28 +02:00
$client = $contact->client;
$validator = Validator::make(Input::all(), ['public_id' => 'required']);
if ($validator->fails()) {
return Redirect::to('client/invoices/recurring');
}
$publicId = Input::get('public_id');
$enable = Input::get('enable');
2016-06-30 19:53:06 +02:00
$invoice = $client->invoices()->where('public_id', intval($publicId))->first();
2016-05-09 22:29:02 +02:00
if ($invoice && $invoice->is_recurring && ($invoice->auto_bill == AUTO_BILL_OPT_IN || $invoice->auto_bill == AUTO_BILL_OPT_OUT)) {
$invoice->client_enable_auto_bill = $enable ? true : false;
$invoice->save();
}
return Redirect::to('client/invoices/recurring');
}
}