TTi18n::gettext('ISO Code'), '-1020-date_stamp' => TTi18n::gettext( 'Date' ), '-1030-conversion_rate' => TTi18n::gettext( 'Conversion Rate' ), '-2000-created_by' => TTi18n::gettext( 'Created By' ), '-2010-created_date' => TTi18n::gettext( 'Created Date' ), '-2020-updated_by' => TTi18n::gettext( 'Updated By' ), '-2030-updated_date' => TTi18n::gettext( 'Updated Date' ), ]; break; case 'list_columns': $retval = Misc::arrayIntersectByKey( [ 'date_stamp', 'conversion_rate' ], Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) ); break; case 'default_display_columns': //Columns that are displayed by default. $retval = [ 'date_stamp', 'conversion_rate', ]; break; case 'unique_columns': //Columns that are unique, and disabled for mass editing. $retval = [ 'date_stamp', ]; break; } return $retval; } /** * @param $data * @return array */ function _getVariableToFunctionMap( $data ) { $variable_function_map = [ 'id' => 'ID', 'currency_id' => 'Currency', //'status_id' => FALSE, //'status' => FALSE, //'name' => FALSE, //'symbol' => FALSE, //'iso_code' => FALSE, 'date_stamp' => 'DateStamp', 'conversion_rate' => 'ConversionRate', 'deleted' => 'Deleted', ]; return $variable_function_map; } /** * @return bool */ function getCurrencyObject() { return $this->getGenericObject( 'CurrencyListFactory', $this->getCurrency(), 'currency_obj' ); } /** * @return bool|mixed */ function getCurrency() { return $this->getGenericDataValue( 'currency_id' ); } /** * @param string $value UUID * @return bool */ function setCurrency( $value ) { $value = TTUUID::castUUID( $value ); return $this->setGenericDataValue( 'currency_id', $value ); } /** * @param bool $raw * @return bool|int */ function getDateStamp( $raw = false ) { $value = $this->getGenericDataValue( 'date_stamp' ); if ( $value !== false ) { if ( $raw === true ) { return $value; } else { if ( !is_numeric( $value ) ) { //Optimization to avoid converting it when run in CalculatePolicy's loops $value = TTDate::getMiddleDayEpoch( TTDate::strtotime( $value ) ); //Make sure we use middle day epoch when pulling the value from the DB the first time, to match setDateStamp() below. Otherwise setting the datestamp then getting it again before save won't match the same value after its saved to the DB. $this->setGenericDataValue( 'date_stamp', $value ); } return $value; } } return false; } /** * @param int $value EPOCH * @return bool */ function setDateStamp( $value ) { $value = (int)$value; if ( $value > 0 ) { //Use middle day epoch to help avoid confusion with different timezones/DST. -- getDateStamp() needs to use middle day epoch too then. //See comments about timezones in CalculatePolicy->_calculate(). $retval = $this->setGenericDataValue( 'date_stamp', TTDate::getMiddleDayEpoch( $value ) ); return $retval; } return false; } /** * @return bool */ function isUnique() { $ph = [ 'currency_id' => TTUUID::castUUID( $this->getCurrency() ), 'date_stamp' => $this->db->BindDate( $this->getDateStamp() ), ]; $query = 'select id from ' . $this->getTable() . ' where currency_id = ? AND date_stamp = ?'; $id = $this->db->GetOne( $query, $ph ); Debug::Arr( $id, 'Unique Currency Rate: ' . $id, __FILE__, __LINE__, __METHOD__, 10 ); if ( $id === false ) { return true; } else { if ( $id == $this->getId() ) { return true; } } return false; } /** * @return bool|string */ function getReverseConversionRate() { $rate = $this->getConversionRate(); if ( $rate != 0 ) { //Prevent division by 0. return bcdiv( 1, $rate ); } return false; } /** * @return bool */ function getConversionRate() { return $this->getGenericDataValue( 'conversion_rate' );//Don't cast to (float) as it may strip some precision. } /** * @param $value * @return bool */ function setConversionRate( $value ) { //Pull out only digits and periods. $value = $this->Validator->stripNonFloat( $value ); return $this->setGenericDataValue( 'conversion_rate', $value ); } /** * @param bool $ignore_warning * @return bool */ function Validate( $ignore_warning = true ) { // // BELOW: Validation code moved from set*() functions. // // Currency if ( $this->Validator->getValidateOnly() == false ) { //Don't do the follow validation checks during Mass Edit. $culf = TTnew( 'CurrencyListFactory' ); /** @var CurrencyListFactory $culf */ $this->Validator->isResultSetWithRows( 'currency_id', $culf->getByID( $this->getCurrency() ), TTi18n::gettext( 'Invalid Currency' ) ); } // Date if ( $this->Validator->getValidateOnly() == false && $this->getDateStamp() != false ) { $this->Validator->isDate( 'date_stamp', $this->getDateStamp(), TTi18n::gettext( 'Incorrect date' ) ); } // Conversion rate if ( $this->Validator->getValidateOnly() == false ) { //Don't do the follow validation checks during Mass Edit. $this->Validator->isTrue( 'conversion_rate', $this->getConversionRate(), TTi18n::gettext( 'Conversion rate not specified' ) ); } if ( $this->getConversionRate() !== false ) { if ( $this->Validator->isError( 'conversion_rate' ) == false ) { $this->Validator->isFloat( 'conversion_rate', $this->getConversionRate(), TTi18n::gettext( 'Incorrect Conversion Rate' ) ); } if ( $this->Validator->isError( 'conversion_rate' ) == false ) { $this->Validator->isLessThan( 'conversion_rate', $this->getConversionRate(), TTi18n::gettext( 'Conversion Rate is too high' ), 99999999 ); } if ( $this->Validator->isError( 'conversion_rate' ) == false ) { $this->Validator->isGreaterThan( 'conversion_rate', $this->getConversionRate(), TTi18n::gettext( 'Conversion Rate is too low' ), -99999999 ); } } // // ABOVE: Validation code moved from set*() functions. // if ( $this->getDeleted() == false ) { if ( $this->Validator->isError( 'date_stamp' ) == false ) { if ( $this->Validator->getValidateOnly() == false && $this->getDateStamp() == false ) { $this->Validator->isTrue( 'date_stamp', false, TTi18n::gettext( 'Date not specified' ) ); } else { if ( $this->Validator->getValidateOnly() == false && $this->isUnique() == false ) { $this->Validator->isTrue( 'date_stamp', false, TTi18n::gettext( 'Currency rate already exists for this date' ) ); } } } } return true; } /** * @return bool */ function postSave() { $this->removeCache( $this->getId() ); return true; } /** * Support setting created_by, updated_by especially for importing data. * Make sure data is set based on the getVariableToFunctionMap order. * @param $data * @return bool */ function setObjectFromArray( $data ) { if ( is_array( $data ) ) { $variable_function_map = $this->getVariableToFunctionMap(); foreach ( $variable_function_map as $key => $function ) { if ( isset( $data[$key] ) ) { $function = 'set' . $function; switch ( $key ) { case 'date_stamp': $this->$function( TTDate::parseDateTime( $data[$key] ) ); break; case 'conversion_rate': $this->$function( TTi18n::parseFloat( $data[$key], 10 ) ); break; default: if ( method_exists( $this, $function ) ) { $this->$function( $data[$key] ); } break; } } } $this->setCreatedAndUpdatedColumns( $data ); return true; } return false; } /** * @param null $include_columns * @return array */ function getObjectAsArray( $include_columns = null ) { /* $include_columns = array( 'id' => TRUE, 'company_id' => TRUE, ... ) */ $data = []; $variable_function_map = $this->getVariableToFunctionMap(); if ( is_array( $variable_function_map ) ) { foreach ( $variable_function_map as $variable => $function_stub ) { if ( $include_columns == null || ( isset( $include_columns[$variable] ) && $include_columns[$variable] == true ) ) { $function = 'get' . $function_stub; switch ( $variable ) { case 'date_stamp': $data[$variable] = $this->$function( true ); break; // case 'conversion_rate': // $data[$variable] = TTi18n::formatNumber( $this->$function(), TRUE, 10, 10 ); //Don't format numbers here, as it could break scripts using the API. // break; default: if ( method_exists( $this, $function ) ) { $data[$variable] = $this->$function(); } break; } } } $this->getCreatedAndUpdatedColumns( $data, $include_columns ); } return $data; } /** * @param $log_action * @return bool */ function addLog( $log_action ) { return TTLog::addEntry( $this->getId(), $log_action, TTi18n::getText( 'Currency Rate' ) . ': ' . $this->getCurrencyObject()->getISOCode() . ' ' . TTi18n::getText( 'Rate' ) . ': ' . $this->getConversionRate(), null, $this->getTable(), $this ); } } ?>