2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
2018-06-10 02:48:51 +03:00
|
|
|
namespace App\Http\Controllers\Common;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Http\Controller;
|
|
|
|
use App\Exports\Common\Items as Export;
|
2018-06-10 02:48:51 +03:00
|
|
|
use App\Http\Requests\Common\Item as Request;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Http\Requests\Common\Import as ImportRequest;
|
|
|
|
use App\Http\Requests\Common\TotalItem as TotalRequest;
|
|
|
|
use App\Imports\Common\Items as Import;
|
|
|
|
use App\Jobs\Common\CreateItem;
|
|
|
|
use App\Jobs\Common\DeleteItem;
|
|
|
|
use App\Jobs\Common\UpdateItem;
|
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\Category;
|
|
|
|
use App\Models\Setting\Currency;
|
|
|
|
use App\Models\Setting\Tax;
|
|
|
|
use App\Traits\Uploads;
|
|
|
|
|
|
|
|
class Items extends Controller
|
|
|
|
{
|
|
|
|
use Uploads;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2020-06-07 12:11:37 +03:00
|
|
|
$items = Item::with('category', 'media')->collect();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-11-06 00:43:46 +03:00
|
|
|
return $this->response('common.items.index', compact('items'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-16 13:59:53 +03:00
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show()
|
|
|
|
{
|
2018-06-11 03:47:32 +03:00
|
|
|
return redirect()->route('items.index');
|
2018-04-16 13:59:53 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2020-05-03 11:15:56 +03:00
|
|
|
$categories = Category::item()->enabled()->orderBy('name')->pluck('name', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-07 19:56:11 +03:00
|
|
|
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-05-01 17:42:16 +03:00
|
|
|
return view('common.items.create', compact('categories', 'taxes'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param $request
|
2017-09-14 22:21:00 +03:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new CreateItem($request));
|
2017-12-28 17:20:16 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('items.index');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$response['redirect'] = route('items.create');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-26 15:20:17 +03:00
|
|
|
/**
|
|
|
|
* Duplicate the specified resource.
|
|
|
|
*
|
|
|
|
* @param Item $item
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function duplicate(Item $item)
|
|
|
|
{
|
|
|
|
$clone = $item->duplicate();
|
|
|
|
|
|
|
|
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.items', 1)]);
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
|
2019-03-15 18:28:57 +03:00
|
|
|
return redirect()->route('items.edit', $clone->id);
|
2017-11-26 15:20:17 +03:00
|
|
|
}
|
|
|
|
|
2017-11-30 11:47:56 +03:00
|
|
|
/**
|
|
|
|
* Import the specified resource.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param ImportRequest $request
|
2017-11-30 11:47:56 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2019-11-16 10:21:14 +03:00
|
|
|
public function import(ImportRequest $request)
|
2017-11-30 11:47:56 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
\Excel::import(new Import(), $request->file('import'));
|
2017-11-30 11:47:56 +03:00
|
|
|
|
|
|
|
$message = trans('messages.success.imported', ['type' => trans_choice('general.items', 2)]);
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
|
2018-06-11 03:47:32 +03:00
|
|
|
return redirect()->route('items.index');
|
2017-11-30 11:47:56 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param Item $item
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit(Item $item)
|
|
|
|
{
|
2020-05-03 11:15:56 +03:00
|
|
|
$categories = Category::item()->enabled()->orderBy('name')->pluck('name', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-07 19:56:11 +03:00
|
|
|
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return view('common.items.edit', compact('item', 'categories', 'taxes'));
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param $item
|
|
|
|
* @param $request
|
2017-09-14 22:21:00 +03:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(Item $item, Request $request)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateItem($item, $request));
|
2017-12-28 17:20:16 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('items.index');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('messages.success.updated', ['type' => $item->name]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
|
|
|
$response['redirect'] = route('items.edit', $item->id);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($response);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-11 11:19:30 +03:00
|
|
|
/**
|
|
|
|
* Enable the specified resource.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param Item $item
|
2018-06-11 11:19:30 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function enable(Item $item)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateItem($item, request()->merge(['enabled' => 1])));
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.enabled', ['type' => $item->name]);
|
|
|
|
}
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2018-06-11 11:19:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the specified resource.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param Item $item
|
2018-06-11 11:19:30 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function disable(Item $item)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new UpdateItem($item, request()->merge(['enabled' => 0])));
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$response['message'] = trans('messages.success.disabled', ['type' => $item->name]);
|
|
|
|
}
|
2018-06-11 11:19:30 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2018-06-11 11:19:30 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param Item $item
|
2017-09-14 22:21:00 +03:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy(Item $item)
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$response = $this->ajaxDispatch(new DeleteItem($item));
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$response['redirect'] = route('items.index');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($response['success']) {
|
|
|
|
$message = trans('messages.success.deleted', ['type' => $item->name]);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
} else {
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = $response['message'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
flash($message)->error();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return response()->json($response);
|
2018-06-11 03:47:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the specified resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function export()
|
|
|
|
{
|
2020-02-12 12:18:08 +03:00
|
|
|
return \Excel::download(new Export(), \Str::filename(trans_choice('general.items', 2)) . '.xlsx');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function autocomplete()
|
|
|
|
{
|
|
|
|
$type = request('type');
|
|
|
|
$query = request('query');
|
|
|
|
$currency_code = request('currency_code');
|
|
|
|
|
|
|
|
if (empty($currency_code) || (strtolower($currency_code) == 'null')) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$currency_code = setting('default.currency');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-14 09:46:37 +03:00
|
|
|
$autocomplete = Item::autocomplete([
|
2019-11-16 10:21:14 +03:00
|
|
|
'name' => $query
|
2018-05-14 09:46:37 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$items = $autocomplete->get();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($items) {
|
|
|
|
foreach ($items as $item) {
|
2017-10-07 19:37:58 +03:00
|
|
|
$tax = Tax::find($item->tax_id);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$item_tax_price = 0;
|
|
|
|
|
|
|
|
if (!empty($tax)) {
|
|
|
|
$item_tax_price = ($item->sale_price / 100) * $tax->rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'bill':
|
|
|
|
$total = $item->purchase_price + $item_tax_price;
|
|
|
|
break;
|
|
|
|
case 'invoice':
|
|
|
|
default:
|
|
|
|
$total = $item->sale_price + $item_tax_price;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->total = money($total, $currency_code, true)->format();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:48:46 +03:00
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'message' => 'Get all items.',
|
|
|
|
'errors' => [],
|
|
|
|
'count' => $items->count(),
|
|
|
|
'data' => ($items->count()) ? $items : null,
|
|
|
|
]);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function total(TotalRequest $request)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
$input_items = $request->input('items');
|
2018-09-03 13:24:27 +03:00
|
|
|
$currency_code = $request->input('currency_code');
|
|
|
|
$discount = $request->input('discount');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if (empty($currency_code)) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$currency_code = setting('default.currency');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$json = new \stdClass();
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$sub_total = 0;
|
|
|
|
$tax_total = 0;
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$items = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($input_items) {
|
|
|
|
foreach ($input_items as $key => $item) {
|
2018-08-13 21:14:58 +03:00
|
|
|
$price = (double) $item['price'];
|
2018-04-30 11:49:47 +03:00
|
|
|
$quantity = (double) $item['quantity'];
|
2017-12-05 17:00:54 +03:00
|
|
|
|
2018-10-22 15:53:56 +03:00
|
|
|
$item_tax_total = 0;
|
2018-11-06 17:55:31 +03:00
|
|
|
$item_tax_amount = 0;
|
|
|
|
|
2017-12-05 17:00:54 +03:00
|
|
|
$item_sub_total = ($price * $quantity);
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_discounted_total = $item_sub_total;
|
2018-11-06 17:55:31 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
// Apply discount to amount
|
2018-11-06 17:55:31 +03:00
|
|
|
if ($discount) {
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_discounted_total = $item_sub_total - ($item_sub_total * ($discount / 100));
|
2018-11-06 17:55:31 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-10-07 19:37:58 +03:00
|
|
|
if (!empty($item['tax_id'])) {
|
2019-12-07 12:54:13 +03:00
|
|
|
$inclusives = $compounds = [];
|
2018-11-06 17:55:31 +03:00
|
|
|
|
2018-10-22 15:53:56 +03:00
|
|
|
foreach ($item['tax_id'] as $tax_id) {
|
|
|
|
$tax = Tax::find($tax_id);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-06 17:55:31 +03:00
|
|
|
switch ($tax->type) {
|
2018-11-07 12:07:38 +03:00
|
|
|
case 'inclusive':
|
|
|
|
$inclusives[] = $tax;
|
2018-11-06 17:55:31 +03:00
|
|
|
break;
|
|
|
|
case 'compound':
|
|
|
|
$compounds[] = $tax;
|
|
|
|
break;
|
2019-11-16 10:21:14 +03:00
|
|
|
case 'fixed':
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_tax_total += $tax->rate * $quantity;
|
2019-11-16 10:21:14 +03:00
|
|
|
break;
|
2018-11-06 17:55:31 +03:00
|
|
|
default:
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_tax_amount = ($item_discounted_total / 100) * $tax->rate;
|
2018-11-06 17:55:31 +03:00
|
|
|
|
|
|
|
$item_tax_total += $item_tax_amount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2018-11-07 12:07:38 +03:00
|
|
|
if ($inclusives) {
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_sub_and_tax_total = $item_discounted_total + $item_tax_total;
|
2018-11-06 17:55:31 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_base_rate = $item_sub_and_tax_total / (1 + collect($inclusives)->sum('rate') / 100);
|
2019-02-03 21:23:50 +05:30
|
|
|
$item_tax_total = $item_sub_and_tax_total - $item_base_rate;
|
2018-11-06 17:55:31 +03:00
|
|
|
|
2019-02-03 21:23:50 +05:30
|
|
|
$item_sub_total = $item_base_rate + $discount;
|
2018-11-06 17:55:31 +03:00
|
|
|
}
|
2018-04-17 16:40:52 +03:00
|
|
|
|
2018-11-06 17:55:31 +03:00
|
|
|
if ($compounds) {
|
|
|
|
foreach ($compounds as $compound) {
|
2019-12-07 12:54:13 +03:00
|
|
|
$item_tax_total += (($item_discounted_total + $item_tax_total) / 100) * $compound->rate;
|
2018-11-06 17:55:31 +03:00
|
|
|
}
|
2018-10-22 15:53:56 +03:00
|
|
|
}
|
2018-04-17 16:40:52 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:53:56 +03:00
|
|
|
$sub_total += $item_sub_total;
|
2017-09-14 22:21:00 +03:00
|
|
|
$tax_total += $item_tax_total;
|
|
|
|
|
2018-04-17 17:05:44 +03:00
|
|
|
$items[$key] = money($item_sub_total, $currency_code, true)->format();
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$json->items = $items;
|
|
|
|
|
|
|
|
$json->sub_total = money($sub_total, $currency_code, true)->format();
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$json->discount_text = trans('invoices.add_discount');
|
2018-04-28 18:43:43 +03:00
|
|
|
$json->discount_total = '';
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$json->tax_total = money($tax_total, $currency_code, true)->format();
|
|
|
|
|
2018-04-17 16:40:52 +03:00
|
|
|
// Apply discount to total
|
|
|
|
if ($discount) {
|
2019-12-07 12:54:13 +03:00
|
|
|
$json->discount_text = trans('invoices.show_discount', ['discount' => $discount]);
|
2018-04-28 18:43:43 +03:00
|
|
|
$json->discount_total = money($sub_total * ($discount / 100), $currency_code, true)->format();
|
|
|
|
|
2018-04-17 16:40:52 +03:00
|
|
|
$sub_total = $sub_total - ($sub_total * ($discount / 100));
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$grand_total = $sub_total + $tax_total;
|
|
|
|
|
|
|
|
$json->grand_total = money($grand_total, $currency_code, true)->format();
|
|
|
|
|
2018-08-18 17:16:19 +03:00
|
|
|
// Get currency object
|
|
|
|
$currency = Currency::where('code', $currency_code)->first();
|
|
|
|
|
|
|
|
$json->currency_name = $currency->name;
|
|
|
|
$json->currency_code = $currency_code;
|
|
|
|
$json->currency_rate = $currency->rate;
|
|
|
|
|
|
|
|
$json->thousands_separator = $currency->thousands_separator;
|
|
|
|
$json->decimal_mark = $currency->decimal_mark;
|
|
|
|
$json->precision = (int) $currency->precision;
|
|
|
|
$json->symbol_first = $currency->symbol_first;
|
|
|
|
$json->symbol = $currency->symbol;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
return response()->json($json);
|
|
|
|
}
|
|
|
|
}
|