migrate_expeditor_data.php 2.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

/**
 * Copyright Maarch since 2008 under licence GPLv3.
 * See LICENCE.txt file at the root folder for more details.
 * This file is part of Maarch software.
 *
 */

/**
 * @brief Synchronization Script
 * @author dev@maarch.org
 */
libxml_use_internal_errors(true);
chdir('../..');

require 'vendor/autoload.php';

main($argv);

function main($argv)
{
    $customId = null;
    if (!empty($argv[1]) && $argv[1] == '--customId' && !empty($argv[2])) {
        $customId = $argv[2];
        $GLOBALS['customId'] = $customId;
    }
    $path = 'apps/maarch_entreprise/xml/config.json';
    if (!empty($customId)) {
        $path = "custom/{$customId}/apps/maarch_entreprise/xml/config.json";
    }
    $file = file_get_contents($path);
    $file = json_decode($file, true)['database'][0];
    if (empty($file['server'])) {
        writeLog(['message' => "[ERROR] Tag maarchUrl is missing in config.json"]);
        exit();
    }

    $dbconn = pg_connect('host='.$file['server'].' dbname='.$file['name'].' port='.$file['port'].' user='.$file['user'].' password='.$file['password']) 
    or die('Connexion impossible : ' . pg_last_error());

ludovic's avatar
ludovic committed
42
    $query = 'SELECT res_id,typist FROM res_letterbox';
43
44
45
46
47
48
49
50
51
52
53
54
55
    $result = pg_fetch_all(pg_query($query));
    $count = count($result);
    $i = 0;
    foreach ($result as $key => $value) {
        echo "res_id : ".$value['res_id'];
        updateExpediteur($value);
        updateDestinataire($value);
        $i++;
        echo " / avancement : $i sur $count".PHP_EOL;
    }
}

function updateExpediteur ($row) {
ludovic's avatar
ludovic committed
56
    $select = "SELECT  lastname || ' ' || firstname as expediteur FROM users WHERE id = ".$row['typist'];
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    $res = pg_query($select) or die('Échec de la requête : ' . pg_last_error());
    $expe = pg_fetch_all($res)[0]['expediteur'];
    echo " / expediteur : ".$expe;
    $update = "UPDATE res_letterbox SET custom_fields = jsonb_set(custom_fields,'{\"20\"}', '".json_encode($expe)."') WHERE res_id = ".$row['res_id']." returning custom_fields;";
    $result = pg_query($update) or die('Échec de la requête : ' . pg_last_error());
}

function updateDestinataire ($row) {
    $select = "SELECT entity_by_res_id(".$row['res_id'].") as destinataire;";
    $res = pg_query($select) or die('Échec de la requête : ' . pg_last_error());
    $dest = pg_fetch_all($res)[0]['destinataire'];
    echo " / societé expéditrice : ".$dest;
    $update = "UPDATE res_letterbox SET custom_fields = jsonb_set(custom_fields,'{\"18\"}', '".json_encode($dest)."') WHERE res_id = ".$row['res_id']." returning custom_fields;";
    $result = pg_query($update) or die('Échec de la requête : ' . pg_last_error());
}