initial commit

This commit is contained in:
Paul Trowbridge 2023-09-13 17:06:57 -04:00
commit dd7cb95502
38 changed files with 8152 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

1194
PriceLists.bas Normal file

File diff suppressed because it is too large Load Diff

3090
TheBigOne.cls Normal file

File diff suppressed because it is too large Load Diff

171
build_f20.pg.sql Normal file
View File

@ -0,0 +1,171 @@
--DROP TABLE IF EXISTS rlarp.plbuildr1;
--
--CREATE TABLE Rlarp.plbuildr1 (
-- stlc text
-- ,coltier text
-- ,branding text
-- ,kit text
-- ,suffix text
-- ,item text
-- ,clss text
-- ,vb_m numeric
-- ,price text
-- ,uomp text
-- ,orig_row text
-- ,orig_col text
-- ,error text
-- ,usern text
--);
--
--GRANT ALL ON rlarp.plbuildr1 TO public;
--
--
DROP FUNCTION IF EXISTS rlarp.build_f20;
CREATE FUNCTION rlarp.build_f20(_items jsonb)
RETURNS TABLE (
stlc text
,coltier text
,branding text
,accs text
--,suffix text
,item text
,uomp text
,clss text
,vb integer
,vb_uom text
,vb_m numeric
,vbm_uom text
,price numeric
,orig_row numeric
,orig_col numeric
,error text
,usern text
)
LANGUAGE plpgsql AS $function$
BEGIN
DROP TABLE IF EXISTS f20;
CREATE TEMPORARY TABLE f20 AS (
WITH
---------get to a target pack unit based on the column; bulk should always be PLT-----------------------------------
vu AS (
SELECT * FROM ( VALUES
('CSE','1','CSE'),
('CSE','2','CSE'),
('CSE','3','PLT'),
('BDL','1','BDL'),
('BDL','2','BDL'),
('BDL','3','PLT'),
('BDL','1','SLV'),
('BDL','2','SLV')
--('BDL','3','PLT')
) x(cont,vol,uom)
)
SELECT
b.stlc
,b.coltier
,b.branding
,b.accs
--,b.suffix
,b.container
,b.volume
,i.item
,i.clss
,i.uomp->>0 uomp
,1 vb
,CASE b.volume
WHEN '1' THEN i.uomp->>0
WHEN '2' THEN 'PLT'
WHEN '3' THEN 'PLT'
END vb_uom
,b.price
,b.orig_row
,b.orig_col
,i.aplnt
FROM
jsonb_to_recordset(_items) AS b(
stlc text
,coltier text
,branding text
,accs text
--,suffix text
,container text
,volume text
,price numeric
,orig_row numeric
,orig_col numeric )
INNER JOIN vu ON
vu.cont = b.container
AND vu.vol = b.volume --curently the volume level is an integer that reflect which column the price is in
LEFT OUTER JOIN rlarp.itemmv i ON
i.stlc = b.stlc
AND i.coltier = b.coltier
AND COALESCE(b.branding,'') = COALESCE(i.branding,'')
---------flipping i.accs_ps/i.accs will change accessory linkage method----------
AND COALESCE(b.accs,'') = COALESCE(i.accs_ps,i.accs,'')
--AND COALESCE(b.suffix,'') = COALESCE(i.suffix,'')
--AND i.uomp->>0 = vu.uom
--revise to hook if any of the default units matches the target
AND i.uomp ? vu.uom
) WITH DATA;
--select * from f20;
RETURN QUERY
WITH
uom AS (
SELECT
uom.*
FROM
--aggregate converion targets to array
(
SELECT
jsonb_agg(row_to_json(d)::jsonb) jdoc
FROM
--inline table made up of conversion targets
(
SELECT DISTINCT
f20.item partn, 'PC' tu, f20.vb_uom fu
FROM
f20
) d
) c
JOIN LATERAL rlarp.uom_array(c.jdoc) uom ON TRUE
)
SELECT
f.stlc
,f.coltier
,f.branding
,f.accs
--,f.suffix
,f.item
,f.uomp
,f.clss
,f.vb
,f.vb_uom
,round(u.nm/u.dm,0)/1000 vb_m
,'M' vbm_uom
,f.price
,f.orig_row
,f.orig_col
,COALESCE(
CASE WHEN f.price IS NOT NULL AND f.item IS NULL THEN 'No SKU' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.aplnt = 'I' THEN 'Inactive' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.item IS NOT NULL AND u.p IS NULL THEN 'No UOM Convesion' ELSE NULL::text END
) errorm
,current_user::text
FROM
f20 f
LEFT OUTER JOIN uom u ON
u.p = f.item
AND u.f = f.vb_uom
WHERE
true
--AND f.aplnt <> 'I'
ORDER BY
orig_row
,orig_col
,errorm DESC
,item ASC;
END
$function$

178
build_f20_suff.pg.sql Normal file
View File

@ -0,0 +1,178 @@
--DROP TABLE IF EXISTS rlarp.plbuildr1;
--
--CREATE TABLE Rlarp.plbuildr1 (
-- stlc text
-- ,coltier text
-- ,branding text
-- ,kit text
-- ,suffix text
-- ,item text
-- ,clss text
-- ,vb_m numeric
-- ,price text
-- ,uomp text
-- ,orig_row text
-- ,orig_col text
-- ,error text
-- ,usern text
--);
--
--GRANT ALL ON rlarp.plbuildr1 TO public;
--
--
DROP FUNCTION IF EXISTS rlarp.build_f20_suff;
CREATE FUNCTION rlarp.build_f20_suff(_items jsonb)
RETURNS TABLE (
stlc text
,coltier text
,branding text
,accs text
,suffix text
,item text
,uomp text
,clss text
,vb integer
,vb_uom text
,vb_m numeric
,vbm_uom text
,price numeric
,orig_row numeric
,orig_col numeric
,error text
,usern text
)
LANGUAGE plpgsql AS $function$
BEGIN
DROP TABLE IF EXISTS f20;
CREATE TEMPORARY TABLE f20 AS (
WITH
---------get to a target pack unit based on the column; bulk should always be PLT-----------------------------------
vu AS (
SELECT * FROM ( VALUES
('CSE','1','CSE'),
('CSE','2','CSE'),
('PC','1','PC'),
('PC','2','PC'),
('CSE','3','PLT'),
('BDL','1','BDL'),
('BDL','2','BDL'),
('BDL','3','PLT'),
('BDL','1','SLV'),
('BDL','2','SLV')
--('BDL','3','PLT')
) x(cont,vol,uom)
)
SELECT
b.stlc
,b.coltier
,b.branding
,b.accs
,b.suffix
,b.container
,b.volume
,i.item
,i.clss
,i.uomp
,1 vb
,CASE b.volume
WHEN '1' THEN i.uomp
WHEN '2' THEN 'PLT'
WHEN '3' THEN 'PLT'
END vb_uom
,b.price
,b.orig_row
,b.orig_col
,i.aplnt
FROM
jsonb_to_recordset(_items) AS b (
stlc text
,coltier text
,branding text
,accs text
,suffix text
,container text
,volume text
,price numeric
,orig_row numeric
,orig_col numeric
)
INNER JOIN vu ON
vu.cont = b.container
AND vu.vol = b.volume --curently the volume level is an integer that reflect which column the price is in
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = b.stlc
AND i.coltier = b.coltier
AND COALESCE(b.branding,'') = COALESCE(i.branding,'')
---------flipping i.accs_ps/i.accs will change accessory linkage method----------
AND COALESCE(b.accs,'') = COALESCE(i.accs_ps,i.accs,'')
AND COALESCE(b.suffix,'') = COALESCE(i.suffix,'')
--AND i.uomp->>0 = vu.uom
--i.uomp is an array of defalt units of measure from STKA, if we are trying to get from
--a style code to a full20 part, we have to consider that column 3 pricing is only intended to hook up
--with PLT denominated parts, and columns 1 and 2 are supposed to hook with with the listed
--package unit. this achieved with the `vu` CTE above. however if a part has multiple default units
--(STKA.V6CNTR) then it will join to the case and the pallet potentially.
AND i.uomp = vu.uom
) WITH DATA;
--select * from f20;
RETURN QUERY
WITH
uom AS (
SELECT
uom.*
FROM
--aggregate converion targets to array
(
SELECT
jsonb_agg(row_to_json(d)::jsonb) jdoc
FROM
--inline table made up of conversion targets
(
SELECT DISTINCT
f20.item partn, 'PC' tu, f20.vb_uom fu
FROM
f20
) d
) c
JOIN LATERAL rlarp.uom_array(c.jdoc) uom ON TRUE
)
SELECT
f.stlc
,f.coltier
,f.branding
,f.accs
,f.suffix
,f.item
,f.uomp
,f.clss
,f.vb
,f.vb_uom
,round(u.nm/u.dm,0)/1000 vb_m
,'M' vbm_uom
,f.price
,f.orig_row
,f.orig_col
,COALESCE(
CASE WHEN f.price IS NOT NULL AND f.item IS NULL THEN 'No SKU' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.aplnt = 'I' THEN 'Inactive' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.item IS NOT NULL AND u.p IS NULL THEN 'No UOM Convesion' ELSE NULL::text END
) errorm
,current_user::text
FROM
f20 f
LEFT OUTER JOIN uom u ON
u.p = f.item
AND u.f = f.vb_uom
WHERE
true
--AND f.aplnt <> 'I'
ORDER BY
orig_row
,orig_col
,errorm DESC
,item ASC;
END
$function$

14
checklist.md Normal file
View File

@ -0,0 +1,14 @@
## Price List Roll-Out
- [x] Update core listing table
- [x] use upload function to push core pricing to rlarp.pcore
- [x] build 2 copies of CMS upload files (one for staging, one for live)
- [x] deposit both sets to price_list git repo
- [x] update git repo for changes
- [x] move files to QDLS
- [X] import staging copy to CMS to build full code listing
- [ ] refreshing full code listing file to reflect staged prices
- [x] refresh master list for pcore contents
- [ ] create all necessary combinations of price list files into price level folders
- [ ] get files to Mark Hembre for use in distributor portal
- [ ] after all orders have been entered, import live price lists in CMS

28
csv_build.cmd Normal file
View File

@ -0,0 +1,28 @@
::---------------------------------------Greenhouse--------------------------------------------------------------------------------------------------------------------------------------------------------------------
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GUAU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GUAU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GUBU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GUBU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GCAU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GCAU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GCBU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GCBU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GCEU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GCEU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'GCWU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\GCWU.csv"
::---------------------------------------Fiber-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FCAC'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FCAC.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FCBC'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FCBC.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FCCC'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FCCC.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FUAU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FUAU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FUBU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FUBU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'FUCU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\FUCU.csv"
::---------------------------------------Nursery-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUEU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUEU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUGU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUGU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUFU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUFU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUSU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUSU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUWU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUWU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUOU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUOU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUYU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUYU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NUDU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NUDU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NCEU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NCEU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'NCWU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\NCWU.csv"
::---------------------------------------Protective Packaging----------------------------------------------------------------------------------------------------------------------------------------------------------
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'PUBU'" -t --csv -o"C:\Users\PTrowbridge\price_lists\PUBU.csv"
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U ptrowbridge -d ubm -p 5432 -h usmidsap01 -c "SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12 FROM rlarp.plcore_upload WHERE listcode = 'PCBC'" -t --csv -o"C:\Users\PTrowbridge\price_lists\PCBC.csv"

View File

@ -0,0 +1,25 @@
SELECT
s.bk7code plevel
,bk7des1
,bk7des2
,count(DISTINCT trim(CASE WHEN dba = '' THEN descr ELSE dba END)) FILTER (WHERE code IS NOT NULL) cnt
,string_agg(DISTINCT i.JBPLCD ,', ') listcodes
,jsonb_agg(DISTINCT trim(CASE WHEN dba = '' THEN descr ELSE dba END)) custs
FROM
lgdat.sach s
LEFT OUTER JOIN rlarp.cust c ON
s.BK7CODE = c.plevel
AND c.status = 'A'
LEFT OUTER JOIN "CMS.CUSLG".IPRCBHC i ON
i.JBPLVL = c.PLEVEL
WHERE
s.bk7code <> ''
GROUP BY
s.bk7code
,bk7des1
,bk7des2
HAVING
count(DISTINCT trim(CASE WHEN dba = '' THEN descr ELSE dba END)) FILTER (WHERE code IS NOT NULL) >= 0
ORDER BY
cnt DESC
, plevel ASC

60
distributor_build.py Normal file
View File

@ -0,0 +1,60 @@
import psycopg2
import os
import shutil
from dotenv import load_dotenv
load_dotenv()
db_host = os.getenv('DB_HOST')
db_name = os.getenv('DB_NAME')
db_user = os.getenv('DB_USER')
db_password = os.getenv('DB_PASSWORD')
openai_api_key = os.getenv('OPENAI_API_KEY')
# Set up the database connection
conn = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password,
connect_timeout=120
)
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
# Execute SQL query to retrieve table data
cursor.execute("SELECT dba, plevel FROM rlarp.cust WHERE status = 'A' AND COALESCE(plevel,'') <> '' AND plevel !~ 'XXX'")
# cursor.execute("SELECT dba, plevel FROM rlarp.cust WHERE status = 'A' AND plevel IN ('C.EEX','C.XEX','U.AGB.DI','U.AGB.GM','U.BGC.DI')")
# Fetch all rows from the result set
rows = cursor.fetchall()
# Iterate through each row
for row in rows:
customer_name = row[0]
price_level = row[1]
print(customer_name)
print(price_level)
# Create a directory with the customer name
customer_directory = '/mnt/c/Users/ptrowbridge/Downloads/2023-08-16/'
#os.makedirs(customer_directory, exist_ok=True)
# Copy contents from the pre-existing folder based on price level
source_directory = '/mnt/c/Users/ptrowbridge/Downloads/2023-08-16/Levels/' + price_level
if os.path.exists(source_directory):
for file in os.listdir(source_directory):
# shutil.copy2(os.path.join(source_directory, file), customer_directory)
source_file = os.path.join(source_directory, file)
print("source file: " + source_file)
destination_file = os.path.join(customer_directory, customer_name + " - " + file)
print("destination file : " + destination_file)
shutil.copy(source_file, destination_file)
else:
print("Source directory does not exist.")
# Close the cursor and connection
cursor.close()
conn.close()

