PHP code example of codesbytvt / smartmart360-sdk

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

    

codesbytvt / smartmart360-sdk example snippets


// This should work without tMartClient;
use SmartMart\External\TRANSACTION_TYPES;
use SmartMart\External\PARTY_TYPES;

// Create client
$client = new SmartMartClient($url, $apiKey, $clientId, $appName);


est class loading
try {
    $reflection = new ReflectionClass('SmartMart\External\SmartMartClient');
    echo "✅ SmartMartClient class found\n";
} catch (Exception $e) {
    echo "❌ SmartMartClient class not found: " . $e->getMessage() . "\n";
}

// Test constants
if (class_exists('SmartMart\External\TRANSACTION_TYPES')) {
    echo "✅ TRANSACTION_TYPES class found\n";
} else {
    echo "❌ TRANSACTION_TYPES class not found\n";
}

// Add this to your script if autoloading fails
set_include_path(get_include_path() . PATH_SEPARATOR . 'vendor/');

use SmartMart\External\SmartMartClient;
use SmartMart\External\DOCUMENT_TYPES;  // Primary - Document Types
use SmartMart\External\PARTY_TYPES;     // Party Types
use SmartMart\External\TRANSACTION_TYPES; // Legacy Transaction Types

$client = new SmartMartClient(
    'https://api.smartmart360.com', // Base URL
    'your_api_key_here',            // API Key
    1,                              // Your client/branch ID
    'HOSPITAL'                      // Application name
);

if ($client->healthCheck()) {
    echo "Connected\n";
}

// Complete catalogue (default)
$allItems = $client->getItems(['search' => 'paracetamol']);

// Only items in stock
$availableItems = $client->getItems(['search' => 'paracetamol'], 'in_stock');

// Items needing restock (below reorder level)
$lowStockItems = $client->getItems([], 'low_stock', 5);

// Out of stock items (procurement list)
$outOfStockItems = $client->getItems([], 'out_of_stock');

// With variant details (e(['search' => 'paracetamol']);

[
    'status' => 1,
    'data' => [
        'records' => [
            [
                'id' => 123,
                'name' => 'Paracetamol 500mg',
                'code' => 'PARA500',
                'categoryName' => 'Medicine',
                'brandName' => 'Generic',
                // Stock data (only when stock_filter ≠ 'all')
                'current_stock' => 25.5,
                'stock_value' => 637.50
            ]
        ],
        'totalRecords' => 150,
        'page' => 1,
        'pageLength' => 50
    ]
]

// These still work but use the new unified API internally
$catalog = $client->getItemCatalog(['search' => 'medicine']);
$stockReport = $client->getStockReport(['search' => 'paracetamol']);
$lowStock = $client->getLegacyLowStockItems();

// Get all categories
$categories = $client->getCategories();

// Get subcategories of a parent category
$subCategories = $client->getCategories(['parent_id' => 5]);

// Search categories
$categories = $client->getCategories(['search' => 'medicine']);

// Paginated results
$categories = $client->getCategories(['page' => 1, 'pageLength' => 50]);

[
    'status' => 1,
    'data' => [
        'records' => [
            [
                'id' => 1,
                'name' => 'Medicines',
                'code' => 'MED',
                'parentName' => null,  // Top-level category
                'created_at' => '2025-01-15 10:30:00'
            ],
            [
                'id' => 5,
                'name' => 'Pain Relief',
                'code' => 'PAIN',
                'parentName' => 'Medicines',  // Subcategory
                'created_at' => '2025-01-15 11:00:00'
            ]
        ],
        'totalRecords' => 25,
        'page' => 1,
        'pageLength' => 50
    ]
]

// success
['status' => 1, 'data' => [...], 'error' => [], 'infoDtls' => []];
// error
['status' => 0, 'data' => [], 'error' => 'message' or ['field' => 'error'], 'http_code' => 400];

// 1. Doctor searches for medicine
$medicines = $client->getMedicineCatalogue(['search' => 'paracetamol']);

// Returns: Paracetamol with all variants (500mg tabs, 250mg tabs, syrup, etc.)
foreach ($medicines['data']['records'] as $medicine) {
    echo "Medicine: {$medicine['name']}\n";
    foreach ($medicine['variants'] as $variant) {
        echo "  - {$variant['display_name']} ({$variant['stock']} in stock)\n";
        echo "    Variant ID: {$variant['id']} (use for prescription)\n";
    }
}

// 2. Doctor selects variant and creates prescription
$prescription = $client->createTransaction(
    DOCUMENT_TYPES::SALES_INVOICE,
    'RX-12345',
    $patientId,
    [
        [
            'item_variant_id' => $selectedVariantId,  // From catalogue
            'quantity' => 10,
            'rate' => $variant['mrp']
        ]
    ]
);

