diff --git a/Modules/Operations/Entities/OfficeLocation.php b/Modules/Operations/Entities/OfficeLocation.php index 52ee7b704b..501999b40d 100644 --- a/Modules/Operations/Entities/OfficeLocation.php +++ b/Modules/Operations/Entities/OfficeLocation.php @@ -2,6 +2,7 @@ namespace Modules\Operations\Entities; +use App\Models\KnowledgeCafe\Library\BookLocation; use Illuminate\Database\Eloquent\Model; use Modules\User\Entities\User; @@ -13,4 +14,9 @@ public function centreHead() { return $this->belongsTo(User::class, 'centre_head_id'); } + + public function locationOfBook() + { + return $this->hasMany(BookLocation::class, 'office_location_id'); + } } diff --git a/app/Http/Controllers/KnowledgeCafe/Library/BookController.php b/app/Http/Controllers/KnowledgeCafe/Library/BookController.php index 7760eb619d..ef4447cb4b 100644 --- a/app/Http/Controllers/KnowledgeCafe/Library/BookController.php +++ b/app/Http/Controllers/KnowledgeCafe/Library/BookController.php @@ -3,10 +3,13 @@ namespace App\Http\Controllers\KnowledgeCafe\Library; use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\DB; use App\Http\Requests\KnowledgeCafe\Library\BookRequest; use App\Models\KnowledgeCafe\Library\Book; use App\Models\KnowledgeCafe\Library\BookAMonth; use App\Models\KnowledgeCafe\Library\BookCategory; +use App\Models\KnowledgeCafe\Library\BookLocation; +use Modules\Operations\Entities\OfficeLocation; use App\Services\BookServices; use Carbon\Carbon; use Illuminate\Http\Request; @@ -29,7 +32,7 @@ public function index(Request $request) $searchCategory = $request->category_name ?? false; $searchString = request()->has('search') ? request()->input('search') : false; $categories = BookCategory::orderBy('name')->get(); - + $bookLocations = BookLocation::withTrashed()->with('location')->get(); switch (request()) { case request()->has('wishlist'): $books = auth()->user()->booksInWishlist; @@ -48,9 +51,18 @@ public function index(Request $request) $books->load('borrowers'); $wishlistedBooksCount = auth()->user()->booksInWishlist->count(); $booksBorrowedCount = auth()->user()->booksBorrower->count(); - - return view('knowledgecafe.library.books.index', compact('books', 'loggedInUser', 'categories', 'wishlistedBooksCount', 'booksBorrowedCount')); - } + $officeLocations = $this->officeLocationData(); + + $books = $books->map(function ($book) use ($bookLocations) { + $locations = $bookLocations->where('book_id', $book->id); + $copies = []; + foreach ($locations as $location) { + $copies[$location->office_location_id] = $location->number_of_copies; + } + $book->copies = $copies; + return $book; + }); + return view('knowledgecafe.library.books.index', compact('books', 'loggedInUser', 'categories', 'wishlistedBooksCount', 'booksBorrowedCount', 'bookLocations', 'officeLocations')); } /** * Show the form for creating a new resource. @@ -59,9 +71,12 @@ public function index(Request $request) */ public function create() { - return view('knowledgecafe.library.books.create'); + return view('knowledgecafe.library.books.create', [ + 'officeLocations' => $this->officeLocationData(), + ]); } + /** * Store a newly created resource in storage. * @@ -71,9 +86,13 @@ public function store(BookRequest $request) { $validatedData = $request->validated(); $ISBN = $validatedData['isbn'] ?? null; - Book::firstOrCreate(['isbn' => $ISBN], $validatedData); - - return response()->json(['error' => false]); + $copiesLocation = $validatedData['copies']; + $book = Book::firstOrCreate(['isbn' => $ISBN], $validatedData); + $this->storeBookLocationWise($book->id, $copiesLocation); + return response()->json([ + 'error' => false, + 'book_id' => $book->id + ]); } /** @@ -106,10 +125,10 @@ public function show(Book $book) public function update(BookRequest $request, Book $book) { $validatedData = $request->validated(); - if (isset($validatedData['number_of_copies'])) { - $book->number_of_copies = $validatedData['number_of_copies']; + if (isset($validatedData['copies'])) { + $copiesLocation = $request['copies']; $book->save(); - + $this->updatedBookLocationWise($book->id, $copiesLocation); return response()->json(['isUpdated' => $book]); } if (isset($validatedData['categories'])) { @@ -342,4 +361,37 @@ public function bookAMonthIndex() 'booksBorrowedCount' => $booksBorrowedCount, ]); } + + public function storeBookLocationWise($bookId, $copiesLocation) + { + $locationCopiesData = $copiesLocation; + foreach ($locationCopiesData as $locationId => $copies) { + BookLocation::create([ + 'book_id' => $bookId, + 'office_location_id' => $locationId, + 'number_of_copies' => $copies + ]); + } + } + + public function officeLocationData() + { + $officeLocations = OfficeLocation::all(); + $officeLocationsData = $officeLocations->map(function ($officeLocation) { + return [ + 'center_id' => $officeLocation->id, + 'centre_name' => $officeLocation->centre_name, + ]; + }); + return $officeLocationsData; + } + + public function updatedBookLocationWise($bookId, $copiesLocation) { + foreach ($copiesLocation as $locationId => $copies) { + DB::table('library_book_location') + ->where('book_id', $bookId) + ->where('office_location_id', $locationId) + ->update(['number_of_copies' => $copies]); + } + } } diff --git a/app/Http/Requests/KnowledgeCafe/Library/BookRequest.php b/app/Http/Requests/KnowledgeCafe/Library/BookRequest.php index 9a619bb066..7c203ef3d2 100644 --- a/app/Http/Requests/KnowledgeCafe/Library/BookRequest.php +++ b/app/Http/Requests/KnowledgeCafe/Library/BookRequest.php @@ -34,6 +34,7 @@ public function rules() 'self_link' => 'nullable|string', 'number_of_copies' => 'nullable|integer|gte:1', 'on_kindle' => 'nullable', + 'copies' => 'nullable', ]; } } diff --git a/app/Models/KnowledgeCafe/Library/Book.php b/app/Models/KnowledgeCafe/Library/Book.php index 6fc510e7d2..85181bcc24 100644 --- a/app/Models/KnowledgeCafe/Library/Book.php +++ b/app/Models/KnowledgeCafe/Library/Book.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Modules\User\Entities\User; +use App\Models\KnowledgeCafe\Library\BookLocation; class Book extends Model { @@ -174,4 +175,9 @@ public function bookAMonths() { return $this->hasMany(BookAMonth::class, 'library_book_id'); } + + public function bookLocations() + { + return $this->hasMany(BookLocation::class, 'book_id'); + } } diff --git a/app/Models/KnowledgeCafe/Library/BookLocation.php b/app/Models/KnowledgeCafe/Library/BookLocation.php new file mode 100644 index 0000000000..01425c254a --- /dev/null +++ b/app/Models/KnowledgeCafe/Library/BookLocation.php @@ -0,0 +1,26 @@ +hasOne(Book::class, 'id', 'book_id'); + } + + public function location() + { + return $this->hasOne(OfficeLocation::class, 'id', 'office_location_id'); + } +} \ No newline at end of file diff --git a/database/migrations/2024_09_17_125527_create_library_book_location_copies_table.php b/database/migrations/2024_09_17_125527_create_library_book_location_copies_table.php new file mode 100644 index 0000000000..ed76116d08 --- /dev/null +++ b/database/migrations/2024_09_17_125527_create_library_book_location_copies_table.php @@ -0,0 +1,39 @@ +id(); + $table->unsignedInteger('book_id'); + $table->unsignedBigInteger('office_location_id'); + $table->integer('number_of_copies'); + $table->timestamps(); + }); + + Schema::table('library_book_location', function (Blueprint $table) { + $table->foreign('book_id')->references('id')->on('library_books'); + $table->foreign('office_location_id')->references('id')->on('office_locations'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('library_book_location'); + } +} diff --git a/resources/js/app.js b/resources/js/app.js index a7fb6212b1..db3aa9d7ac 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -748,7 +748,7 @@ if (document.getElementById("show_and_save_book")) { addMethod: "from_isbn", showInfo: false, book: {}, - number_of_copies: 1, + copies: {}, routes: { index: document.getElementById("show_book").dataset.indexRoute || "", fetch: document.getElementById("book_form").dataset.actionRoute || "", @@ -814,6 +814,7 @@ if (document.getElementById("show_and_save_book")) { this.book["on_kindle"] = document.getElementById("on_kindle").checked ? 1 : 0; + this.$set(this.book, 'copies', this.copies); axios.post(this.routes.store, this.book).then((response) => { this.buttons.disableSaveButton = false; @@ -836,6 +837,9 @@ if (document.getElementById("books_listing")) { books: document.getElementById("books_table").dataset.books ? JSON.parse(document.getElementById("books_table").dataset.books) : {}, + bookLocations: document.getElementById("books_table").dataset.locations + ? JSON.parse(document.getElementById("books_table").dataset.locations) + : [], bookCategories: document.getElementById("books_table").dataset.categories ? JSON.parse(document.getElementById("books_table").dataset.categories) : [], @@ -857,6 +861,7 @@ if (document.getElementById("books_listing")) { sortKeys: document.getElementById("category_input") ? document.getElementById("category_input").dataset.value : "", + copies:{}, }, methods: { updateCategoryMode: function(index) { @@ -939,20 +944,28 @@ if (document.getElementById("books_listing")) { return str.length > length ? str.substring(0, length) + "..." : str; }, - updateCopiesCount: function(index) { - var new_count = parseInt( - prompt( - "Number of copies of this book", - this.books[index].number_of_copies - ) - ); - if (new_count && isFinite(new_count)) { - this.books[index].number_of_copies = new_count; - axios.put(this.updateRoute + "/" + this.books[index].id, { - number_of_copies: new_count, - }); - } - }, + getTotalCopies: function(bookId) { + let totalCopies = 0; + this.bookLocations.forEach((location) => { + if (location.book_id === bookId) { + totalCopies += location.number_of_copies; + } + }); + return totalCopies; + }, + updateCopiesCount(index) { + let updatedCopies = this.books[index].copies; + axios.put(`${this.updateRoute}/${this.books[index].id}`, { + copies: updatedCopies, + }) + .then(response => { + console.log("Copies updated successfully", response); + this.$set(this.books, index, { ...this.books[index], copies: updatedCopies }); + }) + .catch(error => { + console.error("Error updating copies", error); + }); + } }, mounted: function() { @@ -965,6 +978,14 @@ if (document.getElementById("books_listing")) { allCategoryInputs.forEach( (checkbox) => (this.categoryInputs[checkbox.value] = checkbox) ); + this.books.forEach(book => { + this.$set(this.copies, book.id, {}); + if (book.copies) { + Object.keys(book.copies).forEach(locationId => { + this.$set(this.copies[book.id], locationId, book.copies[locationId]); + }); + } + }); }, }); } diff --git a/resources/views/knowledgecafe/library/books/index.blade.php b/resources/views/knowledgecafe/library/books/index.blade.php index cf52da129d..19ae4e883e 100644 --- a/resources/views/knowledgecafe/library/books/index.blade.php +++ b/resources/views/knowledgecafe/library/books/index.blade.php @@ -5,7 +5,7 @@ Loading... -
Read by