public async Task <ActionResult> Post([FromBody] GarmentInvoicePaymentViewModel viewModel)
        {
            try
            {
                VerifyUser();

                ValidateService.Validate(viewModel);
                GarmentInvoicePaymentModel model = Mapper.Map <GarmentInvoicePaymentModel>(viewModel);
                await Service.CreateAsync(model);

                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.CREATED_STATUS_CODE, General.OK_MESSAGE)
                    .Ok();
                return(Created(String.Concat(Request.Path, "/", 0), Result));
            }
            catch (ServiceValidationException e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.BAD_REQUEST_STATUS_CODE, General.BAD_REQUEST_MESSAGE)
                    .Fail(e);
                return(BadRequest(Result));
            }
            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));
            }
        }
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] GarmentInvoicePaymentViewModel viewModel)
        {
            try
            {
                VerifyUser();

                ValidateService.Validate(viewModel);
                GarmentInvoicePaymentModel model = Mapper.Map <GarmentInvoicePaymentModel>(viewModel);
                await Service.UpdateAsync(id, model);

                return(NoContent());
            }
            catch (ServiceValidationException e)
            {
                Dictionary <string, object> Result =
                    new ResultFormatter(ApiVersion, General.BAD_REQUEST_STATUS_CODE, General.BAD_REQUEST_MESSAGE)
                    .Fail(e);
                return(BadRequest(Result));
            }
            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));
            }
        }