13
distributor_levels.pg.sql Normal file
View File

@ -0,0 +1,13 @@
SELECT
dba
,folder
,jsonb_agg(DISTINCT plevel) plevs
FROM
rlarp.cust
WHERE
status = 'A'
AND COALESCE(plevel,'') <> ''
AND plevel !~ 'XXX'
GROUP BY
dba
,folder

View File

@ -0,0 +1,116 @@
WITH
----------sequence the volume breaks and assume that the second one is the master pallet volume-------------------
--notes: this is approach is dubious but requested ASAP by customer service as required by customer---------------
---------pre-aggregate the minimum order quantity for active plants-----------------------------------------------
active_mins AS (
SELECT
AVPART part
,MAX(cast(round(v6moqt,0) as int)) mino
FROM
LGDAT.STKMM
LEFT OUTER JOIN lgdat.stka a ON
a.v6part = avpart
AND a.v6plnt IN ('112','113','152','154','155')
AND a.v6stat = 'A'
GROUP BY
AVPART
---------------------
UNION
---------------------
SELECT
AWPART part
,MAX(cast(round(v6moqt,0) as int)) mino
FROM
LGDAT.STKMP
LEFT OUTER JOIN lgdat.stka a ON
a.v6part = awpart
AND a.v6plnt IN ('112','113','152','154','155')
AND a.v6stat = 'A'
GROUP BY
AWPART
)
,raw AS (
SELECT
substring(japlds, 1, 1)||substring(japlds, 23, 1) pricelist
,a.japlcd
,c.JCPART
,i.DESCR
,i.DESCR2
,c.JCUNIT
,c.JCPRIC
,c.JCVOLL
,i.UOMP
,am.MINO
,i.CLSS
,RTRIM(i.ACC_LIST) ACC_LIST
,row_number() OVER (PARTITION BY japlcd, jcpart ORDER BY jcvoll ASC) SEQ
FROM
lgdat.iprcc c
INNER JOIN lgdat.iprca a ON
a.JAPLCD = c.JCPLCD
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = c.jcpart
LEFT OUTER JOIN active_mins am ON
am.part = c.jcpart
WHERE
a.japlcd LIKE ('X%')
--AND jcpart = 'FCT10400RBRD010'
)
----------start with only all sequence 1 items and then rejoin to get the 2nd break as a new column----------------
,pivot AS (
SELECT
w.pricelist
,w.japlcd
,w.JCPART
,w.DESCR
,w.DESCR2
,w.ACC_LIST
,w.JCUNIT
,w.JCPRIC
,w.seq
,w.uomp
,w.MINO
,w.CLSS
,COALESCE(c.jcvoll/w.jcvoll,1) cse_per_plt
,w.JCPRIC cse_price_m
,c.JCPRIC plt_price_m
,round(w.JCPRIC*w.jcvoll,2) cse_price_pkg
,round(c.JCPRIC*c.jcvoll,2) plt_price_pkg
,round(CASE WHEN w.uomp = 'PLT' THEN w.JCPRIC*w.JCVOLL ElSE CAST(null as numeric) END,2) plt_price_plt
,round(CASE WHEN w.uomp <> 'PLT' THEN cast(c.JCPRIC*C.JCVOLL as numeric)/cast(c.jcvoll/w.jcvoll as numeric) ElSE CAST(null as numeric) END,2) plt_price_cse
FROM
raw w
LEFT OUTER JOIN raw c ON
c.JCPART = w.JCPART
AND c.seq = 2
AND c.japlcd = w.japlcd
WHERE
w.SEQ = 1
ORDER BY
w.jcpart
,w.seq
)
------------pretty column names-----------------------------------------
SELECT
pricelist "Price List"
,rtrim(JCPART) "Product"
,DESCR "Description1"
,DESCR2 "Description2"
,ACC_LIST "Included Accessories"
,JCUNIT "UOM"
,JCPRIC "Price"
,cse_per_plt "Cases Per Pallet"
,CSE_PRICE_M "Single Case/Bulk Price Per M"
,PLT_PRICE_M "Master Pallet Price Per M"
,CSE_PRICE_PKG "Case/Bulk Price Per Package"
,PLT_PRICE_PKG "Master Pallet Price Per Pallet"
,PLT_PRICE_PLT "Bulk Pallet Price per Pallet"
,PLT_PRICE_CSE "Master Pallet Price Per Case"
,mino "Non-Stocking Minimum"
,clss "Inventory Class"
,japlcd "delete this column"
FROM
pivot
ORDER BY
PRICELIST
,JCPART

77
item_inq.pg.sql Normal file
View File

@ -0,0 +1,77 @@
WITH
--------list of list codes-----------
nrl AS (
SELECT DISTINCT
vers->>'NRList' nrlist
FROM
rlarp.ffterr f
WHERE
vers->>'NRList' IS NOT NULL
UNION ALL
SELECT DISTINCT
vers->>'GAList' nrlist
FROM
rlarp.ffterr f
WHERE
vers->>'GAList' IS NOT NULL
)
---------full code listing for all list codes------------------
,fc AS (
SELECT
item
,listcode
,vb_m*1000 vb
,price/1000 price
,row_number() OVER (PARTITION BY item, listcode ORDER BY price ASC) rn
FROM
rlarp.plcore_build_fullcode pbf
WHERE
pbf.listcode IN (SELECT nrlist FROM nrl)
AND item IS NOT NULL
AND errorm IS NULL
)
---------link to matrix---------------------------------------
SELECT
--o.bill_dba
o.bill_cust
--,o.ship_dba
,o.ship_cust
,o.part
,o.qtyord
,o.segm
,f.vers->>CASE o.segm
WHEN 'Nursery' THEN 'NRList'
WHEN 'Greenhouse' THEN 'GAList'
ELSE ''
END rlist
--,fc.vb
,min(fc.price) price
FROM
rlarp.osm_stack o
LEFT OUTER JOIN rlarp.ffterr f ON
f.ctry = o.dest_ctry
AND f.prov = o.dest_prov
LEFT OUTER JOIN fc ON
fc.item = o.part
AND fc.listcode = f.vers->>CASE o.segm
WHEN 'Nursery' THEN 'NRList'
WHEN 'Greenhouse' THEN 'GAList'
ELSE ''
END
AND o.qtyord >= fc.vb
WHERE
--item ~ 'AZA06000G18C'
ordnum = ' 899872 5'
GROUP BY
--o.bill_dba
o.bill_cust
--,o.ship_dba
,o.ship_cust
,o.part
,o.qtyord
,o.segm
,f.vers->>CASE o.segm
WHEN 'Nursery' THEN 'NRList'
WHEN 'Greenhouse' THEN 'GAList'
ELSE ''
END

41
list_audit.db2.sql Normal file
View File

@ -0,0 +1,41 @@
WITH
----------------------------------stack the new and old prices, then sort by stamp----------------------------------------------------------------
stack AS (
---new price log
SELECT 'new' flag, t8part part, t8unit unit, t8plcd plcd, t8date cdate, t8time ctime, t8voll voll, t8pric pric FROM lgdat.iprcctn
UNION ALL
---old price log
SELECT 'old' flag, t7part part, t7unit unit, t7plcd plcd, t7date cdate, t7time ctime, t7voll voll, t7pric pric FROM lgdat.iprccto
)
--------------------------------link in iprctt to get user and program----------------------------------------------------------------------------
,join_user AS (
SELECT
s.*
,t.TAUSER
,t.TAPROG
FROM
stack s
LEFT OUTER JOIN lgdat.iprcct t ON
t.taplcd = s.plcd
AND t.tapart = s.part
AND t.TAUNIT = s.unit
AND t.tadate = s.cdate
AND t.tatime = s.ctime
)
----------------------------------seek a single part number----------------------------------------------------------------------------------------
SELECT
*
FROM
join_user
WHERE
part = 'AZM09000E24C054'
and PLCD = 'GUAU'
AND flag = 'new'
ORDER BY
part
,unit
,plcd
,voll
,cdate ASC
,ctime ASC
,flag desc

90
list_impact.pg.sql Normal file
View File

@ -0,0 +1,90 @@
WITH
----------extract full code listing-------------------------------------------
pbf As (
SELECT
pbf.*
,vb_m * 1000 volume
FROM
rlarp.plcore_build_fullcode pbf
WHERE
errorm IS NULL
AND vb_m IS NOT NULL
AND price IS NOT NULL
)
----------create per part prices for each price level------------------------
,lpbf AS MATERIALIZED (
SELECT
i.jbplvl plevel
,pbf.item
,pbf.listcode
,pbf.volume
,pbf.price / CASE WHEN vbm_uom = 'M' then 1000 ELSE 1 END::numeric price
FROM
"CMS.CUSLG".iprcbhc i
INNER JOIN pbf ON
pbf.listcode = i.jbplcd
WHERE
current_date BETWEEN i.jbfdat AND i.jbtdat
)
--SELECT * FROM lpbf
----------join to price list-----------------------
,plj AS (
SELECT
o.*
,g.sspr || ' - ' || to_char(o.odate,'Mon') omonth
,c.plevel
,lpbf.listcode
,lpbf.volume
,lpbf.price
,row_number() OVER (PARTITION BY "ddord#",
"dditm#",
"fgbol#",
"fgent#",
"diinv#",
"dilin#",
"quoten",
"quotel"
ORDER BY lpbf.price ASC
) rn
FROM
rlarp.osm o
INNER JOIN rlarp.cust c ON
c.code = o.bill_cust
LEFT OUTER JOIN rlarp.gld g ON
o.odate <@ g.drange
LEFT OUTER JOIN lpbf ON
lpbf.plevel = c.plevel
AND lpbf.item = o.part
AND o.ddqtoi::numeric >= lpbf.volume
WHERE
calc_status <> 'CANCELED'
AND substring(glec,1,1) <= '2'
AND fs_line = '41010'
AND version = 'ACTUALS'
AND chan = 'WHS'
AND oseas = 2022
--AND o.odate >= '2022-02-01'
)
SELECT
p.account
,p.shipgrp
,p.oseas
,p.omonth
,p.promo
,p.part
,p.listcode
,ROUND(SUM(p.fb_qty) ,2) qty
,ROUND(SUM(p.fb_val_loc * p.r_rate) ,2) orig_usd
,ROUND(SUM(COALESCE(p.fb_qty * p.price * p.r_rate * CASE WHEN p.promo ~ 'EOP|EARLY' THEN 0.95 ELSE 1.00 END,p.fb_val_loc * p.r_rate)),2) new_usd
FROM
plj p
WHERE
rn = 1
GROUP BY
p.account
,p.shipgrp
,p.oseas
,p.omonth
,p.promo
,p.part
,p.listcode

102
list_revisions.db2.sql Normal file
View File

@ -0,0 +1,102 @@
CREATE OR REPLACE VIEW rlarp.list_revisions AS
WITH
----------------------------------stack the new and old prices, then sort by stamp----------------------------------------------------------------
stack AS (
---new price log
SELECT 'new' flag, t8part part, t8unit unit, t8plcd plcd, t8date cdate, t8time ctime, t8voll voll, t8pric pric FROM lgdat.iprcctn
UNION ALL
---old price log
SELECT 'old' flag, t7part part, t7unit unit, t7plcd plcd, t7date cdate, t7time ctime, t7voll voll, t7pric pric FROM lgdat.iprccto
)
--------------------------------link in iprctt to get user and program----------------------------------------------------------------------------
,join_user AS (
SELECT
s.*
,t.TAUSER
,t.TAPROG
FROM
stack s
LEFT OUTER JOIN lgdat.iprcct t ON
t.taplcd = s.plcd
AND t.tapart = s.part
AND t.TAUNIT = s.unit
AND t.tadate = s.cdate
AND t.tatime = s.ctime
)
----------------------------------seek a single part number----------------------------------------------------------------------------------------
,newload AS (
SELECT
*
FROM
join_user
WHERE
1 = 1
--AND part = 'TFT225G0G18B020'
--and PLCD >= 'NUE'
AND plcd IN ('GUAU','GUBU','GCAU','GCBU','GCEU','GCWU','NUEU','NUFU','NUGU','NUWU','NUYU','NCEU','NCWU','FUAU','FUBU','FUCU','FCAC','FCBC','FCCC')
AND flag = 'new'
ORDER BY
part
,unit
,plcd
,voll
,cdate ASC
,ctime ASC
,flag desc
)
--SELECT * FROM newload
,unqdate AS (
SELECT
part
,unit
,plcd
,voll
,pric
,cdate
,ctime
,ROW_NUMBER() OVER (PARTITION BY PART, unit, plcd, voll ORDER BY cdate, pric) rn
FROM
newload
)
,changes AS (
SELECT
u.PART
,u.unit
,u.plcd
,u.voll
,u.pric new_price
,u.cdate new_date
,u.ctime new_time
,prev.pric prev_price
,CASE WHEN u.voll = max(u.voll) OVER (PARTITION BY u.PART, u.unit, u.plcd, u.cdate) THEN 'X' ELSE '' END bestprice
FROM
unqdate u
LEFT OUTER JOIN unqdate prev ON
prev.PART = u.PART
AND prev.unit = u.unit
AND prev.plcd = u.plcd
AND prev.voll = u.voll
AND prev.rn = u.rn - 1
WHERE
1=1
AND u.pric <> COALESCE(prev.pric,0)
)
SELECT
u.PART
,u.unit
,u.plcd
,u.voll
,u.new_price
,u.new_date
,u.new_time
,u.prev_price
,u.bestprice
,i.majsd
,CASE WHEN COALESCE(m.base_part,'') = '' THEN m.part_group ELSE m.base_part END || ' - ' || m.descr part_groupd
,m.priceg
FROM
changes u
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = u.PART
LEFT OUTER JOIN rlarp.price_map m ON
m.part_group = i.stlc

38
login.frm Normal file
View File

@ -0,0 +1,38 @@
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} login
Caption = "CMS Login"
ClientHeight = 2295
ClientLeft = 120
ClientTop = 465
ClientWidth = 2445
OleObjectBlob = "login.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "login"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public proceed As Boolean
Private Sub cbCancel_Click()
tbU.text = ""
tbP.text = ""
proceed = False
Me.Hide
End Sub
Private Sub cbOK_Click()
proceed = True
Me.Hide
End Sub
Private Sub UserForm_Terminate()
proceed = False
End Sub

BIN
login.frx Normal file

