v2 first commit
This commit is contained in:
36
app/Http/Controllers/Common/BulkActions.php
Normal file
36
app/Http/Controllers/Common/BulkActions.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Http\Requests\Common\BulkAction as Request;
|
||||
|
||||
class
|
||||
|
||||
BulkActions extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @param $group
|
||||
* @param $type
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function action($group, $type, Request $request)
|
||||
{
|
||||
// Check is module
|
||||
$module = module($group);
|
||||
|
||||
if ($module instanceof \Akaunting\Module\Module) {
|
||||
$bulk_actions = app('Modules\\' . $module->getStudlyName() . '\BulkActions\\' . ucfirst($type));
|
||||
} else {
|
||||
$bulk_actions = app('App\BulkActions\\' . ucfirst($group) . '\\' . ucfirst($type));
|
||||
}
|
||||
|
||||
$bulk_actions->{$request->get('handle')}($request);
|
||||
|
||||
return view('common.import.create', compact('group', 'type', 'path', 'namespace'));
|
||||
}
|
||||
}
|
@ -2,17 +2,20 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Events\CompanySwitched;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Http\Requests\Common\Company as Request;
|
||||
use App\Jobs\Common\CreateCompany;
|
||||
use App\Jobs\Common\DeleteCompany;
|
||||
use App\Jobs\Common\UpdateCompany;
|
||||
use App\Models\Common\Company;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Traits\Uploads;
|
||||
use App\Traits\Users;
|
||||
use App\Utilities\Overrider;
|
||||
|
||||
class Companies extends Controller
|
||||
{
|
||||
use Uploads;
|
||||
use Uploads, Users;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
@ -23,10 +26,6 @@ class Companies extends Controller
|
||||
{
|
||||
$companies = Company::collect();
|
||||
|
||||
foreach ($companies as $company) {
|
||||
$company->setSettings();
|
||||
}
|
||||
|
||||
return view('common.companies.index', compact('companies'));
|
||||
}
|
||||
|
||||
@ -37,7 +36,7 @@ class Companies extends Controller
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return redirect('common/companies');
|
||||
return redirect()->route('companies.index');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,40 +62,27 @@ class Companies extends Controller
|
||||
{
|
||||
$company_id = session('company_id');
|
||||
|
||||
// Create company
|
||||
$company = Company::create($request->input());
|
||||
|
||||
// Create settings
|
||||
setting()->set('general.company_name', $request->get('company_name'));
|
||||
setting()->set('general.company_email', $request->get('company_email'));
|
||||
setting()->set('general.company_address', $request->get('company_address'));
|
||||
$response = $this->ajaxDispatch(new CreateCompany($request));
|
||||
|
||||
if ($request->file('company_logo')) {
|
||||
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('companies.index');
|
||||
|
||||
if ($company_logo) {
|
||||
$company->attachMedia($company_logo, 'company_logo');
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
setting()->set('general.company_logo', $company_logo->id);
|
||||
}
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('companies.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
setting()->set('general.default_currency', $request->get('default_currency'));
|
||||
setting()->set('general.default_locale', session('locale'));
|
||||
setting()->save();
|
||||
|
||||
setting()->forgetAll();
|
||||
|
||||
session(['company_id' => $company_id]);
|
||||
|
||||
Overrider::load('settings');
|
||||
|
||||
// Redirect
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/companies');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,17 +94,10 @@ class Companies extends Controller
|
||||
*/
|
||||
public function edit(Company $company)
|
||||
{
|
||||
// Check if user can edit company
|
||||
if (!$this->isUserCompany($company)) {
|
||||
$message = trans('companies.error.not_user_company');
|
||||
|
||||
flash($message)->error();
|
||||
|
||||
return redirect('common/companies');
|
||||
if (!$this->isUserCompany($company->id)) {
|
||||
return redirect()->route('companies.index');
|
||||
}
|
||||
|
||||
$company->setSettings();
|
||||
|
||||
$currencies = Currency::enabled()->pluck('name', 'code');
|
||||
|
||||
return view('common.companies.edit', compact('company', 'currencies'));
|
||||
@ -127,8 +106,8 @@ class Companies extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Company $company
|
||||
* @param Request $request
|
||||
* @param Company $company
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
@ -136,132 +115,89 @@ class Companies extends Controller
|
||||
{
|
||||
$company_id = session('company_id');
|
||||
|
||||
// Check if user can update company
|
||||
if (!$this->isUserCompany($company)) {
|
||||
$message = trans('companies.error.not_user_company');
|
||||
$response = $this->ajaxDispatch(new UpdateCompany($company, $request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('companies.index');
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('companies.edit', $company->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
|
||||
return redirect('common/companies');
|
||||
}
|
||||
|
||||
// Update company
|
||||
$company->update($request->input());
|
||||
|
||||
// Get the company settings
|
||||
setting()->forgetAll();
|
||||
setting()->setExtraColumns(['company_id' => $company->id]);
|
||||
setting()->load(true);
|
||||
|
||||
// Update settings
|
||||
setting()->set('general.company_name', $request->get('company_name'));
|
||||
setting()->set('general.company_email', $request->get('company_email'));
|
||||
setting()->set('general.company_address', $request->get('company_address'));
|
||||
|
||||
if ($request->file('company_logo')) {
|
||||
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
|
||||
|
||||
if ($company_logo) {
|
||||
$company->attachMedia($company_logo, 'company_logo');
|
||||
|
||||
setting()->set('general.company_logo', $company_logo->id);
|
||||
}
|
||||
}
|
||||
|
||||
setting()->set('general.default_payment_method', 'offlinepayment.cash.1');
|
||||
setting()->set('general.default_currency', $request->get('default_currency'));
|
||||
|
||||
setting()->save();
|
||||
|
||||
setting()->forgetAll();
|
||||
|
||||
session(['company_id' => $company_id]);
|
||||
|
||||
Overrider::load('settings');
|
||||
|
||||
// Redirect
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/companies');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource.
|
||||
*
|
||||
* @param Company $company
|
||||
* @param Company $company
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function enable(Company $company)
|
||||
{
|
||||
$company->enabled = 1;
|
||||
$company->save();
|
||||
$response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 1])));
|
||||
|
||||
$message = trans('messages.success.enabled', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('companies.index');
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.companies', 1)]);
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource.
|
||||
*
|
||||
* @param Company $company
|
||||
* @param Company $company
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function disable(Company $company)
|
||||
{
|
||||
// Check if user can update company
|
||||
if (!$this->isUserCompany($company)) {
|
||||
$message = trans('companies.error.not_user_company');
|
||||
$response = $this->ajaxDispatch(new UpdateCompany($company, request()->merge(['enabled' => 0])));
|
||||
|
||||
Overrider::load('settings');
|
||||
|
||||
flash($message)->error();
|
||||
|
||||
return redirect()->route('companies.index');
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.companies', 1)]);
|
||||
}
|
||||
|
||||
$company->enabled = 0;
|
||||
$company->save();
|
||||
|
||||
$message = trans('messages.success.disabled', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('companies.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Company $company
|
||||
* @param Company $company
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Company $company)
|
||||
{
|
||||
// Can't delete active company
|
||||
if ($company->id == session('company_id')) {
|
||||
$message = trans('companies.error.delete_active');
|
||||
$response = $this->ajaxDispatch(new DeleteCompany($company));
|
||||
|
||||
$response['redirect'] = route('companies.index');
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
|
||||
return redirect('common/companies');
|
||||
}
|
||||
|
||||
$company->delete();
|
||||
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/companies');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -271,40 +207,24 @@ class Companies extends Controller
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function set(Company $company)
|
||||
public function switch(Company $company)
|
||||
{
|
||||
// Check if user can manage company
|
||||
if ($this->isUserCompany($company)) {
|
||||
if ($this->isUserCompany($company->id)) {
|
||||
$old_company_id = session('company_id');
|
||||
|
||||
session(['company_id' => $company->id]);
|
||||
session(['dashboard_id' => $company->dashboards()->pluck('id')->first()]);
|
||||
|
||||
Overrider::load('settings');
|
||||
|
||||
event(new CompanySwitched($company));
|
||||
}
|
||||
event(new \App\Events\Common\CompanySwitched($company, $old_company_id));
|
||||
|
||||
// Check wizard
|
||||
if (!setting('general.wizard', false)) {
|
||||
return redirect('wizard');
|
||||
// Check wizard
|
||||
if (!setting('wizard.completed', false)) {
|
||||
return redirect()->route('wizard.edit');
|
||||
}
|
||||
}
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user company assignment
|
||||
*
|
||||
* @param Company $company
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isUserCompany(Company $company)
|
||||
{
|
||||
$companies = auth()->user()->companies()->pluck('id')->toArray();
|
||||
|
||||
if (in_array($company->id, $companies)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,33 +2,14 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Banking\Account;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\BillPayment;
|
||||
use App\Models\Expense\Payment;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\Revenue;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use Charts;
|
||||
use Date;
|
||||
use App\Abstracts\Http\Controller;
|
||||
|
||||
use App\Models\Common\Dashboard as Model;
|
||||
use App\Models\Common\DashboardWidget;
|
||||
use App\Http\Requests\Common\Dashboard as Request;
|
||||
|
||||
class Dashboard extends Controller
|
||||
{
|
||||
use Currencies, DateTime;
|
||||
|
||||
public $today;
|
||||
|
||||
// get any custom financial year beginning
|
||||
public $financial_start;
|
||||
|
||||
public $income_donut = ['colors' => [], 'labels' => [], 'values' => []];
|
||||
|
||||
public $expense_donut = ['colors' => [], 'labels' => [], 'values' => []];
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@ -36,424 +17,98 @@ class Dashboard extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->today = Date::today();
|
||||
$this->financial_start = $financial_start = $this->getFinancialStart()->format('Y-m-d');
|
||||
$dashboard_id = session('dashboard_id', 0);
|
||||
|
||||
list($total_incomes, $total_expenses, $total_profit) = $this->getTotals();
|
||||
// Change Dashboard
|
||||
if (request()->get('dashboard_id', 0)) {
|
||||
$dashboard_id = request()->get('dashboard_id');
|
||||
|
||||
$cashflow = $this->getCashFlow();
|
||||
session(['dashboard_id' => $dashboard_id]);
|
||||
}
|
||||
|
||||
list($donut_incomes, $donut_expenses) = $this->getDonuts();
|
||||
$user_id = user()->id;
|
||||
|
||||
$accounts = Account::enabled()->take(6)->get();
|
||||
$dashboards = Model::where('user_id', $user_id)->enabled()->get();
|
||||
|
||||
$latest_incomes = $this->getLatestIncomes();
|
||||
if (!$dashboard_id) {
|
||||
$dashboard_id = $dashboards->first()->id;
|
||||
}
|
||||
|
||||
$latest_expenses = $this->getLatestExpenses();
|
||||
// Dashboard
|
||||
$dashboard = Model::find($dashboard_id);
|
||||
|
||||
return view('common.dashboard.index', compact(
|
||||
'total_incomes',
|
||||
'total_expenses',
|
||||
'total_profit',
|
||||
'cashflow',
|
||||
'donut_incomes',
|
||||
'donut_expenses',
|
||||
'accounts',
|
||||
'latest_incomes',
|
||||
'latest_expenses',
|
||||
'financial_start'
|
||||
));
|
||||
// Dashboard Widgets
|
||||
$widgets = DashboardWidget::where('dashboard_id', $dashboard->id)
|
||||
->where('user_id', $user_id)
|
||||
->orderBy('sort', 'asc')->get();
|
||||
|
||||
return view('common.dashboard.index', compact('dashboards','dashboard', 'widgets'));
|
||||
}
|
||||
|
||||
public function cashFlow()
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->today = Date::today();
|
||||
$request['enabled'] = 1;
|
||||
$request['user_id'] = user()->id;
|
||||
|
||||
$content = $this->getCashFlow()->render();
|
||||
$dashboard = Model::create($request->input());
|
||||
|
||||
//return response()->setContent($content)->send();
|
||||
$response['data'] = $dashboard;
|
||||
$response['redirect'] = route('dashboard');
|
||||
|
||||
echo $content;
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
private function getTotals()
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Model $dashboard)
|
||||
{
|
||||
list($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill) = $this->calculateAmounts();
|
||||
|
||||
$incomes_progress = 100;
|
||||
|
||||
if (!empty($open_invoice) && !empty($overdue_invoice)) {
|
||||
$incomes_progress = (int) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
|
||||
}
|
||||
|
||||
// Totals
|
||||
$total_incomes = array(
|
||||
'total' => $incomes_amount,
|
||||
'open_invoice' => money($open_invoice, setting('general.default_currency'), true),
|
||||
'overdue_invoice' => money($overdue_invoice, setting('general.default_currency'), true),
|
||||
'progress' => $incomes_progress
|
||||
);
|
||||
|
||||
$expenses_progress = 100;
|
||||
|
||||
if (!empty($open_bill) && !empty($overdue_bill)) {
|
||||
$expenses_progress = (int) ($open_bill * 100) / ($open_bill + $overdue_bill);
|
||||
}
|
||||
|
||||
$total_expenses = array(
|
||||
'total' => $expenses_amount,
|
||||
'open_bill' => money($open_bill, setting('general.default_currency'), true),
|
||||
'overdue_bill' => money($overdue_bill, setting('general.default_currency'), true),
|
||||
'progress' => $expenses_progress
|
||||
);
|
||||
|
||||
$amount_profit = $incomes_amount - $expenses_amount;
|
||||
$open_profit = $open_invoice - $open_bill;
|
||||
$overdue_profit = $overdue_invoice - $overdue_bill;
|
||||
|
||||
$total_progress = 100;
|
||||
|
||||
if (!empty($open_profit) && !empty($overdue_profit)) {
|
||||
$total_progress = (int) ($open_profit * 100) / ($open_profit + $overdue_profit);
|
||||
}
|
||||
|
||||
$total_profit = array(
|
||||
'total' => $amount_profit,
|
||||
'open' => money($open_profit, setting('general.default_currency'), true),
|
||||
'overdue' => money($overdue_profit, setting('general.default_currency'), true),
|
||||
'progress' => $total_progress
|
||||
);
|
||||
|
||||
return array($total_incomes, $total_expenses, $total_profit);
|
||||
return response()->json($dashboard);
|
||||
}
|
||||
|
||||
private function getCashFlow()
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Model $dashboard, Request $request)
|
||||
{
|
||||
// check and assign year start
|
||||
if (($year_start = $this->today->startOfYear()->format('Y-m-d')) !== $this->financial_start) {
|
||||
$year_start = $this->financial_start;
|
||||
}
|
||||
$request['enabled'] = 1;
|
||||
$dashboard->update($request->input());
|
||||
|
||||
$start = Date::parse(request('start', $year_start));
|
||||
$end = Date::parse(request('end', Date::parse($year_start)->addYear(1)->subDays(1)->format('Y-m-d')));
|
||||
$period = request('period', 'month');
|
||||
$range = request('range', 'custom');
|
||||
$response['data'] = $dashboard;
|
||||
$response['redirect'] = route('dashboard');
|
||||
|
||||
$start_month = $start->month;
|
||||
$end_month = $end->month;
|
||||
|
||||
// Monthly
|
||||
$labels = array();
|
||||
|
||||
$s = clone $start;
|
||||
|
||||
if ($range == 'last_12_months') {
|
||||
$end_month = 12;
|
||||
$start_month = 0;
|
||||
} elseif ($range == 'custom') {
|
||||
$end_month = $end->diffInMonths($start);
|
||||
$start_month = 0;
|
||||
}
|
||||
|
||||
for ($j = $end_month; $j >= $start_month; $j--) {
|
||||
$labels[$end_month - $j] = $s->format('M Y');
|
||||
|
||||
if ($period == 'month') {
|
||||
$s->addMonth();
|
||||
} else {
|
||||
$s->addMonths(3);
|
||||
$j -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
$income = $this->calculateCashFlowTotals('income', $start, $end, $period);
|
||||
$expense = $this->calculateCashFlowTotals('expense', $start, $end, $period);
|
||||
|
||||
$profit = $this->calculateCashFlowProfit($income, $expense);
|
||||
|
||||
$chart = Charts::multi('line', 'chartjs')
|
||||
->dimensions(0, 300)
|
||||
->colors(['#6da252', '#00c0ef', '#F56954'])
|
||||
->dataset(trans_choice('general.profits', 1), $profit)
|
||||
->dataset(trans_choice('general.incomes', 1), $income)
|
||||
->dataset(trans_choice('general.expenses', 1), $expense)
|
||||
->labels($labels)
|
||||
->credits(false)
|
||||
->view('vendor.consoletvs.charts.chartjs.multi.line');
|
||||
|
||||
return $chart;
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
private function getDonuts()
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Model $dashboard)
|
||||
{
|
||||
// Show donut prorated if there is no income
|
||||
if (array_sum($this->income_donut['values']) == 0) {
|
||||
foreach ($this->income_donut['values'] as $key => $value) {
|
||||
$this->income_donut['values'][$key] = 1;
|
||||
}
|
||||
}
|
||||
$dashboard->delete();
|
||||
|
||||
// Get 6 categories by amount
|
||||
$colors = $labels = [];
|
||||
$values = collect($this->income_donut['values'])->sort()->reverse()->take(6)->all();
|
||||
foreach ($values as $id => $val) {
|
||||
$colors[$id] = $this->income_donut['colors'][$id];
|
||||
$labels[$id] = $this->income_donut['labels'][$id];
|
||||
}
|
||||
session(['dashboard_id' => user()->dashboards()->pluck('id')->first()]);
|
||||
|
||||
$donut_incomes = Charts::create('donut', 'chartjs')
|
||||
->colors($colors)
|
||||
->labels($labels)
|
||||
->values($values)
|
||||
->dimensions(0, 160)
|
||||
->credits(false)
|
||||
->view('vendor.consoletvs.charts.chartjs.donut');
|
||||
$response['redirect'] = route('dashboard');
|
||||
|
||||
// Show donut prorated if there is no expense
|
||||
if (array_sum($this->expense_donut['values']) == 0) {
|
||||
foreach ($this->expense_donut['values'] as $key => $value) {
|
||||
$this->expense_donut['values'][$key] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Get 6 categories by amount
|
||||
$colors = $labels = [];
|
||||
$values = collect($this->expense_donut['values'])->sort()->reverse()->take(6)->all();
|
||||
foreach ($values as $id => $val) {
|
||||
$colors[$id] = $this->expense_donut['colors'][$id];
|
||||
$labels[$id] = $this->expense_donut['labels'][$id];
|
||||
}
|
||||
|
||||
$donut_expenses = Charts::create('donut', 'chartjs')
|
||||
->colors($colors)
|
||||
->labels($labels)
|
||||
->values($values)
|
||||
->dimensions(0, 160)
|
||||
->credits(false)
|
||||
->view('vendor.consoletvs.charts.chartjs.donut');
|
||||
|
||||
return array($donut_incomes, $donut_expenses);
|
||||
}
|
||||
|
||||
private function getLatestIncomes()
|
||||
{
|
||||
$invoices = collect(Invoice::orderBy('invoiced_at', 'desc')->accrued()->take(10)->get())->each(function ($item) {
|
||||
$item->paid_at = $item->invoiced_at;
|
||||
});
|
||||
|
||||
$revenues = collect(Revenue::orderBy('paid_at', 'desc')->isNotTransfer()->take(10)->get());
|
||||
|
||||
$latest = $revenues->merge($invoices)->take(5)->sortByDesc('paid_at');
|
||||
|
||||
return $latest;
|
||||
}
|
||||
|
||||
private function getLatestExpenses()
|
||||
{
|
||||
$bills = collect(Bill::orderBy('billed_at', 'desc')->accrued()->take(10)->get())->each(function ($item) {
|
||||
$item->paid_at = $item->billed_at;
|
||||
});
|
||||
|
||||
$payments = collect(Payment::orderBy('paid_at', 'desc')->isNotTransfer()->take(10)->get());
|
||||
|
||||
$latest = $payments->merge($bills)->take(5)->sortByDesc('paid_at');
|
||||
|
||||
return $latest;
|
||||
}
|
||||
|
||||
private function calculateAmounts()
|
||||
{
|
||||
$incomes_amount = $open_invoice = $overdue_invoice = 0;
|
||||
$expenses_amount = $open_bill = $overdue_bill = 0;
|
||||
|
||||
// Get categories
|
||||
$categories = Category::with(['bills', 'invoices', 'payments', 'revenues'])->orWhere('type', 'income')->orWhere('type', 'expense')->enabled()->get();
|
||||
|
||||
foreach ($categories as $category) {
|
||||
switch ($category->type) {
|
||||
case 'income':
|
||||
$amount = 0;
|
||||
|
||||
// Revenues
|
||||
foreach ($category->revenues as $revenue) {
|
||||
$amount += $revenue->getConvertedAmount();
|
||||
}
|
||||
|
||||
$incomes_amount += $amount;
|
||||
|
||||
// Invoices
|
||||
$invoices = $category->invoices()->accrued()->get();
|
||||
foreach ($invoices as $invoice) {
|
||||
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
|
||||
|
||||
$incomes_amount += $paid;
|
||||
$open_invoice += $open;
|
||||
$overdue_invoice += $overdue;
|
||||
|
||||
$amount += $paid;
|
||||
}
|
||||
|
||||
$this->addToIncomeDonut($category->color, $amount, $category->name);
|
||||
|
||||
break;
|
||||
case 'expense':
|
||||
$amount = 0;
|
||||
|
||||
// Payments
|
||||
foreach ($category->payments as $payment) {
|
||||
$amount += $payment->getConvertedAmount();
|
||||
}
|
||||
|
||||
$expenses_amount += $amount;
|
||||
|
||||
// Bills
|
||||
$bills = $category->bills()->accrued()->get();
|
||||
foreach ($bills as $bill) {
|
||||
list($paid, $open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
|
||||
|
||||
$expenses_amount += $paid;
|
||||
$open_bill += $open;
|
||||
$overdue_bill += $overdue;
|
||||
|
||||
$amount += $paid;
|
||||
}
|
||||
|
||||
$this->addToExpenseDonut($category->color, $amount, $category->name);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return array($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill);
|
||||
}
|
||||
|
||||
private function calculateCashFlowTotals($type, $start, $end, $period)
|
||||
{
|
||||
$totals = array();
|
||||
|
||||
if ($type == 'income') {
|
||||
$m1 = '\App\Models\Income\Revenue';
|
||||
$m2 = '\App\Models\Income\InvoicePayment';
|
||||
} else {
|
||||
$m1 = '\App\Models\Expense\Payment';
|
||||
$m2 = '\App\Models\Expense\BillPayment';
|
||||
}
|
||||
|
||||
$date_format = 'Y-m';
|
||||
|
||||
if ($period == 'month') {
|
||||
$n = 1;
|
||||
$start_date = $start->format($date_format);
|
||||
$end_date = $end->format($date_format);
|
||||
$next_date = $start_date;
|
||||
} else {
|
||||
$n = 3;
|
||||
$start_date = $start->quarter;
|
||||
$end_date = $end->quarter;
|
||||
$next_date = $start_date;
|
||||
}
|
||||
|
||||
$s = clone $start;
|
||||
|
||||
//$totals[$start_date] = 0;
|
||||
while ($next_date <= $end_date) {
|
||||
$totals[$next_date] = 0;
|
||||
|
||||
if ($period == 'month') {
|
||||
$next_date = $s->addMonths($n)->format($date_format);
|
||||
} else {
|
||||
if (isset($totals[4])) {
|
||||
break;
|
||||
}
|
||||
|
||||
$next_date = $s->addMonths($n)->quarter;
|
||||
}
|
||||
}
|
||||
|
||||
$items_1 = $m1::whereBetween('paid_at', [$start, $end])->isNotTransfer()->get();
|
||||
|
||||
$this->setCashFlowTotals($totals, $items_1, $date_format, $period);
|
||||
|
||||
$items_2 = $m2::whereBetween('paid_at', [$start, $end])->get();
|
||||
|
||||
$this->setCashFlowTotals($totals, $items_2, $date_format, $period);
|
||||
|
||||
return $totals;
|
||||
}
|
||||
|
||||
private function setCashFlowTotals(&$totals, $items, $date_format, $period)
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
if ($period == 'month') {
|
||||
$i = Date::parse($item->paid_at)->format($date_format);
|
||||
} else {
|
||||
$i = Date::parse($item->paid_at)->quarter;
|
||||
}
|
||||
|
||||
if (!isset($totals[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$totals[$i] += $item->getConvertedAmount();
|
||||
}
|
||||
}
|
||||
|
||||
private function calculateCashFlowProfit($incomes, $expenses)
|
||||
{
|
||||
$profit = [];
|
||||
|
||||
foreach ($incomes as $key => $income) {
|
||||
if ($income > 0 && $income > $expenses[$key]) {
|
||||
$profit[$key] = $income - $expenses[$key];
|
||||
} else {
|
||||
$profit[$key] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return $profit;
|
||||
}
|
||||
|
||||
private function calculateInvoiceBillTotals($item, $type)
|
||||
{
|
||||
$paid = $open = $overdue = 0;
|
||||
|
||||
$today = $this->today->toDateString();
|
||||
|
||||
$paid += $item->getConvertedAmount();
|
||||
|
||||
$code_field = $type . '_status_code';
|
||||
|
||||
if ($item->$code_field != 'paid') {
|
||||
$payments = 0;
|
||||
|
||||
if ($item->$code_field == 'partial') {
|
||||
foreach ($item->payments as $payment) {
|
||||
$payments += $payment->getConvertedAmount();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if it's open or overdue invoice
|
||||
if ($item->due_at > $today) {
|
||||
$open += $item->getConvertedAmount() - $payments;
|
||||
} else {
|
||||
$overdue += $item->getConvertedAmount() - $payments;
|
||||
}
|
||||
}
|
||||
|
||||
return array($paid, $open, $overdue);
|
||||
}
|
||||
|
||||
private function addToIncomeDonut($color, $amount, $text)
|
||||
{
|
||||
$this->income_donut['colors'][] = $color;
|
||||
$this->income_donut['labels'][] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $text;
|
||||
$this->income_donut['values'][] = (int) $amount;
|
||||
}
|
||||
|
||||
private function addToExpenseDonut($color, $amount, $text)
|
||||
{
|
||||
$this->expense_donut['colors'][] = $color;
|
||||
$this->expense_donut['labels'][] = money($amount, setting('general.default_currency'), true)->format() . ' - ' . $text;
|
||||
$this->expense_donut['values'][] = (int) $amount;
|
||||
return response()->json($response);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Module;
|
||||
use App\Abstracts\Http\Controller;
|
||||
|
||||
class Import extends Controller
|
||||
{
|
||||
@ -18,7 +17,7 @@ class Import extends Controller
|
||||
{
|
||||
$path = $group . '/' . $type;
|
||||
|
||||
if (Module::findByAlias($group) instanceof \Nwidart\Modules\Module) {
|
||||
if (module($group) instanceof \Akaunting\Module\Module) {
|
||||
$namespace = $group . '::';
|
||||
} else {
|
||||
$namespace = '';
|
||||
|
@ -2,16 +2,20 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Exports\Common\Items as Export;
|
||||
use App\Http\Requests\Common\Item as Request;
|
||||
use App\Http\Requests\Common\TotalItem as TRequest;
|
||||
use App\Http\Requests\Common\Import as ImportRequest;
|
||||
use App\Http\Requests\Common\TotalItem as TotalRequest;
|
||||
use App\Imports\Common\Items as Import;
|
||||
use App\Jobs\Common\CreateItem;
|
||||
use App\Jobs\Common\DeleteItem;
|
||||
use App\Jobs\Common\UpdateItem;
|
||||
use App\Models\Common\Item;
|
||||
use App\Models\Setting\Category;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Models\Setting\Tax;
|
||||
use App\Traits\Uploads;
|
||||
use App\Utilities\Import;
|
||||
use App\Utilities\ImportFile;
|
||||
|
||||
class Items extends Controller
|
||||
{
|
||||
@ -24,11 +28,9 @@ class Items extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$items = Item::with('category')->collect();
|
||||
$items = Item::with(['category', 'tax'])->collect();
|
||||
|
||||
$categories = Category::enabled()->orderBy('name')->type('item')->pluck('name', 'id');
|
||||
|
||||
return view('common.items.index', compact('items', 'categories'));
|
||||
return view('common.items.index', compact('items'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,11 +50,11 @@ class Items extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = Category::enabled()->orderBy('name')->type('item')->pluck('name', 'id');
|
||||
$categories = Category::type('item')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
||||
|
||||
$currency = Currency::where('code', '=', setting('general.default_currency', 'USD'))->first();
|
||||
$currency = Currency::where('code', setting('default.currency', 'USD'))->first();
|
||||
|
||||
return view('common.items.create', compact('categories', 'taxes', 'currency'));
|
||||
}
|
||||
@ -60,26 +62,28 @@ class Items extends Controller
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$item = Item::create($request->input());
|
||||
$response = $this->ajaxDispatch(new CreateItem($request));
|
||||
|
||||
// Upload picture
|
||||
if ($request->file('picture')) {
|
||||
$media = $this->getMedia($request->file('picture'), 'items');
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('items.index');
|
||||
|
||||
$item->attachMedia($media, 'picture');
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('items.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('items.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,15 +107,13 @@ class Items extends Controller
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportFile $import
|
||||
* @param ImportRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportFile $import)
|
||||
public function import(ImportRequest $request)
|
||||
{
|
||||
if (!Import::createFromFile($import, 'Common\Item')) {
|
||||
return redirect('common/import/common/items');
|
||||
}
|
||||
\Excel::import(new Import(), $request->file('import'));
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.items', 2)]);
|
||||
|
||||
@ -129,106 +131,101 @@ class Items extends Controller
|
||||
*/
|
||||
public function edit(Item $item)
|
||||
{
|
||||
$categories = Category::enabled()->orderBy('name')->type('item')->pluck('name', 'id');
|
||||
$categories = Category::type('item')->enabled()->orderBy('name')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->orderBy('name')->get()->pluck('title', 'id');
|
||||
|
||||
$currency = Currency::where('code', '=', setting('general.default_currency', 'USD'))->first();
|
||||
|
||||
return view('common.items.edit', compact('item', 'categories', 'taxes', 'currency'));
|
||||
return view('common.items.edit', compact('item', 'categories', 'taxes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param Request $request
|
||||
*
|
||||
* @param $item
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Item $item, Request $request)
|
||||
{
|
||||
$item->update($request->input());
|
||||
$response = $this->ajaxDispatch(new UpdateItem($item, $request));
|
||||
|
||||
// Upload picture
|
||||
if ($request->file('picture')) {
|
||||
$media = $this->getMedia($request->file('picture'), 'items');
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('items.index');
|
||||
|
||||
$item->attachMedia($media, 'picture');
|
||||
$message = trans('messages.success.updated', ['type' => $item->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('items.edit', $item->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('items.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function enable(Item $item)
|
||||
{
|
||||
$item->enabled = 1;
|
||||
$item->save();
|
||||
$response = $this->ajaxDispatch(new UpdateItem($item, request()->merge(['enabled' => 1])));
|
||||
|
||||
$message = trans('messages.success.enabled', ['type' => trans_choice('general.items', 1)]);
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.enabled', ['type' => $item->name]);
|
||||
}
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('items.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function disable(Item $item)
|
||||
{
|
||||
$item->enabled = 0;
|
||||
$item->save();
|
||||
$response = $this->ajaxDispatch(new UpdateItem($item, request()->merge(['enabled' => 0])));
|
||||
|
||||
$message = trans('messages.success.disabled', ['type' => trans_choice('general.items', 1)]);
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.disabled', ['type' => $item->name]);
|
||||
}
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect()->route('items.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Item $item)
|
||||
{
|
||||
$relationships = $this->countRelationships($item, [
|
||||
'invoice_items' => 'invoices',
|
||||
'bill_items' => 'bills',
|
||||
]);
|
||||
$response = $this->ajaxDispatch(new DeleteItem($item));
|
||||
|
||||
if (empty($relationships)) {
|
||||
$item->delete();
|
||||
$response['redirect'] = route('items.index');
|
||||
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.items', 1)]);
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => $item->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = trans('messages.warning.deleted', ['name' => $item->name, 'text' => implode(', ', $relationships)]);
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->warning();
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return redirect()->route('items.index');
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -238,13 +235,7 @@ class Items extends Controller
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
\Excel::create('items', function($excel) {
|
||||
$excel->sheet('items', function($sheet) {
|
||||
$sheet->fromModel(Item::filter(request()->input())->get()->makeHidden([
|
||||
'id', 'company_id', 'item_id', 'created_at', 'updated_at', 'deleted_at'
|
||||
]));
|
||||
});
|
||||
})->download('xlsx');
|
||||
return \Excel::download(new Export(), trans_choice('general.items', 2) . '.xlsx');
|
||||
}
|
||||
|
||||
public function autocomplete()
|
||||
@ -254,20 +245,13 @@ class Items extends Controller
|
||||
$currency_code = request('currency_code');
|
||||
|
||||
if (empty($currency_code) || (strtolower($currency_code) == 'null')) {
|
||||
$currency_code = setting('general.default_currency');
|
||||
$currency_code = setting('default.currency');
|
||||
}
|
||||
|
||||
$currency = Currency::where('code', $currency_code)->first();
|
||||
|
||||
$autocomplete = Item::autocomplete([
|
||||
'name' => $query,
|
||||
'sku' => $query,
|
||||
'name' => $query
|
||||
]);
|
||||
|
||||
if ($type == 'invoice') {
|
||||
$autocomplete->quantity();
|
||||
}
|
||||
|
||||
$items = $autocomplete->get();
|
||||
|
||||
if ($items) {
|
||||
@ -280,9 +264,6 @@ class Items extends Controller
|
||||
$item_tax_price = ($item->sale_price / 100) * $tax->rate;
|
||||
}
|
||||
|
||||
//$item->sale_price = $this->convertPrice($item->sale_price, $currency_code, $currency->rate);
|
||||
//$item->purchase_price = $this->convertPrice($item->purchase_price, $currency_code, $currency->rate);
|
||||
|
||||
switch ($type) {
|
||||
case 'bill':
|
||||
$total = $item->purchase_price + $item_tax_price;
|
||||
@ -300,14 +281,14 @@ class Items extends Controller
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
public function totalItem(TRequest $request)
|
||||
public function total(TotalRequest $request)
|
||||
{
|
||||
$input_items = $request->input('item');
|
||||
$input_items = $request->input('items');
|
||||
$currency_code = $request->input('currency_code');
|
||||
$discount = $request->input('discount');
|
||||
|
||||
if (empty($currency_code)) {
|
||||
$currency_code = setting('general.default_currency');
|
||||
$currency_code = setting('default.currency');
|
||||
}
|
||||
|
||||
$json = new \stdClass;
|
||||
@ -334,7 +315,7 @@ class Items extends Controller
|
||||
}
|
||||
|
||||
if (!empty($item['tax_id'])) {
|
||||
$inclusives = $compounds = $taxes = [];
|
||||
$inclusives = $compounds = $taxes = $fixed_taxes = [];
|
||||
|
||||
foreach ($item['tax_id'] as $tax_id) {
|
||||
$tax = Tax::find($tax_id);
|
||||
@ -346,6 +327,11 @@ class Items extends Controller
|
||||
case 'compound':
|
||||
$compounds[] = $tax;
|
||||
break;
|
||||
case 'fixed':
|
||||
$fixed_taxes[] = $tax;
|
||||
|
||||
$item_tax_total += $tax->rate;
|
||||
break;
|
||||
case 'normal':
|
||||
default:
|
||||
$taxes[] = $tax;
|
||||
@ -416,19 +402,4 @@ class Items extends Controller
|
||||
|
||||
return response()->json($json);
|
||||
}
|
||||
|
||||
protected function convertPrice($amount, $currency_code, $currency_rate, $format = false, $reverse = false)
|
||||
{
|
||||
$item = new Item();
|
||||
|
||||
$item->amount = $amount;
|
||||
$item->currency_code = $currency_code;
|
||||
$item->currency_rate = $currency_rate;
|
||||
|
||||
if ($reverse) {
|
||||
return $item->getReverseConvertedAmount($format);
|
||||
}
|
||||
|
||||
return $item->getConvertedAmount($format);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use Date;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Traits\Modules as RemoteModules;
|
||||
use App\Http\Requests\Common\Notification as Request;
|
||||
|
||||
|
277
app/Http/Controllers/Common/Reports.php
Normal file
277
app/Http/Controllers/Common/Reports.php
Normal file
@ -0,0 +1,277 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Http\Requests\Common\Report as Request;
|
||||
use App\Jobs\Common\CreateReport;
|
||||
use App\Jobs\Common\DeleteReport;
|
||||
use App\Jobs\Common\UpdateReport;
|
||||
use App\Models\Common\Report;
|
||||
use App\Utilities\Reports as Utility;
|
||||
|
||||
class Reports extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$classes = [];
|
||||
$reports = ['income-expense' => [], 'accounting' => []];
|
||||
|
||||
$items = Report::collect();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$class = Utility::getClassInstance($item);
|
||||
|
||||
if (!$class->canRead()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$reports[$class->getCategory()][] = $item;
|
||||
|
||||
$classes[$item->id] = $class;
|
||||
}
|
||||
|
||||
return view('common.reports.index', compact('reports', 'classes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return Response
|
||||
*/
|
||||
public function show(Report $report)
|
||||
{
|
||||
$class = Utility::getClassInstance($report);
|
||||
|
||||
if (!$class->canRead()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return $class->show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$classes = Utility::getClasses();
|
||||
|
||||
$groups = Utility::getGroups();
|
||||
|
||||
$periods = Utility::getPeriods();
|
||||
|
||||
$basises = Utility::getBasises();
|
||||
|
||||
$charts = Utility::getCharts();
|
||||
|
||||
return view('common.reports.create', compact('classes', 'groups', 'periods', 'basises', 'charts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new CreateReport($request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('reports.index');
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.reports', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('reports.create');
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Report $report
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Report $report)
|
||||
{
|
||||
$classes = Utility::getClasses();
|
||||
|
||||
$groups = Utility::getGroups();
|
||||
|
||||
$periods = Utility::getPeriods();
|
||||
|
||||
$basises = Utility::getBasises();
|
||||
|
||||
$charts = Utility::getCharts();
|
||||
|
||||
return view('common.reports.edit', compact('report', 'classes', 'groups', 'periods', 'basises', 'charts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Report $report
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Report $report, Request $request)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateReport($report, $request));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['redirect'] = route('reports.index');
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => $report->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$response['redirect'] = route('reports.edit', $report->id);
|
||||
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the specified resource.
|
||||
*
|
||||
* @param Report $report
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function enable(Report $report)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateReport($report, request()->merge(['enabled' => 1])));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.enabled', ['type' => $report->name]);
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the specified resource.
|
||||
*
|
||||
* @param Report $report
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function disable(Report $report)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new UpdateReport($report, request()->merge(['enabled' => 0])));
|
||||
|
||||
if ($response['success']) {
|
||||
$response['message'] = trans('messages.success.disabled', ['type' => $report->name]);
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Report $report
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Report $report)
|
||||
{
|
||||
$response = $this->ajaxDispatch(new DeleteReport($report));
|
||||
|
||||
$response['redirect'] = route('reports.index');
|
||||
|
||||
if ($response['success']) {
|
||||
$message = trans('messages.success.deleted', ['type' => $report->name]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = $response['message'];
|
||||
|
||||
flash($message)->error();
|
||||
}
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the report.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return Response
|
||||
*/
|
||||
public function print(Report $report)
|
||||
{
|
||||
$class = Utility::getClassInstance($report);
|
||||
|
||||
if (!$class->canRead()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return $class->print();
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the report.
|
||||
*
|
||||
* @param Report $report
|
||||
* @return Response
|
||||
*/
|
||||
public function export(Report $report)
|
||||
{
|
||||
$class = Utility::getClassInstance($report);
|
||||
|
||||
if (!$class->canRead()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
return $class->export();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups of the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function groups()
|
||||
{
|
||||
$class = request('class');
|
||||
|
||||
if (!class_exists($class)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'error' => true,
|
||||
'data' => false,
|
||||
'message' => "Class doesn't exist",
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'data' => (new $class())->groups,
|
||||
'message' => '',
|
||||
]);
|
||||
}
|
||||
}
|
@ -2,42 +2,31 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use App\Models\Banking\Account;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Models\Common\Contact;
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\Payment;
|
||||
use App\Models\Expense\Vendor;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\Revenue;
|
||||
use App\Models\Income\Customer;
|
||||
use App\Models\Common\Item;
|
||||
use App\Traits\Contacts;
|
||||
|
||||
class Search extends Controller
|
||||
{
|
||||
use Contacts;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$items = Item::enabled()->with('category')->get()->sortBy('name');
|
||||
|
||||
return view('items.items.index', compact('items'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function search()
|
||||
{
|
||||
$results = array();
|
||||
|
||||
$keyword = request('keyword');
|
||||
|
||||
$accounts = Account::enabled()->search($keyword)->get();
|
||||
$accounts = Account::enabled()->usingSearchString($keyword)->get();
|
||||
|
||||
if ($accounts->count()) {
|
||||
foreach ($accounts as $account) {
|
||||
@ -45,13 +34,13 @@ class Search extends Controller
|
||||
'id' => $account->id,
|
||||
'name' => $account->name,
|
||||
'type' => trans_choice('general.accounts', 1),
|
||||
'color' => '#337ab7',
|
||||
'color' => '#55588b',
|
||||
'href' => url('banking/accounts/' . $account->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$items = Item::enabled()->search($keyword)->get();
|
||||
$items = Item::enabled()->usingSearchString($keyword)->get();
|
||||
|
||||
if ($items->count()) {
|
||||
foreach ($items as $item) {
|
||||
@ -59,29 +48,41 @@ class Search extends Controller
|
||||
'id' => $item->id,
|
||||
'name' => $item->name,
|
||||
'type' => trans_choice('general.items', 1),
|
||||
'color' => '#f5bd65',
|
||||
'color' => '#efad32',
|
||||
'href' => url('common/items/' . $item->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$invoices = Invoice::search($keyword)->get();
|
||||
$invoices = Invoice::usingSearchString($keyword)->get();
|
||||
|
||||
if ($invoices->count()) {
|
||||
foreach ($invoices as $invoice) {
|
||||
$results[] = (object)[
|
||||
'id' => $invoice->id,
|
||||
'name' => $invoice->invoice_number . ' - ' . $invoice->customer_name,
|
||||
'name' => $invoice->invoice_number . ' - ' . $invoice->contact_name,
|
||||
'type' => trans_choice('general.invoices', 1),
|
||||
'color' => '#00c0ef',
|
||||
'color' => '#6da252',
|
||||
'href' => url('incomes/invoices/' . $invoice->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
}/*
|
||||
|
||||
//$revenues = Revenue::search($keyword)->get();
|
||||
$revenues = Transaction::type('income')->usingSearchString($keyword)->get();
|
||||
|
||||
$customers = Customer::enabled()->search($keyword)->get();
|
||||
if ($revenues->count()) {
|
||||
foreach ($revenues as $revenue) {
|
||||
$results[] = (object)[
|
||||
'id' => $revenue->id,
|
||||
'name' => $revenue->contact_name,
|
||||
'type' => trans_choice('general.revenues', 1),
|
||||
'color' => '#00c0ef',
|
||||
'href' => url('incomes/revenues/' . $revenue->id),
|
||||
];
|
||||
}
|
||||
}*/
|
||||
|
||||
$customers = Contact::type($this->getCustomerTypes())->enabled()->usingSearchString($keyword)->get();
|
||||
|
||||
if ($customers->count()) {
|
||||
foreach ($customers as $customer) {
|
||||
@ -89,29 +90,41 @@ class Search extends Controller
|
||||
'id' => $customer->id,
|
||||
'name' => $customer->name,
|
||||
'type' => trans_choice('general.customers', 1),
|
||||
'color' => '#03d876',
|
||||
'color' => '#328aef',
|
||||
'href' => url('incomes/customers/' . $customer->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$bills = Bill::search($keyword)->get();
|
||||
$bills = Bill::usingSearchString($keyword)->get();
|
||||
|
||||
if ($bills->count()) {
|
||||
foreach ($bills as $bill) {
|
||||
$results[] = (object)[
|
||||
'id' => $bill->id,
|
||||
'name' => $bill->bill_number . ' - ' . $bill->vendor_name,
|
||||
'name' => $bill->bill_number . ' - ' . $bill->contact_name,
|
||||
'type' => trans_choice('general.bills', 1),
|
||||
'color' => '#dd4b39',
|
||||
'color' => '#ef3232',
|
||||
'href' => url('expenses/bills/' . $bill->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
/*
|
||||
$payments = Transaction::type('expense')->usingSearchString($keyword)->get();
|
||||
|
||||
//$payments = Payment::search($keyword)->get();
|
||||
if ($revenues->count()) {
|
||||
foreach ($revenues as $revenue) {
|
||||
$results[] = (object)[
|
||||
'id' => $revenue->id,
|
||||
'name' => $revenue->contact_name,
|
||||
'type' => trans_choice('general.revenues', 1),
|
||||
'color' => '#00c0ef',
|
||||
'href' => url('incomes/revenues/' . $revenue->id),
|
||||
];
|
||||
}
|
||||
}*/
|
||||
|
||||
$vendors = Vendor::enabled()->search($keyword)->get();
|
||||
$vendors = Contact::type($this->getVendorTypes())->enabled()->usingSearchString($keyword)->get();
|
||||
|
||||
if ($vendors->count()) {
|
||||
foreach ($vendors as $vendor) {
|
||||
@ -119,7 +132,7 @@ class Search extends Controller
|
||||
'id' => $vendor->id,
|
||||
'name' => $vendor->name,
|
||||
'type' => trans_choice('general.vendors', 1),
|
||||
'color' => '#ff8373',
|
||||
'color' => '#efef32',
|
||||
'href' => url('expenses/vendors/' . $vendor->id),
|
||||
];
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Abstracts\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Common\Media;
|
||||
use File;
|
||||
@ -21,12 +21,12 @@ class Uploads extends Controller
|
||||
try {
|
||||
$media = Media::find($id);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
return response(null, 204);
|
||||
}
|
||||
|
||||
// Get file path
|
||||
if (!$path = $this->getPath($media)) {
|
||||
return false;
|
||||
return response(null, 204);
|
||||
}
|
||||
|
||||
return response()->file($path);
|
||||
|
152
app/Http/Controllers/Common/Widgets.php
Normal file
152
app/Http/Controllers/Common/Widgets.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Abstracts\Http\Controller;
|
||||
|
||||
use App\Models\Common\DashboardWidget as Model;
|
||||
use App\Models\Common\Widget;
|
||||
use App\Http\Requests\Common\Widget as Request;
|
||||
|
||||
class Widgets extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$dashboard_widgets = Widget::enabled()->get();
|
||||
|
||||
return response()->json($dashboard_widgets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request['user_id'] = user()->id;
|
||||
|
||||
$request['settings'] = [
|
||||
'width' => $request->get('width')
|
||||
];
|
||||
|
||||
$widget = Model::create($request->input());
|
||||
|
||||
$settings = $widget->settings;
|
||||
unset($settings['widget']);
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'message' => trans('messages.success.added', ['type' => $widget->name]),
|
||||
'data' => [
|
||||
'widget_id' => $widget->widget_id,
|
||||
'name' => $widget->name,
|
||||
'settings' => $settings,
|
||||
'sort' => $widget->sort,
|
||||
],
|
||||
'redirect' => route('dashboard')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Model $widget)
|
||||
{
|
||||
$settings = $widget->settings;
|
||||
unset($settings['widget']);
|
||||
|
||||
return response()->json([
|
||||
'widget_id' => $widget->widget_id,
|
||||
'name' => $widget->name,
|
||||
'settings' => $settings,
|
||||
'sort' => $widget->sort,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Model $widget, Request $request)
|
||||
{
|
||||
$request['user_id'] = user()->id;
|
||||
|
||||
$request['settings'] = [
|
||||
'width' => $request->get('width')
|
||||
];
|
||||
|
||||
$widget->update($request->input());
|
||||
|
||||
$settings = $widget->settings;
|
||||
unset($settings['widget']);
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'message' => trans('messages.success.added', ['type' => $widget->name]),
|
||||
'data' => [
|
||||
'widget_id' => $widget->widget_id,
|
||||
'name' => $widget->name,
|
||||
'settings' => $settings,
|
||||
'sort' => $widget->sort,
|
||||
],
|
||||
'redirect' => route('dashboard')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Model $dashboard
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Model $widget)
|
||||
{
|
||||
$message = trans('messages.success.deleted', ['type' => $widget->name]);
|
||||
|
||||
$widget->delete();
|
||||
|
||||
return response()->json([
|
||||
'status' => 200,
|
||||
'success' => true,
|
||||
'error' => false,
|
||||
'message' => $message,
|
||||
'data' => null,
|
||||
'redirect' => route('dashboard')
|
||||
]);
|
||||
}
|
||||
|
||||
public function getData(Request $request)
|
||||
{
|
||||
// Check is module
|
||||
$module = module($request->get('widget'));
|
||||
|
||||
if ($module instanceof \Akaunting\Module\Module) {
|
||||
$widget = app('Modules\\' . $module->getStudlyName() . '\Widgets\\' . ucfirst($request->get('widget')));
|
||||
} else {
|
||||
$widget = app('App\Widgets\\' . ucfirst($request->get('widget')));
|
||||
}
|
||||
|
||||
$response = $widget->{$request->get('method')}($request);
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user