Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
jedateach committed Jun 28, 2010
1 parent 85042a4 commit 68aa9dc
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
/**
* This module allows you to choose a deposit ammount for an event payment.
*/

Object::add_extension('Event','PayDepositPaymentFormDecorator');

?>
58 changes: 58 additions & 0 deletions code/PayDepositPaymentFormDecorator.php
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();
}
}

}

?>
55 changes: 55 additions & 0 deletions code/fields/LimitedCurrencyField.php
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;
}
}

?>

0 comments on commit 68aa9dc

Please sign in to comment.