forked from devlab-oy/pupesoft
/
aloita_synkronointi.php
139 lines (103 loc) · 3.89 KB
/
aloita_synkronointi.php
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
require "inc/parametrit.inc";
// Laitetaan max time 5H
ini_set("max_execution_time", 18000);
ini_set("mysql.connect_timeout", 600);
echo "<font class='head'>".t("Alkusynkronointi")."</font><hr>";
if (!isset($tee)) $tee = "";
if ($tee == "SYNK") {
// Onko mahdollista synkronoida?
if (substr($table, 0, 9) == "avainsana") {
if (strpos($yhtiorow["synkronoi"], substr($table, 0, 9)) === false) {
echo "VIRHE: Pyydettyä taulua $table ei voida synkronoida, sitä ei ole määritelty!";
exit;
}
$table = substr($table, 0, 9);
$abulisa = preg_match("/(^|,)(avainsana\|*([\|a-zA-Z_\-]*))($|,)/i", $yhtiorow["synkronoi"], $regs);
if ($regs[3] != "") {
$la = explode("|", $regs[3]);
$lajit = " and laji in (";
foreach ($la as $l) {
$lajit .= "'$l',";
}
$lajit = substr($lajit, 0, -1);
$lajit .= ")";
}
}
else {
if (strpos($yhtiorow["synkronoi"], $table) === false or $table == "") {
echo "VIRHE: Pyydettyä taulua $table ei voida synkronoida, sitä ei ole määritelty!";
exit;
}
$lajit = "";
}
require_once "inc/pakolliset_sarakkeet.inc";
list($pakolliset, $kielletyt, $wherelliset, $eiyhtiota, $joinattavat, $saakopoistaa, $oletukset) = pakolliset_sarakkeet($table);
if (count($wherelliset) == 0 and count($pakolliset) == 0) {
echo "VIRHE: Pyydettyä taulua $table ei voida synkronoida, sitä ei ole määritelty!<br>";
exit;
}
else {
// Tehdään kysely
$query = "SELECT group_concat(concat('\'',yhtio.yhtio,'\'')) yhtiot
FROM yhtio
JOIN yhtion_parametrit ON (yhtion_parametrit.yhtio = yhtio.yhtio)
WHERE konserni = '$yhtiorow[konserni]'
AND (synkronoi = '$table' or synkronoi like '$table,%' or synkronoi like '%,$table,%' or synkronoi like '%,$table' or synkronoi like '%,$table|%' or synkronoi like '$table|%')";
$kohderes = pupe_query($query);
$kohderow = mysql_fetch_array($kohderes);
if (strlen($kohderow["yhtiot"]) == 0) {
echo "VIRHE: Pyydettyä taulua $table ei voida synkronoida, yhtiö ei löydy!<br>$query";
exit;
}
$group = " group by ";
$indeksi = array_merge($wherelliset, $pakolliset);
$indeksi = array_unique($indeksi);
foreach ($indeksi as $pakollinen) {
$group .= strtolower($pakollinen).",";
}
$group = substr($group, 0, -1);
$lisa = "";
if ($table == "asiakas") {
$lisa = ", maksuehto READ, toimitustapa READ";
}
if ($table == "tuotteen_toimittajat") {
$lisa = ", tuote READ, toimi READ";
}
$query = "LOCK TABLES yhtio READ, yhtion_parametrit READ, synclog WRITE, $table WRITE $lisa";
$abures = pupe_query($query);
$query = "SELECT group_concat(tunnus) tunnukset
FROM $table
WHERE yhtio in ($kohderow[yhtiot])
$lajit
$group";
$abures = pupe_query($query);
while ($aburow = mysql_fetch_array($abures)) {
$query = "SELECT *
FROM $table
WHERE tunnus in ($aburow[tunnukset])
ORDER BY if(muutospvm = '0000-00-00 00:00:00', luontiaika, muutospvm) DESC
LIMIT 1";
$abures1 = pupe_query($query);
while ($aburow1 = mysql_fetch_assoc($abures1)) {
synkronoi($aburow1["yhtio"], $table, $aburow1["tunnus"], $aburow1, "F");
}
}
$query = "UNLOCK TABLES";
$abures = pupe_query($query);
echo "<font class='error'>$table -synkronointi valmis!</font><br><br>";
}
$tee = "";
}
if ($tee == "") {
$synkattavat = explode(',', $yhtiorow["synkronoi"]);
echo "<form method='post'>
<input type='hidden' name='tee' value='SYNK'>";
echo "<select name='table'>";
foreach ($synkattavat as $synk) {
echo "<option value='$synk'>$synk</option>";
}
echo "</select><br><br>";
echo "<input type='submit' value='".t("Synkronoi")."'></form><br><br>";
}
require "inc/footer.inc";