PHP code example of mlevent / fatura

1. Go to this page and download the library: Download mlevent/fatura library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mlevent / fatura example snippets


use Mlevent\Fatura\Gib;

$gib = (new Gib)->setTestCredentials()
                ->login();

echo $gib->getToken();

print_r($gib->getToken());
print_r($gib->getCredentials());

use Mlevent\Fatura\Gib;

$gib = (new Gib)->setCredentials('Kullanıcı Kodu', 'Parola')
                ->login();

echo $gib->getToken();

$gib->login('Kullanıcı Kodu', 'Parola')

$gib->logout();

$gib->setToken('f72b59eac1366d3115d80fa9dc971fc05daa7aaeea2c4715efce537c6d052e0cf0cdcd28db2f5928bf35d9590f6143f8e58bda5a5fb15ab67964905a4363daf0');

use Mlevent\Fatura\Enums\Currency;
use Mlevent\Fatura\Enums\InvoiceType;
use Mlevent\Fatura\Enums\Unit;
use Mlevent\Fatura\Gib;
use Mlevent\Fatura\Models\InvoiceModel;
use Mlevent\Fatura\Models\InvoiceItemModel;

// Fatura detayları
$invoice = new InvoiceModel(
    tarih            : '20/10/2022',       // ☑️ Opsiyonel @string      @default=(dd/mm/yyyy)
    saat             : '23:50:48',         // ☑️ Opsiyonel @string      @default=(hh/mm/ss)
    paraBirimi       : Currency::USD,      // ☑️ Opsiyonel @Currency    @default=Currency::TRY
    dovizKuru        : 18.56,              // ☑️ Opsiyonel @float       @default=0
    faturaTipi       : InvoiceType::Satis, // ☑️ Opsiyonel @InvoiceType @default=InvoiceType::Satis
    vknTckn          : '11111111111',      // ✴️ Zorunlu   @string
    vergiDairesi     : '',                 // ✅ Opsiyonel @string
    aliciUnvan       : '',                 // ✅ Opsiyonel @string
    aliciAdi         : 'Mert',             // ✴️ Zorunlu   @string
    aliciSoyadi      : 'Levent',           // ✴️ Zorunlu   @string
    mahalleSemtIlce  : 'Nilüfer',          // ✴️ Zorunlu   @string
    sehir            : 'Bursa',            // ✴️ Zorunlu   @string
    ulke             : 'Türkiye',          // ✴️ Zorunlu   @string
    adres            : '',                 // ✅ Opsiyonel @string
    siparisNumarasi  : '',                 // ✅ Opsiyonel @string
    siparisTarihi    : '',                 // ✅ Opsiyonel @string
    irsaliyeNumarasi : '',                 // ✅ Opsiyonel @string
    irsaliyeTarihi   : '',                 // ✅ Opsiyonel @string
    fisNo            : '',                 // ✅ Opsiyonel @string
    fisTarihi        : '',                 // ✅ Opsiyonel @string
    fisSaati         : '',                 // ✅ Opsiyonel @string
    fisTipi          : '',                 // ✅ Opsiyonel @string
    zRaporNo         : '',                 // ✅ Opsiyonel @string
    okcSeriNo        : '',                 // ✅ Opsiyonel @string
    binaAdi          : '',                 // ✅ Opsiyonel @string
    binaNo           : '',                 // ✅ Opsiyonel @string
    kapiNo           : '',                 // ✅ Opsiyonel @string
    kasabaKoy        : '',                 // ✅ Opsiyonel @string
    postaKodu        : '',                 // ✅ Opsiyonel @string
    tel              : '',                 // ✅ Opsiyonel @string
    fax              : '',                 // ✅ Opsiyonel @string
    eposta           : '',                 // ✅ Opsiyonel @string
    not              : '',                 // ✅ Opsiyonel @string
);

