'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)); //uasort($temp, 'pr_bracket_comp'); $bracket =& $this->Application->recallObject($event->getPrefixSpecial()); foreach($temp as $id => $record) { if( $record['MaxQty'] == '∞' || $record['MaxQty'] == '∞') { $temp[$id]['MaxQty'] = -1; } } 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); $min_qty = $this->Conn->GetOne('SELECT MIN(MaxQty) FROM '.$bracket->TableName); 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'; } $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'); 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); 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'; $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(); $stored_ids = $this->Conn->GetCol("SELECT PriceId FROM ".$object->TableName." WHERE ProductId=".$this->Application->GetVar("p_id")); $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') == '∞' || $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']){ $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) pr_bracket_comp(); uasort($temp, 'pr_bracket_comp'); reset($temp); $first_item=each($temp); $first_item_key=$first_item['key']; $ids = $this->Conn->GetCol('SELECT PriceId FROM '.$bracket->TableName.' WHERE ProductId='.$this->Application->GetVar('p_id')); 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; $start=$temp[$key]['MaxQty']; } $temp[$first_item_key]['IsPrimary']=1; } return $temp; } /** * Set's price as primary for product * * @param kEvent $event */ function OnSetPrimary(&$event) { $this->Application->setUnitOption($event->Prefix,'AutoLoad',false); $object =& $event->getObject(); $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', 'pass_events' => true); //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); } } } } ?>