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); } }
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)); } }