// Ürün/Hizmetler
$invoice->addItem(
    new InvoiceItemModel(
        malHizmet     : 'Çimento',  // ✴️ Zorunlu   @string
        miktar        : 3,          // ✴️ Zorunlu   @float
        birim         : Unit::M3,   // ☑️ Opsiyonel @Unit @default=Unit::Adet
        birimFiyat    : 1259,       // ✴️ Zorunlu   @float
        kdvOrani      : 18,         // ✴️ Zorunlu   @float
        iskontoOrani  : 25,         // ✅ Opsiyonel @float
        iskontoTipi   : 'Arttırım', // ☑️ Opsiyonel @string @default=İskonto
        iskontoNedeni : '',         // ✅ Opsiyonel @string
    )
);

$gib = (new Gib)->login('333333054', '******');

if ($gib->createDraft($invoice)) {
    echo $invoice->getUuid(); // 04e17398-468d-11ed-b3cb-4ccc6ae28384
}

$gib->logout();

// Fatura detayları
$invoice = new InvoiceModel(
    faturaTipi: InvoiceType::Iade,
    ...
);

// İade faturası için iadeye konu faturalar
$invoice->addReturnItem(
    new InvoiceReturnItemModel(
        faturaNo        : 'GIB2022000001416',
        duzenlenmeTarihi: '31/12/2022'
    )
);

// Fatura detayları
$invoice = new InvoiceModel(
    faturaTipi: InvoiceType::Tevkifat,
    ...
);

// Ürün/Hizmetler
$invoice->addItem(
    new InvoiceItemModel(
        tevkifatKodu: 613, // 613 - Çevre, Bahçe ve Bakım Hizmetleri [KDVGUT-(I/C-2.1.3.2.11)]
        ...
    )
);

print_r(Tax::KDVTevkifat->codes());

