2019-06-08 23:42:51 +02:00
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-07-09 12:23:31 +02:00
|
|
|
use diesel::prelude::*;
|
2019-06-08 23:42:51 +02:00
|
|
|
|
2019-07-09 12:23:31 +02:00
|
|
|
use crate::models::{Customer, Invoice, TimeEntry};
|
2019-06-09 18:14:32 +02:00
|
|
|
use crate::schema::tasks;
|
2019-07-09 12:23:31 +02:00
|
|
|
use crate::DbConn;
|
2019-06-09 18:14:32 +02:00
|
|
|
|
|
|
|
/// The task (or project) model.
|
2019-06-08 23:42:51 +02:00
|
|
|
///
|
|
|
|
/// This model represents a task (or project) of a customer on which time can be registered.
|
|
|
|
/// generated.
|
2019-06-09 18:14:32 +02:00
|
|
|
#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)]
|
|
|
|
#[belongs_to(Customer)]
|
|
|
|
#[belongs_to(Invoice)]
|
|
|
|
#[table_name = "tasks"]
|
2019-06-08 23:42:51 +02:00
|
|
|
pub struct Task {
|
|
|
|
/// The unique identification number
|
|
|
|
pub id: i32,
|
|
|
|
/// The ID of the associated customer
|
|
|
|
pub customer_id: i32,
|
|
|
|
/// The fixed cost of the task (if applicable)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub fixed_cost: Option<f64>,
|
2019-06-08 23:42:51 +02:00
|
|
|
/// The hourly rate of the task (if applicable)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub hourly_rate: Option<f64>,
|
2019-06-08 23:42:51 +02:00
|
|
|
/// An extra comment for on the invoice
|
|
|
|
pub invoice_comment: String,
|
|
|
|
/// The associated invoice (if billed)
|
|
|
|
pub invoice_id: Option<i32>,
|
|
|
|
/// The name/description
|
|
|
|
pub name: String,
|
|
|
|
/// The VAT rate (at time of billing)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub vat_rate: f64,
|
2019-06-08 23:42:51 +02:00
|
|
|
/// The time of creation
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
/// The time of last update
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
}
|
2019-06-09 18:14:32 +02:00
|
|
|
|
2019-07-09 12:23:31 +02:00
|
|
|
impl Task {
|
|
|
|
/// Returns the associated customer.
|
|
|
|
pub fn customer(&self, conn: &DbConn) -> QueryResult<Customer> {
|
|
|
|
use diesel::associations::HasTable;
|
|
|
|
|
|
|
|
Customer::table().find(self.customer_id).first(&**conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the associated invoice (if billed)
|
|
|
|
pub fn invoice(&self, conn: &DbConn) -> QueryResult<Option<Invoice>> {
|
|
|
|
use diesel::associations::HasTable;
|
|
|
|
|
|
|
|
match self.invoice_id {
|
|
|
|
Some(invoice_id) => Invoice::table().find(invoice_id).first(&**conn).optional(),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the registered time entries.
|
|
|
|
pub fn time_entries(&self, conn: &DbConn) -> QueryResult<Vec<TimeEntry>> {
|
|
|
|
TimeEntry::belonging_to(self).load(&**conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:14:32 +02:00
|
|
|
/// The new task model.
|
|
|
|
///
|
|
|
|
/// This model represents a new task (or project) of a customer that can be inserted into the
|
|
|
|
/// database.
|
|
|
|
#[derive(Debug, Deserialize, Insertable, Serialize)]
|
|
|
|
#[table_name = "tasks"]
|
|
|
|
pub struct NewTask {
|
|
|
|
/// The ID of the associated customer
|
|
|
|
pub customer_id: i32,
|
|
|
|
/// The fixed cost of the task (if applicable)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub fixed_cost: Option<f64>,
|
2019-06-09 18:14:32 +02:00
|
|
|
/// The hourly rate of the task (if applicable)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub hourly_rate: Option<f64>,
|
2019-06-09 18:14:32 +02:00
|
|
|
/// An extra comment for on the invoice
|
|
|
|
pub invoice_comment: String,
|
|
|
|
/// The associated invoice (if billed)
|
|
|
|
pub invoice_id: Option<i32>,
|
|
|
|
/// The name/description
|
|
|
|
pub name: String,
|
|
|
|
/// The VAT rate (at time of billing)
|
2019-07-09 12:15:03 +02:00
|
|
|
pub vat_rate: f64,
|
2019-06-09 18:14:32 +02:00
|
|
|
}
|