@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at recaptime-dev/main 317 lines 8.9 kB view raw
1<?php 2 3final class PhortuneAccountSubscriptionViewController 4 extends PhortuneAccountController { 5 6 protected function shouldRequireAccountEditCapability() { 7 return false; 8 } 9 10 protected function handleAccountRequest(AphrontRequest $request) { 11 $viewer = $this->getViewer(); 12 13 $subscription = id(new PhortuneSubscriptionQuery()) 14 ->setViewer($viewer) 15 ->withIDs(array($request->getURIData('subscriptionID'))) 16 ->needTriggers(true) 17 ->executeOne(); 18 if (!$subscription) { 19 return new Aphront404Response(); 20 } 21 22 $can_edit = PhabricatorPolicyFilter::hasCapability( 23 $viewer, 24 $subscription, 25 PhabricatorPolicyCapability::CAN_EDIT); 26 27 $merchant = $subscription->getMerchant(); 28 $account = $subscription->getAccount(); 29 30 $account_id = $account->getID(); 31 $subscription_id = $subscription->getID(); 32 33 $title = $subscription->getSubscriptionFullName(); 34 35 $header = id(new PHUIHeaderView()) 36 ->setHeader($title) 37 ->setHeaderIcon('fa-retweet'); 38 39 $edit_uri = $subscription->getEditURI(); 40 41 $crumbs = $this->buildApplicationCrumbs() 42 ->addTextCrumb($subscription->getSubscriptionCrumbName()) 43 ->setBorder(true); 44 45 $properties = id(new PHUIPropertyListView()) 46 ->setUser($viewer); 47 48 $next_invoice = $subscription->getTrigger()->getNextEventPrediction(); 49 $properties->addProperty( 50 pht('Next Invoice'), 51 phabricator_datetime($next_invoice, $viewer)); 52 53 $autopay = $this->newAutopayView($subscription); 54 55 $details = id(new PHUIObjectBoxView()) 56 ->setHeaderText(pht('Subscription Details')) 57 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 58 ->addPropertyList($properties); 59 60 $due_box = $this->buildDueInvoices($subscription); 61 $invoice_box = $this->buildPastInvoices($subscription); 62 63 $timeline = $this->buildTransactionTimeline( 64 $subscription, 65 new PhortuneSubscriptionTransactionQuery()); 66 $timeline->setShouldTerminate(true); 67 68 $view = id(new PHUITwoColumnView()) 69 ->setHeader($header) 70 ->setFooter( 71 array( 72 $details, 73 $autopay, 74 $due_box, 75 $invoice_box, 76 $timeline, 77 )); 78 79 return $this->newPage() 80 ->setTitle($title) 81 ->setCrumbs($crumbs) 82 ->appendChild($view); 83 } 84 85 private function buildDueInvoices(PhortuneSubscription $subscription) { 86 $viewer = $this->getViewer(); 87 88 $invoices = id(new PhortuneCartQuery()) 89 ->setViewer($viewer) 90 ->withSubscriptionPHIDs(array($subscription->getPHID())) 91 ->needPurchases(true) 92 ->withInvoices(true) 93 ->execute(); 94 95 $invoice_table = id(new PhortuneOrderTableView()) 96 ->setUser($viewer) 97 ->setCarts($invoices) 98 ->setIsInvoices(true); 99 100 $invoice_header = id(new PHUIHeaderView()) 101 ->setHeader(pht('Invoices Due')); 102 103 return id(new PHUIObjectBoxView()) 104 ->setHeader($invoice_header) 105 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 106 ->appendChild($invoice_table); 107 } 108 109 private function buildPastInvoices(PhortuneSubscription $subscription) { 110 $viewer = $this->getViewer(); 111 112 $invoices = id(new PhortuneCartQuery()) 113 ->setViewer($viewer) 114 ->withSubscriptionPHIDs(array($subscription->getPHID())) 115 ->needPurchases(true) 116 ->withStatuses( 117 array( 118 PhortuneCart::STATUS_PURCHASING, 119 PhortuneCart::STATUS_CHARGED, 120 PhortuneCart::STATUS_HOLD, 121 PhortuneCart::STATUS_REVIEW, 122 PhortuneCart::STATUS_PURCHASED, 123 )) 124 ->setLimit(50) 125 ->execute(); 126 127 $invoice_table = id(new PhortuneOrderTableView()) 128 ->setUser($viewer) 129 ->setCarts($invoices); 130 131 $account = $subscription->getAccount(); 132 $merchant = $subscription->getMerchant(); 133 134 $account_id = $account->getID(); 135 $merchant_id = $merchant->getID(); 136 $subscription_id = $subscription->getID(); 137 138 $invoices_uri = $this->getApplicationURI( 139 "{$account_id}/subscription/order/{$subscription_id}/"); 140 141 $invoice_header = id(new PHUIHeaderView()) 142 ->setHeader(pht('Past Invoices')) 143 ->addActionLink( 144 id(new PHUIButtonView()) 145 ->setTag('a') 146 ->setIcon('fa-list') 147 ->setHref($invoices_uri) 148 ->setText(pht('View All Invoices'))); 149 150 return id(new PHUIObjectBoxView()) 151 ->setHeader($invoice_header) 152 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 153 ->appendChild($invoice_table); 154 } 155 156 private function newAutopayView(PhortuneSubscription $subscription) { 157 $viewer = $this->getViewer(); 158 $account = $subscription->getAccount(); 159 160 $add_method_uri = urisprintf( 161 '/account/%d/methods/new/?subscriptionID=%s', 162 $account->getID(), 163 $subscription->getID()); 164 $add_method_uri = $this->getApplicationURI($add_method_uri); 165 166 $can_edit = PhabricatorPolicyFilter::hasCapability( 167 $viewer, 168 $subscription, 169 PhabricatorPolicyCapability::CAN_EDIT); 170 171 $methods = id(new PhortunePaymentMethodQuery()) 172 ->setViewer($viewer) 173 ->withAccountPHIDs(array($subscription->getAccountPHID())) 174 ->withMerchantPHIDs(array($subscription->getMerchantPHID())) 175 ->withStatuses( 176 array( 177 PhortunePaymentMethod::STATUS_ACTIVE, 178 )) 179 ->execute(); 180 $methods = mpull($methods, null, 'getPHID'); 181 182 $autopay_phid = $subscription->getDefaultPaymentMethodPHID(); 183 $autopay_method = idx($methods, $autopay_phid); 184 185 $header = id(new PHUIHeaderView()) 186 ->setHeader(pht('Autopay')) 187 ->addActionLink( 188 id(new PHUIButtonView()) 189 ->setTag('a') 190 ->setIcon('fa-plus') 191 ->setHref($add_method_uri) 192 ->setText(pht('Add Payment Method')) 193 ->setWorkflow(!$can_edit) 194 ->setDisabled(!$can_edit)); 195 196 $methods = array_select_keys($methods, array($autopay_phid)) + $methods; 197 198 $rows = array(); 199 $rowc = array(); 200 foreach ($methods as $method) { 201 $is_autopay = ($autopay_method === $method); 202 203 $remove_uri = urisprintf( 204 '/card/%d/disable/?subscriptionID=%d', 205 $method->getID(), 206 $subscription->getID()); 207 $remove_uri = $this->getApplicationURI($remove_uri); 208 209 $autopay_uri = urisprintf( 210 '/account/%d/subscriptions/%d/autopay/%d/', 211 $account->getID(), 212 $subscription->getID(), 213 $method->getID()); 214 $autopay_uri = $this->getApplicationURI($autopay_uri); 215 216 $remove_button = id(new PHUIButtonView()) 217 ->setTag('a') 218 ->setColor('grey') 219 ->setIcon('fa-times') 220 ->setText(pht('Delete')) 221 ->setHref($remove_uri) 222 ->setWorkflow(true) 223 ->setDisabled(!$can_edit); 224 225 if ($is_autopay) { 226 $autopay_button = id(new PHUIButtonView()) 227 ->setColor('red') 228 ->setIcon('fa-times') 229 ->setText(pht('Stop Autopay')); 230 } else { 231 if ($autopay_method) { 232 $make_color = 'grey'; 233 } else { 234 $make_color = 'green'; 235 } 236 237 $autopay_button = id(new PHUIButtonView()) 238 ->setColor($make_color) 239 ->setIcon('fa-retweet') 240 ->setText(pht('Start Autopay')); 241 } 242 243 $autopay_button 244 ->setTag('a') 245 ->setHref($autopay_uri) 246 ->setWorkflow(true) 247 ->setDisabled(!$can_edit); 248 249 $rows[] = array( 250 $method->getID(), 251 phutil_tag( 252 'a', 253 array( 254 'href' => $method->getURI(), 255 ), 256 $method->getFullDisplayName()), 257 $method->getDisplayExpires(), 258 $autopay_button, 259 $remove_button, 260 ); 261 262 if ($is_autopay) { 263 $rowc[] = 'highlighted'; 264 } else { 265 $rowc[] = null; 266 } 267 } 268 269 $method_table = id(new AphrontTableView($rows)) 270 ->setHeaders( 271 array( 272 pht('ID'), 273 pht('Payment Method'), 274 pht('Expires'), 275 null, 276 null, 277 )) 278 ->setRowClasses($rowc) 279 ->setColumnClasses( 280 array( 281 null, 282 'pri wide', 283 null, 284 'right', 285 null, 286 )); 287 288 if (!$autopay_method) { 289 $method_table->setNotice( 290 array( 291 id(new PHUIIconView())->setIcon('fa-warning yellow'), 292 ' ', 293 pht('Autopay is not currently configured for this subscription.'), 294 )); 295 } else { 296 $method_table->setNotice( 297 array( 298 id(new PHUIIconView())->setIcon('fa-check green'), 299 ' ', 300 pht( 301 'Autopay is configured using %s.', 302 phutil_tag( 303 'a', 304 array( 305 'href' => $autopay_method->getURI(), 306 ), 307 $autopay_method->getFullDisplayName())), 308 )); 309 } 310 311 return id(new PHUIObjectBoxView()) 312 ->setHeader($header) 313 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 314 ->setTable($method_table); 315 } 316 317}