mirror of
https://github.com/itflow-org/itflow
synced 2026-11-21 20:07:24 +00:00
Invoices, quotes and recurring invoices makeover stage 4: quote lines billed one time or each month, quarter or year with a Billed choice on the add-a-line row and in Edit Line, quotes grouped into One-time, Each month, Each quarter and Each year with a total for each on the page, PDF and client view, lines dragged within their own group, Start Billing on accepted quotes putting one-time lines on a draft invoice due on the client's terms and each recurring part on a new recurring invoice from a chosen date, emailed or left as drafts, or added to one of the client's recurring invoices of the same frequency, with an option to bill the first period on the invoice, an Invoiced as card on the quote linking every invoice and recurring invoice it became including ones it added lines to, From links back on invoices and recurring invoices, monthly amounts in the quotes list and its totals, search, client portal, quote email, Send and Copy windows, quotes export and client export, and Quotes Pipeline valuing quotes at up front plus a year of recurring lines. Fixes Make Invoice invoicing a quote twice or one never accepted, and its audit entry using the current quote prefix instead of the quote's own. Adds DB 2.9.3 with item_frequency, quote_monthly_amount, invoice_quote_id and recurring_invoice_quote_id, linking invoices to their quotes from history, quoteConvert() as the one invoicing path claiming the quote in a transaction, quoteLineGroups(), quoteAmountText() and quoteMadeFrom(), reportCsv() totals written as given, and updates the 26.11 changelog
This commit is contained in:
28
CHANGELOG.md
28
CHANGELOG.md
@@ -6,9 +6,9 @@ This file documents all notable changes made to ITFlow.
|
||||
|
||||
### Upgrading to 26.11
|
||||
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's database changes (2.7.9 to 2.9.2) in the same pass. Nothing else is required.
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's database changes (2.7.9 to 2.9.3) in the same pass. Nothing else is required.
|
||||
|
||||
This is a big release, with thirteen database steps in one go, so take a backup before you update.
|
||||
This is a big release, with fourteen database steps in one go, so take a backup before you update.
|
||||
|
||||
The update also clears out data that deletes in older versions left behind, all of it pointing at something that no longer exists:
|
||||
|
||||
@@ -17,7 +17,7 @@ The update also clears out data that deletes in older versions left behind, all
|
||||
- the tasks, approvals and history of deleted tickets, the projects of deleted clients, and tickets still pointing at a deleted project
|
||||
- project template entries whose ticket template is gone, and the same service link saved twice
|
||||
|
||||
It also gives every existing network a type (LAN, WAN, Guest and so on) guessed from its name or description, which you can change afterwards, and fills in the issue date, names covered and fingerprint of every certificate already on file.
|
||||
It also gives every existing network a type (LAN, WAN, Guest and so on) guessed from its name or description, which you can change afterwards, and fills in the issue date, names covered and fingerprint of every certificate already on file. Invoices made from a quote before the update are linked to that quote, so each one shows where it came from.
|
||||
|
||||
If you are coming from 26.08 or earlier, the database update still has to be run from the command line one time, exactly as described under Upgrading to 26.09 below.
|
||||
|
||||
@@ -50,6 +50,12 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Every invoice made from a recurring invoice takes its taxes at today's rates, and the recurring invoice's lines are updated to match. The nightly run used to copy the amounts saved when each line was added, while Invoice Now used today's rates.
|
||||
- A schedule on the 29th, 30th or 31st keeps that day. After a shorter month it goes back to the 31st, where it used to stay on the 28th from then on.
|
||||
- The email goes to the client's primary and billing contacts that are not archived, once per address. When outgoing mail is not set up, nothing is queued and the invoice stays a draft. It used to be marked Sent anyway.
|
||||
- Quotes:
|
||||
- A quote's amount is now what it bills up front. Lines billed each month, quarter or year are counted apart, as so much a month, and the quotes list, search and the client portal show that next to it. Quotes from before the update have only one-time lines, so their amounts don't change.
|
||||
- The quote's discount comes off its one-time lines. On a quote with only recurring lines it comes off the first recurring part (each month, then each quarter, then each year), and stays on the recurring invoice made from it.
|
||||
- Make Invoice only works on an accepted quote, and only once. The quote is then Invoiced. It used to invoice a quote in any status, as many times as it was pressed. When the client says yes by phone or email, mark the quote Accepted first.
|
||||
- The quote email says "Cost: $1,540.00 up front and $2,034.76 a month" when the quote has recurring lines. A quote with only one-time lines still says "Total Cost".
|
||||
- Quotes Pipeline values a quote at what it bills up front plus a year of its recurring lines, so a quote with monthly lines counts for more than its up-front amount in quoted, won, lost and open. The open quotes table shows that first-year value with the breakdown under it, and its CSV has Up front, Monthly and First year columns.
|
||||
- Notifications now follow access too. An agent only sees notifications for clients and areas they can open, so someone without sales access no longer sees invoice and quote notifications.
|
||||
- Drop-downs: Tom Select is gone, replaced by ITFlow's own picker. It works the way you are used to: type to search, tick several in a multi-select, and type a new value in fields that allow it, such as ticket watchers. See Developer Updates if you have custom pages that used Tom Select.
|
||||
- Several small windows are gone because the change is now made where the value is shown:
|
||||
@@ -191,7 +197,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Recurring invoices: your monthly recurring revenue (quarterly ones count as a third, yearly ones as a twelfth), what bills in the next 30 days, how many are running, and any that need a look. Filter by Active, Next 7 days, Late, Paused or All. Each row shows the amount, what it brings in a month, when it next bills, whether it is on autopay, and whether it bills per user or per device.
|
||||
- Pages: the invoice, quote or recurring invoice is shown the way the client sees it, with the next thing to do at the top:
|
||||
- Invoice: Send (or Mark Sent) a draft, Add Payment on one that is owed, with Charge Saved Card when the client has a card on file. On the side: what has been paid and what is left with a progress bar, the payments (edit, refund through Stripe or delete them there), the tickets billed on it, and its history as a timeline.
|
||||
- Quote: Send, then Accepted or Declined when the client answers by phone or email, then Make Invoice. A quote that ran out without an answer says so, with an Extend button. On the side: the files the client sent back, and its history.
|
||||
- Quote: Send, then Accepted or Declined when the client answers by phone or email, then Make Invoice (Start Billing when it has recurring lines). A quote that ran out without an answer says so, with an Extend button. On the side: the files the client sent back, and its history.
|
||||
- Recurring invoice: Invoice Now, Pause and Resume. On the side: the schedule with the next few dates, the period the next invoice covers and whether invoices go out by themselves, autopay, the invoices it has made and whether they were paid, and its history. A run missed in the last 7 days is billed by the next nightly run. One further behind says so, and offers Invoice Now for each missed run or moving on to the next date without billing them.
|
||||
- Lines are edited on the page:
|
||||
- Type a product's name or code to fill in its description, price and tax. What the line comes to shows as you type, and Enter adds it. The page comes back ready for the next line.
|
||||
@@ -221,6 +227,12 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- The ticket's Invoice window is redone. It shows the time logged by each agent and the hours that bills, with the client's rate (or the default rate) filled in, and it can add to one of the client's drafts. An open ticket can still be billed from here, with a warning that later time won't be.
|
||||
- Add Tickets on an invoice bills their time: tick the tickets, change their hours, and they go on as lines at the rate.
|
||||
- The client overview's Not yet invoiced shows what the unbilled time comes to and opens Unbilled Time. The dashboard and the Unbilled Work report link to it too.
|
||||
- **Quotes with monthly lines**, so one quote can carry the onboarding and the agreement that follows:
|
||||
- Each line is billed one time, or each month, quarter or year. Pick it under Billed on the add-a-line row, or in Edit Line.
|
||||
- The quote groups its lines under One-time, Each month, Each quarter and Each year, with a total for each, on the page, the PDF and the client's view. The top shows what it comes to up front and each month, quarter or year. A line drags within its own group.
|
||||
- Start Billing on an accepted quote: the one-time lines go on a draft invoice, dated as you choose and due on the client's terms. Each month, quarter or year part goes on a new recurring invoice from the date you pick (the first of next month to start), with its invoices emailed as they are made or left as drafts, or is added to one of the client's recurring invoices billed as often. Switch on Bill the first month on the invoice now to put the first period on the invoice too. A quote with only one-time lines keeps Make Invoice.
|
||||
- The quote shows what it was invoiced as, with a link to each invoice and recurring invoice, including any it added lines to. The invoice and the recurring invoices link back to the quote, and each history says what happened.
|
||||
- The quotes list adds the monthly part to each row and to its totals. Search, the client portal's quotes, the Send and Copy windows, the quote email and the client's Accept all show the amounts by part. The quotes export has a Monthly column, and the client export a monthly amount on quotes and how each quote line is billed.
|
||||
- **Tickets**
|
||||
- The ticket list has views for Open, Mine, Unassigned, Needs reply, Waiting, Overdue, Scheduled, Closed and All.
|
||||
- Each row shows whose move it is (yours or the client's) along with SLA and due badges, and status, priority and agent change right in the row.
|
||||
@@ -394,6 +406,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Billing: an invoice made from a quote was due counting from today instead of the invoice date, and with no terms when the client had none set.
|
||||
- Billing: the client's view of a quote offered Accept on a viewed quote that had run out, and the invoice and quote views put the browser's HTML into the history.
|
||||
- Billing: copying an invoice logged its old number with the prefix twice.
|
||||
- Billing: Make Invoice on a quote could make a second invoice when pressed twice or from two tabs, and could invoice a quote that was never accepted or had been declined. Its audit log entry named the quote with the current quote prefix instead of the quote's own.
|
||||
- Recurring invoices: if cron missed a night, that recurring invoice stopped billing until someone moved its date. The missed run is now billed the next night, up to 7 days late.
|
||||
- Invoicing a ticket: on an invoice that already had lines, its line went in near the top instead of at the end. A ticket already on an invoice could be billed again from a second window, and a ticket with 100 hours or more logged had no hours filled in.
|
||||
- Invoicing a ticket for a client with no hourly rate filled in nothing. It now takes the default hourly rate, as the Unbilled Work report already did.
|
||||
@@ -429,7 +442,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
|
||||
### Developer Updates
|
||||
|
||||
- Database 2.8.0 to 2.9.2, one file per step under `admin/database_updates/`, with `db.sql` matching 2.9.2:
|
||||
- Database 2.8.0 to 2.9.3, one file per step under `admin/database_updates/`, with `db.sql` matching 2.9.3:
|
||||
- 2.8.0: `racks.rack_units_descending`, `rack_power_capacity` and `rack_photo_rear`. On `rack_units`: `unit_face`, `unit_full_depth`, `unit_type`, `unit_power_watts`, `unit_notes` and a `unit_asset_id` key. Unlinked items move from asset id 0 to NULL.
|
||||
- 2.8.1: `location_documents` and `location_files`.
|
||||
- 2.8.2: `vendor_tags` (tag type 6), and `vendor_contact_phone_country_code`, `vendor_contact_phone` and `vendor_contact_email`.
|
||||
@@ -455,6 +468,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- `settings.config_share_max_hours` (720) and `config_share_require_protection` (0)
|
||||
- 2.9.1: `recurring_invoice_items.item_seat_source`, `item_seat_filter` (JSON) and `item_seat_minimum`, and `recurring_invoices.recurring_invoice_anchor_day`, backfilled from the day of each next date.
|
||||
- 2.9.2: `settings.config_time_increment` (15), `config_time_rounding` ('nearest'), `config_time_minimum` (0) and `config_time_item_name` ('Support [Hourly]').
|
||||
- 2.9.3: `quote_items.item_frequency` (NULL for one time, or `month`, `quarter` or `year`), `quotes.quote_monthly_amount`, `invoices.invoice_quote_id` and `recurring_invoices.recurring_invoice_quote_id`. Invoices are linked to their quote from the "Invoice created from quote QUO-n" history the old Make Invoice wrote, matched on the client too.
|
||||
- New shared function files, loaded by `functions.php`: `functions/asset.php`, `asset_interface.php`, `certificate.php`, `client_overview.php`, `contact.php`, `credential.php`, `dashboard.php`, `links.php`, `location.php`, `notifications.php`, `project.php`, `rack.php`, `search.php`, `service.php`, `software.php`, `tags.php`, `ticket.php`, `ticket_fields.php`, `user.php` and `vendor.php`. They hold the queries and rules, so the pages, the API and cron share one definition.
|
||||
- Removed post models: `agent/post/{asset_interface,certificate,credential,domain,event,location,network_ip,network,recurring_ticket,vendor}_model.php`, `admin/post/user_model.php` and `api/v1/certificates/certificate_model.php`. Their handlers call the shared functions above. Interface handlers moved to `agent/post/asset_interface.php`.
|
||||
- New scripts: `js/itflow_select.js`, `quick_pick.js`, `tag_picker.js`, `global_search.js`, `notifications.js`, `dashboard.js`, `client_overview.js`, `client_switcher.js`, `credentials.js` and `autosubmit.js`, and `agent/js/calendar.js`, `calendar_event_modal.js`, `files.js`, `interface_form.js`, `network_form.js`, `project_modal.js` and `ticket_schedule_modal.js`.
|
||||
@@ -556,6 +570,10 @@ If you are coming from 26.08 or earlier, the database update still has to be run
|
||||
- Billing time (`functions/billing.php`): `billingTimeRules()` reads the settings once a request (falling back to the old rules before 2.9.2 is applied, so they are not in `load_global_settings.php`), `billingTimeIncrements()`, `billingTimeRulesText()`, `billingTimeHours()`, `billingTimeRate()`, `billingTimeDescription()`. `billingUnbilledSql()` is the one definition of unbilled (billable, status Resolved or Closed, not archived, `ticket_invoice_id = 0`); `billingUnbilledTickets()`, `billingUnbilledByClient()` and `billingUnbilledCount()` read it, scoped to the agent's clients. The dashboard, client overview, sidebars and the Unbilled Work report use them.
|
||||
- `billingTimeInvoice($client_id, $hours, $opts, &$error)` is the one path that bills time - Unbilled Time, `add_invoice_from_ticket`, `add_ticket_to_invoice`. It validates first, makes the draft with `billingInvoiceCreate()` when needed, claims the tickets with a conditional `UPDATE` (deleting a new draft that ends up with none), adds the lines through `billingItemSave()`, and logs ticket history, invoice history and the audit log. `billingTimeWriteOff()` sets `ticket_billable = 0`. `invoiceUnbilledTickets()` is replaced by `billingUnbilledTickets()`, and `roundToNearest15Min()` is no longer used.
|
||||
- New page `agent/unbilled_time.php` with `js/unbilled_time.js`, and handlers `unbilled_invoice`, `unbilled_invoice_clients` and `unbilled_write_off` in `agent/post/billing.php`. `add_ticket_to_invoice` takes `ticket_ids[]`, `hours[id]`, `bill_time`, `item_name`, `price`, `tax_id` and `lines` (`each` or `one`). `add_invoice_from_ticket` keeps its field names. `remove_ticket_from_invoice` writes to the ticket's history.
|
||||
- Quotes (`functions/billing.php`): `quoteFrequencies()`, `quoteFrequency()` (anything else is one time), `quoteLineGroups($items, $discount)` (lines grouped by frequency with subtotal, tax, taxes by name, discount and total each; the discount sits on the one-time group, or the first recurring one when there is none), `quoteGroups($quote)`, `quoteHasRecurring()`, `quoteMonthly()` and `quoteAmountText()`. `quote_amount` is the one-time group's total and `quote_monthly_amount` the recurring ones as a month, both set by `billingRecalc()`. `billingTotals()` for a quote adds `groups`, `recurring` and `monthly`. `billing_item_save` takes `frequency` on quote lines, and a form without it leaves the line's frequency alone.
|
||||
- `quoteConvert($quote_id, $opts, &$error)` is the one path that invoices a quote, used by `add_quote_to_invoice`. `$opts`: `date`, `first_period`, `recurring[frequency]` with `to` (a recurring invoice id, or 0 for a new one), `start` and `auto_send`, and `by`. It checks every target first, claims the quote with a conditional `UPDATE` from Accepted to Invoiced, and makes everything in one transaction, rolled back and logged with `logApp()` on failure. Lines are copied with `billingCopyLines()`. It returns `invoice_id`, `recurring` (id => number) and `numbers`, or null with `$error`. `quoteMadeFrom()` lists what a quote became. A recurring invoice that only got lines is found through its history entry, which carries `history_quote_id` as well as `history_recurring_invoice_id`, so `billingHistory()` leaves such rows off the quote's history. `delete_quote` keeps those rows and clears `invoice_quote_id` and `recurring_invoice_quote_id`.
|
||||
- `agent/modals/quote/quote_to_invoice.php` posts those fields. Quote lines carry `data-group`, with `tr.itflow-paper-group` headings in `includes/billing_paper.php`, and the Sortable in `js/billing_items.js` drags only `tr[data-item-id]`, within its group when the table is grouped.
|
||||
- `reportCsv()` writes a totals row as given, without the column's `csv` function, which read fields a totals row doesn't have. The quotes export takes `quote_monthly_amount`.
|
||||
- Removed: `add_invoice_item`, `edit_invoice_item`, `delete_invoice_item`, `add_quote_item`, `edit_quote_item`, `delete_quote_item`, `add_recurring_invoice_item`, `edit_recurring_invoice_item`, `delete_recurring_invoice_item` and `link_invoice_to_ticket` (it wrote a column that does not exist), `update_invoice_items_order`, `update_quote_items_order` and `update_recurring_invoice_items_order` in `agent/ajax.php`, `agent/modals/invoice/invoice_item_edit.php`, `agent/modals/quote/quote_item_edit.php`, `agent/modals/recurring_invoice/recurring_invoice_item_edit.php`, `guest/guest_quote_upload_file_modal.php` and `js/product_autocomplete.js`.
|
||||
- Sidebar brand: `agent/css/sidebar_brand_fix.css` and `agent/js/sidebar_brand_fix.js` from 26.10 are replaced by CSS in `css/itflow_custom.css`. `includes/load_company_settings.php` now sets `$session_company_logo`.
|
||||
- `alertMessageHtml()` no longer double-encodes, and save messages built from `escapeSql()` values use `stripslashes()` at the flash.
|
||||
|
||||
27
admin/database_updates/2.9.3.php
Normal file
27
admin/database_updates/2.9.3.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 2.9.3 (from 2.9.2)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
// Quotes with monthly lines (functions/billing.php, Quotes). A quote line is
|
||||
// billed once (NULL) or each month, quarter or year - those go on a
|
||||
// recurring invoice when the quote is invoiced. quote_amount stays what is
|
||||
// billed once; quote_monthly_amount is what the recurring lines come to a
|
||||
// month (a quarterly line a third, a yearly one a twelfth).
|
||||
mysqli_query($mysqli, "ALTER TABLE `quote_items` ADD COLUMN `item_frequency` varchar(10) DEFAULT NULL AFTER `item_order`");
|
||||
mysqli_query($mysqli, "ALTER TABLE `quotes` ADD COLUMN `quote_monthly_amount` decimal(15,2) NOT NULL DEFAULT 0.00 AFTER `quote_amount`");
|
||||
|
||||
// What a quote became - the invoice and recurring invoices made from it
|
||||
mysqli_query($mysqli, "ALTER TABLE `invoices` ADD COLUMN `invoice_quote_id` int(11) NOT NULL DEFAULT 0 AFTER `invoice_recurring_invoice_id`");
|
||||
mysqli_query($mysqli, "ALTER TABLE `recurring_invoices` ADD COLUMN `recurring_invoice_quote_id` int(11) NOT NULL DEFAULT 0 AFTER `recurring_invoice_category_id`");
|
||||
|
||||
// Invoices made from a quote before now say so only in their history - link them
|
||||
mysqli_query($mysqli, "UPDATE `invoices`
|
||||
JOIN `history` ON `history_invoice_id` = `invoice_id`
|
||||
JOIN `quotes` ON `history_description` = CONCAT('Invoice created from quote ', `quote_prefix`, `quote_number`) AND `quote_client_id` = `invoice_client_id`
|
||||
SET `invoice_quote_id` = `quote_id`
|
||||
WHERE `invoice_quote_id` = 0");
|
||||
@@ -65,6 +65,10 @@ $csrf = $_SESSION['csrf_token'];
|
||||
$recurring = intval($invoice['invoice_recurring_invoice_id'])
|
||||
? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT recurring_invoice_id, recurring_invoice_prefix, recurring_invoice_number FROM recurring_invoices WHERE recurring_invoice_id = " . intval($invoice['invoice_recurring_invoice_id'])))
|
||||
: null;
|
||||
// Made from a quote?
|
||||
$from_quote = intval($invoice['invoice_quote_id'] ?? 0)
|
||||
? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT quote_id, quote_prefix, quote_number FROM quotes WHERE quote_id = " . intval($invoice['invoice_quote_id'])))
|
||||
: null;
|
||||
|
||||
$progress = $state['paid'] > 0 && $totals['total'] > 0 ? min(100, round($state['paid'] / $totals['total'] * 100)) : 0;
|
||||
|
||||
@@ -112,6 +116,9 @@ $paper = [
|
||||
<?php if ($recurring) { ?>
|
||||
<span><i class="fas fa-fw fa-redo-alt me-1"></i>From <a class="text-reset" href="recurring_invoice.php?recurring_invoice_id=<?= intval($recurring['recurring_invoice_id']) ?>"><?= escapeHtml($recurring['recurring_invoice_prefix'] . $recurring['recurring_invoice_number']) ?></a></span>
|
||||
<?php } ?>
|
||||
<?php if ($from_quote) { ?>
|
||||
<span><i class="fas fa-fw fa-comment-dollar me-1"></i>From <a class="text-reset" href="quote.php?quote_id=<?= intval($from_quote['quote_id']) ?>"><?= escapeHtml($from_quote['quote_prefix'] . $from_quote['quote_number']) ?></a></span>
|
||||
<?php } ?>
|
||||
<span title="<?= escapeHtml($invoice['invoice_created_at']) ?>"><i class="far fa-fw fa-clock me-1"></i>Created <?= escapeHtml(timeAgo($invoice['invoice_created_at'])) ?></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -81,6 +81,21 @@ ob_start();
|
||||
<textarea class="form-control" name="description" rows="3" placeholder="<?= $type === 'recurring' ? 'What it covers - {month}, {quarter}, {year} or {period} become the period each invoice covers' : 'What it is, the period it covers, serial numbers - shown under the item' ?>" data-line-description><?= escapeHtml($item['item_description']) ?></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<?php if ($type === 'quote') {
|
||||
$line_frequency = quoteFrequency($item['item_frequency'] ?? ''); ?>
|
||||
<div class="col-12">
|
||||
<label class="form-label">Billed</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-redo-alt"></i></span>
|
||||
<select class="form-select" name="frequency">
|
||||
<?php foreach (quoteFrequencies() as $frequency_value => $frequency_label) { ?>
|
||||
<option value="<?= $frequency_value ?>" <?= $line_frequency === $frequency_value ? 'selected' : '' ?>><?= $frequency_label ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-text">A line billed each month, quarter or year goes on a recurring invoice when the quote is invoiced, and the quote shows it apart from what is billed once.</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<?php if ($type === 'recurring') {
|
||||
$seat_options = recurringSeatOptions($client_id);
|
||||
$seat_source = (string) ($item['item_seat_source'] ?? '');
|
||||
|
||||
@@ -37,7 +37,7 @@ ob_start();
|
||||
<input type="hidden" name="quote_id" value="<?= intval($doc['quote_id']) ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<p class="text-body-secondary small">A new draft with the same <?= $lines ?> line<?= $lines === 1 ? '' : 's' ?>, scope, discount and notes - <?= escapeHtml(billingMoney($doc['quote_amount'], $doc['quote_currency_code'])) ?>. Quote the same work for another client by picking them here.</p>
|
||||
<p class="text-body-secondary small">A new draft with the same <?= $lines ?> line<?= $lines === 1 ? '' : 's' ?>, scope, discount and notes - <?= escapeHtml(quoteAmountText(quoteGroups($doc), $doc['quote_currency_code'])) ?>. Quote the same work for another client by picking them here.</p>
|
||||
<div class="row g-3">
|
||||
<div class="col-12">
|
||||
<label class="form-label">For <strong class="text-danger">*</strong></label>
|
||||
|
||||
@@ -86,7 +86,7 @@ ob_start();
|
||||
<div class="small text-body-secondary">An email with a link to view it, accept or decline it, and download it as a PDF.</div>
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<div class="fw-semibold itflow-money"><?= escapeHtml(billingMoney($doc['quote_amount'], $doc['quote_currency_code'])) ?></div>
|
||||
<div class="fw-semibold itflow-money"><?= escapeHtml(quoteAmountText(quoteGroups($doc), $doc['quote_currency_code'])) ?></div>
|
||||
<div class="small text-body-secondary">valid until <?= escapeHtml($doc['quote_expire']) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,56 +1,178 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Invoice an accepted quote - a draft invoice with the quote's lines,
|
||||
* scope, category, discount and notes, dated as given and due on the
|
||||
* client's terms. The quote is marked invoiced. ?quote_id=
|
||||
* Invoice an accepted quote. Its one-time lines go on a draft invoice -
|
||||
* dated as given, due on the client's terms - and its lines billed each
|
||||
* month, quarter or year on a recurring invoice for each: a new one from a
|
||||
* start date, or added to one of the client's. The first period can go on
|
||||
* the invoice too. The quote is marked invoiced. ?quote_id=
|
||||
*/
|
||||
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
enforceUserPermission('module_sales', 2);
|
||||
|
||||
$doc_type = 'quote';
|
||||
$doc = billingRow($doc_type, intval($_GET['quote_id'] ?? 0));
|
||||
$doc = billingRow('quote', intval($_GET['quote_id'] ?? 0));
|
||||
if (!$doc) {
|
||||
exit(json_encode(['error' => 'That quote no longer exists.']));
|
||||
}
|
||||
$client_id = intval($doc['client_id']);
|
||||
enforceClientAccess();
|
||||
$doc_number = billingNumber($doc_type, $doc);
|
||||
$lines = count(billingItems('quote', intval($doc['quote_id'])));
|
||||
$terms = intval($doc['client_net_terms']) ?: intval($config_default_net_terms);
|
||||
$doc_number = billingNumber('quote', $doc);
|
||||
if ($doc['quote_status'] !== 'Accepted') {
|
||||
exit(json_encode(['error' => $doc['quote_status'] === 'Invoiced' ? "$doc_number is already invoiced." : "$doc_number has to be accepted before it is invoiced."]));
|
||||
}
|
||||
|
||||
$quote_id = intval($doc['quote_id']);
|
||||
$currency = (string) $doc['quote_currency_code'];
|
||||
$money = function ($amount) use ($currency) {
|
||||
return escapeHtml(billingMoney($amount, $currency));
|
||||
};
|
||||
$groups = quoteGroups($doc);
|
||||
$recurring = quoteHasRecurring($groups);
|
||||
$one_time = $groups[''] ?? null;
|
||||
$terms = intval($doc['client_net_terms']);
|
||||
$frequencies = recurringInvoiceFrequencies();
|
||||
|
||||
// The client's recurring invoices each part could go on
|
||||
$existing = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT recurring_invoice_id, recurring_invoice_prefix, recurring_invoice_number, recurring_invoice_scope, recurring_invoice_frequency, recurring_invoice_amount
|
||||
FROM recurring_invoices WHERE recurring_invoice_client_id = $client_id AND recurring_invoice_archived_at IS NULL ORDER BY recurring_invoice_number DESC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$existing[$row['recurring_invoice_frequency']][intval($row['recurring_invoice_id'])] = $row;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-file-invoice-dollar me-2"></i>Make Invoice <span class="fw-normal text-white-50 ms-1"><?= escapeHtml($doc_number) ?> · <?= escapeHtml($doc['client_name']) ?></span></h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-file-invoice-dollar me-2"></i><?= $recurring ? 'Start Billing' : 'Make Invoice' ?> <span class="fw-normal text-white-50 ms-1"><?= escapeHtml($doc_number) ?> · <?= escapeHtml($doc['client_name']) ?></span></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<form action="post.php" method="post" autocomplete="off" id="quoteToInvoice">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="quote_id" value="<?= intval($doc['quote_id']) ?>">
|
||||
<input type="hidden" name="quote_id" value="<?= $quote_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<div class="d-flex align-items-center gap-3 mb-3 p-3 rounded bg-body-tertiary">
|
||||
<?= billingTile('quote') ?>
|
||||
<i class="fas fa-arrow-right text-body-secondary"></i>
|
||||
<?= billingTile('invoice') ?>
|
||||
<div class="small text-body-secondary">A draft invoice with the quote's <?= $lines ?> line<?= $lines === 1 ? '' : 's' ?>, discount and notes - <?= escapeHtml(billingMoney($doc['quote_amount'], $doc['quote_currency_code'])) ?>. <?= escapeHtml($doc_number) ?> is marked invoiced.</div>
|
||||
<?php if ($one_time && ($one_time['items'] || !$recurring)) { ?><?= billingTile('invoice') ?><?php } ?>
|
||||
<?php if ($recurring) { ?><?= billingTile('recurring') ?><?php } ?>
|
||||
<div class="small text-body-secondary">
|
||||
<?= escapeHtml($doc_number) ?> comes to <?= escapeHtml(quoteAmountText($groups, $currency)) ?>.
|
||||
<?= $recurring ? 'What is billed once goes on a draft invoice, the rest on recurring invoices.' : "A draft invoice with its lines, discount and notes." ?>
|
||||
It is marked invoiced.
|
||||
</div>
|
||||
</div>
|
||||
<label class="form-label">Invoice date <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
|
||||
<input type="date" class="form-control" name="date" max="2999-12-31" value="<?= date('Y-m-d') ?>" required>
|
||||
|
||||
<!-- What is billed once -->
|
||||
<div data-quote-invoice <?= $one_time && $one_time['items'] || !$recurring ? '' : 'hidden' ?>>
|
||||
<h6 class="fw-semibold mb-2">
|
||||
<i class="fas fa-fw fa-file-invoice text-secondary me-1"></i>Invoice
|
||||
<span class="fw-normal text-body-secondary small ms-1">
|
||||
<?= $one_time && $one_time['items'] ? count($one_time['items']) . ' one-time line' . (count($one_time['items']) === 1 ? '' : 's') . ' - ' . $money($one_time['total']) : ($recurring ? 'The first period' : '') ?>
|
||||
</span>
|
||||
</h6>
|
||||
<label class="form-label">Invoice date <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
|
||||
<input type="date" class="form-control" name="date" max="2999-12-31" value="<?= date('Y-m-d') ?>" required>
|
||||
</div>
|
||||
<div class="form-text"><?= $terms ? "Due $terms days after it" : 'Due on receipt' ?> - the client's terms.</div>
|
||||
</div>
|
||||
<div class="form-text">Due <?= $terms ?> days after it - the client's terms.</div>
|
||||
|
||||
<?php foreach ($groups as $frequency => $group) {
|
||||
if ($frequency === '') {
|
||||
continue;
|
||||
}
|
||||
$period = recurringInvoicePeriodWord($frequency);
|
||||
$options = $existing[$frequency] ?? []; ?>
|
||||
<!-- Billed each <?= $period ?> -->
|
||||
<div class="border-top pt-3 mt-3" data-quote-recurring="<?= $frequency ?>">
|
||||
<h6 class="fw-semibold mb-2">
|
||||
<i class="fas fa-fw fa-redo-alt text-secondary me-1"></i>Each <?= $period ?>
|
||||
<span class="fw-normal text-body-secondary small ms-1"><?= count($group['items']) ?> line<?= count($group['items']) === 1 ? '' : 's' ?> - <?= $money($group['total']) ?> a <?= $period ?><?= $group['discount'] != 0 ? ', after the discount' : '' ?></span>
|
||||
</h6>
|
||||
<div class="row g-3">
|
||||
<div class="col-12">
|
||||
<label class="form-label">Bill it on</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-redo-alt"></i></span>
|
||||
<select class="form-select" name="recurring[<?= $frequency ?>][to]" data-quote-recurring-to>
|
||||
<option value="0">A new recurring invoice, billed <?= strtolower($frequencies[$frequency]) ?></option>
|
||||
<?php foreach ($options as $recurring_id => $option) { ?>
|
||||
<option value="<?= $recurring_id ?>">Add to <?= escapeHtml(billingNumber('recurring', $option)) ?><?= trim((string) $option['recurring_invoice_scope']) !== '' ? ' - ' . escapeHtml($option['recurring_invoice_scope']) : '' ?> (<?= $money($option['recurring_invoice_amount']) ?> a <?= $period ?>)</option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6" data-quote-recurring-new>
|
||||
<label class="form-label">First invoice <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-calendar-day"></i></span>
|
||||
<input type="date" class="form-control" name="recurring[<?= $frequency ?>][start]" max="2999-12-31" value="<?= quoteRecurringStart() ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6" data-quote-recurring-new>
|
||||
<label class="form-label">Its invoices</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-paper-plane"></i></span>
|
||||
<select class="form-select" name="recurring[<?= $frequency ?>][auto_send]">
|
||||
<option value="1">Emailed as they are made</option>
|
||||
<option value="0">Left as drafts to check</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($recurring) { ?>
|
||||
<div class="form-check form-switch mt-3 pt-3 border-top">
|
||||
<input class="form-check-input" type="checkbox" role="switch" name="first_period" value="1" id="quoteFirstPeriod" data-quote-first>
|
||||
<label class="form-check-label" for="quoteFirstPeriod">Bill the first <?= escapeHtml(implode(' and ', array_map('recurringInvoicePeriodWord', array_keys(array_diff_key($groups, ['' => true]))))) ?> on the invoice now</label>
|
||||
<div class="form-text">The recurring lines go on the invoice as well. Start the recurring invoice a period later, so the first one isn't billed twice.</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="add_quote_to_invoice" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Make Invoice</button>
|
||||
<button type="submit" name="add_quote_to_invoice" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i><?= $recurring ? 'Start Billing' : 'Make Invoice' ?></button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
var form = document.getElementById('quoteToInvoice');
|
||||
if (!form || form.dataset.wired) {
|
||||
return;
|
||||
}
|
||||
form.dataset.wired = '1';
|
||||
var invoice = form.querySelector('[data-quote-invoice]');
|
||||
var first = form.querySelector('[data-quote-first]');
|
||||
var hasOneTime = <?= $one_time && $one_time['items'] ? 'true' : 'false' ?>;
|
||||
var hasRecurring = <?= $recurring ? 'true' : 'false' ?>;
|
||||
function show(section, on) {
|
||||
section.hidden = !on;
|
||||
section.querySelectorAll('input, select').forEach(function (input) {
|
||||
input.disabled = !on;
|
||||
});
|
||||
}
|
||||
function update() {
|
||||
// The invoice is made for what is billed once, or for the first period
|
||||
show(invoice, hasOneTime || !hasRecurring || (first && first.checked));
|
||||
form.querySelectorAll('[data-quote-recurring]').forEach(function (part) {
|
||||
var to = part.querySelector('[data-quote-recurring-to]');
|
||||
part.querySelectorAll('[data-quote-recurring-new]').forEach(function (section) {
|
||||
show(section, to.value === '0');
|
||||
});
|
||||
});
|
||||
}
|
||||
form.addEventListener('change', update);
|
||||
update();
|
||||
})();
|
||||
</script>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
|
||||
@@ -101,7 +101,7 @@ if (isset($_POST['add_quote_copy'])) {
|
||||
mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Draft', history_description = 'Quote copied!', history_quote_id = $new_quote_id");
|
||||
|
||||
$sql_items = mysqli_query($mysqli,"SELECT item_description, item_id, item_name, item_order, item_price, item_quantity, item_subtotal,
|
||||
item_tax, item_tax_id, item_total FROM quote_items WHERE item_quote_id = $quote_id");
|
||||
item_tax, item_tax_id, item_total, item_frequency FROM quote_items WHERE item_quote_id = $quote_id");
|
||||
while($row = mysqli_fetch_assoc($sql_items)) {
|
||||
$item_id = intval($row['item_id']);
|
||||
$item_name = escapeSql($row['item_name']);
|
||||
@@ -113,9 +113,12 @@ if (isset($_POST['add_quote_copy'])) {
|
||||
$item_total = floatval($row['item_total']);
|
||||
$item_order = intval($row['item_order']);
|
||||
$tax_id = intval($row['item_tax_id']);
|
||||
$item_frequency = quoteFrequency($row['item_frequency']);
|
||||
$item_frequency = $item_frequency === '' ? 'NULL' : "'$item_frequency'";
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO quote_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $item_quantity, item_price = $item_price, item_subtotal = $item_subtotal, item_tax = $item_tax, item_total = $item_total, item_order = $item_order, item_tax_id = $tax_id, item_quote_id = $new_quote_id");
|
||||
mysqli_query($mysqli,"INSERT INTO quote_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $item_quantity, item_price = $item_price, item_subtotal = $item_subtotal, item_tax = $item_tax, item_total = $item_total, item_order = $item_order, item_tax_id = $tax_id, item_frequency = $item_frequency, item_quote_id = $new_quote_id");
|
||||
}
|
||||
billingRecalc('quote', $new_quote_id);
|
||||
|
||||
logAudit("Quote", "Create", "$session_name created quote $config_quote_prefix$quote_number from quote $original_quote_prefix$original_quote_number", $client_id, $new_quote_id);
|
||||
|
||||
@@ -127,98 +130,52 @@ if (isset($_POST['add_quote_copy'])) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Make Invoice / Start Billing - an accepted quote's one-time lines on a
|
||||
* draft invoice, and its monthly, quarterly and yearly lines on recurring
|
||||
* invoices (new, or added to the client's). quoteConvert() does it.
|
||||
*/
|
||||
if (isset($_POST['add_quote_to_invoice'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_sales', 2);
|
||||
|
||||
$quote_id = intval($_POST['quote_id']);
|
||||
$date = escapeSql($_POST['date']);
|
||||
|
||||
$sql = mysqli_query($mysqli,"SELECT client_net_terms, quote_amount, quote_category_id, quote_client_id, quote_currency_code,
|
||||
quote_discount_amount, quote_note, quote_number, quote_prefix, quote_scope FROM clients, quotes WHERE client_id = quote_client_id AND quote_id = $quote_id");
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$client_net_terms = intval($row['client_net_terms']) ?: intval($config_default_net_terms);
|
||||
$quote_prefix = escapeSql($row['quote_prefix']);
|
||||
$quote_number = escapeSql($row['quote_number']);
|
||||
$quote_discount_amount = floatval($row['quote_discount_amount']);
|
||||
$quote_amount = floatval($row['quote_amount']);
|
||||
$quote_currency_code = escapeSql($row['quote_currency_code']);
|
||||
$quote_scope = escapeSql($row['quote_scope']);
|
||||
$quote_note = escapeSql($row['quote_note']);
|
||||
|
||||
$client_id = intval($row['quote_client_id']);
|
||||
$category_id = intval($row['quote_category_id']);
|
||||
$quote = billingRow('quote', intval($_POST['quote_id'] ?? 0));
|
||||
if (!$quote) {
|
||||
flashAlert('That quote no longer exists.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$client_id = intval($quote['client_id']);
|
||||
|
||||
enforceClientAccess();
|
||||
|
||||
$config_invoice_prefix = escapeSql($config_invoice_prefix);
|
||||
|
||||
// Atomically increment and get the new invoice number
|
||||
mysqli_query($mysqli, "
|
||||
UPDATE settings
|
||||
SET
|
||||
config_invoice_next_number = LAST_INSERT_ID(config_invoice_next_number),
|
||||
config_invoice_next_number = config_invoice_next_number + 1
|
||||
WHERE company_id = 1
|
||||
");
|
||||
|
||||
$invoice_number = mysqli_insert_id($mysqli);
|
||||
|
||||
//Generate a unique URL key for clients to access
|
||||
$url_key = randomString(32);
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO invoices SET invoice_prefix = '$config_invoice_prefix', invoice_number = $invoice_number, invoice_scope = '$quote_scope', invoice_date = '$date', invoice_due = DATE_ADD('$date', INTERVAL $client_net_terms day), invoice_category_id = $category_id, invoice_status = 'Draft', invoice_discount_amount = $quote_discount_amount, invoice_amount = $quote_amount, invoice_currency_code = '$quote_currency_code', invoice_note = '$quote_note', invoice_url_key = '$url_key', invoice_client_id = $client_id");
|
||||
|
||||
$new_invoice_id = mysqli_insert_id($mysqli);
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Draft', history_description = 'Invoice created from quote $quote_prefix$quote_number', history_invoice_id = $new_invoice_id");
|
||||
|
||||
$sql_items = mysqli_query($mysqli,"SELECT item_description, item_id, item_name, item_order, item_price, item_quantity, item_subtotal,
|
||||
item_tax, item_tax_id, item_total FROM quote_items WHERE item_quote_id = $quote_id");
|
||||
while($row = mysqli_fetch_assoc($sql_items)) {
|
||||
$item_id = intval($row['item_id']);
|
||||
$item_name = escapeSql($row['item_name']);
|
||||
$item_description = escapeSql($row['item_description']);
|
||||
$item_quantity = floatval($row['item_quantity']);
|
||||
$item_price = floatval($row['item_price']);
|
||||
$item_subtotal = floatval($row['item_subtotal']);
|
||||
$item_tax = floatval($row['item_tax']);
|
||||
$item_total = floatval($row['item_total']);
|
||||
$item_order = intval($row['item_order']);
|
||||
$tax_id = intval($row['item_tax_id']);
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO invoice_items SET item_name = '$item_name', item_description = '$item_description', item_quantity = $item_quantity, item_price = $item_price, item_subtotal = $item_subtotal, item_tax = $item_tax, item_total = $item_total, item_order = $item_order, item_tax_id = $tax_id, item_invoice_id = $new_invoice_id");
|
||||
$recurring = [];
|
||||
foreach ((array) ($_POST['recurring'] ?? []) as $frequency => $choice) {
|
||||
$choice = (array) $choice;
|
||||
$recurring[(string) $frequency] = [
|
||||
'to' => intval($choice['to'] ?? 0),
|
||||
'start' => (string) ($choice['start'] ?? ''),
|
||||
'auto_send' => intval($choice['auto_send'] ?? 1),
|
||||
];
|
||||
}
|
||||
$result = quoteConvert($quote['quote_id'], [
|
||||
'date' => (string) ($_POST['date'] ?? ''),
|
||||
'first_period' => !empty($_POST['first_period']),
|
||||
'recurring' => $recurring,
|
||||
'by' => $session_name,
|
||||
], $error);
|
||||
if (!$result) {
|
||||
flashAlert(escapeHtml($error), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli,"UPDATE quotes SET quote_status = 'Invoiced' WHERE quote_id = $quote_id");
|
||||
flashAlert('<strong>' . escapeHtml(billingNumber('quote', $quote)) . '</strong> invoiced as ' . escapeHtml(implode(', ', $result['numbers'])));
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO history SET history_status = 'Invoiced', history_description = 'Quote invoiced as $config_invoice_prefix$invoice_number', history_quote_id = $quote_id");
|
||||
|
||||
logAudit("Invoice", "Create", "$session_name created invoice $config_invoice_prefix$invoice_number from quote $config_quote_prefix$quote_number", $client_id, $new_invoice_id);
|
||||
|
||||
// Check & update any quote-ticket association
|
||||
$ticket_id = 0;
|
||||
$sql_ticket = "SELECT ticket_id, ticket_prefix, ticket_number
|
||||
FROM tickets
|
||||
WHERE ticket_quote_id = $quote_id
|
||||
LIMIT 1";
|
||||
$result_ticket = mysqli_query($mysqli, $sql_ticket);
|
||||
|
||||
if ($result_ticket && $row = mysqli_fetch_assoc($result_ticket)) {
|
||||
$ticket_id = intval($row['ticket_id']);
|
||||
$ticket_prefix = escapeSql($row['ticket_prefix']);
|
||||
$ticket_number = intval($row['ticket_number']);
|
||||
|
||||
mysqli_query($mysqli, "UPDATE tickets SET ticket_invoice_id = $new_invoice_id WHERE ticket_id = $ticket_id AND ticket_invoice_id = '0'"); // Only if ticket doesn't already have an invoice
|
||||
if ($result['invoice_id']) {
|
||||
redirect("invoice.php?invoice_id={$result['invoice_id']}");
|
||||
}
|
||||
|
||||
triggerCustomAction('invoice_create', $new_invoice_id);
|
||||
|
||||
flashAlert("Invoice created from quote <strong>$quote_prefix$quote_number</strong>");
|
||||
|
||||
redirect("invoice.php?invoice_id=$new_invoice_id");
|
||||
redirect('recurring_invoice.php?recurring_invoice_id=' . array_key_first($result['recurring']));
|
||||
|
||||
}
|
||||
|
||||
@@ -279,12 +236,13 @@ if (isset($_GET['delete_quote'])) {
|
||||
mysqli_query($mysqli,"DELETE FROM quote_items WHERE item_id = $item_id");
|
||||
}
|
||||
|
||||
//Delete History Associated with the Quote
|
||||
$sql = mysqli_query($mysqli,"SELECT history_id FROM history WHERE history_quote_id = $quote_id");
|
||||
while($row = mysqli_fetch_assoc($sql)) {;
|
||||
$history_id = intval($row['history_id']);
|
||||
mysqli_query($mysqli,"DELETE FROM history WHERE history_id = $history_id");
|
||||
}
|
||||
//Delete History Associated with the Quote - a recurring invoice's entry for the lines it added stays theirs
|
||||
mysqli_query($mysqli,"DELETE FROM history WHERE history_quote_id = $quote_id AND history_recurring_invoice_id = 0");
|
||||
mysqli_query($mysqli,"UPDATE history SET history_quote_id = 0 WHERE history_quote_id = $quote_id");
|
||||
|
||||
// What was made from it no longer points at it
|
||||
mysqli_query($mysqli,"UPDATE invoices SET invoice_quote_id = 0 WHERE invoice_quote_id = $quote_id");
|
||||
mysqli_query($mysqli,"UPDATE recurring_invoices SET recurring_invoice_quote_id = 0 WHERE recurring_invoice_quote_id = $quote_id");
|
||||
|
||||
logAudit("Quote", "Delete", "$session_name deleted quote $quote_prefix$quote_number", $client_id);
|
||||
|
||||
@@ -432,6 +390,11 @@ if (isset($_POST['email_quote'])) {
|
||||
|
||||
enforceClientAccess();
|
||||
|
||||
// What it comes to - up front, and each month, quarter or year
|
||||
$quote_groups = quoteGroups(billingRow('quote', $quote_id, false));
|
||||
$quote_groups_recurring = quoteHasRecurring($quote_groups);
|
||||
$quote_cost_text = quoteAmountText($quote_groups, $row['quote_currency_code'] ?? $quote_currency_code);
|
||||
|
||||
// Two ways in. Quick Send skips the modal and resolves the default
|
||||
// recipients server-side; the modal posts an explicit contacts[] list.
|
||||
// Scoping either lookup to this quote's client is what makes a tampered
|
||||
@@ -508,7 +471,7 @@ if (isset($_POST['email_quote'])) {
|
||||
$contact_name = escapeSql($recipient['contact_name']);
|
||||
$contact_email = escapeSql($recipient['contact_email']);
|
||||
|
||||
$body = "Hello $contact_name,<br><br>Thank you for your inquiry, we are pleased to provide you with the following estimate.<br><br><br>$quote_scope<br>Total Cost: " . numfmt_format_currency($currency_format, $quote_amount, $quote_currency_code) . "<br><br><br>View and accept your estimate online <a href=\'https://$config_base_url/guest/guest_view_quote.php?quote_id=$quote_id&url_key=$quote_url_key\'>here</a><br><br><br>--<br>$company_name - Sales<br>$config_quote_from_email<br>$company_phone";
|
||||
$body = "Hello $contact_name,<br><br>Thank you for your inquiry, we are pleased to provide you with the following estimate.<br><br><br>$quote_scope<br>" . ($quote_groups_recurring ? 'Cost: ' : 'Total Cost: ') . escapeSql(htmlspecialchars($quote_cost_text)) . "<br><br><br>View and accept your estimate online <a href=\'https://$config_base_url/guest/guest_view_quote.php?quote_id=$quote_id&url_key=$quote_url_key\'>here</a><br><br><br>--<br>$company_name - Sales<br>$config_quote_from_email<br>$company_phone";
|
||||
|
||||
$data[] = [
|
||||
'from' => $config_quote_from_email,
|
||||
@@ -613,7 +576,7 @@ if (isExportRequest('export_quotes')) {
|
||||
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT client_name, quote_amount, quote_date, quote_number, quote_prefix, quote_scope,
|
||||
"SELECT client_name, quote_amount, quote_monthly_amount, quote_date, quote_number, quote_prefix, quote_scope,
|
||||
quote_status FROM quotes
|
||||
LEFT JOIN clients ON quote_client_id = client_id
|
||||
LEFT JOIN categories ON quote_category_id = category_id
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
* to do: send a draft, record the client's answer, turn an accepted quote
|
||||
* into an invoice. The quote itself is on the left as the client sees it,
|
||||
* its lines edited in place until it is answered. On the right: what it
|
||||
* comes to, the ticket it was raised from, files the client sent back with
|
||||
* their answer, and its history.
|
||||
* comes to - what is billed once and what each month, quarter or year, its
|
||||
* recurring lines kept apart - the ticket it was raised from, what it was
|
||||
* invoiced as, files the client sent back with their answer, and its history.
|
||||
*/
|
||||
|
||||
// If client_id is in URI then show client Side Bar and client header
|
||||
@@ -45,6 +46,7 @@ $money = function ($amount) use ($currency) {
|
||||
|
||||
$items = billingItems('quote', $quote_id);
|
||||
$totals = billingTotals('quote', $quote, $items);
|
||||
$made = in_array($status, ['Invoiced', 'Accepted'], true) ? quoteMadeFrom($quote_id) : [];
|
||||
$history = billingHistory('quote', $quote_id);
|
||||
$expired = quoteExpired($quote);
|
||||
$awaiting = in_array($status, ['Sent', 'Viewed'], true);
|
||||
@@ -150,7 +152,7 @@ $paper = [
|
||||
<?php if ($can_edit && $status === 'Accepted') { ?>
|
||||
<div class="btn-group">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/quote/quote_to_invoice.php?quote_id=<?= $quote_id ?>">
|
||||
<i class="fas fa-file-invoice-dollar me-2"></i>Make Invoice
|
||||
<i class="fas fa-file-invoice-dollar me-2"></i><?= $totals['recurring'] ? 'Start Billing' : 'Make Invoice' ?>
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-label="More"></button>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
@@ -232,8 +234,22 @@ $paper = [
|
||||
<h3 class="card-title mb-0"><i class="fas fa-fw fa-comment-dollar text-secondary me-2"></i>Quote</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="itflow-billing-stat-label">Total</div>
|
||||
<div class="itflow-billing-stat-value mb-3"><?= $money($totals['total']) ?></div>
|
||||
<?php if ($totals['recurring']) { ?>
|
||||
<div class="d-flex flex-wrap gap-4 mb-3">
|
||||
<?php foreach ($totals['groups'] as $frequency => $group) {
|
||||
if ($frequency === '' && !$group['items'] && $group['discount'] == 0) {
|
||||
continue;
|
||||
} ?>
|
||||
<div>
|
||||
<div class="itflow-billing-stat-label"><?= $frequency === '' ? 'Up front' : escapeHtml($group['label']) ?></div>
|
||||
<div class="itflow-billing-stat-value"><?= $money($group['total']) ?></div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } else { ?>
|
||||
<div class="itflow-billing-stat-label">Total</div>
|
||||
<div class="itflow-billing-stat-value mb-3"><?= $money($totals['total']) ?></div>
|
||||
<?php } ?>
|
||||
<dl class="itflow-asset-dl mb-0">
|
||||
<dt>Status</dt><dd><?= quoteStatusBadge($quote) ?></dd>
|
||||
<dt>Dated</dt><dd><?= escapeHtml($quote['quote_date']) ?></dd>
|
||||
@@ -251,6 +267,29 @@ $paper = [
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if ($made) { ?>
|
||||
<!-- What it was invoiced as -->
|
||||
<div class="card mb-3">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title mb-0"><i class="fas fa-fw fa-file-invoice-dollar text-secondary me-2"></i>Invoiced as</h3>
|
||||
</div>
|
||||
<div class="list-group list-group-flush">
|
||||
<?php foreach ($made as $made_doc) {
|
||||
$made_type = $made_doc['type'];
|
||||
$made_url = $made_type === 'invoice' ? 'invoice.php?invoice_id=' . intval($made_doc['id']) : 'recurring_invoice.php?recurring_invoice_id=' . intval($made_doc['id']); ?>
|
||||
<a class="list-group-item list-group-item-action d-flex align-items-center gap-2" href="<?= $made_url ?>">
|
||||
<?= billingTile($made_type, 'sm') ?>
|
||||
<div class="flex-grow-1 min-w-0">
|
||||
<div class="fw-semibold"><?= escapeHtml($made_doc['prefix'] . $made_doc['number']) ?></div>
|
||||
<div class="small text-body-secondary"><?= $made_type === 'invoice' ? 'Invoice' : 'Recurring, ' . strtolower(recurringInvoiceFrequencies()[$made_doc['frequency']] ?? 'monthly') . ($made_doc['added'] ? ', lines added' : '') ?> · <?= escapeHtml($made_doc['status']) ?></div>
|
||||
</div>
|
||||
<div class="text-end text-nowrap fw-semibold"><?= escapeHtml(billingMoney($made_doc['amount'], $made_doc['currency'])) ?></div>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($files) { ?>
|
||||
<div class="card mb-3">
|
||||
<div class="card-header">
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
* been accepted and not invoiced yet, and what was won over the last 90
|
||||
* days with the share of answers that were a yes. The pills filter by where
|
||||
* a quote stands - Expired picks out the ones that ran out unanswered. Each
|
||||
* row has how long a quote has left while it waits.
|
||||
* row has how long a quote has left while it waits. A quote's recurring
|
||||
* lines count apart, as so much a month (quote_monthly_amount), under what
|
||||
* it bills up front.
|
||||
*/
|
||||
|
||||
// Default Column Sortby/Order Filter
|
||||
@@ -89,6 +91,9 @@ foreach (array_keys($statuses) as $i => $key) {
|
||||
// The pipeline along the top - everything in this scope
|
||||
$pipeline = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT
|
||||
COALESCE(SUM(CASE WHEN {$statuses['Awaiting']['where']} THEN quote_amount END), 0) AS awaiting,
|
||||
COALESCE(SUM(CASE WHEN {$statuses['Awaiting']['where']} THEN quote_monthly_amount END), 0) AS awaiting_monthly,
|
||||
COALESCE(SUM(CASE WHEN quote_status = 'Accepted' THEN quote_monthly_amount END), 0) AS accepted_monthly,
|
||||
COALESCE(SUM(CASE WHEN quote_status IN ('Accepted', 'Invoiced') AND quote_date >= CURDATE() - INTERVAL 90 DAY THEN quote_monthly_amount END), 0) AS won_monthly,
|
||||
COALESCE(SUM(CASE WHEN {$statuses['Awaiting']['where']} THEN 1 END), 0) AS awaiting_count,
|
||||
COALESCE(SUM(CASE WHEN quote_status = 'Accepted' THEN quote_amount END), 0) AS accepted,
|
||||
COALESCE(SUM(CASE WHEN quote_status = 'Accepted' THEN 1 END), 0) AS accepted_count,
|
||||
@@ -107,6 +112,10 @@ $filter_url = function ($changes) {
|
||||
$stat_money = function ($amount) use ($session_company_currency) {
|
||||
return escapeHtml(billingMoney($amount, $session_company_currency));
|
||||
};
|
||||
// "+ $2,400.00/mo" under a figure, when there is recurring in it
|
||||
$stat_monthly = function ($amount) use ($stat_money) {
|
||||
return floatval($amount) != 0 ? '<span class="fs-6 fw-normal text-body-secondary ms-1">+ ' . $stat_money($amount) . '/mo</span>' : '';
|
||||
};
|
||||
|
||||
?>
|
||||
|
||||
@@ -115,21 +124,21 @@ $stat_money = function ($amount) use ($session_company_currency) {
|
||||
<div class="col-sm-6 col-xl-3">
|
||||
<a class="itflow-billing-stat <?= $status === 'Awaiting' ? 'active' : '' ?>" href="<?= escapeHtml($filter_url(['status' => 'Awaiting'])) ?>">
|
||||
<div class="itflow-billing-stat-label"><i class="fas fa-hourglass-half me-1"></i>Awaiting answer</div>
|
||||
<div class="itflow-billing-stat-value"><?= $stat_money($pipeline['awaiting']) ?></div>
|
||||
<div class="itflow-billing-stat-value"><?= $stat_money($pipeline['awaiting']) ?><?= $stat_monthly($pipeline['awaiting_monthly']) ?></div>
|
||||
<div class="itflow-billing-stat-note"><?= intval($pipeline['awaiting_count']) ?> out with clients</div>
|
||||
</a>
|
||||
</div>
|
||||
<div class="col-sm-6 col-xl-3">
|
||||
<a class="itflow-billing-stat <?= $status === 'Accepted' ? 'active' : '' ?>" href="<?= escapeHtml($filter_url(['status' => 'Accepted'])) ?>">
|
||||
<div class="itflow-billing-stat-label"><i class="fas fa-thumbs-up me-1"></i>Accepted, to invoice</div>
|
||||
<div class="itflow-billing-stat-value <?= $pipeline['accepted'] > 0 ? 'text-primary-emphasis' : '' ?>"><?= $stat_money($pipeline['accepted']) ?></div>
|
||||
<div class="itflow-billing-stat-value <?= $pipeline['accepted'] > 0 || $pipeline['accepted_monthly'] > 0 ? 'text-primary-emphasis' : '' ?>"><?= $stat_money($pipeline['accepted']) ?><?= $stat_monthly($pipeline['accepted_monthly']) ?></div>
|
||||
<div class="itflow-billing-stat-note"><?= intval($pipeline['accepted_count']) ?> ready to bill</div>
|
||||
</a>
|
||||
</div>
|
||||
<div class="col-sm-6 col-xl-3">
|
||||
<div class="itflow-billing-stat">
|
||||
<div class="itflow-billing-stat-label"><i class="fas fa-trophy me-1"></i>Won, 90 days</div>
|
||||
<div class="itflow-billing-stat-value text-success-emphasis"><?= $stat_money($pipeline['won']) ?></div>
|
||||
<div class="itflow-billing-stat-value text-success-emphasis"><?= $stat_money($pipeline['won']) ?><?= $stat_monthly($pipeline['won_monthly']) ?></div>
|
||||
<div class="itflow-billing-stat-note"><?= intval($pipeline['won_count']) ?> quote<?= intval($pipeline['won_count']) === 1 ? '' : 's' ?> accepted</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -244,7 +253,12 @@ $stat_money = function ($amount) use ($session_company_currency) {
|
||||
<?= escapeHtml($row['quote_expire']) ?>
|
||||
<?php $expiry_pill = quoteExpiryPill($row); if ($expiry_pill) { ?><div><?= $expiry_pill ?></div><?php } ?>
|
||||
</td>
|
||||
<td class="text-end itflow-money fw-semibold"><?= escapeHtml(billingMoney($row['quote_amount'], $row['quote_currency_code'])) ?></td>
|
||||
<td class="text-end itflow-money">
|
||||
<div class="fw-semibold"><?= escapeHtml(billingMoney($row['quote_amount'], $row['quote_currency_code'])) ?></div>
|
||||
<?php if (floatval($row['quote_monthly_amount']) != 0) { ?>
|
||||
<div class="small text-body-secondary text-nowrap" title="Its recurring lines, as so much a month">+ <?= escapeHtml(billingMoney($row['quote_monthly_amount'], $row['quote_currency_code'])) ?>/mo</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-nowrap"><?= quoteStatusBadge($row) ?></td>
|
||||
<td class="text-end">
|
||||
<div class="dropdown dropstart">
|
||||
|
||||
@@ -118,6 +118,9 @@ $paper = [
|
||||
<?php if (trim((string) $recurring['recurring_invoice_scope']) !== '') { ?><span><i class="fas fa-fw fa-comment me-1"></i><?= escapeHtml($recurring['recurring_invoice_scope']) ?></span><?php } ?>
|
||||
<?php if ($recurring['category_name']) { ?><span><i class="fas fa-fw fa-tag me-1"></i><?= escapeHtml($recurring['category_name']) ?></span><?php } ?>
|
||||
<span><i class="fas fa-fw fa-sync-alt me-1"></i><?= escapeHtml($frequency_label) ?></span>
|
||||
<?php if (intval($recurring['recurring_invoice_quote_id'] ?? 0) && ($from_quote = billingRow('quote', intval($recurring['recurring_invoice_quote_id'])))) { ?>
|
||||
<span><i class="fas fa-fw fa-comment-dollar me-1"></i>From <a class="text-reset" href="quote.php?quote_id=<?= intval($from_quote['quote_id']) ?>"><?= escapeHtml(billingNumber('quote', $from_quote)) ?></a></span>
|
||||
<?php } ?>
|
||||
<?php if ($autopay_id && isset($saved_methods[$autopay_id])) { ?><span><i class="fas fa-fw fa-wallet me-1"></i>Autopay</span><?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
* with the win rate by number and by value. Below that, every quote still
|
||||
* waiting for an answer today - soonest to expire first - and how each
|
||||
* client's quotes went. Drafts have not gone out, so they count nowhere but
|
||||
* their own figure; archived quotes are left out.
|
||||
* their own figure; archived quotes are left out. A quote's value is what it
|
||||
* bills up front and a year of its recurring lines (12 x quote_monthly_amount).
|
||||
*/
|
||||
|
||||
require_once "includes/inc_report.php";
|
||||
@@ -15,12 +16,13 @@ reportEnforce('quote_pipeline');
|
||||
$period = reportPeriod('last_12');
|
||||
$client_id = reportClientId();
|
||||
$client_sql = reportClientSql('quote_client_id', $client_id);
|
||||
$value_sql = '(quote_amount + 12 * quote_monthly_amount)';
|
||||
$outcome_sql = "CASE WHEN quote_status IN ('Accepted', 'Invoiced') THEN 'won' WHEN quote_status = 'Declined' THEN 'lost'
|
||||
WHEN quote_status IN ('Sent', 'Viewed') THEN 'open' ELSE 'draft' END";
|
||||
|
||||
// In the period, by outcome
|
||||
$by = ['won' => ['n' => 0, 'v' => 0.0], 'lost' => ['n' => 0, 'v' => 0.0], 'open' => ['n' => 0, 'v' => 0.0], 'draft' => ['n' => 0, 'v' => 0.0]];
|
||||
$sql = mysqli_query($mysqli, "SELECT $outcome_sql AS outcome, COUNT(quote_id) AS n, SUM(quote_amount) AS v FROM quotes
|
||||
$sql = mysqli_query($mysqli, "SELECT $outcome_sql AS outcome, COUNT(quote_id) AS n, SUM($value_sql) AS v FROM quotes
|
||||
WHERE quote_archived_at IS NULL AND " . reportRangeSql('quote_date', $period) . " $client_sql GROUP BY outcome");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$by[$row['outcome']] = ['n' => intval($row['n']), 'v' => floatval($row['v'])];
|
||||
@@ -30,11 +32,11 @@ $quoted_v = $by['won']['v'] + $by['lost']['v'] + $by['open']['v'];
|
||||
$win_rate = reportShare($by['won']['n'], $by['won']['n'] + $by['lost']['n']);
|
||||
$win_rate_value = reportShare($by['won']['v'], $by['won']['v'] + $by['lost']['v']);
|
||||
|
||||
$prev_won = dashboardSum("SELECT SUM(quote_amount) FROM quotes WHERE quote_archived_at IS NULL AND quote_status IN ('Accepted', 'Invoiced') AND " . reportRangeSql('quote_date', $period, true) . " $client_sql");
|
||||
$prev_won = dashboardSum("SELECT SUM($value_sql) FROM quotes WHERE quote_archived_at IS NULL AND quote_status IN ('Accepted', 'Invoiced') AND " . reportRangeSql('quote_date', $period, true) . " $client_sql");
|
||||
|
||||
// Open right now, whenever quoted
|
||||
$open = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT quote_id, quote_prefix, quote_number, quote_scope, quote_status, quote_date, quote_expire, quote_amount,
|
||||
$sql = mysqli_query($mysqli, "SELECT quote_id, quote_prefix, quote_number, quote_scope, quote_status, quote_date, quote_expire, quote_amount, quote_monthly_amount, $value_sql AS value,
|
||||
quote_client_id, client_name, client_abbreviation, DATEDIFF(quote_expire, CURDATE()) AS days_left, DATEDIFF(CURDATE(), quote_date) AS age
|
||||
FROM quotes JOIN clients ON quote_client_id = client_id
|
||||
WHERE quote_archived_at IS NULL AND quote_status IN ('Sent', 'Viewed') $client_sql
|
||||
@@ -45,10 +47,11 @@ while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$open_live = array_filter($open, function ($q) { return $q['quote_expire'] === null || intval($q['days_left']) >= 0; });
|
||||
$open_expired = array_filter($open, function ($q) { return $q['quote_expire'] !== null && intval($q['days_left']) < 0; });
|
||||
$expiring = array_filter($open_live, function ($q) { return $q['quote_expire'] !== null && intval($q['days_left']) <= 14; });
|
||||
$open_monthly = (bool) array_filter($open, function ($q) { return floatval($q['quote_monthly_amount']) != 0; });
|
||||
|
||||
// By client, in the period
|
||||
$clients = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT quote_client_id AS client_id, client_name, client_abbreviation, $outcome_sql AS outcome, COUNT(quote_id) AS n, SUM(quote_amount) AS v
|
||||
$sql = mysqli_query($mysqli, "SELECT quote_client_id AS client_id, client_name, client_abbreviation, $outcome_sql AS outcome, COUNT(quote_id) AS n, SUM($value_sql) AS v
|
||||
FROM quotes JOIN clients ON quote_client_id = client_id
|
||||
WHERE quote_archived_at IS NULL AND quote_status <> 'Draft' AND " . reportRangeSql('quote_date', $period) . " $client_sql
|
||||
GROUP BY quote_client_id, client_name, client_abbreviation, outcome");
|
||||
@@ -105,7 +108,17 @@ $open_columns = [
|
||||
'age' => ['label' => 'Waiting', 'type' => 'count', 'csv_label' => 'Days waiting', 'html' => function ($row) {
|
||||
return intval($row['age']) . ' day' . (intval($row['age']) === 1 ? '' : 's');
|
||||
}],
|
||||
'quote_amount' => ['label' => 'Amount', 'type' => 'money', 'class' => 'fw-semibold'],
|
||||
// Up front and monthly are in the CSV; on screen, one column - what it comes to in its first year, broken down under it
|
||||
'quote_amount' => ['label' => 'Up front', 'type' => 'money', 'screen' => false],
|
||||
'quote_monthly_amount' => ['label' => 'Monthly', 'type' => 'money', 'screen' => false],
|
||||
'value' => ['label' => $open_monthly ? 'First year' : 'Amount', 'csv_label' => 'First year', 'type' => 'money', 'class' => 'fw-semibold',
|
||||
'title' => $open_monthly ? 'Up front, and a year of its recurring lines' : '', 'html' => function ($row) {
|
||||
$html = escapeHtml(reportMoney($row['value']));
|
||||
if (floatval($row['quote_monthly_amount']) != 0) {
|
||||
$html .= '<div class="small fw-normal text-body-secondary">' . escapeHtml(reportMoney($row['quote_amount'])) . ' + ' . escapeHtml(reportMoney($row['quote_monthly_amount'])) . '/mo</div>';
|
||||
}
|
||||
return $html;
|
||||
}],
|
||||
];
|
||||
$client_columns = [
|
||||
'client' => ['label' => 'Client', 'html' => function ($row) {
|
||||
@@ -125,13 +138,14 @@ if (reportExporting('clients')) {
|
||||
reportCsv('Quotes by Client ' . $period['start'] . ' to ' . $period['end'], $client_columns, $clients);
|
||||
}
|
||||
if (reportExporting()) {
|
||||
reportCsv('Open Quotes', $open_columns, $open, ['quote' => 'Total', 'quote_amount' => array_sum(array_column($open, 'quote_amount'))]);
|
||||
reportCsv('Open Quotes', $open_columns, $open, ['quote' => 'Total', 'quote_amount' => array_sum(array_column($open, 'quote_amount')),
|
||||
'quote_monthly_amount' => array_sum(array_column($open, 'quote_monthly_amount')), 'value' => array_sum(array_column($open, 'value'))]);
|
||||
}
|
||||
|
||||
$buckets = reportBuckets($period);
|
||||
$series = [];
|
||||
foreach (['won' => ['Won', 'success'], 'lost' => ['Lost', 'danger'], 'open' => ['Still open', 'info']] as $key => [$label, $tone]) {
|
||||
$series[] = ['label' => $label, 'tone' => $tone, 'stack' => 'q', 'data' => reportSeries("SELECT " . reportBucketSql('quote_date', $buckets['unit']) . " AS b, SUM(quote_amount) AS v
|
||||
$series[] = ['label' => $label, 'tone' => $tone, 'stack' => 'q', 'data' => reportSeries("SELECT " . reportBucketSql('quote_date', $buckets['unit']) . " AS b, SUM($value_sql) AS v
|
||||
FROM quotes WHERE quote_archived_at IS NULL AND $outcome_sql = '$key' AND " . reportRangeSql('quote_date', $period) . " $client_sql GROUP BY b", $buckets['keys'])];
|
||||
}
|
||||
|
||||
@@ -152,7 +166,7 @@ require_once "includes/inc_report_layout.php";
|
||||
['label' => 'Quoted', 'value' => reportMoney($quoted_v), 'sub' => reportCount($quoted_n) . ' <span class="text-body-tertiary">quote' . ($quoted_n === 1 ? '' : 's') . ' sent' . ($by['draft']['n'] ? ', ' . $by['draft']['n'] . ' still drafts' : '') . '</span>'],
|
||||
['label' => 'Won', 'value' => reportMoney($by['won']['v']), 'tone' => $by['won']['v'] > 0 ? 'success' : '', 'sub' => reportChangeHtml($by['won']['v'], $prev_won, 'up', $period['compare'])],
|
||||
['label' => 'Win rate', 'value' => reportPercent($win_rate), 'sub' => $win_rate_value !== null ? escapeHtml(reportPercent($win_rate_value)) . ' <span class="text-body-tertiary">by value, of quotes decided</span>' : '<span class="text-body-tertiary">No quote decided yet</span>'],
|
||||
['label' => 'Open now', 'value' => reportMoney(array_sum(array_column($open_live, 'quote_amount'))), 'url' => '/agent/quotes.php',
|
||||
['label' => 'Open now', 'value' => reportMoney(array_sum(array_column($open_live, 'value'))), 'url' => '/agent/quotes.php',
|
||||
'sub' => count($expiring) ? '<span class="text-warning-emphasis fw-semibold">' . count($expiring) . ' expire within 14 days</span>' : reportCount(count($open_live)) . ' <span class="text-body-tertiary">waiting for an answer</span>'],
|
||||
]) ?>
|
||||
|
||||
@@ -166,7 +180,7 @@ require_once "includes/inc_report_layout.php";
|
||||
<?= reportTable($open_columns, $open, [
|
||||
'empty' => reportEmpty('No quote is waiting for an answer.'),
|
||||
'limit' => 300, 'export_table' => 'open',
|
||||
'totals' => ['quote' => 'Total', 'quote_amount' => array_sum(array_column($open, 'quote_amount'))],
|
||||
'totals' => ['quote' => 'Total', 'value' => array_sum(array_column($open, 'value'))],
|
||||
]) ?>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ require_once "includes/inc_all.php";
|
||||
|
||||
enforceContactCan('accounting');
|
||||
|
||||
$quotes_sql = mysqli_query($mysqli, "SELECT quote_amount, quote_date, quote_id, quote_number, quote_prefix, quote_scope, quote_status,
|
||||
$quotes_sql = mysqli_query($mysqli, "SELECT quote_amount, quote_monthly_amount, quote_date, quote_id, quote_number, quote_prefix, quote_scope, quote_status,
|
||||
quote_url_key FROM quotes WHERE quote_client_id = $session_client_id AND quote_status != 'Draft' ORDER BY quote_date DESC");
|
||||
?>
|
||||
|
||||
@@ -70,7 +70,12 @@ $quotes_sql = mysqli_query($mysqli, "SELECT quote_amount, quote_date, quote_id,
|
||||
<tr>
|
||||
<td><a target="_blank" href="//<?= $config_base_url ?>/guest/guest_view_quote.php?quote_id=<?= "$quote_id&url_key=$quote_url_key" ?>"> <?= "$quote_prefix$quote_number" ?></a></td>
|
||||
<td><?= $quote_scope_display ?></td>
|
||||
<td><?= numfmt_format_currency($currency_format, $quote_amount, $session_company_currency) ?></td>
|
||||
<td>
|
||||
<?= numfmt_format_currency($currency_format, $quote_amount, $session_company_currency) ?>
|
||||
<?php if (floatval($row['quote_monthly_amount']) != 0) { ?>
|
||||
<div class="small text-body-secondary">+ <?= numfmt_format_currency($currency_format, floatval($row['quote_monthly_amount']), $session_company_currency) ?> a month</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td><?= $quote_date ?></td>
|
||||
<td>
|
||||
<span class="p-2 badge text-bg-<?= $quote_badge_color ?>">
|
||||
|
||||
@@ -6016,6 +6016,26 @@ a.itflow-report-stat:focus-visible {
|
||||
.itflow-paper-add-seat {
|
||||
flex: 0 0 7.5rem;
|
||||
}
|
||||
/* A quote's lines billed once and each month, quarter or year - a heading over each, a block of totals each */
|
||||
.itflow-paper-items tr.itflow-paper-group > td {
|
||||
padding-top: 1.1rem;
|
||||
padding-bottom: .35rem;
|
||||
border-bottom: 1px solid var(--bs-border-color);
|
||||
}
|
||||
.itflow-paper-items tbody tr.itflow-paper-group:first-child > td {
|
||||
padding-top: .5rem;
|
||||
}
|
||||
.itflow-paper-totals tr.itflow-paper-totals-head th {
|
||||
padding-top: .9rem;
|
||||
font-size: .7rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: .06em;
|
||||
text-transform: uppercase;
|
||||
color: var(--bs-secondary-color);
|
||||
}
|
||||
.itflow-paper-totals tr.itflow-paper-totals-head:first-child th {
|
||||
padding-top: 0;
|
||||
}
|
||||
/* Unbilled Time - an unticked row fades, the hours box is narrow, and the billing panel stays in view on wide screens */
|
||||
.itflow-unbilled-table tr.itflow-unbilled-off > td:not(.checkbox-column) {
|
||||
opacity: .45;
|
||||
|
||||
4
db.sql
4
db.sql
@@ -1471,6 +1471,7 @@ CREATE TABLE `invoices` (
|
||||
`invoice_archived_at` datetime DEFAULT NULL,
|
||||
`invoice_category_id` int(11) NOT NULL,
|
||||
`invoice_recurring_invoice_id` int(11) NOT NULL DEFAULT 0,
|
||||
`invoice_quote_id` int(11) NOT NULL DEFAULT 0,
|
||||
`invoice_client_id` int(11) NOT NULL,
|
||||
PRIMARY KEY (`invoice_id`),
|
||||
KEY `invoice_client_id` (`invoice_client_id`)
|
||||
@@ -1888,6 +1889,7 @@ CREATE TABLE `quote_items` (
|
||||
`item_tax` decimal(15,2) NOT NULL DEFAULT 0.00,
|
||||
`item_total` decimal(15,2) NOT NULL DEFAULT 0.00,
|
||||
`item_order` int(11) NOT NULL DEFAULT 0,
|
||||
`item_frequency` varchar(10) DEFAULT NULL,
|
||||
`item_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`item_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
`item_archived_at` datetime DEFAULT NULL,
|
||||
@@ -1916,6 +1918,7 @@ CREATE TABLE `quotes` (
|
||||
`quote_date` date NOT NULL,
|
||||
`quote_expire` date DEFAULT NULL,
|
||||
`quote_amount` decimal(15,2) NOT NULL DEFAULT 0.00,
|
||||
`quote_monthly_amount` decimal(15,2) NOT NULL DEFAULT 0.00,
|
||||
`quote_currency_code` varchar(200) NOT NULL,
|
||||
`quote_note` text DEFAULT NULL,
|
||||
`quote_url_key` varchar(200) DEFAULT NULL,
|
||||
@@ -2100,6 +2103,7 @@ CREATE TABLE `recurring_invoices` (
|
||||
`recurring_invoice_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
`recurring_invoice_archived_at` datetime DEFAULT NULL,
|
||||
`recurring_invoice_category_id` int(11) NOT NULL,
|
||||
`recurring_invoice_quote_id` int(11) NOT NULL DEFAULT 0,
|
||||
`recurring_invoice_client_id` int(11) NOT NULL,
|
||||
PRIMARY KEY (`recurring_invoice_id`),
|
||||
KEY `recurring_invoice_client_id` (`recurring_invoice_client_id`)
|
||||
|
||||
@@ -113,7 +113,7 @@ function billingItems($type, $id) {
|
||||
if (!$t || !$id) {
|
||||
return $items;
|
||||
}
|
||||
$seat_columns = $type === 'recurring' ? ', item_seat_source, item_seat_filter, item_seat_minimum' : '';
|
||||
$seat_columns = $type === 'recurring' ? ', item_seat_source, item_seat_filter, item_seat_minimum' : ($type === 'quote' ? ', item_frequency' : '');
|
||||
$sql = mysqli_query($mysqli, "SELECT item_id, item_name, item_description, item_quantity, item_price, item_subtotal, item_tax, item_total,
|
||||
item_order, item_tax_id, item_product_id, item_created_at, tax_name, tax_percent $seat_columns
|
||||
FROM {$t['items']}
|
||||
@@ -245,6 +245,12 @@ function billingItemSave($type, $doc_id, array $in, $item_id = 0, &$error = '')
|
||||
}
|
||||
}
|
||||
|
||||
// A quote line is billed once, or each month, quarter or year
|
||||
if ($type === 'quote' && array_key_exists('frequency', $in)) {
|
||||
$frequency = quoteFrequency($in['frequency']);
|
||||
$seat_set = ", item_frequency = " . ($frequency === '' ? 'NULL' : "'$frequency'");
|
||||
}
|
||||
|
||||
if ($quantity === null || $price === null) {
|
||||
$error = 'The quantity and price have to be numbers.';
|
||||
return null;
|
||||
@@ -347,8 +353,15 @@ function billingRecalc($type, $doc_id) {
|
||||
$t = billingType($type);
|
||||
$doc_id = intval($doc_id);
|
||||
$c = $t['col'];
|
||||
$lines = floatval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(SUM(item_total), 0) FROM {$t['items']} WHERE {$t['item_fk']} = $doc_id"))[0]);
|
||||
$discount = floatval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT {$c}_discount_amount FROM {$t['table']} WHERE {$c}_id = $doc_id"))[0] ?? 0);
|
||||
if ($type === 'quote') {
|
||||
// What is billed once, and what the recurring lines come to a month
|
||||
$groups = quoteLineGroups(billingItems('quote', $doc_id), $discount);
|
||||
$amount = isset($groups['']) ? $groups['']['total'] : 0.0;
|
||||
mysqli_query($mysqli, "UPDATE quotes SET quote_amount = $amount, quote_monthly_amount = " . quoteMonthly($groups) . " WHERE quote_id = $doc_id");
|
||||
return $amount;
|
||||
}
|
||||
$lines = floatval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(SUM(item_total), 0) FROM {$t['items']} WHERE {$t['item_fk']} = $doc_id"))[0]);
|
||||
$amount = round($lines - $discount, 2);
|
||||
mysqli_query($mysqli, "UPDATE {$t['table']} SET {$c}_amount = $amount WHERE {$c}_id = $doc_id");
|
||||
if ($type === 'invoice') {
|
||||
@@ -374,7 +387,7 @@ function billingTotals($type, $row, array $items) {
|
||||
$taxes[$label] = ($taxes[$label] ?? 0) + floatval($item['item_tax']);
|
||||
}
|
||||
}
|
||||
return [
|
||||
$totals = [
|
||||
'subtotal' => round($subtotal, 2),
|
||||
'discount' => floatval($row["{$c}_discount_amount"] ?? 0),
|
||||
'credit' => floatval($row["{$c}_credit_amount"] ?? 0),
|
||||
@@ -382,6 +395,13 @@ function billingTotals($type, $row, array $items) {
|
||||
'taxes' => $taxes,
|
||||
'total' => floatval($row["{$c}_amount"] ?? 0),
|
||||
];
|
||||
if ($type === 'quote') {
|
||||
// One-time and recurring apart - total is what is billed once
|
||||
$totals['groups'] = quoteLineGroups($items, $totals['discount']);
|
||||
$totals['recurring'] = quoteHasRecurring($totals['groups']);
|
||||
$totals['monthly'] = floatval($row['quote_monthly_amount'] ?? 0);
|
||||
}
|
||||
return $totals;
|
||||
}
|
||||
|
||||
/** The next number for a new invoice, quote or recurring invoice - taken atomically. */
|
||||
@@ -398,7 +418,9 @@ function billingHistory($type, $id) {
|
||||
$t = billingType($type);
|
||||
$id = intval($id);
|
||||
$rows = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT history_status, history_description, history_created_at FROM history WHERE {$t['history_fk']} = $id ORDER BY history_id DESC LIMIT 200");
|
||||
// A recurring invoice's entry for the lines a quote added carries the quote's id too - it is the recurring invoice's
|
||||
$own = $type === 'quote' ? ' AND history_recurring_invoice_id = 0' : '';
|
||||
$sql = mysqli_query($mysqli, "SELECT history_status, history_description, history_created_at FROM history WHERE {$t['history_fk']} = $id$own ORDER BY history_id DESC LIMIT 200");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
@@ -983,6 +1005,314 @@ function quoteExpiryPill($row) {
|
||||
return '<span class="badge rounded-pill ' . $class . ' fw-normal">Expires in ' . $days . ' ' . ($days === 1 ? 'day' : 'days') . '</span>';
|
||||
}
|
||||
|
||||
/*
|
||||
* A quote line is billed once, or each month, quarter or year
|
||||
* (item_frequency). The one-time lines go on an invoice when the quote is
|
||||
* invoiced, the others on a recurring invoice for each - quoteConvert().
|
||||
* quote_amount is what is billed once; quote_monthly_amount what the
|
||||
* recurring lines come to a month, for the lists and reports.
|
||||
*/
|
||||
|
||||
/** How often a quote line is billed => label. '' is once. */
|
||||
function quoteFrequencies() {
|
||||
return ['' => 'One time', 'month' => 'Monthly', 'quarter' => 'Quarterly', 'year' => 'Yearly'];
|
||||
}
|
||||
|
||||
/** A line's frequency, cleaned - '' for once. */
|
||||
function quoteFrequency($value) {
|
||||
$value = (string) $value;
|
||||
return in_array($value, ['month', 'quarter', 'year'], true) ? $value : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* A quote's lines by how often they are billed - one-time first, then each
|
||||
* month, quarter and year - each with its lines, subtotal, taxes, discount
|
||||
* and total. The discount comes off the one-time part; on a quote with
|
||||
* nothing one-time, off its first recurring part (and onto that recurring
|
||||
* invoice). Always has at least the one-time group.
|
||||
*/
|
||||
function quoteLineGroups(array $items, $discount = 0) {
|
||||
$labels = ['' => 'One-time', 'month' => 'Each month', 'quarter' => 'Each quarter', 'year' => 'Each year'];
|
||||
$blank = function ($frequency) use ($labels) {
|
||||
return ['frequency' => $frequency, 'label' => $labels[$frequency], 'items' => [], 'subtotal' => 0.0, 'tax' => 0.0, 'taxes' => [], 'discount' => 0.0, 'total' => 0.0];
|
||||
};
|
||||
$groups = [];
|
||||
foreach ($items as $item) {
|
||||
$frequency = quoteFrequency($item['item_frequency'] ?? '');
|
||||
$groups[$frequency] = $groups[$frequency] ?? $blank($frequency);
|
||||
$groups[$frequency]['items'][] = $item;
|
||||
$groups[$frequency]['subtotal'] += floatval($item['item_subtotal']);
|
||||
$groups[$frequency]['tax'] += floatval($item['item_tax']);
|
||||
$groups[$frequency]['total'] += floatval($item['item_total']);
|
||||
if (floatval($item['item_tax']) != 0) {
|
||||
$label = trim(($item['tax_name'] ?? 'Tax') . ' ' . billingQuantity($item['tax_percent'] ?? 0) . '%');
|
||||
$groups[$frequency]['taxes'][$label] = ($groups[$frequency]['taxes'][$label] ?? 0) + floatval($item['item_tax']);
|
||||
}
|
||||
}
|
||||
$ordered = [];
|
||||
foreach (array_keys($labels) as $frequency) {
|
||||
if (isset($groups[$frequency])) {
|
||||
$ordered[$frequency] = $groups[$frequency];
|
||||
}
|
||||
}
|
||||
if (!$ordered) {
|
||||
$ordered[''] = $blank('');
|
||||
}
|
||||
$on = isset($ordered['']) ? '' : array_key_first($ordered);
|
||||
$ordered[$on]['discount'] = floatval($discount);
|
||||
foreach ($ordered as $frequency => $group) {
|
||||
$ordered[$frequency]['subtotal'] = round($group['subtotal'], 2);
|
||||
$ordered[$frequency]['tax'] = round($group['tax'], 2);
|
||||
$ordered[$frequency]['total'] = round($group['total'] - $group['discount'], 2);
|
||||
}
|
||||
return $ordered;
|
||||
}
|
||||
|
||||
/** Whether a quote has recurring lines - from quoteLineGroups(). */
|
||||
function quoteHasRecurring(array $groups) {
|
||||
return (bool) array_diff_key($groups, ['' => true]);
|
||||
}
|
||||
|
||||
/** What the recurring groups come to a month - a quarterly one a third, a yearly one a twelfth. */
|
||||
function quoteMonthly(array $groups) {
|
||||
$monthly = 0.0;
|
||||
foreach ($groups as $frequency => $group) {
|
||||
if ($frequency !== '') {
|
||||
$monthly += recurringInvoiceMonthly($group['total'], $frequency);
|
||||
}
|
||||
}
|
||||
return round($monthly, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a quote comes to, in words - "$1,500.00", "$1,500.00 up front and
|
||||
* $2,400.00 a month", "$2,400.00 a month".
|
||||
*/
|
||||
function quoteAmountText(array $groups, $currency) {
|
||||
$parts = [];
|
||||
foreach ($groups as $frequency => $group) {
|
||||
if ($frequency === '') {
|
||||
if ($group['items'] || count($groups) === 1) {
|
||||
$parts[] = billingMoney($group['total'], $currency) . (count($groups) > 1 ? ' up front' : '');
|
||||
}
|
||||
} else {
|
||||
$parts[] = billingMoney($group['total'], $currency) . ' a ' . recurringInvoicePeriodWord($frequency);
|
||||
}
|
||||
}
|
||||
return count($parts) > 1 ? implode(', ', array_slice($parts, 0, -1)) . ' and ' . end($parts) : (string) reset($parts);
|
||||
}
|
||||
|
||||
/** A quote's groups, from the database - for the pages that only have its row. */
|
||||
function quoteGroups(array $quote) {
|
||||
return quoteLineGroups(billingItems('quote', intval($quote['quote_id'])), $quote['quote_discount_amount'] ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put lines on a document after the ones already there - their stored
|
||||
* amounts as they are. $suffix goes on the end of each description.
|
||||
*/
|
||||
function billingCopyLines(array $items, $to_type, $to_id, $suffix = '') {
|
||||
global $mysqli;
|
||||
$t = billingType($to_type);
|
||||
$to_id = intval($to_id);
|
||||
$order = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(MAX(item_order), 0) FROM {$t['items']} WHERE {$t['item_fk']} = $to_id"))[0]);
|
||||
foreach ($items as $item) {
|
||||
$order++;
|
||||
$name = escapeSql($item['item_name']);
|
||||
$description = escapeSql(trim(trim((string) $item['item_description']) . ($suffix !== '' ? "\n$suffix" : '')));
|
||||
mysqli_query($mysqli, "INSERT INTO {$t['items']} SET item_name = '$name', item_description = '$description', item_quantity = " . floatval($item['item_quantity']) . ",
|
||||
item_price = " . floatval($item['item_price']) . ", item_subtotal = " . floatval($item['item_subtotal']) . ", item_tax = " . floatval($item['item_tax']) . ",
|
||||
item_total = " . floatval($item['item_total']) . ", item_order = $order, item_tax_id = " . intval($item['item_tax_id']) . ", {$t['item_fk']} = $to_id");
|
||||
}
|
||||
}
|
||||
|
||||
/** The day recurring billing starts by default - the first of next month. */
|
||||
function quoteRecurringStart() {
|
||||
return date('Y-m-01', strtotime('first day of next month'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice an accepted quote. Its one-time lines go on a new draft invoice;
|
||||
* its lines billed each month, quarter or year go on a recurring invoice for
|
||||
* each - a new one, or added to one of the client's of that frequency. $opts:
|
||||
* date the invoice's date
|
||||
* first_period put the recurring lines on the invoice too - the first period billed now
|
||||
* recurring frequency => [to (0 for new, or a recurring invoice id), start (its first
|
||||
* invoice date, new ones), auto_send (1 emails them, 0 leaves drafts)]
|
||||
* by who, for the history
|
||||
* The quote is claimed on the way (Accepted to Invoiced), so it is invoiced
|
||||
* once. Returns [invoice_id (0 when nothing one-time), recurring (id =>
|
||||
* number), numbers] or null with $error.
|
||||
*/
|
||||
function quoteConvert($quote_id, array $opts, &$error = '') {
|
||||
global $mysqli, $config_invoice_prefix, $config_recurring_invoice_prefix;
|
||||
$error = '';
|
||||
$quote = billingRow('quote', $quote_id);
|
||||
if (!$quote) {
|
||||
$error = 'That quote no longer exists.';
|
||||
return null;
|
||||
}
|
||||
$quote_id = intval($quote['quote_id']);
|
||||
$client_id = intval($quote['client_id']);
|
||||
$number = billingNumber('quote', $quote);
|
||||
if ($quote['quote_status'] !== 'Accepted') {
|
||||
$error = $quote['quote_status'] === 'Invoiced' ? "$number is already invoiced." : "$number has to be accepted before it is invoiced.";
|
||||
return null;
|
||||
}
|
||||
$by = (string) ($opts['by'] ?? 'ITFlow');
|
||||
$groups = quoteLineGroups(billingItems('quote', $quote_id), $quote['quote_discount_amount']);
|
||||
$valid_date = function ($date) {
|
||||
$date = (string) $date;
|
||||
return ($parsed = DateTime::createFromFormat('!Y-m-d', $date)) && $parsed->format('Y-m-d') === $date ? $date : '';
|
||||
};
|
||||
$date = $valid_date($opts['date'] ?? '') ?: date('Y-m-d');
|
||||
$first_period = !empty($opts['first_period']);
|
||||
if (!$groups['']['items'] && !quoteHasRecurring($groups) && floatval($groups['']['discount'] ?? 0) == 0) {
|
||||
$error = "$number has nothing on it to invoice.";
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check every recurring part's choice before anything is made
|
||||
$plans = [];
|
||||
foreach ($groups as $frequency => $group) {
|
||||
if ($frequency === '') {
|
||||
continue;
|
||||
}
|
||||
$choice = (array) ($opts['recurring'][$frequency] ?? []);
|
||||
$to = intval($choice['to'] ?? 0);
|
||||
if ($to) {
|
||||
$existing = billingRow('recurring', $to);
|
||||
if (!$existing || intval($existing['client_id']) !== $client_id || $existing['recurring_invoice_frequency'] !== $frequency || $existing['recurring_invoice_archived_at']) {
|
||||
$error = 'That recurring invoice is not one of ' . $quote['client_name'] . "'s billed " . strtolower(recurringInvoiceFrequencies()[$frequency]) . '.';
|
||||
return null;
|
||||
}
|
||||
$plans[$frequency] = ['to' => $to, 'row' => $existing];
|
||||
} else {
|
||||
$start = $valid_date($choice['start'] ?? '') ?: quoteRecurringStart();
|
||||
$plans[$frequency] = ['to' => 0, 'start' => $start, 'auto_send' => intval($choice['auto_send'] ?? 1) ? 1 : 0];
|
||||
}
|
||||
}
|
||||
|
||||
mysqli_begin_transaction($mysqli);
|
||||
try {
|
||||
// Claim it
|
||||
mysqli_query($mysqli, "UPDATE quotes SET quote_status = 'Invoiced' WHERE quote_id = $quote_id AND quote_status = 'Accepted'");
|
||||
if (mysqli_affected_rows($mysqli) !== 1) {
|
||||
mysqli_rollback($mysqli);
|
||||
$error = "$number was invoiced just now.";
|
||||
return null;
|
||||
}
|
||||
$currency = escapeSql($quote['quote_currency_code']);
|
||||
$category_id = intval($quote['quote_category_id']);
|
||||
$scope = escapeSql($quote['quote_scope']);
|
||||
$note = escapeSql($quote['quote_note']);
|
||||
$numbers = [];
|
||||
|
||||
// The invoice - what is billed once, and the first period when asked
|
||||
$invoice_id = 0;
|
||||
$one_time = $groups['']['items'] ?? [];
|
||||
$invoice_discount = isset($groups['']) ? floatval($groups['']['discount']) : 0.0;
|
||||
if ($one_time || ($first_period && quoteHasRecurring($groups)) || $invoice_discount != 0) {
|
||||
$terms = intval($quote['client_net_terms']);
|
||||
$invoice_number = billingNextNumber('invoice');
|
||||
$prefix = escapeSql($config_invoice_prefix);
|
||||
$url_key = randomString(32);
|
||||
mysqli_query($mysqli, "INSERT INTO invoices SET invoice_prefix = '$prefix', invoice_number = $invoice_number, invoice_scope = '$scope', invoice_date = '$date',
|
||||
invoice_due = DATE_ADD('$date', INTERVAL $terms DAY), invoice_discount_amount = $invoice_discount, invoice_amount = 0, invoice_currency_code = '$currency',
|
||||
invoice_note = '$note', invoice_category_id = $category_id, invoice_status = 'Draft', invoice_url_key = '$url_key', invoice_quote_id = $quote_id, invoice_client_id = $client_id");
|
||||
$invoice_id = intval(mysqli_insert_id($mysqli));
|
||||
billingCopyLines($one_time, 'invoice', $invoice_id);
|
||||
if ($first_period) {
|
||||
foreach ($groups as $frequency => $group) {
|
||||
if ($frequency !== '') {
|
||||
billingCopyLines($group['items'], 'invoice', $invoice_id, 'First ' . recurringInvoicePeriodWord($frequency));
|
||||
}
|
||||
}
|
||||
}
|
||||
billingRecalc('invoice', $invoice_id);
|
||||
$invoice_ref = billingNumber('invoice', billingRow('invoice', $invoice_id, false));
|
||||
$numbers[] = $invoice_ref;
|
||||
billingLog('invoice', $invoice_id, 'Draft', "Made from quote $number by $by");
|
||||
logAudit('Invoice', 'Create', escapeSql("$by made invoice $invoice_ref from quote $number"), $client_id, $invoice_id);
|
||||
|
||||
// The ticket the quote was raised from is billed with it
|
||||
mysqli_query($mysqli, "UPDATE tickets SET ticket_invoice_id = $invoice_id WHERE ticket_quote_id = $quote_id AND ticket_invoice_id = 0");
|
||||
}
|
||||
|
||||
// The recurring invoices
|
||||
$recurring = [];
|
||||
foreach ($plans as $frequency => $plan) {
|
||||
$group = $groups[$frequency];
|
||||
if ($plan['to']) {
|
||||
$recurring_id = $plan['to'];
|
||||
billingCopyLines($group['items'], 'recurring', $recurring_id);
|
||||
if ($group['discount'] != 0) {
|
||||
mysqli_query($mysqli, "UPDATE recurring_invoices SET recurring_invoice_discount_amount = recurring_invoice_discount_amount + " . floatval($group['discount']) . " WHERE recurring_invoice_id = $recurring_id");
|
||||
}
|
||||
billingRecalc('recurring', $recurring_id);
|
||||
$ref = billingNumber('recurring', $plan['row']);
|
||||
// On the recurring invoice's history, and carrying the quote's id too - that is
|
||||
// how the quote finds a recurring invoice it wasn't made into (quoteMadeFrom)
|
||||
logHistory(escapeSql(intval($plan['row']['recurring_invoice_status']) ? 'Active' : 'Paused'), escapeSql(count($group['items']) . ' line' . (count($group['items']) === 1 ? '' : 's') . " from quote $number added by $by"), 0, $quote_id, $recurring_id);
|
||||
logAudit('Recurring Invoice', 'Edit', escapeSql("$by added the lines of quote $number to recurring invoice $ref"), $client_id, $recurring_id);
|
||||
} else {
|
||||
$recurring_number = billingNextNumber('recurring');
|
||||
$prefix = escapeSql($config_recurring_invoice_prefix);
|
||||
$start = $plan['start'];
|
||||
$discount = floatval($group['discount']);
|
||||
mysqli_query($mysqli, "INSERT INTO recurring_invoices SET recurring_invoice_prefix = '$prefix', recurring_invoice_number = $recurring_number, recurring_invoice_scope = '$scope',
|
||||
recurring_invoice_frequency = '$frequency', recurring_invoice_next_date = '$start', recurring_invoice_anchor_day = DAY('$start'), recurring_invoice_status = 1,
|
||||
recurring_invoice_discount_amount = $discount, recurring_invoice_amount = 0, recurring_invoice_currency_code = '$currency', recurring_invoice_note = '$note',
|
||||
recurring_invoice_auto_send = {$plan['auto_send']}, recurring_invoice_email_notify = 1, recurring_invoice_category_id = $category_id,
|
||||
recurring_invoice_quote_id = $quote_id, recurring_invoice_client_id = $client_id");
|
||||
$recurring_id = intval(mysqli_insert_id($mysqli));
|
||||
billingCopyLines($group['items'], 'recurring', $recurring_id);
|
||||
billingRecalc('recurring', $recurring_id);
|
||||
$ref = billingNumber('recurring', billingRow('recurring', $recurring_id, false));
|
||||
billingLog('recurring', $recurring_id, 'Active', "Made from quote $number by $by - first invoice on $start");
|
||||
logAudit('Recurring Invoice', 'Create', escapeSql("$by made recurring invoice $ref from quote $number"), $client_id, $recurring_id);
|
||||
}
|
||||
$recurring[$recurring_id] = $ref;
|
||||
$numbers[] = $ref;
|
||||
}
|
||||
|
||||
billingLog('quote', $quote_id, 'Invoiced', 'Invoiced as ' . implode(', ', $numbers) . " by $by");
|
||||
logAudit('Quote', 'Edit', escapeSql("$by invoiced quote $number as " . implode(', ', $numbers)), $client_id, $quote_id);
|
||||
mysqli_commit($mysqli);
|
||||
} catch (Throwable $e) {
|
||||
mysqli_rollback($mysqli);
|
||||
logApp('Quote', 'error', "Invoicing quote $number failed: " . $e->getMessage());
|
||||
$error = "$number couldn't be invoiced - nothing was changed.";
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($invoice_id) {
|
||||
triggerCustomAction('invoice_create', $invoice_id);
|
||||
}
|
||||
return ['invoice_id' => $invoice_id, 'recurring' => $recurring, 'numbers' => $numbers];
|
||||
}
|
||||
|
||||
/**
|
||||
* What a quote became - the invoices and recurring invoices made from it, and
|
||||
* the recurring invoices its lines were added to ('added' 1).
|
||||
*/
|
||||
function quoteMadeFrom($quote_id) {
|
||||
global $mysqli;
|
||||
$quote_id = intval($quote_id);
|
||||
$made = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT 'invoice' AS type, invoice_id AS id, invoice_prefix AS prefix, invoice_number AS number, invoice_amount AS amount, invoice_status AS status, invoice_currency_code AS currency, '' AS frequency, 0 AS added
|
||||
FROM invoices WHERE invoice_quote_id = $quote_id
|
||||
UNION ALL
|
||||
SELECT 'recurring', recurring_invoice_id, recurring_invoice_prefix, recurring_invoice_number, recurring_invoice_amount, IF(recurring_invoice_status = 1, 'Active', 'Paused'), recurring_invoice_currency_code, recurring_invoice_frequency,
|
||||
IF(recurring_invoice_quote_id = $quote_id, 0, 1)
|
||||
FROM recurring_invoices WHERE recurring_invoice_quote_id = $quote_id
|
||||
OR recurring_invoice_id IN (SELECT history_recurring_invoice_id FROM history WHERE history_quote_id = $quote_id AND history_recurring_invoice_id > 0)");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$made[] = $row;
|
||||
}
|
||||
return $made;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- Recurring invoices */
|
||||
|
||||
/** How long a missed run is still billed by the nightly run - a night or a few the cron didn't run. */
|
||||
|
||||
@@ -63,6 +63,11 @@ function billingPdfRender($type, array $row, $variant = 'document') {
|
||||
};
|
||||
|
||||
$status = $type === 'recurring' ? '' : (string) $row["{$c}_status"];
|
||||
// A quote with lines billed each month, quarter or year shows them apart from what is billed once
|
||||
$groups = $type === 'quote' && !empty($totals['recurring']) ? $totals['groups'] : null;
|
||||
$shown_groups = $groups ? array_filter($groups, function ($group) {
|
||||
return $group['frequency'] !== '' || $group['items'] || $group['discount'] != 0;
|
||||
}) : null;
|
||||
$paid = $type === 'invoice' ? invoicePaid($id) : 0;
|
||||
$state = $type === 'invoice' ? invoiceState($row, $paid) : null;
|
||||
|
||||
@@ -141,6 +146,11 @@ function billingPdfRender($type, array $row, $variant = 'document') {
|
||||
$done = in_array($status, ['Paid', 'Cancelled', 'Non-Billable'], true);
|
||||
$amount_html = $label($done ? 'Balance' : 'Amount due') . '<br><span style="font-size:17pt; font-weight:bold;">' . $money($state['balance']) . '</span>'
|
||||
. (!$done && $status !== 'Draft' && $state['balance'] > 0 ? '<br><span style="font-size:7.5pt; color:' . $grey . ';">by ' . $e($row['invoice_due']) . '</span>' : '');
|
||||
} elseif ($type === 'quote' && $shown_groups) {
|
||||
$amount_html = '';
|
||||
foreach (array_values($shown_groups) as $i => $group) {
|
||||
$amount_html .= ($i ? '<br>' : '') . $label($group['frequency'] === '' ? 'Up front' : $group['label']) . '<br><span style="font-size:' . ($i ? '12' : '17') . 'pt; font-weight:bold;">' . $money($group['total']) . '</span>';
|
||||
}
|
||||
} elseif ($type === 'quote') {
|
||||
$amount_html = $label('Quote total') . '<br><span style="font-size:17pt; font-weight:bold;">' . $money($totals['total']) . '</span>';
|
||||
} else {
|
||||
@@ -211,16 +221,22 @@ function billingPdfRender($type, array $row, $variant = 'document') {
|
||||
<td width="49%" style="' . $th . '">ITEM</td><td width="9%" align="right" style="' . $th . '">QTY</td>
|
||||
<td width="14%" align="right" style="' . $th . '">PRICE</td><td width="13%" align="right" style="' . $th . '">TAX</td>
|
||||
<td width="15%" align="right" style="' . $th . '">AMOUNT</td></tr></thead>';
|
||||
foreach ($items as $item) {
|
||||
$tax_label = floatval($item['item_tax']) != 0 || intval($item['item_tax_id'])
|
||||
? '<br><span style="font-size:6.5pt; color:' . $grey . ';">' . $e(trim(($item['tax_name'] ?? '') . ' ' . ($item['tax_percent'] !== null ? billingQuantity($item['tax_percent']) . '%' : ''))) . '</span>'
|
||||
: '';
|
||||
$html .= '<tr nobr="true"><td width="49%" style="' . $td . '"><b>' . $e($item['item_name']) . '</b>'
|
||||
. (trim((string) $item['item_description']) !== '' ? '<br><span style="font-size:7.5pt; color:' . $grey . ';">' . nl2br($e($item['item_description'])) . '</span>' : '') . '</td>
|
||||
<td width="9%" align="right" style="' . $td . '">' . $e(billingQuantity($item['item_quantity'])) . '</td>
|
||||
<td width="14%" align="right" style="' . $td . '">' . $money($item['item_price']) . '</td>
|
||||
<td width="13%" align="right" style="' . $td . '">' . ($tax_label !== '' ? $money($item['item_tax']) . $tax_label : '<span style="color:' . $grey . ';">-</span>') . '</td>
|
||||
<td width="15%" align="right" style="' . $td . '"><b>' . $money($item['item_total']) . '</b></td></tr>';
|
||||
$line_groups = $groups ?: ['' => ['frequency' => '', 'label' => '', 'items' => $items]];
|
||||
foreach ($line_groups as $line_group) {
|
||||
if ($groups) {
|
||||
$html .= '<tr nobr="true"><td colspan="5" style="border-bottom:0.3px solid ' . $line . ';"><br>' . $label($line_group['label']) . '</td></tr>';
|
||||
}
|
||||
foreach ($line_group['items'] as $item) {
|
||||
$tax_label = floatval($item['item_tax']) != 0 || intval($item['item_tax_id'])
|
||||
? '<br><span style="font-size:6.5pt; color:' . $grey . ';">' . $e(trim(($item['tax_name'] ?? '') . ' ' . ($item['tax_percent'] !== null ? billingQuantity($item['tax_percent']) . '%' : ''))) . '</span>'
|
||||
: '';
|
||||
$html .= '<tr nobr="true"><td width="49%" style="' . $td . '"><b>' . $e($item['item_name']) . '</b>'
|
||||
. (trim((string) $item['item_description']) !== '' ? '<br><span style="font-size:7.5pt; color:' . $grey . ';">' . nl2br($e($item['item_description'])) . '</span>' : '') . '</td>
|
||||
<td width="9%" align="right" style="' . $td . '">' . $e(billingQuantity($item['item_quantity'])) . '</td>
|
||||
<td width="14%" align="right" style="' . $td . '">' . $money($item['item_price']) . '</td>
|
||||
<td width="13%" align="right" style="' . $td . '">' . ($tax_label !== '' ? $money($item['item_tax']) . $tax_label : '<span style="color:' . $grey . ';">-</span>') . '</td>
|
||||
<td width="15%" align="right" style="' . $td . '"><b>' . $money($item['item_total']) . '</b></td></tr>';
|
||||
}
|
||||
}
|
||||
if (!$items) {
|
||||
$html .= '<tr><td colspan="5" align="center" style="color:' . $grey . ';">Nothing on it yet.</td></tr>';
|
||||
@@ -241,16 +257,39 @@ function billingPdfRender($type, array $row, $variant = 'document') {
|
||||
} else {
|
||||
// Notes, and what it all comes to
|
||||
$rows = '<tr><td style="color:' . $grey . ';">Subtotal</td><td align="right">' . $money($totals['subtotal']) . '</td></tr>';
|
||||
if ($totals['discount'] != 0) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">Discount</td><td align="right">-' . $money($totals['discount']) . '</td></tr>';
|
||||
if ($shown_groups) {
|
||||
// A block for each - what is billed once, and each month, quarter or year
|
||||
// (widths set on each row - "Total each quarter" needs more than half)
|
||||
$rows = '';
|
||||
$w = ' width="60%"';
|
||||
$wr = ' width="40%"';
|
||||
foreach (array_values($shown_groups) as $i => $group) {
|
||||
if ($i) {
|
||||
$rows .= '<tr><td colspan="2" style="font-size:6pt;"> </td></tr>';
|
||||
}
|
||||
$rows .= '<tr><td colspan="2">' . $label($group['label']) . '</td></tr>';
|
||||
$rows .= '<tr><td' . $w . ' style="color:' . $grey . ';">Subtotal</td><td' . $wr . ' align="right">' . $money($group['subtotal']) . '</td></tr>';
|
||||
if ($group['discount'] != 0) {
|
||||
$rows .= '<tr><td' . $w . ' style="color:' . $grey . ';">Discount</td><td' . $wr . ' align="right">-' . $money($group['discount']) . '</td></tr>';
|
||||
}
|
||||
foreach ($group['taxes'] as $tax_label => $tax_amount) {
|
||||
$rows .= '<tr><td' . $w . ' style="color:' . $grey . ';">' . $e($tax_label) . '</td><td' . $wr . ' align="right">' . $money($tax_amount) . '</td></tr>';
|
||||
}
|
||||
$rows .= '<tr><td' . $w . ' style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>' . ($group['frequency'] === '' ? 'Total up front' : 'Total ' . $e(strtolower($group['label']))) . '</b></td><td' . $wr . ' align="right" style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>' . $money($group['total']) . '</b></td></tr>';
|
||||
}
|
||||
}
|
||||
foreach ($totals['taxes'] as $tax_label => $tax_amount) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">' . $e($tax_label) . '</td><td align="right">' . $money($tax_amount) . '</td></tr>';
|
||||
if (!$shown_groups) {
|
||||
if ($totals['discount'] != 0) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">Discount</td><td align="right">-' . $money($totals['discount']) . '</td></tr>';
|
||||
}
|
||||
foreach ($totals['taxes'] as $tax_label => $tax_amount) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">' . $e($tax_label) . '</td><td align="right">' . $money($tax_amount) . '</td></tr>';
|
||||
}
|
||||
if ($totals['credit'] > 0) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">Credit</td><td align="right">-' . $money($totals['credit']) . '</td></tr>';
|
||||
}
|
||||
$rows .= '<tr><td style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>Total</b></td><td align="right" style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>' . $money($totals['total']) . '</b></td></tr>';
|
||||
}
|
||||
if ($totals['credit'] > 0) {
|
||||
$rows .= '<tr><td style="color:' . $grey . ';">Credit</td><td align="right">-' . $money($totals['credit']) . '</td></tr>';
|
||||
}
|
||||
$rows .= '<tr><td style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>Total</b></td><td align="right" style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>' . $money($totals['total']) . '</b></td></tr>';
|
||||
if ($type === 'invoice' && $state['paid'] > 0) {
|
||||
$rows .= '<tr><td style="color:#198754;">Paid</td><td align="right" style="color:#198754;">-' . $money($state['paid']) . '</td></tr>';
|
||||
$rows .= '<tr><td style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>Balance due</b></td><td align="right" style="border-top:0.6px solid #adb5bd; font-size:10pt;"><b>' . $money($state['balance']) . '</b></td></tr>';
|
||||
|
||||
@@ -382,11 +382,12 @@ function clientExportDatasets() {
|
||||
'invoice' => 'Invoice', 'date' => 'Date', 'amount' => 'Amount', 'currency' => 'Currency', 'method' => 'Method', 'reference' => 'Reference',
|
||||
]],
|
||||
'quotes' => ['section' => 'billing', 'label' => 'Quotes', 'columns' => [
|
||||
'number' => 'Quote', 'scope' => 'Scope', 'status' => 'Status', 'date' => 'Date', 'expires' => 'Expires', 'amount' => 'Amount', 'discount' => 'Discount',
|
||||
'number' => 'Quote', 'scope' => 'Scope', 'status' => 'Status', 'date' => 'Date', 'expires' => 'Expires', 'amount' => 'Amount', 'monthly' => 'Monthly', 'discount' => 'Discount',
|
||||
'currency' => 'Currency', 'category' => 'Category', 'note' => 'Note', 'archived' => 'Archived',
|
||||
]],
|
||||
'quote_items' => ['section' => 'billing', 'label' => 'Quote items', 'columns' => [
|
||||
'quote' => 'Quote', 'item' => 'Item', 'description' => 'Description', 'quantity' => 'Quantity', 'price' => 'Price', 'subtotal' => 'Subtotal', 'tax' => 'Tax', 'total' => 'Total',
|
||||
'billed' => 'Billed',
|
||||
]],
|
||||
'recurring_invoices' => ['section' => 'billing', 'label' => 'Recurring invoices', 'columns' => [
|
||||
'number' => 'Recurring invoice', 'scope' => 'Scope', 'frequency' => 'Every', 'active' => 'Active', 'next_date' => 'Next date', 'last_sent' => 'Last sent',
|
||||
@@ -808,7 +809,7 @@ function clientExportRows($dataset, $client_id, $options) {
|
||||
WHERE {$prefix}_client_id = $client_id" . $live("{$prefix}_archived_at") . " AND i.item_archived_at IS NULL ORDER BY {$prefix}_number DESC, i.item_order, i.item_id") as $row) {
|
||||
$rows[] = [$parent => $row['parent_ref'], 'item' => $row['item_name'], 'description' => $row['item_description'], 'quantity' => floatval($row['item_quantity']),
|
||||
'price' => round(floatval($row['item_price']), 2), 'subtotal' => round(floatval($row['item_subtotal']), 2), 'tax' => round(floatval($row['item_tax']), 2),
|
||||
'total' => round(floatval($row['item_total']), 2)];
|
||||
'total' => round(floatval($row['item_total']), 2), 'billed' => quoteFrequencies()[quoteFrequency($row['item_frequency'] ?? '')]];
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -823,7 +824,7 @@ function clientExportRows($dataset, $client_id, $options) {
|
||||
case 'quotes':
|
||||
foreach ($fetch("SELECT * FROM quotes WHERE quote_client_id = $client_id" . $live('quote_archived_at') . " ORDER BY quote_number DESC") as $row) {
|
||||
$rows[] = ['number' => $row['quote_prefix'] . $row['quote_number'], 'scope' => $row['quote_scope'], 'status' => $row['quote_status'],
|
||||
'date' => clientExportDate($row['quote_date']), 'expires' => clientExportDate($row['quote_expire']), 'amount' => round(floatval($row['quote_amount']), 2),
|
||||
'date' => clientExportDate($row['quote_date']), 'expires' => clientExportDate($row['quote_expire']), 'amount' => round(floatval($row['quote_amount']), 2), 'monthly' => round(floatval($row['quote_monthly_amount'] ?? 0), 2),
|
||||
'discount' => round(floatval($row['quote_discount_amount']), 2), 'currency' => $row['quote_currency_code'], 'category' => $name('category', $row['quote_category_id']),
|
||||
'note' => clientExportText($row['quote_note']), 'archived' => clientExportDate($row['quote_archived_at'])];
|
||||
}
|
||||
|
||||
@@ -335,6 +335,7 @@ function getExportColumns($export_type) {
|
||||
'quote_number_display' => ['label' => 'Quote Number'],
|
||||
'quote_scope' => ['label' => 'Scope', 'weight' => 3],
|
||||
'quote_amount' => ['label' => 'Amount', 'format' => 'money'],
|
||||
'quote_monthly_amount' => ['label' => 'Monthly', 'format' => 'money'],
|
||||
'quote_date' => ['label' => 'Date'],
|
||||
'quote_status' => ['label' => 'Status'],
|
||||
'client_name' => ['label' => 'Client', 'weight' => 2, 'default' => false],
|
||||
|
||||
@@ -968,6 +968,8 @@ function reportCsv($name, array $columns, array $rows, array $totals = null) {
|
||||
if ($totals) {
|
||||
$values = [];
|
||||
foreach ($columns as $key => $column) {
|
||||
// A total is its value as given - a column's csv function reads a row's fields, which the totals don't have
|
||||
unset($column['csv']);
|
||||
$values[] = escapeCsvFormula(array_key_exists($key, $totals) ? reportCsvValue($column, $totals, $key) : '');
|
||||
}
|
||||
fputcsv($out, $values, ',', '"', '\\');
|
||||
|
||||
@@ -510,7 +510,7 @@ function globalSearchGroups($terms, $deep) {
|
||||
$quote_number_match = $quote_number !== null ? "OR quote_number = $quote_number" : '';
|
||||
$groups[] = [
|
||||
'key' => 'quotes', 'label' => 'Quotes', 'icon' => 'comment-dollar', 'module' => 'module_sales', 'enabled' => $accounting,
|
||||
'select' => 'quote_id, quote_prefix, quote_number, quote_scope, quote_status, quote_amount,
|
||||
'select' => 'quote_id, quote_prefix, quote_number, quote_scope, quote_status, quote_amount, quote_monthly_amount,
|
||||
quote_currency_code, quote_client_id, client_name',
|
||||
'from' => 'quotes LEFT JOIN clients ON quote_client_id = client_id',
|
||||
'where' => "quote_archived_at IS NULL
|
||||
@@ -524,7 +524,8 @@ function globalSearchGroups($terms, $deep) {
|
||||
return [
|
||||
'ref' => $row['quote_prefix'] . $row['quote_number'],
|
||||
'title' => $row['quote_scope'] ?: 'Quote',
|
||||
'subtitle' => globalSearchMoney($row['quote_amount'], $row['quote_currency_code']),
|
||||
'subtitle' => globalSearchMoney($row['quote_amount'], $row['quote_currency_code'])
|
||||
. (floatval($row['quote_monthly_amount']) != 0 ? ' + ' . globalSearchMoney($row['quote_monthly_amount'], $row['quote_currency_code']) . '/mo' : ''),
|
||||
'context' => $row['client_name'],
|
||||
'badge' => ['text' => $row['quote_status'], 'variant' => $quote_badge_variants[$row['quote_status']] ?? 'secondary'],
|
||||
'url' => globalSearchClientUrl('quote.php', $row['quote_client_id'], ['quote_id' => intval($row['quote_id'])]),
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
/*
|
||||
* A quote as the client opens it from its link - the quote on the left, as
|
||||
* it prints; on the right what it comes to, how long it is good for, and
|
||||
* it prints; on the right what it comes to (up front, and each month,
|
||||
* quarter or year for its recurring lines), how long it is good for, and
|
||||
* Accept and Decline while it waits for an answer. Once accepted, the
|
||||
* client can send back a signed copy or a purchase order. Opening it marks
|
||||
* a sent quote viewed and tells the agents.
|
||||
@@ -54,8 +55,22 @@ $paper = [
|
||||
|
||||
<div class="card mb-3">
|
||||
<div class="card-body">
|
||||
<div class="itflow-billing-stat-label">Quote total</div>
|
||||
<div class="itflow-billing-stat-value mb-3"><?= escapeHtml(billingMoney($totals['total'], $currency)) ?></div>
|
||||
<?php if ($totals['recurring']) { ?>
|
||||
<div class="d-flex flex-wrap gap-4 mb-3">
|
||||
<?php foreach ($totals['groups'] as $frequency => $group) {
|
||||
if ($frequency === '' && !$group['items'] && $group['discount'] == 0) {
|
||||
continue;
|
||||
} ?>
|
||||
<div>
|
||||
<div class="itflow-billing-stat-label"><?= $frequency === '' ? 'Up front' : escapeHtml($group['label']) ?></div>
|
||||
<div class="itflow-billing-stat-value"><?= escapeHtml(billingMoney($group['total'], $currency)) ?></div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } else { ?>
|
||||
<div class="itflow-billing-stat-label">Quote total</div>
|
||||
<div class="itflow-billing-stat-value mb-3"><?= escapeHtml(billingMoney($totals['total'], $currency)) ?></div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($awaiting) { ?>
|
||||
<p class="small text-body-secondary mb-3">
|
||||
@@ -64,7 +79,7 @@ $paper = [
|
||||
</p>
|
||||
<div class="d-grid gap-2 mb-3">
|
||||
<a class="btn btn-success btn-lg confirm-link" href="guest_post.php?accept_quote=<?= $quote_id ?>&url_key=<?= $query_key ?>"
|
||||
data-confirm-title="Accept <?= escapeHtml($number) ?>?" data-confirm-text="We'll be told straight away and be in touch to get started." data-confirm-button="Accept">
|
||||
data-confirm-title="Accept <?= escapeHtml($number) ?>?" data-confirm-text="<?= $totals['recurring'] ? 'That is ' . escapeHtml(quoteAmountText($totals['groups'], $currency)) . '. ' : '' ?>We'll be told straight away and be in touch to get started." data-confirm-button="Accept">
|
||||
<i class="fas fa-thumbs-up me-2"></i>Accept
|
||||
</a>
|
||||
<a class="btn btn-outline-danger confirm-link" href="guest_post.php?decline_quote=<?= $quote_id ?>&url_key=<?= $query_key ?>"
|
||||
|
||||
@@ -67,6 +67,11 @@ if ($paper_type === 'invoice') {
|
||||
$paper_seats = $paper_type === 'recurring' ? recurringSeatLines($paper_id, intval($paper_row['client_id'])) : [];
|
||||
$paper_seat_sources = recurringSeatSources();
|
||||
|
||||
// A quote with lines billed each month, quarter or year shows them apart from
|
||||
// what is billed once - the lines under a heading each, and a total each
|
||||
$paper_groups = $paper_type === 'quote' && !empty($paper_totals['recurring']) ? $paper_totals['groups'] : null;
|
||||
$paper_line_groups = $paper_groups ?: ['' => ['frequency' => '', 'label' => '', 'items' => $paper_items]];
|
||||
|
||||
$paper_post = function ($params) use ($paper_type, $paper_id, $paper) {
|
||||
return 'post.php?' . http_build_query(array_merge($params, ['type' => $paper_type, 'doc_id' => $paper_id, 'csrf_token' => $paper['csrf'] ?? '']));
|
||||
};
|
||||
@@ -124,7 +129,17 @@ $paper_bill_lines = array_filter([
|
||||
<?php if (!in_array($paper_status, ['Paid', 'Cancelled', 'Non-Billable', 'Draft'], true) && ($paper['balance'] ?? 0) > 0) { ?>
|
||||
<div class="small text-body-secondary">by <?= escapeHtml($paper_row['invoice_due']) ?></div>
|
||||
<?php } ?>
|
||||
<?php } elseif ($paper_type === 'quote') { ?>
|
||||
<?php } elseif ($paper_type === 'quote' && $paper_groups) {
|
||||
$paper_first = true;
|
||||
foreach ($paper_groups as $paper_frequency => $paper_group) {
|
||||
if ($paper_frequency === '' && !$paper_group['items'] && $paper_group['discount'] == 0) {
|
||||
continue;
|
||||
} ?>
|
||||
<div class="itflow-paper-label <?= $paper_first ? '' : 'mt-2' ?>"><?= $paper_frequency === '' ? 'Up front' : escapeHtml($paper_group['label']) ?></div>
|
||||
<div class="<?= $paper_first ? 'itflow-paper-amount' : 'fs-5 fw-semibold' ?>"><?= $paper_money($paper_group['total']) ?></div>
|
||||
<?php $paper_first = false;
|
||||
}
|
||||
} elseif ($paper_type === 'quote') { ?>
|
||||
<div class="itflow-paper-label">Quote total</div>
|
||||
<div class="itflow-paper-amount"><?= $paper_money($paper_totals['total']) ?></div>
|
||||
<?php } else { ?>
|
||||
@@ -162,65 +177,72 @@ $paper_bill_lines = array_filter([
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($paper_items as $item) {
|
||||
$item_id = intval($item['item_id']);
|
||||
$item_tax_label = floatval($item['item_tax']) != 0 || intval($item['item_tax_id'])
|
||||
? trim(($item['tax_name'] ?? '') . ' ' . ($item['tax_percent'] !== null ? billingQuantity($item['tax_percent']) . '%' : ''))
|
||||
: ''; ?>
|
||||
<tr id="line-<?= $item_id ?>" data-item-id="<?= $item_id ?>">
|
||||
<?php if ($paper_edit) { ?>
|
||||
<td class="itflow-paper-handle d-print-none"><span class="drag-handle" title="Drag to reorder"><i class="fas fa-grip-vertical"></i></span></td>
|
||||
<?php } ?>
|
||||
<td class="itflow-paper-item">
|
||||
<div class="fw-semibold"><?= escapeHtml($item['item_name']) ?></div>
|
||||
<?php if (trim((string) $item['item_description']) !== '') { ?>
|
||||
<div class="small text-body-secondary"><?= nl2br(escapeHtml($item['item_description'])) ?></div>
|
||||
<?php foreach ($paper_line_groups as $paper_frequency => $paper_line_group) {
|
||||
if ($paper_groups) { ?>
|
||||
<tr class="itflow-paper-group" data-group="<?= $paper_frequency ?>">
|
||||
<td colspan="7"><span class="itflow-paper-label"><?= escapeHtml($paper_line_group['label']) ?></span></td>
|
||||
</tr>
|
||||
<?php }
|
||||
foreach ($paper_line_group['items'] as $item) {
|
||||
$item_id = intval($item['item_id']);
|
||||
$item_tax_label = floatval($item['item_tax']) != 0 || intval($item['item_tax_id'])
|
||||
? trim(($item['tax_name'] ?? '') . ' ' . ($item['tax_percent'] !== null ? billingQuantity($item['tax_percent']) . '%' : ''))
|
||||
: ''; ?>
|
||||
<tr id="line-<?= $item_id ?>" data-item-id="<?= $item_id ?>" data-group="<?= $paper_frequency ?>">
|
||||
<?php if ($paper_edit) { ?>
|
||||
<td class="itflow-paper-handle d-print-none"><span class="drag-handle" title="Drag to reorder"><i class="fas fa-grip-vertical"></i></span></td>
|
||||
<?php } ?>
|
||||
<?php if (isset($paper_seats[$item_id])) {
|
||||
$seat = $paper_seats[$item_id];
|
||||
$seat_source = $paper_seat_sources[$seat['item_seat_source']] ?? ['unit' => 'seat', 'icon' => 'fa-users']; ?>
|
||||
<div class="small mt-1 d-print-none">
|
||||
<span class="badge rounded-pill bg-primary-subtle text-primary-emphasis fw-normal text-wrap text-start" title="The quantity is counted again for each invoice">
|
||||
<i class="fas <?= $seat_source['icon'] ?> me-1"></i>Per <?= $seat_source['unit'] ?> · <?= escapeHtml($seat['seat_label']) ?><?= floatval($seat['item_seat_minimum']) > 0 ? ' · at least ' . billingQuantity($seat['item_seat_minimum']) : '' ?>
|
||||
</span>
|
||||
<?php if ($seat['seat_count'] === null) { ?>
|
||||
<span class="badge rounded-pill text-bg-danger fw-normal">The license is gone - it stays at <?= billingQuantity($item['item_quantity']) ?></span>
|
||||
<?php } elseif ($seat['seat_drift']) { ?>
|
||||
<span class="badge rounded-pill text-bg-warning fw-normal" title="The next invoice bills the new count">Now <?= billingQuantity($seat['seat_quantity']) ?></span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end text-nowrap"><?= billingQuantity($item['item_quantity']) ?></td>
|
||||
<td class="text-end text-nowrap"><?= $paper_money($item['item_price']) ?></td>
|
||||
<td class="text-end text-nowrap">
|
||||
<?php if ($item_tax_label !== '') { ?>
|
||||
<?= $paper_money($item['item_tax']) ?>
|
||||
<div class="small text-body-secondary"><?= escapeHtml($item_tax_label) ?></div>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">-</span>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end text-nowrap fw-semibold"><?= $paper_money($item['item_total']) ?></td>
|
||||
<?php if ($paper_edit) { ?>
|
||||
<td class="text-end d-print-none itflow-paper-actions">
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-link text-body-secondary" title="Line actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/billing/billing_item.php?type=<?= $paper_type ?>&doc_id=<?= $paper_id ?>&item_id=<?= $item_id ?>">
|
||||
<i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit
|
||||
</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="<?= escapeHtml($paper_post(['billing_item_delete' => $item_id])) ?>"
|
||||
data-confirm-text="<?= escapeHtml($item['item_name']) ?> comes off <?= escapeHtml(billingNumber($paper_type, $paper_row)) ?><?= $paper_type === 'invoice' && intval($item['item_product_id']) ? ' - anything taken from stock goes back' : '' ?>.">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Remove
|
||||
</a>
|
||||
<td class="itflow-paper-item">
|
||||
<div class="fw-semibold"><?= escapeHtml($item['item_name']) ?></div>
|
||||
<?php if (trim((string) $item['item_description']) !== '') { ?>
|
||||
<div class="small text-body-secondary"><?= nl2br(escapeHtml($item['item_description'])) ?></div>
|
||||
<?php } ?>
|
||||
<?php if (isset($paper_seats[$item_id])) {
|
||||
$seat = $paper_seats[$item_id];
|
||||
$seat_source = $paper_seat_sources[$seat['item_seat_source']] ?? ['unit' => 'seat', 'icon' => 'fa-users']; ?>
|
||||
<div class="small mt-1 d-print-none">
|
||||
<span class="badge rounded-pill bg-primary-subtle text-primary-emphasis fw-normal text-wrap text-start" title="The quantity is counted again for each invoice">
|
||||
<i class="fas <?= $seat_source['icon'] ?> me-1"></i>Per <?= $seat_source['unit'] ?> · <?= escapeHtml($seat['seat_label']) ?><?= floatval($seat['item_seat_minimum']) > 0 ? ' · at least ' . billingQuantity($seat['item_seat_minimum']) : '' ?>
|
||||
</span>
|
||||
<?php if ($seat['seat_count'] === null) { ?>
|
||||
<span class="badge rounded-pill text-bg-danger fw-normal">The license is gone - it stays at <?= billingQuantity($item['item_quantity']) ?></span>
|
||||
<?php } elseif ($seat['seat_drift']) { ?>
|
||||
<span class="badge rounded-pill text-bg-warning fw-normal" title="The next invoice bills the new count">Now <?= billingQuantity($seat['seat_quantity']) ?></span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<?php } ?>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
<td class="text-end text-nowrap"><?= billingQuantity($item['item_quantity']) ?></td>
|
||||
<td class="text-end text-nowrap"><?= $paper_money($item['item_price']) ?></td>
|
||||
<td class="text-end text-nowrap">
|
||||
<?php if ($item_tax_label !== '') { ?>
|
||||
<?= $paper_money($item['item_tax']) ?>
|
||||
<div class="small text-body-secondary"><?= escapeHtml($item_tax_label) ?></div>
|
||||
<?php } else { ?>
|
||||
<span class="text-body-secondary">-</span>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="text-end text-nowrap fw-semibold"><?= $paper_money($item['item_total']) ?></td>
|
||||
<?php if ($paper_edit) { ?>
|
||||
<td class="text-end d-print-none itflow-paper-actions">
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-link text-body-secondary" title="Line actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/billing/billing_item.php?type=<?= $paper_type ?>&doc_id=<?= $paper_id ?>&item_id=<?= $item_id ?>">
|
||||
<i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit
|
||||
</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="<?= escapeHtml($paper_post(['billing_item_delete' => $item_id])) ?>"
|
||||
data-confirm-text="<?= escapeHtml($item['item_name']) ?> comes off <?= escapeHtml(billingNumber($paper_type, $paper_row)) ?><?= $paper_type === 'invoice' && intval($item['item_product_id']) ? ' - anything taken from stock goes back' : '' ?>.">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Remove
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<?php } ?>
|
||||
</tr>
|
||||
<?php }
|
||||
} ?>
|
||||
<?php if (!$paper_items && !$paper_edit) { ?>
|
||||
<tr><td colspan="5" class="text-center text-body-secondary py-4">Nothing on it yet.</td></tr>
|
||||
<?php } ?>
|
||||
@@ -229,7 +251,7 @@ $paper_bill_lines = array_filter([
|
||||
<tfoot class="d-print-none">
|
||||
<tr class="itflow-paper-add" id="add-line">
|
||||
<td colspan="7">
|
||||
<div class="itflow-paper-add-grid<?= $paper_type === 'recurring' ? ' itflow-paper-add-grid-seats' : '' ?>" data-billing-add>
|
||||
<div class="itflow-paper-add-grid<?= in_array($paper_type, ['recurring', 'quote'], true) ? ' itflow-paper-add-grid-seats' : '' ?>" data-billing-add>
|
||||
<div class="itflow-paper-add-item">
|
||||
<input type="text" class="form-control form-control-sm mb-1" name="name" form="billingAddLine" placeholder="<?= $paper_items ? 'Add a line - type a product, or anything' : 'First line - type a product, or anything' ?>" maxlength="200" required data-line-name aria-label="Item">
|
||||
<textarea class="form-control form-control-sm" name="description" form="billingAddLine" rows="1" placeholder="Description (optional)" data-line-description aria-label="Description"></textarea>
|
||||
@@ -247,6 +269,15 @@ $paper_bill_lines = array_filter([
|
||||
<option value="assets" data-count="<?= intval(recurringSeatCount($paper_row['client_id'], 'assets', [])) ?>">Per device</option>
|
||||
</select>
|
||||
</div>
|
||||
<?php } elseif ($paper_type === 'quote') { ?>
|
||||
<div class="itflow-paper-add-seat">
|
||||
<label class="form-label small text-body-secondary mb-0">Billed</label>
|
||||
<select class="form-select form-select-sm" name="frequency" form="billingAddLine" title="Billed once, or each month, quarter or year - those go on a recurring invoice when the quote is invoiced">
|
||||
<?php foreach (quoteFrequencies() as $frequency_value => $frequency_label) { ?>
|
||||
<option value="<?= $frequency_value ?>"><?= $frequency_label ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<div class="itflow-paper-add-price">
|
||||
<label class="form-label small text-body-secondary mb-0">Price</label>
|
||||
@@ -288,6 +319,24 @@ $paper_bill_lines = array_filter([
|
||||
<div class="small"><?= nl2br(escapeHtml($paper_row["{$paper_c}_note"])) ?></div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php if ($paper_groups) { ?>
|
||||
<table class="itflow-paper-totals">
|
||||
<?php foreach ($paper_groups as $paper_frequency => $paper_group) {
|
||||
if ($paper_frequency === '' && !$paper_group['items'] && $paper_group['discount'] == 0) {
|
||||
continue;
|
||||
} ?>
|
||||
<tr class="itflow-paper-totals-head"><th colspan="2"><?= escapeHtml($paper_group['label']) ?></th></tr>
|
||||
<tr><th>Subtotal</th><td><?= $paper_money($paper_group['subtotal']) ?></td></tr>
|
||||
<?php if ($paper_group['discount'] != 0) { ?>
|
||||
<tr><th>Discount</th><td>-<?= $paper_money($paper_group['discount']) ?></td></tr>
|
||||
<?php } ?>
|
||||
<?php foreach ($paper_group['taxes'] as $tax_label => $tax_amount) { ?>
|
||||
<tr><th><?= escapeHtml($tax_label) ?></th><td><?= $paper_money($tax_amount) ?></td></tr>
|
||||
<?php } ?>
|
||||
<tr class="itflow-paper-total"><th><?= $paper_frequency === '' ? 'Total up front' : 'Total ' . strtolower(escapeHtml($paper_group['label'])) ?></th><td><?= $paper_money($paper_group['total']) ?></td></tr>
|
||||
<?php } ?>
|
||||
</table>
|
||||
<?php } else { ?>
|
||||
<table class="itflow-paper-totals">
|
||||
<tr><th>Subtotal</th><td><?= $paper_money($paper_totals['subtotal']) ?></td></tr>
|
||||
<?php if ($paper_totals['discount'] != 0) { ?>
|
||||
@@ -305,6 +354,7 @@ $paper_bill_lines = array_filter([
|
||||
<tr class="itflow-paper-total"><th>Balance due</th><td><?= $paper_money($paper['balance']) ?></td></tr>
|
||||
<?php } ?>
|
||||
</table>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<?php if (trim((string) $paper_footer) !== '') { ?>
|
||||
|
||||
@@ -217,9 +217,19 @@ function itflowBillingItems(options) {
|
||||
// Drag to reorder
|
||||
var body = paper.querySelector('[data-billing-items] tbody');
|
||||
if (body && typeof Sortable !== 'undefined' && paper.querySelector('.drag-handle')) {
|
||||
// A quote's lines sit under a heading for how often each is billed - a line moves within its own
|
||||
var grouped = !!body.querySelector('tr.itflow-paper-group');
|
||||
new Sortable(body, {
|
||||
handle: '.drag-handle',
|
||||
draggable: 'tr[data-item-id]',
|
||||
animation: 150,
|
||||
onMove: function (evt) {
|
||||
if (!grouped) {
|
||||
return true;
|
||||
}
|
||||
var before = evt.willInsertAfter ? evt.related : evt.related.previousElementSibling;
|
||||
return !!before && before.getAttribute('data-group') === evt.dragged.getAttribute('data-group');
|
||||
},
|
||||
onEnd: function (evt) {
|
||||
if (evt.oldIndex === evt.newIndex) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user