1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/InvoiceApiController.php

296 lines
9.4 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-04-08 20:19:58 +02:00
use Auth;
2015-03-17 02:30:56 +01:00
use Utils;
2015-04-08 20:19:58 +02:00
use Response;
use Input;
use App\Models\Invoice;
use App\Models\Client;
2015-05-10 10:45:03 +02:00
use App\Models\Contact;
2015-04-08 20:19:58 +02:00
use App\Models\Product;
use App\Models\Invitation;
2015-05-10 10:45:03 +02:00
use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Mailers\ContactMailer as Mailer;
2015-03-16 22:45:25 +01:00
class InvoiceApiController extends Controller
{
protected $invoiceRepo;
2015-05-10 10:45:03 +02:00
public function __construct(InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, Mailer $mailer)
2015-03-16 22:45:25 +01:00
{
$this->invoiceRepo = $invoiceRepo;
2015-05-10 10:45:03 +02:00
$this->clientRepo = $clientRepo;
2015-03-16 22:45:25 +01:00
$this->mailer = $mailer;
}
2015-11-08 21:34:26 +01:00
/**
* @SWG\Get(
* path="/invoices",
* summary="List of invoices",
2015-11-08 22:53:13 +01:00
* tags={"invoice"},
2015-11-08 21:34:26 +01:00
* @SWG\Response(
* response=200,
* description="A list with invoices",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Invoice"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-09-07 11:07:55 +02:00
public function index($clientPublicId = false)
2015-03-16 22:45:25 +01:00
{
2015-07-30 19:48:59 +02:00
$invoices = Invoice::scope()
->with('client', 'invitations.account')
2015-09-07 11:07:55 +02:00
->where('invoices.is_quote', '=', false);
if ($clientPublicId) {
$invoices->whereHas('client', function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
});
}
$invoices = $invoices->orderBy('created_at', 'desc')->get();
2015-04-13 09:54:51 +02:00
// Add the first invitation link to the data
foreach ($invoices as $key => $invoice) {
foreach ($invoice->invitations as $subKey => $invitation) {
$invoices[$key]['link'] = $invitation->getLink();
}
unset($invoice['invitations']);
}
2015-03-16 22:45:25 +01:00
2015-07-02 22:21:29 +02:00
$invoices = Utils::remapPublicIds($invoices);
2015-04-13 09:54:51 +02:00
2015-03-16 22:45:25 +01:00
$response = json_encode($invoices, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders(count($invoices));
return Response::make($response, 200, $headers);
}
2015-11-08 22:53:13 +01:00
/**
* @SWG\Post(
* path="/invoices",
* tags={"invoice"},
* summary="Create an invoice",
* @SWG\Parameter(
* in="body",
* name="body",
* @SWG\Schema(ref="#/definitions/Invoice")
* ),
* @SWG\Response(
* response=200,
* description="Newly created invoice",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Invoice"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
2015-03-16 22:45:25 +01:00
public function store()
{
$data = Input::all();
$error = null;
2015-10-28 20:22:07 +01:00
if (isset($data['id']) || isset($data['public_id'])) {
die("We don't yet support updating invoices");
}
2015-05-10 10:45:03 +02:00
if (isset($data['email'])) {
2015-05-11 13:16:36 +02:00
$client = Client::scope()->whereHas('contacts', function($query) use ($data) {
$query->where('email', '=', $data['email']);
})->first();
if (!$client) {
2015-05-10 10:45:03 +02:00
$clientData = ['contact' => ['email' => $data['email']]];
foreach (['name', 'private_notes'] as $field) {
if (isset($data[$field])) {
$clientData[$field] = $data[$field];
}
}
foreach (['first_name', 'last_name'] as $field) {
if (isset($data[$field])) {
$clientData[$field] = $data[$field];
}
}
$error = $this->clientRepo->getErrors($clientData);
if (!$error) {
2015-10-28 20:22:07 +01:00
$client = $this->clientRepo->save($clientData);
2015-05-10 10:45:03 +02:00
}
}
} else if (isset($data['client_id'])) {
2015-03-16 22:45:25 +01:00
$client = Client::scope($data['client_id'])->first();
2015-05-10 10:45:03 +02:00
}
2015-10-22 20:48:12 +02:00
// check if the invoice number is set and unique
if (!isset($data['invoice_number']) && !isset($data['id'])) {
2015-10-23 13:55:18 +02:00
// do nothing... invoice number will be set automatically
2015-10-22 20:48:12 +02:00
} else if (isset($data['invoice_number'])) {
$invoice = Invoice::scope()->where('invoice_number', '=', $data['invoice_number'])->first();
if ($invoice) {
$error = trans('validation.unique', ['attribute' => 'texts.invoice_number']);
}
}
2015-05-10 10:45:03 +02:00
if (!$error) {
if (!isset($data['client_id']) && !isset($data['email'])) {
$error = trans('validation.', ['attribute' => 'client_id or email']);
} else if (!$client) {
2015-03-16 22:45:25 +01:00
$error = trans('validation.not_in', ['attribute' => 'client_id']);
}
}
2015-05-10 10:45:03 +02:00
2015-03-16 22:45:25 +01:00
if ($error) {
$response = json_encode($error, JSON_PRETTY_PRINT);
} else {
$data = self::prepareData($data, $client);
2015-04-28 22:13:52 +02:00
$data['client_id'] = $client->id;
2015-10-28 20:22:07 +01:00
$invoice = $this->invoiceRepo->save($data);
if (!isset($data['id'])) {
$invitation = Invitation::createNew();
$invitation->invoice_id = $invoice->id;
$invitation->contact_id = $client->contacts[0]->id;
$invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
$invitation->save();
}
2015-03-16 22:45:25 +01:00
2015-05-10 10:45:03 +02:00
if (isset($data['email_invoice']) && $data['email_invoice']) {
$this->mailer->sendInvoice($invoice);
}
2015-03-16 22:45:25 +01:00
// prepare the return data
2015-07-02 22:21:29 +02:00
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
$invoice = Utils::remapPublicIds([$invoice]);
2015-03-16 22:45:25 +01:00
$response = json_encode($invoice, JSON_PRETTY_PRINT);
}
$headers = Utils::getApiHeaders();
return Response::make($response, $error ? 400 : 200, $headers);
}
private function prepareData($data, $client)
2015-03-16 22:45:25 +01:00
{
$account = Auth::user()->account;
$account->loadLocalizationSettings($client);
2015-03-16 22:45:25 +01:00
// set defaults for optional fields
$fields = [
'discount' => 0,
'is_amount_discount' => false,
'terms' => '',
'invoice_footer' => '',
'public_notes' => '',
'po_number' => '',
'invoice_design_id' => $account->invoice_design_id,
'invoice_items' => [],
'custom_value1' => 0,
'custom_value2' => 0,
'custom_taxes1' => false,
'custom_taxes2' => false,
2015-05-10 10:45:03 +02:00
'partial' => 0
2015-03-16 22:45:25 +01:00
];
if (!isset($data['invoice_date'])) {
$fields['invoice_date_sql'] = date_create()->format('Y-m-d');
}
if (!isset($data['due_date'])) {
$fields['due_date_sql'] = false;
}
foreach ($fields as $key => $val) {
if (!isset($data[$key])) {
$data[$key] = $val;
}
}
// hardcode some fields
$fields = [
'is_recurring' => false
];
foreach ($fields as $key => $val) {
$data[$key] = $val;
}
// initialize the line items
if (isset($data['product_key']) || isset($data['cost']) || isset($data['notes']) || isset($data['qty'])) {
$data['invoice_items'] = [self::prepareItem($data)];
2015-10-13 09:11:44 +02:00
// make sure the tax isn't applied twice (for the invoice and the line item)
unset($data['invoice_items'][0]['tax_name']);
unset($data['invoice_items'][0]['tax_rate']);
2015-03-16 22:45:25 +01:00
} else {
foreach ($data['invoice_items'] as $index => $item) {
$data['invoice_items'][$index] = self::prepareItem($item);
}
}
return $data;
}
private function prepareItem($item)
{
$fields = [
'cost' => 0,
'product_key' => '',
'notes' => '',
'qty' => 1
];
foreach ($fields as $key => $val) {
if (!isset($item[$key])) {
$item[$key] = $val;
}
}
// if only the product key is set we'll load the cost and notes
if ($item['product_key'] && (is_null($item['cost']) || is_null($item['notes']))) {
2015-03-16 22:45:25 +01:00
$product = Product::findProductByKey($item['product_key']);
if ($product) {
if (is_null($item['cost'])) {
2015-03-16 22:45:25 +01:00
$item['cost'] = $product->cost;
}
if (is_null($item['notes'])) {
2015-03-16 22:45:25 +01:00
$item['notes'] = $product->notes;
}
}
}
return $item;
}
public function emailInvoice()
{
$data = Input::all();
$error = null;
if (!isset($data['id'])) {
$error = trans('validation.required', ['attribute' => 'id']);
} else {
$invoice = Invoice::scope($data['id'])->first();
if (!$invoice) {
$error = trans('validation.not_in', ['attribute' => 'id']);
} else {
$this->mailer->sendInvoice($invoice);
}
}
if ($error) {
$response = json_encode($error, JSON_PRETTY_PRINT);
} else {
$response = json_encode(RESULT_SUCCESS, JSON_PRETTY_PRINT);
}
$headers = Utils::getApiHeaders();
return Response::make($response, $error ? 400 : 200, $headers);
}
}