Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / phortune / management / PhabricatorPhortuneManagementInvoiceWorkflow.php
blobd1fce6dae072b4c951fcab9549177a587c55b764
1 <?php
3 final class PhabricatorPhortuneManagementInvoiceWorkflow
4 extends PhabricatorPhortuneManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('invoice')
9 ->setSynopsis(
10 pht(
11 'Invoices a subscription for a given billing period. This can '.
12 'charge payment accounts twice.'))
13 ->setArguments(
14 array(
15 array(
16 'name' => 'subscription',
17 'param' => 'phid',
18 'help' => pht('Subscription to invoice.'),
20 array(
21 'name' => 'now',
22 'param' => 'time',
23 'help' => pht(
24 'Bill as though the current time is a specific time.'),
26 array(
27 'name' => 'last',
28 'param' => 'time',
29 'help' => pht('Set the start of the billing period.'),
31 array(
32 'name' => 'next',
33 'param' => 'time',
34 'help' => pht('Set the end of the billing period.'),
36 array(
37 'name' => 'auto-range',
38 'help' => pht('Automatically use the current billing period.'),
40 array(
41 'name' => 'force',
42 'help' => pht(
43 'Skip the prompt warning you that this operation is '.
44 'potentially dangerous.'),
46 ));
49 public function execute(PhutilArgumentParser $args) {
50 $console = PhutilConsole::getConsole();
51 $viewer = $this->getViewer();
53 $subscription_phid = $args->getArg('subscription');
54 if (!$subscription_phid) {
55 throw new PhutilArgumentUsageException(
56 pht(
57 'Specify which subscription to invoice with %s.',
58 '--subscription'));
61 $subscription = id(new PhortuneSubscriptionQuery())
62 ->setViewer($viewer)
63 ->withPHIDs(array($subscription_phid))
64 ->needTriggers(true)
65 ->executeOne();
66 if (!$subscription) {
67 throw new PhutilArgumentUsageException(
68 pht(
69 'Unable to load subscription with PHID "%s".',
70 $subscription_phid));
73 $now = $args->getArg('now');
74 $now = $this->parseTimeArgument($now);
75 if (!$now) {
76 $now = PhabricatorTime::getNow();
79 $time_guard = PhabricatorTime::pushTime($now, date_default_timezone_get());
81 $console->writeOut(
82 "%s\n",
83 pht(
84 'Set current time to %s.',
85 phabricator_datetime(PhabricatorTime::getNow(), $viewer)));
87 $auto_range = $args->getArg('auto-range');
88 $last_arg = $args->getArg('last');
89 $next_arg = $args->getArg('next');
91 if (!$auto_range && !$last_arg && !$next_arg) {
92 throw new PhutilArgumentUsageException(
93 pht(
94 'Specify a billing range with %s and %s, or use %s.',
95 '--last',
96 '--next',
97 '--auto-range'));
98 } else if (!$auto_range & (!$last_arg || !$next_arg)) {
99 throw new PhutilArgumentUsageException(
100 pht(
101 'When specifying %s or %s, you must specify both arguments '.
102 'to define the beginning and end of the billing range.',
103 '--last',
104 '--next'));
105 } else if (!$auto_range && ($last_arg && $next_arg)) {
106 $last_time = $this->parseTimeArgument($args->getArg('last'));
107 $next_time = $this->parseTimeArgument($args->getArg('next'));
108 } else if ($auto_range && ($last_arg || $next_arg)) {
109 throw new PhutilArgumentUsageException(
110 pht(
111 'Use either %s or %s and %s to specify the '.
112 'billing range, but not both.',
113 '--auto-range',
114 '--last',
115 '--next'));
116 } else {
117 $trigger = $subscription->getTrigger();
118 $event = $trigger->getEvent();
119 if (!$event) {
120 throw new PhutilArgumentUsageException(
121 pht(
122 'Unable to calculate %s, this subscription has not been '.
123 'scheduled for billing yet. Wait for the trigger daemon to '.
124 'schedule the subscription.',
125 '--auto-range'));
127 $last_time = $event->getLastEventEpoch();
128 $next_time = $event->getNextEventEpoch();
131 $console->writeOut(
132 "%s\n",
133 pht(
134 'Preparing to invoice subscription "%s" from %s to %s.',
135 $subscription->getSubscriptionName(),
136 ($last_time
137 ? phabricator_datetime($last_time, $viewer)
138 : pht('subscription creation')),
139 phabricator_datetime($next_time, $viewer)));
141 PhabricatorWorker::setRunAllTasksInProcess(true);
143 if (!$args->getArg('force')) {
144 $console->writeOut(
145 "**<bg:yellow> %s </bg>**\n%s\n",
146 pht('WARNING'),
147 phutil_console_wrap(
148 pht(
149 'Manually invoicing will double bill payment accounts if the '.
150 'range overlaps an existing or future invoice. This script is '.
151 'intended for testing and development, and should not be part '.
152 'of routine billing operations. If you continue, you may '.
153 'incorrectly overcharge customers.')));
155 if (!phutil_console_confirm(pht('Really invoice this subscription?'))) {
156 throw new Exception(pht('Declining to invoice.'));
160 PhabricatorWorker::scheduleTask(
161 'PhortuneSubscriptionWorker',
162 array(
163 'subscriptionPHID' => $subscription->getPHID(),
164 'trigger.last-epoch' => $last_time,
165 'trigger.this-epoch' => $next_time,
166 'manual' => true,
168 array(
169 'objectPHID' => $subscription->getPHID(),
172 return 0;