v2 first commit
This commit is contained in:
@ -2,24 +2,16 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Expenses;
|
||||
|
||||
use App\Events\BillCreated;
|
||||
use App\Events\BillUpdated;
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Http\Requests\Expense\Bill as Request;
|
||||
use App\Jobs\Expense\CreateBill;
|
||||
use App\Jobs\Expense\DeleteBill;
|
||||
use App\Jobs\Expense\UpdateBill;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\BillHistory;
|
||||
use App\Models\Expense\BillItem;
|
||||
use App\Models\Expense\BillPayment;
|
||||
use App\Models\Expense\BillStatus;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Transformers\Expense\Bill as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Bills extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@ -27,7 +19,7 @@ class Bills extends ApiController
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$bills = Bill::with(['vendor', 'status', 'items', 'payments', 'histories'])->collect();
|
||||
$bills = Bill::with(['contact', 'status', 'items', 'transactions', 'histories'])->collect(['billed_at'=> 'desc']);
|
||||
|
||||
return $this->response->paginator($bills, new Transformer());
|
||||
}
|
||||
@ -51,72 +43,9 @@ class Bills extends ApiController
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$bill = Bill::create($request->all());
|
||||
$bill = $this->dispatch(new CreateBill($request));
|
||||
|
||||
$bill_item = array();
|
||||
$bill_item['company_id'] = $request['company_id'];
|
||||
$bill_item['bill_id'] = $bill->id;
|
||||
|
||||
if ($request['item']) {
|
||||
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;
|
||||
|
||||
// Increase stock (item bought)
|
||||
$item_object->quantity += $item['quantity'];
|
||||
$item_object->save();
|
||||
} 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'];
|
||||
}
|
||||
|
||||
$bill_item['item_id'] = $item_id;
|
||||
$bill_item['name'] = str_limit($item['name'], 180, '');
|
||||
$bill_item['sku'] = $item_sku;
|
||||
$bill_item['quantity'] = $item['quantity'];
|
||||
$bill_item['price'] = $item['price'];
|
||||
$bill_item['tax'] = $tax;
|
||||
$bill_item['tax_id'] = $tax_id;
|
||||
$bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity'];
|
||||
|
||||
$request['amount'] += $bill_item['total'];
|
||||
|
||||
BillItem::create($bill_item);
|
||||
}
|
||||
}
|
||||
|
||||
$bill->update($request->input());
|
||||
|
||||
$request['bill_id'] = $bill->id;
|
||||
$request['status_code'] = $request['bill_status_code'];
|
||||
$request['notify'] = 0;
|
||||
$request['description'] = trans('messages.success.added', ['type' => $request['bill_number']]);
|
||||
|
||||
BillHistory::create($request->input());
|
||||
|
||||
// Fire the event to make it extendible
|
||||
event(new BillCreated($bill));
|
||||
|
||||
return $this->response->created(url('api/bills/'.$bill->id));
|
||||
return $this->response->created(url('api/bills/' . $bill->id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -128,61 +57,9 @@ class Bills extends ApiController
|
||||
*/
|
||||
public function update(Bill $bill, Request $request)
|
||||
{
|
||||
$bill_item = array();
|
||||
$bill_item['company_id'] = $request['company_id'];
|
||||
$bill_item['bill_id'] = $bill->id;
|
||||
$bill = $this->dispatch(new UpdateBill($bill, $request));
|
||||
|
||||
if ($request['item']) {
|
||||
BillItem::where('bill_id', $bill->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'];
|
||||
}
|
||||
|
||||
$bill_item['item_id'] = $item_id;
|
||||
$bill_item['name'] = str_limit($item['name'], 180, '');
|
||||
$bill_item['sku'] = $item_sku;
|
||||
$bill_item['quantity'] = $item['quantity'];
|
||||
$bill_item['price'] = $item['price'];
|
||||
$bill_item['tax'] = $tax;
|
||||
$bill_item['tax_id'] = $tax_id;
|
||||
$bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity'];
|
||||
|
||||
$request['amount'] += $bill_item['total'];
|
||||
|
||||
BillItem::create($bill_item);
|
||||
}
|
||||
}
|
||||
|
||||
$bill->update($request->input());
|
||||
|
||||
// Fire the event to make it extendible
|
||||
event(new BillUpdated($bill));
|
||||
|
||||
return $this->response->item($bill->fresh(), new Transformer());
|
||||
return $this->item($bill->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -193,12 +70,12 @@ class Bills extends ApiController
|
||||
*/
|
||||
public function destroy(Bill $bill)
|
||||
{
|
||||
$bill->delete();
|
||||
try {
|
||||
$this->dispatch(new DeleteBill($bill));
|
||||
|
||||
BillItem::where('bill_id', $bill->id)->delete();
|
||||
BillPayment::where('bill_id', $bill->id)->delete();
|
||||
BillHistory::where('bill_id', $bill->id)->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
return $this->response->noContent();
|
||||
} catch(\Exception $e) {
|
||||
$this->response->errorUnauthorized($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Expenses;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Requests\Expense\Payment as Request;
|
||||
use App\Models\Expense\Payment;
|
||||
use App\Transformers\Expense\Payment as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Payments extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$payments = Payment::with(['account', 'vendor', 'category'])->collect();
|
||||
|
||||
return $this->response->paginator($payments, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param Payment $payment
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function show(Payment $payment)
|
||||
{
|
||||
return $this->response->item($payment, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$payment = Payment::create($request->all());
|
||||
|
||||
return $this->response->created(url('api/payments/'.$payment->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $payment
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Payment $payment, Request $request)
|
||||
{
|
||||
$payment->update($request->all());
|
||||
|
||||
return $this->response->item($payment->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Payment $payment
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Payment $payment)
|
||||
{
|
||||
$payment->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Expenses;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Requests\Expense\Vendor as Request;
|
||||
use App\Models\Expense\Vendor;
|
||||
use App\Transformers\Expense\Vendor as Transformer;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
|
||||
class Vendors extends ApiController
|
||||
{
|
||||
use Helpers;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$vendors = Vendor::collect();
|
||||
|
||||
return $this->response->paginator($vendors, 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)) {
|
||||
$vendor = Vendor::find($id);
|
||||
} else {
|
||||
$vendor = Vendor::where('email', $id)->first();
|
||||
}
|
||||
|
||||
return $this->response->item($vendor, new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$vendor = Vendor::create($request->all());
|
||||
|
||||
return $this->response->created(url('api/vendors/'.$vendor->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param $vendor
|
||||
* @param $request
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function update(Vendor $vendor, Request $request)
|
||||
{
|
||||
$vendor->update($request->all());
|
||||
|
||||
return $this->response->item($vendor->fresh(), new Transformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Vendor $vendor
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function destroy(Vendor $vendor)
|
||||
{
|
||||
$vendor->delete();
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user