private static Boolean AreContractsActive(DataConnector connector, MailingDTO mailing) { Boolean active; ContractDAO contractDAO = new ContractDAO(connector.MySqlConnection); ContractItemDAO contractItemDAO = new ContractItemDAO(connector.MySqlConnection); // Faturamento de um contrato apenas if (mailing.codigoContrato != 0) { ContractDTO contract = contractDAO.GetContract(mailing.codigoContrato); active = (contract.status != 3) && (contract.status != 4); return active; } // Caso contrário é o faturamento de todos os equipamentos do cliente (um ou mais contratos) active = false; List<ContractItemDTO> itemList = contractItemDAO.GetItems("businessPartnerCode = '" + mailing.businessPartnerCode + "'"); foreach (ContractItemDTO item in itemList) { ContractDTO contract = contractDAO.GetContract(item.contrato_id); if ((contract.status != 3) && (contract.status != 4)) active = true; } return active; }
public List<MailingDTO> GetMailings(String filter) { List<MailingDTO> mailingList = new List<MailingDTO>(); if (!String.IsNullOrEmpty(filter)) filter = " WHERE " + filter; String query = "SELECT * FROM `addoncontratos`.`mailing`" + filter + ";"; MySqlCommand command = new MySqlCommand(query, this.mySqlConnection); MySqlDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { MailingDTO mailing = new MailingDTO(); mailing.id = (int)dataReader["id"]; mailing.businessPartnerCode = (String)dataReader["businessPartnerCode"]; mailing.businessPartnerName = (String)dataReader["businessPartnerName"]; mailing.codigoContrato = (int)dataReader["contrato_id"]; mailing.codigoSubContrato = (int)dataReader["subContrato_id"]; mailing.diaFaturamento = (int)dataReader["diaFaturamento"]; mailing.destinatarios = (String)dataReader["destinatarios"]; mailing.enviarDemonstrativo = (Boolean)dataReader["enviarDemonstrativo"]; mailing.ultimoEnvio = (DateTime)dataReader["ultimoEnvio"]; mailingList.Add(mailing); } dataReader.Close(); return mailingList; }
public void SetMailing(MailingDTO mailing) { String commandText = "UPDATE `addoncontratos`.`mailing` SET" + " businessPartnerCode='" + mailing.businessPartnerCode + "'" + ", businessPartnerName='" + mailing.businessPartnerName + "'" + ", contrato_id=" + mailing.codigoContrato + ", subContrato_id=" + mailing.codigoSubContrato + ", diaFaturamento=" + mailing.diaFaturamento + ", destinatarios='" + mailing.destinatarios + "'" + ", enviarDemonstrativo=" + mailing.enviarDemonstrativo + ", ultimoEnvio=@param1" + " WHERE id =" + mailing.id; if (mailing.id == 0) commandText = "INSERT INTO `addoncontratos`.`mailing` VALUES (NULL, '" + mailing.businessPartnerCode + "', " + mailing.codigoContrato + "', " + mailing.diaFaturamento + ", '" + mailing.destinatarios + "', " + mailing.enviarDemonstrativo + ", @param1)"; MySqlParameter param1 = new MySqlParameter("@param1", MySqlDbType.DateTime); param1.Value = mailing.ultimoEnvio; MySqlCommand command = new MySqlCommand(commandText, this.mySqlConnection); command.Parameters.Add(param1); command.ExecuteNonQuery(); }
public MailingDTO GetMailing(int id) { MailingDTO mailing = null; String query = "SELECT * FROM `addoncontratos`.`mailing` WHERE id=" + id; MySqlCommand command = new MySqlCommand(query, this.mySqlConnection); MySqlDataReader dataReader = command.ExecuteReader(); if (dataReader.Read()) { mailing = new MailingDTO(); mailing.id = (int)dataReader["id"]; mailing.businessPartnerCode = (String)dataReader["businessPartnerCode"]; mailing.businessPartnerName = (String)dataReader["businessPartnerName"]; mailing.codigoContrato = (int)dataReader["contrato_id"]; mailing.codigoSubContrato = (int)dataReader["subContrato_id"]; mailing.diaFaturamento = (int)dataReader["diaFaturamento"]; mailing.destinatarios = (String)dataReader["destinatarios"]; mailing.enviarDemonstrativo = (Boolean)dataReader["enviarDemonstrativo"]; mailing.ultimoEnvio = (DateTime)dataReader["ultimoEnvio"]; } dataReader.Close(); return mailing; }
private static void SendMailing(DataConnector connector, MailingDTO mailing, SmtpServerDTO server) { MailMessage mailMessage = null; if (mailing.codigoContrato == 0) mailMessage = MountBusinessPartnerBilling(connector, mailing.businessPartnerCode, mailing.enviarDemonstrativo); else mailMessage = MountContractBilling(connector, mailing.codigoContrato, mailing.codigoSubContrato, mailing.enviarDemonstrativo); List<String> reportFiles = null; if (mailing.enviarDemonstrativo && mailMessage.Attachments.Count > 0) { reportFiles = new List<String>(); foreach (Attachment mailAttachment in mailMessage.Attachments) reportFiles.Add(mailAttachment.Name); } // TODO: Consertar o envio através do .NET, utilizar algum componente que funcione, o SmtpClient // da Microsoft não suporta SSL implicito SmtpServer smtpServer = SmtpServer.ImportFromDTO(server); MailSender mailSender = new MailSender(smtpServer, new TraceHandler()); mailSender.SetContents(mailMessage.Body, reportFiles); Boolean success = mailSender.SendMail("Faturamento de contrato", server.usuario, mailing.destinatarios); if (!success) { // caso não tenha tido sucesso ao enviar através do SmtpClient do .NET tenta enviar pelo PHP String mailerUrl = "http://" + primaryServer + "/Contratos/AjaxCalls/SendEmail.php"; String mailerParams = "subject=Faturamento%20de%20contrato&mailBody=" + mailMessage.Body + "&recipients=" + mailing.destinatarios; String aditionalParams = "&fileCount=0"; if (reportFiles != null) { aditionalParams = "&fileCount=" + reportFiles.Count; int fileIndex = 0; foreach (String filename in reportFiles) { aditionalParams += "&filename" + fileIndex + "=" + reportFiles[fileIndex]; aditionalParams += "&path" + fileIndex + "=" + Environment.CurrentDirectory + @"\" + reportFiles[fileIndex]; fileIndex++; } } RequestHandler mailerRequest = new RequestHandler(mailerUrl, new TraceHandler()); mailerRequest.StartRequest(mailerParams + aditionalParams, null); String mailerResponse = (String)mailerRequest.ParseResponse(typeof(System.String)); if (mailerResponse == "Email enviado com sucesso!") success = true; if (!success) EventLog.WriteEntry("Billing Mailer", mailerResponse); } if (success) // e-mail enviado com sucesso, grava no banco de dados a data { MailingDAO mailingDAO = new MailingDAO(connector.MySqlConnection); mailing.ultimoEnvio = DateTime.Now; mailingDAO.SetMailing(mailing); } }