public async Task <int> CreateForPOS(TransferOutDoc model2, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    EntityExtension.FlagForCreate(model2, username, USER_AGENT);
                    foreach (var i in model2.Items)
                    {
                        var inventorymovement = new InventoryMovement();
                        var inven             = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == model2.SourceId).FirstOrDefault();
                        if (inven != null)
                        {
                            inventorymovement.Before = inven.Quantity;
                            inven.Quantity           = inven.Quantity - i.Quantity;
                        }
                        inventorymovement.After                      = inventorymovement.Before - i.Quantity;
                        inventorymovement.Date                       = DateTimeOffset.UtcNow;
                        inventorymovement.ItemCode                   = i.ItemCode;
                        inventorymovement.ItemDomesticCOGS           = i.DomesticCOGS;
                        inventorymovement.ItemDomesticRetail         = i.DomesticRetail;
                        inventorymovement.ItemDomesticWholeSale      = i.DomesticWholeSale;
                        inventorymovement.ItemDomesticSale           = i.DomesticSale;
                        inventorymovement.ItemId                     = i.ItemId;
                        inventorymovement.ItemInternationalCOGS      = 0;
                        inventorymovement.ItemInternationalRetail    = 0;
                        inventorymovement.ItemInternationalSale      = 0;
                        inventorymovement.ItemInternationalWholeSale = 0;
                        inventorymovement.ItemName                   = i.ItemName;
                        inventorymovement.ItemSize                   = i.Size;
                        inventorymovement.ItemUom                    = i.Uom;
                        inventorymovement.Quantity                   = i.Quantity;
                        inventorymovement.StorageCode                = model2.SourceCode;
                        inventorymovement.StorageId                  = model2.SourceId;
                        inventorymovement.StorageName                = model2.SourceName;
                        inventorymovement.Type                       = "OUT";
                        inventorymovement.Reference                  = model2.Code;
                        inventorymovement.Remark                     = model2.Remark;
                        inventorymovement.StorageIsCentral           = model2.SourceName.Contains("GUDANG") ? true : false;
                        EntityExtension.FlagForCreate(inventorymovement, username, USER_AGENT);
                        dbSetInventoryMovement.Add(inventorymovement);

                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    dbSet.Add(model2);
                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }
            return(Created);
        }
示例#2
0
        public TransferOutDocViewModel MapToViewModel(TransferOutDoc transferOutDoc)
        {
            List <TransferOutDocItem> transferOutDocItems = new List <TransferOutDocItem>(transferOutDoc.Items);

            return(new TransferOutDocViewModel
            {
                code = transferOutDoc.Code,
                date = transferOutDoc.Date,
                destination = new Lib.ViewModels.NewIntegrationViewModel.DestinationViewModel
                {
                    code = transferOutDoc.DestinationCode,
                    name = transferOutDoc.DestinationName,
                    _id = transferOutDoc.DestinationId
                },
                remark = transferOutDoc.Remark,
                reference = transferOutDoc.Reference,
                source = new Lib.ViewModels.NewIntegrationViewModel.SourceViewModel
                {
                    code = transferOutDoc.SourceCode,
                    name = transferOutDoc.SourceName,
                    _id = transferOutDoc.SourceId
                },
                items = new List <TransferOutDocItemViewModel>
                {
                    new TransferOutDocItemViewModel
                    {
                        articleRealizationOrder = transferOutDocItems[0].ArticleRealizationOrder,
                        quantity = transferOutDocItems[0].Quantity,
                        remark = transferOutDocItems[0].Remark,
                        item = new Lib.ViewModels.NewIntegrationViewModel.ItemViewModel {
                            articleRealizationOrder = transferOutDocItems[0].ArticleRealizationOrder,
                            code = transferOutDocItems[0].ItemCode,
                            domesticCOGS = transferOutDocItems[0].DomesticCOGS,
                            domesticRetail = transferOutDocItems[0].DomesticRetail,
                            domesticSale = transferOutDocItems[0].DomesticSale,
                            domesticWholesale = transferOutDocItems[0].DomesticWholeSale,
                            name = transferOutDocItems[0].ItemName,
                            size = transferOutDocItems[0].Size,
                            uom = transferOutDocItems[0].Uom,
                            _id = transferOutDocItems[0].ItemId,
                        },
                    }
                }
            });
        }
