stateset-db 1.22.0

Database implementations for StateSet iCommerce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! SQLite implementation of backorder repository

use chrono::Utc;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rust_decimal::Decimal;
use stateset_core::{
    AllocateBackorder, AllocationStatus, Backorder, BackorderAllocation, BackorderFilter,
    BackorderFulfillment, BackorderRepository, BackorderStatus, BackorderSummary, CommerceError,
    CreateBackorder, FulfillBackorder, Result, SkuBackorderSummary, UpdateBackorder,
    generate_backorder_number,
};
use uuid::Uuid;

use super::{
    map_db_error, parse_datetime_opt, parse_datetime_opt_row, parse_datetime_row,
    parse_decimal_row, parse_enum_row, parse_uuid_opt_row, parse_uuid_row, sum_decimal_query,
    with_immediate_transaction,
};

fn row_to_backorder_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Backorder> {
    Ok(Backorder {
        id: parse_uuid_row(&row.get::<_, String>(0)?, "backorder", "id")?,
        backorder_number: row.get(1)?,
        order_id: parse_uuid_row(&row.get::<_, String>(2)?, "backorder", "order_id")?,
        order_line_id: parse_uuid_opt_row(
            row.get::<_, Option<String>>(3)?,
            "backorder",
            "order_line_id",
        )?,
        customer_id: parse_uuid_row(&row.get::<_, String>(4)?, "backorder", "customer_id")?,
        sku: row.get(5)?,
        quantity_ordered: parse_decimal_row(
            &row.get::<_, String>(6)?,
            "backorder",
            "quantity_ordered",
        )?,
        quantity_fulfilled: parse_decimal_row(
            &row.get::<_, String>(7)?,
            "backorder",
            "quantity_fulfilled",
        )?,
        quantity_remaining: parse_decimal_row(
            &row.get::<_, String>(8)?,
            "backorder",
            "quantity_remaining",
        )?,
        status: parse_enum_row(&row.get::<_, String>(9)?, "backorder", "status")?,
        priority: parse_enum_row(&row.get::<_, String>(10)?, "backorder", "priority")?,
        expected_date: parse_datetime_opt_row(
            row.get::<_, Option<String>>(11)?,
            "backorder",
            "expected_date",
        )?,
        promised_date: parse_datetime_opt_row(
            row.get::<_, Option<String>>(12)?,
            "backorder",
            "promised_date",
        )?,
        source_location_id: row.get(13)?,
        notes: row.get(14)?,
        created_at: parse_datetime_row(&row.get::<_, String>(15)?, "backorder", "created_at")?,
        updated_at: parse_datetime_row(&row.get::<_, String>(16)?, "backorder", "updated_at")?,
    })
}

#[derive(Debug)]
pub struct SqliteBackorderRepository {
    pool: Pool<SqliteConnectionManager>,
}

impl SqliteBackorderRepository {
    #[must_use]
    pub const fn new(pool: Pool<SqliteConnectionManager>) -> Self {
        Self { pool }
    }

    fn row_to_backorder(&self, row: &rusqlite::Row<'_>) -> rusqlite::Result<Backorder> {
        row_to_backorder_row(row)
    }

    fn row_to_fulfillment(
        &self,
        row: &rusqlite::Row<'_>,
    ) -> rusqlite::Result<BackorderFulfillment> {
        Ok(BackorderFulfillment {
            id: parse_uuid_row(&row.get::<_, String>(0)?, "backorder_fulfillment", "id")?,
            backorder_id: parse_uuid_row(
                &row.get::<_, String>(1)?,
                "backorder_fulfillment",
                "backorder_id",
            )?,
            quantity: parse_decimal_row(
                &row.get::<_, String>(2)?,
                "backorder_fulfillment",
                "quantity",
            )?,
            source_type: parse_enum_row(
                &row.get::<_, String>(3)?,
                "backorder_fulfillment",
                "source_type",
            )?,
            source_id: parse_uuid_opt_row(
                row.get::<_, Option<String>>(4)?,
                "backorder_fulfillment",
                "source_id",
            )?,
            notes: row.get(5)?,
            fulfilled_at: parse_datetime_row(
                &row.get::<_, String>(6)?,
                "backorder_fulfillment",
                "fulfilled_at",
            )?,
            fulfilled_by: row.get(7)?,
        })
    }

