diff --git a/src/events/ContactEvent.php b/src/events/ContactEvent.php new file mode 100644 index 0000000..ba11cd2 --- /dev/null +++ b/src/events/ContactEvent.php @@ -0,0 +1,35 @@ +setLastName($contactLastName) ->setEmailAddress($contactEmail); - // TODO: add hook (before_save_contact) + // Raise event for before contact save + $beforeSaveEvent = new ContactEvent( + [ + 'xero' => $this, + 'contact' => $contact, + 'order' => $order + ] + ); + $this->trigger(self::EVENT_BEFORE_SAVE_CONTACT, $beforeSaveEvent); + $contact = $beforeSaveEvent->contact; $contact->save(); + + // Raise event for after contact save + $afterSaveEvent = new ContactEvent( + [ + 'xero' => $this, + 'contact' => $contact, + 'order' => $order + ] + ); + $this->trigger(self::EVENT_AFTER_SAVE_CONTACT, $afterSaveEvent); + $contact = $afterSaveEvent->contact; } return $contact; } catch(Throwable $e) { @@ -205,12 +237,21 @@ public function createInvoice(Contact $contact, Order $order) // TODO: check for line item adjustments - // check if product codes should be used and sent (inventory updates) if ($this->_client->getOrgSettings()->updateInventory) { $lineItem->setItemCode($orderItem->sku); } + $beforeAddLineItemEvent = new LineItemEvent( + [ + 'xero' => $this, + 'lineItem' => $lineItem, + 'orderItem' => $orderItem + ] + ); + $this->trigger(self::EVENT_BEFORE_ADD_LINE_ITEM, $beforeAddLineItemEvent); + $lineItem = $beforeAddLineItemEvent->lineItem; + $invoice->addLineItem($lineItem); } @@ -252,7 +293,17 @@ public function createInvoice(Contact $contact, Order $order) ->setSentToContact(true) ->setDueDate(new \DateTime('NOW')); - // TODO: add hook (before_invoice_save) + + // Raise event for before invoice save + $beforeSaveEvent = new InvoiceEvent( + [ + 'xero' => $this, + 'invoice' => $invoice, + 'order' => $order + ] + ); + $this->trigger(self::EVENT_BEFORE_SAVE_INVOICE, $beforeSaveEvent); + $invoice = $beforeSaveEvent->invoice; try { // save the invoice @@ -284,7 +335,16 @@ public function createInvoice(Contact $contact, Order $order) $invoiceRecord->invoiceId = $invoice->InvoiceID; $invoiceRecord->save(); - // TODO: add hook (after_invoice_save) + // Raise event for after invoice save + $afterSaveEvent = new InvoiceEvent( + [ + 'xero' => $this, + 'invoice' => $invoice, + 'order' => $order + ] + ); + $this->trigger(self::EVENT_AFTER_SAVE_INVOICE, $afterSaveEvent); + $invoice = $afterSaveEvent->invoice; return $invoice;