Advertisement
Pandaaaa906

Untitled

Apr 21st, 2023
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with RECURSIVE prd_catalog as (
  2.  
  3.     SELECT
  4.     id,
  5.     pid,
  6.     cn_name,
  7.     en_name,
  8.     handler
  9.     FROM public.t_product_catalog t
  10.     where t.en_name = 'cato'
  11.  
  12.     UNION ALL
  13.  
  14.     SELECT
  15.     t.id,
  16.     t.pid,
  17.     t.cn_name,
  18.     t.en_name,
  19.     COALESCE(t.handler, prd_catalog.handler) "handler"
  20.     FROM public.t_product_catalog t
  21.     INNER JOIN prd_catalog
  22.     ON prd_catalog.id = t.pid
  23.  
  24. ), initial_wa as (
  25.     select
  26.     "VoucherId"
  27.     from "WFAudit" wa
  28.     where wa."VoucherType" = N'MO21'
  29.     --  AND wa.OperationDate >= cast(? / 1000 as datetime)
  30.     ), processed_wa as (
  31.  
  32.     SELECT
  33.     "VoucherId",
  34.     "VoucherType",
  35.     "VoucherCode",
  36.     "Action",
  37.     "OperatorName",
  38.     "OperationDate",
  39.     row_number() over(partition by "VoucherId" order by "OperationDate" asc) action_r
  40.  
  41.     from "WFAudit" wa
  42.     where wa."VoucherType" = N'MO21'
  43.     AND wa."VoucherId" in (select initial_wa."VoucherId" from initial_wa)
  44.  
  45. ), filtered_wa as (
  46.     -- 后面有5都不要
  47.     SELECT
  48.     t1.*
  49.     FROM processed_wa t1
  50.  
  51.     where not EXISTS (
  52.     SELECT
  53.     1 as ignored
  54.     FROM processed_wa t2
  55.     where t2."VoucherId" = t1."VoucherId"
  56.     and t2."Action" = 5
  57.     and t2.action_r >= t1.action_r
  58.     )
  59.  
  60. ), first_audit as(
  61.     -- Action
  62.     -- 0 提交
  63.     -- 1 同意
  64.     -- 7 弃审
  65.     -- 5 撤销
  66.     SELECT
  67.     *
  68.     FROM (
  69.         SELECT
  70.         filtered_wa.*,
  71.         case when filtered_wa."VoucherId" is null then 0
  72.         else sum(case when filtered_wa."Action" = 7 then 1 else 0 end) over(partition by filtered_wa."VoucherId") end n_canceled,  -- 取消次数
  73.         case when filtered_wa."VoucherId" is null then 0
  74.         else row_number() over(partition by filtered_wa."VoucherId", filtered_wa."Action" order by filtered_wa."OperationDate" asc) end nth_submit  -- 第几次审核
  75.  
  76.         from filtered_wa
  77.     ) tmp
  78.     WHERE tmp."Action" = 1
  79.     AND nth_submit = n_canceled + 1  -- 初审记录
  80. ), raw_material_summary as (
  81.     SELECT
  82.     *
  83.     FROM (
  84.         SELECT
  85.  
  86.         rd10_d."cInvCode",
  87.         rd10_d."cBatch",
  88.         mom_d."MoDId",
  89.         rd11_d."cInvCode" "raw_cInvCode",
  90.         rd11_d."cBatch" "raw_cBatch",
  91.         rd11_d."iQuantity" "raw_iQuantity",
  92.         row_number() over(partition by rd10_d."cInvCode", rd10_d."cBatch" ORDER BY rd11_d."AutoID" desc) r
  93.         FROM "rdrecords10" rd10_d
  94.  
  95.         LEFT JOIN "mom_orderdetail" mom_d
  96.         ON mom_d."MoDId" = rd10_d."iMPoIds"
  97.  
  98.         LEFT JOIN "mom_moallocate" mom_moa
  99.         ON mom_d."MoDId" = mom_moa."MoDId"
  100.  
  101.         LEFT JOIN "rdrecords11" rd11_d
  102.         ON mom_moa."AllocateId" = rd11_d."iMPoIds"
  103.     ) tmp
  104.     where tmp.r = 1
  105. ), po_summary as (
  106.     -- 采购入库明细
  107.     SELECT
  108.     *
  109.     FROM (
  110.         SELECT
  111.         app_m."cMaker" app_cmaker,
  112.         po_m."cMaker" po_cmaker,
  113.         po_m."cPOID" po_cpoid,
  114.         po_m."cAuditTime" "po_cAuditTime",
  115.         po_d."cDefine30" "po_cDefine30",
  116.         ven."cVenName" "po_cVenName",
  117.         rd01_m."dDate" "rd01_dDate",
  118.         rd01_m."cVenCode",
  119.         rd01_d."cInvCode" "rd01_cInvCode",
  120.         rd01_d."cBatch" "rd01_cBatch",
  121.         rd01_d."iQuantity" "rd01_iQuantity",
  122.         rd01_d."iSum" "rd01_iSum",
  123.         rd01_d."iSum" / rd01_d."iQuantity" unit_cost,
  124.         row_number() over(partition by rd01_d."cInvCode", rd01_d."cBatch" ORDER BY rd01_d."AutoID" desc) r,
  125.         count(*) over(partition by rd01_d."cInvCode", rd01_d."cBatch") c
  126.         FROM rdrecords01 rd01_d
  127.  
  128.         LEFT JOIN "RdRecord01" rd01_m
  129.         ON rd01_m."ID" = rd01_d."ID"
  130.  
  131.         LEFT JOIN "Vendor" ven
  132.         ON ven."cVenCode" = rd01_m."cVenCode"
  133.  
  134.         LEFT JOIN "PO_Podetails" po_d
  135.         ON rd01_d."iPOsID" = po_d."ID"
  136.  
  137.         LEFT JOIN "PO_Pomain" po_m
  138.         ON po_d."POID" = po_m."POID"
  139.  
  140.         LEFT JOIN "PU_AppVouchs" app_d
  141.         ON app_d."AutoID" = po_d."iAppIds"
  142.  
  143.         LEFT JOIN "PU_AppVouch" app_m
  144.         ON app_m."ID" = app_d."ID"
  145.  
  146.         where rd01_m.dnverifytime is not null
  147.         AND rd01_d."iQuantity" > 0
  148.     ) tmp
  149.     where tmp.r = 1
  150. ), raw_base as (
  151.  
  152.     SELECT
  153.  
  154.     row_number() over(partition by po_summary."rd01_cInvCode" ORDER BY rd32_m."dDate" ASC, rd32_d."AutoID" ASC) rd32_r,  -- 关键,算rolling min
  155.     rd32_d.iorderdid "so_isosid",
  156.     rd32_d."iDLsID" "rd32_idlsid",
  157.     rd32_d."AutoID" "rd32_AutoID",
  158.     rd32_m."cCode" "rd32_cCode",
  159.     rd32_m."dDate" "rd32_dDate",
  160.     --  so_m.cSOCode,
  161.     inv."cInvCCode",
  162.     inv_cls."cInvCName",
  163.     rd32_d."cInvCode" "rd32_cInvCode",
  164.     rd32_d."cFree1" "rd32_cFree1",
  165.     rd32_d."iQuantity" "rd32_iQuantity",
  166.     --  so_d.iQuantity so_iQuantity,
  167.     --  so_d.iSum so_iSum,
  168.     --  so_d_ex.cbdefine38,
  169.     --  so_d_ex.cbdefine49,
  170.     --  so_d_ex.cbdefine58,
  171.     --  so_d_ex.cbdefine59,
  172.     raw_material_summary."MoDId",
  173.     raw_material_summary."raw_cInvCode",
  174.     raw_material_summary."raw_cBatch",
  175.     po_summary.*
  176.  
  177.     FROM rdrecords32 rd32_d
  178.  
  179.     LEFT JOIN rdrecord32 rd32_m
  180.     ON rd32_d."ID" = rd32_m."ID"
  181.  
  182.     LEFT JOIN "Inventory" inv
  183.     ON inv."cInvCode" = rd32_d."cInvCode"
  184.  
  185.     LEFT JOIN "InventoryClass" inv_cls
  186.     ON inv_cls."cInvCCode" = inv."cInvCCode"
  187.     --
  188.     --  LEFT JOIN SO_SODetails so_d
  189.     --  ON rd32_d.iorderdid = so_d.iSOsID
  190.     --
  191.     --  LEFT JOIN SO_SODetails_extradefine so_d_ex
  192.     --  ON so_d_ex.iSOsID = rd32_d.iorderdid
  193.     --
  194.     --  LEFT JOIN SO_SOMain so_m
  195.     --  ON so_d.ID = so_m.ID
  196.     --
  197.     --  LEFT JOIN DispatchLists disp_d
  198.     --  ON disp_d.iDLsID = rd32_d.iDLsID
  199.  
  200.     -- 使用什么原料批次
  201.     LEFT JOIN raw_material_summary
  202.     ON raw_material_summary."cInvCode" = rd32_d."cInvCode"
  203.     AND raw_material_summary."cBatch" = rd32_d."cBatch"
  204.  
  205.     -- TODO LEFT JOIN purchase time/vendor/cost
  206.     LEFT JOIN po_summary
  207.     ON raw_material_summary."raw_cInvCode" = po_summary."rd01_cInvCode"
  208.     AND raw_material_summary."raw_cBatch" = po_summary."rd01_cBatch"
  209.  
  210. ), rolling_min_unit_cost as (
  211. -- 关键
  212. -- 算出累计单价最低的供应商,以及最低单价
  213.     SELECT
  214.     raw_base."rd32_AutoID",
  215.     raw_base."rd01_cInvCode",
  216.     raw_base.rd32_r,
  217.     raw_base.unit_cost min_unit_cost,
  218.     raw_base."cVenCode" "min_cost_cVenCode"
  219.     FROM raw_base
  220.     WHERE raw_base."rd01_cInvCode" is not null
  221.     AND raw_base.rd32_r = 1
  222.  
  223.     UNION ALL
  224.  
  225.     SELECT
  226.     raw_base."rd32_AutoID",
  227.     raw_base."rd01_cInvCode",
  228.     raw_base.rd32_r,
  229.     case when raw_base.unit_cost < t.min_unit_cost then raw_base.unit_cost else t.min_unit_cost end min_unit_cost,
  230.     case when raw_base.unit_cost < t.min_unit_cost then raw_base."cVenCode" else t."min_cost_cVenCode" end "min_cost_cVenCode"
  231.     FROM raw_base
  232.     INNER JOIN  rolling_min_unit_cost t
  233.     ON raw_base."rd01_cInvCode" = t."rd01_cInvCode"
  234.     AND raw_base.rd32_r = t.rd32_r + 1
  235.  
  236. )
  237. -- SELECT * FROM rolling_min_unit_cost
  238. , base as (
  239.  
  240.     SELECT
  241.     raw_base.*,
  242.     case
  243.     when raw_base."cInvCName" != 'CATO' then null
  244.     when not exists(
  245.     SELECT 1 FROM raw_base t where t."rd32_cInvCode" = raw_base."rd32_cInvCode" and t."rd32_dDate" < raw_base."rd32_dDate"
  246.     ) then 1
  247.     else 0
  248.     end is_new_1,  -- 之前没出库过
  249.     CASE
  250.     when raw_base."cInvCName" != 'CATO' then null
  251.     when rolling_min_unit_cost."min_cost_cVenCode" != raw_base."cVenCode" and rolling_min_unit_cost.min_unit_cost > raw_base.unit_cost then 1  -- 关键
  252.     else 0
  253.     end is_new_2  -- 价格比以前低,且是新供应商
  254.     FROM raw_base
  255.    
  256.     LEFT JOIN rolling_min_unit_cost
  257.     ON rolling_min_unit_cost."rd32_AutoID" = raw_base."rd32_AutoID"
  258.  
  259. )
  260.  
  261. SELECT
  262. -- base.*,
  263. base.so_isosid "销售明细ID",
  264. base."rd32_AutoID" "销售出库明细ID",
  265. base."rd32_cCode" "销售出库单号",
  266. base."rd32_dDate" "销售出库日期",
  267. so_m."cSOCode" "销售单号",
  268. base."cInvCCode" "存货大类编码",
  269. base."cInvCName" "存货大类名称",
  270. base."rd32_cInvCode" "存货编码",
  271. base."rd32_cFree1" "规格",
  272. base."rd32_iQuantity" "出库数量",
  273. -- dbo.extract_numeric(base.rd32_cFree1),
  274. normalize_package(base."rd32_cFree1") * base."rd32_iQuantity" "出库数量(mg)",
  275. so_d."iQuantity" "销售数量",
  276. so_d."iSum" "销售含税小计",
  277. coalesce(so_d_ex.cbdefine38, prd_pkg.cost) "核算成本单价",
  278. so_d_ex."cbdefine49" "销售预计发货日期",
  279. so_d_ex."cbdefine59" "询价单明细ID",
  280. base."MoDId" "生产订单ID",
  281. base."raw_cInvCode" "原料编码",
  282. base."raw_cBatch" "原料批次",
  283. base."app_cmaker" 请购人,
  284. base."po_cmaker" 采购员,
  285. base."po_cpoid" 采购单号,
  286. base."po_cAuditTime" 采购单审核时间,
  287. base."po_cDefine30" 采购类型,
  288. base."po_cVenName" 采购单供应商名称,
  289. base."rd01_dDate" "原料采购入库日期",
  290. base."cVenCode" "原料采购供应商编号",
  291. base."rd01_cInvCode" "原料采购存货编码",
  292. base."rd01_cBatch" "原料采购批号",
  293. base."rd01_iQuantity" "原料采购入库数量",
  294. base."rd01_iSum" "原料采购入库含税合计",
  295. base.unit_cost "原料采购单价",
  296. is_new_1,
  297. is_new_2,
  298. first_audit."OperationDate" as 一审时间,
  299. case
  300. when cast(first_audit."OperationDate" as date) <= cast(so_d_ex.cbdefine49 as date) then '准时'
  301. when cast(first_audit."OperationDate" as date) > cast(so_d_ex.cbdefine49 as date) then '超时'
  302. else null
  303. end 是否准时,
  304. case
  305. when base."cInvCName" != 'CATO' then null
  306. when base.is_new_1 = 1 then 1
  307. when base.is_new_2 = 1 then 1
  308. when exists(
  309.     SELECT 1 FROM base t
  310.     WHERE 1=1
  311.     AND t."rd32_dDate" <= base."rd32_dDate"
  312.     AND t."rd32_dDate" >= (base."rd32_dDate" - interval '6month')
  313.     AND t.is_new_1 = 1 OR t.is_new_2 = 1
  314. ) then 1
  315. else 0
  316. end 是否新产品,
  317. coalesce(inq.offer_handler, prd_catalog.handler, prd.handler) 负责人,
  318. base.rd32_r
  319.  
  320. FROM base
  321.  
  322. LEFT JOIN "SO_SODetails" so_d
  323. ON base.so_isosid = so_d."iSOsID"
  324.  
  325. LEFT JOIN "SO_SODetails_extradefine" so_d_ex
  326. ON so_d_ex."iSOsID" = base.so_isosid
  327.  
  328. LEFT JOIN "SO_SOMain" so_m
  329. ON so_d."ID" = so_m."ID"
  330.  
  331. LEFT JOIN "DispatchLists" disp_d
  332. ON disp_d."iDLsID" = base."rd32_idlsid"
  333.  
  334. -- TODO join mom_first_audit from pmc
  335. LEFT JOIN first_audit
  336. ON first_audit."VoucherId" = base."MoDId"::text
  337. --
  338. -- TODO join inquiry get primary handler
  339. LEFT JOIN public.t_inq_sync_crm_detail inq_sync
  340. ON inq_sync.id::text = so_d_ex.cbdefine59
  341. --
  342. LEFT JOIN public.t_inq_order_detail inq
  343. ON inq.id = inq_sync.inq_order_detail_id
  344. --
  345. -- TODO join product get backup handler
  346. LEFT JOIN public.t_product prd
  347. ON prd.brand_name = 'cato'
  348. AND prd.is_delete = 1
  349. and prd.cat_no = base."rd32_cInvCode"
  350. --
  351. LEFT JOIN (
  352.     SELECT
  353.     *
  354.     FROM (
  355.         SELECT
  356.         *,
  357.         row_number() over(partition by prd_pkg.brand_name, prd_pkg.cat_no, lower(prd_pkg.package_value) ORDER BY coalesce(update_time, create_time) desc) r
  358.         FROM t_product_package prd_pkg
  359.         ) tmp
  360.         WHERE tmp.r = 1
  361. ) prd_pkg
  362. ON prd_pkg.brand_name = 'cato'
  363. AND prd_pkg.is_delete = 1
  364. AND prd_pkg.cat_no = base."rd32_cInvCode"
  365. AND lower(prd_pkg.package_value) = lower(base."rd32_cFree1")
  366. --
  367. LEFT JOIN prd_catalog
  368. ON prd.catalog_id = prd_catalog.id
  369. --
  370. ORDER BY base."rd32_dDate" desc, so_m."cSOCode"
  371. --
  372. --
  373. --
  374. --
  375.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement