예제 #1
0
 public void SynchroniseItems(IProgress <int> progress)
 {
     using (var connect = new AMSDbContext(HandyFunctions.ConnectionString()))
     {
         RebuildMirrorTable(connect, progress);
         // RebuildMirrorLinks(connect);
     }
 }
예제 #2
0
 public void SynchroniseItems(IProgress <int> progressCallback)
 {
     using (var connect = new AMSDbContext(HandyFunctions.ConnectionString()))
     {
         var ar = items.OrderBy(x => x.CompanyName).ThenBy(x => x.LastName).ThenBy(x => x.FirstName).ToArray();
         RebuildMirrorTable(ar, connect, progressCallback);
         // RebuildMirrorLinks(connect, ar);
     }
 }
예제 #3
0
        public void SynchroniseItems(IProgress <int> progressCallback)
        {
            var info = "";

            using (var connect = new AMSDbContext(HandyFunctions.ConnectionString()))
            {
                var ar = items.OrderBy(x => x.Number).ToArray();
                //	RebuildMirrorTable(ar, info, connect,progressCallback);

                //	RebuildMirrorLinks(connect, ar);
            }
        }
예제 #4
0
        //private void RebuildMirrorLinks(AMSDbContext connect, TaxCode[] ar)
        //{
        //	// disconnect mismatches
        //	foreach (var taxrate in connect.TaxRates.Where(x => x.MYOBTaxCode != null && x.Code != x.MYOBTaxCode.Code))
        //	{
        //		taxrate.MYOBTaxCode = null;
        //	}
        //	connect.SaveChanges();

        //	// add missing TaxRates
        //	foreach (var item in ar)
        //	{
        //		HandyFunctions.Log("checking {0}", item.Code);
        //		var taxRate = connect.TaxRates.SingleOrDefault(x => x.Code == item.Code);

        //		if (taxRate == null)
        //		{
        //			var jtItem = GetMatchingJTItemByUid(connect, item);

        //			taxRate = new TaxRate { Code = jtItem.Code, Percentage = jtItem.Rate, MYOBTaxCode = jtItem };
        //			connect.TaxRates.Add(taxRate);
        //			taxRate.MYOBTaxCode = jtItem;
        //		}
        //		else
        //		{
        //			if (taxRate.MYOBTaxCode == null)
        //			{
        //				var jtItem = GetMatchingJTItemByUid(connect, item);
        //				taxRate.MYOBTaxCode = jtItem;
        //			}
        //		}
        //	}
        //	connect.SaveChanges();
        //}

        private void RebuildMirrorTable(TaxCode[] ar, AMSDbContext connect, IProgress <int> progressCallback)
        {
            var info     = "";
            var stepNum  = 1;
            var numSteps = ar.Count();

            try
            {
                foreach (var item in ar)
                {
                    if (item.Code == info)
                    {
                        throw new ExceptionDuplicateKey($"Taxcode {info}");
                    }
                    info = item.Code;
                    HandyFunctions.Log(info);
                    var jtItem = GetMatchingJTItemByUid(connect, item);

                    if (jtItem == null)
                    {
                        jtItem = new MYOBTaxCode();
                        connect.MYOBTaxCodes.Add(jtItem);
                    }

                    CopyItemFromMYOB(connect, jtItem, item);
                    progressCallback.Report((100 * stepNum) / numSteps);

                    stepNum++;
                }
                connect.SaveChanges();

                // delete missing
                foreach (var taxcode in connect.MYOBTaxCodes)
                {
                    var item = ar.SingleOrDefault(x => x.UID == taxcode.Uid);
                    if (item == null)
                    {
                        //var codeString = item.Code;
                        //foreach (var rate in connect.TaxRates.Where(x => x.MYOBTaxCode != null && x.MYOBTaxCode.Code == codeString))
                        //{
                        //	rate.MYOBTaxCode = null;
                        //}
                        connect.MYOBTaxCodes.Remove(taxcode);
                    }
                }
                connect.SaveChanges();
            }
            catch (Exception ex)
            {
                ex.Data.Add("TaxCode", info);
                throw;
            }
        }
