diff --git a/app/Http/Controllers/BaseAPIController.php b/app/Http/Controllers/BaseAPIController.php index f7ebf9b20d7e..14f8dc79a986 100644 --- a/app/Http/Controllers/BaseAPIController.php +++ b/app/Http/Controllers/BaseAPIController.php @@ -92,13 +92,19 @@ class BaseAPIController extends Controller protected function response($response) { $index = Request::get('index') ?: 'data'; - $meta = isset($response['meta']) ? $response['meta'] : null; - $response = [ - $index => $response - ]; - if ($meta) { - $response['meta'] = $meta; - unset($response[$index]['meta']); + + if ($index == 'none') { + unset($response['meta']); + } else { + $meta = isset($response['meta']) ? $response['meta'] : null; + $response = [ + $index => $response + ]; + + if ($meta) { + $response['meta'] = $meta; + unset($response[$index]['meta']); + } } $response = json_encode($response, JSON_PRETTY_PRINT); diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 3f7af9721876..f6a5b9b60b3d 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -12,6 +12,7 @@ use App\Models\Contact; use App\Models\Product; use App\Models\Invitation; use App\Ninja\Repositories\ClientRepository; +use App\Ninja\Repositories\PaymentRepository; use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Mailers\ContactMailer as Mailer; use App\Http\Controllers\BaseAPIController; @@ -23,12 +24,13 @@ class InvoiceApiController extends BaseAPIController { protected $invoiceRepo; - public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, Mailer $mailer) + public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, PaymentRepository $paymentRepo, Mailer $mailer) { parent::__construct(); $this->invoiceRepo = $invoiceRepo; $this->clientRepo = $clientRepo; + $this->paymentRepo = $paymentRepo; $this->mailer = $mailer; } @@ -123,14 +125,26 @@ class InvoiceApiController extends BaseAPIController } $clientData = ['contact' => ['email' => $email]]; - foreach (['name', 'private_notes'] as $field) { + foreach ([ + 'name', + 'address1', + 'address2', + 'city', + 'state', + 'postal_code', + 'private_notes', + ] as $field) { if (isset($data[$field])) { $clientData[$field] = $data[$field]; } } - foreach (['first_name', 'last_name'] as $field) { + foreach ([ + 'first_name', + 'last_name', + 'phone', + ] as $field) { if (isset($data[$field])) { - $clientData[$field] = $data[$field]; + $clientData['contact'][$field] = $data[$field]; } } @@ -143,6 +157,16 @@ class InvoiceApiController extends BaseAPIController $data = self::prepareData($data, $client); $data['client_id'] = $client->id; $invoice = $this->invoiceRepo->save($data); + $payment = false; + + // Optionally create payment with invoice + if (isset($data['paid']) && $data['paid']) { + $payment = $this->paymentRepo->save([ + 'invoice_id' => $invoice->id, + 'client_id' => $client->id, + 'amount' => $data['paid'] + ]); + } if (!isset($data['id'])) { $invitation = Invitation::createNew(); @@ -153,7 +177,11 @@ class InvoiceApiController extends BaseAPIController } if (isset($data['email_invoice']) && $data['email_invoice']) { - $this->mailer->sendInvoice($invoice); + if ($payment) { + $this->mailer->sendPaymentConfirmation($payment); + } else { + $this->mailer->sendInvoice($invoice); + } } $invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first(); diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index 3515dc1c83c6..53a586f804be 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -686,7 +686,7 @@ class Utils return EVENT_CREATE_PAYMENT; } elseif ($eventName == 'create_vendor') { return EVENT_CREATE_VENDOR; - }else { + } else { return false; } } @@ -694,7 +694,7 @@ class Utils public static function notifyZapier($subscription, $data) { $curl = curl_init(); - $jsonEncodedData = json_encode($data->toPublicArray()); + $jsonEncodedData = json_encode($data); $opts = [ CURLOPT_URL => $subscription->target_url, @@ -717,37 +717,6 @@ class Utils } } - public static function hideIds($data, $mapped = false) - { - $publicId = null; - - if (!$mapped) { - $mapped = []; - } - - foreach ($data as $key => $val) { - if (is_array($val)) { - if ($key == 'account' || isset($mapped[$key])) { - // do nothing - } else { - $mapped[$key] = true; - $data[$key] = Utils::hideIds($val, $mapped); - } - } elseif ($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/Listeners/SubscriptionListener.php b/app/Listeners/SubscriptionListener.php index fab5a2c57493..2d12fcaeb953 100644 --- a/app/Listeners/SubscriptionListener.php +++ b/app/Listeners/SubscriptionListener.php @@ -12,50 +12,68 @@ use App\Events\PaymentWasCreated; use App\Events\VendorWasCreated; use App\Events\ExpenseWasCreated; +use App\Ninja\Transformers\InvoiceTransformer; +use App\Ninja\Transformers\ClientTransformer; +use App\Ninja\Transformers\PaymentTransformer; + +use League\Fractal\Manager; +use League\Fractal\Resource\Item; +use App\Ninja\Serializers\ArraySerializer; + class SubscriptionListener { public function createdClient(ClientWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_CLIENT, $event->client); + $transformer = new ClientTransformer(Auth::user()->account); + $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_CLIENT, $event->client, $transformer); } public function createdQuote(QuoteWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_QUOTE, $event->quote); + $transformer = new InvoiceTransformer(Auth::user()->account); + $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_QUOTE, $event->quote, $transformer, ENTITY_CLIENT); } public function createdPayment(PaymentWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_PAYMENT, $event->payment); + $transformer = new PaymentTransformer(Auth::user()->account); + $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_PAYMENT, $event->payment, $transformer, [ENTITY_CLIENT, ENTITY_INVOICE]); } public function createdCredit(CreditWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_CREDIT, $event->credit); + //$this->checkSubscriptions(ACTIVITY_TYPE_CREATE_CREDIT, $event->credit); } public function createdInvoice(InvoiceWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_INVOICE, $event->invoice); - } - - private function checkSubscriptions($activityTypeId, $entity) - { - $subscription = $entity->account->getSubscription($activityTypeId); - - if ($subscription) { - Utils::notifyZapier($subscription, $entity); - } + $transformer = new InvoiceTransformer(Auth::user()->account); + $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_INVOICE, $event->invoice, $transformer, ENTITY_CLIENT); } public function createdVendor(VendorWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_VENDOR, $event->vendor); + //$this->checkSubscriptions(ACTIVITY_TYPE_CREATE_VENDOR, $event->vendor); } public function createdExpense(ExpenseWasCreated $event) { - $this->checkSubscriptions(ACTIVITY_TYPE_CREATE_EXPENSE, $event->expense); + //$this->checkSubscriptions(ACTIVITY_TYPE_CREATE_EXPENSE, $event->expense); + } + + private function checkSubscriptions($activityTypeId, $entity, $transformer, $include = '') + { + $subscription = $entity->account->getSubscription($activityTypeId); + + if ($subscription) { + $manager = new Manager(); + $manager->setSerializer(new ArraySerializer()); + $manager->parseIncludes($include); + + $resource = new Item($entity, $transformer, $entity->getEntityType()); + $data = $manager->createData($resource)->toArray(); + + Utils::notifyZapier($subscription, $data); + } } - } diff --git a/app/Models/Client.php b/app/Models/Client.php index 2984f7372fd5..eae0eb1c148f 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -3,8 +3,6 @@ use Utils; use DB; use Carbon; -use App\Events\ClientWasCreated; -use App\Events\ClientWasUpdated; use Laracasts\Presenter\PresentableTrait; use Illuminate\Database\Eloquent\SoftDeletes; @@ -299,14 +297,6 @@ Client::creating(function ($client) { $client->setNullValues(); }); -Client::created(function ($client) { - event(new ClientWasCreated($client)); -}); - Client::updating(function ($client) { $client->setNullValues(); }); - -Client::updated(function ($client) { - event(new ClientWasUpdated($client)); -}); diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index b8e7d651ada4..9e3fd6ec53e8 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -91,30 +91,6 @@ class EntityModel extends Eloquent return $this->getName(); } - // 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; - } - public function setNullValues() { foreach ($this->fillable as $field) { diff --git a/app/Ninja/Repositories/ClientRepository.php b/app/Ninja/Repositories/ClientRepository.php index 3e43d8f34f5f..324d62a45943 100644 --- a/app/Ninja/Repositories/ClientRepository.php +++ b/app/Ninja/Repositories/ClientRepository.php @@ -5,6 +5,8 @@ use App\Ninja\Repositories\BaseRepository; use App\Models\Client; use App\Models\Contact; use App\Models\Activity; +use App\Events\ClientWasCreated; +use App\Events\ClientWasUpdated; class ClientRepository extends BaseRepository { @@ -97,6 +99,12 @@ class ClientRepository extends BaseRepository } } + if (!$publicId || $publicId == '-1') { + event(new ClientWasCreated($client)); + } else { + event(new ClientWasUpdated($client)); + } + return $client; } } diff --git a/app/Ninja/Transformers/ClientTransformer.php b/app/Ninja/Transformers/ClientTransformer.php index 66fdd35bf064..83ee7e56238b 100644 --- a/app/Ninja/Transformers/ClientTransformer.php +++ b/app/Ninja/Transformers/ClientTransformer.php @@ -40,8 +40,11 @@ class ClientTransformer extends EntityTransformer * @SWG\Property(property="language_id", type="integer", example=1) */ - protected $availableIncludes = [ + protected $defaultIncludes = [ 'contacts', + ]; + + protected $availableIncludes = [ 'invoices', 'credits', ]; @@ -92,7 +95,9 @@ class ClientTransformer extends EntityTransformer 'vat_number' => $client->vat_number, 'id_number' => $client->id_number, 'language_id' => (int) $client->language_id, - 'currency_id' => (int) $client->currency_id + 'currency_id' => (int) $client->currency_id, + 'custom_value1' => $client->custom_value1, + 'custom_value2' => $client->custom_value2, ]; } } \ No newline at end of file diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index 171491995cbe..464fc5e3fad7 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -22,11 +22,12 @@ class InvoiceTransformer extends EntityTransformer protected $defaultIncludes = [ 'invoice_items', - 'payments' ]; protected $availableIncludes = [ 'invitations', + 'payments', + 'client', ]; public function includeInvoiceItems(Invoice $invoice) @@ -47,6 +48,12 @@ class InvoiceTransformer extends EntityTransformer return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT); } + public function includeClient(Invoice $invoice) + { + $transformer = new ClientTransformer($this->account, $this->serializer); + return $this->includeItem($invoice->client, $transformer, 'client'); + } + public function transform(Invoice $invoice) { return [ diff --git a/app/Ninja/Transformers/PaymentTransformer.php b/app/Ninja/Transformers/PaymentTransformer.php index a2750eaad8af..22642259751a 100644 --- a/app/Ninja/Transformers/PaymentTransformer.php +++ b/app/Ninja/Transformers/PaymentTransformer.php @@ -20,6 +20,11 @@ class PaymentTransformer extends EntityTransformer */ protected $defaultIncludes = []; + protected $availableIncludes = [ + 'client', + 'invoice', + ]; + public function __construct(Account $account) { diff --git a/public/built.js b/public/built.js index 4566f5eeb0e0..ec11a94ceb11 100644 --- a/public/built.js +++ b/public/built.js @@ -30311,7 +30311,7 @@ if (window.ko) { function getContactDisplayName(contact) { if (contact.first_name || contact.last_name) { - return contact.first_name + ' ' + contact.last_name; + return (contact.first_name || '') + ' ' + (contact.last_name || ''); } else { return contact.email; } diff --git a/public/js/script.js b/public/js/script.js index e1feaefd8f28..a98db86385aa 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -430,7 +430,7 @@ if (window.ko) { function getContactDisplayName(contact) { if (contact.first_name || contact.last_name) { - return contact.first_name + ' ' + contact.last_name; + return (contact.first_name || '') + ' ' + (contact.last_name || ''); } else { return contact.email; } diff --git a/resources/views/invoices/knockout.blade.php b/resources/views/invoices/knockout.blade.php index c87433430368..7edc741cf81a 100644 --- a/resources/views/invoices/knockout.blade.php +++ b/resources/views/invoices/knockout.blade.php @@ -626,7 +626,7 @@ function ContactModel(data) { self.displayName = ko.computed(function() { var str = ''; if (self.first_name() || self.last_name()) { - str += self.first_name() + ' ' + self.last_name() + '\n'; + str += (self.first_name() || '') + ' ' + (self.last_name() || '') + '\n'; } if (self.email()) { str += self.email() + '\n'; @@ -637,8 +637,9 @@ function ContactModel(data) { self.email.display = ko.computed(function() { var str = ''; + if (self.first_name() || self.last_name()) { - str += self.first_name() + ' ' + self.last_name() + '
'; + str += (self.first_name() || '') + ' ' + (self.last_name() || '') + '
'; } if (self.email()) { str += self.email() + '
';