-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpo-ips-xrechnung.php
331 lines (296 loc) · 9.55 KB
/
wpo-ips-xrechnung.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
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
<?php
/**
* Plugin Name: PDF Invoices & Packing Slips for WooCommerce - XRechnung
* Requires Plugins: woocommerce-pdf-invoices-packing-slips
* Plugin URI: https://github.com/wpovernight/wpo-ips-xrechnung
* Description: XRechnung add-on for PDF Invoices & Packing Slips for WooCommerce plugin.
* Version: 1.0.3-beta-5
* Update URI: https://github.com/wpovernight/wpo-ips-xrechnung
* Author: WP Overnight
* Author URI: https://wpovernight.com
* License: GPLv3
* License URI: https://opensource.org/licenses/gpl-license.php
* Text Domain: wpo-ips-xrechnung
*/
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( ! class_exists( 'WPO_IPS_XRechnung' ) ) {
class WPO_IPS_XRechnung {
/**
* Plugin version
*
* @var string
*/
public $version = '1.0.3-beta-5';
/**
* Base plugin version
*
* @var string
*/
public $base_plugin_version = '3.9.5-beta-9';
/**
* UBL format
*
* @var string
*/
public $ubl_format = 'xrechnung';
/**
* Format name
*
* @var string
*/
public $format_name = 'EN16931 XRechnung';
/**
* Root element
*
* @var string
*/
public $root_element = 'ubl:Invoice';
/**
* Plugin path
*
* @var string
*/
public $plugin_path;
/**
* Plugin instance
*
* @var WPO_IPS_XRechnung
*/
private static $_instance;
/**
* Plugin instance
*
* @return WPO_IPS_XRechnung
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*/
public function __construct() {
$this->plugin_path = plugin_dir_path( __FILE__ );
$plugin_file = basename( $this->plugin_path ) . '/wpo-ips-xrechnung.php';
$github_updater_file = $this->plugin_path . 'github-updater/GitHubUpdater.php';
if ( ! class_exists( '\\WPO\\GitHubUpdater\\GitHubUpdater' ) && file_exists( $github_updater_file ) ) {
require_once $github_updater_file;
}
if ( class_exists( '\\WPO\\GitHubUpdater\\GitHubUpdater' ) ) {
$gitHubUpdater = new \WPO\GitHubUpdater\GitHubUpdater( $plugin_file );
$gitHubUpdater->setChangelog( 'CHANGELOG.md' );
$gitHubUpdater->add();
}
if ( class_exists( 'WPO_WCPDF' ) && version_compare( WPO_WCPDF()->version, $this->base_plugin_version, '<' ) ) {
add_action( 'admin_notices', array( $this, 'base_plugin_dependency_notice' ) );
return;
}
include_once dirname( __FILE__ ) . '/vendor/autoload.php';
add_action( 'init', array( $this, 'load_translations' ) );
add_action( 'before_woocommerce_init', array( $this, 'custom_order_tables_compatibility' ) );
add_filter( 'wpo_wcpdf_document_ubl_settings_formats', array( $this, 'add_format_to_ubl_settings' ), 10, 2 );
add_filter( 'wpo_wc_ubl_document_root_element', array( $this, 'add_root_element' ), 10, 2 );
add_filter( 'wpo_wc_ubl_document_format', array( $this, 'set_document_format' ), 10, 2 );
add_filter( 'wpo_wc_ubl_document_namespaces', array( $this, 'set_document_namespaces' ), 10, 2 );
}
/**
* Base plugin dependency notice
*
* @return void
*/
public function base_plugin_dependency_notice(): void {
$error = sprintf(
/* translators: plugin version */
__( 'PDF Invoices & Packing Slips for WooCommerce - XRechnung requires PDF Invoices & Packing Slips for WooCommerce version %s or higher.', 'wpo-ips-xrechnung' ),
$this->base_plugin_version
);
$message = sprintf(
'<div class="notice notice-error"><p>%s</p></div>',
$error,
);
echo $message;
}
/**
* Load translations
*
* @return void
*/
public function load_translations(): void {
load_plugin_textdomain( 'wpo-ips-xrechnung', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Add HPOS compatibility
*
* @return void
*/
public function custom_order_tables_compatibility(): void {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
/**
* Add format to UBL settings
*
* @param array $formats
* @param \WPO\IPS\Documents\OrderDocument $document
* @return array
*/
public function add_format_to_ubl_settings( array $formats, \WPO\IPS\Documents\OrderDocument $document ): array {
if ( $document && 'invoice' === $document->get_type() ) {
$formats[ $this->ubl_format ] = $this->format_name;
}
return $formats;
}
/**
* Check if UBL document is XRechnung
*
* @param \WPO\IPS\UBL\Documents\UblDocument $ubl_document
* @return bool
*/
private function is_xrechnung_ubl_document( \WPO\IPS\UBL\Documents\UblDocument $ubl_document ): bool {
return (
is_callable( array( $ubl_document->order_document, 'get_ubl_format' ) ) &&
$this->ubl_format === $ubl_document->order_document->get_ubl_format()
);
}
/**
* Add root element
*
* @param string $root_element
* @param \WPO\IPS\UBL\Documents\UblDocument $ubl_document
* @return string
*/
public function add_root_element( string $root_element, \WPO\IPS\UBL\Documents\UblDocument $ubl_document ): string {
if ( $this->is_xrechnung_ubl_document( $ubl_document ) ) {
$root_element = $this->root_element;
}
return $root_element;
}
/**
* Set document format
*
* @param array $format
* @param \WPO\IPS\UBL\Documents\UblDocument $ubl_document
* @return array
*/
public function set_document_format( array $format, \WPO\IPS\UBL\Documents\UblDocument $ubl_document ): array {
if ( $this->is_xrechnung_ubl_document( $ubl_document ) ) {
$format = apply_filters( 'wpo_ips_xrechnung_document_format', array(
'customizationid' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\CustomizationIdHandler::class,
),
'profileid' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\ProfileIdHandler::class,
),
'id' => array(
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Common\IdHandler::class,
),
'issuedate' => array(
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Common\IssueDateHandler::class,
),
'duedate' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\DueDateHandler::class,
),
'invoicetype' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Invoice\InvoiceTypeCodeHandler::class,
),
'invoicenote' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Invoice\InvoiceNoteHandler::class,
),
'documentcurrencycode' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\DocumentCurrencyCodeHandler::class,
),
'buyerreference' => array(
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Common\BuyerReferenceHandler::class,
),
'additionaldocumentreference' => array(
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Common\AdditionalDocumentReferenceHandler::class,
),
'accountsupplierparty' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\AddressHandler::class,
'options' => array(
'root' => 'AccountingSupplierParty',
),
),
'accountingcustomerparty' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\AddressHandler::class,
'options' => array(
'root' => 'AccountingCustomerParty',
),
),
'delivery' => array(
'enabled' => false,
'handler' => \WPO\IPS\UBL\Handlers\Common\DeliveryHandler::class,
),
'paymentmeans' => array(
'enabled' => true,
'handler' => \WPO\IPS\UBL\Handlers\Common\PaymentMeansHandler::class,
),
'paymentterms' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\PaymentTermsHandler::class,
),
'allowancecharge' => array(
'enabled' => false,
'handler' => \WPO\IPS\UBL\Handlers\Common\AllowanceChargeHandler::class,
),
'taxtotal' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\TaxTotalHandler::class,
),
'legalmonetarytotal' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Common\LegalMonetaryTotalHandler::class,
),
'invoicelines' => array(
'enabled' => true,
'handler' => \WPO\IPS\XRechnung\Handlers\Invoice\InvoiceLineHandler::class,
),
), $ubl_document );
}
return $format;
}
/**
* Set document namespaces
*
* @param array $namespaces
* @param \WPO\IPS\UBL\Documents\UblDocument $ubl_document
* @return array
*/
public function set_document_namespaces( array $namespaces, \WPO\IPS\UBL\Documents\UblDocument $ubl_document ): array {
if ( $this->is_xrechnung_ubl_document( $ubl_document ) ) {
$namespaces = apply_filters( 'wpo_ips_xrechnung_document_namespaces', array(
'ubl' => 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
'cac' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2',
'cbc' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2',
), $ubl_document );
}
return $namespaces;
}
}
}
/**
* Plugin instance
*
* @return WPO_IPS_XRechnung
*/
function WPO_IPS_XRechnung() {
return WPO_IPS_XRechnung::instance();
}
add_action( 'plugins_loaded', 'WPO_IPS_XRechnung', 99 );