public void PrintSaleTest() { var testUser = GetTestUser(); var testCompany = GetTestCompanyAU(); // Find a random sales order header int range = db.FindSalesOrderHeaders(testCompany.Id).Count(); var sohList = db.FindSalesOrderHeaders(testCompany.Id) .Skip(RandomInt(0, range - 1)) .Take(1) .FirstOrDefault(); // Uncomment the following line to get a document for a random SOH, possibly with no items //var soh = SalesService.MapToModel(sohList); // Uncomment the following line to get a document known to have a number of items var soh = SalesService.FindSalesOrderHeaderModel(58, testCompany); var soht = SalesService.CopySaleToTemp(testCompany, soh, testUser, false); var customer = CustomerService.FindCustomerModel(soht.CustomerId == null ? 0 : soht.CustomerId.Value, testCompany); SalePrintOptionsViewModel model = new SalePrintOptionsViewModel { SalesOrderHeaderTempId = soht.Id, TemplateId = LookupService.FindDocumentTemplateModel(DocumentTemplateCategory.SalesOrders, DocumentTemplateType.OrderConfirmation).Id, ShowCancelledItems = true, SaveInSaleNotesAttachments = true, ViewCreatedDocument = false, SendAsEMail = true, Subject = "Test Subject", Message = "Test Message", SaveAsContact = true }; model.CustomerContact.CustomerId = customer.Id; // Get all the recipients List <ListItemModel> recipients = CustomerService.FindCustomerRecipients(soht, testCompany, testUser); bool bAlt = false; string selectedIds = "To:OTH"; for (int i = 0; i < recipients.Count(); i++) { var user = recipients[i]; selectedIds += "," + (bAlt ? "To:" : "CC:") + user.Id.ToString(); bAlt = !bAlt; } // 'Other user' model.CustomerContact.ContactFirstname = RandomString(); model.CustomerContact.ContactSurname = RandomString(); model.CustomerContact.ContactEmail = RandomEMail(); // Print the sale var error = SalesService.PrintSale(model, testCompany, testUser, selectedIds); Assert.IsTrue(!error.IsError, error.Message); }
void prepareViewModel(SalePrintOptionsViewModel model) { var soht = SalesService.FindSalesOrderHeaderTempModel(model.SalesOrderHeaderTempId, CurrentCompany, true); PrepareViewModel(model, EvolutionResources.bnrPrintSale, model.SalesOrderHeaderTempId, MakeMenuOptionFlags(0, 0, 0, soht.OriginalRowId)); model.TemplateList = LookupService.FindDocumentTemplatesListItemModel(DocumentTemplateCategory.SalesOrders); model.AvailableRecipientsList = CustomerService.FindCustomerRecipients(soht, CurrentCompany, CurrentUser); model.AvailableRecipientsList.Add(new ListItemModel("Other Recipient", "OTH")); model.SalutationList = LookupService.FindLOVItemsListItemModel(CurrentCompany, LOVName.Salutation); }
ViewModelBase createModel(int id) { var model = new SalePrintOptionsViewModel(); model.SalesOrderHeaderTempId = id; var soht = SalesService.FindSalesOrderHeaderTempModel(id, CurrentCompany, true); PrepareViewModel(model, EvolutionResources.bnrPrintSale, id, MakeMenuOptionFlags(0, 0, 0, soht.OriginalRowId)); var customer = CustomerService.FindCustomerModel(soht.CustomerId == null ? 0 : soht.CustomerId.Value, CurrentCompany); model.CustomerContact.CustomerId = customer.Id; prepareViewModel(model); return(model); }
public ActionResult DoPrintSendSale(SalePrintOptionsViewModel model) { if (ModelState.IsValid) { // Prints the sale according to the user selections string selectedIds; try { selectedIds = Request.Form["SelectedIds"].ToLower(); } catch { selectedIds = ""; } model.Error = SalesService.PrintSale(model, CurrentCompany, CurrentUser, selectedIds); if (!model.Error.IsError && !string.IsNullOrEmpty(model.Error.URL)) { return(Redirect(model.Error.URL)); } } prepareViewModel(model); return(View("SalePrint", model)); }
public Error PrintSale(SalePrintOptionsViewModel model, CompanyModel company, UserModel currentUser, string selectedIds) { // Prints the sale according to the user selections var error = new Error(); var soh = FindSalesOrderHeaderModelFromTempId(model.SalesOrderHeaderTempId, company, false); if (soh != null) { var customer = CustomerService.FindCustomerModel(soh.CustomerId.Value, company, false); if (customer != null) { // Check the recipients and new contacts var recipients = new List <UserModel>(); var copies = new List <UserModel>(); error = BuildRecipientLists(selectedIds.ToLower(), model.CustomerContact, company, customer, model.SaveAsContact, recipients, copies); if (!error.IsError) { // Create the required document PDF string outputFile = ""; string pdfFile = MediaServices.GetMediaFolder(MediaFolder.Temp, soh.CompanyId) + "\\" + soh.OrderNumber + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; error = CreateSalesDocumentPdf(soh, model.TemplateId, pdfFile, model.ShowCancelledItems, ref outputFile); if (!error.IsError) { string url = MediaServices.GetMediaFolder(MediaFolder.Temp, soh.CompanyId, -1, -1, true) + "/" + outputFile.FileName(); if (model.SaveInSaleNotesAttachments) { // Save it against sale notes/attachments error = NoteService.AttachNoteToSalesOrder(soh, currentUser, (string.IsNullOrEmpty(model.Subject) ? "Sale Preview Document" : model.Subject), model.Message, outputFile.ToStringList(), FileCopyType.Copy); } if (!error.IsError) { if (model.SendAsEMail) { // Send it as an email attachment Dictionary <string, string> dict = new Dictionary <string, string>(); if (!error.IsError) { var message = new EMailMessage(currentUser, recipients, model.Subject, model.Message); message.AddCopies(copies); message.AddProperties(dict); message.AddAttachment(outputFile, FileCopyType.Copy); EMailService.EMailService emailService = new Evolution.EMailService.EMailService(db, company); error = emailService.SendEMail(message); if (!error.IsError) { error.SetInfo(EvolutionResources.infEMailSuccessfullySent); if (model.ViewCreatedDocument) { error.URL = url; } } } } else if (model.ViewCreatedDocument) { error.URL = url; } else { error.SetInfo(EvolutionResources.infDocumentSuccessfullyCreated); } } } if (!string.IsNullOrEmpty(outputFile)) { MediaServices.AddFileToLog(outputFile, 30); } } } else { error.SetRecordError("Customer", soh.CustomerId.Value); } } else { error.SetError(EvolutionResources.errFailedToFindSOGForSOHT, "", model.SalesOrderHeaderTempId.ToString()); } model.SetError(error.Icon, error.Message, null, null, null, null, true); return(error); }