Ejemplo n.º 1
0
        public async Task InitDataAsync()
        {
            var warehouse = new List <Warehouse>()
            {
                new Warehouse()
                {
                    Product = new Product
                    {
                        Name  = "小米笔记本Pro",
                        Price = 6599
                    },
                    StoreNum = 10
                },
                new Warehouse()
                {
                    Product = new Product
                    {
                        Name  = "三星S8",
                        Price = 4599
                    },
                    StoreNum = 8
                }
            };
            await _dbContext.AddRangeAsync(warehouse);

            await _dbContext.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task <string> Create(Product product)
        {
            await _context.Products.AddAsync(product);

            await _context.SaveChangesAsync();

            return("Successfully created a product");
        }
Ejemplo n.º 3
0
        public async Task <Inventory> GetTestData()
        {
            var data = GetNewData();

            dbSetInventory.Add(data);
            await dbContext.SaveChangesAsync();

            return(data);
        }
Ejemplo n.º 4
0
 public async Task UploadData(SODocs data, string username)
 {
     foreach (var i in data.Items)
     {
         EntityExtension.FlagForCreate(i, username, USER_AGENT);
     }
     EntityExtension.FlagForCreate(data, username, USER_AGENT);
     dbSetSO.Add(data);
     var result = await dbContext.SaveChangesAsync();
 }
Ejemplo n.º 5
0
        public async Task <IActionResult> InCreate([Bind("ItemId,Name,ShortDescription,LongDescription,Price,ImageUrl,ImageThumbnailUrl,FeaturedItem,Amount,InStock,Category")] Inventory inventory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(inventory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(inventory));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("FName,LName,CustomerNumber,Phone,BAddress,SAddress,Email,Balance")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
        public async Task <CommandResult> Handle(CancelOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var order = await _ordersRepository.GetById(request.Id);

                var item = await _itemsRepository.GetById(order.ItemId);

                order.Cancel(item);
                await _warehouseDbContext.SaveChangesAsync();

                await _warrantyService.DeleteWarranty(order.OrderItemUid);

                return(new CommandResult
                {
                    IsCompleted = true
                });
            }
            catch (Exception ex)
            {
                return(new CommandResult
                {
                    IsCompleted = false,
                    Reason = ex.Message
                });
            }
        }
Ejemplo n.º 8
0
        public async Task <CommandResult> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var item  = _itemsRepository.Query().GetBySizeAndModel(request.Model, request.Size).FirstOrDefault();
                var order = Order.Create(item);
                await _ordersRepository.Add(order);

                await _warehouseDbContext.SaveChangesAsync();

                await _warrantyService.StartWarranty(order.OrderItemUid);

                return(new CommandResult
                {
                    IsCompleted = true
                });
            }
            catch (Exception ex)
            {
                return(new CommandResult
                {
                    IsCompleted = false,
                    Reason = ex.Message
                });
            }
        }
Ejemplo n.º 9
0
        public async Task <string> CreateBasket(Basket basket)
        {
            await _context.Baskets.AddAsync(basket);

            await _context.SaveChangesAsync();

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

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string code = GenerateCode("EFR-TB/BBT");
                    model.Code = code;
                    var SPK = dbContext.SPKDocs.Where(x => x.PackingList == model.Reference).Single();
                    SPK.IsReceived = true;
                    var Id = SPK.Id;
                    EntityExtension.FlagForCreate(model, username, USER_AGENT);
                    foreach (var i in model.Items)
                    {
                        i.Id = 0;
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        var SPKItems = dbContext.SPKDocsItems.Where(x => x.ItemArticleRealizationOrder == i.ArticleRealizationOrder && x.ItemCode == i.ItemCode && i.ItemName == i.ItemName && x.SPKDocsId == Id).Single();
                        SPKItems.SendQuantity = i.Quantity;

                        var inven = dbContext.Inventories.Where(x => x.ItemCode == i.ItemCode).FirstOrDefault();
                        if (inven != null)
                        {
                            inven.StorageCode = model.DestinationCode;
                            inven.StorageId   = model.DestinationId;
                            inven.StorageName = model.DestinationName;
                        }
                    }

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

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

            return(Created);
        }
