1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +01:00

Track total funding amount on Fund initiatives

Summary: Ref T5835. Show total funding amount and payable merchant on initiatives.

Test Plan: {F214936}

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T5835

Differential Revision: https://secure.phabricator.com/D10657
This commit is contained in:
epriestley 2014-10-08 05:31:48 -07:00
parent 91549bb81d
commit 608465da1e
6 changed files with 81 additions and 22 deletions

View file

@ -0,0 +1,4 @@
ALTER TABLE {$NAMESPACE}_fund.fund_initiative
ADD totalAsCurrency VARCHAR(64) NOT NULL COLLATE utf8_bin;
UPDATE {$NAMESPACE}_fund.fund_initiative SET totalAsCurrency = '0.00 USD';

View file

@ -84,12 +84,25 @@ final class FundInitiativeViewController
->setObject($initiative);
$owner_phid = $initiative->getOwnerPHID();
$this->loadHandles(array($owner_phid));
$merchant_phid = $initiative->getMerchantPHID();
$this->loadHandles(
array(
$owner_phid,
$merchant_phid,
));
$view->addProperty(
pht('Owner'),
$this->getHandle($owner_phid)->renderLink());
$view->addProperty(
pht('Payable To Merchant'),
$this->getHandle($merchant_phid)->renderLink());
$view->addProperty(
pht('Total Funding'),
$initiative->getTotalAsCurrency()->formatForDisplay());
$view->invokeWillRenderEvent();
$description = $initiative->getDescription();

View file

@ -78,7 +78,18 @@ final class FundInitiativeEditor
$object->setStatus($xaction->getNewValue());
return;
case FundInitiativeTransaction::TYPE_BACKER:
// TODO: Calculate total funding / backers / etc.
$backer = id(new FundBackerQuery())
->setViewer($this->requireActor())
->withPHIDs(array($xaction->getNewValue()))
->executeOne();
if (!$backer) {
throw new Exception(pht('No such backer!'));
}
$backer_amount = $backer->getAmountAsCurrency();
$total = $object->getTotalAsCurrency()->add($backer_amount);
$object->setTotalAsCurrency($total);
return;
case PhabricatorTransactions::TYPE_SUBSCRIBERS:
case PhabricatorTransactions::TYPE_EDGE:

View file

@ -18,6 +18,7 @@ final class FundInitiative extends FundDAO
protected $viewPolicy;
protected $editPolicy;
protected $status;
protected $totalAsCurrency;
private $projectPHIDs = self::ATTACHABLE;
@ -43,7 +44,8 @@ final class FundInitiative extends FundDAO
->setOwnerPHID($actor->getPHID())
->setViewPolicy($view_policy)
->setEditPolicy($actor->getPHID())
->setStatus(self::STATUS_OPEN);
->setStatus(self::STATUS_OPEN)
->setTotalAsCurrency(PhortuneCurrency::newEmptyCurrency());
}
public function getConfiguration() {
@ -54,6 +56,10 @@ final class FundInitiative extends FundDAO
'description' => 'text',
'status' => 'text32',
'merchantPHID' => 'phid?',
'totalAsCurrency' => 'text64',
),
self::CONFIG_APPLICATION_SERIALIZERS => array(
'totalAsCurrency' => new PhortuneCurrencySerializer(),
),
self::CONFIG_KEY_SCHEMA => array(
'key_status' => array(

View file

@ -72,26 +72,20 @@ final class PhortuneCurrency extends Phobject {
public static function newFromList(array $list) {
assert_instances_of($list, 'PhortuneCurrency');
$total = 0;
$currency = null;
foreach ($list as $item) {
if ($currency === null) {
$currency = $item->getCurrency();
} else if ($currency === $item->getCurrency()) {
// Adding a value denominated in the same currency, which is
// fine.
} else {
throw new Exception(
pht('Trying to sum a list of unlike currencies.'));
}
// TODO: This should check for integer overflows, etc.
$total += $item->getValue();
if (!$list) {
return PhortuneCurrency::newEmptyCurrency();
}
return PhortuneCurrency::newFromValueAndCurrency(
$total,
self::getDefaultCurrency());
$total = null;
foreach ($list as $item) {
if ($total === null) {
$total = $item;
} else {
$total = $total->add($item);
}
}
return $total;
}
public function formatForDisplay() {
@ -132,6 +126,20 @@ final class PhortuneCurrency extends Phobject {
throw new Exception("Invalid currency format ('{$string}').");
}
public function add(PhortuneCurrency $other) {
if ($this->currency !== $other->currency) {
throw new Exception(pht('Trying to add unlike currencies!'));
}
$currency = new PhortuneCurrency();
// TODO: This should check for integer overflows, etc.
$currency->value = $this->value + $other->value;
$currency->currency = $this->currency;
return $currency;
}
/**
* Assert that a currency value lies within a range.
*

View file

@ -19,7 +19,6 @@ final class PhortuneCurrencyTestCase extends PhabricatorTestCase {
}
}
public function testCurrencyFormatBareValue() {
// NOTE: The PayPal API depends on the behavior of the bare value format!
@ -142,4 +141,22 @@ final class PhortuneCurrencyTestCase extends PhabricatorTestCase {
$this->assertTrue($caught instanceof Exception);
}
public function testAddCurrency() {
$cases = array(
array('0.00 USD', '0.00 USD', '$0.00 USD'),
array('1.00 USD', '1.00 USD', '$2.00 USD'),
array('1.23 USD', '9.77 USD', '$11.00 USD'),
);
foreach ($cases as $case) {
list($l, $r, $expect) = $case;
$l = PhortuneCurrency::newFromString($l);
$r = PhortuneCurrency::newFromString($r);
$sum = $l->add($r);
$this->assertEqual($expect, $sum->formatForDisplay());
}
}
}