stoptime-rs/src/models/invoice.rs

76 lines
2.4 KiB
Rust

use chrono::NaiveDateTime;
use diesel::prelude::*;
use juniper_codegen::{GraphQLInputObject, GraphQLObject};
use crate::models::{CompanyInfo, Customer, Task};
use crate::schema::invoices;
use crate::DbConn;
/// The invoice model.
///
/// This model represents an invoice for a customer that contains billed tasks and through the
/// tasks the registered time.
#[derive(AsChangeset, Associations, Debug, GraphQLObject, Identifiable, Queryable)]
#[belongs_to(CompanyInfo)]
#[belongs_to(Customer)]
#[table_name = "invoices"]
pub struct Invoice {
/// The unique identification number
pub id: i32,
/// 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,
/// The time of creation
pub created_at: NaiveDateTime,
/// The time of last update
pub updated_at: NaiveDateTime,
}
impl Invoice {
/// Returns the associated company info at the time of billing.
pub fn company_info(&self, conn: &DbConn) -> QueryResult<CompanyInfo> {
use diesel::associations::HasTable;
CompanyInfo::table()
.find(self.company_info_id)
.first(&**conn)
}
/// 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 billed tasks included in the invoice.
pub fn tasks(&self, conn: &DbConn) -> QueryResult<Vec<Task>> {
Task::belonging_to(self).load(&**conn)
}
}
/// The new invoice model.
///
/// This model represents an new invoice for a customer that can be inserted into the database.
#[derive(Debug, GraphQLInputObject, Insertable)]
#[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,
}