public async Task <bool> SaveClientBelongingAsync(ClientBelonging clientBelonging) { if (_context.Entry(clientBelonging).State == EntityState.Detached) { _context.Add(clientBelonging); } else if (_context.Entry(clientBelonging).State == EntityState.Modified) { _context.Update(clientBelonging); } if (_context.Entry(clientBelonging).State == EntityState.Unchanged) { return(true); } return(await _context.SaveChangesAsync() > 0); }
public async Task <(bool Success, string Error)> SaveClientBelongingAsync(ClientBelongingViewModel model, string userId) { var success = false; var error = string.Empty; try { if (model.ClientReceiptId > 0) { ClientBelonging clientBelonging = null; if (model.ClientBelongingId > 0) { clientBelonging = await _clientBelongingRepository.FetchBaseByIdAsync(model.ClientBelongingId.Value); } if (clientBelonging == null) { clientBelonging = new ClientBelonging { CreatedUserId = userId, CreatedUtc = DateTime.UtcNow }; } else { clientBelonging.AuditUserId = userId; clientBelonging.AuditUtc = DateTime.UtcNow; } // save other values clientBelonging.ClientReceiptId = model.ClientReceiptId; clientBelonging.TransactionAction = model.TransactionAction; if (model.MetalId.HasValue && model.MetalId != Guid.Empty) { clientBelonging.MetalId = model.MetalId; clientBelonging.MetalOther = null; } else { clientBelonging.MetalId = null; clientBelonging.MetalOther = model.MetalOther; } if (model.KaratId.HasValue && model.KaratId != Guid.Empty) { clientBelonging.KaratId = model.KaratId; clientBelonging.KaratOther = null; } else { clientBelonging.KaratId = null; clientBelonging.KaratOther = model.KaratOther; } clientBelonging.ItemDescription = model.ItemDescription; clientBelonging.ItemWeight = model.Weight; clientBelonging.ItemPrice = model.ItemPrice; clientBelonging.FinalPrice = model.FinalPrice; clientBelonging.ReplacementValue = model.ReplacementValue; clientBelonging.HstAmount = model.HstAmount; await _clientBelongingRepository.SaveClientBelongingAsync(clientBelonging); success = true; } else { error = "Invalid request"; } } catch (Exception ex) { error = "Somethong went wrong while processing your request."; _logger.LogError("ClientService.SaveClientBelongingAsync - exception:{@Ex}", args: new object[] { ex }); } return(Success : success, Error : error); }
public async Task <bool> DeleteBelongingAsync(ClientBelonging belonging) { _context.Remove(belonging); return(await _context.SaveChangesAsync() > 0); }
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); }