예제 #5
0
        public async t.Task <int> SyncTable(
            MYOBTypeEnum type,
            ApiConfiguration myConfiguration,
            CompanyFile myCompanyFile,
            CompanyFileCredentials myCredentials,
            OAuthKeyService myOAuthKeyService,
            CancellationToken ct,
            IProgress <int> progress)
        {
            var          _currentPage  = 1;
            const double PageSize      = 400;
            var          myMYOBhandler = MakeServiceHandler(type, myConfiguration, myOAuthKeyService);

            try
            {
                var totalPages = 0;
                var numAdded   = 0;
                do
                {
                    var pageFilter =
                        $"$top={PageSize}&$skip={PageSize * (_currentPage - 1)}&$orderby={myMYOBhandler.OrderBy}";
                    numAdded = await myMYOBhandler.Addtems(myCompanyFile, pageFilter, myCredentials, ct);

                    if (totalPages == 0)
                    {
                        totalPages = (int)Math.Ceiling(myMYOBhandler.ItemCount / PageSize);
                    }
                    _currentPage++;
                } while (numAdded > 0); //(_currentPage <= totalPages);

                myMYOBhandler.SynchroniseItems(progress);
                return(myMYOBhandler.ItemCount);
            }
            catch (ApiCommunicationException ex)
            {
                HandyFunctions.Log(ex.ToString());
                throw;
            }
            catch (Exception ex)
            {
                HandyFunctions.Log(ex.ToString());
                throw;
            }
        }
예제 #6
0
        public async void AddItemToMyob(
            AMSDbContext connect,
            MYOBCustomer jtMyobCustomer,
            CompanyFile myCompanyFile,
            CompanyFileCredentials myCredentials,
            CancellationToken ct)
        {
            var item = new Customer
            {
                IsActive    = jtMyobCustomer.IsActive,
                CompanyName = jtMyobCustomer.CompanyName,
                FirstName   = jtMyobCustomer.FirstName,
                LastName    = jtMyobCustomer.LastName
            };

            HandyFunctions.Log(item.ToJson2());
            var   task = _myService.InsertAsync(myCompanyFile, item, myCredentials, ct, ErrorLevel.WarningsAsErrors);
            await task;

            jtMyobCustomer.Uid = item.UID;
            connect.SaveChanges();
        }
예제 #7
0
        private static void AddNewUpdateExistingRecords(
            Customer[] ar,
            AMSDbContext connect,
            IProgress <int> progressCallback)
        {
            var sb       = new StringBuilder();
            var info     = "";
            var stepNum  = 1;
            var numSteps = ar.Count();

            foreach (var item in ar)
            {
                if (MakeKey(item) == info)
                {
                    // skip this one
                    //throw new ExceptionDuplicateKey($"Unable to import. Duplicate key exists in MYOB for: {info}");
                    sb.AppendLine(info);
                    continue;
                }
                info = MakeKey(item);
                HandyFunctions.Log(info);
                var jtCustomer = connect.MYOBCustomers.SingleOrDefault(x => x.Uid == item.UID);

                //unique idex should ensure only 1
                if (jtCustomer == null)
                {
                    jtCustomer = new MYOBCustomer();
                    connect.MYOBCustomers.Add(jtCustomer);
                }
                CopyRecordFromMyob(connect, jtCustomer, item);
                progressCallback.Report((100 * stepNum) / numSteps);
                stepNum++;
            }
            connect.SaveChanges();
            if (sb.Length > 0)
            {
                MessageBox.Show("Duplicate customers skipped /n" + sb);
            }
        }
예제 #8
0
        private string RebuildMirrorTable(Account[] ar, string info, AMSDbContext connect, IProgress <int> progressCallback)
        {
            var stepNum  = 0;
            var numSteps = ar.Count();

            foreach (var item in ar)
            {
                if (item.DisplayID == info)
                {
                    throw new ExceptionDuplicateKey($"DisplayID {info}");
                }
                info = item.DisplayID;
                HandyFunctions.Log(info);
                var jtItem = GetMatchingJTItemByUid(connect, item);

                if (jtItem == null)
                {
                    jtItem = new MYOBAccount();
                    connect.MYOBAccounts.Add(jtItem);
                }

                CopyItemFromMYOB(jtItem, item);
                progressCallback.Report((100 * stepNum) / numSteps);

                stepNum++;
            }
            connect.SaveChanges();

            foreach (var acc in connect.MYOBAccounts)
            {
                var item = ar.SingleOrDefault(x => x.UID == acc.Uid);
                if (item == null)
                {
                    connect.MYOBAccounts.Remove(acc);
                }
            }
            connect.SaveChanges();
            return(info);
        }
