1
|
--select * Into #upload_temp_di_mobile_txn from di_mobile_txn where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
2
|
--select * Into #upload_temp_di_mobile_txn_salesman_route from di_mobile_txn_salesman_route where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
3
|
--select * Into #upload_temp_di_mobile_txn_collection_debit_notes from di_mobile_txn_collection_debit_notes where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
4
|
-- select * Into #upload_temp_di_mobile_txn_collection_invoices from di_mobile_txn_collection_invoices where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
5
|
-- select * Into #upload_temp_di_mobile_txn_collection_credit_notes from di_mobile_txn_collection_credit_notes where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
6
|
-- select * Into #upload_temp_di_mobile_txn_collection_header from di_mobile_txn_collection_header where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
7
|
-- select * Into #upload_temp_di_mobile_txn_collection_photo from di_mobile_txn_collection_photo where process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
8
|
|
9
|
--Insert into txn_type_running_no (type, member_id, txn_id, org_id, created_date)
|
10
|
select 'collection','46350ac9c70345d4a8aafe6b9a74b38b', collection_id, 'SNEKKU', GETDATE() from #upload_temp_di_mobile_txn_collection_header d where process_id = '136058d4-8902-432c-8ed5-f57d185cb974' and not exists (select 1 from txn_type_running_no a where a.txn_id = d.collection_id and type = 'collection'and a.org_id = 'SNEKKU')
|
11
|
group by collection_id
|
12
|
|
13
|
--Insert Into txn_mobile_collection_header(col_id,col_no, txn_id, total_cheque_amt, total_cn_amt, total_col_amt, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id, reason_id)
|
14
|
|
15
|
Select * from (
|
16
|
Select Max(ch.collection_id) as collection_header_id,
|
17
|
CASE WHEN ISNULL(Max(ch.doc_no), '') = '' THEN Trim(Concat('CC', Max(m.member_cd), Right('00000'+ rTrim(
|
18
|
dbo.fn_getRunningNumberByMobileTransaction('46350ac9c70345d4a8aafe6b9a74b38b', Max(ch.collection_id), 'SNEKKU', 'collection')), 6), '-', convert(char, getdate(), 12))) ELSE Max(ch.doc_no) END as col_no,
|
19
|
txn.transaction_id,
|
20
|
Sum(Cast(convert(float,IsNull(ch.payment_amount, '0.00')) as decimal(18,2))) as total_cheque,
|
21
|
IsNull(Max(cn.cnTotal), 0) as total_cn,
|
22
|
Sum(Cast(convert(float,IsNull(ch.payment_amount, '0.00')) as decimal(18,2))) + IsNull(Max(cn.cnTotal), 0) as total_collection,
|
23
|
--Sum(Cast(IsNull(cn.paid,0) as decimal(18,2))),
|
24
|
--Sum(Cast(ch.payment_amount as decimal(18,2))) +Sum(Cast(IsNull(cn.paid, 0) as decimal(18,2))),
|
25
|
Case when ch.collection_status = 'Cancelled' then ch.collection_status else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
26
|
Max(ch.created_date) as created_date, '46350ac9c70345d4a8aafe6b9a74b38b' as created_by, GetDate() as updated_date, '46350ac9c70345d4a8aafe6b9a74b38b' as updated_by, 'SNEKKU' as org_id, 'SNEKKU' as tenant_id, Max(ch.collection_reason) as reason_id
|
27
|
From #upload_temp_di_mobile_txn txn
|
28
|
Inner Join #upload_temp_di_mobile_txn_salesman_route sr On sr.transaction_id = txn.transaction_id
|
29
|
Left Join (Select customer_id, tenant_id, customer_code from customer union Select new_customer_id, tenant_id, customer_code from Txn_new_customer where approval_status is null) c On c.customer_id = sr.customer_id and c.tenant_id = sr.tenant_id
|
30
|
Left Join member m on m.member_id = '46350ac9c70345d4a8aafe6b9a74b38b'
|
31
|
Left Join #upload_temp_di_mobile_txn_collection_header ch On txn.transaction_id = ch.transaction_id
|
32
|
Left Join (Select transaction_id, process_id, Sum(Cast(IsNull(paid,0) as decimal(18,2))) as cnTotal from #upload_temp_di_mobile_txn_collection_credit_notes group by transaction_id, process_id) cn On cn.transaction_id = ch.transaction_id
|
33
|
|
34
|
--Version 2
|
35
|
Left Join (Select mh.salesman_id, Max(col_no) as col_no
|
36
|
From txn_mobile_collection_header mch Inner Join txn_mobile_header mh On mch.txn_id = mh.txn_id
|
37
|
where mh.salesman_id = '46350ac9c70345d4a8aafe6b9a74b38b'
|
38
|
group by salesman_id
|
39
|
) tcmh On tcmh.salesman_id = '46350ac9c70345d4a8aafe6b9a74b38b'
|
40
|
--Version 2
|
41
|
|
42
|
--Left Join (Select mh.customer_id, Case When Try_Convert(int, Right(mch.col_no, 6)) Is Null Then 0 Else Right(mch.col_no,6) End as col_no from txn_mobile_collection_header mch Inner Join txn_mobile_header mh On mch.txn_id = mh.txn_id) tcmh On tcmh.customer_id = sr.customer_id
|
43
|
Where cast(txn.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974' and IsNull(ch.collection_id, '') <> '' --and ch.payment_amount NOT LIKE '%e%'
|
44
|
Group By txn.transaction_id, sr.customer_id, ch.collection_id, ch.collection_status, m.member_cd
|
45
|
) x Where Not Exists (Select 1 from txn_mobile_collection_header mch where mch.col_id = collection_header_id) ;
|
46
|
|
47
|
--end txn_mobile_collection_header
|
48
|
|
49
|
--txn_mobile_collection_detail
|
50
|
--Select * from txn_mobile_collection_detail
|
51
|
--18/6/2021: added bank, temporary receipt, payment_narration, journal_notation
|
52
|
-- Version 2, 2022-10-14, RL: Added Auto Approval for Van Sales
|
53
|
|
54
|
--Insert Into txn_mobile_collection_detail(col_dtl_id, col_id, doc_type, doc_no, doc_amt, doc_date, doc_unpaid, remark, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id, mobile_paid, web_paid, bank, journal_notation, payment_narration, temporary_receipt )
|
55
|
|
56
|
--Collection Header (Cheque)
|
57
|
Select ch.collection_header_id, Max(tch.colId), Max(ch.collection_type), Max(ch.collection_reference_id), Max(convert(float,IsNull(ch.payment_amount, 0))), Max(ch.cheque_date), 0, '',
|
58
|
Case when ch.collection_status = 'Cancelled' then ch.collection_status else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
59
|
Max(ch.created_date), '46350ac9c70345d4a8aafe6b9a74b38b', GetDate(), '46350ac9c70345d4a8aafe6b9a74b38b', 'SNEKKU', 'SNEKKU', Max(convert(float,IsNull(ch.payment_amount, 0))), Max(convert(float,IsNull(ch.payment_amount, 0))), '', '', '', Max(ch.temporary_receipt)
|
60
|
From #upload_temp_di_mobile_txn_collection_header ch
|
61
|
Inner Join (Select Max(case when isnull(collection_id, '') = '' then collection_header_id else collection_id end) as colId, process_id, transaction_id from #upload_temp_di_mobile_txn_collection_header Group By process_id, transaction_id, case when isnull(collection_id, '') = '' then collection_header_id else collection_id end) tch On tch.transaction_id = ch.transaction_id and ch.collection_id = tch.colId
|
62
|
Where cast(ch.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974'
|
63
|
and not exists (Select 1 from txn_mobile_collection_detail mcd where mcd.col_dtl_id = ch.collection_header_id)
|
64
|
and IsNull(ch.collection_header_id, '') <> '' --and ch.payment_amount NOT LIKE '%e%'
|
65
|
Group By ch.transaction_id, ch.collection_header_id, ch.collection_status
|
66
|
|
67
|
Union All
|
68
|
--CreditNote
|
69
|
Select cn.collection_credit_notes_id, Max(cn.collection_id), 'CN', Max(cnhh.credit_note_no), Max(cnhh.currency_value), Max(cnhh.credit_note_date),
|
70
|
Max(cnhh.credit_note_balance),
|
71
|
'',
|
72
|
Case when Max(cnhh.status) = 'Cancelled' then Max(cnhh.status) else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
73
|
Max(cn.created_date), '46350ac9c70345d4a8aafe6b9a74b38b', GetDate(), '46350ac9c70345d4a8aafe6b9a74b38b', 'SNEKKU', 'SNEKKU', Cast(Max(cn.paid) as decimal(18,2)), Cast(Max(cn.paid) as decimal(18,2)), '','','', ''
|
74
|
From #upload_temp_di_mobile_txn_collection_credit_notes cn
|
75
|
Inner Join txn_credit_note_history_header cnhh On cnhh.credit_note_header_id = cn.credit_note_id
|
76
|
Where cast(cn.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974'
|
77
|
and not exists (Select 1 from txn_mobile_collection_detail mcd where mcd.col_dtl_id = cn.collection_credit_notes_id)
|
78
|
and IsNull(cn.collection_credit_notes_id, '') <> '' and cast(isnull(cn.paid, 0) as decimal(18,2)) <> 0
|
79
|
Group By cn.transaction_id, cn.collection_credit_notes_id
|
80
|
|
81
|
Union All
|
82
|
--CreditNote (Non Product)
|
83
|
Select cn.collection_credit_notes_id, Max(cn.collection_id), 'CN (Non-Product)', Max(cnhh.credit_note_no), Max(cnhh.net_total), Max(cnhh.credit_note_date),
|
84
|
Max(cnhh.credit_note_balance),
|
85
|
'',
|
86
|
Case when Max(cnhh.status) = 'Cancelled' then Max(cnhh.status) else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
87
|
Max(cn.created_date), '46350ac9c70345d4a8aafe6b9a74b38b', GetDate(), '46350ac9c70345d4a8aafe6b9a74b38b', 'SNEKKU', 'SNEKKU', Cast(Max(cn.paid) as decimal(18,2)), Cast(Max(cn.paid) as decimal(18,2)), '','','', ''
|
88
|
From #upload_temp_di_mobile_txn_collection_credit_notes cn
|
89
|
Inner Join txn_credit_note_non_product_header cnhh On cnhh.credit_note_id = cn.credit_note_id
|
90
|
Where cast(cn.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974'
|
91
|
and not exists (Select 1 from txn_mobile_collection_detail mcd where mcd.col_dtl_id = cn.collection_credit_notes_id)
|
92
|
and IsNull(cn.collection_credit_notes_id, '') <> '' and cast(isnull(cn.paid, 0) as decimal(18,2)) <> 0
|
93
|
Group By cn.transaction_id, cn.collection_credit_notes_id;
|
94
|
|
95
|
--end txn_mobile_collection_detail
|
96
|
|
97
|
|
98
|
--txn_mobile_collection_invoice
|
99
|
--Version 2, 2022-10-14, RL: Added Auto Approval for Van Sales
|
100
|
-- 2022-10-20, RL: Added Update for invoice unpaid balance for Van Sales
|
101
|
--Select * from txn_mobile_collection_invoice
|
102
|
--Insert Into txn_mobile_collection_invoice (col_inv_id, col_id, inv_id, receipt_no, previous_balance, col_amt, web_paid, new_balance, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
103
|
|
104
|
Select ci.collection_invoices_id, Max(ci.collection_id), ci.invoice_id, '', Max(ihh.invoice_balance), Max(isnull(ci.paid, 0)), Max(isnull(ci.paid, 0)),
|
105
|
Max(ihh.invoice_balance-isnull(ci.paid, 0)),
|
106
|
Case when Max(ci.collection_status) = 'Cancelled' then Max(ci.collection_status) else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
107
|
Max(ci.created_date), '46350ac9c70345d4a8aafe6b9a74b38b', GetDate(), '46350ac9c70345d4a8aafe6b9a74b38b', 'SNEKKU', 'SNEKKU'
|
108
|
From #upload_temp_di_mobile_txn_collection_invoices ci
|
109
|
Left Join (Select invoice_id, a.currency_value - sum(isnull(b.web_paid, 0)) as invoice_balance from txn_invoice_history_header a
|
110
|
left join txn_mobile_collection_invoice b on a.invoice_id = b.inv_id
|
111
|
--update by arid 20240325 start
|
112
|
where b.status in ('Approved','Auto-Approved','Pending')
|
113
|
--update by arid 20240325 start
|
114
|
group by invoice_id, a.currency_value) ihh On ihh.invoice_id = ci.invoice_id
|
115
|
Where not exists (Select 1 from txn_mobile_collection_invoice mci where mci.col_inv_id = ci.collection_invoices_id)
|
116
|
and IsNull(ci.invoice_id, '') <> '' and cast(ci.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974' and cast(isnull(ci.paid, 0) as decimal(18,2)) <> 0
|
117
|
|
118
|
Group By ci.transaction_id,ci.invoice_id, ci.collection_invoices_id;
|
119
|
|
120
|
--Insert Into txn_mobile_collection_debit_note(col_dn_id, col_id, debit_note_id, previous_balance, col_amt, web_paid, new_balance, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id, debit_note_type)
|
121
|
|
122
|
Select ci.collection_debit_notes_id, Max(ci.collection_id), ci.debit_note_id, Max(isnull(ihh.dn_balance, isnull(dh.debit_note_balance, dnh.debit_note_balance))), Max(isnull(ci.paid, 0)), Max(isnull(ci.paid, 0)),
|
123
|
Max(isnull(ihh.dn_balance, isnull(dh.debit_note_balance, dnh.debit_note_balance))-isnull(ci.paid, 0)),
|
124
|
Case when Max(ci.status) = 'Cancelled' then Max(ci.status) else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) end as status,
|
125
|
Max(ci.created_date), '46350ac9c70345d4a8aafe6b9a74b38b', GetDate(), '46350ac9c70345d4a8aafe6b9a74b38b', 'SNEKKU', 'SNEKKU', case when Max(dh.debit_note_id) is null then 'DN (Non-Product)' else 'DN' end
|
126
|
From #upload_temp_di_mobile_txn_collection_debit_notes ci
|
127
|
Left Join (Select isnull(dh.debit_note_id, dnh.debit_note_id) as debit_note_id, isnull(dh.debit_note_amount, isnull(dnh.net_total, 0)) - sum(isnull(b.col_amt, 0)) as dn_balance from txn_mobile_collection_debit_note b
|
128
|
left join txn_debit_note_header dh on dh.debit_note_id = b.debit_note_id
|
129
|
left join txn_debit_note_non_product_header dnh on dnh.debit_note_id = b.debit_note_id
|
130
|
--update by arid 20240325 start
|
131
|
where b.status in ('Approved','Auto-Approved','Pending')
|
132
|
--update by arid 20240325 start
|
133
|
group by dh.debit_note_id, dnh.debit_note_id, dh.debit_note_amount, dnh.net_total) ihh On ihh.debit_note_id = ci.debit_note_id
|
134
|
left join txn_debit_note_header dh on dh.debit_note_id = ci.debit_note_id
|
135
|
left join txn_debit_note_non_product_header dnh on dnh.debit_note_id = ci.debit_note_id
|
136
|
Where not exists (Select 1 from txn_mobile_collection_debit_note mci where mci.col_dn_id = ci.collection_debit_notes_id)
|
137
|
and IsNull(ci.debit_note_id, '') <> '' and cast(ci.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974' and cast(isnull(ci.paid, 0) as decimal(18,2)) <> 0
|
138
|
|
139
|
Group By ci.transaction_id,ci.debit_note_id, ci.collection_debit_notes_id;
|
140
|
|
141
|
|
142
|
--IF ('Credit Sales' = 'Van Sales') -- Update invoice balance for Van Sales
|
143
|
-- BEGIN
|
144
|
-- Update txn_invoice_history_header Set
|
145
|
-- invoice_balance = (invoice_balance - x.payment_amount),
|
146
|
-- updated_date = GetDate(),
|
147
|
-- updated_by = '46350ac9c70345d4a8aafe6b9a74b38b'
|
148
|
-- From (
|
149
|
-- Select di.invoice_id, SUM(IsNull(Try_Cast(di.paid as decimal(18,2)), 0)) as [payment_amount]
|
150
|
-- From di_mobile_txn_collection_invoices di
|
151
|
-- Where di.process_id = '136058d4-8902-432c-8ed5-f57d185cb974'
|
152
|
-- Group By di.transaction_id, di.invoice_id
|
153
|
-- ) x
|
154
|
-- Where txn_invoice_history_header.invoice_id = x.invoice_id;
|
155
|
-- END
|
156
|
|
157
|
--end txn_mobile_collection_invoice
|
158
|
|
159
|
|
160
|
--Version 2 -- start txn_mobile_collection_photo
|
161
|
--Insert into txn_mobile_collection_photo (col_id, col_photo_id, filename, photo_url, status, created_date, created_by, updated_date, updated_by, org_id, tenant_id)
|
162
|
Select Max(ch.collection_id) as [col_id], di.collection_photo_id as [col_photo_id], Max(di.medias_uri) as [filename],
|
163
|
Concat('/', 'SNEKKU', '/MobileUpload/CollectionPhotos/', Convert(varchar(10),Cast(Max(di.created_date) as datetime), 120) ,'/', '46350ac9c70345d4a8aafe6b9a74b38b','/', di.transaction_id, '/', Max(di.medias_uri)) as [photo_url],
|
164
|
'Active' as [status], Max(di.created_date) as [created_date], '46350ac9c70345d4a8aafe6b9a74b38b' as [created_by], GETDATE() as [updated_date], '46350ac9c70345d4a8aafe6b9a74b38b' as [updated_by], 'SNEKKU' as [org_id], 'SNEKKU' as [tenant_id]
|
165
|
From #upload_temp_di_mobile_txn_collection_photo di
|
166
|
Left Join #upload_temp_di_mobile_txn_collection_invoices ch on ch.transaction_id = di.transaction_id
|
167
|
Where cast(di.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974'
|
168
|
and not exists (Select 1 from txn_mobile_collection_photo mcp where mcp.col_photo_id = di.collection_photo_id)
|
169
|
and IsNull(di.collection_photo_id, '') <> ''
|
170
|
Group By di.transaction_id, di.collection_photo_id
|
171
|
|
172
|
--Version 2 -- end txn_mobile_collection_photo
|
173
|
|
174
|
|
175
|
--txn_collection_approval_history
|
176
|
-- Version 2, 2022-10-14, RL: Added Auto Approval for Van Sales
|
177
|
--Select * from txn_collection_approval_history
|
178
|
|
179
|
--Insert Into txn_collection_approval_history([id], [collection_id], [member_id], [status], [status_date], [remark], [approval_flow_id], [approval_type],[approval_sequence], [created_date], [created_by], [updated_date], [updated_by], [org_id], [tenant_id], approval_mode)
|
180
|
|
181
|
Select newid() as [id],
|
182
|
ch.collection_header_id as [collection_id],
|
183
|
afa.member_id as [member_id],
|
184
|
Case when ch.collection_status = 'Cancelled' then ch.collection_status else (Case When 'Credit Sales' = 'Van Sales' Then 'Auto-Approved' Else 'Pending' End) End as status,
|
185
|
GetDate() as [status_date],
|
186
|
null [remark],
|
187
|
afa.approval_flow_id as [approval_flow_id],
|
188
|
'Y' as [approval_type],
|
189
|
afa.level as [approval_sequence],
|
190
|
GetDate() as [created_date],
|
191
|
'46350ac9c70345d4a8aafe6b9a74b38b' as [created_by],
|
192
|
GetDate() as [updated_date],
|
193
|
'46350ac9c70345d4a8aafe6b9a74b38b' as [updated_by],
|
194
|
ch.org_id as [org_id],
|
195
|
ch.tenant_id as [tenant_id],
|
196
|
approval_mode
|
197
|
from (select Max(collection_id) as collection_header_id,transaction_id, org_id, tenant_id, process_id, collection_status from #upload_temp_di_mobile_txn_collection_invoices Group By transaction_id, org_id, process_id, tenant_id, collection_status
|
198
|
UNION
|
199
|
select Max(collection_id) as collection_header_id,transaction_id, org_id, tenant_id, process_id, status as collection_status from #upload_temp_di_mobile_txn_collection_debit_notes Group By transaction_id, org_id, process_id, tenant_id, status) ch
|
200
|
Inner Join #upload_temp_di_mobile_txn_salesman_route sr On sr.transaction_id = ch.transaction_id
|
201
|
Left Join (Select customer_id, branch_id from customer c union Select new_customer_id, branch_id from Txn_new_customer where approval_status is null) c On c.customer_id = sr.customer_id
|
202
|
inner join (
|
203
|
Select afa.* from approval_flow_approver afa
|
204
|
where exists (Select 1 from approval_flow af Where af.activity = 'Collection'
|
205
|
and convert(date,getdate()) between af.start_date and af.end_date and afa.approval_flow_id=af.approval_flow_id and af.status = 'Active') and afa.status = 'Active'
|
206
|
) afa On exists (select 1 from approval_flow_branch afb where afb.branch_id=c.branch_id and afa.approval_flow_id=afb.approval_flow_id and afb.status = 'Active')
|
207
|
Where cast(sr.process_id as varchar(50)) = '136058d4-8902-432c-8ed5-f57d185cb974'
|
208
|
and not exists (Select 1 from txn_collection_approval_history cah Where cah.collection_id = ch.collection_header_id)
|
209
|
order by ch.collection_header_id, afa.level
|
210
|
|
211
|
--end txn_collection_approval_history
|
212
|
|
213
|
--IF('Credit Sales' <> 'Van Sales')
|
214
|
--BEGIN
|
215
|
-- Update h set h.approver_id=ap.member_id from txn_mobile_collection_header h
|
216
|
-- left join txn_collection_approval_history ap on h.col_id=ap.collection_id
|
217
|
-- where ap.approval_sequence='1' and exists (
|
218
|
-- select 1 From #upload_temp_di_mobile_txn_collection_invoices txn
|
219
|
-- where txn.collection_id = h.col_id
|
220
|
-- )
|
221
|
--END
|