|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorPhortuneManagementInvoiceWorkflow |
| 4 | + extends PhabricatorPhortuneManagementWorkflow { |
| 5 | + |
| 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.'), |
| 19 | + ), |
| 20 | + array( |
| 21 | + 'name' => 'now', |
| 22 | + 'param' => 'time', |
| 23 | + 'help' => pht( |
| 24 | + 'Bill as though the current time is a specific time.'), |
| 25 | + ), |
| 26 | + array( |
| 27 | + 'name' => 'last', |
| 28 | + 'param' => 'time', |
| 29 | + 'help' => pht('Set the start of the billing period.'), |
| 30 | + ), |
| 31 | + array( |
| 32 | + 'name' => 'next', |
| 33 | + 'param' => 'time', |
| 34 | + 'help' => pht('Set the end of the billing period.'), |
| 35 | + ), |
| 36 | + array( |
| 37 | + 'name' => 'auto-range', |
| 38 | + 'help' => pht('Automatically use the current billing period.'), |
| 39 | + ), |
| 40 | + array( |
| 41 | + 'name' => 'force', |
| 42 | + 'help' => pht( |
| 43 | + 'Skip the prompt warning you that this operation is '. |
| 44 | + 'potentially dangerous.'), |
| 45 | + ), |
| 46 | + )); |
| 47 | + } |
| 48 | + |
| 49 | + public function execute(PhutilArgumentParser $args) { |
| 50 | + $console = PhutilConsole::getConsole(); |
| 51 | + $viewer = $this->getViewer(); |
| 52 | + |
| 53 | + $subscription_phid = $args->getArg('subscription'); |
| 54 | + if (!$subscription_phid) { |
| 55 | + throw new PhutilArgumentUsageException( |
| 56 | + pht( |
| 57 | + 'Specify which subscription to invoice with --subscription.')); |
| 58 | + } |
| 59 | + |
| 60 | + $subscription = id(new PhortuneSubscriptionQuery()) |
| 61 | + ->setViewer($viewer) |
| 62 | + ->withPHIDs(array($subscription_phid)) |
| 63 | + ->needTriggers(true) |
| 64 | + ->executeOne(); |
| 65 | + if (!$subscription) { |
| 66 | + throw new PhutilArgumentUsageException( |
| 67 | + pht( |
| 68 | + 'Unable to load subscription with PHID "%s".', |
| 69 | + $subscription_phid)); |
| 70 | + } |
| 71 | + |
| 72 | + $now = $args->getArg('now'); |
| 73 | + $now = $this->parseTimeArgument($now); |
| 74 | + if (!$now) { |
| 75 | + $now = PhabricatorTime::getNow(); |
| 76 | + } |
| 77 | + |
| 78 | + $time_guard = PhabricatorTime::pushTime($now, date_default_timezone_get()); |
| 79 | + |
| 80 | + $console->writeOut( |
| 81 | + "%s\n", |
| 82 | + pht( |
| 83 | + 'Set current time to %s.', |
| 84 | + phabricator_datetime(PhabricatorTime::getNow(), $viewer))); |
| 85 | + |
| 86 | + $auto_range = $args->getArg('auto-range'); |
| 87 | + $last_arg = $args->getArg('last'); |
| 88 | + $next_arg = $args->getARg('next'); |
| 89 | + |
| 90 | + if (!$auto_range && !$last_arg && !$next_arg) { |
| 91 | + throw new PhutilArgumentUsageException( |
| 92 | + pht( |
| 93 | + 'Specify a billing range with --last and --next, or use '. |
| 94 | + '--auto-range.')); |
| 95 | + } else if (!$auto_range & (!$last_arg || !$next_arg)) { |
| 96 | + throw new PhutilArgumentUsageException( |
| 97 | + pht( |
| 98 | + 'When specifying --last or --next, you must specify both arguments '. |
| 99 | + 'to define the beginning and end of the billing range.')); |
| 100 | + } else if (!$auto_range && ($last_arg && $next_arg)) { |
| 101 | + $last_time = $this->parseTimeArgument($args->getArg('last')); |
| 102 | + $next_time = $this->parseTimeArgument($args->getArg('next')); |
| 103 | + } else if ($auto_range && ($last_arg || $next_arg)) { |
| 104 | + throw new PhutilArgumentUsageException( |
| 105 | + pht( |
| 106 | + 'Use either --auto-range or --last and --next to specify the '. |
| 107 | + 'billing range, but not both.')); |
| 108 | + } else { |
| 109 | + $trigger = $subscription->getTrigger(); |
| 110 | + $event = $trigger->getEvent(); |
| 111 | + if (!$event) { |
| 112 | + throw new PhutilArgumentUsageException( |
| 113 | + pht( |
| 114 | + 'Unable to calculate --auto-range, this subscription has not been '. |
| 115 | + 'scheduled for billing yet. Wait for the trigger daemon to '. |
| 116 | + 'schedule the subscription.')); |
| 117 | + } |
| 118 | + $last_time = $event->getLastEventEpoch(); |
| 119 | + $next_time = $event->getNextEventEpoch(); |
| 120 | + } |
| 121 | + |
| 122 | + $console->writeOut( |
| 123 | + "%s\n", |
| 124 | + pht( |
| 125 | + 'Preparing to invoice subscription "%s" from %s to %s.', |
| 126 | + $subscription->getSubscriptionName(), |
| 127 | + ($last_time |
| 128 | + ? phabricator_datetime($last_time, $viewer) |
| 129 | + : pht('subscription creation')), |
| 130 | + phabricator_datetime($next_time, $viewer))); |
| 131 | + |
| 132 | + PhabricatorWorker::setRunAllTasksInProcess(true); |
| 133 | + |
| 134 | + if (!$args->getArg('force')) { |
| 135 | + $console->writeOut( |
| 136 | + "**<bg:yellow> %s </bg>**\n%s\n", |
| 137 | + pht('WARNING'), |
| 138 | + phutil_console_wrap( |
| 139 | + pht( |
| 140 | + 'Manually invoicing will double bill payment accounts if the '. |
| 141 | + 'range overlaps an existing or future invoice. This script is '. |
| 142 | + 'intended for testing and development, and should not be part '. |
| 143 | + 'of routine billing operations. If you continue, you may '. |
| 144 | + 'incorrectly overcharge customers.'))); |
| 145 | + |
| 146 | + if (!phutil_console_confirm(pht('Really invoice this subscription?'))) { |
| 147 | + throw new Exception(pht('Declining to invoice.')); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + PhabricatorWorker::scheduleTask( |
| 152 | + 'PhortuneSubscriptionWorker', |
| 153 | + array( |
| 154 | + 'subscriptionPHID' => $subscription->getPHID(), |
| 155 | + 'trigger.last-epoch' => $last_time, |
| 156 | + 'trigger.next-epoch' => $next_time, |
| 157 | + ), |
| 158 | + array( |
| 159 | + 'objectPHID' => $subscription->getPHID(), |
| 160 | + )); |
| 161 | + |
| 162 | + return 0; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments