diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index 8f26abb..52a14c2 100644 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -552,6 +552,7 @@ use Monolog\Logger; ]; } + // Pass on all targets to deduplicate destinations, remove number in sms stops, etc. $used_destinations = []; foreach ($targets as $key => $target) @@ -603,7 +604,7 @@ use Monolog\Logger; Choose phone if no phone defined for message Phones are choosen using type, priority and remaining volume : 1 - If sms is a mms, try to use mms phone if any available. If mms phone available use mms phone, else use default. - 2 - In group of phones, keep only phones with remaining volume. If no phones with remaining volume, keep all. + 2 - In group of phones, keep only phones with remaining volume. If no phones with remaining volume, use all phones instead. 3 - Groupe keeped phones by priority get group with biggest priority. 4 - Get a random phone in this group. 5 - If their is no phone matching, keep phone at null so sender will directly mark it as failed @@ -612,14 +613,15 @@ use Monolog\Logger; if (null === $phone_to_use) { $phones_subset = $users_phones[$id_user]; - if ($scheduled['mms'] && count($users_mms_phones[$id_user])) + if ($scheduled['mms']) { - $phones_subset = $users_mms_phones[$id_user]; + $phones_subset = $users_mms_phones[$id_user] ?: $phones_subset; } - $phones_subset = array_filter($phones_subset, function ($phone) { + $remaining_volume_phones = array_filter($phones_subset, function ($phone) { return $phone['remaining_volume'] > 0; }); + $phones_subset = $remaining_volume_phones ?: $phones_subset; $max_priority_phones = []; $max_priority = PHP_INT_MIN; @@ -640,14 +642,20 @@ use Monolog\Logger; } } - if ($max_priority_phones) + $phones_subset = $max_priority_phones; + if ($phones_subset) { - $phones_subset = $max_priority_phones; $random_phone = $phones_subset[array_rand($phones_subset)]; } } + + // This should only happen if the user try to send a message without any phone in his account, then we simply ignore. + if (!$random_phone && !$phone_to_use) + { + continue; + } - $id_phone = $phone_to_use['id'] ?? $random_phone['id'] ?? null; + $id_phone = $phone_to_use['id'] ?? $random_phone['id']; $sms_per_scheduled[$id_scheduled][] = [ 'id_user' => $id_user, 'id_scheduled' => $id_scheduled, @@ -659,14 +667,11 @@ use Monolog\Logger; 'text' => $text, ]; - // If we found a matching phone, consume one sms from remaining volume, dont forget mms phone - if ($id_phone) + // Consume one sms from remaining volume of phone, dont forget to do the same for the entry in mms phones + $users_phones[$id_user][$id_phone]['remaining_volume'] --; + if ($users_mms_phones[$id_user][$id_phone] ?? false) { - $users_phones[$id_user][$id_phone]['remaining_volume'] --; - if ($users_mms_phones[$id_user][$id_phone] ?? false) - { - $users_mms_phones[$id_user][$id_phone] --; - } + $users_mms_phones[$id_user][$id_phone] --; } } } diff --git a/controllers/internals/Sended.php b/controllers/internals/Sended.php index 9cce107..318009d 100644 --- a/controllers/internals/Sended.php +++ b/controllers/internals/Sended.php @@ -179,6 +179,20 @@ namespace controllers\internals; return $this->get_model()->get_by_uid_and_adapter_for_user($id_user, $uid, $adapter); } + /** + * Get number of sended SMS since a date for a phone + * + * @param int $id_user : User id + * @param int $id_phone : Phone id we want the number of sended message for + * @param \DateTime $since : Date since which we want sended number + * + * @return int + */ + public function count_since_for_phone_and_user(int $id_user, int $id_phone, \DateTime $since): int + { + return $this->get_model()->count_since_for_phone_and_user($id_user, $id_phone, $since); + } + /** * Get number of sended SMS for every date since a date for a specific user. * diff --git a/daemons/Sender.php b/daemons/Sender.php index 6f0176b..d462739 100644 --- a/daemons/Sender.php +++ b/daemons/Sender.php @@ -22,6 +22,7 @@ class Sender extends AbstractDaemon private $internal_phone; private $internal_scheduled; private $internal_received; + private $internal_sended; private $bdd; private $msg_queue; @@ -45,6 +46,7 @@ class Sender extends AbstractDaemon { //Create the internal controllers $this->internal_scheduled = new \controllers\internals\Scheduled($this->bdd); + $this->internal_sended = new \controllers\internals\Sended($this->bdd); //Get smss and transmit order to send to appropriate phone daemon $smss_per_scheduled = $this->internal_scheduled->get_smss_to_send(); @@ -62,8 +64,8 @@ class Sender extends AbstractDaemon { foreach ($smss_per_scheduled as $id_scheduled => $smss) { - //If queue not already exists - if (!msg_queue_exists(QUEUE_ID_PHONE) || !isset($this->queue)) + //If queue not already exists + if (!msg_queue_exists(QUEUE_ID_PHONE) || !isset($this->msg_queue)) { $this->msg_queue = msg_get_queue(QUEUE_ID_PHONE); } diff --git a/models/Sended.php b/models/Sended.php index b401ccc..1312085 100644 --- a/models/Sended.php +++ b/models/Sended.php @@ -178,6 +178,20 @@ namespace models; return $this->_select_one('sended', ['id_user' => $id_user, 'uid' => $uid, 'adapter' => $adapter]); } + /** + * Get number of sended SMS since a date for a phone + * + * @param int $id_user : User id + * @param int $id_phone : Phone id we want the number of sended message for + * @param \DateTime $since : Date since which we want sended number + * + * @return int + */ + public function count_since_for_phone_and_user(int $id_user, int $id_phone, \DateTime $since) : int + { + return $this->_count('sended', ['id_user' => $id_user, 'id_phone' => $id_phone, '>=at' => $since]); + } + /** * Get number of sended SMS for every date since a date for a specific user. *