PHP code example of jooyeshgar / moadian
1. Go to this page and download the library: Download jooyeshgar/moadian 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/ */
jooyeshgar / moadian example snippets
use Jooyeshgar\Moadian\Facades\Moadian;
// Get server info
$info = Moadian::getServerInfo();
// Get fiscal info
$fiscalInfo = Moadian::getFiscalInfo();
// Get economic code information
$info = Moadian::getEconomicCodeInformation('10840096498');
// Inquiry by reference numbers
$info = Moadian::inquiryByReferenceNumbers('a45aa663-6888-4025-a89d-86fc789672a0');
use Jooyeshgar\Moadian\Invoice as MoadianInvoice;
use Jooyeshgar\Moadian\InvoiceHeader;
use Jooyeshgar\Moadian\InvoiceItem;
use Jooyeshgar\Moadian\Payment;
public function sendInvoice($invoiceId = '') {
$invoiceId = intval($invoiceId);
$invoice = Invoice::find($invoiceId);
if (!$invoice) {
throw new Exception('Invoice not found');
}
$timestamp = Carbon::parse($invoice->date)->timestamp * 1000;
$header = new InvoiceHeader(env('MOADIAN_USERNAME'));
$header->setTaxID(Carbon::parse($invoice->date), $invoice->number);
$header->indati2m = $timestamp;
$header->indatim = $timestamp;
$header->inty = 1; //invoice type
$header->inno = $invoiceId;
$header->irtaxid = null; // invoice reference tax ID
$header->inp = $invoice->inp; //invoice pattern
$header->ins = 1;
$header->tins = env('TAXID');
$header->tob = 2;
$header->bid = $invoice->nationalnum;
$header->tinb = $invoice->nationalnum;
$header->bpc = $invoice->postal;
$amount = $invoice->items->sum('amount');
$discount = $invoice->items->sum('discount');
$vat = $invoice->items->sum('vat');
$header->tprdis = $amount;
$header->tdis = $discount;
$header->tadis = $amount - $discount;
$header->tvam = $vat;
$header->todam = 0;
$header->tbill = $amount - $discount + $vat;
$header->setm = $invoice->setm;
$header->cap = $amount - $discount + $vat;
$moadianInvoice = new MoadianInvoice($header);
foreach ($invoice->items as $item) {
$body = new InvoiceItem();
$body->sstid = $item->seals->sstid;
$body->sstt = $item->desc;
$body->am = '1';
$body->mu = 1627;
$body->fee = $item->amount;
$body->prdis = $item->amount;
$body->dis = $item->discount;
$body->adis = $item->amount - $item->discount;
$body->vra = 9;
$body->vam = $item->vat; // or directly calculate here like floor($body->adis * $body->vra / 100)
$body->tsstam = $item->amount - $item->discount + $item->vat;
$moadianInvoice->addItem($body);
}
foreach ($invoice->cashes as $cashe) {
if ($cashe->active == 1) {
$payment = new Payment();
$payment->trn = $cashe->code;
$payment->pdt = Carbon::parse($cashe->date)->timestamp * 1000;
$moadianInvoice->addPayment($payment);
}
}
$info = Moadian::sendInvoice($moadianInvoice);
$info = $info->getBody();
$info = $info['result'][0];
$invoice->taxID = $header->taxid;
$invoice->uid = $info['uid'] ?? '';
$invoice->referenceNumber = $info['referenceNumber'] ?? '';
$invoice->taxResult = 'send';
$invoice->save();
}