From e77b488cd4d202595e38360de7bbf9300c98ef52 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 31 Jan 2025 18:44:11 -0500 Subject: [PATCH] Fix/add data to products script --- inventory-server/scripts/import/products.js | 76 ++++++++++++++++----- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/inventory-server/scripts/import/products.js b/inventory-server/scripts/import/products.js index ce43418..70fe5e2 100644 --- a/inventory-server/scripts/import/products.js +++ b/inventory-server/scripts/import/products.js @@ -2,13 +2,16 @@ const { outputProgress, formatElapsedTime, estimateRemaining, calculateRate } = // Utility functions const imageUrlBase = 'https://sbing.com/i/products/0000/'; -const getImageUrls = (pid) => { +const getImageUrls = (pid, iid = 1) => { const paddedPid = pid.toString().padStart(6, '0'); - const basePath = `${imageUrlBase}${paddedPid.slice(0, 3)}/${pid}`; + // Use padded PID only for the first 3 digits + const prefix = paddedPid.slice(0, 3); + // Use the actual pid for the rest of the URL + const basePath = `${imageUrlBase}${prefix}/${pid}`; return { - image: `${basePath}-t-`, - image_175: `${basePath}-175x175-`, - image_full: `${basePath}-o-` + image: `${basePath}-t-${iid}.jpg`, + image_175: `${basePath}-175x175-${iid}.jpg`, + image_full: `${basePath}-o-${iid}.jpg` }; }; @@ -118,12 +121,11 @@ async function materializeCalculations(prodConnection, localConnection) { p.pid, COALESCE(pcp.price_each, 0) as price, COALESCE(p.sellingprice, 0) AS regular_price, - COALESCE( - (SELECT ROUND(AVG(costeach), 5) - FROM product_inventory - WHERE pid = p.pid - AND COUNT > 0), 0 - ) AS cost_price + CASE + WHEN EXISTS (SELECT 1 FROM product_inventory WHERE pid = p.pid AND count > 0) + THEN (SELECT ROUND(AVG(costeach), 5) FROM product_inventory WHERE pid = p.pid AND count > 0) + ELSE (SELECT costeach FROM product_inventory WHERE pid = p.pid ORDER BY daterec DESC LIMIT 1) + END AS cost_price FROM products p LEFT JOIN product_current_prices pcp ON p.pid = pcp.pid WHERE pcp.active = 1 @@ -256,6 +258,13 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate line VARCHAR(100), subline VARCHAR(100), artist VARCHAR(100), + landing_cost_price DECIMAL(10,2) DEFAULT NULL, + permalink VARCHAR(255) DEFAULT NULL, + options TEXT DEFAULT NULL, + tags TEXT DEFAULT NULL, + uom VARCHAR(50) DEFAULT NULL, + baskets INT DEFAULT 0, + notifies INT DEFAULT 0, moq INT, rating TINYINT UNSIGNED, reviews INT UNSIGNED, @@ -286,7 +295,14 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate p.harmonized_tariff_code, p.stamp AS updated_at, CASE WHEN si.show + si.buyable > 0 THEN 1 ELSE 0 END AS visible, - CASE WHEN p.reorder >= 0 THEN 1 ELSE 0 END AS replenishable, + CASE + WHEN p.reorder < 0 THEN 0 + WHEN ( + ((p.datein = '0000-00-00 00:00:00' OR p.datein <= DATE_SUB(NOW(), INTERVAL 5 YEAR)) + AND (p.date_refill = '0000-00-00 00:00:00' OR p.date_refill <= DATE_SUB(NOW(), INTERVAL 5 YEAR))) + ) THEN 0 + ELSE 1 + END AS replenishable, s.companyname AS vendor, CASE WHEN s.companyname = 'Notions' THEN sid.notions_itemnumber @@ -297,6 +313,13 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate pc2.name AS line, pc3.name AS subline, pc4.name AS artist, + NULL AS landing_cost_price, + CONCAT('https://www.acherryontop.com/shop/product/', p.pid) AS permalink, + NULL AS options, + NULL AS tags, + NULL AS uom, + (SELECT COUNT(*) FROM mybasket mb WHERE mb.item = p.pid AND mb.qty > 0) AS baskets, + (SELECT COUNT(*) FROM product_notify pn WHERE pn.pid = p.pid) AS notifies, COALESCE(CASE WHEN sid.supplier_id = 92 THEN sid.notions_qty_per_unit ELSE sid.supplier_qty_per_unit @@ -310,7 +333,8 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate p.totalsold AS total_sold, p.country_of_origin, pls.date_sold as date_last_sold, - GROUP_CONCAT(DISTINCT pci.cat_id) as category_ids + GROUP_CONCAT(DISTINCT pci.cat_id) as category_ids, + true // needs_update FROM products p LEFT JOIN shop_inventory si ON p.pid = si.pid AND si.store = 0 LEFT JOIN supplier_item_data sid ON p.pid = sid.pid @@ -362,6 +386,13 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate row.line, row.subline, row.artist, + row.landing_cost_price, + row.permalink, + row.options, + row.tags, + row.uom, + row.baskets, + row.notifies, row.moq, row.rating, row.reviews, @@ -655,16 +686,25 @@ async function importMissingProducts(prodConnection, localConnection, missingPid COALESCE(pnb.inventory, 0) as notions_inv_count, COALESCE(pcp.price_each, 0) as price, COALESCE(p.sellingprice, 0) AS regular_price, - COALESCE((SELECT ROUND(AVG(costeach), 5) - FROM product_inventory - WHERE pid = p.pid - AND COUNT > 0), 0) AS cost_price, + CASE + WHEN EXISTS (SELECT 1 FROM product_inventory WHERE pid = p.pid AND count > 0) + THEN (SELECT ROUND(AVG(costeach), 5) FROM product_inventory WHERE pid = p.pid AND count > 0) + ELSE (SELECT costeach FROM product_inventory WHERE pid = p.pid ORDER BY daterec DESC LIMIT 1) + END AS cost_price, NULL AS landing_cost_price, p.upc AS barcode, p.harmonized_tariff_code, p.stamp AS updated_at, CASE WHEN si.show + si.buyable > 0 THEN 1 ELSE 0 END AS visible, - CASE WHEN p.reorder >= 0 THEN 1 ELSE 0 END AS replenishable, + CASE + WHEN p.reorder < 0 THEN 0 + WHEN ( + (IFNULL(pls.date_sold, '0000-00-00') = '0000-00-00' OR pls.date_sold <= DATE_SUB(CURDATE(), INTERVAL 5 YEAR)) + OR (p.datein = '0000-00-00 00:00:00' OR p.datein <= DATE_SUB(NOW(), INTERVAL 5 YEAR)) + OR (p.date_refill = '0000-00-00 00:00:00' OR p.date_refill <= DATE_SUB(NOW(), INTERVAL 5 YEAR)) + ) THEN 0 + ELSE 1 + END AS replenishable, s.companyname AS vendor, CASE WHEN s.companyname = 'Notions' THEN sid.notions_itemnumber ELSE sid.supplier_itemnumber END AS vendor_reference, sid.notions_itemnumber AS notions_reference,