2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Customers;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2017-11-17 02:08:31 +03:00
|
|
|
use App\Http\Requests\Customer\InvoicePayment as PaymentRequest;
|
2017-11-18 16:57:55 +03:00
|
|
|
use App\Http\Requests\Customer\InvoiceConfirm as ConfirmRequest;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Models\Banking\Account;
|
|
|
|
use App\Models\Income\Customer;
|
|
|
|
use App\Models\Income\Invoice;
|
2017-11-18 16:57:55 +03:00
|
|
|
use App\Models\Income\InvoicePayment;
|
|
|
|
use App\Models\Income\InvoiceHistory;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Models\Income\InvoiceStatus;
|
|
|
|
use App\Models\Setting\Category;
|
|
|
|
use App\Models\Setting\Currency;
|
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
|
|
|
use App\Traits\Uploads;
|
|
|
|
use Auth;
|
2017-11-18 16:57:55 +03:00
|
|
|
use Date;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-17 02:08:31 +03:00
|
|
|
use App\Events\PaymentGatewayConfirm;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Utilities\Modules;
|
|
|
|
|
|
|
|
class Invoices extends Controller
|
|
|
|
{
|
|
|
|
use DateTime, Currencies, Uploads;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$invoices = Invoice::with('status')->where('customer_id', '=', Auth::user()->customer->id)->paginate();
|
|
|
|
|
|
|
|
foreach ($invoices as $invoice) {
|
|
|
|
$paid = 0;
|
|
|
|
|
|
|
|
foreach ($invoice->payments as $item) {
|
|
|
|
$item->default_currency_code = $invoice->currency_code;
|
|
|
|
|
|
|
|
$paid += $item->getDynamicConvertedAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice->amount = $invoice->amount - $paid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$status = collect(InvoiceStatus::all()->pluck('name', 'code'))
|
2017-09-27 21:40:25 +03:00
|
|
|
->prepend(trans('general.all_type', ['type' => trans_choice('general.statuses', 2)]), '');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
return view('customers.invoices.index', compact('invoices', 'status'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @param Invoice $invoice
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$sub_total = 0;
|
|
|
|
$tax_total = 0;
|
|
|
|
$paid = 0;
|
|
|
|
|
|
|
|
foreach ($invoice->items as $item) {
|
|
|
|
$sub_total += ($item->price * $item->quantity);
|
|
|
|
$tax_total += ($item->tax * $item->quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($invoice->payments as $item) {
|
|
|
|
$item->default_currency_code = $invoice->currency_code;
|
|
|
|
|
|
|
|
$paid += $item->getDynamicConvertedAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice->sub_total = $sub_total;
|
|
|
|
$invoice->tax_total = $tax_total;
|
|
|
|
$invoice->paid = $paid;
|
|
|
|
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$customers = Customer::enabled()->pluck('name', 'id');
|
|
|
|
|
|
|
|
$categories = Category::enabled()->type('income')->pluck('name', 'id');
|
|
|
|
|
|
|
|
$payment_methods = Modules::getPaymentMethods();
|
|
|
|
|
|
|
|
return view('customers.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $invoice_id
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2017-11-18 15:23:20 +03:00
|
|
|
public function printInvoice(Invoice $invoice)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
$sub_total = 0;
|
|
|
|
$tax_total = 0;
|
|
|
|
$paid = 0;
|
|
|
|
|
|
|
|
foreach ($invoice->items as $item) {
|
|
|
|
$sub_total += ($item->price * $item->quantity);
|
|
|
|
$tax_total += ($item->tax * $item->quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($invoice->payments as $item) {
|
|
|
|
$item->default_currency_code = $invoice->currency_code;
|
|
|
|
|
|
|
|
$paid += $item->getDynamicConvertedAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice->sub_total = $sub_total;
|
|
|
|
$invoice->tax_total = $tax_total;
|
|
|
|
$invoice->paid = $paid;
|
|
|
|
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
|
|
|
|
|
|
|
|
return view('customers.invoices.invoice', compact('invoice'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $invoice_id
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2017-11-18 15:23:20 +03:00
|
|
|
public function pdfInvoice(Invoice $invoice)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
$sub_total = 0;
|
|
|
|
$tax_total = 0;
|
|
|
|
$paid = 0;
|
|
|
|
|
|
|
|
foreach ($invoice->items as $item) {
|
|
|
|
$sub_total += ($item->price * $item->quantity);
|
|
|
|
$tax_total += ($item->tax * $item->quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($invoice->payments as $item) {
|
|
|
|
$item->default_currency_code = $invoice->currency_code;
|
|
|
|
|
|
|
|
$paid += $item->getDynamicConvertedAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice->sub_total = $sub_total;
|
|
|
|
$invoice->tax_total = $tax_total;
|
|
|
|
$invoice->paid = $paid;
|
|
|
|
$invoice->grand_total = (($sub_total + $tax_total) - $paid);
|
|
|
|
|
|
|
|
$html = view('incomes.invoices.invoice', compact('invoice'))->render();
|
|
|
|
|
|
|
|
$pdf = \App::make('dompdf.wrapper');
|
|
|
|
$pdf->loadHTML($html);
|
|
|
|
|
|
|
|
$file_name = 'invoice_'.time().'.pdf';
|
|
|
|
|
|
|
|
return $pdf->download($file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
*
|
|
|
|
* @param PaymentRequest $request
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2017-11-18 15:23:20 +03:00
|
|
|
public function payment(Invoice $invoice, PaymentRequest $request)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2017-11-18 15:23:20 +03:00
|
|
|
if (!$invoice) {
|
2017-11-18 16:57:55 +03:00
|
|
|
return response()->json([
|
|
|
|
'error' => trans('You can not pay this invoice. Because it is not yours')
|
|
|
|
]);
|
2017-11-18 15:23:20 +03:00
|
|
|
}
|
2017-11-18 16:57:55 +03:00
|
|
|
|
2017-11-17 02:08:31 +03:00
|
|
|
// Fire the event to extend the menu
|
2017-11-18 16:57:55 +03:00
|
|
|
$responses = event(new PaymentGatewayConfirm($request['payment_method'], $invoice));
|
|
|
|
|
|
|
|
$result = [
|
|
|
|
'name' => null,
|
|
|
|
'code' => null,
|
|
|
|
'description' => null,
|
|
|
|
'redirect' => false,
|
|
|
|
'html' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($responses as $response) {
|
|
|
|
if ($response) {
|
|
|
|
$result = $response;
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-17 02:08:31 +03:00
|
|
|
return response()->json($result);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2017-11-18 16:57:55 +03:00
|
|
|
|
|
|
|
public function confirm(Invoice $invoice, ConfirmRequest $request)
|
|
|
|
{
|
|
|
|
$request['invoice_id'] = $invoice->id;
|
|
|
|
$request['account_id'] = setting('general.default_account');
|
|
|
|
|
|
|
|
if (!isset($request['amount'])) {
|
|
|
|
$request['amount'] = $invoice->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request['currency_code'] = $invoice->currency_code;
|
|
|
|
$request['currency_rate'] = $invoice->currency_rate;
|
|
|
|
|
|
|
|
$request['paid_at'] = Date::parse('now')->format('Y-m-d');
|
|
|
|
|
|
|
|
if ($request['amount'] > $invoice->amount) {
|
|
|
|
$message = trans('messages.error.added', ['type' => trans_choice('general.payment', 1)]);
|
|
|
|
|
|
|
|
return response()->json($message);
|
|
|
|
} elseif ($request['amount'] == $invoice->amount) {
|
|
|
|
$invoice->invoice_status_code = 'paid';
|
|
|
|
} else {
|
|
|
|
$invoice->invoice_status_code = 'partial';
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
InvoicePayment::create($request->input());
|
|
|
|
|
|
|
|
$request['status_code'] = $invoice->invoice_status_code;
|
|
|
|
|
|
|
|
$request['notify'] = 0;
|
|
|
|
|
|
|
|
$desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat());
|
|
|
|
|
|
|
|
$desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format();
|
|
|
|
|
|
|
|
$request['description'] = $desc_date . ' ' . $desc_amount;
|
|
|
|
|
|
|
|
InvoiceHistory::create($request->input());
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|