⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 morder.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.model;

import java.util.*;
import java.sql.*;
import java.math.*;
import java.io.Serializable;

import org.compiere.process.*;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Log;
/**
 *  Order Model
 *
 *  @author Jorg Janke
 *  @version $Id: MOrder.java,v 1.9 2003/05/04 06:40:55 jjanke Exp $
 */
public class MOrder extends PO
{
	/**
	 *  Default Constructor
	 *  @param ctx context
	 *  @param  C_Order_ID    order to load, (0 create new order)
	 */
	public MOrder(Properties ctx, int C_Order_ID)
	{
		super (ctx, C_Order_ID);
		//  New
		if (C_Order_ID == 0)
		{
			setDeliveryRule (DeliveryRule_Availability);
			setFreightCostRule (FreightCostRule_Included);
			setInvoiceRule (InvoiceRule_Immediate);
			setPaymentRule(PaymentRule_PaymentTerm);
			setPriorityRule ("5");
			setDeliveryViaRule ("P");
			//
			setDocStatus("DR");		//	Draft
			setDocAction (DocAction_Process);
			//
			setIsDiscountPrinted (false);
			setIsSelected (false);
			setIsTaxIncluded (false);
			setIsSOTrx (true);
			setSendEMail (false);
			//
			setIsApproved(false);
			setIsPrinted(false);
			setIsCreditApproved(false);
			setIsDelivered(false);
			setIsInvoiced(false);
			setIsTransferred(false);
			//
			setProcessed(false);
			setPosted(false);

			setDateAcct (new Timestamp(System.currentTimeMillis()));
			setDatePromised (new Timestamp(System.currentTimeMillis()));
			setDateOrdered (new Timestamp(System.currentTimeMillis()));

			setFreightAmt (Env.ZERO);
			setChargeAmt (Env.ZERO);
			setTotalLines (Env.ZERO);
			setGrandTotal (Env.ZERO);
		}
	}	//	MOrder

	/**
	 *  Load Constructor
	 *  @param ctx context
	 *  @param rs result set record
	 */
	public MOrder (Properties ctx, ResultSet rs)
	{
		super (ctx, rs);
	}	//	MOrder

	/**
	 *  Initialize and return PO_Info
	 *  @param ctx context
	 *  @return POInfo
	 */
	protected POInfo initPO (Properties ctx)
	{
		int AD_Table_ID = 259;
		POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
		return poi;
	}	//	initPO

	/*************************************************************************/

	public static final String		DocSubTypeSO_Standard = "SO";
	public static final String		DocSubTypeSO_Quotation = "OB";
	public static final String		DocSubTypeSO_Proposal = "ON";
	public static final String		DocSubTypeSO_Prepay = "PR";
	public static final String		DocSubTypeSO_POS = "WR";
	public static final String		DocSubTypeSO_Warehouse = "WP";
	public static final String		DocSubTypeSO_OnCredit = "WI";
	public static final String		DocSubTypeSO_RMA = "RM";

	/**
	 * 	Set Target Document Type
	 * 	@param DocSubTypeSO_x SO sub type - see DocSubTypeSO_*
	 */
	public void setC_DocTypeTarget_ID (String DocSubTypeSO_x)
	{
		String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocSubTypeSO=?";
		int C_DocType_ID = DB.getSQLValue(sql, getAD_Client_ID(), DocSubTypeSO_x);
		if (C_DocType_ID <= 0)
			log.error ("setC_DocTypeTarget_ID - Not found for AC_Client_ID=" + getAD_Client_ID () + ", SubType=" + DocSubTypeSO_x);
		else
		{
			log.debug("setC_DocTypeTarget_ID - " + DocSubTypeSO_x);
			setC_DocTypeTarget_ID (C_DocType_ID);
			setIsSOTrx(true);
		}
	}	//	setC_DocTypeTarget_ID

