public async Task <IActionResult> SyncCustomers()
        {
            var errorList        = new List <string>();
            var customersCreated = 0;
            var customersUpdated = 0;

            try
            {
                if (_db.Connection.State == System.Data.ConnectionState.Closed)
                {
                    await _db.Connection.OpenAsync();
                }

                var query     = new CustomerQueries(_db);
                var customers = await query.GetAllCustomers();

                foreach (var customer in customers)
                {
                    var limit = 0;
                    int.TryParse(customer._pos_customer_accountlimit, out limit);

                    var found = await _context.Customer.FindAsync(int.Parse(customer.id.ToString()));

                    if (found == null)
                    {
                        customersCreated++;
                        var newCustomer = new Customer
                        {
                            Address      = customer._pos_customer_address,
                            City         = customer._pos_customer_city,
                            CompanyName  = customer._pos_customer_company_name,
                            Country      = customer._pos_customer_country,
                            CreditLimit  = limit,
                            CustomerCode = customer.id.ToString(),
                            CustomerId   = int.Parse(customer.id.ToString()),
                            Email        = customer._pos_customer_email,
                            FirstName    = customer._pos_customer_first_name,
                            LastName     = customer._pos_customer_last_name,
                            Mobile       = customer._pos_customer_mobile,
                            PhoneNumber  = customer._pos_customer_phone,
                            PostalCode   = customer._pos_customer_postal_code,
                            Province     = customer._pos_customer_province,
                            PstNumber    = customer._pos_customer_pst_number,
                            Status       = "",
                            UserName     = customer._pos_customer_email,
                            Website      = customer._pos_customer_contractorlink
                        };
                        await _context.Customer.AddAsync(newCustomer);

                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        customersUpdated++;
                        found.Address      = customer._pos_customer_address;
                        found.City         = customer._pos_customer_city;
                        found.CompanyName  = customer._pos_customer_company_name;
                        found.Country      = customer._pos_customer_country;
                        found.CreditLimit  = limit;
                        found.CustomerCode = customer.id.ToString();
                        // found.CustomerId = int.Parse(customer.id.ToString());
                        found.Email       = customer._pos_customer_email;
                        found.FirstName   = customer._pos_customer_first_name;
                        found.LastName    = customer._pos_customer_last_name;
                        found.Mobile      = customer._pos_customer_mobile;
                        found.PhoneNumber = customer._pos_customer_phone;
                        found.PostalCode  = customer._pos_customer_postal_code;
                        found.Province    = customer._pos_customer_province;
                        found.PstNumber   = customer._pos_customer_pst_number;
                        found.Status      = "";
                        found.UserName    = customer._pos_customer_email;
                        found.Website     = customer._pos_customer_contractorlink;
                        await _context.SaveChangesAsync();
                    }
                }
                _db.Connection.Close();
            }
            catch (Exception ex)
            {
                errorList.Add("order taxes:" + ex.ToString());
            }

            await _emailSender.SendEmailAsync("*****@*****.**", "Sync Finished: Customers", $"Sync Finished: Customers.  \n Customers Created: {customersCreated}. \n Customers Updated: {customersUpdated}. \n Errors: {string.Join(",", errorList)}");

            return(Ok(errorList));
        }
Exemplo n.º 2
0
 public IEnumerable <CustomerDTO> GetAllCustomers()
 {
     return(cq.GetAllCustomers());
 }