Files

227 lines
8.2 KiB
PHP
Executable File

<?php
namespace Modules\ClinicalData\Http\Controllers;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Streamline\Models\GeneralItem;
use Streamline\Models\UnitOfMeasure;
use Streamline\Models\ChartOfAccount;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\GeneralItemStoreStockReconciliation;
class GeneralItemsController extends Controller
{
// public function __construct() {
// $this->middleware('auth');
// $this->middleware('permission:general_items-list', ['only' => ['index']]);
// $this->middleware('permission:general_items-create', ['only' => ['create', 'store']]);
// $this->middleware('permission:general_items-edit', ['only' => ['edit', 'update']]);
// $this->middleware('permission:general_items-delete', ['only' => ['destroy', 'inactive', 'activate']]);
// }
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$general_items = GeneralItem::get();
$chart_of_accounts = ChartOfAccount::orderBy('name', 'asc')->pluck('name', 'id');
$expense_accounts = ChartOfAccount::where(['type' => 2])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$payables_accounts = ChartOfAccount::where(['type' => 6])
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
return view('clinical_data::general_items.index', compact('general_items', 'chart_of_accounts', 'payables_accounts', 'expense_accounts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$expense_accounts = ChartOfAccount::where(['type' => 2])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$payables_accounts = ChartOfAccount::where(['type' => 6])
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$expense_accounts = ['' => '- select -'] + $expense_accounts;
$payables_accounts = ['' => '- select -'] + $payables_accounts;
return view('clinical_data::general_items.create', compact('cost_of_goods_accounts', 'payables_accounts', 'expense_accounts'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'cost_price' => 'required'
]);
$general_item = new GeneralItem();
$logged_in_user_id = Auth::user()->id;
$general_item->name = $request->name;
$general_item->cost_price = $request->cost_price;
$general_item->expenses_account_id = $request->expenses_account_id;
$general_item->payables_account_id = $request->payables_account_id;
//$general_item->supplier_id = $request->supplier_id;
//$general_item->store_stock = $request->store_stock;
//$general_item->cost_of_goods_account = $request->cost_of_goods_account;
//$general_item->item_type = $request->item_type;
$general_item->created_by = $logged_in_user_id;
$general_item->updated_by = $logged_in_user_id;
try {
$general_item->save();
flash("General item has been saved")->success();
return redirect("/general_items/");
} catch (QueryException $e) {
flash("Something went wrong. Please try again")->error();
return back()->withInput();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$general_item = GeneralItem::where(['id' => $id])->first();
$units = UnitOfMeasure::get();
$cost_of_goods_accounts = ChartOfAccount::where(['type' => 7])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$expenses_accounts = ChartOfAccount::where(['type' => 2])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$payables_accounts = ChartOfAccount::where(['type' => 6])
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$cost_of_goods_accounts = ['' => '- select -'] + $cost_of_goods_accounts;
$expenses_accounts = ['' => '- select -'] + $expenses_accounts;
$payables_accounts = ['' => '- select -'] + $payables_accounts;
if (!$general_item) {
flash()->error("General item not found");
return redirect('/general_items/');
} else {
return view('clinical_data::general_items.edit', compact('general_item', 'payables_accounts', 'expenses_accounts', 'cost_of_goods_accounts'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
request()->validate([
'name' => 'required',
'cost_price' => 'required',
]);
//validation passed
$general_item = GeneralItem::find($id);
$logged_in_user_id = Auth::user()->id;
$general_item->name = $request->name;
$general_item->cost_price = $request->cost_price;
$general_item->expenses_account_id = $request->expenses_account_id;
$general_item->payables_account_id = $request->payables_account_id;
$general_item->updated_by = $logged_in_user_id;
try {
$general_item->save();
flash("General item has been updated")->success();
return redirect("/general_items/");
} catch (QueryException $e) {
flash("An error occurred")->error();
return back()->withInput();
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$general_item = GeneralItem::find($id);
if ($general_item->delete()) {
flash("The item has been deleted.")->success();
return redirect('/general_items/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
*/
public function inactive() {
$general_items = GeneralItem::onlyTrashed()->get();
$expense_accounts = ChartOfAccount::where(['type' => 2])
->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
$payables_accounts = ChartOfAccount::where(['type' => 6])
->orWhere(['type' => 9])->orderBy('name', 'asc')->pluck('name', 'id')->toArray();
if (count($general_items) < 1) {
flash()->error("There is no inactive General Item");
return redirect('/general_items/');
} else {
return view('clinical_data::general_items.inactive', compact('general_items', 'expense_accounts', 'payables_accounts'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function activate($id) {
$general_item = GeneralItem::withTrashed()->find($id);
if($general_item->restore()){
flash("A general item has been activated.")->success();
return redirect('/general_items/inactive');
}
}
}