Add php files
This commit is contained in:
77
modules/EcmSales/bimit_importBaselinkerOrders.php
Normal file
77
modules/EcmSales/bimit_importBaselinkerOrders.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
$IMPORT_START_DATE = date('Y-m-dTH:i:s', strtotime("-1 days"));
|
||||
|
||||
|
||||
global $sugar_config;
|
||||
$baselinker_config = loadConfiguration();
|
||||
brecho($baselinker_config);
|
||||
$db = $GLOBALS['db'];
|
||||
|
||||
//last imported order
|
||||
$dbRes = $db->query("SELECT date_entered FROM baselinker_orders ORDER BY date_entered DESC LIMIT 1");
|
||||
$lastImportDate = strtotime($db->fetchByAssoc($dbRes)['date_entered']);
|
||||
if ($lastImportDate == null) {
|
||||
$lastImportDate = strtotime($IMPORT_START_DATE);
|
||||
}
|
||||
|
||||
$ordersRes = loadOrders($baselinker_config['token'], $lastImportDate);
|
||||
|
||||
if ($ordersRes->status != 'SUCCESS') {
|
||||
die('Błąd pobierania.');
|
||||
}
|
||||
$orders = $ordersRes->orders;
|
||||
|
||||
usort($orders, function ($a, $b) {
|
||||
return $b->date_confirmed - $a->date_confirmed;
|
||||
});
|
||||
|
||||
if (count($orders) > 0) {
|
||||
foreach ($orders as $order) {
|
||||
//brecho($order->order_id);
|
||||
$parsedOrder = base64_encode(json_encode($order));
|
||||
$unparsedOrder = json_decode(base64_decode($parsedOrder));
|
||||
$date = date("Y-m-d H:i:s", $order->date_confirmed);
|
||||
brecho($order->date_confirmed.' '.$date.' '.$order->order_id);
|
||||
brecho($order->order_source);
|
||||
brecho($order->products);
|
||||
//$db->query("INSERT INTO baselinker_orders VALUES('$order->order_id', '$order->external_order_id', '$order->order_source', '$order->date_add', '$parsedOrder')");
|
||||
}
|
||||
}
|
||||
brecho("Last: ".$lastImportDate.' '.date('Y-m-d H:i:s', $lastImportDate));
|
||||
|
||||
echo "Koniec.".PHP_EOL;
|
||||
|
||||
function loadConfiguration()
|
||||
{
|
||||
global $db;
|
||||
$dbRes = $db->query("SELECT * FROM config WHERE category='baselinker'");
|
||||
$config = [];
|
||||
while ($row = $db->fetchByAssoc($dbRes)) {
|
||||
$config[$row['name']] = $row['value'];
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
function loadOrders($token, $lastImportDate) {
|
||||
$methodParams = '{
|
||||
"date_confirmed_from": '.$lastImportDate.',
|
||||
"get_unconfirmed_orders": false
|
||||
}';
|
||||
$apiParams = [
|
||||
"method" => "getOrders",
|
||||
"parameters" => $methodParams
|
||||
];
|
||||
|
||||
$curl = curl_init("https://api.baselinker.com/connector.php");
|
||||
curl_setopt($curl, CURLOPT_POST, 1);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: ".$token]);
|
||||
curl_setopt($curl, CURLOPT_VERBOSE, 0);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
|
||||
return json_decode(curl_exec($curl));
|
||||
}
|
||||
function brecho($msg) {
|
||||
//echo '<br><pre>';
|
||||
var_dump($msg);
|
||||
echo PHP_EOL;
|
||||
//echo '</pre><br>';
|
||||
}
|
||||
Reference in New Issue
Block a user