15天后和45天脚本后发送邮件

I realize a php script that will run in cron every day that allows the sending of an email 15 days after the passage of the order. The script will send a first e-mail, this e-mail is registered in a table for historized e-mail transactions and then 45 days after the date of the order a second e-mail will be sent and it will also be logged in the table.

I'm blocking at the date level that records the first email in the table but never the second. Here is my script:

public function cron()
{
    $state = Db::getInstance()->executeS('
        SELECT o.id_order, o.total_products_wt, o.total_paid_real, op.date_add, op.order_reference, c.id_customer, c.firstname, c.lastname, c.email, o.current_state, o.id_cart, o.date_add
        FROM ' . _DB_PREFIX_ . 'orders o
        LEFT JOIN ' . _DB_PREFIX_ . 'order_payment op ON (o.reference = op.order_reference)
        LEFT JOIN ' . _DB_PREFIX_ . 'customer c ON (o.id_customer = c.id_customer)
        WHERE o.current_state = ' . Configuration::get('BLOCKPAYMENT_STATE_1') . '
        OR o.current_state = ' . Configuration::get('BLOCKPAYMENT_STATE_2'));

    foreach ($state as $item) {
        $historyMail = $this->getHistory((int)$item['id_order']);

        $paiement = 0;

        // Days diff
        $datetime1 = new DateTime(date('Y-m-d'));
        $datetime2 = new DateTime($item['date_add']);
        $interval = $datetime1->diff($datetime2);
        $diff = $interval->format('%a');

        if ($historyMail == null && $diff == 15) {
            $paiement = 2;
        } elseif ($historyMail != null && $diff >= 45) {
            $paiement = 3;
        }

        /*dump($item);
        dump($paiement);
        dump($diff);*/

        if ((int)$item['current_state'] == (int)Configuration::get('BLOCKPAYMENT_STATE_1')) {
            $stateTitle = 'Paiement 2';
            $important = 'Vous recevrez sous quinzaine la dernière échèance de paiement par email.';
        } elseif ((int)$item['current_state'] == (int)Configuration::get('BLOCKPAYMENT_STATE_2')) {
            $stateTitle = 'Paiement 3';
            $important = '';
        } else {
            continue;
        }

        if ($this->is_ssl() != false) {
            $ssl = 'https://';
        } else {
            $ssl = 'http://';
        }

        $data = [
            '{state}' => $stateTitle,
            '{important}' => $important,
            '{firstname}' => $item['firstname'],
            '{lastname}' => $item['lastname'],
            '{reference}' => $item['order_reference'],
            '{date}' => $item['date_add'],
            '{link}' => $ssl . $_SERVER['HTTP_HOST'] . '/module/blockpayment/validation?id_cart=' . $item['id_cart']
        ];

        // Cas paiement 2: première relance
        if ($paiement == 2) {
            $history = new Historymailpayment();
            $history->id_order = (int)$item['id_order'];
            $history->id_customer = (int)$item['id_customer'];
            $history->type = $paiement;
            $history->date_send = date('Y-m-d H:i:s');

            $this->sendEmail($stateTitle, $data, $item['email'], $item['firstname'] . ' ' . $item['lastname'], 1);
            $history->save();
        } elseif ($paiement == 3) {
            $history = new Historymailpayment();
            $history->id_order = (int)$item['id_order'];
            $history->id_customer = (int)$item['id_customer'];
            $history->type = $paiement;
            $history->date_send = date('Y-m-d H:i:s');

            $this->sendEmail($stateTitle, $data, $item['email'], $item['firstname'] . ' ' . $item['lastname'], 1);
            $history->save();
        }
    }
}

The status "BLOCKPAYMENT_STATE_1" corresponds to the first email and "BLOCKPAYMENT_STATE_2" to the second email

I currently have 3 commands:

1- Ordered 16/10/18 in status 1 which should send the first email and save this transaction in the table 2- Ordered on 09/15/18 in status 2, which should send the second email and record a new line in the table 3- Ordered on 09/15/18 in status 2, which should send the second email and record a new line in the table

when I run the cron my table only adds one line

history

when I restart it no other line is added and only the first mail is sent.

Thank you for help