Overview
FacturaScripts supports multi-company operations, allowing you to manage multiple legal entities within a single installation. Each company can have its own configuration, warehouses, bank accounts, and fiscal exercises.Company Model
The Empresa (Company) model is located atCore/Model/Empresa.php and stores the main company data.
Company Properties
Basic Information
- idempresa: Primary key (integer, auto-generated)
- nombre: Full company name (max 100 characters)
- nombrecorto: Short company name (max 32 characters)
- administrador: Administrator name
- fechaalta: Registration date (auto-set on creation)
Tax Information
- cifnif: Tax identification number (fiscal number)
- tipoidfiscal: Type of fiscal ID (default from settings)
- regimeniva: VAT regime (default from
RegimenIVA::defaultValue()) - excepcioniva: VAT exemption code
- personafisica: Boolean - Is physical person (vs. company)
Contact Information
- email: Email address
- telefono1: Primary phone
- telefono2: Secondary phone
- fax: Fax number
- web: Website URL (validated)
Address Information
- direccion: Street address
- apartado: P.O. Box
- codpostal: Postal/ZIP code
- ciudad: City
- provincia: Province/State
- codpais: Country code
Other Properties
- idlogo: Associated logo file ID
- observaciones: Notes/observations
Company Creation
Default Company Installation
During installation (Empresa.php:169), a default company is created:
- idempresa: 1 (first company)
- nombre: From
initial_empresaconfig (default: ‘E-’ + random number) - codpais: From
initial_codpaisconfig (default: ‘ESP’) - regimeniva: Default VAT regime
- cifnif: Placeholder ‘00000014Z’
Creating Additional Companies
When creating a new company (Empresa.php:258):
- Set company properties
- Call
save() - System automatically:
- Generates
idempresaif not provided - Creates a default payment method
- Creates a default warehouse
- Generates
Default Company
Checking Default Company
UseisDefault() method to check if a company is the default:
Default Company Protection
The default company cannot be deleted (Empresa.php:126):
Company Associations
Bank Accounts
Get all bank accounts associated with a company:Empresa.php:141):
Fiscal Exercises
Get all fiscal exercises (accounting periods) for a company:Empresa.php:152):
Warehouses
Get all warehouses belonging to a company:Empresa.php:163):
Data Validation
Company Test Method
Thetest() method (Empresa.php:208) validates and sanitizes all company data:
- HTML Sanitization: All text fields are cleaned with
Tools::noHtml() - URL Validation: Website URLs are validated
- Email Validation: Email addresses are validated
- Phone Validation: Phone numbers are validated
- Fiscal Number Validation: Tax IDs are validated
VAT/VIES Validation
Check VIES Database
Validate a company’s VAT number against the EU VIES database:Empresa.php:104):
- Retrieves the ISO country code from the company’s country
- Validates the fiscal number against VIES
- Optionally displays validation messages
Automatic Setup
Default Payment Method
When a company is created, a default payment method is automatically generated (Empresa.php:231):
Default Warehouse
A default warehouse is created with the company’s address details (Empresa.php:241):
Company Clear/Reset
When clearing a company object (Empresa.php:110), defaults are set:
Cache Management
Company data is cached for performance. Clear the cache after updates:Multi-Company Usage
User-Company Association
Users are associated with companies via:- idempresa: User’s default company ID
- codalmacen: User’s default warehouse (must belong to their company)
User.php:473):
Setting Default Company
Configure the default company in system settings:Traits Used
The Empresa model uses several traits for extended functionality:- EmailAndPhonesTrait: Adds email and phone validation
- FiscalNumberTrait: Adds fiscal number validation
- GravatarTrait: Adds Gravatar image support based on email
Best Practices
- Complete Tax Information: Always fill in VAT number and tax regime correctly
- Validate VIES: For EU companies, validate VAT numbers using
checkVies() - Unique Short Names: Use distinct
nombrecortovalues for easy identification - Proper Address Data: Complete address information for accurate warehouse creation
- Don’t Delete: Disable rather than delete companies to preserve historical data
- Backup Before Changes: Multi-company setups affect many related records
Related Files
- Company Model:
/Core/Model/Empresa.php - Warehouse Model:
/Core/Model/Almacen.php - Payment Method Model:
/Core/Model/FormaPago.php - Fiscal Exercise Model:
/Core/Model/Ejercicio.php - Bank Account Model:
/Core/Model/CuentaBanco.php - VAT Regime Library:
/Dinamic/Lib/RegimenIVA.php - VIES Library:
/Core/Lib/Vies.php

