160 lines
4.7 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Expenses;
use App\Http\Controllers\Controller;
use App\Http\Requests\Expense\Payment as Request;
use App\Models\Banking\Account;
use App\Models\Expense\Payment;
use App\Models\Expense\Vendor;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Uploads;
class Payments extends Controller
{
use Uploads;
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2017-09-18 16:58:25 +03:00
$payments = Payment::with(['account', 'category'])->collect();
2017-09-14 22:21:00 +03:00
2017-09-27 21:40:25 +03:00
$vendors = collect(Vendor::enabled()->pluck('name', 'id'))
->prepend(trans('general.all_type', ['type' => trans_choice('general.vendors', 2)]), '');
2017-09-14 22:21:00 +03:00
$categories = collect(Category::enabled()->type('expense')->pluck('name', 'id'))
2017-09-27 21:40:25 +03:00
->prepend(trans('general.all_type', ['type' => trans_choice('general.categories', 2)]), '');
2017-09-14 22:21:00 +03:00
$accounts = collect(Account::enabled()->pluck('name', 'id'))
2017-09-27 21:40:25 +03:00
->prepend(trans('general.all_type', ['type' => trans_choice('general.accounts', 2)]), '');
2017-09-14 22:21:00 +03:00
2017-09-27 21:40:25 +03:00
return view('expenses.payments.index', compact('payments', 'vendors', 'categories', 'accounts'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$accounts = Account::enabled()->pluck('name', 'id');
$currencies = Currency::enabled()->pluck('name', 'code')->toArray();
$account_currency_code = Account::where('id', setting('general.default_account'))->pluck('currency_code')->first();
$vendors = Vendor::enabled()->pluck('name', 'id');
$categories = Category::enabled()->type('expense')->pluck('name', 'id');
return view('expenses.payments.create', compact('accounts', 'currencies', 'account_currency_code', 'vendors', 'categories'));
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*/
public function store(Request $request)
{
// Get currency object
$currency = Currency::where('code', $request['currency_code'])->first();
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
Payment::create($request->input());
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();
return redirect('expenses/payments');
}
/**
* Show the form for editing the specified resource.
*
* @param Payment $payment
*
* @return Response
*/
public function edit(Payment $payment)
{
$accounts = Account::enabled()->pluck('name', 'id');
$currencies = Currency::enabled()->pluck('name', 'code')->toArray();
$account_currency_code = Account::where('id', $payment->account_id)->pluck('currency_code')->first();
$vendors = Vendor::enabled()->pluck('name', 'id');
$categories = Category::enabled()->type('expense')->pluck('name', 'id');
return view('expenses.payments.edit', compact('payment', 'accounts', 'currencies', 'account_currency_code', 'vendors', 'categories'));
}
/**
* Update the specified resource in storage.
*
* @param Payment $payment
* @param Request $request
*
* @return Response
*/
public function update(Payment $payment, Request $request)
{
// Get currency object
$currency = Currency::where('code', $request['currency_code'])->first();
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$payment->update($request->input());
$message = trans('messages.success.updated', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();
return redirect('expenses/payments');
}
/**
* Remove the specified resource from storage.
*
* @param Payment $payment
*
* @return Response
*/
public function destroy(Payment $payment)
{
$payment->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();
return redirect('expenses/payments');
}
}