Add new (insertable) models and associations to models

Also make models updateable (by deriving `AsChangeset`), debuggable
(by deriving `Debug`) and identifiable (by deriving `Identifiable`).

Note that the `CompanyInfo` is not updateable as a new revision
should be created.  Leaving this derive out ensure we cannot
accidentally update it.
This commit is contained in:
Paul van Tilburg 2019-06-09 18:14:32 +02:00
parent 4a8b3a48e1
commit 1559936d4b
6 changed files with 174 additions and 17 deletions

View File

@ -6,8 +6,8 @@ mod invoice;
mod task;
mod time_entry;
pub use company_info::CompanyInfo;
pub use customer::Customer;
pub use invoice::Invoice;
pub use task::Task;
pub use time_entry::TimeEntry;
pub use company_info::{CompanyInfo, NewCompanyInfo};
pub use customer::{Customer, NewCustomer};
pub use invoice::{Invoice, NewInvoice};
pub use task::{NewTask, Task};
pub use time_entry::{NewTimeEntry, TimeEntry};

View File

@ -1,11 +1,15 @@
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
/// The company (information) model
use crate::schema::company_infos;
/// The company (information) model.
///
/// This model represents information about the company or sole proprietorship of the user of
/// StopTime.
#[derive(Deserialize, Queryable, Serialize)]
#[derive(Associations, Debug, Identifiable, Deserialize, Queryable, Serialize)]
#[belongs_to(CompanyInfo, foreign_key = "original_id")]
#[table_name = "company_infos"]
pub struct CompanyInfo {
/// The unique identification number
pub id: i32,
@ -27,7 +31,7 @@ pub struct CompanyInfo {
pub bank_name: String,
/// The cellular phone number
pub cell: String,
// The chamber of commerce ID number
/// The chamber of commerce ID number
pub chamber: String,
/// The personal contact name
pub contact_name: String,
@ -41,7 +45,7 @@ pub struct CompanyInfo {
pub name: String,
/// The ID of the previous company information model (if any)
pub original_id: Option<i32>,
// The phone number
/// The phone number
pub phone: String,
/// The VAT number
pub vatno: String,
@ -52,3 +56,49 @@ pub struct CompanyInfo {
/// The time of last update
pub updated_at: NaiveDateTime,
}
/// The new company (information) model.
///
/// This model represents new company information that can be inserted into the database.
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "company_infos"]
pub struct NewCompanyInfo {
/// The international bank account number
pub accountiban: String,
/// The name of the bank account holder
pub accountname: String,
/// The number of the bank account
pub accountno: String,
/// The city part of the address
pub address_city: String,
/// The postal code part of the address
pub address_postal_code: String,
/// The street part of the address
pub address_street: String,
/// The bank identification code (or: SWIFT code)
pub bank_bic: String,
/// The name of the bank
pub bank_name: String,
/// The cellular phone number
pub cell: String,
/// The chamber of commerce ID number
pub chamber: String,
/// The personal contact name
pub contact_name: String,
/// The country of residence
pub country: String,
/// The two letter country code
pub country_code: String,
/// The email address
pub email: String,
/// The official company name
pub name: String,
/// The ID of the previous company information model (if any)
pub original_id: Option<i32>,
/// The phone number
pub phone: String,
/// The VAT number
pub vatno: String,
/// The website
pub website: String,
}

View File

@ -1,11 +1,14 @@
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
/// The customer model
use crate::schema::customers;
/// The customer model.
///
/// This model represents a customer that has projects/tasks for which invoices need to be
/// generated.
#[derive(Deserialize, Queryable, Serialize)]
#[derive(AsChangeset, Debug, Deserialize, Identifiable, Queryable, Serialize)]
#[table_name = "customers"]
pub struct Customer {
/// The unique identification number
pub id: i32,
@ -34,3 +37,31 @@ pub struct Customer {
/// The time of last update
pub updated_at: NaiveDateTime,
}
/// The new customer model
///
/// This model represents a new customer that can be inserted into the database.
#[derive(Default, Deserialize, Insertable, Serialize)]
#[table_name = "customers"]
pub struct NewCustomer {
/// The city part of the address
pub address_city: String,
/// The postal code part of the address
pub address_postal_code: String,
/// The street part of the address
pub address_street: String,
/// The email address
pub email: String,
/// The name of the financial contact person/department
pub financial_contact: String,
/// The default hourly rate (if applicable)
pub hourly_rate: Option<f32>,
/// The official (long) name
pub name: String,
/// The phone number
pub phone: String,
/// The abbreviated name
pub short_name: String,
/// Flag whether the customer requires time specificaions
pub time_specification: bool,
}

View File

@ -1,11 +1,17 @@
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
/// The invoice model
use crate::models::{CompanyInfo, Customer};
use crate::schema::invoices;
/// The invoice model.
///
/// This model represents an invoice for a customer that contains billed tasks and through the
/// tasks the registered time.
#[derive(Deserialize, Queryable, Serialize)]
#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)]
#[belongs_to(CompanyInfo)]
#[belongs_to(Customer)]
#[table_name = "invoices"]
pub struct Invoice {
/// The unique identification number
pub id: i32,
@ -24,3 +30,21 @@ pub struct Invoice {
/// The time of last update
pub updated_at: NaiveDateTime,
}
/// The new invoice model.
///
/// This model represents an new invoice for a customer that can be inserted into the database.
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "invoices"]
pub struct NewInvoice {
/// The ID of the company info at the time of billing
pub company_info_id: i32,
/// The ID of associated customer
pub customer_id: i32,
/// Flag whether the invoice should include a time specification
pub include_specification: bool,
/// The invoice number
pub number: i32,
/// Flag whether the invoice has been paid
pub paid: bool,
}

View File

@ -1,11 +1,17 @@
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
/// The task model
use crate::models::{Customer, Invoice};
use crate::schema::tasks;
/// The task (or project) model.
///
/// This model represents a task (or project) of a customer on which time can be registered.
/// generated.
#[derive(Deserialize, Queryable, Serialize)]
#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)]
#[belongs_to(Customer)]
#[belongs_to(Invoice)]
#[table_name = "tasks"]
pub struct Task {
/// The unique identification number
pub id: i32,
@ -28,3 +34,26 @@ pub struct Task {
/// The time of last update
pub updated_at: NaiveDateTime,
}
/// 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)
pub fixed_cost: Option<f32>,
/// The hourly rate of the task (if applicable)
pub hourly_rate: Option<f32>,
/// 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)
pub vat_rate: f32,
}

View File

@ -1,10 +1,15 @@
use chrono::NaiveDateTime;
use serde_derive::{Deserialize, Serialize};
/// The time entry model
use crate::models::Task;
use crate::schema::time_entries;
/// The time entry model.
///
/// This model represents an amount of time that is registered for a certain task.
#[derive(Deserialize, Queryable, Serialize)]
#[derive(AsChangeset, Associations, Debug, Deserialize, Identifiable, Queryable, Serialize)]
#[belongs_to(Task)]
#[table_name = "time_entries"]
pub struct TimeEntry {
/// The unique identification number
pub id: i32,
@ -23,3 +28,21 @@ pub struct TimeEntry {
/// The time of last update
pub updated_at: NaiveDateTime,
}
/// The new time entry model.
///
/// This model represents a new registered amount of time that can be inserted into the database.
#[derive(Debug, Deserialize, Insertable, Serialize)]
#[table_name = "time_entries"]
pub struct NewTimeEntry {
/// Flag whether to bill or not
pub bill: bool,
/// An additional comment
pub comment: String,
/// The finish/end time of the entry
pub end: NaiveDateTime,
/// The start time of the entry
pub start: NaiveDateTime,
/// The ID of the task the entry is registered for
pub task_id: i32,
}