Esempio n. 1
0
        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);
        }