PHP code example of codebar-ag / laravel-odoo

1. Go to this page and download the library: Download codebar-ag/laravel-odoo 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/ */

    

codebar-ag / laravel-odoo example snippets


use CodebarAg\Odoo\OdooConnector;

$connector = new OdooConnector(
    baseUrl: 'https://your-odoo-instance.com',
    apiKey: 'your-api-key',
    db: 'your-database',   // optional
    maxRedirects: 5,       // optional — max HTTP redirects to follow (default 5)
    timeout: 15.0,         // optional — request timeout in seconds (default 15, 0 = no timeout)
);

use CodebarAg\Odoo\Facades\Odoo;

$response = Odoo::health();
$response->isHealthy(); // bool

// Check if the Odoo instance is reachable
$response = $connector->health();
$response->isHealthy(); // bool

// Get the Odoo server version
$response = $connector->version();
$response->serverVersion(); // ?string  e.g. "17"
$response->serie();         // ?string  e.g. "17.0"

// List all available databases
$response = $connector->databases();
$response->databases(); // array<string>

// Get the currently authenticated user
$response = $connector->getUser(
    fields: ['name', 'email'], // optional — omit to get the default field set
    domain: [],                // optional
    limit: 1,                  // optional, default 1
);
$user = $response->dto(); // ?UserDto  (id, name, lang)

// Get a user by their Odoo ID
$response = $connector->getUserById(
    uid: 1,
    fields: ['name', 'email'], // optional
);

// Get the authenticated user's context (timezone, language)
$response = $connector->getUserContext();

// Get an employee by their Odoo user ID
$response = $connector->getEmployeeByUserId(
    userId: 1,
    fields: ['name', 'job_title'], // optional — omit to get all fields
    limit: 1,                      // optional, default 1
);
$response->dto(); // ?EmployeeDto

// Get fields for a specific model
$response = $connector->getFields(
    model: 'account.move',
    attributes: ['string', 'type'], // optional — field meta-attributes to return
);
$response->fields(); // array<string, FieldDto>

// Get all fields across all models
$response = $connector->getAllFields();
$response->fields(); // array<string, FieldDto>

// Check permissions for a model and operation
$response = $connector->getPermissions(
    model: 'project.project',
    operation: 'read', // read, write, create, unlink
);
$response->allowed(); // bool

$response = $connector->getProjects(
    fields: ['name', 'date_start', 'date'], // optional
    domain: [['active', '=', true]],        // optional Odoo domain filter
    limit: 100,                              // optional, default 100
);

/** @var array<ProjectDto> $projects */
$projects = $response->projects();

use CodebarAg\Odoo\Dto\Projects\CreateProjectDto;
use CodebarAg\Odoo\Dto\Projects\UpdateProjectDto;

// Create a project
$response = $connector->createProject(new CreateProjectDto(
    name: 'Website Relaunch',
    partnerId: 7,         // optional, linked contact
    userId: 2,            // optional, project manager
    allocatedHours: 40.0, // optional
    tagIds: [1, 2],       // optional many-to-many tags
    extraValues: [],      // optional, custom/studio fields
));
$id = $response->id(); // ?int

// Update a project (only provided fields are written)
$response = $connector->updateProject(new UpdateProjectDto(
    id: 42,
    name: 'Website Relaunch 2.0',
));
$response->ok(); // bool

// Delete a project
$response = $connector->deleteProject(id: 42);
$response->ok(); // bool

// Get all tasks
$response = $connector->getAllTasks(
    fields: ['name', 'project_id', 'stage_id'], // optional
    domain: [['active', '=', true]],             // optional
    limit: 100,                                   // optional, default 100
);

/** @var array<TaskDto> $tasks */
$tasks = $response->tasks();

// Get tasks for a specific project
$response = $connector->getTasksByProject(
    projectId: 42,
    fields: ['name', 'stage_id', 'date_deadline'], // optional
    limit: 100,                                     // optional, default 100
    operator: '=',                                  // optional domain operator, default '='
);

/** @var array<TaskDto> $tasks */
$tasks = $response->tasks();

use CodebarAg\Odoo\Dto\Tasks\CreateTaskDto;
use CodebarAg\Odoo\Dto\Tasks\UpdateTaskDto;

// Create a task
$response = $connector->createTask(new CreateTaskDto(
    name: 'Design homepage',
    projectId: 42,        // optional
    userIds: [5, 6],      // optional assignees (many-to-many)
    stageId: 1,           // optional
    dateDeadline: '2026-07-01',
    priority: '1',        // optional
    extraValues: [],      // optional, custom/studio fields
));
$id = $response->id(); // ?int

