This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/invoice/classes/Task/Invoice/Email.php
2014-05-20 21:50:23 +10:00

66 lines
1.7 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This task emails invoices.
*
* @package Invoice
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Invoice_Email extends Minion_Task {
protected $_options = array(
'force'=>NULL, // Force emailing if already sent
'id'=>NULL, // Invoice to email
);
protected function _execute(array $params) {
// Used to only process X invoices in a row.
$max = ORM::factory('Invoice')->config('EMAIL_INV_MAX');
$iids = $params['id'] ? explode(':',$params['id']) : array();
$max_count = 0;
$action = array();
// Get our list of invoices to send
$i = $iids ? ORM::factory('Invoice')->where('id','IN',$iids) : ORM::factory('Invoice')->list_tosend();
$key = 'send';
foreach ($i->find_all() as $io) {
// If we have already sent a reminder or we dont email invoices we'll skip to the next one.
if (($io->remind($key) AND (is_null($params['force']) OR $params['force'] != 'again')) OR ($io->account->invoice_delivery != 1))
continue;
// If we have issued the max number of invoices this round, finish.
if (++$max_count > $max)
break;
// Log the emailling
$imo = $io->invoice_memo;
$imo->invoice_id = $io->id;
$imo->type = 'email';
if (Invoice::instance($io)->render('email','all')) {
$imo->memo = 'Invoice Emailed.';
array_push($action,(string)$io);
} else {
$imo->memo = 'Invoice Send Failed.';
}
$imo->save();
$io->print_status = 1;
$io->set_remind($key,time(),($params['force']=='again' ? TRUE : FALSE));
$io->save();
}
return _('Invoiced emailed: ').join('|',$action);
}
}
?>