use of org.compiere.model.MClient in project adempiere by adempiere.
the class PaymentServlet method sendThanksEMail.
// processParameter
/**
* Send Payment EMail.
* @param request request
* @param p payment
* @param wu web user
* @param wo optional web order
*/
private void sendThanksEMail(HttpServletRequest request, Properties ctx, MPayment p, WebUser wu, WebOrder wo) {
StringBuffer message = new StringBuffer().append(p.getPayAmt()).append(" (").append(Msg.getElement(ctx, "R_PnRef")).append("=").append(p.getR_PnRef()).append(") ");
if (wo != null)
message.append("\n").append(Msg.getElement(ctx, "C_Order_ID")).append(": ").append(wo.getDocumentNo());
JSPEnv.sendEMail(request, wu, MMailMsg.MAILMSGTYPE_PaymentAcknowledgement, new Object[] { p.getDocumentNo() + " (" + p.getPayAmt() + ")", wu.getName(), message.toString() });
// SalesRep EMail
if (wo != null && wo.getSalesRep_ID() != 0) {
MClient client = MClient.get(ctx);
client.sendEMail(wo.getSalesRep_ID(), "(CC) Payment: " + p.getDocumentNo() + " (" + p.getPayAmt() + ")", "Order: " + wo.getDocumentNo() + "\nUser: " + wu.getName() + " - " + wu.getEmail(), null);
}
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class CostEngine method createCostDetail.
/**
* Generate by transaction
* @param transaction
*/
public void createCostDetail(MTransaction transaction, IDocumentLine model) {
MClient client = new MClient(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName());
StringBuilder description = new StringBuilder();
if (model != null && model.getDescription() != null && !Util.isEmpty(model.getDescription(), true))
description.append(model.getDescription());
if (model != null) {
description.append(model.isSOTrx() ? "(|->)" : "(|<-)");
}
List<MAcctSchema> acctSchemas = new ArrayList(Arrays.asList(MAcctSchema.getClientAcctSchema(transaction.getCtx(), transaction.getAD_Client_ID(), transaction.get_TrxName())));
List<MCostElement> costElements = MCostElement.getCostElement(transaction.getCtx(), transaction.get_TrxName());
List<MCostType> costTypes = MCostType.get(transaction.getCtx(), transaction.get_TrxName());
for (MAcctSchema accountSchema : acctSchemas) {
for (MCostType costType : costTypes) {
if (!costType.isActive())
continue;
for (MCostElement costElement : costElements) {
createCostDetail(accountSchema, costType, costElement, transaction, model, client.isCostImmediate());
}
}
}
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class InventoryValue method doIt.
// prepare
/**
* Perform process.
* <pre>
* - Fill Table with QtyOnHand for Warehouse and Valuation Date
* - Perform Price Calculations
* </pre>
* @return Message
* @throws Exception
*/
protected String doIt() throws Exception {
log.info("M_Warehouse_ID=" + p_M_Warehouse_ID + ",C_Currency_ID=" + p_C_Currency_ID + ",DateValue=" + p_DateValue + ",M_PriceList_Version_ID=" + p_M_PriceList_Version_ID + ",M_CostElement_ID=" + p_M_CostElement_ID);
MWarehouse wh = MWarehouse.get(getCtx(), p_M_Warehouse_ID);
MClient c = MClient.get(getCtx(), wh.getAD_Client_ID());
MAcctSchema as = c.getAcctSchema();
// Delete (just to be sure)
StringBuffer sql = new StringBuffer("DELETE T_InventoryValue WHERE AD_PInstance_ID=");
sql.append(getAD_PInstance_ID());
int no = DB.executeUpdateEx(sql.toString(), get_TrxName());
// Insert Standard Costs
sql = new StringBuffer("INSERT INTO T_InventoryValue " + "(AD_PInstance_ID, M_Warehouse_ID, M_Product_ID, M_AttributeSetInstance_ID," + " AD_Client_ID, AD_Org_ID, CostStandard) " + "SELECT ").append(getAD_PInstance_ID()).append(", w.M_Warehouse_ID, c.M_Product_ID, c.M_AttributeSetInstance_ID," + " w.AD_Client_ID, w.AD_Org_ID, c.CurrentCostPrice " + "FROM M_Warehouse w" + " INNER JOIN AD_ClientInfo ci ON (w.AD_Client_ID=ci.AD_Client_ID)" + " INNER JOIN C_AcctSchema acs ON (ci.C_AcctSchema1_ID=acs.C_AcctSchema_ID)" + " INNER JOIN M_Cost c ON (acs.C_AcctSchema_ID=c.C_AcctSchema_ID AND acs.M_CostType_ID=c.M_CostType_ID AND c.AD_Org_ID IN (0, w.AD_Org_ID))" + " INNER JOIN M_CostElement ce ON (c.M_CostElement_ID=ce.M_CostElement_ID AND ce.CostingMethod='S' AND ce.CostElementType='M') " + "WHERE w.M_Warehouse_ID=").append(p_M_Warehouse_ID);
int noInsertStd = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Inserted Std=" + noInsertStd);
if (noInsertStd == 0)
return "No Standard Costs found";
// Insert addl Costs
int noInsertCost = 0;
if (p_M_CostElement_ID != 0) {
sql = new StringBuffer("INSERT INTO T_InventoryValue " + "(AD_PInstance_ID, M_Warehouse_ID, M_Product_ID, M_AttributeSetInstance_ID," + " AD_Client_ID, AD_Org_ID, CostStandard, Cost, M_CostElement_ID) " + "SELECT ").append(getAD_PInstance_ID()).append(", w.M_Warehouse_ID, c.M_Product_ID, c.M_AttributeSetInstance_ID," + " w.AD_Client_ID, w.AD_Org_ID, 0, c.CurrentCostPrice, c.M_CostElement_ID " + "FROM M_Warehouse w" + " INNER JOIN AD_ClientInfo ci ON (w.AD_Client_ID=ci.AD_Client_ID)" + " INNER JOIN C_AcctSchema acs ON (ci.C_AcctSchema1_ID=acs.C_AcctSchema_ID)" + " INNER JOIN M_Cost c ON (acs.C_AcctSchema_ID=c.C_AcctSchema_ID AND acs.M_CostType_ID=c.M_CostType_ID AND c.AD_Org_ID IN (0, w.AD_Org_ID)) " + "WHERE w.M_Warehouse_ID=").append(p_M_Warehouse_ID).append(" AND c.M_CostElement_ID=").append(p_M_CostElement_ID).append(" AND NOT EXISTS (SELECT * FROM T_InventoryValue iv " + "WHERE iv.AD_PInstance_ID=").append(getAD_PInstance_ID()).append(" AND iv.M_Warehouse_ID=w.M_Warehouse_ID" + " AND iv.M_Product_ID=c.M_Product_ID" + " AND iv.M_AttributeSetInstance_ID=c.M_AttributeSetInstance_ID)");
noInsertCost = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Inserted Cost=" + noInsertCost);
// Update Std Cost Records
sql = new StringBuffer("UPDATE T_InventoryValue iv " + "SET (Cost, M_CostElement_ID)=" + "(SELECT c.CurrentCostPrice, c.M_CostElement_ID " + "FROM M_Warehouse w" + " INNER JOIN AD_ClientInfo ci ON (w.AD_Client_ID=ci.AD_Client_ID)" + " INNER JOIN C_AcctSchema acs ON (ci.C_AcctSchema1_ID=acs.C_AcctSchema_ID)" + " INNER JOIN M_Cost c ON (acs.C_AcctSchema_ID=c.C_AcctSchema_ID" + " AND acs.M_CostType_ID=c.M_CostType_ID AND c.AD_Org_ID IN (0, w.AD_Org_ID)) " + "WHERE c.M_CostElement_ID=" + p_M_CostElement_ID + " AND iv.M_Warehouse_ID=w.M_Warehouse_ID" + " AND iv.M_Product_ID=c.M_Product_ID" + " AND iv.M_AttributeSetInstance_ID=c.M_AttributeSetInstance_ID) " + "WHERE EXISTS (SELECT * FROM T_InventoryValue ivv " + "WHERE ivv.AD_PInstance_ID=" + getAD_PInstance_ID() + " AND ivv.M_CostElement_ID IS NULL)");
int noUpdatedCost = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Updated Cost=" + noUpdatedCost);
}
if ((noInsertStd + noInsertCost) == 0)
return "No Costs found";
// Update Constants
// YYYY-MM-DD HH24:MI:SS.mmmm JDBC Timestamp format
String myDate = p_DateValue.toString();
sql = new StringBuffer("UPDATE T_InventoryValue SET ").append("DateValue=TO_DATE('").append(myDate.substring(0, 10)).append(" 23:59:59','YYYY-MM-DD HH24:MI:SS'),").append("M_PriceList_Version_ID=").append(p_M_PriceList_Version_ID).append(",").append("C_Currency_ID=").append(p_C_Currency_ID).append(" WHERE AD_PInstance_ID=" + getAD_PInstance_ID());
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Constants=" + no);
// Get current QtyOnHand with ASI
sql = new StringBuffer("UPDATE T_InventoryValue iv SET QtyOnHand = " + "(SELECT SUM(QtyOnHand) FROM M_Storage s" + " INNER JOIN M_Locator l ON (l.M_Locator_ID=s.M_Locator_ID) " + "WHERE iv.M_Product_ID=s.M_Product_ID" + " AND iv.M_Warehouse_ID=l.M_Warehouse_ID" + " AND iv.M_AttributeSetInstance_ID=s.M_AttributeSetInstance_ID) " + "WHERE AD_PInstance_ID=").append(getAD_PInstance_ID()).append(" AND iv.M_AttributeSetInstance_ID<>0");
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("QtHand with ASI=" + no);
// Get current QtyOnHand without ASI
sql = new StringBuffer("UPDATE T_InventoryValue iv SET QtyOnHand = " + "(SELECT SUM(QtyOnHand) FROM M_Storage s" + " INNER JOIN M_Locator l ON (l.M_Locator_ID=s.M_Locator_ID) " + "WHERE iv.M_Product_ID=s.M_Product_ID" + " AND iv.M_Warehouse_ID=l.M_Warehouse_ID) " + "WHERE iv.AD_PInstance_ID=").append(getAD_PInstance_ID()).append(" AND iv.M_AttributeSetInstance_ID=0");
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("QtHand w/o ASI=" + no);
// Adjust for Valuation Date
sql = new StringBuffer("UPDATE T_InventoryValue iv " + "SET QtyOnHand=" + "(SELECT iv.QtyOnHand - NVL(SUM(t.MovementQty), 0) " + "FROM M_Transaction t" + " INNER JOIN M_Locator l ON (t.M_Locator_ID=l.M_Locator_ID) " + "WHERE t.M_Product_ID=iv.M_Product_ID" + " AND t.M_AttributeSetInstance_ID=iv.M_AttributeSetInstance_ID" + " AND t.MovementDate > iv.DateValue" + " AND l.M_Warehouse_ID=iv.M_Warehouse_ID) " + "WHERE iv.M_AttributeSetInstance_ID<>0" + " AND iv.AD_PInstance_ID=").append(getAD_PInstance_ID());
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Update with ASI=" + no);
//
sql = new StringBuffer("UPDATE T_InventoryValue iv " + "SET QtyOnHand=" + "(SELECT iv.QtyOnHand - NVL(SUM(t.MovementQty), 0) " + "FROM M_Transaction t" + " INNER JOIN M_Locator l ON (t.M_Locator_ID=l.M_Locator_ID) " + "WHERE t.M_Product_ID=iv.M_Product_ID" + " AND t.MovementDate > iv.DateValue" + " AND l.M_Warehouse_ID=iv.M_Warehouse_ID) " + "WHERE iv.M_AttributeSetInstance_ID=0 " + "AND iv.AD_PInstance_ID=").append(getAD_PInstance_ID());
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Update w/o ASI=" + no);
// Delete Records w/o OnHand Qty
sql = new StringBuffer("DELETE T_InventoryValue " + "WHERE (QtyOnHand=0 OR QtyOnHand IS NULL) AND AD_PInstance_ID=").append(getAD_PInstance_ID());
int noQty = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("NoQty Deleted=" + noQty);
// Update Prices
sql = new StringBuffer("UPDATE T_InventoryValue iv " + "SET PricePO = " + "(SELECT MAX(currencyConvert (po.PriceList,po.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, po.AD_Client_ID,po.AD_Org_ID))" + " FROM M_Product_PO po WHERE po.M_Product_ID=iv.M_Product_ID" + " AND po.IsCurrentVendor='Y'), " + "PriceList = " + "(SELECT currencyConvert(pp.PriceList,pl.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, pl.AD_Client_ID,pl.AD_Org_ID)" + " FROM M_PriceList pl, M_PriceList_Version plv, M_ProductPrice pp" + " WHERE pp.M_Product_ID=iv.M_Product_ID AND pp.M_PriceList_Version_ID=iv.M_PriceList_Version_ID" + " AND pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID" + " AND plv.M_PriceList_ID=pl.M_PriceList_ID), " + "PriceStd = " + "(SELECT currencyConvert(pp.PriceStd,pl.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, pl.AD_Client_ID,pl.AD_Org_ID)" + " FROM M_PriceList pl, M_PriceList_Version plv, M_ProductPrice pp" + " WHERE pp.M_Product_ID=iv.M_Product_ID AND pp.M_PriceList_Version_ID=iv.M_PriceList_Version_ID" + " AND pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID" + " AND plv.M_PriceList_ID=pl.M_PriceList_ID), " + "PriceLimit = " + "(SELECT currencyConvert(pp.PriceLimit,pl.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, pl.AD_Client_ID,pl.AD_Org_ID)" + " FROM M_PriceList pl, M_PriceList_Version plv, M_ProductPrice pp" + " WHERE pp.M_Product_ID=iv.M_Product_ID AND pp.M_PriceList_Version_ID=iv.M_PriceList_Version_ID" + " AND pp.M_PriceList_Version_ID=plv.M_PriceList_Version_ID" + " AND plv.M_PriceList_ID=pl.M_PriceList_ID)" + " WHERE iv.AD_PInstance_ID=").append(getAD_PInstance_ID());
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
String msg = "";
if (no == 0)
msg = "No Prices";
// Convert if different Currency
if (as.getC_Currency_ID() != p_C_Currency_ID) {
sql = new StringBuffer("UPDATE T_InventoryValue iv " + "SET CostStandard= " + "(SELECT currencyConvert(iv.CostStandard,acs.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, iv.AD_Client_ID,iv.AD_Org_ID) " + "FROM C_AcctSchema acs WHERE acs.C_AcctSchema_ID=" + as.getC_AcctSchema_ID() + ")," + " Cost= " + "(SELECT currencyConvert(iv.Cost,acs.C_Currency_ID,iv.C_Currency_ID,iv.DateValue,null, iv.AD_Client_ID,iv.AD_Org_ID) " + "FROM C_AcctSchema acs WHERE acs.C_AcctSchema_ID=" + as.getC_AcctSchema_ID() + ") " + "WHERE iv.AD_PInstance_ID=" + getAD_PInstance_ID());
no = DB.executeUpdateEx(sql.toString(), get_TrxName());
log.fine("Converted=" + no);
}
// Update Values
no = DB.executeUpdateEx("UPDATE T_InventoryValue SET " + "PricePOAmt = QtyOnHand * PricePO, " + "PriceListAmt = QtyOnHand * PriceList, " + "PriceStdAmt = QtyOnHand * PriceStd, " + "PriceLimitAmt = QtyOnHand * PriceLimit, " + "CostStandardAmt = QtyOnHand * CostStandard, " + "CostAmt = QtyOnHand * Cost " + "WHERE AD_PInstance_ID=" + getAD_PInstance_ID(), get_TrxName());
log.fine("Calculation=" + no);
//
return msg;
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class InvoiceGenerate method createLine.
// createLine
/**
* Create Invoice Line from Shipment
* @param order order
* @param ship shipment header
* @param sLine shipment line
*/
private void createLine(MOrder order, MInOut ship, MInOutLine sLine) {
if (m_invoice == null) {
m_invoice = new MInvoice(order, 0, p_DateInvoiced);
if (!m_invoice.save())
throw new IllegalStateException("Could not create Invoice (s)");
}
// Create Shipment Comment Line
if (m_ship == null || m_ship.getM_InOut_ID() != ship.getM_InOut_ID()) {
MDocType dt = MDocType.get(getCtx(), ship.getC_DocType_ID());
if (m_bp == null || m_bp.getC_BPartner_ID() != ship.getC_BPartner_ID())
m_bp = new MBPartner(getCtx(), ship.getC_BPartner_ID(), get_TrxName());
// Reference: Delivery: 12345 - 12.12.12
MClient client = MClient.get(getCtx(), order.getAD_Client_ID());
String AD_Language = client.getAD_Language();
if (client.isMultiLingualDocument() && m_bp.getAD_Language() != null)
AD_Language = m_bp.getAD_Language();
if (AD_Language == null)
AD_Language = Language.getBaseAD_Language();
java.text.SimpleDateFormat format = DisplayType.getDateFormat(DisplayType.Date, Language.getLanguage(AD_Language));
String reference = dt.getPrintName(m_bp.getAD_Language()) + ": " + ship.getDocumentNo() + " - " + format.format(ship.getMovementDate());
m_ship = ship;
//
MInvoiceLine line = new MInvoiceLine(m_invoice);
line.setIsDescription(true);
line.setDescription(reference);
line.setLine(m_line + sLine.getLine() - 2);
if (!line.save())
throw new IllegalStateException("Could not create Invoice Comment Line (sh)");
// Optional Ship Address if not Bill Address
if (order.getBill_Location_ID() != ship.getC_BPartner_Location_ID()) {
MLocation addr = MLocation.getBPLocation(getCtx(), ship.getC_BPartner_Location_ID(), null);
line = new MInvoiceLine(m_invoice);
line.setIsDescription(true);
line.setDescription(addr.toString());
line.setLine(m_line + sLine.getLine() - 1);
if (!line.save())
throw new IllegalStateException("Could not create Invoice Comment Line 2 (sh)");
}
}
//
MInvoiceLine line = new MInvoiceLine(m_invoice);
line.setShipLine(sLine);
if (sLine.sameOrderLineUOM())
line.setQtyEntered(sLine.getQtyEntered());
else
line.setQtyEntered(sLine.getMovementQty());
line.setQtyInvoiced(sLine.getMovementQty());
line.setLine(m_line + sLine.getLine());
//@Trifon - special handling when ShipLine.ToBeInvoiced='N'
String toBeInvoiced = sLine.get_ValueAsString("ToBeInvoiced");
if ("N".equals(toBeInvoiced)) {
line.setPriceEntered(Env.ZERO);
line.setPriceActual(Env.ZERO);
line.setPriceLimit(Env.ZERO);
line.setPriceList(Env.ZERO);
//setC_Tax_ID(oLine.getC_Tax_ID());
line.setLineNetAmt(Env.ZERO);
line.setIsDescription(true);
}
if (!line.save())
throw new IllegalStateException("Could not create Invoice Line (s)");
// Link
sLine.setIsInvoiced(true);
if (!sLine.save())
throw new IllegalStateException("Could not update Shipment Line");
log.fine(line.toString());
}
use of org.compiere.model.MClient in project adempiere by adempiere.
the class InvoicePrint method doIt.
// prepare
/**
* Perform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws java.lang.Exception {
// Need to have Template
if (p_EMailPDF && p_R_MailText_ID == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@");
log.info("C_BPartner_ID=" + m_C_BPartner_ID + ", C_Invoice_ID=" + m_C_Invoice_ID + ", EmailPDF=" + p_EMailPDF + ",R_MailText_ID=" + p_R_MailText_ID + ", DateInvoiced=" + m_dateInvoiced_From + "-" + m_dateInvoiced_To + ", DocumentNo=" + m_DocumentNo_From + "-" + m_DocumentNo_To);
MMailText mText = null;
if (p_R_MailText_ID != 0) {
mText = new MMailText(getCtx(), p_R_MailText_ID, get_TrxName());
if (mText.get_ID() != p_R_MailText_ID)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
}
// Too broad selection
if (m_C_BPartner_ID == 0 && m_C_Invoice_ID == 0 && m_dateInvoiced_From == null && m_dateInvoiced_To == null && m_DocumentNo_From == null && m_DocumentNo_To == null)
throw new AdempiereUserError("@RestrictSelection@");
MClient client = MClient.get(getCtx());
// Get Info
StringBuffer sql = new StringBuffer(// 1..3
"SELECT i.C_Invoice_ID,bp.AD_Language,c.IsMultiLingualDocument," + // Prio: 1. BPartner 2. DocType, 3. PrintFormat (Org) // see ReportCtl+MInvoice
" COALESCE(bp.Invoice_PrintFormat_ID, dt.AD_PrintFormat_ID, pf.Invoice_PrintFormat_ID)," + // 4
" dt.DocumentCopies+bp.DocumentCopies," + // 5
" bpc.AD_User_ID, i.DocumentNo," + // 6..7
" bp.C_BPartner_ID " + // 8
"FROM C_Invoice i" + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)" + " LEFT OUTER JOIN AD_User bpc ON (i.AD_User_ID=bpc.AD_User_ID)" + " INNER JOIN AD_Client c ON (i.AD_Client_ID=c.AD_Client_ID)" + " INNER JOIN AD_PrintForm pf ON (i.AD_Client_ID=pf.AD_Client_ID)" + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)" + " WHERE i.AD_Client_ID=? AND i.AD_Org_ID=? AND i.isSOTrx='Y' AND " + // more them 1 PF
" pf.AD_Org_ID IN (0,i.AD_Org_ID) AND ");
boolean needAnd = false;
if (m_C_Invoice_ID != 0)
sql.append("i.C_Invoice_ID=").append(m_C_Invoice_ID);
else {
if (m_C_BPartner_ID != 0) {
sql.append("i.C_BPartner_ID=").append(m_C_BPartner_ID);
needAnd = true;
}
if (m_dateInvoiced_From != null && m_dateInvoiced_To != null) {
if (needAnd)
sql.append(" AND ");
sql.append("TRUNC(i.DateInvoiced, 'DD') BETWEEN ").append(DB.TO_DATE(m_dateInvoiced_From, true)).append(" AND ").append(DB.TO_DATE(m_dateInvoiced_To, true));
needAnd = true;
} else if (m_dateInvoiced_From != null) {
if (needAnd)
sql.append(" AND ");
sql.append("TRUNC(i.DateInvoiced, 'DD') >= ").append(DB.TO_DATE(m_dateInvoiced_From, true));
needAnd = true;
} else if (m_dateInvoiced_To != null) {
if (needAnd)
sql.append(" AND ");
sql.append("TRUNC(i.DateInvoiced, 'DD') <= ").append(DB.TO_DATE(m_dateInvoiced_To, true));
needAnd = true;
} else if (m_DocumentNo_From != null && m_DocumentNo_To != null) {
if (needAnd)
sql.append(" AND ");
sql.append("i.DocumentNo BETWEEN ").append(DB.TO_STRING(m_DocumentNo_From)).append(" AND ").append(DB.TO_STRING(m_DocumentNo_To));
} else if (m_DocumentNo_From != null) {
if (needAnd)
sql.append(" AND ");
if (m_DocumentNo_From.indexOf('%') == -1)
sql.append("i.DocumentNo >= ").append(DB.TO_STRING(m_DocumentNo_From));
else
sql.append("i.DocumentNo LIKE ").append(DB.TO_STRING(m_DocumentNo_From));
}
if (p_EMailPDF) {
if (needAnd) {
sql.append(" AND ");
}
/* if emailed to customer only select COmpleted & CLosed invoices */
sql.append("i.DocStatus IN ('CO','CL') ");
}
}
// more than 1 PF record
sql.append(" ORDER BY i.C_Invoice_ID, pf.AD_Org_ID DESC");
log.fine(sql.toString());
MPrintFormat format = null;
int old_AD_PrintFormat_ID = -1;
int old_C_Invoice_ID = -1;
int C_BPartner_ID = 0;
int count = 0;
int errors = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
pstmt.setInt(2, Env.getAD_Org_ID(Env.getCtx()));
rs = pstmt.executeQuery();
while (rs.next()) {
int C_Invoice_ID = rs.getInt(1);
if (// multiple pf records
C_Invoice_ID == old_C_Invoice_ID)
continue;
old_C_Invoice_ID = C_Invoice_ID;
// Set Language when enabled
// Base Language
Language language = Language.getLoginLanguage();
String AD_Language = rs.getString(2);
if (AD_Language != null && "Y".equals(rs.getString(3)))
language = Language.getLanguage(AD_Language);
//
int AD_PrintFormat_ID = rs.getInt(4);
int copies = rs.getInt(5);
if (copies == 0)
copies = 1;
int AD_User_ID = rs.getInt(6);
MUser to = new MUser(getCtx(), AD_User_ID, get_TrxName());
String DocumentNo = rs.getString(7);
C_BPartner_ID = rs.getInt(8);
//
String documentDir = client.getDocumentDir();
if (documentDir == null || documentDir.length() == 0)
documentDir = ".";
//
if (p_EMailPDF && (to.get_ID() == 0 || to.getEMail() == null || to.getEMail().length() == 0)) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailNoTo@");
errors++;
continue;
}
if (AD_PrintFormat_ID == 0) {
addLog(C_Invoice_ID, null, null, DocumentNo + " No Print Format");
errors++;
continue;
}
// Get Format & Data
if (AD_PrintFormat_ID != old_AD_PrintFormat_ID) {
format = MPrintFormat.get(getCtx(), AD_PrintFormat_ID, false);
old_AD_PrintFormat_ID = AD_PrintFormat_ID;
}
format.setLanguage(language);
format.setTranslationLanguage(language);
// query
MQuery query = new MQuery("C_Invoice_Header_v");
query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer(C_Invoice_ID));
// Engine
PrintInfo info = new PrintInfo(DocumentNo, X_C_Invoice.Table_ID, C_Invoice_ID, C_BPartner_ID);
info.setCopies(copies);
ReportEngine re = new ReportEngine(getCtx(), format, query, info);
boolean printed = false;
if (p_EMailPDF) {
String subject = mText.getMailHeader() + " - " + DocumentNo;
EMail email = client.createEMail(to.getEMail(), subject, null);
if (!email.isValid()) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailError@ Invalid EMail: " + to);
errors++;
continue;
}
// Context
mText.setUser(to);
// Context
mText.setBPartner(C_BPartner_ID);
mText.setPO(new MInvoice(getCtx(), C_Invoice_ID, get_TrxName()));
String message = mText.getMailText(true);
if (mText.isHtml())
email.setMessageHTML(subject, message);
else {
email.setSubject(subject);
email.setMessageText(message);
}
//
File invoice = null;
if (!Ini.isClient())
invoice = new File(MInvoice.getPDFFileName(documentDir, C_Invoice_ID));
File attachment = re.getPDF(invoice);
log.fine(to + " - " + attachment);
email.addAttachment(attachment);
//
String msg = email.send();
MUserMail um = new MUserMail(mText, getAD_User_ID(), email);
um.saveEx();
if (msg.equals(EMail.SENT_OK)) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailOK@ - " + to.getEMail());
count++;
printed = true;
} else {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailError@ " + msg + " - " + to.getEMail());
errors++;
}
} else {
ServerReportCtl.startDocumentPrint(ReportEngine.INVOICE, // No custom print format
null, C_Invoice_ID, // No custom printer
null, null);
count++;
printed = true;
}
// Print Confirm
if (printed) {
StringBuffer sb = new StringBuffer("UPDATE C_Invoice " + "SET DatePrinted=SysDate, IsPrinted='Y' WHERE C_Invoice_ID=").append(C_Invoice_ID);
int no = DB.executeUpdate(sb.toString(), get_TrxName());
}
}
// for all entries
} catch (Exception e) {
log.log(Level.SEVERE, "doIt - " + sql, e);
throw new Exception(e);
} finally {
DB.close(rs, pstmt);
}
//
if (p_EMailPDF)
return "@Sent@=" + count + " - @Errors@=" + errors;
return "@Printed@=" + count;
}
Aggregations