---- dataentry integration ----
type : integration # do not change this line
supports-foxycart-version-from : 0.6.0 # Minimum required FoxyCart version for this to work
supports-foxycart-version-to : # Last FoxyCart version that supports this (leave empty if unknown)
systems : pdf # the system(s) that the integration is for, separated with commas if more than one
name : Export to PDF # the name of the integration
description : A simple script to be run locally for converting FoxyCart receipts to printable PDFs.
tags_tags : accounting, receipts, print # tags, separated by commas. don't include the "system" here.
date_dt : 2016-04-29 # the date in YYYY-MM-DD format
version : # if you have a version for your code, enter it here
developer_url : # if you'd like a link back to your site, stick it here
----
====== export to pdf ======
//**Please note:**//
The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please [[http://forum.foxycart.com/|post in our forum]], but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.
===== Description =====
This script uses the API to get all your receipt urls and store them in PDFs, one folder per month.
Author: Luke Stokes
Use however you want and take personal responsibility when doing so.
===== Installation =====
- Save the code below as transaction_to_pdf.php
- In the same folder as this script, create a "receipts" folder.
- Make sure your path can run wkhtmltopdf within this folder.
- Make sure you have PHP installed and can run it in cli mode.
- Set a start date for when you want to begin capturing transactions.
- Configure how many months of receipts you want to save.
- Configure your store's API key
- Configure your store's domain (Example: "yourstore.foxycart.com" or if you're using a custom subdomain, "secure.yourstore.com")
- Run the script locally as: php transaction_to_pdf.php
To view how many receipt files were created per month, you can run this code in bash:
for D in `find . -type d`
do
echo "${D}":;
ls "${D}" | wc -l;
done
===== Requirements =====
wkhtmltopdf
http://wkhtmltopdf.org/
(have the binary installed in the same folder as this script)
===== Code =====
add(new DateInterval('P1M'));
$end_date_string = date_format($start_date, 'Y-m-d');
saveReceiptsAsPDF(
$domain,
$api_token,
$start_date_string,
$end_date_string
);
}
function saveReceiptsAsPDF($domain, $api_token, $start_date, $end_date)
{
$folder_name = $start_date . '_to_' . $end_date;
$foxyData = array();
$foxyData["api_token"] = $api_token;
$foxyData["api_action"] = "transaction_list";
$foxyData["transaction_date_filter_begin"] = $start_date;
$foxyData["transaction_date_filter_end"] = $end_date;
$transactions = getTransactions($domain, $foxyData);
$output_folder = './receipts/' . $folder_name;
print "\nMaking folder $output_folder\n";
@mkdir($output_folder);
foreach ($transactions as $transaction) {
print "Saving $transaction->id...\n";
exec('wkhtmltopdf ' . $transaction->receipt_url . ' ' . $output_folder . '/' . $transaction->id . '.pdf');
}
}
function getTransactions($domain, $foxyData, $additional_results = array())
{
$result = $additional_results;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $domain . "/api");
curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = trim(curl_exec($ch));
curl_close($ch);
$resp = new SimpleXMLElement($response);
if ($resp->result == 'SUCCESS') {
foreach($resp->transactions->transaction as $transaction) {
$result[] = $transaction;
}
}
// keep going if we have more data to work with
if ((int)$resp->statistics->filtered_total > (int)$resp->statistics->pagination_end) {
$foxyData['pagination_start'] = (int)$resp->statistics->pagination_end+1;
$result = getTransactions($domain,$foxyData,$result);
}
return $result;
}