use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class InvoiceGenerateFromShipment method generate.
//@Trifon
private String generate(PreparedStatement pstmt) {
ResultSet rs = null;
MInvoice invoice = null;
try {
rs = pstmt.executeQuery();
while (rs.next()) {
MInOut inOut = new MInOut(getCtx(), rs, get_TrxName());
if (// ignore incomplete or reversals
!inOut.isComplete() || inOut.getDocStatus().equals(MInOut.DOCSTATUS_Reversed)) {
continue;
}
MOrder order = new MOrder(getCtx(), inOut.getC_Order_ID(), get_TrxName());
// New Invoice Location
if (!p_ConsolidateDocument || (invoice != null && invoice.getC_BPartner_Location_ID() != order.getBill_Location_ID())) {
invoice = completeInvoice(invoice);
}
// boolean completeOrder = MOrder.INVOICERULE_AfterOrderDelivered.equals(order.getInvoiceRule());
// Schedule After Delivery
boolean doInvoice = false;
if (MOrder.INVOICERULE_CustomerScheduleAfterDelivery.equals(order.getInvoiceRule())) {
m_bp = new MBPartner(getCtx(), order.getBill_BPartner_ID(), null);
if (m_bp.getC_InvoiceSchedule_ID() == 0) {
log.warning("BPartner has no Schedule - set to After Delivery");
order.setInvoiceRule(MOrder.INVOICERULE_AfterDelivery);
order.save();
} else {
MInvoiceSchedule is = MInvoiceSchedule.get(getCtx(), m_bp.getC_InvoiceSchedule_ID(), get_TrxName());
if (is.canInvoice(order.getDateOrdered(), order.getGrandTotal()))
doInvoice = true;
else
continue;
}
}
// After Delivery - Invoice per Delivery
if (doInvoice || MOrder.INVOICERULE_AfterDelivery.equals(order.getInvoiceRule())) {
MInOutLine[] shipLines = inOut.getLines(false);
for (int j = 0; j < shipLines.length; j++) {
MInOutLine inOutLine = shipLines[j];
if (!inOutLine.isInvoiced()) {
invoice = createInvoiceLineFromShipmentLine(invoice, order, inOut, inOutLine);
}
}
m_line += 1000;
} else // After Order Delivered, Immediate
{
throw new AdempiereException("Not supported Invoice Rules[Immediate, AfterOrderDelivered]");
}
}
// for all Shipments
} catch (Exception e) {
log.log(Level.SEVERE, "", e);
} finally {
DB.close(rs, pstmt);
pstmt = null;
}
completeInvoice(invoice);
return "@Created@ = " + m_created;
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class InvoiceGenerateRMA method createInvoice.
private MInvoice createInvoice(MRMA rma) {
int docTypeId = getInvoiceDocTypeId(rma.get_ID());
if (docTypeId == -1) {
throw new IllegalStateException("Could not get invoice document type for Vendor RMA");
}
MInvoice invoice = new MInvoice(getCtx(), 0, get_TrxName());
invoice.setRMA(rma);
invoice.setC_DocTypeTarget_ID(docTypeId);
if (!invoice.save()) {
throw new IllegalStateException("Could not create invoice");
}
return invoice;
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class CommissionAPInvoice method doIt.
// prepare
/**
* Perform process.
* @return Message (variables are parsed)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("doIt - C_CommissionRun_ID=" + getRecord_ID());
// Load Data
MCommissionRun comRun = new MCommissionRun(getCtx(), getRecord_ID(), get_TrxName());
if (comRun.get_ID() == 0)
throw new IllegalArgumentException("CommissionAPInvoice - No Commission Run");
if (Env.ZERO.compareTo(comRun.getGrandTotal()) == 0)
throw new IllegalArgumentException("@GrandTotal@ = 0");
MCommission com = new MCommission(getCtx(), comRun.getC_Commission_ID(), get_TrxName());
if (com.get_ID() == 0)
throw new IllegalArgumentException("CommissionAPInvoice - No Commission");
if (com.getC_Charge_ID() == 0)
throw new IllegalArgumentException("CommissionAPInvoice - No Charge on Commission");
MBPartner bp = new MBPartner(getCtx(), com.getC_BPartner_ID(), get_TrxName());
if (bp.get_ID() == 0)
throw new IllegalArgumentException("CommissionAPInvoice - No BPartner");
// Create Invoice
MInvoice invoice = new MInvoice(getCtx(), 0, null);
invoice.setClientOrg(com.getAD_Client_ID(), com.getAD_Org_ID());
// API
invoice.setC_DocTypeTarget_ID(MDocType.DOCBASETYPE_APInvoice);
invoice.setBPartner(bp);
// invoice.setDocumentNo (comRun.getDocumentNo()); // may cause unique constraint
// caller
invoice.setSalesRep_ID(getAD_User_ID());
//
if (com.getC_Currency_ID() != invoice.getC_Currency_ID())
throw new IllegalArgumentException("CommissionAPInvoice - Currency of PO Price List not Commission Currency");
//
if (!invoice.save())
throw new IllegalStateException("CommissionAPInvoice - cannot save Invoice");
// Create Invoice Line
MInvoiceLine iLine = new MInvoiceLine(invoice);
iLine.setC_Charge_ID(com.getC_Charge_ID());
iLine.setQty(1);
iLine.setPrice(comRun.getGrandTotal());
iLine.setTax();
if (!iLine.save())
throw new IllegalStateException("CommissionAPInvoice - cannot save Invoice Line");
//
return "@C_Invoice_ID@ = " + invoice.getDocumentNo();
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class CopyFromInvoice method doIt.
// prepare
/**
* Perform process.
* @return Message
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
int To_C_Invoice_ID = getRecord_ID();
log.info("From C_Invoice_ID=" + m_C_Invoice_ID + " to " + To_C_Invoice_ID);
if (To_C_Invoice_ID == 0)
throw new IllegalArgumentException("Target C_Invoice_ID == 0");
if (m_C_Invoice_ID == 0)
throw new IllegalArgumentException("Source C_Invoice_ID == 0");
MInvoice from = new MInvoice(getCtx(), m_C_Invoice_ID, get_TrxName());
MInvoice to = new MInvoice(getCtx(), To_C_Invoice_ID, get_TrxName());
//
int no = to.copyLinesFrom(from, false, false);
//
return "@Copied@=" + no;
}
use of org.compiere.model.MInvoice in project adempiere by adempiere.
the class OrderRePrice method doIt.
// prepare
/**
* Perform process.
* @return Message (clear text)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("C_Order_ID=" + p_C_Order_ID + ", C_Invoice_ID=" + p_C_Invoice_ID);
if (p_C_Order_ID == 0 && p_C_Invoice_ID == 0)
throw new IllegalArgumentException("Nothing to do");
String retValue = "";
if (p_C_Order_ID != 0) {
MOrder order = new MOrder(getCtx(), p_C_Order_ID, get_TrxName());
BigDecimal oldPrice = order.getGrandTotal();
MOrderLine[] lines = order.getLines();
for (int i = 0; i < lines.length; i++) {
lines[i].setPrice(order.getM_PriceList_ID());
lines[i].saveEx();
}
order = new MOrder(getCtx(), p_C_Order_ID, get_TrxName());
BigDecimal newPrice = order.getGrandTotal();
retValue = order.getDocumentNo() + ": " + oldPrice + " -> " + newPrice;
}
if (p_C_Invoice_ID != 0) {
MInvoice invoice = new MInvoice(getCtx(), p_C_Invoice_ID, null);
BigDecimal oldPrice = invoice.getGrandTotal();
MInvoiceLine[] lines = invoice.getLines(false);
for (int i = 0; i < lines.length; i++) {
lines[i].setPrice(invoice.getM_PriceList_ID(), invoice.getC_BPartner_ID());
lines[i].saveEx();
}
invoice = new MInvoice(getCtx(), p_C_Invoice_ID, null);
BigDecimal newPrice = invoice.getGrandTotal();
if (retValue.length() > 0)
retValue += Env.NL;
retValue += invoice.getDocumentNo() + ": " + oldPrice + " -> " + newPrice;
}
//
return retValue;
}
Aggregations