Example #3
0
        public async Task <GarmentInvoicePaymentModel> GetTestData()
        {
            GarmentInvoicePaymentModel model = GetNewData();
            await Service.CreateAsync(model);

            return(await Service.ReadByIdAsync(model.Id));
        }
        public void Should_Success_Map_Garment_Invoice_Payment()
        {
            var mapper = new MapperConfiguration(configuration => configuration.AddProfile <GarmentInvoicePaymentProfile>()).CreateMapper();
            var model  = new GarmentInvoicePaymentModel();
            var vm     = mapper.Map <GarmentInvoicePaymentViewModel>(model);

            Assert.True(true);
        }
        public async Task Should_Success_Create_Data()
        {
            GarmentInvoicePaymentService service = new GarmentInvoicePaymentService(GetServiceProvider().Object, _dbContext(GetCurrentMethod()));
            GarmentInvoicePaymentModel   model   = _dataUtil(service, GetCurrentMethod()).GetNewData();
            var Response = await service.CreateAsync(model);

            Assert.NotEqual(0, Response);
        }
        public async Task Should_Success_Get_Data_By_Id()
        {
            GarmentInvoicePaymentService service = new GarmentInvoicePaymentService(GetServiceProvider().Object, _dbContext(GetCurrentMethod()));
            GarmentInvoicePaymentModel   model   = await _dataUtil(service, GetCurrentMethod()).GetTestData();

            var Response = await service.ReadByIdAsync(model.Id);

            Assert.NotNull(Response);
        }
        public async Task <int> CreateAsync(GarmentInvoicePaymentModel model)
        {
            model.InvoicePaymentNo = await GenerateNo(model, 7);

            EntityExtension.FlagForCreate(model, IdentityService.Username, UserAgent);
            foreach (var item in model.Items)
            {
                EntityExtension.FlagForCreate(item, IdentityService.Username, UserAgent);
            }
            DbSet.Add(model);
            return(await DbContext.SaveChangesAsync());
        }
        public async Task <int> DeleteAsync(int id)
        {
            var existingModel = DbSet
                                .Include(d => d.Items)
                                .Single(x => x.Id == id && !x.IsDeleted);
            GarmentInvoicePaymentModel model = await ReadByIdAsync(id);

            foreach (var item in model.Items)
            {
                EntityExtension.FlagForDelete(item, IdentityService.Username, UserAgent, true);
            }
            EntityExtension.FlagForDelete(model, IdentityService.Username, UserAgent, true);
            DbSet.Update(model);

            return(await DbContext.SaveChangesAsync());
        }
        public async Task <int> UpdateAsync(int id, GarmentInvoicePaymentModel model)
        {
            GarmentInvoicePaymentModel exist = DbSet
                                               .Include(d => d.Items)
                                               .Single(dispo => dispo.Id == id && !dispo.IsDeleted);

            exist.PaymentDate  = model.PaymentDate;
            exist.BuyerId      = model.BuyerId;
            exist.BuyerCode    = model.BuyerCode;
            exist.BuyerName    = model.BuyerName;
            exist.BGNo         = model.BGNo;
            exist.CurrencyCode = model.CurrencyCode;
            exist.CurrencyId   = model.CurrencyId;
            exist.CurrencyRate = model.CurrencyRate;
            exist.Remark       = model.Remark;

            foreach (var item in exist.Items)
            {
                GarmentInvoicePaymentItemModel itemModel = model.Items.FirstOrDefault(prop => prop.Id.Equals(item.Id));

                if (itemModel == null)
                {
                    EntityExtension.FlagForDelete(item, IdentityService.Username, UserAgent, true);
                }
                else
                {
                    EntityExtension.FlagForUpdate(item, IdentityService.Username, UserAgent);
                }
            }
            foreach (var newItem in model.Items)
            {
                if (newItem.Id == 0)
                {
                    exist.Items.Add(newItem);
                    EntityExtension.FlagForCreate(newItem, IdentityService.Username, UserAgent);
                }
            }


            EntityExtension.FlagForUpdate(exist, IdentityService.Username, UserAgent);

            return(await DbContext.SaveChangesAsync());
        }
        public async Task Should_Success_Update_Data()
        {
            GarmentInvoicePaymentService service = new GarmentInvoicePaymentService(GetServiceProvider().Object, _dbContext(GetCurrentMethod()));


            GarmentInvoicePaymentModel model = await _dataUtil(service, GetCurrentMethod()).GetTestData();

            var newModel = await service.ReadByIdAsync(model.Id);

            //newModel.BGCheckNumber = "newBG";
            var Response1 = await service.UpdateAsync(newModel.Id, newModel);

            Assert.NotEqual(0, Response1);

            GarmentInvoicePaymentModel model2 = await _dataUtil(service, GetCurrentMethod()).GetTestData();

            //var newModel2 = await service.ReadByIdAsync(model.Id);
            GarmentInvoicePaymentModel newModel2 = new GarmentInvoicePaymentModel();

            newModel2.Id = model2.Id;

            newModel2.Items = new List <GarmentInvoicePaymentItemModel> {
                model2.Items.First()
            };
            var Response = await service.UpdateAsync(model2.Id, newModel2);

            Assert.NotEqual(0, Response);

            GarmentInvoicePaymentItemModel newItem = new GarmentInvoicePaymentItemModel
            {
                InvoiceId = 3,
                InvoiceNo = "no3",
                Amount    = 3,
                IDRAmount = 3
            };

            newModel2.Items.Add(newItem);
            var Response3 = await service.UpdateAsync(model2.Id, newModel2);

            Assert.NotEqual(0, Response);
        }
        async Task <string> GenerateNo(GarmentInvoicePaymentModel model, int clientTimeZoneOffset)
        {
            DateTimeOffset Now   = model.PaymentDate;
            string         Year  = Now.ToOffset(new TimeSpan(clientTimeZoneOffset, 0, 0)).ToString("yyyy");
            string         Month = Now.ToOffset(new TimeSpan(clientTimeZoneOffset, 0, 0)).ToString("MM");

            string no      = $"/{Month}/E/{Year}";
            int    Padding = 3;

            var lastNo = await this.DbSet.Where(w => w.InvoicePaymentNo.EndsWith(no) && !w.IsDeleted).OrderByDescending(o => o.CreatedUtc).FirstOrDefaultAsync();

            no = $"{no}";

            if (lastNo == null)
            {
                return("1".PadLeft(Padding, '0') + no);
            }
            else
            {
                int lastNoNumber = int.Parse(lastNo.InvoicePaymentNo.Replace(no, "")) + 1;
                return(lastNoNumber.ToString().PadLeft(Padding, '0') + no);
            }
        }