1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/controllers/InvoiceController.php

417 lines
11 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
class InvoiceController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//$invoices = Invoice::with('client')->orderBy('created_at', 'DESC')->get();
2013-12-01 08:33:17 +01:00
//return View::make('invoices.index');
return View::make('list', array(
'entityType'=>ENTITY_INVOICE,
'columns'=>['checkbox', 'Invoice Number', 'Client', 'Total', 'Amount Due', 'Invoice Date', 'Due Date', 'Status']
));
2013-11-26 13:45:07 +01:00
}
2013-11-29 13:09:21 +01:00
public function getDatatable($clientId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-01 08:33:17 +01:00
$collection = Invoice::with('client','invoice_items','invoice_status')->where('account_id','=',Auth::user()->account_id);
2013-11-29 13:09:21 +01:00
if ($clientId) {
$collection->where('client_id','=',$clientId);
}
2013-12-01 08:33:17 +01:00
$table = Datatable::collection($collection->get())->addColumn('checkbox', function($model)
{
return '<input type="checkbox" name="ids[]" value="' . $model->id . '">';
})
->addColumn('invoice_number', function($model)
{
return link_to('invoices/' . $model->id . '/edit', $model->invoice_number);
});
2013-11-29 13:09:21 +01:00
if (!$clientId)
{
$table->addColumn('client', function($model) {
return link_to('clients/' . $model->client->id, $model->client->name);
});
}
2013-12-01 08:33:17 +01:00
return $table->addColumn('total', function($model)
{
return '$' . money_format('%i', $model->getTotal());
})
->addColumn('amount_due', function($model)
2013-11-26 13:45:07 +01:00
{
return '$' . money_format('%i', $model->getTotal());
})
2013-12-01 08:33:17 +01:00
->addColumn('invoice_date', function($model)
2013-11-26 13:45:07 +01:00
{
2013-12-01 08:33:17 +01:00
return (new Carbon($model->invoice_date))->toFormattedDateString();
})
->addColumn('due_date', function($model)
{
return $model->due_date == '0000-00-00' ? '' : (new Carbon($model->due_date))->toFormattedDateString();
})
->addColumn('status', function($model)
{
return $model->invoice_status->name;
2013-11-26 13:45:07 +01:00
})
->orderColumns('number')
2013-11-29 13:09:21 +01:00
->make();
2013-11-26 13:45:07 +01:00
}
2013-11-28 17:40:13 +01:00
public function view($key)
2013-11-26 13:45:07 +01:00
{
2013-11-28 17:40:13 +01:00
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.account.account_gateways')->where('key', '=', $key)->firstOrFail();
2013-11-26 22:45:10 +01:00
$contact = null;
2013-11-28 17:40:13 +01:00
Activity::viewInvoice($invitation);
2013-11-26 22:45:10 +01:00
2013-11-28 17:40:13 +01:00
return View::make('invoices.view')->with('invoice', $invitation->invoice);
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);
return [
'amount' => $invoice->getTotal(),
'card' => $card,
'currency' => 'USD',
'returnUrl' => URL::to('complete'),
'cancelUrl' => URL::to('/'),
];
}
public function show_payment($invoiceKey)
{
2013-11-28 17:40:13 +01:00
$invoice = Invoice::with('invoice_items', 'client.account.account_gateways.gateway')->where('key', '=', $invoiceKey)->firstOrFail();
$accountGateway = $invoice->client->account->account_gateways[0];
$gateway = InvoiceController::createGateway($accountGateway);
2013-11-26 13:45:07 +01:00
try
{
$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
$payment = new Payment;
$payment->invoice_id = $invoice->id;
$payment->account_id = $invoice->account_id;
$payment->contact_id = 0; // TODO_FIX
$payment->transaction_reference = $ref;
2013-11-26 13:45:07 +01:00
$payment->save();
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');
$payment = Payment::with('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->amount = $payment->invoice->getTotal();
$payment->save();
Session::flash('message', 'Successfully applied payment');
2013-11-28 17:40:13 +01:00
return Redirect::to('view/' . $payment->invoice->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
public function edit($id)
{
$invoice = Invoice::with('client', 'invoice_items')->find($id);
2013-11-29 13:09:21 +01:00
trackViewed(Request::url(), $invoice->invoice_number . ' - ' . $invoice->client->name);
2013-11-28 20:06:38 +01:00
$data = array(
'invoice' => $invoice,
'method' => 'PUT',
'url' => 'invoices/' . $id,
'title' => 'Edit',
'account' => Auth::user()->account,
'products' => Product::getProducts()->get(),
'client' => $invoice->client,
'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get());
return View::make('invoices.edit', $data);
}
2013-11-26 13:45:07 +01:00
public function create($clientId = 0)
{
$client = null;
if ($clientId) {
$client = Client::find($clientId);
}
$data = array(
'invoice' => null,
'method' => 'POST',
'url' => 'invoices',
'title' => 'New',
'client' => $client,
'items' => json_decode(Input::old('items')),
2013-11-26 13:45:07 +01:00
'account' => Auth::user()->account,
'products' => Product::getProducts()->get(),
2013-11-28 20:06:38 +01:00
'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get());
2013-11-26 13:45:07 +01:00
return View::make('invoices.edit', $data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
return InvoiceController::save();
}
private function save($id = null)
{
$rules = array(
'client' => 'required',
'invoice_number' => 'required',
'invoice_date' => 'required'
2013-11-26 13:45:07 +01:00
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('invoices/create')
->withInput()
2013-11-26 13:45:07 +01:00
->withErrors($validator);
} else {
$clientId = Input::get('client');
if ($clientId == "-1")
{
$client = new Client;
$client->name = Input::get('client_name');
$client->account_id = Auth::user()->account_id;
$client->save();
$clientId = $client->id;
$contact = new Contact;
$contact->email = Input::get('client_email');
$client->contacts()->save($contact);
}
else
{
$client = Client::with('contacts')->find($clientId);
$contact = $client->contacts[0];
}
if ($id) {
$invoice = Invoice::find($id);
$invoice->invoice_items()->forceDelete();
} else {
$invoice = new Invoice;
$invoice->account_id = Auth::user()->account_id;
}
$invoice->client_id = $clientId;
$invoice->invoice_number = Input::get('invoice_number');
$invoice->discount = 0;
2013-11-28 17:40:13 +01:00
$invoice->invoice_date = toSqlDate(Input::get('invoice_date'));
$invoice->due_date = toSqlDate(Input::get('due_date'));
2013-11-26 13:45:07 +01:00
$invoice->save();
$items = json_decode(Input::get('items'));
foreach ($items as $item) {
if (!isset($item->cost))
{
$item->cost = 0;
2013-11-26 13:45:07 +01:00
}
if (!isset($item->qty))
{
$item->qty = 0;
}
2013-11-26 13:45:07 +01:00
$product = Product::findProduct($item->product_key);
if (!$product)
{
$product = new Product;
$product->account_id = Auth::user()->account_id;
2013-11-28 17:40:13 +01:00
$product->key = $item->product_key;
}
2013-11-26 13:45:07 +01:00
$product->notes = $item->notes;
$product->cost = $item->cost;
$product->qty = $item->qty;
$product->save();
2013-11-26 13:45:07 +01:00
$invoiceItem = new InvoiceItem;
$invoiceItem->product_id = $product->id;
$invoiceItem->product_key = $item->product_key;
$invoiceItem->notes = $item->notes;
$invoiceItem->cost = $item->cost;
$invoiceItem->qty = $item->qty;
$invoice->invoice_items()->save($invoiceItem);
}
if (Input::get('send_email_checkBox'))
{
$data = array('link' => URL::to('view') . '/' . $invoice->invoice_key);
2013-11-28 17:40:13 +01:00
/*
2013-11-26 13:45:07 +01:00
Mail::send(array('html'=>'emails.invoice_html','text'=>'emails.invoice_text'), $data, function($message) use ($contact)
{
$message->from('hillelcoren@gmail.com', 'Hillel Coren');
$message->to($contact->email);
});
2013-11-28 17:40:13 +01:00
*/
$invitation = new Invitation;
$invitation->invoice_id = $invoice->id;
$invitation->user_id = Auth::user()->id;
$invitation->contact_id = $contact->id;
$invitation->key = str_random(20);
$invitation->save();
2013-11-26 13:45:07 +01:00
2013-11-26 22:45:10 +01:00
2013-11-26 13:45:07 +01:00
Session::flash('message', 'Successfully emailed invoice');
} else {
Session::flash('message', 'Successfully saved invoice');
}
2013-11-28 17:40:13 +01:00
$url = 'invoices/' . $invoice->id . '/edit';
processedRequest($url);
return Redirect::to($url);
2013-11-26 13:45:07 +01:00
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$invoice = Invoice::find($id);
return View::make('invoices.show')->with('invoice', $invoice);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
return InvoiceController::save($id);
}
/**
* 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');
$ids = Input::get('ids');
$invoices = Invoice::find($ids);
foreach ($invoices as $invoice) {
if ($action == 'archive') {
$invoice->delete();
} else if ($action == 'delete') {
$invoice->forceDelete();
}
}
$message = pluralize('Successfully '.$action.'d ? invoice', count($ids));
Session::flash('message', $message);
2013-11-26 13:45:07 +01:00
return Redirect::to('invoices');
}
}