Skip to content

Commit

Permalink
feat: order support non payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Reknij committed Jun 6, 2024
1 parent 91e96cd commit 1c78529
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct GuestOrder {
pub order_id: i64,
#[serde(default)]
pub is_record: bool,
#[serde(default)]
pub non_payment: bool,

pub order_category_id: i64,
#[serde(default)]
Expand All @@ -79,6 +81,7 @@ pub struct GetGuestOrdersQuery {
pub person_in_charge_id: Option<i64>,
pub order_type: Option<OrderType>,
pub is_record: Option<bool>,
pub non_payment: Option<bool>,
pub currency: Option<OrderCurrency>,
pub date_start: Option<i64>,
pub date_end: Option<i64>,
Expand Down Expand Up @@ -111,6 +114,7 @@ impl From<GuestOrder> for Order {
description: value.description,
order_type: value.order_type,
is_record: value.is_record,
non_payment: value.non_payment,
}
}
}
Expand All @@ -134,6 +138,7 @@ impl From<Order> for GuestOrder {
confirmed_date: value.date,
order_category_id: value.order_category_id,
is_record: value.is_record,
non_payment: value.non_payment,
}
}
}
Expand Down Expand Up @@ -176,6 +181,10 @@ impl GetGuestOrdersQuery {
let eq = eq_or_not(reverse, "is_record");
conditions.push(format!("guest_orders.is_record{eq}{v}"));
}
if let Some(v) = &self.non_payment {
let eq = eq_or_not(reverse, "non_payment");
conditions.push(format!("guest_orders.non_payment{eq}{v}"));
}
if let Some(v) = &self.currency {
let eq = eq_or_not(reverse, "currency");
conditions.push(format!("guest_orders.currency{eq}'{}'", v.as_ref()));
Expand Down
35 changes: 7 additions & 28 deletions server/crates/elerp_common/src/order_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::{QueryBuilder, Row, SqliteConnection};

use self::model::{
check_order_result::{CheckOrderResult, ItemNotAvailable},
order::{Order, OrderItem, OrderPaymentStatus, OrderType},
order::{Order, OrderType},
};

pub mod model;
Expand Down Expand Up @@ -114,59 +114,38 @@ pub async fn check(order: &Order, fast_check: bool, tx: &mut SqliteConnection) -
Ok(CheckOrderResult { items_not_available })
}

fn calc_total_amount(items: &Vec<OrderItem>) -> f64 {
let mut total: f64 = 0.0;
for item in items.iter() {
if item.exchanged {
continue;
}
total += (item.quantity as f64) * item.price;
}
total
}

