| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\Accounting; |
| 3 |
|
| 4 |
use WeDevs\ERP\Framework\Traits\Hooker; |
| 5 |
|
| 6 |
/** |
| 7 |
* WeDevs_ERP_Accounting class |
| 8 |
* |
| 9 |
* @class WeDevs_ERP_Accounting The class that holds the entire WeDevs_ERP_Accounting plugin |
| 10 |
*/ |
| 11 |
class Accounting { |
| 12 |
|
| 13 |
use Hooker; |
| 14 |
|
| 15 |
/** |
| 16 |
* The main plugin instance |
| 17 |
* |
| 18 |
* @var Object |
| 19 |
*/ |
| 20 |
private $plugin; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initializes the WeDevs_ERP_Accounting() class |
| 24 |
* |
| 25 |
* Checks for an existing WeDevs_ERP_Accounting() instance |
| 26 |
* and if it doesn't find one, creates it. |
| 27 |
*/ |
| 28 |
public static function init() { |
| 29 |
static $instance = false; |
| 30 |
|
| 31 |
if ( ! $instance ) { |
| 32 |
$instance = new self(); |
| 33 |
} |
| 34 |
|
| 35 |
return $instance; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor for the WeDevs_ERP_Accounting class |
| 40 |
* |
| 41 |
* Sets up all the appropriate hooks and actions |
| 42 |
* within our plugin. |
| 43 |
*/ |
| 44 |
public function __construct( $plugin ) { |
| 45 |
// prevent duplicate loading |
| 46 |
if ( did_action( 'erp_accounting_loaded' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$this->plugin = $plugin; |
| 51 |
|
| 52 |
$this->deactive_addon(); |
| 53 |
|
| 54 |
// Define constants |
| 55 |
$this->define_constants(); |
| 56 |
|
| 57 |
// Include required files |
| 58 |
$this->includes(); |
| 59 |
|
| 60 |
// load the module |
| 61 |
add_action( 'erp_loaded', array( $this, 'plugin_init' ) ); |
| 62 |
|
| 63 |
// trigger after accounting module loaded |
| 64 |
do_action('erp_accounting_loaded'); |
| 65 |
|
| 66 |
// pdf plugin is not installed notice |
| 67 |
if ( empty( get_option( 'pdf-notice-dismissed' ) ) ) { |
| 68 |
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
add_action( 'wp_ajax_dismiss_pdf_notice', array( $this, 'dismiss_pdf_notice' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Backward compatibility |
| 76 |
* |
| 77 |
* Check if the previous accounting add-on installed. |
| 78 |
* If found, deactivate the add-on |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
function deactive_addon() { |
| 83 |
/** |
| 84 |
* Detect plugin. For use on Front End only. |
| 85 |
*/ |
| 86 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 87 |
|
| 88 |
// check for plugin using plugin name |
| 89 |
if ( is_plugin_active( 'accounting/accounting.php' ) ) { |
| 90 |
deactivate_plugins( 'accounting/accounting.php' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Init the accounting module |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function plugin_init() { |
| 101 |
$this->init_classes(); |
| 102 |
$this->init_actions(); |
| 103 |
$this->init_filters(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Define the plugin constants |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function define_constants() { |
| 112 |
$this->define( 'WPERP_ACCOUNTING_PATH', dirname( __FILE__ ) ); |
| 113 |
$this->define( 'WPERP_ACCOUNTING_URL', plugins_url( '', __FILE__ ) ); |
| 114 |
$this->define( 'WPERP_ACCOUNTING_ASSETS', WPERP_ACCOUNTING_URL . '/assets' ); |
| 115 |
$this->define( 'WPERP_ACCOUNTING_JS_TMPL', WPERP_ACCOUNTING_PATH . '/includes/views/js-templates' ); |
| 116 |
$this->define( 'WPERP_ACCOUNTING_VIEWS', WPERP_ACCOUNTING_PATH . '/includes/views' ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Define constant if not already set |
| 121 |
* |
| 122 |
* @param string $name |
| 123 |
* @param string|bool $value |
| 124 |
* @return type |
| 125 |
*/ |
| 126 |
private function define( $name, $value ) { |
| 127 |
if ( ! defined( $name ) ) { |
| 128 |
define( $name, $value ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Include the required files |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
private function includes() { |
| 138 |
if ( function_exists( 'erp_ac_get_manager_role' ) ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
require_once WPERP_ACCOUNTING_PATH . '/includes/function-capabilities.php'; |
| 143 |
require_once WPERP_ACCOUNTING_PATH . '/includes/actions-filters.php'; |
| 144 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-transaction.php'; |
| 145 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-chart.php'; |
| 146 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions.php'; |
| 147 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-dashboard.php'; |
| 148 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-reporting.php'; |
| 149 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-bulk-action.php'; |
| 150 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-url.php'; |
| 151 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-tax.php'; |
| 152 |
require_once WPERP_ACCOUNTING_PATH . '/includes/functions-html.php'; |
| 153 |
|
| 154 |
// cli command |
| 155 |
if ( defined('WP_CLI') && WP_CLI ) { |
| 156 |
include WPERP_ACCOUNTING_PATH . '/includes/cli/commands.php'; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Initialize the classes |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function init_classes() { |
| 166 |
new Logger(); |
| 167 |
new Admin_Menu(); |
| 168 |
new Form_Handler(); |
| 169 |
new User_Profile(); |
| 170 |
|
| 171 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 172 |
new Ajax_Handler(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Init the plugin actions |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function init_actions() { |
| 182 |
add_action( 'admin_footer', array( $this, 'admin_js_templates' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Init the plugin filters |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function init_filters() { |
| 191 |
add_filter( 'erp_settings_pages', array( $this, 'add_settings_page' ) ); |
| 192 |
} |
| 193 |
|
| 194 |
public function add_settings_page( $settings = array() ) { |
| 195 |
$settings[] = include __DIR__ . '/includes/class-settings.php'; |
| 196 |
return $settings; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Print JS templates in footer |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public function admin_js_templates() { |
| 205 |
global $current_screen; |
| 206 |
|
| 207 |
$hook = str_replace( sanitize_title( __( 'Accounting', 'erp' ) ) , 'accounting', $current_screen->base ); |
| 208 |
|
| 209 |
if ( $hook == 'accounting_page_erp-accounting-expense' ) { |
| 210 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/invoice.php', 'erp-ac-invoice-payment-pop' ); |
| 211 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/vendor.php', 'erp-ac-new-vendor-content-pop' ); |
| 212 |
} |
| 213 |
|
| 214 |
if ( $hook == 'accounting_page_erp-accounting-bank' ) { |
| 215 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/bank.php', 'erp-ac-transfer-money-pop' ); |
| 216 |
} |
| 217 |
|
| 218 |
if ( $hook == 'accounting_page_erp-accounting-sales' ) { |
| 219 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/invoice.php', 'erp-ac-invoice-payment-pop' ); |
| 220 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/customer.php', 'erp-ac-new-customer-content-pop' ); |
| 221 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/send-invoice.php', 'erp-ac-send-email-invoice-pop' ); |
| 222 |
} |
| 223 |
|
| 224 |
if ( $hook == 'erp-settings_page_erp-settings' && isset( $_GET['section'] ) && $_GET['section'] == 'erp_ac_tax' ) { |
| 225 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/new-tax-form.php', 'erp-ac-new-tax-form-popup' ); |
| 226 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/tax-items.php', 'erp-ac-items-details-popup' ); |
| 227 |
} |
| 228 |
|
| 229 |
if ( $hook == 'accounting_page_erp-accounting-sales' || $hook == 'accounting_page_erp-accounting-expense' ) { |
| 230 |
erp_get_js_template( WPERP_ACCOUNTING_JS_TMPL . '/trash.php', 'erp-ac-trash-form-popup' ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Show notice if PDF plugin is not installed |
| 236 |
* |
| 237 |
* @since 1.3.6 |
| 238 |
* |
| 239 |
* @return void |
| 240 |
*/ |
| 241 |
public function admin_notice() { |
| 242 |
if ( current_user_can( 'install_plugins' ) ) { |
| 243 |
|
| 244 |
$action = empty($_GET['erp-pdf']) ? '' : \sanitize_text_field($_GET['erp-pdf']); |
| 245 |
$plugin = 'erp-pdf-invoice/wp-erp-pdf.php'; |
| 246 |
$pdf_install = new \WeDevs\ERP\Accounting\PDF_Install(); |
| 247 |
|
| 248 |
if ($action === 'install') { |
| 249 |
$pdf_install->install_plugin('https://downloads.wordpress.org/plugin/erp-pdf-invoice.1.0.0.zip'); |
| 250 |
} elseif ($action === 'active') { |
| 251 |
$pdf_install->activate_pdf_plugin($plugin); |
| 252 |
} |
| 253 |
|
| 254 |
if (\file_exists(WP_PLUGIN_DIR . '/' . $plugin)) { |
| 255 |
if (! \is_plugin_active($plugin)) { |
| 256 |
$this->pdf_notice_message('active'); |
| 257 |
} |
| 258 |
} else { |
| 259 |
$this->pdf_notice_message('install'); |
| 260 |
} |
| 261 |
|
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* PDF notice message |
| 267 |
* |
| 268 |
* @since 1.3.6 |
| 269 |
* |
| 270 |
* @param String $type |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
public function pdf_notice_message( $type ) { |
| 275 |
$actual_link = esc_url( (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ); |
| 276 |
$sign = empty( $_GET ) ? '?' : '&'; |
| 277 |
|
| 278 |
echo '<div class="updated notice is-dismissible notice-pdf"><p>'; |
| 279 |
echo __( 'Please ' . $type . ' <a href="' . $actual_link . $sign . 'erp-pdf=' . $type . '">WP ERP PDF</a> extension to get PDF export feature.', 'erp' ); |
| 280 |
echo '</p></div>'; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Dismiss PDF notice message |
| 285 |
* |
| 286 |
* @since 1.3.6 |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function dismiss_pdf_notice() { |
| 291 |
update_option( 'pdf-notice-dismissed', 1 ); |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|