public async Task <SalesDoc> Create(SalesDoc model) { int Created = 0; using (var transaction = this.DbContext.Database.BeginTransaction()) { try { string code = GenerateCode("sales"); model.Code = code; model.FlagForCreate(IdentityService.Username, UserAgent); model.FlagForUpdate(IdentityService.Username, UserAgent); if (model.isReturn != true) { model.isReturn = false; } model.isVoid = false; TransferOutDocViewModel transferOutDocViewModel = new TransferOutDocViewModel(); List <TransferOutDocItemViewModel> transferOutDocItemViewModels = new List <TransferOutDocItemViewModel>(); TransferInDocViewModel transferInDocView = new TransferInDocViewModel(); List <TransferInDocItemViewModel> transferInDocItemViews = new List <TransferInDocItemViewModel>(); transferOutDocViewModel.code = code; transferOutDocViewModel.reference = code; transferOutDocViewModel.source = new SourceViewModel { _id = model.StoreStorageId, code = model.StoreStorageCode, name = model.StoreStorageName }; transferOutDocViewModel.destination = new DestinationViewModel { _id = model.StoreStorageId, code = model.StoreStorageCode, name = model.StoreStorageName }; transferOutDocViewModel.remark = model.Remark; bool isAnyTransferIn = false; transferInDocView.code = code; transferInDocView.reference = code; transferInDocView.source = new SourceViewModel { _id = model.StoreStorageId, code = model.StoreStorageCode, name = model.StoreStorageName }; transferInDocView.destination = new DestinationViewModel { _id = model.StoreStorageId, code = model.StoreStorageCode, name = model.StoreStorageName }; transferInDocView.remark = model.Remark; foreach (var item in model.Details) { if (!item.isReturn) { transferOutDocItemViewModels.Add(new TransferOutDocItemViewModel { articleRealizationOrder = item.ItemArticleRealizationOrder, remark = model.Remark, quantity = item.Quantity, item = new ItemViewModels { articleRealizationOrder = item.ItemArticleRealizationOrder, code = item.ItemCode, domesticCOGS = item.ItemDomesticCOGS, domesticRetail = item.ItemDomesticRetail, domesticSale = item.ItemDomesticSale, domesticWholesale = item.ItemDomesticWholeSale, name = item.ItemName, size = item.ItemSize, uom = item.ItemUom, _id = item.ItemId, } }); } else if (item.isReturn) { transferInDocItemViews.Add(new TransferInDocItemViewModel { remark = model.Remark, sendquantity = item.Quantity, item = new ItemViewModels { articleRealizationOrder = item.ItemArticleRealizationOrder, code = item.ItemCode, domesticCOGS = item.ItemDomesticCOGS, domesticRetail = item.ItemDomesticRetail, domesticSale = item.ItemDomesticSale, domesticWholesale = item.ItemDomesticWholeSale, name = item.ItemName, size = item.ItemSize, uom = item.ItemUom, _id = item.ItemId, } }); isAnyTransferIn = true; } item.FlagForCreate(IdentityService.Username, UserAgent); item.FlagForUpdate(IdentityService.Username, UserAgent); } transferOutDocViewModel.items = transferOutDocItemViewModels; transferInDocView.items = transferInDocItemViews; if (isAnyTransferIn) { string warehouseUritransferin = "transfer-in/for-pos"; var httpClienttarnsfer = (IHttpClientService)ServiceProvider.GetService(typeof(IHttpClientService)); var responsetransfer = await httpClienttarnsfer.PostAsync($"{APIEndpoint.Warehouse}{warehouseUritransferin}", new StringContent(JsonConvert.SerializeObject(transferInDocView).ToString(), Encoding.UTF8, General.JsonMediaType)); responsetransfer.EnsureSuccessStatusCode(); } string warehouseUri = "transfer-out/pos"; var httpClient = (IHttpClientService)ServiceProvider.GetService(typeof(IHttpClientService)); var response = await httpClient.PostAsync($"{APIEndpoint.Warehouse}{warehouseUri}", new StringContent(JsonConvert.SerializeObject(transferOutDocViewModel).ToString(), Encoding.UTF8, General.JsonMediaType)); response.EnsureSuccessStatusCode(); model.FlagForCreate(IdentityService.Username, UserAgent); model.FlagForUpdate(IdentityService.Username, UserAgent); DbSet.Add(model); Created = await DbContext.SaveChangesAsync(); Created = model.Id; transaction.Commit(); } catch (Exception e) { transaction.Rollback(); throw new Exception(e.Message); } } return(model); }
//public List<SalesDocDetailReturnItem> ReadSalesReturnItem(int id) //{ // var b = DbSetSales.Where(m => m.SalesDocDetailId == id); // return b.ToList(); //} public async Task <int> Create(SalesDocReturn model, SalesDocReturnViewModel viewModel) { int Created = 0; using (var transaction = this.DbContext.Database.BeginTransaction()) { try { string code = GenerateCode("sales"); SalesDoc sales = new SalesDoc(); sales.Remark = viewModel.remark; if (viewModel.salesDetail.bankCard != null) { sales.BankCardCode = viewModel.salesDetail.bankCard.code; sales.BankCardId = viewModel.salesDetail.bankCard._id; sales.BankCardName = viewModel.salesDetail.bankCard.name; } if (viewModel.salesDetail.bank != null) { sales.BankCode = viewModel.salesDetail.bank.code; sales.BankId = viewModel.salesDetail.bank._id; sales.BankName = viewModel.salesDetail.bank.name; } sales.CardAmount = viewModel.salesDetail.cardAmount; sales.CardName = viewModel.salesDetail.cardName; sales.CardNumber = viewModel.salesDetail.cardNumber; if (viewModel.salesDetail.cardType != null) { sales.CardTypeCode = viewModel.salesDetail.cardType.code; sales.CardTypeId = viewModel.salesDetail.cardType._id; sales.CardTypeName = viewModel.salesDetail.cardType.name; } if (viewModel.salesDetail.voucher != null) { sales.VoucherValue = viewModel.salesDetail.voucher.value; } sales.CashAmount = viewModel.salesDetail.cashAmount; sales.Date = viewModel.date; sales.Discount = 0; sales.Card = viewModel.salesDetail.card; sales.PaymentType = viewModel.salesDetail.paymentType; sales.isVoid = false; sales.Shift = viewModel.shift; sales.StoreCode = viewModel.store.Code; sales.StoreId = viewModel.store.Id; sales.StoreName = viewModel.store.Name; sales.StoreStorageCode = viewModel.store.Storage.code; sales.StoreStorageId = viewModel.store.Storage._id; sales.StoreStorageName = viewModel.store.Storage.name; sales.SubTotal = viewModel.subTotal; sales.TotalProduct = viewModel.totalProduct; sales.GrandTotal = viewModel.total; sales.Discount = viewModel.totalDiscount; List <SalesDocDetail> docDetails = new List <SalesDocDetail>(); foreach (var i in viewModel.items) { docDetails.Add(new SalesDocDetail { Discount1 = i.discount1, Discount2 = i.discount2, DiscountNominal = i.discountNominal, isReturn = true, ItemArticleRealizationOrder = i.item.item.ArticleRealizationOrder, ItemCode = i.item.item.code, ItemDomesticCOGS = i.item.item.DomesticCOGS, ItemDomesticRetail = i.item.item.DomesticRetail, ItemDomesticSale = i.item.item.DomesticSale, ItemDomesticWholeSale = i.item.item.DomesticWholeSale, ItemId = i.item.item._id, ItemName = i.item.item.name, ItemSize = i.item.item.Size, ItemUom = i.item.item.Uom, Margin = i.margin, Price = i.price, Quantity = i.quantity, Size = i.item.item.Size, SpesialDiscount = i.specialDiscount, Total = i.total }); foreach (var retur in i.returnItems) { docDetails.Add(new SalesDocDetail { Discount1 = retur.discount1, Discount2 = retur.discount2, DiscountNominal = retur.discountNominal, isReturn = false, ItemArticleRealizationOrder = retur.item.ArticleRealizationOrder, ItemCode = retur.item.code, ItemDomesticCOGS = retur.item.DomesticCOGS, ItemDomesticRetail = retur.item.DomesticRetail, ItemDomesticSale = retur.item.DomesticSale, ItemDomesticWholeSale = retur.item.DomesticWholeSale, ItemId = retur.item._id, ItemName = retur.item.name, ItemSize = retur.item.Size, ItemUom = retur.item.Uom, Margin = retur.margin, Price = retur.price, Quantity = retur.quantity, Size = retur.item.Size, SpesialDiscount = retur.specialDiscount, Total = retur.total }); } } //foreach(var i in sales.Details) //{ // if(viewModel.items.Where(x=>x.item.code == i.ItemCode).Count() > 0) // { // if(viewModel.items.Single().returnItems.Count() > 0) // { // i.isReturn = true; // } // } //} sales.Details = docDetails; sales.isReturn = true; sales.Code = code; sales.FlagForCreate(IdentityService.Username, UserAgent); sales.FlagForUpdate(IdentityService.Username, UserAgent); sales.isVoid = false; var salesreturn = await AddSales(sales); model.SalesDocReturnCode = salesreturn.Code; model.SalesDocReturnId = salesreturn.Id; model.SalesDocReturnIsReturn = sales.isReturn; model.Code = sales.Code; model.FlagForCreate(IdentityService.Username, UserAgent); model.FlagForUpdate(IdentityService.Username, UserAgent); foreach (var item in model.Details) { item.FlagForCreate(IdentityService.Username, UserAgent); item.FlagForUpdate(IdentityService.Username, UserAgent); } DbSet.Add(model); Created = await DbContext.SaveChangesAsync(); List <SalesDocDetailReturnItem> docDetail = new List <SalesDocDetailReturnItem>(); foreach (var i in viewModel.items) { var t = sales.Details.Where(x => x.ItemCode == i.item.item.code && x.ItemArticleRealizationOrder == i.item.item.ArticleRealizationOrder).Single(); foreach (var d in i.returnItems) { docDetail.Add(new SalesDocDetailReturnItem { Discount1 = d.discount1, Discount2 = d.discount2, DiscountNominal = d.discountNominal, isReturn = true, ItemArticleRealizationOrder = d.item.ArticleRealizationOrder, ItemCode = d.item.code, ItemDomesticCOGS = d.item.DomesticCOGS, ItemDomesticRetail = d.item.DomesticRetail, ItemDomesticSale = d.item.DomesticSale, ItemDomesticWholeSale = d.item.DomesticWholeSale, ItemId = d.item._id, ItemName = d.item.name, ItemSize = d.item.Size, ItemUom = d.item.Uom, Margin = d.margin, Price = d.price, Quantity = d.quantity, Size = d.item.Size, SpesialDiscount = d.specialDiscount, Total = d.total, SalesDocDetailId = t.Id, }); } } Created += await AddReturnItems(docDetail); transaction.Commit(); } catch (Exception e) { transaction.Rollback(); throw new Exception(e.Message); } } return(model.Id); }