Binary file not shown.

311
pcore_seq.sql Normal file
View File

@ -0,0 +1,311 @@
--CREATE OR REPLACE VIEW rlarp.pcore_seq AS
--EXPLAIN (ANALYZE, BUFFERS)
---------------------------------------------------------------------------------------------------
--there are 3 UOM involved:
--1. the original package (mpckg) from the core price row
--2. the array of pckg codes (pckg) that will allow joining to the item master (hard-coded)
--3. the UOM of the volume break (vol_uom)
---------------------------------------------------------------------------------------------------
WITH
-------------make BDL and SLV the same thing-------------------------------------------------------
-------------assumes that left column has all unique packages in rlarp.pcore-----------------------
vu AS (
SELECT * FROM ( VALUES
('BDL','SLV'),
('BDL','BDL'),
('PLT','PLT'),
('CSE','CSE')
) x(bdl,uom)
)
----------------add columns for a better understanding of what is otherwise implied----------------
,pc AS materialized (
SELECT
plist
,STLC
,COALESCE(COLTIER,'') COLTIER
,coalesce(BRANDING,'' ) branding
,COALESCE(ACCS ,'') ACCS
,COALESCE(SUFF,'') SUFFIX
,COALESCE(pckg,'') mpckg --retain the original price row package indicator
----price 1 -----------------
,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG1
,1 VOLL1
,PCKG VOL1_UOM --the original package, same as mpckg
,PACK VOL1_PRICE
----price 2 -----------------
,jsonb_build_array(PCKG)||CASE WHEN pckg = 'BDL' THEN '["SLV"]' ELSE '[]' END::jsonb PCKG2
,1 VOLl2
,'PLT' VOL2_UOM --the original volume basis unit of measure (PLT = master pallet)
,MP VOL2_PRICE
----price 3 -----------------
,jsonb_build_array('PLT') PCKG3
,1 VOLL3
,'PLT' VOL3_UOM --this one is overriden to PLT to hook with bulk parts
,BULK VOL3_PRICE
FROM
rlarp.PCORE P
-- WHERE
-- stlc = 'FNP07080'
-- AND plist = 'FCAC'
)
--SELECT * FROM pc
--------------list unique colors per line-----------------------------------------------------------
,colors AS (
SELECT
pc.plist
,pc.STLC
,pc.COLTIER
,pc.branding
,pc.ACCS
,pc.SUFFIX
,pc.mpckg
,string_agg( DISTINCT ltrim(rtrim(i.COLC)),', ') tclist
,jsonb_agg( DISTINCT ltrim(rtrim(i.COLC))) clist
FROM
pc
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.STLC = pc.stlc
AND i.COLTIER = pc.coltier
AND i.colc NOT IN ('MIX','')
--relax relationship to pick up any colors that we run for the mold and color tier
--AND COALESCE(i.BRANDING,'') = pc.branding
--AND COALESCE(i.ACCS,'') = pc.ACCS
--AND COALESCE(i.SUFFIX,'') = pc.suffix
AND APLNT <> 'I'
--exclude hangers packed in 400 by targeting `AUTO` in the desription
AND i.descr !~ 'AUTO'
GROUP BY
pc.plist
,pc.STLC
,pc.COLTIER
,pc.branding
,pc.ACCS
,pc.SUFFIX
,pc.mpckg
)
--SELECT * FROM colors
-----------since joining to the item master will be differnt for the last bulk columm---------------
-----------must first pivot the data out then join based on the type of row-------------------------
,unpivot AS (
SELECT
pc.plist
,pc.stlc
,pc.coltier
,pc.branding
,pc.accs
,pc.suffix
---meta package (the one used on the customer facing price row)----------
,pc.mpckg
,1 col
,pc.pckg1 pckg
,pc.vol1_uom vol_uom
,pc.voll1 vol_qty
--,COALESCE(pc.vol1_price,0) price
,pc.vol1_price price
FROM
pc
WHERE
true
--pc.vol1_price IS NOT null
UNION
SELECT
pc.plist
,pc.stlc
,pc.coltier
,pc.branding
,pc.accs
,pc.suffix
---meta package (the one used on the customer facing price row)----------
,pc.mpckg
,2 col
,pc.pckg2 pckg
,pc.vol2_uom vol_uom
,pc.voll2 vol_qty
--,coalesce(pc.vol2_price,0) price
,pc.vol2_price price
FROM
pc
WHERE
true
--pc.vol2_price IS NOT null
UNION
SELECT
pc.plist
,pc.stlc
,pc.coltier
,pc.branding
,pc.accs
,pc.suffix
---meta package (the one used on the customer facing price row)----------
,pc.mpckg
,3 col
,pc.pckg3 pckg
,pc.vol3_uom vol_uom
,pc.voll3 vol_qty
--,coalesce(pc.vol3_price,0) price
,pc.vol3_price price
FROM
pc
WHERE
true
--pc.vol3_price IS NOT null
)
--SELECT * FROM unpivot
--SELECT * FROM unpivot WHERE pckg ? 'SLV'
--SELECT DISTINCT pckg FROM unpivot;
-----------------link to actual part numbers, tag the sequence so a single item can be chosen---------------------
,items_init AS (
SELECT
up.plist
,up.stlc
,up.coltier
,up.branding
,up.accs
,up.suffix
,up.mpckg
,up.pckg --item master joiner
--retain this column to re-pivot back to original format
,up.col
,up.vol_uom
,up.voL_qty
,up.price
,row_number() OVER (PARTITION BY up.plist, up.stlc, up.coltier, up.branding, up.accs, up.suffix, up.pckg, up.col, up.vol_uom ORDER BY item ASC) seq
,item
--subquery option is not performant at all
FROM
unpivot up
----make all the BDL units of measure BDL and also SLV with intentional duplication
----becusae some defaults units on itemm have SLV and not BDL, but still need a join
--LEFT OUTER JOIN vu ON
-- vu.bdl = up.pckg
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.STLC = up.stlc
AND i.COLTIER = up.coltier
--basically the same as does the core list package match the item master package
--the item master could have multiple since its defined per plant but will take the first item for now
AND up.pckg ? i.uomp
AND COALESCE(i.BRANDING,'') = up.branding
AND COALESCE(i.ACCS_PS,'') = up.ACCS
AND COALESCE(i.SUFFIX,'') = up.suffix
AND APLNT <> 'I'
--exclude hangers packaged in 400 for automation pruposes
AND i.descr !~ 'AUTO'
)
--SELECT * FROM items_init
--SELECT * FROM items_init WHERE stlc = 'TRV55420' ORDER BY plist, stlc, col, seq
----compile all the conversions into a single json object----------------------
,items AS (
SELECT
jsonb_agg( DISTINCT
jsonb_build_object(
'partn', item
,'fu', vol_uom
,'tu','PC'
)
) conv
FROM
items_init
WHERE
seq = 1
AND COALESCE(item,'') <> ''
)
--do the conversion-------------------------------------------------------------
, conversion AS (
SELECT
rslt.*
FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
----join conversion back to data initial part link------------------------------
,joinback_init AS (
SELECT
i.plist
,i.stlc
,i.coltier
,i.branding
,i.accs
,i.suffix
,i.mpckg
,i.pckg
,i.col
,i.vol_uom
,i.vol_qty
,i.price
,c.nm
,c.dm
,l.clist
FROM
items_init i
LEFT OUTER JOIN conversion c ON
c.p = i.item
AND c.f = i.vol_uom
LEFT OUTER JOIN colors l ON
l.stlc = i.stlc
AND l.coltier = i.coltier
AND l.branding = i.branding
AND l.accs = i.accs
AND l.suffix = i.suffix
AND l.mpckg = i.mpckg
WHERE
i.seq = 1
)
--SELECT * FROM joinback_init
----aggregate everything offered for the price point----------------------------
,agg_levels AS (
SELECT
plist
,stlc
,string_agg(DISTINCT coltier ,',') FILTER (WHERE clist IS NOT NULL) coltier
,COALESCE(string_agg(DISTINCT branding,',') FILTER (WHERE COALESCE(branding,'') <> ''),'') branding
-------replace blank with 'none'--------------
,regexp_replace(string_agg(DISTINCT accs ,','),'^,','','g') accs
,regexp_replace(string_agg(DISTINCT suffix ,','),'^,','','g') suffix
,mpckg
--,pckg
,col
,vol_uom
,voL_qty
--,CASE WHEN price = 0 then null::numeric ELSE max(nm) END nm
,max(nm) nm
--,CASE WHEN price = 0 then null::numeric ELSE max(dm) END dm
,max(dm) dm
,price
-----convert json array to a plain text representation as well as remove nulls------
,regexp_replace(jsonb_arr_aggcd(clist)::text,'["\[\]]|null, ','','g') clist
FROM
joinback_init
GROUP BY
plist
,stlc
,mpckg
--,pckg
,col
,vol_uom
,vol_qty
,price
)
--SELECT * FROM agg_levels order by stlc , coltier , branding ,accs , suffix, mpckg
,seq_levels AS (
SELECT
plist||'.'||stlc||'.'||mpckg||'.'||col||'.'||to_char(row_number() OVER (PARTITION BY plist, stlc, mpckg, col ORDER BY price ASC),'FM00') lookup
,plist
,stlc
,coltier
,branding
,accs
,suffix
,mpckg pckg
,clist
,col
,vol_uom
,vol_qty
,nm
,dm
,price
,CASE WHEN COALESCE(price,0) = 0 THEN '' ELSE plist||'.'||stlc||'.'||mpckg||'.'||to_char(row_number() OVER (PARTITION BY plist, stlc, mpckg, col ORDER BY price ASC),'FM00') END descr_lookup
FROM
agg_levels
)
SELECT * FROM seq_levels WHERE nm is not null
--ORDER BY stlc , coltier , branding ,accs , suffix, pckg

View File

@ -0,0 +1,136 @@
CREATE OR REPLACE VIEW rlarp.plcore_build_fullcode AS
WITH
f20 AS (
SELECT
b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
,b.vol_uom
,b.vol_qty
,b.vol_price price
,b.listcode
,b.orig_row
,b.orig_col
,i.item
,i.clss
,i.aplnt
FROM
rlarp.plcore b
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = b.stlc
AND i.coltier = b.coltier
AND COALESCE(b.branding,'') = COALESCE(i.branding,'')
AND COALESCE(b.accs,'') = COALESCE(i.accs_ps,'')
AND COALESCE(b.suff,'') = COALESCE(i.suffix,'')
AND b.uomp = i.uomp
AND i.item NOT LIKE '9PT%'
WHERE
true
AND COALESCE(b.vol_price,0) <> 0
--AND listcode = 'GUAU'
)
--select * from f20;
,uom AS (
SELECT
uom.*
FROM
--aggregate converion targets to array
(
SELECT
jsonb_agg(row_to_json(d)::jsonb) jdoc
FROM
--inline table made up of conversion targets
(
SELECT DISTINCT
f20.item partn, 'PC' tu, f20.vol_uom fu
FROM
f20
) d
) c
JOIN LATERAL rlarp.uom_array(c.jdoc) uom ON TRUE
)
--SELECT * FROM uom
-------------------sort pricing to isolate duplicates----------------------
,grp AS (
SELECT
f.stlc
,f.coltier
,f.branding
,f.accs
,f.suff
,f.item
,f.uomp
,f.listcode
,f.orig_row
,f.orig_col
,f.clss
,f.vol_uom
,f.vol_qty
,round(round(u.nm/u.dm,0)/1000,5) vb_m
,'M' vbm_uom
,f.price
--,f.orig_row
--,f.orig_col
,COALESCE(
CASE WHEN f.price IS NOT NULL AND f.item IS NULL THEN 'No SKU' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.aplnt = 'I' THEN 'Inactive' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.item IS NOT NULL AND u.p IS NULL THEN 'No UOM Convesion' ELSE NULL::text END
) errorm
,current_user::text
,row_number() OVER (PARTITION BY f.listcode, f.item, round(round(u.nm/u.dm,0)/1000,5) ORDER BY f.price DESC) srt
FROM
f20 f
LEFT OUTER JOIN uom u ON
u.p = f.item
AND u.f = f.vol_uom
WHERE
true
AND COALESCE(f.price,0) <> 0
--AND f.aplnt <> 'I'
ORDER BY
f.listcode
,f.item
,round(round(u.nm/u.dm,0)/1000,5)
)
SELECT
f.stlc
,f.coltier
,f.branding
,f.accs
,f.suff
,f.item
,f.uomp
,f.listcode
,f.orig_row
,f.orig_col
,f.clss
,f.vol_uom
,f.vol_qty
,f.vb_m
,f.vbm_uom
,f.price
,f.errorm
,f.current_user
--,f.srt
FROM
grp f
WHERE
true
------any row with a srt <> 1 is a duplicate price point for the exact same scenario----
AND f.srt = 1
--AND f.aplnt <> 'I'
--AND f.item <> ''
--AND f.srt <> 1
--AND f.item = 'FHS12000WAXD032GR'
ORDER BY
f.listcode
,f.item
,f.vb_m;
-- ,errorm DESC
-- ,item ASC;
--END
--$function$
GRANT SELECT ON rlarp.plcore_build_fullcode TO PUBLIC;

View File

@ -0,0 +1,129 @@
DROP FUNCTION IF EXISTS rlarp.plcore_build_fullcode_cust(text,date);
CREATE FUNCTION rlarp.plcore_build_fullcode_cust(_price_lev text, _eff_date date)
RETURNS TABLE (
"Currency" text
,"Product" text
,"Description1" text
,"Description2" text
,"Included Accessories" text
,"PKG" text
,"Qty(M)" numeric
--,"Price" numeric
,"Cases Per Pallet" numeric
,"Single Case/Bulk Price Per M" numeric
,"Master Pallet Price Per M" numeric
,"Case/Bulk Price Per Package" numeric
,"Master Pallet Price Per Pallet" numeric
,"Bulk Pallet Price per Pallet" numeric
,"Master Pallet Price Per Case" numeric
,"Minimum Order(M)" numeric
--,"Inventory Class" text
)
LANGUAGE plpgsql AS
$function$
BEGIN
RETURN QUERY
WITH
----------sequence the volume breaks and assume that the second one is the master pallet volume-------------------
raw AS (
SELECT
CASE substring(pbf.listcode,4,1)
WHEN 'U' THEN 'USD'
WHEN 'C' THEN 'CAD'
ELSE ''
END currency
,pbf.listcode
,RTRIM(pbf.item) jcpart
,i.descr
,i.descr2
,pbf.vbm_uom jcunit
,pbf.price jcpric
,pbf.vb_m jcvoll
,i.uomp
,CASE WHEN i.mino = '0' THEN null::bigint ELSE i.mino::bigint END mino
,i.clss
,i.acc_list
,row_number() OVER (PARTITION BY pbf.listcode, pbf.item ORDER BY pbf.vb_m ASC) seq
FROM
rlarp.plcore_build_fullcode pbf
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = pbf.item
WHERE
pbf.price IS NOT NULL
AND pbf.errorm IS NULL
AND pbf.item IS NOT NULL
AND pbf.listcode IN (
SELECT
jbplcd
FROM
"CMS.CUSLG".iprcbhc p
WHERE
p.jbplvl = _price_lev
AND _eff_date BETWEEN p.jbfdat AND p.jbtdat
)
--AND pbf.listcode = 'GUAU'
)
--SELECT * FROM raw
----------start with only all sequence 1 items and then rejoin to get the 2nd break as a new column----------------
,pivot AS (
SELECT
w.currency
,w.listcode
,w.jcpart
,w.descr
,w.descr2
,w.acc_list
,w.jcunit
,w.jcpric
,w.seq
,w.uomp
,w.jcvoll
,ceiling(w.mino::numeric/(w.jcvoll::numeric*1000))*w.jcvoll mino
,w.clss
,CASE WHEN w.uomp = 'PLT' THEN null::numeric ELSE ROUND(COALESCE(c.jcvoll/w.jcvoll,1),2) END cse_per_plt
,CASE WHEN substring(w.listcode,1,1) = 'N' AND w.uomp <> 'PLT' THEN null::numeric ELSE w.jcpric END cse_price_m
,c.jcpric plt_price_m
,CASE WHEN substring(w.listcode,1,1) = 'N' AND w.uomp <> 'PLT' THEN null::numeric ELSE round(w.jcpric*w.jcvoll,2) END cse_price_pkg
,round(c.jcpric*c.jcvoll,2) plt_price_pkg
,round(CASE WHEN w.uomp = 'PLT' THEN w.jcpric*w.jcvoll ElSE CAST(null as numeric) END,2) plt_price_plt
,round(CASE WHEN w.uomp <> 'PLT' THEN cast(c.jcpric*c.jcvoll as numeric)/cast(c.jcvoll/w.jcvoll as numeric) ElSE CAST(null as numeric) END,2) plt_price_cse
FROM
raw w
LEFT OUTER JOIN raw c ON
c.jcpart = w.jcpart
AND c.seq = 2
AND c.listcode = w.listcode
WHERE
w.seq = 1
ORDER BY
w.jcpart
,w.seq
)
------------pretty column names-----------------------------------------
SELECT
currency "Currency"
,jcpart "Product"
,descr "Description1"
,descr2 "Description2"
,acc_list "Included Accessories"
,uomp "UOM"
,jcvoll "Case Qty"
--,jcpric "Price"
,cse_per_plt "Cases Per Pallet"
,cse_price_m "Single Case/Bulk Price Per M"
,plt_price_m "Master Pallet Price Per M"
,cse_price_pkg "Case/Bulk Price Per Package"
,plt_price_pkg "Master Pallet Price Per Pallet"
,plt_price_plt "Bulk Pallet Price per Pallet"
,plt_price_cse "Master Pallet Price Per Case"
,mino "Non-Stocking Minimum"
--,clss "Inventory Class"
--,japlcd "delete this column"
FROM
pivot
ORDER BY
jcpart;
END
$function$

View File

@ -0,0 +1,128 @@
DROP FUNCTION IF EXISTS rlarp.plcore_fullcode_inq;
CREATE FUNCTION rlarp.plcore_fullcode_inq (_items jsonb)
RETURNS TABLE (
stlc text
,coltier text
,branding text
,accs text
,suffix text
,item text
,uomp text
,clss text
,vb numeric
,vb_uom text
,vb_m numeric
,vbm_uom text
,price numeric
,orig_row numeric
,orig_col numeric
,error text
,usern text
,listcode text
)
LANGUAGE plpgsql AS $function$
BEGIN
RETURN QUERY
WITH
f20 AS (
SELECT
b.stlc
,b.coltier
,b.branding
,b.accs
,b.suffix
,b.uomp
,b.vol_uom
,b.vol_qty
,b.vol_price price
,b.listcode
,b.orig_row
,b.orig_col
,i.item
,i.clss
,i.aplnt
FROM
jsonb_to_recordset(_items) AS b (
stlc text
,coltier text
,branding text
,accs text
,suffix text
,uomp text
,vol_uom text
,vol_qty numeric
,vol_price numeric
,listcode text
,orig_row numeric
,orig_col numeric
)
--rlarp.plcore b
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = b.stlc
AND i.coltier = b.coltier
AND COALESCE(b.branding,'') = COALESCE(i.branding,'')
AND COALESCE(b.accs,'') = COALESCE(i.accs_ps,'')
AND COALESCE(b.suffix,'') = COALESCE(i.suffix,'')
AND b.uomp = i.uomp
)
--select * from f20;
,uom AS (
SELECT
uom.*
FROM
--aggregate converion targets to array
(
SELECT
jsonb_agg(row_to_json(d)::jsonb) jdoc
FROM
--inline table made up of conversion targets
(
SELECT DISTINCT
f20.item partn, 'PC' tu, f20.vol_uom fu
FROM
f20
) d
) c
JOIN LATERAL rlarp.uom_array(c.jdoc) uom ON TRUE
)
--SELECT * FROM uom
SELECT
f.stlc
,f.coltier
,f.branding
,f.accs
,f.suffix
,f.item
,f.uomp
,f.clss
,f.vol_qty vb
,f.vol_uom vb_uom
,round(f.vol_qty*round(u.nm/u.dm,0)/1000,5) vb_m
,'M' vbm_uom
,f.price
,f.orig_row
,f.orig_col
,COALESCE(
CASE WHEN f.price IS NOT NULL AND f.item IS NULL THEN 'No SKU' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.aplnt = 'I' THEN 'Inactive' ELSE NULL::text END
,CASE WHEN f.price IS NOT NULL AND f.item IS NOT NULL AND u.p IS NULL THEN 'No UOM Convesion' ELSE NULL::text END
) error
,current_user::text usern
,f.listcode
FROM
f20 f
LEFT OUTER JOIN uom u ON
u.p = f.item
AND u.f = f.vol_uom
WHERE
true
--AND f.aplnt <> 'I'
ORDER BY
orig_row
,orig_col
,error DESC
,item ASC;
END
$function$

788
plcore_build_pretty.pg.sql Normal file
View File

@ -0,0 +1,788 @@
DROP FUNCTION IF EXISTS rlarp.plcore_build_pretty;
CREATE FUNCTION rlarp.plcore_build_pretty(_price_lev text, _segment_regex text)
RETURNS TABLE (
-- header text
--,header_sort int
-- detail_sort int
--,comp_sort bigint
--,seq bigint
--,listcode text
--,flag text
"Product" text
,"Description" text
,"ColTier" text
,"Pack" text
,"Brnd" text
,"Accs" text
,"Opt" text
,"Colors" text
,"Qty (M)1" text
,"Price (M)1" text
,"Price (CS)1" text
,"Qty (M)2" text
,"Price (M)2" text
,"Price (CS)2" text
,"Qty (M)3" text
,"Price (M)3" text
,"Price (PL)3" text
,flag text
,dsort text
,band boolean
,currency text
,segment text
)
LANGUAGE plpgsql AS $function$
BEGIN
DROP TABLE IF EXISTS stage3;
CREATE TEMP TABLE IF NOT EXISTS stage3 AS (
WITH
--------------------------helper table to choose column based on uom-----------------------------------------------------------------
pivot_guide AS (
SELECT * FROM ( VALUES
('BDL','BDL','01'),
('BDL','PLT','02'),
('SLV','SLV','01'),
('SLV','PLT','02'),
('CSE','CSE','01'),
('CSE','PLT','02'),
('PC','PC','01'),
('PC','PLT','02'),
('PLT','PLT','03')
) x(uomp,vol_uom,col)
-------------------------------------------------------------------------------------------------------------------------------------
)
,stage1 AS (
SELECT
p.listcode
,p.stlc
,p.listcode||'-'||p.stlc||'.'||COALESCE(p.coltier,'')||'.'||COALESCE(p.branding,'')||'.'||COALESCE(p.accs,'')||'.'||COALESCE(p.suff,'')||'.'||COALESCE(p.uomp)||'-'||g.col pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,g.col
FROM
rlarp.plcore p
LEFT OUTER JOIN pivot_guide g ON
g.uomp = p.uomp
AND g.vol_uom = p.vol_uom
WHERE
p.vol_price IS NOT NULL
--AND listcode IN (SELECT DISTINCT jbplcd FROM "CMS.CUSLG".iprcbhc i WHERE jbplvl = 'U.BOC.DI' AND current_date BETWEEN jbfdat AND jbtdat)
AND listcode IN (SELECT DISTINCT jbplcd FROM "CMS.CUSLG".iprcbhc i WHERE jbplvl = _price_lev AND current_date BETWEEN jbfdat AND jbtdat)
--only pull target listcodes--
AND listcode ~ _segment_regex
)
-----run this to check for duplicate rows-------------------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage1) x where dup > 1
-------------------------------------------------------------------------------------------------------------------------------------
,stage2 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
,i.item
--every part is linked so all the colors are already here
,i.colc
,pkey1||'-'||substring(i.item,13,3) pkey2
--look at this position to identify potential pack sizes without doing the conversion on every item
,substring(i.item,13,3) nc
,row_number() OVER (PARTITION BY pkey1, substring(i.item,13,3)) rn
FROM
stage1 p
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = p.stlc
AND i.coltier = p.coltier
AND COALESCE(p.branding,'') = COALESCE(i.branding,'')
AND COALESCE(p.accs,'') = COALESCE(i.accs_ps,'')
AND COALESCE(p.suff,'') = COALESCE(i.suffix,'')
AND i.uomp = p.uomp
AND i.aplnt <> 'I'
AND i.ming <> 'C10' --exclude packed for retail
AND i.descr !~ ' AUTO ' --exclude automation packaging
)
--SELECT * FROM stage2 order by pkey1
---------------see how many potnential pack sizes there are per price point-------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage2 WHERE rn = 1) X WHERE dup > 1 ORDER BY pkey2 ASC
,items AS (
SELECT
jsonb_agg( DISTINCT
jsonb_build_object(
'partn', item
,'fu', vol_uom
,'tu','PC'
)
) conv
FROM
stage2
WHERE
rn = 1
)
--do the conversion-------------------------------------------------------------
, conversion AS (
SELECT
rslt.p
,rslt.f
,rslt.t
,rslt.nm
,rslt.dm
FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
--SELECT * FROM CONVERSION
,stage3 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
,jsonb_agg(DISTINCT p.colc) FILTER (WHERE p.colc IS NOT NULL) colors --this array will have to be aggregated again when columns pivot out
,jsonb_agg(DISTINCT to_char(c.nm/c.dm,'999,999')) FILTER(WHERE rn = 1 AND c.nm IS NOT NULL) price_qty
--,jsonb_agg(c.nm/c.dm) FILTER(WHERE rn = 1 AND c.nm IS NOT NULL) price_qty_num
FROM
stage2 p
LEFT OUTER JOIN conversion c ON
c.p = p.item
AND c.f = p.vol_uom
WHERE
TRUE
--this filter shoudl not be used since every color is linked in stage 2 exists on rn <> 1
--AND rn = 1
GROUP BY
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
)
SELECT * FROM stage3
) WITH DATA;
--select jsonb_arr_aggcd(colors) from stage3
RETURN QUERY
WITH
no_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) colors
,jsonb_arr_aggcd(b.price_qty ORDER BY b.price_qty) FILTER (WHERE b.col = '01' AND b.price_qty IS NOT NULL) c1_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '01') c1_price
,jsonb_arr_aggcd(b.price_qty ORDER BY b.price_qty) FILTER (WHERE b.col = '02' AND b.price_qty IS NOT NULL) c2_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '02') c2_price
FROM
stage3 b
WHERE
b.col <> '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM no_bulk
,only_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) FILTER (WHERE b.colors IS NOT NULL) colors
,jsonb_arr_aggcd(b.price_qty ORDER BY b.price_qty) FILTER (WHERE b.col = '03' AND b.price_qty IS NOT NULL) c3_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '03') c3_price
FROM
stage3 b
WHERE
b.col = '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM only_bulk
--SELECT * FROM only_bulk where c3_price->>1 IS NOT NULL
--SELECT * FROM no_bulk where c1_price->>1 IS NOT NULL
----full outer join in case there are bulk prices with no case pricing----
,stage4 AS (
SELECT
COALESCE(n.listcode,b.listcode) listcode
,COALESCE(n.stlc,b.stlc) stlc
,COALESCE(n.coltier,b.coltier) coltier
,COALESCE(n.branding,b.branding,'') branding
,COALESCE(n.accs,b.accs,'') accs
,COALESCE(n.suff,b.suff,'') suff
,COALESCE(n.uomp, b.uomp) uomp
,public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb)) colors_array
--,(SELECT string_agg(ae,', ') FROM (SELECT ae FROM jsonb_array_elements_text(public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb))) ae) x) colors
,n.c1_vol
---there better not be multiple prices---
,COALESCE(n.c1_price,'[]'::jsonb) c1_price
,n.c2_vol
,COALESCE(n.c2_price,'[]'::jsonb) c2_price
,b.c3_vol
,COALESCE(b.c3_price,'[]'::jsonb) c3_price
,COALESCE(n.listcode,b.listcode)||'.'||COALESCE(n.stlc,b.stlc)||'.'||COALESCE(n.uomp, b.uomp)||'.'||COALESCE(n.branding,b.branding,'')||'.'||COALESCE(n.accs,b.accs,'')||'.'||COALESCE(n.suff,b.suff,'') item
FROM
no_bulk n
FULL OUTER JOIN only_bulk b ON
b.listcode = n.listcode
AND b.stlc = n.stlc
AND b.coltier = n.coltier
AND COALESCE(b.branding,'') = COALESCE(n.branding,'')
AND COALESCE(b.accs ,'') = COALESCE(n.accs ,'')
AND COALESCE(b.suff ,'') = COALESCE(n.suff ,'')
)
--select * from stage4
,test_multiprice AS (
--should return no rows
SELECT
*
FROM
stage4
WHERE
(c1_price->>1 IS NOT NULL OR c2_price->>1 IS NOT NULL OR c3_price->>1 IS NOT NULL)
AND colors_array IS NOT NULL
AND jsonb_array_length(c1_vol) > 0
AND jsonb_array_length(c2_vol) > 0
AND jsonb_array_length(c3_vol) > 0
)
--SELECT * FROM test_multiprice
--SELECT * FROM stage4 ORDER BY item ASC
------group to the price level and aggregate attributes----------------------
,stage5 AS (
SELECT
b.listcode
,b.stlc
,jsonb_agg(DISTINCT b.coltier) coltier
,b.uomp
,jsonb_agg(DISTINCT b.branding ORDER BY b.branding DESC) branding
,jsonb_agg(DISTINCT b.accs ORDER BY b.accs ASC) accs
,jsonb_agg(DISTINCT b.suff ORDER BY b.suff ASC) suff
,jsonb_arr_aggcd(b.colors_array) colors_array
,jsonb_arr_aggcd(b.c1_vol) c1_vol
,b.c1_price
,jsonb_arr_aggcd(b.c2_vol) c2_vol
,b.c2_price
,jsonb_arr_aggcd(b.c3_vol) c3_vol
,b.c3_price
--,b.item
,b.c1_price||b.c2_price||b.c3_price pgroup
--,jsonb_build_object('pack',c1_price,'mp',c2_price,'bulk',c3_price) pgroupo
--how to sort 3 items, one of which may be null
--start by attempting to evaluate non-null columns
FROM
stage4 b
GROUP BY
b.listcode
,b.stlc
,b.uomp
--,b.branding
--,b.accs
--,b.suff
,b.c1_price
,b.c2_price
,b.c3_price
--,b.item
ORDER BY
stlc ASC
)
--SELECT * FROM stage5
,dsort AS (
SELECT
m."header"
,m.header_sort::int header_sort
,m.detail_sort::int detail_sort
,m.stlc
,f.flag
,COALESCE(rstt.rn,0) comp_sort
,COALESCE(rstt.c,m.stlc) stlcc
,mj.descr
--,mj."header"
FROM
rlarp.molds m
CROSS JOIN ( VALUES
('base'),
('compatible')
) AS f(flag)
LEFT JOIN LATERAL regexp_split_to_table(m.compatible,', ') WITH ORDINALITY rstt(c, rn) ON
f.flag = 'compatible'
---dont list trays for nursery items---
AND NOT m.glec ~ '1NU'
LEFT OUTER JOIN rlarp.molds mj ON
mj.stlc = COALESCE(rstt.c,m.stlc)
WHERE
true
AND NOT (f.flag = 'compatible' AND COALESCE(rstt.rn,0) = 0)
AND NOT (f.flag = 'compatible' AND COALESCE(m.header,'') = '')
AND m.status <> 'I'
--only list active compatible items
AND mj.status <> 'I'
--exclude compatible items that have their own price list header
AND NOT (f.flag = 'compatible' AND mj."header" IS NOT NULL)
ORDER BY
m.header_sort
,m.detail_sort
,comp_sort
)
--SELECT * FROM dsort where stlc = 'SPH1DPI0'
--SELECT * FROM dsort WHERE header = 'Standard Inserts'
--SELECT * FROM stage5 --where c3_price->>1 IS NOT NULL
,stage6 AS (
SELECT
s."header"
,s.header_sort
,s.detail_sort
,s.comp_sort
,b.listcode
,s.flag
,b.stlc
,s.descr
--,b.coltier
,public.jsonb_array_string_agg(b.coltier,', ') colortiers
,b.uomp
,public.jsonb_array_string_agg(b.branding,', ') branding
,public.jsonb_array_string_agg(b.accs,', ') accs
,public.jsonb_array_string_agg(b.suff,', ') suff
,public.jsonb_array_string_agg(b.colors_array,', ') colors
,ltrim(public.jsonb_array_string_agg(b.c1_vol,' ')) pack_qty
,to_char((b.c1_price->>0)::numeric,'FM999,999.00') pack_price
,to_char(CASE WHEN jsonb_array_length(b.c1_vol) = 1 THEN (b.c1_price->>0)::numeric/1000*(replace(b.c1_vol->>0,',',''))::numeric ELSE null::numeric END,'FM999,999.00') price_per_pack
,ltrim(public.jsonb_array_string_agg(b.c2_vol,' ')) mp_qty
,to_char((b.c2_price->>0)::numeric,'FM999,999.00') mp_price
----------------------------------------this price divided by c1_vol is price per case instead of c2_vol which would give price per pallet--------------------------------------------
,to_char(CASE WHEN jsonb_array_length(b.c1_vol) = 1 THEN (b.c2_price->>0)::numeric/1000*(replace(b.c1_vol->>0,',',''))::numeric ELSE null::numeric END,'FM999,999.00') price_per_mp
,ltrim(public.jsonb_array_string_agg(b.c3_vol,' ')) bulk_qty
,to_char((b.c3_price->>0)::numeric,'FM999,999.00') bulk_price
,to_char(CASE WHEN jsonb_array_length(b.c3_vol) = 1 THEN (b.c3_price->>0)::numeric/1000*(replace(b.c3_vol->>0,',',''))::numeric ELSE null::numeric END,'FM999,999.00') price_per_bulk
,row_number() OVER (PARTITION BY b.listcode, s.stlc, s.comp_sort ORDER BY b.pgroup ASC) seq
,substring(listcode,4,1) currency
,substring(listcode,1,1) segment
FROM
dsort s
INNER JOIN stage5 b ON
b.stlc = s.stlcc
WHERE
TRUE
--AND listcode = 'GUAU'
--exclude price points that don't have any part numbers otherwise you may have sequences above
--that are not real
AND b.colors_array IS NOT NULL
--ORDER BY
-- s.header_sort
-- ,s.detail_sort
-- ,s.comp_sort
--if "colors" column is null there are no part numbers available for this price point
)
--SELECT * FROM stage6 WHERE detail_sort = 1
----any item that has a compatible, but it's base item gets dropped leaving only the compatible item
,orphans AS (
SELECT
detail_sort
,jsonb_agg(DISTINCT s.flag) jflag
FROM
stage6 s
GROUP BY
detail_sort
HAVING
NOT jsonb_agg(DISTINCT s.flag) @? '$[*] ? (@ == "base")'
)
,get_header AS (
SELECT
s.header
,s.header_sort
,0 detail_sort
,0 comp_sort
,null::text listcode
,'header' flag
,s.header
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,0 seq
,substring(listcode,4,1) currency
,string_agg(DISTINCT substring(listcode,1,1),', ') segment
FROM
stage6 s
WHERE
s.header IS NOT NULL
GROUP BY
s.header
,s.header_sort
,substring(listcode,4,1)
)
,stage7 AS (
SELECT
*
FROM
(
SELECT * FROM stage6 WHERE
TRUE
AND detail_sort NOT IN (SELECT detail_sort FROM orphans)
--only list the first price option for compatible products
--AND NOT (flag = 'compatible' AND seq <> 1)
UNION ALL
SELECT * FROM get_header
) x
--ORDER BY
-- header_sort ASC
-- ,detail_sort ASC
-- ,comp_sort ASC
-- ,seq ASC
)
,colors AS (
SELECT
COALESCE(min(colcode) FILTER (WHERE c.col = 0) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 0),''),31) ||
COALESCE(min(colcode) FILTER (WHERE c.col = 1) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 1),''),31) ||
COALESCE(min(colcode) FILTER (WHERE c.col = 2) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 2),''),31) ||
COALESCE(min(colcode) FILTER (WHERE c.col = 3) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 3),''),31) ||
COALESCE(min(colcode) FILTER (WHERE c.col = 4) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 4),''),31) ||
COALESCE(min(colcode) FILTER (WHERE c.col = 5) || ' - ',' ') || RPAD(COALESCE(min(coldesc) FILTER (WHERE c.col = 5),''),31) collist
,null::text descr
,null::text colortiers
,null::text uomp
,null::text branding
,null::text accs
,null::text suff
,null::text colors
,null::text pack_qty
,null::text pack_price
,null::text price_per_pack
,null::text mp_qty
,null::text pt_price
,null::text price_per_mp
,null::text bulk_qty
,null::text bulk_price
,null::text price_per_bulk
,'colors' flag
,max(c.col)::text detail_sort
,null::boolean band
,null::text currency
,null::text segment
FROM
(
SELECT
rn.colcode
,rn.coldesc
,rn.col
,row_number() OVER (PARTITION BY rn.col ORDER BY rn.rn ASC) rn
FROM
(
SELECT
c.colcode
,c.coldesc
,row_number() over (order by c.colcode) rn
,row_number() over (order by c.colcode)/greatest((count(*) over () /3),1) col
FROM
"CMS.CUSLG".color c
WHERE
c.colcode IN (
SELECT
jsonb_array_elements_text(jsonb_arr_aggcd(s.colors)) ae
FROM
stage3 s
)
AND COALESCE(c.colcode,'') <> ''
) rn
) c
GROUP BY
c.rn
ORDER BY
c.rn
)
,colors_header AS (
SELECT
'' stlc
,null::text descr
,null::text colortiers
,null::text uomp
,null::text branding
,null::text accs
,null::text suff
,null::text colors
,null::text pack_qty
,null::text pack_price
,null::text price_per_pack
,null::text mp_qty
,null::text pt_price
,null::text price_per_mp
,null::text bulk_qty
,null::text bulk_price
,null::text price_per_bulk
,'colors' flag
,'0' detail_sort
,null::boolean band
,null::text currency
,null::text segment
UNION ALL
SELECT
repeat('-',(max(c.detail_sort::int+1)*16)::int) || 'Color Codes' || repeat('-',(max(c.detail_sort::int+1)*16)::int) stlc
,null::text descr
,null::text colortiers
,null::text uomp
,null::text branding
,null::text accs
,null::text suff
,null::text colors
,null::text pack_qty
,null::text pack_price
,null::text price_per_pack
,null::text mp_qty
,null::text pt_price
,null::text price_per_mp
,null::text bulk_qty
,null::text bulk_price
,null::text price_per_bulk
,'colors' flag
,'0' detail_sort
,null::boolean band
,null::text currency
,null::text segment
FROM
colors c
GROUP BY
c.flag
)
---any headers that don't have any associated rows------
,orphan_headers AS (
SELECT
header
,count(*)
FROM
stage7 s
GROUP BY
header
HAVING
COUNT(*) <= 1
)
,stage8 AS (
SELECT
--s.header
--,s.header_sort
-- s.detail_sort
--,s.comp_sort
--,s.seq
--,s.listcode
--,s.flag
s.stlc
,s.descr
,s.colortiers
,s.uomp
,s.branding
,s.accs
,CASE WHEN substring(s.suff,1,2) = ', ' THEN '(' || substring(s.suff,3,100) || ')' ELSE s.suff END suff
,s.colors
,CASE WHEN s.segment = 'N' THEN '' ELSE s.pack_qty END pack_qty
,CASE WHEN s.segment = 'N' THEN '' ELSE CASE WHEN s.pack_qty IS NULL THEN null::text ELSE s.pack_price END END pack_price
,CASE WHEN s.segment = 'N' THEN '' ELSE s.price_per_pack END price_per_pack
,s.mp_qty
,CASE WHEN s.mp_qty IS NULL THEN null::text ELSE s.mp_price END
,s.price_per_mp
,s.bulk_qty
,CASE WHEN s.bulk_qty IS NULL THEN null::text ELSE s.bulk_price END
,s.price_per_bulk
,s.flag
,s.detail_sort::text
,CASE dense_rank() OVER (PARTITION BY s.header_sort ORDER BY s.detail_sort) % 2 WHEN 1 THEN true ELSE false END band
,s.currency
,s.segment
FROM
stage7 s
WHERE
TRUE
--AND s.listcode = 'GUAU'
--only list the first price point for compatible items
AND NOT (s.flag = 'compatible' AND s.seq <> 1)
AND s.header IS NOT NULL
AND s.header NOT IN (SELECT header FROM orphan_headers)
ORDER BY
s.header_sort ASC
,s.detail_sort ASC
,s.comp_sort ASC
,s.seq ASC
)
,ctier AS (
SELECT * FROM (VALUES
('B','Base','00'),
('T','Traditional','01'),
('L','Principal','02'),
('M','Premium','03'),
('D','Decorative Premium','04'),
('F','Premium Finish','05'),
('R','Bio','06'),
('P','Program','07'),
('C','Custom','08'),
('E','EcoGrow','09'),
('O','Organic','10'),
('S','Waxed One-Side','11'),
('W','WaxTough','12')
) x(cltier,cldesc,srt)
)
,tiers AS (
SELECT '' descr
UNION ALL
SELECT '----------------Product Tier Codes----------------' descr
UNION ALL
(
SELECT
s.coltier || ' - ' || c.cldesc descr
--,c.srt
--,JSONB_AGG(DISTINCT substring(listcode,1,1)) lc
FROM
stage3 s
LEFT OUTER JOIN ctier c ON
c.cltier = s.coltier
GROUP BY
s.coltier || ' - ' || c.cldesc
,c.srt
ORDER BY
c.srt
)
)
,accsd AS (
SELECT * FROM (VALUES
('N','Handle','06'),
('SH','Saucer & Hanger','04'),
('H','Hanger','05'),
('D','Dish','01'),
('DH','Dish & Hanger','02'),
('S','Saucer','03')
) x(accs,descr,srt)
)
--SELECT * from tiers
,accs AS (
SELECT '' descr
UNION ALL
SELECT '-------------Included Accessory Codes-------------'
UNION ALL
(
SELECT
s.accs || ' - ' || d.descr descr
FROM
stage3 s
INNER JOIN accsd d ON
d.accs = s.accs
WHERE
s.accs <> ''
GROUP BY
s.accs || ' - ' || d.descr
,d.srt
ORDER BY
d.srt ASC
)
)
--SELECT * FROM accs
,optn AS (
SELECT '' descr
UNION ALL
SELECT '----------------Product Option Codes--------------'
UNION ALL
(
SELECT
s.suff || ' - ' || u.uqdesc descr
FROM
stage3 s
INNER JOIN "CMS.CUSLG".uqf u ON
u.uqcode = s.suff
WHERE
s.suff <> ''
GROUP BY
s.suff || ' - ' || u.uqdesc
)
)
--SELECT * FROM optn
,notes AS (
VALUES
('')
,('--------------Freight Information----------------- ')
,('Freight charges may apply ')
,(' ')
,('Confidentiality - The information transmitted is intended only for the addressee and may contain confidential and/or proprietary material. ')
,('Any unauthorized review, distribution or other use of this information, or the taking of any action in reliance upon this information, ')
,('is prohibited. If you received this in error, please contact the sender and delete or destroy this message and any copies. ')
,('')
)
,footer AS (
SELECT *, null::text, null::text, null::text, null::text, null::text, null::text,null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text,'notes','0', null::boolean, null::text, null::text FROM tiers WHERE (select count(*) from tiers) > 2
UNION ALL
SELECT *, null::text, null::text, null::text, null::text, null::text, null::text,null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text,'notes','0', null::boolean, null::text, null::text FROM accs WHERE (select count(*) from accs) > 2
UNION ALL
SELECT *, null::text, null::text, null::text, null::text, null::text, null::text,null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text,'notes','0', null::boolean, null::text, null::text FROM optn WHERE (select count(*) from optn) > 2
UNION ALL
SELECT *, null::text, null::text, null::text, null::text, null::text, null::text,null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text, null::text,'notes','0', null::boolean, null::text, null::text FROM notes
)
--SELECT * FROM footer
SELECT * FROM stage8
UNION ALL
SELECT * FROM colors_header
UNION ALL
SELECT * FROM colors
UNION ALL
SELECT * FROM footer;
END
$function$

