public POExternalItemDto(GarmentExternalPurchaseOrderItem element, GarmentExternalPurchaseOrder entity, GarmentInternalPurchaseOrder purchaseOrder)
 {
     Product         = new ProductDto(element.ProductCode, element.ProductId, element.ProductName, element.DefaultUomId, element.DefaultUomUnit);
     DefaultQuantity = element.DefaultQuantity;
     DealQuantity    = element.DealQuantity;
     DealUOM         = new UOMDto(element.DealUomId, element.DealUomUnit);
     Conversion      = element.Conversion;
     Price           = element.PricePerDealUnit;
     UseVat          = entity.IsUseVat;
     Unit            = new UnitDto(purchaseOrder.UnitId, purchaseOrder.UnitCode, purchaseOrder.UnitName, purchaseOrder.DivisionCode, purchaseOrder.DivisionId, purchaseOrder.DivisionName);
     IncomeTax       = new IncomeTaxDto(entity.IncomeTaxId, entity.IncomeTaxName, entity.IncomeTaxRate);
     UseIncomeTax    = entity.IsIncomeTax;
     EPOId           = (int)entity.Id;
 }
        public async Task <int> Create(GarmentInternNote m, bool isImport, string user, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

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

                    m.INNo = await GenerateNo(m, isImport, clientTimeZoneOffset);

                    m.INDate = DateTimeOffset.Now;

                    foreach (var item in m.Items)
                    {
                        foreach (var detail in item.Details)
                        {
                            GarmentExternalPurchaseOrderItem eksternalPurchaseOrderItem = this.dbContext.GarmentExternalPurchaseOrderItems.FirstOrDefault(s => s.GarmentEPOId == detail.EPOId);
                            GarmentInternalPurchaseOrder     internalPurchaseOrder      = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id == eksternalPurchaseOrderItem.POId);
                            detail.UnitId   = internalPurchaseOrder.UnitId;
                            detail.UnitCode = internalPurchaseOrder.UnitCode;
                            detail.UnitName = internalPurchaseOrder.UnitName;
                            EntityExtension.FlagForCreate(detail, user, USER_AGENT);
                        }
                        GarmentInvoice garmentInvoice = this.dbContext.GarmentInvoices.FirstOrDefault(s => s.Id == item.InvoiceId);
                        garmentInvoice.HasInternNote = true;

                        EntityExtension.FlagForCreate(item, user, USER_AGENT);
                    }

                    this.dbSet.Add(m);

                    Created = await dbContext.SaveChangesAsync();

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

            return(Created);
        }
        public async void Should_Success_Update_Data()
        {
            GarmentExternalPurchaseOrderFacade facade = new GarmentExternalPurchaseOrderFacade(ServiceProvider, _dbContext(GetCurrentMethod()));
            var data = await dataUtil(facade, GetCurrentMethod()).GetTestDataFabric();

            List <GarmentExternalPurchaseOrderItem> item = new List <GarmentExternalPurchaseOrderItem>(data.Items);

            data.Items.Add(new GarmentExternalPurchaseOrderItem
            {
                PO_SerialNumber  = "PO_SerialNumber",
                ProductId        = item[0].ProductId,
                PRId             = item[0].PRId,
                POId             = item[0].POId,
                ProductCode      = "item.ProductCode",
                ProductName      = "item.ProductName",
                DealQuantity     = 2,
                BudgetPrice      = 100,
                DealUomId        = 1,
                DealUomUnit      = "unit",
                Remark           = "item.ProductRemark",
                IsOverBudget     = true,
                OverBudgetRemark = "OB"
            });

            var ResponseUpdate = await facade.Update((int)data.Id, data, USERNAME);

            Assert.NotEqual(ResponseUpdate, 0);
            var newItem = new GarmentExternalPurchaseOrderItem
            {
                PO_SerialNumber  = "PO_SerialNumber",
                ProductId        = item[0].ProductId,
                PRId             = item[0].PRId,
                POId             = item[0].POId,
                ProductCode      = "ProductCode",
                ProductName      = "ProductName",
                DealQuantity     = 2,
                BudgetPrice      = 100,
                DealUomId        = 1,
                DealUomUnit      = "unit",
                Remark           = "ProductRemark",
                IsOverBudget     = true,
                OverBudgetRemark = "OB"
            };
            List <GarmentExternalPurchaseOrderItem> Newitems = new List <GarmentExternalPurchaseOrderItem>(data.Items);

            Newitems.Add(newItem);
            data.Items = Newitems;

            var ResponseUpdate1 = await facade.Update((int)data.Id, data, USERNAME);

            Assert.NotEqual(ResponseUpdate1, 0);

            data.PaymentMethod = "CMT";
            data.PaymentType   = "FREE";
            var ResponseUpdate2 = await facade.Update((int)data.Id, data, USERNAME);

            Assert.NotEqual(ResponseUpdate2, 0);

            List <GarmentExternalPurchaseOrderItem> Newitems1 = new List <GarmentExternalPurchaseOrderItem>(data.Items);
            var newItem2 = new GarmentExternalPurchaseOrderItem
            {
                PO_SerialNumber  = "PO_SerialNumber2",
                ProductId        = item[0].ProductId,
                PRId             = item[0].PRId,
                POId             = item[0].POId,
                ProductCode      = "ProductCode",
                ProductName      = "ProductName",
                DealQuantity     = 2,
                BudgetPrice      = 100,
                DealUomId        = 1,
                DealUomUnit      = "unit",
                Remark           = "ProductRemark",
                IsOverBudget     = true,
                OverBudgetRemark = "OB"
            };

            Newitems1.Add(newItem2);
            data.Items         = Newitems1;
            data.PaymentMethod = "CMT";
            data.PaymentType   = "FREE";
            var ResponseUpdate3 = await facade.Update((int)data.Id, data, USERNAME);

            Assert.NotEqual(ResponseUpdate3, 0);
        }
        public async Task <int> Create(GarmentDeliveryOrder m, string user, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

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

                    m.IsClosed     = false;
                    m.IsCorrection = false;
                    m.IsCustoms    = false;

                    foreach (var item in m.Items)
                    {
                        EntityExtension.FlagForCreate(item, user, USER_AGENT);

                        CurrencyViewModel garmentCurrencyViewModel = GetCurrency(item.CurrencyCode, m.DODate);
                        m.DOCurrencyId   = garmentCurrencyViewModel.Id;
                        m.DOCurrencyCode = garmentCurrencyViewModel.Code;
                        m.DOCurrencyRate = garmentCurrencyViewModel.Rate;

                        foreach (var detail in item.Details)
                        {
                            GarmentInternalPurchaseOrder     internalPurchaseOrder     = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(detail.POId));
                            GarmentInternalPurchaseOrderItem internalPurchaseOrderItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(s => s.GPOId.Equals(internalPurchaseOrder.Id));

                            detail.POItemId = (int)internalPurchaseOrderItem.Id;
                            detail.PRItemId = internalPurchaseOrderItem.GPRItemId;
                            detail.UnitId   = internalPurchaseOrder.UnitId;
                            detail.UnitCode = internalPurchaseOrder.UnitCode;
                            EntityExtension.FlagForCreate(detail, user, USER_AGENT);

                            GarmentExternalPurchaseOrderItem externalPurchaseOrderItem = this.dbContext.GarmentExternalPurchaseOrderItems.FirstOrDefault(s => s.Id.Equals(detail.EPOItemId));
                            externalPurchaseOrderItem.DOQuantity = externalPurchaseOrderItem.DOQuantity + detail.DOQuantity;

                            if (externalPurchaseOrderItem.ReceiptQuantity == 0)
                            {
                                if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity < externalPurchaseOrderItem.DealQuantity)
                                {
                                    internalPurchaseOrderItem.Status = "Barang sudah datang parsial";
                                }
                                else if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity >= externalPurchaseOrderItem.DealQuantity)
                                {
                                    internalPurchaseOrderItem.Status = "Barang sudah datang semua";
                                }
                            }

                            detail.QuantityCorrection         = detail.DOQuantity;
                            detail.PricePerDealUnitCorrection = detail.PricePerDealUnit;
                            detail.PriceTotalCorrection       = detail.PriceTotal;

                            m.TotalAmount += detail.PriceTotal;
                        }
                    }

                    this.dbSet.Add(m);

                    Created = await dbContext.SaveChangesAsync();

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

            return(Created);
        }
        public async Task <int> Delete(int id, string user)
        {
            int Deleted = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    var model = this.dbSet
                                .Include(m => m.Items)
                                .ThenInclude(i => i.Details)
                                .SingleOrDefault(m => m.Id == id && !m.IsDeleted);

                    EntityExtension.FlagForDelete(model, user, USER_AGENT);
                    foreach (var item in model.Items)
                    {
                        EntityExtension.FlagForDelete(item, user, USER_AGENT);

                        foreach (var detail in item.Details)
                        {
                            GarmentExternalPurchaseOrderItem externalPurchaseOrderItem = this.dbContext.GarmentExternalPurchaseOrderItems.FirstOrDefault(s => s.Id.Equals(detail.EPOItemId));
                            GarmentInternalPurchaseOrder     internalPurchaseOrder     = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(detail.POId));
                            GarmentInternalPurchaseOrderItem internalPurchaseOrderItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(s => s.GPOId.Equals(detail.POId));

                            GarmentDeliveryOrderDetail deliveryOrderDetail = this.dbContext.GarmentDeliveryOrderDetails.FirstOrDefault(s => s.Id.Equals(detail.Id));
                            externalPurchaseOrderItem.DOQuantity = externalPurchaseOrderItem.DOQuantity - detail.DOQuantity;

                            if (externalPurchaseOrderItem.ReceiptQuantity == 0)
                            {
                                if (externalPurchaseOrderItem.DOQuantity == 0)
                                {
                                    GarmentPurchaseRequestItem purchaseRequestItem = this.dbContext.GarmentPurchaseRequestItems.FirstOrDefault(s => s.Id.Equals(detail.PRItemId));
                                    purchaseRequestItem.Status       = "Sudah diorder ke Supplier";
                                    internalPurchaseOrderItem.Status = "Sudah diorder ke Supplier";
                                }
                                else if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity < externalPurchaseOrderItem.DealQuantity)
                                {
                                    internalPurchaseOrderItem.Status = "Barang sudah datang parsial";
                                }
                                else if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity >= externalPurchaseOrderItem.DealQuantity)
                                {
                                    internalPurchaseOrderItem.Status = "Barang Sudah Datang Semua";
                                }
                            }

                            EntityExtension.FlagForDelete(detail, user, USER_AGENT);
                        }
                    }
                    Deleted = await dbContext.SaveChangesAsync();

                    await dbContext.SaveChangesAsync();

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

            return(Deleted);
        }
        public async Task <int> Update(int id, GarmentDeliveryOrderViewModel vm, GarmentDeliveryOrder m, string user, int clientTimeZoneOffset = 7)
        {
            int Updated = 0;

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var oldM = this.dbSet.AsNoTracking()
                               .Include(d => d.Items)
                               .ThenInclude(d => d.Details)
                               .SingleOrDefault(pr => pr.Id == id && !pr.IsDeleted);

                    if (oldM != null && oldM.Id == id)
                    {
                        EntityExtension.FlagForUpdate(m, user, USER_AGENT);

                        foreach (var vmItem in vm.items)
                        {
                            foreach (var modelItem in m.Items.Where(i => i.Id == vmItem.Id))
                            {
                                foreach (var item in oldM.Items.Where(i => i.EPOId == modelItem.EPOId).ToList())
                                {
                                    EntityExtension.FlagForUpdate(modelItem, user, USER_AGENT);

                                    CurrencyViewModel garmentCurrencyViewModel = GetCurrency(item.CurrencyCode, m.DODate);
                                    m.DOCurrencyId   = garmentCurrencyViewModel.Id;
                                    m.DOCurrencyCode = garmentCurrencyViewModel.Code;
                                    m.DOCurrencyRate = garmentCurrencyViewModel.Rate;

                                    foreach (var vmDetail in vmItem.fulfillments)
                                    {
                                        foreach (var modelDetail in modelItem.Details.Where(j => j.Id == vmDetail.Id))
                                        {
                                            foreach (var detail in item.Details.Where(j => j.EPOItemId == modelDetail.EPOItemId).ToList())
                                            {
                                                GarmentInternalPurchaseOrder     internalPurchaseOrder     = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(modelDetail.POId));
                                                GarmentInternalPurchaseOrderItem internalPurchaseOrderItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(s => s.GPOId.Equals(modelDetail.POId));
                                                GarmentExternalPurchaseOrderItem externalPurchaseOrderItem = this.dbContext.GarmentExternalPurchaseOrderItems.FirstOrDefault(s => s.Id.Equals(modelDetail.EPOItemId));

                                                if (vmDetail.isSave == false)
                                                {
                                                    externalPurchaseOrderItem.DOQuantity = externalPurchaseOrderItem.DOQuantity - detail.DOQuantity;
                                                }
                                                else
                                                {
                                                    externalPurchaseOrderItem.DOQuantity = externalPurchaseOrderItem.DOQuantity - detail.DOQuantity + modelDetail.DOQuantity;
                                                    modelDetail.POItemId = (int)internalPurchaseOrderItem.Id;
                                                    modelDetail.PRItemId = internalPurchaseOrderItem.GPRItemId;
                                                    modelDetail.UnitId   = internalPurchaseOrder.UnitId;
                                                    modelDetail.UnitCode = internalPurchaseOrder.UnitCode;

                                                    modelDetail.QuantityCorrection         = modelDetail.DOQuantity;
                                                    modelDetail.PricePerDealUnitCorrection = modelDetail.PricePerDealUnit;
                                                    modelDetail.PriceTotalCorrection       = modelDetail.PriceTotal;
                                                }

                                                if (externalPurchaseOrderItem.ReceiptQuantity == 0)
                                                {
                                                    if (externalPurchaseOrderItem.DOQuantity == 0)
                                                    {
                                                        GarmentPurchaseRequestItem purchaseRequestItem = this.dbContext.GarmentPurchaseRequestItems.FirstOrDefault(s => s.Id.Equals(modelDetail.PRItemId));
                                                        purchaseRequestItem.Status       = "Sudah diorder ke Supplier";
                                                        internalPurchaseOrderItem.Status = "Sudah diorder ke Supplier";
                                                    }
                                                    else if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity < externalPurchaseOrderItem.DealQuantity)
                                                    {
                                                        internalPurchaseOrderItem.Status = "Barang sudah datang parsial";
                                                    }
                                                    else if (externalPurchaseOrderItem.DOQuantity > 0 && externalPurchaseOrderItem.DOQuantity >= externalPurchaseOrderItem.DealQuantity)
                                                    {
                                                        internalPurchaseOrderItem.Status = "Barang Sudah Datang Semua";
                                                    }
                                                }

                                                EntityExtension.FlagForUpdate(modelDetail, user, USER_AGENT);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        dbSet.Update(m);

                        Updated = await dbContext.SaveChangesAsync();

                        transaction.Commit();
                    }
                    else
                    {
                        throw new Exception("Invalid Id");
                    }
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(Updated);
        }