public async Task <IActionResult> Update([FromBody] PaymentViewModel value) { if (ModelState.IsValid) { var ioId = Guid.NewGuid(); var io = new Payment { PaymentId = ioId, PaymentCode = value.Code, CreatedBy = userId, CreatedDate = DateTime.Now, UpdatedBy = userId, UpdatedDate = DateTime.Now, PaymentTypeId = value.PaymentTypeId, BranchId = ioId, Note = value.Note }; var ioDetails = value.IODetails.Select(p => new PaymentDetail { PaymentDetailId = p.ID == null ? Guid.NewGuid() : p.ID.Value, PaymentId = ioId, IOStockId = p.IOStockId, IONumber = p.Code, PriceBeforeVAT = 0, PriceAfterVAT = 0, VAT = 0, TotalPricePayment = p.TotalPricePayment, TotalPriceExist = p.TotalPriceExist, TotalPrice = p.TotalPrice, Note = p.Note, CreatedBy = userId, CreatedDate = DateTime.Now, UpdatedBy = userId, UpdatedDate = DateTime.Now }); io.TotalMoney = ioDetails.Sum(p => p.TotalPricePayment); io.TotalMoneyAgain = ioDetails.Sum(p => p.TotalPrice - p.TotalPricePayment); io.TotalMoneyReturn = ioDetails.Sum(p => p.TotalPrice - p.TotalPricePayment); var ret = await this._unitOfWork.Payments.Save(io, ioDetails.ToArray(), value.ID); value.UpdatedByName = ret.UpdatedByName; value.PaymentTypeName = ret.PaymentTypeName; // print // send email var bodyHtml = EmailTemplates.SendEmail_Payment(_serviceSmtpConfig, value); ret.Html = EmailTemplates.GetTempalteEmail(_serviceSmtpConfig, value); return(await GetPayment(ret.PaymentId, ret.Html)); } return(BadRequest(ModelState)); }
private async Task <IOStock> SaveIOStock(IOStockViewModel value) { if (ModelState.IsValid) { if (!string.IsNullOrEmpty(value.PurchaseOrderCode)) { //check purchaseOrder var results = this._unitOfWork.PurchaseOrders.GetPurchaseOrderListDetail( ObjectHelper.GetDateMin, ObjectHelper.GetDateNow, value.PurchaseOrderId.ToString(), this._unitOfWork.Branches.GetAllBranchOfUserString(userId), value.ID.HasValue ? value.ID.Value.ToString() : string.Empty, 0, 0 ); var iods = value.IODetails; foreach (var item in results) { var itemExist = iods.FirstOrDefault(p => p.PurchaseOrderDetailId == item.PurchaseOrderDetailId); if (itemExist != null) { if ((item.PurchaseQuantity - item.IOQuantity) >= itemExist.Quantity) { continue; } else { throw new Exception("Đã hết đơn đặt hàng"); } } else { throw new Exception("Not exist purchase order"); } } } var ioId = Guid.NewGuid(); var io = new IOStock { IOStockId = ioId, IONumber = value.Code, SupplierId = value.SupplierId, CreatedBy = userId, CreatedDate = value.CreateDate, UpdatedBy = userId, UpdatedDate = DateTime.Now, IOTypeId = value.IOTypeId, BranchId = ioId, StudentId = value.StudentId, Note = value.Note, PurchaseOrderCode = value.PurchaseOrderCode, PurchaseOrderId = value.PurchaseOrderId }; var ioDetails = new List <IOStockDetail>(); foreach (var p in value.IODetails) { var ioDetailId = p.ID == null || !value.ID.HasValue ? Guid.NewGuid() : p.ID.Value; var itemNew = new IOStockDetail { IOStockDetailId = ioDetailId, IOStockId = ioId, MaterialId = p.MaterialId, MaterialCode = p.MaterialCode, PriceBeforeVAT = p.SellPrice, PriceAfterVAT = p.SellPrice, VAT = 0, InputQuantity = p.Quantity, DisCountMoney = p.DisCountMoney, TotalPrice = p.SellPrice * p.Quantity - (p.DisCountMoney ?? 0), TotalPriceBeforeVAT = p.SellPrice * p.Quantity - (p.DisCountMoney ?? 0), CreatedBy = userId, CreatedDate = value.CreateDate, UpdatedBy = userId, UpdatedDate = DateTime.Now, PurchaseOrderDetailId = p.PurchaseOrderDetailId, PurchaseOrderId = p.PurchaseOrderId, IOPros = p.Pros.Select(c => new IOStockDetailPro { IOStockDetailId = ioDetailId, IOStockId = ioId, PromotionId = c.PromotionId, PromotionDetailId = c.PromotionDetailId, PercentDiscount = c.PercentDiscount, MoneyDiscount = c.MoneyDiscount, IOStockDetailProId = Guid.NewGuid(), CreatedBy = userId, CreatedDate = value.CreateDate, UpdatedBy = userId, UpdatedDate = DateTime.Now, }).ToArray() }; ioDetails.Add(itemNew); } io.TotalPrice = ioDetails.Sum(p => p.TotalPrice) - ioDetails.Sum(p => p.DisCountMoney); io.TotalPriceBeforeVAT = ioDetails.Sum(p => p.TotalPriceBeforeVAT) - ioDetails.Sum(p => p.DisCountMoney); var ret = await this._unitOfWork.IOStocks.Save(io, ioDetails.ToArray(), value.ID); value.UpdatedByName = ret.UpdatedByName; // send email var bodyHtml = EmailTemplates.SendEmail_IOStock(_serviceSmtpConfig, value); ret.Html = EmailTemplates.GetTempalteEmail(_serviceSmtpConfig, value); return(ret); } return(null); }