-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbahmni_sale_ipd_opd_report.sql
243 lines (216 loc) · 7.61 KB
/
bahmni_sale_ipd_opd_report.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
\set start_date '2015-01-01';
\set end_date '2015-12-31';
-- Number of patient
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
count(distinct partner_id) as "Number of patient"
FROM sale_order
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Number of bills raised
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
count(*) as "Number of Bill Raised"
FROM sale_order
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Total Bill amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(amount_total) as "Total Bill Amount"
FROM sale_order
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Total paid amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(account_voucher_line.amount) as "Total Paid Amount"
FROM account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
INNER JOIN account_move_line on account_voucher_line.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
AND account_voucher.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Total Discount amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(discount) as "Total Discount Amount"
FROM sale_order
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Total Credit amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(amount_original - total_paid) as "Total Credit amount"
FROM
(Select
account_voucher_line.name, move_line_id, amount_original, sum(account_voucher_line.amount) as total_paid, bool_or(reconcile) as reconciled
from account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
WHERE account_voucher.state != 'cancel'
group by account_voucher_line.name, move_line_id, amount_original
) bill_payment
INNER JOIN account_move_line on bill_payment.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
bill_payment.reconciled = false
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Mean bill amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(amount_total)/count(*) as "Mean Bill amount"
FROM sale_order
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Mean paid amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(total_paid) / count(sale_order.id) as "Mean Paid amount"
FROM
(Select
account_voucher_line.name, move_line_id, amount_original, sum(account_voucher_line.amount) as total_paid, bool_or(reconcile) as reconciled
from account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
WHERE account_voucher.state != 'cancel'
group by account_voucher_line.name, move_line_id, amount_original
) bill_payment
INNER JOIN account_move_line on bill_payment.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Mean discount amount
SELECT
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(discount)/count(*) as "Mean Discount Amount"
FROM sale_order
WHERE
discount != 0.0
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- Mean credit amount
Select
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
sum(amount_original - total_paid) / count(sale_order.id) as "Mean Credit Amount"
FROM
(Select
account_voucher_line.name, move_line_id, amount_original, sum(account_voucher_line.amount) as total_paid, bool_or(reconcile) as reconciled
from account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
WHERE account_voucher.state != 'cancel'
group by account_voucher_line.name, move_line_id, amount_original
order by name
) bill_payment
INNER JOIN account_move_line on bill_payment.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
bill_payment.reconciled = false
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- % of bills paid 100%
Select
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
count(sale_order.id) as "Number of bill paid 100%"
FROM
(Select
account_voucher_line.name, move_line_id, amount_original, sum(account_voucher_line.amount) as total_paid, bool_or(reconcile) as reconciled
from account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
WHERE account_voucher.state != 'cancel'
group by account_voucher_line.name, move_line_id, amount_original
order by name
) bill_payment
INNER JOIN account_move_line on bill_payment.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
bill_payment.reconciled = true
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- % of bills receiving discounts
Select
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
count(sale_order.id) as "Number of bill receiving discounts"
FROM sale_order
WHERE
discount > 0
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;
-- % of bills receiving credits
Select
CASE WHEN care_setting is not NULL THEN care_setting
ELSE 'N/A'
END,
count(sale_order.id) as "Number of bills receiving credits"
FROM
(Select
account_voucher_line.name, move_line_id, amount_original, sum(account_voucher_line.amount) as total_paid, bool_or(reconcile) as reconciled
from account_voucher_line
INNER JOIN account_voucher on account_voucher_line.voucher_id = account_voucher.id
WHERE account_voucher.state != 'cancel'
group by account_voucher_line.name, move_line_id, amount_original
order by name
) bill_payment
INNER JOIN account_move_line on bill_payment.move_line_id = account_move_line.id
INNER JOIN sale_order on sale_order.name = account_move_line.ref
WHERE
bill_payment.reconciled = false
AND date_order >= :'start_date' and date_order <= :'end_date'
AND sale_order.state != 'cancel'
GROUP BY care_setting
ORDER BY care_setting;