예제 #1
0
            public static List <Quickbooks.DataTypes.Preferences.CustomField> EnumerateCustomFields()
            {
                try
                {
                    String query = @"
SELECT
	*
FROM
	Preferences
";
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


                    dynamic response = JsonConvert.DeserializeObject(Quickbooks.SubmitQuery(query));
                    if (response != null && response.QueryResponse != null && response.QueryResponse.Preferences != null)                     // Custom fields exist
                    {
                        List <Quickbooks.DataTypes.Preferences.CustomField> customFields = new List <DataTypes.Preferences.CustomField>();


                        foreach (var item in response.QueryResponse.Preferences[0].SalesFormsPrefs.CustomField[1].CustomField)
                        {
                            Quickbooks.DataTypes.Preferences.CustomField customField = new Quickbooks.DataTypes.Preferences.CustomField
                            {
                                ID          = PeakeyTools.Database.MySQL.NullInt32(item.Name.Value.Substring(item.Name.Value.Length - 1, 1)),                        // Parse ID since QB includes the ID in the name field for some reason (Can only have up to 3 custom fields, so subtract 1)
                                Name        = item.Name.Value,
                                Type        = item.Type.Value,
                                StringValue = item.StringValue.Value
                            };

                            customFields.Add(customField);
                        }


                        if (customFields.Count > 0)
                        {
                            return(customFields);
                        }
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "An error has occurred while attempting to enumerate Quickbook custom fields. " + ex.Message, "EXCEPTION");
                    throw new Exception("Could not enumerate Quickbook custom fields.", ex);
                }
            }
예제 #2
0
            public static List <Quickbooks.DataTypes.Term> Enumerate()
            {
                try
                {
                    String query = @"
SELECT
	*
FROM
	Term
";
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


                    dynamic response = JsonConvert.DeserializeObject(Quickbooks.SubmitQuery(query));
                    if (response != null && response.QueryResponse != null && response.QueryResponse.Term != null)                     // Term exists
                    {
                        List <Quickbooks.DataTypes.Term> terms = new List <DataTypes.Term>();


                        foreach (var item in response.QueryResponse.Term)
                        {
                            Quickbooks.DataTypes.Term term = new Quickbooks.DataTypes.Term
                            {
                                ID     = PeakeyTools.Database.MySQL.NullInt32(item.Id.Value),
                                Name   = item.Name.Value,
                                Active = PeakeyTools.Database.MySQL.NullBoolean(item.Active.Value)
                            };

                            terms.Add(term);
                        }


                        if (terms.Count > 0)
                        {
                            return(terms);
                        }
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "An error has occurred while attempting to enumerate Quickbook terms. " + ex.Message, "EXCEPTION");
                    throw new Exception("Could not enumerate Quickbook terms.", ex);
                }
            }
