@@ -232,22 +232,29 @@ def reconcile_vouchers(bank_transaction_name, vouchers):
232
232
233
233
if transaction .unallocated_amount == 0 :
234
234
frappe .throw (_ ("This bank transaction is already fully reconciled" ))
235
- total_amount = 0
235
+
236
+ # total_amount = 0
237
+
236
238
for voucher in vouchers :
237
239
voucher ['payment_entry' ] = frappe .get_doc (voucher ['payment_doctype' ], voucher ['payment_name' ])
238
- total_amount += get_paid_amount (frappe ._dict ({
239
- 'payment_document' : voucher ['payment_doctype' ],
240
- 'payment_entry' : voucher ['payment_name' ],
241
- }), transaction .currency , company_account )
240
+ # total_amount += get_paid_amount(frappe._dict({
241
+ # 'payment_document': voucher['payment_doctype'],
242
+ # 'payment_entry': voucher['payment_name'],
243
+ # }), transaction.currency, company_account)
242
244
243
- if total_amount > transaction .unallocated_amount :
244
- frappe .throw (_ ("The Sum Total of Amounts of All Selected Vouchers Should be Less than the Unallocated Amount of the Bank Transaction" ))
245
+ # if total_amount > transaction.unallocated_amount:
246
+ # frappe.throw(_("The Sum Total of Amounts of All Selected Vouchers Should be Less than the Unallocated Amount of the Bank Transaction"))
245
247
account = frappe .db .get_value ("Bank Account" , transaction .bank_account , "account" )
246
248
247
249
for voucher in vouchers :
248
250
gl_entry = frappe .db .get_value ("GL Entry" , dict (account = account , voucher_type = voucher ['payment_doctype' ], voucher_no = voucher ['payment_name' ]), ['credit' , 'debit' ], as_dict = 1 )
249
- gl_amount , transaction_amount = (gl_entry .credit , transaction .deposit ) if gl_entry .credit > 0 else (gl_entry .debit , transaction .withdrawal )
250
- allocated_amount = gl_amount if gl_amount >= transaction_amount else transaction_amount
251
+ gl_amount = gl_entry .credit if gl_entry .credit > 0 else gl_entry .debit
252
+ transaction_amount = transaction .deposit if transaction .deposit > 0 else transaction .withdrawal
253
+ # gl_amount, transaction_amount = (gl_entry.credit, transaction.deposit) if gl_entry.credit > 0 else (gl_entry.debit, transaction.withdrawal)
254
+ reconcilied_amount = transaction .get_reconcilied_amount (voucher ['payment_entry' ].doctype , voucher ['payment_entry' ].name )
255
+ not_reconcilied = round ((gl_amount - reconcilied_amount ), 2 )
256
+ # allocated_amount = gl_amount if gl_amount >= transaction_amount else transaction_amount
257
+ allocated_amount = not_reconcilied if not_reconcilied <= transaction_amount else transaction_amount
251
258
252
259
transaction .append ("payment_entries" , {
253
260
"payment_document" : voucher ['payment_entry' ].doctype ,
@@ -308,7 +315,7 @@ def check_matching(bank_account, company, transaction, document_types):
308
315
309
316
def get_queries (bank_account , company , transaction , document_types ):
310
317
# get queries to get matching vouchers
311
- amount_condition = "=" if "exact_match" in document_types else "<="
318
+ amount_condition = "=" if "exact_match" in document_types else None
312
319
account_from_to = "paid_to" if transaction .deposit > 0 else "paid_from"
313
320
queries = []
314
321
@@ -341,6 +348,11 @@ def get_pe_matching_query(amount_condition, account_from_to, transaction):
341
348
currency_field = "paid_to_account_currency as currency"
342
349
else :
343
350
currency_field = "paid_from_account_currency as currency"
351
+
352
+ amount_filter = "True"
353
+ if amount_condition :
354
+ amount_filter = f"paid_amount { amount_condition } %(amount)s"
355
+
344
356
return f"""
345
357
SELECT
346
358
(CASE WHEN reference_no=%(reference_no)s THEN 1 ELSE 0 END
@@ -358,7 +370,7 @@ def get_pe_matching_query(amount_condition, account_from_to, transaction):
358
370
FROM
359
371
`tabPayment Entry`
360
372
WHERE
361
- paid_amount { amount_condition } %(amount)s
373
+ { amount_filter }
362
374
AND docstatus = 1
363
375
AND payment_type IN (%(payment_type)s, 'Internal Transfer')
364
376
AND ifnull(clearance_date, '') = ""
@@ -373,8 +385,12 @@ def get_je_matching_query(amount_condition, transaction):
373
385
# So one bank could have both types of bank accounts like asset and liability
374
386
# So cr_or_dr should be judged only on basis of withdrawal and deposit and not account type
375
387
cr_or_dr = "credit" if transaction .withdrawal > 0 else "debit"
376
- return f"""
377
388
389
+ amount_filter = ""
390
+ if amount_condition :
391
+ amount_filter = f"AND jea.{ cr_or_dr } _in_account_currency { amount_condition } %(amount)s"
392
+
393
+ return f"""
378
394
SELECT
379
395
(CASE WHEN je.cheque_no=%(reference_no)s THEN 1 ELSE 0 END
380
396
+ 1) AS rank ,
@@ -396,13 +412,17 @@ def get_je_matching_query(amount_condition, transaction):
396
412
WHERE
397
413
(je.clearance_date is null or je.clearance_date='0000-00-00')
398
414
AND jea.account = %(bank_account)s
399
- AND jea. { cr_or_dr } _in_account_currency { amount_condition } %(amount)s
415
+ { amount_filter }
400
416
AND je.docstatus = 1
401
417
"""
402
418
403
419
404
420
def get_si_matching_query (amount_condition ):
405
421
# get matchin sales invoice query
422
+ amount_filter = ""
423
+ if amount_condition :
424
+ amount_filter = f"AND sip.amount { amount_condition } %(amount)s"
425
+
406
426
return f"""
407
427
SELECT
408
428
( CASE WHEN si.customer = %(party)s THEN 1 ELSE 0 END
@@ -425,12 +445,16 @@ def get_si_matching_query(amount_condition):
425
445
sip.parent = si.name
426
446
WHERE (sip.clearance_date is null or sip.clearance_date='0000-00-00')
427
447
AND sip.account = %(bank_account)s
428
- AND sip.amount { amount_condition } %(amount)s
448
+ { amount_filter }
429
449
AND si.docstatus = 1
430
450
"""
431
451
432
452
def get_pi_matching_query (amount_condition ):
433
453
# get matching purchase invoice query
454
+ amount_filter = "True"
455
+ if amount_condition :
456
+ amount_filter = f"paid_amount { amount_condition } %(amount)s"
457
+
434
458
return f"""
435
459
SELECT
436
460
( CASE WHEN supplier = %(party)s THEN 1 ELSE 0 END
@@ -447,7 +471,7 @@ def get_pi_matching_query(amount_condition):
447
471
FROM
448
472
`tabPurchase Invoice`
449
473
WHERE
450
- paid_amount { amount_condition } %(amount)s
474
+ { amount_filter }
451
475
AND docstatus = 1
452
476
AND is_paid = 1
453
477
AND ifnull(clearance_date, '') = ""
@@ -460,6 +484,11 @@ def get_ec_matching_query(bank_account, company, amount_condition):
460
484
filters = {"default_account" : bank_account }, fields = ["parent" ])]
461
485
mode_of_payments = '(\' ' + '\' , \' ' .join (mode_of_payments ) + '\' )'
462
486
company_currency = get_company_currency (company )
487
+
488
+ amount_filter = "True"
489
+ if amount_condition :
490
+ amount_filter = f"total_sanctioned_amount { amount_condition } %(amount)s"
491
+
463
492
return f"""
464
493
SELECT
465
494
( CASE WHEN employee = %(party)s THEN 1 ELSE 0 END
@@ -476,7 +505,7 @@ def get_ec_matching_query(bank_account, company, amount_condition):
476
505
FROM
477
506
`tabExpense Claim`
478
507
WHERE
479
- total_sanctioned_amount { amount_condition } %(amount)s
508
+ { amount_filter }
480
509
AND docstatus = 1
481
510
AND is_paid = 1
482
511
AND ifnull(clearance_date, '') = ""
0 commit comments