toutpratique/modules/ordertaxprofitreport/ordertaxprofitreport.php
ToutPratique 1d1a2def5f push prod
2016-05-17 15:54:37 +02:00

570 lines
17 KiB
PHP

<?php
/**
* ordertaxprofitreport.php file defines main class of module
* @author Business Tech (www.businesstech.fr) - Contact: http://www.businesstech.fr/en/contact-us
* @version 2.8.5
* @category main class
* @license Business Tech
* @uses Please read included installation and configuration instructions (PDF format)
* @see lib/install
* => i-install.php => interface
* => install-ctrl_class.php => controller, manage factory with config or sql install object
* => install-config classes => manage install / uninstall of config values (register hook)
* => install-sql classes => manage install / uninstall of sql queries
* => install-tab classes => manage install / uninstall of admin tab
* lib/admin
* => i-admin.php => interface
* => admin-ctrl_class.php => controller, manage factory with configure or update admin object
* => display => manage displaying of basic settings and search form in admin tab
* => update => manage updating of basic settings
* => export => manage exporting of report file in admin tab
* lib/module-dao_class.php
* D A O = Data Access Object => manage all sql queries
* lib/module-tools_class.php
* declare all transverse functions which are unclassifiable in specific class
* lib/warnings_class.php
* manage all displaying warnings when module isn't already configured after installation
* @date 10/04/2012
* @revision 01/10/2014
*/
if (!defined('_PS_VERSION_')) {
exit(1);
}
class OrderTaxProfitReport extends Module
{
/**
* @var array $aConfiguration : array of set configuration
*/
public static $aConfiguration = array();
/**
* @var int $iCurrentLang : store id of default lang
*/
public static $iCurrentLang = null;
/**
* @var int $sCurrentLang : store iso code of default lang
*/
public static $sCurrentLang = null;
/**
* @var obj $oModule : obj module itself
*/
public static $oModule = array();
/**
* @var string $sQueryMode : query mode - detect XHR
*/
public static $sQueryMode = null;
/**
* @var string $sBASE_URI : base of URI in prestashop
*/
public static $sBASE_URI = null;
/**
* @var array $aError : array get error
*/
public $aError = array('msg' => null, 'code' => null);
/**
* @var int $iShopId : shop id used for 1.5 and for multi shop
*/
public static $iShopId = 1;
/**
* @var obj $oCookie : store cookie obj
*/
public static $oCookie = null;
/**
* Magic Method __construct assigns few information about module and instantiate parent class
*/
public function __construct()
{
// hack for older version than 1 4 5 1
if (version_compare(_PS_VERSION_, '1.4.5.1') == -1) {
require_once(_PS_MODULE_DIR_ . 'ordertaxprofitreport/conf/common.conf.php');
}
else {
require_once(dirname(__FILE__) . '/conf/common.conf.php');
}
require_once(_OTPR_PATH_LIB . 'module-tools_class.php');
// use case - get context
if (version_compare(_PS_VERSION_, '1.5', '>')) {
$cookie = Context::getContext()->cookie;
self::$iShopId = Context::getContext()->shop->id;
}
else {
global $cookie;
}
// get cookie obj
self::$oCookie = $cookie;
// get default id lang
self::$iCurrentLang = self::$oCookie->id_lang;
// get default iso lang
self::$sCurrentLang = BT_OtprModuleTools::getIsoLang();
$this->name = 'ordertaxprofitreport';
$this->tab = 'billing_invoicing';
$this->version = '2.9.1';
$this->author = 'Business Tech';
$this->module_key = '9d25192b2d4a6901df631d9e8648891f';
parent::__construct();
$this->displayName = $this->l('Order, Tax and Profit Report');
$this->description = $this->l('Configure and export your "Order, Tax and Profit Report"');
$this->confirmUninstall = $this->l('Are you sure you want to remove it ? Your "Order, Tax and Profit Report" will no longer work.');
// stock itself obj
self::$oModule = $this;
// set base of URI
self::$sBASE_URI = $this->_path;
// get configuration options
BT_OtprModuleTools::getConfiguration();
// defines variable for way
// @uses with admin tab interface
$GLOBALS[_OTPR_MODULE_NAME . '_WAY'] = array(
'ASC' => $this->l('ascending'),
'DESC' => $this->l('descending'),
);
// get call mode - Ajax or dynamic - used for clean headers and footer in ajax request
self::$sQueryMode = Tools::getValue('sMode');
}
/**
* install() method installs all mandatory structure (DB or Files) => sql queries and update values and admin tab
*
* @return bool
*/
public function install()
{
require_once(_OTPR_PATH_CONF . 'install.conf.php');
require_once(_OTPR_PATH_LIB_INSTALL . 'install-ctrl_class.php');
// set return
$bReturn = true;
if (
!parent::install()
|| !BT_InstallCtrl::run('install', 'config')
|| !BT_InstallCtrl::run('install', 'sql', _OTPR_PATH_SQL . _OTPR_INSTALL_SQL_FILE)
|| !BT_InstallCtrl::run('install', 'tab')
) {
$bReturn = false;
}
// use case - add already defined report templates
else {
require_once(_OTPR_PATH_LIB . 'module-dao_class.php');
// register templates report
$bReturn = BT_OtprModuleTools::setReportTemplate();
}
return $bReturn;
}
/**
* uninstall() method uninstalls all mandatory structure (DB or Files)
*
* @return bool
*/
public function uninstall()
{
require_once(_OTPR_PATH_CONF . 'install.conf.php');
require_once(_OTPR_PATH_LIB_INSTALL . 'install-ctrl_class.php');
// set return
$bReturn = true;
if (!parent::uninstall()
|| !BT_InstallCtrl::run('uninstall', 'config')
|| !BT_InstallCtrl::run('uninstall', 'sql', _OTPR_PATH_SQL . _OTPR_UNINSTALL_SQL_FILE)
|| !BT_InstallCtrl::run('uninstall', 'tab')
) {
$bReturn = false;
}
return $bReturn;
}
/**
* getContent() method manages all data in Back Office (admin tab too)
*
* @return string
*/
public function getContent()
{
require_once(_OTPR_PATH_CONF . 'admin.conf.php');
require_once(_OTPR_PATH_LIB_ADMIN . 'admin-ctrl_class.php');
// set
$aUpdateModule = array();
try {
// get configuration options
BT_OtprModuleTools::getConfiguration();
// set js msg translation
BT_OtprModuleTools::setTranslatedJsMsg();
// instantiate admin controller object
$oAdmin = new BT_AdminCtrl();
// defines type to execute
// use case : no key sAction sent in POST mode (no form have been posted => first page is displayed with admin-display.class.php)
//use case : keys sAction & sType sent in POST mode (form or ajax query posted => use with update basic settings / export report
$sAction = (!Tools::getIsset('sAction') || (Tools::getIsset('sAction') && 'display' == Tools::getValue('sAction')))? 'display' : Tools::getValue('sAction');
// use case - module updating
// make module update only in case of display general admin page
if ($sAction == 'display' && (empty($_REQUEST['sType']) || $_REQUEST['sType'] == 'searchForm')) {
// update module if necessary
$aUpdateModule = $this->_updateModule();
}
else {
// use case - update module version
Configuration::updateValue(_OTPR_MODULE_NAME . '_MODULE_VERSION', $this->version);
}
// update new module keys
BT_OtprModuleTools::updateConfiguration();
// execute good action in admin
// only displayed with key : tpl and assign in order to display good smarty template
$aDisplay = $oAdmin->run($sAction, $_REQUEST);
// free memory
unset($oAdmin);
if (!empty($aDisplay)) {
$aParams = array(
'aUpdateErrors' => $aUpdateModule,
'oJsTranslatedMsg' => BT_OtprModuleTools::jsonEncode($GLOBALS[_OTPR_MODULE_NAME . '_JS_MSG']),
'iCompare' => version_compare(_PS_VERSION_, '1.4.4.1'),
'iCompare15' => version_compare(_PS_VERSION_, '1.5.0', '>'),
);
// get content
$sContent = $this->displayModule($aDisplay['tpl'], array_merge($aDisplay['assign'], $aParams));
if (!empty(self::$sQueryMode)) {
echo $sContent;
}
else {
return $sContent;
}
}
else {
throw new Exception('action returns empty content', 110);
}
}
catch (Exception $e) {
$this->aError['msg'] = $e->getMessage();
$this->aError['code'] = $e->getCode();
// get content
$sContent = $this->displayErrorModule();
if (!empty(self::$sQueryMode)) {
echo $sContent;
}
else {
return $sContent;
}
}
// exit clean in XHR mode
if( !empty(self::$sQueryMode)) {
exit(0);
}
}
/**
* hookNewOrder() method executes new order hook
*
* @param array $aParams
* @return string
*/
public function hookNewOrder(array $aParams)
{
return (
$this->_execHook('validateOrder', $aParams)
);
}
/**
* hookActionValidateOrder() method executes new order hook
*
* @param array $aParams
* @return string
*/
public function hookActionValidateOrder(array $aParams)
{
return (
$this->_execHook('validateOrder', $aParams)
);
}
/**
* _execHook() method displays selected hook content
*
* @param string $sHookType
* @param array $aParams
* @return string
*/
private function _execHook($sHookType, array $aParams = null)
{
// include
require_once(_OTPR_PATH_CONF . 'hook.conf.php');
require_once(_OTPR_PATH_LIB_HOOK . 'hook_class.php');
try {
// define which hook class is executed in order to display good content in good zone in shop
$oHook = new BT_OtprHook($sHookType);
// displays good block content
$aDisplay = $oHook->run($aParams);
// free memory
unset($oHook);
// execute good action in admin
// only displayed with key : tpl and assign in order to display good smarty template
if (!empty($aDisplay)) {
return (
$this->displayModule($aDisplay['tpl'], $aDisplay['assign'])
);
}
else {
throw new Exception('Choosen hook returns empty content', 110);
}
}
catch (Exception $e) {
$this->aErrors[] = array('msg' => $e->getMessage(), 'code' => $e->getCode());
return (
$this->displayErrorModule()
);
}
}
/**
* setErrorHandler() method manages module error
*
* @param string $sTplName
* @param array $aAssign
*/
public function setErrorHandler($iErrno, $sErrstr, $sErrFile, $iErrLine, $aErrContext)
{
switch ($iErrno) {
case E_USER_ERROR :
$this->aError['code'] = $iErrno;
$this->aError['msg'] = 'Fatal error <b>' . $sErrstr . '</b>';
break;
case E_USER_WARNING :
$this->aError['code'] = $iErrno;
$this->aError['msg'] = 'Warning <b>' . $sErrstr . '</b>';
break;
case E_USER_NOTICE :
$this->aError['code'] = $iErrno;
$this->aError['msg'] = 'Notice <b>' . $sErrstr . '</b>';
break;
default :
$this->aError['code'] = $iErrno;
$this->aError['msg'] = 'Unknow error <b>' . $sErrstr . '</b>';
break;
}
return (
$this->displayErrorModule()
);
}
/**
* displayModule() method displays view
*
* @param string $sTplName
* @param array $aAssign
* @return string html
*/
public function displayModule($sTplName, $aAssign)
{
if (file_exists(_OTPR_PATH_TPL . $sTplName) && is_file(_OTPR_PATH_TPL . $sTplName)) {
global $smarty;
// set assign module name
$aAssign = array_merge($aAssign, array('sModuleName' => Tools::strtolower(_OTPR_MODULE_NAME), 'bDebug' => _OTPR_DEBUG, 'sTs' => time()));
$smarty->assign($aAssign);
return (
$this->display(__FILE__, _OTPR_PATH_TPL_NAME . $sTplName)
);
}
else {
throw new Exception('Template doesn\'t exists', 120);
}
}
/**
* displayErrorModule() method displays view with error
*
* @return string html
*/
public function displayErrorModule()
{
global $smarty;
$smarty->assign(
array(
'sHomeURI' => BT_OtprModuleTools::truncateUri(),
'aErrors' => array($this->aError),
'sErrorTplPath' => _OTPR_PATH_TPL . _OTPR_TPL_ERROR,
'sModuleName' => Tools::strtolower(_OTPR_MODULE_NAME),
'bDebug' => _OTPR_DEBUG,
)
);
return (
$this->display(__FILE__, _OTPR_PATH_TPL_NAME . _OTPR_TPL_ERROR)
);
}
/**
* _updateModule() method updates module as necessary
*
* @return array
*/
private function _updateModule()
{
$aErrors = array();
// include
require_once(_OTPR_PATH_LIB . 'module-dao_class.php');
// set transaction
Db::getInstance()->Execute('BEGIN');
// get templates
$aTemplates = BT_OtprModuleDao::getTemplates();
foreach ($GLOBALS[_OTPR_MODULE_NAME . '_SEARCH_TEMPLATE'] as $sReportName => $aTemplate) {
if (!array_key_exists($sReportName, $aTemplates)) {
if (!BT_OtprModuleDao::addTemplate($sReportName, array('cols' => $aTemplate['cols'], 'group' => $aTemplate['group']), $aTemplate['lang'])) {
$aErrors[] = array('sql' => array('table' => 'ps_configuration', 'file' => $this->l('update template') . ' => "' . $sReportName . '" ' . $this->l('ko')));
}
}
else {
if (!BT_OtprModuleDao::addTemplate($sReportName, array('cols' => $aTemplate['cols'], 'group' => $aTemplate['group']), $aTemplate['lang'], $aTemplates[$sReportName]['id'])) {
$aErrors[] = array('sql' => array('table' => 'ps_configuration', 'file' => $this->l('update template') . ' => "' . $sReportName . '" ' . $this->l('ko')));
}
}
}
// use case - table to add
if (!empty($GLOBALS[_OTPR_MODULE_NAME . '_SQL_UPDATE']['table'])) {
// loop on each elt to update SQL
foreach ($GLOBALS[_OTPR_MODULE_NAME . '_SQL_UPDATE']['table'] as $sTable => $sSqlFile) {
// execute query
$bResult = Db::getInstance()->ExecuteS('SHOW TABLES LIKE "' . _DB_PREFIX_ . Tools::strtolower(_OTPR_MODULE_NAME) . '_'. $sTable .'"');
// if empty - update
if (empty($bResult)) {
require_once(_OTPR_PATH_CONF . 'install.conf.php');
require_once(_OTPR_PATH_LIB_INSTALL . 'install-ctrl_class.php');
// use case - KO update
if (!BT_InstallCtrl::run('install', 'sql', _OTPR_PATH_SQL . $sSqlFile)) {
$aErrors[] = array('sql' => array('table' => $sTable, 'file' => $sSqlFile));
}
}
}
}
// use case - field to add
if (!empty($GLOBALS[_OTPR_MODULE_NAME . '_SQL_UPDATE']['field']) && empty($aErrors)) {
// loop on each elt to update SQL
foreach ($GLOBALS[_OTPR_MODULE_NAME . '_SQL_UPDATE']['field'] as $sFieldName => $aOption) {
// execute query
$bResult = Db::getInstance()->ExecuteS('SHOW COLUMNS FROM ' . _DB_PREFIX_ . Tools::strtolower(_OTPR_MODULE_NAME) . '_'. $aOption['table'] . ' LIKE "' . $sFieldName .'"');
// if empty - update
if (empty($bResult)) {
require_once(_OTPR_PATH_CONF . 'install.conf.php');
require_once(_OTPR_PATH_LIB_INSTALL . 'install-ctrl_class.php');
// use case - KO update
if (!BT_InstallCtrl::run('install', 'sql', _OTPR_PATH_SQL . $aOption['file'])) {
$aErrors[] = array('sql' => array('field' => $sFieldName, 'linked' => $aOption['table'], 'file' => $aOption['file']));
}
}
}
}
// use case - add hook
if (empty($aErrors)) {
require_once(_OTPR_PATH_CONF . 'install.conf.php');
require_once(_OTPR_PATH_LIB_INSTALL . 'install-ctrl_class.php');
// use case - register ko
if (!BT_InstallCtrl::run('install', 'config', array('bHookOnly' => true))) {
$aErrors[] = array('sql' => array('table' => 'ps_hook_module', 'file' => $this->l('register hooks KO')));
}
// execute query
$bResult = Db::getInstance()->ExecuteS('SHOW COLUMNS FROM ' . _DB_PREFIX_ . Tools::strtolower(_OTPR_MODULE_NAME) . '_order_wrapping LIKE "WRA_ID_ORDER_DETAIL"');
// check module version for uninstall and install SQL
if (!empty($bResult)) {
// use case - uninstall
if (!BT_InstallCtrl::run('uninstall', 'sql', _OTPR_PATH_SQL . _OTPR_UNINSTALL_SQL_FILE)) {
$aErrors[] = array('sql' => array('table' => $this->l('all'), 'file' => _OTPR_PATH_SQL . OTPR_UNINSTALL_SQL_FILE));
}
// use case - install
if (!BT_InstallCtrl::run('install', 'sql', _OTPR_PATH_SQL . _OTPR_INSTALL_SQL_FILE)) {
$aErrors[] = array('sql' => array('table' => $this->l('all'), 'file' => _OTPR_PATH_SQL . _OTPR_INSTALL_SQL_FILE));
}
}
// use case - compile templates files
require_once(_OTPR_PATH_LIB_COMMON . 'dir-reader.class.php');
// get templates files
$aTplFiles = BT_DirReader::create()->run(array('path' => _OTPR_PATH_TPL, 'recursive' => true, 'extension' => 'tpl', 'subpath' => true));
if (!empty($aTplFiles)) {
global $smarty;
foreach ($aTplFiles as $aFile) {
if (method_exists($smarty, 'clearCompiledTemplate')) {
$smarty->clearCompiledTemplate($aFile['filename']);
}
elseif (method_exists($smarty, 'clear_compiled_tpl')) {
$smarty->clear_compiled_tpl($aFile['filename']);
}
}
}
}
if (empty($aErrors)) {
Db::getInstance()->Execute('COMMIT');
Configuration::updateValue(_OTPR_MODULE_NAME . '_MODULE_VERSION', $this->version);
}
else {
Db::getInstance()->Execute('ROLLBACK');
}
return $aErrors;
}
}