Page MenuHomeIn-Portal Phabricator

in-commerce
No OneTemporary

File Metadata

Created
Sun, Jan 5, 4:00 PM

in-commerce

This file is larger than 256 KB, so syntax highlighting was skipped.
Index: branches/5.0.x/in-commerce/units/gateways/gw_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/gateways/gw_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/gateways/gw_event_handler.php (revision 12722)
@@ -1,77 +1,81 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class GatewayEventHandler extends kDBEventHandler {
function OnSaveValues(&$event)
{
// if there is no id - it means we need to create an item
$item_id = $this->getPassedID($event);
$event->setPseudoClass('_List');
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$PaymentType_info = $this->Application->GetVar( 'pt' );
$GWConfigValue =& $this->Application->recallObject('gwfv');
$GWConfigValue->SetDBField('PaymentTypeId', $this->Application->GetVar('pt_id'));
//deleting old values
$sql = 'DELETE FROM '.$GWConfigValue->TableName.' WHERE PaymentTypeId = '.$this->Application->GetVar('pt_id');
$res = $this->Conn->Query($sql);
//selecting fields for selected gateway only
$sql = 'SELECT GWConfigFieldId FROM '.TABLE_PREFIX.'GatewayConfigFields
WHERE GatewayId = '.$PaymentType_info[$this->Application->GetVar('pt_id')]['GatewayId'];
$res = $this->Conn->GetCol($sql);
if($items_info)
{
foreach($res as $gw_field_id)
{
$field_values = $items_info[$gw_field_id];
$field_values['GWConfigFieldId'] = $gw_field_id;
$GWConfigValue->SetFieldsFromHash($field_values);
if( $GWConfigValue->Create() )
{
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
break;
}
}
}
}
function OnCheckGateways(&$event)
{
- if (!$this->Application->IsAdmin()) return ;
- $fh=opendir(GW_CLASS_PATH);
+ if (!$this->Application->isAdminUser) {
+ return ;
+ }
+
+ $fh = opendir(GW_CLASS_PATH);
+
while( $entry=readdir($fh) )
{
if ( !is_file(GW_CLASS_PATH.'/'.$entry) ) continue;
$pathinfo = pathinfo($entry);
if ( $pathinfo['extension'] != 'php') continue;
$class_name = false;
include_once(GW_CLASS_PATH.'/'.$entry);
if (!$class_name) continue;
$tmp = new $class_name();
$tmp->Install();
}
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/gateways/gw_classes/authorizenet.php
===================================================================
--- branches/5.0.x/in-commerce/units/gateways/gw_classes/authorizenet.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/gateways/gw_classes/authorizenet.php (revision 12722)
@@ -1,168 +1,168 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
require_once GW_CLASS_PATH.'/gw_base.php';
class kGWAuthorizeNet extends kGWBase
{
function DirectPayment($item_data, $gw_params)
{
$post_fields = Array();
// -- Login Information --
$post_fields['x_version'] = '3.1';
$post_fields['x_delim_data'] = 'True';
$post_fields['x_encap_char'] = $gw_params['encapsulate_char'];
$post_fields['x_relay_response'] = 'False';
$post_fields['x_type'] = $gw_params['shipping_control'] == SHIPPING_CONTROL_PREAUTH ? 'AUTH_ONLY' : 'AUTH_CAPTURE';
$post_fields['x_login'] = $gw_params['user_account'];
$post_fields['x_tran_key'] = $gw_params['transaction_key'];
if( $this->IsTestMode() ) $post_fields['x_test_request'] = 'True';
// -- Payment Details --
$names = explode(' ', $item_data['PaymentNameOnCard'], 2);
$post_fields['x_first_name'] = getArrayValue($names, 0);
$post_fields['x_last_name'] = getArrayValue($names, 1);
$post_fields['x_amount'] = sprintf('%.2f', $item_data['TotalAmount']);
$post_fields['x_company'] = $item_data['BillingCompany'];
$post_fields['x_card_num'] = $item_data['PaymentAccount'];
$post_fields['x_card_code'] = $item_data['PaymentCVV2'];
$post_fields['x_exp_date'] = $item_data['PaymentCCExpDate'];
$post_fields['x_address'] = $item_data['BillingAddress1'].' '.$item_data['BillingAddress2'];
$post_fields['x_city'] = $item_data['BillingCity'];
$post_fields['x_state'] = $item_data['BillingState'];
$post_fields['x_zip'] = $item_data['BillingZip'];
$recurring = getArrayValue($item_data, 'IsRecurringBilling') ? 'YES' : 'NO';
$post_fields['x_recurring_billing'] = $recurring;
$billing_email = $item_data['BillingEmail'];
if (!$billing_email) {
$billing_email = $this->Conn->GetOne(' SELECT Email FROM '.$this->Application->getUnitOption('u', 'TableName').'
WHERE PortalUserId = '.$this->Application->RecallVar('user_id'));
}
$post_fields['x_email'] = $billing_email;
$post_fields['x_phone'] = $item_data['BillingPhone'];
$sql = 'SELECT DestAbbr2 FROM '.TABLE_PREFIX.'StdDestinations WHERE DestAbbr = %s';
$post_fields['x_country'] = $this->Conn->GetOne( sprintf($sql, $this->Conn->qstr($item_data['BillingCountry']) ) );
$post_fields['x_cust_id'] = $item_data['PortalUserId'];
$post_fields['x_invoice_num'] = $item_data['OrderNumber'];
$post_fields['x_description'] = 'Invoice #'.$item_data['OrderNumber'];
$post_fields['x_email_customer'] = 'FALSE';
$this->gw_responce = curl_post($gw_params['submit_url'], $post_fields);
$gw_responce = $this->parseGWResponce(null, $gw_params);
// gw_error_msg: $gw_response['responce_reason_text']
// gw_error_code: $gw_response['responce_reason_code']
return (is_numeric($gw_responce['responce_code']) && $gw_responce['responce_code'] == 1) ? true : false;
}
/**
* Perform SALE type transaction direct from php script wihtout redirecting to 3rd-party website
*
* @param Array $item_data
* @param Array $gw_params
* @return bool
*/
function Charge($item_data, $gw_params)
{
$gw_responce = unserialize( $item_data['GWResult1'] );
- if( ($item_data['PortalUserId'] != $gw_responce['customer_id']) && ($gw_repsponce['customer_id'] != -2 && !$this->Application->IsAdmin())) return false;
+ if( ($item_data['PortalUserId'] != $gw_responce['customer_id']) && ($gw_repsponce['customer_id'] != -2 && !$this->Application->isAdmin)) return false;
if( ( strtolower($gw_responce['transaction_type']) == 'auth_only') )
{
$post_fields = Array();
// -- Login Information --
$post_fields['x_version'] = '3.1';
$post_fields['x_delim_data'] = 'True';
$post_fields['x_encap_char'] = $gw_params['encapsulate_char'];
$post_fields['x_relay_response'] = 'False';
$post_fields['x_type'] = 'PRIOR_AUTH_CAPTURE'; // $gw_params['shipping_control'] == SHIPPING_CONTROL_PREAUTH ? 'PRIOR_AUTH_CAPTURE' : 'AUTH_CAPTURE'; // AUTH_CAPTURE not fully impletemnted/needed here
$post_fields['x_login'] = $gw_params['user_account'];
$post_fields['x_tran_key'] = $gw_params['transaction_key'];
$post_fields['x_trans_id'] = $gw_responce['transaction_id'];
if( $this->IsTestMode() ) $post_fields['x_test_request'] = 'True';
$this->gw_responce = curl_post($gw_params['submit_url'], $post_fields);
$gw_responce = $this->parseGWResponce(null, $gw_params);
// gw_error_msg: $gw_response['responce_reason_text']
// gw_error_code: $gw_response['responce_reason_code']
return (is_numeric($gw_responce['responce_code']) && $gw_responce['responce_code'] == 1) || $this->IsTestMode() ? true : false;
}
else
{
return true;
}
}
/**
* Parse previosly saved gw responce into associative array
*
* @param string $gw_responce
* @param Array $gw_params
* @return Array
*/
function parseGWResponce($gw_responce = null, $gw_params)
{
if( !isset($gw_responce) ) $gw_responce = $this->gw_responce;
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Curl Error #'.$GLOBALS['curl_errorno'].'; Error Message: '.$GLOBALS['curl_error']);
$this->Application->Debugger->appendHTML('Authorize.Net Responce:');
$this->Application->Debugger->dumpVars($gw_responce);
}
$fields = Array('responce_code','responce_sub_code','responce_reason_code','responce_reason_text',
'approval_code','avs_result_code','transaction_id','invoice_number','description',
'amount','method','transaction_type','customer_id','first_name','last_name','company',
'address','city','state','zip','country','phone','fax','email');
$encapsulate_char = $gw_params['encapsulate_char'];
if($encapsulate_char)
{
$ec_length = strlen($encapsulate_char);
$gw_responce = substr($gw_responce, $ec_length, $ec_length * -1);
}
$gw_responce = explode($encapsulate_char.','.$encapsulate_char, $gw_responce);
$ret = Array();
foreach($fields as $field_index => $field_name)
{
$ret[$field_name] = $gw_responce[$field_index];
unset($gw_responce[$field_index]);
}
$this->parsed_responce = $ret;
return array_merge_recursive2($ret, $gw_responce); // returns unparsed fields with they original indexes together with parsed ones
}
function getGWResponce()
{
return serialize($this->parsed_responce);
}
function getErrorMsg()
{
return $this->parsed_responce['responce_reason_text'];
}
function GetTestCCNumbers()
{
return array('370000000000002', '6011000000000012', '5424000000000015', '4007000000027', '4222222222222');
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/pricing/pricing_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/pricing/pricing_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/pricing/pricing_event_handler.php (revision 12722)
@@ -1,460 +1,465 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
// include globals.php from current folder
k4_include_once(FULL_PATH.'/in-commerce/units/pricing/globals.php');
class PricingEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnMoreBrackets' => Array('subitem' => 'add|edit'),
'OnInfinity' => Array('subitem' => 'add|edit'),
'OnArrange' => Array('subitem' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
function mapEvents()
{
parent::mapEvents(); // ensure auto-adding of approve/decine and so on events
$brackets_events = Array( 'OnMoreBrackets' => 'PricingBracketsAction',
'OnArrange' => 'PricingBracketsAction',
'OnInfinity' => 'PricingBracketsAction');
$this->eventMethods = array_merge($this->eventMethods, $brackets_events);
}
function PricingBracketsAction(&$event)
{
$event->redirect=false;
$temp = $this->Application->GetVar($event->getPrefixSpecial(true));
// $object =& $event->GetObject();
// $formatter =& $this->Application->recallObject('kFormatter');
// $temp = $formatter->TypeCastArray($temp, $object);
//uasort($temp, 'pr_bracket_comp');
$bracket =& $this->Application->recallObject($event->getPrefixSpecial());
foreach($temp as $id => $record)
{
if( $record['MaxQty'] == '&#8734;' || $record['MaxQty'] == '∞')
{
$temp[$id]['MaxQty'] = -1;
}
}
$group_id = $this->Application->getVar('group_id');
if($group_id>0){
$where_group=' GroupId = '.$group_id.' ';
}
else {
$where_group= ' TRUE ';
}
switch ($event->Name)
{
case 'OnMoreBrackets':
$new_id = (int)$this->Conn->GetOne('SELECT MIN('.$bracket->IDField.') FROM '.$bracket->TableName);
if($new_id > 0) $new_id = 0;
do
{
$new_id--;
} while
($this->check_array($this->Application->GetVar($event->getPrefixSpecial(true)), 'PriceId', $new_id));
$last_max_qty = $this->Conn->GetOne('SELECT MAX(MaxQty) FROM '.$bracket->TableName.' WHERE '.$where_group);
$min_qty = $this->Conn->GetOne('SELECT MIN(MaxQty) FROM '.$bracket->TableName.' WHERE '.$where_group);
if ($min_qty==-1) $last_max_qty = -1;
if (!$last_max_qty) $last_max_qty=1;
for($i = $new_id; $i > $new_id - 5; $i--)
{
$temp[$i]['PriceId'] = $i;
$temp[$i]['MinQty'] = ($i == $new_id-4 && $last_max_qty != -1) ? $last_max_qty : '';
$temp[$i]['MaxQty'] = ($i == $new_id-4 && $last_max_qty != -1) ? -1 : '';
$temp[$i]['Price'] = '';
$temp[$i]['Cost'] = '';
$temp[$i]['Points'] = '';
$temp[$i]['Negotiated'] = '0';
$temp[$i]['IsPrimary'] = '0';
$temp[$i]['GroupId'] = $group_id;
}
$this->Application->SetVar($event->getPrefixSpecial(true), $temp);
$event->CallSubEvent('OnPreSaveBrackets');
break;
case 'OnArrange':
$temp=$this->OnArrangeBrackets($event, $temp, $bracket);
$this->Application->SetVar($event->getPrefixSpecial(true), $temp);
$event->CallSubEvent('OnPreSaveBrackets');
break;
case 'OnInfinity':
$temp=$this->OnArrangeBrackets($event, $temp, $bracket);
$this->Application->SetVar($event->getPrefixSpecial(true), $temp);
$event->CallSubEvent('OnPreSaveBrackets');
$infinite_exists = $this->Conn->GetOne('SELECT count(*) FROM '.$bracket->TableName.' WHERE MaxQty=-1 '.' AND '.$where_group);
if($infinite_exists==0){
reset($temp);
$last_bracket=end($temp);
$new_id = (int)$this->Conn->GetOne('SELECT MIN('.$bracket->IDField.') FROM '.$bracket->TableName);
$brackets_exist = (int)$this->Conn->GetOne('SELECT COUNT(*) FROM '.$bracket->TableName.' WHERE '.$where_group);
if($new_id > 0) $new_id = 0;
do
{
$new_id--;
} while
($this->check_array($this->Application->GetVar($event->getPrefixSpecial(true)), 'PriceId', $new_id));
$infinite_bracket['PriceId'] = $new_id;
$infinite_bracket['MinQty'] = ($brackets_exist>0)?$last_bracket['MaxQty']:1;
$infinite_bracket['MaxQty'] = '-1';
$infinite_bracket['Price'] = '';
$infinite_bracket['Cost'] = '';
$infinite_bracket['Points'] = '';
$infinite_bracket['Negotiated'] = '0';
$infinite_bracket['IsPrimary'] = '0';
$infinite_bracket['GroupId'] = $group_id;
$temp[$new_id]=$infinite_bracket;
reset($temp);
}
$this->Application->SetVar($event->getPrefixSpecial(true), $temp);
$event->CallSubEvent('OnPreSaveBrackets');
break;
default:
}
$this->Application->SetVar($event->getPrefixSpecial(true), $temp);
}
function OnPreSaveBrackets(&$event)
{
if( $this->Application->GetVar('pr_tang'))
{
$object =& $event->GetObject();
$group_id = $this->Application->getVar('group_id');
if($group_id>0){
$where_group=' AND GroupId = '.$group_id.' ';
}
$stored_ids = $this->Conn->GetCol("SELECT PriceId FROM ".$object->TableName." WHERE ProductId=".$this->Application->GetVar("p_id").' '.$where_group);
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true));
uasort($items_info, 'pr_bracket_comp');
foreach ($items_info as $item_id=>$values)
{
if (in_array($item_id, $stored_ids)) { //if it's already exist
$object->SetDefaultValues();
$object->Load($item_id);
$object->SetFieldsFromHash($values);
if (!$object->Validate()) {
unset($stored_ids[array_search($item_id, $stored_ids)]);
$event->redirect = false;
continue;
}
if( $object->Update($item_id) )
{
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
unset($stored_ids[array_search($item_id, $stored_ids)]);
}
else {
$object->SetDefaultValues();
$object->SetFieldsFromHash($values);
$object->SetDBField('ProductId', $this->Application->GetVar("p_id"));
if( $object->Create() )
{
$object->setTempID();
$event->status=erSUCCESS;
}
}
}
// delete
foreach ($stored_ids as $stored_id){
$this->Conn->Query("DELETE FROM ".$object->TableName." WHERE PriceId=".$stored_id);
}
}
}
function customProcessing(&$event,$type)
{
$bracket =& $event->getObject();
switch ($type)
{
case 'before':
$bracket->SetDBField('ProductId', $this->Application->GetVar('p_id'));
if( $bracket->GetDBField('MaxQty') == '&#8734;' || $bracket->GetDBField('MaxQty') == '∞' )
{
$bracket->SetDBField('MaxQty', -1);
}
break;
case 'after':
break;
default:
}
}
function OnArrangeBrackets(&$event, &$temp, &$bracket)
{
$temp_orig = $temp;
reset($temp);
if (is_array($temp))
{
// array to store max values (2nd column)
$end_values = Array();
// get minimal value of Min
$first_elem=current($temp);
$start = $first_elem['MinQty'];
if (!$start){
$start = 1;
}
foreach($temp as $id => $record)
{
/*
This 3-ifs logic fixes collision with invalid input values having
1 pricing record.
The logic is:
1) If we got Max less than Min, we set Min to 1 that gives us
integrity.
2) If we got equal values for Min and Max, we set range 1..Max like
in previous. But if Min was 1 and Max was 1 we set full range 1..infinity
3) If we got Max = 0 we just set it tom infinity because we can't
guess what user meant
*/
if (sizeof($temp) == 1 && $record['MinQty'] > ($record['MaxQty'] == -1 ? $record['MinQty']+1 : $record['MaxQty']) ){
$record['MinQty'] = 1;
$temp[$id]['MinQty'] = 1;
$start = 1;
}
if (sizeof($temp) == 1 && $record['MinQty'] == $record['MaxQty']){
if ($record['MaxQty'] == 1){
$record['MaxQty'] = -1;
$temp[$id]['MaxQty'] = -1;
}
else {
$record['MinQty'] = 1;
$temp[$id]['MinQty'] = 1;
}
}
if (sizeof($temp) == 1 && $record['MaxQty'] == 0){
$record['MaxQty'] = -1;
$temp[$id]['MaxQty'] = -1;
}
if(
// MAX is less than start
($record['MaxQty'] <= $start && $record['MaxQty'] != -1) ||
// Max is empty
!$record['MaxQty'] ||
// Max already defined in $end_values
(array_search($record['MaxQty'], $end_values) !== false)
) { // then delete from brackets list
unset($temp[$id]);
}
else { // this is when ok - add to end_values list
$end_values[] = $record['MaxQty'];
}
}
// sort brackets by 2nd column (Max values)
uasort($temp, 'pr_bracket_comp');
reset($temp);
$first_item=each($temp);
$first_item_key=$first_item['key'];
$group_id = $this->Application->getVar('group_id');
$default_group = $this->Application->ConfigValue('User_LoggedInGroup');
if($group_id>0){
$where_group=' AND GroupId = '.$group_id.' ';
}
$ids = $this->Conn->GetCol('SELECT PriceId FROM '.$bracket->TableName.' WHERE ProductId='.$this->Application->GetVar('p_id').' '.$where_group);
if(is_array($ids)) {
usort($ids, 'pr_bracket_id_sort');
}
$min_id = min( min($ids) - 1, -1 );
foreach($temp as $key => $record)
{
$temp[$key]['MinQty']=$start;
$temp[$key]['IsPrimary']=0;
$temp[$key]['GroupId']=$group_id;
$start=$temp[$key]['MaxQty'];
}
if ($temp[$first_item_key]['GroupId'] == $default_group) {
$temp[$first_item_key]['IsPrimary']=1;
}
}
return $temp;
}
/**
* Set's price as primary for product
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$this->StoreSelectedIDs($event);
$ids=$this->getSelectedIDs($event);
if($ids)
{
$id = array_shift($ids);
$table_info = $object->getLinkedInfo();
$this->Conn->Query('UPDATE '.$object->TableName.' SET IsPrimary = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET IsPrimary = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (PriceId = '.$id.')');
}
$event->redirect_params = Array('opener' => 's'); //stay!
}
function OnBeforeItemUpdate(&$event)
{
// TODO: during import special of product is not empty as usual and this raises problems to getLinkedInfo
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
$table_info['ParentId'] = ($table_info['ParentId']?$table_info['ParentId']:0);
if ( $object->GetDBField('IsPrimary') == 1 ){
$this->Conn->Query('UPDATE '.$object->TableName.' SET IsPrimary = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject();
$table_info = $object->getLinkedInfo($event->Special);
$table_info['ParentId'] = ($table_info['ParentId']?$table_info['ParentId']:0);
if ( $object->GetDBField('IsPrimary') == 1 ){
$this->Conn->Query('UPDATE '.$object->TableName.' SET IsPrimary = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
}
else {
$prices_qty = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName.' WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
if ($prices_qty == 0) {
$object->SetDBField('IsPrimary', 1);
}
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
- function SetCustomQuery(&$event){
+ function SetCustomQuery(&$event)
+ {
$object =& $event->getObject();
- if (!$this->Application->IsAdmin()){
- $user_groups = $this->Application->RecallVar('UserGroups');
-
- $pricing_method = $this->Application->ConfigValue('Comm_PriceBracketCalculation');
- if ($pricing_method == 1){
- $primary_group = $this->Conn->GetOne('SELECT GroupId FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId='.$this->Application->GetVar('u_id').' AND PrimaryGroup=1');
- $pricing_group = $primary_group;
- if ($pricing_group) {
- $pricing_for_group_exists = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'ProductsPricing WHERE ProductId='.$this->Application->GetVar('p_id').' AND GroupId='.$primary_group.' AND Price IS NOT NULL');
- }
- if ($pricing_group && $pricing_for_group_exists > 0){
- $pricing_group = $primary_group;
- }
- else {
- $pricing_group = $this->Application->ConfigValue('User_LoggedInGroup');
- }
+ if ($this->Application->isAdminUser) {
+ return ;
+ }
+ $user_groups = $this->Application->RecallVar('UserGroups');
+ $pricing_method = $this->Application->ConfigValue('Comm_PriceBracketCalculation');
+ if ($pricing_method == 1) {
+ $primary_group = $this->Conn->GetOne('SELECT GroupId FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId='.$this->Application->GetVar('u_id').' AND PrimaryGroup=1');
+ $pricing_group = $primary_group;
+ if ($pricing_group) {
+ $pricing_for_group_exists = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'ProductsPricing WHERE ProductId='.$this->Application->GetVar('p_id').' AND GroupId='.$primary_group.' AND Price IS NOT NULL');
+ }
+ if ($pricing_group && $pricing_for_group_exists > 0) {
+ $pricing_group = $primary_group;
}
else {
- //$cheapest_group = $this->Conn->GetOne('SELECT GroupId FROM '.$object->TableName.' WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') AND MinQty = 1 GROUP BY GroupId ORDER BY Price ASC');
- $effective_brackets = $this->Conn->Query('SELECT PriceId, Price, GroupId FROM '.$object->TableName.'
- WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') ORDER BY GroupId ASC, MinQty ASC', 'PriceId');
- $group_prices = array();
- $min_price = -1;
- $cheapest_group = 0;
-
- foreach ($effective_brackets as $bracket) {
- if (!isset($group_prices[$bracket['GroupId']])) {
- $group_prices[$bracket['GroupId']] = $bracket['Price'];
- if ($bracket['Price'] < $min_price || $min_price == -1) {
- $min_price = $bracket['Price'];
- $cheapest_group = $bracket['GroupId'];
- }
+ $pricing_group = $this->Application->ConfigValue('User_LoggedInGroup');
+ }
+
+
+ }
+ else {
+ //$cheapest_group = $this->Conn->GetOne('SELECT GroupId FROM '.$object->TableName.' WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') AND MinQty = 1 GROUP BY GroupId ORDER BY Price ASC');
+ $effective_brackets = $this->Conn->Query('SELECT PriceId, Price, GroupId FROM '.$object->TableName.'
+ WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') ORDER BY GroupId ASC, MinQty ASC', 'PriceId');
+ $group_prices = array();
+ $min_price = -1;
+ $cheapest_group = 0;
+
+ foreach ($effective_brackets as $bracket) {
+ if (!isset($group_prices[$bracket['GroupId']])) {
+ $group_prices[$bracket['GroupId']] = $bracket['Price'];
+ if ($bracket['Price'] < $min_price || $min_price == -1) {
+ $min_price = $bracket['Price'];
+ $cheapest_group = $bracket['GroupId'];
}
}
+ }
- if (!$cheapest_group) $cheapest_group = $this->Application->ConfigValue('User_LoggedInGroup');
-
- $pricing_group = $cheapest_group;
+ if (!$cheapest_group) {
+ $cheapest_group = $this->Application->ConfigValue('User_LoggedInGroup');
}
- $object->addFilter('price_user_group', $object->TableName.'.GroupId='.$pricing_group);
+ $pricing_group = $cheapest_group;
}
+
+ $object->addFilter('price_user_group', $object->TableName.'.GroupId='.$pricing_group);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/shipping_quote_engines/custom_shipping_quote_engine.php
===================================================================
--- branches/5.0.x/in-commerce/units/shipping_quote_engines/custom_shipping_quote_engine.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/shipping_quote_engines/custom_shipping_quote_engine.php (revision 12722)
@@ -1,179 +1,179 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class CustomShippingQuoteEngine extends ShippingQuoteEngine {
function GetShippingQuotes($params)
{
$db =& $this->Application->GetADODBConnection();
$packages = $params['packages'];
$default_pack = array_shift($packages);
$query = $this->QueryShippingCost($params['dest_country'], $params['dest_state'], $params['dest_postal'], $default_pack['weight'], $params['items'], $params['amount'], $params['shipping_type'], $params['promo_params']);
$shipping_types = $db->Query($query, 'ShippingId');
- if (!$this->Application->IsAdmin()) {
+ if (!$this->Application->isAdminUser) {
$user_groups = explode(',', $this->Application->RecallVar('UserGroups'));
$filteres_shipping_types = array();
foreach ($shipping_types as $key=>$shipping_type) {
$shipping_type_groups = explode(',', trim($shipping_type['PortalGroups'], ','));
if (array_intersect($shipping_type_groups, $user_groups)) {
$filteres_shipping_types[$key] = $shipping_type;
}
}
$shipping_types = $filteres_shipping_types;
}
return $shipping_types;
}
function QueryShippingCost($user_country_abbr, $user_state_abbr, $user_zip, $weight, $items, $amount, $shipping_type=null, $promo_params)
{
$db =& $this->Application->GetADODBConnection();
$user_country_id = (int) $db->GetOne('SELECT DestId FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=1 AND DestAbbr = '.$db->qstr($user_country_abbr));
$user_state_id = (int) $db->GetOne('SELECT DestId FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType=2 AND DestAbbr = '.$db->qstr($user_state_abbr));
$user_zip = (string) $user_zip;
$weight = (float) $weight;
$items = (int) $items;
$amount = (float) $amount;
$promo_weight = $promo_params['weight'];
$promo_amount = (float) $promo_params['amount'];
$promo_items = (float) $promo_params['items'];
if (isset($shipping_type)) $shipping_type = (int) $shipping_type;
$query = 'SELECT
CONCAT("CUST_", st.ShippingID) as ShippingId,
IF(st.Type = 4, st.BaseFee, '.# taking only base fee for handling
'IF (
st.IsFreePromoShipping = 1 AND
( (st.FreeShippingMinAmount != 0 AND st.FreeShippingMinAmount <= '.$amount.') OR
(st.Type = 1 AND '.$promo_weight.' = 0) OR
(st.Type = 2 AND '.$promo_items.' = 0) OR
(st.Type = 3 AND '.$promo_amount.' = 0)
), 0,
MIN(
IF(st.BaseFee IS NULL, 0, st.BaseFee) +
IF(st.CostType IN (1,3), IF(sc.Flat IS NULL, 0, sc.Flat), 0) +
IF(st.CostType IN (2,3),
IF(sc.PerUnit IS NULL, 0, sc.PerUnit)
*
( CASE st.Type
WHEN 1 THEN IF(st.IsFreePromoShipping = 1, '.$promo_weight.', '.$weight.')
WHEN 2 THEN IF(st.IsFreePromoShipping = 1, '.$promo_items.', '.$items.')
WHEN 3 THEN IF(st.IsFreePromoShipping = 1, '.$promo_amount.', '.$amount.')
ELSE 0 END ),
0)
)
)
) AS TotalCost,
st.Name as ShippingName,
st.Type as Type,
st.CODFlatSurcharge as CODFlat,
st.CODPercentSurcharge as CODPercent,
st.PortalGroups as PortalGroups,
IF (
st.InsuranceType = 1,
st.InsuranceFee,
st.InsuranceFee * '.$amount.' / 100
) AS InsuranceFee,
IF(sz.CODallowed IS NULL, 0, sz.CODallowed) AS COD,
st.Status = 2 as SelectedOnly,
st.Code
FROM '.TABLE_PREFIX.'ShippingType AS st '. #all shipping types
'LEFT JOIN '.TABLE_PREFIX.'ShippingBrackets AS sb '. #getting brackets by shipping type
'ON sb.ShippingTypeID = st.ShippingID '.
'LEFT JOIN '.TABLE_PREFIX.'ShippingZones AS sz '. #getting zones by shipping type
'ON sz.ShippingTypeID = st.ShippingID '.
'LEFT JOIN '.TABLE_PREFIX.'ShippingZonesDestinations AS szd '. #getting destinations by shipping type
'ON szd.ShippingZoneId = sz.ZoneID '.
'LEFT JOIN '.TABLE_PREFIX.'ShippingCosts AS sc '. #getting costs by bracket and zone
'ON sc.BracketId = sb.BracketId AND sc.ZoneID = sz.ZoneID '.
'WHERE
'.(isset($shipping_type) ? 'st.ShippingID = '.$shipping_type.' AND ' : '').'
(st.Status >= 1) '. # enabled (1) or Selected Only (2) shipping types
'AND
(
'.# handlign should require brackets/zones (st.Type = 4) # handling - does not required brackets
# OR
'(
( st.Type IN (1,2,3,4) ) '. # bracket dependant types
'AND
'.# Zone match
'( (sz.Type = 1 AND
(szd.StdDestId = '.$user_country_id.') '.# user country id
')
OR
(sz.Type = 2 AND
(szd.StdDestId = '.$user_state_id.') '.# user state id
')
OR
(sz.Type = 3 AND
(szd.StdDestId = '.$user_country_id.') '.# user country id
'AND
(szd.DestValue = '.$db->qstr($user_zip).') '.# user zip code
')
)
AND
'.# Bracket match
'( (st.Type = 1
AND
sb.Start <= IF(st.IsFreePromoShipping = 1, '.$promo_weight.', '.$weight.') AND (sb.End > IF(st.IsFreePromoShipping = 1, '.$promo_weight.', '.$weight.') OR sb.End = -1) '.# items total weight
')
OR
(st.Type = 2
AND
sb.Start <= IF(st.IsFreePromoShipping = 1, '.$promo_items.', '.$items.') AND (sb.End > IF(st.IsFreePromoShipping = 1, '.$promo_items.', '.$items.') OR sb.End = -1) '.# items total qty
')
OR
(st.Type = 3
AND
sb.Start <= IF(st.IsFreePromoShipping = 1, '.$promo_amount.', '.$amount.') AND (sb.End > IF(st.IsFreePromoShipping = 1, '.$promo_amount.', '.$amount.') OR sb.End = -1) '.# items total amount
')
OR
st.Type = 4 '.# handling - does not depend on brackets
')
AND
'.# Empty costs handling
'( (st.ZeroIfEmpty = 0 AND (sc.Flat IS NOT NULL OR sc.PerUnit IS NOT NULL) ) '.# if no shimpent on empty - flat or perunit should be filled in
'OR
(st.ZeroIfEmpty = 1) '. # zero on empty - is ok (nulls will be converted in SELECT part)
'OR
(st.Type = 4) '. # ignore costs for handling
')
)
)
GROUP BY st.ShippingId '. # getting minimal price (possible with closest address match) for every shipping type
'ORDER BY TotalCost asc';
return $query;
}
function GetAvailableTypes()
{
$conn =& $this->Application->GetADODBConnection();
$types = $conn->Query('SELECT * FROM '.TABLE_PREFIX.'ShippingType');
$ret = array();
foreach ($types as $a_type) {
$a_type['_ClassName'] = get_class($this);
$a_type['_Id'] = 'CUST_'.$a_type['ShippingID'];
$a_type['_Name'] = '(Custom) '.$a_type['Name'];
$ret[] = $a_type;
}
return $ret;
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/manufacturers/manufacturers_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/manufacturers/manufacturers_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/manufacturers/manufacturers_event_handler.php (revision 12722)
@@ -1,116 +1,116 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ManufacturersEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnItemBuild' => Array('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function SetCustomQuery(&$event)
{
- if ($this->Application->IsAdmin()) {
- return true;
+ if ($this->Application->isAdminUser) {
+ return ;
}
$category_id = $this->Application->GetVar('m_cat_id');
$parent_category_id = $event->getEventParam('parent_cat_id');
if ($parent_category_id) {
if ($parent_category_id != 'any' && $parent_category_id > 0) {
$category_id = $parent_category_id;
}
}
$sql = 'SELECT m.ManufacturerId, COUNT(p.ProductId)
FROM '.TABLE_PREFIX.'Manufacturers m
LEFT JOIN '.TABLE_PREFIX.'Products p ON p.ManufacturerId = m.ManufacturerId
LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = p.ResourceId
LEFT JOIN '.TABLE_PREFIX.'Category c ON c.CategoryId = ci.CategoryId
WHERE (ci.PrimaryCat = 1) AND (p.Status = ' . STATUS_ACTIVE . ') AND (c.Status = ' . STATUS_ACTIVE . ') GROUP BY m.ManufacturerId';
// add category filter
$tree_indexes = $this->Application->getTreeIndex($category_id);
// if category_id is 0 returs false
if ($tree_indexes) {
$sql .= ' AND c.TreeLeft BETWEEN '.$tree_indexes['TreeLeft'].' AND '.$tree_indexes['TreeRight'];
}
$manufacturers = $this->Conn->GetCol($sql);
$object =& $event->getObject();
$object->addFilter('category_manufacturer_filter', $manufacturers ? '%1$s.ManufacturerId IN (' . implode(',', $manufacturers) . ')' : 'FALSE');
}
/**
* Prefill states dropdown with correct values
*
* @param kEvent $event
* @access public
*/
function OnPrepareStates(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->PopulateStates($event, 'State', 'Country');
$object =& $event->getObject();
if( $object->isRequired('Country') && $cs_helper->CountryHasStates( $object->GetDBField('Country') ) ) $object->setRequired('State', true);
}
function OnUpdate(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->CheckStateField($event, 'State', 'Country');
parent::OnUpdate($event);
}
function OnCreate(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->CheckStateField($event, 'State', 'Country');
parent::OnCreate($event);
}
function OnPreSave(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->CheckStateField($event, 'State', 'Country');
parent::OnPreSave($event);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/shipping/shipping_tag_processor.php
===================================================================
--- branches/5.0.x/in-commerce/units/shipping/shipping_tag_processor.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/shipping/shipping_tag_processor.php (revision 12722)
@@ -1,307 +1,307 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ShippingTagProcessor extends kDBTagProcessor {
function CostInputSize($params)
{
$object =& $this->Application->recallObject( $this->getPrefixSpecial() );
$prec_before_sep = $object->GetDBField('PrecisionBeforeSep');
$prec_after_sep = $object->GetDBField('PrecisionAfterSep');
return $prec_before_sep + $prec_after_sep + 1 + ($prec_before_sep > 3 ? 1:0);
}
function ShowCostsTable($params)
{
$object =& $this->Application->recallObject( $this->getPrefixSpecial() );
$zones_object =& $this->Application->recallObject('z');
$brackets_object =& $this->Application->recallObject('br');
$costs_object =& $this->Application->recallObject('sc');
$main_processor =& $this->Application->recallObject('m_TagProcessor');
$zones_sql = 'SELECT * FROM '.$zones_object->TableName.' WHERE ShippingTypeID='.$this->Application->GetVar('s_id').' ORDER BY Name ASC';
$brackets_sql = 'SELECT * FROM '.$brackets_object->TableName.' WHERE ShippingTypeID='.$this->Application->GetVar('s_id').' ORDER BY Start ASC';
$sql = 'SELECT * FROM '.$costs_object->TableName;
$costs_array = $this->Conn->Query($sql, 'ShippingCostId');
$zones = $this->Conn->Query($zones_sql, 'ZoneID');
$brackets = $this->Conn->Query($brackets_sql, 'BracketId');
$oddevenparam['odd'] = 'table-color1';
$oddevenparam['even'] = 'table-color2';
if(!$zones || !$brackets)
{
return '<tr class="'.$main_processor->Odd_Even($oddevenparam).'"><td>'.$this->Application->Phrase('la_NoZonesOrBrackets').'</td></tr>';
}
uasort($brackets, 'bracket_comp');
if( $this->Application->GetLinkedVar('CostsTableAligment') )
{
$column_items = $brackets;
$column_object =& $brackets_object;
$column_params['header_caption'] = 'bracket_caption';
$column_params['IdField'] = 'BracketId';
$column_params['prefix'] = 'br';
$row_items = $zones;
$row_object =& $zones_object;
$row_params['header_caption'] = 'zone_caption';
$row_params['IdField'] = 'ZoneID';
$row_params['prefix'] = 'z';
}
else
{
$column_items = $zones;
$column_object =& $zones_object;
$column_params['header_caption'] = 'zone_caption';
$column_params['IdField'] = 'ZoneID';
$column_params['prefix'] = 'z';
$row_items = $brackets;
$row_object =& $brackets_object;
$row_params['header_caption'] = 'bracket_caption';
$row_params['IdField'] = 'BracketId';
$row_params['prefix'] = 'br';
}
$costs_table = '<tr class="'.$main_processor->Odd_Even($oddevenparam).'"><td>&nbsp;</td>';
foreach($column_items as $id => $record)
{
$column_object->Load($id);
$head_row_params = $column_params;
$head_row_params['name'] = 'column_header';
$costs_table .= $this->Application->ParseBlock($head_row_params);
}
$costs_table .= '</tr>';
$cost_ids = Array();
foreach($row_items as $id =>$record)
{
$costs_table .= '<tr class="'.$main_processor->Odd_Even($oddevenparam).'">';
$row_object->Load($id);
$head_row_params = $row_params;
$head_row_params['name'] = 'row_header';
$costs_table .= $this->Application->ParseBlock($head_row_params);
foreach($column_items as $col_id => $col_record)
{
$res = false;
foreach($costs_array as $cost_id => $cost_record)
{
if($cost_record[$row_params['IdField']] == $id && $cost_record[$column_params['IdField']] == $col_id)
{
$costs_object->SetDBFieldsFromHash($cost_record);
$res = true;
break;
}
}
if($res == false)
{
$costs_object->SetDefaultValues();
$sql = 'SELECT MIN(ShippingCostId) FROM '.$costs_object->TableName;
$new_id = $cost_ids ? min( $this->Conn->GetOne($sql) - 1, min($cost_ids) - 1 ) : 0;
$costs_object->SetDBField( 'ShippingCostId', $new_id );
$cost_ids[] = $new_id;
}
$column_object->Load($col_id);
$costs_object->SetID($costs_object->GetDBField('ShippingCostId'));
$cost_cell_params['name'] = 'cost_cell';
$costs_table .= $this->Application->ParseBlock($cost_cell_params);
}
$costs_table .= '</tr>';
}
return $costs_table;
}
function HiddenSelection($params)
{
// $object = $this->getPrefixSpecial();
$zones = $this->Application->GetVar('z');
$brackets = $this->Application->GetVar('br');
$ret = '';
foreach($zones as $id => $record)
{
$ret .= '<input type="hidden" name="z['.$id.'][ZoneID]" value="'.$id.'">'."\n";
}
foreach ($brackets as $id => $record)
{
$ret .= '<input type="hidden" name="br['.$id.'][BracketId]" value="'.$id.'">'."\n";
}
return $ret;
}
function Order_PrintShippingTypes($params)
{
$weight = $this->Application->Parser->GetParam('weight_metric');
$items = $this->Application->Parser->GetParam('items');
$amount = $this->Application->Parser->GetParam('amount');
$selected_id = $this->Application->Parser->GetParam('selected_id');
$package_id = $this->Application->Parser->GetParam('package_num');
// free promo shipping params if applicable, if not then the same as standard
$promo_items = $this->Application->Parser->GetParam('promo_items');
$promo_amount = $this->Application->Parser->GetParam('promo_amount');
$promo_weight = $this->Application->Parser->GetParam('promo_weight_metric');
$user_country_id = $this->Application->Parser->GetParam('user_country_id');
$user_state_id = $this->Application->Parser->GetParam('user_state_id');
$user_zip = $this->Application->Parser->GetParam('user_zip');
$user_city = $this->Application->Parser->GetParam('user_city');
$user_addr1 = $this->Application->Parser->GetParam('user_addr1');
$user_addr2 = $this->Application->Parser->GetParam('user_addr2');
$user_name = $this->Application->Parser->GetParam('user_name');
$limit_types = $this->Application->Parser->GetParam('limit_types');
$this->Application->recallObject('ShippingQuoteEngine');
$quote_engine_collector =& $this->Application->recallObject('ShippingQuoteCollector');
$shipping_quote_params = Array(
'dest_country' => $user_country_id,
'dest_state' => $user_state_id,
'dest_postal' => $user_zip,
'dest_city' => $user_city,
'dest_addr1' => $user_addr1,
'dest_addr2' => $user_addr2,
'dest_name' => $user_name,
'packages' => Array(
Array(
'package_key' => 'package1',
'weight' => $weight,
'weight_unit' => 'KG',
'length' => '',
'width' => '',
'height' => '',
'dim_unit' => 'IN',
'packaging' => 'BOX',
'contents' => 'OTR',
'insurance' => '0'
),
),
'amount' => $amount,
'items' => $items,
'limit_types' => $limit_types,
'promo_params' => Array (
'items' => $promo_items,
'amount' => $promo_amount,
'weight' => $promo_weight,
)
);
$shipping_types = $quote_engine_collector->GetShippingQuotes($shipping_quote_params);
$last_shippings = $this->Application->RecallVar('LastShippings');
if($last_shippings)
{
$last_shippings = unserialize($last_shippings);
}
$order_object =& $this->Application->recallObject('ord');
$original_shipping = $order_object->GetDBField('ShippingInfo');
$original_shipping = unserialize($original_shipping);
$shipping_type_keys = array_keys($shipping_types);
if( getArrayValue($original_shipping, $package_id, 'ShippingId') &&
- ( $this->Application->IsAdmin() || in_array( $original_shipping[$package_id]['ShippingId'], $shipping_type_keys ) ) )
+ ( $this->Application->isAdminUser || in_array( $original_shipping[$package_id]['ShippingId'], $shipping_type_keys ) ) )
{
$original_shipping = $original_shipping[$package_id];
$key = $original_shipping['ShippingId'];
- $shipping_types[$key]['TotalCost'] = $this->Application->IsAdmin() ? $original_shipping['TotalCost'] : $shipping_types[$key]['TotalCost'];
- $shipping_types[$key]['ShippingName'] = $this->Application->IsAdmin() ? 'Original: '.$original_shipping['ShippingName'] : $shipping_types[$key]['ShippingName'];
+ $shipping_types[$key]['TotalCost'] = $this->Application->isAdminUser ? $original_shipping['TotalCost'] : $shipping_types[$key]['TotalCost'];
+ $shipping_types[$key]['ShippingName'] = $this->Application->isAdminUser ? 'Original: '.$original_shipping['ShippingName'] : $shipping_types[$key]['ShippingName'];
$shipping_types[$key]['ShippingId'] = $key;
$selected_id = $key;
}
$last_shippings[$package_id] = $shipping_types;
- if( $this->Application->IsAdmin() && $key)
+ if( $this->Application->isAdminUser && $key)
{
$orig_name = ltrim($last_shippings[$package_id][$key]['ShippingName'], 'Original: ');
$last_shippings[$package_id][$key]['ShippingName'] = $orig_name;
}
$this->Application->StoreVar('LastShippings', serialize($last_shippings));
$o = '';
$def_block_params = Array();
$def_block_params['name'] = $this->SelectParam($params, 'render_as,block');
if(!count($shipping_types))
{
$this->Application->SetVar('ItemShipmentsExists', 0);
$o = '';
}
else
{
$lang =& $this->Application->recallObject('lang.current');
foreach ($shipping_types as $shipping_type) {
$shipping_type['ShippingFree'] = ($shipping_type['TotalCost'] == 0)? 1 : 0;
$iso = $this->GetISO($params['currency']);
$amount = $this->ConvertCurrency($shipping_type['TotalCost'], $iso);
$amount = $lang->formatNumber($amount,2);
$shipping_type['TotalCost'] = $this->AddCurrencySymbol($amount, $iso);
$block_params = array_merge($def_block_params, $shipping_type);
$block_params['selected'] = $shipping_type['ShippingId'] == $selected_id ? 'selected' : '';
if (isset($params['selected_only']) && $block_params['selected'] == '') continue;
$o .= $this->Application->ParseBlock($block_params);
}
}
return $o;
}
function AvailableTypes($params)
{
$quote_engine_collector =& $this->Application->recallObject('ShippingQuoteCollector');
$types = $quote_engine_collector->GetAvailableShippingTypes();
$o;
foreach ($types as $a_type)
{
$block_params = $a_type;
$block_params['name'] = $params['render_as'];
$o .= $this->Application->ParseBlock($block_params);
}
return $o;
}
function ListGroups($params)
{
$object =& $this->getObject($params);
$selected = trim($object->GetDBField('PortalGroups'), ',');
$selected_arr = explode(',', $selected);
$all_groups = $this->Conn->Query('SELECT GroupId, Name FROM '.TABLE_PREFIX.'PortalGroup ORDER BY NAME', 'GroupId');
$o = '';
foreach ($all_groups as $a_group)
{
$is_selected = in_array($a_group['GroupId'], $selected_arr);
$continue = $params['mode'] == 'selected' ? !$is_selected : $is_selected;
if ($continue) continue;
$block_params = $a_group;
$block_params['name'] = $params['render_as'];
$o .= $this->Application->ParseBlock($block_params);
}
return $o;
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/orders/orders_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/orders/orders_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/orders/orders_event_handler.php (revision 12722)
@@ -1,4122 +1,4127 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class OrdersEventHandler extends kDBEventHandler
{
/**
* Checks permissions of user
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
- if (!$this->Application->IsAdmin()) {
+ if (!$this->Application->isAdminUser) {
if ($event->Name == 'OnCreate') {
// user can't initiate custom order creation directly
return false;
}
$user_id = $this->Application->RecallVar('user_id');
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
// when POST is present, then check when is beeing submitted
$order_session_id = $this->Application->RecallVar($event->getPrefixSpecial(true).'_id');
$order_dummy =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true));
foreach ($items_info as $id => $field_values) {
if ($order_session_id != $id) {
// user is trying update not his order, even order from other guest
return false;
}
$order_dummy->Load($id);
// session_id matches order_id from submit
if ($order_dummy->GetDBField('PortalUserId') != $user_id) {
// user performs event on other user order
return false;
}
$status_field = array_shift($this->Application->getUnitOption($event->Prefix, 'StatusField'));
if (isset($field_values[$status_field]) && $order_dummy->GetDBField($status_field) != $field_values[$status_field]) {
// user can't change status by himself
return false;
}
if ($order_dummy->GetDBField($status_field) != ORDER_STATUS_INCOMPLETE) {
// user can't edit orders being processed
return false;
}
if ($event->Name == 'OnUpdate') {
// all checks were ok -> it's user's order -> allow to modify
return true;
}
}
}
}
return parent::CheckPermission($event);
}
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
// admin
'OnRecalculateItems' => Array('self' => 'add|edit'),
'OnResetToUser' => Array('self' => 'add|edit'),
'OnResetToBilling' => Array('self' => 'add|edit'),
'OnResetToShipping' => Array('self' => 'add|edit'),
'OnMassOrderApprove' => Array('self' => 'advanced:approve'),
'OnMassOrderDeny' => Array('self' => 'advanced:deny'),
'OnMassOrderArchive' => Array('self' => 'advanced:archive'),
'OnMassPlaceOrder' => Array('self' => 'advanced:place'),
'OnMassOrderProcess' => Array('self' => 'advanced:process'),
'OnMassOrderShip' => Array('self' => 'advanced:ship'),
'OnResetToPending' => Array('self' => 'advanced:reset_to_pending'),
'OnLoadSelected' => Array('self' => 'view'), // print in this case
'OnGoToOrder' => Array('self' => 'view'),
// front-end
'OnViewCart' => Array('self' => true),
'OnAddToCart' => Array('self' => true),
'OnRemoveFromCart' => Array('self' => true),
'OnUpdateCart' => Array('self' => true),
'OnUpdateItemOptions' => Array('self' => true),
'OnCleanupCart' => Array('self' => true),
'OnContinueShopping' => Array('self' => true),
'OnCheckout' => Array('self' => true),
'OnSelectAddress' => Array('self' => true),
'OnProceedToBilling' => Array('self' => true),
'OnProceedToPreview' => Array('self' => true),
'OnCompleteOrder' => Array('self' => true),
'OnRemoveCoupon' => Array('self' => true),
'OnRemoveGiftCertificate' => Array('self' => true),
'OnCancelRecurring' => Array('self' => true),
'OnAddVirtualProductToCart' => Array('self' => true),
'OnItemBuild' => Array('self' => true),
'OnDownloadLabel' => Array('self' => true, 'subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
function mapEvents()
{
parent::mapEvents();
$common_events = Array(
'OnResetToUser' => 'OnResetAddress',
'OnResetToBilling' => 'OnResetAddress',
'OnResetToShipping' => 'OnResetAddress',
'OnMassOrderProcess' => 'MassInventoryAction',
'OnMassOrderApprove' => 'MassInventoryAction',
'OnMassOrderDeny' => 'MassInventoryAction',
'OnMassOrderArchive' => 'MassInventoryAction',
'OnMassOrderShip' => 'MassInventoryAction',
'OnOrderProcess' => 'InventoryAction',
'OnOrderApprove' => 'InventoryAction',
'OnOrderDeny' => 'InventoryAction',
'OnOrderArchive' => 'InventoryAction',
'OnOrderShip' => 'InventoryAction',
);
$this->eventMethods = array_merge($this->eventMethods, $common_events);
}
/* ======================== FRONT ONLY ======================== */
function OnQuietPreSave(&$event)
{
$object =& $event->getObject();
$object->IgnoreValidation = true;
$event->CallSubEvent('OnPreSave');
$object->IgnoreValidation = false;
}
/**
* Sets new address to order
*
* @param kEvent $event
*/
function OnSelectAddress(&$event)
{
- if ($this->Application->IsAdmin()) return true;
+ if ($this->Application->isAdminUser) {
+ return ;
+ }
$object =& $event->getObject();
$shipping_address_id = $this->Application->GetVar('shipping_address_id');
$billing_address_id = $this->Application->GetVar('billing_address_id');
if ($shipping_address_id || $billing_address_id) {
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$address =& $this->Application->recallObject('addr.-item','addr', Array('skip_autoload' => true));
$addr_list =& $this->Application->recallObject('addr', 'addr_List', Array('per_page'=>-1, 'skip_counting'=>true) );
$addr_list->Query();
}
if ($shipping_address_id > 0) {
$addr_list->CopyAddress($shipping_address_id, 'Shipping');
$address->Load($shipping_address_id);
$address->MarkAddress('Shipping');
$cs_helper->PopulateStates($event, 'ShippingState', 'ShippingCountry');
$object->setRequired('ShippingState', false);
}
elseif ($shipping_address_id == -1) {
$object->ResetAddress('Shipping');
}
if ($billing_address_id > 0) {
$addr_list->CopyAddress($billing_address_id, 'Billing');
$address->Load($billing_address_id);
$address->MarkAddress('Billing');
$cs_helper->PopulateStates($event, 'BillingState', 'BillingCountry');
$object->setRequired('BillingState', false);
}
elseif ($billing_address_id == -1) {
$object->ResetAddress('Billing');
}
$event->redirect = false;
$object->IgnoreValidation = true;
$this->RecalculateTax($event);
$object->Update();
}
/**
* Updates order with registred user id
*
* @param kEvent $event
*/
function OnUserCreate(&$event)
{
if( !($event->MasterEvent->status == erSUCCESS) ) return false;
$ses_id = $this->Application->RecallVar('front_order_id');
if($ses_id)
{
$this->updateUserID($ses_id, $event);
$this->Application->RemoveVar('front_order_id');
}
}
/**
* Enter description here...
*
* @param unknown_type $event
* @return unknown
*/
function OnUserLogin(&$event)
{
if( !($event->MasterEvent->status == erSUCCESS) ) {
return false;
}
$ses_id = $this->Application->RecallVar('ord_id');
if ($ses_id) $this->updateUserID($ses_id, $event);
$user_id = $this->Application->RecallVar('user_id');
$affiliate_id = $this->isAffiliate($user_id);
if($affiliate_id) $this->Application->setVisitField('AffiliateId', $affiliate_id);
$event->CallSubEvent('OnRecalculateItems');
}
function updateUserID($order_id, &$event)
{
$table = $this->Application->getUnitOption($event->Prefix,'TableName');
$id_field = $this->Application->getUnitOption($event->Prefix,'IDField');
$user_id = $this->Application->RecallVar('user_id');
$this->Conn->Query('UPDATE '.$table.' SET PortalUserId = '.$user_id.' WHERE '.$id_field.' = '.$order_id);
$affiliate_id = $this->isAffiliate($user_id);
if($affiliate_id)
{
$this->Conn->Query('UPDATE '.$table.' SET AffiliateId = '.$affiliate_id.' WHERE '.$id_field.' = '.$order_id);
}
}
function isAffiliate($user_id)
{
$affiliate_user =& $this->Application->recallObject('affil.-item', null, Array('skip_autoload' => true) );
$affiliate_user->Load($user_id, 'PortalUserId');
return $affiliate_user->isLoaded() ? $affiliate_user->GetDBField('AffiliateId') : 0;
}
function ChargeOrder(&$order)
{
$gw_data = $order->getGatewayData();
$this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] );
$gateway_object =& $this->Application->recallObject( $gw_data['ClassName'] );
$payment_result = $gateway_object->DirectPayment($order->FieldValues, $gw_data['gw_params']);
$sql = 'UPDATE %s SET GWResult1 = %s WHERE %s = %s';
$sql = sprintf($sql, $order->TableName, $this->Conn->qstr($gateway_object->getGWResponce()), $order->IDField, $order->GetID() );
$this->Conn->Query($sql);
$order->SetDBField('GWResult1', $gateway_object->getGWResponce() );
return array('result'=>$payment_result, 'data'=>$gateway_object->parsed_responce, 'gw_data' => $gw_data, 'error_msg'=>$gateway_object->getErrorMsg());
}
function OrderEmailParams(&$order)
{
$billing_email = $order->GetDBField('BillingEmail');
$user_email = $this->Conn->GetOne(' SELECT Email FROM '.$this->Application->getUnitOption('u', 'TableName').'
WHERE PortalUserId = '.$order->GetDBField('PortalUserId'));
$email_params = Array();
$email_params['_user_email'] = $user_email; //for use when shipping vs user is required in InvetnoryAction
$email_params['to_email'] = $billing_email ? $billing_email : $user_email;
$email_params['to_name'] = $order->GetDBField('BillingTo');
return $email_params;
}
function PrepareCoupons(&$event, &$order)
{
$order_items =& $this->Application->recallObject('orditems.-inv','orditems_List',Array('skip_counting'=>true,'per_page'=>-1) );
$order_items->linkToParent($order->Special);
$order_items->Query();
$order_items->GoFirst();
$assigned_coupons = array();
$coup_handler =& $this->Application->recallObject('coup_EventHandler');
foreach($order_items->Records as $product_item)
{
if ($product_item['ItemData']) {
$item_data = unserialize($product_item['ItemData']);
if (isset($item_data['AssignedCoupon']) && $item_data['AssignedCoupon']) {
$coupon_id = $item_data['AssignedCoupon'];
// clone coupon, get new coupon ID
$coupon =& $this->Application->recallObject('coup',null,array('skip_autload' => true));
/* @var $coupon kDBItem */
$coupon->Load($coupon_id);
if (!$coupon->isLoaded()) continue;
$coup_handler->SetNewCode($coupon);
$coupon->NameCopy();
$coupon->SetDBField('Name', $coupon->GetDBField('Name').' (Order #'.$order->GetField('OrderNumber').')');
$coupon->Create();
// add coupon code to array
array_push($assigned_coupons, $coupon->GetDBField('Code'));
}
}
}
/* @var $order OrdersItem */
if ($assigned_coupons) {
$comments = $order->GetDBField('AdminComment');
if ($comments) $comments .= "\r\n";
$comments .= "Issued coupon(s): ". join(',', $assigned_coupons);
$order->SetDBField('AdminComment', $comments);
$order->Update();
}
if ($assigned_coupons) $this->Application->SetVar('order_coupons', join(',', $assigned_coupons));
}
/**
* Completes order if possible
*
* @param kEvent $event
* @return bool
*/
function OnCompleteOrder(&$event)
{
$this->LockTables($event);
if (!$this->CheckQuantites($event)) return;
$this->ReserveItems($event);
$order =& $event->getObject();
$charge_result = $this->ChargeOrder($order);
if (!$charge_result['result']) {
$this->FreeItems($event);
$this->Application->StoreVar('gw_error', $charge_result['error_msg']);
//$this->Application->StoreVar('gw_error', getArrayValue($charge_result, 'data', 'responce_reason_text') );
$event->redirect = $this->Application->GetVar('failure_template');
$event->redirect_params['m_cat_id'] = 0;
if ($event->Special == 'recurring') { // if we set failed status for other than recurring special the redirect will not occur
$event->status = erFAIL;
}
return false;
}
// call CompleteOrder events for items in order BEFORE SplitOrder (because ApproveEvents are called there)
$order_items =& $this->Application->recallObject('orditems.-inv','orditems_List',Array('skip_counting'=>true,'per_page'=>-1) );
$order_items->linkToParent($order->Special);
$order_items->Query(true);
$order_items->GoFirst();
foreach($order_items->Records as $product_item)
{
if (!$product_item['ProductId']) continue; // product may have been deleted
$this->raiseProductEvent('CompleteOrder', $product_item['ProductId'], $product_item);
}
$shipping_control = getArrayValue($charge_result, 'gw_data', 'gw_params', 'shipping_control');
if ($event->Special != 'recurring') {
if ($shipping_control && $shipping_control != SHIPPING_CONTROL_PREAUTH ) {
// we have to do it here, because the coupons are used in the e-mails
$this->PrepareCoupons($event, $order);
}
$email_event_user =& $this->Application->EmailEventUser('ORDER.SUBMIT', $order->GetDBField('PortalUserId'), $this->OrderEmailParams($order));
$email_event_admin =& $this->Application->EmailEventAdmin('ORDER.SUBMIT');
}
if ($shipping_control === false || $shipping_control == SHIPPING_CONTROL_PREAUTH ) {
$order->SetDBField('Status', ORDER_STATUS_PENDING);
$order->Update();
}
else {
$this->SplitOrder($event, $order);
}
- if( !$this->Application->IsAdmin() )
- {
+ if (!$this->Application->isAdminUser) {
// for tracking code
$this->Application->StoreVar('last_order_amount', $order->GetDBField('TotalAmount'));
$this->Application->StoreVar('last_order_number', $order->GetDBField('OrderNumber'));
$this->Application->StoreVar('last_order_customer', $order->GetDBField('BillingTo'));
$this->Application->StoreVar('last_order_user', $order->GetDBField('Username'));
$event->redirect = $this->Application->GetVar('success_template');
$event->redirect_params['m_cat_id'] = 0;
}
else
{
// $event->CallSubEvent('OnSave');
}
$order_id = $order->GetId();
$order_idfield = $this->Application->getUnitOption('ord','IDField');
$order_table = $this->Application->getUnitOption('ord','TableName');
$original_amount = $order->GetDBField('SubTotal') + $order->GetDBField('ShippingCost') + $order->GetDBField('VAT') + $order->GetDBField('ProcessingFee') + $order->GetDBField('InsuranceFee') - $order->GetDBField('GiftCertificateDiscount');
$sql = 'UPDATE '.$order_table.'
SET OriginalAmount = '.$original_amount.'
WHERE '.$order_idfield.' = '.$order_id;
$this->Conn->Query($sql);
$this->Application->StoreVar('front_order_id', $order_id);
$this->Application->RemoveVar('ord_id');
}
/**
* Set billing address same as shipping
*
* @param kEvent $event
*/
function setBillingAddress(&$event)
{
$object =& $event->getObject();
if ($object->HasTangibleItems()) {
if ($this->Application->GetVar('same_address')) {
// copy shipping address to billing
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
list($id, $field_values) = each($items_info);
$address_fields = Array('To', 'Company', 'Phone', 'Fax', 'Email', 'Address1', 'Address2', 'City', 'State', 'Zip', 'Country');
foreach ($address_fields as $address_field) {
$items_info[$id]['Billing'.$address_field] = $object->GetDBField('Shipping'.$address_field);
}
$this->Application->SetVar($event->getPrefixSpecial(true), $items_info);
}
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnProceedToPreview(&$event)
{
$this->setBillingAddress($event);
$event->CallSubEvent('OnUpdate');
$event->redirect = $this->Application->GetVar('preview_template');
}
function OnViewCart(&$event)
{
$this->StoreContinueShoppingLink();
$event->redirect = $this->Application->GetVar('viewcart_template');
}
function OnContinueShopping(&$event)
{
$env = $this->Application->GetVar('continue_shopping_template');
if (!$env || $env == '__default__') {
$env = $this->Application->RecallVar('continue_shopping');
}
if (!$env) {
$env = 'in-commerce/index';
}
$event->redirect = $env;
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnCheckout(&$event)
{
$this->OnUpdateCart($event);
if ($event->getEventParam('RecalculateChangedCart'))
{
$event->SetRedirectParam('checkout_error', $event->redirect_params['checkout_error']);
}
else
{
$object =& $event->getObject();
if(!$object->HasTangibleItems())
{
$object->SetDBField('ShippingTo', '');
$object->SetDBField('ShippingCompany', '');
$object->SetDBField('ShippingPhone', '');
$object->SetDBField('ShippingFax', '');
$object->SetDBField('ShippingEmail', '');
$object->SetDBField('ShippingAddress1', '');
$object->SetDBField('ShippingAddress2', '');
$object->SetDBField('ShippingCity', '');
$object->SetDBField('ShippingState', '');
$object->SetDBField('ShippingZip', '');
$object->SetDBField('ShippingCountry', '');
$object->SetDBField('ShippingType', 0);
$object->SetDBField('ShippingCost', 0);
$object->SetDBField('ShippingCustomerAccount', '');
$object->SetDBField('ShippingTracking', '');
$object->SetDBField('ShippingDate', 0);
$object->SetDBField('ShippingOption', 0);
$object->SetDBField('ShippingInfo', '');
$object->Update();
}
$event->redirect = $this->Application->GetVar('next_step_template');
$order_id = $this->Application->GetVar('order_id');
if($order_id !== false) $event->redirect_params['ord_id'] = $order_id;
}
}
/**
* Redirect user to Billing checkout step
*
* @param kEvent $event
*/
function OnProceedToBilling(&$event)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
list($id,$field_values) = each($items_info);
$object =& $event->getObject();
$payment_type_id = $object->GetDBField('PaymentType');
if(!$payment_type_id)
{
$default_type = $this->Conn->GetOne('SELECT PaymentTypeId FROM '.TABLE_PREFIX.'PaymentTypes WHERE IsPrimary = 1');
if($default_type)
{
$field_values['PaymentType'] = $default_type;
$items_info[$id] = $field_values;
$this->Application->SetVar( $event->getPrefixSpecial(true), $items_info );
}
}
}
$event->CallSubEvent('OnUpdate');
$event->redirect = $this->Application->GetVar('next_step_template');
}
function OnCancelRecurring(&$event)
{
$order =& $event->GetObject();
$order->SetDBField('IsRecurringBilling', 0);
$order->Update();
if ($this->Application->GetVar('cancelrecurring_ok_template'))
{
$event->redirect = $this->Application->GetVar('cancelrecurring_ok_template');
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
$object =& $event->getObject();
$cvv2 = $object->GetDBField('PaymentCVV2');
if($cvv2 !== false) $this->Application->StoreVar('CVV2Code', $cvv2);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdate(&$event)
{
$this->setBillingAddress($event);
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$object =& $event->getObject();
if( $object->HasTangibleItems() )
{
$cs_helper->CheckStateField($event, 'ShippingState', 'ShippingCountry');
}
$cs_helper->CheckStateField($event, 'BillingState', 'BillingCountry');
parent::OnUpdate($event);
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return true;
}
else {
$event->redirect_params = Array('opener' => 's');
}
if ($event->status == erSUCCESS) {
$this->createMissingAddresses($event);
}
else {
// strange: recalculate total amount on error
$object->SetDBField('TotalAmount', $object->getTotalAmount());
}
}
/**
* Creates new address
*
* @param kEvent $event
*/
function createMissingAddresses(&$event)
{
if (!$this->Application->LoggedIn()) {
return false;
}
$object =& $event->getObject();
$addr_list =& $this->Application->recallObject('addr', 'addr_List', Array('per_page'=>-1, 'skip_counting'=>true) );
$addr_list->Query();
$address_dummy =& $this->Application->recallObject('addr.-item', null, Array('skip_autoload' => true));
$address_prefixes = Array('Billing', 'Shipping');
$address_fields = Array('To','Company','Phone','Fax','Email','Address1','Address2','City','State','Zip','Country');
foreach ($address_prefixes as $address_prefix) {
$address_id = $this->Application->GetVar(strtolower($address_prefix).'_address_id');
if (!$this->Application->GetVar('check_'.strtolower($address_prefix).'_address')) {
// form type doesn't match check type, e.g. shipping check on billing form
continue;
}
if ($address_id > 0) {
$address_dummy->Load($address_id);
}
else {
$address_dummy->SetDBField('PortalUserId', $this->Application->RecallVar('user_id') );
}
foreach ($address_fields as $address_field) {
$address_dummy->SetDBField($address_field, $object->GetDBField($address_prefix.$address_field));
}
$address_dummy->MarkAddress($address_prefix, false);
$ret = ($address_id > 0) ? $address_dummy->Update() : $address_dummy->Create();
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnPreSave(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$object =& $event->getObject();
if ( $object->GetID() !== false)
{
if( $object->HasTangibleItems() )
{
$cs_helper->CheckStateField($event, 'ShippingState', 'ShippingCountry');
}
$cs_helper->CheckStateField($event, 'BillingState', 'BillingCountry');
}
parent::OnPreSave($event);
}
function OnUpdateCart(&$event)
{
$this->Application->HandleEvent($items_event, 'orditems:OnUpdate');
return $event->CallSubEvent('OnRecalculateItems');
}
/**
* Adds item to cart
*
* @param kEvent $event
*/
function OnAddToCart(&$event)
{
$this->StoreContinueShoppingLink();
$qty = $this->Application->GetVar('qty');
$options = $this->Application->GetVar('options');
// multiple or options add
$items = Array();
if (is_array($qty)) {
foreach ($qty as $item_id => $combinations)
{
if (is_array($combinations)) {
foreach ($combinations as $comb_id => $comb_qty) {
if ($comb_qty == 0) continue;
$items[] = array('item_id' => $item_id, 'qty' => $comb_qty, 'comb' => $comb_id);
}
}
else {
$items[] = array('item_id' => $item_id, 'qty' => $combinations);
}
}
}
if (!$items) {
if (!$qty || is_array($qty)) $qty = 1;
$item_id = $this->Application->GetVar('p_id');
if (!$item_id) return ;
$items = array(array('item_id' => $item_id, 'qty' => $qty));
}
// remember item data passed to event when called
$default_item_data = $event->getEventParam('ItemData');
$default_item_data = $default_item_data ? unserialize($default_item_data) : Array();
foreach ($items as $an_item) {
$item_id = $an_item['item_id'];
$qty = $an_item['qty'];
$comb = getArrayValue($an_item, 'comb');
$item_data = $default_item_data;
$product =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
$product->Load($item_id);
$event->setEventParam('ItemData', null);
if ($product->GetDBField('AssignedCoupon')) {
$item_data['AssignedCoupon'] = $product->GetDBField('AssignedCoupon');
}
// 1. store options information OR
if ($comb) {
$combination = $this->Conn->GetOne('SELECT Combination FROM '.TABLE_PREFIX.'ProductOptionCombinations WHERE CombinationId = '.$comb);
$item_data['Options'] = unserialize($combination);
}
elseif (is_array($options)) {
$item_data['Options'] = $options[$item_id];
}
// 2. store subscription information OR
if( $product->GetDBField('Type') == 2 ) // subscriptions
{
$item_data = $this->BuildSubscriptionItemData($item_id, $item_data);
}
// 3. store package information
if( $product->GetDBField('Type') == 5 ) // package
{
$package_content_ids = $product->GetPackageContentIds();
$product_package_item =& $this->Application->recallObject('p.-packageitem');
$package_item_data = array();
foreach ($package_content_ids as $package_item_id){
$product_package_item->Load($package_item_id);
$package_item_data[$package_item_id] = array();
if( $product_package_item->GetDBField('Type') == 2 ) // subscriptions
{
$package_item_data[$package_item_id] = $this->BuildSubscriptionItemData($package_item_id, $item_data);
}
}
$item_data['PackageContent'] = $product->GetPackageContentIds();
$item_data['PackageItemsItemData'] = $package_item_data;
}
$event->setEventParam('ItemData', serialize($item_data));
// 1 for PacakgeNum when in admin - temporary solution to overcome splitting into separate sub-orders
// of orders with items added through admin when approving them
- $this->AddItemToOrder($event, $item_id, $qty, $this->Application->IsAdmin() ? 1 : null);
+ $this->AddItemToOrder($event, $item_id, $qty, $this->Application->isAdminUser ? 1 : null);
}
if ($event->status == erSUCCESS && !$event->redirect) {
$event->redirect_params['pass'] = 'm';
$event->redirect_params['pass_category'] = 0; //otherwise mod-rewrite shop-cart URL will include category
$event->redirect = true;
}
else {
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
$event->redirect_params['opener'] = 'u';
}
}
}
/**
* Check if required options are selected & selected option combination is in stock
*
* @param kEvent $event
* @param Array $options
* @param int $product_id
* @param int $qty
* @param int $selection_mode
* @return bool
*/
function CheckOptions(&$event, &$options, $product_id, $qty, $selection_mode)
{
// 1. check for required options
$selection_filter = $selection_mode == 1 ? ' AND OptionType IN (1,3,6) ' : '';
$req_options = $this->Conn->GetCol('SELECT ProductOptionId FROM '.TABLE_PREFIX.'ProductOptions WHERE ProductId = '.$product_id.' AND Required = 1 '.$selection_filter);
$result = true;
foreach ($req_options as $opt_id) {
if (!getArrayValue($options, $opt_id)) {
$this->Application->SetVar('opt_error', 1); //let the template know we have an error
$result = false;
}
}
// 2. check for option combinations in stock
$comb_salt = $this->OptionsSalt($options, true);
if ($comb_salt) {
// such option combination is defined explicitly
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$sql = 'SELECT Availability
FROM '.$poc_table.'
WHERE CombinationCRC = '.$comb_salt;
$comb_availble = $this->Conn->GetOne($sql);
// 2.1. check if Availability flag is set, then
if ($comb_availble == 1) {
// 2.2. check for quantity in stock
$table = Array();
$table['poc'] = $this->Application->getUnitOption('poc', 'TableName');
$table['p'] = $this->Application->getUnitOption('p', 'TableName');
$table['oi'] = $this->TablePrefix($event).'OrderItems';
$object =& $event->getObject();
$ord_id = $object->GetID();
// 2.3. check if some amount of same combination & product are not already in shopping cart
$sql = 'SELECT '.
$table['p'].'.InventoryStatus,'.
$table['p'].'.BackOrder,
IF('.$table['p'].'.InventoryStatus = 2, '.$table['poc'].'.QtyInStock, '.$table['p'].'.QtyInStock) AS QtyInStock,
IF('.$table['oi'].'.OrderItemId IS NULL, 0, '.$table['oi'].'.Quantity) AS Quantity
FROM '.$table['p'].'
LEFT JOIN '.$table['poc'].' ON
'.$table['p'].'.ProductId = '.$table['poc'].'.ProductId
LEFT JOIN '.$table['oi'].' ON
('.$table['oi'].'.OrderId = '.$ord_id.') AND
('.$table['oi'].'.OptionsSalt = '.$comb_salt.') AND
('.$table['oi'].'.ProductId = '.$product_id.') AND
('.$table['oi'].'.BackOrderFlag = 0)
WHERE '.$table['poc'].'.CombinationCRC = '.$comb_salt;
$product_info = $this->Conn->GetRow($sql);
if ($product_info['InventoryStatus']) {
$backordering = $this->Application->ConfigValue('Comm_Enable_Backordering');
if (!$backordering || $product_info['BackOrder'] == 0) {
// backordering is not enabled generally or for this product directly, then check quantities in stock
if ($qty + $product_info['Quantity'] > $product_info['QtyInStock']) {
$this->Application->SetVar('opt_error', 2);
$result = false;
}
}
}
}
elseif ($comb_availble !== false) {
$this->Application->SetVar('opt_error', 2);
$result = false;
}
}
if ($result) {
$event->status = erSUCCESS;
- $event->redirect = $this->Application->IsAdmin() ? true : $this->Application->GetVar('shop_cart_template');
+ $event->redirect = $this->Application->isAdminUser ? true : $this->Application->GetVar('shop_cart_template');
}
else {
$event->status = erFAIL;
}
return $result;
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdateItemOptions(&$event)
{
$opt_data = $this->Application->GetVar('options');
$options = getArrayValue($opt_data, $this->Application->GetVar('p_id'));
if (!$options) {
$qty_data = $this->Application->GetVar('qty');
$comb_id = key(getArrayValue($qty_data, $this->Application->GetVar('p_id')));
$options = unserialize($this->Conn->GetOne('SELECT Combination FROM '.TABLE_PREFIX.'ProductOptionCombinations WHERE CombinationId = '.$comb_id));
}
if (!$options) return;
$ord_item =& $this->Application->recallObject('orditems.-opt', null, Array ('skip_autoload' => true));
/* @var $ord_item kDBItem */
$ord_item->Load($this->Application->GetVar('orditems_id'));
// assuming that quantity cannot be changed during order item editing
if (!$this->CheckOptions($event, $options, $ord_item->GetDBField('ProductId'), 0, $ord_item->GetDBField('OptionsSelectionMode'))) return;
$item_data = unserialize($ord_item->GetDBField('ItemData'));
$item_data['Options'] = $options;
$ord_item->SetDBField('ItemData', serialize($item_data));
$ord_item->SetDBField('OptionsSalt', $this->OptionsSalt($options));
$ord_item->Update();
$event->CallSubEvent('OnRecalculateItems');
- if ($event->status == erSUCCESS && $this->Application->IsAdmin()) {
+ if ($event->status == erSUCCESS && $this->Application->isAdminUser) {
$event->redirect_params['opener'] = 'u';
}
}
function BuildSubscriptionItemData($item_id, $item_data)
{
$products_table = $this->Application->getUnitOption('p', 'TableName');
$products_idfield = $this->Application->getUnitOption('p', 'IDField');
$sql = 'SELECT AccessGroupId FROM %s WHERE %s = %s';
$item_data['PortalGroupId'] = $this->Conn->GetOne( sprintf($sql, $products_table, $products_idfield, $item_id) );
$pricing_table = $this->Application->getUnitOption('pr', 'TableName');
$pricing_idfield = $this->Application->getUnitOption('pr', 'IDField');
// $sql = 'SELECT AccessDuration, AccessUnit, DurationType, AccessExpiration FROM %s WHERE %s = %s';
$sql = 'SELECT * FROM %s WHERE %s = %s';
$pricing_id = $this->GetPricingId($item_id, $item_data);
$item_data['PricingId'] = $pricing_id;
$pricing_info = $this->Conn->GetRow( sprintf($sql, $pricing_table, $pricing_idfield, $pricing_id ) );
$unit_secs = Array(1 => 1, 2 => 60, 3 => 3600, 4 => 86400, 5 => 604800, 6 => 2592000, 7 => 31536000);
/*
// Customization healtheconomics.org
$item_data['DurationType'] = $pricing_info['DurationType'];
$item_data['AccessExpiration'] = $pricing_info['AccessExpiration'];
// Customization healtheconomics.org --
*/
$item_data['Duration'] = $pricing_info['AccessDuration'] * $unit_secs[ $pricing_info['AccessUnit'] ];
return $item_data;
}
function OnRemoveCoupon(&$event)
{
$object =& $event->getObject();
$this->RemoveCoupon($object);
$event->CallSubEvent('OnRecalculateItems');
$event->SetRedirectParam('checkout_error', 7);
}
function RemoveCoupon(&$object)
{
$coupon_id = $object->GetDBField('CouponId');
$coupon =& $this->Application->recallObject('coup', null, Array('skip_autoload' => true));
$res = $coupon->Load($coupon_id);
$uses = $coupon->GetDBField('NumberOfUses');
if($res && isset($uses))
{
$coupon->SetDBField('NumberOfUses', $uses + 1);
$coupon->SetDBField('Status', 1);
$coupon->Update();
}
$object->SetDBField('CouponId', 0);
$object->SetDBField('CouponDiscount', 0);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAddVirtualProductToCart(&$event)
{
$l_info = $this->Application->GetVar('l');
if($l_info)
{
foreach($l_info as $link_id => $link_info) {}
$item_data['LinkId'] = $link_id;
$item_data['ListingTypeId'] = $link_info['ListingTypeId'];
}
else
{
$link_id = $this->Application->GetVar('l_id');
$sql = 'SELECT ResourceId FROM '.$this->Application->getUnitOption('l', 'TableName').'
WHERE LinkId = '.$link_id;
$sql = 'SELECT ListingTypeId FROM '.$this->Application->getUnitOption('ls', 'TableName').'
WHERE ItemResourceId = '.$this->Conn->GetOne($sql);
$item_data['LinkId'] = $link_id;
$item_data['ListingTypeId'] = $this->Conn->GetOne($sql);
}
$sql = 'SELECT VirtualProductId FROM '.$this->Application->getUnitOption('lst', 'TableName').'
WHERE ListingTypeId = '.$item_data['ListingTypeId'];
$item_id = $this->Conn->GetOne($sql);
$event->setEventParam('ItemData', serialize($item_data));
$this->AddItemToOrder($event, $item_id);
$event->redirect = $this->Application->GetVar('shop_cart_template');
// don't pass unused info to shopping cart, brokes old mod-rewrites
$event->SetRedirectParam('pass', 'm'); // not to pass link id
$event->SetRedirectParam('m_cat_id', 0); // not to pass link id
}
function OnRemoveFromCart(&$event)
{
$ord_item_id = $this->Application->GetVar('orditems_id');
$ord_id = $this->getPassedId($event);
$this->Conn->Query('DELETE FROM '.TABLE_PREFIX.'OrderItems WHERE OrderId = '.$ord_id.' AND OrderItemId = '.$ord_item_id);
$this->OnRecalculateItems($event);
}
function OnCleanupCart(&$event)
{
$object =& $event->getObject();
$sql = 'DELETE FROM '.TABLE_PREFIX.'OrderItems
WHERE OrderId = '.$this->getPassedID($event);
$this->Conn->Query($sql);
$this->RemoveCoupon($object);
$this->RemoveGiftCertificate($object);
$this->OnRecalculateItems($event);
}
/**
* Returns order id from session or last used
*
* @param kEvent $event
* @return int
*/
function getPassedId(&$event)
{
$event->setEventParam('raise_warnings', 0);
$passed = parent::getPassedID($event);
- if ( $this->Application->IsAdmin() ) {
+
+ if ($this->Application->isAdminUser) {
// work as usual in admin
return $passed;
}
if ($event->Special == 'last') {
// return last order id (for using on thank you page)
return $this->Application->RecallVar('front_order_id');
}
$ses_id = $this->Application->RecallVar( $event->getPrefixSpecial(true) . '_id' );
if ($passed && ($passed != $ses_id)) {
// order id given in url doesn't match our current order id
$sql = 'SELECT PortalUserId
FROM ' . TABLE_PREFIX . 'Orders
WHERE OrderId = ' . $passed;
$user_id = $this->Conn->GetOne($sql);
if ($user_id == $this->Application->RecallVar('user_id')) {
// current user is owner of order with given id -> allow him to view order details
return $passed;
}
else {
// current user is not owner of given order -> hacking attempt
$this->Application->SetVar($event->getPrefixSpecial().'_id', 0);
return 0;
}
}
else {
// not passed or equals to ses_id
return $ses_id > 0 ? $ses_id : FAKE_ORDER_ID; // FAKE_ORDER_ID helps to keep parent filter for order items set in "kDBList::linkToParent"
}
return $passed;
}
/**
* Load item if id is available
*
* @param kEvent $event
*/
function LoadItem(&$event)
{
$id = $this->getPassedID($event);
if ($id == FAKE_ORDER_ID) {
// if we already know, that there is no such order,
// then don't run database query, that will confirm that
$object =& $event->getObject();
/* @var $object kDBItem */
$object->Clear($id);
return ;
}
parent::LoadItem($event);
}
/**
* Creates new shopping cart
*
* @param kEvent $event
*/
function _createNewCart(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$object->SetDBField('Number', $this->getNextOrderNumber($event));
$object->SetDBField('SubNumber', 0);
$object->SetDBField('Type', ORDER_STATUS_INCOMPLETE);
$object->SetDBField('VisitId', $this->Application->RecallVar('visit_id') );
// get user
$user_id = $this->Application->RecallVar('user_id');
if (!$user_id) {
$user_id = -2; // Guest
}
$object->SetDBField('PortalUserId', $user_id);
// get affiliate
$affiliate_id = $this->isAffiliate($user_id);
if ($affiliate_id) {
$object->SetDBField('AffiliateId', $affiliate_id);
}
else {
$affiliate_storage_method = $this->Application->ConfigValue('Comm_AffiliateStorageMethod');
$object->SetDBField('AffiliateId', $affiliate_storage_method == 1 ? (int)$this->Application->RecallVar('affiliate_id') : (int)$this->Application->GetVar('affiliate_id') );
}
// get payment type
$default_type = $this->Conn->GetOne('SELECT PaymentTypeId FROM '.TABLE_PREFIX.'PaymentTypes WHERE IsPrimary = 1');
if ($default_type) {
$object->SetDBField('PaymentType', $default_type);
}
$created = $object->Create();
if ($created) {
$id = $object->GetID();
$this->Application->SetVar($event->getPrefixSpecial(true) . '_id', $id);
$this->Application->StoreVar($event->getPrefixSpecial(true) . '_id', $id);
return $id;
}
return 0;
}
function StoreContinueShoppingLink()
{
$this->Application->StoreVar('continue_shopping', 'external:'.PROTOCOL.SERVER_NAME.$this->Application->RecallVar('last_url'));
}
/**
* Sets required fields for order, based on current checkout step
* !!! Do not use switch here, since all cases may be on the same form simultaniously
*
* @param kEvent $event
*/
function SetStepRequiredFields(&$event)
{
$order =& $event->getObject();
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
// updated address available from SUBMIT -> use it
list($id, $field_values) = each($items_info);
}
else {
// no updated address -> use current address
$field_values = Array (
'ShippingCountry' => $order->GetDBField('ShippingCountry'),
'BillingCountry' => $order->GetDBField('BillingCountry'),
'PaymentType' => $order->GetDBField('PaymentType'),
);
}
// shipping address required fields
if ($this->Application->GetVar('check_shipping_address')) {
$has_tangibles = $order->HasTangibleItems();
$req_fields = array('ShippingTo', 'ShippingAddress1', 'ShippingCity', 'ShippingZip', 'ShippingCountry', 'ShippingPhone');
foreach ($req_fields as $field) {
$order->Fields[$field]['required'] = $has_tangibles;
}
if ($cs_helper->CountryHasStates( getArrayValue($field_values, 'ShippingCountry') )) {
$order->Fields['ShippingState']['required'] = true; // $has_tangibles
}
}
// billing address required fields
if ($this->Application->GetVar('check_billing_address')) {
$req_fields = array('BillingTo', 'BillingAddress1', 'BillingCity', 'BillingZip', 'BillingCountry', 'BillingPhone');
foreach ($req_fields as $field) {
$order->Fields[$field]['required'] = true;
}
if ($cs_helper->CountryHasStates( getArrayValue($field_values, 'BillingCountry') )) {
$order->Fields['BillingState']['required'] = true;
}
}
$check_cc = $this->Application->GetVar('check_credit_card');
$ord_event = $this->Application->GetVar($event->getPrefixSpecial().'_event');
- if (($ord_event !== 'OnProceedToPreview') && !$this->Application->IsAdmin()) {
+ if (($ord_event !== 'OnProceedToPreview') && !$this->Application->isAdmin) {
// don't check credit card when going from "billing info" to "order preview" step
$check_cc = 0;
}
if ($check_cc && ($field_values['PaymentType'] == $order->GetDBField('PaymentType'))) {
// cc check required AND payment type was not changed during SUBMIT
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
$req_fields = array('PaymentCardType', 'PaymentAccount', 'PaymentNameOnCard', 'PaymentCCExpDate');
}
else {
$req_fields = array('PaymentCardType', 'PaymentAccount', 'PaymentNameOnCard', 'PaymentCCExpDate', 'PaymentCVV2');
}
foreach ($req_fields as $field) {
$order->Fields[$field]['required'] = true;
}
}
}
/**
* Set's order's user_id to user from session or Guest otherwise
*
* @param kEvent $event
*/
function CheckUser(&$event)
{
- if ( $this->Application->IsAdmin() ) return;
+ if ($this->Application->isAdminUser) {
+ return;
+ }
+
$order =& $event->GetObject();
$ses_user = $this->Application->RecallVar('user_id');
if ($order->GetDBField('PortalUserId') != $ses_user) {
if ($ses_user == 0) $ses_user = -2; //Guest
$order->SetDBField('PortalUserId', $ses_user);
// since CheckUser is called in OnBeforeItemUpdate, we don't need to call udpate here, just set the field
}
}
/* ======================== ADMIN ONLY ======================== */
/**
* Prepare temp tables and populate it
* with items selected in the grid
*
* @param kEvent $event
*/
function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
$new_number = $this->getNextOrderNumber($event);
$object->SetDBField('Number', $new_number);
$object->SetDBField('SubNumber', 0);
$object->SetDBField('OrderIP', $_SERVER['REMOTE_ADDR']);
$order_type = $this->getTypeBySpecial( $this->Application->GetVar('order_type') );
$object->SetDBField('Status', $order_type);
}
/**
* When cloning orders set new order number to them
*
* @param kEvent $event
*/
function OnBeforeClone(&$event)
{
$object =& $event->getObject();
if (substr($event->Special, 0, 9) == 'recurring') {
$object->SetDBField('SubNumber', $object->getNextSubNumber());
$object->SetDBField('OriginalAmount', 0); // needed in this case ?
}
else {
$new_number = $this->getNextOrderNumber($event);
$object->SetDBField('Number', $new_number);
$object->SetDBField('SubNumber', 0);
$object->SetDBField('OriginalAmount', 0);
}
$object->SetDBField('OrderDate', adodb_mktime());
$object->UpdateFormattersSubFields();
$object->SetDBField('GWResult1', '');
$object->SetDBField('GWResult2', '');
}
function OnReserveItems(&$event)
{
$order_items =& $this->Application->recallObject('orditems.-inv','orditems_List',Array('skip_counting'=>true,'per_page'=>-1) );
$order_items->linkToParent('-inv');
// force re-query, since we are updateing through orditem ITEM, not the list, and
// OnReserveItems may be called 2 times when fullfilling backorders through product edit - first time
// from FullFillBackorders and second time from OnOrderProcess
$order_items->Query(true);
$order_items->GoFirst();
// query all combinations used in this order
$product_object =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
$product_object->SwitchToLive();
$order_item =& $this->Application->recallObject('orditems.-item', null, Array('skip_autoload' => true));
$combination_item =& $this->Application->recallObject('poc.-item', null, Array('skip_autoload' => true));
$combinations = $this->queryCombinations($order_items);
$event->status = erSUCCESS;
while (!$order_items->EOL()) {
$rec = $order_items->getCurrentRecord();
$product_object->Load( $rec['ProductId'] );
if (!$product_object->GetDBField('InventoryStatus')) {
$order_items->GoNext();
continue;
}
$inv_object =& $this->getInventoryObject($product_object, $combination_item, $combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ]);
$lack = $rec['Quantity'] - $rec['QuantityReserved'];
if ($lack > 0) {
// reserve lack or what is available (in case if we need to reserve anything, by Alex)
$to_reserve = min($lack, $inv_object->GetDBField('QtyInStock') - $product_object->GetDBField('QtyInStockMin'));
if ($to_reserve < $lack) $event->status = erFAIL; // if we can't reserve the full lack
//reserve in order
$order_item->SetDBFieldsFromHash($rec);
$order_item->SetDBField('QuantityReserved', $rec['QuantityReserved'] + $to_reserve);
$order_item->SetId($rec['OrderItemId']);
$order_item->Update();
//update product - increase reserved, decrease in stock
$inv_object->SetDBField('QtyReserved', $inv_object->GetDBField('QtyReserved') + $to_reserve);
$inv_object->SetDBField('QtyInStock', $inv_object->GetDBField('QtyInStock') - $to_reserve);
$inv_object->SetDBField('QtyBackOrdered', $inv_object->GetDBField('QtyBackOrdered') - $to_reserve);
$inv_object->Update();
if ($product_object->GetDBField('InventoryStatus') == 2) {
// inventory by options, then restore changed combination values back to common $combinations array !!!
$combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ] = $inv_object->FieldValues;
}
}
$order_items->GoNext();
}
return true;
}
function OnOrderPrint(&$event)
{
$event->redirect_params = Array('opener'=>'s');
}
/**
* Processes order each tab info resetting to other tab info / to user info
*
* @param kEvent $event
* @access public
*/
function OnResetAddress(&$event)
{
$to_tab = $this->Application->GetVar('to_tab');
$from_tab = substr($event->Name,strlen('OnResetTo'));
// load values from db
$object =& $event->getObject();
// update values from submit
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info) $field_values = array_shift($items_info);
$object->SetFieldsFromHash($field_values);
$this->DoResetAddress($object, $from_tab, $to_tab);
$object->Update();
$event->redirect = false;
}
/**
* Processes item selection from popup item selector
*
* @todo Is this called ? (by Alex)
* @param kEvent $event
*/
function OnProcessSelected(&$event)
{
$selected_ids = $this->Application->GetVar('selected_ids');
$product_ids = $selected_ids['p'];
if ($product_ids) {
$product_ids = explode(',', $product_ids);
// !!! LOOK OUT - Adding items to Order in admin is handled in order_ITEMS_event_handler !!!
foreach ($product_ids as $product_id) {
$this->AddItemToOrder($event, $product_id);
}
}
$this->finalizePopup($event);
}
function OnMassPlaceOrder(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$ids = $this->StoreSelectedIDs($event);
if($ids)
{
foreach($ids as $id)
{
$object->Load($id);
$this->DoPlaceOrder($event);
}
}
$event->status = erSUCCESS;
}
/**
* Universal
* Checks if QtyInStock is enough to fullfill backorder (Qty - QtyReserved in order)
*
* @param int $ord_id
* @return bool
*/
function ReadyToProcess($ord_id)
{
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' SELECT SUM(IF( IF('.TABLE_PREFIX.'Products.InventoryStatus = 2, '.$poc_table.'.QtyInStock, '.TABLE_PREFIX.'Products.QtyInStock) - '.TABLE_PREFIX.'Products.QtyInStockMin >= ('.TABLE_PREFIX.'OrderItems.Quantity - '.TABLE_PREFIX.'OrderItems.QuantityReserved), 0, 1))
FROM '.TABLE_PREFIX.'OrderItems
LEFT JOIN '.TABLE_PREFIX.'Products ON '.TABLE_PREFIX.'Products.ProductId = '.TABLE_PREFIX.'OrderItems.ProductId
LEFT JOIN '.$poc_table.' ON ('.$poc_table.'.CombinationCRC = '.TABLE_PREFIX.'OrderItems.OptionsSalt) AND ('.$poc_table.'.ProductId = '.TABLE_PREFIX.'OrderItems.ProductId)
WHERE OrderId = '.$ord_id.'
GROUP BY OrderId';
// IF (IF(InventoryStatus = 2, poc.QtyInStock, p.QtyInStock) - QtyInStockMin >= (Quantity - QuantityReserved), 0, 1
return ($this->Conn->GetOne($query) == 0);
}
/**
* Return all option combinations used in order
*
* @param kDBList $order_items
* @return Array
*/
function queryCombinations(&$order_items)
{
// 1. collect combination crc used in order
$combinations = Array();
while (!$order_items->EOL()) {
$row = $order_items->getCurrentRecord();
if ($row['OptionsSalt'] == 0) {
$order_items->GoNext();
continue;
}
$combinations[] = '(poc.ProductId = '.$row['ProductId'].') AND (poc.CombinationCRC = '.$row['OptionsSalt'].')';
$order_items->GoNext();
}
$order_items->GoFirst();
$combinations = array_unique($combinations); // if same combination+product found as backorder & normal order item
if ($combinations) {
// 2. query data about combinations
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$sql = 'SELECT CONCAT(poc.ProductId, "_", poc.CombinationCRC) AS CombinationKey, poc.*
FROM '.$poc_table.' poc
WHERE ('.implode(') OR (', $combinations).')';
return $this->Conn->Query($sql, 'CombinationKey');
}
return Array();
}
/**
* Returns object to perform inventory actions on
*
* @param ProductsItem $product current product object in order
* @param kDBItem $combination combination dummy object
* @param Array $combination_data pre-queried combination data
* @return kDBItem
*/
function &getInventoryObject(&$product, &$combination, $combination_data)
{
if ($product->GetDBField('InventoryStatus') == 2) {
// inventory by option combinations
$combination->SetDBFieldsFromHash($combination_data);
$combination->setID($combination_data['CombinationId']);
$change_item =& $combination;
}
else {
// inventory by product ifself
$change_item =& $product;
}
return $change_item;
}
/**
* Approve order ("Pending" tab)
*
* @param kDBList $order_items
* @return int new status of order if any
*/
function approveOrder(&$order_items)
{
$product_object =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
$order_item =& $this->Application->recallObject('orditems.-item', null, Array('skip_autoload' => true));
$combination_item =& $this->Application->recallObject('poc.-item', null, Array('skip_autoload' => true));
$combinations = $this->queryCombinations($order_items);
while (!$order_items->EOL()) {
$rec = $order_items->getCurrentRecord();
$order_item->SetDBFieldsFromHash($rec);
$order_item->SetId($rec['OrderItemId']);
$order_item->SetDBField('QuantityReserved', 0);
$order_item->Update();
$product_object->Load( $rec['ProductId'] );
if (!$product_object->GetDBField('InventoryStatus')) {
// if no inventory info is collected, then skip this order item
$order_items->GoNext();
continue;
}
$inv_object =& $this->getInventoryObject($product_object, $combination_item, $combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ]);
// decrease QtyReserved by amount of product used in order
$inv_object->SetDBField('QtyReserved', $inv_object->GetDBField('QtyReserved') - $rec['Quantity']);
$inv_object->Update();
if ($product_object->GetDBField('InventoryStatus') == 2) {
// inventory by options, then restore changed combination values back to common $combinations array !!!
$combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ] = $inv_object->FieldValues;
}
$order_items->GoNext();
}
return true;
}
function restoreOrder(&$order_items)
{
$product_object =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
$product_object->SwitchToLive();
$order_item =& $this->Application->recallObject('orditems.-item', null, Array('skip_autoload' => true));
$combination_item =& $this->Application->recallObject('poc.-item', null, Array('skip_autoload' => true));
$combinations = $this->queryCombinations($order_items);
while( !$order_items->EOL() )
{
$rec = $order_items->getCurrentRecord();
$product_object->Load( $rec['ProductId'] );
if (!$product_object->GetDBField('InventoryStatus')) {
// if no inventory info is collected, then skip this order item
$order_items->GoNext();
continue;
}
$inv_object =& $this->getInventoryObject($product_object, $combination_item, $combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ]);
// cancelling backorderd qty if any
$lack = $rec['Quantity'] - $rec['QuantityReserved'];
if ($lack > 0 && $rec['BackOrderFlag'] > 0) { // lack should have been recorded as QtyBackOrdered
$inv_object->SetDBField('QtyBackOrdered', $inv_object->GetDBField('QtyBackOrdered') - $lack);
}
// canceling reservation in stock
$inv_object->SetDBField('QtyReserved', $inv_object->GetDBField('QtyReserved') - $rec['QuantityReserved']);
// putting remaining freed qty back to stock
$inv_object->SetDBField('QtyInStock', $inv_object->GetDBField('QtyInStock') + $rec['QuantityReserved']);
$inv_object->Update();
$product_h =& $this->Application->recallObject('p_EventHandler');
if ($product_object->GetDBField('InventoryStatus') == 2) {
// inventory by options, then restore changed combination values back to common $combinations array !!!
$combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ] = $inv_object->FieldValues;
// using freed qty to fullfill possible backorders
$product_h->FullfillBackOrders($product_object, $inv_object->GetID());
}
else {
// using freed qty to fullfill possible backorders
$product_h->FullfillBackOrders($product_object, 0);
}
$order_item->SetDBFieldsFromHash($rec);
$order_item->SetId($rec['OrderItemId']);
$order_item->SetDBField('QuantityReserved', 0);
$order_item->Update();
$order_items->GoNext();
}
return true;
}
/**
* Approve order + special processing
*
* @param kEvent $event
*/
function MassInventoryAction(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
// process order products
$object =& $this->Application->recallObject($event->Prefix.'.-inv', null, Array('skip_autoload' => true));
$ids = $this->StoreSelectedIDs($event);
if($ids)
{
foreach($ids as $id)
{
$object->Load($id);
$this->InventoryAction($event);
}
}
}
function InventoryAction(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$event_status_map = Array(
'OnMassOrderApprove' => ORDER_STATUS_TOSHIP,
'OnOrderApprove' => ORDER_STATUS_TOSHIP,
'OnMassOrderDeny' => ORDER_STATUS_DENIED,
'OnOrderDeny' => ORDER_STATUS_DENIED,
'OnMassOrderArchive' => ORDER_STATUS_ARCHIVED,
'OnOrderArchive' => ORDER_STATUS_ARCHIVED,
'OnMassOrderShip' => ORDER_STATUS_PROCESSED,
'OnOrderShip' => ORDER_STATUS_PROCESSED,
'OnMassOrderProcess' => ORDER_STATUS_TOSHIP,
'OnOrderProcess' => ORDER_STATUS_TOSHIP,
);
$order_items =& $this->Application->recallObject('orditems.-inv','orditems_List',Array('skip_counting'=>true,'per_page'=>-1) );
$order_items->linkToParent('-inv');
$order_items->Query();
$order_items->GoFirst();
$object =& $this->Application->recallObject($event->Prefix.'.-inv');
/* @var $object OrdersItem */
if ($object->GetDBField('OnHold')) {
// any actions have no effect while on hold
return ;
}
// preparing new status, but not setting it yet
$object->SetDBField('Status', $event_status_map[$event->Name]);
$set_new_status = false;
$event->status = erSUCCESS;
$email_params = $this->OrderEmailParams($object);
switch ($event->Name) {
case 'OnMassOrderApprove':
case 'OnOrderApprove':
$set_new_status = false; //on succsessfull approve order will be split and new orders will have new statuses
if ($object->GetDBField('ChargeOnNextApprove')) {
$charge_info = $this->ChargeOrder($object);
if (!$charge_info['result']) {
break;
}
// removing ChargeOnNextApprove
$object->SetDBField('ChargeOnNextApprove', 0);
$sql = 'UPDATE '.$object->TableName.' SET ChargeOnNextApprove = 0 WHERE '.$object->IDField.' = '.$object->GetID();
$this->Conn->Query($sql);
}
// charge user for order in case if we user 2step charging (e.g. AUTH_ONLY + PRIOR_AUTH_CAPTURE)
$gw_data = $object->getGatewayData();
$this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] );
$gateway_object =& $this->Application->recallObject( $gw_data['ClassName'] );
$charge_result = $gateway_object->Charge($object->FieldValues, $gw_data['gw_params']);
$sql = 'UPDATE %s SET GWResult2 = %s WHERE %s = %s';
$sql = sprintf($sql, $object->TableName, $this->Conn->qstr($gateway_object->getGWResponce()), $object->IDField, $object->GetID() );
$this->Conn->Query($sql);
$object->SetDBField('GWResult2', $gateway_object->getGWResponce() );
if ($charge_result) {
$product_object =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
foreach ($order_items->Records as $product_item) {
if (!$product_item['ProductId']) {
// product may have been deleted
continue;
}
$product_object->Load($product_item['ProductId']);
$hits = floor( $product_object->GetDBField('Hits') ) + 1;
$sql = 'SELECT MAX(Hits) FROM '.$this->Application->getUnitOption('p', 'TableName').'
WHERE FLOOR(Hits) = '.$hits;
$hits = ( $res = $this->Conn->GetOne($sql) ) ? $res + 0.000001 : $hits;
$product_object->SetDBField('Hits', $hits);
$product_object->Update();
/*$sql = 'UPDATE '.$this->Application->getUnitOption('p', 'TableName').'
SET Hits = Hits + '.$product_item['Quantity'].'
WHERE ProductId = '.$product_item['ProductId'];
$this->Conn->Query($sql);*/
}
$this->PrepareCoupons($event, $object);
$this->SplitOrder($event, $object);
if ($object->GetDBField('IsRecurringBilling') != 1) {
$email_event_user =& $this->Application->EmailEventUser('ORDER.APPROVE', $object->GetDBField('PortalUserId'), $email_params);
// Mask credit card with XXXX
if ($this->Application->ConfigValue('Comm_MaskProcessedCreditCards')) {
$this->maskCreditCard($object, 'PaymentAccount');
$set_new_status = 1;
}
}
}
break;
case 'OnMassOrderDeny':
case 'OnOrderDeny':
foreach ($order_items->Records as $product_item) {
if (!$product_item['ProductId']) {
// product may have been deleted
continue;
}
$this->raiseProductEvent('Deny', $product_item['ProductId'], $product_item);
}
$email_event_user =& $this->Application->EmailEventUser('ORDER.DENY', $object->GetDBField('PortalUserId'), $email_params);
if ($event->Name == 'OnMassOrderDeny' || $event->Name == 'OnOrderDeny') {
// inform payment gateway that order was declined
$gw_data = $object->getGatewayData();
$this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] );
$gateway_object =& $this->Application->recallObject( $gw_data['ClassName'] );
$gateway_object->OrderDeclined($object->FieldValues, $gw_data['gw_params']);
}
// !!! LOOK HERE !!!
// !!!! no break !!!! here on purpose!!!
case 'OnMassOrderArchive':
case 'OnOrderArchive':
// it's critical to update status BEFORE processing items because
// FullfillBackorders could be called during processing and in case
// of order denial/archive fullfill could reserve the qtys back for current backorder
$object->Update();
$this->restoreOrder($order_items);
$set_new_status = false; // already set
break;
case 'OnMassOrderShip':
case 'OnOrderShip':
$ret = Array();
// try to create usps order
if ( $object->GetDBField('ShippingType') == 0 && strpos($object->GetDBField('ShippingInfo'), 'USPS')) {
$ses_usps_erros = Array();
$ret = $this->MakeUSPSOrder($object);
}
if ( !array_key_exists('error_number', $ret) ) {
$set_new_status = $this->approveOrder($order_items);
// $set_new_status = $this->shipOrder($order_items);
$object->SetDBField('ShippingDate', adodb_mktime());
$object->UpdateFormattersSubFields();
$shipping_email = $object->GetDBField('ShippingEmail');
$email_params['to_email'] = $shipping_email ? $shipping_email : $email_params['_user_email'];
$email_event_user =& $this->Application->EmailEventUser('ORDER.SHIP', $object->GetDBField('PortalUserId'), $email_params);
// inform payment gateway that order was shipped
$gw_data = $object->getGatewayData();
$this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] );
$gateway_object =& $this->Application->recallObject( $gw_data['ClassName'] );
$gateway_object->OrderShipped($object->FieldValues, $gw_data['gw_params']);
}
else {
$usps_errors[$object->GetField('OrderNumber')] = $ret['error_description'];
$ses_usps_erros = Array();
$ses_usps_erros = unserialize($this->Application->RecallVar('usps_errors'));
if ( is_array($ses_usps_erros) ) {
$usps_errors = array_merge($usps_errors, $ses_usps_erros);
}
$this->Application->StoreVar('usps_errors', serialize($usps_errors));
}
break;
case 'OnMassOrderProcess':
case 'OnOrderProcess':
if ($this->ReadyToProcess($object->GetID())) {
$event->CallSubEvent('OnReserveItems');
if ($event->status == erSUCCESS) $set_new_status = true;
$email_event_user =& $this->Application->EmailEventUser('BACKORDER.PROCESS', $object->GetDBField('PortalUserId'), $email_params);
} else {
$event->status = erFAIL;
}
break;
}
if ($set_new_status) {
$object->Update();
}
}
/**
* Hides last 4 digits from credit card number
*
* @param OrdersItem $object
* @param string $field
*/
function maskCreditCard(&$object, $field)
{
$value = $object->GetDBField($field);
$value = preg_replace('/'.substr($value, -4).'$/', str_repeat('X', 4), $value);
$object->SetDBField($field, $value);
}
/**
* Get next free order number
*
* @param kEvent $event
*/
function getNextOrderNumber(&$event)
{
$object =& $event->getObject();
$sql = 'SELECT MAX(Number)
FROM ' . $this->Application->GetLiveName($object->TableName);
return max($this->Conn->GetOne($sql) + 1, $this->Application->ConfigValue('Comm_Next_Order_Number'));
}
/**
* Set's new order address based on another address from order (e.g. billing from shipping)
*
* @param unknown_type $object
* @param unknown_type $from
* @param unknown_type $to
*/
function DoResetAddress(&$object, $from, $to)
{
$fields = Array('To','Company','Phone','Fax','Email','Address1','Address2','City','State','Zip','Country');
if ($from == 'User') {
// skip theese fields when coping from user, because they are not present in user profile
$tmp_fields = array_flip($fields);
// unset($tmp_fields['Company'], $tmp_fields['Fax'], $tmp_fields['Address2']);
$fields = array_flip($tmp_fields);
}
// apply modification
foreach ($fields as $field_name) {
$object->SetDBField($to.$field_name, $object->GetDBField($from.$field_name));
}
}
/**
* Set's additional view filters set from "Orders" => "Search" tab
*
* @param kEvent $event
*/
function AddFilters(&$event)
{
parent::AddFilters($event);
if($event->Special != 'search') return true;
$search_filter = $this->Application->RecallVar('ord.search_search_filter');
if(!$search_filter) return false;
$search_filter = unserialize($search_filter);
$event->setPseudoClass('_List');
$object =& $event->getObject();
foreach($search_filter as $filter_name => $filter_params)
{
$filter_type = $filter_params['type'] == 'where' ? WHERE_FILTER : HAVING_FILTER;
$object->addFilter($filter_name, $filter_params['value'], $filter_type, FLT_VIEW);
}
}
/**
* Set's status incomplete to all cloned orders
*
* @param kEvent $event
*/
function OnAfterClone(&$event)
{
$id = $event->getEventParam('id');
$table = $this->Application->getUnitOption($event->Prefix,'TableName');
$id_field = $this->Application->getUnitOption($event->Prefix,'IDField');
// set cloned order status to Incomplete
$sql = 'UPDATE '.$table.' SET Status = 0 WHERE '.$id_field.' = '.$id;
$this->Conn->Query($sql);
}
/* ======================== COMMON CODE ======================== */
/**
* Split one timestamp field into 2 virtual fields
*
* @param kEvent $event
* @access public
*/
function OnAfterItemLoad(&$event)
{
// get user fields
$object =& $event->getObject();
$user_id = $object->GetDBField('PortalUserId');
if($user_id)
{
$user_info = $this->Conn->GetRow('SELECT *, CONCAT(FirstName,\' \',LastName) AS UserTo FROM '.TABLE_PREFIX.'PortalUser WHERE PortalUserId = '.$user_id);
$fields = Array('UserTo'=>'UserTo','UserPhone'=>'Phone','UserFax'=>'Fax','UserEmail'=>'Email',
'UserAddress1'=>'Street','UserAddress2'=>'Street2','UserCity'=>'City','UserState'=>'State',
'UserZip'=>'Zip','UserCountry'=>'Country','UserCompany'=>'Company');
foreach($fields as $object_field => $user_field)
{
$object->SetDBField($object_field,$user_info[$user_field]);
}
}
$object->SetDBField('PaymentCVV2', $this->Application->RecallVar('CVV2Code') );
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->PopulateStates($event, 'ShippingState', 'ShippingCountry');
$cs_helper->PopulateStates($event, 'BillingState', 'BillingCountry');
$this->SetStepRequiredFields($event);
// needed in OnAfterItemUpdate
$this->Application->SetVar('OriginalShippingOption', $object->GetDBField('ShippingOption'));
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnBeforeItemUpdate(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->PopulateStates($event, 'ShippingState', 'ShippingCountry');
$cs_helper->PopulateStates($event, 'BillingState', 'BillingCountry');
$object = &$event->getObject();
if ($object->GetDBField('Status') > ORDER_STATUS_PENDING) return;
$this->CheckUser($event);
if(!$object->GetDBField('OrderIP'))
{
$object->SetDBField('OrderIP', $_SERVER['REMOTE_ADDR']);
}
$shipping_option = $this->Application->GetVar('OriginalShippingOption');
$new_shipping_option = $object->GetDBField('ShippingOption');
if ($shipping_option != $new_shipping_option) {
$this->UpdateShippingOption($event);
}
else {
$this->UpdateShippingTypes($event);
}
$this->RecalculateProcessingFee($event);
$this->UpdateShippingTotal($event);
$this->RecalculateGift($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
$types = $event->getEventParam('types');
if($types == 'myorders' || $types == 'myrecentorders')
{
$user_id = $this->Application->RecallVar('user_id');
$object->addFilter('myitems_user1','%1$s.PortalUserId = '.$user_id);
$object->addFilter('myitems_user2','%1$s.PortalUserId > 0');
$object->addFilter('Status','%1$s.Status != 0');
}
else if ($event->Special == 'returns') {
// $object->addFilter('returns_filter',TABLE_PREFIX.'Orders.Status = '.ORDER_STATUS_PROCESSED.' AND (
// SELECT SUM(ReturnType)
// FROM '.TABLE_PREFIX.'OrderItems oi
// WHERE oi.OrderId = '.TABLE_PREFIX.'Orders.OrderId
// ) > 0');
$object->addFilter('returns_filter',TABLE_PREFIX.'Orders.Status = '.ORDER_STATUS_PROCESSED.' AND '.TABLE_PREFIX.'Orders.ReturnTotal > 0');
}
else if ($event->Special == 'user') {
$user_id = $this->Application->GetVar('u_id');
$object->addFilter('user_filter','%1$s.PortalUserId = '.$user_id);
}
else {
$special = $event->Special ? $event->Special : $this->Application->GetVar('order_type');
if ($special != 'search') {
// don't filter out orders by special in case of search tab
$object->addFilter( 'status_filter', '%1$s.Status='.$this->getTypeBySpecial($special) );
}
if ( $event->getEventParam('selected_only') ) {
$ids = $this->StoreSelectedIDs($event);
$object->addFilter( 'selected_filter', '%1$s.OrderId IN ('.implode(',', $ids).')');
}
}
}
function getTypeBySpecial($special)
{
$special2type = Array('incomplete'=>0,'pending'=>1,'backorders'=>2,'toship'=>3,'processed'=>4,'denied'=>5,'archived'=>6);
return $special2type[$special];
}
function getSpecialByType($type)
{
$type2special = Array(0=>'incomplete',1=>'pending',2=>'backorders',3=>'toship',4=>'processed',5=>'denied',6=>'archived');
return $type2special[$type];
}
function LockTables(&$event)
{
$read = Array();
$write_lock = '';
$read_lock = '';
$write = Array('Orders','OrderItems','Products');
foreach ($write as $tbl) {
$write_lock .= TABLE_PREFIX.$tbl.' WRITE,';
}
foreach ($read as $tbl) {
$read_lock .= TABLE_PREFIX.$tbl.' READ,';
}
$write_lock = rtrim($write_lock, ',');
$read_lock = rtrim($read_lock, ',');
$lock = trim($read_lock.','.$write_lock, ',');
//$this->Conn->Query('LOCK TABLES '.$lock);
}
/**
* Checks shopping cart products quantities
*
* @param kEvent $event
* @return bool
*/
function CheckQuantites(&$event)
{
if ($this->OnRecalculateItems($event)) { // if something has changed in the order
- if ( $this->Application->IsAdmin() )
- {
+ if ($this->Application->isAdminUser) {
if ($this->UseTempTables($event)) {
$event->redirect = 'in-commerce/orders/orders_edit_items';
}
}
- else
- {
+ else {
$event->redirect = $this->Application->GetVar('viewcart_template');
}
+
return false;
}
+
return true;
}
function DoPlaceOrder(&$event)
{
$order =& $event->getObject();
$table_prefix = $this->TablePrefix($event);
$this->LockTables($event);
if (!$this->CheckQuantites($event)) return false;
//everything is fine - we could reserve items
$this->ReserveItems($event);
$this->SplitOrder($event, $order);
return true;
}
function &queryOrderItems(&$event, $table_prefix)
{
$order =& $event->getObject();
$ord_id = $order->GetId();
// TABLE_PREFIX and $table_prefix are NOT the same !!!
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' SELECT
BackOrderFlag, '.
$table_prefix.'OrderItems.OrderItemId, '.
$table_prefix.'OrderItems.Quantity, '.
$table_prefix.'OrderItems.QuantityReserved,
IF('.TABLE_PREFIX.'Products.InventoryStatus = 2, '.$poc_table.'.QtyInStock, '.TABLE_PREFIX.'Products.QtyInStock) AS QtyInStock, '.
TABLE_PREFIX.'Products.QtyInStockMin, '.
$table_prefix.'OrderItems.ProductId, '.
TABLE_PREFIX.'Products.InventoryStatus,'.
$table_prefix.'OrderItems.OptionsSalt AS CombinationCRC
FROM '.$table_prefix.'OrderItems
LEFT JOIN '.TABLE_PREFIX.'Products ON '.TABLE_PREFIX.'Products.ProductId = '.$table_prefix.'OrderItems.ProductId
LEFT JOIN '.$poc_table.' ON ('.$poc_table.'.CombinationCRC = '.$table_prefix.'OrderItems.OptionsSalt) AND ('.$poc_table.'.ProductId = '.$table_prefix.'OrderItems.ProductId)
WHERE OrderId = '.$ord_id.' AND '.TABLE_PREFIX.'Products.Type = 1
ORDER BY BackOrderFlag ASC';
$items = $this->Conn->Query($query);
return $items;
}
function ReserveItems(&$event)
{
$table_prefix = $this->TablePrefix($event);
$items =& $this->queryOrderItems($event, $table_prefix);
foreach ($items as $an_item) {
if (!$an_item['InventoryStatus']) {
$to_reserve = $an_item['Quantity'] - $an_item['QuantityReserved'];
}
else {
if ($an_item['BackOrderFlag'] > 0) { // we don't need to reserve if it's backordered item
$to_reserve = 0;
}
else {
$to_reserve = min($an_item['Quantity']-$an_item['QuantityReserved'], $an_item['QtyInStock']-$an_item['QtyInStockMin']); //it should be equal, but just in case
}
$to_backorder = $an_item['BackOrderFlag'] > 0 ? $an_item['Quantity']-$an_item['QuantityReserved'] : 0;
}
if ($to_backorder < 0) $to_backorder = 0; //just in case
$query = ' UPDATE '.$table_prefix.'OrderItems
SET QuantityReserved = IF(QuantityReserved IS NULL, '.$to_reserve.', QuantityReserved + '.$to_reserve.')
WHERE OrderItemId = '.$an_item['OrderItemId'];
$this->Conn->Query($query);
if (!$an_item['InventoryStatus']) continue;
$update_clause = ' QtyInStock = QtyInStock - '.$to_reserve.',
QtyReserved = QtyReserved + '.$to_reserve.',
QtyBackOrdered = QtyBackOrdered + '.$to_backorder;
if ($an_item['InventoryStatus'] == 1) {
// inventory by product, then update it's quantities
$query = ' UPDATE '.TABLE_PREFIX.'Products
SET '.$update_clause.'
WHERE ProductId = '.$an_item['ProductId'];
}
else {
// inventory = 2 -> by product option combinations
$poc_idfield = $this->Application->getUnitOption('poc', 'IDField');
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' UPDATE '.$poc_table.'
SET '.$update_clause.'
WHERE (ProductId = '.$an_item['ProductId'].') AND (CombinationCRC = '.$an_item['CombinationCRC'].')';
}
$this->Conn->Query($query);
}
}
function FreeItems(&$event)
{
$table_prefix = $this->TablePrefix($event);
$items =& $this->queryOrderItems($event, $table_prefix);
foreach ($items as $an_item) {
$to_free = $an_item['QuantityReserved'];
if ($an_item['InventoryStatus']) {
if ($an_item['BackOrderFlag'] > 0) { // we don't need to free if it's backordered item
$to_free = 0;
}
// what's not reserved goes to backorder in stock for orderitems marked with BackOrderFlag
$to_backorder_free = $an_item['BackOrderFlag'] > 0 ? $an_item['Quantity'] - $an_item['QuantityReserved'] : 0;
if ($to_backorder_free < 0) $to_backorder_free = 0; //just in case
$update_clause = ' QtyInStock = QtyInStock + '.$to_free.',
QtyReserved = QtyReserved - '.$to_free.',
QtyBackOrdered = QtyBackOrdered - '.$to_backorder_free;
if ($an_item['InventoryStatus'] == 1) {
// inventory by product
$query = ' UPDATE '.TABLE_PREFIX.'Products
SET '.$update_clause.'
WHERE ProductId = '.$an_item['ProductId'];
}
else {
// inventory by option combinations
$poc_idfield = $this->Application->getUnitOption('poc', 'IDField');
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' UPDATE '.$poc_table.'
SET '.$update_clause.'
WHERE (ProductId = '.$an_item['ProductId'].') AND (CombinationCRC = '.$an_item['CombinationCRC'].')';
}
$this->Conn->Query($query);
}
$query = ' UPDATE '.$table_prefix.'OrderItems
SET QuantityReserved = IF(QuantityReserved IS NULL, 0, QuantityReserved - '.$to_free.')
WHERE OrderItemId = '.$an_item['OrderItemId'];
$this->Conn->Query($query);
}
}
/**
* Enter description here...
*
* @param kEvent $event
* @param OrdersItem $object
*/
function SplitOrder(&$event, &$object)
{
$affiliate_event = new kEvent('affil:OnOrderApprove');
$affiliate_event->setEventParam('Order_PrefixSpecial', $object->getPrefixSpecial() );
$this->Application->HandleEvent($affiliate_event);
$table_prefix = $this->TablePrefix($event);
$order =& $object;
$ord_id = $order->GetId();
$shipping_option = $order->GetDBField('ShippingOption');
$backorder_select = $shipping_option == 0 ? '0 As BackOrderFlag' : 'BackOrderFlag';
// setting PackageNum to 0 for Non-tangible items, for tangibles first package num is always 1
$query = ' SELECT OrderItemId
FROM '.$table_prefix.'OrderItems
LEFT JOIN '.TABLE_PREFIX.'Products
ON '.TABLE_PREFIX.'Products.ProductId = '.$table_prefix.'OrderItems.ProductId
WHERE '.TABLE_PREFIX.'Products.Type > 1 AND OrderId = '.$ord_id;
$non_tangibles = $this->Conn->GetCol($query);
if ($non_tangibles) {
$query = 'UPDATE '.$table_prefix.'OrderItems SET PackageNum = 0 WHERE OrderItemId IN ('.implode(',', $non_tangibles).')';
$this->Conn->Query($query);
}
// grouping_data:
// 0 => Product Type
// 1 => if NOT tangibale and NOT downloadable - OrderItemId,
// 2 => ProductId
// 3 => Shipping PackageNum
$query = 'SELECT
'.$backorder_select.',
PackageNum,
ProductName,
ShippingTypeId,
CONCAT('.TABLE_PREFIX.'Products.Type,
"_",
IF ('.TABLE_PREFIX.'Products.Type NOT IN ('.PRODUCT_TYPE_DOWNLOADABLE.','.PRODUCT_TYPE_TANGIBLE.'),
CONCAT(OrderItemId, "_", '.TABLE_PREFIX.'Products.ProductId),
""),
"_",
PackageNum
) AS Grouping,
SUM(Quantity) AS TotalItems,
SUM('.$table_prefix.'OrderItems.Weight*Quantity) AS TotalWeight,
SUM(Price * Quantity) AS TotalAmount,
SUM(QuantityReserved) AS TotalReserved,
'.TABLE_PREFIX.'Products.Type AS ProductType
FROM '.$table_prefix.'OrderItems
LEFT JOIN '.TABLE_PREFIX.'Products
ON '.TABLE_PREFIX.'Products.ProductId = '.$table_prefix.'OrderItems.ProductId
WHERE OrderId = '.$ord_id.'
GROUP BY BackOrderFlag, Grouping
ORDER BY BackOrderFlag ASC, PackageNum ASC, ProductType ASC';
$sub_orders = $this->Conn->Query($query);
$processed_sub_orders = Array();
// in case of recurring billing this will not be 0 as usual
//$first_sub_number = ($event->Special == 'recurring') ? $object->getNextSubNumber() - 1 : 0;
$first_sub_number = $object->GetDBField('SubNumber');
$next_sub_number = $first_sub_number;
$group = 1;
$order_has_gift = $order->GetDBField('GiftCertificateDiscount') > 0 ? 1 : 0;
$skip_types = Array (PRODUCT_TYPE_TANGIBLE, PRODUCT_TYPE_DOWNLOADABLE);
foreach ($sub_orders as $sub_order_data) {
$sub_order =& $this->Application->recallObject('ord.-sub'.$next_sub_number, 'ord');
/* @var $sub_order OrdersItem */
if ($this->UseTempTables($event) && $next_sub_number == 0) {
$sub_order =& $order;
}
$sub_order->SetDBFieldsFromHash($order->FieldValues);
$sub_order->SetDBField('SubNumber', $next_sub_number);
$sub_order->SetDBField('SubTotal', $sub_order_data['TotalAmount']);
$grouping_data = explode('_', $sub_order_data['Grouping']);
$named_grouping_data['Type'] = $grouping_data[0];
if (!in_array($named_grouping_data['Type'], $skip_types)) {
$named_grouping_data['OrderItemId'] = $grouping_data[1];
$named_grouping_data['ProductId'] = $grouping_data[2];
$named_grouping_data['PackageNum'] = $grouping_data[3];
}
else {
$named_grouping_data['PackageNum'] = $grouping_data[2];
}
if ($named_grouping_data['Type'] == PRODUCT_TYPE_TANGIBLE) {
$sub_order->SetDBField('ShippingCost', getArrayValue( unserialize($order->GetDBField('ShippingInfo')), $sub_order_data['PackageNum'], 'TotalCost') );
$sub_order->SetDBField('InsuranceFee', getArrayValue( unserialize($order->GetDBField('ShippingInfo')), $sub_order_data['PackageNum'], 'InsuranceFee') );
$sub_order->SetDBField('ShippingInfo', serialize(Array(1 => getArrayValue( unserialize($order->GetDBField('ShippingInfo')), $sub_order_data['PackageNum']))));
}
else {
$sub_order->SetDBField('ShippingCost', 0);
$sub_order->SetDBField('InsuranceFee', 0);
$sub_order->SetDBField('ShippingInfo', ''); //otherwise orders w/o shipping wills still have shipping info!
}
$amount_percent = $sub_order->getTotalAmount() * 100 / $order->getTotalAmount();
// proportional affiliate commission splitting
if ($order->GetDBField('AffiliateCommission') > 0) {
$sub_order->SetDBField('AffiliateCommission', $order->GetDBField('AffiliateCommission') * $amount_percent / 100 );
}
$amount_percent = ($sub_order->GetDBField('SubTotal') + $sub_order->GetDBField('ShippingCost')) * 100 / ($order->GetDBField('SubTotal') + $order->GetDBField('ShippingCost'));
if ($order->GetDBField('ProcessingFee') > 0) {
$sub_order->SetDBField('ProcessingFee', round($order->GetDBField('ProcessingFee') * $amount_percent / 100, 2));
}
$sub_order->RecalculateTax();
$original_amount = $sub_order->GetDBField('SubTotal') + $sub_order->GetDBField('ShippingCost') + $sub_order->GetDBField('VAT') + $sub_order->GetDBField('ProcessingFee') + $sub_order->GetDBField('InsuranceFee') - $sub_order->GetDBField('GiftCertificateDiscount');
$sub_order->SetDBField('OriginalAmount', $original_amount);
if ($named_grouping_data['Type'] == 1 && ($sub_order_data['BackOrderFlag'] > 0
||
($sub_order_data['TotalItems'] != $sub_order_data['TotalReserved'])) ) {
$sub_order->SetDBField('Status', ORDER_STATUS_BACKORDERS);
if ($event->Special != 'recurring') { // just in case if admin uses tangible backordered products in recurring orders
$email_event_user =& $this->Application->EmailEventUser('BACKORDER.ADD', $sub_order->GetDBField('PortalUserId'), $this->OrderEmailParams($sub_order));
$email_event_admin =& $this->Application->EmailEventAdmin('BACKORDER.ADD');
}
}
else {
switch ($named_grouping_data['Type']) {
case PRODUCT_TYPE_DOWNLOADABLE:
$sql = 'SELECT oi.*
FROM '.TABLE_PREFIX.'OrderItems oi
LEFT JOIN '.TABLE_PREFIX.'Products p ON p.ProductId = oi.ProductId
WHERE (OrderId = %s) AND (p.Type = '.PRODUCT_TYPE_DOWNLOADABLE.')';
$downl_products = $this->Conn->Query( sprintf($sql, $ord_id) );
$product_ids = Array();
foreach ($downl_products as $downl_product) {
$this->raiseProductEvent('Approve', $downl_product['ProductId'], $downl_product, $next_sub_number);
$product_ids[] = $downl_product['ProductId'];
}
break;
case PRODUCT_TYPE_TANGIBLE:
$sql = 'SELECT '.$backorder_select.', oi.*
FROM '.TABLE_PREFIX.'OrderItems oi
LEFT JOIN '.TABLE_PREFIX.'Products p ON p.ProductId = oi.ProductId
WHERE (OrderId = %s) AND (BackOrderFlag = 0) AND (p.Type = '.PRODUCT_TYPE_TANGIBLE.')';
$products = $this->Conn->Query( sprintf($sql, $ord_id) );
foreach ($products as $product) {
$this->raiseProductEvent('Approve', $product['ProductId'], $product, $next_sub_number);
}
break;
default:
$order_item_fields = $this->Conn->GetRow('SELECT * FROM '.TABLE_PREFIX.'OrderItems WHERE OrderItemId = '.$named_grouping_data['OrderItemId']);
$this->raiseProductEvent('Approve', $named_grouping_data['ProductId'], $order_item_fields, $next_sub_number);
break;
}
$sub_order->SetDBField('Status', $named_grouping_data['Type'] == PRODUCT_TYPE_TANGIBLE ? ORDER_STATUS_TOSHIP : ORDER_STATUS_PROCESSED);
}
if ($next_sub_number == $first_sub_number) {
$sub_order->SetId($order->GetId());
$sub_order->Update();
}
else {
$sub_order->Create();
}
switch ($named_grouping_data['Type']) {
case PRODUCT_TYPE_TANGIBLE:
$query = 'UPDATE '.$table_prefix.'OrderItems SET OrderId = %s WHERE OrderId = %s AND PackageNum = %s';
$query = sprintf($query, $sub_order->GetId(), $ord_id, $sub_order_data['PackageNum']);
break;
case PRODUCT_TYPE_DOWNLOADABLE:
$query = 'UPDATE '.$table_prefix.'OrderItems SET OrderId = %s WHERE OrderId = %s AND ProductId IN (%s)';
$query = sprintf($query, $sub_order->GetId(), $ord_id, implode(',', $product_ids) );
break;
default:
$query = 'UPDATE '.$table_prefix.'OrderItems SET OrderId = %s WHERE OrderId = %s AND OrderItemId = %s';
$query = sprintf($query, $sub_order->GetId(), $ord_id, $named_grouping_data['OrderItemId']);
break;
}
$this->Conn->Query($query);
if ($order_has_gift) {
// gift certificate can be applied only after items are assigned to suborder
$sub_order->RecalculateGift($event);
$original_amount = $sub_order->GetDBField('SubTotal') + $sub_order->GetDBField('ShippingCost') + $sub_order->GetDBField('VAT') + $sub_order->GetDBField('ProcessingFee') + $sub_order->GetDBField('InsuranceFee') - $sub_order->GetDBField('GiftCertificateDiscount');
$sub_order->SetDBField('OriginalAmount', $original_amount);
$sub_order->Update();
}
$processed_sub_orders[] = $sub_order->GetID();
$next_sub_number++;
$group++;
}
foreach ($processed_sub_orders as $sub_id) {
// update DiscountTotal field
$sql = 'SELECT SUM(ROUND(FlatPrice-Price,2)*Quantity) FROM '.$table_prefix.'OrderItems WHERE OrderId = '.$sub_id;
$discount_total = $this->Conn->GetOne($sql);
$sql = 'UPDATE '.$sub_order->TableName.'
SET DiscountTotal = '.$this->Conn->qstr($discount_total).'
WHERE OrderId = '.$sub_id;
$this->Conn->Query($sql);
}
}
/**
* Call products linked event when spefcfic action is made to product in order
*
* @param string $event_type type of event to get from product ProcessingData = {Approve,Deny,CompleteOrder}
* @param int $product_id ID of product to gather processing data from
* @param Array $order_item_fields OrderItems table record fields (with needed product & order in it)
*/
function raiseProductEvent($event_type, $product_id, $order_item_fields, $next_sub_number=null)
{
$sql = 'SELECT ProcessingData
FROM '.TABLE_PREFIX.'Products
WHERE ProductId = '.$product_id;
$processing_data = $this->Conn->GetOne($sql);
if ($processing_data) {
$processing_data = unserialize($processing_data);
$event_key = getArrayValue($processing_data, $event_type.'Event');
// if requested type of event is defined for product, only then process it
if ($event_key) {
$event = new kEvent($event_key);
$event->setEventParam('field_values', $order_item_fields);
$event->setEventParam('next_sub_number', $next_sub_number);
$this->Application->HandleEvent($event);
}
}
}
/**
* Updates product info in shopping cart
*
* @param kEvent $event
* @param unknown_type $prod_id
* @param unknown_type $back_order
* @param unknown_type $qty
* @param unknown_type $price
* @param unknown_type $discounted_price
* @param unknown_type $discount_type
* @param unknown_type $discount_id
* @param unknown_type $order_item_id
* @param unknown_type $options_salt
* @param unknown_type $passed_item_data
* @param unknown_type $cost
* @return unknown
*/
function UpdateOrderItem(&$event, $prod_id, $back_order, $qty, $price, $discounted_price, $discount_type, $discount_id, $order_item_id = 0, $options_salt = 0, $passed_item_data=null, $cost=0)
{
$price = (float) $price;
$discounted_price = (float) $discounted_price;
$qty = (int) $qty;
$ord_id = $this->getPassedId($event);
$table_prefix = $this->TablePrefix($event);
if($order_item_id)
{
$query = ' SELECT OrderItemId, Quantity, FlatPrice, Price, BackOrderFlag, ItemData FROM '.$table_prefix.'OrderItems
WHERE OrderItemId = '.$order_item_id;
}
else
{
// try to load specified Product by its Id and BackOrderFlag in the order
$query = 'SELECT OrderItemId, Quantity, FlatPrice, Price, BackOrderFlag, ItemData FROM '.$table_prefix.'OrderItems
WHERE
OrderId = '.$ord_id.'
AND
ProductId = '.$prod_id.'
AND
BackOrderFlag '.($back_order ? ' >= 1' : ' = 0').'
AND
OptionsSalt = '.$options_salt;
}
$item_row = $this->Conn->GetRow($query);
$item_id = $item_row['OrderItemId'];
$object =& $this->Application->recallObject('orditems.-item', null, Array('skip_autoload' => true));
$item_data = $item_row['ItemData'];
if($item_data)
{
$item_data = unserialize($item_data);
}
$orig_discount_type = (int)getArrayValue($item_data, 'DiscountType');
$orig_discount_id = (int)getArrayValue($item_data, 'DiscountId');
if ($item_id) { // if Product already exists in the order
if ($qty > 0 &&
$item_row['Quantity'] == $qty &&
round($item_row['FlatPrice'], 3) == round($price, 3) &&
round($item_row['Price'], 3) == round($discounted_price, 3) &&
$orig_discount_type == $discount_type &&
$orig_discount_id == $discount_id)
{
return false;
}
$object->Load($item_id);
if ($qty > 0) { // Update Price by _TOTAL_ qty
$object->SetDBField('Quantity', $qty);
$object->SetDBField('FlatPrice', $price );
$object->SetDBField('Price', $discounted_price );
$object->SetDBField('Cost', $cost);
if($item_data = $object->GetDBField('ItemData'))
{
$item_data = unserialize($item_data);
}
else
{
$item_data = Array();
}
$item_data['DiscountType'] = $discount_type;
$item_data['DiscountId'] = $discount_id;
$object->SetDBField('ItemData', serialize($item_data));
$object->Update();
}
else { // delete products with 0 qty
$object->Delete();
}
}
elseif ($qty > 0) { // if we are adding product
$product =& $this->Application->recallObject('p');
$product->Load($prod_id);
$object->SetDBField('ProductId', $prod_id);
$object->SetDBField('ProductName', $product->GetField('Name'));
$object->SetDBField('Quantity', $qty);
$object->SetDBField('FlatPrice', $price );
$object->SetDBField('Price', $discounted_price );
$object->SetDBField('Cost', $cost);
$object->SetDBField('OrderId', $ord_id);
$object->SetDBField('BackOrderFlag', $back_order);
if ($passed_item_data && !is_array($passed_item_data)) {
$passed_item_data = unserialize($passed_item_data);
}
// $item_data = Array('DiscountType' => $discount_type, 'DiscountId' => $discount_id);
$item_data = $passed_item_data;
$object->SetDBField('ItemData', serialize($item_data));
$object->Create();
if( $this->UseTempTables($event) ) $object->SetTempId();
}
else {
return false; // item requiring to set qty to 0, meaning already does not exist
}
return true;
}
function OptionsSalt($options, $comb_only=false)
{
$helper =& $this->Application->recallObject('kProductOptionsHelper');
return $helper->OptionsSalt($options, $comb_only);
}
/**
* Enter description here...
*
* @param kEvent $event
* @param int $item_id
*/
function AddItemToOrder(&$event, $item_id, $qty = null, $package_num = null)
{
if (!isset($qty)) {
$qty = 1;
}
// Loading product to add
$product =& $this->Application->recallObject('p.toadd', null, Array('skip_autoload' => true));
/* @var $product kDBItem */
$product->Load($item_id);
$object =& $this->Application->recallObject('orditems.-item', null, Array('skip_autoload' => true));
/* @var $object kDBItem */
$order =& $this->Application->recallObject('ord');
/* @var $order kDBItem */
- if (!$order->isLoaded() && !$this->Application->IsAdmin()) {
+ if (!$order->isLoaded() && !$this->Application->isAdmin) {
// no order was created before -> create one now
if ($this->_createNewCart($event)) {
$this->LoadItem($event);
}
}
if (!$order->isLoaded()) {
// was unable to create new order
return false;
}
$ord_id = $order->GetID();
if ($item_data = $event->getEventParam('ItemData')) {
$item_data = unserialize($item_data);
}
else {
$item_data = Array ();
}
$options = getArrayValue($item_data, 'Options');
if (!$this->CheckOptions($event, $options, $item_id, $qty, $product->GetDBField('OptionsSelectionMode'))) return;
// Checking if such product already exists in the cart
$keys['OrderId'] = $ord_id;
$keys['ProductId'] = $product->GetId();
if (isset($item_data['Options'])) {
$options_salt = $this->OptionsSalt($item_data['Options']);
$keys['OptionsSalt'] = $options_salt;
}
else {
$options_salt = null;
}
$exists = $object->Load($keys);
$object->SetDBField('ProductId', $product->GetId());
$object->SetDBField('ProductName', $product->GetField('l'.$this->Application->GetDefaultLanguageId().'_Name'));
$object->SetDBField('Weight', $product->GetDBField('Weight'));
if (isset($item_data['Options'])) {
$object->SetDBField('OptionsSalt', $options_salt);
}
if (isset($package_num)) {
$object->SetDBField('PackageNum', $package_num);
}
if($product->GetDBField('Type') == PRODUCT_TYPE_TANGIBLE || $product->GetDBField('Type') == 6)
{
$object->SetDBField('Quantity', $object->GetDBField('Quantity') + $qty);
}
else // Types: 2,3,4
{
$object->SetDBField('Quantity', $qty); // 1
$exists = false;
}
if (isset($item_data['ForcePrice'])) {
$price = $item_data['ForcePrice'];
}
else {
$price = $this->GetPlainProductPrice($product->GetId(), $object->GetDBField('Quantity'), $product->GetDBField('Type'), $order, $options_salt, $item_data);
}
$cost = $this->GetProductCost($product->GetId(), $object->GetDBField('Quantity'), $product->GetDBField('Type'), $options_salt, $item_data);
$object->SetDBField('FlatPrice', $price);
$couponed_price = $this->GetCouponDiscountedPrice($order->GetDBField('CouponId'), $product->GetId(), $price);
$discounted_price = $this->GetDiscountedProductPrice($product->GetId(), $price, $discount_id, $order);
if( $couponed_price < $discounted_price )
{
$discounted_price = $couponed_price;
$discount_type = 'coupon';
$discount_id = $order->GetDBField('CouponId');
}
else
{
$discount_type = 'discount';
$discount_id = $discount_id;
}
$item_data['DiscountType'] = $discount_type;
$item_data['DiscountId'] = $discount_id;
$item_data['IsRecurringBilling'] = $product->GetDBField('IsRecurringBilling');
// it item is processed in order using new style, then put such mark in orderitem record
$processing_data = $product->GetDBField('ProcessingData');
if ($processing_data) {
$processing_data = unserialize($processing_data);
if (getArrayValue($processing_data, 'HasNewProcessing')) {
$item_data['HasNewProcessing'] = 1;
}
}
$object->SetDBField('ItemData', serialize($item_data));
$object->SetDBField('Price', $discounted_price); // will be retrieved later
$object->SetDBField('Cost', $cost);
$object->SetDBField('BackOrderFlag', 0); // it will be updated in OnRecalculateItems later if needed
$object->SetDBField('OrderId', $ord_id);
if ($exists) {
if ($qty > 0) {
$object->Update();
}
else {
$object->Delete();
}
}
else {
$object->Create();
if ($this->UseTempTables($event)) {
$object->setTempID();
}
}
$this->Application->HandleEvent($ord_event, 'ord:OnRecalculateItems');
- /*if ($ord_event->getEventParam('RecalculateChangedCart') && !$this->Application->IsAdmin() ) {
+ /*if ($ord_event->getEventParam('RecalculateChangedCart') && !$this->Application->isAdmin) {
$event->SetRedirectParam('checkout_error', $ord_event->redirect_params['checkout_error']);
}*/
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function UpdateShippingTotal(&$event)
{
if ($this->Application->GetVar('ebay_notification') == 1) {
// TODO: get rid of this "if"
return ;
}
$object =& $event->getObject();
$ord_id = $object->GetId();
$shipping_option = $object->GetDBField('ShippingOption');
$backorder_select = $shipping_option == 0 ? '0 As BackOrderFlag' : 'BackOrderFlag';
$table_prefix = $this->TablePrefix($event);
$shipping_info = $object->GetDBField('ShippingInfo') ? unserialize( $object->GetDBField('ShippingInfo') ) : false;
$shipping_total = 0;
$insurance_fee = 0;
if( is_array($shipping_info) )
{
foreach ($shipping_info as $a_shipping)
{
// $id_elements = explode('_', $a_shipping['ShippingTypeId']);
$shipping_total += $a_shipping['TotalCost'];
$insurance_fee += $a_shipping['InsuranceFee'];
}
}
$object->SetDBField('ShippingCost', $shipping_total);
$object->SetDBField('InsuranceFee', $insurance_fee);
// no need to update, it will be called in calling method
$this->RecalculateTax($event);
}
/**
* Recompile shopping cart, splitting or grouping orders and backorders depending on total quantityes.
* First it counts total qty for each ProductId, and then creates order for available items
* and backorder for others. It also updates the sub-total for the order
*
* @param kEvent $event
* @return bool Returns true if items splitting/grouping were changed
*/
function OnRecalculateItems(&$event)
{
if (is_object($event->MasterEvent) && ($event->MasterEvent->status != erSUCCESS)) {
// e.g. master order update failed, don't recalculate order products
return ;
}
if($checkout_error = $this->Application->GetVar('set_checkout_error'))
{
$event->SetRedirectParam('checkout_error', $checkout_error);
}
$order =& $event->getObject();
/* @var $order OrdersItem */
if ( !$order->isLoaded() ) {
$this->LoadItem($event); // try to load
}
$ord_id = (int)$order->GetID();
if ( !$order->isLoaded() ) return; //order has not been created yet
if( $order->GetDBField('Status') != ORDER_STATUS_INCOMPLETE )
{
return;
}
$table_prefix = $this->TablePrefix($event);
// process only tangible products here
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' SELECT oi.ProductId, oi.OptionsSalt, oi.ItemData, SUM(oi.Quantity) AS Quantity,
IF(p.InventoryStatus = 2, poc.QtyInStock, p.QtyInStock) AS QtyInStock,
p.QtyInStockMin, p.BackOrder, p.InventoryStatus
FROM '.$table_prefix.'OrderItems AS oi
LEFT JOIN '.TABLE_PREFIX.'Products AS p ON oi.ProductId = p.ProductId
LEFT JOIN '.$poc_table.' poc ON (poc.CombinationCRC = oi.OptionsSalt) AND (oi.ProductId = poc.ProductId)
WHERE (oi.OrderId = '.$ord_id.') AND (p.Type = 1)
GROUP BY oi.ProductId, OptionsSalt';
$items = $this->Conn->Query($query);
$result = false;
$cost_total = 0;
$sub_total = 0;
$sub_total_flat = 0;
$coupon_discount = 0;
$pending_operations = Array();
$backordering = $this->Application->ConfigValue('Comm_Enable_Backordering');
$coupon_id = $order->GetDBField('CouponId');
foreach ($items as $row) {
$a_item_data = isset($row['ItemData']) ? unserialize($row['ItemData']) : Array();
$min_qty = $this->GetMinQty($row['ProductId']);
if ($row['Quantity'] > 0 && $row['Quantity'] < $min_qty) {
$row['Quantity'] = $min_qty;
$event->SetRedirectParam('checkout_error', 6);
}
$back_order = 0;
$to_order = 0;
if (!$row['InventoryStatus']) {
$available = $row['Quantity']*2; // always available;
}
else {
// if there are not enough qty AND backorder is auto or backorder is always
$available = $row['QtyInStock'] - $row['QtyInStockMin'];
$available = max(0, $available); // just in case
}
if (
$backordering && // backordering generally enabled
(
($row['Quantity'] > $available)
&&
($row['BackOrder'] == 2) //auto
)
||
$row['BackOrder'] == 1 // always
)
{ // split order into order & backorder
if ($row['BackOrder'] == 1) { //Always backorder
$available = 0;
$to_order = 0;
$back_order = $row['Quantity'];
}
else { //Auto
$to_order = $available;
$back_order = $row['Quantity'] - $available;
}
if (isset($a_item_data['ForcePrice'])) {
$price = $a_item_data['ForcePrice'];
}
else {
$price = $this->GetPlainProductPrice( $row['ProductId'], $to_order + $back_order, 1, $order, $row['OptionsSalt'], $row['ItemData'] );
}
$cost = $this->GetProductCost( $row['ProductId'], $to_order + $back_order, 1, $row['OptionsSalt'], $row['ItemData'] );
$discounted_price = $this->GetDiscountedProductPrice( $row['ProductId'], $price, $discount_id, $order );
$couponed_price = $this->GetCouponDiscountedPrice( $coupon_id, $row['ProductId'], $price );
if($couponed_price < $discounted_price)
{
$discounted_price = $couponed_price;
$coupon_discount += ($price - $couponed_price) * ($to_order + $back_order);
$discount_type = 'coupon';
$discount_id = $coupon_id;
}
else
{
$discount_type = 'discount';
}
$pending_operations[] = Array( $row['ProductId'], 0, $to_order, $price, $discounted_price, $discount_type, $discount_id, 0, $row['OptionsSalt'], $row['ItemData'], $cost );
$pending_operations[] = Array( $row['ProductId'], 1, $back_order, $price, $discounted_price, $discount_type, $discount_id, 0, $row['OptionsSalt'], $row['ItemData'], $cost);
}
else { // store as normal order (and remove backorder)
// we could get here with backorder=never then we should order only what's available
$to_order = min($row['Quantity'], $available);
if (isset($a_item_data['ForcePrice'])) {
$price = $a_item_data['ForcePrice'];
}
else {
$price = $this->GetPlainProductPrice( $row['ProductId'], $to_order + $back_order, 1, $order, $row['OptionsSalt'], $row['ItemData'] );
}
$cost = $this->GetProductCost( $row['ProductId'], $to_order + $back_order, 1, $row['OptionsSalt'], $row['ItemData'] );
$discounted_price = $this->GetDiscountedProductPrice( $row['ProductId'], $price, $discount_id, $order );
$couponed_price = $this->GetCouponDiscountedPrice( $coupon_id, $row['ProductId'], $price );
if($couponed_price < $discounted_price)
{
$discounted_price = $couponed_price;
$coupon_discount += ($price - $couponed_price) * ($to_order + $back_order);
$discount_type = 'coupon';
$discount_id = $coupon_id;
}
else
{
$discount_type = 'discount';
}
$pending_operations[] = Array( $row['ProductId'], 0, $to_order, $price, $discounted_price, $discount_type, $discount_id, 0, $row['OptionsSalt'], $row['ItemData'], $cost );
$pending_operations[] = Array( $row['ProductId'], 1, 0, $price, $discounted_price, $discount_type, $discount_id, 0, $row['OptionsSalt'], $row['ItemData'], $cost ); // this removes backorders
if ($to_order < $row['Quantity']) { // has changed
if ($to_order > 0) {
$event->SetRedirectParam('checkout_error', 2);
}
else {
$event->SetRedirectParam('checkout_error', 3);
}
$result = true;
}
}
$sub_total_flat += ($to_order + $back_order) * $price;
$sub_total += ($to_order + $back_order) * $discounted_price;
$cost_total += ($to_order + $back_order) * $cost;
}
// process subscriptions, services and downloadable: begin
$poc_table = $this->Application->getUnitOption('poc', 'TableName');
$query = ' SELECT oi.OrderItemId, oi.ProductId, oi.Quantity, oi.OptionsSalt, oi.ItemData,
IF(p.InventoryStatus = 2, poc.QtyInStock, p.QtyInStock) AS QtyInStock,
p.QtyInStockMin, p.BackOrder, p.InventoryStatus, p.Type
FROM '.$table_prefix.'OrderItems AS oi
LEFT JOIN '.TABLE_PREFIX.'Products AS p ON oi.ProductId = p.ProductId
LEFT JOIN '.$poc_table.' poc ON (poc.CombinationCRC = oi.OptionsSalt) AND (oi.ProductId = poc.ProductId)
WHERE (oi.OrderId = '.$ord_id.') AND (p.Type IN (2,3,4,5,6))';
$items = $this->Conn->Query($query);
foreach ($items as $row)
{
$a_item_data = isset($row['ItemData']) ? unserialize($row['ItemData']) : Array();
if (isset($a_item_data['ForcePrice'])) {
$price = $a_item_data['ForcePrice'];
}
else {
$price = $this->GetPlainProductPrice( $row['ProductId'], $row['Quantity'], $row['Type'], $order, $row['OptionsSalt'], $row['ItemData'] );
}
$cost = $this->GetProductCost( $row['ProductId'], $row['Quantity'], $row['Type'], $row['OptionsSalt'], $row['ItemData'] );
$discounted_price = $this->GetDiscountedProductPrice( $row['ProductId'], $price, $discount_id, $order );
$couponed_price = $this->GetCouponDiscountedPrice( $coupon_id, $row['ProductId'], $price );
if($couponed_price < $discounted_price)
{
$discounted_price = $couponed_price;
$coupon_discount += ($price - $couponed_price);
$discount_type = 'coupon';
$discount_id = $coupon_id;
}
else
{
$discount_type = 'discount';
}
$pending_operations[] = Array( $row['ProductId'], 0, $row['Quantity'], $price, $discounted_price, $discount_type, $discount_id, $row['OrderItemId'], 0, $row['ItemData'], $cost );
$sub_total_flat += $price * $row['Quantity'];
$sub_total += $discounted_price * $row['Quantity'];
$cost_total += $cost * $row['Quantity'];
}
// process subscriptions, services and downloadable: end
$flat_discount = $this->GetWholeOrderPlainDiscount($global_discount_id, $order);
$flat_discount = ($flat_discount < $sub_total_flat) ? $flat_discount : $sub_total_flat;
$coupon_flat_discount = $this->GetWholeOrderCouponDiscount($coupon_id);
$coupon_flat_discount = ($coupon_flat_discount < $sub_total_flat) ? $coupon_flat_discount : $sub_total_flat;
if($coupon_flat_discount && $coupon_flat_discount > $flat_discount)
{
$flat_discount = $coupon_flat_discount;
$global_discount_type = 'coupon';
$global_discount_id = $coupon_id;
}
else
{
$global_discount_type = 'discount';
}
if($sub_total_flat - $sub_total < $flat_discount)
{
$coupon_discount = ($flat_discount == $coupon_flat_discount) ? $flat_discount : 0;
$sub_total = $sub_total_flat - $flat_discount;
foreach ($pending_operations as $operation_row)
{
list($product_id, $backorder, $qty, $price, $discounted_price, $dummy, $dummy, $order_item_id, $options_salt, $item_data, $cost) = $operation_row;
$new_price = ($price / $sub_total_flat) * $sub_total;
$result = $this->UpdateOrderItem($event, $product_id, $backorder, $qty, $price, $new_price, $global_discount_type, $global_discount_id, $order_item_id, $options_salt, $item_data, $cost) || $result;
}
}
else
{
foreach ($pending_operations as $operation_row)
{
list($product_id, $backorder, $qty, $price, $discounted_price, $discount_type, $discount_id, $order_item_id, $options_salt, $item_data, $cost) = $operation_row;
$result = $this->UpdateOrderItem($event, $product_id, $backorder, $qty, $price, $discounted_price, $discount_type, $discount_id, $order_item_id, $options_salt, $item_data, $cost) || $result;
}
}
$order->SetDBField('SubTotal', $sub_total);
$order->SetDBField('CostTotal', $cost_total);
// $this->CalculateDiscount($event);
$order->SetDBField('DiscountTotal', $sub_total_flat - $sub_total);
if($coupon_id && $coupon_discount == 0)
{
$this->RemoveCoupon($order);
$event->SetRedirectParam('checkout_error', 8);
}
$order->SetDBField('CouponDiscount', $coupon_discount);
if ($result) $this->UpdateShippingOption($event);
$this->UpdateShippingTotal($event);
$this->RecalculateProcessingFee($event);
$this->RecalculateTax($event);
$this->RecalculateGift($event);
if ($event->Name != 'OnAfterItemUpdate') $order->Update();
$event->setEventParam('RecalculateChangedCart', $result);
if (is_object($event->MasterEvent)) {
$event->MasterEvent->setEventParam('RecalculateChangedCart', $result);
}
if ($result && !getArrayValue($event->redirect_params, 'checkout_error')) {
$event->SetRedirectParam('checkout_error', 1);
}
if ($result && is_object($event->MasterEvent) && $event->MasterEvent->Name == 'OnUserLogin')
{
if( ($shop_cart_template = $this->Application->GetVar('shop_cart_template'))
&& is_object($event->MasterEvent->MasterEvent) )
{
$event->MasterEvent->MasterEvent->SetRedirectParam('checkout_error', 9);
$event->MasterEvent->MasterEvent->redirect = $shop_cart_template;
}
}
return $result;
}
/* function GetShippingCost($user_country_id, $user_state_id, $user_zip, $weight, $items, $amount, $shipping_type)
{
$this->Application->recallObject('ShippingQuoteEngine');
$shipping_h =& $this->Application->recallObject('CustomShippingQuoteEngine');
$query = $shipping_h->QueryShippingCost($user_country_id, $user_state_id, $user_zip, $weight, $items, $amount, $shipping_type);
$cost = $this->Conn->GetRow($query);
return $cost['TotalCost'];
}*/
function GetMinQty($p_id)
{
$query = 'SELECT
MIN(pp.MinQty)
FROM '.TABLE_PREFIX.'ProductsPricing AS pp
WHERE pp.ProductId = '.$p_id;
$min_qty = $this->Conn->GetOne($query);
if (!$min_qty) return 1;
return $min_qty;
}
/**
* Return product cost for given qty, taking no discounts into account
*
* @param int $p_id ProductId
* @param int $qty Quantity
* @return float
*/
function GetProductCost($p_id, $qty, $product_type, $options_salt=null, $item_data=null)
{
$user_groups = $this->Application->RecallVar('UserGroups');
if($product_type == 1)
{
// $where_clause = 'pp.ProductId = '.$p_id.' AND pp.MinQty <= '.$qty;
// $orderby_clause = 'ORDER BY ('.$qty.' - pp.MinQty) ASC';
$where_clause = 'GroupId IN ('.$user_groups.') AND pp.ProductId = '.$p_id.' AND pp.MinQty <= '.$qty.' AND ('.$qty.' < pp.MaxQty OR pp.MaxQty=-1)';
$orderby_clause = 'ORDER BY pp.Price ASC';
}
else
{
$price_id = $this->GetPricingId($p_id, $item_data);
$where_clause = 'pp.ProductId = '.$p_id.' AND pp.PriceId = '.$price_id;
$orderby_clause = '';
}
$sql = 'SELECT Cost
FROM '.TABLE_PREFIX.'ProductsPricing AS pp
LEFT JOIN '.TABLE_PREFIX.'Products AS p
ON p.ProductId = pp.ProductId
WHERE '.$where_clause.'
'.$orderby_clause;
// GROUP BY pp.ProductId - removed, this it qty pricing is caclucated incorrectly !!!
$cost = $this->Conn->GetOne($sql);
if (!$cost) $price = 0;
return $cost;
}
/**
* Return product price for given qty, taking no discounts into account
*
* @param int $p_id ProductId
* @param int $qty Quantity
* @return float
*/
function GetPlainProductPrice($p_id, $qty, $product_type, &$order_object, $options_salt=null, $item_data=null)
{
$user_id = $order_object->GetDBField('PortalUserId');
$user_groups = $this->Application->getUserGroups($user_id);
if($product_type == 1)
{
// $where_clause = 'pp.ProductId = '.$p_id.' AND pp.MinQty <= '.$qty;
// $orderby_clause = 'ORDER BY ('.$qty.' - pp.MinQty) ASC';
$where_clause = 'GroupId IN ('.$user_groups.') AND pp.ProductId = '.$p_id.' AND pp.MinQty <= '.$qty.' AND ('.$qty.' < pp.MaxQty OR pp.MaxQty=-1)';
// if we have to stick ti primary group this order by clause force its pricing to go first,
// but if there is no pricing for primary group it will take next optimal
if ($this->Application->ConfigValue('Comm_PriceBracketCalculation') == 1){
if ($user_id <= 0) {
$primary_group = $this->Application->ConfigValue('User_LoggedInGroup'); // actually this is Everyone
}
else {
$primary_group = $this->Conn->GetOne('SELECT GroupId FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId='.$user_id.' AND PrimaryGroup=1');
}
$orderby_clause = 'ORDER BY (IF(GroupId='.$primary_group.',1,2)) ASC, pp.Price ASC';
}
else {
$orderby_clause = 'ORDER BY pp.Price ASC';
}
}
else
{
$price_id = $this->GetPricingId($p_id, $item_data);
$where_clause = 'pp.ProductId = '.$p_id.' AND pp.PriceId = '.$price_id;
$orderby_clause = '';
}
$sql = 'SELECT Price
FROM '.TABLE_PREFIX.'ProductsPricing AS pp
LEFT JOIN '.TABLE_PREFIX.'Products AS p
ON p.ProductId = pp.ProductId
WHERE '.$where_clause.'
'.$orderby_clause;
// GROUP BY pp.ProductId - removed, this it qty pricing is caclucated incorrectly !!!
$price = $this->Conn->GetOne($sql);
if (!$price) $price = 0;
if (isset($item_data) && !is_array($item_data)) {
$item_data = unserialize($item_data);
}
if (isset($item_data['Options'])) {
$addtion = 0;
$opt_helper =& $this->Application->recallObject('kProductOptionsHelper');
foreach ($item_data['Options'] as $opt => $val) {
$data = $this->Conn->GetRow('SELECT * FROM '.TABLE_PREFIX.'ProductOptions WHERE ProductOptionId = '.$opt);
$parsed = $opt_helper->ExplodeOptionValues($data);
if (!$parsed) continue;
$conv_prices = $parsed['Prices'];
$conv_price_types = $parsed['PriceTypes'];
if (is_array($val)) {
foreach ($val as $a_val) {
if (isset($conv_prices[unhtmlentities($a_val)]) && $conv_prices[unhtmlentities($a_val)]) {
if ($conv_price_types[unhtmlentities($a_val)] == '$') {
$addtion += $conv_prices[unhtmlentities($a_val)];
}
elseif ($conv_price_types[unhtmlentities($a_val)] == '%') {
$addtion += $price * $conv_prices[unhtmlentities($a_val)] / 100;
}
}
}
}
else {
if (isset($conv_prices[unhtmlentities($val)]) && $conv_prices[unhtmlentities($val)]) {
if ($conv_price_types[unhtmlentities($val)] == '$') {
$addtion += $conv_prices[unhtmlentities($val)];
}
elseif ($conv_price_types[unhtmlentities($val)] == '%') {
$addtion += $price * $conv_prices[unhtmlentities($val)] / 100;
}
}
}
}
$price += $addtion;
}
$comb_salt = $this->OptionsSalt( getArrayValue($item_data, 'Options'), 1);
if ($comb_salt) {
$query = 'SELECT * FROM '.TABLE_PREFIX.'ProductOptionCombinations WHERE CombinationCRC = '.$comb_salt;
$comb = $this->Conn->GetRow($query);
if ($comb) {
switch ($comb['PriceType']) {
case 1: // = override
$price = $comb['Price'];
break;
case 2: // flat
$price = $price + $comb['Price'];
break;
case 3: // percent
$price = $price * (1 + $comb['Price'] / 100);
break;
}
}
}
return max($price, 0);
}
/**
* Return product price for given qty, taking possible discounts into account
*
* @param int $p_id ProductId
* @param int $qty Quantity
* @return float
*/
function GetDiscountedProductPrice($p_id, $price, &$discount_id, &$order_object)
{
$discount_id = 0;
$user_id = $order_object->GetDBField('PortalUserId');
$user_groups = $this->Application->getUserGroups($user_id);
$sql = '
SELECT
IF(pd.Type = 1,
'.$price.' - pd.Amount,
IF(pd.Type = 2,
('.$price.' * (1-pd.Amount/100)),
'.$price.'
)
) AS DiscountedPrice,
pd.DiscountId
FROM '.TABLE_PREFIX.'Products AS p
LEFT JOIN '.TABLE_PREFIX.'ProductsDiscountItems AS pdi ON
pdi.ItemResourceId = p.ResourceId OR pdi.ItemType = 0
LEFT JOIN '.TABLE_PREFIX.'ProductsDiscounts AS pd ON
pd.DiscountId = pdi.DiscountId
AND
(pdi.ItemType = 1 OR (pdi.ItemType = 0 AND pd.Type = 2))
AND
pd.Status = 1
AND
( pd.GroupId IN ('.$user_groups.') AND
( (pd.Start IS NULL OR pd.Start < UNIX_TIMESTAMP())
AND
(pd.End IS NULL OR pd.End > UNIX_TIMESTAMP())
)
)
WHERE p.ProductId = '.$p_id.' AND pd.DiscountId IS NOT NULL
';
$pricing = $this->Conn->GetCol($sql, 'DiscountId');
if (!$pricing) return $price;
$discounted_price = min($pricing);
$pricing = array_flip($pricing);
$discount_id = $pricing[$discounted_price];
$discounted_price = min($discounted_price, $price);
return max($discounted_price, 0);
}
function GetCouponDiscountedPrice($coupon_id, $p_id, $price)
{
if(!$coupon_id) return $price;
$sql = '
SELECT
'.$price.' AS Price,
MIN(IF(pc.Type = 1,
'.$price.' - pc.Amount,
IF(pc.Type = 2,
('.$price.' * (1-pc.Amount/100)),
'.$price.'
)
)) AS DiscountedPrice
FROM '.TABLE_PREFIX.'Products AS p
LEFT JOIN '.TABLE_PREFIX.'ProductsCouponItems AS pci ON
pci.ItemResourceId = p.ResourceId OR pci.ItemType = 0
LEFT JOIN '.TABLE_PREFIX.'ProductsCoupons AS pc ON
pc.CouponId = pci.CouponId
AND
(pci.ItemType = 1 OR (pci.ItemType = 0 AND pc.Type = 2))
WHERE p.ProductId = '.$p_id.' AND pci.CouponId = '.$coupon_id.'
GROUP BY p.ProductId
';
$pricing = $this->Conn->GetRow($sql);
if ($pricing === false) return $price;
$price = min($pricing['Price'], $pricing['DiscountedPrice']);
return max($price, 0);
}
function GetWholeOrderPlainDiscount(&$discount_id, &$order_object)
{
$user_id = $order_object->GetDBField('PortalUserId');
$user_groups = $this->Application->getUserGroups($user_id);
$sql = '
SELECT pd.Amount AS Discount, pd.DiscountId
FROM '.TABLE_PREFIX.'ProductsDiscountItems AS pdi
LEFT JOIN '.TABLE_PREFIX.'ProductsDiscounts AS pd
ON
pd.DiscountId = pdi.DiscountId
AND
pdi.ItemType = 0 AND pd.Type = 1
AND
pd.Status = 1
AND
( pd.GroupId IN ('.$user_groups.') AND
( (pd.Start IS NULL OR pd.Start < '.$order_object->GetDBField('OrderDate').')
AND
(pd.End IS NULL OR pd.End > '.$order_object->GetDBField('OrderDate').')
)
)
WHERE pd.DiscountId IS NOT NULL
';
$pricing = $this->Conn->GetCol($sql, 'DiscountId');
if (!$pricing) return 0;
$discounted_price = max($pricing);
$pricing = array_flip($pricing);
$discount_id = $pricing[$discounted_price];
return max($discounted_price, 0);
}
function GetWholeOrderCouponDiscount($coupon_id)
{
if (!$coupon_id) return 0;
$sql = 'SELECT Amount
FROM '.TABLE_PREFIX.'ProductsCouponItems AS pci
LEFT JOIN '.TABLE_PREFIX.'ProductsCoupons AS pc
ON pc.CouponId = pci.CouponId
WHERE pci.CouponId = '.$coupon_id.' AND pci.ItemType = 0 AND pc.Type = 1';
return $this->Conn->GetOne($sql);
}
/**
* Return product pricing id for given product, if not passed - return primary pricing ID
*
* @param int $product_id ProductId
* @return float
*/
function GetPricingId($product_id, $item_data) {
if (!is_array($item_data)) {
$item_data = unserialize($item_data);
}
$price_id = getArrayValue($item_data, 'PricingId');
if (!$price_id) {
$price_id = $this->Application->GetVar('pr_id');
}
if (!$price_id){
$price_id = $this->Conn->GetOne('SELECT PriceId FROM '.TABLE_PREFIX.'ProductsPricing WHERE ProductId='.$product_id.' AND IsPrimary=1');
}
return $price_id;
}
function UpdateShippingOption(&$event)
{
$object =& $event->getObject();
$shipping_option = $object->GetDBField('ShippingOption');
if($shipping_option == '') return;
$table_prefix = $this->TablePrefix($event);
if ($shipping_option == 1 || $shipping_option == 0) { // backorder separately
$query = 'UPDATE '.$table_prefix.'OrderItems SET BackOrderFlag = 1 WHERE OrderId = '.$object->GetId().' AND BackOrderFlag > 1';
$this->Conn->Query($query);
}
if ($shipping_option == 2) {
$query = 'SELECT * FROM '.$table_prefix.'OrderItems WHERE OrderId = '.$object->GetId().' AND BackOrderFlag >= 1 ORDER By ProductName asc';
$items = $this->Conn->Query($query);
$backorder_flag = 2;
foreach ($items as $an_item) {
$query = 'UPDATE '.$table_prefix.'OrderItems SET BackOrderFlag = '.$backorder_flag.' WHERE OrderItemId = '.$an_item['OrderItemId'];
$this->Conn->Query($query);
$backorder_flag++;
}
}
}
function UpdateShippingTypes(&$event)
{
$object =& $this->Application->recallObject($event->getPrefixSpecial());
$ord_id = $object->GetId();
$order_info = $this->Application->GetVar('ord');
$shipping_ids = getArrayValue($order_info, $ord_id, 'ShippingTypeId');
if (!$shipping_ids)
{
return;
}
$last_shippings = unserialize($this->Application->RecallVar('LastShippings'));
$shipping_types = Array();
$ret = true;
foreach($shipping_ids as $package => $id)
{
// try to validate
if ( $object->GetDBField('ShippingType') == 0 && (strpos($id, 'USPS') !== false) && in_array($this->Application->GetVar('t'), Array('in-commerce/checkout/shipping','in-commerce/orders/orders_edit_shipping'))) {
$current_usps_shipping_types = unserialize($this->Application->RecallVar('current_usps_shipping_types'));
$object->SetDBField('ShippingInfo', serialize(Array($package => $current_usps_shipping_types[$id])));
$usps_data = $this->MakeUSPSOrder($object, true);
if ( !isset($usps_data['error_number']) ) {
// update only international shipping
if ( $object->GetDBField('ShippingCountry') != 'USA') {
$last_shippings[$package][$id]['TotalCost'] = $usps_data['Postage'];
}
}
else {
$this->Application->SetVar('usps_errors', $usps_data['error_description']);
$ret = false;
}
$object->SetDBField('ShippingInfo', '');
}
$shipping_types[$package] = $last_shippings[$package][$id];
}
$object->SetDBField('ShippingInfo', serialize($shipping_types));
return $ret;
}
/*function shipOrder(&$order_items)
{
$product_object =& $this->Application->recallObject('p', null, Array('skip_autoload' => true));
$order_item =& $this->Application->recallObject('orditems.-item');
while( !$order_items->EOL() )
{
$rec = $order_items->getCurrentRecord();
$order_item->SetDBFieldsFromHash($rec);
$order_item->SetId($rec['OrderItemId']);
$order_item->SetDBField('QuantityReserved', 0);
$order_item->Update();
$order_items->GoNext();
}
return true;
}*/
function RecalculateTax(&$event)
{
$object =& $event->getObject();
if ($object->GetDBField('Status') > ORDER_STATUS_PENDING) return;
$object->RecalculateTax();
}
function RecalculateProcessingFee(&$event)
{
$object =& $event->getObject();
// Do not reset processing fee while orders are being split (see SplitOrder)
if (preg_match("/^-sub/", $object->Special)) return;
if ($object->GetDBField('Status') > ORDER_STATUS_PENDING) return; //no changes for orders other than incomple or pending
$pt = $object->GetDBField('PaymentType');
$processing_fee = $this->Conn->GetOne('SELECT ProcessingFee FROM '.$this->Application->getUnitOption('pt', 'TableName').' WHERE PaymentTypeId = '.$pt);
$object->SetDBField( 'ProcessingFee', $processing_fee );
$this->UpdateTotals($event);
}
function UpdateTotals(&$event)
{
$object =& $event->getObject();
$object->UpdateTotals();
}
function CalculateDiscount(&$event)
{
$object =& $event->getObject();
$coupon =& $this->Application->recallObject('coup', null, Array('skip_autoload' => true));
if(!$coupon->Load( $object->GetDBField('CouponId'), 'CouponId' ))
{
return false;
}
$sql = 'SELECT Price * Quantity AS Amount, ProductId FROM '.$this->Application->getUnitOption('orditems', 'TableName').'
WHERE OrderId = '.$object->GetDBField('OrderId');
$orditems = $this->Conn->GetCol($sql, 'ProductId');
$sql = 'SELECT coupi.ItemType, p.ProductId FROM '.$this->Application->getUnitOption('coupi', 'TableName').' coupi
LEFT JOIN '.$this->Application->getUnitOption('p', 'TableName').' p
ON coupi.ItemResourceId = p.ResourceId
WHERE CouponId = '.$object->GetDBField('CouponId');
$discounts = $this->Conn->GetCol($sql, 'ProductId');
$discount_amount = 0;
foreach($orditems as $product_id => $amount)
{
if(isset($discounts[$product_id]) || array_search('0', $discounts, true) !== false)
{
switch($coupon->GetDBField('Type'))
{
case 1:
$discount_amount += $coupon->GetDBField('Amount') < $amount ? $coupon->GetDBField('Amount') : $amount;
break;
case 2:
$discount_amount += $amount * $coupon->GetDBField('Amount') / 100;
break;
default:
}
break;
}
}
$object->SetDBField('CouponDiscount', $discount_amount);
return $discount_amount;
}
/**
* Jumps to selected order in order's list from search tab
*
* @param kEvent $event
*/
function OnGoToOrder(&$event)
{
$id = array_shift( $this->StoreSelectedIDs($event) );
$idfield = $this->Application->getUnitOption($event->Prefix,'IDField');
$table = $this->Application->getUnitOption($event->Prefix,'TableName');
$sql = 'SELECT Status FROM %s WHERE %s = %s';
$order_status = $this->Conn->GetOne( sprintf($sql, $table, $idfield, $id) );
$prefix_special = $event->Prefix.'.'.$this->getSpecialByType($order_status);
$orders_list =& $this->Application->recallObject($prefix_special, $event->Prefix.'_List', Array('per_page'=>-1) );
$orders_list->Query();
foreach($orders_list->Records as $row_num => $record)
{
if( $record[$idfield] == $id ) break;
}
$per_page = $this->getPerPage( new kEvent($prefix_special.':OnDummy') );
$page = ceil( ($row_num+1) / $per_page );
$this->Application->StoreVar($prefix_special.'_Page', $page);
$event->redirect = 'in-commerce/orders/orders_'.$this->getSpecialByType($order_status).'_list';
}
/**
* Reset's any selected order state to pending
*
* @param unknown_type $event
*/
function OnResetToPending(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
foreach($items_info as $id => $field_values)
{
$object->Load($id);
$object->SetDBField('Status', ORDER_STATUS_PENDING);
if( $object->Update() )
{
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
}
}
}
/**
* Creates list from items selected in grid
*
* @param kEvent $event
*/
function OnLoadSelected(&$event)
{
$event->setPseudoClass('_List');
$object =& $event->getObject( Array('selected_only' => true) );
$event->redirect = false;
}
/**
* Return orders list, that will expire in time specified
*
* @param int $pre_expiration timestamp
* @return Array
*/
function getRecurringOrders($pre_expiration)
{
$ord_table = $this->Application->getUnitOption('ord', 'TableName');
$ord_idfield = $this->Application->getUnitOption('ord', 'IDField');
$processing_allowed = Array(ORDER_STATUS_PROCESSED, ORDER_STATUS_ARCHIVED);
$sql = 'SELECT '.$ord_idfield.', PortalUserId, GroupId, NextCharge
FROM '.$ord_table.'
WHERE (IsRecurringBilling = 1) AND (NextCharge < '.$pre_expiration.') AND Status IN ('.implode(',', $processing_allowed).')';
return $this->Conn->Query($sql, $ord_idfield);
}
/**
* Regular event: checks what orders should expire and renew automatically (if such flag set)
*
* @param kEvent $event
*/
function OnCheckRecurringOrders(&$event)
{
$skip_clause = Array();
$ord_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$ord_idfield = $this->Application->getUnitOption($event->Prefix, 'IDField');
$pre_expiration = adodb_mktime() + $this->Application->ConfigValue('Comm_RecurringChargeInverval') * 3600 * 24;
$to_charge = $this->getRecurringOrders($pre_expiration);
if ($to_charge) {
$order_ids = Array();
foreach ($to_charge as $order_id => $record) {
// skip virtual users (e.g. root, guest, etc.) & invalid subscriptions (with no group specified, no next charge, but Recurring flag set)
if (!$record['PortalUserId'] || !$record['GroupId'] || !$record['NextCharge']) continue;
$order_ids[] = $order_id;
// prevent duplicate user+group pairs
$skip_clause[ 'PortalUserId = '.$record['PortalUserId'].' AND GroupId = '.$record['GroupId'] ] = $order_id;
}
// process only valid orders
$temp_handler =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler');
$cloned_order_ids = $temp_handler->CloneItems($event->Prefix, 'recurring', $order_ids);
$order =& $this->Application->recallObject($event->Prefix.'.recurring', null, Array('skip_autoload' => true));
foreach ($cloned_order_ids as $order_id) {
$order->Load($order_id);
$this->Application->HandleEvent($complete_event, $event->Prefix.'.recurring:OnCompleteOrder' );
if ($complete_event->status == erSUCCESS) {
//send recurring ok email
$email_event_user =& $this->Application->EmailEventUser('ORDER.RECURRING.PROCESSED', $order->GetDBField('PortalUserId'), $this->OrderEmailParams($order));
$email_event_admin =& $this->Application->EmailEventAdmin('ORDER.RECURRING.PROCESSED');
}
else {
//send Recurring failed event
$order->SetDBField('Status', ORDER_STATUS_DENIED);
$order->Update();
$email_event_user =& $this->Application->EmailEventUser('ORDER.RECURRING.DENIED', $order->GetDBField('PortalUserId'), $this->OrderEmailParams($order));
$email_event_admin =& $this->Application->EmailEventAdmin('ORDER.RECURRING.DENIED');
}
}
// remove recurring flag from all orders found, not to select them next time script runs
$sql = 'UPDATE '.$ord_table.'
SET IsRecurringBilling = 0
WHERE '.$ord_idfield.' IN ('.implode(',', array_keys($to_charge)).')';
$this->Conn->Query($sql);
}
$pre_expiration = adodb_mktime() + $this->Application->ConfigValue('User_MembershipExpirationReminder') * 3600 * 24;
$to_charge = $this->getRecurringOrders($pre_expiration);
foreach ($to_charge as $order_id => $record) {
// skip virtual users (e.g. root, guest, etc.) & invalid subscriptions (with no group specified, no next charge, but Recurring flag set)
if (!$record['PortalUserId'] || !$record['GroupId'] || !$record['NextCharge']) continue;
// prevent duplicate user+group pairs
$skip_clause[ 'PortalUserId = '.$record['PortalUserId'].' AND GroupId = '.$record['GroupId'] ] = $order_id;
}
$skip_clause = array_flip($skip_clause);
$event->MasterEvent->setEventParam('skip_clause', $skip_clause);
}
function OnGeneratePDF(&$event)
{
$this->OnLoadSelected($event);
$this->Application->InitParser();
$o = $this->Application->ParseBlock(array('name'=>'in-commerce/orders/orders_pdf'));
$htmlFile = EXPORT_PATH . '/tmp.html';
$fh = fopen($htmlFile, 'w');
fwrite($fh, $o);
fclose($fh);
// return;
// require_once (FULL_PATH.'html2pdf/PDFEncryptor.php');
// Full path to the file to be converted
// $htmlFile = dirname(__FILE__) . '/test.html';
// The default domain for images that use a relative path
// (you'll need to change the paths in the test.html page
// to an image on your server)
$defaultDomain = DOMAIN;
// Full path to the PDF we are creating
$pdfFile = EXPORT_PATH . '/tmp.pdf';
// Remove old one, just to make sure we are making it afresh
@unlink($pdfFile);
$pdf_helper =& $this->Application->recallObject('kPDFHelper');
$pdf_helper->FileToFile($htmlFile, $pdfFile);
return ;
// DOM PDF VERSION
/*require_once(FULL_PATH.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html_file($htmlFile);
if ( isset($base_path) ) {
$dompdf->set_base_path($base_path);
}
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
file_put_contents($pdfFile, $dompdf->output());
return ;*/
// Instnatiate the class with our variables
require_once (FULL_PATH.'/html2pdf/HTML_ToPDF.php');
$pdf = new HTML_ToPDF($htmlFile, $defaultDomain, $pdfFile);
$pdf->setHtml2Ps('/usr/bin/html2ps');
$pdf->setPs2Pdf('/usr/bin/ps2pdf');
$pdf->setGetUrl('/usr/local/bin/curl -i');
// Set headers/footers
$pdf->setHeader('color', 'black');
$pdf->setFooter('left', '');
$pdf->setFooter('right', '$D');
$pdf->setDefaultPath(BASE_PATH.'/kernel/admin_templates/');
$result = $pdf->convert();
// Check if the result was an error
if (PEAR::isError($result)) {
$this->Application->ApplicationDie($result->getMessage());
}
else {
$download_url = rtrim($this->Application->BaseURL(), '/') . EXPORT_BASE_PATH . '/tmp.pdf';
echo "PDF file created successfully: $result";
echo '<br />Click <a href="' . $download_url . '">here</a> to view the PDF file.';
}
}
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
if (defined('IS_INSTALL') && IS_INSTALL) {
return ;
}
$order_number = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_P');
$order_sub_number = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_S');
$calc_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields');
foreach ($calc_fields as $special => $fields) {
$calc_fields[$special]['OrderNumber'] = str_replace('6', $order_number, $calc_fields[$special]['OrderNumber']);
$calc_fields[$special]['OrderNumber'] = str_replace('3', $order_sub_number, $calc_fields[$special]['OrderNumber']);
}
$this->Application->setUnitOption($event->Prefix, 'CalculatedFields', $calc_fields);
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
$fields['Number']['format'] = str_replace('%06d', '%0'.$order_number.'d', $fields['Number']['format']);
$fields['SubNumber']['format'] = str_replace('%03d', '%0'.$order_sub_number.'d', $fields['SubNumber']['format']);
- if (!$this->Application->IsAdmin()) {
+ if (!$this->Application->isAdminUser) {
$user_groups = explode(',', $this->Application->RecallVar('UserGroups'));
$default_group = $this->Application->ConfigValue('User_LoggedInGroup');
if (!in_array($default_group, $user_groups)){
$user_groups[]=$default_group;
}
$fields['PaymentType']['options_sql'] .= ' AND ';
$fields['PaymentType']['options_sql'] .= ' (PortalGroups LIKE "%%,'.implode(',%%" OR PortalGroups LIKE "%%,', $user_groups).',%%")';
}
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
}
/**
* Allows configuring export options
*
* @param kEvent $event
*/
function OnBeforeExportBegin(&$event)
{
$options = $event->getEventParam('options') ;
$items_list =& $this->Application->recallObject($event->Prefix.'.'.$this->Application->RecallVar('export_oroginal_special'), $event->Prefix.'_List');
$items_list->SetPerPage(-1);
if ($options['export_ids'] != '') {
$items_list->AddFilter('export_ids', $items_list->TableName.'.'.$items_list->IDField.' IN ('.implode(',',$options['export_ids']).')');
}
$options['ForceCountSQL'] = $items_list->getCountSQL( $items_list->GetSelectSQL(true,false) );
$options['ForceSelectSQL'] = $items_list->GetSelectSQL();
$event->setEventParam('options',$options);
$object =& $this->Application->recallObject($event->Prefix.'.export');
/* @var $object kDBItem */
$object->SetField('Number', 999999);
$object->SetField('SubNumber', 999);
}
/**
* Returns specific to each item type columns only
*
* @param kEvent $event
* @return Array
*/
function getCustomExportColumns(&$event)
{
$columns = parent::getCustomExportColumns($event);
$new_columns = Array(
'__VIRTUAL__CustomerName' => 'CustomerName',
'__VIRTUAL__TotalAmount' => 'TotalAmount',
'__VIRTUAL__AmountWithoutVAT' => 'AmountWithoutVAT',
'__VIRTUAL__SubtotalWithDiscount' => 'SubtotalWithDiscount',
'__VIRTUAL__SubtotalWithoutDiscount' => 'SubtotalWithoutDiscount',
'__VIRTUAL__OrderNumber' => 'OrderNumber',
);
return array_merge_recursive2($columns, $new_columns);
}
function OnSave(&$event)
{
$res = parent::OnSave($event);
if ($event->status == erSUCCESS) {
$copied_ids = unserialize($this->Application->RecallVar($event->Prefix.'_copied_ids'.$this->Application->GetVar('wid'), serialize(array())));
foreach ($copied_ids as $id) {
$an_event = new kEvent($this->Prefix.':Dummy');
$this->Application->SetVar($this->Prefix.'_id', $id);
$this->Application->SetVar($this->Prefix.'_mode', ''); // this is to fool ReserveItems to use live table
$this->ReserveItems($an_event);
}
}
return $res;
}
/**
* Occures before an item is copied to live table (after all foreign keys have been updated)
* Id of item being copied is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnBeforeCopyToLive(&$event)
{
$id = $event->getEventParam('id');
$copied_ids = unserialize($this->Application->RecallVar($event->Prefix.'_copied_ids'.$this->Application->GetVar('wid'), serialize(array())));
array_push($copied_ids, $id);
$this->Application->StoreVar($event->Prefix.'_copied_ids'.$this->Application->GetVar('wid'), serialize($copied_ids) );
}
/**
* Checks, that currently loaded item is allowed for viewing (non permission-based)
*
* @param kEvent $event
* @return bool
*/
function checkItemStatus(&$event)
{
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return true;
}
$object =& $event->getObject();
if (!$object->isLoaded()) {
return true;
}
return $object->GetDBField('PortalUserId') == $this->Application->RecallVar('user_id');
}
// ===== Gift Certificates Related =====
function OnRemoveGiftCertificate(&$event)
{
$object =& $event->getObject();
$this->RemoveGiftCertificate($object);
$event->CallSubEvent('OnRecalculateItems');
$event->SetRedirectParam('checkout_error', 107);
}
function RemoveGiftCertificate(&$object)
{
$object->RemoveGiftCertificate();
}
function RecalculateGift(&$event)
{
$object =& $event->getObject();
/* @var $object OrdersItem */
if ($object->GetDBField('Status') > ORDER_STATUS_PENDING) {
return ;
}
$object->RecalculateGift($event);
}
function GetWholeOrderGiftCertificateDiscount($gift_certificate_id)
{
if (!$gift_certificate_id) {
return 0;
}
$sql = 'SELECT Debit
FROM '.TABLE_PREFIX.'GiftCertificates
WHERE GiftCertificateId = '.$gift_certificate_id;
return $this->Conn->GetOne($sql);
}
/**
* Creates new USPS order
*
* @param OrdersItem $object
* @param bool $fake_mode
* @return Array
*/
function MakeUSPSOrder(&$object, $fake_mode = false)
{
$this->Application->recallObject('ShippingQuoteEngine');
$aUSPS = $this->Application->recallObject('USPS', 'USPS');
/* @var $aUSPS USPS */
$ShippingInfo = unserialize($object->GetDBField('ShippingInfo'));
$ShippingCode = $USPSMethod = '';
$ShippingCountry = $aUSPS->GetUSPSCountry($object->GetDBField('ShippingCountry'));
$UserName = explode(" ", $object->GetDBField('ShippingTo'));
$item_table = TABLE_PREFIX.'OrderItems';
- if ( $this->Application->IsAdmin() ) {
+ if ($this->Application->isAdminUser) {
// this strange contraption actually uses temp table from object (when in temp mode)
$order_table = $object->TableName;
$item_table = str_replace('Orders', 'OrderItems', $order_table);
}
$sOrder = Array (
'FirstName' => $UserName[0],
'LastName' => $UserName[1],
'ShippingCompany' => $object->GetDBField('ShippingCompany'),
'ShippingAddress1' => $object->GetDBField('ShippingAddress1'),
'ShippingAddress2' => $object->GetDBField('ShippingAddress2'),
'ShippingCity' => $object->GetDBField('ShippingCity'),
'ShippingZip' => $object->GetDBField('ShippingZip'),
'ShippingCountry' => $ShippingCountry,
'ShippingPhone' => $aUSPS->PhoneClean($object->GetDBField('ShippingPhone')),
'ShippingFax' => $aUSPS->PhoneClean($object->GetDBField('ShippingFax')),
'ShippingNumBoxes' => '1',
);
$sql = 'SELECT SUM(`Quantity` * `Weight`)
FROM ' . $item_table . '
WHERE ' . $object->IDField . ' = ' . $object->GetID();
$weight = $this->Application->Conn->GetOne($sql);
$f_weight = Kg2Pounds($weight);
$sOrder['ShippingWeight'] = $f_weight[0].'.'.$f_weight[1];
foreach ($ShippingInfo as $k => $ShippingRow) {
$ShippingCode = $ShippingRow['Code'];
}
if ( $object->GetDBField('ShippingCountry') == 'USA' ) {
$sOrder['ShippingState'] = $object->GetDBField('ShippingState');
$USPSMethod = $ShippingCode;
unset($sOrder['ShippingZip']);
$sOrder['ShippingZip5'] = substr(trim($object->GetDBField('ShippingZip')), 0, 5);
$sOrder['ShippingZip4'] = '';
$sOrder['SubTotal'] = $object->GetDBField('SubTotal');
}
else {
$USPSMethod = array_search($ShippingCode, $aUSPS->intl_types);
$sOrder['ShippingProvince'] = '';
if ( $ShippingCountry == 'CA' ) {
$sOrder['ShippingProvince'] = $object->GetField('ShippingState');
}
// add items
$sql = 'SELECT `Quantity`, `Weight`, `Price`
FROM ' . $item_table . '
WHERE ' . $object->IDField . ' = ' . $object->GetID();
$order_items = $this->Application->Conn->Query($sql);
$i = 1;
$Items = Array();
foreach ($order_items as $k => $order_item) {
$p_weight = Array();
$p_weight = Kg2Pounds($order_item['Weight']);
$Items[$i] = Array('Qty' => $order_item['Quantity'], 'Price' => $order_item['Price'], 'NetPounds' => $p_weight[0], 'NetOunces' => $p_weight[1]);
$i++;
}
$sOrder['Items'] = $Items;
$sOrder['InvoiceNumber'] = $object->GetDBField('OrderNumber');
}
$sOrder['ShippingService'] = $USPSMethod;
// make USPS order
$aUSPS->order = $sOrder;
$usps_data = $aUSPS->PostOrder();
// if errors
if ( array_key_exists('error_number', $usps_data) ) {
return $usps_data;
}
if ( array_key_exists('Postage', $usps_data) ) {
$ShippingPrice = '';
$ShippingPrice = $usps_data['Postage'];
}
$ShippingTracking = '';
if ( isset($usps_data['TrackingNumber']) && $usps_data['TrackingNumber'] != '' ) {
$ShippingTracking = $usps_data['TrackingNumber'];
}
if ( isset($usps_data['PostnetBarCode']) && $usps_data['PostnetBarCode'] != '' && $ShippingTracking == '' ) {
$ShippingTracking = $usps_data['PostnetBarCode'];
}
if ($fake_mode == false) {
$object->SetDBField('ShippingTracking', $ShippingTracking);
$object->Update();
}
else {
$full_path = USPS_LABEL_FOLDER . $ShippingTracking . ".pdf";
if (file_exists($full_path)) {
unlink($full_path);
}
}
return $usps_data;
}
/**
* Downloads shipping tracking bar code, that was already generated by USPS service
*
* @param kEvent $event
*/
function OnDownloadLabel(&$event)
{
$event->status = erSTOP;
ini_set('memory_limit', '300M');
ini_set('max_execution_time', '0');
$object =& $event->getObject();
$file = $object->GetDBField('ShippingTracking').'.pdf';
$full_path = USPS_LABEL_FOLDER.$file;
if (!file_exists($full_path) || !is_file($full_path)) {
return ;
}
$mime = function_exists('mime_content_type') ? mime_content_type($full_path) : 'application/download';
header('Content-type: '.$mime);
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile($full_path);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/orders/orders_item.php
===================================================================
--- branches/5.0.x/in-commerce/units/orders/orders_item.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/orders/orders_item.php (revision 12722)
@@ -1,529 +1,529 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class OrdersItem extends kDBItem
{
function OrdersItem()
{
parent::kDBItem();
$this->ErrorMsgs['credit_card_validation_error'] = $this->Application->Phrase('lu_cc_validation_error');
$this->ErrorMsgs['credit_card_expired'] = $this->Application->Phrase('lu_cc_expired');
}
function Load($id, $id_field_name=null)
{
if( $this->Special == 'sitem' && $id == null || (!$this->IsTempTable() && $id == 0))
{
$this->setID(0);
$this->SetDBField('Number',-1);
$this->SetDBField('SubNumber',-1);
// load previously used search params from session
$search_params = $this->Application->RecallVar('ord.search_search_filter');
if($search_params)
{
$search_params = unserialize($search_params);
foreach($search_params as $search_field => $search_params)
{
$this->SetField($search_field, $search_params['search_value']);
}
$this->UpdateFormattersSubFields(); // used for updating separate virtual date/time fields from DB timestamp (for example)
}
return true;
}
else
{
return parent::Load($id,$id_field_name);
}
}
/**
* Return error message for field
*
* @param string $field
* @return string
* @access public
*/
function GetErrorMsg($field)
{
if( $field != 'OrderNumber' ) return parent::GetErrorMsg($field);
$number['error'] = parent::GetErrorMsg('Number');
$number['pseudo'] = getArrayValue($this->FieldErrors['Number'], 'pseudo');
$subnumber['error'] = parent::GetErrorMsg('SubNumber');
$subnumber['pseudo'] = getArrayValue($this->FieldErrors['SubNumber'], 'pseudo');
// if pseudo match & not empty -> return 1st
// if one of pseudos not empty -> return it
// if we got one pseudo "bad_type" and other pseudo "required", then return "bad_type" error message
if( $number['pseudo'] && ($number['pseudo'] == $subnumber['pseudo']) )
{
return $number['error'];
}
if( $number['pseudo'] && !$subnumber['pseudo'] )
{
return $number['error'];
}
if( !$number['pseudo'] && $subnumber['pseudo'] )
{
return $subnumber['error'];
}
if( $number['pseudo'] == 'bad_type' )
{
return $number['error'];
}
if( $subnumber['pseudo'] == 'bad_type' )
{
return $subnumber['error'];
}
// $msg = '['.$number_error.'('.$number_pseudo.')] ['.$subnumber_error.'] ('.$subnumber_pseudo.')';
//
// return $msg;
}
function SetFieldsFromHash($hash, $set_fields=null)
{
parent::SetFieldsFromHash($hash, $set_fields);
$options = $this->GetFieldOptions('PaymentCCExpDate');
if( $this->GetDirtyField($options['month_field']) || $this->GetDirtyField($options['year_field']) )
{
$this->SetDirtyField('PaymentCCExpDate', 0);
$this->SetField('PaymentCCExpDate', 0);
}
}
/**
* Returns gateway data based on payment type used in order
*
* @return Array
*/
function getGatewayData($pt_id=null)
{
// get Gateway fields
if (!isset($pt_id) || !$pt_id) {
$pt_id = $this->GetDBField('PaymentType');
}
$pt_table = $this->Application->getUnitOption('pt','TableName');
$sql = 'SELECT GatewayId FROM %s WHERE PaymentTypeId = %s';
$gw_id = $this->Conn->GetOne( sprintf($sql, $pt_table, $pt_id) );
$sql = 'SELECT * FROM %s WHERE GatewayId = %s';
$ret = $this->Conn->GetRow( sprintf($sql, TABLE_PREFIX.'Gateways', $gw_id) );
// get Gateway parameters based on payment type
$gwf_table = $this->Application->getUnitOption('gwf','TableName');
$gwfv_table = $this->Application->getUnitOption('gwfv','TableName');
$sql = 'SELECT gwfv.Value, gwf.SystemFieldName
FROM %s gwf
LEFT JOIN %s gwfv ON gwf.GWConfigFieldId = gwfv.GWConfigFieldId
WHERE gwfv.PaymentTypeId = %s AND gwf.GatewayId = %s';
$ret['gw_params'] = $this->Conn->GetCol( sprintf($sql, $gwf_table, $gwfv_table, $pt_id, $gw_id), 'SystemFieldName' );
$ret['gw_params']['gateway_id'] = $gw_id;
if ($this->GetDBField('IsRecurringBilling') && $this->Application->ConfigValue('Comm_AutoProcessRecurringOrders')) {
if (isset($ret['gw_params']['shipping_control'])) {
$ret['gw_params']['shipping_control'] = SHIPPING_CONTROL_DIRECT;
}
}
return $ret;
}
/**
* Checks if tangible items are present in order
*
* @return bool
*/
function HasTangibleItems()
{
$sql = 'SELECT COUNT(*)
FROM '.TABLE_PREFIX.'OrderItems orditems
LEFT JOIN '.TABLE_PREFIX.'Products p ON p.ProductId = orditems.ProductId
WHERE (orditems.OrderId = '.$this->GetID().') AND (p.Type = '.PRODUCT_TYPE_TANGIBLE.')';
return $this->Conn->GetOne($sql) ? true : false;
}
/**
* Calculates tax value of order items based on billing & shipping country specified
*
* @return double
*/
function getTaxPercent()
{
$sql = 'SELECT DestId FROM '.TABLE_PREFIX.'StdDestinations WHERE DestType = %s AND DestAbbr = %s';
$shipping_country_id = (int) $this->Conn->GetOne( sprintf($sql, 1, $this->Conn->qstr($this->GetDBField('ShippingCountry') ) ) );
$shipping_state_id = (int) $this->Conn->GetOne( sprintf($sql, 2, $this->Conn->qstr($this->GetDBField('ShippingState') ) ) );
$shipping_zip = (string) $this->GetDBField('ShippingZip');
$billing_country_id = (int) $this->Conn->GetOne( sprintf($sql, 1, $this->Conn->qstr($this->GetDBField('BillingCountry') ) ) );
$billing_state_id = (int) $this->Conn->GetOne( sprintf($sql, 2, $this->Conn->qstr($this->GetDBField('BillingState') ) ) );
$billing_zip = (string) $this->GetDBField('BillingZip');
/*
$dest_ids = array_diff( array_unique( Array( $shipping_country_id, $shipping_state_id, $billing_country_id, $billing_state_id ) ), Array(0) );
$dest_values = array_diff( array_unique( Array( $this->Conn->qstr($shipping_zip), $this->Conn->qstr($billing_zip) ) ), Array('\'\'') );
*/
$tax = false;
$sql = 'SELECT tx.*
FROM '.$this->Application->getUnitOption('tax', 'TableName').' tx
LEFT JOIN '.$this->Application->getUnitOption('taxdst', 'TableName').' txd ON tx.TaxZoneId = txd.TaxZoneId
WHERE
( txd.StdDestId IN ('.$shipping_country_id.','.$shipping_state_id.')
AND
( (txd.DestValue = "" OR txd.DestValue IS NULL)
OR
txd.DestValue = '.$this->Conn->qstr($shipping_zip).'
)
)
OR
( txd.StdDestId IN ('.$billing_country_id.','.$billing_state_id.')
AND
( (txd.DestValue = "" OR txd.DestValue IS NULL)
OR
txd.DestValue = '.$this->Conn->qstr($billing_zip).'
)
)
ORDER BY tx.TaxValue DESC';
$tax = $this->Conn->GetRow($sql);
if ($tax == false) {
$tax['TaxValue'] = 0;
$tax['ApplyToShipping'] = 0;
$tax['ApplyToProcessing'] = 0;
}
return $tax;
}
function RecalculateTax()
{
$tax = $this->getTaxPercent();
$this->SetDBField( 'VATPercent', $tax['TaxValue'] );
$this->SetDBField( 'ShippingTaxable', $tax['ApplyToShipping']);
$this->SetDBField( 'ProcessingTaxable', $tax['ApplyToProcessing']);
$this->UpdateTotals();
$subtotal = $this->GetDBField('AmountWithoutVAT');
$query = 'SELECT SUM(Quantity * Price) FROM '.TABLE_PREFIX.'OrderItems AS oi
LEFT JOIN '.TABLE_PREFIX.'Products AS p
ON p.ProductId = oi.ProductId
WHERE p.Type = 6 AND oi.OrderId = '.$this->GetDBField('OrderId');
$tax_exempt = $this->Conn->GetOne($query);
if ($tax_exempt) $subtotal -= $tax_exempt;
$this->SetDBField( 'VAT', round($subtotal * $tax['TaxValue'] / 100, 2) );
$this->UpdateTotals();
}
function UpdateTotals()
{
$total = 0;
$total += $this->GetDBField('SubTotal');
if ($this->GetDBField('ShippingTaxable')) $total += $this->GetDBField('ShippingCost');
if ($this->GetDBField('ProcessingTaxable')) $total += $this->GetDBField('ProcessingFee');
$this->SetDBField('AmountWithoutVAT', $total);
$total += $this->GetDBField('VAT');
if (!$this->GetDBField('ShippingTaxable')) $total += $this->GetDBField('ShippingCost');
if (!$this->GetDBField('ProcessingTaxable')) $total += $this->GetDBField('ProcessingFee');
$total += $this->GetDBField('InsuranceFee');
$this->SetDBField('TotalAmount', $total);
}
function getTotalAmount()
{
return $this->GetDBField('SubTotal') +
$this->GetDBField('ShippingCost') +
$this->GetDBField('VAT') +
$this->GetDBField('ProcessingFee') +
$this->GetDBField('InsuranceFee') -
$this->GetDBField('GiftCertificateDiscount');
}
/**
* Check field value by user-defined alghoritm
*
* @param string $field field name
* @param Array $params field options from config
* @return bool
*/
function CustomValidation($field, $params)
{
$res = true;
$res = $res && $this->ValidateCCNumber($field, $params);
$res = $res && $this->ValidateCCExpiration($field, $params);
return $res;
}
function requireCreditCard()
{
$pt_table = $this->Application->getUnitOption('pt', 'TableName');
$sql = 'SELECT RequireCCFields
FROM '.$pt_table.' pt
LEFT JOIN '.TABLE_PREFIX.'Gateways gw ON gw.GatewayId = pt.GatewayId
WHERE pt.PaymentTypeId = '.$this->GetDBField('PaymentType');
return $this->Conn->GetOne($sql);
}
/**
* Check if field value is valid credit card number against credit card type specified
*
* @param string $field field name
* @param Array $params field options from config
* @return bool
* @access private
*/
function ValidateCCNumber($field, $params)
{
$cardtype_field = getArrayValue($params, 'cardtype_field');
$value = $this->GetDBField($field);
if( !$cardtype_field || !$value || !$this->requireCreditCard() ) return true;
if ($this->Application->ConfigValue('Comm_MaskProcessedCreditCards')) {
$mask_found = strpos($value, str_repeat('X', 4)) !== false;
- if ($this->Application->IsAdmin() && $mask_found) {
+ if ($this->Application->isAdminUser && $mask_found) {
// masked card numbers always appear valid in admin
return true;
}
}
if (defined('DEBUG_MODE') && constOn('DBG_PAYMENT_GW')) {
$gw_data = $this->getGatewayData();
$this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] );
$gateway_object =& $this->Application->recallObject( $gw_data['ClassName'] );
$test_numbers = $gateway_object->GetTestCCNumbers();
if (in_array($value, $test_numbers)) return true;
}
$error_field = isset($params['error_field']) ? $params['error_field'] : $field;
// '1' => 'Visa','2' => 'Mastercard', '3' => 'Amex', '4' => 'Discover', 5 => 'Diners Club', 6 => 'JBC'
// Innocent until proven guilty
$cc_valid = true;
// Get rid of any non-digits
$value = ereg_replace("[^[:digit:]]", '', $value);
// Perform card-specific checks, if applicable
switch( $this->GetDBField($cardtype_field) )
{
case 2: // MasterCard
$cc_valid = ereg("^5[1-5].{14}$", $value);
break;
case 1: // Visa
$cc_valid = ereg("^4.{15}$|^4.{12}$", $value);
break;
case 3: // American Express
$cc_valid = ereg("^3[47].{13}$", $value);
break;
case 4: // Discover
$cc_valid = ereg("^6011.{12}$", $value);
break;
case 5: // Diners Club
$cc_valid = ereg("^30[0-5].{11}$|^3[68].{12}$", $value);
break;
case 6: // JBC
$cc_valid = ereg("^3.{15}$|^2131|1800.{11}$", $value);
break;
default:
$this->FieldErrors[$error_field]['pseudo'] = 'credit_card_validation_error';
return false;
break;
}
// The Luhn formula works right to left, so reverse the number.
$value = strrev($value);
$total = 0;
for($x = 0; $x < strlen($value); $x++)
{
$digit = substr($value, $x, 1);
// If it's an odd digit, double it
if( $x / 2 != floor($x/2) )
{
$digit *= 2;
// If the result is two digits, add them
if( strlen($digit) == 2 )
{
$digit = substr($digit, 0, 1) + substr($digit, 1, 1);
}
}
// Add the current digit, doubled and added if applicable, to the Total
$total += $digit;
}
// If it passed (or bypassed) the card-specific check and the Total is
// evenly divisible by 10, it's cool!
if ($cc_valid && $total % 10 == 0)
{
return true;
}
else
{
$this->FieldErrors[$error_field]['pseudo'] = 'credit_card_validation_error';
return false;
}
}
/**
* Check if field value is non-expired credit card expiration date
*
* @param string $field field name
* @param Array $params field options from config
* @return bool
* @access private
*/
function ValidateCCExpiration($field, $params)
{
$formatter = getArrayValue($params, 'formatter');
if( ($formatter != 'kCCDateFormatter') || !$this->requireCreditCard() ) return true;
- if( !$this->Application->IsAdmin() )
- {
+ if(!$this->Application->isAdminUser) {
// validate expiration date only for front
- if( preg_match('/([\d]{2})\/([\d]{2})/', $this->GetDBField($field), $rets) )
- {
+ if (preg_match('/([\d]{2})\/([\d]{2})/', $this->GetDBField($field), $rets)) {
$month = $rets[1];
$year = $rets[2];
$now_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y') );
$day_count = adodb_date('t', adodb_mktime(0, 0, 0, $month, 1, $year) );
$cc_date = adodb_mktime(23, 59, 59, $month, $day_count, $year);
- if($cc_date < $now_date)
- {
+
+ if ($cc_date < $now_date) {
$error_field = isset($params['error_field']) ? $params['error_field'] : $field;
$this->FieldErrors[$error_field]['pseudo'] = 'credit_card_expired';
+
return false;
}
}
}
+
return true;
}
function getNextSubNumber()
{
$table = $this->Application->GetLiveName($this->TableName);
$sql = 'SELECT MAX(SubNumber) FROM '.$table.' WHERE Number = '.$this->GetDBField('Number');
return $this->Conn->GetOne($sql) + 1;
}
function ResetAddress($prefix)
{
$fields = Array('To','Company','Phone','Fax','Email','Address1','Address2','City','State','Zip','Country');
foreach($fields as $field)
{
$this->SetDBField($prefix.$field, $this->Fields[$prefix.$field]['default']);
}
}
function IsProfileAddress($address_type)
{
return $this->Application->GetVar($this->Prefix.'_IsProfileAddress');
}
// ===== Gift Certificates Related =====
function RecalculateGift(&$event)
{
$gc_id = $this->GetDBField('GiftCertificateId');
if ($gc_id < 1) {
return;
}
$gc =& $this->Application->recallObject('gc', null, Array('skip_autoload' => true));
/* @var $gc kDBItem */
$gc->Load($gc_id);
if ($gc->GetDBField('Status') == gcDISABLED) {
// disabled GC
$this->SetDBField('GiftCertificateId', 0);
$this->SetDBField('GiftCertificateDiscount', 0);
// disabled
return;
}
$debit = $gc->GetDBField('Debit') + $this->GetDBField('GiftCertificateDiscount');
$this->UpdateTotals();
$total = $this->GetDBField('TotalAmount');
$gift_certificate_discount = $debit >= $total ? $total : $debit;
$this->SetDBField('TotalAmount', $total - $gift_certificate_discount);
$this->GetDBField('GiftCertificateDiscount', $gift_certificate_discount);
$debit -= $gift_certificate_discount;
$gc->SetDBField('Debit', $debit);
$gc->SetDBField('Status', $debit > 0 ? gcENABLED : gcUSED);
$gc->Update();
if ($gift_certificate_discount == 0) {
$this->RemoveGiftCertificate($object);
$event->SetRedirectParam('checkout_error', 108);
}
$this->SetDBField('GiftCertificateDiscount', $gift_certificate_discount);
}
function RemoveGiftCertificate()
{
$gc_id = $this->GetDBField('GiftCertificateId');
$gc =& $this->Application->recallObject('gc', null, Array('skip_autoload' => true));
/* @var $gc kDBItem */
$gc->Load($gc_id);
$debit = $gc->GetDBField('Debit') + $this->GetDBField('GiftCertificateDiscount');
if ($gc->isLoaded() && ($debit > 0)) {
$gc->SetDBField('Debit', $debit);
$gc->SetDBField('Status', gcENABLED);
$gc->Update();
}
$this->SetDBField('GiftCertificateId', 0);
$this->SetDBField('GiftCertificateDiscount', 0);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/currencies/currencies_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/currencies/currencies_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/currencies/currencies_event_handler.php (revision 12722)
@@ -1,213 +1,213 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class CurrenciesEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
// admin
'OnUpdateRate' => Array('self' => 'add|edit'),
'OnUpdateRates' => Array('self' => 'advanced:update_rate|add|edit'),
'OnDisableUnused' => Array('self' => 'edit'),
// front
'OnChangeCurrency' => Array('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$object =& $event->getObject();
$object->SetDBField('IsPrimary', 1);
$object->Update();
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnBeforeItemUpdate(&$event)
{
$object =& $event->getObject();
if($object->GetDBField('IsPrimary') && $object->Validate())
{
$sql = 'UPDATE '.$this->Application->getUnitOption($this->Prefix, 'TableName').'
SET IsPrimary = 0
WHERE CurrencyId <> '.$object->GetDBField('CurrencyId');
$this->Conn->Query($sql);
$object->SetDBField('Status', 1);
}
$object->SetDBField('Modified_date', adodb_mktime() );
$object->SetDBField('Modified_time', adodb_mktime() );
if($object->GetDBField('Status') == 0)
{
$sql = 'DELETE FROM '.$this->Application->getUnitOption('ptc', 'TableName').
' WHERE CurrencyId='.$object->GetDBField('CurrencyId');
$this->Conn->Query($sql);
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
$object->addFilter('current_lang', 'phr.LanguageId = '.$this->Application->GetVar('m_lang'));
if($event->Special == 'active')
{
$object =& $event->getObject();
$object->addFilter('status_filter', '%1$s.Status = 1');
}
- if ( !$this->Application->IsAdmin() ) {
+ if (!$this->Application->isAdminUser) {
$object->addFilter('status_filter', $object->TableName.'.Status = 1');
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSave(&$event)
{
$this->Application->StoreVar( 'saved_curr_ids', $this->Application->RecallVar($event->Prefix.'_selected_ids') );
parent::OnSave($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnDisableUnused(&$event)
{
if($unused_ids = $this->Application->GetVar('unused_ids'))
{
$sql = 'UPDATE '.$this->Application->getUnitOption($event->Prefix, 'TableName').'
SET Status = 0
WHERE CurrencyId IN('.$unused_ids.') AND IsPrimary <> 1';
$this->Conn->Query($sql);
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdateRate(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$event->CallSubEvent('OnPreSave');
$rate_source = $this->Application->ConfigValue('Comm_ExchangeRateSource');
$rate_source_classes = Array( 2 => 'FRNYCurrencyRates',
3 => 'ECBCurrencyRates',
1 => 'BankLVCurrencyRates'
);
$rates_class = $rate_source_classes[$rate_source];
$rates =& $this->Application->recallObject($rates_class);
$rates->GetRatesData();
$object =& $event->getObject();
$iso = $object->GetDBField('ISO');
$rates->StoreRates($iso);
if($rates->GetRate($iso, 'PRIMARY'))
{
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
$object->FieldErrors['RateToPrimary']['pseudo'] = 'couldnt_retrieve_rate';
$object->ErrorMsgs['couldnt_retrieve_rate'] = $this->Application->Phrase('la_couldnt_retrieve_rate');
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdateRates(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$ids = $this->StoreSelectedIDs($event);
$event->setEventParam('ids', $ids);
$ids = $event->getEventParam('ids');
$object =& $event->getObject();
if(is_array($ids) && $ids[0])
{
$sql = 'SELECT ISO FROM '.$object->TableName.' WHERE CurrencyId IN ('.implode(',', $ids).')';
$iso_list = $this->Conn->GetCol($sql);
}
$rate_source = $this->Application->ConfigValue('Comm_ExchangeRateSource');
$rate_source_classes = Array( 2 => 'FRNYCurrencyRates',
3 => 'ECBCurrencyRates',
1 => 'BankLVCurrencyRates'
);
$rates_class = $rate_source_classes[$rate_source];
$rates =& $this->Application->recallObject($rates_class);
$rates->GetRatesData();
if($iso_list)
{
$rates->StoreRates($iso_list);
}
else
{
$rates->StoreRates();
}
}
function OnChangeCurrency(&$event)
{
$this->Application->StoreVar('curr_iso', $this->Application->GetVar('curr_iso'));
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/products/products_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/products/products_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/products/products_event_handler.php (revision 12722)
@@ -1,1292 +1,1293 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ProductsEventHandler extends kCatDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
// front
'OnCancelAction' => Array('self' => true),
'OnRateProduct' => Array('self' => true),
'OnClearRecent' => Array('self' => true),
'OnRecommendProduct' => Array('self' => true),
// admin
'OnQtyAdd' => Array('self' => 'add|edit'),
'OnQtyRemove' => Array('self' => 'add|edit'),
'OnQtyOrder' => Array('self' => 'add|edit'),
'OnQtyReceiveOrder' => Array('self' => 'add|edit'),
'OnQtyCancelOrder' => Array('self' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
function mapEvents()
{
parent::mapEvents(); // ensure auto-adding of approve/decine and so on events
$product_events = Array( 'OnQtyAdd'=>'InventoryAction',
'OnQtyRemove'=>'InventoryAction',
'OnQtyOrder'=>'InventoryAction',
'OnQtyReceiveOrder'=>'InventoryAction',
'OnQtyCancelOrder'=>'InventoryAction',);
$this->eventMethods = array_merge($this->eventMethods, $product_events);
}
/**
* Sets default processing data for subscriptions
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject();
$product_approve_events = Array(2 => 'p:OnSubscriptionApprove',
4 => 'p:OnDownloadableApprove',
5 => 'p:OnPackageApprove');
$product_type = $object->GetDBField('Type');
$type_found = in_array($product_type, array_keys($product_approve_events) );
if($type_found && !$object->GetDBField('ProcessingData') )
{
$processing_data = Array('ApproveEvent' => $product_approve_events[$product_type] );
$object->SetDBField( 'ProcessingData', serialize($processing_data) );
}
}
/**
* Process product count manipulations
*
* @param kEvent $event
* @access private
*/
function InventoryAction(&$event)
{
$object =& $event->getObject();
$object->SetFieldsFromHash( $this->getSubmittedFields($event) );
if ($object->GetDBField('InventoryStatus') == 2) {
// inventory by options (use first selected combination in grid)
$combination_id = array_shift( array_keys( $this->Application->GetVar('poc_grid') ) );
}
else {
// inventory by product
$combination_id = 0;
}
// save id of selected option combination & preselect it in grid
$this->Application->SetVar('combination_id', $combination_id);
$this->ScheduleInventoryAction($event->Name, $object->GetId(), $object->GetDBField('Qty'), $combination_id);
$object->Validate();
if (!isset($object->FieldErrors['Qty']['pseudo'])){
$this->modifyInventory($event->Name, $object, $object->GetDBField('Qty'), $combination_id);
}
$object->SetDBField('Qty', null);
$event->redirect = false;
}
/**
* Perform inventory action on supplied object
*
* @param string $action event name which is actually called by user
* @param ProductsItem $product
* @param int $qty
* @param int $combination_id
*/
function modifyInventory($action, &$product, $qty, $combination_id)
{
if ($product->GetDBField('InventoryStatus') == 2) {
// save inventory changes to option combination instead of product
$object =& $this->Application->recallObject('poc.-item', null, Array('skip_autoload' => true));
$object->Load($combination_id);
}
elseif ($combination_id > 0) {
// combination id present, but not inventory by combinations => skip
return false;
}
elseif ($product->GetDBField('InventoryStatus') == 1) {
// save inventory changes to product
$object =& $product;
}
else {
// product has inventory actions, but don't use inventory => skip
return false;
}
if (!$object->isLoaded()) {
// product/combination in action doesn't exist in database by now
return false;
}
switch ($action) {
case 'OnQtyAdd':
$object->SetDBField('QtyInStock', $object->GetDBField('QtyInStock') + $qty);
break;
case 'OnQtyRemove':
if ($object->GetDBField('QtyInStock') < $qty) {
$qty = $object->GetDBField('QtyInStock');
}
$object->SetDBField('QtyInStock', $object->GetDBField('QtyInStock') - $qty);
break;
case 'OnQtyOrder':
$object->SetDBField('QtyOnOrder', $object->GetDBField('QtyOnOrder') + $qty);
break;
case 'OnQtyReceiveOrder':
$object->SetDBField('QtyOnOrder', $object->GetDBField('QtyOnOrder') - $qty);
$object->SetDBField('QtyInStock', $object->GetDBField('QtyInStock') + $qty);
break;
case 'OnQtyCancelOrder':
$object->SetDBField('QtyOnOrder', $object->GetDBField('QtyOnOrder') - $qty);
break;
}
return $object->Update();
}
function ScheduleInventoryAction($action, $prod_id, $qty, $combination_id = 0)
{
$inv_actions = $this->Application->RecallVar('inventory_actions');
if (!$inv_actions) {
$inv_actions = Array();
}
else {
$inv_actions = unserialize($inv_actions);
}
array_push($inv_actions, Array('action' => $action, 'product_id' => $prod_id, 'combination_id' => $combination_id, 'qty' => $qty));
$this->Application->StoreVar('inventory_actions', serialize($inv_actions));
}
function RealInventoryAction($action, $prod_id, $qty, $combination_id)
{
$product =& $this->Application->recallObject('p.liveitem', null, Array('skip_autoload' => true));
$product->SwitchToLive();
$product->Load($prod_id);
$this->modifyInventory($action, $product, $qty, $combination_id);
}
function RunScheduledInventoryActions(&$event)
{
$inv_actions = $this->Application->GetVar('inventory_actions');
if (!$inv_actions) {
return;
}
$inv_actions = unserialize($inv_actions);
$products = array();
foreach($inv_actions as $an_action) {
$this->RealInventoryAction($an_action['action'], $an_action['product_id'], $an_action['qty'], $an_action['combination_id']);
array_push($products, $an_action['product_id'].'_'.$an_action['combination_id']);
}
$products = array_unique($products);
if ($products) {
$product_obj =& $this->Application->recallObject('p.liveitem', null, Array('skip_autoload' => true));
$product_obj->SwitchToLive();
foreach ($products as $product_key) {
list($prod_id, $combination_id) = explode('_', $product_key);
$product_obj->Load($prod_id);
$this->FullfillBackOrders($product_obj, $combination_id);
}
}
}
/**
* In case if products arrived into inventory and they are required by old (non processed) orders, then use them (products) in that orders
*
* @param ProductsItem $product
* @param int $combination_id
*/
function FullfillBackOrders(&$product, $combination_id)
{
if ( !$this->Application->ConfigValue('Comm_Process_Backorders_Auto') ) return;
if ($combination_id && ($product->GetDBField('InventoryStatus') == 2)) {
// if combination id present and inventory by combinations
$poc_idfield = $this->Application->getUnitOption('poc', 'IDField');
$poc_tablename = $this->Application->getUnitOption('poc', 'TableName');
$sql = 'SELECT QtyInStock
FROM '.$poc_tablename.'
WHERE '.$poc_idfield.' = '.$combination_id;
$stock_qty = $this->Conn->GetOne($sql);
}
else {
// inventory by product
$stock_qty = $product->GetDBField('QtyInStock');
}
$qty = (int) $stock_qty - $product->GetDBField('QtyInStockMin');
$prod_id = $product->GetID();
if ($prod_id <= 0 || !$prod_id || $qty <= 0) return;
//selecting up to $qty backorders with $prod_id where full qty is not reserved
$query = 'SELECT '.TABLE_PREFIX.'Orders.OrderId
FROM '.TABLE_PREFIX.'OrderItems
LEFT JOIN '.TABLE_PREFIX.'Orders ON '.TABLE_PREFIX.'Orders.OrderId = '.TABLE_PREFIX.'OrderItems.OrderId
WHERE (ProductId = '.$prod_id.') AND (Quantity > QuantityReserved) AND (Status = '.ORDER_STATUS_BACKORDERS.')
GROUP BY '.TABLE_PREFIX.'Orders.OrderId
ORDER BY OrderDate ASC
LIMIT 0,'.$qty; //assuming 1 item per order - minimum possible
$orders = $this->Conn->GetCol($query);
if (!$orders) return;
$order =& $this->Application->recallObject('ord.-inv', null, Array('skip_autoload' => true));
foreach ($orders as $ord_id) {
$order->Load($ord_id);
$email_event_admin =& $this->Application->EmailEventAdmin('BACKORDER.FULLFILL');
//reserve what's possible in any case
$this->Application->HandleEvent( $event, 'ord:OnReserveItems' );
if ($event->status == erSUCCESS) { //
//in case the order is ready to process - process it
$this->Application->HandleEvent( $event, 'ord:OnOrderProcess' );
}
}
}
function OnBeforeDeleteFromLive(&$event)
{
$id = $event->getEventParam('id');
$product =& $this->Application->recallObject($event->Prefix.'.itemlive', null, Array('skip_autoload' => true));
$product->SwitchToLive();
if (!$product->Load($id)) return ; // this will make sure New product will not be overwritten with empty data
$temp =& $this->Application->recallObject($event->Prefix.'.itemtemp', null, Array('skip_autoload' => true));
$temp->SwitchToTemp();
$temp->Load($id);
$temp->SetDBFieldsFromHash($product->FieldValues, Array('QtyInStock','QtyReserved','QtyBackOrdered','QtyOnOrder'));
$temp->Update();
}
function clearSelectedIDs(&$event)
{
parent::clearSelectedIDs($event);
$this->Application->SetVar('inventory_actions', $this->Application->RecallVar('inventory_actions'));
$this->Application->RemoveVar('inventory_actions');
}
function OnSave(&$event)
{
$res = parent::OnSave($event);
if ($event->status == erSUCCESS) {
$this->RunScheduledInventoryActions($event);
}
return $res;
}
function OnPreCreate(&$event)
{
parent::onPreCreate($event);
$object =& $event->GetObject();
$object->SetDBField('Type', $this->Application->GetVar( $event->getPrefixSpecial(true).'_new_type' ));
}
function OnPreSaveAndGo(&$event) {
$event->CallSubEvent('OnPreSave');
$this->LoadItem($event);
$object =& $event->getObject();
$from_type = $object->GetDBField('Type');
if ($event->status==erSUCCESS) {
$this->Application->SetVar($event->Prefix_Special.'_id', $this->Application->GetVar($event->getPrefixSpecial(true).'_GoId'));
$this->LoadItem($event);
$to_type = $object->GetDBField('Type');
if ($from_type != $to_type) {
$from_tabs = $this->GetTabs($from_type);
$from_tab_i = array_search($this->Application->GetVar('t'), $from_tabs);
$to_tabs = $this->GetTabs($to_type);
$to_tab = $this->Application->GetVar('t');
$found = false;
while ( !isset($to_tabs[$from_tab_i]) && $from_tab_i < count($to_tabs)) {
$from_tab_i++;
}
if ( !isset($to_tabs[$from_tab_i]) ) $from_tab_i = 0;
$to_tab = $to_tabs[$from_tab_i];
$event->redirect = $to_tab;
}
}
}
function GetTabs($type)
{
switch($type)
{
case 1:
return Array(
0 => 'in-commerce/products/products_edit',
1 => 'in-commerce/products/products_inventory',
2 => 'in-commerce/products/products_pricing',
3 => 'in-commerce/products/products_categories',
4 => 'in-commerce/products/products_images',
5 => 'in-commerce/products/products_reviews',
6 => 'in-commerce/products/products_custom',
);
case 2:
return Array(
0 => 'in-commerce/products/products_edit',
1 => 'in-commerce/products/products_access',
/*2 => 'in-commerce/products/products_access_pricing',*/
3 => 'in-commerce/products/products_categories',
4 => 'in-commerce/products/products_images',
5 => 'in-commerce/products/products_reviews',
6 => 'in-commerce/products/products_custom',
);
case 3:
return Array(
0 => 'in-commerce/products/products_edit',
2 => 'in-commerce/products/products_access_pricing',
3 => 'in-commerce/products/products_categories',
4 => 'in-commerce/products/products_images',
5 => 'in-commerce/products/products_reviews',
6 => 'in-commerce/products/products_custom',
);
case 4:
return Array(
0 => 'in-commerce/products/products_edit',
2 => 'in-commerce/products/products_files',
3 => 'in-commerce/products/products_categories',
4 => 'in-commerce/products/products_images',
5 => 'in-commerce/products/products_reviews',
6 => 'in-commerce/products/products_custom',
);
}
}
/**
* Return type clauses for list bulding on front
*
* @param kEvent $event
* @return Array
*/
function getTypeClauses(&$event)
{
$types=$event->getEventParam('types');
$except_types=$event->getEventParam('except');
$object =& $event->getObject();
$type_clauses = parent::getTypeClauses($event);
$type_clauses['featured']['include']='%1$s.Featured=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['featured']['except']='%1$s.Featured!=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['featured']['having_filter']=false;
$type_clauses['onsale']['include']='%1$s.OnSale=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['onsale']['except']='%1$s.OnSale!=1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['onsale']['having_filter']=false;
// products from selected manufacturer: begin
$manufacturer = $event->getEventParam('manufacturer');
if ( !$manufacturer ) {
$manufacturer = $this->Application->GetVar('manuf_id');
}
if ( $manufacturer ) {
$type_clauses['manufacturer']['include'] = '%1$s.ManufacturerId='.$manufacturer.' AND PrimaryCat = 1';
$type_clauses['manufacturer']['except'] = '%1$s.ManufacturerId!='.$manufacturer.' AND PrimaryCat = 1';
$type_clauses['manufacturer']['having_filter'] = false;
}
// products from selected manufacturer: end
// recent products: begin
$recent = $this->Application->RecallVar('recent_products');
if ($recent) {
$recent = unserialize($recent);
$type_clauses['recent']['include'] = '%1$s.ProductId IN ('.implode(',', $recent).') AND PrimaryCat = 1';
$type_clauses['recent']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $recent).') AND PrimaryCat = 1';
}
else {
$type_clauses['recent']['include']='0';
$type_clauses['recent']['except']='1';
}
$type_clauses['recent']['having_filter']=false;
// recent products: end
// products already in shopping cart: begin
if (strpos($types, 'in_cart') !== false || strpos($except_types, 'in_cart') !== false) {
$order_id = $this->Application->RecallVar('ord_id');
if ($order_id) {
$in_cart = $this->Conn->GetCol('SELECT ProductId FROM '.TABLE_PREFIX.'OrderItems WHERE OrderId = '.$order_id);
if ($in_cart) {
$type_clauses['in_cart']['include'] = '%1$s.ProductId IN ('.implode(',', $in_cart).') AND PrimaryCat = 1';
$type_clauses['in_cart']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $in_cart).') AND PrimaryCat = 1';
}
else {
$type_clauses['in_cart']['include']='0';
$type_clauses['in_cart']['except']='1';
}
}
else {
$type_clauses['in_cart']['include']='0';
$type_clauses['in_cart']['except']='1';
}
$type_clauses['in_cart']['having_filter']=false;
}
// products already in shopping cart: end
// my downloadable products: begin
if (strpos($types, 'my_downloads') !== false || strpos($except_types, 'my_downloads') !== false)
{
$user_id = $this->Application->RecallVar('user_id');
$my_downloads = ($user_id > 0) ? $this->Conn->GetCol('SELECT ProductId FROM '.TABLE_PREFIX.'UserFileAccess WHERE PortalUserId = '.$user_id) : false;
if ($my_downloads)
{
$type_clauses['my_downloads']['include'] = '%1$s.ProductId IN ('.implode(',', $my_downloads).') AND PrimaryCat = 1';
$type_clauses['my_downloads']['except'] = '%1$s.ProductId NOT IN ('.implode(',', $my_downloads).') AND PrimaryCat = 1';
}
else
{
$type_clauses['my_downloads']['include'] = '0';
$type_clauses['my_downloads']['except'] = '1';
}
$type_clauses['my_downloads']['having_filter'] = false;
}
// my downloadable products: end
// my favorite products: begin
if (strpos($types, 'wish_list') !== false || strpos($except_types, 'wish_list') !== false) {
$sql = 'SELECT ResourceId FROM '.$this->Application->getUnitOption('fav', 'TableName').'
WHERE PortalUserId = '.(int)$this->Application->RecallVar('user_id');
$wishlist_ids = $this->Conn->GetCol($sql);
if ($wishlist_ids) {
$type_clauses['wish_list']['include'] = '%1$s.ResourceId IN ('.implode(',', $wishlist_ids).') AND PrimaryCat = 1';
$type_clauses['wish_list']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $wishlist_ids).') AND PrimaryCat = 1';
}
else {
$type_clauses['wish_list']['include']='0';
$type_clauses['wish_list']['except']='1';
}
$type_clauses['wish_list']['having_filter']=false;
}
// my favorite products: end
// products from package: begin
if (strpos($types, 'content') !== false) {
$object->removeFilter('category_filter');
$object->AddGroupByField('%1$s.ProductId');
$item_type = $this->Application->getUnitOption('p', 'ItemType');
$object_product = &$this->Application->recallObject($event->Prefix);
$content_ids_array = $object_product->GetPackageContentIds();
if (sizeof($content_ids_array)==0) {
$content_ids_array = array('-1');
}
if (sizeof($content_ids_array)>0) {
$type_clauses['content']['include'] = '%1$s.ProductId IN ('.implode(',', $content_ids_array).')';
}
else {
$type_clauses['content']['include']='0';
}
$type_clauses['related']['having_filter']=false;
}
// products from package: end
$object->addFilter('not_virtual', '%1$s.Virtual = 0');
- if ( !$this->Application->IsAdmin() ) {
+ if (!$this->Application->isAdminUser) {
$object->addFilter('expire_filter', '%1$s.Expire IS NULL OR %1$s.Expire > UNIX_TIMESTAMP()');
}
return $type_clauses;
}
function OnClearRecent(&$event)
{
$this->Application->RemoveVar('recent_products');
}
/**
* Occurs, when user rates a product
*
* @param kEvent $event
*/
function OnRateProduct(&$event)
{
$event->redirect_params = Array('pass' => 'all,p');
$event->redirect = $this->Application->GetVar('success_template');
$object =& $event->getObject();
$user_id = ($this->Application->RecallVar('user_id') == 0) ? -2 : $this->Application->RecallVar('user_id');
$sql = ' SELECT * FROM '.TABLE_PREFIX.'SpamControl
WHERE ItemResourceId='.$object->GetDBField('ResourceId').'
AND IPaddress="'.$_SERVER['REMOTE_ADDR'].'"
AND PortalUserId='.$user_id.'
AND DataType="Rating"';
$res = $this->Conn->GetRow($sql);
if( $res && $res['Expire'] < adodb_mktime() )
{
$sql = ' DELETE FROM '.TABLE_PREFIX.'SpamControl
WHERE ItemResourceId='.$object->GetDBField('ResourceId').'
AND IPaddress="'.$_SERVER['REMOTE_ADDR'].'"
AND PortalUserId='.$user_id.'
AND DataType="Rating"';
$this->Conn->Query($sql);
unset($res);
}
$new_rating = $this->Application->GetVar('rating');
if($new_rating !== false && !$res)
{
$rating = $object->GetDBField('CachedRating');
$votes = $object->GetDBField('CachedVotesQty');
$new_votes = $votes + 1;
$rating = (($rating * $votes) + $new_rating) / $new_votes;
$object->SetDBField('CachedRating', $rating);
$object->SetDBField('CachedVotesQty', $new_votes);
$object->Update();
$expire = adodb_mktime() + $this->Application->ConfigValue('product_ReviewDelay_Value') * $this->Application->ConfigValue('product_ReviewDelay_Interval');
$sql = ' INSERT INTO '.TABLE_PREFIX.'SpamControl
(ItemResourceId, IPaddress, PortalUserId, DataType, Expire)
VALUES ('.$object->GetDBField('ResourceId').',
"'.$_SERVER['REMOTE_ADDR'].'",
'.$user_id.',
"Rating",
'.$expire.')';
$this->Conn->Query($sql);
}
else
{
$event->status == erFAIL;
$event->redirect=false;
$object->FieldErrors['CachedRating']['pseudo'] = 'too_frequent';
$object->ErrorMsgs['too_frequent'] = $this->Application->Phrase('lu_ferror_rate_duplicate');
}
}
function OnCancelAction(&$event)
{
$event->redirect_params = Array('pass' => 'all,p');
$event->redirect = $this->Application->GetVar('cancel_template');
}
function OnRecommendProduct(&$event)
{
// used for error reporting only -> rewrite code + theme (by Alex)
$object =& $this->Application->recallObject('u', null, Array('skip_autoload' => true)); // TODO: change theme too
$friend_email = $this->Application->GetVar('friend_email');
$friend_name = $this->Application->GetVar('friend_name');
$my_email = $this->Application->GetVar('your_email');
$my_name = $this->Application->GetVar('your_name');
$my_message = $this->Application->GetVar('your_message');
$send_params = array();
$send_params['to_email']=$friend_email;
$send_params['to_name']=$friend_name;
$send_params['from_email']=$my_email;
$send_params['from_name']=$my_name;
$send_params['message']=$my_message;
if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $friend_email))
{
$user_id = $this->Application->RecallVar('user_id');
$email_event = &$this->Application->EmailEventUser('PRODUCT.SUGGEST', $user_id, $send_params);
$email_event = &$this->Application->EmailEventAdmin('PRODUCT.SUGGEST');
if ($email_event->status == erSUCCESS){
$event->redirect_params = array('opener' => 's', 'pass' => 'all');
$event->redirect = $this->Application->GetVar('template_success');
}
else {
// $event->redirect_params = array('opener' => 's', 'pass' => 'all');
// $event->redirect = $this->Application->GetVar('template_fail');
$object->ErrorMsgs['send_error'] = $this->Application->Phrase('lu_email_send_error');
$object->FieldErrors['Email']['pseudo'] = 'send_error';
$event->status = erFAIL;
}
}
else {
$object->ErrorMsgs['invalid_email'] = $this->Application->Phrase('lu_InvalidEmail');
$object->FieldErrors['Email']['pseudo'] = 'invalid_email';
$event->status = erFAIL;
}
}
/**
* Creates/updates virtual product based on listing type data
*
* @param kEvent $event
*/
function OnSaveVirtualProduct(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$listing_type =& $this->Application->recallObject('lst', null, Array('skip_autoload' => true));
$listing_type->Load($event->MasterEvent->getEventParam('id'));
$product_id = $listing_type->GetDBField('VirtualProductId');
if ($product_id) {
$object->Load($product_id);
}
if (!$listing_type->GetDBField('EnableBuying')) {
if ($product_id) {
// delete virtual product here
$temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp_handler->DeleteItems($event->Prefix, $event->Special, Array($product_id));
$listing_type->SetDBField('VirtualProductId', 0);
$listing_type->Update();
}
return true;
}
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
$object->SetDBField($ml_formatter->LangFieldName('Name'), $listing_type->GetDBField('ShopCartName') );
$object->SetDBField($ml_formatter->LangFieldName('Description'), $listing_type->GetDBField('Description'));
$object->SetDBField('SKU', 'ENHANCE_LINK_'.abs( crc32( $listing_type->GetDBField('Name') ) ) );
if ($product_id) {
$object->Update();
}
else {
$object->SetDBField('Type', 2);
$object->SetDBField('Status', 1);
$object->SetDBField('HotItem', 0);
$object->SetDBField('PopItem', 0);
$object->SetDBField('NewItem', 0);
$object->SetDBField('Virtual', 1);
// $processing_data = Array('ApproveEvent' => 'ls:EnhanceLinkAfterOrderApprove', 'ExpireEvent' => 'ls:ExpireLink');
$processing_data = Array( 'ApproveEvent' => 'ls:EnhanceLinkAfterOrderApprove',
'DenyEvent' => 'ls:EnhanceLinkAfterOrderDeny',
'CompleteOrderEvent' => 'ls:EnhancedLinkOnCompleteOrder',
'ExpireEvent' => 'ls:ExpireLink',
'HasNewProcessing' => 1);
$object->SetDBField('ProcessingData', serialize($processing_data));
$object->Create();
$listing_type->SetDBField('VirtualProductId', $object->GetID());
$listing_type->Update();
}
$additiona_fields = Array( 'AccessDuration' => $listing_type->GetDBField('Duration'),
'AccessUnit' => $listing_type->GetDBField('DurationType'),
);
$this->setPrimaryPrice($object->GetID(), (double)$listing_type->GetDBField('Price'), $additiona_fields);
}
/**
* [HOOK] Deletes virtual product when listing type is deleted
*
* @param kEvent $event
*/
function OnDeleteListingType(&$event)
{
$listing_type = $event->MasterEvent->getObject();
$product_id = $listing_type->GetDBField('VirtualProductId');
if ($product_id) {
$temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp_handler->DeleteItems($event->Prefix, $event->Special, Array($product_id));
}
}
/**
* Extends user membership in group when his order is approved
*
* @param kEvent $event
*/
function OnSubscriptionApprove(&$event)
{
$field_values = $event->getEventParam('field_values');
$item_data = unserialize($field_values['ItemData']);
if (!getArrayValue($item_data,'PortalGroupId')) {
// is subscription product, but no group defined in it's properties
trigger_error('Invalid product <b>'.$field_values['ProductName'].'</b> (id: '.$field_values['ProductId'].')');
return false;
}
$order_table = $this->Application->getUnitOption('ord', 'TableName');
$order_idfield = $this->Application->getUnitOption('ord', 'IDField');
$sql = 'SELECT PortalUserId FROM %s WHERE %s = %s';
$user_id = $this->Conn->GetOne( sprintf($sql, $order_table, $order_idfield, $field_values['OrderId']) );
$group_id = $item_data['PortalGroupId'];
$duration = $item_data['Duration'];
$sql = 'SELECT * FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId = %s';
$user_groups = $this->Conn->Query( sprintf($sql, $user_id), 'GroupId' );
$sql = 'REPLACE INTO '.TABLE_PREFIX.'UserGroup (PortalUserId,GroupId,MembershipExpires,PrimaryGroup) VALUES (%s,%s,%s,%s)';
if( !isset($user_groups[$group_id]) )
{
$primary_group = count($user_groups) == 0 ? 1 : 0;
$expire = adodb_mktime() + $duration;
}
else {
$primary_group = $user_groups[$group_id]['PrimaryGroup'];
$expire = $user_groups[$group_id]['MembershipExpires'];
$expire = $expire < adodb_mktime() ? adodb_mktime() + $duration : $expire + $duration;
}
/*
// Customization healtheconomics.org
if ($item_data['DurationType'] == 2) {
$expire = $item_data['AccessExpiration'];
}
// Customization healtheconomics.org --
*/
$this->Conn->Query( sprintf($sql, $user_id, $group_id, $expire, $primary_group) );
$sub_order =& $this->Application->recallObject('ord.-sub'.$event->getEventParam('next_sub_number'), 'ord');
$sub_order->SetDBField('IsRecurringBilling', getArrayValue($item_data, 'IsRecurringBilling') ? 1 : 0);
$sub_order->SetDBField('GroupId', $group_id);
$sub_order->SetDBField('NextCharge_date', $expire);
$sub_order->SetDBField('NextCharge_time', $expire);
}
function OnDownloadableApprove(&$event)
{
$field_values = $event->getEventParam('field_values');
$product_id = $field_values['ProductId'];
$sql = 'SELECT PortalUserId FROM '.$this->Application->getUnitOption('ord', 'TableName').'
WHERE OrderId = '.$field_values['OrderId'];
$user_id = $this->Conn->GetOne($sql);
$sql = 'INSERT INTO '.TABLE_PREFIX.'UserFileAccess VALUES("", '.$product_id.', '.$user_id.')';
$this->Conn->Query($sql);
}
function OnPackageApprove(&$event){
$field_values = $event->getEventParam('field_values');
$item_data = unserialize($field_values['ItemData']);
$package_content_ids = $item_data['PackageContent'];
$object_item = &$this->Application->recallObject('p.packageitem', null, array('skip_autoload'=>true));
foreach ($package_content_ids as $package_item_id) {
$object_field_values = array();
// query processing data from product and run approve event
$sql = 'SELECT ProcessingData FROM '.TABLE_PREFIX.'Products WHERE ProductId = '.$package_item_id;
$processing_data = $this->Conn->GetOne($sql);
if($processing_data)
{
$processing_data = unserialize($processing_data);
$approve_event = new kEvent($processing_data['ApproveEvent']);
//$order_item_fields = $this->Conn->GetRow('SELECT * FROM '.TABLE_PREFIX.'OrderItems WHERE OrderItemId = '.$grouping_data[1]);
$object_item->Load($package_item_id);
$object_field_values['OrderId'] = $field_values['OrderId'];
$object_field_values['ProductId'] = $package_item_id;
$object_field_values['ItemData'] = serialize($item_data['PackageItemsItemData'][$package_item_id]);
$approve_event->setEventParam('field_values', $object_field_values);
$this->Application->HandleEvent($approve_event);
}
}
}
/**
* Checks, that all required product options are filled in before product is saved
*
* @param kEvent $event
*/
function OnPreSave(&$event)
{
$this->CheckRequiredOptions($event);
parent::OnPreSave($event);
}
/**
* Set new price to ProductsPricing
*
* @param kEvent $event
*/
function OnAfterItemCreate(&$event)
{
parent::OnAfterItemCreate($event);
$this->_updateProductPrice($event);
}
/**
* Set new price to ProductsPricing
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
parent::OnAfterItemUpdate($event);
$this->_updateProductPrice($event);
}
/**
* Updates product's primary price based on Price virtual field value
*
* @param kEvent $event
*/
function _updateProductPrice(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$price = $object->GetDBField('Price');
// always create primary pricing, to show on Pricing tab (in admin) for tangible products
$force_create = ($object->GetDBField('Type') == PRODUCT_TYPE_TANGIBLE) && is_null($price);
if ($force_create || ($price != $object->GetOriginalField('Price'))) {
// new product OR price was changed in virtual field
$this->setPrimaryPrice($object->GetID(), (float)$price);
}
}
function CheckRequiredOptions(&$event)
{
$object =& $event->getObject();
if ($object->GetDBField('ProductId') == '') return ; // if product does not have ID - it's not yet created
$opt_object =& $this->Application->recallObject('po', null, Array('skip_autoload' => true) );
$has_required = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$opt_object->TableName.' WHERE Required = 1 AND ProductId = '.$object->GetDBField('ProductId'));
//we need to imitate data sumbit, as parent' PreSave sets object values from $items_info
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$items_info[$object->GetDBField('ProductId')]['HasRequiredOptions'] = $has_required ? 1:0;
$this->Application->SetVar($event->getPrefixSpecial(true), $items_info);
$object->SetDBField('HasRequiredOptions', $has_required ? 1:0);
}
/**
* Sets required price in primary price backed, if it's missing, then create it
*
* @param int $product_id
* @param double $price
* @param Array $additional_fields
* @return bool
*/
function setPrimaryPrice($product_id, $price, $additional_fields = Array())
{
$pr_object =& $this->Application->recallObject('pr.-item', null, Array('skip_autoload' => true) );
/* @var $pr_object kDBItem */
$pr_object->Load( Array('ProductId' => $product_id, 'IsPrimary' => 1) );
$sql = 'SELECT COUNT(*) FROM '.$pr_object->TableName.' WHERE ProductId = '.$product_id;
$has_pricings = $this->Conn->GetOne($sql);
if ($additional_fields) {
$pr_object->SetDBFieldsFromHash($additional_fields);
}
if( ($price === false) && $has_pricings ) return false;
if( $pr_object->isLoaded() )
{
$pr_object->SetField('Price', $price);
return $pr_object->Update();
}
else
{
$group_id = $this->Application->ConfigValue('User_LoggedInGroup');
$field_values = Array('ProductId' => $product_id, 'IsPrimary' => 1, 'MinQty' => 1, 'MaxQty' => -1, 'GroupId'=>$group_id);
$pr_object->SetDBFieldsFromHash($field_values);
$pr_object->SetField('Price', $price);
$ret = $pr_object->Create();
if ($pr_object->mode == 't') {
$pr_object->setTempID();
}
return $ret;
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
$product_id = $event->getEventParam('id');
if(!$product_id)
{
return;
}
$sql = 'DELETE FROM '.TABLE_PREFIX.'UserFileAccess
WHERE ProductId = '.$product_id;
$this->Conn->Query($sql);
}
/**
* Load price from temp table if product mode is temp table
*
* @param kEvent $event
*/
function OnAfterItemLoad(&$event)
{
parent::OnAfterItemLoad($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$a_pricing = $object->getPrimaryPricing();
if (!$a_pricing) {
// pricing doesn't exist for new products
$price = $cost = null;
}
else {
$price = (float)$a_pricing['Price'];
$cost = (float)$a_pricing['Cost'];
}
// set original fields to use them in OnAfterItemCreate/OnAfterItemUpdate later
$object->SetDBField('Price', $price);
$object->SetOriginalField('Price', $price);
$object->SetDBField('Cost', $cost);
$object->SetOriginalField('Cost', $cost);
}
/**
* Allows to add products to package besides all that parent method does
*
* @param kEvent $event
*/
function OnProcessSelected(&$event)
{
$dst_field = $this->Application->RecallVar('dst_field');
if ($dst_field == 'PackageContent') {
$this->OnAddToPackage($event);
}
elseif ($dst_field == 'AssignedCoupon') {
$coupon_id = $this->Application->GetVar('selected_ids');
$object =& $event->getObject();
$object->SetDBField('AssignedCoupon', $coupon_id);
$this->RemoveRequiredFields($object);
$object->Update();
}
else {
parent::OnProcessSelected($event);
}
$this->finalizePopup($event);
}
/**
* Called when some products are selected in products selector for this prefix
*
* @param kEvent $event
*/
function OnAddToPackage(&$event)
{
$selected_ids = $this->Application->GetVar('selected_ids');
// update current package content with selected products
$object =& $event->getObject();
/* @var $object ProductsItem */
$product_ids = $selected_ids['p'] ? explode(',', $selected_ids['p']) : Array();
if ($product_ids) {
$current_ids = $object->GetPackageContentIds();
$current_ids = array_unique(array_merge($current_ids, $product_ids));
// remove package product from selected list
$this_product = array_search($object->GetID(), $current_ids);
if ($this_product !== false) {
unset($current_ids[$this_product]);
}
$dst_field = $this->Application->RecallVar('dst_field');
$object->SetDBField($dst_field, '|'.implode('|', $current_ids).'|');
$object->Update();
$this->ProcessPackageItems($event);
}
$this->finalizePopup($event);
}
function ProcessPackageItems(&$event)
{
//$this->Application->SetVar('p_mode', 't');
$object =& $event->getObject();
/* @var $object ProductsItem */
$content_ids = $object->GetPackageContentIds();
if (sizeof($content_ids) > 0) {
$total_weight = $this->Conn->GetOne('SELECT SUM(Weight) FROM '.TABLE_PREFIX.'Products WHERE ProductId IN ('.implode(', ', $content_ids).') AND Type=1');
if (!$total_weight) $total_weight = 0;
$this->Conn->Query('UPDATE '.$object->TableName.' SET Weight='.$total_weight.' WHERE ProductId='.$object->GetID());
}
/*
$this->Application->SetVar('p_mode', false);
$list = &$this->Application->recallObject('p.content', 'p_List', array('types'=>'content'));
$this->Application->SetVar('p_mode', 't');
$list->Query();
$total_weight_a = 0;
$total_weight_b = 0;
$list->GoFirst();
while (!$list->EOL())
{
if ($list->GetDBField('Type')==1){
$total_weight_a += $list->GetField('Weight_a');
$total_weight_b += $list->GetField('Weight_b');
}
$list->GoNext();
}
$object->SetField('Weight_a', $total_weight_a);
$object->SetField('Weight_b', $total_weight_b);
*/
//$object->Update();
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSaveItems(&$event)
{
//$event->CallSubEvent('OnUpdate');
$event->redirect = false;
//$event->redirect_params = Array('opener'=>'s','pass'=>'all,p');
}
/**
* Removes product from package
*
* @param kEvent $event
*/
function OnRemovePackageItem(&$event) {
$this->Application->SetVar('p_mode', 't');
$object =& $event->getObject();
$items_info = $this->Application->GetVar('p_content');
if($items_info)
{
$product_ids = array_keys($items_info);
$current_ids = $object->GetPackageContentIds();
$current_ids_flip = array_flip($current_ids);
foreach($product_ids as $key=>$val){
unset($current_ids_flip[$val]);
}
$current_ids = array_keys($current_ids_flip);
$current_ids_str = '|'.implode('|', array_unique($current_ids)).'|';
$object->SetDBField('PackageContent', $current_ids_str);
}
$object->Update();
$this->ProcessPackageItems($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnBeforeItemDelete(&$event){
$object = &$event->getObject();
$product_includes_in = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'Products WHERE PackageContent LIKE "%|'.$object->GetID().'%"');
if ($product_includes_in > 0){
$event->status=erFAIL;
}
}
/**
* Returns specific to each item type columns only
*
* @param kEvent $event
* @return Array
*/
function getCustomExportColumns(&$event)
{
$columns = parent::getCustomExportColumns($event);
$new_columns = Array(
'__VIRTUAL__Price' => 'Price',
'__VIRTUAL__Cost' => 'Cost',
);
return array_merge_recursive2($columns, $new_columns);
}
/**
* Sets non standart virtual fields (e.g. to other tables)
*
* @param kEvent $event
*/
function setCustomExportColumns(&$event)
{
parent::setCustomExportColumns($event);
$object =& $event->getObject();
$this->setPrimaryPrice($object->GetID(), (double)$object->GetDBField('Price'), Array('Cost' => (double)$object->GetDBField('Cost')) );
}
function OnPreSaveAndOpenPopup(&$event)
{
$object =& $event->getObject();
$this->RemoveRequiredFields($object);
$event->CallSubEvent('OnPreSave');
$event->redirect = $this->Application->GetVar('t');
// pass ID too, in case if product is created by OnPreSave call to ensure proper editing
$event->redirect_params = Array(
'pass' => 'all',
$event->getPrefixSpecial(true).'_id' => $object->GetID(),
);
}
function getPassedID(&$event)
{
$event->setEventParam('raise_warnings', 0);
$passed = parent::getPassedID($event);
if ($passed) {
return $passed;
}
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
// we may get product id out of OrderItem, if it exists
$ord_item =& $this->Application->recallObject('orditems', null, Array ('raise_warnings' => 0));
+
if ($ord_item->GetDBField('ProductId')) {
$passed = $ord_item->GetDBField('ProductId');
}
}
return $passed;
}
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
if (!$this->Application->LoggedIn()) {
return ;
}
$user_id = $this->Application->RecallVar('user_id');
$calculated_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields');
$sql = 'SELECT GroupId
FROM ' . TABLE_PREFIX . 'UserGroup
WHERE PrimaryGroup = 1 AND PortalUserId = ' . $user_id;
$primary_group = $this->Conn->GetOne($sql);
if (!$primary_group) {
return;
}
$sub_select = ' SELECT pp.Price
FROM ' . TABLE_PREFIX . 'ProductsPricing AS pp
WHERE pp.ProductId = %1$s.ProductId AND GroupId = ' . $primary_group . '
ORDER BY MinQty
LIMIT 0,1';
$calculated_fields['']['Price'] = 'IFNULL((' . $sub_select . '), ' . $calculated_fields['']['Price'] . ')';
$this->Application->setUnitOption($event->Prefix, 'CalculatedFields', $calculated_fields);
}
/**
* Starts product editing, remove any pending inventory actions
*
* @param kEvent $event
*/
function OnEdit(&$event)
{
$this->Application->RemoveVar('inventory_actions');
parent::OnEdit($event);
}
/**
* Adds "Shop Cart" tab on paid listing type editing tab
*
* @param kEvent $event
*/
function OnModifyPaidListingConfig(&$event)
{
$edit_tab_presets = $this->Application->getUnitOption($event->MasterEvent->Prefix, 'EditTabPresets');
$edit_tab_presets['Default']['shopping_cart'] = Array ('title' => 'la_tab_ShopCartEntry', 't' => 'in-commerce/paid_listings/paid_listing_type_shopcart', 'priority' => 2);
$this->Application->setUnitOption($event->MasterEvent->Prefix, 'EditTabPresets', $edit_tab_presets);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/products/products_item.php
===================================================================
--- branches/5.0.x/in-commerce/units/products/products_item.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/products/products_item.php (revision 12722)
@@ -1,62 +1,67 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ProductsItem extends kCatDBItem
{
function GetPackageContentIds()
{
$ids_string = trim($this->GetDBField('PackageContent'), '|');
if ($ids_string) {
$ids_array = explode('|', $ids_string);
return $ids_array;
}
else {
return array();
}
}
/**
* Returns field values from primary pricing for product
*
* @return array
*/
function getPrimaryPricing()
{
- if (!$this->Application->IsAdmin()) {
+ if (!$this->Application->isAdminUser) {
$user_id = $this->Application->RecallVar('user_id');
$primary_group = $user_id != -2 ? $this->Conn->GetOne('SELECT GroupId FROM '.TABLE_PREFIX.'UserGroup WHERE PrimaryGroup = 1 AND PortalUserId = '.$user_id) : false;
+
if ($primary_group) {
$sql = 'SELECT Price, Cost
FROM '.TABLE_PREFIX.'ProductsPricing
WHERE (ProductId = '.$this->GetID().') AND (GroupId = '.$primary_group.')
ORDER BY MinQty';
$a_values = $this->Conn->GetRow($sql);
+
if ($a_values !== false) {
return $a_values;
}
}
}
+
$pr_table = $this->Application->getUnitOption('pr', 'TableName');
+
if ($this->mode == 't') {
$pr_table = $this->Application->GetTempName($pr_table, 'prefix:'.$this->Prefix);
}
$sql = 'SELECT Price, Cost
FROM '.$pr_table.'
WHERE ('.$this->IDField.' = '.$this->GetID().') AND (IsPrimary = 1)';
+
return $this->Conn->GetRow($sql);
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/affiliates/affiliates_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/affiliates/affiliates_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/affiliates/affiliates_event_handler.php (revision 12722)
@@ -1,534 +1,534 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class AffiliatesEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnItemBuild' => Array ('self' => true),
'OnChangePaymentType' => Array ('self' => true),
'OnBecomeAffiliate' => Array ('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Generate new affiliate code
*
* @param kEvent $event
* @return string
*/
function generateAffiliateCode(&$event)
{
// accepts 1 - 36
$number_length = 11;
$num_chars = Array( '1'=>'a','2'=>'b','3'=>'c','4'=>'d','5'=>'e','6'=>'f',
'7'=>'g','8'=>'h','9'=>'i','10'=>'j','11'=>'k','12'=>'l',
'13'=>'m','14'=>'n','15'=>'o','16'=>'p','17'=>'q','18'=>'r',
'19'=>'s','20'=>'t','21'=>'u','22'=>'v','23'=>'w','24'=>'x',
'25'=>'y','26'=>'z','27'=>'0','28'=>'1','29'=>'2','30'=>'3',
'31'=>'4','32'=>'5','33'=>'6','34'=>'7','35'=>'8','36'=>'9');
$ret = '';
for($i=1; $i<=$number_length; $i++)
{
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,36);
$ret .= $num_chars[$num];
}
$ret = strtoupper($ret);
$idfield = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT %s FROM %s WHERE AffiliateCode = %s';
$code_found = $this->Conn->GetOne( sprintf($sql, $idfield, $table, $this->Conn->qstr($ret) ) );
if($code_found) return $this->generateAffiliateCode($event);
return $ret;
}
/**
* Creates new affiliate code when new affiliate is created
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject( Array('skip_autoload'=>true) );
$object->SetDBField('AffiliateCode', $this->generateAffiliateCode($event) );
}
/**
* Stores affiliate id using method from Config (session or cookie) if correct code is present in url
*
* @param kEvent $event
* @return bool
*/
function OnStoreAffiliate(&$event)
{
$event->Init($event->Prefix, '-item');
$object =& $event->getObject( Array('skip_autoload'=>true) );
$affiliate_storage_method = $this->Application->ConfigValue('Comm_AffiliateStorageMethod');
$affiliate = $this->Application->GetVar('affiliate');
if($affiliate)
{
$object->Load($affiliate, 'AffiliateCode');
}
elseif($affiliate_storage_method == 2)
{
$affiliate_id = $this->Application->GetVar('affiliate_id');
$object->Load($affiliate_id);
}
if( $object->isLoaded() && ($object->GetDBField('Status') == 1) )
{
// user is found with such email
$affiliate_user =& $this->Application->recallObject('u.affiliate', null, Array('skip_autoload'=>true) );
$affiliate_user->Load( $object->GetDBField('PortalUserId') );
if($affiliate_user->GetDBField('Status') == 1)
{
$affiliate_id = $object->GetDBField('AffiliateId');
$this->Application->setVisitField('AffiliateId', $affiliate_id);
if($affiliate_storage_method == 1)
{
$this->Application->StoreVar('affiliate_id', $affiliate_id); // per session
}
else
{
setcookie('affiliate_id', $affiliate_id, $this->getCookieExpiration(), BASE_PATH, '.'.SERVER_NAME); // in cookie
}
}
}
}
/**
* Returns affiliate cookie expiration date
*
* @return int
*/
function getCookieExpiration()
{
$expire = $this->Application->ConfigValue('Comm_AffiliateCookieDuration'); // days
return adodb_mktime() + $expire * 24 * 60 * 60;
}
/**
* Calculate what amount is earned by affiliate based on it's affiliate plan & store it
*
* @param kEvent $event
* @author Alex
*/
function OnOrderApprove(&$event)
{
$order =& $this->Application->recallObject( $event->getEventParam('Order_PrefixSpecial') );
$affiliate_id = $order->GetDBField('AffiliateId');
if(!$affiliate_id) return false;
$object =& $event->getObject( Array('ship_autoload' => true) );
if( $object->Load($affiliate_id) )
{
$affiliate_plan =& $this->Application->recallObject('ap', null, Array('skip_autoload'=>true) );
$affiliate_plan->Load( $object->GetDBField('AffiliatePlanId') );
if( $affiliate_plan->isLoaded() )
{
$sql = 'SELECT SUM(Quantity) FROM %s WHERE OrderId = %s';
$orderitems_table = $this->Application->getUnitOption('orditems', 'TableName');
$items_sold = $this->Conn->GetOne( sprintf($sql, $orderitems_table, $order->GetID() ) );
$object->SetDBField('AccumulatedAmount', $object->GetDBField('AccumulatedAmount') + $order->GetDBField('TotalAmount') );
$object->SetDBField('ItemsSold', $object->GetDBField('ItemsSold') + $items_sold);
switch( $affiliate_plan->GetDBField('PlanType') )
{
case 1: // by amount
$value = $object->GetDBField('AccumulatedAmount');
break;
case 2: // by items sold (count)
$value = $object->GetDBField('ItemsSold');
break;
}
$apb_table = $this->Application->getUnitOption('apbrackets', 'TableName');
$sql = 'SELECT Percent FROM %1$s WHERE (%2$s >= FromAmount) AND ( (%2$s <= ToAmount) OR (ToAmount = -1) ) AND (AffiliatePlanId = %3$s)';
$commission_percent = $this->Conn->GetOne( sprintf($sql, $apb_table, $this->Conn->qstr($value), $affiliate_plan->GetID() ) );
// process only orders of current affiliate from period start to this order date
$period_ends = $order->GetDBField('OrderDate');
$period_starts = $this->getPeriodStartTS( $period_ends, $affiliate_plan->GetDBField('ResetInterval') );
$sql = 'SELECT AffiliateCommission, (SubTotal+ShippingCost+VAT) AS TotalAmount, OrderId FROM '.$order->TableName.' WHERE OrderDate >= %s AND OrderDate <= %s';
$amount_to_pay_before = 0;
$amount_to_pay_after = 0;
$order_update_sql = 'UPDATE '.$order->TableName.' SET AffiliateCommission = %s WHERE '.$order->IDField.' = %s';
$orders = $this->Conn->Query( sprintf($sql, $period_starts, $period_ends), 'OrderId' );
if($orders)
{
foreach($orders as $order_id => $order_data)
{
$amount_to_pay_before += $order_data['AffiliateCommission'];
$commission = $order_data['TotalAmount'] * ($commission_percent / 100);
$this->Conn->Query( sprintf($order_update_sql, $this->Conn->qstr($commission), $order_id) );
$amount_to_pay_after += $commission;
}
}
$object->SetDBField('AmountToPay', $object->GetDBField('AmountToPay') - $amount_to_pay_before + $amount_to_pay_after );
$object->SetDBField('LastOrderDate_date', $order->GetDBField('OrderDate_date') );
$object->SetDBField('LastOrderDate_time', $order->GetDBField('OrderDate_time') );
$object->Update();
$order->SetDBField('AffiliateCommission', $commission); // set last commission to this order, because ApproveEvent was called for him
}
}
}
function OnCheckAffiliateAgreement(&$event)
{
$user_object =& $this->Application->recallObject('u', null, Array('skip_autoload' => true));
$items_info = $this->Application->GetVar('u');
if($items_info)
{
list($id,$field_values) = each($items_info);
$user_object->SetFieldsFromHash($field_values);
$user_object->setID($id);
$user_object->setLogin();
}
$require_affiliate = ($this->Application->GetVar('RegisterAsAffiliate') == 'on');
if($require_affiliate && !$this->Application->GetVar('AgreeToAffiliateTerms') )
{
$this->Application->SetVar('MustAgreeToTerms', 1);
$event->MasterEvent->status = erFATAL;
}
if($require_affiliate && !$this->Application->GetVar('SSN') )
{
$this->Application->SetVar('SSNRequiredError', 1);
$event->MasterEvent->status = erFATAL;
}
if( ($event->MasterEvent->status == erFATAL) && $items_info ) $user_object->Validate();
}
/**
* [AFTER HOOK] to u:OnCreate
*
* @param kEvent $event
*/
function OnRegisterAsAffiliate(&$event)
{
if($this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != erSUCCESS)
{
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$sql = 'SELECT AffiliatePlanId FROM '.$this->Application->getUnitOption('ap', 'TableName').'
WHERE IsPrimary = 1';
$affiliate_plan = $this->Conn->GetOne($sql);
$object->SetDBField('PortalUserId', $this->Application->GetVar('u.current_id'));
$object->SetDBField('Status', 2);
$object->SetDBField('AffiliatePlanId', $affiliate_plan);
$object->SetDBField('SSN', $this->Application->GetVar('SSN'));
$object->SetDBField('Comments', $this->Application->GetVar('Comments'));
$object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId'));
$object->Create();
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTER', $this->Application->GetVar('u.current_id'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTER');
}
function OnBecomeAffiliate(&$event)
{
if(!$this->Application->GetVar('AgreeToAffiliateTerms'))
{
$this->Application->SetVar('MustAgreeToTerms', 1);
$event->status = erFATAL;
}
if(!$this->Application->GetVar('SSN'))
{
$this->Application->SetVar('SSNRequiredError', 1);
$event->status = erFATAL;
}
if($event->status == erFATAL)
{
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$sql = 'SELECT AffiliatePlanId FROM '.$this->Application->getUnitOption('ap', 'TableName').'
WHERE IsPrimary = 1';
$affiliate_plan = $this->Conn->GetOne($sql);
$object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id'));
$object->SetDBField('Status', 2);
$object->SetDBField('AffiliatePlanId', $affiliate_plan);
$object->SetDBField('SSN', $this->Application->GetVar('SSN'));
$object->SetDBField('Comments', $this->Application->GetVar('Comments'));
$object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId'));
$object->Create();
if($next_template = $this->Application->GetVar('next_template'))
{
$event->redirect = $next_template;
}
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTER', $this->Application->RecallVar('user_id'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTER');
}
function OnChangePaymentType(&$event)
{
$user_id = $this->Application->RecallVar('user_id');
$object =& $event->getObject( Array('skip_autoload' => true) );
$object->Load( Array('PortalUserId' => $user_id) );
$object->SetDBField('Comments', $this->Application->GetVar('Comments'));
$object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId'));
$object->Update();
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.PAYMENT.TYPE.CHANGED', $user_id);
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT.TYPE.CHANGED');
$event->redirect = $this->Application->GetVar('next_template');
$event->status = erSUCCESS;
}
/**
* If new payments made, then send email about that
*
* @param kEvent $event
*/
function OnBeforeDeleteFromLive(&$event)
{
$payment_object =& $this->Application->recallObject('apayments', 'apayments', Array('skip_autoload' => true) );
$id = $event->getEventParam('id');
$ap_table = $this->Application->getUnitOption('apayments', 'TableName');
$sql = 'SELECT AffiliatePaymentId FROM '.$ap_table.' WHERE AffiliateId = '.$id;
$live_ids = $this->Conn->GetCol($sql);
$sql = 'SELECT AffiliatePaymentId FROM '.$payment_object->TableName.' WHERE AffiliateId = '.$id;
$temp_ids = $this->Conn->GetCol($sql);
$new_ids = array_diff($temp_ids, $live_ids);
foreach($new_ids as $payment_id)
{
$payment_object->Load($payment_id);
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.PAYMENT', $payment_object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT');
}
$object =& $event->getObject();
$passed_id = $event->getEventParam('id');
if( $object->GetID() != $passed_id )
{
$object->Load($passed_id);
}
$sql = 'SELECT Status FROM '.$this->Application->getUnitOption( $event->Prefix, 'TableName' ).' WHERE '.$object->IDField.' = '.$object->GetID();
$old_status = $this->Conn->GetOne($sql);
if( $old_status == 2 && $object->GetDBField('Status') == 1 )
{
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED');
}
}
/**
* Resets statistics (accumulated amount & items sold) for affiliates based on ResetInterval in their plan
*
* @param kEvent $event
* @author Alex
*/
function OnResetStatistics(&$event)
{
$intervals = Array(86400 => 'la_day', 604800 => 'la_week', 2628000 => 'la_month', 7884000 => 'la_quartely', 31536000 => 'la_year');
$affil_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$ap_table = $this->Application->getUnitOption('ap', 'TableName');
$sql = 'SELECT AffiliateId
FROM '.$affil_table.' a
LEFT JOIN '.$ap_table.' ap ON a.AffiliatePlanId = ap.AffiliatePlanId';
$base_time = adodb_mktime();
$where_clause = Array();
foreach($intervals as $interval_length => $interval_description)
{
$start_timestamp = $this->getPeriodStartTS($base_time, $interval_length);
$where_clause[] = 'ap.ResetInterval = '.$interval_length.' AND LastOrderDate < '.$start_timestamp;
}
$sql .= ' WHERE ('.implode(') OR (', $where_clause).')';
$affiliate_ids = $this->Conn->GetCol($sql);
if( $this->Application->isDebugMode() )
{
$this->Application->Debugger->appendHTML('Affiliates Pending Totals Reset: ');
print_pre($affiliate_ids);
}
$sql = 'UPDATE '.$affil_table.' SET AccumulatedAmount = 0, ItemsSold = 0, LastOrderDate = %s WHERE AffiliateId IN (%s)';
if($affiliate_ids) $this->Conn->Query( sprintf($sql, $base_time, implode(',', $affiliate_ids) ) );
}
/**
* Returns calendar period start timestamp based on current timestamp ($base_time) and $period_length
*
* @param int $base_time
* @param int $period_length
* @return int
* @author Alex
*/
function getPeriodStartTS($base_time, $period_length)
{
switch($period_length)
{
case 86400: // day
$start_timestamp = adodb_mktime(0,0,0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time) );
break;
case 604800: // week
$day_seconds = 86400;
$first_week_day = $this->Application->ConfigValue('FirstDayOfWeek');
$morning = adodb_mktime(0,0,0, adodb_date('m', $base_time), adodb_date('d', $base_time), adodb_date('Y', $base_time) );
$week_day = adodb_date('w', $morning);
if($week_day == $first_week_day)
{
// if it is already first week day, then don't search for previous week day
$day_diff = 0;
}
else
{
// this way, because sunday is 0, but not 7 as it should be
$day_diff = $week_day != 0 ? $week_day - $first_week_day: 7 - $first_week_day;
}
$start_timestamp = $morning - $day_diff * $day_seconds;
break;
case 2628000: // month
$start_timestamp = adodb_mktime(0,0,0, adodb_date('m', $base_time), 1, adodb_date('Y', $base_time) );
break;
case 7884000: // quartal
$first_quartal_month = (ceil( adodb_date('m', $base_time) / 3)-1)*3 + 1;
$start_timestamp = adodb_mktime(0,0,0, $first_quartal_month, 1, adodb_date('Y', $base_time) );
break;
case 31536000:
$start_timestamp = adodb_mktime(0,0,0,1,1, adodb_date('Y', $base_time) );
break;
}
return $start_timestamp;
}
function iterateItems(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$ids = $this->StoreSelectedIDs($event);
if($ids)
{
$status_field = array_shift( $this->Application->getUnitOption($event->Prefix,'StatusField') );
foreach($ids as $id)
{
$object->Load($id);
switch ($event->Name)
{
case 'OnMassApprove':
$object->SetDBField($status_field, 1);
break;
case 'OnMassDecline':
$object->SetDBField($status_field, 0);
break;
case 'OnMassMoveUp':
$object->SetDBField('Priority', $object->GetDBField('Priority') + 1);
break;
case 'OnMassMoveDown':
$object->SetDBField('Priority', $object->GetDBField('Priority') - 1);
break;
}
if( $object->Update() )
{
switch ($event->Name)
{
case 'OnMassApprove':
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED');
break;
case 'OnMassDecline':
$email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.DENIED', $object->GetDBField('PortalUserId'));
$email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.DENIED');
break;
}
$event->status=erSUCCESS;
$event->redirect_params = Array('opener' => 's'); //stay!
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
}
}
}
/**
* Checks that user in affiliate record matches current user
* (non permission-based)
*
* @param kEvent $event
* @return bool
*/
function checkItemStatus(&$event)
{
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return true;
}
$object =& $event->getObject();
if (!$object->isLoaded()) {
return true;
}
return $object->GetDBField('PortalUserId') == $this->Application->RecallVar('user_id');
}
}
\ No newline at end of file
Index: branches/5.0.x/in-commerce/units/addresses/addresses_event_handler.php
===================================================================
--- branches/5.0.x/in-commerce/units/addresses/addresses_event_handler.php (revision 12721)
+++ branches/5.0.x/in-commerce/units/addresses/addresses_event_handler.php (revision 12722)
@@ -1,321 +1,321 @@
<?php
/**
* @version $Id$
* @package In-Commerce
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license Commercial License
* This software is protected by copyright law and international treaties.
* Unauthorized reproduction or unlicensed usage of the code of this program,
* or any portion of it may result in severe civil and criminal penalties,
* and will be prosecuted to the maximum extent possible under the law
* See http://www.in-portal.net/license/commercial/ for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class AddressesEventHandler extends kDBEventHandler {
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
// user can view any form on front-end
'OnItemBuild' => Array('subitem' => true),
'OnUpdate' => Array('subitem' => true),
'OnCreate' => Array('subitem' => true),
'OnDelete' => Array('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
return '';
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function SetCustomQuery(&$event)
{
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return ;
}
$object =& $event->getObject();
$user_id = $this->Application->RecallVar('user_id');
$object->addFilter('myitems_user','%1$s.PortalUserId = '.$user_id);
}
/**
* Makes "use as $type" mark unique among user addresses
*
* @param kDBItem $object
* @param string $type
*/
function processLastUsed(&$object, $type)
{
$is_last = $object->GetDBField('LastUsedAs'.$type);
if ($is_last) {
$fields_hash = Array (
'LastUsedAs'.$type => 0,
);
$this->Conn->doUpdate($fields_hash, $object->TableName, 'PortalUserId = '.$object->GetDBField('PortalUserId'));
}
}
/**
* Ensures, that user have only one "use as billing" / "use as shipping" address
*
* @param kEvent $event
*/
function OnBeforeItemUpdate(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
if (!$object->isLoaded() || !$this->checkItemStatus($event)) {
// not trivially loaded object OR not current user address
$event->status = erPERM_FAIL;
return ;
}
$this->processLastUsed($object, 'Shipping');
$this->processLastUsed($object, 'Billing');
}
function OnUpdate(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
$cs_helper->CheckStateField($event, 'State', 'Country');
parent::OnUpdate($event);
$this->setNextTemplate($event);
}
/**
* Creates new user
*
* @param kEvent $event
*/
function OnCreate(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
$cs_helper->CheckStateField($event, 'State', 'Country');
parent::OnCreate($event);
$this->setNextTemplate($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function setNextTemplate(&$event)
{
- if($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return ;
}
$event->SetRedirectParam('opener', 's');
$next_template = $this->Application->GetVar('next_template');
if ($next_template) {
$event->redirect = $next_template;
}
}
/**
* [HOOK] Prefill states dropdown with correct values
*
* @param kEvent $event
* @access public
*/
function OnPrepareStates(&$event)
{
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
$cs_helper->PopulateStates($event, 'State', 'Country');
$object =& $event->MasterEvent->getObject();
if( $object->isRequired('Country') && $cs_helper->CountryHasStates( $object->GetDBField('Country') ) ) $object->setRequired('State', true);
}
/**
* [HOOK] Update PortalUser table when address marked as ProfileAddress is changed via addr prefix object
*
* @param kEvent $event
*/
function OnUpdateProfileAddress(&$event)
{
$user =& $this->Application->recallObject('u.current');
if ($this->Application->GetVar('billing_address_id') > 0) {
$address_id = $this->Application->GetVar('billing_address_id');
}
elseif ($this->Application->GetVar('shipping_address_id') > 0) {
$address_id = $this->Application->GetVar('shipping_address_id');
}
else {
$address_id = false;
}
if (!$address_id) {
return true;
}
$address =& $event->getObject(Array('skip_autoload' => true));
$address->Load($address_id);
if (!$address->GetDBField('IsProfileAddress')) {
return true;
}
$field_map = Array( 'Company' => 1,
'Phone' => 1,
'Fax' => 1,
'Email' => 1,
'Address1' => 'Street',
'Address2' => 'Street2',
'City' => 1,
'State' => 1,
'Zip' => 1,
'Country' => 1,
);
$user->setName( $address->GetDBField('To') );
foreach ($field_map as $src_field => $dst_field) {
if ($dst_field == 1) $dst_field = $src_field;
$user->SetDBField($dst_field, $address->GetDBField($src_field));
}
return $user->Update();
}
/**
* [HOOK] Create user profile address based on PortalUser table data
*
* @param kEvent $event
*/
function OnUpdateUserProfile(&$event)
{
$user =& $event->MasterEvent->getObject();
$load_keys = Array('PortalUserId' => $user->GetID(), 'IsProfileAddress' => 1);
$address =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true));
$address->Load($load_keys);
$field_map = Array( 'PortalUserId' => 1,
'Company' => 1,
'Phone' => 1,
'Fax' => 1,
'Email' => 1,
'Address1' => 'Street',
'Address2' => 'Street2',
'City' => 1,
'State' => 1,
'Zip' => 1,
'Country' => 1,
);
$full_name = trim($user->GetDBField('FirstName').' '.$user->GetDBField('LastName'));
$address->SetDBField('To', $full_name);
$address->SetDBField('IsProfileAddress', 1);
foreach ($field_map as $dst_field => $src_field) {
if ($src_field == 1) $src_field = $dst_field;
$address->SetDBField($dst_field, $user->GetDBField($src_field));
}
$sql = 'SELECT SUM(IF(LastUsedAsBilling = 1, 1, 0 )) AS HasBilling, SUM(IF(LastUsedAsShipping = 1, 1, 0)) AS HasShipping
FROM '.$address->TableName.'
WHERE PortalUserId = '.$user->GetID();
$address_status = $this->Conn->GetRow($sql);
if (!$address_status['HasBilling']) {
$address->SetDBField('LastUsedAsBilling', 1);
}
if (!$address_status['HasShipping']) {
$address->SetDBField('LastUsedAsShipping', 1);
}
return $address->isLoaded() ? $address->Update() : $address->Create();
}
/**
* Checks if user trying to manipulate address that he Owns (exception for Admins)
* (non permission-based)
*
* @param kEvent $event
* @return bool
*/
function checkItemStatus(&$event)
{
- if ($this->Application->IsAdmin()) {
+ if ($this->Application->isAdminUser) {
return true;
}
if (!$this->Application->LoggedIn()) {
return false;
}
$object =& $event->getObject();
if (!$object->isLoaded()) {
return true;
}
return $object->GetDBField('PortalUserId') == $this->Application->RecallVar('user_id');
}
/**
* Ensures, that user have only one "use as billing" / "use as shipping" address
* Disables Guest ability to create addresses
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
if (!$this->Application->LoggedIn()) {
$event->status = erPERM_FAIL;
return ;
}
$object =& $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id'));
$this->processLastUsed($object, 'Shipping');
$this->processLastUsed($object, 'Billing');
}
function OnBeforeItemDelete(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
if (!$object->isLoaded() || !$this->checkItemStatus($event)) {
// not trivially loaded object OR not current user address
$event->status = erPERM_FAIL;
return ;
}
}
}
\ No newline at end of file

Event Timeline