use of org.compiere.model.MOrderLine in project adempiere by adempiere.
the class CreateFromInvoice method save.
/**
* Save - Create Invoice Lines
* @return true if saved
*/
public boolean save(IMiniTable miniTable, String trxName) {
// Invoice
// Yamel Senih FR [ 114 ] get Record ID from record
MInvoice invoice = new MInvoice(Env.getCtx(), m_Record_ID, trxName);
log.config(invoice.toString());
if (p_order != null) {
// overwrite header values
invoice.setOrder(p_order);
invoice.saveEx();
}
if (m_rma != null) {
invoice.setM_RMA_ID(m_rma.getM_RMA_ID());
invoice.saveEx();
}
// Lines
for (int i = 0; i < miniTable.getRowCount(); i++) {
if (((Boolean) miniTable.getValueAt(i, 0)).booleanValue()) {
MProduct product = null;
// variable values
// 1-Qty
BigDecimal QtyEntered = (BigDecimal) miniTable.getValueAt(i, 1);
// 2-UOM
KeyNamePair pp = (KeyNamePair) miniTable.getValueAt(i, 2);
int C_UOM_ID = pp.getKey();
//
// 3-Product
pp = (KeyNamePair) miniTable.getValueAt(i, 3);
int M_Product_ID = 0;
if (pp != null)
M_Product_ID = pp.getKey();
//
int C_OrderLine_ID = 0;
// 5-OrderLine
pp = (KeyNamePair) miniTable.getValueAt(i, 5);
if (pp != null)
C_OrderLine_ID = pp.getKey();
int M_InOutLine_ID = 0;
// 6-Shipment
pp = (KeyNamePair) miniTable.getValueAt(i, 6);
if (pp != null)
M_InOutLine_ID = pp.getKey();
//
int M_RMALine_ID = 0;
// 7-RMALine
pp = (KeyNamePair) miniTable.getValueAt(i, 7);
if (pp != null)
M_RMALine_ID = pp.getKey();
// Precision of Qty UOM
int precision = 2;
if (M_Product_ID != 0) {
product = MProduct.get(Env.getCtx(), M_Product_ID);
precision = product.getUOMPrecision();
}
QtyEntered = QtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
//
log.fine("Line QtyEntered=" + QtyEntered + ", Product_ID=" + M_Product_ID + ", OrderLine_ID=" + C_OrderLine_ID + ", InOutLine_ID=" + M_InOutLine_ID);
// Create new Invoice Line
MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
// Line UOM
invoiceLine.setM_Product_ID(M_Product_ID, C_UOM_ID);
// Invoiced/Entered
invoiceLine.setQty(QtyEntered);
BigDecimal QtyInvoiced = null;
if (M_Product_ID > 0 && product.getC_UOM_ID() != C_UOM_ID) {
QtyInvoiced = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, QtyEntered);
}
if (QtyInvoiced == null)
QtyInvoiced = QtyEntered;
invoiceLine.setQtyInvoiced(QtyInvoiced);
// Info
MOrderLine orderLine = null;
if (C_OrderLine_ID != 0)
orderLine = new MOrderLine(Env.getCtx(), C_OrderLine_ID, trxName);
//
MRMALine rmaLine = null;
if (M_RMALine_ID > 0)
rmaLine = new MRMALine(Env.getCtx(), M_RMALine_ID, null);
//
MInOutLine inoutLine = null;
if (M_InOutLine_ID != 0) {
inoutLine = new MInOutLine(Env.getCtx(), M_InOutLine_ID, trxName);
if (orderLine == null && inoutLine.getC_OrderLine_ID() != 0) {
C_OrderLine_ID = inoutLine.getC_OrderLine_ID();
orderLine = new MOrderLine(Env.getCtx(), C_OrderLine_ID, trxName);
}
} else if (C_OrderLine_ID > 0) {
String whereClause = "EXISTS (SELECT 1 FROM M_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('CO','CL'))";
MInOutLine[] lines = MInOutLine.getOfOrderLine(Env.getCtx(), C_OrderLine_ID, whereClause, trxName);
log.fine("Receipt Lines with OrderLine = #" + lines.length);
if (lines.length > 0) {
for (int j = 0; j < lines.length; j++) {
MInOutLine line = lines[j];
if (line.getQtyEntered().compareTo(QtyEntered) == 0) {
inoutLine = line;
M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
break;
}
}
if (inoutLine == null) {
// first as default
inoutLine = lines[0];
M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
}
}
} else if (M_RMALine_ID != 0) {
String whereClause = "EXISTS (SELECT 1 FROM M_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('CO','CL'))";
MInOutLine[] lines = MInOutLine.getOfRMALine(Env.getCtx(), M_RMALine_ID, whereClause, null);
log.fine("Receipt Lines with RMALine = #" + lines.length);
if (lines.length > 0) {
for (int j = 0; j < lines.length; j++) {
MInOutLine line = lines[j];
if (rmaLine.getQty().compareTo(QtyEntered) == 0) {
inoutLine = line;
M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
break;
}
}
if (rmaLine == null) {
// first as default
inoutLine = lines[0];
M_InOutLine_ID = inoutLine.getM_InOutLine_ID();
}
}
}
// Shipment Info
if (inoutLine != null) {
// overwrites
invoiceLine.setShipLine(inoutLine);
} else {
log.fine("No Receipt Line");
// Order Info
if (orderLine != null) {
// overwrites
invoiceLine.setOrderLine(orderLine);
} else {
log.fine("No Order Line");
invoiceLine.setPrice();
invoiceLine.setTax();
}
//RMA Info
if (rmaLine != null) {
// overwrites
invoiceLine.setRMALine(rmaLine);
} else
log.fine("No RMA Line");
}
invoiceLine.saveEx();
}
// if selected
}
return true;
}
use of org.compiere.model.MOrderLine in project adempiere by adempiere.
the class MyValidator method orderDiscount.
// docValidate
/**
* Order Discount.
* Make sure that last line is discount and check correctness
* @param order order
* @return error message or null
*/
private String orderDiscount(MOrder order) {
String DISCOUNT = "Discount";
int C_Tax_ID = 0;
BigDecimal totalLines = Env.ZERO;
MOrderLine discountLine = null;
//
MOrderLine[] lines = order.getLines();
for (int i = 0; i < lines.length; i++) {
MOrderLine oLine = lines[i];
String description = oLine.getDescription();
if (description != null && description.equals(DISCOUNT))
discountLine = oLine;
else {
totalLines = totalLines.add(oLine.getLineNetAmt());
if (C_Tax_ID == 0)
C_Tax_ID = oLine.getC_Tax_ID();
else if (C_Tax_ID != oLine.getC_Tax_ID())
return "Order has more then one Tax, cannot add discount";
}
}
if (discountLine == null) {
discountLine = new MOrderLine(order);
discountLine.setDescription(DISCOUNT);
discountLine.setQty(Env.ONE);
discountLine.setC_Tax_ID(C_Tax_ID);
discountLine.setLine(9999);
}
// Calculate Discount
// 3% example
BigDecimal discountPercent = new BigDecimal(3);
BigDecimal discountAmt = totalLines.multiply(discountPercent);
discountAmt = discountAmt.divide(Env.ONEHUNDRED, order.getPrecision(), BigDecimal.ROUND_HALF_UP);
discountLine.setPrice(discountAmt.negate());
if (!discountLine.save())
return "Could not save discount line";
log.info(discountLine.toString());
order.getLines(true, null);
return null;
}
use of org.compiere.model.MOrderLine in project adempiere by adempiere.
the class OrderTest method run.
/**
* Run
*/
public void run() {
long time = System.currentTimeMillis();
int count = 0;
MBPartner bp = new MBPartner(Env.getCtx(), 117, null);
bp.setSOCreditStatus(MBPartner.SOCREDITSTATUS_NoCreditCheck);
bp.saveEx();
//
for (int i = 0; i < m_numberOrders; i++) {
Trx trx = Trx.get(Trx.createTrxName("Test" + m_no + "_" + i), true);
trx.start();
//
MOrder order = new MOrder(Env.getCtx(), 0, trx.getTrxName());
order.setDescription("#" + m_no + "_" + i);
// POS
order.setC_DocTypeTarget_ID(135);
// C&W
order.setC_BPartner_ID(117);
// GardenAdmin
order.setSalesRep_ID(101);
order.setDeliveryRule(MOrder.DELIVERYRULE_Force);
if (!order.save()) {
log.warning("#" + m_no + "_" + i + ": Not saved(1)");
m_errors++;
continue;
}
Random r = new Random();
int linesNumber = r.nextInt(m_maxLines) + 1;
for (int j = 0; j < linesNumber; j++) {
MOrderLine line = new MOrderLine(order);
// Oak Tree
line.setM_Product_ID(123);
line.setQty(new BigDecimal(5));
if (!line.save()) {
log.warning("#" + m_no + "_" + i + ": Line not saved");
m_errors++;
}
}
// Process
order.setDocAction(DocAction.ACTION_Complete);
if (!order.processIt(DocAction.ACTION_Complete)) {
log.warning("#" + m_no + "_" + i + ": Not processed");
m_errors++;
trx.rollback();
trx.close();
continue;
}
if (!order.save()) {
log.warning("#" + m_no + "_" + i + ": Not saved(2)");
m_errors++;
} else
count++;
trx.commit();
trx.close();
//
log.info(order.toString());
}
time = System.currentTimeMillis() - time;
log.warning("#" + m_no + ", Errors=" + m_errors + ", Count=" + count + " " + ((float) count * 100 / m_numberOrders) + "% - " + time + "ms - ea " + ((float) time / count) + "ms");
}
use of org.compiere.model.MOrderLine in project adempiere by adempiere.
the class MPPMRP method C_Order.
/**
* Create MRP record based in Order
* @param MOrder
*/
public static void C_Order(MOrder o) {
MDocType dt = MDocType.get(o.getCtx(), o.getC_DocTypeTarget_ID());
String DocSubTypeSO = dt.getDocSubTypeSO();
if (MDocType.DOCSUBTYPESO_StandardOrder.equals(DocSubTypeSO) || MDocType.DOCSUBTYPESO_WarehouseOrder.equals(DocSubTypeSO) || MDocType.DOCSUBTYPESO_OnCreditOrder.equals(DocSubTypeSO) || MDocType.DOCSUBTYPESO_POSOrder.equals(DocSubTypeSO) || !o.isSOTrx()) {
if ((o.getDocStatus().equals(MOrder.DOCSTATUS_InProgress) || o.getDocStatus().equals(MOrder.DOCSTATUS_Completed)) || !o.isSOTrx()) {
for (MOrderLine line : o.getLines()) {
C_OrderLine(line);
}
}
if (o.is_ValueChanged(MOrder.COLUMNNAME_DocStatus) || o.is_ValueChanged(MOrder.COLUMNNAME_C_BPartner_ID)) {
List<MPPMRP> list = getQuery(o, null, null).list();
for (MPPMRP mrp : list) {
mrp.setC_Order(o);
mrp.saveEx();
}
}
}
}
use of org.compiere.model.MOrderLine in project adempiere by adempiere.
the class PromotionRule method applyPromotionsByLine.
public static void applyPromotionsByLine(MOrder order, MOrderLine ol) throws Exception {
MOrderLine bonLine = null;
String whereClause = "m_product_ID=? and isgiveaway='Y'";
MOrderLine oldbonLine = new Query(ol.getCtx(), MOrderLine.Table_Name, whereClause, ol.get_TrxName()).setParameters(ol.getM_Product_ID()).first();
if (oldbonLine != null)
oldbonLine.deleteEx(false);
MPromotion pr = new MPromotion(ol.getCtx(), ol.getM_Promotion_ID(), ol.get_TrxName());
whereClause = "m_promotion_id =? and validfrom<=? and c_bpartner_id =?";
int promLine = new Query(order.getCtx(), MPromotionLine.Table_Name, whereClause, order.get_TrxName()).setParameters(new Object[] { pr.getM_Promotion_ID(), order.getDateOrdered(), order.getC_BPartner_ID() }).setOrderBy("validfrom desc").firstId();
if (promLine <= 0) {
whereClause = "m_promotion_id =? and trunc(validfrom)<=? and c_bpartner_id is null";
promLine = new Query(order.getCtx(), MPromotionLine.Table_Name, whereClause, order.get_TrxName()).setParameters(new Object[] { pr.getM_Promotion_ID(), order.getDateOrdered() }).setOrderBy("validfrom desc").firstId();
}
if (promLine <= 0)
return;
whereClause = "m_promotionline_ID=?";
List<MPromotionDistribution> pds = new Query(order.getCtx(), MPromotionDistribution.Table_Name, whereClause, order.get_TrxName()).setParameters(promLine).setOrderBy("qty desc").list();
for (MPromotionDistribution dist : pds) {
if (ol.getQtyOrdered().doubleValue() >= dist.getQty().doubleValue()) {
bonLine = new MOrderLine(order);
MOrderLine.copyValues(ol, bonLine);
bonLine.setPrice(Env.ZERO);
bonLine.setDiscount(Env.ONEHUNDRED);
bonLine.setQty(dist.getQtyReward());
bonLine.setDescription("Bonificaciones");
bonLine.set_ValueOfColumn("isGiveAway", true);
bonLine.saveEx();
break;
}
}
}
Aggregations