3 final class PhabricatorPhortuneManagementInvoiceWorkflow
4 extends PhabricatorPhortuneManagementWorkflow
{
6 protected function didConstruct() {
11 'Invoices a subscription for a given billing period. This can '.
12 'charge payment accounts twice.'))
16 'name' => 'subscription',
18 'help' => pht('Subscription to invoice.'),
24 'Bill as though the current time is a specific time.'),
29 'help' => pht('Set the start of the billing period.'),
34 'help' => pht('Set the end of the billing period.'),
37 'name' => 'auto-range',
38 'help' => pht('Automatically use the current billing period.'),
43 'Skip the prompt warning you that this operation is '.
44 'potentially dangerous.'),
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(
57 'Specify which subscription to invoice with %s.',
61 $subscription = id(new PhortuneSubscriptionQuery())
63 ->withPHIDs(array($subscription_phid))
67 throw new PhutilArgumentUsageException(
69 'Unable to load subscription with PHID "%s".',
73 $now = $args->getArg('now');
74 $now = $this->parseTimeArgument($now);
76 $now = PhabricatorTime
::getNow();
79 $time_guard = PhabricatorTime
::pushTime($now, date_default_timezone_get());
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(
94 'Specify a billing range with %s and %s, or use %s.',
98 } else if (!$auto_range & (!$last_arg ||
!$next_arg)) {
99 throw new PhutilArgumentUsageException(
101 'When specifying %s or %s, you must specify both arguments '.
102 'to define the beginning and end of the billing range.',
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(
111 'Use either %s or %s and %s to specify the '.
112 'billing range, but not both.',
117 $trigger = $subscription->getTrigger();
118 $event = $trigger->getEvent();
120 throw new PhutilArgumentUsageException(
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.',
127 $last_time = $event->getLastEventEpoch();
128 $next_time = $event->getNextEventEpoch();
134 'Preparing to invoice subscription "%s" from %s to %s.',
135 $subscription->getSubscriptionName(),
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')) {
145 "**<bg:yellow> %s </bg>**\n%s\n",
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',
163 'subscriptionPHID' => $subscription->getPHID(),
164 'trigger.last-epoch' => $last_time,
165 'trigger.this-epoch' => $next_time,
169 'objectPHID' => $subscription->getPHID(),