PHP code example of scottlaurent / accounting

1. Go to this page and download the library: Download scottlaurent/accounting 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/ */

    

scottlaurent / accounting example snippets


use Scottlaurent\Accounting\ModelTraits\AccountingJournal;

class User extends Model
{
    use AccountingJournal;

    protected static function boot()
    {
        parent::boot();

        // Automatically create a journal when a user is created
        static::created(function ($user) {
            $user->initJournal();
        });
    }
}

// locate a user (or ANY MODEL that implements the AccountingJournal trait)
$user = User::find(1);

// locate a product (optional)
$product = Product::find(1);

// init a journal for this user (do this only once)
$user->initJournal();

// credit the user and reference the product
$transactionOne = $user->journal->creditDollars(100);
$transactionOne->referencesObject($product);

// check our balance (should be 100)
// Note: getCurrentBalanceInDollars() will return a positive number for credit balances
$currentBalance = $user->journal->getCurrentBalanceInDollars();

// debit the user
$transactionTwo = $user->journal->debitDollars(75);

// check our balance (should be 25)
// The balance will be positive if credits > debits, negative if debits > credits
$currentBalance = $user->journal->getCurrentBalanceInDollars();

// get the product referenced in the journal (optional)
$productCopy = $transactionOne->getReferencedObject();

    use Scottlaurent\Accounting\Transaction;
    
    // Create a new transaction group
    $transaction = Transaction::newDoubleEntryTransactionGroup();
    
    // Add transactions (debit and credit)
    $transaction->addDollarTransaction(
        $journal,     // Journal instance
        'debit',      // or 'credit'
        100.00,      // amount
        'Memo text'   // optional memo
    );
    
    // Commit the transaction (will throw if debits != credits)
    $transaction->commit();
    

    // this represents some kind of sale to a customer for $500 based on an invoiced amount of 500.
    $transactionGroup = Transaction::newDoubleEntryTransactionGroup();
    $transactionGroup->addDollarTransaction($user->journal, 'credit', 500);  // your user journal probably is an income ledger
    $transactionGroup->addDollarTransaction($this->companyAccountsReceivableJournal, 'debit', 500); // this is an asset ledger
    $transactionGroup->commit();
    

    // this represents payment in cash to satisfy that AR entry
    $transactionGroup = Transaction::newDoubleEntryTransactionGroup();
    $transactionGroup->addDollarTransaction($this->companyAccountsReceivableJournal, 'credit', 500);
    $transactionGroup->addDollarTransaction($this->companyCashJournal, 'debit', 500);
    $transactionGroup->commit();

    // at this point, our assets are 500 still and our income is 500.  If you review the code you will notice that assets and expenses are on the 'left' side of a balance sheet rollup and the liabilities and owners equity (and income) are rolled up on the right.  In that way, the left and right always stay in sync.  You could do an adjustment transaction of course to zero out expenses/income and transfer that to equity or however you do year-end or period-end clearances on your income/expense ledgers.
    

// Basic operations
$journal->debit(5000, 'Equipment purchase');           // Amount in cents
$journal->credit(2500, 'Payment received');            // Amount in cents

// Dollar convenience methods (recommended)
$journal->debitDollars(50.00, 'Office supplies');      // Amount in dollars
$journal->creditDollars(25.00, 'Refund issued');       // Amount in dollars

// Get balances
$currentBalance = $journal->getBalance();               // Money object
$dollarBalance = $journal->getBalanceInDollars();       // Float
$balanceOnDate = $journal->getBalanceOn($date);         // Money object

// Daily totals
$debitedToday = $journal->getDollarsDebitedToday();     // Float
$creditedToday = $journal->getDollarsCreditedToday();   // Float

use Scottlaurent\Accounting\Transaction;

// Create transaction group
$transaction = Transaction::newDoubleEntryTransactionGroup();

// Add transactions with proper camelCase parameters
$transaction->addDollarTransaction(
    journal: $journal,
    method: 'debit',
    value: 100.00,
    memo: 'Transaction description',
    referencedObject: $product,  // Optional reference
    postdate: Carbon::now()      // Optional date
);

// Commit (validates debits = credits)
$transactionGroupId = $transaction->commit();

use Scottlaurent\Accounting\Models\Ledger;
use Scottlaurent\Accounting\Enums\LedgerType;

// Create ledgers
$assetLedger = Ledger::create([
    'name' => 'Current Assets',
    'type' => LedgerType::ASSET
]);

// Assign journal to ledger
$journal->assignToLedger($assetLedger);

// Get ledger balance
$totalBalance = $assetLedger->getCurrentBalance('USD');
bash
php artisan vendor:publish --provider="Scottlaurent\Accounting\Providers\AccountingServiceProvider"
bash
php artisan migrate