예제 #9
0
        public void SynchroniseItems(IProgress <int> progressCallback)
        {
            var info = "";

            try
            {
                // psuedo code
                // add item if it is missing.
                // update item if it needs updating
                // delete items that no longer match MYOB's UID

                var ar = items.OrderBy(x => x.DisplayID).ToArray();
                using (var connect = new AMSDbContext(HandyFunctions.ConnectionString()))
                {
                    info = RebuildMirrorTable(ar, info, connect, progressCallback);
                }
            }
            catch (Exception ex)
            {
                ex.Data.Add("ItemName", info);
                throw;
            }
        }
예제 #10
0
        private static void AddNewUpdateExistingRecords(
            Supplier[] ar,
            AMSDbContext connect,
            IProgress <int> progressCallback)
        {
            var info     = "";
            var stepNum  = 1;
            var numSteps = ar.Count();

            foreach (var item in ar)
            {
                var key = HandyContactFunctions.MakeKey(
                    item.IsIndividual,
                    item.LastName,
                    item.FirstName,
                    item.CompanyName);
                if (key == info)
                {
                    throw new ExceptionDuplicateKey($"{info}");
                }
                info = key;
                HandyFunctions.Log(info);
                var jtSupplier = connect.MYOBSuppliers.SingleOrDefault(x => x.Uid == item.UID);

                //unique idex should ensure only 1
                if (jtSupplier == null)
                {
                    jtSupplier = new MYOBSupplier();
                    connect.MYOBSuppliers.Add(jtSupplier);
                }
                CopyRecordFromMYOB(connect, jtSupplier, item);
                progressCallback.Report((100 * stepNum) / numSteps);
                stepNum++;
            }
            connect.SaveChanges();
        }
예제 #11
0
        private static void SyncAddresses(AMSDbContext connect, MYOBCustomer jtItem, Customer item)
        {
            foreach (var a in jtItem.Addresses)
            {
                a.TaggedToDelete = true;
            }
            if (item.Addresses != null)
            {
                foreach (var address in item.Addresses)
                {
                    var addresses         = jtItem.Addresses.AsQueryable();
                    var tempAddress       = address;
                    var existingAddresses = addresses.Where(x => x.Location == tempAddress.Location).ToArray();
                    if (existingAddresses.Count() == 1)
                    {
                        var existingAddress = existingAddresses[0];
                        ModifyIfNeeded(
                            connect,
                            existingAddress,
                            "ContactName",
                            existingAddress.ContactName,
                            tempAddress.ContactName);
                        ModifyIfNeeded(connect, existingAddress, "Street", existingAddress.Street, tempAddress.Street);
                        ModifyIfNeeded(connect, existingAddress, "City", existingAddress.City, tempAddress.City);
                        ModifyIfNeeded(connect, existingAddress, "State", existingAddress.State, tempAddress.State);
                        ModifyIfNeeded(
                            connect,
                            existingAddress,
                            "Postcode",
                            existingAddress.Postcode,
                            tempAddress.PostCode);
                        ModifyIfNeeded(
                            connect,
                            existingAddress,
                            "Country",
                            existingAddress.Country,
                            tempAddress.Country);
                        ModifyIfNeeded(connect, existingAddress, "Email", existingAddress.Email, tempAddress.Email);
                        ModifyIfNeeded(connect, existingAddress, "Phone1", existingAddress.Phone1, tempAddress.Phone1);
                        ModifyIfNeeded(connect, existingAddress, "Phone2", existingAddress.Phone2, tempAddress.Phone2);
                        ModifyIfNeeded(connect, existingAddress, "Fax", existingAddress.Fax, tempAddress.Fax);
                        ModifyIfNeeded(connect, existingAddress, "WebSite", existingAddress.WebSite, tempAddress.Website);

                        existingAddress.TaggedToDelete = false;
                        if (existingAddress.ContactName != tempAddress.ContactName)
                        {
                            existingAddress.ContactName          = tempAddress.ContactName;
                            connect.Entry(existingAddress).State = EntityState.Modified;
                        }
                    }
                    else
                    {
                        foreach (var duplAddress in existingAddresses)
                        {
                            duplAddress.TaggedToDelete = true;
                        }
                        if (string.IsNullOrEmpty(address.Street))
                        {
                            continue;
                        }
                        var newAddress = new MYOBCustomerAddress();
                        HandyFunctions.PopulateMyobAddress(newAddress, address);
                        jtItem.Addresses.Add(newAddress);
                        newAddress.MyobCustomer = jtItem;
                    }
                }
            }
            var deleteQueue = jtItem.Addresses.Where(x => x.TaggedToDelete).ToArray();

            foreach (var a in deleteQueue)
            {
                var entry = connect.Entry(a);
                entry.State = EntityState.Deleted;
            }
        }