Skip to content

Commit

Permalink
includes/classes/shipping.php modernisiert
Browse files Browse the repository at this point in the history
  • Loading branch information
webchills committed Jan 11, 2023
1 parent 36d907e commit 9e3cffa
Showing 1 changed file with 140 additions and 97 deletions.
237 changes: 140 additions & 97 deletions UPLOAD/includes/classes/shipping.php
@@ -1,12 +1,12 @@
<?php
/**
* shipping class
*
* Zen Cart German Specific (158 code in 157)
* @copyright Copyright 2003-2022 Zen Cart Development Team
* Zen Cart German Version - www.zen-cart-pro.at
* @copyright Portions Copyright 2003 osCommerce
* @license https://www.zen-cart-pro.at/license/3_0.txt GNU General Public License V3.0
* @version $Id: shipping.php 2021-11-28 20:12:16Z webchills $
* @version $Id: shipping.php 2023-01-11 16:25:16Z webchills $
*/
if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
Expand All @@ -16,31 +16,55 @@
* Class used for interfacing with shipping modules
*
*/
class shipping extends base {
var $modules;
class shipping extends base
{
/**
* $enabled allows notifier to turn off shipping method
* @var boolean
*/
public $enabled;
/**
* $modules is an array of installed shipping module names can be altered by notifier
* @var array
*/
public $modules;
/**
* $abort_legacy_calculations allows a notifier to enable the calculate_boxes_weight_and_tare method
* @var boolean
*/
public $abort_legacy_calculations;

function __construct($module = null) {
global $PHP_SELF, $messageStack;
public function __construct($module = null)
{
global $PHP_SELF, $messageStack;

if (defined('MODULE_SHIPPING_INSTALLED') && !empty(MODULE_SHIPPING_INSTALLED)) {
$this->modules = explode(';', MODULE_SHIPPING_INSTALLED);
}
$this->notify('NOTIFY_SHIPPING_CLASS_GET_INSTALLED_MODULES', $module);

if (empty($this->modules)) return;
if (empty($this->modules)) {
return;
}

$include_modules = array();
$include_modules = [];

if ( (zen_not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules)) ) {
$include_modules[] = array('class' => substr($module['id'], 0, strpos($module['id'], '_')), 'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)));
} else {
foreach($this->modules as $value) {
$class = substr($value, 0, strrpos($value, '.'));
$include_modules[] = array('class' => $class, 'file' => $value);
if (!empty($module) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules))) {
$include_modules[] = [
'class' => substr($module['id'], 0, strpos($module['id'], '_')),
'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1))
];
} else {
foreach($this->modules as $value) {
$class = substr($value, 0, strrpos($value, '.'));
$include_modules[] = [
'class' => $class,
'file' => $value
];
}
}
}

for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
for ($i = 0, $n = count($include_modules); $i < $n; $i++) {
$lang_file = null;
$module_file = DIR_WS_MODULES . 'shipping/' . $include_modules[$i]['file'];
if (IS_ADMIN_FLAG === true) {
Expand All @@ -63,39 +87,41 @@ function __construct($module = null) {
}
$this->enabled = TRUE;
$this->notify('NOTIFY_SHIPPING_MODULE_ENABLE', $include_modules[$i]['class'], $include_modules[$i]['class']);
if ($this->enabled)
{
include_once($module_file);
$GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
if ($this->enabled) {
include_once $module_file;
$GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];

$enabled = $this->check_enabled($GLOBALS[$include_modules[$i]['class']]);
if ($enabled == FALSE ) unset($GLOBALS[$include_modules[$i]['class']]);
$enabled = $this->check_enabled($GLOBALS[$include_modules[$i]['class']]);
if ($enabled == false ) {
unset($GLOBALS[$include_modules[$i]['class']]);
}
}
}

function check_enabled($class)
{
$enabled = $class->enabled;
if (method_exists($class, 'check_enabled_for_zone') && $class->enabled)
{
$enabled = $class->check_enabled_for_zone();
}
$this->notify('NOTIFY_SHIPPING_CHECK_ENABLED_FOR_ZONE', array(), $class, $enabled);
if (method_exists($class, 'check_enabled') && $enabled)

public function check_enabled($class)
{
$enabled = $class->check_enabled();
$enabled = $class->enabled;
if (method_exists($class, 'check_enabled_for_zone') && $class->enabled) {
$enabled = $class->check_enabled_for_zone();
}
$this->notify('NOTIFY_SHIPPING_CHECK_ENABLED_FOR_ZONE', [], $class, $enabled);
if (method_exists($class, 'check_enabled') && $enabled) {
$enabled = $class->check_enabled();
}
$this->notify('NOTIFY_SHIPPING_CHECK_ENABLED', [], $class, $enabled);
return $enabled;
}
$this->notify('NOTIFY_SHIPPING_CHECK_ENABLED', array(), $class, $enabled);
return $enabled;
}

function calculate_boxes_weight_and_tare() {
global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;
public function calculate_boxes_weight_and_tare()
{
global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes;

$this->abort_legacy_calculations = false;
$this->notify('NOTIFY_SHIPPING_MODULE_PRE_CALCULATE_BOXES_AND_TARE', array(), $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes);
if ($this->abort_legacy_calculations) return;
$this->abort_legacy_calculations = false;
$this->notify('NOTIFY_SHIPPING_MODULE_PRE_CALCULATE_BOXES_AND_TARE', [], $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes);
if ($this->abort_legacy_calculations) {
return;
}

if (is_array($this->modules)) {
$shipping_quoted = '';
Expand All @@ -114,13 +140,6 @@ function calculate_boxes_weight_and_tare() {
// SHIPPING_BOX_PADDING = Large Box % increase
// SHIPPING_MAX_WEIGHT = Largest package

/*
if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100) {
$shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT;
} else {
$shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100);
}
*/

switch (true) {
// large box add padding
Expand All @@ -142,84 +161,108 @@ function calculate_boxes_weight_and_tare() {
$shipping_weight = $shipping_weight/$shipping_num_boxes;
}
}
$this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_BOXES_AND_TARE', array(), $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes);
$this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_BOXES_AND_TARE', [], $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes);
}

function quote($method = '', $module = '', $calc_boxes_weight_tare = true, $insurance_exclusions = array()) {
global $shipping_weight, $uninsurable_value;
$quotes_array = array();
public function quote($method = '', $module = '', $calc_boxes_weight_tare = true, $insurance_exclusions = [])
{
global $shipping_weight, $uninsurable_value;
$quotes_array = [];

if ($calc_boxes_weight_tare) {
$this->calculate_boxes_weight_and_tare();
}

if ($calc_boxes_weight_tare) $this->calculate_boxes_weight_and_tare();
// calculate amount not to be insured on shipping
$uninsurable_value = (method_exists($this, 'get_uninsurable_value')) ? $this->get_uninsurable_value($insurance_exclusions) : 0;
// calculate amount not to be insured on shipping
$uninsurable_value = (method_exists($this, 'get_uninsurable_value')) ? $this->get_uninsurable_value($insurance_exclusions) : 0;

if (is_array($this->modules)) {
$include_quotes = array();
if (is_array($this->modules)) {
$include_quotes = [];

foreach($this->modules as $value) {
$class = substr($value, 0, strrpos($value, '.'));
if (zen_not_null($module)) {
if ( ($module == $class) && (isset($GLOBALS[$class]) && $GLOBALS[$class]->enabled) ) {
if (!empty($module)) {
if ($module == $class && isset($GLOBALS[$class]) && $GLOBALS[$class]->enabled) {
$include_quotes[] = $class;
}
} elseif (isset($GLOBALS[$class]) && $GLOBALS[$class]->enabled) {
$include_quotes[] = $class;
}
}

$size = sizeof($include_quotes);
for ($i=0; $i<$size; $i++) {
if (method_exists($GLOBALS[$include_quotes[$i]], 'update_status')) $GLOBALS[$include_quotes[$i]]->update_status();
if (FALSE == $GLOBALS[$include_quotes[$i]]->enabled) continue;
$save_shipping_weight = $shipping_weight;
$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
if (!isset($quotes['tax'])) $quotes['tax'] = 0;
$shipping_weight = $save_shipping_weight;
if (is_array($quotes)) $quotes_array[] = $quotes;
}
}
$size = count($include_quotes);
for ($i = 0; $i < $size; $i++) {
if (method_exists($GLOBALS[$include_quotes[$i]], 'update_status')) {
$GLOBALS[$include_quotes[$i]]->update_status();
}
if (false === $GLOBALS[$include_quotes[$i]]->enabled) {
continue;
}
$save_shipping_weight = $shipping_weight;
$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
if (!isset($quotes['tax']) && !empty($quotes)) {
$quotes['tax'] = 0;
}
$shipping_weight = $save_shipping_weight;
if (is_array($quotes)) {
$quotes_array[] = $quotes;
}
}
}
$this->notify('NOTIFY_SHIPPING_MODULE_GET_ALL_QUOTES', $quotes_array, $quotes_array);
return $quotes_array;
}

function cheapest() {
if (!is_array($this->modules)) return false;
$rates = array();
public function cheapest()
{
if (!is_array($this->modules)) {
return false;
}
$rates = [];

foreach($this->modules as $value) {
$class = substr($value, 0, strrpos($value, '.'));
if (isset($GLOBALS[$class]) && is_object($GLOBALS[$class]) && $GLOBALS[$class]->enabled) {
$quotes = isset($GLOBALS[$class]->quotes) ? $GLOBALS[$class]->quotes : null;
if (empty($quotes['methods'])) {
continue;
}
$size = sizeof($quotes['methods']);
for ($i=0; $i<$size; $i++) {
if (isset($quotes['methods'][$i]['cost'])){
$rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
'cost' => $quotes['methods'][$i]['cost'],
'module' => $quotes['id']
);
foreach($this->modules as $value) {
$class = substr($value, 0, strrpos($value, '.'));
if (isset($GLOBALS[$class]) && is_object($GLOBALS[$class]) && $GLOBALS[$class]->enabled) {
$quotes = isset($GLOBALS[$class]->quotes) ? $GLOBALS[$class]->quotes : null;
if (empty($quotes['methods']) || isset($quotes['error'])) {
continue;
}
$size = count($quotes['methods']);
for ($i = 0; $i < $size; $i++) {
if (isset($quotes['methods'][$i]['cost'])) {
$rates[] = [
'id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'],
'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')',
'cost' => $quotes['methods'][$i]['cost'],
'module' => $quotes['id']
];
}
}
}
}

$cheapest = false;
$size = sizeof($rates);
for ($i=0; $i<$size; $i++) {
if (is_array($cheapest)) {
// never quote storepickup as lowest - needs to be configured in shipping module
if ($rates[$i]['cost'] < $cheapest['cost'] and $rates[$i]['module'] != 'storepickup') {
$cheapest = $rates[$i];
}
} else {
if ($rates[$i]['module'] != 'storepickup') {
$size = count($rates);
for ($i = 0; $i < $size; $i++) {
if ($cheapest !== false) {
// never quote storepickup as lowest - needs to be configured in shipping module
if ($rates[$i]['cost'] < $cheapest['cost'] && $rates[$i]['module'] !== 'storepickup') {
// -----
// Give a customized shipping module the opportunity to exclude itself from being quoted
// as the cheapest. The observer must set the $exclude_from_cheapest to specifically
// (bool)true to be excluded.
//
$exclude_from_cheapest = false;
$this->notify('NOTIFY_SHIPPING_EXCLUDE_FROM_CHEAPEST', $rates[$i]['module'], $exclude_from_cheapest);
if ($exclude_from_cheapest === true) {
continue;
}
$cheapest = $rates[$i];
}
} elseif ($size === 1 || $rates[$i]['module'] !== 'storepickup') {
$cheapest = $rates[$i];
}
}
}
$this->notify('NOTIFY_SHIPPING_MODULE_CALCULATE_CHEAPEST', $cheapest, $cheapest, $rates);
return $cheapest;
}
Expand Down

0 comments on commit 9e3cffa

Please sign in to comment.