Update first'); } if (intval($config_module_enable_accounting) !== 1) { apiFormRefuse('Recurring invoices need accounting turned on, in Admin > Settings > Modules'); } if (!contractRecurringShouldRun($contract) && $contract['contract_status'] !== 'Draft') { apiFormRefuse("An ended contract isn't billed - renew it first"); } if (contractRecurringRow($contract)) { apiFormRefuse('It already has a recurring invoice - unlink that one first'); } $by = 'the API (' . html_entity_decode($api_key_name, ENT_QUOTES, 'UTF-8') . ')'; if (!empty($_POST['recurring_invoice_id'])) { $recurring_id = intval($_POST['recurring_invoice_id']); $choices = contractRecurringChoices($client_id, $contract_id); if (!isset($choices[$recurring_id])) { apiFormRefuse("recurring_invoice_id is one of the client's recurring invoices that isn't another contract's"); } $number = billingNumber('recurring', $choices[$recurring_id]); $state = contractRecurringLink($contract, $recurring_id, $by); $what = "Linked recurring invoice " . escapeSql($number) . " to"; } else { $in = []; foreach (['item_name', 'amount', 'frequency', 'start', 'description', 'tax_id', 'category', 'auto_send'] as $key) { if (array_key_exists($key, $_POST)) { $in[$key] = is_scalar($_POST[$key]) ? (string) $_POST[$key] : ''; } } $in += ['frequency' => 'month', 'auto_send' => '1', 'start' => contractRecurringFirstDate($contract)]; [$f, $error] = contractRecurringFromInput($in); if ($error !== '') { apiFormRefuse($error); } [$recurring_id, $number, $state] = contractRecurringCreate($contract, $f, $by); $what = "Made recurring invoice " . escapeSql($number) . " for"; } $update_count = 1; $return_arr['recurring_invoice_id'] = intval($recurring_id); $return_arr['reference'] = $number; $return_arr['state'] = $state; logAudit("Contract", "Edit", "$what contract $contract_label via API ($api_key_name)", $client_id, $contract_id); logAudit("API", "Success", "$what contract $contract_label via API ($api_key_name)", $client_id); } // Output require_once '../update_output.php';