Skip to content

Commit

Permalink
implemented transactions display on sale of products and print them:
Browse files Browse the repository at this point in the history
:wq
  • Loading branch information
raghav2404 committed Dec 3, 2024
1 parent 9f42a68 commit a5c2d04
Show file tree
Hide file tree
Showing 42 changed files with 1,236 additions and 48 deletions.
Binary file modified .DS_Store
Binary file not shown.
45 changes: 45 additions & 0 deletions ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# modified: .DS_Store
# modified: Controllers/CategoriesController.cs
# deleted: Models/CategoryRepositoruy.cs
# modified: Program.cs
# modified: Views/Categories/Index.cshtml
# modified: Views/Shared/_Layout.cshtml
# modified: bin/Debug/net8.0/demoMvcCore.dll
# modified: bin/Debug/net8.0/demoMvcCore.pdb
# modified: demoMvcCore.csproj
# modified: obj/Debug/net8.0/demoMvcCore.AssemblyInfo.cs
# modified: obj/Debug/net8.0/demoMvcCore.AssemblyInfoInputs.cache
# modified: obj/Debug/net8.0/demoMvcCore.GeneratedMSBuildEditorConfig.editorconfig
# modified: obj/Debug/net8.0/demoMvcCore.csproj.CoreCompileInputs.cache
# modified: obj/Debug/net8.0/demoMvcCore.csproj.FileListAbsolute.txt
# modified: obj/Debug/net8.0/demoMvcCore.dll
# modified: obj/Debug/net8.0/demoMvcCore.pdb
# modified: obj/Debug/net8.0/ref/demoMvcCore.dll
# modified: obj/Debug/net8.0/refint/demoMvcCore.dll
# modified: wwwroot/css/site.css
#
# Untracked files:
# Controllers/ProductsController.cs
# Controllers/SalesController.cs
# Controllers/TransactionsController.cs
# Models/CategoryRepository.cs
# Models/Product.cs
# Models/ProductRepository.cs
# Models/Transaction.cs
# Models/TransactionsRepository.cs
# ViewComponents/
# ViewModels/
# Views/Products/
# Views/Sales/
# Views/Shared/Components/
# Views/Transactions/
# obj/Debug/net8.0/demoMvcCore.sourcelink.json
#
13 changes: 5 additions & 8 deletions Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using demoMvcCore.Models;
using demoMvcCore.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand Down Expand Up @@ -38,13 +34,14 @@ public IActionResult Edit(Category category)
CategoryRepository.UpdateCategory(category.CategoryId, category);
return RedirectToAction(nameof(Index));
}
ViewBag.OnClick = "edit";
return View(category);
}


public IActionResult Add()
{
//ViewData["OnClick"] = "add";

ViewBag.OnClick = "add";
return View();
}
Expand All @@ -58,11 +55,11 @@ public IActionResult Add(Category category)
return RedirectToAction(nameof(Index));

}

ViewBag.OnClick = "add";
return View(category);
}

[HttpGet]

public IActionResult Delete(int categoryId)
{
CategoryRepository.DeleteCategory(categoryId);
Expand Down
100 changes: 100 additions & 0 deletions Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using demoMvcCore.Models;
using demoMvcCore.ViewModels;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace demoMvcCore.Controllers
{
public class ProductsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
var products = ProductsRepository.GetProducts(loadCategory:true);
return View(products);

}

public IActionResult Add()
{
ViewBag.Action = "add";
var productViewModels = new ProductsViewModel
{
Categories = CategoryRepository.GetCategories(),
//Product
};
return View(productViewModels);


}

[HttpPost]
public IActionResult Add(ProductsViewModel productsViewModel)
{
if (ModelState.IsValid)
{
ProductsRepository.AddProduct(productsViewModel.Product);
return RedirectToAction(nameof(Index));
}
ViewBag.Action = "edit";
productsViewModel.Categories = CategoryRepository.GetCategories();
return View(productsViewModel);
}




public IActionResult Edit(int id)
{
ViewBag.Action = "edit";
var productView = new ProductsViewModel
{
Product = ProductsRepository.GetProductById(id) ?? new Product(),
Categories = CategoryRepository.GetCategories()
};
return View(productView);
}

[HttpPost]

public IActionResult Edit(ProductsViewModel productsViewModel)
{
if (ModelState.IsValid)
{
ProductsRepository.UpdateProduct(productsViewModel.Product.ProductId, productsViewModel.Product);

return RedirectToAction(nameof(Index));
}
ViewBag.Action = "edit";
productsViewModel.Categories = CategoryRepository.GetCategories();
return View(productsViewModel);
}


public IActionResult Delete(int id)
{
ProductsRepository.DeleteProduct(id);
return RedirectToAction(nameof(Index));
}

public IActionResult ProductsByCategoryPartial(int categoryId)
{
var products = ProductsRepository.GetProductsByCategoryId(categoryId);
return PartialView("_Product",products);

}





}
}

