use of org.compiere.model.MWarehouse in project adempiere by adempiere.
the class BPartnerOrgLink method doIt.
// prepare
/**
* Perform process.
* @return Message (text with variables)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("C_BPartner_ID=" + p_C_BPartner_ID + ", AD_Org_ID=" + p_AD_Org_ID + ", AD_OrgType_ID=" + p_AD_OrgType_ID + ", AD_Role_ID=" + p_AD_Role_ID);
if (p_C_BPartner_ID == 0)
throw new AdempiereUserError("No Business Partner ID");
MBPartner bp = new MBPartner(getCtx(), p_C_BPartner_ID, get_TrxName());
if (bp.get_ID() == 0)
throw new AdempiereUserError("Business Partner not found - C_BPartner_ID=" + p_C_BPartner_ID);
// BP Location
MBPartnerLocation[] locs = bp.getLocations(false);
if (locs == null || locs.length == 0)
throw new IllegalArgumentException("Business Partner has no Location");
// Location
int C_Location_ID = locs[0].getC_Location_ID();
if (C_Location_ID == 0)
throw new IllegalArgumentException("Business Partner Location has no Address");
// Create Org
boolean newOrg = p_AD_Org_ID == 0;
MOrg org = new MOrg(getCtx(), p_AD_Org_ID, get_TrxName());
if (newOrg) {
org.setValue(bp.getValue());
org.setName(bp.getName());
org.setDescription(bp.getDescription());
if (!org.save())
throw new Exception("Organization not saved");
} else // check if linked to already
{
int C_BPartner_ID = org.getLinkedC_BPartner_ID(get_TrxName());
if (C_BPartner_ID > 0)
throw new IllegalArgumentException("Organization '" + org.getName() + "' already linked (to C_BPartner_ID=" + C_BPartner_ID + ")");
}
p_AD_Org_ID = org.getAD_Org_ID();
// Update Org Info
MOrgInfo oInfo = org.getInfo();
oInfo.setAD_OrgType_ID(p_AD_OrgType_ID);
if (newOrg)
oInfo.setC_Location_ID(C_Location_ID);
// Create Warehouse
MWarehouse wh = null;
if (!newOrg) {
MWarehouse[] whs = MWarehouse.getForOrg(getCtx(), p_AD_Org_ID);
if (whs != null && whs.length > 0)
// pick first
wh = whs[0];
}
// New Warehouse
if (wh == null) {
wh = new MWarehouse(org);
if (!wh.save(get_TrxName()))
throw new Exception("Warehouse not saved");
}
// Create Locator
MLocator mLoc = wh.getDefaultLocator();
if (mLoc == null) {
mLoc = new MLocator(wh, "Standard");
mLoc.setIsDefault(true);
mLoc.save(get_TrxName());
}
// Update/Save Org Info
oInfo.setM_Warehouse_ID(wh.getM_Warehouse_ID());
if (!oInfo.save(get_TrxName()))
throw new Exception("Organization Info not saved");
// Update BPartner
bp.setAD_OrgBP_ID(p_AD_Org_ID);
if (bp.getAD_Org_ID() != 0)
// Shared BPartner
bp.setClientOrg(bp.getAD_Client_ID(), 0);
// Save BP
if (!bp.save())
throw new Exception("Business Partner not updated");
// Limit to specific Role
if (p_AD_Role_ID != 0) {
boolean found = false;
MRoleOrgAccess[] orgAccesses = MRoleOrgAccess.getOfOrg(getCtx(), p_AD_Org_ID);
// delete all accesses except the specific
for (int i = 0; i < orgAccesses.length; i++) {
if (orgAccesses[i].getAD_Role_ID() == p_AD_Role_ID)
found = true;
else
orgAccesses[i].delete(true);
}
// create access
if (!found) {
MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, p_AD_Role_ID);
orgAccess.save(get_TrxName());
}
}
// Reset Client Role
MRole.getDefault(getCtx(), true);
return "Business Partner - Organization Link created";
}
use of org.compiere.model.MWarehouse in project adempiere by adempiere.
the class CreateFromShipment method getLocatorKeyNamePair.
/**
* Get KeyNamePair for Locator.
* If no locator specified or the specified locator is not valid (e.g. warehouse not match),
* a default one will be used.
* @param M_Locator_ID
* @return KeyNamePair
*/
protected KeyNamePair getLocatorKeyNamePair(int M_Locator_ID) {
MLocator locator = null;
// Load desired Locator
if (M_Locator_ID > 0) {
locator = MLocator.get(Env.getCtx(), M_Locator_ID);
// Validate warehouse
if (locator != null && locator.getM_Warehouse_ID() != getM_Warehouse_ID()) {
locator = null;
}
}
// Try to use default locator from Order Warehouse
if (locator == null && p_order != null && p_order.getM_Warehouse_ID() == getM_Warehouse_ID()) {
MWarehouse wh = MWarehouse.get(Env.getCtx(), p_order.getM_Warehouse_ID());
if (wh != null) {
locator = wh.getDefaultLocator();
}
}
// Try to get from locator field
if (locator == null) {
if (defaultLocator_ID > 0) {
locator = MLocator.get(Env.getCtx(), defaultLocator_ID);
}
}
// Validate Warehouse
if (locator == null || locator.getM_Warehouse_ID() != getM_Warehouse_ID()) {
locator = MWarehouse.get(Env.getCtx(), getM_Warehouse_ID()).getDefaultLocator();
}
KeyNamePair pp = null;
if (locator != null) {
pp = new KeyNamePair(locator.get_ID(), locator.getValue());
}
return pp;
}
use of org.compiere.model.MWarehouse in project adempiere by adempiere.
the class FreightModelValidator method getFreight.
/**
* get Freight based on Sales Order Line and inOutBound
*
* @param orderLine
* @param inOutBound
* @return
*/
private BigDecimal getFreight(MOrderLine orderLine, MWMInOutBound inOutBound) {
MOrder order = orderLine.getParent();
MWarehouse warehouse = (MWarehouse) order.getM_Warehouse();
I_M_Shipper shipper = MOrder.FREIGHTCOSTRULE_Line.equals(order.getFreightCostRule()) ? orderLine.getM_Shipper() : order.getM_Shipper();
int freightCategoryId = inOutBound == null ? MOrder.FREIGHTCOSTRULE_Line.equals(order.getFreightCostRule()) ? orderLine.getM_FreightCategory_ID() : order.getM_FreightCategory_ID() : inOutBound.getM_FreightCategory_ID();
if (isCalculatedFreight(order.getFreightCostRule()) && freightCategoryId > 0) {
freightEngine = FreightEngineFactory.getFreightEngine(orderLine.getAD_Client_ID());
return freightEngine.getFreightRuleFactory(shipper, order.getFreightCostRule()).calculate(order.getCtx(), orderLine.getM_Product_ID(), shipper.getM_Shipper_ID(), warehouse.getC_Location_ID(), order.getC_BPartner_Location().getC_Location_ID(), freightCategoryId, order.getC_Currency_ID(), order.getDateOrdered(), order.get_TrxName());
}
return BigDecimal.ZERO;
}
use of org.compiere.model.MWarehouse in project adempiere by adempiere.
the class HTMLMessenger method getStorageInfo.
public String getStorageInfo(MProduct p, MAttributeSetInstance asi) {
StorageReasoner mr = new StorageReasoner();
int[] ids = mr.getPOIDs(MLocator.Table_Name, null, null);
MWarehouse warehouse = null;
MStorage storage = null;
MLocator locator = null;
StringBuffer sb = new StringBuffer(STORAGE_HEADER_INFO_PATTERN);
Object[] obj = null;
BigDecimal sumQtyOnHand = BigDecimal.ZERO;
BigDecimal sumQtyReserved = BigDecimal.ZERO;
BigDecimal sumQtyOrdered = BigDecimal.ZERO;
int count = 0;
for (int i = 0; i < ids.length; i++) {
storage = MStorage.get(Env.getCtx(), ids[i], p.get_ID(), asi.get_ID(), null);
if (storage == null) {
continue;
}
count++;
warehouse = new MWarehouse(Env.getCtx(), storage.getM_Warehouse_ID(), null);
locator = new MLocator(Env.getCtx(), storage.getM_Locator_ID(), null);
sumQtyOnHand = sumQtyOnHand.add(storage.getQtyOnHand());
sumQtyReserved = sumQtyReserved.add(storage.getQtyReserved());
sumQtyOrdered = sumQtyOrdered.add(storage.getQtyOrdered());
// the quantities of specific locator
obj = new Object[] { locator.getX() + " - " + locator.getY() + " - " + locator.getZ(), warehouse.getName(), storage.getQtyOnHand(), storage.getQtyReserved(), storage.getQtyOrdered(), storage.getQtyOnHand().subtract(storage.getQtyReserved()) };
sb.append(MessageFormat.format(STORAGE_LINE_INFO_PATTERN, obj));
}
// the sum of the single quantities, if there is more than one line
if (count > 1) {
obj = new Object[] { sumQtyOnHand, sumQtyReserved, sumQtyOrdered, sumQtyOnHand.subtract(sumQtyReserved) };
sb.append(MessageFormat.format(STORAGE_SUM_LINE_INFO_PATTERN, obj));
}
double available = sumQtyOnHand.subtract(sumQtyReserved).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
if (count == 0 || (available <= 0.00d)) {
sb.append(MessageFormat.format(STORAGE_NOINVENTORY_INFO_PATTERN, obj));
}
sb.append(STORAGE_FOOTER_INFO_PATTERN);
return sb.toString();
}
use of org.compiere.model.MWarehouse in project adempiere by adempiere.
the class CalloutDistributionOrder method setLocatorTo.
/**
* Set Default Locator To
* @param ctx
* @param WindowNo
* @param mTab
* @param mField
* @param value
* @return
*/
public String setLocatorTo(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
I_DD_OrderLine line = GridTabWrapper.create(mTab, I_DD_OrderLine.class);
if (value != null) {
MProduct product = MProduct.get(ctx, (Integer) value);
if (line.getC_UOM_ID() <= 0) {
line.setC_UOM_ID(product.getC_UOM_ID());
}
}
MWarehouse[] ws = MWarehouse.getForOrg(ctx, line.getAD_Org_ID());
if (ws == null && ws.length < 0) {
return "";
}
MLocator locator_to = MLocator.getDefault(ws[0]);
if (locator_to != null) {
line.setM_LocatorTo_ID(locator_to.getM_Locator_ID());
}
return "";
}
Aggregations