-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathzatca-e-invoicing.php
More file actions
362 lines (315 loc) · 12.2 KB
/
zatca-e-invoicing.php
File metadata and controls
362 lines (315 loc) · 12.2 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* Plugin Name: Zatca-wordpress-plugin
* Plugin URI: https://github.com/Husam-Almiyah/Zatca-wordpress-plugin
* Description: A comprehensive WordPress plugin that enables Saudi Arabian businesses to comply with ZATCA (Zakat, Tax and Customs Authority) e-invoicing requirements through WooCommerce integration.
* Version: 1.0.0
* Author: Husam Al-Miyah
* Author URI: https://github.com/Husam-Almiyah
* Text Domain: zatca-invoicing
* Domain Path: /languages
* Requires at least: 5.0
* Tested up to: 5.9.3
* WC requires at least: 6.0
* WC tested up to: 6.4.1
* License: GPL v3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('ZATCA_INVOICING_VERSION', '1.0.0');
define('ZATCA_INVOICING_PLUGIN_FILE', __FILE__);
define('ZATCA_INVOICING_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('ZATCA_INVOICING_PLUGIN_URL', plugin_dir_url(__FILE__));
define('ZATCA_INVOICING_PLUGIN_BASENAME', plugin_basename(__FILE__));
/**
* Main ZATCA Invoicing Class
*/
final class ZATCA_EInvoicing {
/**
* The single instance of the class.
*/
protected static $_instance = null;
/**
* Main ZATCA_EInvoicing Instance.
*
* Ensures only one instance of ZATCA_EInvoicing is loaded or can be loaded.
*
* @return ZATCA_EInvoicing - Main instance.
*/
public static function instance() {
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor.
*/
public function __construct() {
$this->init_hooks();
}
/**
* Hook into actions and filters.
*/
private function init_hooks() {
register_activation_hook(__FILE__, array($this, 'activate'));
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
add_action('init', array($this, 'init'), 0);
add_action('plugins_loaded', array($this, 'plugins_loaded'), 11);
}
/**
* Initialize the plugin after all plugins are loaded.
*/
public function plugins_loaded() {
// Check if WooCommerce is active
if (!$this->is_woocommerce_active()) {
add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
return;
}
// Load plugin textdomain
load_plugin_textdomain('zatca-invoicing', false, dirname(plugin_basename(__FILE__)) . '/languages');
// Initialize classes
$this->includes();
$this->init_classes();
}
/**
* Initialize plugin functionality.
*/
public function init() {
// This is called early, before plugins_loaded
do_action('zatca_invoicing_init');
}
/**
* Include required files.
*/
private function includes() {
// Core utility classes
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-qr-generator.php';
// Core includes
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-settings.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-phase1.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-phase2.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-api-manager.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-certificate-manager.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/class-zatca-woocommerce-integration.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/XML/XMLGenerator.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Signature/InvoiceSigner.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Schema.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Helpers/Certificate.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Helpers/InvoiceExtension.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Helpers/InvoiceSignatureBuilder.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Helpers/QRCodeGenerator.php';
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Helpers/Storage.php';
// Admin includes
if (is_admin()) {
// Use correct case-sensitive paths for cross-platform compatibility
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/Admin/class-zatca-admin.php';
}
// API includes
include_once ZATCA_INVOICING_PLUGIN_DIR . 'includes/API/class-zatca-api.php';
}
/**
* Initialize plugin classes.
*/
private function init_classes() {
// Initialize settings
ZATCA_Settings::instance();
// Initialize integrations
ZATCA_WooCommerce_Integration::instance();
// Initialize admin
if (is_admin()) {
ZATCA_Admin::instance();
// Unified admin combines original and enhanced admin features
}
// Initialize API
ZATCA_API::instance();
}
/**
* Check if WooCommerce is active.
*/
private function is_woocommerce_active() {
return class_exists('WooCommerce');
}
/**
* WooCommerce fallback notice.
*/
public function woocommerce_missing_notice() {
echo '<div class="error"><p><strong>' . sprintf(
esc_html__('ZATCA E-Invoicing requires WooCommerce to be installed and active. You can download %s here.', 'zatca-invoicing'),
'<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>'
) . '</strong></p></div>';
}
/**
* Plugin activation.
*/
public function activate() {
// Create database tables if needed
$this->create_tables();
// Set default options
$this->set_default_options();
// Schedule any necessary cron jobs
$this->schedule_events();
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Plugin deactivation.
*/
public function deactivate() {
// Clear scheduled events
wp_clear_scheduled_hook('zatca_invoicing_daily_tasks');
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Create plugin database tables.
*/
private function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Table for storing certificates (supports dual certificate system per environment)
$table_name = $wpdb->prefix . 'zatca_certificates';
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
certificate_name varchar(255) NOT NULL,
certificate_type enum('onboarding', 'production', 'csr') NOT NULL DEFAULT 'onboarding',
environment enum('sandbox', 'simulation', 'production') NOT NULL DEFAULT 'sandbox',
certificate_data longtext NOT NULL,
csr_data longtext NOT NULL,
private_key_data longtext NOT NULL,
binary_security_token longtext NULL,
secret varchar(255) NULL,
request_id varchar(255) NULL COMMENT 'compliance_request_id for production cert requests',
is_active tinyint(1) DEFAULT 0,
invoice_types varchar(10) DEFAULT '0100' COMMENT '0100=simplified, 1000=standard, 1100=both',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
expires_at datetime NULL,
PRIMARY KEY (id),
UNIQUE KEY environment_type_active (environment, certificate_type, is_active),
KEY environment (environment),
KEY certificate_type (certificate_type),
KEY is_active (is_active)
) $charset_collate;";
dbDelta($sql);
// Table for tracking onboarding sessions per environment
$table_name = $wpdb->prefix . 'zatca_onboarding_sessions';
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
environment enum('sandbox', 'simulation', 'production') NOT NULL,
session_status enum('pending', 'in_progress', 'completed', 'failed') DEFAULT 'pending',
invoice_types varchar(10) NOT NULL COMMENT 'Types to test: 0100, 1000, or 1100',
onboarding_cert_id int(11) NULL,
production_cert_id int(11) NULL,
compliance_tests json NULL COMMENT 'Results of compliance tests for each invoice type',
started_at datetime DEFAULT CURRENT_TIMESTAMP,
completed_at datetime NULL,
PRIMARY KEY (id),
FOREIGN KEY (onboarding_cert_id) REFERENCES {$wpdb->prefix}zatca_certificates(id) ON DELETE SET NULL,
FOREIGN KEY (production_cert_id) REFERENCES {$wpdb->prefix}zatca_certificates(id) ON DELETE SET NULL,
UNIQUE KEY environment_unique (environment),
KEY session_status (session_status)
) $charset_collate;";
dbDelta($sql);
// Table for tracking invoice submissions with flow separation
$table_name = $wpdb->prefix . 'zatca_invoice_submissions';
$sql = "CREATE TABLE $table_name (
id int(11) NOT NULL AUTO_INCREMENT,
order_id int(11) NOT NULL,
invoice_type varchar(255) NOT NULL,
submission_type enum('phase1', 'onboarding', 'production') NOT NULL,
certificate_id int(11) NULL,
environment enum('sandbox', 'simulation', 'production') NOT NULL,
status enum('pending', 'completed', 'submitted', 'cleared', 'reported', 'failed', 'validated') DEFAULT 'pending',
irn varchar(255) NULL,
pih varchar(255) NULL,
qr_code text NULL,
signed_xml longtext NULL,
invoice_hash varchar(255) NULL,
uuid varchar(255) NULL,
zatca_response json NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
submitted_at datetime DEFAULT CURRENT_TIMESTAMP,
processed_at datetime NULL,
PRIMARY KEY (id),
FOREIGN KEY (certificate_id) REFERENCES {$wpdb->prefix}zatca_certificates(id) ON DELETE SET NULL,
UNIQUE KEY order_submission_type (order_id, submission_type, invoice_type),
KEY order_id (order_id),
KEY submission_type (submission_type),
KEY environment (environment),
KEY status (status),
KEY created_at (created_at)
) $charset_collate;";
dbDelta($sql);
}
/**
* Set default plugin options.
*/
private function set_default_options() {
$default_options = array(
'zatca_enabled' => 'yes',
'zatca_phase' => 'phase1',
'zatca_environment' => 'sandbox',
'zatca_seller_name' => '',
'zatca_vat_number' => '',
'zatca_business_category' => '',
'zatca_csr_invoice_type' => '0100',
'zatca_auto_generate' => 'yes',
'zatca_qr_size' => '150',
'zatca_debug' => 'no'
);
foreach ($default_options as $key => $value) {
if (get_option($key) === false) {
add_option($key, $value);
}
}
}
/**
* Schedule plugin events.
*/
private function schedule_events() {
if (!wp_next_scheduled('zatca_invoicing_daily_tasks')) {
wp_schedule_event(time(), 'daily', 'zatca_invoicing_daily_tasks');
}
}
/**
* Get the plugin version.
*/
public function get_version() {
return ZATCA_INVOICING_VERSION;
}
/**
* Get the plugin file.
*/
public function get_plugin_file() {
return ZATCA_INVOICING_PLUGIN_FILE;
}
/**
* Get the plugin directory.
*/
public function get_plugin_dir() {
return ZATCA_INVOICING_PLUGIN_DIR;
}
/**
* Get the plugin URL.
*/
public function get_plugin_url() {
return ZATCA_INVOICING_PLUGIN_URL;
}
}
/**
* Main instance of ZATCA_EInvoicing.
*
* Returns the main instance of ZATCA_EInvoicing to prevent the need to use globals.
*
* @return ZATCA_EInvoicing
*/
function ZATCA_EInvoicing() {
return ZATCA_EInvoicing::instance();
}
// Initialize the plugin
ZATCA_EInvoicing();