Ejemplo n.º 11
0
        public async Task <int> Create(TransferInDoc model, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string code = GenerateCode("MM-TB/BBP");
                    model.Code = code;
                    var SPK = dbContext.SPKDocs.Where(x => x.PackingList == model.Reference).Single();
                    SPK.IsReceived = true;
                    var Id = SPK.Id;
                    EntityExtension.FlagForCreate(model, username, USER_AGENT);
                    foreach (var i in model.Items)
                    {
                        i.Id = 0;
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        var SPKItems = dbContext.SPKDocsItems.Where(x => x.ItemArticleRealizationOrder == i.ArticleRealizationOrder && x.ItemCode == i.ItemCode && i.ItemName == i.ItemName && x.SPKDocsId == Id).Single();
                        SPKItems.SendQuantity = i.Quantity;
                        var inventorymovement = new InventoryMovement();
                        var inven             = dbContext.Inventories.Where(x => x.ItemId == i.ItemId && x.StorageId == model.DestinationId).FirstOrDefault();
                        if (inven != null)
                        {
                            inventorymovement.Before = inven.Quantity;
                            inven.Quantity           = inven.Quantity + i.Quantity;//inven.Quantity + i.quantity;
                            //dbSetInventory.Update(inven);
                        }
                        else
                        {
                            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         = i.Quantity,
                                StorageCode      = model.DestinationCode,
                                StorageId        = model.DestinationId,
                                StorageName      = model.DestinationName,
                                StorageIsCentral = model.DestinationName.Contains("GUDANG") ? true : false,
                            };
                            EntityExtension.FlagForCreate(inventory, username, USER_AGENT);
                            dbSetInventory.Add(inventory);
                        }

                        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.DomesticRetail;
                        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                = model.DestinationCode;
                        inventorymovement.StorageId                  = model.DestinationId;
                        inventorymovement.StorageName                = model.DestinationName;
                        inventorymovement.Type                       = "IN";
                        inventorymovement.Reference                  = code;
                        inventorymovement.Remark                     = model.Remark;
                        inventorymovement.StorageIsCentral           = model.DestinationName.Contains("GUDANG") ? true : false;
                        EntityExtension.FlagForCreate(inventorymovement, username, USER_AGENT);
                        dbSetInventoryMovement.Add(inventorymovement);
                    }

                    dbSet.Add(model);
                    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);
        }
