mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Zapier improvements
This commit is contained in:
parent
ae6a81ea48
commit
049c20f062
@ -92,13 +92,19 @@ class BaseAPIController extends Controller
|
|||||||
protected function response($response)
|
protected function response($response)
|
||||||
{
|
{
|
||||||
$index = Request::get('index') ?: 'data';
|
$index = Request::get('index') ?: 'data';
|
||||||
$meta = isset($response['meta']) ? $response['meta'] : null;
|
|
||||||
$response = [
|
if ($index == 'none') {
|
||||||
$index => $response
|
unset($response['meta']);
|
||||||
];
|
} else {
|
||||||
if ($meta) {
|
$meta = isset($response['meta']) ? $response['meta'] : null;
|
||||||
$response['meta'] = $meta;
|
$response = [
|
||||||
unset($response[$index]['meta']);
|
$index => $response
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($meta) {
|
||||||
|
$response['meta'] = $meta;
|
||||||
|
unset($response[$index]['meta']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = json_encode($response, JSON_PRETTY_PRINT);
|
$response = json_encode($response, JSON_PRETTY_PRINT);
|
||||||
|
@ -12,6 +12,7 @@ use App\Models\Contact;
|
|||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\Models\Invitation;
|
use App\Models\Invitation;
|
||||||
use App\Ninja\Repositories\ClientRepository;
|
use App\Ninja\Repositories\ClientRepository;
|
||||||
|
use App\Ninja\Repositories\PaymentRepository;
|
||||||
use App\Ninja\Repositories\InvoiceRepository;
|
use App\Ninja\Repositories\InvoiceRepository;
|
||||||
use App\Ninja\Mailers\ContactMailer as Mailer;
|
use App\Ninja\Mailers\ContactMailer as Mailer;
|
||||||
use App\Http\Controllers\BaseAPIController;
|
use App\Http\Controllers\BaseAPIController;
|
||||||
@ -23,12 +24,13 @@ class InvoiceApiController extends BaseAPIController
|
|||||||
{
|
{
|
||||||
protected $invoiceRepo;
|
protected $invoiceRepo;
|
||||||
|
|
||||||
public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, Mailer $mailer)
|
public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, PaymentRepository $paymentRepo, Mailer $mailer)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->invoiceRepo = $invoiceRepo;
|
$this->invoiceRepo = $invoiceRepo;
|
||||||
$this->clientRepo = $clientRepo;
|
$this->clientRepo = $clientRepo;
|
||||||
|
$this->paymentRepo = $paymentRepo;
|
||||||
$this->mailer = $mailer;
|
$this->mailer = $mailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,14 +125,26 @@ class InvoiceApiController extends BaseAPIController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$clientData = ['contact' => ['email' => $email]];
|
$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])) {
|
if (isset($data[$field])) {
|
||||||
$clientData[$field] = $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])) {
|
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 = self::prepareData($data, $client);
|
||||||
$data['client_id'] = $client->id;
|
$data['client_id'] = $client->id;
|
||||||
$invoice = $this->invoiceRepo->save($data);
|
$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'])) {
|
if (!isset($data['id'])) {
|
||||||
$invitation = Invitation::createNew();
|
$invitation = Invitation::createNew();
|
||||||
@ -153,7 +177,11 @@ class InvoiceApiController extends BaseAPIController
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($data['email_invoice']) && $data['email_invoice']) {
|
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();
|
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
|
||||||
|
@ -686,7 +686,7 @@ class Utils
|
|||||||
return EVENT_CREATE_PAYMENT;
|
return EVENT_CREATE_PAYMENT;
|
||||||
} elseif ($eventName == 'create_vendor') {
|
} elseif ($eventName == 'create_vendor') {
|
||||||
return EVENT_CREATE_VENDOR;
|
return EVENT_CREATE_VENDOR;
|
||||||
}else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -694,7 +694,7 @@ class Utils
|
|||||||
public static function notifyZapier($subscription, $data)
|
public static function notifyZapier($subscription, $data)
|
||||||
{
|
{
|
||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
$jsonEncodedData = json_encode($data->toPublicArray());
|
$jsonEncodedData = json_encode($data);
|
||||||
|
|
||||||
$opts = [
|
$opts = [
|
||||||
CURLOPT_URL => $subscription->target_url,
|
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)
|
public static function getApiHeaders($count = 0)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -12,50 +12,68 @@ use App\Events\PaymentWasCreated;
|
|||||||
use App\Events\VendorWasCreated;
|
use App\Events\VendorWasCreated;
|
||||||
use App\Events\ExpenseWasCreated;
|
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
|
class SubscriptionListener
|
||||||
{
|
{
|
||||||
public function createdClient(ClientWasCreated $event)
|
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)
|
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)
|
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)
|
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)
|
public function createdInvoice(InvoiceWasCreated $event)
|
||||||
{
|
{
|
||||||
$this->checkSubscriptions(ACTIVITY_TYPE_CREATE_INVOICE, $event->invoice);
|
$transformer = new InvoiceTransformer(Auth::user()->account);
|
||||||
}
|
$this->checkSubscriptions(ACTIVITY_TYPE_CREATE_INVOICE, $event->invoice, $transformer, ENTITY_CLIENT);
|
||||||
|
|
||||||
private function checkSubscriptions($activityTypeId, $entity)
|
|
||||||
{
|
|
||||||
$subscription = $entity->account->getSubscription($activityTypeId);
|
|
||||||
|
|
||||||
if ($subscription) {
|
|
||||||
Utils::notifyZapier($subscription, $entity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createdVendor(VendorWasCreated $event)
|
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)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
use Utils;
|
use Utils;
|
||||||
use DB;
|
use DB;
|
||||||
use Carbon;
|
use Carbon;
|
||||||
use App\Events\ClientWasCreated;
|
|
||||||
use App\Events\ClientWasUpdated;
|
|
||||||
use Laracasts\Presenter\PresentableTrait;
|
use Laracasts\Presenter\PresentableTrait;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
@ -299,14 +297,6 @@ Client::creating(function ($client) {
|
|||||||
$client->setNullValues();
|
$client->setNullValues();
|
||||||
});
|
});
|
||||||
|
|
||||||
Client::created(function ($client) {
|
|
||||||
event(new ClientWasCreated($client));
|
|
||||||
});
|
|
||||||
|
|
||||||
Client::updating(function ($client) {
|
Client::updating(function ($client) {
|
||||||
$client->setNullValues();
|
$client->setNullValues();
|
||||||
});
|
});
|
||||||
|
|
||||||
Client::updated(function ($client) {
|
|
||||||
event(new ClientWasUpdated($client));
|
|
||||||
});
|
|
||||||
|
@ -91,30 +91,6 @@ class EntityModel extends Eloquent
|
|||||||
return $this->getName();
|
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()
|
public function setNullValues()
|
||||||
{
|
{
|
||||||
foreach ($this->fillable as $field) {
|
foreach ($this->fillable as $field) {
|
||||||
|
@ -5,6 +5,8 @@ use App\Ninja\Repositories\BaseRepository;
|
|||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Contact;
|
use App\Models\Contact;
|
||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
|
use App\Events\ClientWasCreated;
|
||||||
|
use App\Events\ClientWasUpdated;
|
||||||
|
|
||||||
class ClientRepository extends BaseRepository
|
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;
|
return $client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,11 @@ class ClientTransformer extends EntityTransformer
|
|||||||
* @SWG\Property(property="language_id", type="integer", example=1)
|
* @SWG\Property(property="language_id", type="integer", example=1)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected $availableIncludes = [
|
protected $defaultIncludes = [
|
||||||
'contacts',
|
'contacts',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $availableIncludes = [
|
||||||
'invoices',
|
'invoices',
|
||||||
'credits',
|
'credits',
|
||||||
];
|
];
|
||||||
@ -92,7 +95,9 @@ class ClientTransformer extends EntityTransformer
|
|||||||
'vat_number' => $client->vat_number,
|
'vat_number' => $client->vat_number,
|
||||||
'id_number' => $client->id_number,
|
'id_number' => $client->id_number,
|
||||||
'language_id' => (int) $client->language_id,
|
'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,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,11 +22,12 @@ class InvoiceTransformer extends EntityTransformer
|
|||||||
|
|
||||||
protected $defaultIncludes = [
|
protected $defaultIncludes = [
|
||||||
'invoice_items',
|
'invoice_items',
|
||||||
'payments'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $availableIncludes = [
|
protected $availableIncludes = [
|
||||||
'invitations',
|
'invitations',
|
||||||
|
'payments',
|
||||||
|
'client',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function includeInvoiceItems(Invoice $invoice)
|
public function includeInvoiceItems(Invoice $invoice)
|
||||||
@ -47,6 +48,12 @@ class InvoiceTransformer extends EntityTransformer
|
|||||||
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
|
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)
|
public function transform(Invoice $invoice)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -20,6 +20,11 @@ class PaymentTransformer extends EntityTransformer
|
|||||||
*/
|
*/
|
||||||
protected $defaultIncludes = [];
|
protected $defaultIncludes = [];
|
||||||
|
|
||||||
|
protected $availableIncludes = [
|
||||||
|
'client',
|
||||||
|
'invoice',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
public function __construct(Account $account)
|
public function __construct(Account $account)
|
||||||
{
|
{
|
||||||
|
@ -30311,7 +30311,7 @@ if (window.ko) {
|
|||||||
function getContactDisplayName(contact)
|
function getContactDisplayName(contact)
|
||||||
{
|
{
|
||||||
if (contact.first_name || contact.last_name) {
|
if (contact.first_name || contact.last_name) {
|
||||||
return contact.first_name + ' ' + contact.last_name;
|
return (contact.first_name || '') + ' ' + (contact.last_name || '');
|
||||||
} else {
|
} else {
|
||||||
return contact.email;
|
return contact.email;
|
||||||
}
|
}
|
||||||
|
@ -430,7 +430,7 @@ if (window.ko) {
|
|||||||
function getContactDisplayName(contact)
|
function getContactDisplayName(contact)
|
||||||
{
|
{
|
||||||
if (contact.first_name || contact.last_name) {
|
if (contact.first_name || contact.last_name) {
|
||||||
return contact.first_name + ' ' + contact.last_name;
|
return (contact.first_name || '') + ' ' + (contact.last_name || '');
|
||||||
} else {
|
} else {
|
||||||
return contact.email;
|
return contact.email;
|
||||||
}
|
}
|
||||||
|
@ -626,7 +626,7 @@ function ContactModel(data) {
|
|||||||
self.displayName = ko.computed(function() {
|
self.displayName = ko.computed(function() {
|
||||||
var str = '';
|
var str = '';
|
||||||
if (self.first_name() || self.last_name()) {
|
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()) {
|
if (self.email()) {
|
||||||
str += self.email() + '\n';
|
str += self.email() + '\n';
|
||||||
@ -637,8 +637,9 @@ function ContactModel(data) {
|
|||||||
|
|
||||||
self.email.display = ko.computed(function() {
|
self.email.display = ko.computed(function() {
|
||||||
var str = '';
|
var str = '';
|
||||||
|
|
||||||
if (self.first_name() || self.last_name()) {
|
if (self.first_name() || self.last_name()) {
|
||||||
str += self.first_name() + ' ' + self.last_name() + '<br/>';
|
str += (self.first_name() || '') + ' ' + (self.last_name() || '') + '<br/>';
|
||||||
}
|
}
|
||||||
if (self.email()) {
|
if (self.email()) {
|
||||||
str += self.email() + '<br/>';
|
str += self.email() + '<br/>';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user