<commit_before>#!/usr/bin/php -q
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file is part of A2Billing (http://www.a2billing.net/)
 *
 * A2Billing, Commercial Open Source Telecom Billing platform,
 * powered by Star2billing S.L. <http://www.star2billing.com/>
 *
 * @copyright   Copyright (C) 2004-2012 - Star2billing S.L.
 * @author      <NAME> <<EMAIL>>
 * @license     http://www.fsf.org/licensing/licenses/agpl-3.0.html
 * @package     A2Billing
 *
 * Software License Agreement (GNU Affero General Public License)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
**/

/***************************************************************************
 *            a2billing_subscription_fee.php
 *
 *  Purpose: manage the monthly services subscription
 *  Fri Feb 27 14:17:10 2007
 *  Copyright  2007  User : Areski
 *  ADD THIS SCRIPT IN A CRONTAB JOB
 *
    crontab -e
    0 6 * * * php /usr/local/a2billing/Cronjobs/a2billing_subscription_fee.php

    field	 allowed values
    -----	 --------------
    minute	 		0-59
    hour		 	0-23
    day of month	1-31
    month	 		1-12 (or names, see below)
    day of week	 	0-7 (0 or 7 is Sun, or use names)

    The sample above will run the script every 21 of each month at 10AM

****************************************************************************/

set_time_limit(0);
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));

include (dirname(__FILE__) . "/lib/admin.defines.php");
include (dirname(__FILE__) . "/lib/ProcessHandler.php");

if (!defined('PID')) {
    define("PID", "/var/run/a2billing/a2billing_subscription_fee_pid.php");
}

// CHECK IF THE CRONT PROCESS IS ALREADY RUNNING
$pH= new ProcessHandler();
if ($pH->isActive()) {
        die(); // Already running!
        } else {
                $pH->activate();
                }

$verbose_level = 1;

$groupcard = 5000;

$A2B = new A2Billing();
$A2B->load_conf($agi, NULL, 0, $idconfig);

