2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2020-06-12 20:03:43 +00:00
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
2018-03-25 17:30:42 +00:00
module UpdateDonation
def self . from_followup ( donation , params )
donation . designation = params [ :designation ] if params [ :designation ] . present?
donation . dedication = params [ :dedication ] if params [ :dedication ] . present?
donation . comment = params [ :comment ] if params [ :comment ] . present?
donation . save
2019-07-30 21:29:24 +00:00
donation
2018-03-25 17:30:42 +00:00
end
# @param [Integer] donation_id the donation for the payment you wish to modify
def self . update_payment ( donation_id , data )
2019-07-30 21:29:24 +00:00
ParamValidation . new ( { id : donation_id , data : data } ,
id : { required : true , is_reference : true } ,
data : { required : true , is_hash : true } )
2018-03-25 17:30:42 +00:00
existing_payment = Payment . where ( 'donation_id = ?' , donation_id ) . last
unless existing_payment
raise ParamValidation :: ValidationError . new ( " #{ donation_id } is does not correspond to a valid donation " ,
2019-07-30 21:29:24 +00:00
key : :id )
2018-03-25 17:30:42 +00:00
end
is_offsite = ! existing_payment . offsite_payment . nil?
validations = {
2019-07-30 21:29:24 +00:00
designation : { is_a : String } ,
dedication : { is_a : String } ,
comment : { is_a : String } ,
campaign_id : { is_reference : true , required : true } ,
event_id : { is_reference : true , required : true }
2018-03-25 17:30:42 +00:00
}
if is_offsite
# if offline test the other values (fee_total, gross_amount, check_number, date)
#
2019-07-30 21:29:24 +00:00
validations . merge! ( gross_amount : { is_integer : true , min : 1 } ,
fee_total : { is_integer : true } ,
check_number : { is_a : String } ,
date : { can_be_date : true } )
2018-03-25 17:30:42 +00:00
end
ParamValidation . new ( data , validations )
2019-07-30 21:29:24 +00:00
set_to_nil = { campaign : data [ :campaign_id ] == '' , event : data [ :event_id ] == '' }
2018-03-25 17:30:42 +00:00
# validate campaign and event ids if there and if they belong to nonprofit
2019-07-30 21:29:24 +00:00
if set_to_nil [ :campaign ]
2018-03-25 17:30:42 +00:00
campaign = nil
else
campaign = Campaign . where ( 'id = ?' , data [ :campaign_id ] ) . first
unless campaign
2019-07-30 21:29:24 +00:00
raise ParamValidation :: ValidationError . new ( " #{ data [ :campaign_id ] } is not a valid campaign " , key : :campaign_id )
2018-03-25 17:30:42 +00:00
end
unless campaign . nonprofit == existing_payment . nonprofit
2019-07-30 21:29:24 +00:00
raise ParamValidation :: ValidationError . new ( " #{ data [ :campaign_id ] } campaign does not belong to this nonprofit for payment #{ existing_payment . id } " , key : :campaign_id )
2018-03-25 17:30:42 +00:00
end
end
2019-07-30 21:29:24 +00:00
if set_to_nil [ :event ]
2018-03-25 17:30:42 +00:00
event = nil
else
event = Event . where ( 'id = ?' , data [ :event_id ] ) . first
unless event
2019-07-30 21:29:24 +00:00
raise ParamValidation :: ValidationError . new ( " #{ data [ :event_id ] } is not a valid event " , key : :event_id )
2018-03-25 17:30:42 +00:00
end
unless event . nonprofit == existing_payment . nonprofit
2019-07-30 21:29:24 +00:00
raise ParamValidation :: ValidationError . new ( " #{ data [ :event_id ] } event does not belong to this nonprofit for payment #{ existing_payment . id } " , key : :event_id )
2018-03-25 17:30:42 +00:00
end
end
2021-03-06 21:54:01 +00:00
donation = existing_payment . donation
ret = donation . attributes
2018-03-25 17:30:42 +00:00
Qx . transaction do
donation . designation = data [ :designation ] if data [ :designation ]
donation . comment = data [ :comment ] if data [ :comment ]
donation . dedication = data [ :dedication ] if data [ :dedication ]
donation . event = event if event
donation . event = nil if data [ :event_id ] == ''
donation . campaign = campaign if campaign
donation . campaign = nil if data [ :campaign_id ] == ''
if is_offsite
donation . amount = data [ :gross_amount ] if data [ :gross_amount ]
donation . date = data [ :date ] if data [ :date ]
end
2021-02-08 20:55:31 +00:00
modern_donation = donation . modern_donation
trx = modern_donation . trx
2018-03-25 17:30:42 +00:00
# edits_to_payments
if is_offsite
2019-07-30 21:29:24 +00:00
# if offline, set date, gross_amount, fee_total, net_amount
2018-10-03 21:02:54 +00:00
existing_payment . towards = data [ :designation ] if data [ :designation ]
2018-03-25 17:30:42 +00:00
existing_payment . date = data [ :date ] if data [ :date ]
existing_payment . gross_amount = data [ :gross_amount ] if data [ :gross_amount ]
existing_payment . fee_total = data [ :fee_total ] if data [ :fee_total ]
existing_payment . net_amount = existing_payment . gross_amount - existing_payment . fee_total
2021-02-08 20:55:31 +00:00
if existing_payment . changed?
existing_payment . save!
if existing_payment . previous_changes . has_key? 'gross_amount'
modern_donation . amount = existing_payment . gross_amount
trx . amount = existing_payment . gross_amount
end
end
2018-03-25 17:30:42 +00:00
else
if donation . designation
2019-07-30 21:29:24 +00:00
Payment . where ( 'donation_id = ?' , donation . id ) . update_all ( towards : donation . designation , updated_at : Time . now )
2018-03-25 17:30:42 +00:00
end
end
2019-07-30 21:29:24 +00:00
# if offsite, set check_number, date, gross_amount
2018-03-25 17:30:42 +00:00
if is_offsite
offsite_payment = existing_payment . offsite_payment
offsite_payment . check_number = data [ :check_number ] if data [ :check_number ]
offsite_payment . date = data [ :date ] if data [ :date ]
offsite_payment . gross_amount = data [ :gross_amount ] if data [ :gross_amount ]
2019-07-30 21:29:24 +00:00
offsite_payment . save! if offsite_payment . changed?
2018-03-25 17:30:42 +00:00
end
2021-02-08 20:55:31 +00:00
trx . save! if trx . changed?
2019-07-30 21:29:24 +00:00
donation . save! if donation . changed?
2021-02-08 20:55:31 +00:00
md_changed = modern_donation . changed?
modern_donation . save! if modern_donation . changed?
if ( [ 'dedication' , 'designation' ] . any? { | i | donation . previous_changes . has_key? i } || md_changed )
modern_donation . publish_updated
end
2018-03-25 17:30:42 +00:00
existing_payment . reload
ret = donation . attributes
ret [ :payment ] = existing_payment . attributes
2019-07-30 21:29:24 +00:00
ret [ :offsite_payment ] = offsite_payment . attributes if is_offsite
2018-03-25 17:30:42 +00:00
end
2021-03-06 21:54:01 +00:00
ret
2018-03-25 17:30:42 +00:00
end
end