public void DownloadOrderQuotation() { // given var resources = new Resources(); const string orderNo = "TEST123"; const int orderId = 123; var responseModel = new OrderQuotationViewModel { CustomerDetail = new OrderCustomerDetailModel { OrderId = orderId, CustomerName = "Test Costume", CustomerDetails = "This is some long customer description", CustomerContactNo = "0214472215", CustomerAccountNo = "DC1122", CustomerMobileNo = "0728543333", CustomerEmailAddress = "*****@*****.**", ContactAdded = false, ContactName = "Contraption", ContactNo = "0214472215", ContactEmailAddress = "*****@*****.**", }, DeliveryAddress = new AddressDetailsModel { AddressDetailId = 1, AddressType = "Work", AddressLine1 = "24 Victoria Street", AddressLine2 = "Muizenberg", City = "Cape Town", PostalCode = "7786", Country = "RSA", CreateUser = resources.TestUsername, CreateDate = DateTime.Now }, OrderTotals = new OrderDetailModel { OrderId = orderId, OrderNo = "CCoT" + orderId.ToString(), CreateDate = DateTime.Now, SubTotal = 800M, VatTotal = 120M, Total = 920M, Discount = 0M, OrderLineDetails = new List <OrderLineDetailModel> { new OrderLineDetailModel { OrderId = orderId, ItemDescription = "TestProduct", UnitPrice = 100M, Quantity = 2, Discount = 0M, LineTotal = 200M }, new OrderLineDetailModel { OrderId = orderId, ItemDescription = "AnotherProductName", UnitPrice = 300M, Quantity = 2, Discount = 0M, LineTotal = 600M }, } }, CompanyProfile = new CompanyProfileModel { AddressLine1 = "24 Victoria Street", AddressLine2 = "Muizenberg", City = "Cape Town", PostalCode = "7786", Country = "RSA", EmailAddress = "*****@*****.**", DisplayName = "A Nice Company Name", RegistrationNo = "2018/2378945", TaxReferenceNo = "819823", TelephoneNo = "0217123344", BankingDetails = new BankingDetailsModel { AccountNo = "4075896644", AccountHolder = "Some Account Holder", AccountType = "Cheque", BankName = "ABSA", BranchCode = "632005" } } }; var requestModel = new GetOrderQuoteRequestModel { OrderId = orderId, CompanyProfileId = 1 }; resources.MockApiCaller.AddMockResponse("WebApi:Orders:GetOrderQuote", requestModel, responseModel); // when var result = resources.Controller.DownloadOrder(orderId, orderNo) as ViewAsPdf; // then Assert.IsNotNull(result); var model = (OrderQuotationViewModel)result.Model; Assert.IsNotNull(model); Assert.AreEqual("OrderQuotation", result.ViewName); Assert.AreEqual(model.CompanyProfile, responseModel.CompanyProfile); Assert.AreEqual(model.CustomerDetail, responseModel.CustomerDetail); Assert.AreEqual(model.DeliveryAddress, responseModel.DeliveryAddress); Assert.AreEqual(model.OrderTotals, responseModel.OrderTotals); }
public OrderQuotationViewModel GetOrderQuote([FromBody] GetOrderQuoteRequestModel requestModel) { var companyProfile = new CompanyProfileModel(); var customerDetails = new OrderCustomerDetailModel(); var orderDetails = new OrderDetailModel(); var addressDetails = new AddressDetailsModel(); var existingCompany = Context.CompanyProfiles.Include(cp => cp.BankingDetails).Include(cp => cp.Addresses).FirstOrDefault(cp => cp.CompanyProfileId == requestModel.CompanyProfileId); if (existingCompany != null) { companyProfile.DisplayName = existingCompany.DisplayName; companyProfile.RegistrationNo = existingCompany.RegistrationNo; companyProfile.TaxReferenceNo = existingCompany.TaxReferenceNo; companyProfile.TelephoneNo = existingCompany.TelephoneNo; companyProfile.EmailAddress = existingCompany.EmailAddress; if (existingCompany.Addresses.Count > 0) { companyProfile.AddressLine1 = existingCompany.Addresses[0].AddressLine1; companyProfile.AddressLine2 = existingCompany.Addresses[0].AddressLine2; companyProfile.City = existingCompany.Addresses[0].City; companyProfile.Country = existingCompany.Addresses[0].Country; companyProfile.PostalCode = existingCompany.Addresses[0].PostalCode; } if (existingCompany.BankingDetails.Count > 0) { companyProfile.BankingDetails = new BankingDetailsModel { AccountHolder = existingCompany.BankingDetails[0].AccountHolder, AccountNo = existingCompany.BankingDetails[0].AccountNo, BankName = existingCompany.BankingDetails[0].BankName, AccountType = existingCompany.BankingDetails[0].AccountType, BranchCode = existingCompany.BankingDetails[0].BranchCode, }; } } var order = Context.Orders.Include(o => o.OrderDetails).First(o => o.OrderId == requestModel.OrderId); var existingCustomer = Context.Customers.Include(c => c.Addresses).Include(c => c.ContactPeople).FirstOrDefault(c => c.CustomerId == order.CustomerId); var existingContact = existingCustomer != null?existingCustomer.ContactPeople.FirstOrDefault(cp => cp.ContactId == order.ContactId) : null; var existingAddresss = existingCustomer != null?existingCustomer.Addresses.FirstOrDefault(cp => cp.AddressDetailId == order.AddressDetailId) : null; if (existingCustomer != null) { customerDetails.OrderId = order.OrderId; customerDetails.CustomerName = existingCustomer.CustomerName; customerDetails.CustomerContactNo = existingCustomer.ContactNo; customerDetails.CustomerAccountNo = existingCustomer.AccountNo; customerDetails.CustomerDetails = existingCustomer.CustomerDetails; customerDetails.CustomerEmailAddress = existingCustomer.EmailAddress; if (existingContact != null) { customerDetails.ContactName = existingContact.ContactName; customerDetails.ContactNo = existingContact.ContactNo; customerDetails.ContactEmailAddress = existingContact.EmailAddress; } else { customerDetails.ContactName = existingCustomer.CustomerName; } if (existingAddresss != null) { addressDetails.AddressLine1 = existingAddresss.AddressLine1; addressDetails.AddressLine2 = existingAddresss.AddressLine2; addressDetails.City = existingAddresss.City; addressDetails.Country = existingAddresss.Country; addressDetails.PostalCode = existingAddresss.PostalCode; } } orderDetails.Discount = order.DiscountTotal; orderDetails.OrderNo = order.OrderNo; orderDetails.OrderId = order.OrderId; orderDetails.CreateDate = order.CreateDate; orderDetails.SubTotal = order.SubTotal; orderDetails.VatTotal = order.VatTotal; orderDetails.Total = order.OrderTotal; var salesPerson = SecurityContext.Users.FirstOrDefault(u => u.Username == order.CreateUser); if (salesPerson != null) { orderDetails.CreateUser = (salesPerson.FirstName + " " + salesPerson.LastName).Trim() != string.Empty ? (salesPerson.FirstName + " " + salesPerson.LastName) : order.CreateUser; // replace with first and last name of user } else { orderDetails.CreateUser = order.CreateUser; } var maxLineNo = order.OrderDetails.Max(o => o.LineNo); foreach (var linedetail in order.OrderDetails.Where(o => o.LineNo == maxLineNo)) { var newLineDetail = new OrderLineDetailModel { Discount = linedetail.Discount, ItemDescription = linedetail.ItemDescription, Quantity = linedetail.Quantity, UnitPrice = linedetail.UnitPrice, LineTotal = linedetail.LineTotal }; orderDetails.OrderLineDetails.Add(newLineDetail); } return(new OrderQuotationViewModel { CompanyProfile = companyProfile, CustomerDetail = customerDetails, DeliveryAddress = addressDetails, OrderTotals = orderDetails }); }