| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Moloni\Services\Stocks\SyncStockFromMoloni; |
| 7 |
|
| 8 |
/** |
| 9 |
* This crons will run in isolation |
| 10 |
*/ |
| 11 |
class Crons |
| 12 |
{ |
| 13 |
public static function addCronInterval($schedules = []) |
| 14 |
{ |
| 15 |
$schedules['everyficeminutes'] = array( |
| 16 |
'interval' => 300, |
| 17 |
'display' => __('A cada cinco minutos') |
| 18 |
); |
| 19 |
|
| 20 |
return $schedules; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Service handler |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
* |
| 28 |
* @global $wpdb |
| 29 |
*/ |
| 30 |
public static function productsSync(): bool |
| 31 |
{ |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$runningAt = time(); |
| 35 |
|
| 36 |
try { |
| 37 |
self::requires(); |
| 38 |
|
| 39 |
if (!Start::login(true)) { |
| 40 |
Storage::$LOGGER->error(__('Não foi possível estabelecer uma ligação a uma empresa Moloni')); |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
if (defined('MOLONI_STOCK_SYNC') && (int)MOLONI_STOCK_SYNC !== 0) { |
| 45 |
if (!defined('MOLONI_STOCK_SYNC_TIME')) { |
| 46 |
define('MOLONI_STOCK_SYNC_TIME', (time() - 600)); |
| 47 |
|
| 48 |
$wpdb->insert($wpdb->get_blog_prefix() . 'moloni_api_config', [ |
| 49 |
'config' => 'moloni_stock_sync_time', |
| 50 |
'selected' => MOLONI_STOCK_SYNC_TIME |
| 51 |
]); |
| 52 |
} |
| 53 |
|
| 54 |
$service = new SyncStockFromMoloni(MOLONI_STOCK_SYNC_TIME); |
| 55 |
$service->run(); |
| 56 |
|
| 57 |
if ($service->countFoundRecord() > 0) { |
| 58 |
Storage::$LOGGER->info(__('Sincronização de stock automática'), [ |
| 59 |
'action' => 'stock:sync:cron', |
| 60 |
'since' => $service->getSince(), |
| 61 |
'equal' => $service->getEqual(), |
| 62 |
'not_found' => $service->getNotFound(), |
| 63 |
'get_updated' => $service->getUpdated(), |
| 64 |
'get_locked' => $service->getLocked(), |
| 65 |
]); |
| 66 |
} |
| 67 |
} |
| 68 |
} catch (Exception $ex) { |
| 69 |
Storage::$LOGGER->critical(__('Erro fatal'), [ |
| 70 |
'action' => 'stock:sync:cron:error', |
| 71 |
'exception' => $ex->getMessage() |
| 72 |
]); |
| 73 |
} |
| 74 |
|
| 75 |
Model::setOption('moloni_stock_sync_time', $runningAt); |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
public static function requires() |
| 81 |
{ |
| 82 |
$composer_autoloader = '../vendor/autoload.php'; |
| 83 |
if (is_readable($composer_autoloader)) { |
| 84 |
/** @noinspection PhpIncludeInspection */ |
| 85 |
require $composer_autoloader; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|