コード例 #1
0
        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)
                    {
                        GarmentInvoice garmentInvoice = this.dbContext.GarmentInvoices.FirstOrDefault(s => s.Id == item.InvoiceId);
                        if (garmentInvoice != null)
                        {
                            garmentInvoice.HasInternNote = true;
                        }
                        EntityExtension.FlagForCreate(item, user, USER_AGENT);
                        foreach (var detail in item.Details)
                        {
                            GarmentDeliveryOrder         garmentDeliveryOrder  = this.dbContext.GarmentDeliveryOrders.FirstOrDefault(s => s.Id == detail.DOId);
                            GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.RONo.Equals(detail.RONo));
                            if (internalPurchaseOrder != null)
                            {
                                detail.UnitId   = internalPurchaseOrder.UnitId;
                                detail.UnitCode = internalPurchaseOrder.UnitCode;
                                detail.UnitName = internalPurchaseOrder.UnitName;
                            }
                            if (garmentDeliveryOrder != null)
                            {
                                garmentDeliveryOrder.InternNo = m.INNo;
                            }
                            EntityExtension.FlagForCreate(detail, user, USER_AGENT);

                            await _garmentDebtBalanceService.UpdateFromInternalNote((int)detail.DOId, new InternalNoteFormDto((int)m.Id, m.INNo));
                        }
                    }

                    this.dbSet.Add(m);

                    Created = await dbContext.SaveChangesAsync();

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

            return(Created);
        }
        public IActionResult Get(int id)
        {
            try
            {
                var indexAcceptPdf = Request.Headers["Accept"].ToList().IndexOf("application/pdf");

                GarmentExternalPurchaseOrder          model     = facade.ReadById(id);
                GarmentExternalPurchaseOrderViewModel viewModel = mapper.Map <GarmentExternalPurchaseOrderViewModel>(model);
                if (viewModel == null)
                {
                    throw new Exception("Invalid Id");
                }
                if (indexAcceptPdf < 0)
                {
                    viewModel.IsUnpost = facade.GetIsUnpost(id);
                    return(Ok(new
                    {
                        apiVersion = ApiVersion,
                        statusCode = General.OK_STATUS_CODE,
                        message = General.OK_MESSAGE,
                        data = viewModel,
                    }));
                }
                else
                {
                    int clientTimeZoneOffset   = int.Parse(Request.Headers["x-timezone-offset"].First());
                    SupplierViewModel supplier = facade.GetSupplier(viewModel.Supplier.Id);

                    viewModel.Supplier = supplier == null? viewModel.Supplier : supplier;
                    foreach (var item in viewModel.Items)
                    {
                        GarmentInternalPurchaseOrder ipo = IPOfacade.ReadById(item.POId);
                        item.ShipmentDate = ipo == null ? item.ShipmentDate : ipo.ShipmentDate;
                        GarmentProductViewModel product = facade.GetProduct(item.Product.Id);
                        item.Product = product == null ? item.Product : product;
                    }

                    GarmentExternalPurchaseOrderPDFTemplate PdfTemplateLocal = new GarmentExternalPurchaseOrderPDFTemplate();
                    MemoryStream stream = PdfTemplateLocal.GeneratePdfTemplate(viewModel, clientTimeZoneOffset);

                    return(new FileStreamResult(stream, "application/pdf")
                    {
                        FileDownloadName = $"{viewModel.EPONo}.pdf"
                    });
                }
            }
            catch (Exception e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.INTERNAL_ERROR_STATUS_CODE, e.Message)
                    .Fail();
                return(StatusCode(General.INTERNAL_ERROR_STATUS_CODE, Result));
            }
        }