View File

@ -0,0 +1,67 @@
CREATE OR REPLACE VIEW rlarp.plcore_upload AS
WITH
hdr AS (
SELECT DISTINCT
listcode listcode
,'00000' skey2
,'HDR' c1
,'2' c2
,p.listcode c3
,i.JAPLDS c4
,i.JAPLD1 c5
,i.JAPLD2 c6
,'Y' c7
,'N' c8
,'' c9
,'' c10
,'' c11
,'' c12
FROM
rlarp.plcore_build_fullcode p
INNER JOIN LGDAT.iprca i ON
i.japlcd = p.listcode
WHERE
p.item IS NOT NULL
AND p.errorm IS NULL
UNION ALL
SELECT
listcode listcode
,p.item||repeat(' ',20-length(p.item))||to_char(p.vb_m*1000,'FM0000000.0') skey2
,'DTL' c1
,p.listcode c2
,p.item c3
,p.vbm_uom c4
,p.vb_m::text c5
,p.price::text c6
,'' c7
,'' c8
,'' c9
,'' c10
,'' c11
,'1' c12
FROM
rlarp.plcore_build_fullcode p
WHERE
p.item IS NOT NULL
AND p.errorm IS NULL
)
--SELECT * FROM hdr
SELECT
listcode
,c1
,c2
,c3
,c4
,c5
,c6
,c7
,c8
,c9
,c10
,c11
,c12
FROM
hdr
ORDER BY
listcode
,skey2