示例#3
0
        public IActionResult GetExpeditionPDF(int id)
        {
            try
            {
                var indexAcceptPdf = Request.Headers["Accept"].ToList().IndexOf("application/pdf");

                TransferOutDoc          model     = facade.ReadById(id);
                TransferOutDocViewModel viewModel = mapper.Map <TransferOutDocViewModel>(model);
                if (viewModel == null)
                {
                    throw new Exception("Invalid Id");
                }
                if (indexAcceptPdf < 0)
                {
                    return(Ok(new
                    {
                        apiVersion = ApiVersion,
                        statusCode = General.OK_STATUS_CODE,
                        message = General.OK_MESSAGE,
                        data = viewModel,
                    }));
                }
                else
                {
                    int clientTimeZoneOffset = int.Parse(Request.Headers["x-timezone-offset"].First());
                    var Spk = pkpbjFacade.ReadByReference(viewModel.code);
                    PkbjByUserViewModel     viewModelSpk     = mapper.Map <PkbjByUserViewModel>(Spk);
                    ReturnToUnitPdfTemplate PdfTemplateLocal = new ReturnToUnitPdfTemplate();
                    MemoryStream            stream           = PdfTemplateLocal.GeneratePdfTemplate(viewModel, viewModelSpk, serviceProvider, clientTimeZoneOffset);

                    return(new FileStreamResult(stream, "application/pdf")
                    {
                        FileDownloadName = $"{viewModel.code}.pdf"
                    });
                }
            }
            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 <int> Create(TransferOutDocViewModel model, TransferOutDoc model2, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string codeOut = GenerateCode("EFR-KB/RTP");
                    model2.Code = codeOut;
                    model2.Date = DateTimeOffset.Now;
                    List <ExpeditionItem>   expeditionItems   = new List <ExpeditionItem>();
                    List <ExpeditionDetail> expeditionDetails = new List <ExpeditionDetail>();
                    List <SPKDocsItem>      sPKDocsItem       = new List <SPKDocsItem>();
                    EntityExtension.FlagForCreate(model2, username, USER_AGENT);
                    foreach (var i in model2.Items)
                    {
                        sPKDocsItem.Add(new SPKDocsItem
                        {
                            ItemArticleRealizationOrder = i.ArticleRealizationOrder,
                            ItemCode              = i.ItemCode,
                            ItemDomesticCOGS      = i.DomesticCOGS,
                            ItemDomesticRetail    = i.DomesticRetail,
                            ItemDomesticSale      = i.DomesticSale,
                            ItemDomesticWholesale = i.DomesticWholeSale,
                            ItemId       = i.ItemId,
                            ItemName     = i.ItemName,
                            ItemSize     = i.Size,
                            ItemUom      = i.Uom,
                            Quantity     = i.Quantity,
                            Remark       = i.Remark,
                            SendQuantity = i.Quantity
                        });
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }

                    dbSet.Add(model2);
                    //Created = await dbContext.SaveChangesAsync();

                    SPKDocs sPKDocs = new SPKDocs
                    {
                        Code            = GenerateCode("EFR-PK/PBJ"),
                        Date            = DateTimeOffset.Now,
                        IsDistributed   = true,
                        IsDraft         = false,
                        IsReceived      = false,
                        DestinationCode = model2.DestinationCode,
                        DestinationId   = model2.DestinationId,
                        DestinationName = model2.DestinationName,
                        PackingList     = GenerateCode("EFR-KB/PLR"),
                        Password        = String.Join("", GenerateCode(DateTime.Now.ToString("dd")).Split("/")),
                        Reference       = codeOut,
                        SourceCode      = model2.SourceCode,
                        SourceName      = model2.SourceName,
                        SourceId        = model2.SourceId,
                        Weight          = 0,
                        Items           = sPKDocsItem
                    };
                    EntityExtension.FlagForCreate(sPKDocs, username, USER_AGENT);
                    foreach (var i in sPKDocs.Items)
                    {
                        var inventorymovement = new InventoryMovement();
                        var inven             = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == model2.SourceId).FirstOrDefault();
                        if (inven != null)
                        {
                            inventorymovement.Before = inven.Quantity;
                            inven.Quantity           = inven.Quantity - i.Quantity;
                        }
                        inventorymovement.After                      = inventorymovement.Before + i.Quantity;
                        inventorymovement.Date                       = DateTimeOffset.UtcNow;
                        inventorymovement.ItemCode                   = i.ItemCode;
                        inventorymovement.ItemDomesticCOGS           = i.ItemDomesticCOGS;
                        inventorymovement.ItemDomesticRetail         = i.ItemDomesticRetail;
                        inventorymovement.ItemDomesticWholeSale      = i.ItemDomesticRetail;
                        inventorymovement.ItemDomesticSale           = i.ItemDomesticSale;
                        inventorymovement.ItemId                     = i.ItemId;
                        inventorymovement.ItemInternationalCOGS      = 0;
                        inventorymovement.ItemInternationalRetail    = 0;
                        inventorymovement.ItemInternationalSale      = 0;
                        inventorymovement.ItemInternationalWholeSale = 0;
                        inventorymovement.ItemName                   = i.ItemName;
                        inventorymovement.ItemSize                   = i.ItemSize;
                        inventorymovement.ItemUom                    = i.ItemUom;
                        inventorymovement.Quantity                   = i.Quantity;
                        inventorymovement.StorageCode                = model2.SourceCode;
                        inventorymovement.StorageId                  = model2.SourceId;
                        inventorymovement.StorageName                = model2.SourceName;
                        inventorymovement.Type                       = "OUT";
                        inventorymovement.Reference                  = codeOut;
                        inventorymovement.Remark                     = model2.Remark;
                        inventorymovement.StorageIsCentral           = model2.SourceName.Contains("GUDANG") ? true : false;
                        EntityExtension.FlagForCreate(inventorymovement, username, USER_AGENT);
                        dbSetInventoryMovement.Add(inventorymovement);

                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    dbSetSPKDocs.Add(sPKDocs);
                    Created = await dbContext.SaveChangesAsync();

                    foreach (var i in sPKDocs.Items)
                    {
                        expeditionDetails.Add(new ExpeditionDetail
                        {
                            ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                            DomesticCOGS            = i.ItemDomesticCOGS,
                            DomesticRetail          = i.ItemDomesticRetail,
                            DomesticSale            = i.ItemDomesticSale,
                            DomesticWholesale       = i.ItemDomesticWholesale,
                            ItemCode     = i.ItemCode,
                            ItemId       = i.ItemId,
                            ItemName     = i.ItemName,
                            Quantity     = i.Quantity,
                            Remark       = i.Remark,
                            SendQuantity = i.SendQuantity,
                            Uom          = i.ItemUom,
                            Size         = i.ItemSize,
                            //SPKDocsId = (int)dbContext.SPKDocs.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1
                            SPKDocsId = (int)sPKDocs.Id
                        });
                    }

                    expeditionItems.Add(new ExpeditionItem
                    {
                        Code            = sPKDocs.Code,
                        Date            = sPKDocs.Date,
                        DestinationCode = sPKDocs.DestinationCode,
                        DestinationId   = (int)sPKDocs.DestinationId,
                        DestinationName = sPKDocs.DestinationName,
                        IsDistributed   = sPKDocs.IsDistributed,
                        IsDraft         = sPKDocs.IsDraft,
                        IsReceived      = sPKDocs.IsReceived,
                        PackingList     = sPKDocs.PackingList,
                        Password        = sPKDocs.Password,
                        Reference       = sPKDocs.Reference,
                        SourceCode      = sPKDocs.SourceCode,
                        SourceId        = (int)sPKDocs.SourceId,
                        SourceName      = sPKDocs.SourceName,
                        //SPKDocsId = (int)dbContext.SPKDocs.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1,
                        SPKDocsId = (int)sPKDocs.Id,
                        Weight    = sPKDocs.Weight,
                        Details   = expeditionDetails
                    });

                    Expedition expedition = new Expedition
                    {
                        Code = GenerateCode("EFR-KB/EXP"),
                        Date = DateTimeOffset.Now,
                        ExpeditionServiceCode = model.expeditionService.code,
                        ExpeditionServiceId   = (int)model.expeditionService._id,
                        ExpeditionServiceName = model.expeditionService.name,
                        Remark = "",
                        Weight = 0,
                        Items  = expeditionItems,
                    };
                    EntityExtension.FlagForCreate(expedition, username, USER_AGENT);
                    foreach (var i in expeditionItems)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        foreach (var d in expeditionDetails)
                        {
                            EntityExtension.FlagForCreate(d, username, USER_AGENT);
                        }
                    }

                    dbSetExpedition.Add(expedition);
                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(Created);
        }
        public async Task <int> Create(Expedition model, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    int    totalweight = 0;
                    string code        = GenerateCode("EFR-KB/EXP");

                    model.Code = code;
                    model.Date = DateTimeOffset.Now;
                    TransferOutDoc transferOutDoc = new TransferOutDoc();
                    foreach (var i in model.Items)
                    {
                        i.Id         = 0;
                        totalweight += i.Weight;
                        string CodeTransferOut = GenerateCode("EFR-KB/EXP");
                        var    SPK             = dbContext.SPKDocs.Where(x => x.PackingList == i.PackingList).Single();
                        SPK.IsDistributed              = true;
                        transferOutDoc.Code            = CodeTransferOut;
                        transferOutDoc.Reference       = model.Code;
                        transferOutDoc.DestinationId   = i.DestinationId;
                        transferOutDoc.DestinationCode = i.DestinationCode;
                        transferOutDoc.DestinationName = i.DestinationName;
                        transferOutDoc.Remark          = model.Remark;
                        transferOutDoc.SourceId        = i.SourceId;
                        transferOutDoc.SourceCode      = i.SourceCode;
                        transferOutDoc.SourceName      = i.SourceName;
                        List <TransferOutDocItem> transferOutDocItems = new List <TransferOutDocItem>();
                        foreach (var d in i.Details)
                        {
                            d.Id = 0;
                            var inven = dbContext.Inventories.Where(x => x.ItemArticleRealizationOrder == d.ArticleRealizationOrder && x.ItemCode == d.ItemCode && x.ItemName == d.ItemName && x.StorageId == i.SourceId).Single();
                            //inven.Quantity = inven.Quantity - d.SendQuantity;

                            InventoryMovement movement = new InventoryMovement {
                                After  = inven.Quantity - d.SendQuantity,
                                Before = inven.Quantity,
                                Date   = DateTimeOffset.Now,
                                ItemArticleRealizationOrder = d.ArticleRealizationOrder,
                                ItemCode                   = d.ItemCode,
                                ItemDomesticCOGS           = d.DomesticCOGS,
                                ItemDomesticRetail         = d.DomesticRetail,
                                ItemDomesticSale           = d.DomesticSale,
                                ItemDomesticWholeSale      = d.DomesticWholesale,
                                ItemInternationalCOGS      = 0,
                                ItemInternationalRetail    = 0,
                                ItemInternationalSale      = 0,
                                ItemInternationalWholeSale = 0,
                                ItemId           = d.ItemId,
                                ItemName         = d.ItemName,
                                ItemSize         = d.Size,
                                Quantity         = d.Quantity,
                                Reference        = CodeTransferOut,
                                Remark           = d.Remark,
                                StorageCode      = i.SourceCode,
                                StorageIsCentral = i.SourceName.Contains("GUDANG") ? true : false,
                                StorageId        = i.SourceId,
                                StorageName      = i.DestinationName,
                                Type             = "OUT"
                            };

                            inven.Quantity = inven.Quantity - d.SendQuantity;
                            TransferOutDocItem transferItem = new TransferOutDocItem
                            {
                                ArticleRealizationOrder = d.ArticleRealizationOrder,
                                DomesticCOGS            = d.DomesticCOGS,
                                DomesticRetail          = d.DomesticRetail,
                                DomesticSale            = d.DomesticSale,
                                DomesticWholeSale       = d.DomesticWholesale,
                                ItemCode = d.ItemCode,
                                ItemId   = d.ItemId,
                                ItemName = d.ItemName,
                                Quantity = d.Quantity,
                                Remark   = d.Remark,
                                Size     = d.Size,
                                Uom      = d.Uom
                            };
                            EntityExtension.FlagForCreate(transferItem, username, USER_AGENT);
                            transferOutDocItems.Add(transferItem);
                            //transferOutDoc.Items.Add(transferItem);
                            //transferOutDoc.Items.Add(new TransferOutDocItem
                            //{
                            //    ArticleRealizationOrder = d.ArticleRealizationOrder
                            //    DomesticCOGS = d.DomesticCOGS,
                            //    DomesticRetail = d.DomesticRetail,
                            //    DomesticSale = d.DomesticSale,
                            //    DomesticWholeSale = d.DomesticWholesale,
                            //    ItemCode = d.ItemCode,
                            //    ItemId = d.ItemId,
                            //    ItemName = d.ItemName,
                            //    Quantity = d.Quantity,
                            //    Remark = d.Remark,
                            //    Size = d.Size,
                            //    Uom = d.Uom


                            //});
                            EntityExtension.FlagForCreate(d, username, USER_AGENT);
                            EntityExtension.FlagForCreate(movement, username, USER_AGENT);
                            this.dbSetInventoryMovement.Add(movement);
                        }
                        transferOutDoc.Items = transferOutDocItems;
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        EntityExtension.FlagForCreate(transferOutDoc, username, USER_AGENT);
                        this.dbSetTransfer.Add(transferOutDoc);
                    }
                    model.Weight = totalweight;
                    model.Remark = "";
                    EntityExtension.FlagForCreate(model, username, USER_AGENT);

                    dbSet.Add(model);
                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(Created);
        }
