private function buildPastInvoices(PhortuneSubscription $subscription, $authority)
 {
     $viewer = $this->getViewer();
     $invoices = id(new PhortuneCartQuery())->setViewer($viewer)->withSubscriptionPHIDs(array($subscription->getPHID()))->needPurchases(true)->withStatuses(array(PhortuneCart::STATUS_PURCHASING, PhortuneCart::STATUS_CHARGED, PhortuneCart::STATUS_HOLD, PhortuneCart::STATUS_REVIEW, PhortuneCart::STATUS_PURCHASED))->setLimit(50)->execute();
     $phids = array();
     foreach ($invoices as $invoice) {
         $phids[] = $invoice->getPHID();
         foreach ($invoice->getPurchases() as $purchase) {
             $phids[] = $purchase->getPHID();
         }
     }
     $handles = $this->loadViewerHandles($phids);
     $invoice_table = id(new PhortuneOrderTableView())->setUser($viewer)->setCarts($invoices)->setHandles($handles);
     $account = $subscription->getAccount();
     $merchant = $subscription->getMerchant();
     $account_id = $account->getID();
     $merchant_id = $merchant->getID();
     $subscription_id = $subscription->getID();
     if ($authority) {
         $invoices_uri = $this->getApplicationURI("merchant/{$merchant_id}/subscription/order/{$subscription_id}/");
     } else {
         $invoices_uri = $this->getApplicationURI("{$account_id}/subscription/order/{$subscription_id}/");
     }
     $invoice_header = id(new PHUIHeaderView())->setHeader(pht('Past Invoices'))->addActionLink(id(new PHUIButtonView())->setTag('a')->setIcon(id(new PHUIIconView())->setIconFont('fa-list'))->setHref($invoices_uri)->setText(pht('View All Invoices')));
     return id(new PHUIObjectBoxView())->setHeader($invoice_header)->appendChild($invoice_table);
 }
 protected function loadPage()
 {
     $table = new PhortuneSubscription();
     $conn = $table->establishConnection('r');
     $rows = queryfx_all($conn, 'SELECT subscription.* FROM %T subscription %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn), $this->buildOrderClause($conn), $this->buildLimitClause($conn));
     return $table->loadAllFromArray($rows);
 }
 /**
  * Get the start and end epoch timestamps for this billing period.
  *
  * @param PhortuneSubscription The subscription being billed.
  * @return pair<int, int> Beginning and end of the billing range.
  */
 private function getBillingPeriodRange(PhortuneSubscription $subscription)
 {
     $data = $this->getTaskData();
     $last_epoch = idx($data, 'trigger.last-epoch');
     if (!$last_epoch) {
         // If this is the first time the subscription is firing, use the
         // creation date as the start of the billing period.
         $last_epoch = $subscription->getDateCreated();
     }
     $this_epoch = idx($data, 'trigger.this-epoch');
     if (!$last_epoch || !$this_epoch) {
         throw new PhabricatorWorkerPermanentFailureException(pht('Subscription is missing billing period information.'));
     }
     $period_length = $this_epoch - $last_epoch;
     if ($period_length <= 0) {
         throw new PhabricatorWorkerPermanentFailureException(pht('Subscription has invalid billing period.'));
     }
     if (empty($data['manual'])) {
         if (PhabricatorTime::getNow() < $this_epoch) {
             throw new Exception(pht('Refusing to generate a subscription invoice for a billing period ' . 'which ends in the future.'));
         }
     }
     return array($last_epoch, $this_epoch);
 }