magnussolution / magnusbilling7

MagnusBilling is a fast, secure, efficient, high availability, VOIP Billing.
https://www.magnusbilling.org
GNU Lesser General Public License v3.0
174 stars 106 forks source link

Loss preventation #591

Open uzair329 opened 2 years ago

uzair329 commented 2 years ago

Is your feature request related to a problem? Please describe. Right now their is no loss preventative system in magnus like other switches . So we face many time loss due to rate charged to client is low and we are getting high charged from vendor because as in trunk group it is method to select the nearest prefix and prefixes of every vendor is different or some vendors have more prefixes than other . Further, we did not change agents rate sheet every month it is not an easy task so, we upload only vendor rate sheet changing weekly so rate of vendor increases but we didn't change the rate of agent and so we sell call in loss

Describe the solution you'd like I would like to see their is an option of minimum profit marking on plan like in Sippy soft switch VOS etc their they can apply limit of rate that if sell - buy is less than 5% reject the call with error status rate issue or profit loss or anything which we can filter and find rate issues so we can change the rates of that prefix . Magnus is checking the LCR in trunk group right now after that their must be one more conditional check should be applied to check difference of sell and buy with percent formula applied in plan if its true reject call if its false call be process normally .

uzair329 commented 1 year ago

Any in this platform can work on this ? Or respond us ?

Aminuxer commented 1 year ago

Main idea for this - block calls with [Buy price] > [Sell price] in CDR BEFORE make call to trunk. It can be realized as option in Settings - Configuration.

uzair329 commented 1 year ago

Aminuxer i have checked their is no option like this in Settings

Aminuxer commented 1 year ago

Who good understand architecture of MagnusBilling ? Which code / script check tariff plans ? I can try modify this / make code-fix for support this feature.

Aminuxer commented 1 year ago

I make experimental fix for loss prevention:

https://github.com/Aminuxer/magnusbilling7/commit/f99416bc9cf4d40567da4410e358f3e1ea7598a7

I fix one SQL-query (Prefixes distribution in provider rates and clients rates don't have to match obe-to-one ;-) ) and add some code to ./mbilling/resources/asterisk/CalcAgi.php

https://github.com/Aminuxer/magnusbilling7/blob/source/resources/asterisk/CalcAgi.php

In asterisk -r log must appear this strings: -- "/var/www/html/mbilling/resources/asterisk/mbilling.php": AMIN-FIX LossPrevent, NUM XYYYcccddee, PREF XYYY, ClientCost: 2.800000, ProviderCost: 29.990000, Prov-iD 1; provider_test -- "/var/www/html/mbilling/resources/asterisk/mbilling.php": AMIN-FIX: Client cost for prefix XYYY, = 2.800000 less then ProviderCost: 29.990000, Prov-iD 1 provider_test; HANGUP!!

Caller got 403 Forbidden error at this case.

But CDR-Failed record will not added. How to add failed CDR record correctly ?

I mark my fixes with text AMIN-FIX in code and logs.

Aminuxer commented 1 year ago

https://github.com/magnussolution/magnusbilling7/pull/612

Pull request created

uzair329 commented 1 year ago

AMIN how magnus team will check its working perfect and if its working perfect when they will deploy in their update ?

Further will it work like client call destination 966573XXXXX have tariff rate 96657 0.16 and our one vendor have tariff rate 966573 0.17 will it block the call with forbidden or it should log in another table so we can check the reason and set the rate otherwise 403 is same response from vendor due to multiple reason of call blocking from their side . We need to get this rate logging in separate to check the reason

Aminuxer commented 1 year ago

I'm not member of MagnusSolution team and i start testing magnusbilling7 only two weeks ago. I don't have enough experience and product understanding nowtime for make beautiful realized feature in web-ui / options. I only found php-code which check uplinks and contain all necessary data.

I make it on my new server and test - feature work. For fast testing you can backup your file CalcAgi.php and use my patched version: https://raw.githubusercontent.com/Aminuxer/magnusbilling7/source/resources/asterisk/CalcAgi.php See AMIN-FIX comments.

In asterisk -r console alerts will appear anyway, but your trunk_group must has LCR type ! If your want only monitoring - comment line 643: # $MAGNUS->hangup($agi, 21); // Hangup and return 403 (see Magnus.php) to caller

Separated database logging in another table is sketchy and excessive idea - all data already present in pkg_cdr table. In case of succesfull call you can see money-loss calls by one simple SQL-query:

SELECT
   id_user, id_prefix, callerid, calledstation, starttime,
   sessionbill AS client_cost, buycost AS provider_cost
FROM `pkg_cdr`
  WHERE sessionbill < buycost
  LIMIT 50

Logging in cdr_failed table can be good idea. But now this don't proceed. Possibly need end call another way against simple hangup. I will try later modify my fix. Nowtime most critical task closed for me.

MagnusBilling primarily check Client 's tariff for make credit discharge. Providers tariff usually recorded in CDR only. More deep search of provider costs started only in case of LCR trunk groups, if i correctly understand source code.

I modified SQL -query for search providers rates directly from destination number - in your case LossPrevention must work too.

P.S. For comfortable database management from Web browser i use Adminer : https://www.adminer.org/