89 lines
2.6 KiB
PHP
89 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\OrderingBundle\Util\Invoice;
|
|
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\OrderingBundle\Entity\Invoicing\BankDetails;
|
|
use KupShop\OrderingBundle\Entity\Invoicing\InvoicingData;
|
|
use PHP_IBAN\IBAN;
|
|
|
|
class InvoicingUtil
|
|
{
|
|
public function getInvoicingData(): InvoicingData
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$invoicingFields = ['shop_firm_owner', 'shop_email', 'shop_phone', 'shop_ico', 'shop_dic', 'shop_address'];
|
|
|
|
$userContext = Contexts::get(UserContext::class);
|
|
|
|
$data = (array) $dbcfg;
|
|
// user is B2B, so use invoicing data from b2b settings
|
|
if ($userContext->isDealer() || $userContext->isType('b2b')) {
|
|
$data = array_replace($data, array_filter($dbcfg->b2b['invoicing'] ?? []));
|
|
}
|
|
|
|
$invoicingData = [];
|
|
// prepare $invoicingData
|
|
foreach ($invoicingFields as $field) {
|
|
$key = lcfirst(str_replace('_', '', ucwords(str_replace('shop_', '', $field), '_')));
|
|
$invoicingData[$key] = !empty($data[$field]) ? $data[$field] : null;
|
|
}
|
|
|
|
return new InvoicingData(...$invoicingData);
|
|
}
|
|
|
|
public function getBankDetails(): BankDetails
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$bankDetails = [];
|
|
|
|
if (!$dbcfg->bank_account_number && !$dbcfg->bank_iban) {
|
|
$bankDetails = $this->getBankAccountFromCurrency();
|
|
|
|
return new BankDetails(...$bankDetails);
|
|
}
|
|
|
|
$bankDetails['account'] = ($dbcfg->bank_account_prefix ? $dbcfg->bank_account_prefix.'-' : '').$dbcfg->bank_account_number.'/'.$dbcfg->bank_code;
|
|
$bankDetails['iban'] = $dbcfg->bank_iban;
|
|
$bankDetails['swift'] = $dbcfg->bank_swift;
|
|
|
|
return new BankDetails(...$bankDetails);
|
|
}
|
|
|
|
private function getBankAccountFromCurrency(): ?array
|
|
{
|
|
$currencyContext = Contexts::get(CurrencyContext::class);
|
|
|
|
$currency = $currencyContext->getActive();
|
|
$data = $currency->getData();
|
|
|
|
if (!$data['bank_iban']) {
|
|
return [
|
|
'account' => '',
|
|
'iban' => '',
|
|
'swift' => '',
|
|
];
|
|
}
|
|
|
|
$iban = new IBAN($data['bank_iban']);
|
|
|
|
$account = intval($iban->Account()).'/'.$iban->Bank();
|
|
|
|
if (intval($iban->Branch()) > 0) {
|
|
$account = intval($iban->Branch()).'-'.$account;
|
|
}
|
|
|
|
return [
|
|
'account' => $account,
|
|
'iban' => $data['bank_iban'],
|
|
'swift' => $data['bank_swift'],
|
|
];
|
|
}
|
|
}
|