Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fredwu
Created August 18, 2010 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredwu/533646 to your computer and use it in GitHub Desktop.
Save fredwu/533646 to your computer and use it in GitHub Desktop.
<?php
public static function masspay($data)
{
$ci = get_instance();
$ci->load->library('zip');
$ci->load->helper('download');
// sort by payment_site then payment_job
$sorted_data = $ci->db->select('wufoo_id')
->where_in('wufoo_id', $data['selected'])
->order_by('payment_site')
->order_by('payment_job')
->group_by('wufoo_id')
->get(Wufoo::INVOICE_TABLE)
->result();
$txt_data = array();
foreach ($sorted_data as $record) {
$id = $record->wufoo_id;
$entry = $ci->db->get_where(Wufoo::INVOICE_TABLE, array('wufoo_id' => $id, 'department' => ucfirst($data['department'])))->row();
$reference = htmlspecialchars_decode($entry->payment_site.'|'.$entry->payment_job.'|'.$entry->wufoo_id);
// split the files for different payemnt methods and currencies
$entry_type = "{$entry->payment_method}_{$entry->payment_currency}";
// Paypal and MB have different mass pay file structures
switch ($entry->payment_method) {
case 'Paypal':
$entry_data = array(
$entry->payment_email,
$entry->payment_amount,
$entry->payment_currency,
$reference,
);
break;
case 'Moneybookers':
$entry_data = array(
$entry->payment_email,
$entry->payment_currency,
$entry->payment_amount,
$reference,
);
break;
default:
break;
}
// putting the data together
isset($txt_data[$entry_type]) or $txt_data[$entry_type] = '';
$txt_data[$entry_type] .= implode("\t", $entry_data)."\n";
}
$filename = implode('_', array(
'masspay',
$data['department'],
mktime(),
));
// create a zip, or just output the txt directly?
if (count($txt_data) > 1) {
foreach ($txt_data as $payment_method => $txt_file) {
$ci->zip->add_data(implode('_', array(
'masspay',
$data['department'],
$payment_method,
mktime(),
)).'.txt', $txt_file);
}
$ci->zip->download(implode('_', array(
'masspay',
$data['department'],
mktime(),
)).'.zip');
} else {
force_download(implode('_', array(
'masspay',
$data['department'],
key($txt_data),
mktime(),
)).'.txt', array_shift($txt_data));
}
}
module MassPay
class << self
def build(params)
temp_file = Tempfile.new("mass_pay-#{Time.now.to_i}")
Zip::ZipOutputStream.open(temp_file.path) do |zip|
fetch_entries(params[:ids]).each do |entry_group|
group, entries = entry_group
content = entries.join("\n")
zip.put_next_entry("#{group}.txt")
zip.puts(content)
end
end
temp_file.close
temp_file.path
end
protected
def fetch_entries(invoice_ids)
files = {}
Invoice.where(:id => invoice_ids).all.each do |invoice|
files[:"#{invoice.payment_service.name}_#{invoice.payment_currency.code}"] ||= []
files[:"#{invoice.payment_service.name}_#{invoice.payment_currency.code}"] << format(invoice)
end
files
end
def format(invoice)
format = case invoice.payment_service.name
when "Paypal"
[invoice.payment_email, invoice.amount, invoice.payment_currency.code, reference_text_for(invoice)]
when "Moneybookers"
[invoice.payment_email, invoice.payment_currency.code, invoice.amount, reference_text_for(invoice)]
end
format.join("\t")
end
def reference_text_for(invoice)
[invoice.site.name, invoice.job.name, invoice.id].join("|")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment