parent
12d8bc0e21
commit
5c0e250a36
@ -0,0 +1,17 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="HtmlUnknownAttribute" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="myValues">
|
||||
<value>
|
||||
<list size="3">
|
||||
<item index="0" class="java.lang.String" itemvalue="x-show" />
|
||||
<item index="1" class="java.lang.String" itemvalue="x-model" />
|
||||
<item index="2" class="java.lang.String" itemvalue="x-data" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
<option name="myCustomValuesEnabled" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<file url="file://$PROJECT_DIR$" libraries="{alpinejs}" />
|
||||
</component>
|
||||
</project>
|
||||
@ -1,12 +0,0 @@
|
||||
use askama::Template;
|
||||
use askama_axum::{IntoResponse, Response};
|
||||
use crate::error::{AppError};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "audit.html")]
|
||||
struct AuditLogTemplate;
|
||||
|
||||
|
||||
pub async fn audit_log_handler() -> Result<Response, AppError> {
|
||||
Ok(AuditLogTemplate.into_response())
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
use askama::Template;
|
||||
use askama_axum::{IntoResponse, Response};
|
||||
use axum::extract::State;
|
||||
use axum_htmx::HxRequest;
|
||||
use serde::Deserialize;
|
||||
use sqlx::SqlitePool;
|
||||
use tracing::info;
|
||||
use chrono::prelude::*;
|
||||
use crate::db::positive_adjustment::{get_positive_adjustments_target_date_range, DbPositiveAdjustment};
|
||||
use crate::error::{AppError, QueryExtractor};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "history.html")]
|
||||
struct HistoryLogTemplate {
|
||||
items: Vec<DbPositiveAdjustment>,
|
||||
start_date: String,
|
||||
start_time: String,
|
||||
end_date: String,
|
||||
end_time: String,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "history_item_fragment.html")]
|
||||
struct HistoryLogItemFragmentTemplate {
|
||||
items: Vec<DbPositiveAdjustment>
|
||||
}
|
||||
|
||||
/// Common query args for datetime ranges
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct DatetimeRangeQueryArgs {
|
||||
#[serde(rename = "start-date", alias = "sd")]
|
||||
pub start_date: Option<String>,
|
||||
#[serde(rename = "start-time", alias = "st")]
|
||||
pub start_time: Option<String>,
|
||||
#[serde(rename = "end-date", alias = "ed")]
|
||||
pub end_date: Option<String>,
|
||||
#[serde(rename = "end-time", alias = "et")]
|
||||
pub end_time: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn history_log_handler(
|
||||
QueryExtractor(query): QueryExtractor<DatetimeRangeQueryArgs>,
|
||||
HxRequest(hx_request): HxRequest,
|
||||
State(db): State<SqlitePool>
|
||||
) -> Result<Response, AppError> {
|
||||
|
||||
let today = Local::now().naive_local().date();
|
||||
|
||||
let start_date = query.start_date.unwrap_or("2000-01-01".to_string());
|
||||
let start_time = query.start_time.unwrap_or("00:00:00".to_string());
|
||||
let end_date = query.end_date.unwrap_or(today.to_string());
|
||||
let end_time = query.end_time.unwrap_or("11:59:59".to_string());
|
||||
let timezone = FixedOffset::west_opt(6 * 3600)
|
||||
.ok_or(anyhow::anyhow!("Invalid timezone"))?;
|
||||
|
||||
let naive_start_date = start_date.parse::<NaiveDate>()?;
|
||||
let naive_start_time = start_time.parse::<NaiveTime>()?;
|
||||
let naive_end_date = end_date.parse::<NaiveDate>()?;
|
||||
let naive_end_time = end_time.parse::<NaiveTime>()?;
|
||||
|
||||
let combined_start = naive_start_date
|
||||
.and_time(naive_start_time)
|
||||
.and_local_timezone(timezone)
|
||||
.earliest()
|
||||
.ok_or(anyhow::anyhow!("Invalid start"))?
|
||||
.to_utc();
|
||||
|
||||
let combined_end = naive_end_date
|
||||
.and_time(naive_end_time)
|
||||
.and_local_timezone(timezone)
|
||||
.latest()
|
||||
.ok_or(anyhow::anyhow!("Invalid start"))?
|
||||
.to_utc();
|
||||
|
||||
|
||||
info!("Get items from: {} to {}", combined_start, combined_end);
|
||||
|
||||
let items = get_positive_adjustments_target_date_range(&db,
|
||||
combined_start, combined_end).await?;
|
||||
|
||||
info!("Item count: {}", items.len());
|
||||
|
||||
if hx_request {
|
||||
Ok(HistoryLogItemFragmentTemplate {items}.into_response())
|
||||
}
|
||||
else {
|
||||
Ok(HistoryLogTemplate {
|
||||
items,
|
||||
start_date,
|
||||
start_time,
|
||||
end_date,
|
||||
end_time,
|
||||
}.into_response())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
use serde::Serialize;
|
||||
use sqlx::SqlitePool;
|
||||
use anyhow::Result;
|
||||
use chrono::{DateTime, Utc};
|
||||
use tracing::error;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[derive(sqlx::FromRow)]
|
||||
pub struct DbNegativeAdjustment {
|
||||
pub id: i64,
|
||||
pub item: i64,
|
||||
pub user: i64,
|
||||
pub create_date: i64,
|
||||
pub target_date: i64,
|
||||
pub amount: f64,
|
||||
pub reason: DbNegativeAdjustmentReason,
|
||||
}
|
||||
|
||||
pub async fn add_negative_adjustment(db: &SqlitePool, item: i64, user: i64,
|
||||
create_date: DateTime<Utc>, target_date: DateTime<Utc>,
|
||||
amount: f64, reason: DbNegativeAdjustmentReason) -> Result<i64> {
|
||||
let reason: i64 = reason.into();
|
||||
let res = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO NegativeAdjustment (item, user, create_date, target_date, amount, reason)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
"#,
|
||||
item, user, create_date, target_date, amount, reason
|
||||
).execute(db).await?;
|
||||
|
||||
let new_id = res.last_insert_rowid();
|
||||
|
||||
Ok(new_id)
|
||||
}
|
||||
|
||||
pub async fn get_negative_adjustments_target_date_range(
|
||||
db: &SqlitePool, start_date: DateTime<Utc>, end_date: DateTime<Utc>
|
||||
) -> Result<Vec<DbNegativeAdjustment>> {
|
||||
sqlx::query_as!(
|
||||
DbNegativeAdjustment,
|
||||
r#"
|
||||
SELECT id, item, user, create_date, target_date, amount, reason
|
||||
FROM NegativeAdjustment
|
||||
WHERE target_date >= ? AND target_date <= ?
|
||||
"#,
|
||||
start_date, end_date
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await.map_err(Into::into)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize)]
|
||||
pub enum DbNegativeAdjustmentReason {
|
||||
Unknown,
|
||||
Sale,
|
||||
Destruction,
|
||||
Expiration,
|
||||
Theft,
|
||||
}
|
||||
|
||||
impl Into<i64> for DbNegativeAdjustmentReason {
|
||||
fn into(self) -> i64 {
|
||||
match self {
|
||||
Self::Unknown => 0,
|
||||
Self::Sale => 10,
|
||||
Self::Destruction => 20,
|
||||
Self::Expiration => 25,
|
||||
Self::Theft => 30,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for DbNegativeAdjustmentReason {
|
||||
fn from(item: i64) -> Self {
|
||||
match item {
|
||||
0 => Self::Unknown,
|
||||
10 => Self::Sale,
|
||||
20 => Self::Destruction,
|
||||
25 => Self::Expiration,
|
||||
30 => Self::Theft,
|
||||
_ => {
|
||||
error!("unknown negative adjustment reason value: {}", item);
|
||||
Self::Unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for DbNegativeAdjustmentReason {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
|
||||
match value {
|
||||
"unknown" => Ok(Self::Unknown),
|
||||
"sale" => Ok(Self::Sale),
|
||||
"destruction" => Ok(Self::Destruction),
|
||||
"expiration" => Ok(Self::Expiration),
|
||||
"theft" => Ok(Self::Theft),
|
||||
_ => Err(anyhow::anyhow!("unknown negative adjustment reason"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<String> for DbNegativeAdjustmentReason {
|
||||
fn into(self) -> String {
|
||||
match self {
|
||||
Self::Unknown => { String::from("unknown") }
|
||||
Self::Sale => { String::from("sale") }
|
||||
Self::Destruction => { String::from("destruction") }
|
||||
Self::Expiration => { String::from("expiration") }
|
||||
Self::Theft => { String::from("theft") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
{% extends "main.html" %}
|
||||
|
||||
{% block title %} Audit Log {% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Audit Log (Coming soon)</h1>
|
||||
|
||||
{% endblock %}
|
||||
@ -0,0 +1,37 @@
|
||||
{% extends "main.html" %}
|
||||
|
||||
{% block title %} Audit Log {% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1>Audit Log (Coming soon)</h1>
|
||||
|
||||
<form action="/history" hx-get="/history" hx-trigger="change" hx-target="#items">
|
||||
<div class="grid">
|
||||
<fieldset>
|
||||
<label for="start-date">Start Date</label>
|
||||
<input type="date" id="start-date" name="start" value="{{ start_date }}" />
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<label for="start-time">Start Time</label>
|
||||
<input type="time" id="start-time" name="start" value="{{ start_time }}"/>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
<div class="grid">
|
||||
<fieldset>
|
||||
<label for="end-date">End Date</label>
|
||||
<input type="date" id="end-date" name="end" value="{{ end_date }}"/>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<label for="end-time">End Time</label>
|
||||
<input type="time" id="end-time" name="end" value="{{ end_time }}"/>
|
||||
</fieldset>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div id="items" class="container">
|
||||
{% include "history_item_fragment.html" %}
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@ -0,0 +1,9 @@
|
||||
|
||||
{% for item in items %}
|
||||
<article id="item-{{item.id}}-card">
|
||||
<div class="grid">
|
||||
<p>{{ item.item }}</p>
|
||||
<p>{{ item.amount }}</p>
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
Loading…
Reference in new issue