ToutPratique 1d1a2def5f push prod
2016-05-17 15:54:37 +02:00

283 lines
10 KiB
PHP

<?php
/**
* admin-update_class.php file defines method update content for basic settings
* @author Business Tech (www.businesstech.fr) - Contact: http://www.businesstech.fr/en/contact-us
* @category admin collection
* @license Business Tech
* @uses Please read included installation and configuration instructions (PDF format)
* @see IInstall Interface
*/
class BT_AdminUpdate implements BT_IAdmin
{
/**
* Magic Method __construct
*/
public function __construct()
{
}
/**
* Magic Method __destruct
*/
public function __destruct()
{
}
/**
* run() method update all tabs content of admin page
*
* @param array $aParam
* @return array
*/
public function run($aParam = null)
{
// set variables
$aDisplayData = array();
switch ($aParam['sType']) {
case 'settings' : /* use case - update basic settings */
case 'ordersDetail' : /* use case - update order details */
// execute match function
$aDisplayData = call_user_func_array(array($this, '_update' . ucfirst($aParam['sType'])), array($aParam));
break;
default :
break;
}
return $aDisplayData;
}
/**
* _updateSettings() method update basic settings
*
* @param array $aPost
* @return array
*/
private function _updateSettings(array $aPost)
{
// set
$aUpdateData = array();
try {
// use case - update use Headings and total
if (Tools::getIsset(strtolower(_OTPR_MODULE_NAME) . 'Headings')) {
if ('on' == $aPost[strtolower(_OTPR_MODULE_NAME) . 'Headings']) {
$bUse = true;
}
}
else {
$bUse = false;
}
// set use Headings & total
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_HEADINGS_TOTAL', $bUse)) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "Headings & total" update', 'admin-update_class'), 140);
}
// set date format
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_DATE_FORMAT', $aPost[strtolower(_OTPR_MODULE_NAME) . 'Date'])) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "Date format" update', 'admin-update_class'), 141);
}
// set number after decimal point
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_FLOAT_NUMBER', $aPost[strtolower(_OTPR_MODULE_NAME) . 'FloatNumber'])) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "number after decimal point" update', 'admin-update_class'), 142);
}
// set float number
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_FLOAT_SEPARATOR', $aPost[strtolower(_OTPR_MODULE_NAME) . 'FloatSeparator'])) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "float separator" update', 'admin-update_class'), 143);
}
// set float bool
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_FIX_CELL_SIZE', $aPost[strtolower(_OTPR_MODULE_NAME) . 'FixCellSize'])) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "Fix cell size update" update', 'admin-update_class'), 144);
}
// set float integer
if (!Configuration::updateValue(_OTPR_MODULE_NAME . '_CELL_SIZE', $aPost['cellSizeValue'])) {
throw new Exception(OrderTaxProfitReport::$oModule->l('An error occured during the "Fix cell size update" update', 'admin-update_class'), 145);
}
}
catch (Exception $e) {
$aUpdateData['aError'] = array('msg' => $e->getMessage(), 'code' => $e->getCode());
$aUpdateData['sErrorTplPath'] = _OTPR_PATH_TPL . _OTPR_TPL_ERROR;
}
// destruct
unset($bUse);
// get configuration options
BT_OtprModuleTools::getConfiguration();
// require admin configure classe - to factorise
require_once(_OTPR_PATH_LIB_ADMIN . 'admin-display_class.php');
// get run of admin display in order to display first page of admin with basic settings updated
$aData = BT_AdminDisplay::create()->run(array('sType' => 'settings'));
// use case - empty error
$aData['assign'] = array_merge($aData['assign'], array(
'bXHR' => true,
'bUpdate' => (empty($aUpdateData['aError']) ? true : false),
'sConfirmTplPath' => _OTPR_PATH_TPL . _OTPR_TPL_ADMIN_PATH ._OTPR_TPL_CONFIRM,
), $aUpdateData);
return $aData;
}
/**
* _updateOrdersDetail() method register orders detail in table for discount and wrapping
*
* @param array $aPost
* @return array
*/
private function _updateOrdersDetail(array $aPost)
{
// flush header
@ob_end_clean();
// set
$aUpdateData = array();
// include
require_once(_OTPR_PATH_LIB . 'module-dao_class.php');
try {
$bReturn = true;
$aOrdersDetail = array();
// get order detail
$rOrdersDetail = BT_OtprModuleDao::getOrdersDetail();
// get number of records
$iNumRows = Db::getInstance()->numRows();
// use case - no orders
if ($iNumRows > 0 && $rOrdersDetail !== false) {
// set counter
$iCounter = 0;
// set previous record
$aPreviousRecord = array();
// set transaction
Db::getInstance()->Execute('BEGIN');
// get tables
$aDiscount = $GLOBALS[_OTPR_MODULE_NAME . '_SQL_TABLES']['discount'];
$aWrapping = $GLOBALS[_OTPR_MODULE_NAME . '_SQL_TABLES']['wrapping'];
$aWholePrice = $GLOBALS[_OTPR_MODULE_NAME . '_SQL_TABLES']['wholesale_price'];
// loop on each orders details
while ($aDetail = Db::getInstance()->nextRow($rOrdersDetail)) {
// populate orders detail discount
if (!BT_OtprModuleTools::populateDiscountOrdersDetail($aDetail['id_order'], $aDetail['id_order_detail'], $aDetail['total_discounts'], $aDetail['total_products'], $aDetail['prod_unit_price'], $aDetail['tax_rate'], $aDetail['product_quantity'])) {
// set error msg
$sMsg = OrderTaxProfitReport::$oModule->l('An error occurred during inserting order detail for', 'admin-update_class') . ' ' . $aDiscount['name']
. ' => ' . OrderTaxProfitReport::$oModule->l('Id order', 'admin-update_class') . ' : ' . $aDetail['id_order']
. ' - ' . OrderTaxProfitReport::$oModule->l('Id order detail', 'admin-update_class') . ' : ' . $aDetail['id_order_detail']
;
throw new Exception($sMsg, 150);
}
// populate orders wrapping
if ($iCounter == 0
||
(!empty($aPreviousRecord) && $aPreviousRecord['id_order'] != $aDetail['id_order'])
) {
$fWrappingTaxIncl = isset($aDetail['total_wrapping_tax_incl'])? $aDetail['total_wrapping_tax_incl'] : null;
$fWrappingTaxExcl = isset($aDetail['total_wrapping_tax_excl'])? $aDetail['total_wrapping_tax_excl'] : null;
if (!BT_OtprModuleTools::populateWrappingOrdersDetail($aDetail['id_order'], $aDetail['total_wrapping'], $fWrappingTaxExcl, $fWrappingTaxIncl)) {
// set error msg
$sMsg = OrderTaxProfitReport::$oModule->l('An error occurred during inserting order detail for', 'admin-update_class') . ' ' . $aWrapping['name']
. ' => ' . OrderTaxProfitReport::$oModule->l('Id order', 'admin-update_class') . ' : ' . $aDetail['id_order']
. ' - ' . OrderTaxProfitReport::$oModule->l('Id order detail', 'admin-update_class') . ' : ' . $aDetail['id_order_detail']
;
throw new Exception($sMsg, 151);
}
}
// manage the best whole_saleprice to use
if (!empty($aDetail['purchase_supplier_price'])
&& $aDetail['purchase_supplier_price'] != 0.0
) {
$fWholeSalePrice = (float)$aDetail['purchase_supplier_price'];
}
elseif (!empty($aDetail['attribute_wholesale_price'])
&& $aDetail['attribute_wholesale_price'] != 0.0
) {
$fWholeSalePrice = (float)$aDetail['attribute_wholesale_price'];
}
elseif (!empty($aDetail['product_wholesale_price'])
&& $aDetail['product_wholesale_price'] != 0.0
) {
$fWholeSalePrice = (float)$aDetail['product_wholesale_price'];
}
else {
$fWholeSalePrice = 0.00;
}
//populate wholesaleprice
if (!BT_OtprModuleDao::populateWholeSalePrice($aDetail['id_order'], $fWholeSalePrice, $aDetail['id_order_detail'])) {
$sMsg = OrderTaxProfitReport::$oModule->l('An error occurred during inserting wholesale price for', 'admin-update_class') . ' ' . $aWholePrice['name']
. ' => ' . OrderTaxProfitReport::$oModule->l('Id order', 'admin-update_class') . ' : ' . $aDetail['id_order']
. ' - ' . OrderTaxProfitReport::$oModule->l('Id order detail', 'admin-update_class') . ' : ' . $aDetail['id_order_detail'];
throw new Exception($sMsg, 152);
}
// register current record
$aPreviousRecord = $aDetail;
// increase counter
++$iCounter;
}
// commit register
Db::getInstance()->Execute('COMMIT');
}
else {
$sMsg = OrderTaxProfitReport::$oModule->l('There is no Orders for this shop', 'admin-update_class') ;
throw new Exception($sMsg, 153);
}
}
catch (Exception $e) {
Db::getInstance()->Execute('ROLLBACK');
$aUpdateData['aErrors'][] = array('msg' => $e->getMessage(), 'code' => $e->getCode());
$aUpdateData['sErrorTplPath'] = _OTPR_PATH_TPL . _OTPR_TPL_ERROR;
}
// get configuration options
BT_OtprModuleTools::getConfiguration();
// require admin configure class - to factorise
require_once(_OTPR_PATH_LIB_ADMIN . 'admin-display_class.php');
// get run of admin display in order to display first page of admin with basic settings updated
$aData = BT_AdminDisplay::create()->run(array('sType' => (strtolower(Tools::getValue('tab')) == 'adminordertaxprofitreport' || strtolower(Tools::getValue('controller')) == 'adminordertaxprofitreport'? 'searchForm' : 'settings')));
// use case - empty error
$aData['assign'] = array_merge($aData['assign'], array(
'bUpdate' => (empty($aUpdateData['aErrors']) ? true : false),
'sConfirmTplPath' => _OTPR_PATH_TPL . _OTPR_TPL_ADMIN_PATH ._OTPR_TPL_CONFIRM,
'bAdminTab' => (Tools::getValue('tab') == 'AdminOrderTaxProfitReport' ? true : false),
), $aUpdateData);
// use case - empty error
OrderTaxProfitReport::$sQueryMode = 'xhr';
return $aData;
}
/**
* create() method manages singleton
*
* @return obj
*/
public static function create()
{
static $oUpdate;
if (null === $oUpdate) {
$oUpdate = new BT_AdminUpdate();
}
return $oUpdate;
}
}