middleware('auth'); $this->middleware('permission:budget-list', ['only' => ['index']]); $this->middleware('permission:budget-create', ['only' => ['create', 'clone', 'store']]); $this->middleware('permission:budget-edit', ['only' => ['edit', 'update', 'edit_all', 'update_all']]); $this->middleware('permission:budget-delete', ['only' => ['destroy', 'inactive', 'activate']]); $this->middleware('permission:budget-performance-report', ['only' => ['budget_performance']]); $this->middleware('permission:budget-detail-performance-report', ['only' => ['budget_performance']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $created_by = $request->created_by; $dates = $request->dates; $filters = []; if ($created_by == null) : // pass elseif ($created_by != 'all') : array_push($filters, ['created_by', '=', $created_by]); endif; switch ($dates) { case 'today': $today = Carbon::today()->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereDate('created_at', $today)->paginate(1000); break; case 'yesterday': $yesterday = Carbon::yesterday()->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereDate('created_at', $yesterday)->paginate(1000); break; case 'week': $week_ago = Carbon::today()->subDays(7)->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereDate('created_at', '>=', $week_ago)->paginate(1000); break; case 'month': $month_ago = Carbon::today()->subDays(30)->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereDate('created_at', '>=', $month_ago)->paginate(1000); break; case 'custom-date': $start_date = Carbon::parse($request->start_date)->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereDate('created_at', '=', $start_date)->paginate(1000); break; case 'custom-range': $start_date = Carbon::parse($request->start_date)->format('Y-m-d'); $end_date = Carbon::parse($request->end_date)->format('Y-m-d'); $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->whereBetween('created_at', [$start_date, $end_date])->paginate(1000); break; default: $budgets = Budget::orderBy('created_at', 'desc')->where($filters)->paginate(1000); break; } if ($budgets->count() <= 0) { flash("There are no budgets found!")->error(); } // users with activity $created_by_array = DB::table('budgets')->groupBy('created_by')->select('created_by')->get()->toArray(); $created_by = array(count($created_by_array)); foreach ($created_by_array as $value) { $created_by[$value->created_by] = get_full_name($value->created_by, 'id', 'first_name', 'last_name', 'users'); } // remove the count from the array unset($created_by[0]); // add --select-- $created_by = ['all' => 'ALL'] + $created_by; // $budgets = Budget::orderBy('id', 'asc')->paginate(2000); $budgets = DB::table('budgets')->leftJoin('users', 'users.id', '=', 'budgets.created_by') ->whereNull('budgets.deleted_at') ->select('budgets.*', DB::raw("CONCAT(users.first_name,' ',users.last_name) AS user_by")) ->orderBy('budgets.id', 'desc') ->paginate(2000); return view('budgets::budgets.index', compact('budgets', 'created_by')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $this_year = Carbon::today()->year; $accounts = DB::table('chart_of_accounts as c') ->leftJoin('account_types AS a', 'a.id', '=', 'c.type') ->select('c.id', 'c.name', 'c.sub_account_of', 'a.name as type') ->whereIn('c.type', [1, 2, 7]) // 1 - Income, 2 - Expense, 7 - Cost of Goods ->get(); return view('budgets::budgets.create', compact('this_year', 'accounts')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'period' => 'required', 'budget_name' => 'required', ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); } else { $logged_in_user_id = Auth::user()->id; $budget = new Budget; $period = $request->period; $period_count = 0; $period_start = 0; switch ($period) { case 'months': $period_count = $request->period_count_months; // $period_start = $request->period_start; break; case 'quarters': $period_count = $request->period_count_quarters; // $period_start = $request->period_start; break; case 'years': $period_count = $request->period_count_years; // $period_start = $request->period_start; break; default: break; } $entries = []; $accs = []; $row_entry = explode(',', $request->accountsNumber); for ($i = 0; $i < count($row_entry); $i++) { $acc_id = 'account_id_row_' . $row_entry[$i]; $entry_id = 'budget_entry_row_' . $row_entry[$i]; $account = preg_split("/\~/", $request->$acc_id); $entry = ['account_id' => $account[0], 'name' => $account[1], 'type' => $account[2], 'sub_account'=>$account[3], 'account_entries' => $request->$entry_id]; array_push($entries, $entry); array_push($accs, $account[0]); } try { Budget::create([ "name" => $request->budget_name, "period" => $period, // dropdown months, quarters, years "period_count" => $period_count, // no. of months, quarters, years "period_start" => $request->period_start, // date "accounts" => implode(",", $accs), "entries" => json_encode($entries), "affected_tables" => implode(",", []), "created_by" => $logged_in_user_id, ]); flash($request->budget_name . " Budget has been saved")->success(); return redirect('/budgets/'); } catch (QueryException $e) { if ($e->errorInfo != null) { flash($request->budget_name . " Budget already exists!")->error(); return back()->withInput(); } else { flash($e->getMessage())->error(); return back()->withInput(); } } } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $budget = Budget::findOrFail($id); $arr3 = json_decode($budget->entries, true); $income = $income_section = $income_sub_accounts = $expense = $expense_section = $expense_sub_accounts = []; $cost_of_goods = $cost_of_goods_section = $cost_of_goods_sub_accounts = $options = []; foreach ($arr3 as $rkey => $resource) { if ($resource['type'] == 'Income') { $income[] = $resource; if(!empty($resource['sub_account'])) $income_sub_accounts[$resource['sub_account']][] = $resource; else $income_section[] = $resource; } else if ($resource['type'] == 'Cost Of Goods') { $cost_of_goods[] = $resource; if(!empty($resource['sub_account'])) $cost_of_goods_sub_sub_accounts[$resource['sub_account']][] = $resource; else $cost_of_goods_section[] = $resource; } else { $expense[] = $resource; if(!empty($resource['sub_account'])) $expense_sub_accounts[$resource['sub_account']][] = $resource; else $expense_section[] = $resource; } } $options[] = ['section_header' => 'Income', 'total_header' => 'Total Income', 'entries' => $income, 'sub_accounts' =>$income_sub_accounts, 'section'=>$income_section]; $options[] = ['section_header' => 'Cost of Goods', 'total_header' => 'Cost of Goods Total', 'entries' => $cost_of_goods, 'sub_accounts' =>$cost_of_goods_sub_accounts, 'section' => $cost_of_goods_section]; $options[] = ['section_header' => 'Expenses', 'total_header' => 'Total Expenditure', 'entries' => $expense, 'sub_accounts' =>$expense_sub_accounts, 'section'=>$expense_section]; // echo '
' . var_export($budgets, true) . '
';exit; return view('budgets::budgets.show', compact('budget', 'options')); } /** * Print the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function print_budget($id) { $budget = Budget::findOrFail($id); $arr3 = json_decode($budget->entries, true); $income = $income_section = $income_sub_accounts = $expense = $expense_section = $expense_sub_accounts = []; $cost_of_goods = $cost_of_goods_section = $cost_of_goods_sub_accounts = $options = []; foreach ($arr3 as $rkey => $resource) { if ($resource['type'] == 'Income') { $income[] = $resource; if(!empty($resource['sub_account'])) $income_sub_accounts[$resource['sub_account']][] = $resource; else $income_section[] = $resource; } else if ($resource['type'] == 'Cost Of Goods') { $cost_of_goods[] = $resource; if(!empty($resource['sub_account'])) $cost_of_goods_sub_sub_accounts[$resource['sub_account']][] = $resource; else $cost_of_goods_section[] = $resource; } else { $expense[] = $resource; if(!empty($resource['sub_account'])) $expense_sub_accounts[$resource['sub_account']][] = $resource; else $expense_section[] = $resource; } } $options[] = ['section_header' => 'Income', 'total_header' => 'Total Income', 'entries' => $income, 'sub_accounts' =>$income_sub_accounts, 'section'=>$income_section]; $options[] = ['section_header' => 'Cost of Goods', 'total_header' => 'Cost of Goods Total', 'entries' => $cost_of_goods, 'sub_accounts' =>$cost_of_goods_sub_accounts, 'section' => $cost_of_goods_section]; $options[] = ['section_header' => 'Expenses', 'total_header' => 'Total Expenditure', 'entries' => $expense, 'sub_accounts' =>$expense_sub_accounts, 'section'=>$expense_section]; $data = [ 'budget' => $budget, 'options' => $options ]; $pdf = SnappyPDF::loadView('budgets::budgets/print_budget', $data) ->setOrientation('landscape') ->setOption('margin-bottom', 7) ->setOption('margin-top', 5) ->setOption('footer-html', 'Stre@mline - Printed On ' . date('Y-m-d') . ' By ' . auth()->user()->first_name . " " . auth()->user()->last_name . ''); return $pdf->inline(ucfirst($budget->name) . date(" d-m-y h:ia") . '.pdf'); } /** * Display the clone resource. * * @param int $id * @return \Illuminate\Http\Response */ public function clone($id) { $budget = Budget::findOrFail($id); $this_year = Carbon::today()->year; $accounts = DB::table('chart_of_accounts as c') ->leftJoin('account_types AS a', 'a.id', '=', 'c.type') ->select('c.id', 'c.name', 'c.sub_account_of', 'a.name as type') ->whereIn('c.type', [1, 2, 7]) ->get(); $arr = explode(',', $budget->accounts); $budget_accounts_ids = []; for ($i = 0; $i < count($arr); $i++) array_push($budget_accounts_ids, $arr[$i]); $arr2 = explode(',', $budget->entries); $entries = []; for ($i = 0; $i < count($arr2); $i++) array_push($entries, $arr2[$i]); $budget_accounts = DB::table('chart_of_accounts as c') ->leftJoin('account_types AS a', 'a.id', '=', 'c.type') ->select('c.id', 'c.name', 'c.sub_account_of', 'a.name as type') ->whereIn('c.id', $budget_accounts_ids) ->orderByRaw("FIELD(c.id, " . $budget->accounts . ")") ->get(); return view('budgets::budgets.clone', compact('budget', 'budget_accounts', 'entries', 'this_year', 'accounts')); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $budget = Budget::findOrFail($id); $this_year = Carbon::today()->year; $accounts = DB::table('chart_of_accounts as c') ->leftJoin('account_types AS a', 'a.id', '=', 'c.type') ->select('c.id', 'c.name', 'c.sub_account_of', 'a.name as type') ->whereIn('c.type', [1, 2, 7]) ->get(); $arr2 = explode(',', $budget->entries); $entries = []; for ($i = 0; $i < count($arr2); $i++) array_push($entries, $arr2[$i]); return view('budgets::budgets.edit', compact('budget', 'this_year', 'accounts', 'entries')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $budget = Budget::findOrFail($id); $validator = Validator::make($request->all(), [ 'budget_name' => 'required', 'period' => 'required', ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); } else { $period_count = 0; $period = $request->period; switch ($period) { case 'months': $period_count = $request->period_count_months; // $period_start = $request->period_start_month; break; case 'quarters': $period_count = $request->period_count_quarters; // $period_start = $request->period_start_quarter; break; case 'years': $period_count = $request->period_count_years; // $period_start = $request->period_start_year; break; default: break; } } $entries = []; $row_ids = explode(',', $request->accountsNumber); $accs = []; for ($i = 0; $i < count($row_ids); $i++) { $acc_id = 'account_id_row_' . $row_ids[$i]; $entry_id = 'budget_entry_row_' . $row_ids[$i]; $account = preg_split("/\~/", $request->$acc_id); $entry = ['account_id' => $account[0], 'name' => $account[1], 'type' => $account[2], 'sub_account'=>$account[3], 'account_entries' => $request->$entry_id]; array_push($entries, $entry); array_push($accs, $account[0]); } try { $budget->update([ 'name' => $request->budget_name, 'period' => $request->period, 'period_count' => $period_count, // no. of months, quarters, years 'period_start' => $request->period_start, 'accounts' => implode(",", $accs), 'entries' => $entries, 'updated_by' => Auth::user()->id, ]); flash($request->budget_name . " has been updated")->success(); return redirect("/budgets/"); } catch (QueryException $e) { flash($request->budget_name . " already exists!")->error(); return back()->withInput(); } } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $budget = Budget::findOrFail($id); if ($budget->delete()) { flash("Budget has been deleted.")->success(); return redirect('/budgets/'); } } /** * Display a listing of the inactive resource(s). * * @return \Illuminate\Http\Response */ public function inactive() { $budgets = Budget::onlyTrashed()->orderBy('name', 'asc')->paginate(50); // users with activity $created_by_array = DB::table('budgets')->groupBy('created_by')->select('created_by')->get()->toArray(); $created_by = array(count($created_by_array)); foreach ($created_by_array as $value) { $created_by[$value->created_by] = get_full_name($value->created_by, 'id', 'first_name', 'last_name', 'users'); } // remove the count from the array unset($created_by[0]); // add --select-- $created_by = ['all' => 'ALL'] + $created_by; if ($budgets->isEmpty()) { flash()->error("There is no inactive budget."); return redirect('/budgets/'); } else { return view('budgets::budgets.inactive', compact('budgets', 'created_by')); } } /** * Activate the specified resource in storage. * * @param int $id * @return \Illuminate\Http\Response */ public function activate($id) { $budget = Budget::onlyTrashed()->findOrFail($id); if ($budget->restore()) { flash("Budget has been activated.")->success(); return redirect('/budgets/'); } else { flash()->error("Budget hasn't been activated."); return redirect()->route('budgets.inactive'); } } /** * Search a resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function search(Request $request) { $filters = []; if ($request->created_by != 'all') $filters[] = ['created_by', '=', $request->created_by]; if ($request->dates && $request->dates != 'all') { switch ($request->dates) { case 'today': $end_date = Carbon::today()->endOfDay(); $start_date = Carbon::today()->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; case 'yesterday': $end_date = Carbon::yesterday()->endOfDay(); $start_date = Carbon::yesterday()->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; case 'week': $end_date = Carbon::today()->endOfDay(); $start_date = Carbon::today()->subDays(7)->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; case 'month': $end_date = Carbon::today()->endOfDay(); $start_date = Carbon::today()->subDays(7)->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; case 'custom-date': $end_date = Carbon::parse($request->start_date)->endOfDay(); $start_date = Carbon::parse($request->start_date)->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; case 'custom-range': $end_date = Carbon::parse($request->end_date)->endOfDay(); $start_date = Carbon::parse($request->start_date)->startOfDay(); $filters[] = ['created_at', '>=', $start_date]; $filters[] = ['created_at', '<=', $end_date]; break; } } $created_by_array = DB::table('budgets')->groupBy('created_by')->select('created_by')->get()->toArray(); $created_by = array(count($created_by_array)); foreach ($created_by_array as $value) { $created_by[$value->created_by] = get_full_name($value->created_by, 'id', 'first_name', 'last_name', 'users'); } // remove the count from the array unset($created_by[0]); // add --select-- $created_by = ['all' => 'ALL'] + $created_by; if ($request->query_active) { if ($request->active_state == 'active') $budgets = Budget::where($filters)->orderBy('id', 'desc')->get(); else $budgets = Budget::withTrashed()->where($filters)->orderBy('id', 'desc')->get(); return view('budgets::budgets.index', compact('budgets', 'created_by')); } else { $budgets = Budget::where($filters)->onlyTrashed()->orderBy('deleted_at', 'desc')->get(); return view('budgets::budgets.inactive', compact('budgets', 'created_by')); } } public function get_budgets() { //code to be returned to view $code = ""; $budgets = Budget::orderBy('name', 'asc')->get(); foreach ($budgets as $budget) { $code .= ""; } return $code; } /* test server side scolling with datatables */ public function datatable_test() { $budgets = Budget::orderBy('name', 'asc')->get(); return response()->json($budgets); } //budget vs actual report public function budget_performance(Request $request) { $budget = (!empty($request->budget_id))? Budget::findOrFail($request->budget_id) : Budget::latest()->first(); if (empty($budget)) return redirect('/budgets/'); $other_budgets = Budget::where('id', '<>', $budget->id)->select('name', 'id')->get(); $entries = json_decode($budget->entries, true); $income = $income_ids = $expense = $expense_ids = $cost_of_goods = $cost_of_goods_ids = $options = []; foreach ($entries as $rkey => $resource) { if ($resource['type'] == 'Income') $income_ids[] = $resource['account_id']; else if ($resource['type'] == 'Cost Of Goods') $cost_of_goods_ids[] = $resource['account_id']; else $expense_ids[] = $resource['account_id']; } $budget_start_date = Carbon::parse($budget->period_start)->startOfDay()->toDateString(); switch ($budget->period) { case 'months': $budget_end_date = date("Y-m-t", strtotime("+". $budget->period_count - 1 ." month", strtotime($budget_start_date))); $end_date =Carbon::parse($budget_end_date)->endOfDay()->toDateString(); break; case 'quarters': $budget_end_date = date("Y-m-t", strtotime("+". ($budget->period_count-1) * 3 ." month", strtotime('-1 day', strtotime($budget_start_date)))); $end_date = Carbon::parse($budget_end_date)->endOfDay()->toDateString(); break; case 'years': $day = (date('d', strtotime($budget_start_date)) > 28)? date('Y-m-d', strtotime('-3 day', strtotime($budget_start_date))) : $budget_start_date; $budget_end_date = date("Y-m-t", strtotime("+". $budget->period_count - 1 ." year", strtotime('-1 month', strtotime($day)))); $end_date =Carbon::parse($budget_end_date)->endOfDay()->toDateString(); break; default: break; } $request->request->add(['dates' => 'custom_date_range']); $request->request->add(['start_date' => $budget_start_date]); $request->request->add(['end_date' => $end_date]); $request->request->add(['cost_of_goods_ids' => $cost_of_goods_ids]); $request->request->add(['expense_ids' => $expense_ids]); $request->request->add(['income_ids' => $income_ids]); // echo '
' . var_export($budget, true) . '
';exit; $data = getNetIncome($request); $actual_income = $data['accrual_income_total'] + $data['cash_income_total']; $actual_cost_of_goods = $data['cog_accrual_total'] + $data['cog_cash_total']; $actual_expense = $data['expenses_accrual_total'] + $data['expenses_cash_total']; foreach ($entries as $entry) { if ($entry['type'] == 'Income') { $actual_entry = isset($data['revenue'][0][$entry['account_id']]['cash'])? $data['revenue'][0][$entry['account_id']]['cash'] + $data['revenue'][0][$entry['account_id']]['accrual'] : 0; $entry['actual_entry'] = $actual_entry; $income [] = $entry; } else if ($entry['type'] == 'Cost Of Goods') { $actual_entry = isset($data['revenue'][1][$entry['account_id']]['cash'])? $data['revenue'][1][$entry['account_id']]['cash'] + $data['revenue'][1][$entry['account_id']]['accrual'] : 0; $entry['actual_entry'] = $actual_entry; $cost_of_goods[] = $entry; } else { $actual_entry = isset($data['expense_accounts'][$entry['account_id']]['cash'])? $data['expense_accounts'][$entry['account_id']]['cash'] + $data['expense_accounts'][$entry['account_id']]['accrual'] : 0; $entry['actual_entry'] = $actual_entry; $expense[] = $entry; } } $options[] = ['section_header' => 'Income', 'total_header' => 'Total Income', 'entries' => $income, 'actual' => $actual_income]; $options[] = ['section_header' => 'Cost of Goods', 'total_header' => 'Cost of Goods Total', 'entries' => $cost_of_goods, 'actual' => $actual_cost_of_goods]; $options[] = ['section_header' => 'Expenses', 'total_header' => 'Total Expenditure', 'entries' => $expense, 'actual' => $actual_expense]; $type = explode('/',$request->url()); $view = ($type[count($type) - 1] == 'summary') ? 'budgets::budgets.budget_performance' : 'budgets::budgets.budget_performance_detail'; return view($view, compact('budget', 'options', 'data','budget_start_date','budget_end_date','other_budgets')); } function print_budget_report(Request $request){ $budget = json_decode($request->budget); $options = json_decode($request->options, true); $data = json_decode($request->data, true); $range = $request->range; if ($request->type == 'summary') { $view = 'budgets::budgets.print_budget_performance'; $orientation = 'portrait'; } else { $view = 'budgets::budgets.print_budget_performance_detail'; $orientation = 'landscape'; } $pdf = SnappyPDF::loadView($view, compact('budget', 'options','data', 'range')) ->setOrientation($orientation) ->setOption('margin-bottom', 7) ->setOption('margin-top', 5) ->setOption('footer-html', 'Stre@mline - Printed On ' . date('Y-m-d') . ' By ' . auth()->user()->first_name . " " . auth()->user()->last_name . ''); return $pdf->inline(ucfirst($budget->name) . date(" d-m-y h:ia") . '.pdf'); } }