-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
class PayDepositPaymentFormDecorator extends DataObjectDecorator{ | ||
|
||
function extraStatics(){ | ||
return array( | ||
'db' => array( | ||
'AllowPaymentModification' => 'Boolean', | ||
'LowerLimitValue' => 'Currency', | ||
'LowerLimitPercent' => 'Percentage' | ||
) | ||
); | ||
} | ||
|
||
function updateCMSFields(&$fields){ | ||
|
||
$fields->addFieldToTab('Root.Content.BookingOptions',new CheckboxField('AllowPaymentModification','Allow payments to be modified')); | ||
|
||
if($this->owner->AllowPaymentModification){ | ||
$fields->addFieldsToTab('Root.Content.BookingOptions', | ||
array( | ||
new NumericField('LowerLimitPercent','Minimimum percentage of total that can be paid') | ||
//new CurrencyField('LowerLimitValue','Lowest amount that can be paid (will override percent, if present)') | ||
) | ||
); | ||
} | ||
|
||
} | ||
|
||
function updatePaymentFields(&$fields){ | ||
|
||
if($this->owner->AllowPaymentModification && $afield = $fields->fieldByName('Amount')){ | ||
$value = $afield->Value(); | ||
$fields->replaceField('Amount',$lcf = new LimitedCurrencyField('Amount','Amount',$value)); | ||
|
||
$datavalue = $lcf->dataValue(); | ||
$lcf->setUpperLimit($datavalue); //can't pay more | ||
|
||
if($this->owner->LowerLimitPercent > 0) $lcf->setLowerLimit(ceil((double)$datavalue * (double)$this->owner->LowerLimitPercent)); | ||
|
||
//if($this->owner->LowerLimitValue) $lcf->setLowerLimit($this->owner->LowerLimitValue); | ||
|
||
$lcf->setTitle($lcf->Title()." (deposit can be any amount ".$lcf->LabelExtra().")"); | ||
} | ||
} | ||
|
||
function onBeforePayment(&$registration,&$data,&$form){ | ||
|
||
$payment = $registration->Payment(); | ||
if($afield = $form->Fields()->fieldByName('Amount')){ | ||
$payment->Amount = $afield->Value(); | ||
$payment->write(); | ||
} | ||
} | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
class LimitedCurrencyField extends CurrencyField{ | ||
|
||
private $upperlimit = null; | ||
private $lowerlimit = null; | ||
|
||
function setUpperLimit($limit){ | ||
$this->upperlimit = $this->convertTo($limit); | ||
} | ||
|
||
function setLowerLimit($limit = 0){ | ||
$this->lowerlimit = $this->convertTo($limit); | ||
} | ||
|
||
function validate($validator) { | ||
if(!parent::validate($validator)) return false; | ||
|
||
if($this->dataValue() > $this->convertFrom($this->upperlimit)) { | ||
$validator->validationError($this->name, "Value must be below $this->upperlimit", "validation", false); | ||
return false; | ||
} | ||
|
||
if($this->dataValue() < $this->convertFrom($this->lowerlimit)) { | ||
$validator->validationError($this->name, "Value must be above $this->lowerlimit", "validation", false); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function convertFrom($value){ | ||
if($value){ | ||
return preg_replace('/[^0-9.]/',"", $value); | ||
}else{ | ||
return 0.00; | ||
} | ||
} | ||
|
||
private function convertTo($value){ | ||
return '$' . number_format(ereg_replace('[^0-9.]',"",$value), 2); | ||
} | ||
|
||
public function LabelExtra(){ | ||
if($this->upperlimit && $this->lowerlimit) | ||
return "between $this->lowerlimit and $this->upperlimit"; | ||
if($this->lowerlimit) | ||
return "above $this->lowerlimit"; | ||
if($this->upperlimit) | ||
return "below $this->upperlimit"; | ||
return null; | ||
} | ||
} | ||
|
||
?> |