1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/commands/SendRecurringInvoices.php

112 lines
3.4 KiB
PHP
Raw Normal View History

2013-12-09 10:38:49 +01:00
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
2013-12-25 22:34:42 +01:00
use ninja\mailers\ContactMailer as Mailer;
2013-12-09 10:38:49 +01:00
class SendRecurringInvoices extends Command {
protected $name = 'ninja:send-invoices';
protected $description = 'Send recurring invoices';
2013-12-10 18:18:35 +01:00
protected $mailer;
2013-12-09 10:38:49 +01:00
2013-12-10 18:18:35 +01:00
public function __construct(Mailer $mailer)
2013-12-09 10:38:49 +01:00
{
parent::__construct();
2013-12-10 18:18:35 +01:00
$this->mailer = $mailer;
2013-12-09 10:38:49 +01:00
}
public function fire()
{
2013-12-10 18:18:35 +01:00
$this->info(date('Y-m-d') . ' Running SendRecurringInvoices...');
2014-06-04 22:41:34 +02:00
$today = new DateTime();
$invoices = Invoice::with('account.timezone', 'invoice_items', 'client')
2014-05-26 11:43:46 +02:00
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', array($today, $today))->get();
2013-12-10 18:18:35 +01:00
$this->info(count($invoices) . ' recurring invoice(s) found');
foreach ($invoices as $recurInvoice)
{
2014-06-04 22:41:34 +02:00
if ($recurInvoice->client->deleted_at)
{
continue;
}
2014-01-09 20:00:08 +01:00
date_default_timezone_set($recurInvoice->account->getTimezone());
2013-12-10 18:18:35 +01:00
$this->info('Processing Invoice ' . $recurInvoice->id . ' - Should send ' . ($recurInvoice->shouldSendToday() ? 'YES' : 'NO'));
2014-01-09 14:44:55 +01:00
2013-12-10 18:18:35 +01:00
if (!$recurInvoice->shouldSendToday())
{
continue;
}
2014-01-09 12:24:03 +01:00
$invoice = Invoice::createNew($recurInvoice);
2013-12-10 18:18:35 +01:00
$invoice->client_id = $recurInvoice->client_id;
2013-12-10 23:10:43 +01:00
$invoice->recurring_invoice_id = $recurInvoice->id;
2014-01-06 19:03:00 +01:00
$invoice->invoice_number = 'R' . $recurInvoice->account->getNextInvoiceNumber();
2013-12-30 21:17:45 +01:00
$invoice->amount = $recurInvoice->amount;
2014-01-09 11:05:35 +01:00
$invoice->balance = $recurInvoice->amount;
2014-01-09 12:15:19 +01:00
$invoice->invoice_date = date_create()->format('Y-m-d');
2014-05-01 19:20:52 +02:00
$invoice->discount = $recurInvoice->discount;
$invoice->po_number = $recurInvoice->po_number;
$invoice->public_notes = $recurInvoice->public_notes;
$invoice->terms = $recurInvoice->terms;
$invoice->tax_name = $recurInvoice->tax_name;
$invoice->tax_rate = $recurInvoice->tax_rate;
$invoice->invoice_design_id = $recurInvoice->invoice_design_id;
2014-01-09 11:05:35 +01:00
if ($invoice->client->payment_terms)
{
2014-01-09 12:15:19 +01:00
$invoice->due_date = date_create()->modify($invoice->client->payment_terms . ' day')->format('Y-m-d');
2014-01-09 11:05:35 +01:00
}
2013-12-10 18:18:35 +01:00
$invoice->save();
2014-01-09 13:37:38 +01:00
2013-12-10 18:18:35 +01:00
foreach ($recurInvoice->invoice_items as $recurItem)
{
$item = InvoiceItem::createNew($recurItem);
$item->product_id = $recurItem->product_id;
$item->qty = $recurItem->qty;
$item->cost = $recurItem->cost;
$item->notes = Utils::processVariables($recurItem->notes);
$item->product_key = Utils::processVariables($recurItem->product_key);
2014-05-01 19:20:52 +02:00
$item->tax_name = $recurItem->tax_name;
$item->tax_rate = $recurItem->tax_rate;
2014-01-09 11:05:35 +01:00
$invoice->invoice_items()->save($item);
}
foreach ($recurInvoice->invitations as $recurInvitation)
{
$invitation = Invitation::createNew($recurInvitation);
$invitation->contact_id = $recurInvitation->contact_id;
2014-01-13 20:22:43 +01:00
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
2014-01-09 11:05:35 +01:00
$invoice->invitations()->save($invitation);
2013-12-10 18:18:35 +01:00
}
$this->mailer->sendInvoice($invoice);
2014-05-01 21:02:36 +02:00
$recurInvoice->last_sent_date = Carbon::now()->toDateTimeString();
$recurInvoice->save();
2013-12-10 18:18:35 +01:00
}
2013-12-09 10:38:49 +01:00
$this->info('Done');
}
protected function getArguments()
{
return array(
//array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
protected function getOptions()
{
return array(
//array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}