示例#6
0
        public async Task <int> Create(TransferOutDocViewModel model, TransferOutDoc model2, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string codeOut        = GenerateCode("EFR-KB/RTT");
                    string packingList1   = GenerateCode("EFR-KB/PLR");
                    string CodeIn         = GenerateCode("EFR-TB/BRT");
                    string packingList2   = GenerateCode("EFR-KB/PLB");
                    string expCode        = GenerateCode("EFR-KB/EXP");
                    string codetransferin = GenerateCode("EFR-TB/BRT");
                    model2.Code = codeOut;
                    model2.Date = DateTimeOffset.Now;
                    var storages          = GetStorage("GDG.05");
                    var expeditionService = GetExpedition("Dikirim Sendiri");
                    List <ExpeditionItem>     expeditionItems     = new List <ExpeditionItem>();
                    List <ExpeditionDetail>   expeditionDetails   = new List <ExpeditionDetail>();
                    List <SPKDocsItem>        sPKDocsItem1        = new List <SPKDocsItem>();
                    List <SPKDocsItem>        sPKDocsItem2        = new List <SPKDocsItem>();
                    List <TransferInDocItem>  transferInDocs      = new List <TransferInDocItem>();
                    List <InventoryMovement>  inventoryMovements  = new List <InventoryMovement>();
                    List <TransferOutDocItem> transferOutDocItems = new List <TransferOutDocItem>();
                    EntityExtension.FlagForCreate(model2, username, USER_AGENT);
                    foreach (var i in model2.Items)
                    {
                        var invenInTransfer = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == storages.Id).FirstOrDefault();
                        if (invenInTransfer == null)
                        {
                            Inventory inventory = new Inventory
                            {
                                ItemArticleRealizationOrder = i.ArticleRealizationOrder,
                                ItemCode              = i.ItemCode,
                                ItemDomesticCOGS      = i.DomesticCOGS,
                                ItemDomesticRetail    = i.DomesticRetail,
                                ItemDomesticSale      = i.DomesticSale,
                                ItemDomesticWholeSale = i.DomesticWholeSale,
                                ItemId = i.ItemId,
                                ItemInternationalCOGS      = 0,
                                ItemInternationalRetail    = 0,
                                ItemInternationalSale      = 0,
                                ItemInternationalWholeSale = 0,
                                ItemName         = i.ItemName,
                                ItemSize         = i.Size,
                                ItemUom          = i.Uom,
                                Quantity         = 0,
                                StorageCode      = storages.Code,
                                StorageId        = storages.Id,
                                StorageName      = storages.Name,
                                StorageIsCentral = storages.Name.Contains("GUDANG") ? true : false,
                            };
                            EntityExtension.FlagForCreate(inventory, username, USER_AGENT);
                            dbSetInventory.Add(inventory);
                            transferOutDocItems.Add(new TransferOutDocItem
                            {
                                ArticleRealizationOrder = i.ArticleRealizationOrder,
                                DomesticCOGS            = i.DomesticCOGS,
                                DomesticRetail          = i.DomesticRetail,
                                DomesticSale            = i.DomesticSale,
                                DomesticWholeSale       = i.DomesticWholeSale,
                                ItemCode = i.ItemCode,
                                ItemId   = i.ItemId,
                                ItemName = i.ItemName,
                                Quantity = i.Quantity,
                                Remark   = i.Remark,
                                Size     = i.Size,
                                Uom      = i.Uom
                            });
                        }
                        else
                        {
                            invenInTransfer.Quantity = invenInTransfer.Quantity - i.Quantity;
                        }
                        sPKDocsItem1.Add(new SPKDocsItem
                        {
                            ItemArticleRealizationOrder = i.ArticleRealizationOrder,
                            ItemCode              = i.ItemCode,
                            ItemDomesticCOGS      = i.DomesticCOGS,
                            ItemDomesticRetail    = i.DomesticRetail,
                            ItemDomesticSale      = i.DomesticSale,
                            ItemDomesticWholesale = i.DomesticWholeSale,
                            ItemId       = i.ItemId,
                            ItemName     = i.ItemName,
                            ItemSize     = i.Size,
                            ItemUom      = i.Uom,
                            Quantity     = i.Quantity,
                            Remark       = i.Remark,
                            SendQuantity = i.Quantity
                        });
                        sPKDocsItem2.Add(new SPKDocsItem
                        {
                            ItemArticleRealizationOrder = i.ArticleRealizationOrder,
                            ItemCode              = i.ItemCode,
                            ItemDomesticCOGS      = i.DomesticCOGS,
                            ItemDomesticRetail    = i.DomesticRetail,
                            ItemDomesticSale      = i.DomesticSale,
                            ItemDomesticWholesale = i.DomesticWholeSale,
                            ItemId       = i.ItemId,
                            ItemName     = i.ItemName,
                            ItemSize     = i.Size,
                            ItemUom      = i.Uom,
                            Quantity     = i.Quantity,
                            Remark       = i.Remark,
                            SendQuantity = i.Quantity
                        });
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    EntityExtension.FlagForCreate(model2, username, USER_AGENT);
                    foreach (var i in model2.Items)
                    {
                        //var inventorymovement = new InventoryMovement();

                        transferInDocs.Add(new TransferInDocItem
                        {
                            ArticleRealizationOrder = i.ArticleRealizationOrder,
                            ItemCode          = i.ItemCode,
                            DomesticCOGS      = i.DomesticCOGS,
                            DomesticRetail    = i.DomesticRetail,
                            DomesticSale      = i.DomesticSale,
                            DomesticWholeSale = i.DomesticWholeSale,
                            ItemId            = i.ItemId,
                            ItemName          = i.ItemName,
                            Size     = i.Size,
                            Uom      = i.Uom,
                            Quantity = i.Quantity,
                            Remark   = i.Remark
                        });
                    }
                    SPKDocs sPKDocs1 = new SPKDocs
                    {
                        Code            = GenerateCode("EFR-PK/PBJ"),
                        Date            = DateTimeOffset.Now,
                        SourceId        = model2.SourceId,
                        SourceCode      = model2.SourceCode,
                        SourceName      = model2.SourceName,
                        DestinationId   = storages.Id,
                        DestinationCode = storages.Code,
                        DestinationName = storages.Name,
                        IsDistributed   = true,
                        IsReceived      = true,
                        IsDraft         = false,
                        PackingList     = packingList1,
                        Reference       = codeOut,
                        Password        = String.Join("", GenerateCode(DateTime.Now.ToString("dd")).Split("/")),
                        Weight          = 0,
                        Items           = sPKDocsItem1
                    };
                    EntityExtension.FlagForCreate(sPKDocs1, username, USER_AGENT);

                    TransferInDoc transferInDoc = new TransferInDoc
                    {
                        Code            = codetransferin,
                        Date            = DateTimeOffset.Now,
                        DestinationId   = storages.Id,
                        DestinationCode = storages.Code,
                        DestinationName = storages.Name,
                        SourceCode      = model2.SourceCode,
                        SourceId        = model2.SourceId,
                        SourceName      = model2.SourceName,
                        Reference       = packingList1,
                        Remark          = "",
                        Items           = transferInDocs
                    };
                    EntityExtension.FlagForCreate(transferInDoc, username, USER_AGENT);

                    SPKDocs sPKDocs2 = new SPKDocs
                    {
                        Code            = GenerateCode("EFR-PK/PBJ"),
                        Date            = DateTimeOffset.Now,
                        DestinationId   = model2.DestinationId,
                        DestinationCode = model2.DestinationCode,
                        DestinationName = model2.DestinationName,
                        SourceId        = storages.Id,
                        SourceCode      = storages.Code,
                        SourceName      = storages.Name,
                        IsDistributed   = true,
                        IsReceived      = false,
                        IsDraft         = false,
                        PackingList     = packingList2,
                        Reference       = codeOut,
                        Password        = String.Join("", GenerateCode(DateTime.Now.ToString("dd")).Split("/")),
                        Weight          = 0,
                        Items           = sPKDocsItem2
                    };
                    EntityExtension.FlagForCreate(sPKDocs2, username, USER_AGENT);

                    foreach (var i in sPKDocs1.Items)
                    {
                        var QtySource      = 0.0;
                        var invenOutSource = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == model2.SourceId).FirstOrDefault();

                        if (invenOutSource != null)
                        {
                            QtySource = invenOutSource.Quantity;
                            invenOutSource.Quantity = invenOutSource.Quantity - i.Quantity;
                        }

                        inventoryMovements.Add(new InventoryMovement
                        {
                            Before                     = QtySource,
                            After                      = invenOutSource.Quantity,
                            Date                       = DateTimeOffset.Now,
                            ItemCode                   = i.ItemCode,
                            ItemDomesticCOGS           = i.ItemDomesticCOGS,
                            ItemDomesticRetail         = i.ItemDomesticRetail,
                            ItemDomesticWholeSale      = i.ItemDomesticRetail,
                            ItemDomesticSale           = i.ItemDomesticSale,
                            ItemId                     = i.ItemId,
                            ItemInternationalCOGS      = 0,
                            ItemInternationalRetail    = 0,
                            ItemInternationalSale      = 0,
                            ItemInternationalWholeSale = 0,
                            ItemName                   = i.ItemName,
                            ItemSize                   = i.ItemSize,
                            ItemUom                    = i.ItemUom,
                            Quantity                   = i.Quantity,
                            StorageCode                = model2.SourceCode,
                            StorageId                  = model2.SourceId,
                            StorageName                = model2.SourceName,
                            Type                       = "OUT",
                            Reference                  = codeOut,
                            Remark                     = model2.Remark,
                            StorageIsCentral           = model2.SourceName.Contains("GUDANG") ? true : false,
                        });
                        inventoryMovements.Add(new InventoryMovement
                        {
                            Before                     = 0,
                            After                      = 1,
                            Date                       = DateTimeOffset.Now,
                            ItemCode                   = i.ItemCode,
                            ItemDomesticCOGS           = i.ItemDomesticCOGS,
                            ItemDomesticRetail         = i.ItemDomesticRetail,
                            ItemDomesticWholeSale      = i.ItemDomesticRetail,
                            ItemDomesticSale           = i.ItemDomesticSale,
                            ItemId                     = i.ItemId,
                            ItemInternationalCOGS      = 0,
                            ItemInternationalRetail    = 0,
                            ItemInternationalSale      = 0,
                            ItemInternationalWholeSale = 0,
                            ItemName                   = i.ItemName,
                            ItemSize                   = i.ItemSize,
                            ItemUom                    = i.ItemUom,
                            Quantity                   = i.Quantity,
                            StorageCode                = storages.Code,
                            StorageId                  = storages.Id,
                            StorageName                = storages.Name,
                            Type                       = "IN",
                            Reference                  = codetransferin,
                            Remark                     = model2.Remark,
                            StorageIsCentral           = storages.Name.Contains("GUDANG") ? true : false,
                        });
                        inventoryMovements.Add(new InventoryMovement
                        {
                            Before                     = 1,
                            After                      = 0,
                            Date                       = DateTimeOffset.Now,
                            ItemCode                   = i.ItemCode,
                            ItemDomesticCOGS           = i.ItemDomesticCOGS,
                            ItemDomesticRetail         = i.ItemDomesticRetail,
                            ItemDomesticWholeSale      = i.ItemDomesticRetail,
                            ItemDomesticSale           = i.ItemDomesticSale,
                            ItemId                     = i.ItemId,
                            ItemInternationalCOGS      = 0,
                            ItemInternationalRetail    = 0,
                            ItemInternationalSale      = 0,
                            ItemInternationalWholeSale = 0,
                            ItemName                   = i.ItemName,
                            ItemSize                   = i.ItemSize,
                            ItemUom                    = i.ItemUom,
                            Quantity                   = i.Quantity,
                            StorageCode                = model2.DestinationCode,
                            StorageId                  = model2.DestinationId,
                            StorageName                = model2.DestinationName,
                            Type                       = "OUT",
                            Reference                  = expCode,
                            Remark                     = model2.Remark,
                            StorageIsCentral           = model2.DestinationName.Contains("GUDANG") ? true : false,
                        });
                        expeditionDetails.Add(new ExpeditionDetail
                        {
                            ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                            DomesticCOGS            = i.ItemDomesticCOGS,
                            DomesticRetail          = i.ItemDomesticRetail,
                            DomesticSale            = i.ItemDomesticSale,
                            DomesticWholesale       = i.ItemDomesticWholesale,
                            ItemCode     = i.ItemCode,
                            ItemId       = i.ItemId,
                            ItemName     = i.ItemName,
                            Quantity     = i.Quantity,
                            Remark       = i.Remark,
                            SendQuantity = i.SendQuantity,
                            Uom          = i.ItemUom,
                            Size         = i.ItemSize,
                            //SPKDocsId = (int)dbContext.SPKDocs.OrderByDescending(x=>x.Id).FirstOrDefault().Id + 1
                            SPKDocsId = (int)sPKDocs1.Id
                        });
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    expeditionItems.Add(new ExpeditionItem
                    {
                        Code            = sPKDocs2.Code,
                        Date            = sPKDocs2.Date,
                        DestinationCode = sPKDocs2.DestinationCode,
                        DestinationId   = (int)sPKDocs2.DestinationId,
                        DestinationName = sPKDocs2.DestinationName,
                        IsDistributed   = sPKDocs2.IsDistributed,
                        IsDraft         = sPKDocs2.IsDraft,
                        IsReceived      = sPKDocs2.IsReceived,
                        PackingList     = sPKDocs2.PackingList,
                        Password        = sPKDocs2.Password,
                        Reference       = codeOut,
                        SourceCode      = sPKDocs2.SourceCode,
                        SourceId        = (int)sPKDocs2.SourceId,
                        SourceName      = sPKDocs2.SourceName,
                        //SPKDocsId = (int)dbContext.SPKDocs.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1,
                        SPKDocsId = (int)sPKDocs2.Id,
                        Weight    = sPKDocs2.Weight,
                        Details   = expeditionDetails
                    });

                    Expedition expedition = new Expedition
                    {
                        Code = expCode,
                        Date = DateTimeOffset.Now,
                        ExpeditionServiceCode = expeditionService.code,
                        ExpeditionServiceId   = (int)expeditionService._id,
                        ExpeditionServiceName = expeditionService.name,
                        Remark = "",
                        Weight = 0,
                        Items  = expeditionItems,
                    };
                    EntityExtension.FlagForCreate(expedition, username, USER_AGENT);

                    TransferOutDoc transferOutDoc2 = new TransferOutDoc
                    {
                        Code            = expCode,
                        Date            = DateTimeOffset.Now,
                        DestinationCode = model2.DestinationCode,
                        DestinationId   = model2.DestinationId,
                        DestinationName = model2.DestinationName,
                        Reference       = packingList2,
                        Remark          = "",
                        SourceId        = storages.Id,
                        SourceCode      = storages.Code,
                        SourceName      = storages.Name,
                        Items           = transferOutDocItems
                    };
                    EntityExtension.FlagForCreate(transferOutDoc2, username, USER_AGENT);
                    #region Saving
                    foreach (var i in transferOutDoc2.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    foreach (var i in sPKDocs2.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    foreach (var i in expedition.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        foreach (var d in i.Details)
                        {
                            EntityExtension.FlagForCreate(d, username, USER_AGENT);
                        }
                    }
                    foreach (var i in transferInDoc.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    foreach (var i in inventoryMovements)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        dbSetInventoryMovement.Add(i);
                    }
                    dbSetExpedition.Add(expedition);
                    dbSetSpk.Add(sPKDocs1);
                    dbSetSpk.Add(sPKDocs2);
                    dbSet.Add(model2);
                    dbSet.Add(transferOutDoc2);
                    dbSetTransferIn.Add(transferInDoc);

                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();

                    #endregion
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }

                return(Created);
            }
        }
