From 53d0f8a8415e84c688236a14338774db9b3c2988 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Fri, 16 Oct 2026 19:12:03 -0400 Subject: [PATCH] Expenses stage 4: getting expenses in - Import Statement from the expenses menu brings a card or bank CSV in as expenses, reading lines before the header, commas, semicolons or tabs, Windows text and amounts like (12.00), 12.00 CR or 1.234,56, guessing the date and its format, description, memo, amount or money out and in columns, sign and reference and remembering them per account so the next statement goes straight to the review, matching each line to a vendor from what was picked for that merchant last time or a vendor name in the text (shortened names too) with its last category, suggesting a new vendor named from the line when nothing matches, locking lines imported before so a statement never comes in twice, leaving out lines that look entered by hand within 4 days and linking them, leaving out money in unless ticked, filling in the same merchant's other lines, bulk category and client and billing on, making new vendors once and opening the account's expenses for those dates, with remembered merchants to see or forget; paid personally expenses with who paid them that count as spent on their date but move no balance until paid back, a To reimburse pill with what each agent is owed, Reimburse paying back the ticked expenses from one account on one date as a Reimbursement in Transactions with undo for full financial access, quick look, list, vendor page, bulk, export and dashboard showing who paid and whether they were paid back, database 3.0.2 with expense_import_key, expense_paid_by, expense_reimbursement_id, expense_import_rules, reimbursements and account_import_map, CHANGELOG updated --- CHANGELOG.md | 32 +- admin/database_updates/3.0.2.php | 59 ++ agent/expense_import.php | 530 ++++++++++++ agent/expenses.php | 55 +- agent/modals/expense/expense.php | 40 +- agent/modals/expense/expense_export.php | 6 + agent/modals/expense/expense_form.php | 72 +- agent/modals/expense/expense_import_rules.php | 74 ++ agent/modals/expense/expense_reimburse.php | 147 ++++ .../modals/expense/expense_reimbursement.php | 89 ++ .../modals/transaction/transaction_export.php | 2 +- agent/post/expense.php | 62 +- agent/post/expense_import.php | 181 ++++ agent/post/transaction.php | 8 +- agent/transactions.php | 14 +- agent/vendor.php | 5 +- css/itflow_custom.css | 17 + db.sql | 55 +- functions.php | 1 + functions/dashboard.php | 8 + functions/expense_import.php | 770 ++++++++++++++++++ functions/expenses.php | 235 +++++- functions/export.php | 1 + js/expense_import.js | 218 +++++ 24 files changed, 2637 insertions(+), 44 deletions(-) create mode 100644 admin/database_updates/3.0.2.php create mode 100644 agent/expense_import.php create mode 100644 agent/modals/expense/expense_import_rules.php create mode 100644 agent/modals/expense/expense_reimburse.php create mode 100644 agent/modals/expense/expense_reimbursement.php create mode 100644 agent/post/expense_import.php create mode 100644 functions/expense_import.php create mode 100644 js/expense_import.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 58ca610d6..d6e995fb9 100644 --- a/CHANGELOG.md +++ b/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 3.0.1) 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 3.0.2) in the same pass. Nothing else is required. -This is a big release, with twenty-two database steps in one go, so take a backup before you update. +This is a big release, with twenty-three 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: @@ -87,6 +87,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run - A recurring expense the nightly run missed by up to 7 nights is recorded the next night, dated the day it was due. One further behind waits for you to Record Now or Move On. The notification every night for each recurring expense with a date in the past is replaced by one notification, on the day it falls too far behind. - Deleting a recurring expense needs full Financial access, the same as deleting an expense. It used to need write access. - Unbilled Time also lists expenses to bill for agents with Financial access, and its count in the menu adds them. For those agents it shows in the menu even with ticketing turned off. + - Transactions has a Reimbursement type for money paid back to an agent. A Transactions export filtered to Expense leaves those out. - 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: @@ -360,6 +361,20 @@ If you are coming from 26.08 or earlier, the database update still has to be run - A client's **recurring expense** can be billed on each time it records one, with a markup, or be **covered by** one of the client's recurring invoices. A covered one isn't billed by itself: the recurring invoice's new **Costs** card lists what it covers, what it bills a month before tax against what they cost a month, and the margin left, with Add to set up another one it covers. The recurring expenses list and quick look show which it is. - Expenses to bill show on the dashboard and the client overview's attention list, and the client overview's Not yet invoiced includes them. The export has Billing and Billed On columns. - The demo data's latest client expenses are to bill, at 15%. + - **Import Statement**, from the expenses menu, brings a card or bank statement in as expenses: + - Upload the CSV your bank or card exports, and pick the account it is for and how it was paid. Lines before the header, semicolons or tabs, and amounts like (12.00), 12.00 CR or 1.234,56 are read as they come. + - The columns are worked out for you: the date and its format, the description and a memo, the amount (charges positive or negative) or separate money out and money in columns, and a reference such as the check number. You check them once against the statement - the next statement from the same account goes straight to the review. + - The review lists every line with the vendor, category and client it matched: what you picked for that merchant last time, or one of your vendors whose name is in it (cut short too, such as THREE RIVERS COLO for Three Rivers Colocation), with the vendor's last category. A line with no match offers a new vendor named from its text - "SQ *JOES COFFEE #123 PITTSBURGH PA" is Joes Coffee. + - Picking a vendor fills in the other lines from the same merchant. Set a category or client for all the ticked lines at once. A line for a client can be billed on to them. + - Lines brought in before are locked, so importing the same statement twice brings nothing in twice - two coffees on one day are still two. A line with the same amount as an expense entered by hand on that account within 4 days is left out unless ticked, and is linked to that expense so the next statement knows it too. + - Payments to the card, deposits and refunds are left out unless ticked - then they come in as money back. + - Import brings in the ticked lines, making each new vendor once, and opens the expenses list on that account and those dates. The merchants are remembered for next time - see or forget them from the Import Statement page. + - **Paid personally**: an expense an agent paid for themselves - a hotel, parts from the store - is picked as Paid personally, with who paid it. It counts as spent on its date like any other, but comes out of none of your accounts until they are paid back. + - The list has a To reimburse pill. On it, each agent shows with what they are owed and a Reimburse button. Rows show who paid and whether they have been paid back. + - Reimburse pays an agent back for the ticked expenses from one account, on a date, with how and a reference. The account's balance goes down then, and the payment shows in Transactions as a Reimbursement. Spending totals and reports count the expenses once, on their own dates. + - The expense's quick look says who paid it, To reimburse with a Reimburse button, or when it was paid back. The reimbursement opens with what it paid back, and Undo (full Financial access) takes the payment back off the account. + - Bulk Reimburse pays back the ticked expenses of one agent. Bulk Set Account puts a paid personally expense on an account instead, unless it was paid back already. + - Expenses to reimburse show on the dashboard. The export has a Paid Personally column, and the list can be cut to one agent's. - **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. @@ -617,6 +632,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run - 2.9.9: `expenses.expense_created_by` (0 for cron and payment providers), backfilled from the audit log's Expense Create entries, and `expense_refund_of_id`. - 3.0.0 (after 2.9.9, the way 2.0.0 followed 1.9.9): `recurring_expenses.recurring_expense_frequency` becomes `varchar(10)` (`month`, `quarter` or `year`, from 1 and 2), with `recurring_expense_anchor_day` (backfilled from the next date) and `recurring_expense_end_date`. `recurring_expense_day` and `recurring_expense_month` are dropped. `expenses.expense_recurring_expense_id`, backfilled by vendor, category, client and description from the day each recurring expense was set up. - 3.0.1: `expenses.expense_billable` (0), `expense_markup` (`decimal(7,2)`, 0.00), `expense_invoice_id` (keyed) and `expense_invoice_item_id`. `recurring_expenses.recurring_expense_billable` (0), `recurring_expense_markup` and `recurring_expense_recurring_invoice_id`. `settings.config_expense_markup` (0.00). + - 3.0.2: `expenses.expense_import_key` (keyed), `expense_paid_by` (0) and `expense_reimbursement_id` (keyed). `expense_import_rules` (`import_rule_match` unique, `import_rule_vendor_id`, `import_rule_category_id`, `import_rule_client_id`, `import_rule_billable`, `import_rule_used_count`). `reimbursements` (`reimbursement_user_id`, `reimbursement_date`, `reimbursement_amount`, `reimbursement_currency_code`, `reimbursement_account_id`, `reimbursement_payment_method`, `reimbursement_reference`, `reimbursement_expense_id`, `reimbursement_created_by`). `accounts.account_import_map`. - 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`. @@ -785,6 +801,18 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Handlers: GET `expense_unbill` (Financial and Sales at 2) and POST `invoice_add_expenses` (`invoice_id`, `expense_ids[]`, `expense_price[id]`, `tax_id`). `unbilled_invoice` and `unbilled_write_off` take `expense_ids[]` and `expense_price[id]` too (Financial at 2 when they are posted), and `unbilled_invoice_clients` bills each client's expenses for agents with Financial at 2. `bulk_edit_expenses` takes `field=billable` (`value` yes with `markup`, or no). `edit_invoice_settings` saves `config_expense_markup`. The export adds `expense_billing_display` and `expense_billed_on`. - New window `agent/modals/invoice/invoice_add_expenses.php?invoice_id=`. `recurring_expense_add.php` takes `?recurring_invoice_id=`. `agent/invoice.php` has the Expenses card and `agent/recurring_invoice.php` the Costs card for agents with Financial access. `js/unbilled_time.js` counts `[data-unbilled-expense-row]` rows with their `[data-unbilled-expense-pick]` and `[data-unbilled-expense-price]`. CSS: `.itflow-unbilled-price`. - `dashboardAttention()` and `clientOverviewAttention()` add Expenses to bill, and `clientOverviewMoney()` adds `unbilled_expenses` and `unbilled_expense_value`. `get_side_nav_counts.php` and `inc_all_client.php` set `$num_unbilled_expenses`. + - Statement import, in the new `functions/expense_import.php` (loaded by `functions.php`): + - `expenseImportRead($path, &$error)` reads a CSV up to `EXPENSE_IMPORT_MAX_BYTES` (2 MB) and `EXPENSE_IMPORT_MAX_LINES` (2000): byte order mark, Windows-1252, the delimiter the first lines agree on, and the header as the line before the first with a date. `expenseImportDateFormats()`, `expenseImportDate($value, $format)`, `expenseImportLooksLikeDate()` and `expenseImportAmount()`. + - `expenseImportGuess($header, $rows)` maps date, description, memo, amount or debit and credit, and reference by header name then by content, with `date_format` and `sign`. `expenseImportMapClean()` checks a posted or saved one. `expenseImportSavedMap()` and `expenseImportSaveMap()` keep it in `accounts.account_import_map`, keyed on the column names (`expenseImportColumnsKey()`). + - `expenseImportLines($rows, $map)` gives each line `date`, `description` (with the memo), `match_text`, `amount` (charges positive), `reference` and `problem`. `expenseImportReview($import)` runs `expenseImportMatch()` (rules from `expenseImportRules()`, then vendor names through `expenseImportWords()`), `expenseImportKeys()` (SHA-256 of account, date, amount, text and repeat, as `key_hash`) and `expenseImportDuplicates()` (`imported`, and `entered` within `EXPENSE_IMPORT_DUPLICATE_DAYS`). `expenseImportMatchKey()` and `expenseImportVendorName()`. + - `expenseImportSave($import, $picks, &$error)` checks every ticked line first, makes new vendors once (`vendorFormSet()`), inserts the expenses with `expense_import_key`, upserts `expense_import_rules` on `import_rule_match`, and stamps the key on hand-entered expenses a left-out line matched. + - New page `agent/expense_import.php` (`?step=columns` and `?step=review`, the statement in `$_SESSION['expense_import']`) and `js/expense_import.js` (`itflowExpenseImport()`). Handlers in the new `agent/post/expense_import.php`: `expense_import_upload` (POST `account`, `payment_method`, `file`), `expense_import_columns`, `expense_import_save` (POST `picks`, one JSON list of `n`, `take`, `vendor` or `new` with `new_vendor`, `category`, `client`, `billable` - under `max_input_vars` for long statements), GET `expense_import_discard` and `expense_import_rule_delete`. New window `agent/modals/expense/expense_import_rules.php`. CSS: `.itflow-import-*`. + - Paid personally, in `functions/expenses.php`: + - `expenseSave()` takes `account=personal` with `paid_by` (one of `expensePayers()`), saving `expense_account_id = 0` and `expense_paid_by`, in the company currency. Money back can't be paid personally, and a paid back expense keeps who paid it. `expenseSave()` and `expenseDelete()` refuse the payment row of a reimbursement, as they do a transfer's. + - `expenseToReimburseSql()`, `expenseToReimburse($user_id, $ids)` (by agent, scoped), `expenseToReimburseCount()` and `expenseReimburse($user_id, $ids, $opts, &$error)`, which claims the expenses with a conditional `UPDATE`, then inserts the payment as an expense with no vendor and category on the account and the `reimbursements` row pointing at it. `reimbursementRow()`, `reimbursementExpenses()` and `reimbursementDelete()`. + - `expenseRow()` adds `paid_by_name`, `reimbursement_date`, `reimbursed_account_name`, `reimbursement_out_id` and `reimbursed_to_name` (its joins on `accounts` and `users` are aliased now). `expenseShows()` adds `to_reimburse`, and `expenseFiltersSql()` takes `paid_by`. + - Handlers: POST `expense_reimburse` (`user_id`, `expense_ids[]`, `account`, `date`, `payment_method`, `reference`) and GET `expense_reimbursement_delete` (Financial at 3). `bulk_edit_expenses` with `field=account` clears `expense_paid_by` and skips paid back ones. The export adds `expense_paid_by_display`. + - New windows `agent/modals/expense/expense_reimburse.php` (`?user_id=` or `?expense_ids[]=`) and `expense_reimbursement.php?id=`. `agent/transactions.php`, its export and `agent/post/transaction.php` give the payment the type Reimbursement, with the agent as the other side. `dashboardAttention()` adds Expenses to reimburse. - The billing helpers (`billingTimeInvoice()`, `quoteConvert()`, `recurringInvoiceGenerate()` and the rest) take the agent's name as plain text. Their handlers now pass `stripslashes($session_name)`, so a name with an apostrophe no longer shows a backslash in history and the audit log. - 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. diff --git a/admin/database_updates/3.0.2.php b/admin/database_updates/3.0.2.php new file mode 100644 index 000000000..5675338e4 --- /dev/null +++ b/admin/database_updates/3.0.2.php @@ -0,0 +1,59 @@ + 'Statement', 'columns' => 'Columns', 'review' => 'Review']; + +?> + + + + +
+ $label) { $i++; + $done = array_search($key, array_keys($steps)) < array_search($step, array_keys($steps)); ?> + 1) { ?> + + ' : $i . '. ' ?> + + + + + · + Start over + + +
+ + + + +
+ + is part way through - nothing from it is imported yet. + Carry on with it +
+ + +
+
+
+
+

Import Statement

+
+
+ +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
The CSV your bank or card's website exports - often under Download, Export or Activity. Up to lines.
+
+
+
+ +
+
+
+
+
+
+

How it goes

+
    +
  • The columns are worked out for you - check them once, and the next statement from the same account goes straight to the review.
  • +
  • Each line is matched to a vendor: what you picked for that merchant last time, or one of your vendors whose name is in it. A new vendor can be made from the line's text.
  • +
  • Lines brought in before, and ones with the same amount as an expense entered by hand within days, are left out unless you tick them.
  • +
  • Payments to the card and other money in are left out unless you tick them - then they come in as money back.
  • +
  • Nothing is imported until you press Import on the review.
  • +
+
+ merchant remembered + + See them + +
+
+
+
+
+ +'; + $html .= ''; + foreach ($header as $c => $label) { + $html .= ''; + } + return $html . ''; + }; + ?> + +
+
+

Which column is which lines

+
+
+ +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
A memo or notes column, added after the description.
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+
+ + +
+ + + + $label) { ?> + + + + + + + + $cell) { ?> + + + + + +
+
+ +
+
+ + + + 0, 'imported' => 0, 'entered' => 0, 'money_in' => 0, 'problem' => 0, 'matched' => 0]; + foreach ($lines as $i => $line) { + $state = $line['problem'] !== '' ? 'problem' : ($line['imported'] ? 'imported' : ($line['entered'] ? 'entered' : ($line['amount'] < 0 ? 'money_in' : 'new'))); + $lines[$i]['state'] = $state; + $count[$state]++; + $count['matched'] += $state === 'new' && $line['vendor_id'] ? 1 : 0; + } + $vendor_options = ''; + foreach ($vendors as $vendor_id => $vendor) { + $vendor_options .= ''; + } + $category_options = ''; + foreach ($categories as $category_id => $category) { + $category_options .= ''; + } + $client_options = ''; + foreach ($clients as $client_id => $client_name) { + $client_options .= ''; + } + $states = [ + 'new' => ['label' => 'New', 'tone' => 'primary'], + 'imported' => ['label' => 'Imported before', 'tone' => 'secondary'], + 'entered' => ['label' => 'Entered already?', 'tone' => 'warning'], + 'money_in' => ['label' => 'Money in', 'tone' => 'success'], + 'problem' => ['label' => "Can't read", 'tone' => 'danger'], + ]; + ?> + + +
+
+
+
New charges
+
+
matched to a vendor
+
+
+
+
+
Already in
+
+
imported before · look entered
+
+
+
+
+
Money in
+
+
Payments and refunds - left out
+
+
+
+
+
Columns
+
+
Change them' . $count['problem'] . " can't be read" : '' ?>
+
+
+
+ +
+ + + +
+
+

The lines

+
+ + +
+
+ + +
+ +
+ + +
+ +
+
+ + + + + + + + + + + + + + 0; + $fixed = in_array($state, ['imported', 'problem'], true); + $vendor_value = $line['vendor_id'] ? (string) $line['vendor_id'] : 'new'; ?> + class=""> + + + + + + + + + + + + + + +
+
+
DateOn the statementAmountVendorCategoryClient
+
+ aria-label="Import this line"> +
+
?' ?> +
-' ?>
+
+ + + Open it + + + + + + as last time + + by name + + # +
+
+ + + + + + +
+ > + +
+
+
+ +
+
+ + + + + + + 0 ? round(($stats['month'] - $stats['last_to_date']) / $stats['last_to_date'] * 100) : null; @@ -152,8 +156,12 @@ $month_change = $stats['last_to_date'] > 0 ? round(($stats['month'] - $stats['la