예제 #1
0
        public async Task Should_Success_Update_Data_Type_Transfer_null_Summary()
        {
            var dbContext    = _dbContext(GetCurrentMethod());
            var facade       = new GarmentUnitExpenditureNoteFacade(GetServiceProvider(), dbContext);
            var dataUtil     = this.dataUtil(facade, GetCurrentMethod());
            var dataTransfer = await dataUtil.GetTestDataWithStorageReqeust();

            var newData2 = new GarmentUnitExpenditureNote
            {
                Id    = dataTransfer.Id,
                Items = new List <GarmentUnitExpenditureNoteItem>
                {
                    new GarmentUnitExpenditureNoteItem
                    {
                        Id = dataTransfer.Items.First().Id
                    }
                }
            };

            foreach (var item in dataTransfer.Items)
            {
                item.Quantity = 1;
            }

            var ResponseUpdate2 = await facade.Update((int)dataTransfer.Id, dataTransfer);

            Assert.NotEqual(ResponseUpdate2, 0);
        }
        private GarmentInventorySummary GenerateGarmentInventorySummary(GarmentUnitExpenditureNote garmentUnitExpenditureNote, GarmentUnitExpenditureNoteItem garmentUnitExpenditureNoteItem, GarmentInventoryMovement garmentInventoryMovement)
        {
            var garmentInventorySummary = new GarmentInventorySummary();

            EntityExtension.FlagForCreate(garmentInventorySummary, identityService.Username, USER_AGENT);
            do
            {
                garmentInventorySummary.No = CodeGenerator.Generate();
            }while (dbSetGarmentInventorySummary.Any(m => m.No == garmentInventorySummary.No));

            garmentInventorySummary.ProductId   = garmentUnitExpenditureNoteItem.ProductId;
            garmentInventorySummary.ProductCode = garmentUnitExpenditureNoteItem.ProductCode;
            garmentInventorySummary.ProductName = garmentUnitExpenditureNoteItem.ProductName;

            garmentInventorySummary.StorageId   = garmentUnitExpenditureNote.StorageId;
            garmentInventorySummary.StorageCode = garmentUnitExpenditureNote.StorageCode;
            garmentInventorySummary.StorageName = garmentUnitExpenditureNote.StorageName;

            garmentInventorySummary.Quantity = garmentInventoryMovement.After;

            garmentInventorySummary.UomId   = garmentUnitExpenditureNoteItem.UomId;
            garmentInventorySummary.UomUnit = garmentUnitExpenditureNoteItem.UomUnit;

            garmentInventorySummary.StockPlanning = 0;

            return(garmentInventorySummary);
        }
        private async Task <string> GenerateNo(GarmentUnitExpenditureNote garmentUnitExpenditureNote)
        {
            string Year  = garmentUnitExpenditureNote.ExpenditureDate.ToOffset(new TimeSpan(identityService.TimezoneOffset, 0, 0)).ToString("yy");
            string Month = garmentUnitExpenditureNote.ExpenditureDate.ToOffset(new TimeSpan(identityService.TimezoneOffset, 0, 0)).ToString("MM");
            string Day   = garmentUnitExpenditureNote.ExpenditureDate.ToOffset(new TimeSpan(identityService.TimezoneOffset, 0, 0)).ToString("dd");

            string no = "";

            if (garmentUnitExpenditureNote.ExpenditureType == "PROSES" || garmentUnitExpenditureNote.ExpenditureType == "SAMPLE" || garmentUnitExpenditureNote.ExpenditureType == "EXTERNAL")
            {
                no = string.Concat("BUK", garmentUnitExpenditureNote.UnitRequestCode, Year, Month, Day);
            }
            else if (garmentUnitExpenditureNote.ExpenditureType == "TRANSFER")
            {
                no = string.Concat("BUK", garmentUnitExpenditureNote.UnitSenderCode, Year, Month, Day);
            }
            int Padding = 3;

            var lastNo = await dbSet.Where(w => w.UENNo.StartsWith(no) && !w.IsDeleted).OrderByDescending(o => o.UENNo).FirstOrDefaultAsync();

            if (lastNo == null)
            {
                return(no + "1".PadLeft(Padding, '0'));
            }
            else
            {
                int lastNoNumber = Int32.Parse(lastNo.UENNo.Replace(no, string.Empty)) + 1;
                return(no + lastNoNumber.ToString().PadLeft(Padding, '0'));
            }
        }
        public void SetDataWithStorageRequest(GarmentUnitExpenditureNote garmentUnitExpenditureNote, long?unitId = null)
        {
            long nowTicks = unitId ?? DateTimeOffset.Now.Ticks;

            garmentUnitExpenditureNote.StorageRequestId   = nowTicks;
            garmentUnitExpenditureNote.StorageRequestCode = string.Concat("StorageCode", nowTicks);
            garmentUnitExpenditureNote.StorageRequestName = string.Concat("StorageName", nowTicks);
        }
        private GarmentInventoryDocument GenerateGarmentInventoryDocument(GarmentUnitExpenditureNote garmentUnitExpenditureNote, string type = "IN")
        {
            var garmentInventoryDocument = new GarmentInventoryDocument
            {
                Items = new List <GarmentInventoryDocumentItem>()
            };

            EntityExtension.FlagForCreate(garmentInventoryDocument, identityService.Username, USER_AGENT);
            do
            {
                garmentInventoryDocument.No = CodeGenerator.Generate();
            }while (dbSetGarmentInventoryDocument.Any(m => m.No == garmentInventoryDocument.No));

            garmentInventoryDocument.Date          = garmentUnitExpenditureNote.ExpenditureDate;
            garmentInventoryDocument.ReferenceNo   = garmentUnitExpenditureNote.UENNo;
            garmentInventoryDocument.ReferenceType = string.Concat("Bon Pengeluaran Unit - ", garmentUnitExpenditureNote.UnitSenderName);

            garmentInventoryDocument.Type = (type ?? "").ToUpper() == "IN" ? "IN" : "OUT";

            garmentInventoryDocument.StorageId   = garmentUnitExpenditureNote.StorageId;
            garmentInventoryDocument.StorageCode = garmentUnitExpenditureNote.StorageCode;
            garmentInventoryDocument.StorageName = garmentUnitExpenditureNote.StorageName;

            garmentInventoryDocument.Remark = "";

            foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNote.Items)
            {
                var garmentInventoryDocumentItem = new GarmentInventoryDocumentItem();
                EntityExtension.FlagForCreate(garmentInventoryDocumentItem, identityService.Username, USER_AGENT);

                garmentInventoryDocumentItem.ProductId   = garmentUnitExpenditureNoteItem.ProductId;
                garmentInventoryDocumentItem.ProductCode = garmentUnitExpenditureNoteItem.ProductCode;
                garmentInventoryDocumentItem.ProductName = garmentUnitExpenditureNoteItem.ProductName;

                garmentInventoryDocumentItem.Quantity = (decimal)garmentUnitExpenditureNoteItem.Quantity;

                garmentInventoryDocumentItem.UomId   = garmentUnitExpenditureNoteItem.UomId;
                garmentInventoryDocumentItem.UomUnit = garmentUnitExpenditureNoteItem.UomUnit;

                garmentInventoryDocumentItem.ProductRemark = garmentUnitExpenditureNoteItem.ProductRemark;

                garmentInventoryDocument.Items.Add(garmentInventoryDocumentItem);
            }

            return(garmentInventoryDocument);
        }
        private GarmentInventoryMovement GenerateGarmentInventoryMovement(GarmentUnitExpenditureNote garmentUnitExpenditureNote, GarmentUnitExpenditureNoteItem garmentUnitExpenditureNoteItem, GarmentInventorySummary garmentInventorySummary, string type = "IN")
        {
            var garmentInventoryMovement = new GarmentInventoryMovement();

            EntityExtension.FlagForCreate(garmentInventoryMovement, identityService.Username, USER_AGENT);
            do
            {
                garmentInventoryMovement.No = CodeGenerator.Generate();
            }while (dbSetGarmentInventoryMovement.Any(m => m.No == garmentInventoryMovement.No));

            garmentInventoryMovement.Date = garmentInventoryMovement.CreatedUtc;

            garmentInventoryMovement.ReferenceNo   = garmentUnitExpenditureNote.UENNo;
            garmentInventoryMovement.ReferenceType = string.Concat("Bon Pengeluaran Unit - ", garmentUnitExpenditureNote.UnitSenderName);

            garmentInventoryMovement.ProductId   = garmentUnitExpenditureNoteItem.ProductId;
            garmentInventoryMovement.ProductCode = garmentUnitExpenditureNoteItem.ProductCode;
            garmentInventoryMovement.ProductName = garmentUnitExpenditureNoteItem.ProductName;

            garmentInventoryMovement.Type = (type ?? "").ToUpper() == "IN" ? "IN" : "OUT";

            garmentInventoryMovement.StorageId   = garmentUnitExpenditureNote.StorageId;
            garmentInventoryMovement.StorageCode = garmentUnitExpenditureNote.StorageCode;
            garmentInventoryMovement.StorageName = garmentUnitExpenditureNote.StorageName;

            garmentInventoryMovement.StockPlanning = 0;
            if (garmentUnitExpenditureNote.ExpenditureType == "TRANSFER")
            {
                garmentInventoryMovement.Before   = garmentInventorySummary == null ? 0 : garmentInventorySummary.Quantity;
                garmentInventoryMovement.Quantity = (decimal)garmentUnitExpenditureNoteItem.Quantity * (type.ToUpper() == "OUT" ? -1 : 1);
                garmentInventoryMovement.After    = garmentInventorySummary == null || garmentInventorySummary.Quantity == 0  ? garmentInventoryMovement.Quantity : garmentInventoryMovement.Before + garmentInventoryMovement.Quantity;
            }
            else
            {
                garmentInventoryMovement.Before   = garmentInventorySummary == null ? 0 : garmentInventorySummary.Quantity;
                garmentInventoryMovement.Quantity = (decimal)garmentUnitExpenditureNoteItem.Quantity * ((type ?? "").ToUpper() == "OUT" ? -1 : 1);
                garmentInventoryMovement.After    = garmentInventoryMovement.Before + garmentInventoryMovement.Quantity;
            }

            garmentInventoryMovement.UomId   = garmentUnitExpenditureNoteItem.UomId;
            garmentInventoryMovement.UomUnit = garmentUnitExpenditureNoteItem.UomUnit;

            garmentInventoryMovement.Remark = garmentUnitExpenditureNoteItem.ProductRemark;

            return(garmentInventoryMovement);
        }
        public async Task <GarmentUnitExpenditureNote> GetNewData()
        {
            long nowTicks = DateTimeOffset.Now.Ticks;

            var garmentUnitDeliveryOrder = await Task.Run(() => garmentUnitDeliveryOrderDataUtil.GetTestDataMultipleItem());

            var garmentUnitExpenditureNote = new GarmentUnitExpenditureNote
            {
                UnitSenderId   = garmentUnitDeliveryOrder.UnitSenderId,
                UnitSenderCode = garmentUnitDeliveryOrder.UnitSenderCode,
                UnitSenderName = garmentUnitDeliveryOrder.UnitSenderName,

                UnitRequestId   = garmentUnitDeliveryOrder.UnitRequestId,
                UnitRequestCode = garmentUnitDeliveryOrder.UnitRequestCode,
                UnitRequestName = garmentUnitDeliveryOrder.UnitRequestName,

                UnitDOId = garmentUnitDeliveryOrder.Id,
                UnitDONo = garmentUnitDeliveryOrder.UnitDONo,

                StorageId   = garmentUnitDeliveryOrder.StorageId,
                StorageCode = garmentUnitDeliveryOrder.StorageCode,
                StorageName = garmentUnitDeliveryOrder.StorageName,

                StorageRequestId   = garmentUnitDeliveryOrder.StorageRequestId,
                StorageRequestCode = garmentUnitDeliveryOrder.StorageRequestCode,
                StorageRequestName = garmentUnitDeliveryOrder.StorageRequestName,

                ExpenditureType = "PROSES",
                ExpenditureTo   = "PROSES",
                UENNo           = "UENNO12345",

                ExpenditureDate = DateTimeOffset.Now,

                Items = new List <GarmentUnitExpenditureNoteItem>()
            };

            foreach (var item in garmentUnitDeliveryOrder.Items)
            {
                var garmentUnitExpenditureNoteItem = new GarmentUnitExpenditureNoteItem
                {
                    IsSave     = true,
                    DODetailId = item.DODetailId,

                    EPOItemId = item.EPOItemId,

                    URNItemId    = item.URNItemId,
                    UnitDOItemId = item.Id,
                    PRItemId     = item.PRItemId,

                    FabricType     = item.FabricType,
                    POItemId       = item.POItemId,
                    POSerialNumber = item.POSerialNumber,

                    ProductId     = item.ProductId,
                    ProductCode   = item.ProductCode,
                    ProductName   = item.ProductName,
                    ProductRemark = item.ProductRemark,
                    Quantity      = 5,

                    RONo = item.RONo,

                    UomId   = item.UomId,
                    UomUnit = item.UomUnit,

                    PricePerDealUnit = item.PricePerDealUnit,
                };
                new GarmentUnitExpenditureNoteItem
                {
                    Id         = 0,
                    IsSave     = true,
                    DODetailId = item.DODetailId,

                    EPOItemId = item.EPOItemId,

                    URNItemId    = item.URNItemId,
                    UnitDOItemId = item.Id,
                    PRItemId     = item.PRItemId,

                    FabricType     = item.FabricType,
                    POItemId       = item.POItemId,
                    POSerialNumber = item.POSerialNumber,

                    ProductId     = item.ProductId,
                    ProductCode   = item.ProductCode,
                    ProductName   = item.ProductName,
                    ProductRemark = item.ProductRemark,
                    Quantity      = item.Quantity,

                    RONo = item.RONo,

                    UomId   = item.UomId,
                    UomUnit = item.UomUnit,

                    PricePerDealUnit = item.PricePerDealUnit,
                };

                garmentUnitExpenditureNote.Items.Add(garmentUnitExpenditureNoteItem);
            }

            return(garmentUnitExpenditureNote);
        }
        public async Task <int> Create(GarmentUnitExpenditureNote garmentUnitExpenditureNote)
        {
            int Created = 0;

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    EntityExtension.FlagForCreate(garmentUnitExpenditureNote, identityService.Username, USER_AGENT);
                    garmentUnitExpenditureNote.UENNo = await GenerateNo(garmentUnitExpenditureNote);

                    var garmentUnitDeliveryOrder = dbSetGarmentUnitDeliveryOrder.First(d => d.Id == garmentUnitExpenditureNote.UnitDOId);
                    EntityExtension.FlagForUpdate(garmentUnitDeliveryOrder, identityService.Username, USER_AGENT);
                    garmentUnitDeliveryOrder.IsUsed = true;

                    var UnitDO = dbContext.GarmentUnitDeliveryOrders.Include(d => d.Items).FirstOrDefault(d => d.Id.Equals(garmentUnitExpenditureNote.UnitDOId));
                    foreach (var unitDOItem in UnitDO.Items)
                    {
                        var unitExSaved = garmentUnitExpenditureNote.Items.FirstOrDefault(d => d.UnitDOItemId == unitDOItem.Id);
                        if (unitExSaved == null || unitExSaved.IsSave == false)
                        {
                            var garmentUnitReceiptNoteItem = dbSetGarmentUnitReceiptNoteItem.FirstOrDefault(u => u.Id == unitDOItem.URNItemId);
                            EntityExtension.FlagForUpdate(garmentUnitReceiptNoteItem, identityService.Username, USER_AGENT);
                            garmentUnitReceiptNoteItem.OrderQuantity = garmentUnitReceiptNoteItem.OrderQuantity - (decimal)unitDOItem.Quantity;
                            unitDOItem.Quantity = 0;
                        }
                    }

                    var garmentUnitExpenditureNoteItems = garmentUnitExpenditureNote.Items.Where(x => x.IsSave).ToList();
                    foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNoteItems)
                    {
                        EntityExtension.FlagForCreate(garmentUnitExpenditureNoteItem, identityService.Username, USER_AGENT);

                        var garmentUnitDeliveryOrderItem = dbSetGarmentUnitDeliveryOrderItem.FirstOrDefault(s => s.Id == garmentUnitExpenditureNoteItem.UnitDOItemId);
                        var garmentUnitReceiptNoteItem   = dbSetGarmentUnitReceiptNoteItem.FirstOrDefault(u => u.Id == garmentUnitExpenditureNoteItem.URNItemId);
                        if (garmentUnitDeliveryOrderItem != null && garmentUnitReceiptNoteItem != null)
                        {
                            if (garmentUnitDeliveryOrderItem.Quantity != garmentUnitExpenditureNoteItem.Quantity)
                            {
                                EntityExtension.FlagForUpdate(garmentUnitDeliveryOrderItem, identityService.Username, USER_AGENT);
                                garmentUnitReceiptNoteItem.OrderQuantity = garmentUnitReceiptNoteItem.OrderQuantity - ((decimal)garmentUnitDeliveryOrderItem.Quantity - (decimal)garmentUnitExpenditureNoteItem.Quantity);
                                garmentUnitDeliveryOrderItem.Quantity    = garmentUnitExpenditureNoteItem.Quantity;
                            }
                        }
                    }

                    //var garmentUENIsSaveFalse = garmentUnitExpenditureNote.Items.Where(d => d.IsSave == false).ToList();

                    //if (garmentUENIsSaveFalse.Count > 0)
                    //{
                    //    foreach (var itemFalseIsSave in garmentUENIsSaveFalse)
                    //    {
                    //        var garmentUnitDeliveryOrderItem = dbSetGarmentUnitDeliveryOrderItem.FirstOrDefault(s => s.Id == itemFalseIsSave.UnitDOItemId);
                    //        var garmentUnitReceiptNoteItem = dbSetGarmentUnitReceiptNoteItem.FirstOrDefault(u => u.Id == itemFalseIsSave.URNItemId);
                    //        EntityExtension.FlagForUpdate(garmentUnitReceiptNoteItem, identityService.Username, USER_AGENT);
                    //        garmentUnitReceiptNoteItem.OrderQuantity = garmentUnitReceiptNoteItem.OrderQuantity - (decimal)itemFalseIsSave.Quantity;
                    //        garmentUnitDeliveryOrderItem.Quantity = 0;
                    //    }
                    //}


                    if (garmentUnitExpenditureNote.ExpenditureType == "TRANSFER")
                    {
                        var garmentInventoryDocumentTransferOutStorage = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentTransferOutStorage);

                        var garmentInventoryDocumentTransferInStorageRequest = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "IN");
                        garmentInventoryDocumentTransferInStorageRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                        garmentInventoryDocumentTransferInStorageRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                        garmentInventoryDocumentTransferInStorageRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentTransferInStorageRequest);

                        var garmentInventoryDocumentTransferOutStorageRequest = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        garmentInventoryDocumentTransferOutStorageRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                        garmentInventoryDocumentTransferOutStorageRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                        garmentInventoryDocumentTransferOutStorageRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentTransferOutStorageRequest);

                        foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNote.Items)
                        {
                            var garmentInventorySummaryExisting = dbSetGarmentInventorySummary.SingleOrDefault(s => s.ProductId == garmentUnitExpenditureNoteItem.ProductId && s.StorageId == garmentUnitExpenditureNote.StorageId && s.UomId == garmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovement = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExisting, "OUT");
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovement);

                            if (garmentInventorySummaryExisting == null)
                            {
                                var garmentInventorySummary = GenerateGarmentInventorySummary(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventoryMovement);
                                dbSetGarmentInventorySummary.Add(garmentInventorySummary);
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(garmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                                garmentInventorySummaryExisting.Quantity = garmentInventoryMovement.After;
                            }

                            var garmentInventorySummaryExistingRequest = dbSetGarmentInventorySummary.SingleOrDefault(s => s.ProductId == garmentUnitExpenditureNoteItem.ProductId && s.StorageId == garmentUnitExpenditureNote.StorageRequestId && s.UomId == garmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovementRequestIn = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExistingRequest, "IN");
                            garmentInventoryMovementRequestIn.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                            garmentInventoryMovementRequestIn.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                            garmentInventoryMovementRequestIn.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                            garmentInventoryMovementRequestIn.After       = garmentInventoryMovementRequestIn.Before + (decimal)garmentUnitExpenditureNoteItem.Quantity;
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementRequestIn);

                            var garmentInventoryMovementRequestOut = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExistingRequest, "OUT");
                            garmentInventoryMovementRequestOut.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                            garmentInventoryMovementRequestOut.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                            garmentInventoryMovementRequestOut.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                            garmentInventoryMovementRequestOut.Before      = garmentInventoryMovementRequestIn.After;
                            garmentInventoryMovementRequestOut.After       = garmentInventoryMovementRequestOut.Before - (decimal)garmentUnitExpenditureNoteItem.Quantity;

                            //if (garmentInventorySummaryExistingRequest == null || garmentInventorySummaryExistingRequest.Quantity == 0)
                            //{
                            //    garmentInventoryMovementRequestOut.Before = garmentInventoryMovementRequestIn.After;
                            //    garmentInventoryMovementRequestOut.After = garmentInventoryMovementRequestIn.Before;
                            //}
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementRequestOut);

                            if (garmentInventorySummaryExistingRequest == null)
                            {
                                var garmentInventorySummaryRequest = GenerateGarmentInventorySummary(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventoryMovementRequestOut);
                                garmentInventorySummaryRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                                garmentInventorySummaryRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                                garmentInventorySummaryRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                                dbSetGarmentInventorySummary.Add(garmentInventorySummaryRequest);
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(garmentInventorySummaryExistingRequest, identityService.Username, USER_AGENT);
                                garmentInventorySummaryExistingRequest.Quantity = garmentInventoryMovementRequestOut.After;
                            }
                            await dbContext.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        var garmentInventoryDocument = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocument);

                        foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNote.Items)
                        {
                            var garmentInventorySummaryExisting = dbSetGarmentInventorySummary.SingleOrDefault(s => s.ProductId == garmentUnitExpenditureNoteItem.ProductId && s.StorageId == garmentUnitExpenditureNote.StorageId && s.UomId == garmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovement = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExisting, "OUT");
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovement);

                            if (garmentInventorySummaryExisting == null)
                            {
                                var garmentInventorySummary = GenerateGarmentInventorySummary(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventoryMovement);
                                dbSetGarmentInventorySummary.Add(garmentInventorySummary);
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(garmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                                garmentInventorySummaryExisting.Quantity = garmentInventoryMovement.After;
                            }
                            await dbContext.SaveChangesAsync();
                        }
                    }

                    dbSet.Add(garmentUnitExpenditureNote);

                    Created = await dbContext.SaveChangesAsync();

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

            return(Created);
        }
        public async Task <int> Update(int id, GarmentUnitExpenditureNote garmentUnitExpenditureNote)
        {
            int Updated = 0;

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var itemIsSaveFalse = garmentUnitExpenditureNote.Items.Where(i => i.IsSave == false).ToList();
                    garmentUnitExpenditureNote.Items = garmentUnitExpenditureNote.Items.Where(x => x.IsSave).ToList();

                    var oldGarmentUnitExpenditureNote = dbSet
                                                        .Include(d => d.Items)
                                                        .Single(m => m.Id == id);

                    foreach (var uncheck in itemIsSaveFalse)
                    {
                        var garmentInventorySummaryExisting = dbSetGarmentInventorySummary.FirstOrDefault(s => s.ProductId == uncheck.ProductId && s.StorageId == oldGarmentUnitExpenditureNote.StorageId && s.UomId == uncheck.UomId);

                        var garmentInventoryMovement = GenerateGarmentInventoryMovement(oldGarmentUnitExpenditureNote, uncheck, garmentInventorySummaryExisting, "IN");
                        dbSetGarmentInventoryMovement.Add(garmentInventoryMovement);

                        if (garmentInventorySummaryExisting != null)
                        {
                            EntityExtension.FlagForUpdate(garmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                            garmentInventorySummaryExisting.Quantity = garmentInventorySummaryExisting.Quantity + (decimal)uncheck.Quantity;
                            garmentInventoryMovement.After           = garmentInventorySummaryExisting.Quantity;
                        }
                    }


                    if (garmentUnitExpenditureNote.ExpenditureType == "TRANSFER")
                    {
                        var garmentInventoryDocumentIn = GenerateGarmentInventoryDocument(oldGarmentUnitExpenditureNote, "IN");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentIn);

                        var garmentInventoryDocumentOut = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentOut);

                        var garmentInventoryDocumentInRequest = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "IN");
                        garmentInventoryDocumentInRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                        garmentInventoryDocumentInRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                        garmentInventoryDocumentInRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentInRequest);

                        var garmentInventoryDocumentOutRequest = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        garmentInventoryDocumentOutRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                        garmentInventoryDocumentOutRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                        garmentInventoryDocumentOutRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentOutRequest);

                        foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNote.Items)
                        {
                            var oldGarmentUnitExpenditureNoteItem = oldGarmentUnitExpenditureNote.Items.Single(i => i.Id == garmentUnitExpenditureNoteItem.Id);

                            //Buat IN untuk gudang yang mengeluarkan
                            var oldGarmentInventorySummaryExisting = dbSetGarmentInventorySummary.Single(s => s.ProductId == oldGarmentUnitExpenditureNoteItem.ProductId && s.StorageId == oldGarmentUnitExpenditureNote.StorageId && s.UomId == oldGarmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovementIn = GenerateGarmentInventoryMovement(oldGarmentUnitExpenditureNote, oldGarmentUnitExpenditureNoteItem, oldGarmentInventorySummaryExisting, "IN");
                            garmentInventoryMovementIn.After = garmentInventoryMovementIn.Before + (decimal)oldGarmentUnitExpenditureNoteItem.Quantity;
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementIn);

                            var garmentInventoryMovementOut = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, oldGarmentInventorySummaryExisting, "OUT");
                            garmentInventoryMovementOut.Before = garmentInventoryMovementIn.After;
                            garmentInventoryMovementOut.After  = garmentInventoryMovementOut.Before - (decimal)garmentUnitExpenditureNoteItem.Quantity;
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementOut);

                            if (oldGarmentInventorySummaryExisting != null)
                            {
                                EntityExtension.FlagForUpdate(oldGarmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                                oldGarmentInventorySummaryExisting.Quantity = garmentInventoryMovementOut.After;
                            }

                            //Buat OUT untuk gudang yang mengeluarkan
                            var garmentInventorySummaryExisting = dbSetGarmentInventorySummary.SingleOrDefault(s => s.ProductId == garmentUnitExpenditureNoteItem.ProductId && s.StorageId == garmentUnitExpenditureNote.StorageRequestId && s.UomId == garmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovementInRequest = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExisting, "IN");
                            garmentInventoryMovementInRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                            garmentInventoryMovementInRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                            garmentInventoryMovementInRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                            garmentInventoryMovementInRequest.After       = garmentInventoryMovementInRequest.Before + (decimal)garmentUnitExpenditureNoteItem.Quantity;
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementInRequest);

                            var garmentInventoryMovementOutRequest = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventorySummaryExisting, "OUT");
                            garmentInventoryMovementOutRequest.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                            garmentInventoryMovementOutRequest.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                            garmentInventoryMovementOutRequest.StorageName = garmentUnitExpenditureNote.StorageRequestName;
                            garmentInventoryMovementOutRequest.Before      = garmentInventoryMovementInRequest.After;
                            garmentInventoryMovementOutRequest.After       = garmentInventoryMovementOutRequest.Before - (decimal)garmentUnitExpenditureNoteItem.Quantity;
                            if (garmentInventorySummaryExisting == null || garmentInventorySummaryExisting.Quantity == 0)
                            {
                                garmentInventoryMovementOutRequest.Before = garmentInventoryMovementInRequest.After;
                                garmentInventoryMovementOutRequest.After  = garmentInventoryMovementInRequest.Before;
                            }
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementOutRequest);

                            if (garmentInventorySummaryExisting == null)
                            {
                                var garmentInventorySummary = GenerateGarmentInventorySummary(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, garmentInventoryMovementOutRequest);
                                garmentInventorySummary.StorageId   = garmentUnitExpenditureNote.StorageRequestId;
                                garmentInventorySummary.StorageCode = garmentUnitExpenditureNote.StorageRequestCode;
                                garmentInventorySummary.StorageName = garmentUnitExpenditureNote.StorageRequestName;

                                dbSetGarmentInventorySummary.Add(garmentInventorySummary);
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(garmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                                garmentInventorySummaryExisting.Quantity = garmentInventoryMovementOutRequest.After;
                            }
                        }
                    }
                    else
                    {
                        var garmentInventoryDocumentIn = GenerateGarmentInventoryDocument(oldGarmentUnitExpenditureNote, "IN");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentIn);

                        var garmentInventoryDocumentOut = GenerateGarmentInventoryDocument(garmentUnitExpenditureNote, "OUT");
                        dbSetGarmentInventoryDocument.Add(garmentInventoryDocumentOut);

                        foreach (var garmentUnitExpenditureNoteItem in garmentUnitExpenditureNote.Items)
                        {
                            var oldGarmentUnitExpenditureNoteItem = oldGarmentUnitExpenditureNote.Items.Single(i => i.Id == garmentUnitExpenditureNoteItem.Id);

                            //Buat IN untuk gudang yang mengeluarkan
                            var oldGarmentInventorySummaryExisting = dbSetGarmentInventorySummary.Single(s => s.ProductId == oldGarmentUnitExpenditureNoteItem.ProductId && s.StorageId == oldGarmentUnitExpenditureNote.StorageId && s.UomId == oldGarmentUnitExpenditureNoteItem.UomId);

                            var garmentInventoryMovementIn = GenerateGarmentInventoryMovement(oldGarmentUnitExpenditureNote, oldGarmentUnitExpenditureNoteItem, oldGarmentInventorySummaryExisting, "IN");
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementIn);

                            var garmentInventoryMovementOut = GenerateGarmentInventoryMovement(garmentUnitExpenditureNote, garmentUnitExpenditureNoteItem, oldGarmentInventorySummaryExisting, "OUT");
                            garmentInventoryMovementOut.Before = garmentInventoryMovementIn.After;
                            garmentInventoryMovementOut.After  = garmentInventoryMovementOut.Before - (decimal)garmentUnitExpenditureNoteItem.Quantity;
                            dbSetGarmentInventoryMovement.Add(garmentInventoryMovementOut);

                            EntityExtension.FlagForUpdate(oldGarmentInventorySummaryExisting, identityService.Username, USER_AGENT);
                            oldGarmentInventorySummaryExisting.Quantity = garmentInventoryMovementOut.After;
                        }
                    }

                    EntityExtension.FlagForUpdate(oldGarmentUnitExpenditureNote, identityService.Username, USER_AGENT);

                    foreach (var oldGarmentUnitExpenditureNoteItem in oldGarmentUnitExpenditureNote.Items)
                    {
                        var newGarmentUnitExpenditureNoteItem = garmentUnitExpenditureNote.Items.FirstOrDefault(i => i.Id == oldGarmentUnitExpenditureNoteItem.Id);
                        if (newGarmentUnitExpenditureNoteItem == null)
                        {
                            var coba = dbContext.GarmentUnitExpenditureNotes.AsNoTracking().FirstOrDefault(d => d.Id == id);
                            coba.Items = itemIsSaveFalse;


                            EntityExtension.FlagForDelete(oldGarmentUnitExpenditureNoteItem, identityService.Username, USER_AGENT);

                            var garmentUnitDeliveryOrderItem = dbSetGarmentUnitDeliveryOrderItem.FirstOrDefault(s => s.Id == oldGarmentUnitExpenditureNoteItem.UnitDOItemId);
                            garmentUnitDeliveryOrderItem.Quantity = 0;

                            GarmentUnitReceiptNoteItem garmentUnitReceiptNoteItem = dbContext.GarmentUnitReceiptNoteItems.Single(s => s.Id == oldGarmentUnitExpenditureNoteItem.URNItemId);
                            EntityExtension.FlagForUpdate(garmentUnitReceiptNoteItem, identityService.Username, USER_AGENT);
                            garmentUnitReceiptNoteItem.OrderQuantity = garmentUnitReceiptNoteItem.OrderQuantity - (decimal)oldGarmentUnitExpenditureNoteItem.Quantity;
                        }
                        else
                        {
                            EntityExtension.FlagForUpdate(oldGarmentUnitExpenditureNoteItem, identityService.Username, USER_AGENT);

                            var garmentUnitDeliveryOrderItem = dbSetGarmentUnitDeliveryOrderItem.FirstOrDefault(s => s.Id == oldGarmentUnitExpenditureNoteItem.UnitDOItemId);
                            var garmentUnitReceiptNoteItem   = dbSetGarmentUnitReceiptNoteItem.FirstOrDefault(u => u.Id == oldGarmentUnitExpenditureNoteItem.URNItemId);

                            if (garmentUnitDeliveryOrderItem != null && garmentUnitReceiptNoteItem != null)
                            {
                                EntityExtension.FlagForUpdate(garmentUnitReceiptNoteItem, identityService.Username, USER_AGENT);
                                garmentUnitReceiptNoteItem.OrderQuantity = garmentUnitReceiptNoteItem.OrderQuantity - ((decimal)oldGarmentUnitExpenditureNoteItem.Quantity - (decimal)newGarmentUnitExpenditureNoteItem.Quantity);
                                garmentUnitDeliveryOrderItem.Quantity    = newGarmentUnitExpenditureNoteItem.Quantity;
                            }
                            oldGarmentUnitExpenditureNoteItem.Quantity = garmentUnitExpenditureNote.Items.FirstOrDefault(i => i.Id == oldGarmentUnitExpenditureNoteItem.Id).Quantity;
                        }
                    }

                    //dbSet.Update(garmentUnitExpenditureNote);

                    Updated = await dbContext.SaveChangesAsync();

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

            return(Updated);
        }
