<?php

class Employee extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddDateTime('Time');
    $Desc->AddString('To');
    $Desc->AddString('Subject');
    $Desc->AddText('Content');
    $Desc->AddString('Headers');
    $Desc->AddBoolean('Archive');
    $Desc->AddString('From');
    $Desc->AddReference('AttachmentFile', File::GetClassName());
    return $Desc;
  }
}

class EmployeeSalary extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddDate('Date');
    $Desc->AddReference('Employee', Employee::GetClassName());
    $Desc->AddInteger('Amount');
    $Desc->AddReference('Contract', Contract::GetClassName());
    return $Desc;
  }
}

class ModuleEmployee extends Module
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'Employee';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Employee wages management';
    $this->Dependencies = array(ModuleUser::GetName(), ModuleFinance::GetName());
    $this->Models = array(Employee::GetClassName(), EmployeeSalary::GetClassName());
  }

  function DoStart(): void
  {
    $this->System->FormManager->RegisterClass('Employee', array(
      'Title' => 'Zaměstnanci',
      'Table' => 'Employee',
      'Items' => array(
        'FirstName' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'SecondName' => array('Type' => 'String', 'Caption' => 'Příjmení', 'Default' => ''),
        'Salary' => array('Type' => 'Integer', 'Caption' => 'Pevná mzda', 'Default' => '0', 'Suffix' => 'Kč'),
        'ValidFrom' => array('Type' => 'Date', 'Caption' => 'Platnost od', 'Default' => ''),
        'ValidTo' => array('Type' => 'Date', 'Caption' => 'Platnost do', 'Default' => '', 'Null' => true),
        'SalaryList' => array('Type' => 'TEmployeeSalaryList', 'Caption' => 'Mzda', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('EmployeeSalary', array(
      'Title' => 'Výplaty zaměstnanců',
      'Table' => 'EmployeeSalary',
      'Items' => array(
        'Date' => array('Type' => 'Date', 'Caption' => 'Datum', 'Default' => ''),
        'Employee' => array('Type' => 'TEmployee', 'Caption' => 'Zaměstnance', 'Default' => ''),
        'Amount' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
        'Contract' => array('Type' => 'TContract', 'Caption' => 'Smlouva', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TEmployee', array(
      'Type' => 'Reference',
      'Table' => 'Employee',
      'Id' => 'Id',
      'Name' => 'CONCAT_WS(" ", NULLIF(`FirstName`, ""), NULLIF(`SecondName`, ""))',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TEmployeeSalaryList', array(
      'Type' => 'ManyToOne',
      'Table' => 'EmployeeSalary',
      'Id' => 'Id',
      'Ref' => 'Employee',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TEmployeeSalaryListContract', array(
      'Type' => 'ManyToOne',
      'Table' => 'EmployeeSalary',
      'Id' => 'Id',
      'Ref' => 'Contract',
      'Filter' => '1',
    ));
  }
}
