Index: branches/5.2.x/units/orders/orders_event_handler.php =================================================================== diff -u -N -r14569 -r14582 --- branches/5.2.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 14569) +++ branches/5.2.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 14582) @@ -1,6 +1,6 @@ Application->getUnitOption('pr', 'TableName'); $pricing_idfield = $this->Application->getUnitOption('pr', 'IDField'); - + /* TODO check on implementation $sql = 'SELECT AccessDuration, AccessUnit, DurationType, AccessExpiration FROM %s WHERE %s = %s'; */ @@ -936,7 +936,7 @@ /* TODO check on implementation (code from customization healtheconomics.org) $item_data['DurationType'] = $pricing_info['DurationType']; - $item_data['AccessExpiration'] = $pricing_info['AccessExpiration']; + $item_data['AccessExpiration'] = $pricing_info['AccessExpiration']; */ $item_data['Duration'] = $pricing_info['AccessDuration'] * $unit_secs[ $pricing_info['AccessUnit'] ]; @@ -1112,8 +1112,7 @@ $object =& $event->getObject( Array('skip_autoload' => true) ); /* @var $object kDBItem */ - $object->SetDBField('Number', $this->getNextOrderNumber($event)); - $object->SetDBField('SubNumber', 0); + $this->setNextOrderNumber($event); $object->SetDBField('Status', ORDER_STATUS_INCOMPLETE); $object->SetDBField('VisitId', $this->Application->RecallVar('visit_id') ); @@ -1289,9 +1288,9 @@ parent::OnPreCreate($event); $object =& $event->getObject(); - $new_number = $this->getNextOrderNumber($event); - $object->SetDBField('Number', $new_number); - $object->SetDBField('SubNumber', 0); + + $this->setNextOrderNumber($event); + $object->SetDBField('OrderIP', $_SERVER['REMOTE_ADDR']); $order_type = $this->getTypeBySpecial( $this->Application->GetVar('order_type') ); $object->SetDBField('Status', $order_type); @@ -1311,9 +1310,7 @@ $object->SetDBField('OriginalAmount', 0); // needed in this case ? } else { - $new_number = $this->getNextOrderNumber($event); - $object->SetDBField('Number', $new_number); - $object->SetDBField('SubNumber', 0); + $this->setNextOrderNumber($event); $object->SetDBField('OriginalAmount', 0); } $object->SetDBField('OrderDate', adodb_mktime()); @@ -1813,12 +1810,12 @@ /* @var $quote_engine_collector ShippingQuoteCollector */ $shipping_info = unserialize($shipping_info); - $shipping_quote_engine = $quote_engine_collector->GetClassByType($shipping_info, 1); + $sqe_class_name = $quote_engine_collector->GetClassByType($shipping_info, 1); } // try to create usps order - if (($object->GetDBField('ShippingType') == 0) && ($shipping_quote_engine !== false)) { - $shipping_quote_engine =& $this->Application->recallObject($shipping_quote_engine); + if (($object->GetDBField('ShippingType') == 0) && ($sqe_class_name !== false)) { + $shipping_quote_engine =& $this->Application->recallObject($sqe_class_name); /* @var $shipping_quote_engine ShippingQuoteEngine */ $ret = $shipping_quote_engine->MakeOrder($object); @@ -1883,16 +1880,31 @@ } /** - * Get next free order number + * Set next available order number * * @param kEvent $event */ - function getNextOrderNumber(&$event) + function setNextOrderNumber(&$event) { $object =& $event->getObject(); + /* @var $object OrdersItem */ + $sql = 'SELECT MAX(Number) FROM ' . $this->Application->GetLiveName($object->TableName); - return max($this->Conn->GetOne($sql) + 1, $this->Application->ConfigValue('Comm_Next_Order_Number')); + $next_order_number = $this->Conn->GetOne($sql) + 1; + + $next_order_number = max($next_order_number, $this->Application->ConfigValue('Comm_Next_Order_Number')); + $this->Application->SetConfigValue('Comm_Next_Order_Number', $next_order_number + 1); + + $object->SetDBField('Number', $next_order_number); + $object->SetDBField('SubNumber', 0); + + // set virtual field too + $number_format = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_P'); + $sub_number_format = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_S'); + $order_number = sprintf('%0' . $number_format . 'd', $next_order_number) . '-' . str_repeat('0', $sub_number_format); + + $object->SetDBField('OrderNumber', $order_number); } /** @@ -2837,14 +2849,14 @@ foreach ($shipping_ids as $package => $id) { // try to validate $shipping_types[$package] = $last_shippings[$package][$id]; - $shipping_quote_engine = $quote_engine_collector->GetClassByType($shipping_types, $package); + $sqe_class_name = $quote_engine_collector->GetClassByType($shipping_types, $package); - if (($object->GetDBField('ShippingType') == 0) && ($shipping_quote_engine !== false) && in_array($template, $shipping_templates)) { - $shipping_quote_engine =& $this->Application->recallObject($shipping_quote_engine); + if (($object->GetDBField('ShippingType') == 0) && ($sqe_class_name !== false) && in_array($template, $shipping_templates)) { + $shipping_quote_engine =& $this->Application->recallObject($sqe_class_name); /* @var $shipping_quote_engine ShippingQuoteEngine */ // USPS related part - // TODO: remove USPS condition from here + // TODO: remove USPS condition from here // set first of found shippings just to check if any errors are returned $current_usps_shipping_types = unserialize($this->Application->RecallVar('current_usps_shipping_types')); $object->SetDBField('ShippingInfo', serialize( Array($package => $current_usps_shipping_types[$id])) );