예제 #3
0
            public static Quickbooks.DataTypes.Item FindByName(String itemName)
            {
                try
                {
                    String query = String.Format(@"
SELECT
	*
FROM
	Item
WHERE
	Name = '{0}'
", itemName);
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


                    dynamic response = JsonConvert.DeserializeObject(Quickbooks.SubmitQuery(query));
                    if (response != null && response.QueryResponse != null && response.QueryResponse.Item != null)
                    {
                        dynamic data = response.QueryResponse.Item[0];

                        Quickbooks.DataTypes.Item item = new Quickbooks.DataTypes.Item
                        {
                            ID   = PeakeyTools.Database.MySQL.NullInt32(data.Id.Value),
                            Name = data.Name.Value
                        };

                        return(item);
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "Could not find Quickbook Item by name. [Item Name]: " + itemName + " " + ex.Message, "EXCEPTION");
                    throw new Exception("An error has occurred while checking if the customer exists in Quickbooks.", ex);
                }
            }
예제 #4
0
            private static Quickbooks.DataTypes.Customer FindExisting(String firstName, String lastName)
            {
                try
                {
                    String query = String.Format(@"
SELECT
	*
FROM
	Customer
WHERE
	GivenName = '{0}' AND
	FamilyName = '{1}'
", firstName, lastName);
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


                    dynamic response = JsonConvert.DeserializeObject(Quickbooks.SubmitQuery(query));
                    if (response != null && response.QueryResponse != null && response.QueryResponse.Customer != null)                     // Customer exists
                    {
                        dynamic data = response.QueryResponse.Customer[0];

                        Quickbooks.DataTypes.Customer customer = new Quickbooks.DataTypes.Customer
                        {
                            ID         = PeakeyTools.Database.MySQL.NullInt32(data.Id.Value),
                            GivenName  = data.GivenName.Value,
                            FamilyName = data.FamilyName.Value
                        };

                        if (data.SalesTermRef != null)
                        {
                            customer.TermID   = PeakeyTools.Database.MySQL.NullInt32(data.SalesTermRef.value);
                            customer.TermName = data.SalesTermRef.name;
                        }
                        if (data.DisplayName != null)
                        {
                            customer.DisplayName = data.DisplayName.Value;
                        }
                        if (data.PrimaryEmailAddr != null)
                        {
                            customer.PrimaryEmailAddr = data.PrimaryEmailAddr.Address.Value;
                        }
                        if (data.PrimaryPhone != null)
                        {
                            customer.PrimaryPhone = data.PrimaryPhone.FreeFormNumber.Value;
                        }

                        return(customer);
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "An error has occurred while checking if the customer exists in Quickbooks. " + ex.Message +
                                        "\t[First Name]: " + firstName +
                                        "\t[Last Name]: " + lastName,
                                        "EXCEPTION");
                    throw new Exception("An error has occurred while checking if the customer exists in Quickbooks.", ex);
                }
            }
예제 #5
0
            private static List <Quickbooks.DataTypes.Invoice> EnumerateByCustomer(DataTypes.Customer customer)
            {
                try
                {
                    String query = String.Format(@"
SELECT
	Id,
	CustomField,
	DocNumber,
	SyncToken
FROM
	Invoice
WHERE
	CustomerRef = '{0}'
", customer.ID);
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


                    dynamic response = JsonConvert.DeserializeObject(Quickbooks.SubmitQuery(query));
                    if (response != null && response.QueryResponse != null && response.QueryResponse.Invoice != null)                     // Invoice exists
                    {
                        List <Quickbooks.DataTypes.Invoice> invoices = new List <Quickbooks.DataTypes.Invoice>();


                        foreach (var item in response.QueryResponse.Invoice)
                        {
                            Quickbooks.DataTypes.Invoice invoice = new Quickbooks.DataTypes.Invoice
                            {
                                ID        = PeakeyTools.Database.MySQL.NullInt32(item.Id.Value),
                                SyncToken = PeakeyTools.Database.MySQL.NullInt32(item.SyncToken.Value)                                 // Required on update, to lock an object for use (will receive an 'Stale Object Error' if not used)
                            };

                            if (item.DocNumber != null)
                            {
                                invoice.DocNumber = PeakeyTools.Database.MySQL.NullString(item.DocNumber.Value);
                            }

                            // Loop through each custom field
                            foreach (var customFieldItem in item.CustomField)
                            {
                                if (customFieldItem.StringValue != null)
                                {
                                    switch (customFieldItem.Name.Value.ToLower())
                                    {
                                    case "sales rep":
                                        invoice.SalesRep = customFieldItem.StringValue.Value;
                                        break;

                                    case "office id":
                                        invoice.OfficeID = customFieldItem.StringValue.Value;
                                        break;
                                    }
                                }
                            }

                            invoices.Add(invoice);
                        }


                        if (invoices.Count > 0)
                        {
                            return(invoices);
                        }
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "Could not enumerate Quickbook invoices by customer. " + ex.Message +
                                        "\t[First Name]: " + customer.GivenName +
                                        "\t[Last Name]: " + customer.FamilyName,
                                        "EXCEPTION");
                    throw new Exception("Could not enumerate Quickbook invoices by customer.", ex);
                }
            }