public IHttpActionResult PostSync( [FromBody] QBDInventoryItemSyncDetails details, [FromUri] string productName, [FromUri] string majorVersion, [FromUri] string minorVersion, [FromUri] string country, [FromUri] string supportedQBXMLVersion, [FromUri] string hostname, [FromUri] string companyFilePath) { var currentUser = CurrentUser(); // Ensure that user is authorized. if (!currentUser.CanSyncInventoryItems) { return(StatusCode(HttpStatusCode.Forbidden)); } // Ensure that the sync is for the same company file. var companyFileName = Path.GetFileName(companyFilePath); var previous = _context.QBDInventoryItemSyncs .Where(q => q.OrganizationId == currentUser.OrganizationId) .Where(q => q.HostCompanyFileName != companyFileName); if (previous.Any()) { return(BadRequest("The company file appears to be different.")); } // Attempt to sync. using (var transaction = _context.Database.BeginTransaction()) { try { var sync = new QBDInventoryItemSync() { CreatedAt = DateTime.UtcNow, CreatedByUserId = currentUser.Id, OrganizationId = currentUser.OrganizationId, HostProductName = productName, HostMajorVersion = majorVersion, HostMinorVersion = minorVersion, HostCountry = country, HostSupportedQBXMLVersion = supportedQBXMLVersion, HostCompanyFileName = companyFileName, HostCompanyFilePath = companyFilePath, Hostname = hostname }; _context.QBDInventoryItemSyncs.Add(sync); _context.SaveChanges(); // ---------------------------------------------------------------- // Create or update the inventory sites // ---------------------------------------------------------------- foreach (var site in details.InventorySites) { // Find by list id, which is unique across inventory sites var found = _context.QBDInventorySites .Where(s => s.ListId == site.ListId) .FirstOrDefault(); if (found != null) { // Update any changes found.FullName = site.FullName; } else { // Create the inventory site site.QBDInventoryItemSyncId = sync.Id; _context.QBDInventorySites.Add(site); } } _context.SaveChanges(); // ---------------------------------------------------------------- // Create or update the unit of measure sets // ---------------------------------------------------------------- foreach (var unit in details.UnitOfMeasureSets) { // Find by list id, which is unique across unit of measure sets var found = _context.QBDUnitOfMeasureSets .Where(s => s.ListId == unit.ListId) .FirstOrDefault(); if (found != null) { // Update any changes found.Name = unit.Name; found.UnitNamesAndAbbreviations = unit.UnitNamesAndAbbreviations; found.IsActive = unit.IsActive; found.UnitOfMeasureType = unit.UnitOfMeasureType; } else { // Create the unit of measure set unit.QBDInventoryItemSyncId = sync.Id; _context.QBDUnitOfMeasureSets.Add(unit); } } _context.SaveChanges(); // ---------------------------------------------------------------- // Create or update the inventory items // ---------------------------------------------------------------- foreach (var item in details.InventoryItems) { // Find by list id, which is unique across inventory items for the organization var found = _context.QBDInventoryItems .Where(i => i.OrganizationId == currentUser.OrganizationId) .Where(i => i.ListId == item.ListId) .FirstOrDefault(); if (found != null) { // Update any changes. found.BarCodeValue = string.IsNullOrEmpty(item.BarCodeValue) ? "" : item.BarCodeValue; found.ManufacturerPartNumber = string.IsNullOrEmpty(item.ManufacturerPartNumber) ? "" : item.ManufacturerPartNumber; found.PurchaseDescription = string.IsNullOrEmpty(item.PurchaseDescription) ? "" : item.PurchaseDescription; found.PurchaseCost = item.PurchaseCost; found.Name = item.Name; found.FullName = item.FullName; found.SalesDescription = string.IsNullOrEmpty(item.SalesDescription) ? "" : item.SalesDescription; found.SalesPrice = item.SalesPrice; found.QBDCOGSAccountFullName = item.QBDCOGSAccountFullName; found.QBDCOGSAccountListId = item.QBDCOGSAccountListId; found.OffsetItemFullName = string.IsNullOrEmpty(item.OffsetItemFullName) ? "" : item.OffsetItemFullName; // Associate the Unit of Measure Set. found.QBDUnitOfMeasureSetFullName = string.IsNullOrEmpty(item.QBDUnitOfMeasureSetFullName) ? "" : item.QBDUnitOfMeasureSetFullName; found.QBDUnitOfMeasureSetListId = string.IsNullOrEmpty(item.QBDUnitOfMeasureSetListId) ? "" : item.QBDUnitOfMeasureSetListId; if (!string.IsNullOrEmpty(item.QBDUnitOfMeasureSetFullName)) { var uomsId = _context.QBDUnitOfMeasureSets .Where(u => u.ListId == found.QBDUnitOfMeasureSetListId) .Select(u => u.Id) .FirstOrDefault(); found.QBDUnitOfMeasureSetId = uomsId; } } else { // Create the inventory item - massage properties if necessary. item.QBDInventoryItemSyncId = sync.Id; item.BarCodeValue = string.IsNullOrEmpty(item.BarCodeValue) ? "" : item.BarCodeValue; item.CustomBarCodeValue = ""; // Default to blank item.ManufacturerPartNumber = string.IsNullOrEmpty(item.ManufacturerPartNumber) ? "" : item.ManufacturerPartNumber; item.PurchaseDescription = string.IsNullOrEmpty(item.PurchaseDescription) ? "" : item.PurchaseDescription; item.SalesDescription = string.IsNullOrEmpty(item.SalesDescription) ? "" : item.SalesDescription; item.OrganizationId = currentUser.OrganizationId; item.OffsetItemFullName = string.IsNullOrEmpty(item.OffsetItemFullName) ? "" : item.OffsetItemFullName; // Associate the Unit of Measure Set. item.QBDUnitOfMeasureSetFullName = string.IsNullOrEmpty(item.QBDUnitOfMeasureSetFullName) ? "" : item.QBDUnitOfMeasureSetFullName; item.QBDUnitOfMeasureSetListId = string.IsNullOrEmpty(item.QBDUnitOfMeasureSetListId) ? "" : item.QBDUnitOfMeasureSetListId; if (!string.IsNullOrEmpty(item.QBDUnitOfMeasureSetFullName)) { var uomsId = _context.QBDUnitOfMeasureSets .Where(u => u.ListId == item.QBDUnitOfMeasureSetListId) .Select(u => u.Id) .FirstOrDefault(); item.QBDUnitOfMeasureSetId = uomsId; } _context.QBDInventoryItems.Add(item); } } _context.SaveChanges(); transaction.Commit(); return(Ok()); } catch (DbEntityValidationException ex) { string message = ""; foreach (var eve in ex.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { message += string.Format("{0} has error '{1}'; ", ve.PropertyName, ve.ErrorMessage); } } transaction.Rollback(); return(BadRequest(message)); } catch (DbUpdateConcurrencyException ex) { transaction.Rollback(); return(BadRequest(ex.Message)); } catch (DbUpdateException ex) { transaction.Rollback(); return(BadRequest(ex.ToString())); } } }
public void Prepare() { try { // ---------------------------------------------------------------- // Scaffold an organization. // ---------------------------------------------------------------- var organization = new Organization() { CreatedAt = DateTime.UtcNow, Name = "Test Organization", Code = "1234", StripeCustomerId = "BLANK", StripeSubscriptionId = "BLANK", SortCustomersByColumn = "Number", SortProjectsByColumn = "Number", SortTasksByColumn = "Number", ShowCustomerNumber = true, ShowProjectNumber = true, ShowTaskNumber = true }; _context.Organizations.Add(organization); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold a user. // ---------------------------------------------------------------- var user = new User() { CreatedAt = DateTime.UtcNow, EmailAddress = "*****@*****.**", Name = "Test User A", OrganizationId = organization.Id, IsDeleted = false, IsActive = true, Pin = "0000", Role = "Standard", TimeZone = "America/New_York", AllowedPhoneNumbers = "*", UsesTouchToneClock = true, CanCreateUsers = true, CanDeleteUsers = true, CanModifyUsers = true, CanViewUsers = true, CanCreateCustomers = true, CanDeleteCustomers = true, CanModifyCustomers = true, CanViewCustomers = true, CanCreateProjects = true, CanDeleteProjects = true, CanModifyProjects = true, CanViewProjects = true, CanCreateTasks = true, CanDeleteTasks = true, CanModifyTasks = true, CanViewTasks = true, CanCreateRates = true, CanDeleteRates = true, CanModifyRates = true, CanViewRates = true, CanCreateLocks = true, CanUndoLocks = true, CanViewLocks = true, CanCreatePunches = true, CanDeletePunches = true, CanModifyPunches = true, CanViewPunches = true }; _context.Users.Add(user); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold a customer. // ---------------------------------------------------------------- var customer = new Customer() { CreatedAt = DateTime.UtcNow, Number = "1000", Name = "General Electric", OrganizationId = organization.Id }; _context.Customers.Add(customer); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold a project. // ---------------------------------------------------------------- var customerId = _context.Customers .Where(c => c.Name == "General Electric") .Select(c => c.Id) .FirstOrDefault(); var project = new Job() { CreatedAt = DateTime.UtcNow, Number = "1000", Name = "Install Motor", QuickBooksCustomerJob = "", QuickBooksClass = "", CustomerId = customerId, Status = "Open" }; _context.Jobs.Add(project); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold a task. // ---------------------------------------------------------------- var jobId = _context.Jobs .Where(j => j.Name == "Install Motor") .Select(j => j.Id) .FirstOrDefault(); var task = new Task() { CreatedAt = DateTime.UtcNow, Number = "1000", Name = "Installation", JobId = jobId }; _context.Tasks.Add(task); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold a punch. // ---------------------------------------------------------------- var punch = new Punch() { CreatedAt = DateTime.UtcNow, TaskId = task.Id, InAt = new DateTime(2022, 1, 1, 8, 0, 0), OutAt = new DateTime(2022, 1, 1, 17, 0, 0), Guid = Guid.NewGuid(), UserId = user.Id }; _context.Punches.Add(punch); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold an inventory item sync. // ---------------------------------------------------------------- var inventoryItemSync = new QBDInventoryItemSync() { OrganizationId = organization.Id, CreatedAt = DateTime.UtcNow, CreatedByUserId = user.Id, Hostname = "HOSTNAME-01", HostCompanyFileName = "COMPANY FILE NAME", HostCompanyFilePath = "PATH TO FILE", HostCountry = "US", HostMajorVersion = "1", HostMinorVersion = "0", HostProductName = "PRODUCT NAME", HostSupportedQBXMLVersion = "12" }; _context.QBDInventoryItemSyncs.Add(inventoryItemSync); _context.SaveChanges(); // ---------------------------------------------------------------- // Scaffold an inventory item. // ---------------------------------------------------------------- var inventoryItem = new QBDInventoryItem() { ListId = "019F79CACE27HDKDUD32", Name = "25-ft 12/2 Solid NM", FullName = "25-ft 12/2 Solid NM", PurchaseCost = 38.49m, PurchaseDescription = "Romex SIMpull 25-ft 12/2 Solid Non-Metallic Wire (By-the-Roll)", SalesPrice = 48.11m, SalesDescription = "Romex SIMpull 25-ft 12/2 Solid Non-Metallic Wire (By-the-Roll)", BarCodeValue = "70012", CustomBarCodeValue = "70012", OrganizationId = organization.Id, ManufacturerPartNumber = "28828221", QBDInventoryItemSyncId = inventoryItemSync.Id }; _context.QBDInventoryItems.Add(inventoryItem); _context.SaveChanges(); } catch (DbUpdateException ex) { Trace.TraceError(ex.ToString()); } catch (Exception ex) { Trace.TraceError(ex.ToString()); } }