use of org.compiere.model.MClient in project adempiere by adempiere.
the class DunningPrint method doIt.
// prepare
/**
* Process
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ",R_MailText_ID=" + p_R_MailText_ID + ", EmailPDF=" + p_EMailPDF + ",IsOnlyIfBPBalance=" + p_IsOnlyIfBPBalance + ",PrintUnprocessedOnly=" + p_PrintUnprocessedOnly);
// Need to have Template
if (p_EMailPDF && p_R_MailText_ID == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@");
// String subject = "";
MMailText mText = null;
if (p_EMailPDF) {
mText = new MMailText(getCtx(), p_R_MailText_ID, get_TrxName());
if (p_EMailPDF && mText.get_ID() == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
// subject = mText.getMailHeader();
}
//
MDunningRun run = new MDunningRun(getCtx(), p_C_DunningRun_ID, get_TrxName());
if (run.get_ID() == 0)
throw new AdempiereUserError("@NotFound@: @C_DunningRun_ID@ - " + p_C_DunningRun_ID);
MClient client = MClient.get(getCtx());
int count = 0;
int errors = 0;
MDunningRunEntry[] entries = run.getEntries(false);
for (int i = 0; i < entries.length; i++) {
MDunningRunEntry entry = entries[i];
// Print Format on Dunning Level
MDunningLevel level = new MDunningLevel(getCtx(), entry.getC_DunningLevel_ID(), get_TrxName());
MPrintFormat format = null;
if (level.getDunning_PrintFormat_ID() > 0)
format = MPrintFormat.get(getCtx(), level.getDunning_PrintFormat_ID(), false);
if (p_IsOnlyIfBPBalance && entry.getAmt().signum() <= 0)
continue;
if (p_PrintUnprocessedOnly && entry.isProcessed())
continue;
// To BPartner
MBPartner bp = new MBPartner(getCtx(), entry.getC_BPartner_ID(), get_TrxName());
if (bp.get_ID() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @C_BPartner_ID@ " + entry.getC_BPartner_ID());
errors++;
continue;
}
// To User
MUser to = new MUser(getCtx(), entry.getAD_User_ID(), get_TrxName());
if (p_EMailPDF) {
if (to.get_ID() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @AD_User_ID@ - " + bp.getName());
errors++;
continue;
} else if (to.getEMail() == null || to.getEMail().length() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @EMail@ - " + to.getName());
errors++;
continue;
}
}
// query
MQuery query = new MQuery("C_Dunning_Header_v");
query.addRestriction("C_DunningRunEntry_ID", MQuery.EQUAL, new Integer(entry.getC_DunningRunEntry_ID()));
// Engine
PrintInfo info = new PrintInfo(bp.getName(), MDunningRunEntry.Table_ID, entry.getC_DunningRunEntry_ID(), entry.getC_BPartner_ID());
info.setDescription(bp.getName() + ", Amt=" + entry.getAmt());
ReportEngine re = null;
if (format != null)
re = new ReportEngine(getCtx(), format, query, info);
boolean printed = false;
if (p_EMailPDF) {
EMail email = client.createEMail(to.getEMail(), null, null);
if (!email.isValid()) {
addLog(entry.get_ID(), null, null, "@RequestActionEMailError@ Invalid EMail: " + to);
errors++;
continue;
}
// variable context
mText.setUser(to);
mText.setBPartner(bp);
mText.setPO(entry);
String message = mText.getMailText(true);
if (mText.isHtml())
email.setMessageHTML(mText.getMailHeader(), message);
else {
email.setSubject(mText.getMailHeader());
email.setMessageText(message);
}
//
if (re != null) {
File attachment = re.getPDF(File.createTempFile("Dunning", ".pdf"));
log.fine(to + " - " + attachment);
email.addAttachment(attachment);
}
//
String msg = email.send();
MUserMail um = new MUserMail(mText, entry.getAD_User_ID(), email);
um.saveEx();
if (msg.equals(EMail.SENT_OK)) {
addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailOK@");
count++;
printed = true;
} else {
addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailError@ " + msg);
errors++;
}
} else {
if (re != null) {
re.print();
count++;
printed = true;
}
}
if (printed) {
entry.setProcessed(true);
entry.save();
}
}
// for all dunning letters
if (errors == 0) {
run.setProcessed(true);
run.saveEx();
}
if (p_EMailPDF)
return "@Sent@=" + count + " - @Errors@=" + errors;
return "@Printed@=" + count;
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class ZkReportViewer method cmd_find.
// cmd_report
/**
* Query Report
*/
private void cmd_find() {
int AD_Table_ID = m_reportEngine.getPrintFormat().getAD_Table_ID();
String title = null;
String tableName = null;
// Get Find Tab Info
String sql = "SELECT t.AD_Tab_ID " + // ,w.Name, t.Name, w.IsDefault, t.SeqNo, ABS (tt.AD_Window_ID-t.AD_Window_ID)
"FROM AD_Tab t" + " INNER JOIN AD_Window w ON (t.AD_Window_ID=w.AD_Window_ID)" + " INNER JOIN AD_Table tt ON (t.AD_Table_ID=tt.AD_Table_ID) " + "WHERE tt.AD_Table_ID=? " + "ORDER BY w.IsDefault DESC, t.SeqNo, ABS (tt.AD_Window_ID-t.AD_Window_ID)";
int AD_Tab_ID = DB.getSQLValue(null, sql, AD_Table_ID);
// ASP
MClient client = MClient.get(Env.getCtx());
String ASPFilter = "";
if (client.isUseASP())
ASPFilter = " AND ( AD_Tab_ID IN ( " + // Just ASP subscribed tabs for client "
" SELECT t.AD_Tab_ID " + " FROM ASP_Tab t, ASP_Window w, ASP_Level l, ASP_ClientLevel cl " + " WHERE w.ASP_Level_ID = l.ASP_Level_ID " + " AND cl.AD_Client_ID = " + client.getAD_Client_ID() + " AND cl.ASP_Level_ID = l.ASP_Level_ID " + " AND t.ASP_Window_ID = w.ASP_Window_ID " + " AND t.IsActive = 'Y' " + " AND w.IsActive = 'Y' " + " AND l.IsActive = 'Y' " + " AND cl.IsActive = 'Y' " + // Show
" AND t.ASP_Status = 'S') " + " OR AD_Tab_ID IN ( " + // + show ASP exceptions for client
" SELECT AD_Tab_ID " + " FROM ASP_ClientException ce " + " WHERE ce.AD_Client_ID = " + client.getAD_Client_ID() + " AND ce.IsActive = 'Y' " + " AND ce.AD_Tab_ID IS NOT NULL " + " AND ce.AD_Field_ID IS NULL " + // Show
" AND ce.ASP_Status = 'S') " + " ) " + " AND AD_Tab_ID NOT IN ( " + // minus hide ASP exceptions for client
" SELECT AD_Tab_ID " + " FROM ASP_ClientException ce " + " WHERE ce.AD_Client_ID = " + client.getAD_Client_ID() + " AND ce.IsActive = 'Y' " + " AND ce.AD_Tab_ID IS NOT NULL " + " AND ce.AD_Field_ID IS NULL " + // Hide
" AND ce.ASP_Status = 'H')";
//
sql = "SELECT Name, TableName FROM AD_Tab_v WHERE AD_Tab_ID=? " + ASPFilter;
if (!Env.isBaseLanguage(Env.getCtx(), "AD_Tab"))
sql = "SELECT Name, TableName FROM AD_Tab_vt WHERE AD_Tab_ID=?" + " AND AD_Language='" + Env.getAD_Language(Env.getCtx()) + "' " + ASPFilter;
try {
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, AD_Tab_ID);
ResultSet rs = pstmt.executeQuery();
//
if (rs.next()) {
title = rs.getString(1);
tableName = rs.getString(2);
}
//
rs.close();
pstmt.close();
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
}
GridField[] findFields = null;
if (tableName != null)
findFields = GridField.createFields(m_ctx, m_WindowNo, 0, AD_Tab_ID);
// FR [ 295 ]
if (findFields == null) {
// No Tab for Table exists
if (launchProcessPara()) {
try {
renderReport();
} catch (Exception e) {
throw new AdempiereException("Failed to render report", e);
}
revalidate();
} else {
return;
}
} else {
FindWindow find = new FindWindow(m_WindowNo, title, AD_Table_ID, tableName, "", findFields, 1, AD_Tab_ID);
if (!find.isCancel()) {
m_reportEngine.setQuery(find.getQuery());
try {
renderReport();
} catch (Exception e) {
throw new AdempiereException("Failed to render report", e);
}
revalidate();
}
find = null;
}
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class ExportModelValidator method loadReplicationStrategy.
public void loadReplicationStrategy(Properties ctx) {
MClient m_client = MClient.get(Env.getCtx(), clientId);
/*replicationStrategyId = MRole.get(m_client.getCtx(), roleId).get_ValueAsInt("AD_ReplicationStrategy_ID");
if(replicationStrategyId <= 0)
{
replicationStrategyId = MOrg.get(m_client.getCtx(), orgId).getAD_ReplicationStrategy_ID();
}
if(replicationStrategyId <= 0)
{
replicationStrategyId = m_client.getAD_ReplicationStrategy_ID();
log.info("client.getAD_ReplicationStrategy_ID() = " + replicationStrategyId);
}
if (replicationStrategyId > 0) {
replicationStrategy = new MReplicationStrategy(m_client.getCtx(), replicationStrategyId, null);
if(!replicationStrategy.isActive())
{
return;
}
exportHelper = new ExportHelper(m_client, replicationStrategy);
}*/
// Add Tables
// We want to be informed when records in Replication tables are created/updated/deleted!
//engine.addModelChange(MBPartner.Table_Name, this);
//engine.addModelChange(MOrder.Table_Name, this);
//engine.addModelChange(MOrderLine.Table_Name, this);
MReplicationStrategy.getByOrgAndRole(ctx, orgId, roleId, null).stream().filter(replicationStrategy -> replicationStrategy != null).forEach(replicationStrategy -> {
for (X_AD_ReplicationTable rplTable : replicationStrategy.getReplicationTables()) {
if (X_AD_ReplicationTable.REPLICATIONTYPE_Merge.equals(rplTable.getReplicationType()) || X_AD_ReplicationTable.REPLICATIONTYPE_Broadcast.equals(rplTable.getReplicationType()) || X_AD_ReplicationTable.REPLICATIONTYPE_Reference.equals(rplTable.getReplicationType())) {
String tableName = MTable.getTableName(replicationStrategy.getCtx(), rplTable.getAD_Table_ID());
modelValidationEngine.addModelChange(tableName, this);
}
}
for (X_AD_ReplicationDocument rplDocument : replicationStrategy.getReplicationDocuments()) {
if (X_AD_ReplicationDocument.REPLICATIONTYPE_Merge.equals(rplDocument.getReplicationType()) || X_AD_ReplicationDocument.REPLICATIONTYPE_Reference.equals(rplDocument.getReplicationType())) {
String tableName = MTable.getTableName(replicationStrategy.getCtx(), rplDocument.getAD_Table_ID());
modelValidationEngine.addDocValidate(tableName, this);
}
}
});
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class MWFActivity method sendEMail.
// fillParameter
/*********************************
* Send EMail
*/
private void sendEMail() {
DocAction doc = (DocAction) m_po;
MMailText text = new MMailText(getCtx(), m_node.getR_MailText_ID(), null);
text.setPO(m_po, true);
//
String subject = doc.getDocumentInfo() + ": " + text.getMailHeader();
String message = text.getMailText(true) + "\n-----\n" + doc.getDocumentInfo() + "\n" + doc.getSummary();
File pdf = doc.createPDF();
//
MClient client = MClient.get(doc.getCtx(), doc.getAD_Client_ID());
// Explicit EMail
sendEMail(client, 0, m_node.getEMail(), subject, message, pdf, text.isHtml());
// Recipient Type
String recipient = m_node.getEMailRecipient();
// email to document user
if (recipient == null || recipient.length() == 0)
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentBusinessPartner)) {
int index = m_po.get_ColumnIndex("AD_User_ID");
if (index > 0) {
Object oo = m_po.get_Value(index);
if (oo instanceof Integer) {
int AD_User_ID = ((Integer) oo).intValue();
if (AD_User_ID != 0)
sendEMail(client, AD_User_ID, null, subject, message, pdf, text.isHtml());
else
log.fine("No User in Document");
} else
log.fine("Empty User in Document");
} else
log.fine("No User Field in Document");
} else if (recipient.equals(MWFNode.EMAILRECIPIENT_DocumentOwner))
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
else if (recipient.equals(MWFNode.EMAILRECIPIENT_WFResponsible)) {
MWFResponsible resp = getResponsible();
if (resp.isInvoker())
sendEMail(client, doc.getDoc_User_ID(), null, subject, message, pdf, text.isHtml());
else if (resp.isHuman())
sendEMail(client, resp.getAD_User_ID(), null, subject, message, pdf, text.isHtml());
else if (resp.isRole()) {
MRole role = resp.getRole();
if (role != null) {
MUser[] users = MUser.getWithRole(role);
for (int i = 0; i < users.length; i++) sendEMail(client, users[i].getAD_User_ID(), null, subject, message, pdf, text.isHtml());
}
} else if (resp.isOrganization()) {
MOrgInfo org = MOrgInfo.get(getCtx(), m_po.getAD_Org_ID(), get_TrxName());
if (org.getSupervisor_ID() == 0)
log.fine("No Supervisor for AD_Org_ID=" + m_po.getAD_Org_ID());
else
sendEMail(client, org.getSupervisor_ID(), null, subject, message, pdf, text.isHtml());
}
}
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class MWFActivity method setUserChoice.
// setVariable
/**
* Set User Choice
* @param AD_User_ID user
* @param value new Value
* @param displayType display type
* @param textMsg optional Message
* @return true if set
* @throws Exception if error
*/
public boolean setUserChoice(int AD_User_ID, String value, int displayType, String textMsg) throws Exception {
// Check if user approves own document when a role is reponsible
/*
* 2007-06-08, matthiasO.
* The following sequence makes sure that only users in roles which
* have the 'Approve own document flag' set can set the user choice
* of 'Y' (approve) or 'N' (reject).
* IMHO this is against the meaning of 'Approve own document': Why
* should a user who is faced with the task of approving documents
* generally be required to have the ability to approve his OWN
* documents? If the document to approve really IS his own document
* this will be respected when trying to find an approval user in
* the call to getApprovalUser(...) below.
*/
/*
if (getNode().isUserApproval() && getPO() instanceof DocAction)
{
DocAction doc = (DocAction)m_po;
MUser user = new MUser (getCtx(), AD_User_ID, null);
MRole[] roles = user.getRoles(m_po.getAD_Org_ID());
boolean canApproveOwnDoc = false;
for (int r = 0; r < roles.length; r++)
{
if (roles[r].isCanApproveOwnDoc())
{
canApproveOwnDoc = true;
break;
} // found a role which allows to approve own document
}
if (!canApproveOwnDoc)
{
String info = user.getName() + " cannot approve own document " + doc;
addTextMsg(info);
log.fine(info);
return false; // ignore
}
}*/
setWFState(StateEngine.STATE_Running);
setAD_User_ID(AD_User_ID);
Trx trx = (get_TrxName() != null) ? Trx.get(get_TrxName(), false) : null;
boolean ok = setVariable(value, displayType, textMsg, trx);
if (!ok)
return false;
String newState = StateEngine.STATE_Completed;
// Approval
if (getNode().isUserApproval() && getPO(trx) instanceof DocAction) {
DocAction doc = (DocAction) m_po;
try {
// Not approved
if (!"Y".equals(value)) {
newState = StateEngine.STATE_Aborted;
if (!(doc.processIt(DocAction.ACTION_Reject)))
setTextMsg("Cannot Reject - Document Status: " + doc.getDocStatus());
} else {
if (isInvoker()) {
int startAD_User_ID = Env.getAD_User_ID(getCtx());
if (startAD_User_ID == 0)
startAD_User_ID = doc.getDoc_User_ID();
int nextAD_User_ID = getApprovalUser(startAD_User_ID, doc.getC_Currency_ID(), doc.getApprovalAmt(), doc.getAD_Org_ID(), // own doc
startAD_User_ID == doc.getDoc_User_ID());
// No Approver
if (nextAD_User_ID <= 0) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - No Approver");
doc.processIt(DocAction.ACTION_Reject);
} else if (startAD_User_ID != nextAD_User_ID) {
forwardTo(nextAD_User_ID, "Next Approver");
newState = StateEngine.STATE_Suspended;
} else // Approve
{
if (!(doc.processIt(DocAction.ACTION_Approve))) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - Document Status: " + doc.getDocStatus());
}
}
} else // No Invoker - Approve
if (!(doc.processIt(DocAction.ACTION_Approve))) {
newState = StateEngine.STATE_Aborted;
setTextMsg("Cannot Approve - Document Status: " + doc.getDocStatus());
}
}
doc.saveEx();
} catch (Exception e) {
newState = StateEngine.STATE_Terminated;
setTextMsg("User Choice: " + e.toString());
addTextMsg(e);
log.log(Level.WARNING, "", e);
}
// Send Approval Notification
if (newState.equals(StateEngine.STATE_Aborted)) {
MUser to = new MUser(getCtx(), doc.getDoc_User_ID(), null);
// send email
if (to.isNotificationEMail()) {
MClient client = MClient.get(getCtx(), doc.getAD_Client_ID());
client.sendEMail(doc.getDoc_User_ID(), Msg.getMsg(getCtx(), "NotApproved") + ": " + doc.getDocumentNo(), (doc.getSummary() != null ? doc.getSummary() + "\n" : "") + (doc.getProcessMsg() != null ? doc.getProcessMsg() + "\n" : "") + (getTextMsg() != null ? getTextMsg() : ""), null);
}
// Send Note
if (to.isNotificationNote()) {
MNote note = new MNote(getCtx(), "NotApproved", doc.getDoc_User_ID(), null);
note.setTextMsg((doc.getSummary() != null ? doc.getSummary() + "\n" : "") + (doc.getProcessMsg() != null ? doc.getProcessMsg() + "\n" : "") + (getTextMsg() != null ? getTextMsg() : ""));
// 2007-06-08, matthiasO.
// Add record information to the note, so that the user receiving the
// note can jump to the doc easily
note.setRecord(m_po.get_Table_ID(), m_po.get_ID());
note.saveEx();
}
}
}
setWFState(newState);
return ok;
}
Aggregations