diff --git a/src/applications/phortune/cart/PhortuneSubscriptionCart.php b/src/applications/phortune/cart/PhortuneSubscriptionCart.php index f913974a6c..6c17e00331 100644 --- a/src/applications/phortune/cart/PhortuneSubscriptionCart.php +++ b/src/applications/phortune/cart/PhortuneSubscriptionCart.php @@ -25,7 +25,7 @@ final class PhortuneSubscriptionCart } public function getName(PhortuneCart $cart) { - return pht('Subscription'); + return $this->getSubscription()->getCartName($cart); } public function willCreateCart( diff --git a/src/applications/phortune/controller/PhortuneAccountViewController.php b/src/applications/phortune/controller/PhortuneAccountViewController.php index c7f7877844..543f30ed99 100644 --- a/src/applications/phortune/controller/PhortuneAccountViewController.php +++ b/src/applications/phortune/controller/PhortuneAccountViewController.php @@ -2,15 +2,8 @@ final class PhortuneAccountViewController extends PhortuneController { - private $accountID; - - public function willProcessRequest(array $data) { - $this->accountID = $data['accountID']; - } - - public function processRequest() { - $request = $this->getRequest(); - $user = $request->getUser(); + public function handleRequest(AphrontRequest $request) { + $viewer = $this->getViewer(); // TODO: Currently, you must be able to edit an account to view the detail // page, because the account must be broadly visible so merchants can @@ -20,8 +13,8 @@ final class PhortuneAccountViewController extends PhortuneController { $can_edit = true; $account = id(new PhortuneAccountQuery()) - ->setViewer($user) - ->withIDs(array($this->accountID)) + ->setViewer($viewer) + ->withIDs(array($request->getURIData('accountID'))) ->requireCapabilities( array( PhabricatorPolicyCapability::CAN_VIEW, @@ -34,6 +27,13 @@ final class PhortuneAccountViewController extends PhortuneController { $title = $account->getName(); + $invoices = id(new PhortuneCartQuery()) + ->setViewer($viewer) + ->withAccountPHIDs(array($account->getPHID())) + ->needPurchases(true) + ->withInvoices(true) + ->execute(); + $crumbs = $this->buildApplicationCrumbs(); $this->addAccountCrumb($crumbs, $account, $link = false); @@ -43,7 +43,7 @@ final class PhortuneAccountViewController extends PhortuneController { $edit_uri = $this->getApplicationURI('account/edit/'.$account->getID().'/'); $actions = id(new PhabricatorActionListView()) - ->setUser($user) + ->setUser($viewer) ->setObjectURI($request->getRequestURI()) ->addAction( id(new PhabricatorActionView()) @@ -55,7 +55,7 @@ final class PhortuneAccountViewController extends PhortuneController { $properties = id(new PHUIPropertyListView()) ->setObject($account) - ->setUser($user); + ->setUser($viewer); $this->loadHandles($account->getMemberPHIDs()); @@ -63,12 +63,29 @@ final class PhortuneAccountViewController extends PhortuneController { pht('Members'), $this->renderHandlesForPHIDs($account->getMemberPHIDs())); + $status_items = $this->getStatusItemsForAccount($account, $invoices); + $status_view = new PHUIStatusListView(); + foreach ($status_items as $item) { + $status_view->addItem( + id(new PHUIStatusItemView()) + ->setIcon( + idx($item, 'icon'), + idx($item, 'color'), + idx($item, 'label')) + ->setTarget(idx($item, 'target')) + ->setNote(idx($item, 'note'))); + } + $properties->addProperty( + pht('Status'), + $status_view); + $properties->setActionList($actions); - $payment_methods = $this->buildPaymentMethodsSection($account); + $invoices = $this->buildInvoicesSection($account, $invoices); $purchase_history = $this->buildPurchaseHistorySection($account); $charge_history = $this->buildChargeHistorySection($account); $subscriptions = $this->buildSubscriptionsSection($account); + $payment_methods = $this->buildPaymentMethodsSection($account); $timeline = $this->buildTransactionTimeline( $account, @@ -83,10 +100,11 @@ final class PhortuneAccountViewController extends PhortuneController { array( $crumbs, $object_box, - $payment_methods, + $invoices, $purchase_history, $charge_history, $subscriptions, + $payment_methods, $timeline, ), array( @@ -165,6 +183,38 @@ final class PhortuneAccountViewController extends PhortuneController { ->appendChild($list); } + private function buildInvoicesSection( + PhortuneAccount $account, + array $carts) { + + $request = $this->getRequest(); + $viewer = $request->getUser(); + + $phids = array(); + foreach ($carts as $cart) { + $phids[] = $cart->getPHID(); + $phids[] = $cart->getMerchantPHID(); + foreach ($cart->getPurchases() as $purchase) { + $phids[] = $purchase->getPHID(); + } + } + $handles = $this->loadViewerHandles($phids); + + $table = id(new PhortuneOrderTableView()) + ->setNoDataString(pht('You have no unpaid invoices.')) + ->setIsInvoices(true) + ->setUser($viewer) + ->setCarts($carts) + ->setHandles($handles); + + $header = id(new PHUIHeaderView()) + ->setHeader(pht('Invoices Due')); + + return id(new PHUIObjectBoxView()) + ->setHeader($header) + ->appendChild($table); + } + private function buildPurchaseHistorySection(PhortuneAccount $account) { $request = $this->getRequest(); $viewer = $request->getUser(); @@ -308,4 +358,34 @@ final class PhortuneAccountViewController extends PhortuneController { return $crumbs; } + private function getStatusItemsForAccount( + PhortuneAccount $account, + array $invoices) { + + assert_instances_of($invoices, 'PhortuneCart'); + + $items = array(); + + if ($invoices) { + $items[] = array( + 'icon' => PHUIStatusItemView::ICON_WARNING, + 'color' => 'yellow', + 'target' => pht('Invoices'), + 'note' => pht('You have %d unpaid invoice(s).', count($invoices)), + ); + } else { + $items[] = array( + 'icon' => PHUIStatusItemView::ICON_ACCEPT, + 'color' => 'green', + 'target' => pht('Invoices'), + 'note' => pht('This account has no unpaid invoices.'), + ); + } + + // TODO: If a payment method has expired or is expiring soon, we should + // add a status check for it. + + return $items; + } + } diff --git a/src/applications/phortune/query/PhortuneCartQuery.php b/src/applications/phortune/query/PhortuneCartQuery.php index 6c7aaf77a3..df2003949c 100644 --- a/src/applications/phortune/query/PhortuneCartQuery.php +++ b/src/applications/phortune/query/PhortuneCartQuery.php @@ -9,6 +9,7 @@ final class PhortuneCartQuery private $merchantPHIDs; private $subscriptionPHIDs; private $statuses; + private $invoices; private $needPurchases; @@ -42,6 +43,18 @@ final class PhortuneCartQuery return $this; } + + /** + * Include or exclude carts which represent invoices with payments due. + * + * @param bool `true` to select invoices; `false` to exclude invoices. + * @return this + */ + public function withInvoices($invoices) { + $this->invoices = $invoices; + return $this; + } + public function needPurchases($need_purchases) { $this->needPurchases = $need_purchases; return $this; @@ -178,6 +191,20 @@ final class PhortuneCartQuery $this->statuses); } + if ($this->invoices !== null) { + if ($this->invoices) { + $where[] = qsprintf( + $conn, + 'cart.status = %s AND cart.subscriptionPHID IS NOT NULL', + PhortuneCart::STATUS_READY); + } else { + $where[] = qsprintf( + $conn, + 'cart.status != %s OR cart.subscriptionPHID IS NULL', + PhortuneCart::STATUS_READY); + } + } + return $this->formatWhereClause($where); } diff --git a/src/applications/phortune/storage/PhortuneSubscription.php b/src/applications/phortune/storage/PhortuneSubscription.php index 7b1666260a..51ac283668 100644 --- a/src/applications/phortune/storage/PhortuneSubscription.php +++ b/src/applications/phortune/storage/PhortuneSubscription.php @@ -166,6 +166,10 @@ final class PhortuneSubscription extends PhortuneDAO return $this->getImplementation()->getName($this); } + public function getCartName(PhortuneCart $cart) { + return $this->getImplementation()->getCartName($this, $cart); + } + public function getURI() { $account_id = $this->getAccount()->getID(); $id = $this->getID(); diff --git a/src/applications/phortune/subscription/PhortuneSubscriptionImplementation.php b/src/applications/phortune/subscription/PhortuneSubscriptionImplementation.php index 8376669a2d..8e7559c2e9 100644 --- a/src/applications/phortune/subscription/PhortuneSubscriptionImplementation.php +++ b/src/applications/phortune/subscription/PhortuneSubscriptionImplementation.php @@ -15,4 +15,10 @@ abstract class PhortuneSubscriptionImplementation { array()); } + public function getCartName( + PhortuneSubscription $subscription, + PhortuneCart $cart) { + return pht('Subscription'); + } + } diff --git a/src/applications/phortune/view/PhortuneOrderTableView.php b/src/applications/phortune/view/PhortuneOrderTableView.php index 9691d1fb0b..1abf128361 100644 --- a/src/applications/phortune/view/PhortuneOrderTableView.php +++ b/src/applications/phortune/view/PhortuneOrderTableView.php @@ -4,6 +4,8 @@ final class PhortuneOrderTableView extends AphrontView { private $carts; private $handles; + private $noDataString; + private $isInvoices; public function setHandles(array $handles) { $this->handles = $handles; @@ -23,11 +25,31 @@ final class PhortuneOrderTableView extends AphrontView { return $this->carts; } + public function setIsInvoices($is_invoices) { + $this->isInvoices = $is_invoices; + return $this; + } + + public function getIsInvoices() { + return $this->isInvoices; + } + + public function setNoDataString($no_data_string) { + $this->noDataString = $no_data_string; + return $this; + } + + public function getNoDataString() { + return $this->noDataString; + } + public function render() { $carts = $this->getCarts(); $handles = $this->getHandles(); $viewer = $this->getUser(); + $is_invoices = $this->getIsInvoices(); + $rows = array(); $rowc = array(); foreach ($carts as $cart) { @@ -42,9 +64,16 @@ final class PhortuneOrderTableView extends AphrontView { $purchase_name = ''; } + if ($is_invoices) { + $merchant_link = $handles[$cart->getMerchantPHID()]->renderLink(); + } else { + $merchant_link = null; + } + $rowc[] = ''; $rows[] = array( $cart->getID(), + $merchant_link, phutil_tag( 'strong', array(), @@ -56,6 +85,14 @@ final class PhortuneOrderTableView extends AphrontView { $cart->getTotalPriceAsCurrency()->formatForDisplay()), PhortuneCart::getNameForStatus($cart->getStatus()), phabricator_datetime($cart->getDateModified(), $viewer), + phabricator_datetime($cart->getDateCreated(), $viewer), + phutil_tag( + 'a', + array( + 'href' => $cart->getCheckoutURI(), + 'class' => 'small green button', + ), + pht('Pay Now')), ); foreach ($purchases as $purchase) { $id = $purchase->getID(); @@ -64,34 +101,56 @@ final class PhortuneOrderTableView extends AphrontView { $rowc[] = ''; $rows[] = array( + '', '', $handles[$purchase->getPHID()]->renderLink(), $price, '', '', + '', + '', ); } } $table = id(new AphrontTableView($rows)) + ->setNoDataString($this->getNoDataString()) ->setRowClasses($rowc) ->setHeaders( array( pht('ID'), - pht('Order'), + pht('Merchant'), + $is_invoices ? pht('Invoice') : pht('Order'), pht('Purchase'), pht('Amount'), pht('Status'), pht('Updated'), + pht('Invoice Date'), + null, )) ->setColumnClasses( array( + '', '', '', 'wide', 'right', '', 'right', + 'right', + '', + )) + ->setColumnVisibility( + array( + true, + $is_invoices, + true, + true, + true, + !$is_invoices, + !$is_invoices, + $is_invoices, + $is_invoices, )); return $table;