You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.2 KiB
121 lines
4.2 KiB
|
|
use serde::Serialize;
|
|
use anyhow::Result;
|
|
use chrono::{DateTime, NaiveDate, Utc};
|
|
use sqlx::SqlitePool;
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
|
#[derive(sqlx::FromRow)]
|
|
pub struct DbVetcoveItemHistoryEntry {
|
|
pub id: i64,
|
|
pub vetcove_entry_id: i64,
|
|
pub vetcove_item_id: i64,
|
|
pub order_date: DateTime<Utc>,
|
|
pub item_name: String,
|
|
pub order_id: String,
|
|
pub units: f64,
|
|
pub quantity: f64,
|
|
pub total_price: String,
|
|
pub unit_price: Option<String>,
|
|
pub list_price: Option<String>,
|
|
pub cost_per_dose: Option<String>,
|
|
pub hospital: Option<String>,
|
|
pub po_number: Option<String>,
|
|
pub supplier: Option<String>,
|
|
pub manufacturer: Option<String>,
|
|
pub manufacturer_number: Option<String>,
|
|
pub primary_category: Option<String>,
|
|
pub secondary_category: Option<String>,
|
|
pub unit_measurement: Option<String>,
|
|
pub item_status: Option<String>,
|
|
pub supplier_sku: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
pub async fn add_vetcove_item_history_entry(db: &SqlitePool,
|
|
vetcove_entry_id: i64,
|
|
vetcove_item_id: i64,
|
|
order_date: NaiveDate,
|
|
item_name: String,
|
|
order_id: String,
|
|
units: f64,
|
|
quantity: f64,
|
|
total_price: String,
|
|
unit_price: Option<String>,
|
|
list_price: Option<String>,
|
|
cost_per_dose: Option<String>,
|
|
hospital: Option<String>,
|
|
po_number: Option<String>,
|
|
supplier: Option<String>,
|
|
manufacturer: Option<String>,
|
|
manufacturer_number: Option<String>,
|
|
primary_category: Option<String>,
|
|
secondary_category: Option<String>,
|
|
unit_measurement: Option<String>,
|
|
item_status: Option<String>,
|
|
supplier_sku: Option<String>)
|
|
-> Result<Option<i64>> {
|
|
|
|
let existing: i64 = sqlx::query_scalar(r#"
|
|
SELECT COUNT(1) FROM VetcoveItemHistory WHERE vetcove_entry_id = ?
|
|
"#).bind(vetcove_entry_id).fetch_one(db).await?;
|
|
|
|
if existing > 0 {
|
|
return Ok(None)
|
|
}
|
|
|
|
let res = sqlx::query(
|
|
r#"
|
|
INSERT INTO VetcoveItemHistory (
|
|
vetcove_entry_id,
|
|
vetcove_item_id,
|
|
order_date,
|
|
item_name,
|
|
order_id,
|
|
units,
|
|
quantity,
|
|
total_price,
|
|
unit_price,
|
|
list_price,
|
|
cost_per_dose,
|
|
hospital,
|
|
po_number,
|
|
supplier,
|
|
manufacturer,
|
|
manufacturer_number,
|
|
primary_category,
|
|
secondary_category,
|
|
unit_measurement,
|
|
item_status,
|
|
supplier_sku )
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
"#)
|
|
.bind(vetcove_entry_id)
|
|
.bind(vetcove_item_id)
|
|
.bind(order_date)
|
|
.bind(item_name)
|
|
.bind(order_id)
|
|
.bind(units)
|
|
.bind(quantity)
|
|
.bind(total_price)
|
|
.bind(unit_price)
|
|
.bind(list_price)
|
|
.bind(cost_per_dose)
|
|
.bind(hospital)
|
|
.bind(po_number)
|
|
.bind(supplier)
|
|
.bind(manufacturer)
|
|
.bind(manufacturer_number)
|
|
.bind(primary_category)
|
|
.bind(secondary_category)
|
|
.bind(unit_measurement)
|
|
.bind(item_status)
|
|
.bind(supplier_sku)
|
|
.execute(db).await?;
|
|
|
|
let new_id = res.last_insert_rowid();
|
|
|
|
Ok(Some(new_id))
|
|
}
|