$txn = $client->createTransaction(
    DOCUMENT_TYPES::SALES_INVOICE,   // Document type (primary approach)
    'RX-12345',                      // your reference
    'hosp_pat_12345',                // external party id
    [
        [
            'item_variant_id' => 123, // OR 'item_id' => 456 (auto-converts to default variant)
            'quantity' => 2,
            'uom_id' => 1,             // Optional: auto-filled from variant
            'rate' => 25.00
        ]
    ],
    [
        'party_type_id' => 9,        // PATIENT type
        'notes' => 'Urgent prescription'
    ]
    // useTransactionType defaults to false (document type mode)
);

$txn = $client->createTransaction(
    TRANSACTION_TYPES::SALES,        // Transaction type ID (legacy approach)
    'RX-12345',                      // your reference
    'hosp_pat_12345',                // external party id
    [
        [
            'item_variant_id' => 123, // OR 'item_id' => 456 (auto-converts to default variant)
            'quantity' => 2,
            'uom_id' => 1,             // Optional: auto-filled from variant
            'rate' => 25.00
        ]
    ],
    [
        'party_type_id' => 9,        // PATIENT type
        'notes' => 'Urgent prescription'
    ],
    true                            // useTransactionType = true
);

[
  'status' => 1,
  'data' => [
    'transaction_id' => 9876,
    'external_reference_id' => 'RX-12345',
    'status' => 'created'
  ]
]

// Browse items with stock filtering
$client->getItems(['search' => 'paracetamol', 'page' => 1]);
$client->searchItems('paracetamol 500mg');
$client->getItem(123);
$client->checkStock(123, $locationId);

// Get items for specific document type (RECOMMENDED)
$items = $client->getItemsForTransaction(DOCUMENT_TYPES::SALES_INVOICE, $locationId);

// Get items for legacy transaction type (Only if needed)
$items = $client->getItemsForTransaction(25, $locationId, true); 

$client->createOrSyncParty('hosp_pat_12345', PARTY_TYPES::PATIENT, [
    'name' => 'John Doe',
    'phone' => '+919876543210'
], 'MRN-789');

$client->searchPartyByReference('MRN-789', PARTY_TYPES::PATIENT);
$client->getPartyDues($partyLinkId);
$client->getPartyStatement($partyLinkId, '2025-01-01', '2025-01-31');

$client->createTransaction(...);                  // create order
$client->getTransaction($transactionId);          // fetch by id
$client->getTransactionByReference('RX-12345');   // fetch by your ref
$client->getTransactionStatus($transactionId);    // status
$client->listTransactions(['from_date' => '2025-01-01', 'to_date' => '2025-01-31']);
$client->cancelTransaction($transactionId, 'reason here');
$client->updateTransactionStatus($transactionId, 5, 'In transit'); // example status id

$client->getInvoice($transactionId);
$client->getInvoicePDF($transactionId);
$client->recordPayment($transactionId, 1500.00, [
    'payment_mode' => 'Insurance Claim',
    'reference_no' => 'CLAIM-12345',
]);
$client->getPaymentHistory($partyLinkId, '2025-01-01', '2025-01-31');
$client->getOutstandingBills($partyLinkId);

// Get transaction summary for a date range
$client->getTransactionSummary('2025-01-01', '2025-01-31');

// Filter summary by document type (RECOMMENDED)
$client->getTransactionSummary('2025-01-01', '2025-01-31', ['document_type_id' => DOCUMENT_TYPES::SALES_INVOICE]);

// Filter summary by legacy transaction type
$client->getTransactionSummary('2025-01-01', '2025-01-31', ['transaction_type_id' => 25]);

$client->getStockReport(['location_id' => 1]);
$client->getLowStockItems();

PARTY_TYPES::CUSTOMER  // 1
PARTY_TYPES::SUPPLIER  // 2
PARTY_TYPES::EMPLOYEE  // 3
PARTY_TYPES::PATIENT   // 9
PARTY_TYPES::DOCTOR    // 10
PARTY_TYPES::LAB       // 11
PARTY_TYPES::INSURANCE // 12

TRANSACTION_TYPES::PURCHASE_ORDER   // 2
TRANSACTION_TYPES::PURCHASE         // 3
TRANSACTION_TYPES::SALES_ORDER      // 24
TRANSACTION_TYPES::SALES            // 25
TRANSACTION_TYPES::PURCHASE_RETURN  // 13
TRANSACTION_TYPES::SALES_RETURN     // 14
TRANSACTION_TYPES::STOCK_OPENING    // 12
TRANSACTION_TYPES::STOCK_ADJUSTMENT // 10
bash
php --version
bash
# Copy the test script to your project
cp vendor/codesbytvt/smartmart360-sdk/test-autoload.php .

# Run the test
php test-autoload.php