moved folders to common directory
This commit is contained in:
240
app/Http/Controllers/Common/Companies.php
Normal file
240
app/Http/Controllers/Common/Companies.php
Normal file
@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Events\CompanySwitched;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Common\Company as Request;
|
||||
use App\Models\Common\Company;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Traits\Uploads;
|
||||
|
||||
class Companies extends Controller
|
||||
{
|
||||
use Uploads;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$companies = Company::collect();
|
||||
|
||||
foreach ($companies as $company) {
|
||||
$company->setSettings();
|
||||
}
|
||||
|
||||
return view('common.companies.index', compact('companies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return redirect('common/companies');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$currencies = Currency::enabled()->pluck('name', 'code');
|
||||
|
||||
return view('common.companies.create', compact('currencies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
setting()->forgetAll();
|
||||
|
||||
// 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'));
|
||||
|
||||
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_currency', $request->get('default_currency'));
|
||||
setting()->set('general.default_locale', session('locale'));
|
||||
|
||||
setting()->setExtraColumns(['company_id' => $company->id]);
|
||||
setting()->save();
|
||||
|
||||
// Redirect
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/companies');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Company $company
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
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');
|
||||
}
|
||||
|
||||
$company->setSettings();
|
||||
|
||||
$currencies = Currency::enabled()->pluck('name', 'code');
|
||||
|
||||
return view('common.companies.edit', compact('company', 'currencies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Company $company
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Company $company, Request $request)
|
||||
{
|
||||
// Check if user can update company
|
||||
if (!$this->isUserCompany($company)) {
|
||||
$message = trans('companies.error.not_user_company');
|
||||
|
||||
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();
|
||||
|
||||
// Redirect
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.companies', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/companies');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @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');
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the active company.
|
||||
*
|
||||
* @param Company $company
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function set(Company $company)
|
||||
{
|
||||
// Check if user can manage company
|
||||
if ($this->isUserCompany($company)) {
|
||||
session(['company_id' => $company->id]);
|
||||
|
||||
event(new CompanySwitched($company));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
439
app/Http/Controllers/Common/Dashboard.php
Normal file
439
app/Http/Controllers/Common/Dashboard.php
Normal file
@ -0,0 +1,439 @@
|
||||
<?php
|
||||
|
||||
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 Charts;
|
||||
use Date;
|
||||
|
||||
class Dashboard extends Controller
|
||||
{
|
||||
use Currencies;
|
||||
|
||||
public $today;
|
||||
|
||||
public $income_donut = ['colors' => [], 'labels' => [], 'values' => []];
|
||||
|
||||
public $expense_donut = ['colors' => [], 'labels' => [], 'values' => []];
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->today = Date::today();
|
||||
|
||||
list($total_incomes, $total_expenses, $total_profit) = $this->getTotals();
|
||||
|
||||
$cashflow = $this->getCashFlow();
|
||||
|
||||
list($donut_incomes, $donut_expenses) = $this->getDonuts();
|
||||
|
||||
$accounts = Account::enabled()->take(6)->get();
|
||||
|
||||
$latest_incomes = $this->getLatestIncomes();
|
||||
|
||||
$latest_expenses = $this->getLatestExpenses();
|
||||
|
||||
return view('common.dashboard.index', compact(
|
||||
'total_incomes',
|
||||
'total_expenses',
|
||||
'total_profit',
|
||||
'cashflow',
|
||||
'donut_incomes',
|
||||
'donut_expenses',
|
||||
'accounts',
|
||||
'latest_incomes',
|
||||
'latest_expenses'
|
||||
));
|
||||
}
|
||||
|
||||
public function cashFlow()
|
||||
{
|
||||
$this->today = Date::today();
|
||||
|
||||
$content = $this->getCashFlow()->render();
|
||||
|
||||
//return response()->setContent($content)->send();
|
||||
|
||||
echo $content;
|
||||
}
|
||||
|
||||
private function getTotals()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private function getCashFlow()
|
||||
{
|
||||
$start = Date::parse(request('start', $this->today->startOfYear()->format('Y-m-d')));
|
||||
$end = Date::parse(request('end', $this->today->endOfYear()->format('Y-m-d')));
|
||||
$period = request('period', 'month');
|
||||
|
||||
$start_month = $start->month;
|
||||
$end_month = $end->month;
|
||||
|
||||
// Monthly
|
||||
$labels = array();
|
||||
|
||||
$s = clone $start;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private function getDonuts()
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
$donut_incomes = Charts::create('donut', 'chartjs')
|
||||
->colors($colors)
|
||||
->labels($labels)
|
||||
->values($values)
|
||||
->dimensions(0, 160)
|
||||
->credits(false)
|
||||
->view('vendor.consoletvs.charts.chartjs.donut');
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
331
app/Http/Controllers/Common/Items.php
Normal file
331
app/Http/Controllers/Common/Items.php
Normal file
@ -0,0 +1,331 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Common\Item as Request;
|
||||
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\ImportFile;
|
||||
|
||||
class Items extends Controller
|
||||
{
|
||||
use Uploads;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$items = Item::with('category')->collect();
|
||||
|
||||
$categories = Category::enabled()->type('item')->pluck('name', 'id')
|
||||
->prepend(trans('general.all_type', ['type' => trans_choice('general.categories', 2)]), '');
|
||||
|
||||
return view('common.items.index', compact('items', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for viewing the specified resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return redirect('common/items');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = Category::enabled()->type('item')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->get()->pluck('title', 'id');
|
||||
|
||||
return view('common.items.create', compact('categories', 'taxes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$item = Item::create($request->input());
|
||||
|
||||
// Upload picture
|
||||
if ($request->file('picture')) {
|
||||
$media = $this->getMedia($request->file('picture'), 'items');
|
||||
|
||||
$item->attachMedia($media, 'picture');
|
||||
}
|
||||
|
||||
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/items');
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate the specified resource.
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function duplicate(Item $item)
|
||||
{
|
||||
$clone = $item->duplicate();
|
||||
|
||||
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/items/' . $clone->id . '/edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Import the specified resource.
|
||||
*
|
||||
* @param ImportFile $import
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function import(ImportFile $import)
|
||||
{
|
||||
$rows = $import->all();
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$data = $row->toArray();
|
||||
$data['company_id'] = session('company_id');
|
||||
|
||||
Item::create($data);
|
||||
}
|
||||
|
||||
$message = trans('messages.success.imported', ['type' => trans_choice('general.items', 2)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/items');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function edit(Item $item)
|
||||
{
|
||||
$categories = Category::enabled()->type('item')->pluck('name', 'id');
|
||||
|
||||
$taxes = Tax::enabled()->get()->pluck('title', 'id');
|
||||
|
||||
return view('common.items.edit', compact('item', 'categories', 'taxes'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param Item $item
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function update(Item $item, Request $request)
|
||||
{
|
||||
$item->update($request->input());
|
||||
|
||||
// Upload picture
|
||||
if ($request->file('picture')) {
|
||||
$media = $this->getMedia($request->file('picture'), 'items');
|
||||
|
||||
$item->attachMedia($media, 'picture');
|
||||
}
|
||||
|
||||
$message = trans('messages.success.updated', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
return redirect('common/items');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(Item $item)
|
||||
{
|
||||
$relationships = $this->countRelationships($item, [
|
||||
'invoice_items' => 'invoices',
|
||||
'bill_items' => 'bills',
|
||||
]);
|
||||
|
||||
if (empty($relationships)) {
|
||||
$item->delete();
|
||||
|
||||
$message = trans('messages.success.deleted', ['type' => trans_choice('general.items', 1)]);
|
||||
|
||||
flash($message)->success();
|
||||
} else {
|
||||
$message = trans('messages.warning.deleted', ['name' => $item->name, 'text' => implode(', ', $relationships)]);
|
||||
|
||||
flash($message)->warning();
|
||||
}
|
||||
|
||||
return redirect('common/items');
|
||||
}
|
||||
|
||||
public function autocomplete()
|
||||
{
|
||||
$type = request('type');
|
||||
$query = request('query');
|
||||
$currency_code = request('currency_code');
|
||||
|
||||
if (empty($currency_code) || (strtolower($currency_code) == 'null')) {
|
||||
$currency_code = setting('general.default_currency');
|
||||
}
|
||||
|
||||
$currency = Currency::where('code', $currency_code)->first();
|
||||
|
||||
$autocomplete = Item::autocomplete([
|
||||
'name' => $query,
|
||||
'sku' => $query,
|
||||
]);
|
||||
|
||||
if ($type == 'invoice') {
|
||||
$autocomplete->quantity();
|
||||
}
|
||||
|
||||
$items = $autocomplete->get();
|
||||
|
||||
if ($items) {
|
||||
foreach ($items as $item) {
|
||||
$tax = Tax::find($item->tax_id);
|
||||
|
||||
$item_tax_price = 0;
|
||||
|
||||
if (!empty($tax)) {
|
||||
$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;
|
||||
break;
|
||||
case 'invoice':
|
||||
default:
|
||||
$total = $item->sale_price + $item_tax_price;
|
||||
break;
|
||||
}
|
||||
|
||||
$item->total = money($total, $currency_code, true)->format();
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
public function totalItem()
|
||||
{
|
||||
$input_items = request('item');
|
||||
$currency_code = request('currency_code');
|
||||
$discount = request('discount');
|
||||
|
||||
if (empty($currency_code)) {
|
||||
$currency_code = setting('general.default_currency');
|
||||
}
|
||||
|
||||
$json = new \stdClass;
|
||||
|
||||
$sub_total = 0;
|
||||
$tax_total = 0;
|
||||
|
||||
$items = array();
|
||||
|
||||
if ($input_items) {
|
||||
foreach ($input_items as $key => $item) {
|
||||
$price = (double) $item['price'];
|
||||
$quantity = (double) $item['quantity'];
|
||||
|
||||
$item_tax_total= 0;
|
||||
$item_sub_total = ($price * $quantity);
|
||||
|
||||
if (!empty($item['tax_id'])) {
|
||||
$tax = Tax::find($item['tax_id']);
|
||||
|
||||
$item_tax_total = (($price * $quantity) / 100) * $tax->rate;
|
||||
}
|
||||
|
||||
$sub_total += $item_sub_total;
|
||||
|
||||
// Apply discount to tax
|
||||
if ($discount) {
|
||||
$item_tax_total = $item_tax_total - ($item_tax_total * ($discount / 100));
|
||||
}
|
||||
|
||||
$tax_total += $item_tax_total;
|
||||
|
||||
$items[$key] = money($item_sub_total, $currency_code, true)->format();
|
||||
}
|
||||
}
|
||||
|
||||
$json->items = $items;
|
||||
|
||||
$json->sub_total = money($sub_total, $currency_code, true)->format();
|
||||
|
||||
$json->discount_text= trans('invoices.add_discount');
|
||||
$json->discount_total = '';
|
||||
|
||||
$json->tax_total = money($tax_total, $currency_code, true)->format();
|
||||
|
||||
// Apply discount to total
|
||||
if ($discount) {
|
||||
$json->discount_text= trans('invoices.show_discount', ['discount' => $discount]);
|
||||
$json->discount_total = money($sub_total * ($discount / 100), $currency_code, true)->format();
|
||||
|
||||
$sub_total = $sub_total - ($sub_total * ($discount / 100));
|
||||
}
|
||||
|
||||
$grand_total = $sub_total + $tax_total;
|
||||
|
||||
$json->grand_total = money($grand_total, $currency_code, true)->format();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
130
app/Http/Controllers/Common/Search.php
Normal file
130
app/Http/Controllers/Common/Search.php
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Common;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Banking\Account;
|
||||
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;
|
||||
|
||||
class Search extends Controller
|
||||
{
|
||||
/**
|
||||
* 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();
|
||||
|
||||
if ($accounts->count()) {
|
||||
foreach ($accounts as $account) {
|
||||
$results[] = (object)[
|
||||
'id' => $account->id,
|
||||
'name' => $account->name,
|
||||
'type' => trans_choice('general.accounts', 1),
|
||||
'color' => '#337ab7',
|
||||
'href' => url('banking/accounts/' . $account->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$items = Item::enabled()->search($keyword)->get();
|
||||
|
||||
if ($items->count()) {
|
||||
foreach ($items as $item) {
|
||||
$results[] = (object)[
|
||||
'id' => $item->id,
|
||||
'name' => $item->name,
|
||||
'type' => trans_choice('general.items', 1),
|
||||
'color' => '#f5bd65',
|
||||
'href' => url('items/items/' . $item->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$invoices = Invoice::search($keyword)->get();
|
||||
|
||||
if ($invoices->count()) {
|
||||
foreach ($invoices as $invoice) {
|
||||
$results[] = (object)[
|
||||
'id' => $invoice->id,
|
||||
'name' => $invoice->invoice_number . ' - ' . $invoice->customer_name,
|
||||
'type' => trans_choice('general.invoices', 1),
|
||||
'color' => '#00c0ef',
|
||||
'href' => url('incomes/invoices/' . $invoice->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
//$revenues = Revenue::search($keyword)->get();
|
||||
|
||||
$customers = Customer::enabled()->search($keyword)->get();
|
||||
|
||||
if ($customers->count()) {
|
||||
foreach ($customers as $customer) {
|
||||
$results[] = (object)[
|
||||
'id' => $customer->id,
|
||||
'name' => $customer->name,
|
||||
'type' => trans_choice('general.customers', 1),
|
||||
'color' => '#03d876',
|
||||
'href' => url('incomes/customers/' . $customer->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$bills = Bill::search($keyword)->get();
|
||||
|
||||
if ($bills->count()) {
|
||||
foreach ($bills as $bill) {
|
||||
$results[] = (object)[
|
||||
'id' => $bill->id,
|
||||
'name' => $bill->bill_number . ' - ' . $bill->vendor_name,
|
||||
'type' => trans_choice('general.bills', 1),
|
||||
'color' => '#dd4b39',
|
||||
'href' => url('expenses/bills/' . $bill->id),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
//$payments = Payment::search($keyword)->get();
|
||||
|
||||
$vendors = Vendor::enabled()->search($keyword)->get();
|
||||
|
||||
if ($vendors->count()) {
|
||||
foreach ($vendors as $vendor) {
|
||||
$results[] = (object)[
|
||||
'id' => $vendor->id,
|
||||
'name' => $vendor->name,
|
||||
'type' => trans_choice('general.vendors', 1),
|
||||
'color' => '#ff8373',
|
||||
'href' => url('expenses/vendors/' . $vendor->id . '/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json((object) $results);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user