public static CheckoutItem ToCheckoutItem(this product_variant p, int quantity, long?viewerid) { var unitprice = p.product.ToUserPrice(viewerid); var item = new CheckoutItem() { id = p.id, productUrl = p.product.ToProductUrl(), quantity = quantity, UnitPrice = unitprice.Value, UnitPriceWithTax = p.product.tax.HasValue ? (unitprice.Value * (p.product.tax.Value / 100 + 1)) : unitprice.Value, SKU = p.sku, thumbnailUrl = p.product.thumb.HasValue ? p.product.product_image.ToModel(Imgsize.THUMB).url : GeneralConstants.PHOTO_NO_THUMBNAIL, description = p.ToProductFullTitle() }; if (!string.IsNullOrEmpty(p.product.dimensions)) { var serializer = new JavaScriptSerializer(); item.dimension = serializer.Deserialize <Dimension>(p.product.dimensions); } return(item); }
public static Variant ToLiquidModel(this product_variant row, decimal?price) { var v = new Variant() { id = row.id, title = row.ToVariantTitle(), inventory_quantity = row.inventoryLocationItems.Sum(y => y.available) ?? 0, sku = row.sku, price = price, option1 = row.color, option2 = row.size }; // handle options if (!string.IsNullOrEmpty(v.option1)) { v.options.Add(v.option1); } if (!string.IsNullOrEmpty(v.option2)) { v.options.Add(v.option2); } v.available = row.HasStock(); return(v); }
public static string ToVariantTitle(this product_variant v) { var options = new List <string>(); if (!string.IsNullOrEmpty(v.color)) { options.Add(v.color); } if (!string.IsNullOrEmpty(v.size)) { options.Add(v.size); } return(string.Join(" / ", options)); }
public static string ToProductFullTitle(this product_variant v) { var sb = new StringBuilder(); sb.Append(v.product.title); if (!string.IsNullOrEmpty(v.color)) { sb.AppendFormat(" / {0}", v.color); } if (!string.IsNullOrEmpty(v.size)) { sb.AppendFormat(" / {0}", v.size); } return(sb.ToString()); }
private void Update() { if (string.IsNullOrEmpty(warehouse)) { Syslog.Write(string.Format("Shipwire warehouse not specified: {0}", subdomainid)); return; } service.CreateInventoryUpdate(warehouse); var resp = service.SubmitInventoryUpdate(); if (resp != null) { // check for Error if (resp.Status == ShipwireService.StatusError && resp.ErrorMessage.ToLower().Contains("password")) { // clear invalid credentials using (var repository = new TradelrRepository()) { var sd = repository.GetSubDomain(subdomainid); if (sd != null) { sd.shipwireEmail = ""; sd.shipwirePassword = ""; repository.Save(); } } } // we want to create a location only if there are products if (resp.Products.Count != 0) { Debug.WriteLine(string.Format("{0}: {1} products", warehouse, resp.Products.Count)); using (var repository = new TradelrRepository()) { // if items not zero then we create inventory location if it does not already exist var inventoryloc = new inventoryLocation { subdomain = subdomainid, name = warehouse, lastUpdate = DateTime.UtcNow }; var locid = repository.AddInventoryLocation(inventoryloc, subdomainid); // we go through each product foreach (var product in resp.Products) { var variant = repository.GetProductVariant(product.code, subdomainid, ProductFlag.ARCHIVED); // if product exist and not archived if (variant != null) { Debug.WriteLine("Existing product: " + product.code); // then we just update the inventorylocitem var ilocitem = repository.GetInventoryLocationItems(locid, subdomainid).SingleOrDefault(x => x.variantid == variant.id); if (ilocitem == null) { // can be null from do these updates from Shipwire as we're doing on a per location basis // anyway create an entry ilocitem = new inventoryLocationItem { variantid = variant.id, locationid = locid, lastUpdate = DateTime.UtcNow, available = product.quantity }; repository.AddInventoryLocationItem(ilocitem, subdomainid); } else { // just update as this is a sync ilocitem.available = product.quantity; ilocitem.lastUpdate = DateTime.UtcNow; repository.Save("InventoryUpdate Update"); } } else { Debug.WriteLine("New product: " + product.code); // create new product var productInfo = new ProductInfo(); // do product var p = new product { subdomainid = subdomainid, title = product.code, details = "", created = DateTime.UtcNow }; productInfo.p = p; variant = new product_variant(); variant.sku = product.code; // do inventory location item var ilocitem = new inventoryLocationItem { variantid = variant.id, locationid = locid, lastUpdate = DateTime.UtcNow }; var invWorker = new InventoryWorker(ilocitem, subdomainid, true, false); // assume not digital invWorker.SetValues("Shipwire Update", product.quantity, null, null, null); variant.inventoryLocationItems.Add(ilocitem); productInfo.p.product_variants.Add(variant); // finally add to db repository.AddProduct(productInfo, subdomainid); } } } } else { //Syslog.Write(ErrorLevel.WARNING,string.Format("{0}: No products", warehouse)); } } else { Syslog.Write("No response from warehouse " + warehouse); } }
public static string ToHtmlLink(this product_variant v, bool openInNewPage = true) { return(string.Format("<a href='{0}' target='{1}'>{2}</a>", v.product.ToProductUrl(), openInNewPage ? "_blank" : "_self", v.ToProductFullTitle())); }
public List <ProductInfo> Import(Stream inputStream, long owner, long subdomain) { var templateWorkbook = new HSSFWorkbook(inputStream, true); var sheet = templateWorkbook.GetSheet("Products"); int count = 0; var productsList = new List <ProductInfo>(); using (var repository = new TradelrRepository()) { while (true) { var row = sheet.GetRow(count++); if (row == null) { break; } var sku = GetCellValueAsString(row.GetCell(0, MissingCellPolicy.RETURN_NULL_AND_BLANK)); if (sku.StartsWith(";")) { continue; } if (string.IsNullOrEmpty(sku)) { break; } var title = GetCellValueAsString(row.GetCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var description = GetCellValueAsString(row.GetCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var maincategory = GetCellValueAsString(row.GetCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var subcategory = GetCellValueAsString(row.GetCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var stockunit = GetCellValueAsString(row.GetCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var costprice = GetCellValueAsDecimal(row.GetCell(6, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var sellingprice = GetCellValueAsDecimal(row.GetCell(7, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var instock = GetCellValueAsInt(row.GetCell(8, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var photos = GetCellValueAsString(row.GetCell(9, MissingCellPolicy.RETURN_NULL_AND_BLANK)); var product = new product { subdomainid = subdomain, details = description, title = title, costPrice = costprice, sellingPrice = sellingprice }; if (!string.IsNullOrEmpty(stockunit)) { var masterunit = repository.AddMasterStockUnit(stockunit); var su = new stockUnit { unitID = masterunit.id, subdomainid = subdomain }; product.stockUnitId = repository.AddStockUnit(su); } product.otherNotes = ""; var inventoryloc = repository.GetInventoryLocation(GeneralConstants.INVENTORY_LOCATION_DEFAULT, subdomain); // create inventoryLocItem var inventoryLocItem = new inventoryLocationItem { locationid = inventoryloc.id, lastUpdate = DateTime.UtcNow }; var invWorker = new InventoryWorker(inventoryLocItem, subdomain, true, false); invWorker.SetValues("product created", instock, null, null, null); MASTERproductCategory mastercat; long?catid = null; if (!string.IsNullOrEmpty(maincategory)) { mastercat = repository.AddMasterProductCategory(maincategory); var cat = new productCategory() { masterID = mastercat.id, subdomain = subdomain }; catid = repository.AddProductCategory(cat, subdomain); } // add sub category if (!string.IsNullOrEmpty(subcategory) && !string.IsNullOrEmpty(maincategory)) { mastercat = repository.AddMasterProductCategory(subcategory); var subcat = new productCategory() { masterID = mastercat.id, subdomain = subdomain, parentID = catid }; catid = repository.AddProductCategory(subcat, subdomain); } product.category = catid; product.created = DateTime.UtcNow; product.updated = product.created; if (!productsList.Where(x => x.p.product_variants.Count(y => y.sku == sku) != 0).Any()) { var pi = new ProductInfo() { p = product }; if (!string.IsNullOrEmpty(photos)) { var photourls = photos.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (var photourl in photourls) { pi.AddPhotoUrl(photourl); } } var variant = new product_variant { sku = sku }; variant.inventoryLocationItems.Add(inventoryLocItem); pi.p.product_variants.Add(variant); productsList.Add(pi); } } } return(productsList); }
public ProductInfo ImportEbay(Listing listing, long subdomainid) { var p = new product { subdomainid = subdomainid, details = listing.description, title = listing.title, ebay_product = new ebay_product() { ebayid = listing.id, listingType = listing.listingType.ToString(), siteid = listing.siteid.ToString(), categoryid = listing.categoryid1, categoryid_secondary = listing.categoryid2, condition = listing.condition, returnPolicy = listing.returnPolicy, returnWithin = listing.returnWithin, refundPolicy = listing.refundPolicy, duration = listing.duration, startTime = listing.startTime, endTime = listing.endTime, quantity = listing.quantity, dispatchTime = listing.dispatchTime, isActive = listing.isActive, startPrice = Convert.ToDecimal(listing.startPrice), buynowPrice = listing.buynowPrice.HasValue? Convert.ToDecimal(listing.buynowPrice.Value):(decimal?)null, reservePrice = listing.reservePrice.HasValue? Convert.ToDecimal(listing.reservePrice.Value):(decimal?)null }, created = DateTime.UtcNow, updated = DateTime.UtcNow, sellingPrice = Convert.ToDecimal(listing.productPrice) }; // and create inventory location if not exist long locationid; using (var repository = new TradelrRepository()) { locationid = repository.GetInventoryLocation(GeneralConstants.INVENTORY_LOCATION_DEFAULT, subdomainid).id; } var inventoryLocItem = new inventoryLocationItem { locationid = locationid, lastUpdate = DateTime.UtcNow }; // create our add object var pinfo = new ProductInfo { p = p }; foreach (var photoUrl in listing.photoUrls) { pinfo.AddPhotoUrl(photoUrl); } // create variant foreach (var ebayvariant in listing.variants) { var variant = new product_variant { sku = string.IsNullOrEmpty(ebayvariant.sku) ? listing.id : ebayvariant.sku }; foreach (var name in ebayvariant.properties.AllKeys) { switch (name) { case "colour": case "color": variant.color = ebayvariant.properties[name]; break; case "size": variant.size = ebayvariant.properties[name]; break; default: Syslog.Write("Unknown eBay variant property {0}:{1}", name, ebayvariant.properties[name]); break; } } variant.inventoryLocationItems.Add(inventoryLocItem); pinfo.p.product_variants.Add(variant); } return(pinfo); }
public ProductInfo ImportFacebook(FBImportProduct info, string access_token, long subdomainid) { // check for duplicate sku var p = new product { subdomainid = subdomainid, details = info.description, title = info.title, }; p.facebook_imports.Add(new facebook_import() { facebookID = info.id }); if (!string.IsNullOrEmpty(info.sellingprice)) { decimal price; if (decimal.TryParse(info.sellingprice, out price)) { p.sellingPrice = price; } } p.updated = DateTime.UtcNow; p.created = DateTime.UtcNow; // handle photos var photourls = info.photoids.Select(x => x.ToFacebookPhotoUrl(access_token)).Take(MaxPhotoImport); // create inventoryLocItem long locationid; using (var repository = new TradelrRepository()) { locationid = repository.GetInventoryLocation(GeneralConstants.INVENTORY_LOCATION_DEFAULT, subdomainid).id; } var inventoryLocItem = new inventoryLocationItem { locationid = locationid, lastUpdate = DateTime.UtcNow }; // create variant var variant = new product_variant { sku = string.IsNullOrEmpty(info.sku) ? info.id.ToString(): info.sku }; variant.inventoryLocationItems.Add(inventoryLocItem); // add photourls // create our add object var pinfo = new ProductInfo { p = p }; foreach (var photourl in photourls) { pinfo.photo_urls.Add(photourl); } pinfo.p.product_variants.Add(variant); return(pinfo); }
public override void StartSynchronisation(bool?upload) { // pull items var gbase = new GoogleBaseExporter(subdomainid, hostName, sessionid); gbase.GetAllProducts(); using (var repository = new TradelrRepository()) { // create network location var inventoryLocation = new inventoryLocation { name = LOCATIONNAME_GBASE, subdomain = subdomainid, lastUpdate = DateTime.UtcNow }; locationid = repository.AddInventoryLocation(inventoryLocation, subdomainid); // init some settings locationid = repository.GetInventoryLocation(LOCATIONNAME_GBASE, subdomainid).id; currency = repository.GetSubDomain(subdomainid).currency.ToCurrency(); var products = repository.GetProducts(subdomainid); foreach (var entry in gbase.entries) { ProductEntry entry1 = entry; var oldTypeEntry = products.SingleOrDefault(x => x.gbaseID == entry1.Id.AbsoluteUri); if (oldTypeEntry != null) { // old type entry exists // remove id oldTypeEntry.gbaseID = null; // create gbase entry, don't need to create a variant since one would already exist oldTypeEntry.gbase_product = CreateGbaseInformationEntry(entry); } else { var newtypeEntry = products.Where(x => x.gbase_product.externalid == entry1.Id.AbsoluteUri).Select( x => x.gbase_product).SingleOrDefault(); if (newtypeEntry != null) { // new type entry exists // update status newtypeEntry.expirydate = entry.ExpirationDate; if (entry.IsDraft) { newtypeEntry.flags |= (int)InventoryItemFlag.DRAFT; } else { newtypeEntry.flags &= ~(int)InventoryItemFlag.DRAFT; } } else { // entry does not exist // create and add entry to tradelr var p = CreateProductFromEntry(entry); // create variant var variants = new List <product_variant>(); var variant = new product_variant() { sku = string.Concat("GBASE", entry.Id.AbsoluteUri.Substring( entry.Id.AbsoluteUri.LastIndexOf('/') + 1)) }; variants.Add(variant); // add gbase info p.gbase_product = CreateGbaseInformationEntry(entry); p.product_variants.AddRange(variants); var pinfo = new ProductInfo() { p = p }; repository.AddProduct(pinfo, subdomainid); // images foreach (var link in entry.AdditionalImageLinks) { var image = link.Value.ReadAndSaveProductImageFromUrl(subdomainid, sessionid, p.id); p.thumb = image.id; } } } } // upload to google base for each entry that does not have a special entry if (upload.HasValue && upload.Value) { var newproducts = repository.GetProducts(subdomainid).Where( x => !x.gbase.HasValue && (x.flags & (int)ProductFlag.ARCHIVED) == 0); foreach (var p in newproducts) { var gb = new GoogleBaseExporter(subdomainid, hostName, sessionid); gb.InitValues(p); #if !DEBUG IEnumerable <Photo> productPhotos = repository.GetImages(PhotoType.PRODUCT, p.id).ToModel(Imgsize.LARGE); gb.AddProductImages(productPhotos); #endif var worker = new GoogleBaseWorker(gb); new Thread(worker.Post).Start(); } } // save repository.Save(); } }