use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class InvoiceGenerate method createLine.
// generate
/**************************************************************************
* Create Invoice Line from Order Line
* @param order order
* @param orderLine line
* @param qtyInvoiced qty
* @param qtyEntered qty
*/
private void createLine(MOrder order, MOrderLine orderLine, BigDecimal qtyInvoiced, BigDecimal qtyEntered) {
if (m_invoice == null) {
m_invoice = new MInvoice(order, 0, p_DateInvoiced);
if (!m_invoice.save())
throw new IllegalStateException("Could not create Invoice (o)");
}
//
MInvoiceLine line = new MInvoiceLine(m_invoice);
line.setOrderLine(orderLine);
line.setQtyInvoiced(qtyInvoiced);
line.setQtyEntered(qtyEntered);
line.setLine(m_line + orderLine.getLine());
if (!line.save())
throw new IllegalStateException("Could not create Invoice Line (o)");
log.fine(line.toString());
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class InvoicePayScheduleValidate method doIt.
// prepare
/**
* Perform process.
* @return Message (clear text)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("C_InvoicePaySchedule_ID=" + getRecord_ID());
MInvoicePaySchedule[] schedule = MInvoicePaySchedule.getInvoicePaySchedule(getCtx(), 0, getRecord_ID(), null);
if (schedule.length == 0)
throw new IllegalArgumentException("InvoicePayScheduleValidate - No Schedule");
// Get Invoice
MInvoice invoice = new MInvoice(getCtx(), schedule[0].getC_Invoice_ID(), null);
if (invoice.get_ID() == 0)
throw new IllegalArgumentException("InvoicePayScheduleValidate - No Invoice");
//
BigDecimal total = Env.ZERO;
for (int i = 0; i < schedule.length; i++) {
BigDecimal due = schedule[i].getDueAmt();
if (due != null)
total = total.add(due);
}
boolean valid = invoice.getGrandTotal().compareTo(total) == 0;
invoice.setIsPayScheduleValid(valid);
invoice.saveEx();
// Schedule
for (int i = 0; i < schedule.length; i++) {
if (schedule[i].isValid() != valid) {
schedule[i].setIsValid(valid);
schedule[i].saveEx();
}
}
String msg = "@OK@";
if (!valid)
msg = "@GrandTotal@ = " + invoice.getGrandTotal() + " <> @Total@ = " + total + " - @Difference@ = " + invoice.getGrandTotal().subtract(total);
return Msg.parseTranslation(getCtx(), msg);
}
use of org.compiere.model.MInvoice 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;
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class VPayment method actionPerformed.
// loadCurrencies
/**************************************************************************
* Action Listener
* @param e event
*/
public void actionPerformed(ActionEvent e) {
log.fine("VPayment.actionPerformed - " + e.getActionCommand());
// Finish
if (e.getActionCommand().equals(ConfirmPanel.A_OK)) {
if (checkMandatory()) {
// cannot recover
saveChanges();
dispose();
}
} else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
dispose();
else // Payment Method Change
if (e.getSource() == paymentCombo) {
// get selection
ValueNamePair pp = (ValueNamePair) paymentCombo.getSelectedItem();
if (pp != null) {
String s = pp.getValue().toLowerCase();
if (X_C_Order.PAYMENTRULE_DirectDebit.equalsIgnoreCase(s))
s = X_C_Order.PAYMENTRULE_DirectDeposit.toLowerCase();
s += "Panel";
// switch to panel
centerLayout.show(centerPanel, s);
//Bojana&Daniel
//If Invoice is Vendor invoice then Cash has to be created by negative amount
int C_Invoice_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "C_Invoice_ID");
MInvoice invoice_tmp = new MInvoice(Env.getCtx(), C_Invoice_ID, null);
if (!invoice_tmp.isSOTrx()) {
bAmountField.setValue(m_Amount.negate());
} else {
bAmountField.setValue(m_Amount);
}
invoice_tmp = null;
}
} else // Check Currency change
if (e.getSource() == sCurrencyCombo) {
KeyNamePair pp = (KeyNamePair) sCurrencyCombo.getSelectedItem();
BigDecimal amt = MConversionRate.convert(Env.getCtx(), m_Amount, m_C_Currency_ID, pp.getKey(), m_AD_Client_ID, m_AD_Org_ID);
sAmountField.setValue(amt);
} else // Cash Currency change
if (e.getSource() == bCurrencyCombo) {
KeyNamePair pp = (KeyNamePair) bCurrencyCombo.getSelectedItem();
BigDecimal amt = MConversionRate.convert(Env.getCtx(), m_Amount, m_C_Currency_ID, pp.getKey(), m_AD_Client_ID, m_AD_Org_ID);
bAmountField.setValue(amt);
} else // Online
if (e.getSource() == kOnline || e.getSource() == sOnline)
processOnline();
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class ReverseTheSalesTransaction method doIt.
@Override
protected String doIt() throws Exception {
today = new Timestamp(System.currentTimeMillis());
// Get Order
MOrder sourceOrder = new MOrder(getCtx(), getOrderId(), get_TrxName());
// Get Invoices for ths order
MInOut[] shipments = sourceOrder.getShipments();
// If not exist invoice then only is necessary reverse shipment
if (shipments.length > 0) {
// Validate if partner not is POS partner standard then reverse shipment
if (sourceOrder.getC_BPartner_ID() != getInvoicePartnerId() || isCancelled()) {
cancelShipments(shipments);
}
}
MInvoice[] invoices = sourceOrder.getInvoices();
if (invoices.length > 0) {
if (sourceOrder.getC_BPartner_ID() != getInvoicePartnerId() || isCancelled())
cancelInvoices();
}
//Cancel original payment
for (MPayment payment : cancelPayments(sourceOrder, today)) addLog(payment.getDocumentInfo());
sourceOrder.processIt(DocAction.ACTION_Close);
sourceOrder.saveEx();
return "@Ok@";
}
Aggregations