Ejemplo n.º 1
0
        public static CreditNote ToEntity(this CreditNoteEditDto e)
        {
            if (e == null)
            {
                return(null);
            }

            var res = new CreditNote();

            res.Id          = e.Id;
            res.Description = e.Description;
            res.IdReceipt   = e.IdReceipt;
            return(res);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <CreditNoteDto> > Post([FromBody] CreditNoteEditDto value)
        {
            var res = await _service.Save(value);

            return(res);
        }
Ejemplo n.º 3
0
        public async Task <CreditNoteDto> Save(CreditNoteEditDto itemToEdit)
        {
            CreditNote res;

            if (itemToEdit.Id != Guid.Empty)
            {
                _logger.LogDebug($"Calling Update CreditNote for id=[{itemToEdit.Id}]");
                throw new Exception("Credit notes are not editable!!!");
            }
            else
            {
                var receiptData = await _dbCtx.PaymentReceipts.FindAsync(itemToEdit.IdReceipt);

                if (receiptData == null)
                {
                    throw new Exception($"Unable to find receipt with id {itemToEdit.IdReceipt}");
                }
                else if (string.IsNullOrWhiteSpace(receiptData.InvoiceNumber))
                {
                    throw new Exception($"The selected receipt has no invoce number for credit note generation ({itemToEdit.IdReceipt})");
                }

                var checkCreditNote = _dbCtx.CreditNotes.Where(x => x.IdReceipt == itemToEdit.IdReceipt).Count();
                if (checkCreditNote > 0)
                {
                    throw new Exception($"A credit note alredy exist for this receipt!");
                }

                res               = itemToEdit.ToEntity();
                res.Id            = Guid.NewGuid();
                res.IssuedBy      = new Guid(_claimPrincipal.Value.Identity.Name);
                res.InvoiceNumber = receiptData.InvoiceNumber;
                res.InvoiceDate   = receiptData.PaymentDate.Value;

                _logger.LogDebug($"Calling Insert CreditNote for id=[{res.Id}] (temp id, not created yet!)");
                await _dbCtx.CreditNotes.AddAsync(res);

                _dbCtx.SaveChanges();

                var creditNoteData = await Get(res.Id);

                if (!creditNoteData.Year.HasValue)
                {
                    creditNoteData.Year = DateTime.UtcNow.Year;
                }

                if (_appSettings.Value.UseSqLite)
                {
                    await CreateCreditNote(creditNoteData);
                }
                else
                {
                    await InvokeCreditNoteSP(creditNoteData, res);
                }

                //remove paid products
                var productInstances = await _dbCtx.CustomerProductInstances.Where(x => x.IdReceipt == receiptData.Id).ToArrayAsync();

                foreach (var pInstance in productInstances)
                {
                    pInstance.IdReceipt     = null;
                    pInstance.PaymentStatus = dal.Entities.PaymentStatus.Aborted;
                    _dbCtx.CustomerProductInstances.Update(pInstance);
                }

                //remove paid fees
                var customer = _dbCtx.Customers.Find(receiptData.IdCustomer);
                if (customer != null)
                {
                    var fees = receiptData.PaymentReceiptDetails.Where(x => x.ReceiptDetailType == dal.Entities.ReceiptDetailType.Fee);
                    foreach (var fee in fees)
                    {
                        var now = DateTime.UtcNow;
                        customer.MembershipLastPayDate = now;
                        if (customer.MembershipFeeExpiryDate.HasValue)
                        {
                            customer.MembershipFeeExpiryDate = customer.MembershipFeeExpiryDate.Value.AddYears(-1);
                            _dbCtx.Customers.Update(customer);
                        }
                    }
                }

                await _dbCtx.SaveChangesAsync();
            }
            return(res.ToDto());
        }