mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-08 12:12:48 +01:00
Enabled updating/archiving invoice through the API
This commit is contained in:
parent
9dc890d4f0
commit
7beeb78126
@ -15,6 +15,8 @@ use App\Ninja\Repositories\InvoiceRepository;
|
||||
use App\Ninja\Mailers\ContactMailer as Mailer;
|
||||
use App\Http\Controllers\BaseAPIController;
|
||||
use App\Ninja\Transformers\InvoiceTransformer;
|
||||
use App\Http\Requests\CreateInvoiceRequest;
|
||||
use App\Http\Requests\UpdateInvoiceRequest;
|
||||
|
||||
class InvoiceApiController extends BaseAPIController
|
||||
{
|
||||
@ -103,15 +105,11 @@ class InvoiceApiController extends BaseAPIController
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function store()
|
||||
public function store(CreateInvoiceRequest $request)
|
||||
{
|
||||
$data = Input::all();
|
||||
$error = null;
|
||||
|
||||
if (isset($data['id']) || isset($data['public_id'])) {
|
||||
die("We don't yet support updating invoices");
|
||||
}
|
||||
|
||||
if (isset($data['email'])) {
|
||||
$email = $data['email'];
|
||||
$client = Client::scope()->whereHas('contacts', function($query) use ($email) {
|
||||
@ -140,52 +138,30 @@ class InvoiceApiController extends BaseAPIController
|
||||
$client = $this->clientRepo->save($clientData);
|
||||
}
|
||||
} else if (isset($data['client_id'])) {
|
||||
$client = Client::scope($data['client_id'])->first();
|
||||
$client = Client::scope($data['client_id'])->firstOrFail();
|
||||
}
|
||||
|
||||
// check if the invoice number is set and unique
|
||||
if (!isset($data['invoice_number']) && !isset($data['id'])) {
|
||||
// do nothing... invoice number will be set automatically
|
||||
} 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']);
|
||||
}
|
||||
$data = self::prepareData($data, $client);
|
||||
$data['client_id'] = $client->id;
|
||||
$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();
|
||||
}
|
||||
|
||||
if (!$error) {
|
||||
if (!isset($data['client_id']) && !isset($data['email'])) {
|
||||
$error = trans('validation.required_without', ['attribute' => 'client_id', 'values' => 'email']);
|
||||
} else if (!$client) {
|
||||
$error = trans('validation.not_in', ['attribute' => 'client_id']);
|
||||
}
|
||||
if (isset($data['email_invoice']) && $data['email_invoice']) {
|
||||
$this->mailer->sendInvoice($invoice);
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
return $error;
|
||||
} else {
|
||||
$data = self::prepareData($data, $client);
|
||||
$data['client_id'] = $client->id;
|
||||
$invoice = $this->invoiceRepo->save($data);
|
||||
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
|
||||
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
||||
$data = $this->createItem($invoice, $transformer, 'invoice');
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (isset($data['email_invoice']) && $data['email_invoice']) {
|
||||
$this->mailer->sendInvoice($invoice);
|
||||
}
|
||||
|
||||
$invoice = Invoice::scope($invoice->public_id)->with('client', 'invoice_items', 'invitations')->first();
|
||||
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
||||
$data = $this->createItem($invoice, $transformer, 'invoice');
|
||||
|
||||
return $this->response($data);
|
||||
}
|
||||
return $this->response($data);
|
||||
}
|
||||
|
||||
private function prepareData($data, $client)
|
||||
@ -306,8 +282,25 @@ class InvoiceApiController extends BaseAPIController
|
||||
}
|
||||
|
||||
|
||||
public function update($invoiceId = null)
|
||||
public function update(UpdateInvoiceRequest $request, $publicId)
|
||||
{
|
||||
//PUT Stub
|
||||
if ($request->action == ACTION_ARCHIVE) {
|
||||
$invoice = Invoice::scope($publicId)->firstOrFail();
|
||||
$this->invoiceRepo->archive($invoice);
|
||||
|
||||
$response = json_encode(RESULT_SUCCESS, JSON_PRETTY_PRINT);
|
||||
$headers = Utils::getApiHeaders();
|
||||
return Response::make($response, 200, $headers);
|
||||
}
|
||||
|
||||
$data = $request->input();
|
||||
$data['public_id'] = $publicId;
|
||||
$this->invoiceRepo->save($data);
|
||||
|
||||
$invoice = Invoice::scope($publicId)->with('client', 'invoice_items', 'invitations')->firstOrFail();
|
||||
$transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer'));
|
||||
$data = $this->createItem($invoice, $transformer, 'invoice');
|
||||
|
||||
return $this->response($data);
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ use App\Events\InvoiceInvitationWasViewed;
|
||||
use App\Events\QuoteInvitationWasViewed;
|
||||
use App\Services\InvoiceService;
|
||||
use App\Services\RecurringInvoiceService;
|
||||
use App\Http\Requests\SaveInvoiceRequest;
|
||||
use App\Http\Requests\SaveInvoiceWithClientRequest;
|
||||
|
||||
class InvoiceController extends BaseController
|
||||
{
|
||||
@ -440,7 +440,7 @@ class InvoiceController extends BaseController
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(SaveInvoiceRequest $request)
|
||||
public function store(SaveInvoiceWithClientRequest $request)
|
||||
{
|
||||
$action = Input::get('action');
|
||||
$entityType = Input::get('entityType');
|
||||
@ -474,7 +474,7 @@ class InvoiceController extends BaseController
|
||||
* @param int $id
|
||||
* @return Response
|
||||
*/
|
||||
public function update(SaveInvoiceRequest $request)
|
||||
public function update(SaveInvoiceWithClientRequest $request)
|
||||
{
|
||||
$action = Input::get('action');
|
||||
$entityType = Input::get('entityType');
|
||||
|
@ -39,7 +39,7 @@ class PublicClientController extends BaseController
|
||||
'hideLogo' => $account->isWhiteLabel(),
|
||||
'clientViewCSS' => $account->clientViewCSS(),
|
||||
];
|
||||
|
||||
|
||||
return response()->view('invited.dashboard', $data);
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,9 @@ class CreateClientRequest extends Request
|
||||
}
|
||||
|
||||
return $factory->make(
|
||||
$this->input(), $this->container->call([$this, 'rules']), $this->messages()
|
||||
$this->input(),
|
||||
$this->container->call([$this, 'rules']),
|
||||
$this->messages()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
37
app/Http/Requests/CreateInvoiceRequest.php
Normal file
37
app/Http/Requests/CreateInvoiceRequest.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php namespace app\Http\Requests;
|
||||
|
||||
use Auth;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Validation\Factory;
|
||||
use App\Models\Invoice;
|
||||
|
||||
class CreateInvoiceRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$rules = [
|
||||
'email' => 'required_without:client_id',
|
||||
'client_id' => 'required_without:email',
|
||||
'invoice_items' => 'valid_invoice_items',
|
||||
'invoice_number' => 'unique:invoices,invoice_number,,id,account_id,'.Auth::user()->account_id,
|
||||
'discount' => 'positive',
|
||||
];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ use App\Http\Requests\Request;
|
||||
use Illuminate\Validation\Factory;
|
||||
use App\Models\Invoice;
|
||||
|
||||
class SaveInvoiceRequest extends Request
|
||||
class SaveInvoiceWithClientRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
42
app/Http/Requests/UpdateInvoiceRequest.php
Normal file
42
app/Http/Requests/UpdateInvoiceRequest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php namespace app\Http\Requests;
|
||||
|
||||
use Auth;
|
||||
use App\Http\Requests\Request;
|
||||
use Illuminate\Validation\Factory;
|
||||
use App\Models\Invoice;
|
||||
|
||||
class UpdateInvoiceRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
if ($this->action == ACTION_ARCHIVE) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$publicId = $this->route('invoices');
|
||||
$invoiceId = Invoice::getPrivateId($publicId);
|
||||
|
||||
$rules = [
|
||||
'invoice_items' => 'required|valid_invoice_items',
|
||||
'invoice_number' => 'unique:invoices,invoice_number,'.$invoiceId.',id,account_id,'.Auth::user()->account_id,
|
||||
'discount' => 'positive',
|
||||
];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
@ -876,7 +876,7 @@ class Account extends Eloquent
|
||||
}
|
||||
|
||||
public function clientViewCSS(){
|
||||
if (($this->isNinjaAccount() && $this->isPro()) || $this->isWhiteLabel()) {
|
||||
if ((Utils::isNinja() && $this->isPro()) || $this->isWhiteLabel()) {
|
||||
return $this->client_view_css;
|
||||
}
|
||||
|
||||
|
@ -233,7 +233,11 @@ class InvoiceRepository extends BaseRepository
|
||||
if (isset($data['partial'])) {
|
||||
$invoice->partial = round(Utils::parseFloat($data['partial']), 2);
|
||||
}
|
||||
$invoice->invoice_date = isset($data['invoice_date_sql']) ? $data['invoice_date_sql'] : Utils::toSqlDate($data['invoice_date']);
|
||||
if (isset($data['invoice_date_sql'])) {
|
||||
$invoice->invoice_date = $data['invoice_date_sql'];
|
||||
} elseif (isset($data['invoice_date'])) {
|
||||
$invoice->invoice_date = Utils::toSqlDate($data['invoice_date']);
|
||||
}
|
||||
|
||||
if ($invoice->is_recurring) {
|
||||
if ($invoice->start_date && $invoice->start_date != Utils::toSqlDate($data['start_date'])) {
|
||||
|
@ -20,7 +20,7 @@ class InvoiceItemTransformer extends EntityTransformer
|
||||
'cost' => (float) $item->cost,
|
||||
'qty' => (float) $item->qty,
|
||||
'tax_name' => $item->tax_name,
|
||||
'tax_rate' => $item->tax_rate
|
||||
'tax_rate' => (float) $item->tax_rate
|
||||
];
|
||||
}
|
||||
}
|
@ -73,8 +73,8 @@ class InvoiceTransformer extends EntityTransformer
|
||||
'auto_bill' => (bool) $invoice->auto_bill,
|
||||
'account_key' => $this->account->account_key,
|
||||
'user_id' => (int) $invoice->user->public_id + 1,
|
||||
'custom_value1' => $invoice->custom_value1,
|
||||
'custom_value2' => $invoice->custom_value2,
|
||||
'custom_value1' => (float) $invoice->custom_value1,
|
||||
'custom_value2' => (float) $invoice->custom_value2,
|
||||
'custom_taxes1' => (bool) $invoice->custom_taxes1,
|
||||
'custom_taxes2' => (bool) $invoice->custom_taxes2,
|
||||
];
|
||||
|
Loading…
Reference in New Issue
Block a user