コード例 #3
0
        public async void Should_Success_Get_Data_By_Tags()
        {
            GarmentInternalPurchaseOrderFacade facade = new GarmentInternalPurchaseOrderFacade(_dbContext(GetCurrentMethod()));
            var model = await dataUtil(facade, GetCurrentMethod()).GetTestData();

            GarmentInternalPurchaseOrder data = model.FirstOrDefault();
            var Response = facade.ReadByTags("Accessories", $"#{data.UnitName} #{data.BuyerName}", data.ShipmentDate.AddDays(-1), data.ShipmentDate.AddDays(1));

            Assert.NotNull(Response);

            var ResponseWhiteSpace = facade.ReadByTags("Accessories", "", DateTimeOffset.MinValue, DateTimeOffset.MinValue);

            Assert.NotNull(ResponseWhiteSpace);
        }
        public async Task <int> Split(int id, GarmentInternalPurchaseOrder model, string user, int clientTimeZoneOffset = 7)
        {
            int Splited = 0;

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var oldModel = dbSet.SingleOrDefault(m => m.Id == id);

                    EntityExtension.FlagForUpdate(oldModel, user, USER_AGENT);
                    foreach (var oldItem in oldModel.Items)
                    {
                        EntityExtension.FlagForUpdate(oldItem, user, USER_AGENT);
                        var newQuantity = model.Items.Single(i => i.Id == oldItem.Id).Quantity;
                        oldItem.Quantity -= newQuantity;
                    }

                    model.Id = 0;
                    foreach (var item in model.Items)
                    {
                        item.Id = 0;
                    }

                    EntityExtension.FlagForCreate(model, user, USER_AGENT);
                    do
                    {
                        model.PONo = CodeGenerator.Generate();
                    }while (dbSet.Any(m => m.PONo.Equals(model.PONo)));

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

                    dbSet.Add(model);

                    Splited = await dbContext.SaveChangesAsync();

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

            return(Splited);
        }
コード例 #5
0
        public int EPOCancel(int id, string user)
        {
            int Updated = 0;

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

                    EntityExtension.FlagForUpdate(m, user, "Facade");
                    m.IsCanceled = true;

                    foreach (var item in m.Items)
                    {
                        GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id == item.POId);
                        internalPurchaseOrder.IsPosted = false;

                        GarmentInternalPurchaseOrderItem IPOItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(item.POId));

                        if (item.ProductId.ToString() == IPOItem.ProductId)
                        {
                            IPOItem.Status = "Dibatalkan";
                        }

                        var ipoItems = this.dbContext.GarmentInternalPurchaseOrderItems.Where(a => a.GPRItemId.Equals(IPOItem.GPRItemId) && a.ProductId.Equals(item.ProductId.ToString())).ToList();
                        //returning Values
                        foreach (var a in ipoItems)
                        {
                            a.RemainingBudget += item.UsedBudget;
                        }

                        EntityExtension.FlagForUpdate(item, user, "Facade");
                    }

                    Updated = dbContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(Updated);
        }
        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);
        }
コード例 #7
0
        public int EPOClose(int id, string user)
        {
            int Updated = 0;

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

                    EntityExtension.FlagForUpdate(m, user, "Facade");
                    m.IsClosed = true;

                    foreach (var item in m.Items)
                    {
                        GarmentInternalPurchaseOrder IPO = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(a => a.Id.Equals(item.POId));

                        IPO.IsClosed = true;

                        EntityExtension.FlagForUpdate(item, user, "Facade");
                    }

                    Updated = dbContext.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(Updated);
        }
コード例 #8
0
        public async Task <int> Create(GarmentExternalPurchaseOrder m, string user, int clientTimeZoneOffset = 7)
        {
            int Created = 0;

            using (var transaction = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    foreach (var item in m.Items)
                    {
                        if (item.IsOverBudget)
                        {
                            m.IsOverBudget = true;
                            break;
                        }
                    }
                    m.EPONo = await GenerateNo(m, clientTimeZoneOffset);

                    EntityExtension.FlagForCreate(m, user, USER_AGENT);

                    foreach (var item in m.Items)
                    {
                        GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(item.POId));
                        internalPurchaseOrder.IsPosted = true;

                        GarmentInternalPurchaseOrderItem IPOItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(item.POId));

                        if (item.ProductId.ToString() == IPOItem.ProductId)
                        {
                            IPOItem.Status = "Sudah dibuat PO Eksternal";
                        }

                        if ((m.PaymentMethod == "CMT" || m.PaymentMethod == "FREE FROM BUYER") && (m.PaymentType == "FREE" || m.PaymentType == "EX MASTER FREE"))
                        {
                            item.UsedBudget = 0;
                        }
                        else
                        {
                            var ipoItems = this.dbContext.GarmentInternalPurchaseOrderItems.Where(a => a.GPRItemId.Equals(IPOItem.GPRItemId) && a.ProductId.Equals(item.ProductId.ToString())).ToList();

                            foreach (var a in ipoItems)
                            {
                                a.RemainingBudget -= item.UsedBudget;
                            }
                        }

                        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);
        }
