public IActionResult GetById([FromRoute] int id) { try { SalesDocReturn model = Service.ReadModelById(id); SalesDocReturnViewModel viewModel = Service.MapToViewModel(model); if (model == null) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.NOT_FOUND_STATUS_CODE, General.NOT_FOUND_MESSAGE) .Fail(); return(NotFound(Result)); } else { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.OK_STATUS_CODE, General.OK_MESSAGE) .Ok(viewModel); return(Ok(Result)); } } catch (Exception e) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message) .Fail(); return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result)); } }
public async Task <IActionResult> Post([FromBody] SalesDocReturnViewModel viewModel) { try { VerifyUser(); ValidateService.Validate(viewModel); SalesDocReturn model = Service.MapToModel(viewModel); var id = await Service.Create(model, viewModel); Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.CREATED_STATUS_CODE, General.OK_MESSAGE) .Ok(id); return(Created(String.Concat(Request.Path, "/", 0), Result)); } catch (ServiceValidationExeption e) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.BAD_REQUEST_STATUS_CODE, General.BAD_REQUEST_MESSAGE) .Fail(e); return(BadRequest(Result)); } catch (Exception e) { Dictionary <string, object> Result = new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message) .Fail(); return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result)); } }
public SalesDocReturn MapToModel(SalesDocReturnViewModel viewModel) { SalesDocReturn model = new SalesDocReturn(); PropertyCopier <SalesDocReturnViewModel, SalesDocReturn> .Copy(viewModel, model); model.Date = viewModel.date; model.IsVoid = viewModel.isVoid; model.StoreCode = viewModel.store.Code; model.StoreId = viewModel.store.Id; model.StoreName = viewModel.store.Name; model.StoreStorageCode = viewModel.store.Storage.Code; model.StoreStorageId = viewModel.store.Storage._id; model.StoreStorageName = viewModel.store.Storage.Name; model.SalesDocCode = viewModel.sales.code; model.SalesDocId = viewModel.sales.Id; model.SalesDocIsReturn = viewModel.sales.isReturn; model.Details = new List <SalesDocReturnDetail>(); foreach (SalesDocReturnDetailViewModel i in viewModel.items) { SalesDocReturnDetail salesDocDetail = new SalesDocReturnDetail(); PropertyCopier <SalesDocReturnDetailViewModel, SalesDocReturnDetail> .Copy(i, salesDocDetail); salesDocDetail.Discount1 = i.discount1; salesDocDetail.Discount2 = i.discount2; salesDocDetail.DiscountNominal = i.discountNominal; salesDocDetail.isReturn = false; salesDocDetail.ItemArticleRealizationOrder = i.item.item.ArticleRealizationOrder; salesDocDetail.ItemCode = i.item.item.code; salesDocDetail.ItemDomesticCOGS = i.item.item.DomesticCOGS; salesDocDetail.ItemDomesticRetail = i.item.item.DomesticRetail; salesDocDetail.ItemDomesticSale = i.item.item.DomesticSale; salesDocDetail.ItemDomesticWholeSale = i.item.item.DomesticWholeSale; salesDocDetail.ItemId = i.item.item._id; salesDocDetail.ItemName = i.item.item.name; salesDocDetail.ItemSize = i.item.item.Size; salesDocDetail.ItemUom = i.item.item.Uom; salesDocDetail.Margin = i.margin; salesDocDetail.Price = i.price; salesDocDetail.Quantity = i.quantity; salesDocDetail.Size = i.item.item.Size; salesDocDetail.SpesialDiscount = i.specialDiscount; salesDocDetail.Total = i.total; model.Details.Add(salesDocDetail); } 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); }
public SalesDocReturnViewModel MapToViewModel(SalesDocReturn model) { SalesDocReturnViewModel viewModel = new SalesDocReturnViewModel(); PropertyCopier <SalesDocReturn, SalesDocReturnViewModel> .Copy(model, viewModel); viewModel.code = model.Code; viewModel.date = model.Date; viewModel.Id = model.Id; viewModel.isVoid = model.IsVoid; var sales = DbContext.Set <SalesDoc>().Include(x => x.Details).Where(x => x.Id == model.SalesDocId).FirstOrDefault(); var salesitems = new List <SalesDocDetailViewModel>(); foreach (SalesDocDetail i in sales.Details) { SalesDocDetailViewModel salesDocDetailViewModel = new SalesDocDetailViewModel(); PropertyCopier <SalesDocDetail, SalesDocDetailViewModel> .Copy(i, salesDocDetailViewModel); salesDocDetailViewModel.Id = i.Id; salesDocDetailViewModel.item = new Bateeq.Service.Pos.Lib.ViewModels.NewIntegrationViewModel.ItemViewModel { ArticleRealizationOrder = i.ItemArticleRealizationOrder, code = i.ItemCode, DomesticCOGS = i.ItemDomesticCOGS, DomesticRetail = i.ItemDomesticRetail, DomesticSale = i.ItemDomesticSale, DomesticWholeSale = i.ItemDomesticWholeSale, name = i.ItemName, Size = i.ItemSize, Uom = i.ItemUom, _id = i.ItemId }; salesDocDetailViewModel.discount1 = i.Discount1; salesDocDetailViewModel.discount2 = i.Discount2; salesDocDetailViewModel.discountNominal = i.DiscountNominal; salesDocDetailViewModel.itemCode = i.ItemCode; salesDocDetailViewModel.itemId = (int)i.ItemId; salesDocDetailViewModel.margin = i.Margin; salesDocDetailViewModel.price = i.Price; salesDocDetailViewModel.quantity = i.Quantity; salesDocDetailViewModel.specialDiscount = i.SpesialDiscount; salesDocDetailViewModel.total = i.Total; salesitems.Add(salesDocDetailViewModel); } viewModel.sales = new Bateeq.Service.Pos.Lib.ViewModels.SalesDoc.SalesDocViewModel { Active = sales.Active, date = sales.Date, discount = sales.Discount, grandTotal = sales.GrandTotal, Id = sales.Id, reference = sales.Reference, remark = sales.Remark, code = sales.Code, isReturn = sales.isReturn, isVoid = sales.isVoid, salesDetail = new SalesDetail { bank = new Bank { code = sales.BankCode, description = "", name = sales.BankName, _id = sales.BankId }, bankCard = new Bank { code = sales.BankCardCode, description = "", name = sales.BankCardName, _id = sales.BankCardId }, cardAmount = sales.CardAmount, cardName = sales.CardName, cardNumber = sales.CardNumber, cardType = new Card { code = sales.CardTypeCode, description = "", name = sales.CardTypeName, _id = sales.CardTypeId }, cashAmount = sales.CashAmount, paymentType = sales.PaymentType, voucher = new Voucher { value = sales.VoucherValue }, card = sales.Card }, store = new Store { Code = sales.StoreCode, Name = sales.StoreName, Id = sales.StoreId, Storage = new StorageViewModel { code = sales.StoreStorageCode, name = model.StoreStorageName, _id = model.StoreStorageId, } }, shift = sales.Shift, subTotal = sales.SubTotal, totalProduct = sales.TotalProduct, items = salesitems }; var salesreturn = DbContext.Set <SalesDoc>().Include(x => x.Details).Where(x => x.Id == model.SalesDocReturnId).FirstOrDefault(); var salesreturnitems = new List <SalesDocDetailViewModel>(); foreach (SalesDocDetail i in salesreturn.Details) { SalesDocDetailViewModel salesDocDetailViewModel = new SalesDocDetailViewModel(); PropertyCopier <SalesDocDetail, SalesDocDetailViewModel> .Copy(i, salesDocDetailViewModel); salesDocDetailViewModel.Id = i.Id; salesDocDetailViewModel.item = new Bateeq.Service.Pos.Lib.ViewModels.NewIntegrationViewModel.ItemViewModel { ArticleRealizationOrder = i.ItemArticleRealizationOrder, code = i.ItemCode, DomesticCOGS = i.ItemDomesticCOGS, DomesticRetail = i.ItemDomesticRetail, DomesticSale = i.ItemDomesticSale, DomesticWholeSale = i.ItemDomesticWholeSale, name = i.ItemName, Size = i.ItemSize, Uom = i.ItemUom, _id = i.ItemId }; salesDocDetailViewModel.discount1 = i.Discount1; salesDocDetailViewModel.discount2 = i.Discount2; salesDocDetailViewModel.discountNominal = i.DiscountNominal; salesDocDetailViewModel.itemCode = i.ItemCode; salesDocDetailViewModel.itemId = (int)i.ItemId; salesDocDetailViewModel.margin = i.Margin; salesDocDetailViewModel.price = i.Price; salesDocDetailViewModel.quantity = i.Quantity; salesDocDetailViewModel.specialDiscount = i.SpesialDiscount; salesDocDetailViewModel.total = i.Total; salesDocDetailViewModel.isReturn = i.isReturn; salesDocDetailViewModel.returnItems = new List <SalesDocDetailViewModel>(); if (i.isReturn) { var a = DbContext.SalesDocDetailReturnItems.Where(x => x.SalesDocDetailId == i.Id).ToList(); foreach (var retur in a) { SalesDocDetailViewModel salesDocDetailreturnitemViewModel = new SalesDocDetailViewModel(); PropertyCopier <SalesDocDetail, SalesDocDetailViewModel> .Copy(i, salesDocDetailreturnitemViewModel); salesDocDetailreturnitemViewModel.Id = retur.Id; salesDocDetailreturnitemViewModel.item = new Bateeq.Service.Pos.Lib.ViewModels.NewIntegrationViewModel.ItemViewModel { ArticleRealizationOrder = retur.ItemArticleRealizationOrder, code = retur.ItemCode, DomesticCOGS = retur.ItemDomesticCOGS, DomesticRetail = retur.ItemDomesticRetail, DomesticSale = retur.ItemDomesticSale, DomesticWholeSale = retur.ItemDomesticWholeSale, name = retur.ItemName, Size = retur.ItemSize, Uom = retur.ItemUom, _id = retur.ItemId }; salesDocDetailreturnitemViewModel.discount1 = retur.Discount1; salesDocDetailreturnitemViewModel.discount2 = retur.Discount2; salesDocDetailreturnitemViewModel.discountNominal = retur.DiscountNominal; salesDocDetailreturnitemViewModel.itemCode = retur.ItemCode; salesDocDetailreturnitemViewModel.itemId = (int)retur.ItemId; salesDocDetailreturnitemViewModel.margin = retur.Margin; salesDocDetailreturnitemViewModel.price = retur.Price; salesDocDetailreturnitemViewModel.quantity = retur.Quantity; salesDocDetailreturnitemViewModel.specialDiscount = retur.SpesialDiscount; salesDocDetailreturnitemViewModel.total = retur.Total; salesDocDetailViewModel.returnItems.Add(salesDocDetailreturnitemViewModel); } } salesreturnitems.Add(salesDocDetailViewModel); } viewModel.salesDocReturn = new SalesDocViewModel { Active = salesreturn.Active, date = salesreturn.Date, discount = salesreturn.Discount, grandTotal = salesreturn.GrandTotal, Id = salesreturn.Id, reference = salesreturn.Reference, remark = salesreturn.Remark, code = salesreturn.Code, isReturn = salesreturn.isReturn, isVoid = salesreturn.isVoid, salesDetail = new SalesDetail { bank = new Bank { code = salesreturn.BankCode, description = "", name = salesreturn.BankName, _id = salesreturn.BankId }, bankCard = new Bank { code = salesreturn.BankCardCode, description = "", name = salesreturn.BankCardName, _id = salesreturn.BankCardId }, cardAmount = salesreturn.CardAmount, cardName = salesreturn.CardName, cardNumber = salesreturn.CardNumber, cardType = new Card { code = salesreturn.CardTypeCode, description = "", name = salesreturn.CardTypeName, _id = salesreturn.CardTypeId }, cashAmount = salesreturn.CashAmount, paymentType = salesreturn.PaymentType, voucher = new Voucher { value = salesreturn.VoucherValue }, card = salesreturn.Card }, store = new Store { Code = salesreturn.StoreCode, Name = salesreturn.StoreName, Id = salesreturn.StoreId, Storage = new StorageViewModel { code = salesreturn.StoreStorageCode, name = salesreturn.StoreStorageName, _id = salesreturn.StoreStorageId, } }, shift = salesreturn.Shift, subTotal = salesreturn.SubTotal, totalProduct = salesreturn.TotalProduct, totalBayar = salesreturn.GrandTotal, totalDiscount = salesreturn.Discount, total = salesreturn.GrandTotal, items = salesreturnitems }; viewModel._CreatedAgent = model._CreatedAgent; viewModel._CreatedBy = model._CreatedBy; viewModel._CreatedUtc = model._CreatedUtc; viewModel._IsDeleted = model._IsDeleted; viewModel._LastModifiedAgent = model._LastModifiedAgent; viewModel._LastModifiedUtc = model._LastModifiedUtc; viewModel._LastModifiedBy = model._LastModifiedBy; var store = GetStore(salesreturn.StoreCode); viewModel.store = new Store { Id = salesreturn.StoreId, Code = salesreturn.StoreCode, Name = salesreturn.StoreName, Address = store.address, Phone = store.phone }; viewModel.items = new List <SalesDocReturnDetailViewModel>(); foreach (var i in model.Details) { SalesDocReturnDetailViewModel salesDocDetailViewModel = new SalesDocReturnDetailViewModel(); PropertyCopier <SalesDocReturnDetail, SalesDocReturnDetailViewModel> .Copy(i, salesDocDetailViewModel); salesDocDetailViewModel.Id = i.Id; salesDocDetailViewModel.Active = i.Active; salesDocDetailViewModel.discount1 = i.Discount1; salesDocDetailViewModel.discount2 = i.Discount2; salesDocDetailViewModel.discountNominal = i.DiscountNominal; salesDocDetailViewModel.isReturn = i.isReturn; salesDocDetailViewModel.itemCode = i.ItemCode; salesDocDetailViewModel.itemId = (int)i.ItemId; salesDocDetailViewModel.item = new SalesDocDetailViewModel { discount1 = i.Discount1, discount2 = i.Discount2, discountNominal = i.DiscountNominal, itemCode = i.ItemCode, itemId = (int)i.ItemId, margin = i.Margin, price = i.Price, quantity = i.Quantity, specialDiscount = i.SpesialDiscount, total = i.Total, item = new Bateeq.Service.Pos.Lib.ViewModels.NewIntegrationViewModel.ItemViewModel { ArticleRealizationOrder = i.ItemArticleRealizationOrder, code = i.ItemCode, DomesticCOGS = i.ItemDomesticCOGS, DomesticRetail = i.ItemDomesticRetail, DomesticSale = i.ItemDomesticSale, DomesticWholeSale = i.ItemDomesticWholeSale, name = i.ItemName, Size = i.ItemSize, Uom = i.ItemUom, _id = i.ItemId } }; salesDocDetailViewModel.margin = i.Margin; salesDocDetailViewModel.price = i.Price; salesDocDetailViewModel.quantity = i.Quantity; salesDocDetailViewModel.specialDiscount = i.SpesialDiscount; salesDocDetailViewModel.total = i.Total; salesDocDetailViewModel.returnItems = new List <SalesDocDetailViewModel>(); foreach (var d in salesreturn.Details) { SalesDocDetailViewModel salesDocDetailreturnViewModel = new SalesDocDetailViewModel(); PropertyCopier <SalesDocDetail, SalesDocDetailViewModel> .Copy(d, salesDocDetailreturnViewModel); salesDocDetailreturnViewModel.Id = d.Id; salesDocDetailreturnViewModel.item = new Bateeq.Service.Pos.Lib.ViewModels.NewIntegrationViewModel.ItemViewModel { ArticleRealizationOrder = d.ItemArticleRealizationOrder, code = d.ItemCode, DomesticCOGS = d.ItemDomesticCOGS, DomesticRetail = d.ItemDomesticRetail, DomesticSale = d.ItemDomesticSale, DomesticWholeSale = d.ItemDomesticWholeSale, name = d.ItemName, Size = d.ItemSize, Uom = d.ItemUom, _id = d.ItemId }; salesDocDetailreturnViewModel.discount1 = d.Discount1; salesDocDetailreturnViewModel.discount2 = d.Discount2; salesDocDetailreturnViewModel.discountNominal = d.DiscountNominal; salesDocDetailreturnViewModel.itemCode = d.ItemCode; salesDocDetailreturnViewModel.itemId = (int)d.ItemId; salesDocDetailreturnViewModel.margin = d.Margin; salesDocDetailreturnViewModel.price = d.Price; salesDocDetailreturnViewModel.quantity = d.Quantity; salesDocDetailreturnViewModel.specialDiscount = d.SpesialDiscount; salesDocDetailreturnViewModel.total = d.Total; salesDocDetailViewModel.returnItems.Add(salesDocDetailreturnViewModel); } viewModel.items.Add(salesDocDetailViewModel); } return(viewModel); }