/// <summary> /// save new foundry invoice /// </summary> /// <param name="newFoundryInvoice"></param> /// <returns></returns> public OperationResult SaveFoundryInvoice(FoundryInvoice newFoundryInvoice) { var operationResult = new OperationResult(); try { var existingFoundryInvoice = _db.FoundryInvoice.FirstOrDefault(x => x.Number.ToLower() == newFoundryInvoice.Number.ToLower()); if (existingFoundryInvoice == null) { _db.FoundryInvoice.Add(newFoundryInvoice); _db.SaveChanges(); operationResult.Success = true; operationResult.Message = "Create this newFoundryInvoice success!"; } else { operationResult.Success = false; operationResult.Message = "Duplicate Entry"; } } catch (Exception ex) { operationResult.Success = false; operationResult.Message = "Can not create this newFoundryInvoice"; logger.ErrorFormat("Error saving new foundry invoice: {0} ", ex.ToString()); } return(operationResult); }
/// <summary> /// convert foundry invoice view model to domain /// </summary> /// <param name="model"></param> /// <returns></returns> public FoundryInvoice ConvertToDomain(FoundryInvoiceViewModel model) { FoundryInvoice foundryInvoice = new FoundryInvoice(); foundryInvoice.FoundryInvoiceId = model.FoundryInvoiceId; foundryInvoice.FoundryId = model.FoundryId; foundryInvoice.Number = model.InvoiceNumber; foundryInvoice.Amount = model.InvoiceAmount; foundryInvoice.ScheduledPaymentDate = model.ScheduledPaymentDate; foundryInvoice.ActualPaymentDate = model.ActualPaymentDate; foundryInvoice.Notes = model.Notes; foundryInvoice.AirFreight = model.AirFreight; foundryInvoice.HasBeenProcessed = model.HasBeenProcessed; var buckets = new List <Bucket>(); if (model.Buckets != null && model.Buckets.Count > 0) { foreach (var bucket in model.Buckets) { Bucket convertedModel = new BucketConverter().ConvertToDomain(bucket); buckets.Add(convertedModel); } } foundryInvoice.Buckets = buckets; return(foundryInvoice); }
/// <summary> /// convert foundry invoice to view model /// </summary> /// <param name="invoice"></param> /// <returns></returns> public FoundryInvoiceViewModel ConvertToView(FoundryInvoice invoice) { FoundryInvoiceViewModel model = new FoundryInvoiceViewModel(); var _foundryDynamicsRepository = new FoundryDynamicsRepository(); var _bucketRepository = new BucketRepository(); var dynamicsFoundry = _foundryDynamicsRepository.GetFoundry(invoice.FoundryId); var buckets = _bucketRepository.GetBuckets().Where(x => x.FoundryInvoiceId == invoice.FoundryInvoiceId).ToList(); model.FoundryInvoiceId = invoice.FoundryInvoiceId; model.BillOfLadingId = invoice.FoundryInvoiceId; model.InvoiceNumber = (!string.IsNullOrEmpty(invoice.Number)) ? invoice.Number : "N/A"; model.InvoiceAmount = invoice.Amount; model.ScheduledPaymentDate = (invoice.ScheduledPaymentDate != null) ? invoice.ScheduledPaymentDate : DateTime.MinValue; model.ScheduledPaymentDateStr = (invoice.ScheduledPaymentDate != null) ? invoice.ScheduledPaymentDate.Value.ToShortDateString() : "N/A"; model.ActualPaymentDate = (invoice.ActualPaymentDate != null) ? invoice.ActualPaymentDate : DateTime.MinValue;; model.ActualPaymentDateStr = (invoice.ActualPaymentDate != null) ? invoice.ActualPaymentDate.Value.ToShortDateString() : "N/A"; model.Notes = (!string.IsNullOrEmpty(invoice.Notes)) ? invoice.Notes : "N/A"; model.FoundryId = invoice.FoundryId; model.FoundryName = (dynamicsFoundry != null && !string.IsNullOrEmpty(dynamicsFoundry.VENDSHNM)) ? dynamicsFoundry.VENDSHNM : "N/A"; model.AirFreight = invoice.AirFreight; model.HasBeenProcessed = invoice.HasBeenProcessed; model.CreateDate = (invoice.CreatedDate != null) ? invoice.CreatedDate : DateTime.MinValue; model.CreateDateStr = (invoice.CreatedDate != null) ? invoice.CreatedDate.Value.ToShortDateString() : "N/A"; model.Buckets = new List <BucketViewModel>(); if (buckets != null && buckets.Count > 0) { foreach (var bucket in buckets) { BucketViewModel convertedModel = new BucketConverter().ConvertToView(bucket); model.Buckets.Add(convertedModel); } } if (_foundryDynamicsRepository != null) { _foundryDynamicsRepository.Dispose(); _foundryDynamicsRepository = null; } if (_bucketRepository != null) { _bucketRepository.Dispose(); _bucketRepository = null; } return(model); }
/// <summary> /// get foundry invoice /// </summary> /// <param name="foundryInvoiceId"></param> /// <returns></returns> public FoundryInvoice GetFoundryInvoice(Guid foundryInvoiceId) { var foundryInvoice = new FoundryInvoice(); try { foundryInvoice = _db.FoundryInvoice.FirstOrDefault(x => x.FoundryInvoiceId == foundryInvoiceId); } catch (Exception ex) { logger.ErrorFormat("Error while getting foundry invoice: { 0} ", ex.ToString()); } return(foundryInvoice); }
/// <summary> /// get foundry invoice number /// </summary> /// <param name="foundryInvoiceNumber"></param> /// <returns></returns> public FoundryInvoice GetFoundryInvoice(string foundryInvoiceNumber) { var foundryInvoice = new FoundryInvoice(); try { foundryInvoice = _db.FoundryInvoice.FirstOrDefault(x => x.Number.Replace(" ", string.Empty).ToLower() == foundryInvoiceNumber.Replace(" ", string.Empty).ToLower()); } catch (Exception ex) { logger.ErrorFormat("Error while getting foundry invoice: { 0} ", ex.ToString()); } return(foundryInvoice); }
/// <summary> /// update foundry invoice /// </summary> /// <param name="foundryInvoice"></param> /// <returns></returns> public OperationResult UpdateFoundryInvoice(FoundryInvoice foundryInvoice) { var operationResult = new OperationResult(); var existingFoundryInvoice = _db.FoundryInvoice.Find(foundryInvoice.FoundryInvoiceId); if (existingFoundryInvoice != null) { logger.Debug("foundry invoice is being updated."); try { _db.FoundryInvoice.Attach(existingFoundryInvoice); _db.Entry(existingFoundryInvoice).CurrentValues.SetValues(foundryInvoice); _db.SaveChanges(); var existingBuckets = _db.Bucket.Where(x => x.FoundryInvoiceId == foundryInvoice.FoundryInvoiceId).ToList(); if (foundryInvoice.Buckets != null && foundryInvoice.Buckets.Count > 0) { foreach (var bucket in foundryInvoice.Buckets) { var existingBucket = _db.Bucket.Find(bucket.BucketId); if (existingBucket == null) { existingFoundryInvoice.Buckets.Add(bucket); _db.SaveChanges(); } } } if (existingBuckets != null && existingBuckets.Count > 0) { foreach (var bucket in existingBuckets) { var existingBucket = foundryInvoice.Buckets.FirstOrDefault(x => x.BucketId == bucket.BucketId); if (existingBucket == null) { _db.Bucket.Remove(bucket); _db.SaveChanges(); } } } operationResult.Success = true; operationResult.Message = "Success"; } catch (Exception ex) { operationResult.Success = false; operationResult.Message = "Error"; logger.ErrorFormat("Error while updating foundry invoice: { 0} ", ex.ToString()); } } else { operationResult.Success = false; operationResult.Message = "Unable to find selected foundry invoice."; } return(operationResult); }