Limit total minutes outbound calls for a extension monthly

Hello, all
I’ve to write dialplan which could make a limit extension to total minutes of outbound calls monthly. For example for one extension, every month should wasted no more than 100 minutes.
I think i need measure call time in mysql cdr!
But i don’t know to do it ?
Please everybody help me ?

SELECT sum(billsec) as totalminutes from cdr where MONTH(start)=month(CURDATE()) FROM cdr

Save the returned value of the query on a variable and then do the logic

A basic example would be something like this, of course you will need to add more logic and filter to your query

same => n,GotoIf($[${totalminutes}>=100]?minutesexceed)

Hi, ambiorixg12,

I want filter only a extension, and call answered.
Can you help me write script ?

Thank

I don’t think you want it for an extension. I think you want it for a local device, and I think you want it for incoming calls from that device, probably specifically ones that terminate on a non-local device. You would need to filter the result by either the caller ID or the source channel name, and use the destination channel name and/or extension to determine if the destination was non-local. If you write the dialplan correctly, billsec will only start when the call is answered.

If you want to cut them off in mid-call when they go over the limit, you will have difficulty if the phone supports more than one logical line from the same device, as is the case for most SIP phones.

Although I don’t do paid consultancy here, if you want someone to write the script for you, you need to indicate the payment terms you intend.

Hi, david,

I want limit a extension call out.
When a extension call out to a limit minutes in a month, user can’t call out.
I trying to write a sql query to filter cdr. ( I am a new beginer.)
Can you instruct or recommend me ?

Thank

Only devices can call out.

yes, david, I thought device ~ extension.

An extension is pattern that matches the first field in extensions.conf. It might not have a device at all. It might have many devices. It is best practice, although rarely observed, that it should not have the same name as any associated device.

(Note that FreePBX uses the term differently.)

In your case, you haven’t told us enough to even know whether a device is specific enough, e.g. a single device name may be used for all calls from the PSTN, but different caller IDs may require different treatment. These will have individual entries in sip.conf, but need not have extensions in extensions.conf (if they can’t receive calls), or may share an extension (if several phones ring at once).

I think others are generally assuming that you are talking about SIP devices on your own LAN which behave like single line telephone instruments.

Yes, david551,

I only wanna Sip device has a extension : 7777 call out.
and when dial to mobile, will be limit time call in a months.
Ex : in a month, Sip device have 1000 minutes dial to mobile.