From 598d8683dcb83933854541b93074731e5da38ecc Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Mon, 21 Jun 2021 16:45:54 +0200 Subject: [PATCH] TT#127151 fix empty billing profile returned when searching by host IP Change-Id: Id1ebf820cf1fd8433933b0bfa1b26e380ef50ee3 --- db_scripts/diff/15682.up | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 db_scripts/diff/15682.up diff --git a/db_scripts/diff/15682.up b/db_scripts/diff/15682.up new file mode 100644 index 00000000..e6372d29 --- /dev/null +++ b/db_scripts/diff/15682.up @@ -0,0 +1,52 @@ +use billing; + +delimiter ;; +drop function get_billing_profile_by_contract_id_network;; +create function get_billing_profile_by_contract_id_network( + _contract_id int(11), + _epoch decimal(13,3), + _ip varchar(46) +) returns int(11) +deterministic reads sql data +begin + + declare _effective_start_date decimal(13,3); + declare _cbpn_id,_profile_id int(11); + declare _network_bytes varbinary(16); + declare _is_valid_ip,_is_ipv6 boolean default false; + + if _contract_id is null or _epoch is null then + return null; + end if; + set _network_bytes = inet6_aton(_ip); + set _is_valid_ip = if(_network_bytes is null or hex(_network_bytes) = "00000000",0,1); + set _is_ipv6 = if(locate(".",_ip) = 0,1,0); + + set _effective_start_date = (select max(cbpns.effective_start_time) from billing.contracts_billing_profile_network_schedule cbpns join + billing.contracts_billing_profile_network cbpn on cbpn.id = cbpns.profile_network_id + left join billing.billing_networks bn on cbpn.billing_network_id = bn.id + left join billing.billing_network_blocks bnb on bn.id = bnb.network_id + where cbpn.contract_id = _contract_id and cbpns.effective_start_time <= _epoch + and ((_is_valid_ip and if(_is_ipv6,bnb._ipv6_net_from <= _network_bytes and bnb._ipv6_net_to >= _network_bytes, + bnb._ipv4_net_from <= _network_bytes and bnb._ipv4_net_to >= _network_bytes)) or cbpn.billing_network_id is null)); + + if _effective_start_date is null then + set _cbpn_id = (billing.get_billing_profile_by_contract_id(_contract_id,_epoch)); + else + set _cbpn_id = (select cbpn.id from billing.contracts_billing_profile_network_schedule cbpns join + billing.contracts_billing_profile_network cbpn on cbpn.id = cbpns.profile_network_id + left join billing.billing_networks bn on cbpn.billing_network_id = bn.id + left join billing.billing_network_blocks bnb on bn.id = bnb.network_id + where cbpn.contract_id = _contract_id and cbpns.effective_start_time = _effective_start_date + and ((_is_valid_ip and if(_is_ipv6,bnb._ipv6_net_from <= _network_bytes and bnb._ipv6_net_to >= _network_bytes, + bnb._ipv4_net_from <= _network_bytes and bnb._ipv4_net_to >= _network_bytes)) or cbpn.billing_network_id is null) + order by cbpn.billing_network_id desc limit 1); + end if; + + set _profile_id = (select billing_profile_id from billing.contracts_billing_profile_network where id = _cbpn_id); + + return _profile_id; + +end;; + +delimiter ;