-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-1-1-05.txt
470 lines (210 loc) · 24.1 KB
/
4-1-1-05.txt
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
select
CASE
WHEN '${E3PLUS_QC_QUERY_TYPE}' in ('1') THEN tc1.id
else tc1.id
END id,
CASE
WHEN '${E3PLUS_QC_QUERY_TYPE}' in ('1') THEN tc1.name
else tc1.name
END name,
sales_aty,
sale_amount
from (
select
SUM(workflow_change_qty + trade_qty - settlement_qty) sales_aty,-- 销售数量
sum(workflow_amount + trade_amount - settlement_amount) sale_amount, -- 含税销售金额
sum(workflow_extax_amount + trade_extax_amount - settlement_extax_amount) extax_amount, -- 不含税销售金额
sum(workflow_cost + trade_cost - settlement_cost) cost_amount, -- 成本金额
sum(workflow_extax_amount + trade_extax_amount - settlement_extax_amount) - sum(workflow_cost + trade_cost - settlement_cost) gross_amount,-- 毛利额(不含税销售金额 -成本金额)
sum(workflow_extax_amount + trade_extax_amount - settlement_extax_amount) - sum(workflow_cost + trade_cost - settlement_cost)
/
sum(workflow_cost + trade_cost - settlement_cost) gross_margin, -- 毛利率(毛利额/不含税销售金额)
sum(workflow_price + trade_price - settlement_price) retail_amount,-- 零售金额
CASE
when '${E3PLUS_QC_QUERY_TYPE}' in ('1') then t.category_tree_id_1
when '${E3PLUS_QC_QUERY_TYPE}' in ('2') then t.category_tree_id_2
else t.category_tree_id_1
END column1
from (
-- 库存流水表
select IFNULL((workflow.change_qty) * -1, 0) workflow_change_qty,
IFNULL((workflow.amount) * -1, 0) workflow_amount,
IFNULL((workflow.extax_amount) * -1, 0) workflow_extax_amount,
IFNULL(
IFNULL((workflow.change_qty) * -1, 0) * IFNULL(fin_cst_pac_item_costs_summary.item_cost, 0)
, 0) workflow_cost,
IFNULL(
IFNULL((workflow.change_qty) * -1, 0)
*
IFNULL(t9.price, 0)
, 0) workflow_price,
0 trade_qty,
0 trade_amount,
0 trade_extax_amount,
0 trade_cost,
0 trade_price,
0 settlement_qty,
0 settlement_amount,
0 settlement_extax_amount,
0 settlement_cost,
0 settlement_price,
(CASE
WHEN gds_category_tree.level_num=1 THEN
gds_category_tree.id
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.parent_id
else null
END) category_tree_id_1,-- 大类
(CASE
WHEN gds_category_tree.level_num=1 THEN
null
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.id
ELSE NULL
END) category_tree_id_2 -- 小类
from stk_stock_logic_workflow workflow
left join fin_cst_pac_item_costs_summary on workflow.stock_organization_id = fin_cst_pac_item_costs_summary.summary_org_id
and workflow.goods_id = fin_cst_pac_item_costs_summary.summary_item_id
left join (select t1.goods_id, t1.price
from mkt_price_control t1
inner join (select t1.goods_id, MAX(t1.create_time) create_time
from mkt_price_control t1
left join mkt_price_regulation t2 on t1.price_id = t2.id
where t2.code = 'MarketPrice'
group by t1.goods_id) t on t1.goods_id = t.goods_id
where t1.create_time = t.create_time
-- and t1.status='09'
-- and enable_time<=IF('${E3PLUS_QC_STOCK_DATE_START}'='', date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now())-1 day), '${E3PLUS_QC_STOCK_DATE_START}')
-- and disable_time>=IF('${E3PLUS_QC_STOCK_DATE_END}'='', now(), '${E3PLUS_QC_STOCK_DATE_END}')
) t9 on t9.goods_id = workflow.goods_id
left join gds_goods on workflow.goods_id = gds_goods.id
left join gds_goods_base_attribute on gds_goods.id = gds_goods_base_attribute.goods_id
left join gds_category_tree on gds_goods_base_attribute.category_tree_id = gds_category_tree.id
where 1 = 1
and workflow.qty_type in ('1')
and workflow.bill_name in ('71', '77')
and workflow.sale_confirm = 'Y'
UNION ALL
select 0 workflow_change_qty,
0 workflow_amount,
0 workflow_extax_amount,
0 workflow_cost,
0 workflow_price,
IFNULL((trade_sale_bill.total_qty) * -1, 0) trade_qty,
IFNULL((trade_sale_bill.total_amount) * -1, 0) trade_amount,
IFNULL((trade_sale_bill.total_extax_amount) * -1, 0) trade_extax_amount,
IFNULL(
IFNULL((trade_sale_bill.total_qty) * -1, 0) * IFNULL(fin_cst_pac_item_costs_summary.item_cost, 0)
, 0) trade_cost,
IFNULL(
IFNULL((trade_sale_bill.total_qty) * -1, 0)
*
IFNULL(t9.price, 0)
, 0) trade_price,
0 settlement_qty,
0 settlement_amount,
0 settlement_extax_amount,
0 settlement_cost,
0 settlement_price,
(CASE
WHEN gds_category_tree.level_num=1 THEN
gds_category_tree.id
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.parent_id
else null
END) category_tree_id_1,-- 大类
(CASE
WHEN gds_category_tree.level_num=1 THEN
null
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.id
ELSE NULL
END) category_tree_id_2 -- 小类
from trade_sale_bill
left join trade_sale_bill_line
on trade_sale_bill.id = trade_sale_bill_line.bill_id
left join fin_cst_pac_item_costs_summary on trade_sale_bill.stock_organization_id = fin_cst_pac_item_costs_summary.summary_org_id
and trade_sale_bill_line.goods_id = fin_cst_pac_item_costs_summary.summary_item_id
left join (select t1.goods_id,t1.price from mkt_price_control t1
inner join (select t1.goods_id,MAX(t1.create_time) create_time from mkt_price_control t1
left join mkt_price_regulation t2 on t1.price_id = t2.id
where t2.code = 'MarketPrice'
group by t1.goods_id) t on t1.goods_id = t.goods_id
where t1.create_time = t.create_time
-- and t1.status='09'
-- and enable_time<=IF('${E3PLUS_QC_STOCK_DATE_START}'='', date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now())-1 day), '${E3PLUS_QC_STOCK_DATE_START}')
-- and disable_time>=IF('${E3PLUS_QC_STOCK_DATE_END}'='', now(), '${E3PLUS_QC_STOCK_DATE_END}')
) t9 on t9.goods_id = trade_sale_bill_line.goods_id
left join gds_goods on trade_sale_bill_line.goods_id = gds_goods.id
left join gds_goods_base_attribute on gds_goods.id = gds_goods_base_attribute.goods_id
left join gds_category_tree on gds_goods_base_attribute.category_tree_id = gds_category_tree.id
where 1= 1
and trade_sale_bill.bill_type in ('24')
and trade_sale_bill.delivery_status in ('8')
UNION ALL
--
select 0 workflow_change_qty,
0 workflow_amount,
0 workflow_extax_amount,
0 workflow_cost,
0 workflow_price,
0 trade_qty,
0 trade_amount,
0 trade_extax_amount,
0 trade_cost,
0 trade_price,
IFNULL((settlement_so_bill.total_settle_qty) * -1, 0) settlement_qty,
IFNULL((settlement_so_bill.total_amount) * -1, 0) settlement_amount,
IFNULL((settlement_so_bill.total_extax_amount) * -1, 0) settlement_extax_amount,
IFNULL(
IFNULL((settlement_so_bill.total_settle_qty) * -1, 0) * IFNULL(fin_cst_pac_item_costs_summary.item_cost, 0)
, 0) settlement_cost,
IFNULL(
IFNULL((settlement_so_bill.total_settle_qty) * -1, 0)
*
IFNULL(t9.price, 0)
, 0) settlement_price,
(CASE
WHEN gds_category_tree.level_num=1 THEN
gds_category_tree.id
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.parent_id
else null
END) category_tree_id_1,-- 大类
(CASE
WHEN gds_category_tree.level_num=1 THEN
null
WHEN gds_category_tree.level_num=2 THEN
gds_category_tree.id
ELSE NULL
END) category_tree_id_2 -- 小类
from settlement_so_bill
left join settlement_so_line
on settlement_so_line.bill_id = settlement_so_line.id
left join bas_logic_warehouse on settlement_so_line.logic_warehouse_id = bas_logic_warehouse.stock_organization_id
left join fin_cst_pac_item_costs_summary on bas_logic_warehouse.stock_organization_id = fin_cst_pac_item_costs_summary.summary_org_id
and settlement_so_line.goods_id = fin_cst_pac_item_costs_summary.summary_item_id
left join (select t1.goods_id,t1.price from mkt_price_control t1
inner join (select t1.goods_id,MAX(t1.create_time) create_time from mkt_price_control t1
left join mkt_price_regulation t2 on t1.price_id = t2.id
where t2.code = 'MarketPrice'
group by t1.goods_id) t on t1.goods_id = t.goods_id
where t1.create_time = t.create_time
-- and t1.status='09'
-- and enable_time<=IF('${E3PLUS_QC_STOCK_DATE_START}'='', date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now())-1 day), '${E3PLUS_QC_STOCK_DATE_START}')
-- and disable_time>=IF('${E3PLUS_QC_STOCK_DATE_END}'='', now(), '${E3PLUS_QC_STOCK_DATE_END}')
) t9 on t9.goods_id = settlement_so_line.goods_id
left join gds_goods on settlement_so_line.goods_id = gds_goods.id
left join gds_goods_base_attribute on gds_goods.id = gds_goods_base_attribute.goods_id
left join gds_category_tree on gds_goods_base_attribute.category_tree_id = gds_category_tree.id
where 1=1
and settlement_so_bill.bill_type in ('1V')
and settlement_so_bill.bill_name in ('78')
) t
GROUP BY
case '${E3PLUS_QC_QUERY_TYPE}'
when '1' then t.category_tree_id_1
else t.category_tree_id_1
end
)tt
left join gds_category_tree tc1 on CONVERT(tt.column1,SIGNED) = tc1.id