diff --git a/app/Http/Controllers/MigrationController.php b/app/Http/Controllers/MigrationController.php index 0b0bbfcc17..66c01fba43 100644 --- a/app/Http/Controllers/MigrationController.php +++ b/app/Http/Controllers/MigrationController.php @@ -77,6 +77,18 @@ class MigrationController extends BaseController */ public function purgeCompany(Company $company) { + // $account = $company->account; + + // if($account->default_company_id == $company->id) + // { + // $companies = $account->companies; + + // if($companies->count() > 1) + // { + + + // } + // } $company->delete(); return response()->json(['message' => 'Company purged'], 200); @@ -207,6 +219,9 @@ class MigrationController extends BaseController $account = auth()->user()->account; $company = (new ImportMigrations())->getCompany($account); + $account->default_company_id = $company->id; + $account->save(); + $company_token = new CompanyToken(); $company_token->user_id = $user->id; $company_token->company_id = $company->id; @@ -262,6 +277,10 @@ class MigrationController extends BaseController $account = auth()->user()->account; $company = (new ImportMigrations())->getCompany($account); + $account->default_company_id = $company->id; + $account->save(); + + $company_token = new CompanyToken(); $company_token->user_id = $user->id; $company_token->company_id = $company->id; @@ -291,6 +310,10 @@ class MigrationController extends BaseController $account = auth()->user()->account; $company = (new ImportMigrations())->getCompany($account); + $account->default_company_id = $company->id; + $account->save(); + + $company_token = new CompanyToken(); $company_token->user_id = $user->id; $company_token->company_id = $company->id; diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 6a84c10207..5980825ad5 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -147,6 +147,9 @@ class Import implements ShouldQueue */ public function handle() { + + set_time_limit(0); + foreach ($this->data as $key => $resource) { if (! in_array($key, $this->available_imports)) { //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); @@ -193,6 +196,12 @@ class Import implements ShouldQueue $company_repository->save($data, $this->company); Company::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $rules = null; + $validator = null; + $company_repository = null; } private function transformCompanyData(array $data): array @@ -258,6 +267,11 @@ class Import implements ShouldQueue } TaxRate::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $rules = null; + $validator = null; } /** @@ -305,6 +319,12 @@ class Import implements ShouldQueue } User::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $rules = null; + $validator = null; + $user_repository = null; } /** @@ -358,6 +378,11 @@ class Import implements ShouldQueue } Client::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $contact_repository = null; + $client_repository = null; } private function processProducts(array $data): void @@ -396,6 +421,10 @@ class Import implements ShouldQueue } Product::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $product_repository = null; } private function processInvoices(array $data): void @@ -414,7 +443,8 @@ class Import implements ShouldQueue $invoice_repository = new InvoiceRepository(); - foreach ($data as $resource) { + foreach ($data as $key => $resource) { + $modified = $resource; if (array_key_exists('client_id', $resource) && ! array_key_exists('clients', $this->ids)) { @@ -442,6 +472,11 @@ class Import implements ShouldQueue } Invoice::reguard(); + + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $invoice_repository = null; } private function processCredits(array $data): void @@ -487,6 +522,10 @@ class Import implements ShouldQueue } Credit::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $credit_repository = null; } private function processQuotes(array $data): void @@ -535,6 +574,10 @@ class Import implements ShouldQueue } Quote::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $quote_repository = null; } private function processPayments(array $data): void @@ -570,6 +613,7 @@ class Import implements ShouldQueue unset($modified['invoice_id']); if (isset($modified['invoices'])) { + foreach ($modified['invoices'] as $invoice) { $invoice['invoice_id'] = $this->transformId('invoices', $invoice['invoice_id']); } @@ -591,6 +635,10 @@ class Import implements ShouldQueue } Payment::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; + $payment_repository = null; } private function processDocuments(array $data): void @@ -642,6 +690,9 @@ class Import implements ShouldQueue } Document::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; } private function processCompanyGateways(array $data) :void @@ -688,6 +739,9 @@ class Import implements ShouldQueue } CompanyGateway::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; } private function processClientGatewayTokens(array $data) :void @@ -715,6 +769,9 @@ class Import implements ShouldQueue } ClientGatewayToken::reguard(); + + /*Improve memory handling by setting everything to null when we have finished*/ + $data = null; } /** diff --git a/app/Jobs/Util/SubscriptionHandler.php b/app/Jobs/Util/SubscriptionHandler.php index ec4a7da247..9743503233 100644 --- a/app/Jobs/Util/SubscriptionHandler.php +++ b/app/Jobs/Util/SubscriptionHandler.php @@ -2,24 +2,34 @@ namespace App\Jobs\Util; +use App\Models\Subscription; +use App\Transformers\ArraySerializer; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use League\Fractal\Manager; +use League\Fractal\Resource\Item; class SubscriptionHandler implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + private $entity; + + private $event_id; + /** * Create a new job instance. * * @return void */ - public function __construct() + + public function __construct($event_id, $entity) { - // + $this->event_id = $event_id; + $this->entity = $entity; } /** @@ -29,6 +39,55 @@ class SubscriptionHandler implements ShouldQueue */ public function handle() { - // + $subscriptions = Subscription::where('company_id', $this->entity->company_id) + ->where('event_id', $this->event_id) + ->get(); + + if(!$subscriptions || $subscriptions->count() == 0) + return; + + $subscriptions->each(function($subscription) { + $this->process($subscription); + }); + } + + private function process($subscription) + { + // generate JSON data + $manager = new Manager(); + $manager->setSerializer(new ArraySerializer()); + $manager->parseIncludes($include); + + $transformer = new $this->getTransformerClassName(); + + $resource = new Item($this->entity, $transformer, $this->entity->getEntityType()); + $data = $manager->createData($resource)->toArray(); + + $this->postData($subscription, $data, []); + } + + private function getTransformerClassName() + { + return sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity)); + } + + private function postData($subscription, $data, $headers = []) + { + $base_headers = [ + 'Content-Length' => strlen($data), + 'Accept' => 'application/json' + ]; + + $client = new \GuzzleHttp\Client(['headers' => array_merge($base_headers, $headers)]); + + //$response = $client->request('POST', $subscription->target_url, ['form_params' => $data]); + + $response = $client->post($subscription->target_url, [ + GuzzleHttp\RequestOptions::JSON => $data // or 'json' => [...] + ]); + + if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) { + $subscription->delete(); + } } } diff --git a/app/Models/Account.php b/app/Models/Account.php index 8bfc6a6512..fc162dcb6a 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -84,9 +84,12 @@ class Account extends BaseModel const RESULT_FAILURE = 'failure'; const RESULT_SUCCESS = 'success'; - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ + + + public function getEntityType() + { + return Account::class; + } public function users() { diff --git a/app/Models/Activity.php b/app/Models/Activity.php index b37f25160d..b8dd595800 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -79,6 +79,12 @@ class Activity extends StaticModel 'deleted_at' => 'timestamp', ]; + public function getEntityType() + { + return Activity::class; + } + + public function backup() { return $this->hasOne(Backup::class); diff --git a/app/Models/Backup.php b/app/Models/Backup.php index af3a164b06..ab066cfe8b 100644 --- a/app/Models/Backup.php +++ b/app/Models/Backup.php @@ -18,6 +18,12 @@ use Laracasts\Presenter\PresentableTrait; class Backup extends BaseModel { + + public function getEntityType() + { + return Backup::class; + } + public function activity() { return $this->belongsTo(Activity::class); diff --git a/app/Models/Bank.php b/app/Models/Bank.php index 368e89ab5d..f123232904 100644 --- a/app/Models/Bank.php +++ b/app/Models/Bank.php @@ -32,4 +32,10 @@ class Bank extends StaticModel return new \App\Libraries\Bank($finance, $config->fid, $config->url, $config->org); } + + public function getEntityType() + { + return Bank::class; + } + } diff --git a/app/Models/BankAccount.php b/app/Models/BankAccount.php index c43482d26c..aee5e84e3e 100644 --- a/app/Models/BankAccount.php +++ b/app/Models/BankAccount.php @@ -67,4 +67,10 @@ class BankAccount extends BaseModel { return $this->hasMany('App\Models\BankSubaccount'); } + + public function getEntityType() + { + return BankAccount::class; + } + } diff --git a/app/Models/Client.php b/app/Models/Client.php index 92198fcf57..c6eecb28d5 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -113,6 +113,11 @@ class Client extends BaseModel implements HasLocalePreference 'deleted_at' => 'timestamp', ]; + public function getEntityType() + { + return Client::class; + } + public function ledger() { return $this->hasMany(CompanyLedger::class); diff --git a/app/Models/ClientContact.php b/app/Models/ClientContact.php index 8bc3dc21b5..4ed125e84e 100644 --- a/app/Models/ClientContact.php +++ b/app/Models/ClientContact.php @@ -86,6 +86,11 @@ class ClientContact extends Authenticatable implements HasLocalePreference 'is_primary', ]; + public function getEntityType() + { + return ClientContact::class; + } + public function getHashedIdAttribute() { return $this->encodePrimaryKey($this->id); diff --git a/app/Models/ClientGatewayToken.php b/app/Models/ClientGatewayToken.php index 25eb936e17..b227f2b256 100644 --- a/app/Models/ClientGatewayToken.php +++ b/app/Models/ClientGatewayToken.php @@ -29,6 +29,11 @@ class ClientGatewayToken extends BaseModel 'deleted_at' => 'timestamp', ]; + public function getEntityType() + { + return ClientGatewayToken::class; + } + public function client() { return $this->hasOne(Client::class)->withTrashed(); diff --git a/app/Models/Company.php b/app/Models/Company.php index 217c2ec1e4..9f3856ab1f 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -133,6 +133,11 @@ class Company extends BaseModel self::ENTITY_RECURRING_QUOTE => 2048, ]; + public function getEntityType() + { + return Company::class; + } + public function ledger() { return $this->hasMany(CompanyLedger::class); diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 5f656f95ec..f499288f34 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -55,6 +55,11 @@ class CompanyGateway extends BaseModel // return json_decode($this->attributes['fees_and_limits']); // } + public function getEntityType() + { + return CompanyGateway::class; + } + public function company() { return $this->belongsTo(Company::class); diff --git a/app/Models/CompanyLedger.php b/app/Models/CompanyLedger.php index b74da766db..b5e660a6b9 100644 --- a/app/Models/CompanyLedger.php +++ b/app/Models/CompanyLedger.php @@ -27,6 +27,11 @@ class CompanyLedger extends Model 'deleted_at' => 'timestamp', ]; + public function getEntityType() + { + return CompanyLedger::class; + } + public function user() { return $this->belongsTo(User::class); diff --git a/app/Models/CompanyToken.php b/app/Models/CompanyToken.php index 9cb8295cf4..1e12652aa2 100644 --- a/app/Models/CompanyToken.php +++ b/app/Models/CompanyToken.php @@ -27,6 +27,11 @@ class CompanyToken extends BaseModel protected $with = [ ]; + public function getEntityType() + { + return CompanyToken::class; + } + public function account() { return $this->belongsTo(Account::class); diff --git a/app/Models/CompanyUser.php b/app/Models/CompanyUser.php index bd357ca191..5f7b5713c2 100644 --- a/app/Models/CompanyUser.php +++ b/app/Models/CompanyUser.php @@ -48,6 +48,11 @@ class CompanyUser extends Pivot 'slack_webhook_url', ]; + public function getEntityType() + { + return CompanyUser::class; + } + public function account() { return $this->belongsTo(Account::class); diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 609a03b7aa..a07a422dc0 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -80,6 +80,11 @@ class Credit extends BaseModel const STATUS_PARTIAL = 3; const STATUS_APPLIED = 4; + public function getEntityType() + { + return Credit::class; + } + public function getDateAttribute($value) { if (!empty($value)) { diff --git a/app/Models/CreditInvitation.php b/app/Models/CreditInvitation.php index 139a5b9a40..d9470f2bb2 100644 --- a/app/Models/CreditInvitation.php +++ b/app/Models/CreditInvitation.php @@ -36,6 +36,11 @@ class CreditInvitation extends BaseModel // 'company', ]; + public function getEntityType() + { + return CreditInvitation::class; + } + public function getSignatureDateAttribute($value) { if (!$value) { diff --git a/app/Models/Document.php b/app/Models/Document.php index 31bedb1eec..a16bdaaeb2 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -88,6 +88,10 @@ class Document extends BaseModel 'tif' => 'tiff', ]; + public function getEntityType() + { + return Document::class; + } public function documentable() { diff --git a/app/Models/Expense.php b/app/Models/Expense.php index a3e3269437..460215af79 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -60,6 +60,10 @@ class Expense extends BaseModel 'deleted_at' => 'timestamp', ]; + public function getEntityType() + { + return Expense::class; + } public function documents() { diff --git a/app/Models/ExpenseCategory.php b/app/Models/ExpenseCategory.php index 8cb39c13a3..6e923e2559 100644 --- a/app/Models/ExpenseCategory.php +++ b/app/Models/ExpenseCategory.php @@ -23,6 +23,11 @@ class ExpenseCategory extends BaseModel 'name', ]; + public function getEntityType() + { + return ExpenseCategory::class; + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index bb88ca399f..032fff4f4c 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -131,6 +131,11 @@ class Invoice extends BaseModel const STATUS_OVERDUE = -1; //status < 4 || < 3 && !is_deleted && !trashed() && due_date < now() const STATUS_UNPAID = -2; //status < 4 || < 3 && !is_deleted && !trashed() + public function getEntityType() + { + return Invoice::class; + } + public function getDateAttribute($value) { if (!empty($value)) { diff --git a/app/Models/InvoiceInvitation.php b/app/Models/InvoiceInvitation.php index 2fc0e1a992..304b52352b 100644 --- a/app/Models/InvoiceInvitation.php +++ b/app/Models/InvoiceInvitation.php @@ -35,6 +35,10 @@ class InvoiceInvitation extends BaseModel // 'company', ]; + public function getEntityType() + { + return InvoiceInvitation::class; + } public function getSignatureDateAttribute($value) { diff --git a/app/Models/Payment.php b/app/Models/Payment.php index c56dadcac6..cfc3f2a4a4 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -79,6 +79,11 @@ class Payment extends BaseModel 'paymentables', ]; + public function getEntityType() + { + return Payment::class; + } + public function client() { return $this->belongsTo(Client::class)->withTrashed(); diff --git a/app/Models/Product.php b/app/Models/Product.php index 192431641c..d6cf016ad6 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -40,6 +40,11 @@ class Product extends BaseModel 'tax_rate3', ]; + public function getEntityType() + { + return Product::class; + } + public function company() { return $this->belongsTo(Company::class); diff --git a/app/Models/Project.php b/app/Models/Project.php index e924474301..9ac53ca4ef 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -37,6 +37,11 @@ class Project extends BaseModel 'created_at' => 'timestamp', ]; + public function getEntityType() + { + return Project::class; + } + /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Models/Proposal.php b/app/Models/Proposal.php index bfbf29ab3d..d31500e7d8 100644 --- a/app/Models/Proposal.php +++ b/app/Models/Proposal.php @@ -22,6 +22,11 @@ class Proposal extends BaseModel 'id', ]; + public function getEntityType() + { + return Proposal::class; + } + protected $appends = ['proposal_id']; public function getRouteKeyName() diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 8868db1786..9c758b9445 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -86,6 +86,11 @@ class Quote extends BaseModel const STATUS_APPROVED = 3; const STATUS_EXPIRED = -1; + public function getEntityType() + { + return Quote::class; + } + public function getDateAttribute($value) { if (!empty($value)) { diff --git a/app/Models/QuoteInvitation.php b/app/Models/QuoteInvitation.php index b456297d72..eef652324d 100644 --- a/app/Models/QuoteInvitation.php +++ b/app/Models/QuoteInvitation.php @@ -30,6 +30,11 @@ class QuoteInvitation extends BaseModel 'client_contact_id', ]; + public function getEntityType() + { + return QuoteInvitation::class; + } + public function getSignatureDateAttribute($value) { if (!$value) { diff --git a/app/Models/RecurringInvoice.php b/app/Models/RecurringInvoice.php index 8af1bc2101..5c4abe7632 100644 --- a/app/Models/RecurringInvoice.php +++ b/app/Models/RecurringInvoice.php @@ -111,6 +111,11 @@ class RecurringInvoice extends BaseModel 'status' ]; + public function getEntityType() + { + return RecurringInvoice::class; + } + public function getDateAttribute($value) { if (!empty($value)) { diff --git a/app/Models/RecurringInvoiceInvitation.php b/app/Models/RecurringInvoiceInvitation.php index afed4c6176..d991a60fa0 100644 --- a/app/Models/RecurringInvoiceInvitation.php +++ b/app/Models/RecurringInvoiceInvitation.php @@ -18,6 +18,11 @@ class RecurringInvoiceInvitation extends BaseModel { use MakesDates; + public function getEntityType() + { + return RecurringInvoiceInvitation::class; + } + /** * @return mixed */ diff --git a/app/Models/RecurringQuote.php b/app/Models/RecurringQuote.php index e71c33bad6..60310b0e8d 100644 --- a/app/Models/RecurringQuote.php +++ b/app/Models/RecurringQuote.php @@ -94,6 +94,11 @@ class RecurringQuote extends BaseModel // 'company', ]; + public function getEntityType() + { + return RecurringQuote::class; + } + public function getDateAttribute($value) { if (!empty($value)) { diff --git a/app/Models/Task.php b/app/Models/Task.php index bea4fe51ca..8880c08c76 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -35,6 +35,11 @@ class Task extends BaseModel 'created_at' => 'timestamp', ]; + public function getEntityType() + { + return Task::class; + } + public function documents() { return $this->morphMany(Document::class, 'documentable'); diff --git a/app/Models/TaxRate.php b/app/Models/TaxRate.php index ea0af6a6e2..926f733a0b 100644 --- a/app/Models/TaxRate.php +++ b/app/Models/TaxRate.php @@ -27,6 +27,11 @@ class TaxRate extends BaseModel protected $appends = ['tax_rate_id']; + public function getEntityType() + { + return TaxRate::class; + } + public function getRouteKeyName() { return 'tax_rate_id'; diff --git a/app/Models/User.php b/app/Models/User.php index dddededc5a..8467ea069a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -101,6 +101,11 @@ class User extends Authenticatable implements MustVerifyEmail //'last_login' => 'timestamp', ]; + public function getEntityType() + { + return User::class; + } + public function getHashedIdAttribute() { return $this->encodePrimaryKey($this->id); diff --git a/app/Models/Vendor.php b/app/Models/Vendor.php index de2cd58320..289eae635c 100644 --- a/app/Models/Vendor.php +++ b/app/Models/Vendor.php @@ -57,6 +57,11 @@ class Vendor extends BaseModel // 'contacts', ]; + public function getEntityType() + { + return Vendor::class; + } + public function documents() { return $this->morphMany(Document::class, 'documentable'); diff --git a/app/Models/VendorContact.php b/app/Models/VendorContact.php index 57bdf3a95a..952cbcc92e 100644 --- a/app/Models/VendorContact.php +++ b/app/Models/VendorContact.php @@ -71,6 +71,11 @@ class VendorContact extends Authenticatable implements HasLocalePreference 'is_primary', ]; + public function getEntityType() + { + return VendorContact::class; + } + public function getHashedIdAttribute() { return $this->encodePrimaryKey($this->id); diff --git a/app/Notifications/Admin/EntitySentNotification.php b/app/Notifications/Admin/EntitySentNotification.php index 7f8f3a8df2..039affe86a 100644 --- a/app/Notifications/Admin/EntitySentNotification.php +++ b/app/Notifications/Admin/EntitySentNotification.php @@ -97,7 +97,10 @@ class EntitySentNotification extends Notification implements ShouldQueue return (new MailMessage) ->subject($subject) - ->markdown('email.admin.generic', $data); + ->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/Admin/EntityViewedNotification.php b/app/Notifications/Admin/EntityViewedNotification.php index 4cae663102..e61e64057b 100644 --- a/app/Notifications/Admin/EntityViewedNotification.php +++ b/app/Notifications/Admin/EntityViewedNotification.php @@ -73,7 +73,10 @@ class EntityViewedNotification extends Notification implements ShouldQueue return (new MailMessage) ->subject($subject) - ->markdown('email.admin.generic', $data); + ->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/Admin/InvoiceSentNotification.php b/app/Notifications/Admin/InvoiceSentNotification.php index 102d092255..fa2e9cdcac 100644 --- a/app/Notifications/Admin/InvoiceSentNotification.php +++ b/app/Notifications/Admin/InvoiceSentNotification.php @@ -91,7 +91,10 @@ class InvoiceSentNotification extends Notification implements ShouldQueue return (new MailMessage) ->subject($subject) - ->markdown('email.admin.generic', $data); + ->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/Admin/InvoiceViewedNotification.php b/app/Notifications/Admin/InvoiceViewedNotification.php index a5634e5dc9..75ac346da4 100644 --- a/app/Notifications/Admin/InvoiceViewedNotification.php +++ b/app/Notifications/Admin/InvoiceViewedNotification.php @@ -89,7 +89,10 @@ class InvoiceViewedNotification extends Notification implements ShouldQueue return (new MailMessage) ->subject($subject) - ->markdown('email.admin.generic', $data); + ->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/Admin/NewPartialPaymentNotification.php b/app/Notifications/Admin/NewPartialPaymentNotification.php index c7dfa01aa8..b088267ef0 100644 --- a/app/Notifications/Admin/NewPartialPaymentNotification.php +++ b/app/Notifications/Admin/NewPartialPaymentNotification.php @@ -93,7 +93,10 @@ class NewPartialPaymentNotification extends Notification implements ShouldQueue 'texts.notification_partial_payment_paid_subject', ['client' => $this->payment->client->present()->name()] ) - )->markdown('email.admin.generic', $data); + )->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/Admin/NewPaymentNotification.php b/app/Notifications/Admin/NewPaymentNotification.php index c42536b2e4..6069e7cafc 100644 --- a/app/Notifications/Admin/NewPaymentNotification.php +++ b/app/Notifications/Admin/NewPaymentNotification.php @@ -96,7 +96,10 @@ class NewPaymentNotification extends Notification implements ShouldQueue 'texts.notification_payment_paid_subject', ['client' => $this->payment->client->present()->name(),] ) - )->markdown('email.admin.generic', $data); + )->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/BaseNotification.php b/app/Notifications/BaseNotification.php index 1a7f2f30c6..5b9d71b768 100644 --- a/app/Notifications/BaseNotification.php +++ b/app/Notifications/BaseNotification.php @@ -88,7 +88,9 @@ class BaseNotification extends Notification implements ShouldQueue } - return $mail_message; + return $mail_message->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->invitation->company->company_key); + }); } public function buildMailMessageData() :array diff --git a/app/Notifications/NewAccountCreated.php b/app/Notifications/NewAccountCreated.php index ee2c2df48b..72018c2888 100644 --- a/app/Notifications/NewAccountCreated.php +++ b/app/Notifications/NewAccountCreated.php @@ -70,6 +70,9 @@ class NewAccountCreated extends Notification implements ShouldQueue return (new MailMessage) ->subject(ctrans('texts.new_signup')) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }) ->markdown('email.admin.generic', $data); } diff --git a/app/Notifications/Ninja/NewAccountCreated.php b/app/Notifications/Ninja/NewAccountCreated.php index 3714b0494d..eeed78b237 100644 --- a/app/Notifications/Ninja/NewAccountCreated.php +++ b/app/Notifications/Ninja/NewAccountCreated.php @@ -70,7 +70,10 @@ class NewAccountCreated extends Notification implements ShouldQueue return (new MailMessage) ->subject(ctrans('texts.new_signup')) - ->markdown('email.admin.generic', $data); + ->markdown('email.admin.generic', $data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->company->company_key); + }); } /** diff --git a/app/Notifications/SendGenericNotification.php b/app/Notifications/SendGenericNotification.php index 0deab68d31..fa1395900d 100644 --- a/app/Notifications/SendGenericNotification.php +++ b/app/Notifications/SendGenericNotification.php @@ -74,7 +74,9 @@ class SendGenericNotification extends BaseNotification implements ShouldQueue public function toMail($notifiable) { $mail_message = (new MailMessage) - ->markdown('email.admin.generic_email', $this->buildMailMessageData()); + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->invitation->company->company_key); + })->markdown('email.admin.generic_email', $this->buildMailMessageData()); $mail_message = $this->buildMailMessageSettings($mail_message); diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index 15a42b37a7..47405f4382 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -12,7 +12,9 @@ namespace App\Observers; use App\Events\Client\ClientWasCreated; +use App\Jobs\Util\SubscriptionHandler; use App\Models\Client; +use App\Models\Subscription; class ClientObserver { @@ -25,6 +27,8 @@ class ClientObserver public function created(Client $client) { event(new ClientWasCreated($client)); + + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_CLIENT, $client); } /** @@ -35,7 +39,7 @@ class ClientObserver */ public function updated(Client $client) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_CLIENT, $client); } /** @@ -46,7 +50,8 @@ class ClientObserver */ public function deleted(Client $client) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_CLIENT, $client); + } /** diff --git a/app/Observers/ExpenseObserver.php b/app/Observers/ExpenseObserver.php index c0485ab8e6..74e9fd1a13 100644 --- a/app/Observers/ExpenseObserver.php +++ b/app/Observers/ExpenseObserver.php @@ -11,7 +11,9 @@ namespace App\Observers; +use App\Jobs\Util\SubscriptionHandler; use App\Models\Expense; +use App\Models\Subscription; class ExpenseObserver { @@ -23,7 +25,7 @@ class ExpenseObserver */ public function created(Expense $expense) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_EXPENSE, $expense); } /** @@ -34,7 +36,7 @@ class ExpenseObserver */ public function updated(Expense $expense) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_EXPENSE, $expense); } /** @@ -45,7 +47,7 @@ class ExpenseObserver */ public function deleted(Expense $expense) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_EXPENSE, $expense); } /** diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 881b3084ab..dcd427bfd7 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -11,7 +11,9 @@ namespace App\Observers; +use App\Jobs\Util\SubscriptionHandler; use App\Models\Invoice; +use App\Models\Subscription; class InvoiceObserver { @@ -23,7 +25,7 @@ class InvoiceObserver */ public function created(Invoice $invoice) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_INVOICE, $invoice); } /** @@ -34,7 +36,7 @@ class InvoiceObserver */ public function updated(Invoice $invoice) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_INVOICE, $invoice); } /** @@ -45,7 +47,7 @@ class InvoiceObserver */ public function deleted(Invoice $invoice) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_INVOICE, $invoice); } /** diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index be378907ee..9f8ca33546 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -12,7 +12,9 @@ namespace App\Observers; use App\Events\Payment\PaymentWasCreated; +use App\Jobs\Util\SubscriptionHandler; use App\Models\Payment; +use App\Models\Subscription; class PaymentObserver { @@ -24,6 +26,7 @@ class PaymentObserver */ public function created(Payment $payment) { + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_PAYMENT, $payment); } /** @@ -34,7 +37,6 @@ class PaymentObserver */ public function updated(Payment $payment) { - // } /** @@ -45,7 +47,7 @@ class PaymentObserver */ public function deleted(Payment $payment) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_PAYMENT, $payment); } /** diff --git a/app/Observers/QuoteObserver.php b/app/Observers/QuoteObserver.php index a7cfd0d63c..3661d3c207 100644 --- a/app/Observers/QuoteObserver.php +++ b/app/Observers/QuoteObserver.php @@ -11,7 +11,9 @@ namespace App\Observers; +use App\Jobs\Util\SubscriptionHandler; use App\Models\Quote; +use App\Models\Subscription; class QuoteObserver { @@ -23,7 +25,7 @@ class QuoteObserver */ public function created(Quote $quote) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_QUOTE, $quote); } /** @@ -34,7 +36,7 @@ class QuoteObserver */ public function updated(Quote $quote) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_QUOTE, $quote); } /** @@ -45,7 +47,7 @@ class QuoteObserver */ public function deleted(Quote $quote) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_QUOTE, $quote); } /** diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index f4b3ad60d5..40b25075a7 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -11,6 +11,8 @@ namespace App\Observers; +use App\Jobs\Util\SubscriptionHandler; +use App\Models\Subscription; use App\Models\Task; class TaskObserver @@ -23,7 +25,7 @@ class TaskObserver */ public function created(Task $task) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_TASK, $task); } /** @@ -34,7 +36,7 @@ class TaskObserver */ public function updated(Task $task) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_TASK, $task); } /** @@ -45,7 +47,7 @@ class TaskObserver */ public function deleted(Task $task) { - // + SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_TASK, $task); } /** diff --git a/app/Transformers/CompanyGatewayTransformer.php b/app/Transformers/CompanyGatewayTransformer.php index c6f7e9c1b3..65c9673d06 100644 --- a/app/Transformers/CompanyGatewayTransformer.php +++ b/app/Transformers/CompanyGatewayTransformer.php @@ -53,7 +53,7 @@ class CompanyGatewayTransformer extends EntityTransformer 'show_shipping_address' => (bool)$company_gateway->show_shipping_address, 'update_details' => (bool)$company_gateway->update_details, 'config' => (string) $company_gateway->getConfigTransformed(), - 'fees_and_limits' => $company_gateway->fees_and_limits ?: '', + 'fees_and_limits' => $company_gateway->fees_and_limits ?: new \stdClass, 'updated_at' => (int)$company_gateway->updated_at, 'archived_at' => (int)$company_gateway->deleted_at, 'created_at' => (int)$company_gateway->created_at, diff --git a/composer.json b/composer.json index ac2d648730..7825b2ea57 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "fideloper/proxy": "^4.0", "fzaninotto/faker": "^1.4", "google/apiclient": "^2.0", + "guzzlehttp/guzzle": "^6.5", "hashids/hashids": "^3.0", "intervention/image": "^2.4", "laracasts/presenter": "^0.2.1", diff --git a/database/migrations/2020_04_08_234530_add_is_deleted_column_to_company_tokens_table.php b/database/migrations/2020_04_08_234530_add_is_deleted_column_to_company_tokens_table.php index 48cc5e5969..e9ba5d74ed 100644 --- a/database/migrations/2020_04_08_234530_add_is_deleted_column_to_company_tokens_table.php +++ b/database/migrations/2020_04_08_234530_add_is_deleted_column_to_company_tokens_table.php @@ -31,6 +31,9 @@ class AddIsDeletedColumnToCompanyTokensTable extends Migration $table->softDeletes('deleted_at', 6); $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); + + $table->index(['event_id', 'company_id']); + }); Schema::table('companies', function (Blueprint $table) { diff --git a/tests/Unit/EntityTest.php b/tests/Unit/EntityTest.php new file mode 100644 index 0000000000..afccc46b54 --- /dev/null +++ b/tests/Unit/EntityTest.php @@ -0,0 +1,56 @@ +makeTestData(); + + $this->invoice->line_items = $this->buildLineItems(); + + $this->invoice_calc = new InvoiceSum($this->invoice); + } + + public function testEntityNameResolution() + { + + $entity_type = $this->invoice->getEntityType(); + + $this->assertEquals('Invoice', class_basename($entity_type)); + + $invitation = $this->invoice->invitations->first(); + + $entity_type = $invitation->getEntityType(); + + $this->assertEquals('InvoiceInvitation', class_basename($entity_type)); + + $this->assertEquals('InvoiceInvitation', class_basename($invitation)); + + } + + + +}