33
plcore_dsort.pg.sql Normal file
View File

@ -0,0 +1,33 @@
SELECT
m."header"
,m.header_sort::int header_sort
,m.detail_sort::int detail_sort
,m.stlc
,f.flag
,COALESCE(rstt.rn,0) comp_sort
,COALESCE(rstt.c,m.stlc) stlcc
,mj.descr
--,mj."header"
FROM
rlarp.molds m
CROSS JOIN ( VALUES
('base'),
('compatible')
) AS f(flag)
LEFT JOIN LATERAL regexp_split_to_table(m.compatible,', ') WITH ORDINALITY rstt(c, rn) ON
f.flag = 'compatible'
LEFT OUTER JOIN rlarp.molds mj ON
mj.stlc = COALESCE(rstt.c,m.stlc)
WHERE
true
AND NOT (f.flag = 'compatible' AND COALESCE(rstt.rn,0) = 0)
AND NOT (f.flag = 'compatible' AND COALESCE(m.header,'') = '')
AND m.status <> 'I'
--only list active compatible items
AND mj.status <> 'I'
--exclude compatible items that have their own price list header
AND NOT (f.flag = 'compatible' AND mj."header" IS NOT NULL)
ORDER BY
m.header_sort
,m.detail_sort
,comp_sort

1
plcore_overlap.pg.sql Normal file
View File

@ -0,0 +1 @@
select stlc,jsonb_agg(distinct substring(listcode,1,1)) from rlarp.plcore group by stlc having jsonb_array_length(jsonb_agg(distinct substring(listcode,1,1))) > 1;

356
plcore_seq.sql Normal file
View File

@ -0,0 +1,356 @@
--explain (analyze, buffers)
DROP FUNCTION IF EXISTS rlarp.plcore_seq;
CREATE FUNCTION rlarp.plcore_seq()
RETURNS TABLE (
listcode text
,stlc text
,coltiers text
,uomp text
,branding text
,accs text
,suff text
,colors text
,pack_qty text
,pack_price text
,mp_qty text
,mp_price text
,bulk_qty text
,bulk_price text
,seq text
,descr text
,priceg text
,image_url text
,compatible text
,header text
,header_sort int
,detail_sort int
)
LANGUAGE plpgsql AS $function$
BEGIN
DROP TABLE IF EXISTS stage3;
CREATE TEMP TABLE stage3 AS (
WITH
--------------------------helper table to choose column based on uom-----------------------------------------------------------------
pivot_guide AS (
SELECT * FROM ( VALUES
('BDL','BDL','01'),
('BDL','PLT','02'),
('SLV','SLV','01'),
('SLV','PLT','02'),
('CSE','CSE','01'),
('CSE','PLT','02'),
('PLT','PLT','03')
) x(uomp,vol_uom,col)
-------------------------------------------------------------------------------------------------------------------------------------
)
,stage1 AS (
SELECT
p.listcode
,p.stlc
,p.listcode||'-'||p.stlc||'.'||COALESCE(p.coltier,'')||'.'||COALESCE(p.branding,'')||'.'||COALESCE(p.accs,'')||'.'||COALESCE(p.suff,'')||'.'||COALESCE(p.uomp)||'-'||g.col pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,g.col
FROM
rlarp.plcore p
LEFT OUTER JOIN pivot_guide g ON
g.uomp = p.uomp
AND g.vol_uom = p.vol_uom
WHERE
p.vol_price IS NOT NULL
--AND listcode = 'GUAU'
)
-----run this to check for duplicate rows-------------------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage1) x where dup > 1
-------------------------------------------------------------------------------------------------------------------------------------
,stage2 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
,i.item
--every part is linked so all the colors are already here
,i.colc
,pkey1||'-'||substring(i.item,13,3) pkey2
--look at this position to identify potential pack sizes without doing the conversion on every item
,substring(i.item,13,3) nc
,row_number() OVER (PARTITION BY pkey1, substring(i.item,13,3)) rn
FROM
stage1 p
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = p.stlc
AND i.coltier = p.coltier
AND COALESCE(p.branding,'') = COALESCE(i.branding,'')
AND COALESCE(p.accs,'') = COALESCE(i.accs_ps,'')
AND COALESCE(p.suff,'') = COALESCE(i.suffix,'')
AND i.uomp = p.uomp
AND i.aplnt <> 'I'
)
--SELECT * FROM stage2 order by pkey1
---------------see how many potnential pack sizes there are per price point-------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage2 WHERE rn = 1) X WHERE dup > 1 ORDER BY pkey2 ASC
,items AS (
SELECT
jsonb_agg( DISTINCT
jsonb_build_object(
'partn', item
,'fu', vol_uom
,'tu','PC'
)
) conv
FROM
stage2
WHERE
rn = 1
)
--do the conversion-------------------------------------------------------------
, conversion AS (
SELECT
rslt.p
,rslt.f
,rslt.t
,rslt.nm
,rslt.dm
FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
--SELECT * FROM CONVERSION
,stage3 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
--,p.item
--,p.pkey2
--,string_agg(DISTINCT item, ', ') items
,jsonb_agg(DISTINCT p.colc) FILTER (WHERE p.colc IS NOT NULL) colors --this array will have to be aggregated again when columns pivot out
--,string_agg(DISTINCT to_char(c.nm/c.dm,'FM999,999'),' | ') FILTER(WHERE rn = 1) price_qty
,jsonb_agg(DISTINCT to_char(c.nm/c.dm,'FM999,999')) FILTER(WHERE rn = 1 AND c.nm IS NOT NULL) price_qty
FROM
stage2 p
LEFT OUTER JOIN conversion c ON
c.p = p.item
AND c.f = p.vol_uom
WHERE
TRUE
--this filter shoudl not be used since every color is linked in stage 2 exists on rn <> 1
--AND rn = 1
GROUP BY
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
)
SELECT * FROM stage3
) WITH DATA;
--select * from stage3
RETURN QUERY
WITH
no_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) colors
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '01' AND b.price_qty IS NOT NULL) c1_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '01') c1_price
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '02' AND b.price_qty IS NOT NULL) c2_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '02') c2_price
FROM
stage3 b
WHERE
b.col <> '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM no_bulk
,only_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) FILTER (WHERE b.colors IS NOT NULL) colors
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '03' AND b.price_qty IS NOT NULL) c3_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '03') c3_price
FROM
stage3 b
WHERE
b.col = '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM only_bulk
--SELECT * FROM only_bulk where c3_price->>1 IS NOT NULL
--SELECT * FROM no_bulk where c1_price->>1 IS NOT NULL
----full outer join in case there are bulk prices with no case pricing----
,stage4 AS (
SELECT
COALESCE(n.listcode,b.listcode) listcode
,COALESCE(n.stlc,b.stlc) stlc
,COALESCE(n.coltier,b.coltier) coltier
,COALESCE(n.branding,b.branding,'') branding
,COALESCE(n.accs,b.accs,'') accs
,COALESCE(n.suff,b.suff,'') suff
,COALESCE(n.uomp, b.uomp) uomp
,public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb)) colors_array
--,(SELECT string_agg(ae,', ') FROM (SELECT ae FROM jsonb_array_elements_text(public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb))) ae) x) colors
,n.c1_vol
---there better not be multiple prices---
,COALESCE(n.c1_price,'[]'::jsonb) c1_price
,n.c2_vol
,COALESCE(n.c2_price,'[]'::jsonb) c2_price
,b.c3_vol
,COALESCE(b.c3_price,'[]'::jsonb) c3_price
,COALESCE(n.listcode,b.listcode)||'.'||COALESCE(n.stlc,b.stlc)||'.'||COALESCE(n.uomp, b.uomp)||'.'||COALESCE(n.branding,b.branding,'')||'.'||COALESCE(n.accs,b.accs,'')||'.'||COALESCE(n.suff,b.suff,'') item
FROM
no_bulk n
FULL OUTER JOIN only_bulk b ON
b.listcode = n.listcode
AND b.stlc = n.stlc
AND b.coltier = n.coltier
AND COALESCE(b.branding,'') = COALESCE(n.branding,'')
AND COALESCE(b.accs ,'') = COALESCE(n.accs ,'')
AND COALESCE(b.suff ,'') = COALESCE(n.suff ,'')
)
,test_multiprice AS (
--should return no rows
SELECT * FROM stage4 where c1_price->>1 IS NOT NULL OR c2_price->>1 IS NOT NULL OR c3_price->>1 IS NOT NULL
)
--SELECT * FROM test_multiprice
--SELECT * FROM stage4 ORDER BY item ASC
------group to the price level and aggregate attributes----------------------
,stage5 AS (
SELECT
b.listcode
,b.stlc
,jsonb_agg(DISTINCT b.coltier) coltier
,b.uomp
,jsonb_agg(DISTINCT b.branding ORDER BY b.branding DESC) branding
,jsonb_agg(DISTINCT b.accs ORDER BY b.accs DESC) accs
,jsonb_agg(DISTINCT b.suff ORDER BY b.suff DESC) suff
,jsonb_arr_aggcd(b.colors_array) colors_array
,jsonb_arr_aggcd(b.c1_vol) c1_vol
,b.c1_price
,jsonb_arr_aggcd(b.c2_vol) c2_vol
,b.c2_price
,jsonb_arr_aggcd(b.c3_vol) c3_vol
,b.c3_price
--,b.item
,b.c1_price||b.c2_price||b.c3_price pgroup
--,jsonb_build_object('pack',c1_price,'mp',c2_price,'bulk',c3_price) pgroupo
--how to sort 3 items, one of which may be null
--start by attempting to evaluate non-null columns
FROM
stage4 b
GROUP BY
b.listcode
,b.stlc
,b.uomp
--,b.branding
--,b.accs
--,b.suff
,b.c1_price
,b.c2_price
,b.c3_price
--,b.item
ORDER BY
stlc ASC
)
--SELECT * FROM stage5 --where c3_price->>1 IS NOT NULL
SELECT
b.listcode
,b.stlc
--,b.coltier
,public.jsonb_array_string_agg(b.coltier,', ') colortiers
,b.uomp
,public.jsonb_array_string_agg(b.branding,', ') branding
,public.jsonb_array_string_agg(b.accs,', ') accs
,public.jsonb_array_string_agg(b.suff,', ') suff
,public.jsonb_array_string_agg(b.colors_array,', ') colors
,public.jsonb_array_string_agg(b.c1_vol,' | ') pack_qty
,to_char((b.c1_price->>0)::numeric,'FM999,999.00') pack_price
,public.jsonb_array_string_agg(b.c2_vol,', | ') mp_qty
,to_char((b.c2_price->>0)::numeric,'FM999,999.00') mp_price
,public.jsonb_array_string_agg(b.c3_vol,' | ') bulk_qty
,to_char((b.c3_price->>0)::numeric,'FM999,999.00') bulk_price
,b.listcode||'-'||b.stlc||'-'||b.uomp||'-'||row_number() OVER (PARTITION BY b.listcode, b.stlc, b.uomp ORDER BY b.pgroup ASC) seq
,m.descr
,m.priceg
,m.image_url
,m.compatible
,m.header
,m.header_sort::int
,m.detail_sort::int
FROM
stage5 b
LEFT OUTER JOIN rlarp.molds m ON
m.stlc = b.stlc
ORDER BY
b.stlc,
b.pgroup;
--if "colors" column is null there are no part numbers available for this price point
END
$function$;

