<?php
session_start();
unset($_SESSION);

// Import Core Interfaces
require_once('../../modules/InterfaceConfig.php');
require_once('../../modules/InterfacePostgreSQL.php');

// Import Core Classes
require_once('../../modules/ClassConfig.php');
require_once('../../modules/ClassUser.php');
require_once('../../modules/ClassCountry.php');
require_once('../../modules/ClassCurrency.php');
require_once('../../modules/ClassLang.php');

// Import Custom Classes
require_once('../../custom/modules/ClassUserCustom.php');
require_once('../../custom/modules/ClassERP.php');
require_once('../../custom/modules/ripcord.php');
echo "test";


$oConf = new ClassConfig();
$oUser = new ClassUserCustom();
$oCountry = new ClassCountry();
$oCurrency = new ClassCurrency();
$oLang = new ClassLang();
$oERP = new ClassERP();

// Create session arrays
if (!isset($_SESSION['config'])) {
    $_SESSION['config'] = $oConf->getConfig();
    $_SESSION['erp'] = $oERP->getConfig();
    //$_SESSION['translations'] = $oTrans->listTranslations($_SESSION['config']['default_lang_code']);
    //$_SESSION['countries'] = $oCountry->listCountries('priority ASC, name ASC');
    //$_SESSION['activeCountries'] = $oCountry->listActiveCountries('priority ASC, name ASC');
    //$_SESSION['currencies'] = $oCurrency->listCurrencies('priority ASC, name ASC');
    //$_SESSION['languages'] = $oLang->listLanguages();
    //$_SESSION['activeLanguages'] = $oLang->listActiveLanguages();
}

// Open connection with Odoo
$url = $_SESSION['erp']['url'];
$db = $_SESSION['erp']['db'];
$username = $_SESSION['erp']['admin_user'];
$password = $_SESSION['erp']['admin_password'];

$common = ripcord::client($url."xmlrpc/2/common");
$uid = $common->authenticate($db, $username, $password, array());
$models = ripcord::client($url."xmlrpc/2/object");

// Step 1. Create user admin@wefra
$data = array('formRegisterFieldPassword'=>'admin', 'formRegisterFieldConfirmPassword'=>'admin', 'formRegisterFieldFirstname'=>'Admin', 'formRegisterFieldLastname'=>'Wefra',
              'formRegisterFieldEmail'=>'admin@wefra', 'formRegisterFieldCountry'=>44);

$name = $data['formRegisterFieldFirstname'].' '.$data['formRegisterFieldLastname'];

//create user in ERP
$ext_id = $models->execute_kw($db, $uid, $password, 'res.users', 'create',
                              array(array('name'=>$name,
                                          'email'=>$data['formRegisterFieldEmail'],
                                          'login'=>$data['formRegisterFieldEmail'], //yes, the login IS the email, there is no mistake here
                                          'country_id'=>(integer) $models->execute_kw($db, $uid, $password, 'res.country', 'search', array(array(array('code', '=', 'CH'))))[0],
                                          'project_task_level_id'=>(integer) $models->execute_kw($db, $uid, $password, 'project.task.level', 'search', array(array(array('name', '=', 'White Belt'))))[0]
                                          )));

echo "ext_id has value: ";
print_r($ext_id);
echo "<br><br>";

//if user created in ERP, then create it in Wefra with password linked
if(isset($ext_id) and !is_array($ext_id)){
  //create user in Wefra and link the user to ERP
  $user = $oUser->createUserForWefraAdmin($data, $ext_id);
  echo "Wefra user status: ";
  print_r($user);
  echo "<br><br>";
  
  if($user){
    //update the password for user in ERP
    //..
    $getUser = $oUser->getUser($user['user_id']);
    $models->execute_kw($db, $uid, $password, 'res.users', 'write', array(array($ext_id), array('password'=>$getUser['password'], 'password_uncrypted'=>$data['formRegisterFieldPassword'])));
    //..
    //that way, ERP rights will be checked everytime Wefra will use ORM to allow user to to actions which will have an influence in the ERP (ie. get a task which will be assigned to his ERP's user related)
    $message['content'] = '<span style="color:green"><b>Success: </b><p>User Admin Wefra has been created in globsi-admin</p>';
  } else {
    echo '<span style="color:red"><b>Failure: </b><p>User Admin Wefra could not been created in globsi-admin</p>';
    
  }
  //display a confirmation message that everything went well
  $message['content'] = '<span style="color:green"><b>Success: </b><p>User Admin Wefra has been created in globsi-backend</p>';
} else {
  //if user not created in ERP, displaying a warning message to ask for a new try
  $message['content'] = '<span style="color:red"><b>Failure: </b><p>User Admin Wefra could not been created in globsi-backend</p>';
}

echo $message['content'];

echo "<br><br>Session Config<br>";
print_r($_SESSION['config']);
echo "<br><br>Session ERP<br>";
print_r($_SESSION['erp']);
