Esempio n. 1
0
        public IActionResult AddInvoice([FromBody] DTOs.Invoice invoiceForCreate)
        {
            Entities.Invoice invoice;
            List <Entities.InvoiceDetail> invoiceDetails;

            try
            {
                invoice        = _mapper.Map <Entities.Invoice>(invoiceForCreate);
                invoice.UserId = "preicher";


                var invoiceToAdd = _invoiceRepository.AddInvoice(invoice);
                invoiceDetails = invoiceForCreate.InvoiceDetails.Select(d => new Entities.InvoiceDetail(invoiceToAdd.InvoiceId, "preicher", d)).ToList();
                invoiceDetails.ForEach(invDtl => _invoiceRepository.AddInvoiceDetail(invDtl));
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return(StatusCode(500));
            }
            var InvoiceToSend = _mapper.Map <DTOs.Invoice>(invoice);

            foreach (var itm in invoiceDetails)
            {
                InvoiceToSend.InvoiceDetails.Add(new DTOs.InvoiceDetail(itm.budgetCategoryId, itm.budgetId, itm.InvAmt));
            }

            InvoiceToSend.DocCnt = 0;
            return(Ok(InvoiceToSend));
        }
Esempio n. 2
0
        public async Task <IActionResult> UpdateInvoice(Guid invoiceId, [FromBody] DTOs.Invoice invoiceForUpdate)
        {
            var invoiceFromRepo = await _invoiceRepository.GetInvoiceByIdAsync(invoiceId);

            if (invoiceFromRepo == null)
            {
                return(BadRequest());
            }

            var invoiceToUpdate = _mapper.Map(invoiceForUpdate, invoiceFromRepo);

            var invoiceDetailsForUpdate = invoiceForUpdate.InvoiceDetails.Select(d => new Entities.InvoiceDetail(invoiceId, "preicher", d)).ToList();

            try
            {
                invoiceToUpdate = _invoiceRepository.UpdateInvoice(invoiceToUpdate);

                var invDetlsFromRepo = await _invoiceRepository.GetInvoiceDetailsByInvoiceIdAsync(invoiceId);

                var invDetlsToAdd = invoiceDetailsForUpdate.Where(item => !invDetlsFromRepo.Any(item2 => item2.invoiceDetailId == item.invoiceDetailId)).ToList();

                var invDetlsToDelete = invDetlsFromRepo.Where(item => !invoiceDetailsForUpdate.Any(item2 => item2.invoiceDetailId == item.invoiceDetailId)).ToList();
                var invDetlsToUpdate = (from indFromUpdate in invoiceForUpdate.InvoiceDetails
                                        join indFromRepo in invDetlsFromRepo on
                                        indFromUpdate.invoiceDetailId equals indFromRepo.invoiceDetailId
                                        where indFromUpdate.InvAmt != indFromRepo.InvAmt
                                        select _mapper.Map(indFromUpdate, indFromRepo)).ToList();

                invDetlsToDelete.ForEach(invDtl => _invoiceRepository.DeleteInvoiceDetail(invDtl));
                invDetlsToAdd.ForEach(invDtl => _invoiceRepository.AddInvoiceDetail(invDtl));
                invDetlsToUpdate.ForEach(invDtl => _invoiceRepository.UpdateInvoiceDetail(invDtl));
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return(StatusCode(500));
            }
            var InvoiceToSend = _mapper.Map <DTOs.Invoice>(invoiceToUpdate);

            foreach (var itm in invoiceDetailsForUpdate)
            {
                InvoiceToSend.InvoiceDetails.Add(new DTOs.InvoiceDetail(itm.budgetCategoryId, itm.budgetId, itm.InvAmt)); //(int budgetCategoryId, int? budgetId, decimal InvAmt)
            }
            InvoiceToSend.DocCnt = await _fileUploads.GetUploadCountBySrcIdAsync(invoiceForUpdate.InvoiceId ?? Guid.Empty);

            return(Ok(InvoiceToSend));
        }