예제 #1
0
        public async Task <ActionResult> UpdateDraftAsync(string id, [FromBody] PartDTO partDto, CancellationToken cancellationToken = default)
        {
            InitUserInfo();
            if (!AllowCreate && !AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PartFilterSpecification(partDto.Id, true);
            var rowCount   = await _partService.CountAsync(specFilter, cancellationToken);

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

            var part   = _mapper.Map <Part>(partDto);
            var result = await _partService.PatchDraft(part, cancellationToken);

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

            return(CreatedAtAction(nameof(GetIdAsync), new { id = id }, null));
        }
예제 #2
0
        public async Task <ActionResult> UpdateAsync([FromBody] PartDTO part, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PartFilterSpecification(part.Id, true);
            var rowCount   = await _partService.CountAsync(specFilter, cancellationToken);

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

            // bind to old item


            var objItem = _mapper.Map <Part>(part);

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

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

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

            return(CreatedAtAction(nameof(GetIdAsync), new { id = objItem.Id }, null));
        }
예제 #3
0
        private async Task <Part> GetByIdAsync(string id, bool includeChilds = false, CancellationToken cancellationToken = default)
        {
            var specFilter = new PartFilterSpecification(id, true);
            var part       = await _unitOfWork.PartRepository.FirstOrDefaultAsync(specFilter, cancellationToken);

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



            return(part);
        }
예제 #4
0
        public async Task <string> GenerateExcel(string excelFilename, int?refId = null,
                                                 string id = "", List <string> partNames = null, List <string> descriptions = null,
                                                 Dictionary <string, int> exact      = null,
                                                 CancellationToken cancellationToken = default)
        {
            try
            {
                PartFilterSpecification filterSpec = null;
                if (!string.IsNullOrEmpty(id))
                {
                    filterSpec = new PartFilterSpecification(id);
                }
                else
                {
                    filterSpec = new PartFilterSpecification(exact)
                    {
                        Id           = id,
                        PartNames    = partNames,
                        Descriptions = descriptions
                    }
                }.BuildSpecification();

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

                cancellationToken.ThrowIfCancellationRequested();

                if (ExcelMapper.WriteToExcel <Part>(excelFilename, "part.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);
            }
예제 #5
0
        public async Task <List <PurchaseOrderDetail> > UploadExcel(string tempExcelFile, int parent_id, CancellationToken cancellationToken = default)
        {
            var result = new List <PurchaseOrderDetail>();

            // convert dari excel menjadi dictionary
            var resultDictionary = ExcelToDictionary(tempExcelFile, true);

            if (resultDictionary?.Count <= 0)
            {
                return(null);
            }

            // field pada excel: Part Number, Qty, Price, Sub Total
            // ambil daftar part number untuk diambi objeknya
            List <string> partNumbers = new List <string>();

            foreach (var row in resultDictionary)
            {
                if (row["Part Number"] == DBNull.Value)
                {
                    continue;
                }
                partNumbers.Add(row["Part Number"].ToString());
            }

            // ambil daftar part yang ada di excel
            var partSpecification = new PartFilterSpecification()
            {
                Ids = partNumbers
            }.BuildSpecification();
            var parts = await _unitOfWork.PartRepository.ListAsync(partSpecification, null, cancellationToken);

            if (parts == null)
            {
                AddError("Data part tidak ada yang ditemukan. Periksa kembali Part Number yang dimasukkan.");
                return(null);
            }

            // bind informasi excel ke dalam objek PurchaseOrderDetail, termasuk objek part
            // var parent = await _unitOfWork.PurchaseOrderRepository.GetByIdAsync(parent_id, cancellationToken);
            foreach (var row in resultDictionary)
            {
                var    part      = parts.Where(e => e.Id == row["Part Number"].ToString()).FirstOrDefault();
                double partPrice = (row["Price"] == DBNull.Value) ? 0 : (double)row["Price"];
                int    partQty   = (row["Qty"] == DBNull.Value) ? 0 : Convert.ToInt32(row["Qty"]);
                double partTotal = (row["Sub Total"] == DBNull.Value) ? 0 : (double)row["Sub Total"];
                var    poDetail  = new PurchaseOrderDetail(part.Id, partPrice, partQty, partTotal, null)
                {
                    Part            = part,
                    PurchaseOrderId = parent_id
                };
                result.Add(poDetail);
            }

            // set flag upload & draft
            SetUploadDraftFlags(result);

            // validasi master data part
            await RunMasterDataValidation(result, cancellationToken);

            // save ke database
            foreach (var item in result)
            {
                var id = item.Id;
                if (id > 0)
                {
                    await _unitOfWork.PurchaseOrderDetailRepository.UpdateAsync(item, cancellationToken);
                }
                else
                {
                    await _unitOfWork.PurchaseOrderDetailRepository.AddAsync(item, cancellationToken);
                }
            }
            await _unitOfWork.CommitAsync();

            return(result);
        }