public async Task <bool> AddMasterCustomer(MasterCustomerModel model) { MasterCustomer customer = new MasterCustomer() { Id = model.Id, Name = model.Name, Phone = model.Phone }; await _dbContext.MasterCustomers.AddAsync(customer); int affectedRows = await _dbContext.SaveChangesAsync(); return(affectedRows > 0); }
public async Task <JsonResult> PurchaseItem(ItemModel[] items, string customerName, string mobileNumber, string saleOrReturn) { if (items == null || items.Count() == 0) { return(Json(data: new { success = "fail", message = "There is no item selected from item list", data = "" })); } if (string.IsNullOrEmpty(customerName) || string.IsNullOrWhiteSpace(customerName)) { return(Json(data: new { success = "fail", message = "Customer Name is a required field", data = "" })); } if (string.IsNullOrEmpty(mobileNumber) || string.IsNullOrWhiteSpace(mobileNumber)) { return(Json(data: new { success = "fail", message = "Mobile Number is a required field", data = "" })); } if (mobileNumber.Length > 10) { return(Json(data: new { success = "fail", message = "Invalid mobile number", data = "" })); } if (customerName.Length > 10) { return(Json(data: new { success = "fail", message = "Invalid customer name", data = "" })); } if (string.IsNullOrEmpty(saleOrReturn) || string.IsNullOrWhiteSpace(saleOrReturn)) { return(Json(data: new { success = "fail", message = "Invalid Sale Or Return", data = "" })); } if (saleOrReturn != "S" && saleOrReturn != "R") { return(Json(data: new { success = "fail", message = "Invalid Sale Or Return", data = "" })); } var customer = await _repo.MasterCustomerByPhone(mobileNumber); if (customer == null) { MasterCustomerModel customerModel = new MasterCustomerModel() { Id = _generateID.GenerateId(), Name = customerName, Phone = mobileNumber }; bool isAdded = await _repo.AddMasterCustomer(model : customerModel); if (!isAdded) { return(Json(data: new { success = "fail", message = "Technical error! Please try again after some time.", data = "" })); } customer = customerModel; } bool isItemAvailable = await _repo.IsItemAvailable(items : items); if (!isItemAvailable) { return(Json(data: new { success = "fail", message = "Selected item is more than the stock", data = "" })); } decimal totalPrice = await _repo.TotalAmmountOfItems(items : items, ids : items.Select(x => x.Id).ToArray()); POSMainModel mainModel = new POSMainModel() { Id = _generateID.GenerateId(), CustomerId = customer.Id, PosDate = DateTime.Now, TotalAmount = totalPrice, TotalQuantity = items.Select(x => x.Quantity).Sum() }; bool isPosAdded = await _repo.AddPOSMain(mainModel); if (isPosAdded) { bool isDetailsAdded = await _repo.AddPosDetails(items : items, posMainId : mainModel.Id, saleOrReturn : saleOrReturn); if (isDetailsAdded) { if (saleOrReturn == "S") { bool isStockRemoved = await _repo.RemoveStockMasterItem(items : items); } else { bool isStockAdded = await _repo.AddStockMasterItem(items : items); } return(Json(data: new { success = "success", message = "Purchase success.", data = "" })); } else { await _repo.DeletePosMain(id : mainModel.Id); return(Json(data: new { success = "fail", message = "Technical error! Please try again after some time.", data = "" })); } } else { return(Json(data: new { success = "fail", message = "Technical error! Please try again after some time.", data = "" })); } }