v2 first commit
This commit is contained in:
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Incomes;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Requests\Income\Customer as Request;
|
||||
use App\Models\Income\Customer;
|
||||
use App\Transformers\Income\Customer as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Customers extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$customers = Customer::collect();
|
||||
|
||||
return $this->response->paginator($customers, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int|string $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
// Check if we're querying by id or email
|
||||
if (is_numeric($id)) {
|
||||
$customer = Customer::find($id);
|
||||
} else {
|
||||
$customer = Customer::where('email', $id)->first();
|
||||
}
|
||||
|
||||
return $this->response->item($customer, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$customer = Customer::create($request->all());
|
||||
|
||||
return $this->response->created(url('api/customers/'.$customer->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $customer
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Customer $customer, Request $request)
|
||||
{
|
||||
$customer->update($request->all());
|
||||
|
||||
return $this->response->item($customer->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Customer $customer
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Customer $customer)
|
||||
{
|
||||
$customer->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
@ -1,126 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Incomes;
|
||||
|
||||
use App\Http\Requests\Income\InvoicePayment as Request;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Traits\DateTime;
|
||||
use App\Transformers\Income\InvoicePayments as Transformer;
|
||||
use Date;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class InvoicePayments extends BaseController
|
||||
{
|
||||
use DateTime, Helpers, AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index($invoice_id)
|
||||
{
|
||||
$invoice_payments = InvoicePayment::where('invoice_id', $invoice_id)->get();
|
||||
|
||||
return $this->response->collection($invoice_payments, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show($invoice_id, $id)
|
||||
{
|
||||
$invoice_payment = InvoicePayment::find($id);
|
||||
|
||||
return $this->response->item($invoice_payment, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store($invoice_id, Request $request)
|
||||
{
|
||||
// Get currency object
|
||||
$currency = Currency::where('code', $request['currency_code'])->first();
|
||||
|
||||
$request['currency_code'] = $currency->code;
|
||||
$request['currency_rate'] = $currency->rate;
|
||||
|
||||
$request['invoice_id'] = $invoice_id;
|
||||
|
||||
$invoice = Invoice::find($invoice_id);
|
||||
|
||||
if ($request['currency_code'] == $invoice->currency_code) {
|
||||
if ($request['amount'] > $invoice->amount) {
|
||||
return $this->response->noContent();
|
||||
} elseif ($request['amount'] == $invoice->amount) {
|
||||
$invoice->invoice_status_code = 'paid';
|
||||
} else {
|
||||
$invoice->invoice_status_code = 'partial';
|
||||
}
|
||||
} else {
|
||||
$request_invoice = new Invoice();
|
||||
|
||||
$request_invoice->amount = (float) $request['amount'];
|
||||
$request_invoice->currency_code = $currency->code;
|
||||
$request_invoice->currency_rate = $currency->rate;
|
||||
|
||||
$amount = $request_invoice->getConvertedAmount();
|
||||
|
||||
if ($amount > $invoice->amount) {
|
||||
return $this->response->noContent();
|
||||
} elseif ($amount == $invoice->amount) {
|
||||
$invoice->invoice_status_code = 'paid';
|
||||
} else {
|
||||
$invoice->invoice_status_code = 'partial';
|
||||
}
|
||||
}
|
||||
|
||||
$invoice->save();
|
||||
|
||||
$invoice_payment = InvoicePayment::create($request->input());
|
||||
|
||||
$request['status_code'] = $invoice->invoice_status_code;
|
||||
$request['notify'] = 0;
|
||||
|
||||
$desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat());
|
||||
$desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format();
|
||||
$request['description'] = $desc_date . ' ' . $desc_amount;
|
||||
|
||||
InvoiceHistory::create($request->input());
|
||||
|
||||
return $this->response->created(url('api/invoices/' . $invoice_id . '/payments' . $invoice_payment->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy($invoice_id, $id)
|
||||
{
|
||||
$invoice_payment = InvoicePayment::find($id);
|
||||
|
||||
$invoice_payment->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
79
app/Http/Controllers/Api/Incomes/InvoiceTransactions.php
Normal file
79
app/Http/Controllers/Api/Incomes/InvoiceTransactions.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Incomes;
|
||||
|
||||
use App\Http\Requests\Banking\Transaction as Request;
|
||||
use App\Jobs\Banking\CreateDocumentTransaction;
|
||||
use App\Jobs\Banking\DeleteTransaction;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Transformers\Banking\Transaction as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class InvoiceTransactions extends BaseController
|
||||
{
|
||||
use Helpers, AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index($invoice_id)
|
||||
{
|
||||
$transactions = Transaction::document($invoice_id)->get();
|
||||
|
||||
return $this->response->collection($transactions, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show($invoice_id, $id)
|
||||
{
|
||||
$transaction = Transaction::document($invoice_id)->find($id);
|
||||
|
||||
return $this->response->item($transaction, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store($invoice_id, Request $request)
|
||||
{
|
||||
$invoice = Invoice::find($invoice_id);
|
||||
|
||||
$transaction = $this->dispatch(new CreateDocumentTransaction($invoice, $request));
|
||||
|
||||
return $this->response->created(url('api/invoices/' . $invoice_id . '/transactions/' . $transaction->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param $invoice_id
|
||||
* @param $id
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy($invoice_id, $id)
|
||||
{
|
||||
$transaction = Transaction::document($invoice_id)->find($id);
|
||||
|
||||
$this->dispatch(new DeleteTransaction($transaction));
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
@ -2,25 +2,16 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Incomes;
|
||||
|
||||
use App\Events\InvoiceUpdated;
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Income\Invoice as Request;
|
||||
use App\Jobs\Income\CreateInvoice;
|
||||
use App\Jobs\Income\DeleteInvoice;
|
||||
use App\Jobs\Income\UpdateInvoice;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoiceItem;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\InvoiceTotal;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Traits\Incomes;
|
||||
use App\Transformers\Income\Invoice as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Invoices extends ApiController
|
||||
{
|
||||
use Helpers, Incomes;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@ -28,7 +19,7 @@ class Invoices extends ApiController
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$invoices = Invoice::with(['customer', 'status', 'items', 'payments', 'histories'])->collect();
|
||||
$invoices = Invoice::with(['contact', 'status', 'items', 'transactions', 'histories'])->collect(['invoiced_at'=> 'desc']);
|
||||
|
||||
return $this->response->paginator($invoices, new Transformer());
|
||||
}
|
||||
@ -59,11 +50,7 @@ class Invoices extends ApiController
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
if (empty($request['amount'])) {
|
||||
$request['amount'] = 0;
|
||||
}
|
||||
|
||||
$invoice = dispatch(new CreateInvoice($request));
|
||||
$invoice = $this->dispatch(new CreateInvoice($request));
|
||||
|
||||
return $this->response->created(url('api/invoices/' . $invoice->id));
|
||||
}
|
||||
@ -77,89 +64,7 @@ class Invoices extends ApiController
|
||||
*/
|
||||
public function update(Invoice $invoice, Request $request)
|
||||
{
|
||||
$taxes = [];
|
||||
$tax_total = 0;
|
||||
$sub_total = 0;
|
||||
|
||||
$invoice_item = array();
|
||||
$invoice_item['company_id'] = $request['company_id'];
|
||||
$invoice_item['invoice_id'] = $invoice->id;
|
||||
|
||||
if ($request['item']) {
|
||||
InvoiceItem::where('invoice_id', $invoice->id)->delete();
|
||||
|
||||
foreach ($request['item'] as $item) {
|
||||
$item_id = 0;
|
||||
$item_sku = '';
|
||||
|
||||
if (!empty($item['item_id'])) {
|
||||
$item_object = Item::find($item['item_id']);
|
||||
|
||||
$item_id = $item['item_id'];
|
||||
|
||||
$item['name'] = $item_object->name;
|
||||
$item_sku = $item_object->sku;
|
||||
} elseif (!empty($item['sku'])) {
|
||||
$item_sku = $item['sku'];
|
||||
}
|
||||
|
||||
$tax = $tax_id = 0;
|
||||
|
||||
if (!empty($item['tax_id'])) {
|
||||
$tax_object = Tax::find($item['tax_id']);
|
||||
|
||||
$tax_id = $item['tax_id'];
|
||||
|
||||
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
|
||||
} elseif (!empty($item['tax'])) {
|
||||
$tax = $item['tax'];
|
||||
}
|
||||
|
||||
$invoice_item['item_id'] = $item_id;
|
||||
$invoice_item['name'] = str_limit($item['name'], 180, '');
|
||||
$invoice_item['sku'] = $item_sku;
|
||||
$invoice_item['quantity'] = $item['quantity'];
|
||||
$invoice_item['price'] = $item['price'];
|
||||
$invoice_item['tax'] = $tax;
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['total'] = $item['price'] * $item['quantity'];
|
||||
|
||||
$request['amount'] += $invoice_item['total'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
|
||||
if (isset($tax_object)) {
|
||||
if (array_key_exists($tax_object->id, $taxes)) {
|
||||
$taxes[$tax_object->id]['amount'] += $tax;
|
||||
} else {
|
||||
$taxes[$tax_object->id] = [
|
||||
'name' => $tax_object->name,
|
||||
'amount' => $tax
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$tax_total += $tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
unset($item_object);
|
||||
unset($tax_object);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($request['amount'])) {
|
||||
$request['amount'] = $sub_total + $tax_total;
|
||||
}
|
||||
|
||||
$invoice->update($request->input());
|
||||
|
||||
// Delete previous invoice totals
|
||||
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
|
||||
|
||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
||||
|
||||
// Fire the event to make it extendible
|
||||
event(new InvoiceUpdated($invoice));
|
||||
$invoice = $this->dispatch(new UpdateInvoice($invoice, $request));
|
||||
|
||||
return $this->response->item($invoice->fresh(), new Transformer());
|
||||
}
|
||||
@ -172,81 +77,12 @@ class Invoices extends ApiController
|
||||
*/
|
||||
public function destroy(Invoice $invoice)
|
||||
{
|
||||
$this->deleteRelationships($invoice, ['items', 'histories', 'payments', 'recurring', 'totals']);
|
||||
$invoice->delete();
|
||||
try {
|
||||
$this->dispatch(new DeleteInvoice($invoice));
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
|
||||
protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total) {
|
||||
// Add invoice total taxes
|
||||
if ($request['totals']) {
|
||||
$sort_order = 1;
|
||||
|
||||
foreach ($request['totals'] as $total) {
|
||||
if (!empty($total['sort_order'])) {
|
||||
$sort_order = $total['sort_order'];
|
||||
}
|
||||
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => $total['code'],
|
||||
'name' => $total['name'],
|
||||
'amount' => $total['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
|
||||
if (empty($total['sort_order'])) {
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Added invoice total sub total
|
||||
$invoice_sub_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => 1,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_sub_total);
|
||||
|
||||
$sort_order = 2;
|
||||
|
||||
// Added invoice total taxes
|
||||
if ($taxes) {
|
||||
foreach ($taxes as $tax) {
|
||||
$invoice_tax_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_tax_total);
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Added invoice total total
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $sub_total + $tax_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
return $this->response->noContent();
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Incomes;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Requests\Income\Revenue as Request;
|
||||
use App\Models\Income\Revenue;
|
||||
use App\Transformers\Income\Revenue as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Revenues extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$revenues = Revenue::with(['account', 'customer', 'category'])->collect();
|
||||
|
||||
return $this->response->paginator($revenues, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param Revenue $revenue
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show(Revenue $revenue)
|
||||
{
|
||||
return $this->response->item($revenue, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$revenue = Revenue::create($request->all());
|
||||
|
||||
return $this->response->created(url('api/revenues/'.$revenue->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $revenue
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Revenue $revenue, Request $request)
|
||||
{
|
||||
$revenue->update($request->all());
|
||||
|
||||
return $this->response->item($revenue->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Revenue $revenue
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Revenue $revenue)
|
||||
{
|
||||
$revenue->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user