205 lines
6.1 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Api\Expenses;
use App\Events\BillCreated;
use App\Events\BillUpdated;
use App\Http\Controllers\ApiController;
use App\Http\Requests\Expense\Bill as Request;
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;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Item;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Tax;
2017-10-16 21:02:32 +03:00
use App\Transformers\Expense\Bill as Transformer;
2017-09-14 22:21:00 +03:00
use Dingo\Api\Routing\Helpers;
class Bills extends ApiController
{
use Helpers;
/**
* Display a listing of the resource.
*
* @return \Dingo\Api\Http\Response
*/
public function index()
{
2017-09-18 16:58:25 +03:00
$bills = Bill::with(['vendor', 'status', 'items', 'payments', 'histories'])->collect();
2017-09-14 22:21:00 +03:00
return $this->response->paginator($bills, new Transformer());
}
/**
* Display the specified resource.
*
* @param Bill $bill
* @return \Dingo\Api\Http\Response
*/
public function show(Bill $bill)
{
return $this->response->item($bill, new Transformer());
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function store(Request $request)
{
$bill = Bill::create($request->all());
$bill_item = array();
$bill_item['company_id'] = $request['company_id'];
$bill_item['bill_id'] = $bill->id;
if ($request['item']) {
foreach ($request['item'] as $item) {
2017-10-11 14:05:17 +03:00
$item_id = 0;
2017-09-14 22:21:00 +03:00
$item_sku = '';
if (!empty($item['item_id'])) {
2017-10-07 19:37:58 +03:00
$item_object = Item::find($item['item_id']);
2017-09-14 22:21:00 +03:00
2017-10-11 14:05:17 +03:00
$item_id = $item['item_id'];
2018-04-10 17:36:48 +03:00
$item['name'] = $item_object->name;
2017-10-07 19:37:58 +03:00
$item_sku = $item_object->sku;
2017-11-22 00:26:39 +03:00
// Increase stock (item bought)
2017-12-11 10:00:14 +03:00
$item_object->quantity += $item['quantity'];
2017-11-22 00:26:39 +03:00
$item_object->save();
2017-10-11 14:05:17 +03:00
} elseif (!empty($item['sku'])) {
$item_sku = $item['sku'];
2017-09-14 22:21:00 +03:00
}
2017-10-07 19:37:58 +03:00
$tax = $tax_id = 0;
2017-09-14 22:21:00 +03:00
2017-10-07 19:37:58 +03:00
if (!empty($item['tax_id'])) {
$tax_object = Tax::find($item['tax_id']);
2017-09-14 22:21:00 +03:00
2017-10-07 19:37:58 +03:00
$tax_id = $item['tax_id'];
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
} elseif (!empty($item['tax'])) {
$tax = $item['tax'];
2017-09-14 22:21:00 +03:00
}
2017-10-11 14:05:17 +03:00
$bill_item['item_id'] = $item_id;
2017-12-24 09:25:48 +03:00
$bill_item['name'] = str_limit($item['name'], 180, '');
2017-09-14 22:21:00 +03:00
$bill_item['sku'] = $item_sku;
$bill_item['quantity'] = $item['quantity'];
$bill_item['price'] = $item['price'];
2017-10-07 19:37:58 +03:00
$bill_item['tax'] = $tax;
2017-09-14 22:21:00 +03:00
$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;
2017-10-10 18:32:01 +03:00
$request['status_code'] = $request['bill_status_code'];
2017-09-14 22:21:00 +03:00
$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));
}
/**
* Update the specified resource in storage.
*
* @param $bill
* @param $request
* @return \Dingo\Api\Http\Response
*/
public function update(Bill $bill, Request $request)
{
$bill_item = array();
$bill_item['company_id'] = $request['company_id'];
$bill_item['bill_id'] = $bill->id;
if ($request['item']) {
BillItem::where('bill_id', $bill->id)->delete();
foreach ($request['item'] as $item) {
2017-10-11 14:05:17 +03:00
$item_id = 0;
2017-09-14 22:21:00 +03:00
$item_sku = '';
if (!empty($item['item_id'])) {
2017-10-07 19:37:58 +03:00
$item_object = Item::find($item['item_id']);
2017-09-14 22:21:00 +03:00
2017-10-11 14:05:17 +03:00
$item_id = $item['item_id'];
2018-04-10 17:36:48 +03:00
$item['name'] = $item_object->name;
2017-10-07 19:37:58 +03:00
$item_sku = $item_object->sku;
2017-10-11 14:05:17 +03:00
} elseif (!empty($item['sku'])) {
$item_sku = $item['sku'];
2017-09-14 22:21:00 +03:00
}
2017-10-07 19:37:58 +03:00
$tax = $tax_id = 0;
if (!empty($item['tax_id'])) {
$tax_object = Tax::find($item['tax_id']);
2017-09-14 22:21:00 +03:00
2017-10-07 19:37:58 +03:00
$tax_id = $item['tax_id'];
2017-09-14 22:21:00 +03:00
2017-10-07 19:37:58 +03:00
$tax = (($item['price'] * $item['quantity']) / 100) * $tax_object->rate;
} elseif (!empty($item['tax'])) {
$tax = $item['tax'];
2017-09-14 22:21:00 +03:00
}
2017-10-11 14:05:17 +03:00
$bill_item['item_id'] = $item_id;
2017-12-24 09:25:48 +03:00
$bill_item['name'] = str_limit($item['name'], 180, '');
2017-09-14 22:21:00 +03:00
$bill_item['sku'] = $item_sku;
$bill_item['quantity'] = $item['quantity'];
$bill_item['price'] = $item['price'];
2017-10-07 19:37:58 +03:00
$bill_item['tax'] = $tax;
2017-09-14 22:21:00 +03:00
$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());
}
/**
* Remove the specified resource from storage.
*
* @param Bill $bill
* @return \Dingo\Api\Http\Response
*/
public function destroy(Bill $bill)
{
$bill->delete();
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();
}
}