257 lines
7.3 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Http\Controllers\Purchases;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
2019-12-31 15:49:09 +03:00
use App\Exports\Purchases\Payments as Export;
2019-11-16 10:21:14 +03:00
use App\Http\Requests\Banking\Transaction as Request;
use App\Http\Requests\Common\Import as ImportRequest;
2019-12-31 15:49:09 +03:00
use App\Imports\Purchases\Payments as Import;
2019-11-16 10:21:14 +03:00
use App\Jobs\Banking\CreateTransaction;
use App\Jobs\Banking\DeleteTransaction;
use App\Jobs\Banking\UpdateTransaction;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
use App\Models\Common\Contact;
2017-09-14 22:21:00 +03:00
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
2019-11-16 10:21:14 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Utilities\Modules;
2017-09-14 22:21:00 +03:00
class Payments extends Controller
{
2020-04-07 14:39:35 +03:00
use Currencies, DateTime;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2020-06-07 12:11:37 +03:00
$payments = Transaction::with('account', 'bill', 'category', 'contact')->expense()->isNotTransfer()->collect(['paid_at'=> 'desc']);
2017-09-14 22:21:00 +03:00
return $this->response('purchases.payments.index', compact('payments'));
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()
{
2019-11-16 10:21:14 +03:00
return redirect()->route('payments.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()
{
2018-06-29 14:42:15 +03:00
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
2017-09-14 22:21:00 +03:00
2018-11-29 12:51:01 +03:00
$currency = Currency::where('code', $account_currency_code)->first();
$vendors = Contact::vendor()->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
$categories = Category::expense()->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
$payment_methods = Modules::getPaymentMethods();
2021-02-09 11:52:01 +03:00
$file_type_mimes = explode(',', config('filesystems.mimes'));
$file_types = [];
foreach ($file_type_mimes as $mime) {
$file_types[] = '.' . $mime;
}
$file_types = implode(',', $file_types);
return view('purchases.payments.create', compact('accounts', 'currencies', 'account_currency_code', 'currency', 'vendors', 'categories', 'payment_methods', 'file_types'));
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*/
public function store(Request $request)
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new CreateTransaction($request));
2017-12-28 17:20:16 +03:00
2019-11-16 10:21:14 +03:00
if ($response['success']) {
$response['redirect'] = route('payments.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.payments', 1)]);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
} else {
$response['redirect'] = route('payments.create');
2018-04-26 18:40:04 +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();
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
2017-11-26 15:20:17 +03:00
/**
* Duplicate the specified resource.
*
2019-11-16 10:21:14 +03:00
* @param Transaction $payment
2017-11-26 15:20:17 +03:00
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function duplicate(Transaction $payment)
2017-11-26 15:20:17 +03:00
{
$clone = $payment->duplicate();
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();
2019-11-16 10:21:14 +03:00
return redirect()->route('payments.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
{
if (true !== $result = $this->importExcel(new Import, $request, 'purchases/payments')) {
return $result;
}
2017-11-30 11:47:56 +03:00
$message = trans('messages.success.imported', ['type' => trans_choice('general.payments', 2)]);
flash($message)->success();
2019-11-16 10:21:14 +03:00
return redirect()->route('payments.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.
*
2019-11-16 10:21:14 +03:00
* @param Transaction $payment
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function edit(Transaction $payment)
2017-09-14 22:21:00 +03:00
{
2018-06-29 14:42:15 +03:00
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
2017-09-14 22:21:00 +03:00
2018-11-29 12:51:01 +03:00
$currency = Currency::where('code', $payment->currency_code)->first();
$vendors = Contact::vendor()->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2020-12-31 09:32:00 +03:00
if ($payment->contact && !$vendors->has($payment->contact_id)) {
$vendors->put($payment->contact->id, $payment->contact->name);
}
$categories = Category::expense()->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2020-12-31 09:32:00 +03:00
if ($payment->category && !$categories->has($payment->category_id)) {
2020-12-31 00:15:04 +03:00
$categories->put($payment->category->id, $payment->category->name);
}
$payment_methods = Modules::getPaymentMethods();
2019-11-16 10:21:14 +03:00
$date_format = $this->getCompanyDateFormat();
2021-02-09 11:52:01 +03:00
$file_type_mimes = explode(',', config('filesystems.mimes'));
$file_types = [];
foreach ($file_type_mimes as $mime) {
$file_types[] = '.' . $mime;
}
$file_types = implode(',', $file_types);
return view('purchases.payments.edit', compact('payment', 'accounts', 'currencies', 'currency', 'vendors', 'categories', 'payment_methods', 'date_format', 'file_types'));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
2019-11-16 10:21:14 +03:00
* @param Transaction $payment
* @param Request $request
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function update(Transaction $payment, Request $request)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new UpdateTransaction($payment, $request));
2017-12-28 17:20:16 +03:00
2019-11-16 10:21:14 +03:00
if ($response['success']) {
$response['redirect'] = route('payments.index');
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$message = trans('messages.success.updated', ['type' => trans_choice('general.payments', 1)]);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
} else {
$response['redirect'] = route('payments.edit', $payment->id);
2018-04-26 18:40:04 +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();
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
/**
* Remove the specified resource from storage.
*
2019-11-16 10:21:14 +03:00
* @param Transaction $payment
2017-09-14 22:21:00 +03:00
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function destroy(Transaction $payment)
2017-09-14 22:21:00 +03:00
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new DeleteTransaction($payment));
2017-12-05 18:31:18 +03:00
2019-11-16 10:21:14 +03:00
$response['redirect'] = route('payments.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' => trans_choice('general.payments', 1)]);
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
flash($message)->success();
} else {
$message = $response['message'];
flash($message)->error();
}
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
return response()->json($response);
2017-09-14 22:21:00 +03:00
}
2018-06-11 03:47:32 +03:00
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
return $this->exportExcel(new Export, trans_choice('general.payments', 2));
2018-06-11 03:47:32 +03:00
}
2017-09-14 22:21:00 +03:00
}