write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[#### BATCH BEGIN ####]");

if (!$A2B->DbConnect()) {
    echo "[Cannot connect to the database]\n";
    write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[Cannot connect to the database]");
    exit;
}

$instance_table = new Table();

/*
    Pay_Status :
        0 : First USE
        1 : Billed
        2 : Paid
        3 : UnPaid
*/

$QUERY = 'SELECT count(*) FROM cc_card INNER JOIN cc_card_subscription ON cc_card.id = cc_card_subscription.id_cc_card INNER JOIN cc_subscription_service ON cc_card_subscription.id_subscription_fee=cc_subscription_service.id' .
' WHERE cc_subscription_service.status=1 AND cc_card_subscription.startdate < NOW() AND (cc_card_subscription.stopdate = "0000-00-00 00:00:00" OR cc_card_subscription.stopdate > NOW())'.
' AND cc_subscription_service.startdate < NOW() AND (cc_subscription_service.stopdate = "0000-00-00 00:00:00" OR cc_subscription_service.stopdate > NOW()) AND cc_card_subscription.paid_status !=3';

$result = $instance_table->SQLExec($A2B->DBHandle, $QUERY);
$nb_card = $result[0][0];
$nbpagemax = (ceil($nb_card / $groupcard));
if ($verbose_level >= 1)
    echo "===> NB_CARD : $nb_card - NBPAGEMAX:$nbpagemax\n";

if (!($nb_card > 0)) {
    if ($verbose_level >= 1)
        echo "[No card to run the Subscription service]\n";
    write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[No card to run the Subscription Feeservice]");
    exit ();
}

$billdaybefor_anniversary = $A2B->config['global']['subscription_bill_days_before_anniversary'];

$currencies_list = get_currencies($A2B->DBHandle);

$service_array = array();

for ($page = 0; $page < $nbpagemax; $page++) {

    $sql = 'SELECT cc_card.id card_id ,cc_subscription_service.id service_id, cc_subscription_service.label, cc_subscription_service.fee, cc_subscription_service.emailreport,DATE(cc_card_subscription.startdate) startdate , cc_card_subscription.paid_status , cc_card_subscription.last_run, cc_card_subscription.next_billing_date , cc_card_subscription.limit_pay_date , cc_card_subscription.id card_subscription_id, cc_card_subscription.product_name product_name'.
    ' FROM cc_card INNER JOIN cc_card_subscription ON cc_card.id = cc_card_subscription.id_cc_card  INNER JOIN cc_subscription_service ON cc_card_subscription.id_subscription_fee=cc_subscription_service.id '  .
    ' WHERE cc_subscription_service.status=1 AND cc_card_subscription.startdate < NOW() AND (cc_card_subscription.stopdate = "0000-00-00 00:00:00" OR cc_card_subscription.stopdate > NOW())'.
    ' AND cc_subscription_service.startdate < NOW() AND (cc_subscription_service.stopdate = "0000-00-00 00:00:00" OR cc_subscription_service.stopdate > NOW()) AND cc_card_subscription.paid_status !=3'.
    ' ORDER BY cc_card.id';

    if ($A2B->config["database"]['dbtype'] == "postgres") {
        $sql .= " LIMIT $groupcard OFFSET " . $page * $groupcard;
    } else {
        $sql .= " LIMIT " . $page * $groupcard . ", $groupcard";
    }

    $result_subscriptions = $instance_table->SQLExec($A2B->DBHandle, $sql);

    foreach ($result_subscriptions as $subscription) {
        $service_id = $subscription['service_id'];

        if (!is_array($service_array[$service_id])) $service_array[$service_id] = array("totalcardperform" => 0 , "totalcredit" => 0 );

        $action = "";

        switch ($subscription['paid_status']) {

            case 0:
                //firstuse : billed
                $action = "bill";
                $unix_startdate = strtotime($subscription['startdate']);
                $day_now = date("j");
                $last_run = date("Y-m-d");
                $day_startdate = date("j",$unix_startdate);
                $month_startdate = date("m",$unix_startdate);
                $year_startdate= date("Y",$unix_startdate);
                $lastday_of_startdate_month = lastDayOfMonth($month_startdate,$year_startdate,"j");

                $next_bill_date = strtotime("01-$month_startdate-$year_startdate + 1 month");
                $lastday_of_next_month= lastDayOfMonth(date("m",$next_bill_date),date("Y",$next_bill_date),"j");

                $limite_pay_date = date("Y-m-d",strtotime(" + $billdaybefor_anniversary day")) ;

                if ($day_startdate>$lastday_of_next_month) {
                    $next_limite_pay_date = date ("$lastday_of_next_month-m-Y" ,$next_bill_date);
                } else {
                    $next_limite_pay_date = date ("$day_startdate-m-Y" ,$next_bill_date);
                }

                $next_bill_date = date("Y-m-d",strtotime("$next_limite_pay_date - $billdaybefor_anniversary day")) ;
                break;

            case 1:
                // billed : check if out of date -> unpaid
                // date('m',strtotime($mycard['last_run']));
                $unix_limit = strtotime($subscription['limit_pay_date']);
                $unix_now = strtotime(date("d-m-Y"));

                if ($unix_now>$unix_limit) {
                    $action = "unpaid";
                }

                break;

            case 2:
                // paid : check if the system have to bill it again
                $unix_bill_time = strtotime($subscription['next_billing_date']);
                $unix_now = strtotime(date("d-m-Y"));
                if ($unix_now>=$unix_limit) {
                    $action = "bill";

                    $unix_startdate = strtotime($subscription['startdate']);
                    $last_run = date("Y-m-d");

                    $day_startdate = date("j",$unix_startdate);
                    $month_lastbill_date = date("m",$unix_bill_time);
                    $year_lastbill_date = date("Y",$unix_bill_time);
                    $lastday_of_next_billmonth = lastDayOfMonth($month_lastbill_date,$year_lastbill_date,"j");

                    $next_bill_date = strtotime("01-$month_lastbill_date-$year_lastbill_date + 1 month");
                    $lastday_of_next_month= lastDayOfMonth(date("m",$next_bill_date),date("Y",$next_bill_date),"j");

                    $limite_pay_date = date("Y-m-d",strtotime(" + $billdaybefor_anniversary day")) ;

                    if ($day_startdate>$lastday_of_next_month) {
                        $next_limite_pay_date = date ("$lastday_of_next_month-m-Y" ,$next_bill_date);
                    } else {
                        $next_limite_pay_date = date ("$day_startdate-m-Y" ,$next_bill_date);
                    }
<commit_msg>Fix bug on subscription fee<commit_after>#!/usr/bin/php -q
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file is part of A2Billing (http://www.a2billing.net/)
 *
 * A2Billing, Commercial Open Source Telecom Billing platform,
 * powered by Star2billing S.L. <http://www.star2billing.com/>
 *
 * @copyright   Copyright (C) 2004-2012 - Star2billing S.L.
 * @author      <NAME> <<EMAIL>>
 * @license     http://www.fsf.org/licensing/licenses/agpl-3.0.html
 * @package     A2Billing
 *
 * Software License Agreement (GNU Affero General Public License)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
**/

/***************************************************************************
 *            a2billing_subscription_fee.php
 *
 *  Purpose: manage the monthly services subscription
 *  Fri Feb 27 14:17:10 2007
 *  Copyright  2007  User : Areski
 *  ADD THIS SCRIPT IN A CRONTAB JOB
 *
    crontab -e
    0 6 * * * php /usr/local/a2billing/Cronjobs/a2billing_subscription_fee.php

    field	 allowed values
    -----	 --------------
    minute	 		0-59
    hour		 	0-23
    day of month	1-31
    month	 		1-12 (or names, see below)
    day of week	 	0-7 (0 or 7 is Sun, or use names)

    The sample above will run the script every 21 of each month at 10AM

****************************************************************************/

set_time_limit(0);
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));

include (dirname(__FILE__) . "/lib/admin.defines.php");
include (dirname(__FILE__) . "/lib/ProcessHandler.php");

if (!defined('PID')) {
    define("PID", "/var/run/a2billing/a2billing_subscription_fee_pid.php");
}

// CHECK IF THE CRONT PROCESS IS ALREADY RUNNING
$pH= new ProcessHandler();
if ($pH->isActive()) {
        die(); // Already running!
        } else {
                $pH->activate();
                }

$verbose_level = 1;

$groupcard = 5000;

$A2B = new A2Billing();
$A2B->load_conf($agi, NULL, 0, $idconfig);

write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[#### BATCH BEGIN ####]");

if (!$A2B->DbConnect()) {
    echo "[Cannot connect to the database]\n";
    write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[Cannot connect to the database]");
    exit;
}

$instance_table = new Table();

/*
    Pay_Status :
        0 : First USE
        1 : Billed
        2 : Paid
        3 : UnPaid
*/

$QUERY = 'SELECT count(*) FROM cc_card INNER JOIN cc_card_subscription ON cc_card.id = cc_card_subscription.id_cc_card INNER JOIN cc_subscription_service ON cc_card_subscription.id_subscription_fee=cc_subscription_service.id' .
' WHERE cc_subscription_service.status=1 AND cc_card_subscription.startdate < NOW() AND (cc_card_subscription.stopdate = "0000-00-00 00:00:00" OR cc_card_subscription.stopdate > NOW())'.
' AND cc_subscription_service.startdate < NOW() AND (cc_subscription_service.stopdate = "0000-00-00 00:00:00" OR cc_subscription_service.stopdate > NOW()) AND cc_card_subscription.paid_status !=3';

$result = $instance_table->SQLExec($A2B->DBHandle, $QUERY);
$nb_card = $result[0][0];
$nbpagemax = (ceil($nb_card / $groupcard));
if ($verbose_level >= 1)
    echo "===> NB_CARD : $nb_card - NBPAGEMAX:$nbpagemax\n";

if (!($nb_card > 0)) {
    if ($verbose_level >= 1)
        echo "[No card to run the Subscription service]\n";
    write_log(LOGFILE_CRONT_SUBSCRIPTIONFEE, basename(__FILE__) . ' line:' . __LINE__ . "[No card to run the Subscription Feeservice]");
    exit ();
}

$billdaybefor_anniversary = $A2B->config['global']['subscription_bill_days_before_anniversary'];

$currencies_list = get_currencies($A2B->DBHandle);

$service_array = array();

for ($page = 0; $page < $nbpagemax; $page++) {

    $sql = 'SELECT cc_card.id card_id ,cc_subscription_service.id service_id, cc_subscription_service.label, cc_subscription_service.fee, cc_subscription_service.emailreport,DATE(cc_card_subscription.startdate) startdate , cc_card_subscription.paid_status , cc_card_subscription.last_run, cc_card_subscription.next_billing_date , cc_card_subscription.limit_pay_date , cc_card_subscription.id card_subscription_id, cc_card_subscription.product_name product_name'.
    ' FROM cc_card INNER JOIN cc_card_subscription ON cc_card.id = cc_card_subscription.id_cc_card  INNER JOIN cc_subscription_service ON cc_card_subscription.id_subscription_fee=cc_subscription_service.id '  .
    ' WHERE cc_subscription_service.status=1 AND cc_card_subscription.startdate < NOW() AND (cc_card_subscription.stopdate = "0000-00-00 00:00:00" OR cc_card_subscription.stopdate > NOW())'.
    ' AND cc_subscription_service.startdate < NOW() AND (cc_subscription_service.stopdate = "0000-00-00 00:00:00" OR cc_subscription_service.stopdate > NOW()) AND cc_card_subscription.paid_status !=3'.
    ' ORDER BY cc_card.id';

    if ($A2B->config["database"]['dbtype'] == "postgres") {
        $sql .= " LIMIT $groupcard OFFSET " . $page * $groupcard;
    } else {
        $sql .= " LIMIT " . $page * $groupcard . ", $groupcard";
    }

    $result_subscriptions = $instance_table->SQLExec($A2B->DBHandle, $sql);

    foreach ($result_subscriptions as $subscription) {
        $service_id = $subscription['service_id'];

        if (!is_array($service_array[$service_id])) $service_array[$service_id] = array("totalcardperform" => 0 , "totalcredit" => 0 );

        $action = "";

        switch ($subscription['paid_status']) {

            case 0:
                //firstuse : billed
                $action = "bill";
                $unix_startdate = strtotime($subscription['startdate']);
                $day_now = date("j");
                $last_run = date("Y-m-d");
                $day_startdate = date("j",$unix_startdate);
                $month_startdate = date("m",$unix_startdate);
                $year_startdate= date("Y",$unix_startdate);
                $lastday_of_startdate_month = lastDayOfMonth($month_startdate,$year_startdate,"j");

                $next_bill_date = strtotime("01-$month_startdate-$year_startdate + 1 month");
                $lastday_of_next_month= lastDayOfMonth(date("m",$next_bill_date),date("Y",$next_bill_date),"j");

                $limite_pay_date = date("Y-m-d",strtotime(" + $billdaybefor_anniversary day")) ;

                if ($day_startdate>$lastday_of_next_month) {
                    $next_limite_pay_date = date ("$lastday_of_next_month-m-Y" ,$next_bill_date);
                } else {
                    $next_limite_pay_date = date ("$day_startdate-m-Y" ,$next_bill_date);
                }

                $next_bill_date = date("Y-m-d",strtotime("$next_limite_pay_date - $billdaybefor_anniversary day")) ;
                break;

            case 1:
                // billed : check if out of date -> unpaid
                // date('m',strtotime($mycard['last_run']));
                $unix_limit = strtotime($subscription['limit_pay_date']);
                $unix_now = strtotime(date("d-m-Y"));

                if ($unix_now>$unix_limit) {
                    $action = "unpaid";
                }

                break;

            case 2:
                // paid : check if the system have to bill it again
                $unix_bill_time = strtotime($subscription['next_billing_date']);
                $unix_now = strtotime(date("d-m-Y"));
                if ($unix_now>=$unix_bill_time) {
                    $action = "bill";

                    $unix_startdate = strtotime($subscription['startdate']);
                    $last_run = date("Y-m-d");

                    $day_startdate = date("j",$unix_startdate);
                    $month_lastbill_date = date("m",$unix_bill_time);
                    $year_lastbill_date = date("Y",$unix_bill_time);
                    $lastday_of_next_billmonth = lastDayOfMonth($month_lastbill_date,$year_lastbill_date,"j");

                    $next_bill_date = strtotime("01-$month_lastbill_date-$year_lastbill_date + 1 month");
                    $lastday_of_next_month= lastDayOfMonth(date("m",$next_bill_date),date("Y",$next_bill_date),"j");

                    $limite_pay_date = date("Y-m-d",strtotime(" + $billdaybefor_anniversary day")) ;

                    if ($day_startdate>$lastday_of_next_month) {
                        $next_limite_pay_date = date ("$lastday_of_next_month-m-Y" ,$next_bill_date);
                    } else {
                        $next_limite_pay_date = date ("$day_startdate-m-Y" ,$next_bill_date);
                    }

{'fraction_non_alphanumeric': 0.14279447247203333, 'fraction_numerical': 0.017657381004606273, 'mean_word_length': 3.0107763360457445, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938922', 'n_tokens_mistral': 6160, 'n_tokens_neox': 5810, 'n_words': 1224}