1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 14:42:42 +01:00
invoiceninja/app/controllers/InvoiceController.php

704 lines
21 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2013-12-10 18:18:35 +01:00
use Ninja\Mailers\ContactMailer as Mailer;
2013-11-26 13:45:07 +01:00
class InvoiceController extends \BaseController {
2013-12-10 18:18:35 +01:00
protected $mailer;
public function __construct(Mailer $mailer)
{
parent::__construct();
$this->mailer = $mailer;
}
2013-11-26 13:45:07 +01:00
public function index()
{
2013-12-11 12:11:59 +01:00
$data = [
2013-12-03 23:00:01 +01:00
'title' => '- Invoices',
2013-12-11 12:11:59 +01:00
'entityType'=>ENTITY_INVOICE,
2013-12-11 21:33:44 +01:00
'columns'=>['checkbox', 'Invoice Number', 'Client', 'Invoice Date', 'Invoice Total', 'Balance Due', 'Due Date', 'Status', 'Action']
2013-12-11 12:11:59 +01:00
];
if (Invoice::scope()->where('is_recurring', '=', true)->count() > 0)
2013-12-11 12:11:59 +01:00
{
$data['secEntityType'] = ENTITY_RECURRING_INVOICE;
2013-12-11 21:33:44 +01:00
$data['secColumns'] = ['checkbox', 'Frequency', 'Client', 'Start Date', 'End Date', 'Invoice Total', 'Action'];
2013-12-11 12:11:59 +01:00
}
return View::make('list', $data);
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
public function getDatatable($clientPublicId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-05 16:23:24 +01:00
$query = DB::table('invoices')
->join('clients', 'clients.id', '=','invoices.client_id')
2013-12-05 21:25:20 +01:00
->join('invoice_statuses', 'invoice_statuses.id', '=', 'invoices.invoice_status_id')
->where('invoices.account_id', '=', Auth::user()->account_id)
->where('invoices.deleted_at', '=', null)
2013-12-15 13:55:50 +01:00
->where('clients.deleted_at', '=', null)
->where('invoices.is_recurring', '=', false)
2013-12-05 21:25:20 +01:00
->select('clients.public_id as client_public_id', 'invoice_number', 'clients.name as client_name', 'invoices.public_id', 'total', 'invoices.balance', 'invoice_date', 'due_date', 'invoice_statuses.name as invoice_status_name');
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
2013-12-05 16:23:24 +01:00
$query->where('clients.public_id', '=', $clientPublicId);
2013-11-29 13:09:21 +01:00
}
2013-12-11 21:33:44 +01:00
$filter = Input::get('sSearch');
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('invoices.invoice_number', 'like', '%'.$filter.'%')
->orWhere('invoice_statuses.name', 'like', '%'.$filter.'%');
});
}
2013-12-05 21:25:20 +01:00
$table = Datatable::query($query);
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
2013-11-29 13:09:21 +01:00
}
2013-12-04 17:20:14 +01:00
$table->addColumn('invoice_number', function($model) { return link_to('invoices/' . $model->public_id . '/edit', $model->invoice_number); });
2013-12-01 21:58:25 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
2013-12-05 21:25:20 +01:00
$table->addColumn('client', function($model) { return link_to('clients/' . $model->client_public_id, $model->client_name); });
2013-12-01 21:58:25 +01:00
}
2013-12-11 21:33:44 +01:00
return $table->addColumn('invoice_date', function($model) { return Utils::fromSqlDate($model->invoice_date); })
->addColumn('total', function($model) { return '$' . money_format('%i', $model->total); })
2013-12-05 21:25:20 +01:00
->addColumn('balance', function($model) { return '$' . money_format('%i', $model->balance); })
2013-12-07 21:33:07 +01:00
->addColumn('due_date', function($model) { return Utils::fromSqlDate($model->due_date); })
2013-12-05 21:25:20 +01:00
->addColumn('invoice_status_name', function($model) { return $model->invoice_status_name; })
2013-12-01 21:58:25 +01:00
->addColumn('dropdown', function($model)
{
2013-12-05 21:25:20 +01:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-01 21:58:25 +01:00
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
Select <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
2013-12-04 17:20:14 +01:00
<li><a href="' . URL::to('invoices/'.$model->public_id.'/edit') . '">Edit Invoice</a></li>
2013-12-01 21:58:25 +01:00
<li class="divider"></li>
2013-12-15 13:55:50 +01:00
<li><a href="javascript:archiveEntity(' . $model->public_id . ')">Archive Invoice</a></li>
2013-12-04 17:20:14 +01:00
<li><a href="javascript:deleteEntity(' . $model->public_id . ')">Delete Invoice</a></li>
2013-12-01 21:58:25 +01:00
</ul>
</div>';
})
2013-12-05 21:25:20 +01:00
->orderColumns('invoice_number','client','total','balance','invoice_date','due_date','invoice_status_name')
2013-11-29 13:09:21 +01:00
->make();
2013-11-26 13:45:07 +01:00
}
2013-12-11 12:11:59 +01:00
public function getRecurringDatatable($clientPublicId = null)
{
$query = DB::table('invoices')
->join('clients', 'clients.id', '=','invoices.client_id')
->join('frequencies', 'frequencies.id', '=', 'invoices.frequency_id')
->where('invoices.account_id', '=', Auth::user()->account_id)
->where('invoices.deleted_at', '=', null)
->where('invoices.is_recurring', '=', true)
2013-12-11 12:11:59 +01:00
->select('clients.public_id as client_public_id', 'clients.name as client_name', 'invoices.public_id', 'total', 'frequencies.name as frequency', 'start_date', 'end_date');
if ($clientPublicId) {
$query->where('clients.public_id', '=', $clientPublicId);
}
2013-12-11 21:33:44 +01:00
$filter = Input::get('sSearch');
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('invoices.invoice_number', 'like', '%'.$filter.'%');
});
}
2013-12-11 12:11:59 +01:00
$table = Datatable::query($query);
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
}
2013-12-11 21:33:44 +01:00
$table->addColumn('frequency', function($model) { return link_to('invoices/' . $model->public_id, $model->frequency); });
2013-12-11 12:11:59 +01:00
if (!$clientPublicId) {
$table->addColumn('client', function($model) { return link_to('clients/' . $model->client_public_id, $model->client_name); });
}
2013-12-11 21:33:44 +01:00
return $table->addColumn('start_date', function($model) { return Utils::fromSqlDate($model->start_date); })
2013-12-11 12:11:59 +01:00
->addColumn('end_date', function($model) { return Utils::fromSqlDate($model->end_date); })
2013-12-11 21:33:44 +01:00
->addColumn('total', function($model) { return '$' . money_format('%i', $model->total); })
2013-12-11 12:11:59 +01:00
->addColumn('dropdown', function($model)
{
return '<div class="btn-group tr-action" style="visibility:hidden;">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
Select <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="' . URL::to('invoices/'.$model->public_id.'/edit') . '">Edit Invoice</a></li>
<li class="divider"></li>
2013-12-15 13:55:50 +01:00
<li><a href="javascript:archiveEntity(' . $model->public_id . ')">Archive Invoice</a></li>
2013-12-11 12:11:59 +01:00
<li><a href="javascript:deleteEntity(' . $model->public_id . ')">Delete Invoice</a></li>
</ul>
</div>';
})
->orderColumns('client','total','frequency','start_date','end_date')
->make();
}
2013-11-26 13:45:07 +01:00
2013-12-04 17:20:14 +01:00
public function view($invitationKey)
2013-11-26 13:45:07 +01:00
{
2013-12-15 13:55:50 +01:00
$invitation = Invitation::with('user', 'invoice.account', 'invoice.client', 'invoice.invoice_items', 'invoice.client.account.account_gateways')
2013-12-04 17:20:14 +01:00
->where('invitation_key', '=', $invitationKey)->firstOrFail();
2013-12-02 13:22:29 +01:00
$user = $invitation->user;
2013-12-03 18:32:33 +01:00
$invoice = $invitation->invoice;
2013-12-15 13:55:50 +01:00
if (!$invoice || $invoice->is_deleted) {
return View::make('invoices.deleted');
}
$client = $invoice->client;
if (!$client || $client->is_deleted) {
return View::make('invoices.deleted');
}
2013-12-07 19:45:00 +01:00
if ($invoice->invoice_status_id < INVOICE_STATUS_VIEWED) {
$invoice->invoice_status_id = INVOICE_STATUS_VIEWED;
$invoice->save();
}
2013-12-03 18:32:33 +01:00
$now = Carbon::now()->toDateTimeString();
2013-11-26 22:45:10 +01:00
2013-12-03 18:32:33 +01:00
$invitation->viewed_date = $now;
2013-12-01 21:58:25 +01:00
$invitation->save();
2013-12-03 18:32:33 +01:00
$client = $invoice->client;
$client->last_login = $now;
$client->save();
2013-12-02 13:22:29 +01:00
Activity::viewInvoice($invitation);
2013-11-26 22:45:10 +01:00
2013-12-07 19:45:00 +01:00
$data = array(
'invoice' => $invoice,
'invitation' => $invitation
);
return View::make('invoices.view', $data);
2013-11-26 13:45:07 +01:00
}
private function createGateway($accountGateway)
2013-11-26 13:45:07 +01:00
{
$gateway = Omnipay::create($accountGateway->gateway->provider);
$config = json_decode($accountGateway->config);
/*
$gateway->setSolutionType ("Sole");
$gateway->setLandingPage("Billing");
*/
foreach ($config as $key => $val)
{
if (!$val)
{
continue;
}
2013-11-26 13:45:07 +01:00
$function = "set" . ucfirst($key);
$gateway->$function($val);
}
2013-11-26 13:45:07 +01:00
return $gateway;
}
private function getPaymentDetails($invoice)
{
$data = array(
'firstName' => '',
'lastName' => '',
2013-11-26 13:45:07 +01:00
);
$card = new CreditCard($data);
2013-12-07 19:45:00 +01:00
2013-11-26 13:45:07 +01:00
return [
2013-12-07 19:45:00 +01:00
'amount' => $invoice->total,
2013-11-26 13:45:07 +01:00
'card' => $card,
'currency' => 'USD',
'returnUrl' => URL::to('complete'),
'cancelUrl' => URL::to('/'),
];
}
2013-12-04 17:20:14 +01:00
public function show_payment($invitationKey)
2013-11-26 13:45:07 +01:00
{
2013-12-07 19:45:00 +01:00
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
$invoice = $invitation->invoice;
$accountGateway = $invoice->client->account->account_gateways[0];
$gateway = InvoiceController::createGateway($accountGateway);
2013-11-26 13:45:07 +01:00
try
{
2013-12-07 19:45:00 +01:00
$details = InvoiceController::getPaymentDetails($invoice);
$response = $gateway->purchase($details)->send();
$ref = $response->getTransactionReference();
if (!$ref)
{
var_dump($response);
exit('Sorry, there was an error processing your payment. Please try again later.');
}
2013-11-26 13:45:07 +01:00
2013-12-07 19:45:00 +01:00
$payment = Payment::createNew();
$payment->invitation_id = $invitation->id;
2013-11-26 13:45:07 +01:00
$payment->invoice_id = $invoice->id;
2013-12-07 19:45:00 +01:00
$payment->amount = $invoice->total;
$payment->client_id = $invoice->client_id;
//$payment->contact_id = 0; // TODO_FIX
$payment->transaction_reference = $ref;
2013-11-26 13:45:07 +01:00
$payment->save();
2013-12-07 19:45:00 +01:00
$invoice->balance = floatval($invoice->total) - floatval($paymount->amount);
2013-11-26 13:45:07 +01:00
if ($response->isSuccessful())
{
2013-11-26 13:45:07 +01:00
}
else if ($response->isRedirect())
{
$response->redirect();
}
else
{
2013-11-26 13:45:07 +01:00
}
}
catch (\Exception $e)
{
exit('Sorry, there was an error processing your payment. Please try again later.<p>'.$e);
}
exit;
}
public function do_payment()
{
$payerId = Request::query('PayerID');
$token = Request::query('token');
2013-12-07 19:45:00 +01:00
$payment = Payment::with('invitation', 'invoice.invoice_items')->where('transaction_reference','=',$token)->firstOrFail();
$invoice = Invoice::with('client.account.account_gateways.gateway')->where('id', '=', $payment->invoice_id)->firstOrFail();
$accountGateway = $invoice->client->account->account_gateways[0];
$gateway = InvoiceController::createGateway($accountGateway);
2013-11-26 13:45:07 +01:00
try
{
$details = InvoiceController::getPaymentDetails($payment->invoice);
$response = $gateway->completePurchase($details)->send();
$ref = $response->getTransactionReference();
if ($response->isSuccessful())
{
$payment->payer_id = $payerId;
$payment->transaction_reference = $ref;
$payment->save();
2013-12-07 19:45:00 +01:00
if ($payment->amount >= $invoice->amount) {
$invoice->invoice_status_id = INVOICE_STATUS_PAID;
} else {
$invoice->invoice_status_id = INVOICE_STATUS_PARTIAL;
}
$invoice->save();
2013-11-26 13:45:07 +01:00
Session::flash('message', 'Successfully applied payment');
2013-12-07 19:45:00 +01:00
return Redirect::to('view/' . $payment->invitation->invitation_key);
2013-11-26 13:45:07 +01:00
}
else
{
exit($response->getMessage());
}
}
catch (\Exception $e)
{
exit('Sorry, there was an error processing your payment. Please try again later.' . $e);
}
}
2013-11-28 20:06:38 +01:00
2013-12-04 17:20:14 +01:00
public function edit($publicId)
2013-11-28 20:06:38 +01:00
{
2013-12-04 17:20:14 +01:00
$invoice = Invoice::scope($publicId)->with('account.country', 'client', 'invoice_items')->firstOrFail();
2013-12-07 21:33:07 +01:00
Utils::trackViewed($invoice->invoice_number . ' - ' . $invoice->client->name, ENTITY_INVOICE);
2013-11-29 13:09:21 +01:00
$contactIds = DB::table('invitations')
->join('contacts', 'contacts.id', '=','invitations.contact_id')
->where('invitations.invoice_id', '=', $invoice->id)
->where('invitations.account_id', '=', Auth::user()->account_id)
->where('invitations.deleted_at', '=', null)
->select('contacts.public_id')->lists('public_id');
2013-11-28 20:06:38 +01:00
$data = array(
2013-12-01 21:58:25 +01:00
'account' => $invoice->account,
2013-11-28 20:06:38 +01:00
'invoice' => $invoice,
'method' => 'PUT',
'invitationContactIds' => $contactIds,
'clientSizes' => ClientSize::orderBy('id')->get(),
'clientIndustries' => ClientIndustry::orderBy('name')->get(),
2013-12-04 17:20:14 +01:00
'url' => 'invoices/' . $publicId,
2013-12-03 23:00:01 +01:00
'title' => '- ' . $invoice->invoice_number,
2013-12-09 10:38:49 +01:00
'client' => $invoice->client);
$data = array_merge($data, InvoiceController::getViewModel());
2013-11-28 20:06:38 +01:00
return View::make('invoices.edit', $data);
}
2013-12-04 17:20:14 +01:00
public function create($clientPublicId = 0)
2013-11-26 13:45:07 +01:00
{
$client = null;
2013-12-01 13:22:08 +01:00
$invoiceNumber = Auth::user()->account->getNextInvoiceNumber();
2013-12-03 18:32:33 +01:00
$account = Account::with('country')->findOrFail(Auth::user()->account_id);
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
$client = Client::scope($clientPublicId)->firstOrFail();
2013-12-03 18:32:33 +01:00
}
2013-12-01 21:58:25 +01:00
2013-11-26 13:45:07 +01:00
$data = array(
2013-12-01 21:58:25 +01:00
'account' => $account,
2013-11-26 13:45:07 +01:00
'invoice' => null,
2013-12-01 13:22:08 +01:00
'invoiceNumber' => $invoiceNumber,
2013-11-26 13:45:07 +01:00
'method' => 'POST',
'url' => 'invoices',
'clientSizes' => ClientSize::orderBy('id')->get(),
'clientIndustries' => ClientIndustry::orderBy('name')->get(),
2013-12-03 23:00:01 +01:00
'title' => '- New Invoice',
2013-11-26 13:45:07 +01:00
'client' => $client,
2013-12-09 10:38:49 +01:00
'items' => json_decode(Input::old('items')));
$data = array_merge($data, InvoiceController::getViewModel());
2013-11-26 13:45:07 +01:00
return View::make('invoices.edit', $data);
}
2013-12-11 12:11:59 +01:00
public static function getViewModel()
2013-12-09 10:38:49 +01:00
{
return [
'account' => Auth::user()->account,
'products' => Product::scope()->get(array('product_key','notes','cost','qty')),
'countries' => Country::orderBy('name')->get(),
2013-12-11 21:33:44 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
2013-12-09 10:38:49 +01:00
'frequencies' => array(
1 => 'Weekly',
2 => 'Two weeks',
3 => 'Four weeks',
4 => 'Monthly',
5 => 'Three months',
6 => 'Six months',
7 => 'Annually'
)
];
}
2013-11-26 13:45:07 +01:00
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
return InvoiceController::save();
}
2013-12-04 17:20:14 +01:00
private function save($publicId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-03 18:32:33 +01:00
$action = Input::get('action');
2013-12-07 19:45:00 +01:00
2013-12-05 21:25:20 +01:00
if ($action == 'archive' || $action == 'delete')
2013-12-03 18:32:33 +01:00
{
2013-12-05 21:25:20 +01:00
return InvoiceController::bulk();
2013-12-03 18:32:33 +01:00
}
2013-11-26 13:45:07 +01:00
$input = json_decode(Input::get('data'));
$inputClient = $input->client;
$inputClient->name = trim($inputClient->name);
if (!$inputClient->name) {
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices/create')
->withInput();
2013-11-26 13:45:07 +01:00
} else {
$clientPublicId = $input->client->public_id;
2013-12-04 17:20:14 +01:00
if ($clientPublicId == "-1")
2013-11-26 13:45:07 +01:00
{
2013-12-04 17:20:14 +01:00
$client = Client::createNew();
$contact = Contact::createNew();
2013-12-11 21:33:44 +01:00
$contact->is_primary = true;
2013-11-26 13:45:07 +01:00
}
else
{
2013-12-04 17:20:14 +01:00
$client = Client::scope($clientPublicId)->with('contacts')->firstOrFail();
2013-12-11 21:33:44 +01:00
$contact = $client->contacts()->where('is_primary', '=', true)->firstOrFail();
2013-11-26 13:45:07 +01:00
}
$inputClient = $input->client;
$client->name = trim($inputClient->name);
$client->work_phone = trim($inputClient->work_phone);
$client->address1 = trim($inputClient->address1);
$client->address2 = trim($inputClient->address2);
$client->city = trim($inputClient->city);
$client->state = trim($inputClient->state);
$client->postal_code = trim($inputClient->postal_code);
$client->country_id = $inputClient->country_id ? $inputClient->country_id : null;
2013-12-15 13:55:50 +01:00
$client->notes = trim($inputClient->notes);
$client->client_size_id = $inputClient->client_size_id ? $inputClient->client_size_id : null;
$client->client_industry_id = $inputClient->client_industry_id ? $inputClient->client_industry_id : null;
$client->website = trim($inputClient->website);
$client->save();
$isPrimary = true;
$contacts = [];
$contactIds = [];
$sendInvoiceIds = [];
foreach ($inputClient->contacts as $contact)
{
if (isset($contact->public_id) && $contact->public_id)
{
$record = Contact::scope($contact->public_id)->firstOrFail();
}
else
{
$record = Contact::createNew();
}
2013-11-26 13:45:07 +01:00
$record->email = trim($contact->email);
$record->first_name = trim($contact->first_name);
$record->last_name = trim($contact->last_name);
$record->phone = trim($contact->phone);
$record->is_primary = $isPrimary;
$isPrimary = false;
$client->contacts()->save($record);
$contacts[] = $record;
$contactIds[] = $record->public_id;
if ($contact->send_invoice)
{
$sendInvoiceIds[] = $record->id;
}
2013-12-11 21:33:44 +01:00
}
foreach ($client->contacts as $contact)
{
if (!in_array($contact->public_id, $contactIds))
{
$contact->forceDelete();
}
}
2013-12-11 21:33:44 +01:00
2013-12-04 17:20:14 +01:00
if ($publicId) {
$invoice = Invoice::scope($publicId)->firstOrFail();
2013-11-26 13:45:07 +01:00
$invoice->invoice_items()->forceDelete();
2013-12-10 23:10:43 +01:00
} else {
2013-12-04 17:20:14 +01:00
$invoice = Invoice::createNew();
2013-12-01 21:58:25 +01:00
}
2013-12-10 18:18:35 +01:00
$invoice->client_id = $client->id;
$invoice->discount = $input->discount;
$invoice->invoice_number = trim($input->invoice_number);
$invoice->invoice_date = Utils::toSqlDate($input->invoice_date);
$invoice->due_date = Utils::toSqlDate($input->due_date);
$invoice->is_recurring = $input->is_recurring;
$invoice->frequency_id = $input->frequency_id ? $input->frequency_id : 0;
$invoice->start_date = Utils::toSqlDate($input->start_date);
$invoice->end_date = Utils::toSqlDate($input->end_date);
2013-12-15 13:55:50 +01:00
$invoice->terms = $input->terms;
$invoice->po_number = $input->po_number;
2013-12-11 21:33:44 +01:00
2013-12-04 17:20:14 +01:00
$client->invoices()->save($invoice);
$items = $input->invoice_items;
2013-12-07 19:45:00 +01:00
$total = 0;
2013-12-05 21:25:20 +01:00
foreach ($items as $item)
{
if (!isset($item->cost)) {
$item->cost = 0;
2013-11-26 13:45:07 +01:00
}
2013-12-05 21:25:20 +01:00
if (!isset($item->qty)) {
$item->qty = 0;
2013-12-03 18:32:33 +01:00
}
2013-12-08 14:32:49 +01:00
$total += floatval($item->qty) * floatval($item->cost);
2013-12-07 19:45:00 +01:00
}
if ($action == 'email' && $invoice->invoice_status_id == INVOICE_STATUS_DRAFT)
{
$invoice->invoice_status_id = INVOICE_STATUS_SENT;
$client->balance = $invoice->client->balance + $invoice->total;
$client->save();
}
$invoice->total = $total;
$invoice->save();
foreach ($contacts as $contact)
{
$invitation = Invitation::scope()->whereContactId($contact->id)->whereInvoiceId($invoice->id)->first();
if (in_array($contact->id, $sendInvoiceIds) && !$invitation)
{
$invitation = Invitation::createNew();
$invitation->invoice_id = $invoice->id;
$invitation->contact_id = $contact->id;
$invitation->invitation_key = str_random(20);
$invitation->save();
}
else if (!in_array($contact->id, $sendInvoiceIds) && $invitation)
{
$invitation->forceDelete();
}
}
2013-12-07 19:45:00 +01:00
foreach ($items as $item)
{
2013-12-03 18:32:33 +01:00
if (!$item->cost && !$item->qty && !$item->product_key && !$item->notes)
{
continue;
}
2013-11-26 13:45:07 +01:00
2013-12-01 13:22:08 +01:00
if ($item->product_key)
{
2013-12-07 19:45:00 +01:00
$product = Product::findProductByKey(trim($item->product_key));
2013-12-01 13:22:08 +01:00
if (!$product)
{
2013-12-04 17:20:14 +01:00
$product = Product::createNew();
2013-12-07 19:45:00 +01:00
$product->product_key = trim($item->product_key);
2013-12-01 13:22:08 +01:00
}
2013-12-02 13:22:29 +01:00
/*
2013-12-01 13:22:08 +01:00
$product->notes = $item->notes;
$product->cost = $item->cost;
$product->qty = $item->qty;
2013-12-02 13:22:29 +01:00
*/
2013-12-01 13:22:08 +01:00
$product->save();
}
2013-11-26 13:45:07 +01:00
2013-12-04 17:20:14 +01:00
$invoiceItem = InvoiceItem::createNew();
2013-12-01 13:22:08 +01:00
$invoiceItem->product_id = isset($product) ? $product->id : null;
2013-12-07 19:45:00 +01:00
$invoiceItem->product_key = trim($item->product_key);
$invoiceItem->notes = trim($item->notes);
$invoiceItem->cost = floatval($item->cost);
2013-12-08 14:32:49 +01:00
$invoiceItem->qty = floatval($item->qty);
2013-11-26 13:45:07 +01:00
$invoice->invoice_items()->save($invoiceItem);
}
2013-12-07 19:45:00 +01:00
/*
*/
2013-12-15 13:55:50 +01:00
$message = $clientPublicId == "-1" ? ' and created client' : '';
2013-12-11 21:33:44 +01:00
if ($action == 'clone')
{
return InvoiceController::cloneInvoice($publicId);
}
else if ($action == 'email')
2013-12-10 18:18:35 +01:00
{
$this->mailer->sendInvoice($invoice);
2013-12-10 18:18:35 +01:00
2013-12-15 13:55:50 +01:00
Session::flash('message', 'Successfully emailed invoice'.$message);
2013-11-26 13:45:07 +01:00
} else {
2013-12-15 13:55:50 +01:00
Session::flash('message', 'Successfully saved invoice'.$message);
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
$url = 'invoices/' . $invoice->public_id . '/edit';
2013-11-28 17:40:13 +01:00
return Redirect::to($url);
2013-11-26 13:45:07 +01:00
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function show($publicId)
2013-11-26 13:45:07 +01:00
{
2013-12-04 17:20:14 +01:00
return Redirect::to('invoices/'.$publicId.'/edit');
2013-11-26 13:45:07 +01:00
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function update($publicId)
2013-11-26 13:45:07 +01:00
{
2013-12-04 17:20:14 +01:00
return InvoiceController::save($publicId);
2013-11-26 13:45:07 +01:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
2013-12-01 08:33:17 +01:00
public function bulk()
2013-11-26 13:45:07 +01:00
{
2013-12-01 08:33:17 +01:00
$action = Input::get('action');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2013-12-04 17:20:14 +01:00
$invoices = Invoice::scope($ids)->get();
2013-12-01 08:33:17 +01:00
foreach ($invoices as $invoice) {
2013-12-15 13:55:50 +01:00
if ($action == 'delete') {
$invoice->is_deleted = true;
$invoice->save();
2013-12-01 08:33:17 +01:00
}
2013-12-15 13:55:50 +01:00
$invoice->delete();
2013-12-01 08:33:17 +01:00
}
2013-12-15 13:55:50 +01:00
$message = Utils::pluralize('Successfully '.$action.'d ? invoice', count($invoices));
2013-12-01 08:33:17 +01:00
Session::flash('message', $message);
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices');
}
2013-12-11 21:33:44 +01:00
public static function cloneInvoice($publicId)
{
$invoice = Invoice::with('invoice_items')->scope($publicId)->firstOrFail();
$clone = Invoice::createNew();
foreach (['client_id', 'discount', 'invoice_date', 'due_date', 'is_recurring', 'frequency_id', 'start_date', 'end_date', 'notes'] as $field)
2013-12-11 21:33:44 +01:00
{
$clone->$field = $invoice->$field;
}
if (!$clone->is_recurring)
2013-12-11 21:33:44 +01:00
{
$clone->invoice_number = Auth::user()->account->getNextInvoiceNumber();
}
$clone->save();
foreach ($invoice->invoice_items as $item)
{
$cloneItem = InvoiceItem::createNew();
foreach (['product_id', 'product_key', 'notes', 'cost', 'qty'] as $field)
{
$cloneItem->$field = $item->$field;
}
$clone->invoice_items()->save($cloneItem);
}
Session::flash('message', 'Successfully cloned invoice');
return Redirect::to('invoices/' . $clone->public_id);
}
2013-12-05 21:25:20 +01:00
}