/// <summary> /// /// </summary> /// <param name="tLine"></param> private int GenerateInvoice(VAdvantage.Model.X_S_TimeExpenseLine tLine, VAdvantage.Model.X_S_TimeExpense tExp, bool IsExpense) { int C_PaymentTerm_ID = 0; VAdvantage.Model.X_C_Order ord = null; if (tLine.GetC_Order_ID() != 0) { ord = new VAdvantage.Model.X_C_Order(GetCtx(), tLine.GetC_Order_ID(), null); } sql = "select C_BPartner_Location_ID from c_Bpartner_Location where c_bpartner_ID = " + tLine.GetC_BPartner_ID(); int C_BPartner_Location_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); if (_DateInvoiced == null) { _DateInvoiced = System.DateTime.Now; } // X_C_Invoice inv = new X_C_Invoice(GetCtx(), 0, null); VAdvantage.Model.MInvoice inv = new VAdvantage.Model.MInvoice(GetCtx(), 0, null); inv.SetAD_Client_ID(GetCtx().GetAD_Client_ID()); inv.SetAD_Org_ID(GetCtx().GetAD_Org_ID()); inv.SetAD_User_ID(tExp.GetAD_User_ID()); inv.SetC_BPartner_ID(tLine.GetC_BPartner_ID()); inv.SetC_BPartner_Location_ID(C_BPartner_Location_ID); inv.SetC_Currency_ID(tLine.GetC_Currency_ID()); inv.SetDateInvoiced(_DateInvoiced); inv.SetM_PriceList_ID(tExp.GetM_PriceList_ID()); inv.SetDateAcct(_DateInvoiced); inv.SetIsApproved(true); //inv.SetPaymentRule(); if (!IsExpense) { inv.SetIsSOTrx(true); sql = "select C_DocType_ID from c_doctype where name = 'AR Invoice' and ad_client_id = " + GetCtx().GetAD_Client_ID(); int C_DocType_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); inv.SetC_DocType_ID(C_DocType_ID); inv.SetC_DocTypeTarget_ID(C_DocType_ID); } else { inv.SetIsSOTrx(false); sql = "select C_DocType_ID from c_doctype where docbasetype = 'API' and ad_client_id = " + GetCtx().GetAD_Client_ID(); int C_DocType_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); inv.SetC_DocType_ID(C_DocType_ID); inv.SetC_DocTypeTarget_ID(C_DocType_ID); } //inv.SetSalesRep_ID(); inv.SetDocAction("CO"); inv.SetDocStatus("DR"); if (ord != null) { inv.SetC_PaymentTerm_ID(ord.GetC_PaymentTerm_ID()); inv.SetSalesRep_ID(ord.GetSalesRep_ID()); inv.SetC_Order_ID(ord.GetC_Order_ID()); inv.SetM_PriceList_ID(ord.GetM_PriceList_ID()); } else { sql = " select c_paymentterm_id from c_bpartner where c_bpartner_id = " + tLine.GetC_BPartner_ID(); C_PaymentTerm_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); if (C_PaymentTerm_ID == 0) { sql = "select c_paymentterm_id from c_paymentterm where isdefault = 'Y' and ad_client_id= " + GetCtx().GetAD_Client_ID(); C_PaymentTerm_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null)); } sql = "select M_Pricelist_ID from m_Pricelist where issopricelist = 'Y' and isactive = 'Y'"; inv.SetM_PriceList_ID(Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null))); inv.SetC_PaymentTerm_ID(C_PaymentTerm_ID); inv.SetSalesRep_ID(tExp.GetSalesRep_ID()); } if (!inv.Save()) { log.SaveError("InvoiceNotSaved", "InvoiceNotSaved"); return(0); } return(inv.GetC_Invoice_ID()); }