Array
(
    [601] => Array
        (
            [rate] => 40
            [name] => Yapım İşleri ile Bu İşlerle Birlikte İfa Edilen Mühendislik-Mimarlık ve Etüt-Proje Hizmetleri [KDVGUT-(I/C-2.1.3.2.1)]
        )
    ...

// Fatura detayları
$invoice = new InvoiceModel(
    faturaTipi: InvoiceType::Istisna,
    ...
);

// Ürün/Hizmetler
$invoice->addItem(
    new InvoiceItemModel(
        gtip: '080810100000',
        ...
    )
);

// Fatura detayları
$invoice = new InvoiceModel(
    faturaTipi: InvoiceType::OzelMatrah,
    ...
);

// Ürün/Hizmetler
$invoice->addItem(
    new InvoiceItemModel(
        ...
        ozelMatrahNedeni: 805, // 805 - Altından Mamül veya Altın İçeren Ziynet Eşyaları İle Sikke Altınların Teslimi
        ozelMatrahTutari: 1250,
    )
);

print_r(InvoiceType::OzelMatrah->reasons());

Array
(
    [801] => Milli Piyango, Spor Toto vb. Oyunlar
    [802] => At yarışları ve diğer müşterek bahis ve talih oyunları
    [803] => Profesyonel Sanatçıların Yer Aldığı Gösteriler, Konserler, Profesyonel Sporcuların Katıldığı Sportif Faaliyetler, Maçlar, Yarışlar ve Yarışmalar
    ...
)

use Mlevent\Fatura\Enums\Unit;
use Mlevent\Fatura\Gib;
use Mlevent\Fatura\Models\ProducerReceiptModel;
use Mlevent\Fatura\Models\ProducerReceiptItemModel;

// Müstahsil Makbuzu Detayları
$producerReceipt = new ProducerReceiptModel(
    tarih       : '20/10/2022',  // ☑️ Opsiyonel @string @default=(dd/mm/yyyy)
    saat        : '23:50:48',    // ☑️ Opsiyonel @string @default=(hh/mm/ss)
    vknTckn     : '11111111111', // ✴️ Zorunlu   @string
    aliciAdi    : 'Walter',      // ✴️ Zorunlu   @string
    aliciSoyadi : 'Bishop',      // ✴️ Zorunlu   @string
    sehir       : '',            // ✅ Opsiyonel @string
    websitesi   : '',            // ✅ Opsiyonel @string
    not         : '',            // ✅ Opsiyonel @string
    teslimTarihi: '',            // ✅ Opsiyonel @string
);

// Ürün/Hizmetler
$producerReceipt->addItem(
    new ProducerReceiptItemModel(
        malHizmet    : 'Yazılım Hizmeti', // ✴️ Zorunlu @string
        miktar       : 3,                 // ✴️ Zorunlu @float
        birim        : Unit::Saat,        // ✴️ Zorunlu @Unit
        birimFiyat   : 100,               // ✴️ Zorunlu @float
        gvStopajOrani: 20                 // ✴️ Zorunlu @int
    )
);

$service = (new Gib(DocumentType::ProducerReceipt))->login('333333054', '******');

if ($service->createDraft($producerReceipt)) {
    echo $producerReceipt->getUuid(); // 04e17398-468d-11ed-b3cb-4ccc6ae28384
}

$service->logout();

use Mlevent\Fatura\Enums\Unit;
use Mlevent\Fatura\Gib;
use Mlevent\Fatura\Models\SelfEmployedReceiptModel;
use Mlevent\Fatura\Models\SelfEmployedReceiptItemModel;

// Serbest Meslek Makbuzu
$selfEmployedReceipt = new SelfEmployedReceiptModel(
    tarih          : '20/10/2022',  // ☑️ Opsiyonel @string   @default=(dd/mm/yyyy)
    saat           : '14:25:34',    // ☑️ Opsiyonel @string   @default=(hh/mm/ss)
    paraBirimi     : Currency::USD, // ☑️ Opsiyonel @Currency @default=Currency::TRY
    dovizKuru      : 18.56,         // ☑️ Opsiyonel @float    @default=0
    vknTckn        : '11111111111', // ✴️ Zorunlu   @string
    aliciAdi       : 'Walter',      // ✴️ Zorunlu   @string
    aliciSoyadi    : 'Bishop',      // ✴️ Zorunlu   @string
    aliciUnvan     : '',            // ✅ Opsiyonel @string
    adres          : '',            // ✅ Opsiyonel @string
    binaAdi        : '',            // ✅ Opsiyonel @string
    binaNo         : '',            // ✅ Opsiyonel @string
    kapiNo         : '',            // ✅ Opsiyonel @string
    kasabaKoy      : '',            // ✅ Opsiyonel @string
    mahalleSemtIlce: '',            // ✅ Opsiyonel @string
    sehir          : '',            // ✅ Opsiyonel @string
    postaKodu      : '',            // ✅ Opsiyonel @string
    ulke           : 'Türkiye',     // ✴️ Zorunlu   @string
    vergiDairesi   : '',            // ✅ Opsiyonel @string
    aciklama       : '',            // ✅ Opsiyonel @string
    kdvTahakkukIcin: false,         // ☑️ Opsiyonel @boolean  @default=false
);

$selfEmployedReceipt->addItem(
    new SelfEmployedReceiptItemModel(
        neIcinAlindigi  : 'Dava Vekilliği', // ✴️ Zorunlu   @string
        brutUcret       : 100,              // ✴️ Zorunlu   @float
        kdvOrani        : 18,               // ✴️ Zorunlu   @float
        gvStopajOrani   : 0,                // ✅ Opsiyonel @int
        kdvTevkifatOrani: 0,                // ✅ Opsiyonel @int
    )
);

$service = (new Gib(DocumentType::SelfEmployedReceipt))->login('333333054', '******');

if ($service->createDraft($producerReceipt)) {
    echo $producerReceipt->getUuid(); // 04e17398-468d-11ed-b3cb-4ccc6ae28384
}

$service->logout();

use Mlevent\Fatura\Models\InvoiceModel;
use Mlevent\Fatura\Models\InvoiceItemModel;

$invoice = new InvoiceModel(...);

$invoice->addItem(
    new InvoiceItemModel(...),
    new InvoiceItemModel(...),
);

$invoice->setNote(number_to_words($invoice->getPaymentTotal())); // ALTI YÜZ OTUZ DÖRT TÜRK LİRASI ALTMIŞ BİR KURUŞ

use Mlevent\Fatura\Enums\Tax;
use Mlevent\Fatura\Models\InvoiceItemModel;

$invoiceItem = new InvoiceItemModel(
    malHizmet  : 'Çimento',
    birim      : Unit::Ton,
    miktar     : 3,
    birimFiyat : 1259,
    kdvOrani   : 18,
);

// Hizmete vergi ekleme
$invoiceItem->addTax(Tax::Damga,    15)  // %15 damga vergisi
            ->addTax(Tax::GVStopaj, 25); // %25 gelir vergisi

// Vergi kodu kullanarak vergi ekleme
$invoiceItem->addTax(Tax::from(1047), 15); // %15 damga vergisi

use Mlevent\Fatura\Enums\Tax;

// Vergiler
foreach (Tax::cases() as $tax) {
    echo $tax->value;   // 4071
    echo $tax->name;    // ElkHavagazTuketim
    echo $tax->alias(); // Elektrik Havagaz Tüketim Vergisi
}

use Mlevent\Fatura\Models\InvoiceModel;
use Mlevent\Fatura\Models\InvoiceItemModel;

$invoice = new InvoiceModel(...);

$invoice->addItem(
    new InvoiceItemModel(...),
    new InvoiceItemModel(...),
);

// Her bir öğeye ait vergiler
foreach ($invoice->getItems() as $item) {

    // Öğeye eklenen vergiler toplamı
    print_r($item->totalTaxAmount());

    // Öğeye eklenen vergilere ait kdv toplamı
    print_r($item->totalTaxVat());

    // Öğeye eklenen vergiler
    print_r($item->getTaxes());

    // Öğeye ait toplamlar
    print_r($item->getTotals());
}

// Belgeye ait vergiler
print_r($invoice->getTaxes());

// Belgeye ait toplamlar
print_r($invoice->getTotals());

// Fatura detayları
$invoice = new InvoiceModel(
    uuid          : '04e17398-468d-11ed-b3cb-4ccc6ae28384',
    belgeNumarasi : 'GIB2022000000003',
    ...
);

use Mlevent\Fatura\Gib;
use Mlevent\Fatura\Models\InvoiceModel;

$gib = (new Gib)->login('333333054', '******');

$invoice = InvoiceModel::importFromApi(
    $gib->getDocument('c4e9e0a2-4788-11ed-bbd4-4ccc6ae28384')
);

$invoice->aliciAdi    = 'Nureddin';
$invoice->aliciSoyadi = 'Nebati';
$invoice->adres       = 'Bankalar Cd. Faiz Sk. No:128/A';

// Faturayı güncelle
if ($gib->createDraft($invoice)) {
    echo $invoice->getUuid(); // c4e9e0a2-4788-11ed-bbd4-4ccc6ae28384
}

$fetchToDelete = $gib->selectColumn('ettn')
                     ->onlyUnsigned()
                     ->getAll('10/10/2022', '10/15/2022');

if ($gib->deleteDraft($fetchToDelete)) {
    echo "{$gib->rowCount()} adet belge silindi!"; // x adet belge silindi
}

$setToDelete = [
    '90559052-8bd0-4f68-a733-12157cf53cfb',
    '521ce2b1-290c-45fa-8312-d455672289ef',
];

if ($gib->deleteDraft($setToDelete)) {
    echo "{$gib->rowCount()} adet belge silindi!"; // 2 adet belge silindi
}

$operationId = $gib->startSmsVerification();

// Portaldan belirli bir tarih aralığındaki tüm onaysız belgeleri getir
$setToSign = $gib->selectColumn('ettn')
                 ->onlyUnsigned()
                 ->getAll('01/10/2022', '15/10/2022');

// Onaylanacak belgelere ait UUID'leri kendiniz de belirtebilirsiniz
$setToSign = [
    '2e989428-63ca-11ed-b617-4ccc6ae28384',
    '54c5df01-038b-4e01-973d-cd31e4a547f3',
];

// Belgeleri onayla
if ($gib->completeSmsVerification($smsCode, $operationId, $setToSign)) {
    echo "{$gib->rowCount()} adet belge onaylandı!"; // x adet belge onaylandı
}

$documents = $gib->getAll('01/09/2022', '15/09/2022');

Array
(
    [0] => Array
        (
            [belgeNumarasi] => GIB2022000000356
            [aliciVknTckn] => 11111111111
            [aliciUnvanAdSoyad] => Mert Levent
            [belgeTarihi] => 09-10-2022
            [belgeTuru] => FATURA
            [onayDurumu] => Onaylanmadı
            [ettn] => c4e9e0a2-4788-11ed-bbd4-4ccc6ae28384
        )
    ...
)

$documents = $gib->getAllIssuedToMe('01/09/2022', '15/09/2022');

$gib->getDocument('6115993e-3e77-473c-8ea5-c24036b4106c');

$gib->getLastDocument();

$gib->getHtml('1d78ef40-6491-11ed-a280-4ccc6ae28384');

$gib->getDownloadURL('44ba5b87-81a3-4474-bd0f-27d771fb4064');

$gib->saveToDisk('44ba5b87-81a3-4474-bd0f-27d771fb4064');

$documents = $gib->onlyUnsigned()
                 ->findRecipientName('mehmet')
                 ->getAll('01/09/2022', '15/09/2022');

$requests = $gib->getRequests('07/12/2020', '07/11/2022');

use Mlevent\Fatura\Enums\ObjectionMethod;
use Mlevent\Fatura\Gib;

// Portal Bağlantısı
$gib = (new Gib)->setTestCredentials()
                ->login();

// İtiraz Talebi
$gib->objectionRequest(
    objectionMethod : ObjectionMethod::Kep,
    uuid            : '94d0d436-d91d-40c0-a238-e335f29b8275',
    documentId      : 'GIB2020000000218',
    documentDate    : '23/11/2020',
    explanation     : 'Hatalı İşlem'
);

// İptal Talebi
$gib->cancellationRequest(
    uuid        : '94d0d436-d91d-40c0-a238-e335f29b8275',
    explanation : 'Hatalı İşlem'
);

$userData = $gib->getUserData();

use Mlevent\Fatura\Gib;
use Mlevent\Fatura\Models\UserDataModel;

$gib = (new Gib)->setTestCredentials()
                ->login();

$userData = UserDataModel::import($gib->getUserData());

$userData->apartmanAdi = 'Lale Apartmanı';
$userData->kapiNo      = '12';

if ($gib->updateUserData($userData)) {
    // Bilgileriniz başarıyla güncellendi.
}

$recipientData = $gib->getRecipientData('2920084496');

Array
(
    [unvan] => DENİZBANK ANONİM ŞİRKETİ
    [adi] =>
    [soyadi] =>
    [vergiDairesi] => Büyük Mükellefler VD. BAŞKANLIĞI
)

use Mlevent\Fatura\Enums\Unit;

foreach (Unit::cases() as $unit) {
    echo $unit->name;    // Dk
    echo $unit->value;   // D61
    echo $unit->alias(); // Dakika
}

use Mlevent\Fatura\Enums\Tax;

foreach (Tax::cases() as $tax) {
    echo $tax->name;    // BankaMuameleleri
    echo $tax->value;   // 0021
    echo $tax->alias(); // Banka Muameleleri Vergisi
}

use Mlevent\Fatura\Enums\Currency;

foreach (Currency::cases() as $currency) {
    echo $currency->name;    // TRY
    echo $currency->value;   // TRY
    echo $currency->alias(); // Türk Lirası
}