@@ -67,8 +67,6 @@ const getPaymentsSum = async (receiptId) => {
67
67
*/
68
68
const getNewPaymentsSum = async ( receiptId ) => {
69
69
const receipt = await getById ( 'BillingReceipt' , receiptId )
70
- const billingContext = await getById ( 'BillingIntegrationOrganizationContext' , receipt . context )
71
- const account = await getById ( 'BillingAccount' , receipt . account )
72
70
const defaultConditions = [
73
71
{ status_in : [ PAYMENT_DONE_STATUS , PAYMENT_WITHDRAWN_STATUS ] } ,
74
72
{ deletedAt : null } ,
@@ -86,20 +84,27 @@ const getNewPaymentsSum = async (receiptId) => {
86
84
}
87
85
const conditionsByReceipt = [
88
86
{ receipt : { id : receiptId } } ,
87
+ ...defaultConditions ,
89
88
]
90
- const conditionsWithNoReceipt = [
91
- { receipt_is_null : true } ,
92
- { invoice_is_null : true } ,
93
- { organization : { id : billingContext . organization } } ,
94
- { period : receipt . period } ,
95
- { accountNumber : account . number } ,
96
- ]
97
- const payments = await find ( 'Payment' , {
98
- AND : [
99
- { AND : defaultConditions } ,
100
- { OR : [ { AND : conditionsByReceipt } , { AND : conditionsWithNoReceipt } ] } ,
101
- ] ,
89
+ let payments = await find ( 'Payment' , {
90
+ AND : conditionsByReceipt ,
102
91
} )
92
+ const billingContext = await getById ( 'BillingIntegrationOrganizationContext' , receipt . context )
93
+ const account = await getById ( 'BillingAccount' , receipt . account )
94
+ if ( billingContext && account ) {
95
+ const conditionsWithNoReceipt = [
96
+ { receipt_is_null : true } ,
97
+ { invoice_is_null : true } ,
98
+ { organization : { id : billingContext . organization } } ,
99
+ { period : receipt . period } ,
100
+ { accountNumber : account . number } ,
101
+ ...defaultConditions ,
102
+ ]
103
+ const qrPayments = await find ( 'Payment' , {
104
+ AND : conditionsWithNoReceipt ,
105
+ } )
106
+ payments = payments . concat ( qrPayments )
107
+ }
103
108
return payments . reduce ( ( total , current ) => ( Big ( total ) . plus ( current . amount ) ) , 0 ) . toFixed ( 8 ) . toString ( )
104
109
}
105
110
0 commit comments