Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(invoices): raise an InvoiceEvent before and after invoice save #10

Merged
merged 3 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/events/ContactEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace thejoshsmith\commerce\xero\events;

use craft\commerce\elements\Order;
use thejoshsmith\commerce\xero\services\XeroAPI;
use XeroPHP\Models\Accounting\Contact;
use yii\base\Event;

/**
* A Contact event
*/
class ContactEvent extends Event
{
/**
* Contact Model
*
* @var Contact
*/
public $contact;

/**
* Order Element
*
* @var Order
*/
public $order;

/**
* XeroApi
*
* @var XeroAPI
*/
public $xero;
}
35 changes: 35 additions & 0 deletions src/events/InvoiceEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace thejoshsmith\commerce\xero\events;

use craft\commerce\elements\Order;
use thejoshsmith\commerce\xero\services\XeroAPI;
use XeroPHP\Models\Accounting\Invoice;
use yii\base\Event;

/**
* An Invoice event
*/
class InvoiceEvent extends Event
{
/**
* Contact Model
*
* @var Invoice
*/
public $invoice;

/**
* Order Element
*
* @var Order
*/
public $order;

/**
* XeroApi
*
* @var XeroAPI
*/
public $xero;
}
35 changes: 35 additions & 0 deletions src/events/LineItemEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace thejoshsmith\commerce\xero\events;

use craft\commerce\models\LineItem as LineItemModel;
use XeroPHP\Models\Accounting\LineItem;
use thejoshsmith\commerce\xero\services\XeroAPI;
use yii\base\Event;

/**
* An Invoice event
*/
class LineItemEvent extends Event
{
/**
* Contact Model
*
* @var LineItem
*/
public $lineItem;

/**
* Order Element
*
* @var LineItemModel
*/
public $orderItem;

/**
* XeroApi
*
* @var XeroAPI
*/
public $xero;
}
68 changes: 64 additions & 4 deletions src/services/XeroAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace thejoshsmith\commerce\xero\services;

use Craft;
use thejoshsmith\commerce\xero\events\ContactEvent;
use thejoshsmith\commerce\xero\events\InvoiceEvent;
use thejoshsmith\commerce\xero\events\LineItemEvent;
use Throwable;

use yii\base\Exception;
Expand Down Expand Up @@ -44,6 +47,15 @@ class XeroAPI extends Component
{
const CACHE_DURATION = 3600; // 1 hour

/**
* Events
*/
public const EVENT_BEFORE_SAVE_CONTACT = 'beforeSaveContact';
public const EVENT_AFTER_SAVE_CONTACT = 'afterSaveContact';
public const EVENT_BEFORE_SAVE_INVOICE = 'beforeSaveInvoice';
public const EVENT_AFTER_SAVE_INVOICE = 'afterSaveInvoice';
public const EVENT_BEFORE_ADD_LINE_ITEM = 'beforeAddLineItem';

/**
* Defines the number of decimals to use
*
Expand Down Expand Up @@ -173,9 +185,29 @@ public function findOrCreateContact(Order $order)
->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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down