Skip to Content
0
Former Member
Dec 13, 2006 at 03:37 PM

Problem with creating invoice through code

30 Views

Hi all,

I am creating a ARInvoice from an existing DeliveryNote.The folllowing piece of code was working fine.Now it suddenly throws error.The error I get on invoice addition is "Date deviates from permissible range [OINV.DocDueDate]"(error code is -5002).So I checked this in debug mode and found that OINV.Docduedate carries value "12/30/1899 12:00:00 AM".How is this happening? I think the docdate and docduedate are internally assigned by B1 to documents.Surprisingly this code was working fine without any problem upto somedays ago.We are using ServicePack PL-12 in our B1.Does any body can help me? Here is my code sample which creates invoice..

private void ConvertToInvoice(SAPbobsCOM.Documents oDLN, double dblUPSExpense, int iUPSExpenseCode)

{

int no= oDLN.DocNum;

string taxcode="";

try

{

SAPbobsCOM.Documents oINV;

//SAPbobsCOM.DocumentsAdditionalExpenses oEXP;

oINV=(SAPbobsCOM.Documents)

modups.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices);

oINV.CardCode=oDLN.CardCode;

oINV.DocDate = System.DateTime.Now;

for (int i=0;i<oDLN.Lines.Count;i++)

{

oDLN.Lines.SetCurrentLine(i);

if (i != 0)

{

oINV.Lines.Add();

}

oINV.Lines.BaseType = Convert.ToInt32(SAPbobsCOM.BoObjectTypes.oDeliveryNotes);

oINV.Lines.BaseEntry = oDLN.DocEntry;

oINV.Lines.BaseLine = i;

taxcode = oDLN.Lines.TaxCode;

}

//Update Expenses

//oEXP = oINV.Expenses;

string docnum = oDLN.DocNum.ToString();

//d = oDLN.DocEntry;

int DeliveryExpenseCount = this.GetExpenseCount(docnum); //oDLN.Expenses.Count;

if( oDLN.UserFields.Fields.Item("U_TWBS_UPS_FreeShp").Value.ToString() == "N")

{

if (DeliveryExpenseCount>0)

{

bool flg = false;

for(int i=0;i<oDLN.Expenses.Count;i++)

{

oDLN.Expenses.SetCurrentLine(i);

if(i!=0)

{

oINV.Expenses.Add();

}

int x = oDLN.Expenses.ExpenseCode;

oINV.Expenses.ExpenseCode = oDLN.Expenses.ExpenseCode;

if(oDLN.Expenses.ExpenseCode==iUPSExpenseCode)

{

oINV.Expenses.LineTotal = oDLN.Expenses.LineTotal + dblUPSExpense;

string s = oDLN.Expenses.TaxCode;

oINV.Expenses.TaxCode = oDLN.Expenses.TaxCode;

flg = true;

}

else

{

oINV.Expenses.LineTotal = oDLN.Expenses.LineTotal;

oINV.Expenses.TaxCode = oDLN.Expenses.TaxCode;

}

}

if(flg==false)

{

oINV.Expenses.Add();

oINV.Expenses.ExpenseCode = modups.upsco.UPSExpenseCode;

oINV.Expenses.TaxCode = taxcode;

oINV.Expenses.LineTotal = dblUPSExpense;

}

}

else

{

// oINV.Expenses.Add();

oINV.Expenses.ExpenseCode = modups.upsco.UPSExpenseCode;

frmMain.WriteErrorInfo("ConvertToInvoice : DeliveryNum# <"no.ToString()"> Taxcode2 = " + taxcode);

oINV.Expenses.TaxCode = taxcode;

oINV.Expenses.LineTotal = oDLN.Expenses.LineTotal + dblUPSExpense;

//oINV.Expenses.TaxCode = oDLN.Expenses.TaxCode;

}

}

else //Freeship = 'Y'

{

if (DeliveryExpenseCount>0)

{

for(int i=0;i<oDLN.Expenses.Count;i++)

{

oDLN.Expenses.SetCurrentLine(i);

if(i!=0)

{

oINV.Expenses.Add();

}

//oINV.Expenses.ExpenseCode=i;

oINV.Expenses.ExpenseCode = oDLN.Expenses.ExpenseCode;

oINV.Expenses.LineTotal = oDLN.Expenses.LineTotal;

oINV.Expenses.TaxCode = oDLN.Expenses.TaxCode;

}

}

}

string docdate = oINV.DocDueDate.ToString();

int retVal = oINV.Add();

if(retVal!=0)

{

int lErrCode; string sErrCode;

modups.oCompany.GetLastError(out lErrCode, out sErrCode);

frmMain.WriteErrorInfo("ConvertToInvoice : DeliveryNum# <"oDLN.DocNum.ToString()"> "+sErrCode);

string s = "10";

}

}

catch(Exception ex)

{

frmMain.WriteErrorInfo("ConvertToInvoice : DeliveryNum# <"no.ToString()"> "+ex.Message.ToString());

}

}