forked from devlab-oy/pupesoft
/
seurantakoodi_ulkoisesta_jarjestelmasta.php
151 lines (113 loc) · 4.91 KB
/
seurantakoodi_ulkoisesta_jarjestelmasta.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
140
141
142
143
144
145
146
147
148
149
150
151
<?php
// Kutsutaanko CLI:stä
if (php_sapi_name() != 'cli') {
die ("Tätä scriptiä voi ajaa vain komentoriviltä!\n");
}
date_default_timezone_set('Europe/Helsinki');
if (trim($argv[1]) == '') {
die ("Et antanut lähettävää yhtiötä!\n");
}
if (trim($argv[2]) == '') {
die ("Et antanut luettavien tiedostojen polkua!\n");
}
// lisätään includepathiin pupe-root
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.dirname(__FILE__));
// otetaan tietokanta connect ja funktiot
require "inc/connect.inc";
require "inc/functions.inc";
// Logitetaan ajo
cron_log();
// Sallitaan vain yksi instanssi tästä skriptistä kerrallaan
pupesoft_flock();
$yhtio = mysql_escape_string(trim($argv[1]));
$yhtiorow = hae_yhtion_parametrit($yhtio);
// Haetaan kukarow
$query = "SELECT *
FROM kuka
WHERE yhtio = '{$yhtio}'
AND kuka = 'admin'";
$kukares = pupe_query($query);
if (mysql_num_rows($kukares) != 1) {
exit("VIRHE: Admin käyttäjä ei löydy!\n");
}
$kukarow = mysql_fetch_assoc($kukares);
$path = trim($argv[2]);
$path = substr($path, -1) != '/' ? $path.'/' : $path;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' or $file == '..' or $file == '.DS_Store' or is_dir($path.$file)) continue;
$path_parts = pathinfo($file);
$ext = strtoupper($path_parts['extension']);
if ($ext == 'TC' or $ext == 'TXT') {
$filehandle = fopen($path.$file, "r");
$rahtikirja_hukassa = false;
while ($tietue = fgets($filehandle)) {
// Tyhjät rivit skipataan
if (trim($tietue) == "") continue;
if ($ext == 'TC') {
list($seurantakoodi, $posten_lahetenumero, $tilausnumero) = explode(';', $tietue);
}
else {
list($posten_lahetenumero, $tilausnumero, $seurantakoodi) = explode(';', $tietue);
}
if (trim($seurantakoodi) == '') continue;
// Otetaan vain eka ilmentymä tilausnumerosta jos sattuu olemaan monta eroteltuna spacella
list($tilausnumero) = explode(' ', $tilausnumero);
$tilausnumero = (int) $tilausnumero;
$seurantakoodi = preg_replace("/\r\n|\r|\n/", '', $seurantakoodi);
if ($tilausnumero == 0 or trim($seurantakoodi) == '') continue;
$query = "UPDATE rahtikirjat SET
rahtikirjanro = trim(concat(rahtikirjanro, ' ', '{$seurantakoodi}')),
tulostettu = now()
WHERE yhtio = '{$kukarow['yhtio']}'
AND otsikkonro = '{$tilausnumero}'";
pupe_query($query);
if (mysql_affected_rows() == 0) {
$rahtikirja_hukassa = true;
break;
}
$query = "SELECT SUM(kilot) kilotyht
FROM rahtikirjat
WHERE yhtio = '{$kukarow['yhtio']}'
AND otsikkonro = '{$tilausnumero}'";
$kilotres = pupe_query($query);
$kilotrow = mysql_fetch_assoc($kilotres);
paivita_rahtikirjat_tulostetuksi_ja_toimitetuksi(array('otunnukset' => $tilausnumero, 'kilotyht' => $kilotrow['kilotyht']));
$_magento_kaytossa = (!empty($magento_api_tt_url) and !empty($magento_api_tt_usr) and !empty($magento_api_tt_pas));
// Katsotaan onko Magento käytössä, silloin merkataan tilaus toimitetuksi Magentoon kun rahtikirja tulostetaan
if ($_magento_kaytossa) {
$query = "SELECT toimitustapa
FROM rahtikirjat
WHERE yhtio = '{$kukarow['yhtio']}'
AND otsikkonro = '{$tilausnumero}'";
$chk_res = pupe_query($query);
if (mysql_num_rows($chk_res) > 0) {
$chk_row = mysql_fetch_assoc($chk_res);
$query = "SELECT *
FROM toimitustapa
WHERE yhtio = '{$kukarow['yhtio']}'
AND selite = '{$chk_row['toimitustapa']}'";
$toitares = pupe_query($query);
$toitarow = mysql_fetch_assoc($toitares);
$query = "SELECT asiakkaan_tilausnumero
FROM lasku
WHERE yhtio = '{$kukarow['yhtio']}'
AND tunnus = '{$tilausnumero}'
AND laatija = 'Magento'
AND asiakkaan_tilausnumero != ''";
$mageres = pupe_query($query);
while ($magerow = mysql_fetch_assoc($mageres)) {
$magento_api_met = $toitarow['virallinen_selite'] != '' ? $toitarow['virallinen_selite'] : $toitarow['selite'];
$magento_api_rak = $seurantakoodi;
$magento_api_ord = $magerow["asiakkaan_tilausnumero"];
require "magento_toimita_tilaus.php";
}
}
}
}
// Jos rahtikirjaa ei löydetty niin ei siirretä done-kansioon
if (!$rahtikirja_hukassa) rename($path.$file, $path."done/".$file);
}
}
closedir($handle);
}