Skip to content

Instantly share code, notes, and snippets.

@loftx
Created April 8, 2013 21:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loftx/5340660 to your computer and use it in GitHub Desktop.
Save loftx/5340660 to your computer and use it in GitHub Desktop.
PHP example showing generation of an invoice using the Gnucash REST API
<?php
// PHP example showing generation of an invoice using the Gnucash REST API
$rest_url = 'http://192.168.56.101:5000/invoices/000003';
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $rest_url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$invoice = json_decode($result);
function h($value) {
return htmlspecialchars($value);
}
?>
<div style="float: right;">
<p>Your Company Ltd.<br />
Suite 12345<br />
145-157 Your Office<br />
London<br />
EC1 4AB</p>
</div>
<p style="font-weight: bold;">Invoice #<?php print h($invoice->id); ?></p>
<p>Invoice Date: <?php print h(date('d/m/y', strtotime($invoice->date_opened))); ?><br />
Due Date: <?php print h(date('d/m/y', strtotime($invoice->date_due))); ?></p>
<p>
<?php print h($invoice->owner->name); ?><br />
<?php print h($invoice->owner->address->line_1); ?><br />
<?php print h($invoice->owner->address->line_2); ?><br />
<?php print h($invoice->owner->address->line_3); ?><br />
<?php print h($invoice->owner->address->line_4); ?><br />
</p>
<br />
<br />
<table cellspacing="1" cellpadding="2" style="border-collapse:collapse; width: 100%">
<tr>
<td style="border: 1px solid #000000; width: 10%;">Date</td>
<td style="border: 1px solid #000000;">Description</td>
<td style="border: 1px solid #000000; width: 10%; text-align: right;">Quantity</td>
<td style="border: 1px solid #000000; width: 10%; text-align: right;">Unit Price</td>
<td style="border: 1px solid #000000; width: 10%; text-align: right;">Discount</td>
<td style="border: 1px solid #000000; width: 10%; text-align: right;">Total</td>
</tr>
<?php
foreach ($invoice->entries as $entry) {
$line_total_ex_discount = $entry->quantity * $entry->inv_price;
// does not take into account discounts - how do these work?
$line_total_inc_discount = $line_total_ex_discount;
?>
<tr>
<td style="border: 1px solid #000000"><?php print h(date('d/m/y', strtotime($entry->date_entered))); ?></td>
<td style="border: 1px solid #000000"><?php print h($entry->description); ?></td>
<td style="border: 1px solid #000000; text-align: right;"><?php print h($entry->quantity); ?></td>
<td style="border: 1px solid #000000; text-align: right;">&pound;<?php print h(number_format($entry->inv_price, 2)); ?></td>
<td style="border: 1px solid #000000; text-align: right;">&pound;<?php print h(number_format($entry->discount, 2)); ?></td>
<!-- does not take into account discounts -->
<td style="border: 1px solid #000000; text-align: right;">&pound;<?php print h(number_format($line_total_inc_discount, 2)); ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="5" style="border: 1px solid #000000">Subtotal</td>
<td style="border: 1px solid #000000; text-align: right;">&pound;<?php print h(number_format($invoice->total, 2)); ?></td>
</tr>
<tr>
<td colspan="5" style="border: 1px solid #000000">Amount Due</td>
<td style="border: 1px solid #000000; text-align: right;">&pound;<?php print h(number_format($invoice->total, 2)); ?></td>
</tr>
</table>
<br />
<br />
<br />
<br />
<?php print nl2br(h($invoice->notes)); ?>
<br />
<p>Your Company Ltd. is a Company registered in England and Wales. Number 00000000</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment