-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdmin UNION Using PARENT with rates.~sql
2584 lines (2418 loc) · 106 KB
/
Admin UNION Using PARENT with rates.~sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--Admin Order Data by Parent or Customer
/*decalre variables*/
var cust varchar2(20)
exec :cust := 'WILLIS'
var ordernum varchar2(20)
exec :ordernum := '1363806'
var stock varchar2(20)
exec :stock := 'COURIER'
var source varchar2(20)
exec :source := 'BSPRINTNSW'
var anal varchar2(20)
exec :anal := '22'
var start_date varchar2(20)
exec :start_date := To_Date('1-Jul-2013')
var end_date varchar2(20)
exec :end_date := To_Date('31-Jul-2013')
var nRM_XX_FEE03 NUMBER /*PhoneOrderEntryFee*/
exec SELECT To_Number(regexp_substr(RM_XX_FEE03,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) INTO :nRM_XX_FEE03 FROM RM where RM_CUST = :cust;
var nRM_XX_FEE02 NUMBER /*EmailOrderEntryFee*/
exec SELECT To_Number(regexp_substr(RM_XX_FEE02,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) INTO :nRM_XX_FEE02 FROM RM where RM_CUST = :cust;
/*decalre variables*/
/* Drop table to hold II values */
DROP TABLE Tmp_Admin_Data_BreakPrices
/* Drop table to hold II values */
/* Create table to hold II values */
CREATE TABLE Tmp_Admin_Data_BreakPrices (vIIStock VARCHAR(30), vIICust VARCHAR(20), vUnitPrice NUMBER)-- AS 'Customer',
/* Create table to hold II values */
/* Insert into table to hold II values */
INSERT INTO Tmp_Admin_Data_BreakPrices
SELECT II_STOCK,II_CUST,II_BREAK_LCL
FROM II INNER JOIN IM ON IM_STOCK = II_STOCK
AND II_BREAK_LCL > 0.000001
WHERE IM_CUST= 'TABCORP'
/* Insert into table to hold II values */
/* query table to hold II values */
SELECT * FROM Tmp_Admin_Data_BreakPrices
/* query table to hold II values */
/*drop temp table to hold pickslip numbers*/
DROP TABLE Tmp_Admin_Data_Pickslips
/*drop temp table to hold pickslip numbers*/
/*create temp table to hold pickslip numbers*/
CREATE TABLE Tmp_Admin_Data_Pickslips (vPickslip VARCHAR(200),vPslip VARCHAR(10), vDateDesp DATE, vPackages NUMBER,
vWeight NUMBER, vST_XX_NUM_PAL_SW NUMBER,vST_XX_NUM_PALLETS NUMBER, vST_XX_NUM_CARTONS NUMBER)-- AS 'Customer',
/*create temp table to hold pickslip numbers*/
/*insert into temp table to hold pickslip numbers*/
INSERT INTO Tmp_Admin_Data_Pickslips
SELECT LTrim(ST_PICK),LTrim(ST_PSLIP), substr(To_Char(ST_DESP_DATE),0,10), ST_WEIGHT, ST_PACKAGES,ST_XX_NUM_PAL_SW,ST_XX_NUM_PALLETS,ST_XX_NUM_CARTONS
FROM ST INNER JOIN SH ON SH_ORDER = ST_ORDER
WHERE ST_DESP_DATE >= :start_date AND ST_DESP_DATE <= :end_date
AND ST_PSLIP <> 'CANCELLED'
AND SH_STATUS <> 3
/*insert into temp table to hold pickslip numbers*/
/*query temp table to hold pickslip numbers*/
SELECT *
FROM Tmp_Admin_Data_Pickslips
/*query temp table to hold pickslip numbers*/
/* Drop temp table to hold SL line data */
DROP TABLE Tmp_Admin_Data_Pick_LineCounts
/* Drop temp table to hold SL line data */
/* Create temp table to hold SL line data */
CREATE TABLE Tmp_Admin_Data_Pick_LineCounts ( nCountOfLines NUMBER, vSLPickslipNum VARCHAR(10), vSLOrderNum VARCHAR2(10), vSLPslip VARCHAR(10), vDateDespSL VARCHAR(255)
,vPackagesSL NUMBER, vWeightSL NUMBER,vST_XX_NUM_PAL_SW_SL NUMBER,vST_XX_NUM_PALLETS_SL NUMBER, vST_XX_NUM_CARTONS_SL NUMBER,vST_PICK_QTY NUMBER, vSL_ORDER_LINE NUMBER)
/* Create temp table to hold SL line data */
/* Insert into temp table to hold SL line data */
INSERT INTO Tmp_Admin_Data_Pick_LineCounts
SELECT Count(SL_LINE),LTrim(SL_PICK), LTrim(SL_ORDER), LTrim(SL_PSLIP),TP.vDateDesp, TP.vPackages,TP.vWeight,TP.vST_XX_NUM_PAL_SW,TP.vST_XX_NUM_PALLETS,TP.vST_XX_NUM_CARTONS,SL_PICK_QTY, SL_ORDER_LINE
FROM Tmp_Admin_Data_Pickslips TP LEFT OUTER JOIN SL ON LTrim(SL_PICK) = TP.vPickslip WHERE SL_PSLIP <> 'CANCELLED' GROUP BY SL_PICK,SL_ORDER,SL_PSLIP,TP.vDateDesp,TP.vPackages,TP.vWeight,TP.vST_XX_NUM_PAL_SW,TP.vST_XX_NUM_PALLETS,TP.vST_XX_NUM_CARTONS,SL_PICK_QTY, SL_ORDER_LINE
/* Insert into temp table to hold SL line data */
/* query temp table to hold SL line data */
SELECT * FROM Tmp_Admin_Data_Pick_LineCounts;
/* query temp table to hold SL line data */
/*Drop temp table for batch prices*/
DROP TABLE Tmp_Batch_Price_SL_Stock
/*Drop temp table for batch prices*/
/*Create temp table for batch prices*/
CREATE TABLE Tmp_Batch_Price_SL_Stock ( vBatchStock VARCHAR(30), vBatchPickNum VARCHAR(10), vDExcl NUMBER, vUnitPrice NUMBER, vQuantity NUMBER)
/*Create temp table for batch prices*/
/*Insert into temp table for batch prices*/
INSERT INTO Tmp_Batch_Price_SL_Stock(vBatchStock,vBatchPickNum,vUnitPrice,vDExcl, vQuantity)
SELECT nz.NI_STOCK, LTrim(RTrim(nz.NI_SL_PSLIP)), CAST((NX_SELL_VALUE/NX_QUANTITY) AS DECIMAL(22,2)) AS "UnitPrice", CAST(xz.NX_SELL_VALUE AS DECIMAL(22,2)), xz.NX_QUANTITY
--Count(ROWNUM) AS "RecordCount"
FROM NX xz INNER JOIN NI nz ON nz.NI_NX_MOVEMENT = xz.NX_MOVEMENT
INNER JOIN NE ez ON ez.NE_ENTRY = nz.NI_ENTRY AND ez.NE_PRICE_ENTRY = xz.NX_ENTRY
WHERE ez.NE_NV_EXT_TYPE = 1810105
AND ez.NE_STRENGTH = 3
AND xz.NX_SELL_VALUE > 0 AND xz.NX_SELL_VALUE IS NOT NULL
AND xz.NX_QUANTITY > 0
AND ez.NE_ADD_DATE >= :start_date
--AND ez.NE_NV_EXT_KEY = (SELECT SL_UID FROM SL WHERE LTrim(RTrim(SL_PICK)) = '2607366')-- AND SL_ORDER_LINE = 1)
GROUP BY nz.NI_STOCK, nz.NI_SL_PSLIP, xz.NX_SELL_VALUE, xz.NX_QUANTITY
/*Insert into temp table for batch prices*/
/*query temp table for batch prices*/
SELECT * FROM Tmp_Batch_Price_SL_Stock
/*query temp table for batch prices*/
/*Drop temp admin data table*/
DROP TABLE Tmp_Admin_Data2
/*Drop temp admin data table*/
/*create temp admin data table*/
CREATE TABLE Tmp_Admin_Data2
( Customer VARCHAR(255),-- AS "Customer",
Parent VARCHAR(255),-- AS "Parent",
CostCentre VARCHAR(255),-- AS "CostCentre",
OrderNum VARCHAR(255),-- AS "Order",
OrderwareNum VARCHAR(255),-- AS "Order",
CustRef VARCHAR(255),-- AS "CustRef"
Pickslip VARCHAR(255),-- AS "Pickslip",
PickNum VARCHAR(255),-- AS "PickNum",
DespatchNote VARCHAR(255),-- AS "DespatchNote",
DespatchDate VARCHAR(255),-- AS "DespatchDate",
FeeType VARCHAR(255),-- AS "FeeType",
Item VARCHAR(255),-- AS "Item",
Description VARCHAR(255),-- AS "Description",
Qty NUMBER,-- AS "Qty",
UOI VARCHAR(255),-- AS "UOI",
Unit_Sell_Price NUMBER,-- AS "UnitPrice",
OW_Unit_Sell_Price NUMBER,-- AS "OWUnitPrice",
Sell_Excl NUMBER,-- AS "Excl_Total",
Sell_Excl_Total NUMBER,-- AS "DIncl",
Sell_Incl NUMBER,-- AS "Incl_Total",
Sell_Incl_Total NUMBER,
ReportingPrice NUMBER,-- AS "ReportingPrice",
Address VARCHAR(255),-- AS "Address",
Address2 VARCHAR(255),-- AS "Address2",
Suburb VARCHAR(255),-- AS "Suburb",
State VARCHAR(255),-- AS "State",
Postcode VARCHAR(255),-- AS "Postcode",
DeliverTo VARCHAR(255),-- AS "DeliverTo",
AttentionTo VARCHAR(255),-- AS "AttentionTo" ,
Weight NUMBER,-- AS "Weight",
Packages NUMBER,-- AS "Packages",
OrderSource INTEGER,-- AS "OrderSource",
ILNOTE2 VARCHAR(255),-- AS "Palett/Shelf",
NILOCN VARCHAR(255),-- AS "Location",
NIAVAILACTUAL NUMBER,-- AS "SOH",
CountOfStocks NUMBER -- AS "Count"
)
/*create temp admin data table*/
/*insert into temp admin data table*/
INSERT into Tmp_Admin_Data2(
Customer,
Parent,
CostCentre,
OrderNum,
OrderwareNum,
CustRef,
Pickslip,
PickNum,
DespatchNote,
DespatchDate,
FeeType,
Item,
Description,
Qty,
UOI,
Unit_Sell_Price,
OW_Unit_Sell_Price,
Sell_Excl,
Sell_Excl_Total,
Sell_Incl,
Sell_Incl_Total,
ReportingPrice,
Address,
Address2,
Suburb,
State,
Postcode,
DeliverTo,
AttentionTo,
Weight,
Packages,
OrderSource,
ILNOTE2,
NILOCN,
NIAVAILACTUAL,
CountOfStocks
)
/*insert into temp admin data table*/
/*freight fees*/
select s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
s.SH_SPARE_STR_4 AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
t.ST_PICK AS "Pickslip",
d.SD_XX_PICKLIST_NUM AS "PickNum",
t.ST_PSLIP AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN d.SD_STOCK like 'COURIER%' AND d.SD_SELL_PRICE >= 1 THEN 'Freight Fee'
ELSE To_Char(d.SD_DESC)
END AS "FeeType",
d.SD_STOCK AS "Item",
'="' || To_Char(d.SD_DESC) || '"' AS "Description",
CASE WHEN d.SD_LINE IS NOT NULL THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN d.SD_LINE IS NOT NULL THEN '1'
ELSE NULL
END AS "UOI",
d.SD_SELL_PRICE AS "SellUnitPrice",
d.SD_XX_OW_UNIT_PRICE AS "OWUnitPrice",
d.SD_EXCL AS "DExcl",
Sum(d.SD_EXCL) AS "Excl_Total",
d.SD_INCL AS "DIncl",
Sum(d.SD_INCL) AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
t.ST_WEIGHT AS "Weight",
t.ST_PACKAGES AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SD d
INNER JOIN PWIN175.SH s ON s.SH_ORDER = d.SD_ORDER
INNER JOIN PWIN175.ST t ON LTRIM(RTRIM(t.ST_PICK)) = LTRIM(RTRIM(d.SD_XX_PICKLIST_NUM))
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
WHERE s.SH_ORDER = d.SD_ORDER
--AND r.RM_ANAL = :anal
AND (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND d.SD_STOCK = :stock
AND s.SH_ORDER = t.ST_ORDER
AND d.SD_SELL_PRICE >= 1
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
GROUP BY s.SH_CUST,
s.SH_SPARE_STR_4,
s.SH_ORDER,
t.ST_PICK,
d.SD_XX_PICKLIST_NUM,
t.ST_PSLIP,
t.ST_DESP_DATE,
d.SD_STOCK,
d.SD_DESC,
d.SD_LINE,
d.SD_EXCL,
d.SD_INCL,
d.SD_NOTE_1,
d.SD_SELL_PRICE,
d.SD_XX_OW_UNIT_PRICE,
d.SD_QTY_ORDER,
d.SD_QTY_ORDER,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
t.ST_WEIGHT,
t.ST_PACKAGES,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
UNION ALL
/*freight fees*/
/*manual freight fees*/
select s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
s.SH_SPARE_STR_4 AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
t.ST_PICK AS "Pickslip",
d.SD_XX_PICKLIST_NUM AS "PickNum",
t.ST_PSLIP AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN d.SD_STOCK like 'COURIER%' AND d.SD_SELL_PRICE >= 1 THEN 'Freight Fee'
ELSE To_Char(d.SD_DESC)
END AS "FeeType",
d.SD_STOCK AS "Item",
'="' || To_Char(d.SD_DESC) || '"' AS "Description",
CASE WHEN d.SD_LINE IS NOT NULL THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN d.SD_LINE IS NOT NULL THEN '1'
ELSE NULL
END AS "UOI",
d.SD_SELL_PRICE AS "SellUnitPrice",
d.SD_XX_OW_UNIT_PRICE AS "OWUnitPrice",
d.SD_EXCL AS "DExcl",
Sum(d.SD_EXCL) AS "Excl_Total",
d.SD_INCL AS "DIncl",
Sum(d.SD_INCL) AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
t.ST_WEIGHT AS "Weight",
t.ST_PACKAGES AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SD d
INNER JOIN PWIN175.SH s ON s.SH_ORDER = d.SD_ORDER
INNER JOIN PWIN175.ST t ON LTRIM(RTRIM(t.ST_ORDER)) = LTRIM(RTRIM(d.SD_ORDER))
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
WHERE s.SH_ORDER = d.SD_ORDER
--AND r.RM_ANAL = :anal
AND (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND d.SD_STOCK IN ('COURIERM','COURIERS','COURIER','DETENTIONTIMEM','DETENTIONTIMES','FREIGHTDUTY')
AND s.SH_ORDER = t.ST_ORDER
AND d.SD_SELL_PRICE >= 1
AND d.SD_ADD_DATE >= '2-Aug-2013'
--AND d.SD_ADD_DATE >= :start_date AND d.SD_ADD_DATE <= :end_date
AND d.SD_ADD_OP NOT LIKE 'SERV%'
GROUP BY s.SH_CUST,
s.SH_SPARE_STR_4,
s.SH_ORDER,
t.ST_PICK,
d.SD_XX_PICKLIST_NUM,
t.ST_PSLIP,
d.SD_ADD_DATE,
d.SD_STOCK,
d.SD_DESC,
d.SD_LINE,
d.SD_EXCL,
d.SD_INCL,
d.SD_NOTE_1,
d.SD_SELL_PRICE,
d.SD_XX_OW_UNIT_PRICE,
d.SD_QTY_ORDER,
d.SD_QTY_ORDER,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
t.ST_WEIGHT,
t.ST_PACKAGES,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
s.SH_SPARE_STR_5,
s.SH_CUST_REF,
d.SD_ADD_OP,
t.ST_DESP_DATE
UNION ALL
/*manual freight fees*/
/*PhoneOrderEntryFee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
CASE WHEN i.IM_CUST <> 'TABCORP' THEN s.SH_SPARE_STR_4
WHEN i.IM_CUST = 'TABCORP' THEN i.IM_XX_COST_CENTRE01
ELSE s.SH_SPARE_STR_4
END AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
NULL AS "Pickslip",
NULL AS "PickNum",
NULL AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN 'OrderEntryFee'
ELSE ''
END AS "FeeType",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN 'FEEORDERENTRYS'
ELSE ''
END AS "Item",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN 'Phone Order Entry Fee'
ELSE ''
END AS "Description",
CASE WHEN d.SD_LINE = 1 THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN d.SD_LINE = 1 THEN '1'
ELSE ''
END AS "UOI",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 --(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 --(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 --(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "OWUnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 --(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "Excl_Total",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 * 1.1--(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE WHEN s.SH_SPARE_DBL_9 = 1 THEN :nRM_XX_FEE03 * 1.1--(Select To_Number(rm3.RM_XX_FEE03) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
NULL AS "Weight",
NULL AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SH s
INNER JOIN PWIN175.SD d ON d.SD_ORDER = s.SH_ORDER
INNER JOIN PWIN175.IM i ON i.IM_STOCK = d.SD_STOCK
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER AND t.ST_PICK = s.SH_LAST_PICK_NUM
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
--LEFT JOIN PWIN175.RM r2 ON r2.RM_ANAL = r.RM_ANAL AND r2.RM_CUST = :cust
WHERE (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
AND LTrim(s.SH_PREV_PSLIP_NUM) IS NULL
AND s.SH_SPARE_DBL_9 = 1
AND d.SD_LINE = 1
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> 0
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> '0'
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) IS NOT NULL
AND To_Number(regexp_substr(r.RM_XX_FEE03,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0
GROUP BY s.SH_CUST,
s.SH_ADD_DATE,
s.SH_SPARE_STR_4,
s.SH_ORDER,
s.SH_PREV_PSLIP_NUM,
t.ST_DESP_DATE,
s.SH_SPARE_DBL_9,
d.SD_LINE,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
i.IM_CUST,
i.IM_XX_COST_CENTRE01,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
UNION ALL
/*PhoneOrderEntryFee*/
/*EmailOrderEntryFee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
CASE WHEN i.IM_CUST <> 'TABCORP' THEN s.SH_SPARE_STR_4
WHEN i.IM_CUST = 'TABCORP' THEN i.IM_XX_COST_CENTRE01
ELSE s.SH_SPARE_STR_4
END AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
NULL AS "Pickslip",
NULL AS "PickNum",
NULL AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN 'OrderEntryFee'
ELSE ''
END AS "FeeType",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN 'FEEORDERENTRYS'
ELSE ''
END AS "Item",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN 'Email Order Entry Fee'
ELSE ''
END AS "Description",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN '1'
ELSE ''
END AS "UOI",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "OWUnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "Excl_Total",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE WHEN s.SH_SPARE_DBL_9 = 3 THEN (Select To_Number(rm3.RM_XX_FEE02) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
NULL AS "Weight",
NULL AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SH s
INNER JOIN PWIN175.SD d ON d.SD_ORDER = s.SH_ORDER
INNER JOIN PWIN175.IM i ON i.IM_STOCK = d.SD_STOCK
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER AND t.ST_PICK = s.SH_LAST_PICK_NUM
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
--LEFT JOIN PWIN175.RM r2 ON r2.RM_ANAL = r.RM_ANAL AND r2.RM_CUST = :cust
WHERE (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
AND LTrim(s.SH_PREV_PSLIP_NUM) IS NULL
AND s.SH_SPARE_DBL_9 = 3
AND d.SD_LINE = 1
--AND Select rm3.RM_XX_FEE02 from RM rm3 where To_Number(regexp_substr(rm3.RM_XX_FEE02,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0 AND rm3.RM_CUST = :cust
AND To_Number(regexp_substr(r.RM_XX_FEE02,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0 --AND rm3.RM_CUST = :cust
--AND (Select rm3.RM_XX_FEE02 from RM rm3 where rm3.RM_CUST = :cust) <> 0
--AND (Select rm3.RM_XX_FEE02 from RM rm3 where rm3.RM_CUST = :cust) <> '0'
--AND (Select rm3.RM_XX_FEE02 from RM rm3 where rm3.RM_CUST = :cust) IS NOT NULL
GROUP BY s.SH_CUST,
s.SH_ADD_DATE,
s.SH_SPARE_STR_4,
s.SH_ORDER,
s.SH_PREV_PSLIP_NUM,
t.ST_DESP_DATE,
s.SH_SPARE_DBL_9,
d.SD_LINE,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
i.IM_CUST,
i.IM_XX_COST_CENTRE01,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
UNION ALL
/*EmailOrderEntryFee*/
/*FaxOrderEntryFee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
CASE WHEN i.IM_CUST <> 'TABCORP' THEN s.SH_SPARE_STR_4
WHEN i.IM_CUST = 'TABCORP' THEN i.IM_XX_COST_CENTRE01
ELSE s.SH_SPARE_STR_4
END AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
NULL AS "Pickslip",
NULL AS "PickNum",
NULL AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN 'OrderEntryFee'
ELSE ''
END AS "FeeType",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN 'FEEORDERENTRYS'
ELSE ''
END AS "Item",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN 'Fax Order Entry Fee'
ELSE ''
END AS "Description",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN '1'
ELSE ''
END AS "UOI",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "OWUnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "Excl_Total",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE WHEN s.SH_SPARE_DBL_9 = 2 THEN (Select To_Number(rm3.RM_XX_FEE07) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
NULL AS "Weight",
NULL AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SH s
INNER JOIN PWIN175.SD d ON d.SD_ORDER = s.SH_ORDER
INNER JOIN PWIN175.IM i ON i.IM_STOCK = d.SD_STOCK
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER AND t.ST_PICK = s.SH_LAST_PICK_NUM
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
--LEFT JOIN PWIN175.RM r2 ON r2.RM_ANAL = r.RM_ANAL AND r2.RM_CUST = :cust
WHERE (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
AND LTrim(s.SH_PREV_PSLIP_NUM) IS NULL
AND s.SH_SPARE_DBL_9 = 2
AND d.SD_LINE = 1
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> 0
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> '0'
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) IS NOT NULL
AND To_Number(regexp_substr(r.RM_XX_FEE07,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0
GROUP BY s.SH_CUST,
s.SH_ADD_DATE,
s.SH_SPARE_STR_4,
s.SH_ORDER,
s.SH_PREV_PSLIP_NUM,
t.ST_DESP_DATE,
s.SH_SPARE_DBL_9,
d.SD_LINE,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
i.IM_CUST,
i.IM_XX_COST_CENTRE01,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
UNION ALL
/*FaxOrderEntryFee*/
/*VerbalOrderEntryFee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
CASE WHEN i.IM_CUST <> 'TABCORP' THEN s.SH_SPARE_STR_4
WHEN i.IM_CUST = 'TABCORP' THEN i.IM_XX_COST_CENTRE01
ELSE s.SH_SPARE_STR_4
END AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
NULL AS "Pickslip",
NULL AS "PickNum",
NULL AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN 'OrderEntryFee'
ELSE ''
END AS "FeeType",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN 'FEEORDERENTRYS'
ELSE ''
END AS "Item",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN 'Phone Order Entry Fee'
ELSE ''
END AS "Description",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN '1'
ELSE ''
END AS "UOI",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "OWUnitPrice",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust)
ELSE NULL
END AS "Excl_Total",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE WHEN s.SH_SPARE_DBL_9 = 4 THEN (Select To_Number(rm3.RM_XX_FEE01) from RM rm3 where rm3.RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
NULL AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
NULL AS "Weight",
NULL AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SH s
INNER JOIN PWIN175.SD d ON d.SD_ORDER = s.SH_ORDER
INNER JOIN PWIN175.IM i ON i.IM_STOCK = d.SD_STOCK
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER AND t.ST_PICK = s.SH_LAST_PICK_NUM
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
--LEFT JOIN PWIN175.RM r2 ON r2.RM_ANAL = r.RM_ANAL AND r2.RM_CUST = :cust
WHERE (r.RM_PARENT = :cust OR r.RM_CUST = :cust)
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
AND LTrim(s.SH_PREV_PSLIP_NUM) IS NULL
AND s.SH_SPARE_DBL_9 = 4
AND d.SD_LINE = 1
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> 0
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) <> '0'
--AND (Select rm3.RM_XX_FEE07 from RM rm3 where rm3.RM_CUST = :cust) IS NOT NULL
AND To_Number(regexp_substr(r.RM_XX_FEE01,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0
GROUP BY s.SH_CUST,
s.SH_ADD_DATE,
s.SH_SPARE_STR_4,
s.SH_ORDER,
s.SH_PREV_PSLIP_NUM,
t.ST_DESP_DATE,
s.SH_SPARE_DBL_9,
d.SD_LINE,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
i.IM_CUST,
i.IM_XX_COST_CENTRE01,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
UNION ALL
/*VerbalOrderEntryFee*/
/*BB PackingFee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
s.SH_SPARE_STR_4 AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
t.ST_PICK AS "Pickslip",
d.SD_XX_PICKLIST_NUM AS "PickNum",
t.ST_PSLIP AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN (i.IM_TYPE = 'BB_PACK' AND (d.SD_STOCK NOT like 'COURIER%' AND d.SD_STOCK NOT like 'FEE%')) THEN 'Packing Fee'
ELSE NULL
END AS "FeeType",
d.SD_STOCK AS "Item",
d.SD_DESC AS "Description",
CASE WHEN d.SD_LINE IS NOT NULL THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN d.SD_LINE IS NOT NULL THEN '1'
ELSE ''
END AS "UOI",
CASE
WHEN i.IM_TYPE = 'BB_PACK' THEN (Select To_Number(RM_XX_FEE08) from RM where RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE
WHEN i.IM_TYPE = 'BB_PACK' THEN (Select To_Number(RM_XX_FEE08) from RM where RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
NULL AS "OWUnitPrice",
NULL AS "Excl_Total",
CASE
WHEN i.IM_TYPE = 'BB_PACK' THEN (Select To_Number(RM_XX_FEE08) from RM where RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE
WHEN i.IM_TYPE = 'BB_PACK' THEN (Select To_Number(RM_XX_FEE08) from RM where RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
CASE WHEN d.SD_STOCK NOT like 'COURIER%' THEN (Select To_Number(i.IM_REPORTING_PRICE) from IM i where i.IM_STOCK = d.SD_STOCK)
ELSE NULL
END AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
t.ST_WEIGHT AS "Weight",
t.ST_PACKAGES AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SD d
INNER JOIN PWIN175.SH s ON s.SH_ORDER = d.SD_ORDER
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER AND t.ST_PICK = d.SD_LAST_PICK_NUM
INNER JOIN PWIN175.RM r ON r.RM_CUST = s.SH_CUST
INNER JOIN PWIN175.IM i ON i.IM_STOCK = d.SD_STOCK
/*WHERE (Select rmP.RM_XX_FEE08
from RM rmP
where To_Number(regexp_substr(rmP.RM_XX_FEE08, '^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0 AND rmp.RM_CUST = :cust) > 0
--To_Number(regexp_substr(r2.RM_XX_FEE01,'^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0 */
AND s.SH_ORDER = d.SD_ORDER
AND i.IM_TYPE = 'BB_PACK'
AND r.RM_ANAL = :anal
AND (r.RM_CUST = 'BEYONDBLUE' OR r.RM_PARENT = 'BEYONDBLUE')
AND To_Number(regexp_substr(r.RM_XX_FEE08, '^[-]?[[:digit:]]*\.?[[:digit:]]*$')) > 0
AND s.SH_STATUS <> 3
AND d.SD_STOCK NOT IN ('EMERQSRFEE','COURIER%','FEE%','FEE*','COURIER*','COURIER')
AND s.SH_ORDER = t.ST_ORDER
AND t.ST_DESP_DATE >= :start_date AND t.ST_DESP_DATE <= :end_date
GROUP BY s.SH_CUST,
s.SH_SPARE_STR_4,
s.SH_ORDER,
t.ST_PICK,
d.SD_XX_PICKLIST_NUM,
t.ST_PSLIP,
t.ST_DESP_DATE,
i.IM_TYPE,
d.SD_STOCK,
d.SD_DESC,
d.SD_LINE,
d.SD_EXCL,
d.SD_INCL,
d.SD_SELL_PRICE,
d.SD_XX_OW_UNIT_PRICE,
d.SD_QTY_ORDER,
d.SD_QTY_ORDER,
s.SH_ADDRESS,
s.SH_SUBURB,
s.SH_CITY,
s.SH_STATE,
s.SH_POST_CODE,
s.SH_NOTE_1,
s.SH_NOTE_2,
t.ST_WEIGHT,
t.ST_PACKAGES,
s.SH_SPARE_DBL_9,
r.RM_PARENT,
s.SH_SPARE_STR_5,
s.SH_CUST_REF
--HAVING Sum(s.SH_ORDER) <> 1
UNION ALL
/*BB PackingFee*/
/*Destruction Fee*/
SELECT s.SH_CUST AS "Customer",
r.RM_PARENT AS "Parent",
CASE WHEN IM_CUST <> 'TABCORP' THEN s.SH_SPARE_STR_4
WHEN IM_CUST = 'TABCORP' THEN IM_XX_COST_CENTRE01
ELSE s.SH_SPARE_STR_4
END AS "CostCentre",
s.SH_ORDER AS "Order",
s.SH_SPARE_STR_5 AS "OrderwareNum",
s.SH_CUST_REF AS "CustomerRef",
t.ST_PICK AS "Pickslip",
d.SD_XX_PICKLIST_NUM AS "PickNum",
t.ST_PSLIP AS "DespatchNote",
substr(To_Char(t.ST_DESP_DATE),0,10) AS "DespatchDate",
CASE WHEN s.SH_NOTE_1 = 'DESTROY' OR s.SH_CAMPAIGN = 'OBSOLETE' THEN 'Destruction Fee is '
ELSE NULL
END AS "FeeType",
CASE WHEN d.SD_LINE IS NOT NULL THEN 'DESTRUCT'
ELSE NULL
END AS "Item",
CASE WHEN d.SD_LINE IS NOT NULL THEN 'Destruction Fee'
ELSE NULL
END AS "Description",
CASE WHEN d.SD_LINE IS NOT NULL THEN 1
ELSE NULL
END AS "Qty",
CASE WHEN d.SD_LINE IS NOT NULL THEN '1'
ELSE ''
END AS "UOI",
CASE WHEN s.SH_CAMPAIGN = 'DESTROY' OR s.SH_CAMPAIGN = 'OBSOLETE' THEN (Select To_Number(RM_XX_FEE25) from RM where RM_CUST = :cust)
ELSE NULL
END AS "UnitPrice",
CASE WHEN s.SH_CAMPAIGN = 'DESTROY' OR s.SH_CAMPAIGN = 'OBSOLETE' THEN (Select To_Number(RM_XX_FEE25) from RM where RM_CUST = :cust)
ELSE NULL
END AS "DExcl",
NULL AS "OWUnitPrice",
NULL AS "Excl_Total",
CASE WHEN s.SH_CAMPAIGN = 'DESTROY' OR s.SH_CAMPAIGN = 'OBSOLETE' THEN (Select To_Number(RM_XX_FEE25) from RM where RM_CUST = :cust) * 1.1
ELSE NULL
END AS "DIncl",
CASE WHEN s.SH_CAMPAIGN = 'DESTROY' OR s.SH_CAMPAIGN = 'OBSOLETE' THEN (Select To_Number(RM_XX_FEE25) from RM where RM_CUST = :cust) * 1.1
ELSE NULL
END AS "Incl_Total",
CASE WHEN d.SD_STOCK NOT like 'COURIER%' THEN (Select To_Number(i.IM_REPORTING_PRICE) from IM i where i.IM_STOCK = d.SD_STOCK)
ELSE NULL
END AS "ReportingPrice",
s.SH_ADDRESS AS "Address",
s.SH_SUBURB AS "Address2",
s.SH_CITY AS "Suburb",
s.SH_STATE AS "State",
s.SH_POST_CODE AS "Postcode",
s.SH_NOTE_1 AS "DeliverTo",
s.SH_NOTE_2 AS "AttentionTo" ,
t.ST_WEIGHT AS "Weight",
t.ST_PACKAGES AS "Packages",
s.SH_SPARE_DBL_9 AS "OrderSource",
NULL AS "Pallet/Shelf Space", /*Pallet/Space*/
NULL AS "Locn", /*Locn*/
NULL AS "AvailSOH",/*Avail SOH*/
NULL AS "CountOfStocks"
FROM PWIN175.SD d
INNER JOIN PWIN175.SH s ON s.SH_ORDER = d.SD_ORDER
INNER JOIN PWIN175.ST t ON t.ST_ORDER = s.SH_ORDER