2022-11-13 05:12:50 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-11-13 05:12:50 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Transformers;
|
|
|
|
|
|
|
|
use App\Models\BankTransaction;
|
|
|
|
use App\Models\BankTransactionRule;
|
2023-02-07 21:31:24 +01:00
|
|
|
use App\Models\Client;
|
2022-11-13 05:12:50 +01:00
|
|
|
use App\Models\Company;
|
2023-02-07 01:38:54 +01:00
|
|
|
use App\Models\ExpenseCategory;
|
2023-02-07 21:31:24 +01:00
|
|
|
use App\Models\Vendor;
|
2022-11-13 05:12:50 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BankTransactionRuleTransformer.
|
|
|
|
*/
|
|
|
|
class BankTransactionRuleTransformer extends EntityTransformer
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultIncludes = [
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $availableIncludes = [
|
|
|
|
'company',
|
|
|
|
'vendor',
|
|
|
|
'client',
|
|
|
|
'expense_category',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param BankTransaction $bank_integration
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(BankTransactionRule $bank_transaction_rule)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => (string) $this->encodePrimaryKey($bank_transaction_rule->id),
|
|
|
|
'name' => (string) $bank_transaction_rule->name,
|
|
|
|
'rules' => $bank_transaction_rule->rules ?: (array) [],
|
|
|
|
'auto_convert' => (bool) $bank_transaction_rule->auto_convert,
|
|
|
|
'matches_on_all' => (bool) $bank_transaction_rule->matches_on_all,
|
|
|
|
'applies_to' => (string) $bank_transaction_rule->applies_to,
|
|
|
|
'client_id' => $this->encodePrimaryKey($bank_transaction_rule->client_id) ?: '',
|
|
|
|
'vendor_id' => $this->encodePrimaryKey($bank_transaction_rule->vendor_id) ?: '',
|
|
|
|
'category_id' => $this->encodePrimaryKey($bank_transaction_rule->category_id) ?: '',
|
|
|
|
'is_deleted' => (bool) $bank_transaction_rule->is_deleted,
|
|
|
|
'created_at' => (int) $bank_transaction_rule->created_at,
|
|
|
|
'updated_at' => (int) $bank_transaction_rule->updated_at,
|
|
|
|
'archived_at' => (int) $bank_transaction_rule->deleted_at,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeCompany(BankTransactionRule $bank_transaction_rule)
|
|
|
|
{
|
|
|
|
$transformer = new CompanyTransformer($this->serializer);
|
|
|
|
|
|
|
|
return $this->includeItem($bank_transaction_rule->company, $transformer, Company::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeClient(BankTransactionRule $bank_transaction_rule)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$bank_transaction_rule->client) {
|
2023-02-07 01:38:54 +01:00
|
|
|
return null;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-07 01:38:54 +01:00
|
|
|
|
2023-02-07 21:31:24 +01:00
|
|
|
$transformer = new ClientTransformer($this->serializer);
|
|
|
|
|
|
|
|
return $this->includeItem($bank_transaction_rule->client, $transformer, Client::class);
|
2022-11-13 05:12:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function includeVendor(BankTransactionRule $bank_transaction_rule)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$bank_transaction_rule->vendor) {
|
2023-02-07 01:38:54 +01:00
|
|
|
return null;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-07 01:38:54 +01:00
|
|
|
|
2023-02-07 21:31:24 +01:00
|
|
|
$transformer = new VendorTransformer($this->serializer);
|
|
|
|
|
2022-11-13 05:12:50 +01:00
|
|
|
return $this->includeItem($bank_transaction_rule->vendor, $transformer, Vendor::class);
|
|
|
|
}
|
|
|
|
|
2023-02-07 01:38:54 +01:00
|
|
|
public function includeExpenseCategory(BankTransactionRule $bank_transaction_rule)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$bank_transaction_rule->expense_category) {
|
2023-02-07 01:38:54 +01:00
|
|
|
return null;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-07 01:38:54 +01:00
|
|
|
|
2023-02-07 21:31:24 +01:00
|
|
|
$transformer = new ExpenseCategoryTransformer($this->serializer);
|
|
|
|
|
2023-02-07 01:38:54 +01:00
|
|
|
return $this->includeItem($bank_transaction_rule->expense_category, $transformer, ExpenseCategory::class);
|
|
|
|
}
|
2022-11-13 05:12:50 +01:00
|
|
|
}
|