コード例 #9
0
        public async Task <int> Update(int id, GarmentExternalPurchaseOrder 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)
                               .SingleOrDefault(pr => pr.Id == id && !pr.IsDeleted);

                    m.IsOverBudget = false;
                    foreach (var item in m.Items)
                    {
                        if (item.IsOverBudget)
                        {
                            m.IsOverBudget = true;
                            break;
                        }
                    }

                    if (oldM != null && oldM.Id == id)
                    {
                        EntityExtension.FlagForUpdate(m, user, USER_AGENT);
                        foreach (var Olditem in oldM.Items)
                        {
                            GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(Olditem.POId));
                            //internalPurchaseOrder.IsPosted = true;

                            GarmentInternalPurchaseOrderItem IPOItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(Olditem.POId));

                            var ipoItems = this.dbContext.GarmentInternalPurchaseOrderItems.Where(a => a.GPRItemId.Equals(IPOItem.GPRItemId) && a.ProductId.Equals(Olditem.ProductId.ToString())).ToList();
                            //returning Values
                            foreach (var a in ipoItems)
                            {
                                a.RemainingBudget += Olditem.UsedBudget;
                            }
                        }

                        foreach (var item in m.Items)
                        {
                            if (item.Id == 0)
                            {
                                GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(item.POId));
                                internalPurchaseOrder.IsPosted = true;

                                GarmentInternalPurchaseOrderItem IPOItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(item.POId));

                                if (item.ProductId.ToString() == IPOItem.ProductId)
                                {
                                    IPOItem.Status = "Sudah dibuat PO Eksternal";
                                }

                                if ((m.PaymentMethod == "CMT" || m.PaymentMethod == "FREE FROM BUYER") && (m.PaymentType == "FREE" || m.PaymentType == "EX MASTER FREE"))
                                {
                                    item.UsedBudget = 0;
                                }
                                else
                                {
                                    var ipoItems = this.dbContext.GarmentInternalPurchaseOrderItems.Where(a => a.GPRItemId.Equals(IPOItem.GPRItemId) && a.ProductId.Equals(item.ProductId.ToString())).ToList();

                                    foreach (var a in ipoItems)
                                    {
                                        a.RemainingBudget -= item.UsedBudget;
                                    }
                                }


                                EntityExtension.FlagForCreate(item, user, USER_AGENT);
                            }
                            else
                            {
                                GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(item.POId));
                                internalPurchaseOrder.IsPosted = true;

                                GarmentInternalPurchaseOrderItem IPOItem = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(item.POId));

                                var ipoItems = this.dbContext.GarmentInternalPurchaseOrderItems.Where(a => a.GPRItemId.Equals(IPOItem.GPRItemId) && a.ProductId.Equals(item.ProductId.ToString())).ToList();
                                if ((m.PaymentMethod == "CMT" || m.PaymentMethod == "FREE FROM BUYER") && (m.PaymentType == "FREE" || m.PaymentType == "EX MASTER FREE"))
                                {
                                    item.UsedBudget = 0;
                                }
                                else
                                {
                                    foreach (var a in ipoItems)
                                    {
                                        a.RemainingBudget -= item.UsedBudget;
                                    }
                                }
                                EntityExtension.FlagForUpdate(item, user, USER_AGENT);
                            }
                        }

                        dbSet.Update(m);

                        foreach (var oldItem in oldM.Items)
                        {
                            var newItem = m.Items.FirstOrDefault(i => i.Id.Equals(oldItem.Id));
                            if (newItem == null)
                            {
                                GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.Id.Equals(oldItem.POId));
                                internalPurchaseOrder.IsPosted = false;

                                GarmentInternalPurchaseOrderItem IPOItems = this.dbContext.GarmentInternalPurchaseOrderItems.FirstOrDefault(a => a.GPOId.Equals(oldItem.POId));

                                if (oldItem.ProductId.ToString() == IPOItems.ProductId)
                                {
                                    IPOItems.Status = "PO Internal belum diorder";
                                }

                                EntityExtension.FlagForDelete(oldItem, user, USER_AGENT);
                                dbContext.GarmentExternalPurchaseOrderItems.Update(oldItem);
                            }
                        }

                        Updated = await dbContext.SaveChangesAsync();

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

            return(Updated);
        }
