public ActionResult AddPWM(int?id) { ProductWarehouseMap model = new ProductWarehouseMap(); List <SelectListItem> ProductList = new List <SelectListItem>(); List <SelectListItem> WarehouseList = new List <SelectListItem>(); if (id == null) { ProductList.Add(new SelectListItem { Text = "Products", Value = "00000000-0000-0000-0000-000000000000" }); ViewBag.ProductList = ProductList; WarehouseList.Add(new SelectListItem { Text = "Warehouses", Value = "00000000-0000-0000-0000-000000000000" }); ViewBag.WarehouseList = WarehouseList; } //ViewBag.ProductList = productsFacade.GetAll().Select(x => // new SelectListItem() // { // Text = x.ProductName.ToString() + "(" + x.Category + " - " + x.SubCategory + ")", // Value = x.ProductId.ToString() // }).ToList(); //ViewBag.WarehouseList = wareHouseFacade.GetAll().Select(x => // new SelectListItem() // { // Text = x.WarehouseName.ToString(), // Value = x.WarehouseId.ToString() // }).ToList(); if (id.HasValue && id > 0) { model = pWMFacade.Get(id.Value); WareHouse wareHouse = wareHouseFacade.GetAll().Where(x => x.WarehouseId == model.WarehouseId).FirstOrDefault(); WarehouseList.Add(new SelectListItem { Text = wareHouse.WarehouseName, Value = wareHouse.WarehouseId.ToString(), Selected = true }); ViewBag.WarehouseList = WarehouseList; Product product = productsFacade.GetAll().Where(x => x.ProductId == model.ProductId).FirstOrDefault(); ProductList.Add(new SelectListItem { Text = product.ProductName, Value = product.ProductId.ToString(), Selected = true }); ViewBag.ProductList = ProductList; } return(View(model)); }
public ActionResult AddInvoice(SalesOrderModel SalesOrderModel) { SalesOrder salesOrder = new SalesOrder(); bool result = false; string massege = ""; try { if (SalesOrderModel.SalesOrder.Id == 0) { if (SalesOrderModel.SalesOrder.PaymentAmount >= 0 && SalesOrderModel.SalesOrder.Total >= SalesOrderModel.SalesOrder.PaymentAmount) { SalesOrderModel.SalesOrder.SalesOrderId = Guid.NewGuid(); SalesOrderModel.SalesOrder.CreatedDate = DateTime.Now; SalesOrderModel.SalesOrder.WarehouseId = Guid.NewGuid(); SalesOrderModel.SalesOrder.OrderDate = DateTime.Now; SalesOrderModel.SalesOrder.DelivaryDate = DateTime.Now; SalesOrderModel.SalesOrder.SubTotal = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount; salesFacade.Insert(SalesOrderModel.SalesOrder); if (SalesOrderModel.SalesOrderDetails.Count > 0) { foreach (var item in SalesOrderModel.SalesOrderDetails) { item.SalesOrderDetailId = Guid.NewGuid(); item.SalesOrderId = SalesOrderModel.SalesOrder.SalesOrderId; #region Product Deduct ProductWarehouseMap checkWarehouseProduct = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault(); checkWarehouseProduct.Quantity = checkWarehouseProduct.Quantity - item.Quantity; pWMFacade.Update(checkWarehouseProduct); Product oldProduct = productsFacade.GetByProductId(item.ProductId); oldProduct.Quantity = oldProduct.Quantity - item.Quantity; productsFacade.Update(oldProduct); #endregion salesDetailFacade.Insert(item); } } #region PaymentReceive PaymentReceive payment = new PaymentReceive(); payment.PaymentId = Guid.NewGuid(); payment.SalesOrderId = SalesOrderModel.SalesOrder.SalesOrderId; payment.BalanceDue = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount; payment.PaymentAmount = SalesOrderModel.SalesOrder.PaymentAmount; if (payment.BalanceDue == 0) { payment.PaymentStatus = "Paid"; } else if (payment.BalanceDue == SalesOrderModel.SalesOrder.Amount) { payment.PaymentStatus = "UnPaid"; } else { payment.PaymentStatus = "Partialy Paid"; } payment.PaymentDate = SalesOrderModel.SalesOrder.PaymentDate != null ? SalesOrderModel.SalesOrder.PaymentDate.Value : DateTime.Now; payment.Note = SalesOrderModel.SalesOrder.PaymentNote; payFacade.Insert(payment); } else { result = false; massege = "Payment amount should be less or equal to the Total amount"; return(Json(new { result = result, message = massege })); } #endregion } else { PaymentReceive oldPayment = payFacade.GetPaymentBySOId(SalesOrderModel.SalesOrder.SalesOrderId); SalesOrder sales = salesFacade.Get(SalesOrderModel.SalesOrder.Id); if (SalesOrderModel.SalesOrder.PaymentAmount >= 0 && sales.Amount >= (oldPayment.PaymentAmount + SalesOrderModel.SalesOrder.PaymentAmount)) { sales.OrderDate = SalesOrderModel.SalesOrder.OrderDate; sales.DelivaryDate = SalesOrderModel.SalesOrder.DelivaryDate; sales.DiscountAmount = SalesOrderModel.SalesOrder.DiscountAmount; sales.Amount = SalesOrderModel.SalesOrder.Amount; sales.Freight = SalesOrderModel.SalesOrder.Freight; sales.Total = SalesOrderModel.SalesOrder.Total; salesFacade.Update(sales); List <SalesOrderDetailVM> salesdetaillist = salesDetailFacade.GetAllSalesDetailsBySaleOrderId(sales.SalesOrderId); if (SalesOrderModel.SalesOrderDetails.Count > 0) { foreach (var item in salesdetaillist) { ProductWarehouseMap productWarehouseMap = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault(); if (productWarehouseMap != null) { productWarehouseMap.Quantity = productWarehouseMap.Quantity + item.Quantity; pWMFacade.Update(productWarehouseMap); } Product oldProduct = productsFacade.GetByProductId(item.ProductId); if (oldProduct != null) { oldProduct.Quantity = oldProduct.Quantity + item.Quantity; productsFacade.Update(oldProduct); } salesDetailFacade.Delete(item.Id); } foreach (var item in SalesOrderModel.SalesOrderDetails) { SalesOrderDetail tempSalesOrderDetail = new SalesOrderDetail(); tempSalesOrderDetail.Price = item.Price; tempSalesOrderDetail.ProductId = item.ProductId; tempSalesOrderDetail.Quantity = item.Quantity; tempSalesOrderDetail.SalesOrderDetailId = Guid.NewGuid(); tempSalesOrderDetail.SalesOrderId = SalesOrderModel.SalesOrder.SalesOrderId; tempSalesOrderDetail.SubTotal = item.SubTotal; tempSalesOrderDetail.Total = item.Total; tempSalesOrderDetail.Amount = item.Amount; tempSalesOrderDetail.WarehouseId = item.WarehouseId; #region Product Deduct ProductWarehouseMap checkWarehouseProduct = pWMFacade.GetAll().Where(x => x.ProductId == item.ProductId && x.WarehouseId == item.WarehouseId).FirstOrDefault(); if (checkWarehouseProduct != null) { checkWarehouseProduct.Quantity = checkWarehouseProduct.Quantity - item.Quantity; pWMFacade.Update(checkWarehouseProduct); } Product oldProduct = productsFacade.GetByProductId(item.ProductId); if (oldProduct != null) { oldProduct.Quantity = oldProduct.Quantity - item.Quantity; productsFacade.Update(oldProduct); } #endregion salesDetailFacade.Insert(tempSalesOrderDetail); } } #region PaymentReceive oldPayment.BalanceDue = SalesOrderModel.SalesOrder.Amount - SalesOrderModel.SalesOrder.PaymentAmount; oldPayment.PaymentAmount = (oldPayment.PaymentAmount + SalesOrderModel.SalesOrder.PaymentAmount); oldPayment.BalanceDue = SalesOrderModel.SalesOrder.Amount - oldPayment.PaymentAmount; sales.SubTotal = oldPayment.BalanceDue; salesFacade.Update(sales); if (oldPayment.BalanceDue == 0) { oldPayment.PaymentStatus = "Paid"; } else if (oldPayment.BalanceDue == SalesOrderModel.SalesOrder.Amount) { oldPayment.PaymentStatus = "UnPaid"; } else { oldPayment.PaymentStatus = "Partially Paid"; } oldPayment.PaymentDate = SalesOrderModel.SalesOrder.PaymentDate != null ? SalesOrderModel.SalesOrder.PaymentDate.Value : DateTime.Now; oldPayment.Note = SalesOrderModel.SalesOrder.PaymentNote; payFacade.Update(oldPayment); } else { result = false; massege = "Payment amount should be less or equal to the Total amount"; return(Json(new { result = result, message = massege })); } #endregion } result = true; massege = "Invoice saved successfully"; } catch (Exception ex) { result = false; massege = "Invoice not saved"; } return(Json(new { result = result, message = massege })); }
public JsonResult SavePWM(ProductWarehouseMap newPWM) { var result = false; var message = ""; if (newPWM != null) { Product tempProduct = productsFacade.GetByProductId(newPWM.ProductId); var RemainQuantity = tempProduct.Quantity; if (newPWM.Id > 0) { var oldPWM = pWMFacade.Get(newPWM.Id); List <ProductWarehouseMap> pwmList = pWMFacade.GetAll().Where(x => x.ProductId == newPWM.ProductId).ToList(); if (pwmList.Count > 0) { RemainQuantity = tempProduct.Quantity - pwmList.Where(x => x.Id != oldPWM.Id).Sum(x => Convert.ToInt32(x.Quantity)); } if (RemainQuantity < newPWM.Quantity) { message = "Quantity must be less or equal to the remaining product"; return(Json(new { result = result, message = message })); } oldPWM.WarehouseId = newPWM.WarehouseId; oldPWM.ProductId = newPWM.ProductId; oldPWM.Quantity = newPWM.Quantity; if (pWMFacade.Update(oldPWM) > 0) { result = true; } } else { List <ProductWarehouseMap> pwmList = pWMFacade.GetAll().Where(x => x.ProductId == newPWM.ProductId).ToList(); if (pwmList.Count > 0) { RemainQuantity = tempProduct.Quantity - pwmList.Sum(x => Convert.ToInt32(x.Quantity)); } if (RemainQuantity < newPWM.Quantity) { message = "Quantity must be less or equal to the remaining product"; return(Json(new { result = result, message = message })); } var samePWM = pWMFacade.GetAll().Where(x => x.ProductId == newPWM.ProductId && x.WarehouseId == newPWM.WarehouseId).FirstOrDefault(); if (samePWM != null) { samePWM.WarehouseId = newPWM.WarehouseId; samePWM.ProductId = newPWM.ProductId; samePWM.Quantity += newPWM.Quantity; if (pWMFacade.Update(samePWM) > 0) { result = true; } } else { newPWM.ProductWarehouseMapId = Guid.NewGuid(); if (pWMFacade.Insert(newPWM) > 0) { result = true; } } } } return(Json(new { result = result })); }