public async Task <(bool Success, string Error, int ReceiptId)> DuplicateClientReceiptByIdAsync(int receiptId, string userId) { var success = false; var error = ""; int duplicateReceiptId = 0; try { if (receiptId > 0) { var originalReceipt = await _clientReceiptRepository.FetchFullByIdAsync(receiptId); if (originalReceipt != null) { // duplicate receipt info var maxReceiptId = await _clientReceiptRepository.GetMaxReceiptIdAsync(); var receiptNumber = $"RE{DateTime.Now.ToString("yyyyMMdd")}{maxReceiptId + 1}"; var duplicateReceipt = new Models.ClientReceipt { CreatedUserId = userId, CreatedUtc = DateTime.UtcNow, ClientId = originalReceipt.ClientId, Date = DateTime.Now.Date, ReceiptNumber = receiptNumber, ClientIdentificationId = originalReceipt.ClientIdentificationId, PaymentDate = null, PaymentAmount = null, IsPaidInterestOnly = null, PaymentMethod = null }; // copy belongings var originalBelongings = originalReceipt.ClientBelongings; if (originalBelongings?.Any() == true) { foreach (var item in originalBelongings) { var duplicateBelonging = new ClientBelonging { CreatedUserId = userId, CreatedUtc = DateTime.UtcNow, FinalPrice = item.FinalPrice, ItemPrice = item.ItemPrice, ItemWeight = item.ItemWeight, KaratId = item.KaratId, KaratOther = item.KaratOther, MetalId = item.MetalId, MetalOther = item.MetalOther, ItemDescription = item.ItemDescription, ReplacementValue = item.ReplacementValue, TransactionAction = item.TransactionAction, HstAmount = item.HstAmount }; duplicateReceipt.ClientBelongings.Add(duplicateBelonging); } } // save receipt to database await _clientReceiptRepository.SaveClientReceiptAsync(duplicateReceipt); // return values duplicateReceiptId = duplicateReceipt.ClientReceiptId; success = true; } else { error = "Unable to locate receipt information"; } } else { error = "Invalid Request"; } } catch (Exception ex) { error = "Unexpected error occurred while processing your request."; _logger.LogError("ClientReceiptService.DuplicateClientReceiptByIdAsync - ex:{@Ex}", new object[] { ex }); } return(success, error, duplicateReceiptId); }
public async Task <(bool Success, string Error, int ReceiptId)> SaveClientReceiptAsync(CreateClientReceiptViewModel model, string userId) { var success = false; var error = ""; int receiptId = 0; try { Models.ClientReceipt receipt = null; if (model.ClientReceiptId > 0) { receipt = await _clientReceiptRepository.FetchBaseByIdAsync(model.ClientReceiptId.Value); } if (receipt == null) { // save reference number for new records var maxReceiptId = await _clientReceiptRepository.GetMaxReceiptIdAsync(); var receiptNumber = $"RE{DateTime.Now.ToString("yyyyMMdd")}{maxReceiptId + 1}"; receipt = new Models.ClientReceipt { CreatedUserId = userId, CreatedUtc = DateTime.UtcNow, // assign date and client once only when the receipt is created, do not allow modifying them ClientId = model.ClientId, Date = DateTime.Now.Date, ReceiptNumber = receiptNumber }; } else { receipt.AuditUserId = userId; receipt.AuditUtc = DateTime.UtcNow; } // if saved id is selected, use that, else create a new one if (model.ClientIdentificationId.HasValue) { receipt.ClientIdentificationId = model.ClientIdentificationId.Value; } else { // save client id first and assign it to receipt // encrypt id info var idNumberEncryptionResult = Encryption.Encrypt(model.IdentificationDocumentNumber); var clientIdentification = new ClientIdentification { ClientId = model.ClientId, IdentificationDocumentId = model.IdentificationDocumentId, IdentificationDocumentNumberEncrypted = idNumberEncryptionResult.EncryptedString, IdentificaitonDocumentNumberUniqueKey = idNumberEncryptionResult.UniqueKey, CreatedUserId = userId, CreatedUtc = DateTime.UtcNow }; await _clientIdentificationRepository.SaveClientIdentificationAsync(clientIdentification); receipt.ClientIdentificationId = clientIdentification.ClientIdentificationId; } receipt.PaymentDate = model.PaymentDate; receipt.PaymentAmount = model.PaymentAmount; receipt.IsPaidInterestOnly = model.IsPaidInterestOnly; receipt.PaymentMethod = model.PaymentMethod; // save to database await _clientReceiptRepository.SaveClientReceiptAsync(receipt); receiptId = receipt.ClientReceiptId; success = true; } catch (Exception ex) { error = "Unexpected error occurred while processing your request."; _logger.LogError("ClientReceiptService.SaveClientReceiptAsync - ex:{@Ex}", new object[] { ex }); } return(success, error, receiptId); }