akaunting/app/Http/Controllers/Banking/Transactions.php

415 lines
12 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Banking;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
2022-06-01 10:15:55 +03:00
use App\Events\Banking\TransactionPrinting;
use App\Events\Banking\TransactionSent;
2019-11-16 10:21:14 +03:00
use App\Exports\Banking\Transactions as Export;
2022-06-01 10:15:55 +03:00
use App\Http\Requests\Banking\Transaction as Request;
use App\Http\Requests\Banking\TransactionConnect;
2019-11-16 10:21:14 +03:00
use App\Http\Requests\Common\Import as ImportRequest;
use App\Imports\Banking\Transactions as Import;
2022-06-01 10:15:55 +03:00
use App\Jobs\Banking\CreateTransaction;
2019-11-16 10:21:14 +03:00
use App\Jobs\Banking\DeleteTransaction;
2022-06-06 14:53:05 +03:00
use App\Jobs\Banking\DuplicateTransaction;
2022-06-01 10:15:55 +03:00
use App\Jobs\Banking\MatchBankingDocumentTransaction;
use App\Jobs\Banking\SplitTransaction;
use App\Jobs\Banking\UpdateTransaction;
2017-10-31 00:50:41 +03:00
use App\Models\Banking\Account;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Transaction;
2022-06-01 10:15:55 +03:00
use App\Models\Document\Document;
use App\Models\Setting\Currency;
use App\Notifications\Banking\Transaction as Notification;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Transactions as TransactionsTrait;
2017-09-14 22:21:00 +03:00
class Transactions extends Controller
{
2022-06-01 10:15:55 +03:00
use Currencies, DateTime, TransactionsTrait;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$transactions = Transaction::with('account', 'category', 'contact')->collect(['paid_at'=> 'desc']);
2022-06-01 10:15:55 +03:00
$totals = [
'income' => 0,
'expense' => 0,
'profit' => 0,
];
$transactions->each(function ($transaction) use (&$totals) {
2022-07-20 17:29:55 +03:00
if (($transaction->isNotIncome() && $transaction->isNotExpense()) || $transaction->isTransferTransaction()) {
2022-06-01 10:15:55 +03:00
return;
}
2022-06-05 03:48:31 +03:00
$type = $transaction->isIncome() ? 'income' : 'expense';
$totals[$type] += $transaction->getAmountConvertedToDefault();
2022-06-01 10:15:55 +03:00
});
$totals['profit'] = $totals['income'] - $totals['expense'];
$incoming_amount = money($totals['income'], default_currency(), true);
$expense_amount = money($totals['expense'], default_currency(), true);
$profit_amount = money($totals['profit'], default_currency(), true);
$summary_amounts = [
'incoming_exact' => $incoming_amount->format(),
'incoming_for_humans' => $incoming_amount->formatForHumans(),
'expense_exact' => $expense_amount->format(),
'expense_for_humans' => $expense_amount->formatForHumans(),
'profit_exact' => $profit_amount->format(),
'profit_for_humans' => $profit_amount->formatForHumans(),
];
2022-06-01 10:15:55 +03:00
$translations = $this->getTranslationsForConnect('income');
return $this->response('banking.transactions.index', compact(
'transactions',
'translations',
'summary_amounts'
2022-06-01 10:15:55 +03:00
));
}
/**
* Show the form for viewing the specified resource.
*
* @return Response
*/
public function show(Transaction $transaction)
{
2022-06-05 03:51:17 +03:00
$title = $transaction->isIncome() ? trans_choice('general.receipts', 1) : trans('transactions.payment_made');
$real_type = $this->getRealTypeTransaction($transaction->type);
2022-06-01 10:15:55 +03:00
return view('banking.transactions.show', compact('transaction', 'title', 'real_type'));
2022-06-01 10:15:55 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$type = $this->getTypeTransaction(request()->get('type', 'income'));
$real_type = $this->getRealTypeTransaction($type);
2022-06-01 10:15:55 +03:00
2023-05-15 19:10:45 +03:00
$number = $this->getNextTransactionNumber($type);
2022-06-01 10:15:55 +03:00
$contact_type = config('type.transaction.' . $type . '.contact_type');
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
$currency = Currency::where('code', $account_currency_code)->first();
return view('banking.transactions.create', compact(
'type',
'real_type',
2022-06-01 10:15:55 +03:00
'number',
'contact_type',
'account_currency_code',
'currency'
));
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*/
public function store(Request $request)
{
$response = $this->ajaxDispatch(new CreateTransaction($request));
if ($response['success']) {
$response['redirect'] = route('transactions.show', $response['data']->id);
2017-10-31 00:50:41 +03:00
2022-06-01 10:15:55 +03:00
$message = trans('messages.success.added', ['type' => trans_choice('general.transactions', 1)]);
2017-10-31 00:50:41 +03:00
2022-06-01 10:15:55 +03:00
flash($message)->success();
} else {
$response['redirect'] = route('transactions.create');
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
$message = $response['message'];
flash($message)->error()->important();
}
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
return response()->json($response);
}
/**
* Duplicate the specified resource.
*
* @param Transaction $transaction
*
* @return Response
*/
public function duplicate(Transaction $transaction)
{
2022-06-06 14:53:05 +03:00
$clone = $this->dispatch(new DuplicateTransaction($transaction));
2022-06-01 10:15:55 +03:00
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.transactions', 1)]);
flash($message)->success();
return redirect()->route('transactions.edit', $clone->id);
2017-12-26 11:56:43 +03:00
}
2017-09-14 22:21:00 +03:00
2017-12-26 11:56:43 +03:00
/**
2019-11-16 10:21:14 +03:00
* Import the specified resource.
*
* @param ImportRequest $request
2017-12-26 11:56:43 +03:00
*
2019-11-16 10:21:14 +03:00
* @return Response
2017-12-26 11:56:43 +03:00
*/
2019-11-16 10:21:14 +03:00
public function import(ImportRequest $request)
2017-12-26 11:56:43 +03:00
{
2021-04-16 00:59:43 +03:00
$response = $this->importExcel(new Import, $request, trans_choice('general.transactions', 2));
2019-11-16 10:21:14 +03:00
2021-02-11 18:08:06 +03:00
if ($response['success']) {
$response['redirect'] = route('transactions.index');
2019-11-16 10:21:14 +03:00
2021-04-16 00:59:43 +03:00
flash($response['message'])->success();
2021-02-11 18:08:06 +03:00
} else {
$response['redirect'] = route('import.create', ['banking', 'transactions']);
2021-04-16 00:59:43 +03:00
flash($response['message'])->error()->important();
2021-02-11 18:08:06 +03:00
}
return response()->json($response);
2017-12-26 11:56:43 +03:00
}
2017-09-14 22:21:00 +03:00
2022-06-01 10:15:55 +03:00
/**
* Show the form for editing the specified resource.
*
* @param Transaction $transaction
*
* @return Response
*/
public function edit(Transaction $transaction)
{
$type = $transaction->type;
$contact_type = config('type.transaction.' . $type . '.contact_type');
$currency = Currency::where('code', $transaction->currency_code)->first();
$date_format = $this->getCompanyDateFormat();
return view('banking.transactions.edit', compact(
'type',
'contact_type',
'transaction',
'currency',
'date_format'
));
}
/**
* Update the specified resource in storage.
*
* @param Transaction $transaction
* @param Request $request
*
* @return Response
*/
public function update(Transaction $transaction, Request $request)
{
$response = $this->ajaxDispatch(new UpdateTransaction($transaction, $request));
if ($response['success']) {
$response['redirect'] = route('transactions.show', $transaction->id);
$message = trans('messages.success.updated', ['type' => trans_choice('general.transactions', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('transactions.edit', $transaction->id);
$message = $response['message'];
flash($message)->error()->important();
}
return response()->json($response);
}
2019-11-16 10:21:14 +03:00
/**
* Remove the specified resource from storage.
*
* @param Transaction $transaction
*
* @return Response
*/
public function destroy(Transaction $transaction)
2017-12-26 11:56:43 +03:00
{
2019-11-16 10:21:14 +03:00
$response = $this->ajaxDispatch(new DeleteTransaction($transaction));
$response['redirect'] = url()->previous();
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => trans_choice('general.transactions', 1)]);
flash($message)->success();
2017-12-26 11:56:43 +03:00
} else {
2019-11-16 10:21:14 +03:00
$message = $response['message'];
flash($message)->error()->important();
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
return response()->json($response);
}
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
return $this->exportExcel(new Export, trans_choice('general.transactions', 2));
2017-09-14 22:21:00 +03:00
}
2022-06-01 10:15:55 +03:00
/**
* Download the PDF file of transaction.
*
* @param Transaction $transaction
*
* @return Response
*/
public function emailTransaction(Transaction $transaction)
{
if (empty($transaction->contact->email)) {
return redirect()->back();
}
// Notify the customer/vendor
$transaction->contact->notify(new Notification($transaction, config('type.transaction.' . $transaction->type . '.email_template'), true));
event(new TransactionSent($transaction));
flash(trans('documents.messages.email_sent', ['type' => trans_choice('general.transactions', 1)]))->success();
return redirect()->back();
}
/**
* Print the transaction.
*
* @param Transaction $transaction
*
* @return Response
*/
public function printTransaction(Transaction $transaction)
{
event(new TransactionPrinting($transaction));
$view = view('banking.transactions.print_default', compact('transaction'));
return mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
}
/**
* Download the PDF file of transaction.
*
* @param Transaction $transaction
*
* @return Response
*/
public function pdfTransaction(Transaction $transaction)
{
event(new TransactionPrinting($transaction));
$currency_style = true;
$view = view('banking.transactions.print_default', compact('transaction', 'currency_style'))->render();
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
$pdf = app('dompdf.wrapper');
$pdf->loadHTML($html);
//$pdf->setPaper('A4', 'portrait');
$file_name = $this->getTransactionFileName($transaction);
return $pdf->download($file_name);
}
public function dial(Transaction $transaction)
{
$documents = collect([]);
2022-06-05 03:51:17 +03:00
if ($transaction->isIncome() && $transaction->contact->exists) {
$builder = $transaction->contact->invoices();
}
2022-06-05 03:51:17 +03:00
if ($transaction->isIncome() && ! $transaction->contact->exists) {
$builder = Document::invoice();
}
2022-06-05 03:51:17 +03:00
if ($transaction->isExpense() && $transaction->contact->exists) {
$builder = $transaction->contact->bills();
}
2022-06-05 03:51:17 +03:00
if ($transaction->isExpense() && ! $transaction->contact->exists) {
$builder = Document::bill();
}
if (isset($builder)) {
$documents = $builder->notPaid()
2022-06-05 03:51:17 +03:00
->where('currency_code', $transaction->currency_code)
->with(['media', 'totals', 'transactions'])
->get()
->toJson();
}
$data = [
'transaction' => $transaction->load(['account', 'category'])->toJson(),
'currency' => $transaction->currency->toJson(),
'documents' => $documents,
];
return response()->json($data);
}
2022-06-01 10:15:55 +03:00
public function connect(Transaction $transaction, TransactionConnect $request)
{
$total_items = count($request->data['items']);
if ($total_items == 1) {
$document = Document::find($request->data['items'][0]['document_id']);
if (!is_null($document)) {
$response = $this->ajaxDispatch(new MatchBankingDocumentTransaction($document, $transaction));
}
}
if ($total_items > 1) {
$response = $this->ajaxDispatch(new SplitTransaction($transaction, $request->data));
}
$response['redirect'] = route('transactions.index');
if ($response['success']) {
$message = trans('messages.success.connected', ['type' => trans_choice('general.transactions', 1)]);
flash($message)->success();
} else {
$message = $response['message'];
flash($message)->error()->important();
}
return response()->json($response);
}
2017-09-14 22:21:00 +03:00
}