Ejemplo n.º 1
0
        /// <summary>
        /// Determines whether this instance can transmit.
        /// <c>true</c> if this instance can transmit; otherwise, <c>false</c>.
        /// </summary>
        public override bool CanTransmit()
        {
            PartStates partState = (PartStates)this.protoPart.state;

            if (partState == PartStates.DEAD || partState == PartStates.DEACTIVATED)
            {
                Logging.PostDebugMessage(string.Format(
                                             "{0}: {1} on {2} cannot transmit: {3}",
                                             this.GetType().Name,
                                             this.Title,
                                             this.vessel.vesselName,
                                             Enum.GetName(typeof(PartStates), partState)
                                             ));
                return(false);
            }
            return(base.CanTransmit());
        }
Ejemplo n.º 2
0
        public dynamic MassLoad(HttpPostedFileBase dealers)
        {
            PartStates state = new PartStates();
            List<string> errors = new List<string>();
            try {
                CurtDevDataContext db = new CurtDevDataContext();

                string dir_path = Server.MapPath("/Content/locator_csv");
                GoogleMaps map = new GoogleMaps(System.Configuration.ConfigurationManager.AppSettings["GoogleMapsKey"]);

                DirectoryInfo dir_info = new DirectoryInfo(dir_path);
                int file_count = dir_info.GetFiles().Count();
                string file_path = Server.MapPath("/Content/locator_csv/" + (file_count + 1) + ".xlsx");
                dealers.SaveAs(file_path);
                FileInfo existingFile = new FileInfo(file_path);
                using (ExcelPackage package = new ExcelPackage(existingFile)) {
                    // get the first worksheet in the workbook
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                    int row = 2; //The first row with data
                    while (worksheet.Cells[row, 2].Value != null) {
                        // col 1 = Customer ID Code
                        // col 2 = Customer Name
                        // col 3 = Website
                        // col 4 = Location Name
                        // col 5 = Contact First Name
                        // col 6 = Contact Last Name
                        // col 7 = phone
                        // col 8 = street
                        // col 9 = city
                        // col 10 = state
                        // col 11 = zip
                        // col 12 = sales rep
                        // col 13 = Dealer Tier
                        // col 14 = Dealer Type
                        // col 15 = Parent curt acct # (meaning a third party client)

                        try {
                            int acctnum = (worksheet.Cells[row, 1].Value != null) ? Convert.ToInt32(worksheet.Cells[row, 1].Value) : 0;

                            string rep = (worksheet.Cells[row, 12].Value != null) ? worksheet.Cells[row, 12].Value.ToString() : "";
                            string dtier = worksheet.Cells[row, 13].Value.ToString().Trim().ToLower();
                            string dtype = worksheet.Cells[row, 14].Value.ToString().Trim().ToLower();
                            string customername = (worksheet.Cells[row, 2].Value != null) ? worksheet.Cells[row, 2].Value.ToString() : "";
                            string locationname = (worksheet.Cells[row, 4].Value != null) ? worksheet.Cells[row, 4].Value.ToString() : "";
                            string contact = ((worksheet.Cells[row, 5].Value != null) ? worksheet.Cells[row, 5].Value.ToString() : "") + ((worksheet.Cells[row, 6].Value != null) ? " " + worksheet.Cells[row, 6].Value.ToString() : "");
                            string website = (worksheet.Cells[row, 3].Value != null) ? worksheet.Cells[row, 3].Value.ToString().Trim() : null;
                            string statestr = (worksheet.Cells[row, 10].Value != null) ? worksheet.Cells[row, 10].Value.ToString() : "";
                            string postalCode = (worksheet.Cells[row, 11].Value != null) ? worksheet.Cells[row, 11].Value.ToString() : "";
                            string address = (worksheet.Cells[row, 8].Value != null) ? worksheet.Cells[row, 8].Value.ToString() : "";
                            string city = (worksheet.Cells[row, 9].Value != null) ? worksheet.Cells[row, 9].Value.ToString() : "";
                            string phone = (worksheet.Cells[row, 7].Value != null) ? worksheet.Cells[row, 7].Value.ToString() : "";
                            string parentacct = (worksheet.Cells[row, 15].Value != null) ? worksheet.Cells[row, 15].Value.ToString() : "";
                            int stateid = db.PartStates.Where(x => x.abbr.ToLower().Trim() == statestr.ToLower().Trim()).Select(x => x.stateID).FirstOrDefault();

                            LatLng geo = map.GetLatLng(address, city, stateid);

                            double latitude = Convert.ToDouble(geo.latitude);
                            double longitude = Convert.ToDouble(geo.longitude);

                            SalesRepresentative salesrep = db.SalesRepresentatives.Where(x => x.name.ToLower().Trim().Equals(rep.Trim().ToLower())).FirstOrDefault<SalesRepresentative>();
                            DealerType type = db.DealerTypes.Where(x => x.type.Trim().ToLower() == dtype).First();
                            DealerTier tier = db.DealerTiers.Where(x => x.tier.Trim().ToLower() == dtier).First();
                            Customer customer = new Customer();
                            if (acctnum != 0) {
                                customer = (from c in db.Customers
                                            join d in db.DealerTypes on c.dealer_type equals d.dealer_type
                                            where c.customerID.Equals(acctnum) && d.online.Equals(type.online)
                                            select c).FirstOrDefault<Customer>();
                            } else {
                                customer = (from c in db.Customers
                                            join d in db.DealerTypes on c.dealer_type equals d.dealer_type
                                            where c.name.ToLower().Equals(customername.Trim().ToLower()) && d.online.Equals(type.online) && c.parentID.Equals(Convert.ToInt32(parentacct))
                                            select c).FirstOrDefault<Customer>();
                            }
                            if (customer == null) {
                                // new customer
                                Customer new_customer = new Customer {
                                    name = customername,
                                    contact_person = contact,
                                    tier = tier.ID,
                                    dealer_type = type.dealer_type,
                                };

                                if (salesrep != null && salesrep.salesRepID != 0) {
                                    new_customer.salesRepID = salesrep.salesRepID;
                                }

                                if (acctnum != 0) {
                                    new_customer.customerID = acctnum;
                                }

                                if (parentacct.Trim() != "") {
                                    new_customer.parentID = Convert.ToInt32(parentacct);
                                }

                                if (website != "") {
                                    new_customer.website = website;
                                }

                                db.Customers.InsertOnSubmit(new_customer);
                                db.SubmitChanges();

                                if (address != "" && city != "" && postalCode != "") {
                                    CustomerLocation location = new CustomerLocation {
                                        cust_id = new_customer.cust_id,
                                        name = locationname,
                                        address = address,
                                        city = city,
                                        postalCode = postalCode,
                                        phone = phone,
                                        contact_person = contact,
                                        latitude = latitude,
                                        longitude = longitude,
                                        isprimary = true
                                    };

                                    if (stateid != 0) {
                                        location.stateID = stateid;
                                    }

                                    db.CustomerLocations.InsertOnSubmit(location);
                                }
                                db.SubmitChanges();

                            } else {
                                if (parentacct.Trim() != "") {
                                    customer.parentID = Convert.ToInt32(parentacct);
                                }
                                // prior customer
                                // check for location
                                customer.dealer_type = type.dealer_type;
                                customer.tier = tier.ID;
                                if (salesrep != null && salesrep.salesRepID != 0) {
                                    customer.salesRepID = salesrep.salesRepID;
                                }
                                CustomerLocation location = db.CustomerLocations.Where(x => x.address.ToLower() == address.ToLower() && x.postalCode.Trim() == postalCode.Trim() && x.cust_id.Equals(customer.cust_id)).FirstOrDefault<CustomerLocation>();
                                if (location == null) {
                                    CustomerLocation new_location = new CustomerLocation {
                                        cust_id = customer.cust_id,
                                        name = locationname,
                                        address = address,
                                        city = city,
                                        postalCode = postalCode,
                                        phone = phone,
                                        latitude = latitude,
                                        longitude = longitude,
                                        contact_person = contact
                                    };

                                    if (stateid != 0) {
                                        new_location.stateID = stateid;
                                    }

                                    db.CustomerLocations.InsertOnSubmit(new_location);
                                    db.SubmitChanges();
                                } else {
                                    // update location
                                    location.contact_person = contact;
                                    location.name = locationname;
                                    location.city = city;
                                    location.phone = phone;
                                    location.postalCode = postalCode;
                                    location.latitude = latitude;
                                    location.longitude = longitude;
                                    db.SubmitChanges();
                                }
                            }
                        } catch (Exception e) {
                            errors.Add(row + ": " + e.Message);
                        }
                        row++;
                    }

                }
                if (errors.Count == 0) {
                    return RedirectToAction("Index", new { msg = "Customer Import Successful" });
                } else {
                    return RedirectToAction("MassUpload", new { msg = "There were some errors", errors = errors });
                }
            } catch (Exception e) {
                return RedirectToAction("MassUpload", new { msg = e.Message });
            }
        }