Legacy Order Import - Quick Reference Guide

Prerequisites

  1. Import Customers First

    bin/rails legacy:import_customers
    

    This creates companies with "Legacy ID: XXX" in notes for mapping.

  2. Verify Vendors & Products Exist

Available Commands

1. Test Legacy Database Connection

bin/rails legacy:test

Shows if you can connect to the legacy MySQL database.

2. View Legacy Data Statistics

bin/rails legacy:stats

Shows order counts, status breakdown, top vendors, recent orders.

3. Preview Order Import

# Preview all orders from last 2 years
bin/rails legacy:preview_order_import

# Preview specific date range
bin/rails legacy:preview_order_import[2024-01-01,2025-12-31]

This shows:

4. Dry Run Import

# Test import without saving (default is dry run)
bin/rails legacy:import_orders[2024-01-01,2025-12-31]

# Or explicitly
bin/rails legacy:import_orders[2024-01-01,2025-12-31,true]

Shows exactly what would happen without actually importing.

5. Actual Import

# CAUTION: This actually imports data!
bin/rails legacy:import_orders[2024-01-01,2025-12-31,false]

6. Generate Uninvoiced Orders Report

bin/rails legacy:uninvoiced_orders_report

Creates a CSV file with all imported orders that need invoicing.

Import Process Flow

1. Connect to Legacy DB
   ↓
2. Map Vendors (by name)
   ↓
3. Map Products (by name)
   ↓
4. Map Customers (by legacy ID in notes)
   ↓
5. For Each Order:
   - Check if already imported (skip if yes)
   - Map status (delivered → active, etc.)
   - Map payment method (cc → credit_card, etc.)
   - Calculate rental days from dates
   - Create company/contact if needed
   - Import order with all fields
   - Track with legacy_order_id
   ↓
6. Generate Summary Report

What Gets Imported

Order Fields Mapped:

Tracking Fields Added:

After Import

Review Imported Orders

# In Rails console
bin/rails console

# See all imported orders
Order.where.not(legacy_order_id: nil).count

# See uninvoiced imported orders
Order.where.not(legacy_order_id: nil).where(invoice_id: nil).count

# Find a specific legacy order
Order.find_by(legacy_order_id: 12345)

Generate Invoices

  1. Run uninvoiced report:

    bin/rails legacy:uninvoiced_orders_report
    
  2. Review the CSV file

  3. Use the billing run system to create invoices:

Rollback

If something goes wrong:

# In Rails console
bin/rails console

# Delete ALL imported orders
Order.where.not(legacy_order_id: nil).delete_all

# Delete orders imported after a certain time
Order.where.not(legacy_order_id: nil)
     .where("imported_at > ?", 1.hour.ago)
     .delete_all

# Then re-run import with corrections

Troubleshooting

"Can't connect to server on '127.0.0.1'"

"Vendor/Product not found"

"Customer not found"

"Already imported" messages

Best Practices

  1. Start Small

  2. Always Use Dry Run First

    bin/rails legacy:import_orders[2024-01-01,2024-03-31]
    # Review output, then:
    bin/rails legacy:import_orders[2024-01-01,2024-03-31,false]
    
  3. Import in Batches

  4. Focus on Uninvoiced First

  5. Verify After Import

Data Mapping Reference

Status Mapping

Legacy New Invoiceable?
pending pending No
confirmed confirmed No
delivered active No
active active Yes
completed completed Yes
cancelled cancelled No

Payment Method Mapping

Legacy New
cc, credit_card credit_card
po, purchase_order purchase_order
cash purchase_order
check purchase_order

Support

For issues or questions:

  1. Check import_notes field on orders
  2. Review error messages in import output
  3. Use Rails console to inspect data
  4. Check docs/legacy_order_import.md for detailed strategy

Example Workflow

# 1. Test connection
bin/rails legacy:test

# 2. Import customers (if not done)
bin/rails legacy:import_customers

# 3. Preview what would be imported
bin/rails legacy:preview_order_import[2024-01-01,2024-12-31]

# 4. Dry run to see details
bin/rails legacy:import_orders[2024-01-01,2024-12-31]

# 5. Review output, then actually import
bin/rails legacy:import_orders[2024-01-01,2024-12-31,false]

# 6. Generate uninvoiced report
bin/rails legacy:uninvoiced_orders_report

# 7. Open the CSV and review
# 8. Create invoices via billing runs in admin UI