	/**
	 * 	Set Business Partner Defaults & Details
	 * 	@param bp business partner
	 */
	public void setBPartner (MBPartner bp)
	{
		if (bp == null)
			return;

		setC_BPartner_ID(bp.getC_BPartner_ID());
		//	Set Defaults
		int ii = 0;
		if (isSOTrx())
			ii = bp.getC_PaymentTerm_ID();
		else
			ii = bp.getPO_PaymentTerm_ID();
		if (ii != 0)
			setC_PaymentTerm_ID(ii);
		//
		if (isSOTrx())
			ii = bp.getM_PriceList_ID();
		else
			ii = bp.getPO_PriceList_ID();
		if (ii != 0)
			setM_PriceList_ID(ii);
		//
		String ss = bp.getDeliveryRule();
		if (ss != null)
			setDeliveryRule(ss);
		ss = bp.getDeliveryViaRule();
		if (ss != null)
			setDeliveryViaRule(ss);
		ss = bp.getInvoiceRule();
		if (ss != null)
			setInvoiceRule(ss);
		ss = bp.getPaymentRule();
		if (ss != null)
			setPaymentRule(ss);


		//	Set Locations
		MBPartner_Location[] locs = bp.getLocations();
		if (locs != null)
		{
			for (int i = 0; i < locs.length; i++)
			{
				if (locs[i].isBillTo())
					setBillTo_ID(locs[i].getC_BPartner_Location_ID());
				if (locs[i].isShipTo())
					setC_BPartner_Location_ID(locs[i].getC_BPartner_Location_ID());
			}
			//	set to first
			if (getBillTo_ID() == 0 && locs.length > 0)
				setBillTo_ID(locs[0].getC_BPartner_Location_ID());
			if (getC_BPartner_Location_ID() == 0 && locs.length > 0)
				setC_BPartner_Location_ID(locs[0].getC_BPartner_Location_ID());
		}
		if (getBillTo_ID() == 0)
			log.error("MOrder.setBPartner - Has no Bill To Address: " + bp);
		if (getC_BPartner_Location_ID() == 0)
			log.error("MOrder.setBPartner - Has no Ship To Address: " + bp);

		//	Set Contact
		MBPartner_Contact[] contacts = bp.getContacts();
		if (contacts != null && contacts.length == 1)
			setC_BPartner_Contact_ID(contacts[0].getC_BPartner_Contact_ID());
	}	//	setBPartner

	/**
	 * 	Set Defaults for mandatory values where not set yet
	 */
	private void setDefaults ()
	{
		int AD_Client_ID = getAD_Client_ID();
		int AD_Org_ID = getAD_Org_ID();
		log.debug("setDefaults - " + "Client/Org=" + AD_Client_ID + "/" + AD_Org_ID);

		//	No Partner Info - set Template
		if (getC_BPartner_ID() == 0)
			setBPartner(MBPartner.getTemplate(getCtx(), getAD_Client_ID()));
		if (getC_BPartner_Location_ID() == 0 || getBillTo_ID() == 0)
			setBPartner(new MBPartner(getCtx(), getC_BPartner_ID()));

		//	Price List
		if (getM_PriceList_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#M_PriceList_ID");
			if (ii != 0)
				setM_PriceList_ID(ii);
			else
			{
				String sql = "SLECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'";
				ii = DB.getSQLValue (sql, AD_Client_ID);
				if (ii != 0)
					setM_PriceList_ID (ii);
			}
		}
		//	Currency
		if (getC_Currency_ID() == 0)
		{
			String sql = "SLECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?";
			int ii = DB.getSQLValue (sql, getM_PriceList_ID());
			if (ii != 0)
				setC_Currency_ID (ii);
			else
				setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID"));
		}

		//	Sales Rep
		if (getSalesRep_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#SalesRep_ID");
			if (ii != 0)
				setSalesRep_ID (ii);
		}

		//	Document Type
		if (getC_DocType_ID() == 0)
			setC_DocType_ID (0);	//	make sure it's set to 0
		if (getC_DocTypeTarget_ID() == 0)
			setC_DocTypeTarget_ID(DocSubTypeSO_Standard);

		//	Payment Term
		if (getC_PaymentTerm_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID");
			if (ii != 0)
				setC_PaymentTerm_ID(ii);
			else
			{
				String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'";
				ii = DB.getSQLValue(sql, AD_Client_ID);
				if (ii != 0)
					setC_PaymentTerm_ID (ii);
			}
		}
		if (getM_Warehouse_ID() == 0)
		{
			int ii = Env.getContextAsInt(getCtx(), "#M_Warehouse_ID");
			if (ii != 0)
				setM_Warehouse_ID(ii);
		}
	}	//	setDefaults

