ItemController.php
<?php
namespace App\Http\Controllers;
use App\Models\Invoice;
use App\Models\Item;
use App\Models\ItemLog;
use App\Models\Project;
use App\Models\Role;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class ItemController extends Controller
{
public function projectVendors(Request $request) {
$this->checkPermission();
$vendors = Project::findOrFail($request->post('project_id'))->vendors();
return view('admin.inventory.ajax-vendors')
->with('vendors', $vendors);
}
public function index() {
$this->checkPermission();
if(Auth::user()->isAdmin() || Auth::user()->isAccountant()) {
$projects = Project::whereProjectStatus('active')
->orderBy('project_name')->get();
}
else {
$projects = Auth::user()->projects()
->where('project_status', '=', 'active')
->get();
}
return view('admin.inventory.lists')
->with([
'projects' => $projects
]);
}
public function indexAllItem() {
$this->checkPermission();
if(Auth::user()->isAdmin() || Auth::user()->isAccountant()) {
// $projects = Project::whereProjectStatus('active')
// ->orderBy('project_name')->get();
$projects = DB::table('bsoft_projects')->select('project_id', 'project_name')->orderBy('project_name')->get();
}
else {
$projects = Auth::user()->projects()
->where('project_status', '=', 'active')
->get();
}
return view('admin.inventory.lists-all')
->with([
'projects' => $projects
]);
}
public function show($pid, $id) {
$project = Project::findOrFail($pid);
$item = Item::findOrFail($id);
$itemLogs = $project->itemLogs()
->where('il_item_id', '=', $item->item_id)
->get();
$itemTransferLogs = $project->transferredItemLogs()
->where('il_item_id', '=', $item->item_id)
->get();
return view('admin.inventory.item-details')
->with([
'item' => $item,
'itemLogs' => $itemLogs,
'itemTransferLogs' => $itemTransferLogs,
'project' => $project
]);
}
public function showItems(Request $request) {
$this->checkPermission();
if(Auth::user()->isAdmin() || Auth::user()->isAccountant()) {
$project = Project::findOrFail($request->post('pid'));
$projects = Project::where('project_status', '=', 'active')
->whereKeyNot($request->post('pid'))
->get();
}
else {
$project = Auth::user()->projects()->findOrFail($request->post('pid'));
$projects = Auth::user()->projects()
->whereKeyNot($request->post('pid'))
->where('project_status', '=', 'active')
->get();
}
return view('admin.inventory.ajax-lists')
->with([
'project' => $project,
'projects' => $projects
]);
}
public function showAllItems(Request $request) {
$this->checkPermission();
if(Auth::user()->isAdmin() || Auth::user()->isAccountant()) {
$project = Project::findOrFail($request->post('pid'));
$projects = Project::where('project_status', '=', 'active')
->whereKeyNot($request->post('pid'))
->get();
$items = Item::select('item_name')->get();
}
else {
$project = Auth::user()->projects()->findOrFail($request->post('pid'));
$projects = Auth::user()->projects()
->whereKeyNot($request->post('pid'))
->where('project_status', '=', 'active')
->get();
}
return view('admin.inventory.ajax-all-lists')
->with([
'project' => $project,
'projects' => $projects,
'items' => $items
]);
}
public function showTransferredItems(Request $request) {
$project = Project::findOrFail($request->post('pid'));
return view('admin.inventory.ajax-lists-transferred')
->with([
'project' => $project
]);
}
public function create() {
$this->checkPermission();
$items = Item::orderBy('item_id')
->get();
return view('admin.inventory.item-create')
->with([
'items' => $items
]);
}
public function saveItem(Request $request) {
$validator = Validator::make($request->all(), [
'name' => ['required', 'string'],
'unit' => ['required', 'string'],
'reusable' => ['required', 'numeric' , 'max:1'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$item = new Item();
$item->item_name = $request->post('name');
$item->item_unit = $request->post('unit');
$item->item_reusable = $request->post('reusable');
if ($item->save()) {
addActivity('item', $item->item_id, 'Item Created');
return redirectBackWithNotification('success', 'Item Created Successfully!');
}
return redirectBackWithNotification();
}
public function edit($id) {
$this->checkPermission();
$item = Item::findOrFail($id);
return view('admin.inventory.item-edit')
->with([
'item' => $item
]);
}
public function updateItem(Request $request, $id) {
$validator = Validator::make($request->all(), [
'name' => ['required', 'string'],
'unit' => ['required', 'string'],
'reusable' => ['required', 'numeric' , 'max:1'],
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$item = Item::findOrFail($id);
$item->item_name = $request->post('name');
$item->item_unit = $request->post('unit');
$item->item_reusable = $request->post('reusable');
if ($item->save()) {
addActivity('item', $item->item_id, 'Item Updated');
return redirectBackWithNotification('success', 'Item Updated Successfully!');
}
return redirectBackWithNotification();
}
public function add() {
$this->checkPermission();
$items = Item::orderBy('item_id')
->get();
if(Auth::user()->isAdmin()) {
$projects = Project::whereProjectStatus('active')
->orderBy('project_name')->get();
}
else {
$projects = Auth::user()->projects()->where('project_status', '=', 'active')->get();
}
return view('admin.inventory.add-item')
->with([
'projects' => $projects,
'items' => $items
]);
}
public function storeItem(Request $request) {
$this->checkPermission();
if(Auth::user()->isAdmin()) {
$project = Project::findOrFail($request->post('project_id'));
}
else {
$project = Auth::user()->projects()->findOrFail($request->post('project_id'));
}
$validator = Validator::make($request->all(), [
'project_id' => ['required', 'numeric'],
'vendor_id' => ['required', 'numeric'],
'item_id' => ['required', 'numeric'],
'quantity' => ['required', 'numeric'],
'payable' => ['required', 'numeric']
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$item = Item::findOrFail($request->post('item_id'));
$vendor = Role::whereRoleSlug('supplier')
->first()
->users()
->findOrFail($request->post('vendor_id'));
$log = new ItemLog();
$log->il_item_id = $item->item_id;
$log->il_project_id = $project->project_id;
$log->il_vendor_id = $vendor->id;
$log->il_quantity = $request->post('quantity');
$log->il_cost = $request->post('payable');
$log->il_note = $request->post('note');
if(!$log->save()) {
return redirectBackWithNotification('error', 'Something Went Wrong!');
}
addActivity('item_log', $log->il_id, 'New Item Stored');
return redirectUrlWithNotification(route('items.index'), 'success', 'Item Successfully Stored!');
}
public function transferItem(Request $request) {
$validator = Validator::make($request->all(), [
'project_to' => ['required', 'numeric'],
'project_from' => ['required', 'numeric'],
'quantity' => ['required', 'numeric']
]);
if($validator->fails()) {
return redirectBackWithValidationError($validator);
}
$project = Project::findOrFail($request->post('project_to'));
if($project->project_status === 'canceled' || $project->project_status === 'completed') {
return redirectBackWithNotification('error', 'You Can\'t Transfer To That Project!');
}
$oldLogs = Project::findOrFail($request->post('project_from'))
->itemLogs()->where('il_item_id', '=', $request->post('item_id'))->get();
$cost = ($oldLogs->sum('il_cost') / $oldLogs->sum('il_quantity')) * $request->post('quantity');
$log = new ItemLog();
$log->il_item_id = $request->post('item_id');
$log->il_project_id = $request->post('project_to');
$log->il_project_from = $request->post('project_from');
$log->il_quantity = $request->post('quantity');
$log->il_cost = $cost;
$log->il_paid_amount = 0;
if(!$log->save()) {
return redirectBackWithNotification('error', 'Something Went Wrong!');
}
addActivity('item_log', $log->il_id, 'Item Transferred!');
$payment = createNewPayment([
'type' => 'debit',
'to_user' => null,
'from_user' => null,
'to_bank_account' => null,
'from_bank_account' => null,
'amount' => $log->il_cost - ($log->il_cost * 2),
'project' => $request->post('project_from'),
'purpose' => 'item_transfer',
'by' => 'cash',
'date' => Carbon::now()->toDateString(),
'image' => null,
'note' => $request->post('note')
], 'Item Transferred!');
if(!$payment) {
return redirectBackWithNotification();
}
$payment1 = createNewPayment([
'type' => 'debit',
'to_user' => null,
'from_user' => null,
'to_bank_account' => null,
'from_bank_account' => null,
'amount' => $log->il_cost,
'project' => $request->post('project_to'),
'purpose' => 'item_receive',
'by' => 'cash',
'date' => Carbon::now()->toDateString(),
'image' => null,
'note' => $request->post('note')
], 'Item Received');
if(!$payment1) {
return redirectBackWithNotification();
}
$invoice = $this->createInvoice($log);
//return redirectBackWithNotification('success', 'Item Transferred!');
return redirectUrlWithNotification(route('items.invoice', ['id' => $invoice->invoice_item_log]), 'success', 'Item Transferred!');
}
public function showInvoice($id) {
$invoice = Invoice::where('invoice_item_log', '=', $id)
->first();
if(!$invoice) {
$log = ItemLog::findOrFail($id);
$invoice = $this->createInvoice($log);
}
return view('admin.inventory.invoice')
->with([
'invoice' => $invoice
]);
}
/**
* @return bool|\Illuminate\Http\RedirectResponse
*/
protected function checkPermission() {
$role = Auth::user()->role->role_slug;
if($role == 'administrator' || $role == 'manager' || $role == 'accountant') {
return true;
}
return redirectBackWithNotification('error', 'Sorry! You Are Not Authorised!.');
}
protected function createInvoice(ItemLog $log) {
$invoice = new Invoice();
$invoice->invoice_item_log = $log->il_id;
$invoice->invoice_address_from = $log->projectTransferredFrom->project_location;
$invoice->invoice_address_to = $log->project->project_location;
$invoice->save();
addActivity('invoice', $invoice->invoice_id, 'New Invoice Generated!');
return $invoice;
}
}
ajax-all-lists.blade.php
<br>
<div class="row">
<div class="col-md-12">
<div class="card comp-card">
<div class="card-body">
<h3 style="text-align: center;">All Items of <span style="color: #f91484;"></span> Project</h3>
<div class="table-responsive">
<table class="table" id="allProjects">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Item Name</th>
<th scope="col">Quantity</th>
<th scope="col">Reusable</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($project->items->unique() as $index => $inv)
<tr>
<th scope="row"></th>
<td>
<a href="" title="See Details">
---
</a>
</td>
<td>
@if($trans = $inv->itemLogs()->where('il_project_from', '=', $project->project_id)->get())
@else
@endif
</td>
<td>
@if( $inv->item_reusable )
<span class="label label-success">Yes</span>
@else
<span class="label label-danger">No</span>
@endif
</td>
<td>
@if( $inv->item_reusable )
<button type="button" class="btn btn-warning transferBtn" id="" data-toggle="modal" data-target="#transferModal" title="Transfer To Another Project" style="padding: 0 8px;">
<i class="feather icon-trending-up" id=""></i>
</button>
@else
<span class="font-weight-bold">N/A</span>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="transferModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="" method="post">
<div class="modal-header">
<h4 class="modal-title text-center w-100" id="exampleModalLabel">Transfer Item</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@csrf
@method('PATCH')
<input type="hidden" name="project_from" value="">
<input type="hidden" name="item_id" value="" id="itemId">
<div class="form-group">
<label for="project_to">Transfer To: <span class="text-danger">*</span></label>
<select name="project_to" id="project_to" class="custom-select">
<option selected disabled>--- Select A Project ---</option>
@foreach($projects as $project)
<option value=""></option>
@endforeach
</select>
</div>
<br>
<div class="form-group">
<label for="quantity">Transfer Quantity: <span class="text-danger">*</span></label>
<input type="number" class="form-control" id="quantity" name="quantity">
</div>
<div class="form-group">
<label for="note">Note: </label>
<textarea rows="3" class="form-control" id="note" name="note" style="resize: none;"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Transfer</button>
</div>
</form>
</div>
</div>
</div>
@section('script')
<script>
$(document).ready(function () {
$('.transferBtn').on('click', function (e) {
$('#itemId').val(e.target.id);
});
});
</script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.flash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.1/js/buttons.print.min.js"></script>
<script>
$(document).ready(function () {
$('#allProjects').DataTable( {
responsive: true,
dom: 'Bfrtip',
buttons: [
'csv', 'pdf', 'print'
]
});
$('.buttons-csv, .buttons-print, .buttons-pdf').addClass('btn btn-success mr-1');
});
</script>
@endsection
lists-all.blade.php
@extends('layouts.master')
@section('title', 'Items')
@section('style')
<style>
.form-group {
margin-bottom: unset;
}
.form-group {
margin-bottom: unset;
}
</style>
@endsection
@section('content')
<div class="row">
<div class="col-md-4 offset-4">
<div class="card comp-card">
<div class="card-body">
<h3 style="text-align: center;">Items of a Project</h3><br>
<form action="" method="post" id="labSearch">
@csrf
<div class="form-group">
<select name="pid" id="pid" class="form-control" required="">
<option disabled="" selected>Select Project To See Item List</option>
@foreach($projects as $project)
<option value=""></option>
@endforeach
">All</option>--}}
</select>
</div><br>
<br>
<div class="form-group">
<button type="submit" class="btn btn-sm btn-block btn-primary"><span style="font-size: 15px;">Search</span></button>
</div>
</form>
</div>
</div>
</div>
<div class="col-md-4">
</div>
</div>
<br>
<div id="ajaxResult">
</div>
@endsection
@section('script')
<script type="text/javascript">
$(document).ready(function() {
$("#labSearch").submit(function(e) {
e.preventDefault();
let pid=$("#pid").val();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url : "",
type : "POST",
data : {pid:pid},
success : function(response){
if (response.status == "error") {
alert(response.message);
}
else{
$("#ajaxResult").html(response);
}
},
error : function(xhr, status){
}
});
});
$("#itemSearch").submit(function(e) {
e.preventDefault();
let ipid=$("#ipid").val();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
url : "",
type : "POST",
data : {pid: ipid},
success : function(response){
if (response.status == "error") {
alert(response.message);
}
else{
$("#ajaxResult").html(response);
}
},
error : function(xhr, status){
}
});
});
});
</script>
@endsection
i want to put a value that show all the project item information in option All Suppose that i have 14 project in 1 project there are 58 item i want to show all project item in a page and when user click on all option they should see all item of all project in a single table
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire