diff --git a/app/Http/Controllers/ClientApiController.php b/app/Http/Controllers/ClientApiController.php index f7140c2943..ccb7a1c34b 100644 --- a/app/Http/Controllers/ClientApiController.php +++ b/app/Http/Controllers/ClientApiController.php @@ -24,8 +24,11 @@ class ClientApiController extends Controller public function index() { - $clients = Client::scope()->with('contacts')->orderBy('created_at', 'desc')->get(); - $clients = Utils::remapPublicIds($clients->toArray()); + $clients = Client::scope() + ->with('country', 'contacts', 'industry', 'size', 'currency') + ->orderBy('created_at', 'desc') + ->get(); + $clients = Utils::remapPublicIds($clients); $response = json_encode($clients, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(count($clients)); @@ -44,8 +47,8 @@ class ClientApiController extends Controller return Response::make($error, 500, $headers); } else { $client = $this->clientRepo->save(isset($data['id']) ? $data['id'] : false, $data, false); - $client->load('contacts'); - $client = Utils::remapPublicIds($client->toArray()); + $client = Client::scope($client->public_id)->with('country', 'contacts', 'industry', 'size', 'currency')->first(); + $client = Utils::remapPublicIds([$client]); $response = json_encode($client, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(); diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 764a599866..43911db7d8 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -26,7 +26,7 @@ class InvoiceApiController extends Controller public function index() { - $invoices = Invoice::scope()->with('invitations')->where('invoices.is_quote', '=', false)->orderBy('created_at', 'desc')->get(); + $invoices = Invoice::scope()->with('client', 'invitations.account')->where('invoices.is_quote', '=', false)->orderBy('created_at', 'desc')->get(); // Add the first invitation link to the data foreach ($invoices as $key => $invoice) { @@ -36,7 +36,7 @@ class InvoiceApiController extends Controller unset($invoice['invitations']); } - $invoices = Utils::remapPublicIds($invoices->toArray()); + $invoices = Utils::remapPublicIds($invoices); $response = json_encode($invoices, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(count($invoices)); @@ -99,7 +99,6 @@ class InvoiceApiController extends Controller $data = self::prepareData($data); $data['client_id'] = $client->id; $invoice = $this->invoiceRepo->save(false, $data, false); - $invoice->load('invoice_items'); $invitation = Invitation::createNew(); $invitation->invoice_id = $invoice->id; @@ -112,13 +111,9 @@ class InvoiceApiController extends Controller } // prepare the return data - $invoice = $invoice->toArray(); - $invoice['link'] = $invitation->getLink(); - unset($invoice['account']); - unset($invoice['client']); - $invoice = Utils::remapPublicIds($invoice); - $invoice['client_id'] = $client->public_id; - + $invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first(); + $invoice = Utils::remapPublicIds([$invoice]); + $response = json_encode($invoice, JSON_PRETTY_PRINT); } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index bdec69ccea..0a9220c727 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -236,6 +236,11 @@ class InvoiceController extends BaseController } } + $paymentURL = ''; + if (count($paymentTypes)) { + $paymentURL = $paymentTypes[0]['url']; + } + $data = array( 'isConverted' => $invoice->quote_invoice_id ? true : false, 'showBreadcrumbs' => false, @@ -244,7 +249,8 @@ class InvoiceController extends BaseController 'invitation' => $invitation, 'invoiceLabels' => $account->getInvoiceLabels(), 'contact' => $contact, - 'paymentTypes' => $paymentTypes + 'paymentTypes' => $paymentTypes, + 'paymentURL' => $paymentURL ); return View::make('invoices.view', $data); @@ -505,9 +511,13 @@ class InvoiceController extends BaseController return $this->convertQuote($publicId); } elseif ($action == 'email') { if (Auth::user()->confirmed && !Auth::user()->isDemo()) { - $message = trans("texts.emailed_{$entityType}"); - $this->mailer->sendInvoice($invoice); - Session::flash('message', $message); + $response = $this->mailer->sendInvoice($invoice); + if ($response === true) { + $message = trans("texts.emailed_{$entityType}"); + Session::flash('message', $message); + } else { + Session::flash('error', $response); + } } else { $errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required'); Session::flash('error', $errorMessage); diff --git a/app/Http/Controllers/PaymentApiController.php b/app/Http/Controllers/PaymentApiController.php index 40ba155b54..0b8b6cba01 100644 --- a/app/Http/Controllers/PaymentApiController.php +++ b/app/Http/Controllers/PaymentApiController.php @@ -16,9 +16,9 @@ class PaymentApiController extends Controller public function index() { - $payments = Payment::scope()->orderBy('created_at', 'desc')->get(); - $payments = Utils::remapPublicIds($payments->toArray()); - + $payments = Payment::scope()->with('client', 'contact', 'invitation', 'user', 'invoice')->orderBy('created_at', 'desc')->get(); + $payments = Utils::remapPublicIds($payments); + $response = json_encode($payments, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(count($payments)); diff --git a/app/Http/Controllers/QuoteApiController.php b/app/Http/Controllers/QuoteApiController.php index 2be0dc9b25..70257644c5 100644 --- a/app/Http/Controllers/QuoteApiController.php +++ b/app/Http/Controllers/QuoteApiController.php @@ -16,8 +16,8 @@ class QuoteApiController extends Controller public function index() { - $invoices = Invoice::scope()->where('invoices.is_quote', '=', true)->orderBy('created_at', 'desc')->get(); - $invoices = Utils::remapPublicIds($invoices->toArray()); + $invoices = Invoice::scope()->with('client', 'user')->where('invoices.is_quote', '=', true)->orderBy('created_at', 'desc')->get(); + $invoices = Utils::remapPublicIds($invoices); $response = json_encode($invoices, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(count($invoices)); diff --git a/app/Http/routes.php b/app/Http/routes.php index 8cf9123269..24d98c6667 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -431,7 +431,6 @@ function otrans($text) } } -/* // Log all SQL queries to laravel.log Event::listen('illuminate.query', function($query, $bindings, $time, $name) { @@ -456,7 +455,7 @@ Event::listen('illuminate.query', function($query, $bindings, $time, $name) Log::info($query, $data); }); -*/ + /* if (Auth::check() && Auth::user()->id === 1) diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index 94025347fc..55e1dced67 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -568,14 +568,15 @@ class Utils { $curl = curl_init(); - $jsonEncodedData = json_encode($data->toJson()); + $jsonEncodedData = json_encode($data->toPublicArray()); + $opts = [ - CURLOPT_URL => $subscription->target_url, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_CUSTOMREQUEST => 'POST', - CURLOPT_POST => 1, - CURLOPT_POSTFIELDS => $jsonEncodedData, - CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Content-Length: '.strlen($jsonEncodedData)], + CURLOPT_URL => $subscription->target_url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => $jsonEncodedData, + CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Content-Length: '.strlen($jsonEncodedData)], ]; curl_setopt_array($curl, $opts); @@ -591,27 +592,39 @@ class Utils } - public static function remapPublicIds(array $data) + public static function remapPublicIds($items) { $return = []; - - foreach ($data as $key => $val) { - if ($key === 'public_id') { - $key = 'id'; - } elseif (strpos($key, '_id')) { - continue; - } - - if (is_array($val)) { - $val = Utils::remapPublicIds($val); - } - - $return[$key] = $val; + + foreach ($items as $item) { + $return[] = $item->toPublicArray(); } return $return; } + public static function hideIds($data) + { + $publicId = null; + + foreach ($data as $key => $val) { + if (is_array($val)) { + $data[$key] = Utils::hideIds($val); + } else if ($key == 'id' || strpos($key, '_id')) { + if ($key == 'public_id') { + $publicId = $val; + } + unset($data[$key]); + } + } + + if ($publicId) { + $data['id'] = $publicId; + } + + return $data; + } + public static function getApiHeaders($count = 0) { return [ diff --git a/app/Models/Client.php b/app/Models/Client.php index a7a0a1b8dc..f2357742e0 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -74,7 +74,7 @@ class Client extends EntityModel public function getName() { - return $this->getDisplayName(); + return $this->name; } public function getDisplayName() @@ -82,8 +82,9 @@ class Client extends EntityModel if ($this->name) { return $this->name; } - + $this->load('contacts'); + $contact = $this->contacts()->first(); return $contact->getDisplayName(); diff --git a/app/Models/Contact.php b/app/Models/Contact.php index b789d2f370..0856a6d431 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -38,6 +38,11 @@ class Contact extends EntityModel } */ + public function getName() + { + return $this->getDisplayName(); + } + public function getDisplayName() { if ($this->getFullName()) { diff --git a/app/Models/Country.php b/app/Models/Country.php index d5143e6562..219251a44c 100644 --- a/app/Models/Country.php +++ b/app/Models/Country.php @@ -4,7 +4,12 @@ use Eloquent; class Country extends Eloquent { - public $timestamps = false; + public $timestamps = false; protected $visible = ['id', 'name']; + + public function getName() + { + return $this->name; + } } diff --git a/app/Models/Currency.php b/app/Models/Currency.php index 4a2cbc21f8..944f9f2d8e 100644 --- a/app/Models/Currency.php +++ b/app/Models/Currency.php @@ -5,4 +5,9 @@ use Eloquent; class Currency extends Eloquent { public $timestamps = false; + + public function getName() + { + return $this->name; + } } diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index 98becf4258..bf44b6f6d8 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -77,4 +77,34 @@ class EntityModel extends Eloquent return $query; } + + public function getName() + { + return $this->public_id; + } + + // Remap ids to public_ids and show name + public function toPublicArray() + { + $data = $this->toArray(); + + foreach ($this->attributes as $key => $val) { + if (strpos($key, '_id')) { + list($field, $id) = explode('_', $key); + if ($field == 'account') { + // do nothing + } else { + $entity = @$this->$field; + if ($entity) { + $data["{$field}_name"] = $entity->getName(); + } + } + } + } + + $data = Utils::hideIds($data); + + return $data; + } + } diff --git a/app/Models/Industry.php b/app/Models/Industry.php index 569bb14ddc..fe2d1fa1e2 100644 --- a/app/Models/Industry.php +++ b/app/Models/Industry.php @@ -5,4 +5,9 @@ use Eloquent; class Industry extends Eloquent { public $timestamps = false; + + public function getName() + { + return $this->name; + } } diff --git a/app/Models/Invitation.php b/app/Models/Invitation.php index b7895fda96..7941c81f47 100644 --- a/app/Models/Invitation.php +++ b/app/Models/Invitation.php @@ -29,7 +29,10 @@ class Invitation extends EntityModel public function getLink() { - $this->load('account'); + if (!$this->account) { + $this->load('account'); + } + $url = SITE_URL; if ($this->account->subdomain) { @@ -41,4 +44,9 @@ class Invitation extends EntityModel return "{$url}/view/{$this->invitation_key}"; } + + public function getName() + { + return $this->invitation_key; + } } diff --git a/app/Models/Size.php b/app/Models/Size.php index 76a9dc91a9..99d1b12f5d 100644 --- a/app/Models/Size.php +++ b/app/Models/Size.php @@ -5,4 +5,9 @@ use Eloquent; class Size extends Eloquent { public $timestamps = false; + + public function getName() + { + return $this->name; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 66d33492db..55dbac000f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -48,6 +48,11 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return $this->belongsTo('App\Models\Theme'); } + public function getName() + { + return $this->getDisplayName(); + } + public function getPersonType() { return PERSON_USER; diff --git a/app/Ninja/Mailers/ContactMailer.php b/app/Ninja/Mailers/ContactMailer.php index be1f91147f..03dfdd326f 100644 --- a/app/Ninja/Mailers/ContactMailer.php +++ b/app/Ninja/Mailers/ContactMailer.php @@ -54,7 +54,11 @@ class ContactMailer extends Mailer $data['invoice_id'] = $invoice->id; $fromEmail = $invitation->user->email; - $this->sendTo($invitation->contact->email, $fromEmail, $accountName, $subject, $view, $data); + $response = $this->sendTo($invitation->contact->email, $fromEmail, $accountName, $subject, $view, $data); + + if ($response !== true) { + return $response; + } Activity::emailInvoice($invitation); } diff --git a/app/Ninja/Mailers/Mailer.php b/app/Ninja/Mailers/Mailer.php index 9e32e4ee2f..50a9c87b2c 100644 --- a/app/Ninja/Mailers/Mailer.php +++ b/app/Ninja/Mailers/Mailer.php @@ -1,5 +1,6 @@ where('id', '=', $data['invoice_id'])->get()->first(); - if($invoice->account->pdf_email_attachment && file_exists($invoice->getPDFPath())) { - $message->attach( - $invoice->getPDFPath(), - array('as' => $invoice->getFileName(), 'mime' => 'application/pdf') - ); + try { + Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $subject, $data) { + $replyEmail = $fromEmail; + $fromEmail = NINJA_FROM_EMAIL; + + if(isset($data['invoice_id'])) { + $invoice = Invoice::with('account')->where('id', '=', $data['invoice_id'])->get()->first(); + if($invoice->account->pdf_email_attachment && file_exists($invoice->getPDFPath())) { + $message->attach( + $invoice->getPDFPath(), + array('as' => $invoice->getFileName(), 'mime' => 'application/pdf') + ); + } } - } + + $message->to($toEmail)->from($fromEmail, $fromName)->replyTo($replyEmail, $fromName)->subject($subject); + }); - //$message->setEncoder(\Swift_Encoding::get8BitEncoding()); - $message->to($toEmail)->from($fromEmail, $fromName)->replyTo($replyEmail, $fromName)->subject($subject); - }); + return true; + } catch (Exception $e) { + $response = $e->getResponse()->getBody()->getContents(); + $response = json_decode($response); + return nl2br($response->Message); + } } } diff --git a/resources/views/header.blade.php b/resources/views/header.blade.php index 80d6446420..faafd48c15 100644 --- a/resources/views/header.blade.php +++ b/resources/views/header.blade.php @@ -454,7 +454,7 @@ @endif @if (Session::has('error')) -
{{ Session::get('error') }}
+
{!! Session::get('error') !!}
@endif @if (!isset($showBreadcrumbs) || $showBreadcrumbs) diff --git a/resources/views/invoices/view.blade.php b/resources/views/invoices/view.blade.php index 0bdb791af1..c2694273c1 100644 --- a/resources/views/invoices/view.blade.php +++ b/resources/views/invoices/view.blade.php @@ -36,7 +36,7 @@ @if (count($paymentTypes) > 1) {!! DropdownButton::success(trans('texts.pay_now'))->withContents($paymentTypes)->large() !!} @else - {!! Button::success(trans('texts.pay_now'))->asLinkTo(URL::to('/payment/' . $invitation->invitation_key))->large() !!} + {!! Button::success(trans('texts.pay_now'))->asLinkTo(URL::to($paymentURL))->large() !!} @endif @else {!! Button::normal('Download PDF')->withAttributes(['onclick' => 'onDownloadClick()'])->large() !!}