private Tuple <string, int> GetDocumentNo(VBRequestDocumentWithPOFormDto form, VBRequestDocumentModel existingData)
        {
            var now   = form.Date.GetValueOrDefault().AddHours(_identityService.TimezoneOffset);
            var year  = now.ToString("yy");
            var month = now.ToString("MM");


            //var unit = model.UnitCode.ToString().Split(" - ");

            var unitCode = "T";

            if (form.SuppliantUnit.Division.Name.ToUpper() == "GARMENT")
            {
                unitCode = "G";
            }

            var documentNo = $"VB-{unitCode}-{month}{year}-";

            var index = 1;

            if (existingData != null)
            {
                index = existingData.Index + 1;
            }

            documentNo += string.Format("{0:000}", index);

            return(new Tuple <string, int>(documentNo, index));
        }
        public int UpdateWithPO(int id, VBRequestDocumentWithPOFormDto form)
        {
            var header = _dbContext.VBRequestDocuments.FirstOrDefault(entity => entity.Id == id);

            header.UpdateFromForm(form);
            EntityExtension.FlagForUpdate(header, _identityService.Username, UserAgent);
            _dbContext.Update(header);
            _dbContext.SaveChanges();

            UpdateWithPOEPODetail(id, form.Items);
            return(id);
        }
        public int CreateWithPO(VBRequestDocumentWithPOFormDto form)
        {
            var unitCode     = GetDocumentUnitCode(form.SuppliantUnit.Division.Name.ToUpper(), form.IsInklaring);
            var existingData = _dbContext.VBRequestDocuments.Where(a => a.Date.AddHours(_identityService.TimezoneOffset).Month == form.Date.GetValueOrDefault().AddHours(_identityService.TimezoneOffset).Month&& a.DocumentNo.StartsWith(unitCode)).OrderByDescending(s => s.Index).FirstOrDefault();
            var documentNo   = GetDocumentNo(form, existingData);

            var model = new VBRequestDocumentModel(
                documentNo.Item1,
                form.Date.GetValueOrDefault(),
                form.RealizationEstimationDate.GetValueOrDefault(),
                form.SuppliantUnit.Id.GetValueOrDefault(),
                form.SuppliantUnit.Code,
                form.SuppliantUnit.Name,
                form.SuppliantUnit.Division.Id.GetValueOrDefault(),
                form.SuppliantUnit.Division.Code,
                form.SuppliantUnit.Division.Name,
                form.Currency.Id.GetValueOrDefault(),
                form.Currency.Code,
                form.Currency.Symbol,
                form.Currency.Description,
                form.Currency.Rate.GetValueOrDefault(),
                form.Purpose,
                form.Amount.GetValueOrDefault(),
                false,
                false,
                VBType.WithPO,
                documentNo.Item2,
                form.IsInklaring,
                null, // NoBL
                null, // NoPO
                form.TypePurchasing
                );

            EntityExtension.FlagForCreate(model, _identityService.Username, UserAgent);
            _dbContext.VBRequestDocuments.Add(model);
            _dbContext.SaveChanges();

            AddItems(model.Id, form.Items);

            return(model.Id);
        }