    fn row_to_allocation(&self, row: &rusqlite::Row<'_>) -> rusqlite::Result<BackorderAllocation> {
        Ok(BackorderAllocation {
            id: parse_uuid_row(&row.get::<_, String>(0)?, "backorder_allocation", "id")?,
            backorder_id: parse_uuid_row(
                &row.get::<_, String>(1)?,
                "backorder_allocation",
                "backorder_id",
            )?,
            sku: row.get(2)?,
            quantity: parse_decimal_row(
                &row.get::<_, String>(3)?,
                "backorder_allocation",
                "quantity",
            )?,
            location_id: row.get(4)?,
            lot_id: parse_uuid_opt_row(
                row.get::<_, Option<String>>(5)?,
                "backorder_allocation",
                "lot_id",
            )?,
            status: parse_enum_row(&row.get::<_, String>(6)?, "backorder_allocation", "status")?,
            allocated_at: parse_datetime_row(
                &row.get::<_, String>(7)?,
                "backorder_allocation",
                "allocated_at",
            )?,
            expires_at: parse_datetime_opt_row(
                row.get::<_, Option<String>>(8)?,
                "backorder_allocation",
                "expires_at",
            )?,
        })
    }
}

pub(crate) fn create_backorder_in_tx(
    tx: &rusqlite::Transaction<'_>,
    input: &CreateBackorder,
) -> std::result::Result<Backorder, rusqlite::Error> {
    let id = Uuid::new_v4();
    let now = Utc::now();
    let backorder_number = generate_backorder_number();
    let priority = input.priority.unwrap_or_default();

    tx.execute(
        "INSERT INTO backorders (id, backorder_number, order_id, order_line_id, customer_id,
            sku, quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
            expected_date, promised_date, source_location_id, notes, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, '0', ?, 'pending', ?, ?, ?, ?, ?, ?, ?)",
        rusqlite::params![
            id.to_string(),
            &backorder_number,
            input.order_id.to_string(),
            input.order_line_id.map(|id| id.to_string()),
            input.customer_id.to_string(),
            &input.sku,
            input.quantity.to_string(),
            input.quantity.to_string(),
            priority.to_string(),
            input.expected_date.map(|d| d.to_rfc3339()),
            input.promised_date.map(|d| d.to_rfc3339()),
            input.source_location_id,
            input.notes,
            now.to_rfc3339(),
            now.to_rfc3339(),
        ],
    )?;

    let row = tx.query_row(
        "SELECT id, backorder_number, order_id, order_line_id, customer_id, sku,
                quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                expected_date, promised_date, source_location_id, notes, created_at, updated_at
         FROM backorders WHERE id = ?",
        [id.to_string()],
        row_to_backorder_row,
    );

    match row {
        Ok(bo) => Ok(bo),
        Err(rusqlite::Error::QueryReturnedNoRows) => {
            Err(rusqlite::Error::ToSqlConversionFailure(Box::new(CommerceError::NotFound)))
        }
        Err(e) => Err(e),
    }
}

pub(crate) fn cancel_backorders_for_order_in_tx(
    tx: &rusqlite::Transaction<'_>,
    order_id: Uuid,
) -> std::result::Result<(), rusqlite::Error> {
    let now = Utc::now();

    tx.execute(
        "UPDATE backorders SET status = 'cancelled', updated_at = ? WHERE order_id = ?",
        [now.to_rfc3339(), order_id.to_string()],
    )?;

    tx.execute(
        "UPDATE backorder_allocations SET status = 'released'
         WHERE backorder_id IN (SELECT id FROM backorders WHERE order_id = ?)
           AND status = 'reserved'",
        [order_id.to_string()],
    )?;

    Ok(())
}

impl BackorderRepository for SqliteBackorderRepository {
    fn create_backorder(&self, input: CreateBackorder) -> Result<Backorder> {
        let id = Uuid::new_v4();
        let now = Utc::now();
        let backorder_number = generate_backorder_number();
        let priority = input.priority.unwrap_or_default();

        {
            let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
            conn.execute(
                "INSERT INTO backorders (id, backorder_number, order_id, order_line_id, customer_id,
                    sku, quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                    expected_date, promised_date, source_location_id, notes, created_at, updated_at)
                 VALUES (?, ?, ?, ?, ?, ?, ?, '0', ?, 'pending', ?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    id.to_string(),
                    &backorder_number,
                    input.order_id.to_string(),
                    input.order_line_id.map(|id| id.to_string()),
                    input.customer_id.to_string(),
                    &input.sku,
                    input.quantity.to_string(),
                    input.quantity.to_string(), // quantity_remaining starts same as ordered
                    priority.to_string(),
                    input.expected_date.map(|d| d.to_rfc3339()),
                    input.promised_date.map(|d| d.to_rfc3339()),
                    input.source_location_id,
                    input.notes,
                    now.to_rfc3339(),
                    now.to_rfc3339(),
                ],
            ).map_err(map_db_error)?;
        }

        self.get_backorder(id)?.ok_or(CommerceError::NotFound)
    }

    fn get_backorder(&self, id: Uuid) -> Result<Option<Backorder>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let result = conn.query_row(
            "SELECT id, backorder_number, order_id, order_line_id, customer_id, sku,
                    quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                    expected_date, promised_date, source_location_id, notes, created_at, updated_at
             FROM backorders WHERE id = ?",
            [id.to_string()],
            |row| self.row_to_backorder(row),
        );

        match result {
            Ok(bo) => Ok(Some(bo)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(map_db_error(e)),
        }
    }

    fn get_backorder_by_number(&self, number: &str) -> Result<Option<Backorder>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let result = conn.query_row(
            "SELECT id, backorder_number, order_id, order_line_id, customer_id, sku,
                    quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                    expected_date, promised_date, source_location_id, notes, created_at, updated_at
             FROM backorders WHERE backorder_number = ?",
            [number],
            |row| self.row_to_backorder(row),
        );

        match result {
            Ok(bo) => Ok(Some(bo)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(map_db_error(e)),
        }
    }

    fn update_backorder(&self, id: Uuid, input: UpdateBackorder) -> Result<Backorder> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let now = Utc::now();

        conn.execute(
            "UPDATE backorders SET
                priority = COALESCE(?, priority),
                expected_date = COALESCE(?, expected_date),
                promised_date = COALESCE(?, promised_date),
                source_location_id = COALESCE(?, source_location_id),
                notes = COALESCE(?, notes),
                updated_at = ?
             WHERE id = ?",
            rusqlite::params![
                input.priority.map(|p| p.to_string()),
                input.expected_date.map(|d| d.to_rfc3339()),
                input.promised_date.map(|d| d.to_rfc3339()),
                input.source_location_id,
                input.notes,
                now.to_rfc3339(),
                id.to_string(),
            ],
        )
        .map_err(map_db_error)?;

        self.get_backorder(id)?.ok_or(CommerceError::NotFound)
    }

    fn list_backorders(&self, filter: BackorderFilter) -> Result<Vec<Backorder>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let mut sql = String::from(
            "SELECT id, backorder_number, order_id, order_line_id, customer_id, sku,
                    quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                    expected_date, promised_date, source_location_id, notes, created_at, updated_at
             FROM backorders WHERE 1=1",
        );
        let mut params: Vec<Box<dyn rusqlite::ToSql>> = Vec::new();

        if let Some(ref order_id) = filter.order_id {
            sql.push_str(" AND order_id = ?");
            params.push(Box::new(order_id.to_string()));
        }
        if let Some(ref customer_id) = filter.customer_id {
            sql.push_str(" AND customer_id = ?");
            params.push(Box::new(customer_id.to_string()));
        }
        if let Some(ref sku) = filter.sku {
            sql.push_str(" AND sku = ?");
            params.push(Box::new(sku.clone()));
        }
        if let Some(ref status) = filter.status {
            sql.push_str(" AND status = ?");
            params.push(Box::new(status.to_string()));
        }
        if let Some(ref priority) = filter.priority {
            sql.push_str(" AND priority = ?");
            params.push(Box::new(priority.to_string()));
        }

        // Order by priority (critical first) then by created_at
        sql.push_str(" ORDER BY CASE priority WHEN 'critical' THEN 1 WHEN 'high' THEN 2 WHEN 'normal' THEN 3 ELSE 4 END, created_at ASC");

        if let Some(limit) = filter.limit {
            sql.push_str(&format!(" LIMIT {limit}"));
        }

        let param_refs: Vec<&dyn rusqlite::ToSql> =
            params.iter().map(std::convert::AsRef::as_ref).collect();
        let mut stmt = conn.prepare(&sql).map_err(map_db_error)?;
        let rows = stmt
            .query_map(param_refs.as_slice(), |row| self.row_to_backorder(row))
            .map_err(map_db_error)?;

        let mut backorders = Vec::new();
        for row in rows {
            backorders.push(row.map_err(map_db_error)?);
        }
        Ok(backorders)
    }

    fn cancel_backorder(&self, id: Uuid) -> Result<Backorder> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let now = Utc::now();

        conn.execute(
            "UPDATE backorders SET status = 'cancelled', updated_at = ? WHERE id = ?",
            [now.to_rfc3339(), id.to_string()],
        )
        .map_err(map_db_error)?;

        // Release any allocations
        conn.execute(
            "UPDATE backorder_allocations SET status = 'released' WHERE backorder_id = ? AND status = 'reserved'",
            [id.to_string()],
        ).map_err(map_db_error)?;

        self.get_backorder(id)?.ok_or(CommerceError::NotFound)
    }

    fn get_backorders_for_order(&self, order_id: Uuid) -> Result<Vec<Backorder>> {
        self.list_backorders(BackorderFilter { order_id: Some(order_id), ..Default::default() })
    }

    fn get_backorders_for_customer(&self, customer_id: Uuid) -> Result<Vec<Backorder>> {
        self.list_backorders(BackorderFilter {
            customer_id: Some(customer_id),
            ..Default::default()
        })
    }

    fn get_backorders_for_sku(&self, sku: &str) -> Result<Vec<Backorder>> {
        self.list_backorders(BackorderFilter { sku: Some(sku.to_string()), ..Default::default() })
    }

    fn fulfill_backorder(&self, input: FulfillBackorder) -> Result<Backorder> {
        if input.quantity <= Decimal::ZERO {
            return Err(CommerceError::ValidationError(
                "Fulfillment quantity must be greater than zero".to_string(),
            ));
        }

        let now = Utc::now();
        let fulfillment_id = Uuid::new_v4();
        let backorder_id_str = input.backorder_id.to_string();

        // The status/quantity read, the guards, the fulfillment INSERT and the
        // backorder UPDATE all run inside ONE `IMMEDIATE` transaction, so
        // concurrent fulfillments serialize (no lost update / over-fulfill) and a
        // failed UPDATE can't leave an orphaned fulfillment row.
        with_immediate_transaction(&self.pool, |tx| {
            let (status_str, remaining_str, fulfilled_str): (String, String, String) = tx
                .query_row(
                    "SELECT status, quantity_remaining, quantity_fulfilled FROM backorders WHERE id = ?",
                    [&backorder_id_str],
                    |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
                )
                .map_err(|e| match e {
                    rusqlite::Error::QueryReturnedNoRows => {
                        rusqlite::Error::ToSqlConversionFailure(Box::new(CommerceError::NotFound))
                    }
                    other => other,
                })?;
            let status: BackorderStatus = parse_enum_row(&status_str, "backorder", "status")?;
            let remaining = parse_decimal_row(&remaining_str, "backorder", "quantity_remaining")?;
            let fulfilled = parse_decimal_row(&fulfilled_str, "backorder", "quantity_fulfilled")?;

            // A cancelled or already-fulfilled backorder cannot be fulfilled.
            if matches!(status, BackorderStatus::Cancelled | BackorderStatus::Fulfilled) {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError("Backorder cannot be fulfilled".to_string()),
                )));
            }
            // Cannot fulfill more units than remain.
            if input.quantity > remaining {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError(format!(
                        "Cannot fulfill {} - only {} remaining",
                        input.quantity, remaining
                    )),
                )));
            }

            let new_fulfilled = fulfilled + input.quantity;
            let new_remaining = remaining - input.quantity;
            let new_status = if new_remaining <= Decimal::ZERO {
                BackorderStatus::Fulfilled
            } else {
                BackorderStatus::PartiallyFulfilled
            };

            tx.execute(
                "INSERT INTO backorder_fulfillments (id, backorder_id, quantity, source_type,
                    source_id, notes, fulfilled_at, fulfilled_by)
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    fulfillment_id.to_string(),
                    backorder_id_str,
                    input.quantity.to_string(),
                    input.source_type.to_string(),
                    input.source_id.map(|id| id.to_string()),
                    input.notes.as_deref(),
                    now.to_rfc3339(),
                    input.fulfilled_by.as_deref(),
                ],
            )?;

            tx.execute(
                "UPDATE backorders SET quantity_fulfilled = ?, quantity_remaining = ?, status = ?, updated_at = ?
                 WHERE id = ?",
                rusqlite::params![
                    new_fulfilled.to_string(),
                    new_remaining.to_string(),
                    new_status.to_string(),
                    now.to_rfc3339(),
                    backorder_id_str,
                ],
            )?;
            Ok(())
        })?;

        self.get_backorder(input.backorder_id)?.ok_or(CommerceError::NotFound)
    }

    fn get_fulfillment_history(&self, backorder_id: Uuid) -> Result<Vec<BackorderFulfillment>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let mut stmt = conn.prepare(
            "SELECT id, backorder_id, quantity, source_type, source_id, notes, fulfilled_at, fulfilled_by
             FROM backorder_fulfillments WHERE backorder_id = ? ORDER BY fulfilled_at"
        ).map_err(map_db_error)?;

        let rows = stmt
            .query_map([backorder_id.to_string()], |row| self.row_to_fulfillment(row))
            .map_err(map_db_error)?;

        let mut fulfillments = Vec::new();
        for row in rows {
            fulfillments.push(row.map_err(map_db_error)?);
        }
        Ok(fulfillments)
    }

    fn allocate_backorder(&self, input: AllocateBackorder) -> Result<BackorderAllocation> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let id = Uuid::new_v4();
        let now = Utc::now();

        let backorder = self.get_backorder(input.backorder_id)?.ok_or(CommerceError::NotFound)?;

        conn.execute(
            "INSERT INTO backorder_allocations (id, backorder_id, sku, quantity, location_id,
                lot_id, status, allocated_at, expires_at)
             VALUES (?, ?, ?, ?, ?, ?, 'reserved', ?, ?)",
            rusqlite::params![
                id.to_string(),
                input.backorder_id.to_string(),
                &backorder.sku,
                input.quantity.to_string(),
                input.location_id,
                input.lot_id.map(|id| id.to_string()),
                now.to_rfc3339(),
                input.expires_at.map(|d| d.to_rfc3339()),
            ],
        )
        .map_err(map_db_error)?;

        // Update backorder status to allocated
        conn.execute(
            "UPDATE backorders SET status = 'allocated', updated_at = ?
             WHERE id = ? AND status = 'pending'",
            [now.to_rfc3339(), input.backorder_id.to_string()],
        )
        .map_err(map_db_error)?;

        Ok(BackorderAllocation {
            id,
            backorder_id: input.backorder_id,
            sku: backorder.sku,
            quantity: input.quantity,
            location_id: input.location_id,
            lot_id: input.lot_id,
            status: AllocationStatus::Reserved,
            allocated_at: now,
            expires_at: input.expires_at,
        })
    }

    fn get_allocations(&self, backorder_id: Uuid) -> Result<Vec<BackorderAllocation>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let mut stmt = conn.prepare(
            "SELECT id, backorder_id, sku, quantity, location_id, lot_id, status, allocated_at, expires_at
             FROM backorder_allocations WHERE backorder_id = ? AND status = 'reserved'"
        ).map_err(map_db_error)?;

        let rows = stmt
            .query_map([backorder_id.to_string()], |row| self.row_to_allocation(row))
            .map_err(map_db_error)?;

        let mut allocations = Vec::new();
        for row in rows {
            allocations.push(row.map_err(map_db_error)?);
        }
        Ok(allocations)
    }

    fn release_allocation(&self, allocation_id: Uuid) -> Result<BackorderAllocation> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;

        conn.execute(
            "UPDATE backorder_allocations SET status = 'released' WHERE id = ?",
            [allocation_id.to_string()],
        )
        .map_err(map_db_error)?;

        let result = conn.query_row(
            "SELECT id, backorder_id, sku, quantity, location_id, lot_id, status, allocated_at, expires_at
             FROM backorder_allocations WHERE id = ?",
            [allocation_id.to_string()],
            |row| self.row_to_allocation(row),
        ).map_err(map_db_error)?;

        Ok(result)
    }

    fn confirm_allocation(&self, allocation_id: Uuid) -> Result<BackorderAllocation> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;

        conn.execute(
            "UPDATE backorder_allocations SET status = 'confirmed' WHERE id = ?",
            [allocation_id.to_string()],
        )
        .map_err(map_db_error)?;

        let result = conn.query_row(
            "SELECT id, backorder_id, sku, quantity, location_id, lot_id, status, allocated_at, expires_at
             FROM backorder_allocations WHERE id = ?",
            [allocation_id.to_string()],
            |row| self.row_to_allocation(row),
        ).map_err(map_db_error)?;

        Ok(result)
    }

    fn expire_allocations(&self) -> Result<u32> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let now = Utc::now();

        let count = conn
            .execute(
                "UPDATE backorder_allocations SET status = 'expired'
             WHERE status = 'reserved' AND expires_at IS NOT NULL AND expires_at < ?",
                [now.to_rfc3339()],
            )
            .map_err(map_db_error)?;

        Ok(count as u32)
    }

    fn auto_allocate_inventory(&self, sku: &str) -> Result<Vec<BackorderAllocation>> {
        // Get pending backorders for this SKU ordered by priority
        let _backorders = self.list_backorders(BackorderFilter {
            sku: Some(sku.to_string()),
            status: Some(BackorderStatus::Pending),
            ..Default::default()
        })?;

        // Note: In a real implementation, we would check inventory availability
        // and create allocations. For now, return empty as this requires integration
        // with the inventory module.
        Ok(Vec::new())
    }

    fn get_summary(&self) -> Result<BackorderSummary> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let now = Utc::now();

        let (total, pending, allocated, critical): (i32, i32, i32, i32) = conn
            .query_row(
                "SELECT
                    COUNT(*),
                    SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END),
                    SUM(CASE WHEN status = 'allocated' THEN 1 ELSE 0 END),
                    SUM(CASE WHEN priority = 'critical' THEN 1 ELSE 0 END)
                 FROM backorders WHERE status NOT IN ('fulfilled', 'cancelled')",
                [],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
            )
            .map_err(map_db_error)?;

        let overdue: i32 = conn
            .query_row(
                "SELECT COUNT(*) FROM backorders
                 WHERE status NOT IN ('fulfilled', 'cancelled')
                 AND expected_date IS NOT NULL AND expected_date < ?",
                [now.to_rfc3339()],
                |row| row.get(0),
            )
            .map_err(map_db_error)?;

        let total_quantity = sum_decimal_query(
            &conn,
            "SELECT quantity_remaining FROM backorders WHERE status NOT IN ('fulfilled', 'cancelled')",
            &[],
            "backorders",
            "quantity_remaining",
        )?;

        Ok(BackorderSummary {
            total_backorders: total,
            total_quantity,
            pending_count: pending,
            allocated_count: allocated,
            critical_count: critical,
            overdue_count: overdue,
        })
    }

    fn get_sku_summary(&self, sku: &str) -> Result<Option<SkuBackorderSummary>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;

        let (count, oldest_date_raw, earliest_expected_raw): (i32, Option<String>, Option<String>) =
            conn.query_row(
                "SELECT COUNT(*), MIN(created_at), MIN(expected_date)
                 FROM backorders
                 WHERE sku = ? AND status NOT IN ('fulfilled', 'cancelled')",
                [sku],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
            )
            .map_err(map_db_error)?;

        if count == 0 {
            return Ok(None);
        }

        let sku_param = sku.to_string();
        let sku_params: [&dyn rusqlite::ToSql; 1] = [&sku_param];
        let total_quantity = sum_decimal_query(
            &conn,
            "SELECT quantity_remaining FROM backorders WHERE sku = ? AND status NOT IN ('fulfilled', 'cancelled')",
            &sku_params,
            "backorders",
            "quantity_remaining",
        )?;
        let oldest_date =
            parse_datetime_opt(oldest_date_raw, "sku_backorder_summary", "oldest_date")?;
        let earliest_expected = parse_datetime_opt(
            earliest_expected_raw,
            "sku_backorder_summary",
            "earliest_expected",
        )?;

        Ok(Some(SkuBackorderSummary {
            sku: sku.to_string(),
            total_quantity,
            backorder_count: count,
            oldest_date,
            earliest_expected,
        }))
    }

    fn get_overdue_backorders(&self) -> Result<Vec<Backorder>> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let now = Utc::now();

        let mut stmt = conn
            .prepare(
                "SELECT id, backorder_number, order_id, order_line_id, customer_id, sku,
                    quantity_ordered, quantity_fulfilled, quantity_remaining, status, priority,
                    expected_date, promised_date, source_location_id, notes, created_at, updated_at
             FROM backorders
             WHERE status NOT IN ('fulfilled', 'cancelled')
             AND expected_date IS NOT NULL AND expected_date < ?
             ORDER BY expected_date",
            )
            .map_err(map_db_error)?;

        let rows = stmt
            .query_map([now.to_rfc3339()], |row| self.row_to_backorder(row))
            .map_err(map_db_error)?;

        let mut backorders = Vec::new();
        for row in rows {
            backorders.push(row.map_err(map_db_error)?);
        }
        Ok(backorders)
    }

    fn count_pending(&self) -> Result<u64> {
        let conn = self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))?;
        let count: i64 = conn
            .query_row("SELECT COUNT(*) FROM backorders WHERE status = 'pending'", [], |row| {
                row.get(0)
            })
            .map_err(map_db_error)?;
        Ok(count as u64)
    }
}