public IActionResult GetVoidbyCode(string code, string storecode)
        {
            try
            {
                SalesDoc model = Service.ReadModelByCode(code, storecode);

                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 <SalesDoc, SalesDocViewModel>(model, Service.MaptoViewModel);
                    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] SalesDocViewModel viewModel)
        {
            try
            {
                VerifyUser();
                ValidateService.Validate(viewModel);

                SalesDoc model = Service.MapToModel(viewModel);
                var      id    = await Service.Create(model);

                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.CREATED_STATUS_CODE, General.OK_MESSAGE)
                    .Ok(id.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));
            }
        }
        void RemoveTemporaryLink(SalesDoc document)
        {
            var temporaryLink = document?.TemporaryPaymentLink;

            if (temporaryLink != null)
            {
                temporaryLink.cmdDelete.Execute();
                document.TemporaryPaymentLink = null;
            }
        }
        void AddTemporaryPaymentLink(SalesDoc document, CustomerPayment payment)
        {
            var link = new SalesDocPaymentLink(payment)
            {
                Document = document
            };

            link.PaymentAmount            = document.Balance;
            document.TemporaryPaymentLink = link;
        }
        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 SalesDocViewModel MaptoViewModel(SalesDoc model)
        {
            SalesDocViewModel viewModel = new SalesDocViewModel();

            PropertyCopier <SalesDoc, SalesDocViewModel> .Copy(model, viewModel);

            var salesdoc = DbContext.Set <SalesDoc>().Where(x => x.Id == model.Id).FirstOrDefault();

            viewModel.Active      = model.Active;
            viewModel.date        = model.Date;
            viewModel.discount    = model.Discount;
            viewModel.grandTotal  = model.GrandTotal;
            viewModel.Id          = model.Id;
            viewModel.reference   = model.Reference;
            viewModel.remark      = model.Remark;
            viewModel.code        = model.Code;
            viewModel.isReturn    = model.isReturn;
            viewModel.isVoid      = model.isVoid;
            viewModel.salesDetail = new SalesDetail
            {
                bank = new Bank
                {
                    code        = model.BankCode,
                    description = "",
                    name        = model.BankName,
                    _id         = model.BankId
                },
                bankCard = new Bank
                {
                    code        = model.BankCardCode,
                    description = "",
                    name        = model.BankCardName,
                    _id         = model.BankCardId
                },
                cardAmount = model.CardAmount,
                cardName   = model.CardName,
                cardNumber = model.CardNumber,
                cardType   = new Card
                {
                    code        = model.CardTypeCode,
                    description = "",
                    name        = model.CardTypeName,
                    _id         = model.CardTypeId
                },
                cashAmount  = model.CashAmount,
                paymentType = model.PaymentType,
                voucher     = new Voucher
                {
                    value = model.VoucherValue
                },
                card = model.Card
            };
            var Store = GetStore(salesdoc.StoreCode);

            viewModel.store = new Store
            {
                Code          = model.StoreCode,
                Name          = model.StoreName,
                Id            = model.StoreId,
                Address       = Store.address,
                Phone         = Store.phone,
                StoreCategory = model.StoreCategory,

                Storage = new StorageViewModel
                {
                    code = model.StoreStorageCode,
                    name = model.StoreStorageName,
                    _id  = model.StoreStorageId,
                }
            };
            viewModel.shift         = model.Shift;
            viewModel.subTotal      = model.SubTotal;
            viewModel.totalProduct  = model.TotalProduct;
            viewModel.grandTotal    = model.GrandTotal;
            viewModel.totalDiscount = model.Discount;

            viewModel.Id    = model.Id;
            viewModel.items = new List <SalesDocDetailViewModel>();
            foreach (SalesDocDetail i in model.Details)
            {
                SalesDocDetailViewModel salesDocDetailViewModel = new SalesDocDetailViewModel();
                PropertyCopier <SalesDocDetail, SalesDocDetailViewModel> .Copy(i, salesDocDetailViewModel);

                salesDocDetailViewModel.Id   = i.Id;
                salesDocDetailViewModel.item = new 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;


                viewModel.items.Add(salesDocDetailViewModel);
            }
            return(viewModel);
        }
        public SalesDoc MapToModel(SalesDocViewModel viewModel)
        {
            SalesDoc model = new SalesDoc();

            PropertyCopier <SalesDocViewModel, SalesDoc> .Copy(viewModel, model);

            if (viewModel.salesDetail.bankCard != null)
            {
                model.BankCardCode = viewModel.salesDetail.bankCard.code;
                model.BankCardId   = viewModel.salesDetail.bankCard._id;
                model.BankCardName = viewModel.salesDetail.bankCard.name;
            }
            if (viewModel.salesDetail.bank != null)
            {
                model.BankCode = viewModel.salesDetail.bank.code;
                model.BankId   = viewModel.salesDetail.bank._id;
                model.BankName = viewModel.salesDetail.bank.name;
            }

            model.CardAmount = viewModel.salesDetail.cardAmount;
            model.CardName   = viewModel.salesDetail.cardName;
            model.CardNumber = viewModel.salesDetail.cardNumber;
            if (viewModel.salesDetail.cardType != null)
            {
                model.CardTypeCode = viewModel.salesDetail.cardType.code;
                model.CardTypeId   = viewModel.salesDetail.cardType._id;
                model.CardTypeName = viewModel.salesDetail.cardType.name;
            }
            model.CashAmount       = viewModel.salesDetail.cashAmount;
            model.Card             = viewModel.salesDetail.card;
            model.Date             = viewModel.date;
            model.Discount         = viewModel.discount;
            model.GrandTotal       = viewModel.grandTotal;
            model.isReturn         = false;
            model.isVoid           = false;
            model.PaymentType      = viewModel.salesDetail.paymentType;
            model.Reference        = viewModel.reference;
            model.Remark           = viewModel.remark;
            model.Shift            = viewModel.shift;
            model.StoreCode        = viewModel.store.Code;
            model.StoreId          = viewModel.store.Id;
            model.StoreName        = viewModel.store.Name;
            model.StoreCategory    = viewModel.store.StoreCategory;
            model.StoreStorageCode = viewModel.store.Storage.code;
            model.StoreStorageId   = viewModel.store.Storage._id;
            model.StoreStorageName = viewModel.store.Storage.name;
            model.SubTotal         = viewModel.subTotal;
            model.TotalProduct     = viewModel.totalProduct;
            model.VoucherValue     = viewModel.salesDetail.voucher.value;

            model.Details = new List <SalesDocDetail>();

            foreach (SalesDocDetailViewModel i in viewModel.items)
            {
                SalesDocDetail salesDocDetail = new SalesDocDetail();
                PropertyCopier <SalesDocDetailViewModel, SalesDocDetail> .Copy(i, salesDocDetail);

                salesDocDetail.Discount1                   = i.discount1;
                salesDocDetail.Discount2                   = i.discount2;
                salesDocDetail.DiscountNominal             = i.discountNominal;
                salesDocDetail.isReturn                    = false;
                salesDocDetail.ItemArticleRealizationOrder = i.item.ArticleRealizationOrder;
                salesDocDetail.ItemCode                    = i.item.code;
                salesDocDetail.ItemDomesticCOGS            = i.item.DomesticCOGS;
                salesDocDetail.ItemDomesticRetail          = i.item.DomesticRetail;
                salesDocDetail.ItemDomesticSale            = i.item.DomesticSale;
                salesDocDetail.ItemDomesticWholeSale       = i.item.DomesticWholeSale;
                salesDocDetail.ItemId          = i.item._id;
                salesDocDetail.ItemName        = i.item.name;
                salesDocDetail.ItemSize        = i.item.Size;
                salesDocDetail.ItemUom         = i.item.Uom;
                salesDocDetail.Margin          = i.margin;
                salesDocDetail.Price           = i.price;
                salesDocDetail.Quantity        = i.quantity;
                salesDocDetail.Size            = i.item.Size;
                salesDocDetail.SpesialDiscount = i.specialDiscount;
                salesDocDetail.Total           = i.total;


                model.Details.Add(salesDocDetail);
            }
            return(model);
        }
Пример #8
0
        //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);
        }