445
plcore_seq_comp.pg.sql Normal file
View File

@ -0,0 +1,445 @@
DROP TABLE IF EXISTS stage3;
CREATE TEMP TABLE IF NOT EXISTS stage3 AS (
WITH
--------------------------helper table to choose column based on uom-----------------------------------------------------------------
pivot_guide AS (
SELECT * FROM ( VALUES
('BDL','BDL','01'),
('BDL','PLT','02'),
('SLV','SLV','01'),
('SLV','PLT','02'),
('CSE','CSE','01'),
('CSE','PLT','02'),
('PLT','PLT','03')
) x(uomp,vol_uom,col)
-------------------------------------------------------------------------------------------------------------------------------------
)
,stage1 AS (
SELECT
p.listcode
,p.stlc
,p.listcode||'-'||p.stlc||'.'||COALESCE(p.coltier,'')||'.'||COALESCE(p.branding,'')||'.'||COALESCE(p.accs,'')||'.'||COALESCE(p.suff,'')||'.'||COALESCE(p.uomp)||'-'||g.col pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,g.col
FROM
rlarp.plcore p
LEFT OUTER JOIN pivot_guide g ON
g.uomp = p.uomp
AND g.vol_uom = p.vol_uom
WHERE
p.vol_price IS NOT NULL
--AND listcode = 'GUAU'
)
-----run this to check for duplicate rows-------------------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage1) x where dup > 1
-------------------------------------------------------------------------------------------------------------------------------------
,stage2 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
,i.item
--every part is linked so all the colors are already here
,i.colc
,pkey1||'-'||substring(i.item,13,3) pkey2
--look at this position to identify potential pack sizes without doing the conversion on every item
,substring(i.item,13,3) nc
,row_number() OVER (PARTITION BY pkey1, substring(i.item,13,3)) rn
FROM
stage1 p
LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = p.stlc
AND i.coltier = p.coltier
AND COALESCE(p.branding,'') = COALESCE(i.branding,'')
AND COALESCE(p.accs,'') = COALESCE(i.accs_ps,'')
AND COALESCE(p.suff,'') = COALESCE(i.suffix,'')
AND i.uomp = p.uomp
AND i.aplnt <> 'I'
)
--SELECT * FROM stage2 order by pkey1
---------------see how many potnential pack sizes there are per price point-------------------------------------------------
--SELECT * FROM (SELECT *, count(*) OVER (PARTITION BY pkey1) dup FROM stage2 WHERE rn = 1) X WHERE dup > 1 ORDER BY pkey2 ASC
,items AS (
SELECT
jsonb_agg( DISTINCT
jsonb_build_object(
'partn', item
,'fu', vol_uom
,'tu','PC'
)
) conv
FROM
stage2
WHERE
rn = 1
)
--do the conversion-------------------------------------------------------------
, conversion AS (
SELECT
rslt.p
,rslt.f
,rslt.t
,rslt.nm
,rslt.dm
FROM
items
JOIN LATERAL rlarp.uom_array(conv) rslt ON true
)
--SELECT * FROM CONVERSION
,stage3 AS (
SELECT
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
--,p.item
--,p.pkey2
--,string_agg(DISTINCT item, ', ') items
,jsonb_agg(DISTINCT p.colc) FILTER (WHERE p.colc IS NOT NULL) colors --this array will have to be aggregated again when columns pivot out
--,string_agg(DISTINCT to_char(c.nm/c.dm,'FM999,999'),' | ') FILTER(WHERE rn = 1) price_qty
,jsonb_agg(DISTINCT to_char(c.nm/c.dm,'FM999,999')) FILTER(WHERE rn = 1 AND c.nm IS NOT NULL) price_qty
FROM
stage2 p
LEFT OUTER JOIN conversion c ON
c.p = p.item
AND c.f = p.vol_uom
WHERE
TRUE
--this filter shoudl not be used since every color is linked in stage 2 exists on rn <> 1
--AND rn = 1
GROUP BY
p.listcode
,p.stlc
,p.pkey1
,p.coltier
,p.branding
,p.accs
,p.suff
,p.uomp
,p.vol_uom
,p.vol_qty
,p.vol_price
,p.orig_col
,p.col
)
SELECT * FROM stage3
) WITH DATA;
--select * from stage3
WITH
no_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) colors
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '01' AND b.price_qty IS NOT NULL) c1_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '01') c1_price
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '02' AND b.price_qty IS NOT NULL) c2_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '02') c2_price
FROM
stage3 b
WHERE
b.col <> '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM no_bulk
,only_bulk AS (
SELECT
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
--,pkey1
,b.uomp
,jsonb_arr_aggcd(b.colors) FILTER (WHERE b.colors IS NOT NULL) colors
,jsonb_arr_aggcd(b.price_qty) FILTER (WHERE b.col = '03' AND b.price_qty IS NOT NULL) c3_vol
,jsonb_agg(DISTINCT b.vol_price) FILTER (WHERE b.col = '03') c3_price
FROM
stage3 b
WHERE
b.col = '03'
GROUP BY
b.listcode
,b.stlc
,b.coltier
,b.branding
,b.accs
,b.suff
,b.uomp
)
--SELECT * FROM only_bulk
--SELECT * FROM only_bulk where c3_price->>1 IS NOT NULL
--SELECT * FROM no_bulk where c1_price->>1 IS NOT NULL
----full outer join in case there are bulk prices with no case pricing----
,stage4 AS (
SELECT
COALESCE(n.listcode,b.listcode) listcode
,COALESCE(n.stlc,b.stlc) stlc
,COALESCE(n.coltier,b.coltier) coltier
,COALESCE(n.branding,b.branding,'') branding
,COALESCE(n.accs,b.accs,'') accs
,COALESCE(n.suff,b.suff,'') suff
,COALESCE(n.uomp, b.uomp) uomp
,public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb)) colors_array
--,(SELECT string_agg(ae,', ') FROM (SELECT ae FROM jsonb_array_elements_text(public.jsonb_concat_distinct_arr(COALESCE(n.colors,'[]'::jsonb),COALESCE(b.colors,'[]'::jsonb))) ae) x) colors
,n.c1_vol
---there better not be multiple prices---
,COALESCE(n.c1_price,'[]'::jsonb) c1_price
,n.c2_vol
,COALESCE(n.c2_price,'[]'::jsonb) c2_price
,b.c3_vol
,COALESCE(b.c3_price,'[]'::jsonb) c3_price
,COALESCE(n.listcode,b.listcode)||'.'||COALESCE(n.stlc,b.stlc)||'.'||COALESCE(n.uomp, b.uomp)||'.'||COALESCE(n.branding,b.branding,'')||'.'||COALESCE(n.accs,b.accs,'')||'.'||COALESCE(n.suff,b.suff,'') item
FROM
no_bulk n
FULL OUTER JOIN only_bulk b ON
b.listcode = n.listcode
AND b.stlc = n.stlc
AND b.coltier = n.coltier
AND COALESCE(b.branding,'') = COALESCE(n.branding,'')
AND COALESCE(b.accs ,'') = COALESCE(n.accs ,'')
AND COALESCE(b.suff ,'') = COALESCE(n.suff ,'')
)
,test_multiprice AS (
--should return no rows
SELECT * FROM stage4 where c1_price->>1 IS NOT NULL OR c2_price->>1 IS NOT NULL OR c3_price->>1 IS NOT NULL
)
--SELECT * FROM test_multiprice
--SELECT * FROM stage4 ORDER BY item ASC
------group to the price level and aggregate attributes----------------------
,stage5 AS (
SELECT
b.listcode
,b.stlc
,jsonb_agg(DISTINCT b.coltier) coltier
,b.uomp
,jsonb_agg(DISTINCT b.branding ORDER BY b.branding DESC) branding
,jsonb_agg(DISTINCT b.accs ORDER BY b.accs DESC) accs
,jsonb_agg(DISTINCT b.suff ORDER BY b.suff DESC) suff
,jsonb_arr_aggcd(b.colors_array) colors_array
,jsonb_arr_aggcd(b.c1_vol) c1_vol
,b.c1_price
,jsonb_arr_aggcd(b.c2_vol) c2_vol
,b.c2_price
,jsonb_arr_aggcd(b.c3_vol) c3_vol
,b.c3_price
--,b.item
,b.c1_price||b.c2_price||b.c3_price pgroup
--,jsonb_build_object('pack',c1_price,'mp',c2_price,'bulk',c3_price) pgroupo
--how to sort 3 items, one of which may be null
--start by attempting to evaluate non-null columns
FROM
stage4 b
GROUP BY
b.listcode
,b.stlc
,b.uomp
--,b.branding
--,b.accs
--,b.suff
,b.c1_price
,b.c2_price
,b.c3_price
--,b.item
ORDER BY
stlc ASC
)
--SELECT * FROM stage5
,dsort AS (
SELECT
m."header"
,m.header_sort::int header_sort
,m.detail_sort::int detail_sort
,m.stlc
,f.flag
,COALESCE(rstt.rn,0) comp_sort
,COALESCE(rstt.c,m.stlc) stlcc
,mj.descr
--,mj."header"
FROM
rlarp.molds m
CROSS JOIN ( VALUES
('base'),
('compatible')
) AS f(flag)
LEFT JOIN LATERAL regexp_split_to_table(m.compatible,', ') WITH ORDINALITY rstt(c, rn) ON
f.flag = 'compatible'
LEFT OUTER JOIN rlarp.molds mj ON
mj.stlc = COALESCE(rstt.c,m.stlc)
WHERE
true
AND NOT (f.flag = 'compatible' AND COALESCE(rstt.rn,0) = 0)
AND NOT (f.flag = 'compatible' AND COALESCE(m.header,'') = '')
AND m.status <> 'I'
--only list active compatible items
AND mj.status <> 'I'
--exclude compatible items that have their own price list header
AND NOT (f.flag = 'compatible' AND mj."header" IS NOT NULL)
ORDER BY
m.header_sort
,m.detail_sort
,comp_sort
)
--SELECT * FROM dsort where stlc = 'SPH1DPI0'
--SELECT * FROM dsort WHERE header = 'Standard Inserts'
--SELECT * FROM stage5 --where c3_price->>1 IS NOT NULL
,stage6 AS (
SELECT
s."header"
,s.header_sort
,s.detail_sort
,s.comp_sort
,b.listcode
,s.flag
,b.stlc
,s.descr
--,b.coltier
,public.jsonb_array_string_agg(b.coltier,', ') colortiers
,b.uomp
,public.jsonb_array_string_agg(b.branding,', ') branding
,public.jsonb_array_string_agg(b.accs,', ') accs
,public.jsonb_array_string_agg(b.suff,', ') suff
,public.jsonb_array_string_agg(b.colors_array,', ') colors
,public.jsonb_array_string_agg(b.c1_vol,' | ') pack_qty
,to_char((b.c1_price->>0)::numeric,'FM999,999.00') pack_price
,public.jsonb_array_string_agg(b.c2_vol,', | ') mp_qty
,to_char((b.c2_price->>0)::numeric,'FM999,999.00') mp_price
,public.jsonb_array_string_agg(b.c3_vol,' | ') bulk_qty
,to_char((b.c3_price->>0)::numeric,'FM999,999.00') bulk_price
,row_number() OVER (PARTITION BY b.listcode, s.stlc, s.comp_sort ORDER BY b.pgroup ASC) seq
FROM
dsort s
INNER JOIN stage5 b ON
b.stlc = s.stlcc
WHERE
TRUE
--AND listcode = 'GUAU'
--exclude price points that don't have any part numbers otherwise you may have sequences above
--that are not real
AND b.colors_array IS NOT NULL
--ORDER BY
-- s.header_sort
-- ,s.detail_sort
-- ,s.comp_sort
--if "colors" column is null there are no part numbers available for this price point
)
--SELECT * FROM stage6 WHERE detail_sort = 1
,get_header AS (
SELECT DISTINCT
header
,header_sort
,0 detail_sort
,0 comp_sort
,listcode
,'header' flag
,header
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,null::text
,0 seq
FROM
stage6
WHERE
header IS NOT NULL
)
,stage7 AS (
SELECT
*
FROM
(
SELECT * FROM stage6 WHERE
TRUE
--only list the first price option for compatible products
--AND NOT (flag = 'compatible' AND seq <> 1)
UNION ALL
SELECT * FROM get_header
) x
ORDER BY
header_sort ASC
,detail_sort ASC
,comp_sort ASC
,seq ASC
)
SELECT
s.header
,s.header_sort
,s.detail_sort
,s.comp_sort
,s.seq
,s.listcode
,s.flag
,s.stlc
,s.descr
,s.colortiers
,s.uomp
,s.branding
,s.accs
,s.suff
,s.colors
,s.pack_qty
,CASE WHEN s.pack_qty IS NULL THEN null::text ELSE s.pack_price END
,s.mp_qty
,CASE WHEN s.mp_qty IS NULL THEN null::text ELSE s.mp_price END
,s.bulk_qty
,CASE WHEN s.bulk_qty IS NULL THEN null::text ELSE s.bulk_price END
FROM
stage7 s
WHERE
TRUE
AND s.listcode = 'GUAU'
--only list the first price point for compatible items
AND NOT (s.flag = 'compatible' AND s.seq <> 1)
AND header IS NOT NULL

18
plcore_table.ms.sql Normal file
View File

@ -0,0 +1,18 @@
DROP TABLE /*IF EXISTS*/ rlarp.plcore;
CREATE TABLE rlarp.plcore (
stlc varchar(8)
,coltier varchar(1)
,branding varchar(1)
,accs varchar(5)
,suff varchar(5)
,uomp varchar(5)
,vol_uom varchar(5)
,vol_qty numeric(18,5)
,vol_price numeric(18,5)
,listcode varchar(5)
,orig_row numeric(18,5)
,orig_col numeric(18,5)
);
--CREATE UNIQUE INDEX plcore_pk ON rlarp.plcore (stlc, coltier, branding, accs, suff, uomp, listcode);

18
plcore_table.pg.sql Normal file
View File

