i3or1s/efakture

电子发票 SEF 库

dev-main 2022-12-30 14:01 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:01:28 UTC


README

使用 SEF API 发送发票的服务列表。

安装

贡献

EFakture 是一个开源库,将保持免费。您可以在 github 上创建问题和拉取请求。

许可

目前,此产品未附加特殊许可。将来会解决这个问题。没有限制或以任何方式干扰库的使用意图。

代码组织

系统依赖于 UBL 标准来创建要发送给 SEF 的对象。
该库依赖的 UBL 实现
所有来自 SEF 的响应都构建在 Model 目录中的对象。目前,将 SEF 的响应结果存储在文件系统中。通过使用 StorageInterface 支持其他类型的存储。
默认情况下,缓存文件夹位于 /tmp/sefStorage 位置。
一些 SEF 响应较大,因此 API 可能需要一些时间来缓存。

UBL 目录包含用于为 SEF 构造 UBL 和减少类实例化复杂性的适配器。

示例用法

每次 API 调用之前的初始设置

$browser = new \React\Http\Browser(); // Used for all the API calls
$apiKey = '*****'; // Obtain key from SEF
$apiService = new \i3or1s\EFakture\Util\EFaktureApi($browser, $apiKey); // Set of available API calls

检索可用单位

$UnitMeasures = new \i3or1s\EFakture\Service\UnitMeasures();
$measures = $UnitMeasures->retrieve($apiService);
print_r($measures);

检索可用公司

$storageManager = new \i3or1s\EFakture\Storage\StorageManager(new \i3or1s\EFakture\Storage\FileSystemStorage(__DIR__.'/cache'));
$companies = new \i3or1s\EFakture\Service\Companies();
$companiesResourceStream = new \i3or1s\EFakture\ResourceStream\FileSystemResourceStream($storageManager->storage, EFakturaAPIRoutes::GET_ALL_COMPANIES->SEFObject());

$listOfCompanies = $companies->retrieve($apiService, $companiesResourceStream, 0,3);
print_r($listOfCompanies);

查找公司

为了使此调用工作,您应该在缓存中生成公司列表。

$storageManager = new \i3or1s\EFakture\Storage\StorageManager(new \i3or1s\EFakture\Storage\FileSystemStorage(__DIR__.'/cache', true));
$companies = new \i3or1s\EFakture\Service\Companies();
$companiesResourceStream = new \i3or1s\EFakture\ResourceStream\FileSystemResourceStream($storageManager->storage, EFakturaAPIRoutes::GET_ALL_COMPANIES->SEFObject());

$listOfCompanies = $companiesResourceStream->getStorageInterface()->seek(EFakturaAPIRoutes::GET_ALL_COMPANIES->SEFObject(), [
    "registrationCode" => "08804516"
]);
print_r($listOfCompanies);

检索免税原因

为了使此调用工作,您应该在缓存中生成公司列表。

$storageManager = new \i3or1s\EFakture\Storage\StorageManager(new \i3or1s\EFakture\Storage\FileSystemStorage(__DIR__.'/cache'));
$taxExemptionListService = new \i3or1s\EFakture\Service\ValueAddedTaxExemptionReasonList();
$taxExemptionResourceStream = new \i3or1s\EFakture\ResourceStream\FileSystemResourceStream($storageManager->storage, EFakturaAPIRoutes::VALUE_ADDED_TAX_EXEMPTION_LIST->SEFObject());
$taxExemptionList = $taxExemptionListService->retrieve($apiService, $taxExemptionResourceStream, 0,1);
print_r($taxExemptionList);

创建并发送发票

注意:为了使此工作,公司数据必须在 SEF 中有效

$invoice = new \i3or1s\EFakture\UBL\Invoice(
    new \i3or1s\EFakture\UBL\InvoiceDetails(
        'invoice number (01/2022)', new DateTimeImmutable(), new DateTimeImmutable('+ 7days'), new \i3or1s\EFakture\UBL\CBC\InvoiceTypeCode(\i3or1s\EFakture\UBL\CBC\InvoiceTypeCode::COMMERCIAL_INVOICE),
        'RSD', new \i3or1s\EFakture\UBL\CAC\InvoicePeriod(\i3or1s\EFakture\UBL\CAC\InvoicePeriod::DELIVERY_ACTUAL_DATE),
        '123456', new DateTimeImmutable('+ 7days')

    ),
    new \i3or1s\EFakture\UBL\Party(
        'company PIB', null, 'company name', null, 'city', 'RS',
        'company Identification', 'e-mail address', true
    ),
    new \i3or1s\EFakture\UBL\Party(
        'company PIB', null, 'company name', 'address', 'city',
        'RS','company Identification', 'e-mail address', true
    ),
    new \i3or1s\EFakture\UBL\PaymentMeans(
        '30', '(mod97) 123456/2022', 'Tekući račun pravnog lica'
    ),
    [
        new \i3or1s\EFakture\UBL\TaxSubtotal(2801.15, 20),
        new \i3or1s\EFakture\UBL\TaxSubtotal(1825.28, 10),
        new \i3or1s\EFakture\UBL\TaxSubtotal(1560, 0, null, 'PDV-RS-11-1-4'),
    ],
    6186.43, 6186.43, 6929.19, 0, 0, 6929.19,
    [
        new \i3or1s\EFakture\UBL\InvoiceLine(5, $unitMeasures['MTR'], 560.23, 'test item 1', 20, 1),
        new \i3or1s\EFakture\UBL\InvoiceLine(4, $unitMeasures['KMT'], 456.32, 'test item 2', 10, 2),
        new \i3or1s\EFakture\UBL\InvoiceLine(1, $unitMeasures['H87'], 1560, 'test item 3', 0, 3),
    ]
);

$invoiceService = new \i3or1s\EFakture\Service\Invoice();
$sefResponse = $invoiceService->send($apiService, $invoice);
print_r($sefResponse);