pub async fn add(mut order: Order, tx: &mut SqliteConnection) -> Result<Order> {
let items = order.items.as_ref();
let total_amount = if let Some(items) = items {
if let Some(items) = items {
if !order.is_record {
inventory_module::change(order.warehouse_id, items, order.order_type, tx).await?;
}
calc_total_amount(items)
} else {
0.0
};
let order_payment_status = if order.order_type == OrderType::StockOut && total_amount > 0.0 {
OrderPaymentStatus::Unsettled
} else {
OrderPaymentStatus::None
};
let r = sqlx::query("INSERT INTO orders (from_guest_order_id, created_by_user_id, updated_by_user_id, warehouse_id, currency, total_amount, person_related_id, person_in_charge_id, date, last_updated_date, description, order_type, is_record, order_category_id, total_amount_settled, order_payment_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
}
let r = sqlx::query("INSERT INTO orders (from_guest_order_id, created_by_user_id, updated_by_user_id, warehouse_id, currency, total_amount, person_related_id, person_in_charge_id, date, last_updated_date, description, order_type, is_record, non_payment, order_category_id, total_amount_settled, order_payment_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
.bind(order.from_guest_order_id)
.bind(order.created_by_user_id)
.bind(order.updated_by_user_id)
.bind(order.warehouse_id)
.bind(&order.currency)
.bind(total_amount)
.bind(order.total_amount)
.bind(order.person_related_id)
.bind(order.person_in_charge_id)
.bind(order.date)
.bind(order.last_updated_date)
.bind(&order.description)
.bind(&order.order_type)
.bind(order.is_record)
.bind(order.non_payment)
.bind(order.order_category_id)
.bind(0)
.bind(order_payment_status)
.bind(order.order_payment_status)
.execute(&mut *tx)
.await?;
if r.rows_affected() != 1 {
bail!("Can't insert the order to history!");
}

order.id = sql::try_set_standard_id(r.last_insert_rowid(), "orders", tx).await?;
order.total_amount = total_amount;
order.total_amount_settled = 0.0;
order.order_payment_status = order_payment_status;

add_order_items(&order, tx).await?;

Expand Down
8 changes: 8 additions & 0 deletions server/crates/elerp_common/src/order_module/model/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub struct Order {
pub order_type: OrderType,
#[serde(default)]
pub is_record: bool,
#[serde(default)]
pub non_payment: bool,
}

#[derive(Debug, Serialize, Deserialize, ToSchema, Clone, IntoParams, FromRow)]
Expand All @@ -125,6 +127,7 @@ pub struct GetOrdersQuery {
pub order_type: Option<OrderType>,
pub order_category_id: Option<i64>,
pub is_record: Option<bool>,
pub non_payment: Option<bool>,
pub currency: Option<OrderCurrency>,
pub items: Option<HashSet<i64>>,
pub item_categories: Option<HashSet<i64>>,
Expand Down Expand Up @@ -159,6 +162,7 @@ impl GetOrdersQuery {
reverse: None,
last_updated_date_start: None,
last_updated_date_end: None,
non_payment: None,
}
}
pub fn get_where_condition(&self) -> String {
Expand Down Expand Up @@ -212,6 +216,10 @@ impl GetOrdersQuery {
let eq = eq_or_not(reverse, "is_record");
conditions.push(format!("orders.is_record{eq}{v}"));
}
if let Some(v) = &self.non_payment {
let eq = eq_or_not(reverse, "non_payment");
conditions.push(format!("orders.non_payment{eq}{v}"));
}
if let Some(v) = &self.order_payment_status {
let eq = in_or_not(reverse, "order_payment_status");
let v = set_to_string(v, "','");
Expand Down
7 changes: 6 additions & 1 deletion server/crates/guest_order_module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl GuestOrderModule {
description TEXT NOT NULL,
order_type TEXT NOT NULL,
is_record BOOLEAN NOT NULL,
non_payment BOOLEAN NOT NULL,
guest_order_status TEXT NOT NULL,
order_id INT NOT NULL DEFAULT 0,
order_category_id INT NOT NULL,
Expand Down Expand Up @@ -133,7 +134,7 @@ impl GuestOrderModule {

pub async fn add(&self, sub_token: &str, mut order: GuestOrder, tx: &mut SqliteConnection) -> Result<GuestOrder> {
let now = self.ps.get_timestamp_seconds() as i64;
let r = sqlx::query("INSERT INTO guest_orders (date, confirmed_date, sub_token, created_by_user_id, warehouse_id, currency, person_related_id, person_in_charge_id, description, order_type, is_record, guest_order_status, order_category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
let r = sqlx::query("INSERT INTO guest_orders (date, confirmed_date, sub_token, created_by_user_id, warehouse_id, currency, person_related_id, person_in_charge_id, description, order_type, is_record, non_payment, guest_order_status, order_category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
.bind(now)
.bind(now)
.bind(sub_token)
Expand All @@ -145,6 +146,7 @@ impl GuestOrderModule {
.bind(&order.description)
.bind(&order.order_type)
.bind(order.is_record)
.bind(order.non_payment)
.bind(GuestOrderStatus::Pending)
.bind(order.order_category_id)
.execute(&mut *tx)
Expand Down Expand Up @@ -235,6 +237,7 @@ impl GuestOrderModule {
description: row.get("description"),
order_type: row.get("order_type"),
is_record: row.get("is_record"),
non_payment: row.get("non_payment"),
guest_order_status,
order_id,
date,
Expand All @@ -252,6 +255,7 @@ impl GuestOrderModule {
guest_orders.created_by_user_id,
guest_orders.warehouse_id,
guest_orders.is_record,
guest_orders.non_payment,
CASE WHEN guest_order_status='Confirmed' THEN orders.currency ELSE guest_orders.currency END AS currency,
CASE WHEN guest_order_status='Confirmed' THEN orders.person_related_id ELSE guest_orders.person_related_id END AS person_related_id,
CASE WHEN guest_order_status='Confirmed' THEN orders.person_in_charge_id ELSE guest_orders.person_in_charge_id END AS person_in_charge_id,
Expand Down Expand Up @@ -301,6 +305,7 @@ impl GuestOrderModule {
guest_orders.currency,
guest_orders.order_type,
guest_orders.is_record,
guest_orders.non_payment,
guest_orders.guest_order_status,
guest_orders.date,
guest_orders.confirmed_date,
Expand Down
22 changes: 22 additions & 0 deletions server/crates/order_module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl OrderModule {
description TEXT NOT NULL,
order_type TEXT NOT NULL,
is_record BOOLEAN NOT NULL,
non_payment BOOLEAN NOT NULL,
order_category_id INT NOT NULL
)",
)
Expand Down Expand Up @@ -133,6 +134,17 @@ impl OrderModule {
Ok(count >= self.ps.get_config().limit.orders)
}

fn calc_total_amount(&self, items: &Vec<OrderItem>) -> f64 {
let mut total: f64 = 0.0;
for item in items.iter() {
if item.exchanged {
continue;
}
total += (item.quantity as f64) * item.price;
}
total
}

pub fn preprocess(&self, order: &mut Order, user: &UserInfo, initial: bool, person_in_charge_id: i64) {
if let Some(items) = order.items.clone() {
order.items = Some(
Expand All @@ -151,6 +163,7 @@ impl OrderModule {
.collect(),
);
};
order.total_amount = if let Some(items) = order.items.as_ref() { self.calc_total_amount(items) } else { 0.0 };

let now = self.ps.get_timestamp_seconds() as i64;
order.updated_by_user_id = user.id;
Expand All @@ -161,6 +174,13 @@ impl OrderModule {
order.last_updated_date = now;
order.person_in_charge_id = person_in_charge_id;
order.from_guest_order_id = 0;
order.total_amount_settled = 0.0;

order.order_payment_status = if !order.non_payment && order.total_amount > 0.0 {
OrderPaymentStatus::Unsettled
} else {
OrderPaymentStatus::None
};
}

pub async fn can_access(&self, id: i64, user: &UserInfo, tx: &mut SqliteConnection) -> Result<bool> {
Expand Down Expand Up @@ -345,6 +365,7 @@ impl OrderModule {
order_type: row.get("order_type"),
order_category_id: row.get("order_category_id"),
is_record: row.get("is_record"),
non_payment: row.get("non_payment"),
items: None,
}
}
Expand Down Expand Up @@ -391,6 +412,7 @@ impl OrderModule {
orders.currency,
orders.order_type,
orders.is_record,
orders.non_payment,
orders.order_category_id,
orders.total_amount,
orders.total_amount_settled,
Expand Down
2 changes: 2 additions & 0 deletions server/tests/guest_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async fn test_order_preprocess() {
description: "".to_owned(),
order_type: OrderType::StockOut,
is_record: false,
non_payment: false,
guest_order_status: GuestOrderStatus::Expired,
order_id: 0,
order_category_id: p.order_category1.id,
Expand Down Expand Up @@ -58,6 +59,7 @@ async fn test_module() {
description: "".to_owned(),
order_type: OrderType::StockOut,
is_record: false,
non_payment: false,
guest_order_status: GuestOrderStatus::Expired,
order_id: 0,
order_category_id: p.order_category1.id,
Expand Down
Loading

0 comments on commit 1c78529

Please sign in to comment.