示例#7
0
        public async Task <int> Process(SODocs viewModel, string username, int clientTimeZoneOffset = 7)
        {
            int Process = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    var model = dbContext.StockOpnameDocs.Where(x => x.Id == viewModel.Id).FirstOrDefault();
                    model.IsProcessed = true;

                    string inventoryMovementIn  = GenerateCode("EFR-TB/SO");
                    string inventoryMovementOut = GenerateCode("EFR-KB/SO");

                    List <TransferInDocItem>  transferInDocsItems  = new List <TransferInDocItem>();
                    List <InventoryMovement>  inventoryMovements   = new List <InventoryMovement>();
                    List <TransferOutDocItem> transferOutDocsItems = new List <TransferOutDocItem>();

                    EntityExtension.FlagForUpdate(model, username, USER_AGENT);
                    foreach (var i in viewModel.Items)
                    {
                        var inventoriesAvailable = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == viewModel.StorageId).FirstOrDefault();
                        var sourceQty            = inventoriesAvailable.Quantity;

                        if (i.IsAdjusted == true)
                        {
                            var items = dbContext.StockOpnameDocsItems.Where(x => x.Id == i.Id && x.SODocsId == viewModel.Id).FirstOrDefault();
                            items.IsAdjusted = true;
                            EntityExtension.FlagForUpdate(items, username, USER_AGENT);

                            if (i.QtyBeforeSO < i.QtySO)
                            {
                                transferInDocsItems.Add(new TransferInDocItem
                                {
                                    ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    DomesticCOGS            = i.ItemDomesticCOGS,
                                    DomesticRetail          = i.ItemDomesticRetail,
                                    DomesticSale            = i.ItemDomesticSale,
                                    DomesticWholeSale       = i.ItemDomesticWholeSale,
                                    ItemCode = i.ItemCode,
                                    ItemId   = i.ItemId,
                                    ItemName = i.ItemName,
                                    Quantity = i.QtySO - i.QtyBeforeSO,
                                    Remark   = i.Remark,
                                    Size     = i.ItemSize,
                                    Uom      = i.ItemUom
                                });

                                inventoryMovements.Add(new InventoryMovement
                                {
                                    Before = sourceQty,
                                    After  = sourceQty + (i.QtySO - i.QtyBeforeSO),
                                    Date   = DateTimeOffset.Now,
                                    ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    ItemCode              = i.ItemCode,
                                    ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                    ItemDomesticRetail    = i.ItemDomesticRetail,
                                    ItemDomesticWholeSale = i.ItemDomesticRetail,
                                    ItemDomesticSale      = i.ItemDomesticSale,
                                    ItemId = i.ItemId,
                                    ItemInternationalCOGS      = i.ItemInternationalCOGS,
                                    ItemInternationalRetail    = i.ItemInternationalRetail,
                                    ItemInternationalSale      = i.ItemInternationalSale,
                                    ItemInternationalWholeSale = i.ItemInternationalWholeSale,
                                    ItemName         = i.ItemName,
                                    ItemSize         = i.ItemSize,
                                    ItemUom          = i.ItemUom,
                                    Quantity         = i.QtySO - i.QtyBeforeSO,
                                    StorageCode      = viewModel.StorageCode,
                                    StorageId        = viewModel.StorageId,
                                    StorageName      = viewModel.StorageName,
                                    Type             = "IN",
                                    Reference        = inventoryMovementIn,
                                    Remark           = i.Remark,
                                    StorageIsCentral = viewModel.StorageName.Contains("GUDANG") ? true : false,
                                });

                                inventoriesAvailable.Quantity += (i.QtySO - i.QtyBeforeSO);
                                EntityExtension.FlagForUpdate(inventoriesAvailable, username, USER_AGENT);
                                EntityExtension.FlagForCreate(i, username, USER_AGENT);
                            }

                            else
                            {
                                transferOutDocsItems.Add(new TransferOutDocItem
                                {
                                    ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    DomesticCOGS            = i.ItemDomesticCOGS,
                                    DomesticRetail          = i.ItemDomesticRetail,
                                    DomesticSale            = i.ItemDomesticSale,
                                    DomesticWholeSale       = i.ItemDomesticWholeSale,
                                    ItemCode = i.ItemCode,
                                    ItemId   = i.ItemId,
                                    ItemName = i.ItemName,
                                    Quantity = i.QtyBeforeSO - i.QtySO,
                                    Remark   = i.Remark,
                                    Size     = i.ItemSize,
                                    Uom      = i.ItemUom
                                });

                                inventoryMovements.Add(new InventoryMovement
                                {
                                    Before = sourceQty,
                                    After  = sourceQty - (i.QtyBeforeSO - i.QtySO),
                                    Date   = DateTimeOffset.Now,
                                    ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    ItemCode              = i.ItemCode,
                                    ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                    ItemDomesticRetail    = i.ItemDomesticRetail,
                                    ItemDomesticWholeSale = i.ItemDomesticRetail,
                                    ItemDomesticSale      = i.ItemDomesticSale,
                                    ItemId = i.ItemId,
                                    ItemInternationalCOGS      = i.ItemInternationalCOGS,
                                    ItemInternationalRetail    = i.ItemInternationalRetail,
                                    ItemInternationalSale      = i.ItemInternationalSale,
                                    ItemInternationalWholeSale = i.ItemInternationalWholeSale,
                                    ItemName         = i.ItemName,
                                    ItemSize         = i.ItemSize,
                                    ItemUom          = i.ItemUom,
                                    Quantity         = i.QtyBeforeSO - i.QtySO,
                                    StorageCode      = viewModel.StorageCode,
                                    StorageId        = viewModel.StorageId,
                                    StorageName      = viewModel.StorageName,
                                    Type             = "OUT",
                                    Reference        = inventoryMovementOut,
                                    Remark           = i.Remark,
                                    StorageIsCentral = viewModel.StorageName.Contains("GUDANG") ? true : false,
                                });

                                inventoriesAvailable.Quantity -= (i.QtyBeforeSO - i.QtySO);
                                EntityExtension.FlagForUpdate(inventoriesAvailable, username, USER_AGENT);
                                EntityExtension.FlagForCreate(i, username, USER_AGENT);
                            }
                        }
                    }

                    if (transferInDocsItems.Count > 0)
                    {
                        TransferInDoc transferInDoc = new TransferInDoc
                        {
                            Code            = inventoryMovementIn,
                            Date            = DateTimeOffset.Now,
                            DestinationId   = viewModel.StorageId,
                            DestinationCode = viewModel.StorageCode,
                            DestinationName = viewModel.StorageName,
                            SourceId        = viewModel.StorageId,
                            SourceCode      = viewModel.StorageCode,
                            SourceName      = viewModel.StorageName,
                            Reference       = viewModel.Code,
                            Remark          = "",
                            Items           = transferInDocsItems
                        };
                        EntityExtension.FlagForCreate(transferInDoc, username, USER_AGENT);

                        foreach (var tii in transferInDoc.Items)
                        {
                            EntityExtension.FlagForCreate(tii, username, USER_AGENT);
                        }

                        dbSetTransferIn.Add(transferInDoc);
                    }

                    if (transferOutDocsItems.Count > 0)
                    {
                        TransferOutDoc transferOutDoc = new TransferOutDoc
                        {
                            Code            = inventoryMovementOut,
                            Date            = DateTimeOffset.Now,
                            DestinationId   = viewModel.StorageId,
                            DestinationCode = viewModel.StorageCode,
                            DestinationName = viewModel.StorageName,
                            SourceId        = viewModel.StorageId,
                            SourceCode      = viewModel.StorageCode,
                            SourceName      = viewModel.StorageName,
                            Reference       = viewModel.Code,
                            Remark          = "",
                            Items           = transferOutDocsItems
                        };
                        EntityExtension.FlagForCreate(transferOutDoc, username, USER_AGENT);

                        foreach (var toi in transferOutDoc.Items)
                        {
                            EntityExtension.FlagForCreate(toi, username, USER_AGENT);
                        }

                        dbSetTransferOut.Add(transferOutDoc);
                    }

                    foreach (var im in inventoryMovements)
                    {
                        EntityExtension.FlagForCreate(im, username, USER_AGENT);
                        dbSetInventoryMovement.Add(im);
                    }

                    Process = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }

                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }

                return(Process);
            }
        }
        public async Task <int> Create(AdjustmentDocs model, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string code     = GenerateCode("EFR-ADJ/INT");
                    var    storages = GetStorage(model.StorageCode);

                    string inventoryMovementIn  = GenerateCode("EFR-TB/ADJ");
                    string inventoryMovementOut = GenerateCode("EFR-KB/ADJ");

                    List <TransferInDocItem>  transferInDocsItems  = new List <TransferInDocItem>();
                    List <InventoryMovement>  inventoryMovements   = new List <InventoryMovement>();
                    List <TransferOutDocItem> transferOutDocsItems = new List <TransferOutDocItem>();

                    model.Code = code;

                    EntityExtension.FlagForCreate(model, username, USER_AGENT);
                    foreach (var i in model.Items)
                    {
                        if (i.Type == "IN")
                        {
                            var SourceQuantity = 0.0;

                            var inventoriesAvailable = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == storages.Id).FirstOrDefault();
                            if (inventoriesAvailable == null)
                            {
                                Inventory inventory = new Inventory
                                {
                                    ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    ItemCode              = i.ItemCode,
                                    ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                    ItemDomesticRetail    = i.ItemDomesticRetail,
                                    ItemDomesticSale      = i.ItemDomesticSale,
                                    ItemDomesticWholeSale = i.ItemDomesticWholeSale,
                                    ItemId = i.ItemId,
                                    ItemInternationalCOGS      = 0,
                                    ItemInternationalRetail    = 0,
                                    ItemInternationalSale      = 0,
                                    ItemInternationalWholeSale = 0,
                                    ItemName         = i.ItemName,
                                    ItemSize         = i.ItemSize,
                                    ItemUom          = i.ItemUom,
                                    Quantity         = i.QtyAdjustment,
                                    StorageCode      = model.StorageCode,
                                    StorageId        = model.StorageId,
                                    StorageName      = model.StorageName,
                                    StorageIsCentral = model.StorageName.Contains("GUDANG") ? true : false
                                };
                                EntityExtension.FlagForCreate(inventory, username, USER_AGENT);
                                dbSetInventory.Add(inventory);
                            }
                            else
                            {
                                SourceQuantity = inventoriesAvailable.Quantity;
                                inventoriesAvailable.Quantity = inventoriesAvailable.Quantity + i.QtyAdjustment;
                                EntityExtension.FlagForUpdate(inventoriesAvailable, username, USER_AGENT);
                            }

                            i.QtyBeforeAdjustment = SourceQuantity;

                            transferInDocsItems.Add(new TransferInDocItem
                            {
                                ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                DomesticCOGS            = i.ItemDomesticCOGS,
                                DomesticRetail          = i.ItemDomesticRetail,
                                DomesticSale            = i.ItemDomesticSale,
                                DomesticWholeSale       = i.ItemDomesticWholeSale,
                                ItemCode = i.ItemCode,
                                ItemId   = i.ItemId,
                                ItemName = i.ItemName,
                                Quantity = i.QtyAdjustment,
                                Remark   = i.Remark,
                                Size     = i.ItemSize,
                                Uom      = i.ItemUom
                            });
                            EntityExtension.FlagForCreate(i, username, USER_AGENT);

                            inventoryMovements.Add(new InventoryMovement
                            {
                                Before = SourceQuantity,
                                After  = SourceQuantity + i.QtyAdjustment,
                                Date   = DateTimeOffset.Now,
                                ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                ItemCode              = i.ItemCode,
                                ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                ItemDomesticRetail    = i.ItemDomesticRetail,
                                ItemDomesticWholeSale = i.ItemDomesticRetail,
                                ItemDomesticSale      = i.ItemDomesticSale,
                                ItemId = i.ItemId,
                                ItemInternationalCOGS      = i.ItemInternationalCOGS,
                                ItemInternationalRetail    = i.ItemInternationalRetail,
                                ItemInternationalSale      = i.ItemInternationalSale,
                                ItemInternationalWholeSale = i.ItemInternationalWholeSale,
                                ItemName         = i.ItemName,
                                ItemSize         = i.ItemSize,
                                ItemUom          = i.ItemUom,
                                Quantity         = i.QtyAdjustment,
                                StorageCode      = model.StorageCode,
                                StorageId        = model.StorageId,
                                StorageName      = model.StorageName,
                                Type             = "IN",
                                Reference        = inventoryMovementIn,
                                Remark           = i.Remark,
                                StorageIsCentral = model.StorageName.Contains("GUDANG") ? true : false
                            });
                        }
                        else
                        {
                            var SourceQuantity = 0.0;

                            var inventoriesAvailable = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == storages.Id).FirstOrDefault();
                            if (inventoriesAvailable != null)
                            {
                                SourceQuantity = inventoriesAvailable.Quantity;
                                inventoriesAvailable.Quantity = inventoriesAvailable.Quantity - i.QtyAdjustment;
                                EntityExtension.FlagForUpdate(inventoriesAvailable, username, USER_AGENT);
                            }
                            else
                            {
                                Inventory inventory = new Inventory
                                {
                                    ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                    ItemCode              = i.ItemCode,
                                    ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                    ItemDomesticRetail    = i.ItemDomesticRetail,
                                    ItemDomesticSale      = i.ItemDomesticSale,
                                    ItemDomesticWholeSale = i.ItemDomesticWholeSale,
                                    ItemId = i.ItemId,
                                    ItemInternationalCOGS      = 0,
                                    ItemInternationalRetail    = 0,
                                    ItemInternationalSale      = 0,
                                    ItemInternationalWholeSale = 0,
                                    ItemName         = i.ItemName,
                                    ItemSize         = i.ItemSize,
                                    ItemUom          = i.ItemUom,
                                    Quantity         = i.QtyAdjustment,
                                    StorageCode      = model.StorageCode,
                                    StorageId        = model.StorageId,
                                    StorageName      = model.StorageName,
                                    StorageIsCentral = model.StorageName.Contains("GUDANG") ? true : false
                                };
                                EntityExtension.FlagForCreate(inventory, username, USER_AGENT);
                                dbSetInventory.Add(inventory);
                            }

                            i.QtyBeforeAdjustment = SourceQuantity;

                            transferOutDocsItems.Add(new TransferOutDocItem
                            {
                                ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                DomesticCOGS            = i.ItemDomesticCOGS,
                                DomesticRetail          = i.ItemDomesticRetail,
                                DomesticSale            = i.ItemDomesticSale,
                                DomesticWholeSale       = i.ItemDomesticWholeSale,
                                ItemCode = i.ItemCode,
                                ItemId   = i.ItemId,
                                ItemName = i.ItemName,
                                Quantity = i.QtyAdjustment,
                                Remark   = i.Remark,
                                Size     = i.ItemSize,
                                Uom      = i.ItemUom
                            });
                            EntityExtension.FlagForCreate(i, username, USER_AGENT);

                            inventoryMovements.Add(new InventoryMovement
                            {
                                Before = SourceQuantity,
                                After  = SourceQuantity - i.QtyAdjustment,
                                Date   = DateTimeOffset.Now,
                                ItemArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                ItemCode              = i.ItemCode,
                                ItemDomesticCOGS      = i.ItemDomesticCOGS,
                                ItemDomesticRetail    = i.ItemDomesticRetail,
                                ItemDomesticWholeSale = i.ItemDomesticRetail,
                                ItemDomesticSale      = i.ItemDomesticSale,
                                ItemId = i.ItemId,
                                ItemInternationalCOGS      = i.ItemInternationalCOGS,
                                ItemInternationalRetail    = i.ItemInternationalRetail,
                                ItemInternationalSale      = i.ItemInternationalSale,
                                ItemInternationalWholeSale = i.ItemInternationalWholeSale,
                                ItemName         = i.ItemName,
                                ItemSize         = i.ItemSize,
                                ItemUom          = i.ItemUom,
                                Quantity         = i.QtyAdjustment,
                                StorageCode      = model.StorageCode,
                                StorageId        = model.StorageId,
                                StorageName      = model.StorageName,
                                Type             = "OUT",
                                Reference        = inventoryMovementOut,
                                Remark           = i.Remark,
                                StorageIsCentral = model.StorageName.Contains("GUDANG") ? true : false,
                            });
                        }
                    }

                    if (transferInDocsItems.Count > 0)
                    {
                        TransferInDoc transferInDoc = new TransferInDoc
                        {
                            Code            = inventoryMovementIn,
                            Date            = DateTimeOffset.Now,
                            DestinationId   = model.StorageId,
                            DestinationCode = model.StorageCode,
                            DestinationName = model.StorageName,
                            SourceId        = model.StorageId,
                            SourceCode      = model.StorageCode,
                            SourceName      = model.StorageName,
                            Reference       = code,
                            Remark          = "",
                            Items           = transferInDocsItems
                        };
                        EntityExtension.FlagForCreate(transferInDoc, username, USER_AGENT);

                        foreach (var tii in transferInDoc.Items)
                        {
                            EntityExtension.FlagForCreate(tii, username, USER_AGENT);
                        }

                        dbSetTransferIn.Add(transferInDoc);
                    }

                    if (transferOutDocsItems.Count > 0)
                    {
                        TransferOutDoc transferOutDoc = new TransferOutDoc
                        {
                            Code            = inventoryMovementOut,
                            Date            = DateTimeOffset.Now,
                            DestinationId   = model.StorageId,
                            DestinationCode = model.StorageCode,
                            DestinationName = model.StorageName,
                            SourceId        = model.StorageId,
                            SourceCode      = model.StorageCode,
                            SourceName      = model.StorageName,
                            Reference       = code,
                            Remark          = "",
                            Items           = transferOutDocsItems
                        };
                        EntityExtension.FlagForCreate(transferOutDoc, username, USER_AGENT);

                        foreach (var toi in transferOutDoc.Items)
                        {
                            EntityExtension.FlagForCreate(toi, username, USER_AGENT);
                        }

                        dbSetTransferOut.Add(transferOutDoc);
                    }

                    foreach (var im in inventoryMovements)
                    {
                        EntityExtension.FlagForCreate(im, username, USER_AGENT);
                        dbSetInventoryMovement.Add(im);
                    }

                    dbSetAdjustment.Add(model);
                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }

                return(Created);
            }
        }
        public async Task <int> CreateForPOS(TransferOutDoc model2, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    EntityExtension.FlagForCreate(model2, username, USER_AGENT);
                    foreach (var i in model2.Items)
                    {
                        var inventorymovement = new InventoryMovement();
                        var inven             = dbContext.Inventories.Where(x => x.ItemCode == i.ItemCode && x.StorageId == model2.SourceId).FirstOrDefault();
                        if (inven != null)
                        {
                            inventorymovement.Before = inven.Quantity;
                            inven.Quantity           = inven.Quantity - i.Quantity;
                        }
                        inventorymovement.After                      = inventorymovement.Before - i.Quantity;
                        inventorymovement.Date                       = DateTimeOffset.UtcNow;
                        inventorymovement.ItemCode                   = i.ItemCode;
                        inventorymovement.ItemDomesticCOGS           = i.DomesticCOGS;
                        inventorymovement.ItemDomesticRetail         = i.DomesticRetail;
                        inventorymovement.ItemDomesticWholeSale      = i.DomesticWholeSale;
                        inventorymovement.ItemDomesticSale           = i.DomesticSale;
                        inventorymovement.ItemId                     = i.ItemId;
                        inventorymovement.ItemInternationalCOGS      = 0;
                        inventorymovement.ItemInternationalRetail    = 0;
                        inventorymovement.ItemInternationalSale      = 0;
                        inventorymovement.ItemInternationalWholeSale = 0;
                        inventorymovement.ItemName                   = i.ItemName;
                        inventorymovement.ItemSize                   = i.Size;
                        inventorymovement.ItemUom                    = i.Uom;
                        inventorymovement.Quantity                   = i.Quantity;
                        inventorymovement.StorageCode                = model2.SourceCode;
                        inventorymovement.StorageId                  = model2.SourceId;
                        inventorymovement.StorageName                = model2.SourceName;
                        inventorymovement.Type                       = "OUT";
                        inventorymovement.Reference                  = model2.Code;
                        inventorymovement.Remark                     = model2.Remark;
                        inventorymovement.StorageIsCentral           = model2.SourceName.Contains("GUDANG") ? true : false;
                        EntityExtension.FlagForCreate(inventorymovement, username, USER_AGENT);
                        dbSetInventoryMovement.Add(inventorymovement);

                        EntityExtension.FlagForCreate(i, username, USER_AGENT);

                        //update TotalQty di tabel Items
                        var existItemId = (int)i.ItemId;
                        ItemCoreViewModelUsername itemCore = new ItemCoreViewModelUsername
                        {
                            _id      = existItemId,
                            Username = username,
                            Token    = "Bearer ",
                            TotalQty = i.Quantity
                        };

                        string             itemPutUri = $"items/finished-goods/reduce-qty-by-id/{i.ItemId}";
                        IHttpClientService httpClient = (IHttpClientService)serviceProvider.GetService(typeof(IHttpClientService));
                        var response = await httpClient.PutAsync($"{APIEndpoint.Core}{itemPutUri}", new StringContent(JsonConvert.SerializeObject(itemCore).ToString(), Encoding.UTF8, General.JsonMediaType));

                        response.EnsureSuccessStatusCode();
                    }

                    dbSet.Add(model2);
                    Created = await dbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }
            return(Created);
        }