Ejemplo n.º 13
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);
            }
        }
        public async Task <int> Create(SPKDocsFromFinihsingOutsViewModel viewModel, string username, string token)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    List <SPKDocsItem> sPKDocsItems = new List <SPKDocsItem>();

                    foreach (var item in viewModel.Items)
                    {
                        var isDifferentSize = item.IsDifferentSize;
                        if (isDifferentSize == true)
                        {
                            foreach (var detail in item.Details)
                            {
                                var sizeId       = detail.Size.Id.ToString("00");
                                var counterId    = viewModel.counters._id.ToString("00");
                                var subCounterId = viewModel.subCounters._id.ToString("00");
                                var asal         = viewModel.SourceId.ToString("0");
                                var roCreatedUtc = viewModel.RoCreatedUtc;
                                var materialId   = viewModel.materials._id.ToString("00");

                                var barcode = asal + counterId + subCounterId + materialId + sizeId + roCreatedUtc;
                                Console.WriteLine("barcodefad " + barcode);
                                var itemx = GetItem(barcode);

                                if (itemx == null || itemx.Count() == 0) //barcode belum terdaftar, insert ke tabel items (BMS) terlebih dahulu
                                {
                                    ItemCoreViewModelUsername itemCore = new ItemCoreViewModelUsername
                                    {
                                        dataDestination = new List <ItemViewModelRead>
                                        {
                                            new ItemViewModelRead
                                            {
                                                ArticleRealizationOrder = viewModel.RONo,
                                                code        = barcode,
                                                name        = viewModel.Comodity.name,
                                                Size        = item.Size.Size,
                                                Uom         = item.Uom.Unit,
                                                ImagePath   = viewModel.ImagePath,
                                                ImgFile     = "",
                                                Tags        = "",
                                                Remark      = "",
                                                Description = "",
                                                _id         = 0
                                            }
                                        },
                                        color                  = viewModel.color,
                                        process                = viewModel.process,
                                        materials              = viewModel.materials,
                                        materialCompositions   = viewModel.materialCompositions,
                                        collections            = viewModel.collections,
                                        seasons                = viewModel.seasons,
                                        counters               = viewModel.counters,
                                        subCounters            = viewModel.subCounters,
                                        categories             = viewModel.categories,
                                        DomesticCOGS           = item.BasicPrice,
                                        DomesticRetail         = 0,
                                        DomesticSale           = item.BasicPrice + item.ComodityPrice,
                                        DomesticWholesale      = 0,
                                        InternationalCOGS      = 0,
                                        InternationalWholesale = 0,
                                        InternationalRetail    = 0,
                                        InternationalSale      = 0,
                                        ImageFile              = "",
                                        _id      = 0,
                                        Username = username,
                                        Token    = token,
                                        TotalQty = detail.Quantity
                                    };

                                    string itemsUri   = "items/finished-goods/item";
                                    var    httpClient = (IHttpClientService)serviceProvider.GetService(typeof(IHttpClientService));
                                    var    response   = await httpClient.PostAsync($"{APIEndpoint.Core}{itemsUri}", new StringContent(JsonConvert.SerializeObject(itemCore).ToString(), Encoding.UTF8, General.JsonMediaType));

                                    response.EnsureSuccessStatusCode();

                                    var item2 = GetItem2(barcode);

                                    sPKDocsItems.Add(new SPKDocsItem
                                    {
                                        ItemArticleRealizationOrder = viewModel.RONo,
                                        ItemCode         = barcode,
                                        ItemDomesticCOGS = item.BasicPrice,
                                        ItemDomesticSale = item.BasicPrice + item.ComodityPrice,
                                        ItemId           = item2.FirstOrDefault()._id,
                                        ItemName         = viewModel.Comodity.name,
                                        ItemSize         = detail.Size.Size,
                                        ItemUom          = item.Uom.Unit,
                                        Quantity         = detail.Quantity,
                                        Remark           = "",
                                        SendQuantity     = detail.Quantity,
                                    });
                                }
                                else // barcode sudah terdaftar
                                {
                                    var existItemId = itemx.FirstOrDefault()._id;
                                    ItemCoreViewModelUsername itemCore = new ItemCoreViewModelUsername
                                    {
                                        _id      = existItemId,
                                        Username = username,
                                        Token    = token,
                                        TotalQty = detail.Quantity
                                    };

                                    sPKDocsItems.Add(new SPKDocsItem
                                    {
                                        ItemArticleRealizationOrder = viewModel.RONo,
                                        ItemCode         = barcode,
                                        ItemDomesticCOGS = item.BasicPrice,
                                        ItemDomesticSale = item.BasicPrice + item.ComodityPrice,
                                        ItemId           = existItemId,
                                        ItemName         = viewModel.Comodity.name,
                                        ItemSize         = detail.Size.Size,
                                        ItemUom          = item.Uom.Unit,
                                        Quantity         = detail.Quantity,
                                        Remark           = "",
                                        SendQuantity     = detail.Quantity,
                                    });

                                    //update TotalQty di tabel Items
                                    string             itemPutUri = $"items/finished-goods/update-qty-by-id/{existItemId}";
                                    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));

                                    if (response != null)
                                    {
                                        response.EnsureSuccessStatusCode();
                                    }
                                }
                            }
                        }
                        else
                        {
                            var sizeId       = item.Size.Id.ToString("00");
                            var counterId    = viewModel.counters._id.ToString("00");
                            var subCounterId = viewModel.subCounters._id.ToString("00");
                            var asal         = viewModel.SourceId.ToString("0");
                            var roCreatedUtc = viewModel.RoCreatedUtc;
                            var materialId   = viewModel.materials._id.ToString("00");

                            var barcode = asal + counterId + subCounterId + materialId + sizeId + roCreatedUtc;
                            Console.WriteLine("barcodefad " + barcode);
                            var itemx = GetItem(barcode);

                            if (itemx == null || itemx.Count() == 0) //barcode belum terdaftar, insert ke tabel items (BMS) terlebih dahulu
                            {
                                ItemCoreViewModelUsername itemCore = new ItemCoreViewModelUsername
                                {
                                    dataDestination = new List <ItemViewModelRead>
                                    {
                                        new ItemViewModelRead
                                        {
                                            ArticleRealizationOrder = viewModel.RONo,
                                            code        = barcode,
                                            name        = viewModel.Comodity.name,
                                            Size        = item.Size.Size,
                                            Uom         = item.Uom.Unit,
                                            ImagePath   = viewModel.ImagePath,
                                            ImgFile     = "",
                                            Tags        = "",
                                            Remark      = "",
                                            Description = "",
                                            _id         = 0
                                        }
                                    },
                                    color                  = viewModel.color,
                                    process                = viewModel.process,
                                    materials              = viewModel.materials,
                                    materialCompositions   = viewModel.materialCompositions,
                                    collections            = viewModel.collections,
                                    seasons                = viewModel.seasons,
                                    counters               = viewModel.counters,
                                    subCounters            = viewModel.subCounters,
                                    categories             = viewModel.categories,
                                    DomesticCOGS           = item.BasicPrice,
                                    DomesticRetail         = 0,
                                    DomesticSale           = item.BasicPrice + item.ComodityPrice,
                                    DomesticWholesale      = 0,
                                    InternationalCOGS      = 0,
                                    InternationalWholesale = 0,
                                    InternationalRetail    = 0,
                                    InternationalSale      = 0,
                                    ImageFile              = "",
                                    _id      = 0,
                                    Username = username,
                                    Token    = token,
                                    TotalQty = item.Quantity
                                };

                                string itemsUri   = "items/finished-goods/item";
                                var    httpClient = (IHttpClientService)serviceProvider.GetService(typeof(IHttpClientService));
                                var    response   = await httpClient.PostAsync($"{APIEndpoint.Core}{itemsUri}", new StringContent(JsonConvert.SerializeObject(itemCore).ToString(), Encoding.UTF8, General.JsonMediaType));

                                response.EnsureSuccessStatusCode();

                                var item2 = GetItem2(barcode);

                                sPKDocsItems.Add(new SPKDocsItem
                                {
                                    ItemArticleRealizationOrder = viewModel.RONo,
                                    ItemCode         = barcode,
                                    ItemDomesticCOGS = item.BasicPrice,
                                    ItemDomesticSale = item.BasicPrice + item.ComodityPrice,
                                    ItemId           = item2.FirstOrDefault()._id,
                                    ItemName         = viewModel.Comodity.name,
                                    ItemSize         = item.Size.Size,
                                    ItemUom          = item.Uom.Unit,
                                    Quantity         = item.Quantity,
                                    Remark           = "",
                                    SendQuantity     = item.Quantity,
                                });
                            }
                            else // barcode sudah terdaftar
                            {
                                var existItemId = itemx.FirstOrDefault()._id;
                                ItemCoreViewModelUsername itemCore = new ItemCoreViewModelUsername
                                {
                                    _id      = existItemId,
                                    Username = username,
                                    Token    = token,
                                    TotalQty = item.Quantity
                                };

                                sPKDocsItems.Add(new SPKDocsItem
                                {
                                    ItemArticleRealizationOrder = viewModel.RONo,
                                    ItemCode         = barcode,
                                    ItemDomesticCOGS = item.BasicPrice,
                                    ItemDomesticSale = item.BasicPrice + item.ComodityPrice,
                                    ItemId           = existItemId,
                                    ItemName         = viewModel.Comodity.name,
                                    ItemSize         = item.Size.Size,
                                    ItemUom          = item.Uom.Unit,
                                    Quantity         = item.Quantity,
                                    Remark           = "",
                                    SendQuantity     = item.Quantity,
                                });

                                //update TotalQty di tabel Items
                                string             itemPutUri = $"items/finished-goods/update-qty-by-id/{existItemId}";
                                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));

                                if (response != null)
                                {
                                    response.EnsureSuccessStatusCode();
                                }
                            }
                        }
                    }

                    var packingListCode = GeneratePackingList();

                    SPKDocs data = new SPKDocs()
                    {
                        Code                 = GenerateCode("EFR-PK/PBJ"),
                        Date                 = viewModel.FinishingOutDate,
                        DestinationId        = (long)viewModel.DestinationStorageId,
                        DestinationCode      = viewModel.DestinationStorageCode,
                        DestinationName      = viewModel.DestinationStorageName,
                        IsDistributed        = false,
                        IsReceived           = false,
                        PackingList          = packingListCode,
                        Password             = "******",
                        Reference            = packingListCode,
                        SourceId             = (long)viewModel.SourceStorageId,
                        SourceCode           = viewModel.SourceStorageCode,
                        SourceName           = viewModel.SourceStorageName,
                        Weight               = 0,
                        FinishingOutIdentity = viewModel.FinishingOutIdentity,
                        Items                = sPKDocsItems
                    };

                    foreach (var i in data.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                    }
                    EntityExtension.FlagForCreate(data, username, USER_AGENT);
                    dbContext.Add(data);

                    Created = await dbContext.SaveChangesAsync();

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

            return(Created);
        }
        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> 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(SPKDocs model, string username, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    EntityExtension.FlagForCreate(model, username, USER_AGENT);

                    string packingList = GenerateCode("EFR-KB/PLB");
                    string code        = GenerateCode("EFR-PK/PBJ");
                    string password    = String.Join("", GenerateCode(DateTime.Now.ToString("dd")).Split("/"));
                    //(generateCode(("0" + date.getDate()).slice(-2))).split('/').join('')

                    foreach (var i in model.Items)
                    {
                        EntityExtension.FlagForCreate(i, username, USER_AGENT);
                        var inven = GetItems(i.ItemCode, i.ItemName, i.ItemArticleRealizationOrder);
                        if (inven == null)
                        {
                            ItemCoreViewModel item = new ItemCoreViewModel
                            {
                                dataDestination = new List <ItemViewModelRead> {
                                    new ItemViewModelRead {
                                        ArticleRealizationOrder = i.ItemArticleRealizationOrder,
                                        code   = i.ItemCode,
                                        name   = i.ItemName,
                                        Remark = i.Remark,
                                        Size   = i.ItemSize,
                                        Uom    = i.ItemUom,
                                    }
                                },
                                DomesticCOGS      = i.ItemDomesticCOGS,
                                DomesticRetail    = i.ItemDomesticRetail,
                                DomesticSale      = i.ItemDomesticSale,
                                DomesticWholesale = i.ItemDomesticWholesale
                            };

                            string itemsUri   = "items/finished-goods";
                            var    httpClient = (IHttpClientService)serviceProvider.GetService(typeof(IHttpClientService));
                            var    response   = await httpClient.PostAsync($"{APIEndpoint.Core}{itemsUri}", new StringContent(JsonConvert.SerializeObject(item).ToString(), Encoding.UTF8, General.JsonMediaType));

                            response.EnsureSuccessStatusCode();
                        }
                    }
                    model.Code          = code;
                    model.Date          = new DateTimeOffset(model.CreatedUtc);
                    model.PackingList   = packingList;
                    model.Password      = password;
                    model.IsReceived    = false;
                    model.IsDraft       = false;
                    model.IsDistributed = false;

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

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

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

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    string code = GenerateCode("EFR-TB/BBP");
                    model.Code = code;


                    var SPK        = dbContext.SPKDocs.Where(x => x.PackingList == model.Reference).FirstOrDefault();
                    var expedition = dbContext.ExpeditionItems.Where(x => x.PackingList == model.Reference);
                    if (expedition.Count() != 0)
                    {
                        expedition.Single().IsReceived = true;
                    }
                    SPK.IsReceived = true;
                    var Id = SPK.Id;
                    EntityExtension.FlagForCreate(model, username, USER_AGENT);

                    var newItems = new List <TransferInDocItem>();

                    foreach (var i in model.Items)
                    {
                        var SPKItems = dbContext.SPKDocsItems.Where(x => x.ItemArticleRealizationOrder == i.ArticleRealizationOrder && x.ItemCode == i.ItemCode && i.ItemName == i.ItemName && x.SPKDocsId == Id).Single();
                        SPKItems.SendQuantity = i.Quantity;
                        var inventorymovement = new InventoryMovement();

                        int status = 0;
                        var inven  = dbContext.Inventories.OrderByDescending(x => x.CreatedUtc).Where(x => x.ItemId == i.ItemId && x.ItemCode.Contains(i.ItemCode)).FirstOrDefault();
                        if (inven != null)
                        {
                            var latestItemCode       = inven.ItemCode;
                            var latestItemCodeLength = latestItemCode.Length;
                            var latestStatus         = latestItemCode.Substring(latestItemCodeLength - 2);
                            status = int.Parse(latestStatus);
                        }
                        var countLoop = i.Quantity;
                        var itemcode  = i.ItemCode;
                        for (var j = 0; j < countLoop; j++)
                        {
                            status = status + 1;

                            i.Id       = 0;
                            i.Quantity = 1;
                            i.ItemCode = "" + itemcode + status.ToString("00");

                            TransferInDocItem transferInDocItem = new TransferInDocItem
                            {
                                ArticleRealizationOrder = i.ArticleRealizationOrder,
                                DomesticCOGS            = i.DomesticCOGS,
                                DomesticRetail          = i.DomesticRetail,
                                DomesticSale            = i.DomesticSale,
                                DomesticWholeSale       = i.DomesticWholeSale,
                                ItemCode       = "" + itemcode + status.ToString("00"),
                                ItemId         = i.ItemId,
                                ItemName       = i.ItemName,
                                Quantity       = 1,
                                Remark         = i.Remark,
                                Size           = i.Size,
                                TransferDocsId = i.TransferDocsId,
                                TransferInDocs = i.TransferInDocs,
                                Uom            = i.Uom,
                                Id             = 0
                            };

                            EntityExtension.FlagForCreate(transferInDocItem, username, USER_AGENT);
                            newItems.Add(transferInDocItem);

                            Inventory inventory = new Inventory
                            {
                                ItemArticleRealizationOrder = i.ArticleRealizationOrder,
                                ItemCode              = "" + itemcode + status.ToString("00"),
                                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         = 1,
                                StorageCode      = model.DestinationCode,
                                StorageId        = model.DestinationId,
                                StorageName      = model.DestinationName,
                                StorageIsCentral = model.DestinationName.Contains("GUDANG") ? true : false,
                            };
                            EntityExtension.FlagForCreate(inventory, username, USER_AGENT);
                            dbSetInventory.Add(inventory);

                            inventorymovement.After                      = inventorymovement.Before + 1;
                            inventorymovement.Date                       = DateTimeOffset.UtcNow;
                            inventorymovement.ItemCode                   = "" + itemcode + status.ToString("00");
                            inventorymovement.ItemDomesticCOGS           = i.DomesticCOGS;
                            inventorymovement.ItemDomesticRetail         = i.DomesticRetail;
                            inventorymovement.ItemDomesticWholeSale      = i.DomesticRetail;
                            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                   = 1;
                            inventorymovement.StorageCode                = model.DestinationCode;
                            inventorymovement.StorageId                  = model.DestinationId;
                            inventorymovement.StorageName                = model.DestinationName;
                            inventorymovement.Type                       = "IN";
                            inventorymovement.Reference                  = code;
                            inventorymovement.Remark                     = model.Remark;
                            inventorymovement.StorageIsCentral           = model.DestinationName.Contains("GUDANG") ? true : false;
                            EntityExtension.FlagForCreate(inventorymovement, username, USER_AGENT);
                            dbSetInventoryMovement.Add(inventorymovement);
                        }
                    }

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

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

            return(Created);
        }