1.1.0 2020-04-16 15:09 UTC

README

Latest Version on Packagist Build Status StyleCI Scrutinizer Code Quality Total Downloads

一个小型Laravel包,用于与Xero通信。

安装

您可以通过composer安装此包

composer require macsidigital/laravel-xero

服务提供者应自动注册,适用于Laravel > 5.4。

对于Laravel < 5.5,打开config/app.php文件,并在提供者数组中追加

MacsiDigital\Xero\Providers\XeroServiceProvider::class

配置文件

发布配置文件

php artisan vendor:publish --provider="MacsiDigital\Xero\Providers\XeroServiceProvider"

这将在您的配置目录中创建一个xero/config.php。检查Xero文档以获取config.php文件中的相关值。确保RSA密钥的位置匹配。

使用方法

一切均已设置,以类似于Laravel语法的样式。

我们还使用一些魔法来处理Xero的模型名称。在Xero中,有几个不同的模块(会计、工资澳大利亚等),目前我们只支持会计区域的一小部分,但我们已设置命名,以便将来可以添加更多模块。

如果响应不是'200',则我们将抛出异常,因此请使用try-catch块。

因此,要使用会计模块中的联系人,我们将使用以下语法。

	$xero = new \MacsiDigital\Xero\Xero;
	$xero->AccountingContact->functionName();

查找所有

find all函数返回一个Laravel Collection,因此您可以使用所有Laravel Collection魔法

	$xero = new \MacsiDigital\Xero\Xero;
	$contacts = $xero->AccountingContact->all();

过滤

filtered find函数返回一个Laravel Collection,因此您可以使用所有Laravel Collection魔法

	$xero = new \MacsiDigital\Xero\Xero;
	$contacts = $xero->AccountingContact->where('Name', '=', 'Test Name')->get();

要获取单个项目,请使用'first'方法

	$xero = new \MacsiDigital\Xero\Xero;
	$contact = $xero->AccountingContact->where('Name', '=', 'Test Name')->first();

如果您要传递的名称和值相等,也可以直接传递

	$xero = new \MacsiDigital\Xero\Xero;
	$contact = $xero->AccountingContact->where('Name', 'Test Name')->get();
	$contact = $xero->AccountingContact->where('Name', 'Test Name')->first();

通过ID查找

与Laravel一样,我们可以使用'find'方法返回ID的单个匹配结果

	$xero = new \MacsiDigital\Xero\Xero;
	$contact = $xero->AccountingContact->find('ID_String');

创建项目

我们可以使用save函数创建和更新记录,以下是一个创建的完整save脚本。请注意,对于如地址之类的多数组项,我们可以添加多个数组项目。

	$contact = $xero->AccountingContact->make([
        'Name' => 'Test Name',
        'FirstName' => 'First Name',
        'LastName' => 'Last Name',
        'EmailAddress' => 'test@test.com',
        'IsCustomer' => true,
    ]);
    $contact->addAddress($xero->AccountingAddress->make([
        'AddressType' => 'POBOX',
        'AddressLine1' => 'Test1',
        'AddressLine2' => 'Test2',
        'AddressLine3' => 'Test3',
        'City' => 'City',
        'PostalCode' => 'Test Code',
        'Country' => 'Test Country',
    ]));
    $contact->save();

示例

这是一个查询联系人、如果找不到则创建,然后创建发票的示例使用案例

	$contact = $xero->AccountingContact->where('name', 'Test Name')->first();
    if($contact == null){
        $contact = $xero->AccountingContact->make([
	        'Name' => 'Test Name',
	        'FirstName' => 'First Name',
	        'LastName' => 'Last Name',
	        'EmailAddress' => 'test@test.com',
	        'IsCustomer' => true,
	    ]);
	    $contact->addAddress($xero->AccountingAddress->make([
	        'AddressType' => 'POBOX',
	        'AddressLine1' => 'Test1',
	        'AddressLine2' => 'Test2',
	        'AddressLine3' => 'Test3',
	        'City' => 'City',
	        'PostalCode' => 'Test Code',
	        'Country' => 'Test Country',
	    ]));
	    $contact->save();
    }
    $invoice = $xero->AccountingInvoice->make([
        'Contact' => $contact,
    ]);
    $invoice->addLineItem($xero->AccountingLineItem->make([
        'Description' => 'Test Description',
        'Quantity' => '1',
        'UnitAmount' => '1234.56',
        'AccountCode' => '200'
    ]));
    $invoice->save();

资源

目前我们有以下资源

  • 账户
  • 联系人
  • 发票
  • 付款
  • 预付款
  • 超额付款
  • 贷项通知

我们计划在未来添加更多资源,但设置额外的模型很简单,以下是一个发票模型的设置。如果您创建了任何模型,请创建一个pull request,我们将将其添加到主存储库中。

	namespace MacsiDigital\Xero;

	use MacsiDigital\Xero\Support\Model;

	class AccountingInvoice extends Model
	{
	    const ENDPOINT = 'Invoices';
	    const NODE_NAME = 'Invoice';
	    const KEY_FIELD = 'InvoiceID';

	    protected $methods = ['get', 'post', 'put'];

	    protected $attributes = [
	    	'Type' => 'ACCREC',
	        'Contact' => '',
	        'LineItems' => [],
	        'Date' => '',
	        'DueDate' => '',
	        'LineAmountTypes' => '',
	        'InvoiceNumber' => '',
	        'Reference' => '',
	        'BrandingThemeID' => '',
	        'Url' => '',
	        'CurrencyCode' => '',
	        'CurrencyRate' => '',
	        'Status' => '',
	        'SentToContact' => '',
	        'ExpectedPaymentDate' => '',
	        'PlannedPaymentDate' => '',
	        'SubTotal' => '',
	        'TotalTax' => '',
	        'Total' => '',
	        'TotalDiscount' => '',
	        'InvoiceID' => '',
	        'HasAttachments' => '',
	        'Payments' => [],
	        'Prepayments' => [],
	        'Overpayments' => [],
	        'AmountDue' => '',
	        'AmountPaid' => '',
	        'FullyPaidOnDate' => '',
	        'AmountCredited' => '',
	        'UpdatedDateUTC' => '',
	        'CreditNotes' => []
	    ];

	    protected $relationships = [
	        'Contact' => '\MacsiDigital\Xero\Models\Accounting\Contact',
	        'LineItems' => '\MacsiDigital\Xero\Models\Accounting\LineItem',
	        'Payments' => '\MacsiDigital\Xero\Models\Accounting\Payment',
	        'Prepayments' => '\MacsiDigital\Xero\Models\Accounting\Prepayment',
	        'Overpayments' => '\MacsiDigital\Xero\Models\Accounting\Overpayment',
	    ];

	    public function addLineItem($item) 
	    {
	        $this->attributes['LineItems'][] = $item;
	    }
	}

测试

目前没有PHP单元测试,但我们计划在未来添加它。

更改日志

有关最近更改的更多信息,请参阅更改日志

贡献

有关详细信息,请参阅贡献指南

安全

如果您发现任何与安全相关的错误,请通过电子邮件colin@macsi.co.uk报告,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件