PHP code example of hippone / algerian-invoice-code-generator

1. Go to this page and download the library: Download hippone/algerian-invoice-code-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hippone / algerian-invoice-code-generator example snippets



declare(strict_types=1);

namespace MyNamespace\Persistence\SQLite;

use DateTimeImmutable;
use Hippone\InvoiceCode\Model\CodeComponents;
use Hippone\InvoiceCode\Model\IdentifiableProvider;
use PDO;

class SQLiteIdentifiableProvider implements IdentifiableProvider
{
    private PDO $pdo;
    
    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function nextCode(DateTimeImmutable $year): CodeComponents
    {
         $statement = $this->pdo->prepare('
                            SELECT COUNT(*) FROM invoices 
                            AS i 
                            WHERE strftime("%Y", i.created_at) = :currentYear
        ');
        $statement->bindValue(':currentYear', $year->format('Y'));
        $statement->execute();
        $currentCount = $statement->fetchColumn();
        $sequentialNumber = $currentCount + 1;
        return CodeComponents::from($sequentialNumber, $year);
    }
}

$codeGenerator = new CodeGenerator(new SQLiteIdentifiableProvider($pdo)); // assume PDO object is instantiated
$codeGenerator->generateForYear(DateTimeImmutable::createFromFormat('Y', '2021')); // replace 2021 by the desired value