// Update a task (only provided fields are written)
$response = $connector->updateTask(new UpdateTaskDto(
    id: 42,
    name: 'Design homepage v2',
    stageId: 2,
));
$response->ok(); // bool

// Delete a task
$response = $connector->deleteTask(id: 42);
$response->ok(); // bool

use CodebarAg\Odoo\Dto\Timesheets\CreateTimesheetDto;
use CodebarAg\Odoo\Dto\Timesheets\UpdateTimesheetDto;

// Get timesheet entries
$response = $connector->getTimesheetEntries(
    fields: ['name', 'project_id', 'task_id', 'unit_amount', 'date'], // optional
    domain: [['employee_id', '=', 5]],                                 // optional
    limit: 100,                                                         // optional
);

/** @var array<TimesheetEntryDto> $entries */
$entries = $response->entries();

// Get timesheet entries from the last N days
$response = $connector->getTimesheetEntriesLastDays(
    days: 7,
    fields: ['name', 'date', 'unit_amount'], // optional
    operator: '>=',                          // optional domain operator, default '>='
);
$entries = $response->entries(); // array<TimesheetEntryDto>

// Read a single timesheet entry
$response = $connector->readTimesheet(id: 123);
$entry = $response->dto(); // ?TimesheetEntryDto

// Create a timesheet entry
$response = $connector->createTimesheet(new CreateTimesheetDto(
    name: 'Fixed bug #456',
    projectId: 1,
    taskId: 10,
    date: '2024-06-11',
    unitAmount: 1.5,
    employeeId: 5,   // optional
    extraValues: [], // optional — extra Odoo fields (e.g. custom Studio fields)
));
$newId = $response->id(); // ?int

// Update a timesheet entry
$response = $connector->updateTimesheet(new UpdateTimesheetDto(
    id: 123,
    values: ['name' => 'Updated description', 'unit_amount' => 2.0],
));
$response->ok(); // bool

// Delete a timesheet entry
$response = $connector->deleteTimesheet(id: 123);
$response->ok(); // bool

use CodebarAg\Odoo\Dto\BankAccounts\CreateBankAccountDto;
use CodebarAg\Odoo\Dto\BankAccounts\UpdateBankAccountDto;

// Search + read fields in one call (search_read)
$response = $connector->getBankAccounts(
    fields: ['id', 'acc_number', 'partner_id', 'bank_id'], // optional (version-aware default)
    domain: [['partner_id', '=', 7]],                      // optional
    limit: 100,                                            // optional
);

/** @var array<BankAccountDto> $bankAccounts */
$bankAccounts = $response->bankAccounts();

// Search only for matching IDs (search)
$response = $connector->searchBankAccounts(domain: [['partner_id', '=', 7]]);
$ids = $response->ids(); // array<int>

// Read a record by ID (read)
$response = $connector->readBankAccount(id: 5);
$bankAccounts = $response->bankAccounts(); // array<BankAccountDto>

// Count matching records (search_count)
$response = $connector->searchCountBankAccounts(domain: [['partner_id', '=', 7]]);
$count = $response->count(); // ?int

// Create a bank account (create)
$response = $connector->createBankAccount(new CreateBankAccountDto(
    accNumber: 'CH9300762011623852957',
    partnerId: 7,
    accHolderName: 'Jane Doe', // optional
    bankName: 'UBS',           // optional
    bankBic: 'UBSWCHZH80A',    // optional
    bankId: 3,                 // optional — Odoo 19.0 only (ignored on 19.3)
    currencyId: 1,             // optional — Odoo 19.0 only (ignored on 19.3)
    allowOutPayment: true,     // optional
    sequence: 10,              // optional
    extraValues: [],           // optional — extra Odoo fields (e.g. custom Studio fields)
));
$newId = $response->id(); // ?int

// Update a bank account (write)
$response = $connector->updateBankAccount(new UpdateBankAccountDto(
    id: 5,
    accHolderName: 'John Doe',
));
$response->ok(); // bool

// Delete a bank account (unlink)
$response = $connector->deleteBankAccount(id: 5);
$response->ok(); // bool

$results = $connector->syncAll();

$projects   = $results['projects']->projects();     // array<ProjectDto>
$tasks      = $results['tasks']->tasks();           // array<TaskDto>
$timesheets = $results['timesheets']->entries();    // array<TimesheetEntryDto>
bash
php artisan vendor:publish --provider="CodebarAg\Odoo\OdooServiceProvider" --tag="laravel-odoo-config"