コード例 #10
0
 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;
     VatTax          = new VatTaxDto(entity.VatId, entity.VatRate);
     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;
 }
コード例 #11
0
        public async Task <int> Update(int id, GarmentInternNote m, string user, int clientTimeZoneOffset = 7)
        {
            int Updated = 0;

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    if (m.Items != null)
                    {
                        double         total     = 0;
                        HashSet <long> detailIds = GetGarmentInternNoteId(id);
                        foreach (var itemId in detailIds)
                        {
                            GarmentInternNoteItem data = m.Items.FirstOrDefault(prop => prop.Id.Equals(itemId));
                            if (data == null)
                            {
                                GarmentInternNoteItem dataItem = dbContext.GarmentInternNoteItems.FirstOrDefault(prop => prop.Id.Equals(itemId));
                                EntityExtension.FlagForDelete(dataItem, user, USER_AGENT);
                                var            Details         = dbContext.GarmentInternNoteDetails.Where(prop => prop.GarmentItemINId.Equals(itemId)).ToList();
                                GarmentInvoice garmentInvoices = dbContext.GarmentInvoices.FirstOrDefault(s => s.Id.Equals(dataItem.InvoiceId));
                                if (garmentInvoices != null)
                                {
                                    garmentInvoices.HasInternNote = false;
                                }
                                foreach (GarmentInternNoteDetail detail in Details)
                                {
                                    GarmentDeliveryOrder garmentDeliveryOrder = this.dbContext.GarmentDeliveryOrders.FirstOrDefault(s => s.Id == detail.DOId);
                                    garmentDeliveryOrder.InternNo = null;

                                    EntityExtension.FlagForDelete(detail, user, USER_AGENT);
                                }
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(data, user, USER_AGENT);
                            }

                            foreach (GarmentInternNoteItem item in m.Items)
                            {
                                total += item.TotalAmount;
                                if (item.Id <= 0)
                                {
                                    GarmentInvoice garmentInvoice = this.dbContext.GarmentInvoices.FirstOrDefault(s => s.Id == item.InvoiceId);
                                    if (garmentInvoice != null)
                                    {
                                        garmentInvoice.HasInternNote = true;
                                    }
                                    EntityExtension.FlagForCreate(item, user, USER_AGENT);
                                }
                                else
                                {
                                    EntityExtension.FlagForUpdate(item, user, USER_AGENT);
                                }

                                foreach (GarmentInternNoteDetail detail in item.Details)
                                {
                                    if (item.Id <= 0)
                                    {
                                        GarmentDeliveryOrder         garmentDeliveryOrder  = this.dbContext.GarmentDeliveryOrders.FirstOrDefault(s => s.Id == detail.DOId);
                                        GarmentInternalPurchaseOrder internalPurchaseOrder = this.dbContext.GarmentInternalPurchaseOrders.FirstOrDefault(s => s.RONo == detail.RONo);
                                        if (internalPurchaseOrder != null)
                                        {
                                            detail.UnitId   = internalPurchaseOrder.UnitId;
                                            detail.UnitCode = internalPurchaseOrder.UnitCode;
                                            detail.UnitName = internalPurchaseOrder.UnitName;
                                        }
                                        if (garmentDeliveryOrder != null)
                                        {
                                            garmentDeliveryOrder.InternNo = m.INNo;
                                        }
                                        EntityExtension.FlagForCreate(detail, user, USER_AGENT);
                                    }
                                    else
                                    {
                                        EntityExtension.FlagForUpdate(detail, user, USER_AGENT);
                                    }
                                }
                            }
                        }
                    }
                    EntityExtension.FlagForUpdate(m, user, USER_AGENT);
                    this.dbSet.Update(m);
                    Updated = await dbContext.SaveChangesAsync();

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

            return(Updated);
        }
        public bool CheckDuplicate(GarmentInternalPurchaseOrder model)
        {
            var countPOIntByPRAndRefNo = dbSet.Count(m => m.PRNo == model.PRNo && m.Items.Any(i => i.PO_SerialNumber == model.Items.Single().PO_SerialNumber));

            return(countPOIntByPRAndRefNo > 1);
        }
