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-02-11 21:55:13 +03:00
|
|
|
use App\Jobs\Banking\CreateDocumentTransaction;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Banking\Account;
|
|
|
|
use App\Models\Banking\Transaction;
|
2019-12-31 15:49:09 +03:00
|
|
|
use App\Models\Sale\Invoice;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Setting\Currency;
|
|
|
|
use App\Utilities\Modules;
|
|
|
|
use App\Traits\Uploads;
|
|
|
|
|
|
|
|
class InvoiceTransactions extends Controller
|
|
|
|
{
|
|
|
|
use Uploads;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new controller instance.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Add CRUD permission check
|
2020-06-21 18:00:31 +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');
|
2019-12-31 15:49:09 +03:00
|
|
|
$this->middleware('permission:delete-sales-invoices')->only('destroy');
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @param Invoice $invoice
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
|
|
|
|
|
|
|
|
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
|
|
|
|
|
|
|
|
$currency = Currency::where('code', $invoice->currency_code)->first();
|
|
|
|
|
|
|
|
$payment_methods = Modules::getPaymentMethods();
|
|
|
|
|
2020-02-11 21:55:13 +03:00
|
|
|
$paid = $invoice->paid;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
// Get Invoice Totals
|
|
|
|
foreach ($invoice->totals as $invoice_total) {
|
|
|
|
$invoice->{$invoice_total->code} = $invoice_total->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total = money($invoice->total, $currency->code, true)->format();
|
|
|
|
|
|
|
|
$invoice->grand_total = money($total, $currency->code)->getAmount();
|
|
|
|
|
|
|
|
if (!empty($paid)) {
|
2020-02-19 18:44:53 +03:00
|
|
|
$invoice->grand_total = round($invoice->total - $paid, $currency->precision);
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 18:44:53 +03:00
|
|
|
$html = view('modals.invoices.payment', compact('invoice', 'accounts', 'currencies', 'currency', 'payment_methods'))->render();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'error' => false,
|
|
|
|
'message' => 'null',
|
|
|
|
'html' => $html,
|
2020-06-09 18:19:17 +03:00
|
|
|
'data' => [
|
|
|
|
'title' => trans('general.title.new', ['type' => trans_choice('general.payments', 1)]),
|
|
|
|
'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' => 'btn-success'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
2019-11-16 10:21:14 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param Invoice $invoice
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function store(Invoice $invoice, Request $request)
|
|
|
|
{
|
2020-02-11 21:55:13 +03:00
|
|
|
$response = $this->ajaxDispatch(new CreateDocumentTransaction($invoice, $request));
|
2020-02-11 19:17:50 +03:00
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
$response['redirect'] = route('invoices.show', $invoice->id);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
|
|
|
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|