58 changes: 58 additions & 0 deletions Controllers/SalesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using demoMvcCore.Models;
using demoMvcCore.ViewModels;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace demoMvcCore.Controllers
{
public class SalesController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
var salesVm = new SalesViewModel
{
Categories = CategoryRepository.GetCategories()
};
return View(salesVm);
}


public IActionResult SellProductPartial(int productId)
{
var product = ProductsRepository.GetProductById(productId);

return PartialView("_SellProduct", product);


}

public IActionResult Sell(SalesViewModel salesViewModel)
{
if(ModelState.IsValid)
{
var prod = ProductsRepository.GetProductById(salesViewModel.SelectedProductId);
if(prod !=null)
{
TransactionsRepository.Add("Cashier1"
, salesViewModel.SelectedProductId,
prod.Name,
prod.Price.HasValue ? prod.Price.Value : 0,
prod.Quantity.HasValue ? prod.Quantity.Value : 0,
salesViewModel.QuantityToSell);

prod.Quantity -= salesViewModel.QuantityToSell;
ProductsRepository.UpdateProduct(salesViewModel.SelectedProductId, prod);


}
}
var product = ProductsRepository.GetProductById(salesViewModel.SelectedProductId);
salesViewModel.SelectedCategoryId = (product?.CategoryId is null) ? 0 : product.CategoryId.Value;
salesViewModel.Categories = CategoryRepository.GetCategories();
return View("Index", salesViewModel);
}
}
}

36 changes: 36 additions & 0 deletions Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using demoMvcCore.Models;
using demoMvcCore.ViewModels;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace demoMvcCore.Controllers
{
public class TransactionsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
TransactionsViewModel transactionsVM = new();

return View(transactionsVM);
}



public IActionResult Search(TransactionsViewModel transactionsViewModel)
{

//search

var transactions = TransactionsRepository.Search(transactionsViewModel.CashierName ?? string.Empty, transactionsViewModel.StartDate, transactionsViewModel.EndDate);
transactionsViewModel.Transactions = transactions;

return View("Index", transactionsViewModel);


}
}
}


18 changes: 14 additions & 4 deletions Models/CategoryRepositoruy.cs → Models/CategoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public CategoryRepository()

public static void AddCategory(Category category)
{
var maxId = _categories.Max(x => x.CategoryId);
category.CategoryId = maxId + 1;
_categories.Add(category);
}
if (_categories is not null && _categories.Count > 0)
{
var maxId = _categories.Max(x => x.CategoryId);
category.CategoryId = maxId + 1;

}
else
{
var maxId = 1;
category.CategoryId = maxId;
}
_categories ??= new List<Category>();
_categories.Add(category);
}
public static Category? GetCategoryById(int categoryId) =>
_categories.FirstOrDefault(x => x.CategoryId == categoryId);

Expand Down
28 changes: 28 additions & 0 deletions Models/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel.DataAnnotations;

namespace demoMvcCore.Models
{

public class Product
{
public int ProductId { get; set; }

[Required]
[Display(Name = "Category")]
public int? CategoryId { get; set; }

[Required]
public string Name { get; set; } = string.Empty;

[Required]
public int? Quantity { get; set; }

[Required]
[Range(0, int.MaxValue)]
public double? Price { get; set; }
public Category? Category { get; set; }


}
}

Loading

0 comments on commit a5c2d04

Please sign in to comment.