PHP code example of ismaildasci / laravel-sapb1-toolkit

1. Go to this page and download the library: Download ismaildasci/laravel-sapb1-toolkit 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/ */

    

ismaildasci / laravel-sapb1-toolkit example snippets


use SapB1\Toolkit\Models\Sales\Order;

$orders = Order::where('DocTotal', '>', 1000)
    ->where('DocumentStatus', 'bost_Open')
    ->orderBy('DocDate', 'desc')
    ->with('partner')
    ->get();

$order = Order::create([
    'CardCode' => 'C001',
    'DocumentLines' => [
        ['ItemCode' => 'ITEM001', 'Quantity' => 10, 'Price' => 100],
    ],
]);

use SapB1\Toolkit\Actions\Sales\OrderAction;

$orderAction = app(OrderAction::class);

$order = $orderAction->create([
    'CardCode' => 'C001',
    'DocumentLines' => [
        ['ItemCode' => 'ITEM001', 'Quantity' => 10, 'Price' => 100],
    ],
]);

$orderAction->close(123);

use SapB1\Toolkit\Builders\Sales\OrderBuilder;

$data = OrderBuilder::create()
    ->cardCode('C001')
    ->docDate('2024-01-15')
    ->addLine(fn ($line) => $line
        ->itemCode('ITEM001')
        ->quantity(10)
        ->price(100)
    )
    ->build();

use SapB1\Toolkit\Services\DocumentFlowService;

$flow = app(DocumentFlowService::class);
$invoice = $flow->orderToInvoice(123);
$delivery = $flow->orderToDelivery(123);

use SapB1\Toolkit\Models\Sales\Order;

$order = Order::find(123);

// Read UDFs
$value = $order->getUdf('CustomField');
$allUdfs = $order->getUdfs();

// Write UDFs
$order->setUdf('CustomField', 'value');
$order->save();

// Builder support
$data = OrderBuilder::create()
    ->cardCode('C001')
    ->udf('CustomField', 'value')
    ->build();

use SapB1\Toolkit\Models\Inventory\Item;

// Query-level cache control
$item = Item::cache()->find('A001');           // Enable with default TTL
$item = Item::cache(600)->find('A001');        // 10 minute TTL
$item = Item::noCache()->find('A001');         // Disable cache

// Flush cache
Item::flushCache();
Item::forgetCached('A001');

use SapB1\Toolkit\ChangeTracking\ChangeTracker;

$tracker = ChangeTracker::for('Orders')
    ->primaryKey('DocEntry')
    ->detectCreated(true)
    ->detectUpdated(true);

$changes = $tracker->poll();

foreach ($changes as $change) {
    if ($change->isCreated()) {
        // Handle new order
    }
}

use SapB1\Toolkit\Sync\LocalSyncService;

$syncService = app(LocalSyncService::class);

// Sync to local database
$result = $syncService->sync('Items');
// SyncResult { created: 10, updated: 140, deleted: 0, duration: 1.23s }

// Full sync with soft delete detection
$result = $syncService->fullSyncWithDeletes('Items');

// Scheduler integration
$schedule->command('sapb1:sync Items')->hourly();
$schedule->command('sapb1:sync Items --full')->weekly();
bash
php artisan vendor:publish --tag="sapb1-toolkit-config"