mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-30 21:12:57 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			154 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			154 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Listeners;
 | |
| 
 | |
| use App\Events\ClientWasCreated;
 | |
| use App\Events\CreditWasCreated;
 | |
| use App\Events\ExpenseWasCreated;
 | |
| use App\Events\QuoteItemsWereCreated;
 | |
| use App\Events\QuoteItemsWereUpdated;
 | |
| use App\Events\InvoiceWasDeleted;
 | |
| use App\Events\PaymentWasCreated;
 | |
| use App\Events\InvoiceItemsWereCreated;
 | |
| use App\Events\InvoiceItemsWereUpdated;
 | |
| use App\Events\QuoteWasDeleted;
 | |
| use App\Events\VendorWasCreated;
 | |
| use App\Models\EntityModel;
 | |
| use App\Ninja\Serializers\ArraySerializer;
 | |
| use App\Ninja\Transformers\ClientTransformer;
 | |
| use App\Ninja\Transformers\InvoiceTransformer;
 | |
| use App\Ninja\Transformers\PaymentTransformer;
 | |
| use League\Fractal\Manager;
 | |
| use League\Fractal\Resource\Item;
 | |
| use Utils;
 | |
| 
 | |
| /**
 | |
|  * Class SubscriptionListener.
 | |
|  */
 | |
| class SubscriptionListener
 | |
| {
 | |
|     /**
 | |
|      * @param ClientWasCreated $event
 | |
|      */
 | |
|     public function createdClient(ClientWasCreated $event)
 | |
|     {
 | |
|         $transformer = new ClientTransformer($event->client->account);
 | |
|         $this->checkSubscriptions(EVENT_CREATE_CLIENT, $event->client, $transformer);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param PaymentWasCreated $event
 | |
|      */
 | |
|     public function createdPayment(PaymentWasCreated $event)
 | |
|     {
 | |
|         $transformer = new PaymentTransformer($event->payment->account);
 | |
|         $this->checkSubscriptions(EVENT_CREATE_PAYMENT, $event->payment, $transformer, [ENTITY_CLIENT, ENTITY_INVOICE]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param CreditWasCreated $event
 | |
|      */
 | |
|     public function createdCredit(CreditWasCreated $event)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param VendorWasCreated $event
 | |
|      */
 | |
|     public function createdVendor(VendorWasCreated $event)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param ExpenseWasCreated $event
 | |
|      */
 | |
|     public function createdExpense(ExpenseWasCreated $event)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param InvoiceWasCreated $event
 | |
|      */
 | |
|     public function createdInvoice(InvoiceItemsWereCreated $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->invoice->account);
 | |
|         $this->checkSubscriptions(EVENT_CREATE_INVOICE, $event->invoice, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param InvoiceWasUpdated $event
 | |
|      */
 | |
|     public function updatedInvoice(InvoiceItemsWereUpdated $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->invoice->account);
 | |
|         $this->checkSubscriptions(EVENT_UPDATE_INVOICE, $event->invoice, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param QuoteWasCreated $event
 | |
|      */
 | |
|     public function createdQuote(QuoteItemsWereCreated $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->quote->account);
 | |
|         $this->checkSubscriptions(EVENT_CREATE_QUOTE, $event->quote, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param QuoteWasUpdated $event
 | |
|      */
 | |
|     public function updatedQuote(QuoteItemsWereUpdated $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->quote->account);
 | |
|         $this->checkSubscriptions(EVENT_UPDATE_QUOTE, $event->quote, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param InvoiceWasDeleted $event
 | |
|      */
 | |
|     public function deletedInvoice(InvoiceWasDeleted $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->invoice->account);
 | |
|         $this->checkSubscriptions(EVENT_DELETE_INVOICE, $event->invoice, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param InvoiceWasDeleted $event
 | |
|      */
 | |
|     public function deletedQuote(QuoteWasDeleted $event)
 | |
|     {
 | |
|         $transformer = new InvoiceTransformer($event->quote->account);
 | |
|         $this->checkSubscriptions(EVENT_DELETE_QUOTE, $event->quote, $transformer, ENTITY_CLIENT);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $eventId
 | |
|      * @param $entity
 | |
|      * @param $transformer
 | |
|      * @param string $include
 | |
|      */
 | |
|     private function checkSubscriptions($eventId, $entity, $transformer, $include = '')
 | |
|     {
 | |
|         if (! EntityModel::$notifySubscriptions) {
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         $subscription = $entity->account->getSubscription($eventId);
 | |
| 
 | |
|         if ($subscription) {
 | |
|             $manager = new Manager();
 | |
|             $manager->setSerializer(new ArraySerializer());
 | |
|             $manager->parseIncludes($include);
 | |
| 
 | |
|             $resource = new Item($entity, $transformer, $entity->getEntityType());
 | |
|             $data = $manager->createData($resource)->toArray();
 | |
| 
 | |
|             // For legacy Zapier support
 | |
|             if (isset($data['client_id'])) {
 | |
|                 $data['client_name'] = $entity->client->getDisplayName();
 | |
|             }
 | |
| 
 | |
|             Utils::notifyZapier($subscription, $data);
 | |
|         }
 | |
|     }
 | |
| }
 |