Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Support spreadsheet and CSV formats for purchase order documents (#14869)
Damian Legawiecbf1f47daaca6
diff --git a/.changeset/po-document-spreadsheets.md b/.changeset/po-document-spreadsheets.mdnew file mode 100644index 0000000000..2777cb6904--- /dev/null+++ b/.changeset/po-document-spreadsheets.md@@ -0,0 +1,5 @@+---+"@spree/dashboard": patch+---++Purchase order documents on orders can now be Excel, OpenDocument or CSV spreadsheets, as well as PDFs, images and Word documents.diff --git a/docs/developer/how-to/build-a-b2b-store.mdx b/docs/developer/how-to/build-a-b2b-store.mdxindex f9ea15643d..a5cfea4787 100644--- a/docs/developer/how-to/build-a-b2b-store.mdx+++ b/docs/developer/how-to/build-a-b2b-store.mdx@@ -610,7 +610,7 @@ For a corporate buyer, their own purchase order number matters as much as your o- The cart and the order carry `po_number`. It shows in order search, on the order, and in the confirmation email.- Turn on `po_number_required` on a company and its buyers must supply a number before they can complete checkout. The cart reports `po_number_required: true` and a `po_number_required` requirement until they do.-- A buyer can also attach the signed PO document itself: a PDF, an image or a Word file of up to 10 MB, stored privately.+- A buyer can also attach the signed PO document itself: a PDF, an image, a Word document or a spreadsheet (Excel, OpenDocument or CSV) of up to 10 MB, stored privately.<CodeGroup>diff --git a/packages/dashboard/src/components/spree/orders/order-purchase-order-card.tsx b/packages/dashboard/src/components/spree/orders/order-purchase-order-card.tsxindex 5463f0fbc1..40620e4ba0 100644--- a/packages/dashboard/src/components/spree/orders/order-purchase-order-card.tsx+++ b/packages/dashboard/src/components/spree/orders/order-purchase-order-card.tsx@@ -23,10 +23,7 @@ import { DownloadIcon, FileTextIcon, PencilIcon } from '@spree/dashboard-ui/iconimport { type FormEvent, useState } from 'react'import { useTranslation } from 'react-i18next'import { useOrderMutation } from '../../../hooks/use-order'--/** What a purchase order plausibly arrives as — mirrors the server allowlist. */-const PO_DOCUMENT_ACCEPT =- 'application/pdf,image/jpeg,image/png,image/heic,image/webp,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'+import { PO_DOCUMENT_ACCEPT } from '../../../schemas/order'/** Narrowed from the SDK's own contract so the payload cannot drift from it. */type UpdateParams = Pick<OrderUpdateParams, 'po_number' | 'po_document_signed_id'>diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.jsonindex 4ec984d91d..8466f49f23 100644--- a/packages/dashboard/src/locales/ar.json+++ b/packages/dashboard/src/locales/ar.json@@ -2957,7 +2957,7 @@"priced_by": "التسعير من {{name}}","purchase_order": {"document": "مستند أمر الشراء",- "document_help": "ملف PDF أو صورة أو Word، بحد أقصى 10 ميغابايت.",+ "document_help": "ملف PDF أو صورة أو مستند Word أو جدول بيانات أو ملف CSV، بحد أقصى 10 ميغابايت.","download_failed": "تعذر تنزيل أمر الشراء","save_failed": "تعذر حفظ أمر الشراء","title": "أمر الشراء"diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.jsonindex 264eb96a3d..7ddd03e69e 100644--- a/packages/dashboard/src/locales/de.json+++ b/packages/dashboard/src/locales/de.json@@ -2937,7 +2937,7 @@"priced_by": "Preis von {{name}}","purchase_order": {"document": "Bestelldokument",- "document_help": "PDF-, Bild- oder Word-Datei, bis zu 10 MB.",+ "document_help": "PDF, Bild, Word-Dokument, Tabelle oder CSV-Datei, bis zu 10 MB.","download_failed": "Die Bestellung konnte nicht heruntergeladen werden","save_failed": "Die Bestellung konnte nicht gespeichert werden","title": "Bestellung des Kunden"diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex da5d2001b2..39c5faab02 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -2937,7 +2937,7 @@"priced_by": "Priced by {{name}}","purchase_order": {"document": "Purchase order document",- "document_help": "PDF, image or Word file, up to 10 MB.",+ "document_help": "PDF, image, Word document, spreadsheet or CSV file, up to 10 MB.","download_failed": "Could not download the purchase order","save_failed": "Could not save the purchase order","title": "Purchase order"diff --git a/packages/dashboard/src/locales/es.json b/packages/dashboard/src/locales/es.jsonindex 2a187ac692..dfe609b91c 100644--- a/packages/dashboard/src/locales/es.json+++ b/packages/dashboard/src/locales/es.json@@ -2724,7 +2724,7 @@"priced_by": "Precio según {{name}}","purchase_order": {"document": "Documento de orden de compra",- "document_help": "PDF, imagen o archivo de Word, hasta 10 MB.",+ "document_help": "PDF, imagen, documento de Word, hoja de cálculo o archivo CSV, hasta 10 MB.","download_failed": "No se pudo descargar la orden de compra","save_failed": "No se pudo guardar la orden de compra","title": "Orden de compra"diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex 11df56d1ef..f9b16ee148 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -2937,7 +2937,7 @@"priced_by": "Tarifé par {{name}}","purchase_order": {"document": "Document de bon de commande",- "document_help": "Fichier PDF, image ou Word, jusqu’à 10 Mo.",+ "document_help": "PDF, image, document Word, feuille de calcul ou fichier CSV, jusqu’à 10 Mo.","download_failed": "Impossible de télécharger le bon de commande","save_failed": "Impossible d’enregistrer le bon de commande","title": "Bon de commande"diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.jsonindex 2dc8d62c57..686321cb4f 100644--- a/packages/dashboard/src/locales/pl.json+++ b/packages/dashboard/src/locales/pl.json@@ -2965,7 +2965,7 @@"priced_by": "Cena z {{name}}","purchase_order": {"document": "Dokument zamówienia zakupu",- "document_help": "Plik PDF, obraz lub dokument Word, do 10 MB.",+ "document_help": "Plik PDF, obraz, dokument Word, arkusz kalkulacyjny lub plik CSV, do 10 MB.","download_failed": "Nie udało się pobrać zamówienia zakupu","save_failed": "Nie udało się zapisać zamówienia zakupu","title": "Zamówienie zakupu"diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.jsonindex 0bb87db504..150b760046 100644--- a/packages/dashboard/src/locales/zh-CN.json+++ b/packages/dashboard/src/locales/zh-CN.json@@ -2929,7 +2929,7 @@"priced_by": "定价来自 {{name}}","purchase_order": {"document": "采购订单文件",- "document_help": "PDF、图片或 Word 文件,最大 10 MB。",+ "document_help": "PDF、图片、Word 文档、电子表格或 CSV 文件,最大 10 MB。","download_failed": "无法下载采购订单","save_failed": "无法保存采购订单","title": "采购订单"diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsxindex 3221bcfddc..5880568ce5 100644--- a/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx+++ b/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx@@ -47,12 +47,9 @@ import {NEW_ORDER_DEFAULTS,type NewOrderFormValues,newOrderFormSchema,+ PO_DOCUMENT_ACCEPT,} from '../../../../schemas/order'-/** What a purchase order plausibly arrives as — mirrors the server allowlist. */-const PO_DOCUMENT_ACCEPT =- 'application/pdf,image/jpeg,image/png,image/heic,image/webp,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'-export const Route = createFileRoute('/_authenticated/$storeId/orders/new')({component: NewOrderPage,})diff --git a/packages/dashboard/src/schemas/order.ts b/packages/dashboard/src/schemas/order.tsindex e8762fc448..5c724b1d5c 100644--- a/packages/dashboard/src/schemas/order.ts+++ b/packages/dashboard/src/schemas/order.ts@@ -223,3 +223,19 @@ export function buildOrderItemsPayload(items: OrderEditItemValues[]): OrderItemsreturn payload}++/** What a purchase order plausibly arrives as — mirrors the server allowlist. */+export const PO_DOCUMENT_ACCEPT = [+ 'application/pdf',+ 'image/jpeg',+ 'image/png',+ 'image/heic',+ 'image/webp',+ 'application/msword',+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',+ 'application/vnd.ms-excel',+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',+ 'application/vnd.oasis.opendocument.spreadsheet',+ 'text/csv',+ '.csv',+].join(',')diff --git a/spree/core/app/models/concerns/spree/purchase/purchase_order.rb b/spree/core/app/models/concerns/spree/purchase/purchase_order.rbindex 7dd8ba4de1..a984a299ae 100644--- a/spree/core/app/models/concerns/spree/purchase/purchase_order.rb+++ b/spree/core/app/models/concerns/spree/purchase/purchase_order.rb@@ -17,8 +17,8 @@ module SpreeMAX_PO_DOCUMENT_SIZE = 10.megabytes# What a purchase order plausibly arrives as: an export from the buyer's- # procurement system, a scan, or a photo of one. Matches the list the- # seller document requirement accepts — the same kinds of paperwork.+ # procurement system (often a spreadsheet or CSV), a scan, or a photo of+ # one.PO_DOCUMENT_CONTENT_TYPES = %w[application/pdfimage/jpeg@@ -27,6 +27,10 @@ module Spreeimage/webpapplication/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.document+ application/vnd.ms-excel+ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet+ application/vnd.oasis.opendocument.spreadsheet+ text/csv].freezeincluded dodiff --git a/spree/core/app/validators/spree/attachment_content_type_validator.rb b/spree/core/app/validators/spree/attachment_content_type_validator.rbindex e55e8e1c36..78dccdf551 100644--- a/spree/core/app/validators/spree/attachment_content_type_validator.rb+++ b/spree/core/app/validators/spree/attachment_content_type_validator.rb@@ -44,6 +44,16 @@ module Spree# identified outright and never reaches the tie-break at all.CONTAINER_CONTENT_TYPES = %w[application/zip application/x-ole-storage].freeze+ # Plain-text formats carry no signature at all, so the bytes can only prove+ # what they are not. Marcel names anything it recognises — HTML, XML, SVG,+ # scripts with a shebang, executables — and those never reach this+ # tie-break. What is left is accepted under a text extension only when it+ # is free of the control bytes every binary format carries. No particular+ # encoding is required, since spreadsheet tools still export in legacy ones.+ TEXT_CONTENT_TYPES = %w[text/csv].freeze+ UNIDENTIFIED_CONTENT_TYPES = %w[application/octet-stream text/plain].freeze+ BINARY_BYTES = /[\x00-\x08\x0B\x0E-\x1F\x7F]/n+# A misspelled or forgotten option would otherwise leave this validator# silently accepting everything, on the one path whose whole purpose is# refusing hostile files. An allowlist that resolves to empty at runtime is@@ -116,9 +126,16 @@ module Spree# read, which would hand the decision back to whoever uploaded the file.detected = Marcel::MimeType.for(StringIO.new(head))return true if permitted.include?(detected)+ return permitted_plain_text?(blob, head, permitted) if UNIDENTIFIED_CONTENT_TYPES.include?(detected)return false unless CONTAINER_CONTENT_TYPES.include?(detected)- permitted.include?(office_type_from_extension(blob))+ permitted.include?(type_from_extension(blob))+ end++ def permitted_plain_text?(blob, head, permitted)+ text_type = type_from_extension(blob)++ TEXT_CONTENT_TYPES.include?(text_type) && permitted.include?(text_type) && !head.b.match?(BINARY_BYTES)end# Asks the extension alone, rather than re-reading the bytes with a filename@@ -129,7 +146,7 @@ module Spree# bytes we read.## @return [String, nil]- def office_type_from_extension(blob)+ def type_from_extension(blob)filename = blob.filename.to_sreturn if File.extname(filename).blank?diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex c92dcb10ac..5522fd9215 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -1142,7 +1142,7 @@ en:phone: Phonepickup:store_pickup: Store Pickup- po_document_invalid: A purchase order document must be a PDF, image or Word file of %{size} or less.+ po_document_invalid: A purchase order document must be a PDF, image, Word document, spreadsheet or CSV file of %{size} or less.po_document_missing: This order has no attached purchase order document.po_document_too_large: is larger than %{size}.po_document_upload_incomplete: The purchase order document was not uploaded completely. Please try attaching it again.diff --git a/spree/core/spec/validators/spree/attachment_content_type_validator_spec.rb b/spree/core/spec/validators/spree/attachment_content_type_validator_spec.rbindex a9007afc0e..cdcc664cf9 100644--- a/spree/core/spec/validators/spree/attachment_content_type_validator_spec.rb+++ b/spree/core/spec/validators/spree/attachment_content_type_validator_spec.rb@@ -140,6 +140,38 @@ RSpec.describe Spree::AttachmentContentTypeValidator doexpect(record.errors[:document]).to be_emptyend++ context 'against the purchase order allowlist' do+ let(:model_class) do+ Class.new(Spree::Cart) do+ def self.name = 'Spree::Cart'++ has_one_attached :document+ validates_with Spree::AttachmentContentTypeValidator,+ attributes: [:document],+ in: Spree::Purchase::PurchaseOrder::PO_DOCUMENT_CONTENT_TYPES+ end+ end++ it 'accepts a PDF' do+ attach(pdf_bytes, filename: 'po.pdf', content_type: 'application/pdf').validate++ expect(record.errors[:document]).to be_empty+ end++ it 'accepts a spreadsheet' do+ attach(docx_bytes, filename: 'po.xlsx',+ content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet').validate++ expect(record.errors[:document]).to be_empty+ end++ it 'accepts a CSV file' do+ attach("po_number,sku,quantity\r\nPO-1,A1,3\r\n", filename: 'po.csv', content_type: 'text/csv').validate++ expect(record.errors[:document]).to be_empty+ end+ endenddescribe 'when the allowlist is empty' do