@ -0,0 +1,18 @@
DROP TABLE IF EXISTS rlarp.plcore;
CREATE TABLE rlarp.plcore (
stlc text
,coltier text
,branding text
,accs text
,suff text
,uomp text
,vol_uom text
,vol_qty numeric
,vol_price numeric
,listcode text
,orig_row numeric
,orig_col numeric
);
--CREATE UNIQUE INDEX plcore_pk ON rlarp.plcore (stlc, coltier, branding, accs, suff, uomp, listcode);

51
plm.db2.sql Normal file
View File

@ -0,0 +1,51 @@
DROP VIEW rlarp.plm;
CREATE VIEW rlarp.plm AS
WITH
------------------parse description lines into columns-------------------
plm AS (
SELECT
japlcd plcode
,substr(japld1,1,1) func
,substr(japld1,10,1) basis
,substr(japld1,3,2) tier
,substr(japld2,1,3) currency
,substr(japld1,6,3) country
,substr(japld1,12,5) parent
,min(jbfdat) fdat
,max(jbtdat) tdat
--,listagg(distinct bvcurr,',') curr
--,listagg(distinct bvctry,',') ctry
,p.JAPLDS D1
,p.JAPLD1 D2
,p.JAPLD2 D3
FROM
lgdat.iprca p
LEFT OUTER JOIN "CMS.CUSLG".iprcbhc i ON
japlcd = i.jbplcd
--INNER JOIN lgdat.cust c ON
-- bvfut14 = i.jbplvl
WHERE
1=1
--only include regional, catalog, and programs
--substr(japld1,10,1) IN ('R','C','P')
--or any other applicable price list, but they shoudl have already been defined so this is somewhat redundant
--OR (
-- DATERANGE('2020-05-31','2021-06-01') && DATERANGE(jbfdat,jbtdat)
-- AND bvstat = 'A'
--)
GROUP BY
japlcd
,substr(japld1,1,1)
,substr(japld1,3,2)
,substr(japld2,5,3)
,substr(japld1,6,3)
,substr(japld1,10,1)
,substr(japld1,12,5)
,p.JAPLDS
,p.JAPLD1
,p.JAPLD2
)
SELECT * FROM plm
GRANT SELECT ON rlarp.plm to public;

45
plm.pg.sql Normal file
View File

@ -0,0 +1,45 @@
DROP VIEW IF EXISTS rlarp.plm CASCADE;
CREATE OR REPLACE VIEW rlarp.plm AS
SELECT
p.japlcd AS plcode
,substr(p.japld1, 1, 1) AS func
,substr(p.japld1, 10, 1) AS basis
,substr(p.japld1, 3, 2) AS tier
,substr(p.japld2, 1, 3) AS currency
--substr(p.japld2, 5, 3) AS des_currency,
--round(substr(p.japld2, 9, 7)::numeric(16,5) / 100000::numeric,5) AS fx,
--substr(p.japld2, 9, 7) fx,
,substr(p.japld1, 6, 3) AS country
,substr(p.japld1, 12, 5) AS parent
,min(i.jbfdat) AS fdat
,max(i.jbtdat) AS tdat
--jsonb_agg(DISTINCT c.bvcurr) AS curr,
--jsonb_agg(DISTINCT c.bvctry) AS ctry,
--jsonb_agg(DISTINCT bvname) cust
,p.JAPLDS D1
,p.JAPLD1 D2
,p.JAPLD2 D3
FROM
lgdat.iprca p
LEFT OUTER JOIN "CMS.CUSLG".iprcbhc i ON
p.japlcd = i.jbplcd
WHERE
true
--include any price list that is flagged with R,C,P in position 10 of descr line 2
--or simply any price list that is considered currently active in IPRCBHC
--substr(p.japld1, 10, 1) = ANY (ARRAY['R'::text, 'C'::text, 'P'::text])
--OR daterange('2019-06-01'::date, '2021-06-01'::date) && daterange(i.jbfdat, i.jbtdat)
GROUP BY
p.japlcd
,(substr(p.japld1, 1, 1))
,(substr(p.japld1, 3, 2))
,(substr(p.japld2, 5, 3))
,(substr(p.japld1, 6, 3))
,(substr(p.japld1, 10, 1))
,(substr(p.japld1, 12, 5))
,p.JAPLDS
,p.JAPLD1
,p.JAPLD2;
GRANT SELECT ON rlarp.plm TO public;

102
pricelevel.frm Normal file
View File

@ -0,0 +1,102 @@
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} pricelevel
Caption = "Build Customer Price List"
ClientHeight = 9975.001
ClientLeft = 120
ClientTop = 465
ClientWidth = 10620
OleObjectBlob = "pricelevel.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "pricelevel"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public x As New TheBigOne
Public cancel As Boolean
Private Sub cbCancel_Click()
cancel = True
Me.Hide
End Sub
Private Sub cbFolder_Click()
Dim fd As Object
'--------Open file-------------
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Show
tbPATH.text = fd.SelectedItems(1)
End Sub
Private Sub cbNUMERIC_Click()
End Sub
Private Sub cbOK_Click()
cancel = False
Me.Hide
End Sub
Private Sub lbPriceLev_Click()
Dim i As Long
For i = 1 To lbPriceLev.ListCount
If lbPriceLev.Selected(i) Then
tbPriceLev.text = lbPriceLev.list(i, 0)
Exit For
End If
Next i
End Sub
Private Sub UserForm_Activate()
End Sub
Private Sub UserForm_Initialize()
Me.cancel = True
End Sub
Sub repopulate()
Dim i As Long
Dim j As Long
Dim numRows As Long
Dim numCols As Long
Dim colWidths() As Long
Dim lbColumnWidths As String
Dim pl() As String
pl = x.SHTp_Get("Price Levels", 3, 1, True)
Call x.TBLp_FilterSingle(pl, 3, 0, False)
Me.lbPriceLev.list = x.TBLp_StringToVar(x.TBLp_Transpose(pl))
For i = 1 To lbPriceLev.ListCount - 1
If lbPriceLev.list(i, 0) = Selection Then
lbPriceLev.Selected(i) = True
Me.tbPriceLev = Selection
Exit For
End If
Next i
tbEddDate.text = Format(Date, "mm/dd/yyyy")
End Sub

BIN
pricelevel.frx Normal file

Binary file not shown.

200
pricelist.frm Normal file
View File

@ -0,0 +1,200 @@
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} pricelist
Caption = "Price List Name"
ClientHeight = 7995
ClientLeft = 120
ClientTop = 465
ClientWidth = 11865
OleObjectBlob = "pricelist.frx":0000
StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "pricelist"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public proceed As Boolean
Private pl() As String
Private plv() As Variant
Private plfv() As Variant
Public tbo As New TheBigOne
Private Sub bCANCEL_Click()
proceed = False
Me.Hide
End Sub
Private Sub bOK_Click()
If tbPATH = "" Then
MsgBox ("no directory specified")
Exit Sub
End If
proceed = True
Me.Hide
End Sub
Private Sub bPICK_Click()
Dim fd As Object
'--------Open file-------------
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Show
tbPATH.text = fd.SelectedItems(1)
End Sub
Private Sub cbInactive_Click()
End Sub
Private Sub cbLIST_Change()
Dim plc() As String
plc = pl
Call tbo.TBLp_FilterSingle(plc, 0, cbLIST.value, True)
If UBound(plc, 2) = 0 Then Exit Sub
Me.tbD1 = plc(1, 1)
Me.tbD2 = plc(2, 1)
Me.tbD3 = plc(3, 1)
End Sub
Private Sub lbLIST_Click()
Dim i As Long
For i = 1 To lbLIST.ListCount
If lbLIST.Selected(i) Then
cbLIST.value = lbLIST.list(i, 0)
Exit Sub
End If
Next i
Me.cbHDR.value = "3 - Update"
End Sub
Private Sub UserForm_Initialize()
proceed = False
Dim x() As Variant
Dim i As Long
ReDim x(3)
x(1) = "1 - New"
x(2) = "2 - Replace"
x(3) = "3 - Update"
Dim dtl() As Variant
ReDim dtl(3)
dtl(1) = "1 - Add"
dtl(2) = "2 - Update"
dtl(3) = "3 - Delete"
cbHDR.list = x
cbDTL.list = dtl
login.Caption = "CMS Login"
login.tbU = UCase(Mid(Mid(Application.UserLibraryPath, 10, InStr(10, Application.UserLibraryPath, "\") - 10), 1, 10))
login.tbP = ""
login.Show
If Not login.proceed Then Exit Sub
If Not tbo.ADOp_OpenCon(1, ISeries, "S7830956", False, login.tbU.text, login.tbP.text) Then
MsgBox (tbo.ADOo_errstring)
Exit Sub
End If
pl = tbo.ADOp_SelectS(1, "SELECT JAPLCD, JAPLDS, JAPLD1, JAPLD2 FROM lgdat.iprca WHERE TRIM(COALESCE(JAPLCD,'')) <> '' ORDER BY JAPLCD ASC", True, 1000, False)
'pl = FL.x.ADOp_SelectS(1, "SELECT plcode, d1,d2,d3 FROM RLARP.PLM p ORDER BY plcode", True, 1000, True)
Call tbo.ADOp_CloseCon(1)
ReDim plv(1 To UBound(pl, 2))
For i = 1 To UBound(pl, 2)
plv(i) = pl(0, i)
Next i
plfv = tbo.TBLp_StringToVar(tbo.TBLp_Transpose(pl))
cbLIST.list = plv
lbLIST.list = plfv
'lbHEAD.ColumnCount = lbHist.ColumnCount
'lbHEAD.ColumnWidths = lbHist.ColumnWidths
Call tbo.frmListBoxHeader(lbHEAD, lbLIST, "plcode", "descr1", "descr2", "descr3")
End Sub
Private Sub UserForm_Terminate()
proceed = False
End Sub
Sub load_lists()
Dim x() As Variant
Dim i As Long
ReDim x(3)
x(1) = "1 - New"
x(2) = "2 - Replace"
x(3) = "3 - Update"
Dim dtl() As Variant
ReDim dtl(3)
dtl(1) = "1 - Add"
dtl(2) = "2 - Update"
dtl(3) = "3 - Delete"
cbHDR.list = x
cbDTL.list = dtl
login.Caption = "CMS Login"
login.tbU = Mid(UCase(Mid(Application.UserLibraryPath, 10, InStr(10, Application.UserLibraryPath, "\") - 10)), 1, 10)
login.tbP = ""
login.Show
If Not login.proceed Then Exit Sub
If Not tbo.ADOp_OpenCon(1, ISeries, "S7830956", False, login.tbU.text, login.tbP.text) Then
MsgBox (tbo.ADOo_errstring)
Exit Sub
End If
'pl = FL.x.ADOp_SelectS(1, "SELECT plcode, d1, d2, d3 FROM RLARP.PLM p ORDER BY plcode", True, 1000, True)
pl = tbo.ADOp_SelectS(1, "SELECT JAPLCD, JAPLDS, JAPLD1, JAPLD2 FROM lgdat.iprca WHERE TRIM(COALESCE(JAPLCD,'')) <> '' ORDER BY JAPLCD ASC", True, 1000, False)
Call tbo.ADOp_CloseCon(1)
ReDim plv(1 To UBound(pl, 2))
For i = 1 To UBound(pl, 2)
plv(i) = pl(0, i)
Next i
plfv = tbo.TBLp_StringToVar(tbo.TBLp_Transpose(pl))
cbLIST.list = plv
lbLIST.list = plfv
'lbHEAD.ColumnCount = lbHist.ColumnCount
'lbHEAD.ColumnWidths = lbHist.ColumnWidths
Call tbo.frmListBoxHeader(lbHEAD, lbLIST, "plcode", "d1", "d2", "d3")
End Sub

BIN
pricelist.frx Normal file

Binary file not shown.

View File

@ -0,0 +1,22 @@
SELECT
c.JCPLCD
,a.JAPLDS
,c.JCPART
,i.DESCR
,i.STLC
,i.COLTIER
,i.BRANDING
,i.ACCS_PS
,i.SUFFIX
,i.UOMP
,c.JCUNIT
,c.JCVOLL
,c.JCPRIC
FROM
LGDAT.IPRCC c
INNER JOIN LGDAT.IPRCA a ON
a.JAPLCD = c.JCPLCD
LEFT OUTER JOIN "CMS.CUSLG".ITEMM i ON
i.ITEM = c.JCPART
WHERE
c.JCPLCD = 'NUWU'

27
sachdef.pg.sql Normal file
View File

@ -0,0 +1,27 @@
CREATE VIEW rlarp.sachdef AS
WITH
sach_def AS (
SELECT
s.BK7CODE plev,
s.BK7DES1 d2,
s.BK7DES2 d3,
b.JBPLCD plcd,
b.JBFDAT fdate,
b.JBTDAT tdate,
d.func,
d.tier,
d.currency,
--d.des_currency,
--d.fx,
d.country,
d.basis,
d.parent
FROM
lgdat.sach s
INNER JOIN "CMS.CUSLG".IPRCBHC b ON
b.JBPLVL = s.BK7CODE
INNER JOIN rlarp.plm d ON
d.plcode = b.JBPLCD
)
SELECT * FROM sach_def

37
shipto.pg.sql Normal file
View File

@ -0,0 +1,37 @@
WITH
--ship customer list
scl AS (
SELECT DISTINCT
bill_cust
,ship_cust
FROM
rlarp.osm
WHERE
oseas >= 2021
AND calc_status <> 'CANCELED'
AND version = 'ACTUALS'
AND ship_cust <> ''
AND part <> ''
AND gldc <> 'SAM'
AND substring(glec,1,1) <= '2'
)
,rslt AS (
SELECT
scl.bill_cust
,bc.cclass bill_class
,CASE WHEN bc.dba = '' THEN bc.descr ELSE bc.dba END bill_descr
,bc.ctype bill_type
,bc.pricing bill_pricing
,scl.ship_cust
,bc.cclass ship_class
,CASE WHEN sc.dba = '' THEN sc.descr ELSE sc.dba END ship_descr
,sc.ctype ship_type
,sc.pricing ship_pricing
FROM
scl
LEFT OUTER JOIN rlarp.cust bc ON
bc.code = scl.bill_cust
LEFT OUTER JOIN rlarp.cust sc ON
sc.code = scl.ship_cust
)
SELECT *, CASE WHEN bill_cust = ship_cust THEN 'same' ELSE '' END flag FROM rslt --WHERE bill_cust <> ship_cust AND ship_pricing <> ''