<?php
ini_set('max_execution_time', 0); 
$dir = '../inc/';
require_once $dir."database.php";
require_once $dir."DW_class.php";
$dw = new DWDB();
include "BizimHesap/EFatura.php";
$id = $_GET["id"];
$veri = array(
   'fatura' => 1
);
$guncelle = $dw->update('siparisler', $veri, array('id = ?'), array($id), array('configKey' => 'secondaryDB'));
$siparisbilgi = $dw->getRow('SELECT * FROM siparisler WHERE id = ?', array('bindValues' => array($id)));
$musteribilgi = $dw->getRow('SELECT * FROM kullanicilar WHERE id = ?', array('bindValues' => array($siparisbilgi->uyeid)));
$faturabilgi = $dw->getRow('SELECT * FROM adresler WHERE id = ?', array('bindValues' => array($siparisbilgi->fadres)));
$sehirbilgi = $dw->getRow('SELECT * FROM sehir WHERE id = ?', array('bindValues' => array($faturabilgi->sehir)));
$ilcebilgi = $dw->getRow('SELECT * FROM ilceler WHERE id = ?', array('bindValues' => array($faturabilgi->ilce)));
$geneltutar = $siparisbilgi->tutar;
$kdvhesap = $siparisbilgi->tutar / (1 + (8/100));
$kdvtutar = $siparisbilgi->tutar - $kdvhesap;
$kdvharic = $siparisbilgi->tutar - $kdvtutar;
$kdvtutar = number_format($kdvtutar, 2, ',' , '');
$kdvharic = number_format($kdvharic, 2, ',' , '');
$siparisno = $siparisbilgi->siparisno;
$musteriid = "300".$musteribilgi->id;
$musteriad = $faturabilgi->unvan;
$musterimail = $musteribilgi->kullanici;
$musteritel = $faturabilgi->ceptel;
$musteriadres = $faturabilgi->adres." ".$sehirbilgi->sehir." ".$ilcebilgi->ilce;
$musterisehir = $ilcebilgi->sehir;
$musteriilce = $ilcebilgi->ilce;
$musteritckimlik = $faturabilgi->tckimlik;
$efatura = new EFatura();
$efatura->setFirmId( "55FE8C51DEC24FFF83C8083302F115CF" ); 
$efatura->setInvoiceNo('');
$efatura->addNote($siparisno.' Nolu Sipariş');
$efatura->setInvoiceDate(time());
$efatura->setDueDate(time());
$efatura->setDeliveryDate(time());
$efatura->setCustomerId($musteriid);
$efatura->setCustomerFullName($musteriad);
$efatura->setCustomerEmail($musterimail);
$efatura->setCustomerPhone($musteritel);
$efatura->setCustomerAddress($musteriadres);
$efatura->setCustomerTaxOffice($musteriilce);
$efatura->setCustomerTaxNo($musteritckimlik);
$efatura->setAmountCurrency('TL');
$efatura->setAmountGross($kdvharic); 
$efatura->setAmountDiscount(0.00);
$efatura->setAmountNet($kdvharic);
$efatura->setAmountTax(0.8);
$efatura->setAmountTotal($geneltutar);
$sipurunler = $dw->getResults("SELECT * FROM siparis_urun where siparisid = ".$siparisbilgi->id." ");
foreach ($sipurunler as $sipurun) {
$urunbilgi = $dw->getRow('SELECT * FROM urunler WHERE id = ?', array('bindValues' => array($sipurun->urunid)));
	$efatura->addProduct(array(
		"Id"       => $sipurun->id,
		"name"     => $urunbilgi->urun,
		"note"     => "",
		"barcode"  => $urunbilgi->kod,
		"taxrate"  => 8.00,
		"count"    => $sipurun->adet,
		"price"    => $kdvharic,
		"gross"    => $geneltutar,
		"discount" => 0.00,
		"net"      => $geneltutar,
		"tax"      => 0.8,
		"total"    => $geneltutar
	));
}
$response = $efatura->sendInvoice();
if ($response['error']) {
    echo "<pre>";
    print_r( $response );
    echo "</pre>";
	echo 'Başarısız: ' . $response['error'];
}else{
	echo 'Fatura bizim hesapta oluşturuldu.';
	exit;
}
?>
