public function processGatewayRoutingByOrder($order) { if (!$order) { return false; } // action flags $switchedAlready = false; $nextGateway = false; $turnOffGateway = false; $hasLimit = true; // have to load the AFActiveDataProvider->models result into a public variable $allGateways = $this->getProfileGatewayByIds($order->campaign->profile_id, $order->payment->method_id); foreach ($allGateways as $g) { // build private gateways array with pk as array key $this->gateways[$g->profiles_gateways_id] = $g; // order gateway. set pk and build limit object if ($g->gateway_id == $order->gateway_id) { if (isset($g->limit_id) && $g->limit_id) { $this->limit = GatewayLimit::model()->fillFromDbPk($g->limit_id); } $this->orderGateway = $g->profiles_gateways_id; } // set realtime current gateway if ($g->flags == 'cur') { $this->currentGateway = $g->profiles_gateways_id; } } unset($allGateways); /*fb($this->gateways); fb($this->currentGateway); fb($this->orderGateway . ' = ' . $order->gateway_id);*/ // make sure the 'cur' gateway is the same as what was used for the order. // if it was switched already for some reason, then we'll only update the limit record if ($this->currentGateway != $this->orderGateway) { $switchedAlready = true; } //fb($switchedAlready); // check limits. if no limits present then set the flag if (!$this->limit) { $hasLimit = false; } else { // update limit count/amount // not using $order->payment_total since it is only set when payment has actually gone through. $this->limit->orders_count++; $this->limit->amount_count += $order->amount_product + $order->amount_shipping; if (!$this->limit->save()) { // this should never happen //fb($this->limit->getErrors()); } } // gateway already switched? return out of function if ($switchedAlready) { //fb('gateway already switched'); return; } else { if ($hasLimit) { // check limits. what limit type? if limit is reached then the next gateway must be selected if ($this->limit->orders_max > 0 && $this->limit->amount_max > 0) { // both limits apply if ($this->limit->orders_count >= $this->limit->orders_max || $this->limit->amount_count >= $this->limit->amount_max) { $nextGateway = true; $turnOffGateway = true; //fb('one'); //fb($this->limit->orders_count . ' >= ' . $this->limit->orders_max. ' || ' . $this->limit->amount_count . ' >= ' . $this->limit->amount_max); } } else { if ($this->limit->orders_max > 0) { // order number limit if ($this->limit->orders_count >= $this->limit->orders_max) { $nextGateway = true; $turnOffGateway = true; //fb('two'); } } else { if ($this->limit->amount_max > 0) { // order amount limit if ($this->limit->amount_count >= $this->limit->amount_max) { $nextGateway = true; $turnOffGateway = true; //fb('three'); } } else { // no limits set in limit record $hasLimit = false; } } } } } // grab load balance type from profiles_methods by profile_id and method_id $pm = ProfileMethod::model()->findByPk(array('profile_id' => $order->campaign->profile_id, 'method_id' => $order->payment->method_id)); // what type is it? rotation. grab the next gateway according to rank no matter what the limits are if ($pm->load_balance == 'rot') { $nextGateway = true; } unset($pm); // enable next gateway? if ($nextGateway) { // if we don't need to turn off the gateway then this is a rotating profile. we need to set the flag to blank if (!$turnOffGateway) { $this->gateways[$this->orderGateway]->flags = ''; if (!$this->gateways[$this->orderGateway]->save()) { fb($this->gateways[$this->orderGateway]->getErrors()); } } // what's the next gateway $pgNext = $this->nextActiveGateway(); // is this equal to our current gateway? if so, something's wrong. disable $turnOffGateway if (!$pgNext || $pgNext == $this->orderGateway) { $turnOffGateway = false; } else { $this->gateways[$pgNext]->flags = 'cur'; if (!$this->gateways[$pgNext]->save()) { fb($this->gateways[$pgNext]->getErrors()); } } } // disable the gateway? if ($turnOffGateway) { $this->gateways[$this->orderGateway]->flags = 'off'; if (!$this->gateways[$this->orderGateway]->save()) { fb($this->gateways[$this->orderGateway]->getErrors()); } } return true; }