akaunting/app/Http/Controllers/Modals/DocumentTransactions.php

211 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Modals;
use App\Abstracts\Http\Controller;
use App\Http\Requests\Banking\Transaction as Request;
2020-12-24 01:28:38 +03:00
use App\Jobs\Banking\CreateBankingDocumentTransaction;
2022-06-01 10:15:55 +03:00
use App\Jobs\Banking\UpdateBankingDocumentTransaction;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document;
2019-11-16 10:21:14 +03:00
use App\Models\Setting\Currency;
use App\Utilities\Modules;
use App\Traits\Uploads;
2022-06-01 10:15:55 +03:00
use App\Traits\Transactions;
2022-07-03 14:59:17 +03:00
use Date;
2022-06-01 10:15:55 +03:00
2019-11-16 10:21:14 +03:00
2021-01-13 23:04:41 +03:00
class DocumentTransactions extends Controller
2019-11-16 10:21:14 +03:00
{
2022-06-01 10:15:55 +03:00
use Uploads, Transactions;
2019-11-16 10:21:14 +03:00
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
// Add CRUD permission check
2021-01-13 23:04:41 +03:00
//$this->middleware('permission:create-sales-invoices')->only('create', 'store', 'duplicate', 'import');
//$this->middleware('permission:read-sales-invoices')->only('index', 'show', 'edit', 'export');
//$this->middleware('permission:update-sales-invoices')->only('update', 'enable', 'disable');
//$this->middleware('permission:delete-sales-invoices')->only('destroy');
2019-11-16 10:21:14 +03:00
}
/**
* Show the form for creating a new resource.
*
2021-01-13 23:04:41 +03:00
* @param Document $document
2019-11-16 10:21:14 +03:00
*
* @return Response
*/
2021-01-13 23:04:41 +03:00
public function create(Document $document)
2019-11-16 10:21:14 +03:00
{
2021-01-13 23:04:41 +03:00
$currency = Currency::where('code', $document->currency_code)->first();
2019-11-16 10:21:14 +03:00
2021-01-13 23:04:41 +03:00
$paid = $document->paid;
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
$number = $this->getNextTransactionNumber();
2021-01-13 23:04:41 +03:00
// Get document Totals
foreach ($document->totals as $document_total) {
$document->{$document_total->code} = $document_total->amount;
2019-11-16 10:21:14 +03:00
}
$total = money($document->total, $currency->code)->format();
2019-11-16 10:21:14 +03:00
$document->grand_total = money($total, $currency->code, false)->getAmount();
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($paid)) {
2021-01-13 23:04:41 +03:00
$document->grand_total = round($document->total - $paid, $currency->precision);
2019-11-16 10:21:14 +03:00
}
2022-07-03 14:59:17 +03:00
$document->paid_at = Date::now()->toDateString();
2022-06-01 10:15:55 +03:00
$buttons = [
'cancel' => [
'text' => trans('general.cancel'),
'class' => 'btn-outline-secondary'
],
'payment' => [
'text' => trans('invoices.accept_payments'),
'class' => 'long-texts',
2022-06-09 11:18:47 +03:00
'url' => route('apps.categories.show', [
'alias' => 'payment-method',
'utm_source' => $document->type . '_payment',
'utm_medium' => 'app',
'utm_campaign' => 'payment_method',
])
2022-06-01 10:15:55 +03:00
],
'confirm' => [
'text' => trans('general.save'),
'class' => 'disabled:bg-green-100'
],
];
2022-07-03 14:59:17 +03:00
$method = 'POST';
2022-06-01 10:15:55 +03:00
$route = ['modals.documents.document.transactions.store', $document->id];
2022-07-03 14:59:17 +03:00
$html = view('modals.documents.payment', compact('document', 'method', 'route', 'currency', 'number'))->render();
2019-11-16 10:21:14 +03:00
return response()->json([
'success' => true,
'error' => false,
'message' => 'null',
'html' => $html,
'data' => [
'title' => trans('general.title.new', ['type' => trans_choice('general.payments', 1)]),
2022-06-01 10:15:55 +03:00
'buttons' => $buttons,
]
2019-11-16 10:21:14 +03:00
]);
}
/**
* Store a newly created resource in storage.
*
2021-01-13 23:04:41 +03:00
* @param Document $document
2019-11-16 10:21:14 +03:00
* @param Request $request
*
* @return Response
*/
2021-01-13 23:04:41 +03:00
public function store(Document $document, Request $request)
2019-11-16 10:21:14 +03:00
{
2021-01-13 23:04:41 +03:00
$response = $this->ajaxDispatch(new CreateBankingDocumentTransaction($document, $request));
2020-02-11 19:17:50 +03:00
if ($response['success']) {
$response['redirect'] = url()->previous();
2022-06-01 10:15:55 +03:00
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();
} else {
$response['redirect'] = null;
}
return response()->json($response);
}
/**
* Show the form for creating a new resource.
*
* @param Document $document
*
* @return Response
*/
public function edit(Document $document, Transaction $transaction)
{
$currency = Currency::where('code', $document->currency_code)->first();
$paid = $document->paid;
2022-07-03 14:59:17 +03:00
$number = $transaction->number;
2022-06-01 10:15:55 +03:00
$document->grand_total = money($transaction->amount, $currency->code, false)->getAmount();
2022-06-01 10:15:55 +03:00
2022-07-03 14:59:17 +03:00
$document->paid_at = $transaction->paid_at;
2022-06-01 10:15:55 +03:00
$buttons = [
'cancel' => [
'text' => trans('general.cancel'),
'class' => 'btn-outline-secondary'
],
'payment' => [
'text' => trans('invoices.accept_payments'),
'class' => 'long-texts',
'url' => route('apps.categories.show', 'payment-method')
],
'confirm' => [
'text' => trans('general.save'),
'class' => 'disabled:bg-green-100'
],
];
2022-07-03 14:59:17 +03:00
$method = 'PATCH';
2022-06-01 10:15:55 +03:00
$route = ['modals.documents.document.transactions.update', $document->id, $transaction->id];
2022-07-03 14:59:17 +03:00
$html = view('modals.documents.payment', compact('document', 'transaction', 'method', 'route', 'currency', 'number'))->render();
2022-06-01 10:15:55 +03:00
return response()->json([
'success' => true,
'error' => false,
'message' => 'null',
'html' => $html,
'data' => [
'title' => trans('general.title.edit', ['type' => trans_choice('general.payments', 1)]),
'buttons' => $buttons,
]
]);
}
/**
* Update the specified resource in storage.
*
* @param $item
* @param $request
* @return Response
*/
public function update(Document $document, Transaction $transaction, Request $request)
{
$response = $this->ajaxDispatch(new UpdateBankingDocumentTransaction($document, $transaction, $request));
if ($response['success']) {
$route = config('type.document.' . $document->type . '.route.prefix');
2021-01-13 23:04:41 +03:00
2022-06-01 10:15:55 +03:00
if ($alias = config('type.document.' . $document->type . '.alias')) {
2021-01-13 23:04:41 +03:00
$route = $alias . '.' . $route;
}
$response['redirect'] = route($route . '.show', $document->id);
2019-11-16 10:21:14 +03:00
$message = trans('messages.success.updated', ['type' => trans_choice('general.payments', 1)]);
2019-11-16 10:21:14 +03:00
flash($message)->success();
2020-02-11 19:17:50 +03:00
} else {
$response['redirect'] = null;
2019-11-16 10:21:14 +03:00
}
return response()->json($response);
}
}