Exemplo n.º 1
0
        public async Task <ActionResult> UpdateDraftAsync(int id, [FromBody] PurchaseRequestDTO purchaseRequestDto, CancellationToken cancellationToken = default)
        {
            InitUserInfo();
            if (!AllowCreate && !AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PurchaseRequestFilterSpecification(int.Parse(purchaseRequestDto.Id), true);
            var rowCount   = await _purchaseRequestService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(PurchaseRequest), purchaseRequestDto.Id);
            }

            var purchaseRequest = _mapper.Map <PurchaseRequest>(purchaseRequestDto);
            var result          = await _purchaseRequestService.PatchDraft(purchaseRequest, cancellationToken);

            if (!result)
            {
                AssignToModelState(_purchaseRequestService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = id }, null));
        }
Exemplo n.º 2
0
        private async Task <PurchaseRequest> GetByIdAsync(int id, bool includeChilds = false, CancellationToken cancellationToken = default)
        {
            var specFilter      = new PurchaseRequestFilterSpecification(id, true);
            var purchaseRequest = await _unitOfWork.PurchaseRequestRepository.FirstOrDefaultAsync(specFilter, cancellationToken);

            if (purchaseRequest == null || includeChilds == false)
            {
                return(purchaseRequest);
            }

            var purchaseRequestDetailsFilter = new PurchaseRequestDetailFilterSpecification()
            {
                PurchaseRequestIds = new List <int>()
                {
                    id
                },
                ShowDraftList = BaseEntity.DraftStatus.All
            }.BuildSpecification();
            var purchaseRequestDetailss = await _unitOfWork.PurchaseRequestDetailRepository.ListAsync(purchaseRequestDetailsFilter, null, cancellationToken);

            purchaseRequest.AddRangePurchaseRequestDetails(purchaseRequestDetailss.ToList());


            return(purchaseRequest);
        }
Exemplo n.º 3
0
        public async Task <string> GenerateExcel(string excelFilename, int?refId = null,
                                                 int?id = null, DateTime?prDateFrom = null, DateTime?prDateTo = null, List <string> prNos = null, List <string> remarkss = null,
                                                 Dictionary <string, int> exact      = null,
                                                 CancellationToken cancellationToken = default)
        {
            try
            {
                PurchaseRequestFilterSpecification filterSpec = null;
                if (id.HasValue)
                {
                    filterSpec = new PurchaseRequestFilterSpecification(id.Value);
                }
                else
                {
                    filterSpec = new PurchaseRequestFilterSpecification(exact)
                    {
                        Id         = id,
                        PrDateFrom = prDateFrom,
                        PrDateTo   = prDateTo,
                        PrNos      = prNos,
                        Remarkss   = remarkss
                    }
                }.BuildSpecification();

                var results = await this.ListAsync(filterSpec, null, true, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();

                if (ExcelMapper.WriteToExcel <PurchaseRequest>(excelFilename, "purchaseRequest.json", results) == false)
                {
                    if (refId.HasValue)
                    {
                        await _downloadProcessService.FailedToGenerate(refId.Value, "Failed to generate excel file");
                    }
                    return("");
                }

                // update database information (if needed)
                if (refId.HasValue)
                {
                    excelFilename = Path.GetFileName(excelFilename);
                    await _downloadProcessService.SuccessfullyGenerated(refId.Value, excelFilename);
                }

                return(excelFilename);
            }
Exemplo n.º 4
0
        public async Task <ActionResult> UpdateAsync([FromBody] PurchaseRequestDTO purchaseRequest, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PurchaseRequestFilterSpecification(int.Parse(purchaseRequest.Id), true);
            var rowCount   = await _purchaseRequestService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(PurchaseRequest), purchaseRequest.Id);
            }

            // bind to old item
            foreach (var item in purchaseRequest.PurchaseRequestDetails)
            {
                if (!int.TryParse(item.Id, out _))
                {
                    item.Id = "0";
                }
                if (!int.TryParse(item.PurchaseRequestId, out _))
                {
                    item.PurchaseRequestId = "0";
                }
            }


            var objItem = _mapper.Map <PurchaseRequest>(purchaseRequest);

            // untuk data yang mereference object, perlu di set null agar tidak insert sebagai data baru
            CleanReferenceObject(objItem);

            var result = await _purchaseRequestService.UpdateAsync(objItem, cancellationToken);

            if (!result)
            {
                AssignToModelState(_purchaseRequestService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = objItem.Id }, null));
        }