2020-08-14 13:36:36 +02:00

187 lines
4.7 KiB
PHP

<?php
class InvfoxAPI {
var $api;
public function __construct($apitoken, $apidomain, $debugMode=false) {
$this->api = new StrpcAPI($apitoken, $apidomain, $debugMode);
}
function setDebugHook($hook) {
$this->api->debugHook = $hook;
}
function assurePartner($data) {
$res = $this->api->call('partner', 'assure', $data);
if ($res->isErr()) {
echo 'error' . $res->getErr();
}
return $res;
}
function createInvoice($header, $body) {
$res = $this->api->call('invoice-sent', 'insert-smart', $header);
if ($res->isErr()) {
echo 'error' . $res->getErr();
}
else {
foreach ($body as $bl) {
$resD = $res->getData();
//print_r($resD);
$bl['id_invoice_sent'] = $resD[0]['id'];
$res2 = $this->api->call('invoice-sent-b', 'insert-into', $bl);
if ($res2->isErr()) {
echo 'error' . $res->getErr();
}
}
}
return $res;
}
/*
$header = array('title','date_sent','days_valid','id_partner','pub_notes','date_served','date_to_pay','date_payed','vat_level', 'taxnum');
$body = array(
array('title','qty','mu','price','vat','discount'),
...
)
*/
function createProFormaInvoice($header, $body) {
$res = $this->api->call('preinvoice', 'insert-smart', $header);
// print_r($res);
if ($res->isErr())
echo 'error' . $res->getErr();
else {
foreach ($body as $bl) {
$resD = $res->getData();
$bl['id_preinvoice'] = $resD[0]['id'];
$res2 = $this->api->call('preinvoice-b', 'insert-into', $bl);
if ($res2->isErr()) {
echo 'error' . $res->getErr();
}
}
}
return $res;
}
/*
- date format dd.mm.yyyy
$head = array('id','date_sent','date_to_pay','date_served')
*/
function invoiceFromProforma($head){
$res = $this->api->call('preinvoice', 'make-invoice-from', $head);
if ($res->isErr())
echo 'error' . $res->getErr();
return $res;
}
function createInventorySale($header, $body) {
$res = $this->api->call('transfer', 'insert-smart', $header);
// print_r($res);
if ($res->isErr()) {
echo 'error' . $res->getErr();
}
else {
foreach ($body as $bl) {
$resD = $res->getData();
//print_r($resD);
$bl['id_transfer'] = $resD[0]['id'];
$res2 = $this->api->call('transfer-b', 'insert-into', $bl);
if ($res2->isErr()) {
echo 'error' . $res->getErr();
}
}
}
return $res;
}
function downloadPDF($id, $res, $lang='si') {
// $res - invoice-sent / preinvoice / transfer
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Authorization: Basic ".base64_encode($this->api->apitoken.':x')."\r\n"
)
);
//echo "https://{$this->api->domain}/API-pdf?id=$id&res=invoice-sent&format=pdf&doctitle=Invoice%20No.&lang=en&res=invoice-sent";
$context = stream_context_create($opts);
if($res == 'invoice-sent'){
// Popravimo doctitle glede na jezik
$doc_title = ($lang == 'si') ? 'Račun%20št.' : 'Invoice%20no.';
$data = file_get_contents("https://{$this->api->domain}/API-pdf?id=$id&res={$res}&format=PDF&doctitle=".$doc_title."&lang=".$lang, false, $context);
}
else{
// Popravimo doctitle glede na jezik
$doc_title = ($lang == 'si') ? 'Predračun%20št.' : 'Pro%20forma%20invoice%20no.';
$data = file_get_contents("https://{$this->api->domain}/API-pdf?id=$id&res={$res}&format=PDF&doctitle=".$doc_title."&lang=".$lang, false, $context);
}
if ($data === false){
echo 'error downloading PDF';
}
else {
if($res=='invoice-sent')
$file = SITE_ROOT.MAPA_RACUNI.'1ka_racun_'.$id.".pdf";
else
$file = SITE_ROOT.MAPA_PREDRACUNI.'1ka_predracun_'.$id.".pdf";
file_put_contents($file, $data);
}
}
function markPayed($header) {
$res = $this->api->call('invoice-sent-p', 'insert-into', $header);
if ($res->isErr())
echo 'error' . $res->getErr();
}
function fiscalize($header){
$res = $this->api->call('invoice-sent', 'finalize-invoice', $header);
if ($res->isErr())
echo 'error' . $res->getErr();
}
function finalizeInvoiceNonFiscal($header){
$res = $this->api->call('invoice-sent', 'finalize-invoice-2015', $header);
if ($res->isErr()) {
echo 'error' . $res->getErr();
}
else {
$resD = $res->getData();
return $resD;
}
}
function _toUSDate($d) {
if (strpos($d, "-") > 0) {
$da = explode(" ", $d);
$d1 = explode("-", $da[0]);
return $d1[1]."/".$d1[2]."/".$d1[0];
} else {
return $d;
}
}
function _toSIDate($d) {
if (strpos($d, "-") > 0) {
$da = explode(" ", $d);
$d1 = explode("-", $da[0]);
return $d1[2].".".$d1[1].".".$d1[0];
}
else {
return $d;
}
}
}
?>