Beispiel #1
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);
                }
            }
Beispiel #2
0
            public static Quickbooks.DataTypes.Term FindByName(String termName)
            {
                try
                {
                    String query = String.Format(@"
SELECT
	*
FROM
	Term
WHERE
	Name = '{0}'
", termName);
                    query = PeakeyTools.Database.MySQL.TrimQueryText(query);


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

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

                        return(term);
                    }


                    return(null);
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "Could not find Quickbook Term by name. [Term Name]: " + termName + " " + ex.Message, "EXCEPTION");
                    throw new Exception("An error has occurred while checking if the customer exists in Quickbooks.", ex);
                }
            }
Beispiel #3
0
            public static String SyncToQuickbooks(Int32 orderTransactionID)
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                DataTable         quickbookData     = null;

                try
                {
                    // Retrieve refresh token & realmID from database (which is saved from a CF page after logging into QB)
                    RetrieveCredentials();


                    // Retrieve oAuth credentials
                    //clientCredentials = StairSupplies.API.Quickbooks.RetrieveCredentialsWithoutLogin();


                    // Retrieve information from database
                    quickbookData = StairSupplies.Database.TblOrdersBOM.LoadQuickbookInformation(orderTransactionID);
                    if (quickbookData.Rows.Count == 0)
                    {
                        PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "Could not retrieve information from database for Quickbooks.", "EXCEPTION");
                        return(PeakeyTools.Api.ToJSON(false, true, "Could not retrieve information from database for Quickbooks."));
                    }


                    // Retrieve item 'Products and Services'
                    Quickbooks.DataTypes.Item itemProductAndServices = StairSupplies.API.Quickbooks.Item.FindByName("Products and Services");


                    // Retrieve term 'Due on receipt'
                    Quickbooks.DataTypes.Term termDueOnReceipt = Quickbooks.Term.FindByName("Due on receipt");


                    // Create customer if not existing
                    Int32  orderID   = PeakeyTools.Database.MySQL.NullInt32(quickbookData.Rows[0]["OrderID"].ToString());
                    String firstName = PeakeyTools.Database.MySQL.NullString(quickbookData.Rows[0]["BillContactFirstName"].ToString());
                    String lastName  = PeakeyTools.Database.MySQL.NullString(quickbookData.Rows[0]["BillContactLastName"].ToString());
                    //String orderTermName = PeakeyTools.Database.MySQL.NullString(quickbookData.Rows[0]["TermsName"].ToString());
                    String   salesEmployeeFirstName = PeakeyTools.Database.MySQL.NullString(quickbookData.Rows[0]["SalesEmployeeFirstName"].ToString());
                    DateTime transactionDate        = PeakeyTools.Database.MySQL.NullDate(quickbookData.Rows[0]["TransactionDate"].ToString());
                    dynamic  customer = StairSupplies.API.Quickbooks.Customer.Create(firstName, lastName);
                    if (customer != null)
                    {
                        // Transaction info
                        DataTypes.Transaction transaction = new DataTypes.Transaction();
                        transaction.ID        = orderTransactionID;
                        transaction.OrderID   = orderID;
                        transaction.OfficeID  = transaction.OrderID + "-" + transaction.ID;                        // Set a unique ID for each transaction, to reference later on
                        transaction.DocNumber = transaction.OrderID.ToString();
                        transaction.Date      = transactionDate.ToString("yyyy-MM-ddTHH:mm:ss");                   // 2014-09-19T13:16:17-07:00 : "2017-08-01T01:33:50" yyyy-MM-ddTHH:mm:ssK
                        transaction.SalesEmployeeFirstName = salesEmployeeFirstName;


                        // Retrieve custom fields
                        List <Quickbooks.DataTypes.Preferences.CustomField> customFields = Quickbooks.Preferences.EnumerateCustomFields();


                        // Retrieve Products
                        List <Quickbooks.DataTypes.AccountingTypes> accountingTypes = new List <DataTypes.AccountingTypes>();
                        DataTable accountingTypeData = StairSupplies.Database.TblOrdersBOM.EnumerateAccountingTypesByTransactionID(orderTransactionID);
                        if (accountingTypeData.Rows.Count > 0)
                        {
                            foreach (DataRow row in accountingTypeData.Rows)
                            {
                                Quickbooks.DataTypes.AccountingTypes accountingType = new DataTypes.AccountingTypes();
                                accountingType.Title = PeakeyTools.Database.MySQL.NullString(row["Title"].ToString());
                                accountingType.Total = PeakeyTools.Database.MySQL.NullDouble(row["Total"].ToString());

                                accountingTypes.Add(accountingType);
                            }
                        }


                        // Create invoice
                        return(Create(customer, transaction, itemProductAndServices, termDueOnReceipt, customFields, accountingTypes));
                    }


                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "Failed to process Quickbook information for adding invoice." + "\t[OrderID]: " + orderID + "\t[OrderTransactionID]: " + orderTransactionID, "EXCEPTION");
                    return(PeakeyTools.Api.ToJSON(false, false, "Failed to add a Quickbook invoice."));
                }
                catch (Exception ex)
                {
                    PeakeyTools.Api.Log(PeakeyTools.Api.ApiName.quickbooks, "An error has occurred while attempting to add a Quickbook invoice. " + ex.Message + "\t[OrderTransactionID]: " + orderTransactionID, "EXCEPTION");
                    return(PeakeyTools.Api.ToJSON(false, true, "An error has occurred while attempting to add a Quickbook invoice. " + ex.Message));
                }
            }