kg-bot/rackbeat-integration-dashboard

Rackbeat 所有集成的仪表盘

v1.0.37 2020-08-19 12:01 UTC

README

1. 在您的集成项目中执行 composer require kg-bot/rackbeat-integration-dashboard

2. 导出配置 php artisan vendor:publish 此包并更改您的连接类

3. 运行迁移

$ php artisan migrate

4. 运行 php artisan make:dashboard-token

5. 将 rackbeat-integration-dashboard/* 添加到您的 VerifyCsrfToken 中间件的 $except 属性

此包要求您在 Rackbeat 和第三方集成之间的所有传输和任务中都要使用 Laravel Jobs。

您的每个作业类都必须扩展 KgBot\RackbeatDashboard\Classes\DashboardJob,并且它必须具有特殊的 __construct 代码。

作业不是直接分发的,相反,您需要创建一个新的 KgBot\RackbeatDashboard\Models\Job 模型,它将自动使用观察者分发作业。

如果您需要任何特殊的构造函数数据,您必须在 Job 模型的 create() 方法中发送它们,它们将被序列化并保存到数据库中,因此您必须从作业的构造函数中使用它们。

这个例子看起来是这样的

// App\Jobs\Webhooks\TransferInvoice.php

<?php

namespace App\Jobs\Webhooks;

use App\Connection;
use App\Transformers\CustomerTransformer;
use App\Transformers\InvoiceTransformer;
use App\Transformers\SupplierTransformer;
use App\Transformers\TransformerInterface;
use Illuminate\Support\Facades\Log;
use KgBot\Billy\Billy;
use KgBot\Billy\Models\Customer;
use KgBot\Billy\Models\Supplier;
use KgBot\RackbeatDashboard\Classes\DashboardJob;
use KgBot\RackbeatDashboard\Models\Job;
use Rackbeat\Utils\Model;

class TransferInvoice extends DashboardJob
{

	/**
	 * @var $conn Connection
	 */
	protected $conn;

	/**
	 * @var $type string
	 */
	protected $type;

	/**
	 * @var $request array
	 */
	protected $request;

	protected $tries = 1;

	protected $timeout = 0;

	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	public function __construct( Job $job, $connection, $request, string $type = 'customer' ) {

		parent::__construct( $job );
		$connection = Connection::find( $connection->id );

		$this->conn    = $connection;
		$this->type    = $type;
		$this->request = (array) $request;
	}

	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function execute() {
		$this->conn->focus();

		// todo Determine is this supplier or customer invoice so we know what integration to check if enabled

		try {

			if ( $this->type === 'customer' ) {
				$invoice = $this->conn->createRackbeatClient()->rb->customer_invoices()->find( $this->request['key'] );

				if ( ! $this->conn->hasDebtorIntegration() ) {
					return;

				}
			} else {

				$invoice = $this->conn->createRackbeatClient()->rb->supplier_invoices()->find( $this->request['key'] );

				if ( ! $this->conn->hasCreditorIntegration() ) {

					return;

				}
			}

			$this->jobModel->updateProgress( 10 );
		} catch ( \Exception $exception ) {

			$this->jobModel->updateProgress( 5 );
			Log::error( 'Can\'t get invoice from webhook: ' . $exception->getMessage() );
			throw $exception;
		}


		$this->createInvoice( $this->conn, $invoice );
		$this->jobModel->updateProgress( 100 );
	}

	/**
	 * @param Connection $connection
	 * @param Model      $invoice
	 *
	 * @return bool
	 */
	protected function createInvoice( Connection $connection, Model $invoice ) {

		$billy    = $connection->createBillyClient();
		$rackbeat = $connection->createRackbeatClient();

		$type      = ( $invoice->getEntity() === 'supplier-invoices' ) ? 'supplier' : 'customer';
		$rb_entity = $type . 's';

		$contact = $this->findOrCreateContact( $rackbeat->rb->{$rb_entity}()->find( $invoice->{$type . '_id'} ), $billy, $type );

		$this->jobModel->updateProgress( 50 );
		$invoice = InvoiceTransformer::rackbeat( $connection, $invoice );

		$invoice['contactId'] = $contact->id;

		if ( $type === 'customer' ) {
			$this->jobModel->updateProgress( 80 );
			$this->createBillyInvoice( $invoice, $billy );
		} else {
			$this->jobModel->updateProgress( 80 );
			$this->createBillyBill( $invoice, $billy );
		}

		return true;
	}

	/**
	 * @param        $contact Model
	 * @param        $billy   Billy
	 * @param string $type    string
	 *
	 * @return Supplier|Customer
	 */
	protected function findOrCreateContact( $contact, $billy, $type = 'supplier' ) {
		switch ( $type ) {

			case 'customer':
				$billy = $billy->customers();
				/**
				 * @var $transformer TransformerInterface
				 */
				$transformer = new CustomerTransformer( $this->conn );
				break;
			default:
				$billy       = $billy->suppliers();
				$transformer = new SupplierTransformer( $this->conn );
		}

		$billy_contact = $billy->get( [
			[ 'contactNo', '=', $contact->number ]
		] )->first();

		if ( ! $billy_contact ) {

			$billy_contact = $billy->create( $transformer->fromRackbeat( $contact ) );
		}

		return $billy_contact;
	}

	/**
	 * @param $invoice
	 * @param $billy Billy
	 */
	protected function createBillyInvoice( $invoice, $billy ) {

		$billy->invoices()->create( $invoice );
	}

	/**
	 * @param $invoice
	 * @param $billy Billy
	 */
	protected function createBillyBill( $invoice, $billy ) {

		$billy->bills()->create( $invoice );
	}
}

这是您从控制器启动此作业的方式

// App\Http\Controllers\Webhooks\InvoiceController.php

<?php

namespace App\Http\Controllers\Webhooks;

use App\Connection;
use App\Jobs\Webhooks\TransferInvoice;
use KgBot\RackbeatDashboard\Models\Job;

class InvoiceController extends Controller
{
	public function test() {

		Job::create( [

			'command'    => TransferInvoice::class,
			'queue'      => 'rackbeat-dashboard',
			'args'       => [ Connection::find( 1 ), [ 'key' => 3001 ], 'customer' ],
			'title'      => 'Transfer Invoice',
			'created_by' => 1,
		] );

		return redirect()->to( '/' );
	}
}

created_by 参数是您创建此模型/作业的连接 ID。

如果您需要更多信息,请随时联系维护者,阅读此代码或阅读 Billy 集成代码,因为所有这些都在那个集成中使用。