| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni; |
| 4 |
|
| 5 |
use Moloni\Controllers\Documents; |
| 6 |
|
| 7 |
/** |
| 8 |
* Main constructor |
| 9 |
* Class Plugin |
| 10 |
* @package Moloni |
| 11 |
*/ |
| 12 |
class Plugin |
| 13 |
{ |
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$this->actions(); |
| 17 |
$this->crons(); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Define some table params |
| 22 |
* Load scripts and CSS as needed |
| 23 |
*/ |
| 24 |
public static function defines() |
| 25 |
{ |
| 26 |
if (isset($_REQUEST['page']) && !wp_doing_ajax() && sanitize_text_field($_REQUEST['page']) === 'moloni') { |
| 27 |
wp_enqueue_style('jquery-modal', plugins_url('assets/external/jquery.modal.min.css', MOLONI_PLUGIN_FILE)); |
| 28 |
wp_enqueue_script('jquery-modal', plugins_url('assets/external/jquery.modal.min.js', MOLONI_PLUGIN_FILE)); |
| 29 |
|
| 30 |
wp_enqueue_style('moloni-styles', plugins_url('assets/css/moloni.css', MOLONI_PLUGIN_FILE)); |
| 31 |
wp_enqueue_script('moloni-actions-bulk-documentes-js', plugins_url('assets/js/BulkDocuments.js', MOLONI_PLUGIN_FILE)); |
| 32 |
|
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
private function actions() |
| 37 |
{ |
| 38 |
new Menus\Admin($this); |
| 39 |
new Hooks\ProductUpdate($this); |
| 40 |
new Hooks\ProductView($this); |
| 41 |
new Hooks\OrderView($this); |
| 42 |
new Hooks\OrderPaid($this); |
| 43 |
new Ajax($this); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Setting up the crons if needed |
| 48 |
*/ |
| 49 |
private function crons() |
| 50 |
{ |
| 51 |
add_filter('cron_schedules', '\Moloni\Crons::addCronInterval'); |
| 52 |
add_action('moloniProductsSync', '\Moloni\Crons::productsSync'); |
| 53 |
|
| 54 |
if (!wp_next_scheduled('moloniProductsSync')) { |
| 55 |
wp_schedule_event(time(), 'everyficeminutes', 'moloniProductsSync'); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Main function |
| 61 |
* This will run when accessing the page "moloni" and the routing shoud be done here with and $_GET['action'] |
| 62 |
*/ |
| 63 |
public function run() |
| 64 |
{ |
| 65 |
try { |
| 66 |
/** If the user is not logged in show the login form */ |
| 67 |
if (Start::login()) { |
| 68 |
$action = isset($_REQUEST['action']) ? sanitize_text_field($_REQUEST['action']) : ''; |
| 69 |
|
| 70 |
switch ($action) { |
| 71 |
case 'remInvoice': |
| 72 |
$this->removeOrder((int)$_GET['id']); |
| 73 |
break; |
| 74 |
|
| 75 |
case 'remInvoiceAll': |
| 76 |
$this->removeOrdersAll(); |
| 77 |
break; |
| 78 |
|
| 79 |
case 'genInvoice': |
| 80 |
$orderId = (int)$_REQUEST['id']; |
| 81 |
/** @var Documents $document intended */ |
| 82 |
/** @noinspection PhpUnusedLocalVariableInspection */ |
| 83 |
$document = $this->createDocument($orderId); |
| 84 |
break; |
| 85 |
|
| 86 |
case 'syncStocks': |
| 87 |
$this->syncStocks(); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'remLogs': |
| 91 |
Log::removeLogs(); |
| 92 |
add_settings_error('moloni', 'moloni-rem-logs', __('A limpeza de logs foi concluída.'), 'updated'); |
| 93 |
break; |
| 94 |
|
| 95 |
case 'getInvoice': |
| 96 |
$document = false; |
| 97 |
$documentId = (int)$_REQUEST['id']; |
| 98 |
|
| 99 |
if ($documentId > 0) { |
| 100 |
$document = Documents::showDocument($documentId); |
| 101 |
} |
| 102 |
|
| 103 |
if (!$document) { |
| 104 |
add_settings_error('moloni', 'moloni-document-not-found', __('Documento não encontrado')); |
| 105 |
} |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if (!wp_doing_ajax()) { |
| 110 |
include MOLONI_TEMPLATE_DIR . 'MainContainer.php'; |
| 111 |
} |
| 112 |
} |
| 113 |
} catch (Error $error) { |
| 114 |
$error->showError(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param $orderId |
| 120 |
* @return Documents |
| 121 |
* @throws Error |
| 122 |
*/ |
| 123 |
private function createDocument($orderId) |
| 124 |
{ |
| 125 |
$document = new Documents($orderId); |
| 126 |
$document->createDocument(); |
| 127 |
|
| 128 |
if ($document->document_id) { |
| 129 |
$viewUrl = ' <a href="' . admin_url('admin.php?page=moloni&action=getInvoice&id=' . $document->document_id) . '" target="_BLANK">Ver documento</a>'; |
| 130 |
add_settings_error('moloni', 'moloni-document-created-success', __('O documento foi gerado!') . $viewUrl, 'updated'); |
| 131 |
} |
| 132 |
|
| 133 |
return $document; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param int $orderId |
| 138 |
*/ |
| 139 |
private function removeOrder($orderId) |
| 140 |
{ |
| 141 |
if (isset($_GET['confirm']) && sanitize_text_field($_GET['confirm']) === 'true') { |
| 142 |
add_post_meta($orderId, '_moloni_sent', '-1', true); |
| 143 |
add_settings_error('moloni', 'moloni-order-remove-success', __('A encomenda ' . $orderId . ' foi marcada como gerada!'), 'updated'); |
| 144 |
} else { |
| 145 |
add_settings_error( |
| 146 |
'moloni', |
| 147 |
'moloni-order-remove', |
| 148 |
__('Confirma que pretende marcar a encomenda ' . $orderId . " como paga? <a href='" . admin_url('admin.php?page=moloni&action=remInvoice&confirm=true&id=' . $orderId) . "'>Sim confirmo!</a>") |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
private function removeOrdersAll() |
| 155 |
{ |
| 156 |
if (isset($_GET['confirm']) && sanitize_text_field($_GET['confirm']) === 'true') { |
| 157 |
$allOrders = Controllers\PendingOrders::getAllAvailable(); |
| 158 |
if (!empty($allOrders) && is_array($allOrders)) { |
| 159 |
foreach ($allOrders as $order) { |
| 160 |
add_post_meta($order['id'], '_moloni_sent', '-1', true); |
| 161 |
} |
| 162 |
add_settings_error('moloni', 'moloni-order-all-remove-success', __('Todas as encomendas foram marcadas como geradas!'), 'updated'); |
| 163 |
} else { |
| 164 |
add_settings_error('moloni', 'moloni-order-all-remove-not-found', __('Não foram encontradas encomendas por gerar')); |
| 165 |
} |
| 166 |
} else { |
| 167 |
add_settings_error( |
| 168 |
'moloni', 'moloni-order-remove', __("Confirma que pretende marcar todas as encomendas como já geradas? <a href='" . admin_url('admin.php?page=moloni&action=remInvoiceAll&confirm=true') . "'>Sim confirmo!</a>") |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private function syncStocks() |
| 174 |
{ |
| 175 |
$date = isset($_GET['since']) ? sanitize_text_field($_GET['since']) : gmdate('Y-m-d', strtotime('-1 week')); |
| 176 |
|
| 177 |
$syncStocksResult = (new Controllers\SyncProducts($date))->run(); |
| 178 |
|
| 179 |
if ($syncStocksResult->countUpdated() > 0) { |
| 180 |
add_settings_error('moloni', 'moloni-sync-stocks-updated', __('Foram actualizados ' . $syncStocksResult->countUpdated() . ' artigos.'), 'updated'); |
| 181 |
} |
| 182 |
|
| 183 |
if ($syncStocksResult->countEqual() > 0) { |
| 184 |
add_settings_error('moloni', 'moloni-sync-stocks-updated', __('Existem ' . $syncStocksResult->countEqual() . ' artigos com stock igual.'), 'updated'); |
| 185 |
} |
| 186 |
|
| 187 |
if ($syncStocksResult->countNotFound() > 0) { |
| 188 |
add_settings_error('moloni', 'moloni-sync-stocks-not-found', __('Não foram encontrados no WooCommerce ' . $syncStocksResult->countNotFound() . ' artigos.')); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|