renamed income/expense
This commit is contained in:
344
app/Http/Controllers/Sales/Customers.php
Normal file
344
app/Http/Controllers/Sales/Customers.php
Normal file
@ -0,0 +1,344 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Sales;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Exports\Sales\Customers as Export;
|
||||
use App\Http\Requests\Common\Contact as Request;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Imports\Sales\Customers as Import;
|
||||
use App\Jobs\Common\CreateContact;
|
||||
use App\Jobs\Common\DeleteContact;
|
||||
use App\Jobs\Common\UpdateContact;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Traits\Contacts;
|
||||
use Date;
|
||||
use Illuminate\Http\Request as BaseRequest;
|
||||
|
||||
class Customers extends Controller
|
||||
{
|
||||
use Contacts;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$customers = Contact::type($this->getCustomerTypes())->collect();
|
||||
|
||||
return view('sales.customers.index', compact('customers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function show(Contact $customer)
|
||||
{
|
||||
$amounts = [
|
||||
'paid' => 0,
|
||||
'open' => 0,
|
||||
'overdue' => 0,
|
||||
];
|
||||
|
||||
$counts = [];
|
||||
|
||||
// Handle invoices
|
||||
$invoices = Invoice::where('contact_id', $customer->id)->get();
|
||||
|
||||
$counts['invoices'] = $invoices->count();
|
||||
|
||||
$today = Date::today()->toDateString();
|
||||
|
||||
foreach ($invoices as $item) {
|
||||
// Already in transactions
|
||||
if ($item->invoice_status_code == 'paid') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$transactions = 0;
|
||||
|
||||
foreach ($item->transactions as $transaction) {
|
||||
$transactions += $transaction->getAmountConvertedToDefault();
|
||||
}
|
||||
|
||||
// Check if it's open or overdue invoice
|
||||
if ($item->due_at > $today) {
|
||||
$amounts['open'] += $item->getAmountConvertedToDefault() - $transactions;
|
||||
} else {
|
||||
$amounts['overdue'] += $item->getAmountConvertedToDefault() - $transactions;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle transactions
|
||||
$transactions = Transaction::where('contact_id', $customer->id)->type('income')->get();
|
||||
|
||||
$counts['transactions'] = $transactions->count();
|
||||
|
||||
// Prepare data
|
||||
$transactions->each(function ($item) use (&$amounts) {
|
||||
$amounts['paid'] += $item->getAmountConvertedToDefault();
|
||||
});
|
||||
|
||||
$limit = request('limit', setting('default.list_limit', '25'));
|
||||
$transactions = $this->paginate($transactions->sortByDesc('paid_at'), $limit);
|
||||
$invoices = $this->paginate($invoices->sortByDesc('invoiced_at'), $limit);
|
||||
|
||||
return view('sales.customers.show', compact('customer', 'counts', 'amounts', 'transactions', 'invoices'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$currencies = Currency::enabled()->pluck('name', 'code');
|
||||
|
||||
return view('sales.customers.create', compact('currencies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new CreateContact($request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('customers.index');
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.customers', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('customers.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate the specified resource.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function duplicate(Contact $customer)
|
||||
{
|
||||
$clone = $customer->duplicate();
|
||||
|
||||
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.customers', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('customers.edit', $clone->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.customers', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('customers.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Contact $customer)
|
||||
{
|
||||
$currencies = Currency::enabled()->pluck('name', 'code');
|
||||
|
||||
return view('sales.customers.edit', compact('customer', 'currencies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Contact $customer
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Contact $customer, Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateContact($customer, $request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('customers.index');
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => $customer->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('customers.edit', $customer->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function enable(Contact $customer)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateContact($customer, request()->merge(['enabled' => 1])));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.enabled', ['type' => $customer->name]);
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function disable(Contact $customer)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateContact($customer, request()->merge(['enabled' => 0])));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.disabled', ['type' => $customer->name]);
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Contact $customer
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Contact $customer)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new DeleteContact($customer));
|
||||
|
||||
$response['redirect'] = route('customers.index');
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => $customer->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return \Excel::download(new Export(), trans_choice('general.customers', 2) . '.xlsx');
|
||||
}
|
||||
|
||||
public function currency(Contact $customer)
|
||||
{
|
||||
if (empty($customer)) {
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
$currency_code = setting('default.currency');
|
||||
|
||||
if (isset($customer->currency_code)) {
|
||||
$currencies = Currency::enabled()->pluck('name', 'code')->toArray();
|
||||
|
||||
if (array_key_exists($customer->currency_code, $currencies)) {
|
||||
$currency_code = $customer->currency_code;
|
||||
}
|
||||
}
|
||||
|
||||
// Get currency object
|
||||
$currency = Currency::where('code', $currency_code)->first();
|
||||
|
||||
$customer->currency_name = $currency->name;
|
||||
$customer->currency_code = $currency_code;
|
||||
$customer->currency_rate = $currency->rate;
|
||||
|
||||
$customer->thousands_separator = $currency->thousands_separator;
|
||||
$customer->decimal_mark = $currency->decimal_mark;
|
||||
$customer->precision = (int) $currency->precision;
|
||||
$customer->symbol_first = $currency->symbol_first;
|
||||
$customer->symbol = $currency->symbol;
|
||||
|
||||
return response()->json($customer);
|
||||
}
|
||||
|
||||
public function field(BaseRequest $request)
|
||||
{
|
||||
$html = '';
|
||||
|
||||
if ($request['fields']) {
|
||||
foreach ($request['fields'] as $field) {
|
||||
switch ($field) {
|
||||
case 'password':
|
||||
$html .= \Form::passwordGroup('password', trans('auth.password.current'), 'key', [], 'col-md-6 password');
|
||||
break;
|
||||
case 'password_confirmation':
|
||||
$html .= \Form::passwordGroup('password_confirmation', trans('auth.password.current_confirm'), 'key', [], 'col-md-6 password');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$json = [
|
||||
'html' => $html
|
||||
];
|
||||
|
||||
return response()->json($json);
|
||||
}
|
||||
}
|
455
app/Http/Controllers/Sales/Invoices.php
Normal file
455
app/Http/Controllers/Sales/Invoices.php
Normal file
@ -0,0 +1,455 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Sales;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Exports\Sales\Invoices as Export;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Http\Requests\Sale\Invoice as Request;
|
||||
use App\Http\Requests\Sale\InvoiceAddItem as ItemRequest;
|
||||
use App\Imports\Sales\Invoices as Import;
|
||||
use App\Jobs\Sale\CreateInvoice;
|
||||
use App\Jobs\Sale\DeleteInvoice;
|
||||
use App\Jobs\Sale\DuplicateInvoice;
|
||||
use App\Jobs\Sale\UpdateInvoice;
|
||||
use App\Models\Banking\Account;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Sale\Invoice;
|
||||
use App\Models\Sale\InvoiceStatus;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Notifications\Sale\Invoice as Notification;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Incomes;
|
||||
use App\Utilities\Modules;
|
||||
use File;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class Invoices extends Controller
|
||||
{
|
||||
use Contacts, Currencies, DateTime, Incomes;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$invoices = Invoice::with(['contact', 'items', 'histories', 'status', 'transactions'])->collect(['invoice_number'=> 'desc']);
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$statuses = collect(InvoiceStatus::get()->each(function ($item) {
|
||||
$item->name = trans('invoices.status.' . $item->code);
|
||||
return $item;
|
||||
})->pluck('name', 'code'));
|
||||
|
||||
return view('sales.invoices.index', compact('invoices', 'customers', 'categories', 'statuses'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function show(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();
|
||||
|
||||
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$payment_methods = Modules::getPaymentMethods();
|
||||
|
||||
$signed_url = URL::signedRoute('signed.invoices.show', [$invoice->id, 'company_id' => session('company_id')]);
|
||||
|
||||
$date_format = $this->getCompanyDateFormat();
|
||||
|
||||
return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
|
||||
|
||||
$currency = Currency::where('code', setting('default.currency'))->first();
|
||||
|
||||
$items = Item::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$number = $this->getNextInvoiceNumber();
|
||||
|
||||
return view('sales.invoices.create', compact('customers', 'currencies', 'currency', 'items', 'taxes', 'categories', 'number'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new CreateInvoice($request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('invoices.show', $response['data']->id);
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.invoices', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('invoices.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate the specified resource.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function duplicate(Invoice $invoice)
|
||||
{
|
||||
$clone = $this->dispatch(new DuplicateInvoice($invoice));
|
||||
|
||||
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.invoices', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('invoices.edit', $clone->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
$success = true;
|
||||
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
if (!$success) {
|
||||
return redirect('common/import/sales/invoices');
|
||||
}
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.invoices', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('sales/invoices');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Invoice $invoice)
|
||||
{
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
|
||||
|
||||
$currency = Currency::where('code', $invoice->currency_code)->first();
|
||||
|
||||
$items = Item::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
return view('sales.invoices.edit', compact('invoice', 'customers', 'currencies', 'currency', 'items', 'taxes', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Invoice $invoice, Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateInvoice($invoice, $request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('invoices.index');
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.invoices', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('invoices.edit', $invoice->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Invoice $invoice)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new DeleteInvoice($invoice));
|
||||
|
||||
$response['redirect'] = route('invoices.index');
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return \Excel::download(new Export(), trans_choice('general.invoices', 2) . '.xlsx');
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the invoice as sent.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function markSent(Invoice $invoice)
|
||||
{
|
||||
event(new \App\Events\Sale\InvoiceSent($invoice));
|
||||
|
||||
$message = trans('invoices.messages.marked_sent');
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Download the PDF file of invoice.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function emailInvoice(Invoice $invoice)
|
||||
{
|
||||
if (empty($invoice->contact_email)) {
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
|
||||
$view = view($invoice->template_path, compact('invoice'))->render();
|
||||
$html = mb_convert_encoding($view, 'HTML-ENTITIES');
|
||||
|
||||
$pdf = app('dompdf.wrapper');
|
||||
$pdf->loadHTML($html);
|
||||
|
||||
$file = storage_path('app/temp/invoice_'.time().'.pdf');
|
||||
|
||||
$invoice->pdf_path = $file;
|
||||
|
||||
// Save the PDF file into temp folder
|
||||
$pdf->save($file);
|
||||
|
||||
// Notify the customer
|
||||
$invoice->contact->notify(new Notification($invoice, 'invoice_new_customer'));
|
||||
|
||||
// Delete temp file
|
||||
File::delete($file);
|
||||
|
||||
unset($invoice->paid);
|
||||
unset($invoice->template_path);
|
||||
unset($invoice->pdf_path);
|
||||
unset($invoice->reconciled);
|
||||
|
||||
event(new \App\Events\Sale\InvoiceSent($invoice));
|
||||
|
||||
flash(trans('invoices.messages.email_sent'))->success();
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the invoice.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function printInvoice(Invoice $invoice)
|
||||
{
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
|
||||
$view = view($invoice->template_path, compact('invoice'));
|
||||
|
||||
return mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Download the PDF file of invoice.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function pdfInvoice(Invoice $invoice)
|
||||
{
|
||||
$invoice = $this->prepareInvoice($invoice);
|
||||
|
||||
$currency_style = true;
|
||||
|
||||
$view = view($invoice->template_path, compact('invoice', '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 = 'invoice_'.time().'.pdf';
|
||||
|
||||
return $pdf->download($file_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the invoice as paid.
|
||||
*
|
||||
* @param Invoice $invoice
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function markPaid(Invoice $invoice)
|
||||
{
|
||||
try {
|
||||
event(new \App\Events\Sale\PaymentReceived($invoice, []));
|
||||
|
||||
$message = trans('invoices.messages.marked_paid');
|
||||
|
||||
flash($message)->success();
|
||||
} catch(\Exception $e) {
|
||||
$message = $e->getMessage();
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
public function addItem(ItemRequest $request)
|
||||
{
|
||||
$item_row = $request['item_row'];
|
||||
$currency_code = $request['currency_code'];
|
||||
|
||||
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
||||
|
||||
$currency = Currency::where('code', $currency_code)->first();
|
||||
|
||||
if (empty($currency)) {
|
||||
$currency = Currency::where('code', setting('default.currency', 'USD'))->first();
|
||||
}
|
||||
|
||||
if ($currency) {
|
||||
// it should be integer for amount mask
|
||||
$currency->precision = (int) $currency->precision;
|
||||
}
|
||||
|
||||
$html = view('sales.invoices.item', compact('item_row', 'taxes', 'currency'))->render();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'currency' => $currency
|
||||
],
|
||||
'message' => 'null',
|
||||
'html' => $html,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function prepareInvoice(Invoice $invoice)
|
||||
{
|
||||
$paid = 0;
|
||||
|
||||
foreach ($invoice->transactions as $item) {
|
||||
$amount = $item->amount;
|
||||
|
||||
if ($invoice->currency_code != $item->currency_code) {
|
||||
$item->default_currency_code = $invoice->currency_code;
|
||||
|
||||
$amount = $item->getAmountConvertedFromCustomDefault();
|
||||
}
|
||||
|
||||
$paid += $amount;
|
||||
}
|
||||
|
||||
$invoice->paid = $paid;
|
||||
|
||||
$invoice->template_path = 'sales.invoices.print_' . setting('invoice.template' ,'default');
|
||||
|
||||
event(new \App\Events\Sale\InvoicePrinting($invoice));
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
}
|
235
app/Http/Controllers/Sales/Revenues.php
Normal file
235
app/Http/Controllers/Sales/Revenues.php
Normal file
@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Sales;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Exports\Sales\Revenues as Export;
|
||||
use App\Http\Requests\Banking\Transaction as Request;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Imports\Common\Items as Import;
|
||||
use App\Jobs\Banking\CreateTransaction;
|
||||
use App\Jobs\Banking\DeleteTransaction;
|
||||
use App\Jobs\Banking\UpdateTransaction;
|
||||
use App\Models\Banking\Account;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Utilities\Modules;
|
||||
|
||||
class Revenues extends Controller
|
||||
{
|
||||
use Contacts, Currencies, DateTime;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$revenues = Transaction::type('income')->with(['account', 'category', 'contact'])->isNotTransfer()->collect(['paid_at'=> 'desc']);
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$transfer_cat_id = Category::transfer();
|
||||
|
||||
return view('sales.revenues.index', compact('revenues', 'customers', 'categories', 'accounts', 'transfer_cat_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return redirect()->route('revenues.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
|
||||
|
||||
$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();
|
||||
|
||||
$currency = Currency::where('code', $account_currency_code)->first();
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$payment_methods = Modules::getPaymentMethods();
|
||||
|
||||
return view('sales.revenues.create', compact('accounts', 'currencies', 'account_currency_code', 'currency', 'customers', 'categories', 'payment_methods'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('revenues.index');
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.revenues', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('revenues.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate the specified resource.
|
||||
*
|
||||
* @param Transaction $revenue
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function duplicate(Transaction $revenue)
|
||||
{
|
||||
$clone = $revenue->duplicate();
|
||||
|
||||
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.revenues', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('revenues.edit', $clone->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.revenues', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('revenues.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Transaction $revenue
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Transaction $revenue)
|
||||
{
|
||||
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
|
||||
|
||||
$currency = Currency::where('code', $revenue->currency_code)->first();
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$payment_methods = Modules::getPaymentMethods();
|
||||
|
||||
$date_format = $this->getCompanyDateFormat();
|
||||
|
||||
return view('sales.revenues.edit', compact('revenue', 'accounts', 'currencies', 'currency', 'customers', 'categories', 'payment_methods', 'date_format'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Transaction $revenue
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Transaction $revenue, Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateTransaction($revenue, $request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('revenues.index');
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.revenues', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('revenues.edit', $revenue->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Transaction $revenue
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Transaction $revenue)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new DeleteTransaction($revenue));
|
||||
|
||||
$response['redirect'] = route('revenues.index');
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.revenues', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return \Excel::download(new Export(), trans_choice('general.revenues', 2) . '.xlsx');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user