Skip to content

Developer notes

  • A posted charge is one core transaction in ?:vendor_payouts: payout_type = payout, positive payout_amount, approval_status = C, order_id = 0, comment ending with [VLC-<charge id>]. Core subtracts positive payout rows from the vendor income and balance and counts them as marketplace income (the way vendor_plans charges plan fees). Verified in Tygh\VendorPayouts of 4.17.2 MV and 4.21.2 MV (identical code).
  • A refund or credit is a payout row with a negative amount, approval C: core adds it back to the vendor balance (the way vendor_debt_payout books a refill). The original row is never edited or deleted. Note: the core admin “income” sums only positive payout rows, so the add-on’s Monthly summary shows the net.
  • Amounts are integer cents internally; one transaction per charge.
Table Contents
?:kv_vlc_charges Ledger: company, kind (L label, F fee, M manual, R refund), source + source_ref (unique with kind), order, reference, carrier, service, country, weight, cost, cost_source (C carrier, T table, D default, M manual, N none), markup, amount, status (P, A, Q posting, O posted, V void), payout_id, reversal_of / reversed_by, fee_id, period (YYYY-MM), period.
?:kv_vlc_rates Price table.
?:kv_vlc_fees Monthly service fees (scope all / plans / vendors).
?:kv_vlc_sources Source switch, watermark (last label id read), last scan.

The four tables are kept on uninstall unless “Delete all charge data on uninstall” is ticked.

  • INSERT IGNORE on the unique key (source, source_ref, kind): a label, a fee month (<fee>-<vendor>-<YYYY-MM>) or a refund is recorded once, whether the hook, the scan or a retry comes first.
  • Posting runs under GET_LOCK('kv_vlc_posting'), claims each row with UPDATE ... SET status = 'Q' WHERE status = 'A' and posts only when one row changed. A row left in Q (crash) is repaired: posted when its core row with the marker exists, else approved again.
  • The scan reads each source above its watermark in batches of 500; a row that is still being created (pending status, younger than 3 days) holds the watermark. A pending status wins over a stored reference (UPS, FedEx, Canada Post and MyParcel store their recovery reference before the carrier call); for those four only tracking_number counts as a label.
  • Cancellations: a charged row in a cancelled status is voided or refunded (for 90 days after the label date). A charged row that no longer exists in the source table is only flagged (source_removed = 'Y', note, admin filter “removed=1” and a card); the administrator refunds or voids it (reverse). Order deletion removes label rows in some connectors while the label stays billed, so no automatic refund.
  • Per-source pending_noref: statuses that are pending only while the row has no reference (MyParcel C: label created, barcode not synced yet); GLS A (prepared, print pending) is pending.
  • If writing the core row throws after the INSERT (an exception from a vendor_payouts_update_post handler), the row is adopted by its [VLC-<id>] marker instead of being posted again.
  • Return labels (UPS, FedEx, Canada Post rows with kind = R) are charged only with the setting “Return labels: Charge when created”.
  • MySQL lock names carry a hash of the database name and table prefix, so two stores on one database server do not block each other.
Hook Why
kv_sendcloud_label_created($order_id, $company_id, $account_id, $shipment_ids, $auto) Sendcloud 1.2+: charge at once when $account_id <= 0.
kv_sendcloud_label_canceled($order_id, $company_id, $account_id, $shipment_ids, $state) cancelled = refund now; queued = the next run reads the final state.
before_dispatch Admin-area pseudo-cron, at most every 10 minutes (GET, signed in).

kv_vendor_label_charges_sources(&$list): add a table adapter for another label add-on.

$list['acme_post'] = [
'title' => 'Acme Post',
'addon' => 'kv_acme_post', // must be active
'table' => 'kv_acme_post_labels', // without prefix
'pk' => 'label_id', // stored as source_ref
'refs' => ['tracking_number'], // label reference, first non-empty
'carrier_name' => 'Acme',
'service' => 'service_code',
'parcels' => 'parcel_count',
'account' => ['column' => 'account_id'], // <= 0 = marketplace account; or ['setting' => 'owner_mode']
'cost' => ['amount' => ['net_charge'], 'currency' => 'currency'],
'cancelled' => ['X'],
'pending' => ['W'],
];

The table needs order_id, company_id and status columns. Missing optional columns are ignored.

// Record a label the MARKETPLACE account paid for a vendor's order (idempotent per source + ref). Returns the charge id.
fn_kv_vendor_label_charges_record_label([
'source' => 'acme_post', 'ref' => '12345', 'company_id' => 7, 'order_id' => 1001,
'reference' => 'ACME123', 'carrier' => 'Acme', 'service' => 'express', 'parcels' => 1,
'cost' => '4.20', 'cost_currency' => 'EUR', // optional; else price table / default price
]);
// The label was cancelled: void or refund its charge. Returns voided | reversed | none.
fn_kv_vendor_label_charges_cancel_label('acme_post', '12345');
  • admin.php?dispatch=kv_label_charges.manage|report|rates|fees|sources|export|report_export (GET), POST modes update, m_approve, m_void, reverse, add_manual, post_now, save_rates, update_fee, delete_fee, source_toggle (administrators only).
  • Vendors: manage, report, export, report_export only, always limited to their own company.
  • Cron: php admin.php --dispatch=kv_label_charges.cron or the URL with the core cron password.

Gated: recording (hooks, scan, API), posting and fees. Never gated: the pages, reports, CSV and the vendor statement.

app/addons/kv_vendor_label_charges/ (Tygh/Addons/KvVendorLabelCharges/{Config,Money,Sources,Rates,Pricing,Ledger,Scanner,Fees,Maintenance,Report,Retention}.php, controllers/backend/kv_label_charges.php, schemas), design/backend/templates/addons/kv_vendor_label_charges/, design/backend/css/addons/kv_vendor_label_charges/, var/langs/en/addons/kv_vendor_label_charges.po. No storefront files (nothing changes for shoppers; full page cache safe).