Guest User

Untitled

a guest
Oct 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.51 KB | None | 0 0
  1. IF (@ship_id is not null)
  2. {
  3. [SELECT Top 1 sl.ordnum,
  4. sl.ship_id,
  5. isnull(st.stop_id, '') as stop_id,
  6. isnull(cm.car_move_id, '') as car_move_id,
  7. sl.client_id,
  8. sl.wh_id
  9. From shipment sh
  10. Inner
  11. join shipment_line sl
  12. on sl.ship_id = sh.ship_id
  13. left outer
  14. join stop st
  15. on st.stop_id = sh.stop_id
  16. left outer
  17. join car_move cm
  18. on cm.car_move_id = st.car_move_id
  19. Where @+sh.ship_id
  20. and sh.shpsts != 'B']
  21. }
  22. Else IF (@ordnum is not null AND @client_id is not null)
  23. {
  24. [SELECT Top 1 sl.ordnum,
  25. sl.ship_id,
  26. isnull(st.stop_id, '') as stop_id,
  27. isnull(cm.car_move_id, '') as car_move_id,
  28. sl.client_id,
  29. sl.wh_id
  30. From shipment sh
  31. Inner
  32. join shipment_line sl
  33. on sl.ship_id = sh.ship_id
  34. left outer
  35. join stop st
  36. on st.stop_id = sh.stop_id
  37. left outer
  38. join car_move cm
  39. on cm.car_move_id = st.car_move_id
  40. Where @+sl.ordnum
  41. AND @+sl.wh_id
  42. AND @+sl.client_id
  43. and sh.shpsts != 'B']
  44. }
  45. |
  46. IF (@ship_id is null)
  47. {
  48. [select '' as ordnum,
  49. '' as client_id,
  50. '' as wh_id,
  51. '' as bt_adr_id,
  52. '' as st_adr_id,
  53. '' as cponum,
  54. '' as cpodte,
  55. '' as vc_lbl_typ,
  56. '' as vc_lbl_frmt,
  57. '' as vc_mh10_supvnno,
  58. '' as custno,
  59. '' as st_adrnam,
  60. '' as st_attn_name '' as st_adr_ln1,
  61. '' as st_adr_ln2,
  62. '' as st_adr_ln3,
  63. '' as st_adrcty,
  64. '' as st_adrstc,
  65. '' as st_adrpsz,
  66. '' st_adrctry_name,
  67. '' as st_adrpsz,
  68. '' as ship_id,
  69. '' as client_name,
  70. '' as client_name,
  71. '' as carcod,
  72. '' as srvlvl,
  73. '' as late_shpdte,
  74. '' as late_dlvdte,
  75. '' as early_dlvdte,
  76. '' as entdte,
  77. '' orderheadermsg,
  78. '' as shiptomsg,
  79. '' as csterms,
  80. '' as salespersonmsg,
  81. '' as nottxt,
  82. '' as carnam,
  83. '' as bt_adrnam '' as bt_adrln1,
  84. '' as bt_adrln2,
  85. '' as bt_adrln3,
  86. '' as bt_adrcty,
  87. '' as bt_adrstc,
  88. '' as bt_adrpsz,
  89. '' as bt_adrctry_name,
  90. '' as bt_attn_name,
  91. '' as stclntcustno,
  92. '' as totcases,
  93. '' as pckqty]
  94. }
  95. Else IF (@ship_id is not null)
  96. {
  97. [select adrmst.adrnam client_name
  98. from client
  99. inner
  100. join adrmst
  101. on client.adr_id = adrmst.adr_id
  102. where client.client_id = @client_id]
  103. |
  104. [select nvl(stgdte, '01/01/1900') stgdte,
  105. late_shpdte,
  106. late_dlvdte,
  107. early_dlvdte,
  108. nvl(carcod, ' ') carcod,
  109. nvl(srvlvl, ' ') srvlvl
  110. from shipment
  111. where wh_id = @wh_id
  112. and ship_id = @ship_id]
  113. |
  114. [Select
  115. -- Ship To Address here --
  116. st_adrnam = MAX(stadrmst.adrnam),
  117. st_attn_name = MAX(stadrmst.attn_name),
  118. st_adrln1 = MAX(ISNULL(stadrmst.adrln1, '')),
  119. st_adrln2 = MAX(ISNULL(stadrmst.adrln2, '')),
  120. st_adrln3 = MAX(ISNULL(stadrmst.adrln3, '')),
  121. st_adrcty = MAX(stadrmst.adrcty),
  122. st_adrstc = MAX(stadrmst.adrstc),
  123. st_adrpsz = MAX(stadrmst.adrpsz),
  124. st_adrctry_name = MAX(stadrmst.ctry_name)
  125. From ord
  126. left outer
  127. Join adrmst stadrmst
  128. on ord.st_adr_id = stadrmst.adr_id
  129. where ord.ordnum = @ordnum
  130. and ord.client_id = @client_id
  131. and ord.wh_id = @wh_id]
  132. |
  133. [Select ad.host_ext_id,
  134. ad.adrnam bt_adrnam,
  135. ad.adrln1 bt_adrln1,
  136. ad.adrln2 bt_adrln2,
  137. ad.adrln3 bt_adrln3,
  138. ad.adrcty bt_adrcty,
  139. ad.adrstc bt_adrstc,
  140. ad.adrpsz bt_adrpsz,
  141. ad.ctry_name bt_adrctry_name,
  142. ad.attn_name bt_attn_name
  143. from ord
  144. inner
  145. join adrmst ad
  146. on ord.btcust = ad.adr_id
  147. Where ord.ordnum = @ordnum
  148. and ord.client_id = @client_id
  149. and ord.wh_id = @wh_id
  150. and ad.adr_id = ord.btcust]
  151. |
  152. [select nottxt
  153. from ord_note
  154. where ordnum = @ordnum
  155. and client_id = @client_id
  156. and nottyp = 'REFERENCENO'
  157. and rownum < 2] catch(-1403)
  158. |
  159. [select nottxt stclntcustno
  160. from ord_note
  161. where ordnum = @ordnum
  162. and client_id = @client_id
  163. and nottyp = 'STCLNTCUSTNO'
  164. and rownum < 2] catch(-1403)
  165. |
  166. [select carnam
  167. from carhdr
  168. where carcod = @carcod
  169. and rownum < 2] catch(-1403)
  170. |
  171. [select sum(pckqty) pckqty
  172. from ord_line
  173. where ordnum = @ordnum
  174. and client_id = @client_id
  175. and wh_id = @wh_id] catch(-1403)
  176. |
  177. [select sum(uc_sub.totcases) totcases
  178. from (SELECT count(distinct iss.subnum) totcases,
  179. -- 6 Total Pieces Shipped
  180. max(s.carcod) carcod,
  181. -- 10 Carrier
  182.  
  183. CASE WHEN UPPER(max(s.carcod)) = 'SKELPARCEL' THEN 0
  184. ELSE (SUM(isnull(sl.tot_pln_pal_qty, 0)))
  185. end totpal,
  186. CASE WHEN UPPER(max(s.carcod)) = 'SKELPARCEL' THEN 0
  187. ELSE ISNULL(COUNT(distinct iv.lodnum), 0)
  188. end totpallet,
  189. -- 7 Lift Count
  190. sum(isnull(sl.tot_pln_cube, 0)) / 1728 cube,
  191. -- 13 Cube
  192.  
  193. (SUM(ind.untqty / isnull(pdt.untqty, 1) * pdt.grswgt) * 0.02835) wgh
  194. --- Linst C
  195.  
  196. FROM ORD o
  197. INNER
  198. JOIN SHIPMENT_LINE sl
  199. on sl.ordnum = o.ordnum
  200. and sl.wh_id = o.wh_id
  201. and sl.client_id = o.client_id
  202. INNER
  203. JOIN SHIPMENT s
  204. on s.ship_id = sl.ship_id
  205. INNER
  206. JOIN invdtl ind
  207. on ind.ship_line_id = sl.ship_line_id
  208. INNER
  209. JOIN invsub iss
  210. on iss.subnum = ind.subnum
  211. INNER
  212. JOIN invlod iv
  213. on iv.lodnum = iss.lodnum
  214. INNER
  215. JOIN (SELECT MAX(ordnum) ordnum,
  216. prtnum,
  217. ftpcod,
  218. prt_client_id,
  219. MAX(wh_id) wh_id,
  220. ordlin,
  221. SUM(shpqty) shpqty,
  222. MAX(client_id) client_id
  223. FROM ORD_LINE
  224. WHERE ordnum = @ordnum
  225. and wh_id = @wh_id
  226. and client_id = @client_id
  227. GROUP BY prtnum,
  228. ftpcod,
  229. prt_client_id,
  230. client_id,
  231. ordlin) ol
  232. ON ol.ordnum = o.ordnum
  233. and ol.wh_id = o.wh_id
  234. and ol.client_id = o.client_id
  235. and ol.ordlin = sl.ordlin
  236. LEFT
  237. JOIN ORD_LINE_NOTE oln
  238. ON ol.ordnum = oln.ordnum
  239. AND ol.ordlin = oln.ordlin
  240. and ol.client_id = oln.client_id
  241. and ol.wh_id = oln.wh_id
  242. and oln.nottyp = 'ORDEREDBASE'
  243. LEFT
  244. JOIN PRTFTP_DTL pdt
  245. ON ol.prtnum = pdt.prtnum
  246. and ol.ftpcod = pdt.ftpcod
  247. AND ol.prt_client_id = pdt.prt_client_id
  248. and ol.wh_id = pdt.wh_id
  249. and pdt.uomcod = oln.nottxt
  250. JOIN PRTFTP_DTL pdt1
  251. ON ol.prtnum = pdt1.prtnum
  252. /* and ol.ftpcod = pdt.ftpcod */
  253. AND ol.prt_client_id = pdt1.prt_client_id
  254. and ol.wh_id = pdt1.wh_id
  255. and pdt1.ftpcod = ind.ftpcod
  256. and pdt1.cas_flg = 1
  257. WHERE o.ordnum = @ordnum
  258. and o.client_id = @client_id
  259. and o.wh_id = @wh_id
  260. and s.shpsts in ('C', 'S')
  261. and upper(iss.subnum) like 'C%'
  262. GROUP BY o.ordnum,
  263. sl.ship_id
  264. union
  265. SELECT CASE WHEN UPPER(max(s.carcod)) = 'SKELPARCEL' THEN MAX(IV.PALPOS)
  266. else (CASE when (sum(CAST(ind.untqty as float) / isnull(pdt1.untqty, 1)) < 1) THEN count(distinct iss.subnum)
  267. ELSE ceil(sum(CAST(ind.untqty as float) / isnull(pdt1.untqty, 1)))
  268. END)
  269. end totcases,
  270. -- 6 Total Pieces Shipped
  271. max(s.carcod) carcod,
  272. -- 10 Carrier
  273.  
  274. CASE WHEN UPPER(max(s.carcod)) = 'SKELPARCEL' THEN 0
  275. ELSE (SUM(isnull(sl.tot_pln_pal_qty, 0)))
  276. end totpal,
  277. CASE WHEN UPPER(max(s.carcod)) = 'SKELPARCEL' THEN 0
  278. ELSE ISNULL(COUNT(distinct iv.lodnum), 0)
  279. end totpallet,
  280. -- 7 Lift Count
  281. sum(isnull(sl.tot_pln_cube, 0)) / 1728 cube,
  282. -- 13 Cube
  283.  
  284. (SUM(ind.untqty / isnull(pdt.untqty, 1) * pdt.grswgt) * 0.02835) wgh
  285. --- Linst C
  286.  
  287. FROM ORD o
  288. INNER
  289. JOIN SHIPMENT_LINE sl
  290. on sl.ordnum = o.ordnum
  291. and sl.wh_id = o.wh_id
  292. and sl.client_id = o.client_id
  293. INNER
  294. JOIN SHIPMENT s
  295. on s.ship_id = sl.ship_id
  296. INNER
  297. JOIN invdtl ind
  298. on ind.ship_line_id = sl.ship_line_id
  299. INNER
  300. JOIN invsub iss
  301. on iss.subnum = ind.subnum
  302. INNER
  303. JOIN invlod iv
  304. on iv.lodnum = iss.lodnum
  305. INNER
  306. JOIN (SELECT MAX(ordnum) ordnum,
  307. prtnum,
  308. ftpcod,
  309. prt_client_id,
  310. MAX(wh_id) wh_id,
  311. ordlin,
  312. SUM(shpqty) shpqty,
  313. MAX(client_id) client_id
  314. FROM ORD_LINE
  315. WHERE ordnum = @ordnum
  316. and wh_id = @wh_id
  317. and client_id = @client_id
  318. GROUP BY prtnum,
  319. ftpcod,
  320. prt_client_id,
  321. client_id,
  322. ordlin) ol
  323. ON ol.ordnum = o.ordnum
  324. and ol.wh_id = o.wh_id
  325. and ol.client_id = o.client_id
  326. and ol.ordlin = sl.ordlin
  327. LEFT
  328. JOIN ORD_LINE_NOTE oln
  329. ON ol.ordnum = oln.ordnum
  330. AND ol.ordlin = oln.ordlin
  331. and ol.client_id = oln.client_id
  332. and ol.wh_id = oln.wh_id
  333. and oln.nottyp = 'ORDEREDBASE'
  334. LEFT
  335. JOIN PRTFTP_DTL pdt
  336. ON ol.prtnum = pdt.prtnum
  337. and ol.ftpcod = pdt.ftpcod
  338. AND ol.client_id = pdt.prt_client_id
  339. and ol.wh_id = pdt.wh_id
  340. and pdt.uomcod = oln.nottxt
  341. JOIN PRTFTP_DTL pdt1
  342. ON ol.prtnum = pdt1.prtnum
  343. /* and ol.ftpcod = pdt1.ftpcod */
  344. AND ol.client_id = pdt1.prt_client_id
  345. and ol.wh_id = pdt1.wh_id
  346. and pdt1.ftpcod = ind.ftpcod
  347. and pdt1.cas_flg = 1
  348. WHERE o.ordnum = @ordnum
  349. and o.client_id = @client_id
  350. and o.wh_id = @wh_id
  351. and s.shpsts in ('C', 'S')
  352. and upper(iss.subnum) not like 'C%'
  353. GROUP BY o.ordnum,
  354. sl.ship_id) uc_sub]
  355. |
  356. [Select ord.ordnum,
  357. ord.client_id,
  358. ord.wh_id,
  359. ord.bt_adr_id,
  360. ord.st_adr_id,
  361. isnull(ord.cponum, ' ') as cponum,
  362. nvl(Convert(char(10), ord.cpodte, 101), ' / / ') as cpodte,
  363. ord.vc_lbl_typ,
  364. ord.vc_mh10_typ,
  365. ord.vc_lbl_frmt,
  366. ord.vc_mh10_supvnno,
  367. @host_ext_id as custno,
  368. @st_adrnam as st_adrnam,
  369. @st_attn_name as st_attn_name,
  370. @st_adrln1 as st_adrln1,
  371. @st_adrln2 as st_adrln2,
  372. @st_adrln3 as st_adrln3,
  373. @st_adrcty as st_adrcty,
  374. @st_adrstc as st_adrstc,
  375. @st_adrpsz as st_adrpsz,
  376. @st_adrctry_name as st_adrctry_name,
  377. @ship_id as ship_id,
  378. @stop_id as stop_id,
  379. @client_name as client_name,
  380. @carcod as carcod,
  381. @srvlvl as srvlvl,
  382. to_date(@late_shpdte, 'YYYYMMDD') late_shpdte,
  383. to_date(@late_dlvdte, 'YYYYMMDD') late_dlvdte,
  384. to_date(@early_dlvdte, 'YYYYMMDD') early_dlvdte,
  385. to_date(@stgdte, 'YYYYMMDD') stgdte,
  386. ord.entdte,
  387. -- Note Message here --
  388. ORDERHEADERMSG = dbo.fnGetNoteTxtIn1Line(ord.ordnum, 'ORDERHEADERMSG'),
  389. SHIPTOMSG = dbo.fnGetNoteTxtIn1Line(ord.ordnum, 'SHIPTOMSG'),
  390. CSTERMS = dbo.fnGetNoteTxtIn1Line(ord.ordnum, 'CSTTERMS'),
  391. SALEPERSONMSG = dbo.fnGetNoteTxtIn1Line(ord.ordnum, 'SALEPERSONMSG'),
  392. @nottxt nottxt,
  393. @carnam carnam,
  394. @bt_adrnam bt_adrnam,
  395. @bt_adrln1 bt_adrln1,
  396. @bt_adrln2 bt_adrln2,
  397. @bt_adrln3 bt_adrln3,
  398. @bt_adrcty bt_adrcty,
  399. @bt_adrstc bt_adrstc,
  400. @bt_adrpsz bt_adrpsz,
  401. @bt_adrctry_name bt_adrctry_name,
  402. @bt_attn_name bt_attn_name,
  403. @stclntcustno stclntcustno,
  404. @totcases totcases,
  405. @pckqty pckqty
  406. From ord ord
  407. where ord.client_id = @client_id
  408. and ord.wh_id = @wh_id
  409. and ord.ordnum = @ordnum]
  410. }
Advertisement
Add Comment
Please, Sign In to add comment