Files
itflow/api/v1/contracts/send.php
johnnyq ecbb974fff API stage 3, calendar events, trips, accounts and money, contracts, and files and folders: calendars can be read, and calendar events read with their attendees, created once or repeating for a client or nobody, changed, sent as invitations, skipped for a day of a series and deleted, following calendarEventCan(); trips can be read, logged, changed and deleted through tripSave() and tripDelete(), anyone their own and someone else's with Financial; accounts can be read with their balances, added, changed, archived, unarchived and deleted while nothing is on them, other income and transfers read, recorded, changed and deleted, and recurring expenses read, set up, changed, paused and resumed, recorded now, moved on past missed runs and deleted, keeping reconciled lines as they were; contracts can be read with their prepaid hours and signing state, added, changed, made active, renewed, cancelled and deleted, sent for signature or the link sent again, the request withdrawn, prepaid hours added with a draft invoice or deleted, tickets' time drawn or put back, and the billing recurring invoice made, linked or unlinked; a client's files can be read, uploaded as base64, downloaded, renamed, moved, archived, unarchived and deleted once archived, and folders read, made, renamed, moved and deleted with their contents moved up; new api/v1/form_input.php builds a save function's input from the API's field names; the upload allow-list moves from agent/post/file.php into fileUploadExtensions() so Upload Files and the API check the same list; enforce_api_rbac.php maps the new resources, lets events, calendars and trips check their own rules, puts the new state changes at Modify and treats files/download as a read; 26.11.9 changelog entries
2026-11-13 23:51:10 -05:00

81 lines
4.2 KiB
PHP

<?php
/*
* API - Contracts - Send
* POST /api/v1/contracts/send.php
*
* Send a contract to be signed, as Send for signature does - to one of the client's
* contacts, or a name and email address - with a link to read and sign it. With
* contract_signature_id, the link of a request still waiting goes again instead.
*
* Parameters (POST, JSON body): api_key, contract_id, client_id (its client - needed
* when the key's user is limited to certain clients),
* contact_id, or to_name and to_email - who signs it
* note optional - A note in the email
* contract_signature_id optional - Send this waiting request's link again
*
* Answer: contract_signature_id, and emailed - 0 when mail isn't set up (the link is then in the contract)
*/
require_once '../validate_api_key.php';
require_once '../require_post_method.php';
require_once "../../../includes/load_global_settings.php";
require_once "../../../includes/load_company_settings.php";
require_once 'contract_find.php';
$update_count = false;
if ($contract) {
if (!contractSignaturesReady()) {
apiFormRefuse('Signing needs the database update - run it from Maintenance > Update first');
}
// The link of one still waiting, again
if (!empty($_POST['contract_signature_id'])) {
$round = contractSignatureRow($_POST['contract_signature_id']);
$state = $round && intval($round['contract_id']) === $contract_id ? contractSignatureState($round) : null;
if (!$state || !in_array($state['key'], ['sent', 'viewed'], true)) {
apiFormRefuse("That request isn't waiting for the client any more");
}
$queued = contractSignatureRequestEmail($round);
$update_count = 1;
$return_arr['contract_signature_id'] = intval($round['contract_signature_id']);
$return_arr['emailed'] = $queued ? 1 : 0;
logAudit("Contract", "Send", "Sent the signing link for contract $contract_label again to " . escapeSql($round['contract_signature_sent_to_name'] . ' <' . $round['contract_signature_sent_to_email'] . '>') . " via API ($api_key_name)", $client_id, $contract_id);
require_once '../update_output.php';
}
if (in_array($contract['contract_status'], ['Ended', 'Cancelled'], true)) {
apiFormRefuse("An ended or cancelled contract isn't sent for signature - renew it first");
}
if (!empty(getFieldById('clients', $client_id, 'client_archived_at'))) {
apiFormRefuse('The client is archived - restore them before sending them a contract to sign');
}
$contact_id = intval($_POST['contact_id'] ?? 0);
if ($contact_id > 0) {
$contact = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_name, contact_email FROM contacts WHERE contact_id = $contact_id AND contact_client_id = $client_id AND contact_archived_at IS NULL"));
$to_name = (string) ($contact['contact_name'] ?? '');
$to_email = trim((string) ($contact['contact_email'] ?? ''));
if (!$contact || !filter_var($to_email, FILTER_VALIDATE_EMAIL)) {
apiFormRefuse("contact_id is one of the client's contacts with an email address - or send to_name and to_email");
}
} else {
$contact_id = 0;
$to_name = mb_substr(trim(preg_replace('/\s+/u', ' ', strip_tags((string) ($_POST['to_name'] ?? '')))), 0, 200);
$to_email = trim((string) ($_POST['to_email'] ?? ''));
if ($to_name === '' || !filter_var($to_email, FILTER_VALIDATE_EMAIL) || strlen($to_email) > 255) {
apiFormRefuse('Send contact_id, or to_name and to_email - who signs it');
}
}
$note = mb_substr(trim((string) ($_POST['note'] ?? '')), 0, 2000);
[$round_id, $queued] = contractSignatureSend($contract, $to_name, $to_email, $contact_id, $note, $session_user_id);
$update_count = 1;
$return_arr['contract_signature_id'] = intval($round_id);
$return_arr['emailed'] = $queued ? 1 : 0;
logAudit("Contract", "Send", "Sent contract $contract_label for signature to " . escapeSql("$to_name <$to_email>") . " via API ($api_key_name)", $client_id, $contract_id);
logAudit("API", "Success", "Sent contract $contract_label for signature via API ($api_key_name)", $client_id);
}
// Output
require_once '../update_output.php';