include './lib/customer.defines.php';
include './lib/customer.module.access.php';
include './lib/customer.smarty.php';
include './lib/support/classes/invoice.php';
include './lib/support/classes/invoiceItem.php';
if (!has_rights(ACX_INVOICES)) {
    Header("HTTP/1.0 401 Unauthorized");
    Header("Location: PP_error.php?c=accessdenied");
    die;
}
getpost_ifset(array('id'));
if (empty($id)) {
    Header("Location: A2B_entity_invoice.php?atmenu=payment&section=13");
}
$invoice = new invoice($id);
if ($invoice->getCard() != $_SESSION["card_id"]) {
    Header("HTTP/1.0 401 Unauthorized");
    Header("Location: PP_error.php?c=accessdenied");
    die;
}
$items = $invoice->loadItems();
//load customer
$DBHandle = DbConnect();
$card_table = new Table('cc_card', '*');
$card_clause = "id = " . $_SESSION["card_id"];
$card_result = $card_table->Get_list($DBHandle, $card_clause, 0);
$card = $card_result[0];
if (empty($card)) {
    echo "Customer doesn't exist or is not correctly defined for this invoice !";
    die;
}
$payment_assigned = 0;
foreach ($payments as $payment) {
    $payment_assigned = $payment_assigned + $payment['payment'];
}
?>

<SCRIPT LANGUAGE="javascript">
<!--
var win= null;
function addpayment(selvalue) {
    //test si win est encore ouvert et close ou refresh
    win=MM_openBrWindow('A2B_entity_payment_invoice.php?popup_select=1&invoice=<?php 
echo $id;
?>
&card=<?php 
echo $invoice->getCard();
?>
','','scrollbars=yes,resizable=yes,width=700,height=500');
}
function delpayment() {
    //test si val is not null & numeric
    if ($('#payment').val()!=null) {
        self.location.href= "A2B_invoice_manage_payment.php?id=<?php 
echo $id;
?>
&delpayment="+$('#payment').val();
    }
}

function changeStatus() {
    self.location.href= "A2B_invoice_manage_payment.php?id=<?php 
				}
				if(!empty($error_msg)) break;
				$DBHandle = DbConnect();
				$instance_sub_table = new Table("cc_invoice_item", "*");
				$instance_sub_table -> Update_table($DBHandle,"date='$date',description='$description',price='$price',vat='$vat'", "id = $idc" );
				Header ("Location: A2B_invoice_edit.php?"."id=".$id);
				
	 		}
			break;
	}
}


$invoice = new invoice($id);
$table_card = new Table("cc_card","vat");
$result_vat = $table_card->Get_list(DbConnect(),"id=".$invoice->getCard());
$card_vat =  $result_vat[0][0];
$items = $invoice->loadItems();


$smarty->display('main.tpl');

?>
<table class="invoice_table" >
	<tr class="form_invoice_head">
	    <td width="75%"><font color="#FFFFFF"><?php echo gettext("INVOICE: "); ?></font><font color="#FFFFFF"><b><?php echo $invoice->getTitle();  ?></b></font></td>
	    <td width="25%"><font color="#FFFFFF"><?php echo gettext("REF: "); ?> </font><font color="#EE6564"> <?php echo $invoice->getReference(); ?></font></td>
	</tr>
	<tr>
		<td>
		&nbsp;
}


getpost_ifset(array('id','curr'));

if (empty($id)) {
	Header ("Location: A2B_entity_invoice.php?atmenu=payment&section=13");
}


$invoice = new invoice($id);
$items = $invoice->loadItems();
//load customer
$DBHandle  = DbConnect();
$card_table = new Table('cc_card','*');
$card_clause = "id = ".$invoice->getCard();
$card_result = $card_table -> Get_list($DBHandle, $card_clause, 0);
$card = $card_result[0];

if (empty($card)) {
	echo "Customer doesn't exist or is not correctly defined for this invoice !";
	die();
}
$smarty->display('main.tpl');
//Load invoice conf
$invoice_conf_table = new Table('cc_invoice_conf','value');
$conf_clause = "key_val = 'company_name'";
$result = $invoice_conf_table -> Get_list($DBHandle, $conf_clause, 0);
$company_name = $result[0][0];

$conf_clause = "key_val = 'address'";
		$vat_array[$item->getVAT()] =  $item->getPrice()*($item->getVAT()/100) ;
	}
} 
$payment_assigned = 0;
foreach ($payments as $payment) {
	$payment_assigned = $payment_assigned + $payment['payment'];
}

?>

<SCRIPT LANGUAGE="javascript">
<!-- Begin
var win= null;
function addpayment(selvalue){
	//test si win est encore ouvert et close ou refresh
    win=MM_openBrWindow('A2B_entity_payment_invoice.php?popup_select=1&invoice=<?php echo $id ?>&card=<?php echo $invoice->getCard() ?>','','scrollbars=yes,resizable=yes,width=700,height=500');
}
function delpayment(){
	//test si val is not null & numeric
	if($('#payment').val()!=null){
		self.location.href= "A2B_invoice_manage_payment.php?id=<?php echo $id; ?>&delpayment="+$('#payment').val();
	}
}

function changeStatus(){
	self.location.href= "A2B_invoice_manage_payment.php?id=<?php echo $id; ?>&status=<?php echo ($invoice->getPaidStatus()+1)%2; ?>";
}
// End -->

</script>
<table class="invoice_table" >