コード例 #13
0
        public List <GarmentInternalPurchaseOrder> ReadByTags(string tags, DateTimeOffset shipmentDateFrom, DateTimeOffset shipmentDateTo)
        {
            IQueryable <GarmentPurchaseRequest> Models = this.dbSet.AsQueryable();

            if (shipmentDateFrom != DateTimeOffset.MinValue && shipmentDateTo != DateTimeOffset.MinValue)
            {
                Models = Models.Where(m => m.ShipmentDate >= shipmentDateFrom && m.ShipmentDate <= shipmentDateTo);
            }

            string[] stringKeywords = new string[3];

            if (tags != null)
            {
                List <string> Keywords = new List <string>();

                if (tags.Contains("#"))
                {
                    Keywords = tags.Split("#").ToList();
                    Keywords.RemoveAt(0);
                    Keywords = Keywords.Take(stringKeywords.Length).ToList();
                }
                else
                {
                    Keywords.Add(tags);
                }

                for (int n = 0; n < Keywords.Count; n++)
                {
                    stringKeywords[n] = Keywords[n].Trim().ToLower();
                }
            }

            Models = Models
                     .Where(m =>
                            (string.IsNullOrWhiteSpace(stringKeywords[0]) || m.UnitName.ToLower().Contains(stringKeywords[0])) &&
                            (string.IsNullOrWhiteSpace(stringKeywords[1]) || m.BuyerName.ToLower().Contains(stringKeywords[1])) &&
                            m.Items.Any(i => i.IsUsed == false) &&
                            m.IsUsed == false
                            )
                     .Select(m => new GarmentPurchaseRequest
            {
                Id                   = m.Id,
                Date                 = m.Date,
                PRNo                 = m.PRNo,
                RONo                 = m.RONo,
                BuyerId              = m.BuyerId,
                BuyerCode            = m.BuyerCode,
                BuyerName            = m.BuyerName,
                Article              = m.Article,
                ExpectedDeliveryDate = m.ExpectedDeliveryDate,
                ShipmentDate         = m.ShipmentDate,
                UnitId               = m.UnitId,
                UnitCode             = m.UnitCode,
                UnitName             = m.UnitName,
                Items                = m.Items
                                       .Where(i =>
                                              i.IsUsed == false &&
                                              (string.IsNullOrWhiteSpace(stringKeywords[2]) || i.CategoryName.ToLower().Contains(stringKeywords[2]))
                                              )
                                       .ToList()
            })
                     .Where(m => m.Items.Count > 0);

            var IPOModels = new List <GarmentInternalPurchaseOrder>();

            foreach (var model in Models)
            {
                foreach (var item in model.Items)
                {
                    var IPOModel = new GarmentInternalPurchaseOrder
                    {
                        PRId                 = model.Id,
                        PRDate               = model.Date,
                        PRNo                 = model.PRNo,
                        RONo                 = model.RONo,
                        BuyerId              = model.BuyerId,
                        BuyerCode            = model.BuyerCode,
                        BuyerName            = model.BuyerName,
                        Article              = model.Article,
                        ExpectedDeliveryDate = model.ExpectedDeliveryDate,
                        ShipmentDate         = model.ShipmentDate,
                        UnitId               = model.UnitId,
                        UnitCode             = model.UnitCode,
                        UnitName             = model.UnitName,
                        //IsPosted = false,
                        //IsClosed = false,
                        //Remark = "",
                        Items = new List <GarmentInternalPurchaseOrderItem>
                        {
                            new GarmentInternalPurchaseOrderItem
                            {
                                GPRItemId       = item.Id,
                                PO_SerialNumber = item.PO_SerialNumber,
                                ProductId       = item.ProductId,
                                ProductCode     = item.ProductCode,
                                ProductName     = item.ProductName,
                                Quantity        = item.Quantity,
                                BudgetPrice     = item.BudgetPrice,
                                UomId           = item.UomId,
                                UomUnit         = item.UomUnit,
                                CategoryId      = item.CategoryId,
                                CategoryName    = item.CategoryName,
                                ProductRemark   = item.ProductRemark,
                                //Status = "PO Internal belum diorder"
                            }
                        }
                    };
                    IPOModels.Add(IPOModel);
                }
            }

            return(IPOModels);
        }
        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);
        }