예제 #10
0
        public async Task <GarmentUnitReceiptNote> GetNewDataMonitoringFlow(long?ticks = null, GarmentDeliveryOrder garmentDeliveryOrder = null, GarmentUnitExpenditureNote garmentUnitExpenditureNote = null)
        {
            long nowTicks = ticks ?? DateTimeOffset.Now.Ticks;

            garmentDeliveryOrder = garmentDeliveryOrder ?? await Task.Run(() => garmentDeliveryOrderDataUtil.GetTestData());

            var garmentUnitReceiptNote = new GarmentUnitReceiptNote
            {
                URNType  = "PEMBELIAN",
                UnitId   = nowTicks,
                UnitCode = "SMP1",
                UnitName = string.Concat("UnitName", nowTicks),

                StorageId   = nowTicks,
                StorageCode = string.Concat("StorageCode", nowTicks),
                StorageName = string.Concat("StorageName", nowTicks),

                SupplierId   = garmentDeliveryOrder.SupplierId,
                SupplierCode = "SupplierCode",
                SupplierName = "SupplierName",

                DOId = garmentDeliveryOrder.Id,
                DONo = "DONo",

                DeletedReason = nowTicks.ToString(),

                DOCurrencyRate = garmentDeliveryOrder.DOCurrencyRate,

                ReceiptDate = DateTimeOffset.Now,
                URNNo       = "URNNo",

                Items = new List <GarmentUnitReceiptNoteItem>()
            };

            foreach (var item in garmentDeliveryOrder.Items)
            {
                foreach (var detail in item.Details)
                {
                    var garmentUnitReceiptNoteItem = new GarmentUnitReceiptNoteItem
                    {
                        DODetailId = detail.Id,

                        EPOItemId = detail.EPOItemId,
                        DRItemId  = string.Concat("drItemId", nowTicks),
                        PRId      = detail.PRId,
                        PRNo      = "PRNo",
                        PRItemId  = detail.PRItemId,

                        POId           = detail.POId,
                        POItemId       = detail.POItemId,
                        POSerialNumber = "POSerialNumber",

                        ProductId     = detail.ProductId,
                        ProductCode   = detail.ProductCode,
                        ProductName   = "FABRIC",
                        ProductRemark = detail.ProductRemark,

                        RONo = "RONo",

                        ReceiptQuantity = (decimal)100,

                        UomId   = long.Parse(detail.UomId),
                        UomUnit = detail.UomUnit,

                        PricePerDealUnit = (decimal)detail.PricePerDealUnit,

                        DesignColor = string.Concat("DesignColor", nowTicks),

                        SmallQuantity        = (decimal)detail.SmallQuantity,
                        OrderQuantity        = 30,
                        SmallUomId           = long.Parse(detail.SmallUomId),
                        SmallUomUnit         = detail.SmallUomUnit,
                        Conversion           = (decimal)1,
                        CorrectionConversion = (decimal)12,

                        DOCurrencyRate = 1,

                        UENItemId = 1
                    };
                    var garmentUnitReceiptNoteItem2 = new GarmentUnitReceiptNoteItem
                    {
                        DODetailId = detail.Id,

                        EPOItemId = detail.EPOItemId,
                        DRItemId  = string.Concat("drItemId", nowTicks),
                        PRId      = detail.PRId,
                        PRNo      = detail.PRNo,
                        PRItemId  = detail.PRItemId,

                        POId           = detail.POId,
                        POItemId       = detail.POItemId,
                        POSerialNumber = detail.POSerialNumber,

                        ProductId     = detail.ProductId,
                        ProductCode   = detail.ProductCode,
                        ProductName   = detail.ProductName,
                        ProductRemark = detail.ProductRemark,

                        RONo = detail.RONo + "S",

                        ReceiptQuantity = (decimal)100,

                        UomId   = long.Parse(detail.UomId),
                        UomUnit = detail.UomUnit,

                        PricePerDealUnit = (decimal)detail.PricePerDealUnit,

                        DesignColor = string.Concat("DesignColor", nowTicks),

                        SmallQuantity        = (decimal)detail.SmallQuantity,
                        OrderQuantity        = 30,
                        SmallUomId           = long.Parse(detail.SmallUomId),
                        SmallUomUnit         = detail.SmallUomUnit,
                        Conversion           = (decimal)1,
                        CorrectionConversion = (decimal)12,

                        DOCurrencyRate = 1,

                        UENItemId = 1
                    };

                    garmentUnitReceiptNote.Items.Add(garmentUnitReceiptNoteItem);
                    garmentUnitReceiptNote.Items.Add(garmentUnitReceiptNoteItem2);
                }
            }

            return(garmentUnitReceiptNote);
        }