	/*************************************************************************/

	/**
	 * 	Save Order
	 * 	@return true if saved
	 */
	public boolean save ()
	{
		log.debug ("save");
		setDefaults();
		setDocumentNo(DB.getDocumentNo(getAD_Client_ID(), getC_DocTypeTarget_ID()));
		return super.save ();
	}	//	save

	/**
	 * 	Process Order
	 * 	@param docAction doc action
	 *	@return true if no error
	 */
	public boolean process (String docAction)
	{
		setDocAction(docAction);
		return process();
	}	//	process

	/**
	 * 	Process Order
	 *	@return true if no error
	 */
	public boolean process ()
	{
		save();
		log.debug ("process - " + getDocAction());
		int AD_Process_ID = 104;	//	C_Order_Post
		MProcess pp = new MProcess (getCtx(), AD_Process_ID);
		boolean ok = pp.process(getC_Order_ID()).isOK();
		load();		//	reload
		log.debug("process - ok=" + ok + " - GradndTotal=" + getGrandTotal());
		return ok;
	}	//	process

	public String toString ()
	{
		StringBuffer sb = new StringBuffer ("MOrder[")
			.append(getID())
			.append ("]");
		return sb.toString ();
	}

	public void setC_BPartner_ID (int C_BPartner_ID)
	{
		setValue ("C_BPartner_ID", new Integer (C_BPartner_ID));
	}

	public int getC_BPartner_ID ()
	{
		Integer ii = (Integer)getValue ("C_BPartner_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public static final String		DeliveryRule_Availability = "A";
	public static final String		DeliveryRule_Line = "L";
	public static final String		DeliveryRule_Order = "O";
	public static final String		DeliveryRule_Receipt = "R";

	public void setDeliveryRule (String DeliveryRule)
	{
		if (DeliveryRule.equals ("A") || DeliveryRule.equals ("L")
		  || DeliveryRule.equals ("O") || DeliveryRule.equals ("R"))
			;
		else
			throw new IllegalArgumentException (
			  "DeliveryRule Invalid value - Reference_ID=151 - A - L - O - R");
		if (DeliveryRule == null)
			throw new IllegalArgumentException ("DeliveryRule is mandatory");
		setValue ("DeliveryRule", DeliveryRule);
	}

	public String getDeliveryRule ()
	{
		return (String)getValue ("DeliveryRule");
	}

	public int getC_Order_ID ()
	{
		return getID();
	}

	public void setC_BPartner_Contact_ID (int C_BPartner_Contact_ID)
	{
		setValue ("C_BPartner_Contact_ID", new Integer (C_BPartner_Contact_ID));
	}

	public int getC_BPartner_Contact_ID ()
	{
		Integer ii = (Integer)getValue ("C_BPartner_Contact_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public void setC_Project_ID (int C_Project_ID)
	{
		setValue ("C_Project_ID", new Integer (C_Project_ID));
	}

	public int getC_Project_ID ()
	{
		Integer ii = (Integer)getValue ("C_Project_ID");
		if (ii == null)
			return 0;
		return ii.intValue ();
	}

	public void setChargeAmt (BigDecimal ChargeAmt)
	{
		setValue ("ChargeAmt", ChargeAmt);
	}

	public BigDecimal getChargeAmt ()
	{
		BigDecimal bd = (BigDecimal)getValue ("ChargeAmt");
		if (bd == null)
			return Env.ZERO;
		return bd;
	}

	public void setC_Activity_ID (int C_Activity_ID)
	{
		setValue ("C_Activity_ID", new Integer (C_Activity_ID));
	}

	public int getC_Activity_ID ()
	{
		Integer ii = (Integer)getValue ("C_Activity_ID");
		if (ii == null)
			return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -