如何使用 Quickbooks QBFC (8.0 SDK) 获取客户、工作和员工列表

发布于 2024-07-29 21:58:45 字数 902 浏览 6 评论 0原文

我接到了一项艰巨的任务:编写一个 C# 应用程序,以将单独数据库中的员工时间条目与 Quickbooks 同步。 由于我是 QB 编程的新手,因此我正在尝试执行基本任务,例如获取客户列表,然后为每个客户分配工作,然后为员工分配工作。 我一直在阅读 SDK 文档,但我对细节仍然有点模糊,因为我找到的示例目前对我来说有点太先进了:-P

为了让事情简单,我想问一下获取一个代码片段,为我提供了初学者的客户列表。 这是我得到的代码:

        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest customerSet = SessionManager.CreateMsgSetRequest("US", 8, 0);

        //          
        // Code to get list of customers here.
        //

        SessionManager.OpenConnection2("", "New App", ENConnectionType.ctLocalQBD);
        SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        IMsgSetResponse Resp = SessionManager.DoRequests(customerSet);
        MessageBox.Show(Resp.ToXMLString());
        SessionManager.EndSession();
        SessionManager.CloseConnection();

任何人都可以帮我填写“获取客户列表的代码”吗? 预先非常感谢您!

胜利者

I have been given the painful task of writing a C# application to sync up employee time entries in a separate database with Quickbooks. Since I'm brand new to QB programming, I'm trying to peform basic tasks, such as getting a list of customers, then jobs for each customer, then employees. I've been reading the SDK documentation, but I'm still a little fuzzy on the details because the examples I'm finding are a little too advanced for me at the moment :-P

To keep things simple, I would like to ask for a code snippet that gives me the list of customers for starters. Here's the code I've got:

        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest customerSet = SessionManager.CreateMsgSetRequest("US", 8, 0);

        //          
        // Code to get list of customers here.
        //

        SessionManager.OpenConnection2("", "New App", ENConnectionType.ctLocalQBD);
        SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        IMsgSetResponse Resp = SessionManager.DoRequests(customerSet);
        MessageBox.Show(Resp.ToXMLString());
        SessionManager.EndSession();
        SessionManager.CloseConnection();

Can anyone fill in the "code to get list of customers here" for me? Thank you very much in advance!

Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转瞬即逝 2024-08-05 21:58:48

添加到 Victors、Chili 和 Hassan 的答案中。 很高兴偶然发现这个问题,因为我自己也在为 QBFC 的例子而苦苦挣扎。
这是一套完整的代码,可能会对以后的人有所帮助。 如果与此同时有人可以向我指出一些有用的文档……那就太好了。

将员工数据获取到 XML 字符串

    public static string EmployeeListXML()
    {
        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest msgSetReq = SessionManager.CreateMsgSetRequest("US", 8, 0);
        IEmployeeQuery employee = msgSetReq.AppendEmployeeQueryRq();
        employee.IncludeRetElementList.Add("IsActive");
        employee.IncludeRetElementList.Add("ListID");
        employee.IncludeRetElementList.Add("EditSequence");
        employee.IncludeRetElementList.Add("FirstName");
        employee.IncludeRetElementList.Add("LastName");
        employee.IncludeRetElementList.Add("SSN");
        //employee.IncludeRetElementList.Add("ParentRef");
        //employee.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
        employee.OwnerIDList.Add("0"); // required for non-private data extn fields
        //customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID
        SessionManager.OpenConnection2("", Application.ProductName, ENConnectionType.ctLocalQBD);
        //SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        SessionManager.BeginSession(frmMain.QBFileName, ENOpenMode.omDontCare); // I have the filename on frmMain
        IMsgSetResponse Resp = SessionManager.DoRequests(msgSetReq);
        SessionManager.EndSession();
        SessionManager.CloseConnection();
        //MessageBox.Show(Resp.ToXMLString());
        return Resp.ToXMLString();
    }

将 XML 字符串放入员工对象列表中

    public static List<Employee> EmployeeXMLtoList()
    {
        string sXML = EmployeeListXML();

        List<Employee> lstEmp = new List<Employee>();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(sXML);
        XmlNodeList parentNode = xmlDoc.GetElementsByTagName("EmployeeRet");
        foreach (XmlNode childNode in parentNode)
        {
            Employee oEmp = new Employee();
            oEmp.ListID = childNode.SelectSingleNode("ListID").InnerText;
            oEmp.EditSequence = childNode.SelectSingleNode("EditSequence").InnerText;
            oEmp.Active = childNode.SelectSingleNode("IsActive").InnerText;
            oEmp.FirstName = childNode.SelectSingleNode("FirstName").InnerText;
            oEmp.LastName = childNode.SelectSingleNode("LastName").InnerText;
            oEmp.SSN = childNode.SelectSingleNode("SSN").InnerText;
            lstEmp.Add(oEmp);
        }
        return lstEmp;
    }

使用 Linq 返回 Employee 对象的函数

    public static Employee GetEmployeeObject(string sSSN)
    {
        Employee oReturn = null;
        List<Employee> lstEmployee = EmployeeXMLtoList();

        IEnumerable<Employee> lstEmps = from oEmp in lstEmployee
                                        where oEmp.SSN == sSSN
                                        select oEmp;

        foreach (var oEmp in lstEmps)
        {
            oReturn = oEmp;
        }
        return oReturn;
    }

代码示例

 Employee oEmployee = QB.GetEmployeeObject("112-35-8560");

员工类别

public class Employee
{
    private string sEmployeeID;
    private string sSSN;
    private string sLastName;
    private string sFirstName;
    private string sAddress1;
    private string sAddress2;
    private string sCity;
    private string sState;
    private string sZipCode;
    private string sGender;
    private string sEthnicity;
    private DateTime dDOB;
    private string sMaritalStatus;
    private int iDependants;
    private string sUScitizen;
    private decimal iPayRate;
    private string sPhone;
    private DateTime dHireDate;
    private string sEmail;

    public Employee() { }

    public string EmployeeID
    {
        get { return sEmployeeID; }
        set { sEmployeeID = value; }
    }

    public string ListID
    {
        get; set;
    }

    public string EditSequence
    {
        get;  set;
    }

    public string Active
    {
        get; set;
    }


    public string SSN
    {
        get { return sSSN; }
        set { sSSN = value; }
    }

    public string LastName
    {
        get { return sLastName; }
        set { sLastName = value; }
    }

    public string FirstName
    {
        get { return sFirstName; }
        set { sFirstName = value; }
    }

    public string FullName
    {
        get { return FirstName + " " + LastName; }
        set { }
    }

    public string Address1
    {
        get { return sAddress1; }
        set { sAddress1 = value; }
    }

    public string Address2
    {
        get { return sAddress2; }
        set { sAddress2 = value; }
    }

    public string State
    {
        get { return sState; }
        set { sState = value; }
    }
    public string City
    {
        get { return sCity; }
        set { sCity = value; }
    }
    public string ZipCode
    {
        get { return sZipCode; }
        set { sZipCode = value; }
    }
    public string Gender
    {
        get { return sGender; }
        set { sGender = value; }
    }

    public string Ethnicity
    {
        get { return sEthnicity; }
        set { sEthnicity = value; }
    }

    public DateTime DOB
    {
        get { return dDOB; }
        set { dDOB = value; }
    }
    public string MaritalStatus
    {
        get { return sMaritalStatus; }
        set { sMaritalStatus = value; }
    }
    public int Dependants
    {
        get { return iDependants; }
        set { iDependants = value; }
    }
    public string UScitizen
    {
        get { return sUScitizen; }
        set { sUScitizen = value; }
    }

    public decimal PayRate
    {
        get { return iPayRate; }
        set { iPayRate = value; }
    }
    public DateTime HireDate
    {
        get { return dHireDate; }
        set { dHireDate = value; }
    }
    public string Phone
    {
        get { return sPhone; }
        set { sPhone = value; }
    }
    public string Email
    {
        get { return sEmail; }
        set { sEmail = value; }
    }

}

Adding to Victors, Chili and Hassan's answer. Glad to have stumbled on this question as I myself was struggling with the QBFC examples.
Here is a full set of code that might just help someone down the road. If in the meantime someone could just point me in the direction of some useful documentation...that would be great.

Getting the Employee data to an XML string

    public static string EmployeeListXML()
    {
        QBSessionManager SessionManager = new QBSessionManager();
        IMsgSetRequest msgSetReq = SessionManager.CreateMsgSetRequest("US", 8, 0);
        IEmployeeQuery employee = msgSetReq.AppendEmployeeQueryRq();
        employee.IncludeRetElementList.Add("IsActive");
        employee.IncludeRetElementList.Add("ListID");
        employee.IncludeRetElementList.Add("EditSequence");
        employee.IncludeRetElementList.Add("FirstName");
        employee.IncludeRetElementList.Add("LastName");
        employee.IncludeRetElementList.Add("SSN");
        //employee.IncludeRetElementList.Add("ParentRef");
        //employee.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
        employee.OwnerIDList.Add("0"); // required for non-private data extn fields
        //customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID
        SessionManager.OpenConnection2("", Application.ProductName, ENConnectionType.ctLocalQBD);
        //SessionManager.BeginSession(string.Empty, ENOpenMode.omDontCare);
        SessionManager.BeginSession(frmMain.QBFileName, ENOpenMode.omDontCare); // I have the filename on frmMain
        IMsgSetResponse Resp = SessionManager.DoRequests(msgSetReq);
        SessionManager.EndSession();
        SessionManager.CloseConnection();
        //MessageBox.Show(Resp.ToXMLString());
        return Resp.ToXMLString();
    }

Putting the XML string into a List of Emplpoyee Objects

    public static List<Employee> EmployeeXMLtoList()
    {
        string sXML = EmployeeListXML();

        List<Employee> lstEmp = new List<Employee>();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(sXML);
        XmlNodeList parentNode = xmlDoc.GetElementsByTagName("EmployeeRet");
        foreach (XmlNode childNode in parentNode)
        {
            Employee oEmp = new Employee();
            oEmp.ListID = childNode.SelectSingleNode("ListID").InnerText;
            oEmp.EditSequence = childNode.SelectSingleNode("EditSequence").InnerText;
            oEmp.Active = childNode.SelectSingleNode("IsActive").InnerText;
            oEmp.FirstName = childNode.SelectSingleNode("FirstName").InnerText;
            oEmp.LastName = childNode.SelectSingleNode("LastName").InnerText;
            oEmp.SSN = childNode.SelectSingleNode("SSN").InnerText;
            lstEmp.Add(oEmp);
        }
        return lstEmp;
    }

Function that return an Employee Object using Linq

    public static Employee GetEmployeeObject(string sSSN)
    {
        Employee oReturn = null;
        List<Employee> lstEmployee = EmployeeXMLtoList();

        IEnumerable<Employee> lstEmps = from oEmp in lstEmployee
                                        where oEmp.SSN == sSSN
                                        select oEmp;

        foreach (var oEmp in lstEmps)
        {
            oReturn = oEmp;
        }
        return oReturn;
    }

Example of Code

 Employee oEmployee = QB.GetEmployeeObject("112-35-8560");

Employee Class

public class Employee
{
    private string sEmployeeID;
    private string sSSN;
    private string sLastName;
    private string sFirstName;
    private string sAddress1;
    private string sAddress2;
    private string sCity;
    private string sState;
    private string sZipCode;
    private string sGender;
    private string sEthnicity;
    private DateTime dDOB;
    private string sMaritalStatus;
    private int iDependants;
    private string sUScitizen;
    private decimal iPayRate;
    private string sPhone;
    private DateTime dHireDate;
    private string sEmail;

    public Employee() { }

    public string EmployeeID
    {
        get { return sEmployeeID; }
        set { sEmployeeID = value; }
    }

    public string ListID
    {
        get; set;
    }

    public string EditSequence
    {
        get;  set;
    }

    public string Active
    {
        get; set;
    }


    public string SSN
    {
        get { return sSSN; }
        set { sSSN = value; }
    }

    public string LastName
    {
        get { return sLastName; }
        set { sLastName = value; }
    }

    public string FirstName
    {
        get { return sFirstName; }
        set { sFirstName = value; }
    }

    public string FullName
    {
        get { return FirstName + " " + LastName; }
        set { }
    }

    public string Address1
    {
        get { return sAddress1; }
        set { sAddress1 = value; }
    }

    public string Address2
    {
        get { return sAddress2; }
        set { sAddress2 = value; }
    }

    public string State
    {
        get { return sState; }
        set { sState = value; }
    }
    public string City
    {
        get { return sCity; }
        set { sCity = value; }
    }
    public string ZipCode
    {
        get { return sZipCode; }
        set { sZipCode = value; }
    }
    public string Gender
    {
        get { return sGender; }
        set { sGender = value; }
    }

    public string Ethnicity
    {
        get { return sEthnicity; }
        set { sEthnicity = value; }
    }

    public DateTime DOB
    {
        get { return dDOB; }
        set { dDOB = value; }
    }
    public string MaritalStatus
    {
        get { return sMaritalStatus; }
        set { sMaritalStatus = value; }
    }
    public int Dependants
    {
        get { return iDependants; }
        set { iDependants = value; }
    }
    public string UScitizen
    {
        get { return sUScitizen; }
        set { sUScitizen = value; }
    }

    public decimal PayRate
    {
        get { return iPayRate; }
        set { iPayRate = value; }
    }
    public DateTime HireDate
    {
        get { return dHireDate; }
        set { dHireDate = value; }
    }
    public string Phone
    {
        get { return sPhone; }
        set { sPhone = value; }
    }
    public string Email
    {
        get { return sEmail; }
        set { sEmail = value; }
    }

}
苏佲洛 2024-08-05 21:58:48

好吧,看来我找到了缺失的部分:

ICustomerQuery customers = customerSet.AppendCustomerQueryRq();

这会生成与每个客户相关的所有数据,这是向前迈出的一步。 为客户解析 XML 应该非常简单,但是为每个客户解析单独的任务/作业将很费力,因为每个任务没有子节点 - 基本上您会得到重复的 XML 块,其中包含所有基本客户信息(地址、账单)地址、送货地址等),然后是一个名为“FullName”的属性,它将一个冒号附加到客户名称后面,后跟任务标题(其本身可以后跟另一个带有子任务标题的冒号等)。 我想知道是否可以对请求查询做一些聪明的事情来获得更好的 xml 响应(例如,指定我想要返回的属性,并且可能强制为给定客户的每个任务创建子节点)...感谢评论。

Ok, seems like I found the missing piece:

ICustomerQuery customers = customerSet.AppendCustomerQueryRq();

This produces all the data related to each customer, which is a step forward. Parsing the XML for customers should be pretty straightforward, but parsing the individual tasks/jobs for each customer will be laborious, because there are no subnodes for each task - basically you get repeating chunks of XML with all the basic customer info (address, billing address, shipping address, etc.), then this one property called "FullName" which appends a colon to the customer name, followed by the task title (which itself can be followed by another colon with a subtask title, etc.). I'm wondering if there's something clever I can do with the request query to get a better xml response (for instance, specify what properties I want returned, and maybe enforce the creation of subnodes for each task for a given customer)...comments are appreciated.

楠木可依 2024-08-05 21:58:47
customers.IncludeRetElementList.Add("IsActive");
customers.IncludeRetElementList.Add("ListID");
customers.IncludeRetElementList.Add("EditSequence");
customers.IncludeRetElementList.Add("Name");
customers.IncludeRetElementList.Add("ParentRef");

只有上面列表中指定的字段才会从 QuickBooks 返回 - 在正确的情况下使用正确的字符串非常重要 - 如果出现问题,不会产生错误消息。 您不能指定子字段(例如,地址块中的城市;您必须获取整个地址块)。 对于自定义字段,您还必须指定 OwnerID(对于非应用程序私有的自定义字段使用 0)

customers.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
customers.OwnerIDList.Add("0"); // required for non-private data extn fields
customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID
customers.IncludeRetElementList.Add("IsActive");
customers.IncludeRetElementList.Add("ListID");
customers.IncludeRetElementList.Add("EditSequence");
customers.IncludeRetElementList.Add("Name");
customers.IncludeRetElementList.Add("ParentRef");

Only the fields specified in the above list will be returned from QuickBooks - it is very important to use the correct strings in the correct case - no error messages will result if something is wrong. You cannot specify sub-fields (eg, City within an Address block; you must get the entire Address block). For custom fields, you also must specify the OwnerID (use 0 for custom fields that are not private to an application)

customers.IncludeRetElementList.Add("DataExtRet"); //will return non-private and/or private data extension fields depending on the OwnerIDList, below
customers.OwnerIDList.Add("0"); // required for non-private data extn fields
customers.OwnerIDList.Add("Your Appln GUID"); // Use this to get private data extns for the Appln identified by the GUID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文