Phase 11, step 1: the parties of a contract and the conflict check
A contract's other side was one line of text. Now each other party is a row with its names in English and Thai, an ID, passport or registration number and its role (migration 0010; the old line became the first party). Parties are never deleted or rewritten: taking one off marks it removed, and both are in the journal. The conflict check (src/conflicts.rs) looks for the name or number among the clients, the other parties, the visa people and the employers. Names match regardless of case, word order, punctuation and company words in English and Thai (Co., Ltd., บริษัท ... จำกัด), or when every word of the shorter name, at least two, is in the longer one. A new party that is a client, a visa client or an employer is a conflict; a new or renamed client that was on the other side of a contract is one too; a client's name or number again is perhaps the same client. Those stop the save (409) until a lawyer ticks that they looked, with what they checked; the matches and the note go into the journal entry. /conflicts runs the check alone before a first meeting and saves nothing. Tests: 119 unit, 63 integration.
This commit is contained in:
1 parent
f7fb2c7af4
commit
9ae4d7bd07
16 files changed
+1464
-25
No files matched your search
+161
-6
@@ -193,9 +193,6 @@ const CONTRACT_FIELDS: &[Field] = &[
|
||||
.wide(),
|
||||
field("kind", "Kind", Input::Choice(CONTRACT_KINDS)).req("Choose the kind of contract."),
|
||||
field("client_id", "Client", Input::Client).req("Choose the client."),
|
||||
field("counterparty", "Other party", Input::Text)
|
||||
.hint("The venue, label, employer, landlord or buyer on the other side.")
|
||||
.wide(),
|
||||
field("status", "Status", Input::Choice(CONTRACT_STATUSES)).req("Choose the status."),
|
||||
field("lawyer_id", "Lawyer in charge", Input::Lawyer),
|
||||
field("signed_on", "Signed on", Input::Date).section("Dates"),
|
||||
@@ -421,7 +418,9 @@ pub struct Filter {
|
||||
pub async fn list_contracts(db: &PgPool, f: &Filter) -> Result<Vec<Listed>> {
|
||||
Ok(sqlx::query_as(
|
||||
"SELECT c.id, c.code, c.title, c.kind, c.status, c.client_id, cl.name AS client, \
|
||||
c.counterparty, c.lawyer_id, l.name AS lawyer, c.ends_on, c.notice_by, \
|
||||
COALESCE((SELECT string_agg(p.name, ', ' ORDER BY p.id) FROM contract_parties p \
|
||||
WHERE p.contract_id = c.id AND p.removed_at IS NULL), '') AS counterparty, \
|
||||
c.lawyer_id, l.name AS lawyer, c.ends_on, c.notice_by, \
|
||||
(SELECT count(*) FROM contract_versions v WHERE v.contract_id = c.id) AS versions, \
|
||||
(SELECT count(*) FROM paper_copies p WHERE p.contract_id = c.id) AS papers \
|
||||
FROM contracts c JOIN clients cl ON cl.id = c.client_id \
|
||||
@@ -490,6 +489,162 @@ pub async fn list_clients(db: &PgPool, former: bool) -> Result<Vec<ClientRow>> {
|
||||
.await?)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- parties
|
||||
|
||||
/// What a party is in a contract, for the form's suggestions.
|
||||
pub const PARTY_ROLES: &[&str] = &[
|
||||
"Label",
|
||||
"Publisher",
|
||||
"Manager",
|
||||
"Artist",
|
||||
"Venue",
|
||||
"Promoter",
|
||||
"Festival",
|
||||
"Sponsor",
|
||||
"Employer",
|
||||
"Employee",
|
||||
"Landlord",
|
||||
"Tenant",
|
||||
"Buyer",
|
||||
"Seller",
|
||||
"Lender",
|
||||
"Borrower",
|
||||
"Service provider",
|
||||
"Guarantor",
|
||||
];
|
||||
|
||||
/// One of the other parties of a contract.
|
||||
#[derive(Clone, Debug, sqlx::FromRow, Serialize)]
|
||||
pub struct Party {
|
||||
pub id: i64,
|
||||
pub contract_id: i64,
|
||||
pub name: String,
|
||||
pub name_th: String,
|
||||
pub id_no: String,
|
||||
pub role: String,
|
||||
pub added_by: String,
|
||||
pub added_at: DateTime<Utc>,
|
||||
pub removed_by: Option<String>,
|
||||
pub removed_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
/// The parties of a contract in the order they were added, the removed
|
||||
/// ones too.
|
||||
pub async fn parties(db: &PgPool, contract: i64) -> Result<Vec<Party>> {
|
||||
Ok(
|
||||
sqlx::query_as("SELECT * FROM contract_parties WHERE contract_id = $1 ORDER BY id")
|
||||
.bind(contract)
|
||||
.fetch_all(db)
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
|
||||
/// A party as the form gives it.
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct NewParty {
|
||||
pub name: String,
|
||||
pub name_th: String,
|
||||
pub id_no: String,
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
impl NewParty {
|
||||
/// The party as the journal states it.
|
||||
pub fn said(&self) -> String {
|
||||
let mut s = self.name.trim().to_string();
|
||||
if !self.name_th.trim().is_empty() {
|
||||
s.push_str(&format!(" ({})", self.name_th.trim()));
|
||||
}
|
||||
if !self.id_no.trim().is_empty() {
|
||||
s.push_str(&format!(", number {}", self.id_no.trim()));
|
||||
}
|
||||
if !self.role.trim().is_empty() {
|
||||
s.push_str(&format!(", as {}", self.role.trim()));
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a party, with the conflict check's words in its journal entry.
|
||||
pub async fn add_party(
|
||||
conn: &mut PgConnection,
|
||||
head: &Head,
|
||||
p: &NewParty,
|
||||
check: &str,
|
||||
by: &str,
|
||||
) -> Result<Party> {
|
||||
let party: Party = sqlx::query_as(
|
||||
"INSERT INTO contract_parties (contract_id, name, name_th, id_no, role, added_by) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
|
||||
)
|
||||
.bind(head.id)
|
||||
.bind(p.name.trim())
|
||||
.bind(p.name_th.trim())
|
||||
.bind(p.id_no.trim())
|
||||
.bind(p.role.trim())
|
||||
.bind(by)
|
||||
.fetch_one(&mut *conn)
|
||||
.await?;
|
||||
let mut detail = format!("{}: {}.", head.code, p.said());
|
||||
if !check.is_empty() {
|
||||
detail.push(' ');
|
||||
detail.push_str(check);
|
||||
}
|
||||
journal::append(
|
||||
conn,
|
||||
&journal::New {
|
||||
actor: by,
|
||||
client: Some(head.client_id),
|
||||
contract: Some(head.id),
|
||||
event: "party added",
|
||||
detail,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(party)
|
||||
}
|
||||
|
||||
/// Takes a party off a contract: it stays on record as removed. False when
|
||||
/// it is not a party of this contract, or removed already.
|
||||
pub async fn remove_party(
|
||||
conn: &mut PgConnection,
|
||||
head: &Head,
|
||||
party: i64,
|
||||
by: &str,
|
||||
) -> Result<bool> {
|
||||
let row: Option<Party> = sqlx::query_as(
|
||||
"UPDATE contract_parties SET removed_by = $3, removed_at = now() \
|
||||
WHERE id = $1 AND contract_id = $2 AND removed_at IS NULL RETURNING *",
|
||||
)
|
||||
.bind(party)
|
||||
.bind(head.id)
|
||||
.bind(by)
|
||||
.fetch_optional(&mut *conn)
|
||||
.await?;
|
||||
let Some(p) = row else {
|
||||
return Ok(false);
|
||||
};
|
||||
let said = NewParty {
|
||||
name: p.name,
|
||||
name_th: p.name_th,
|
||||
id_no: p.id_no,
|
||||
role: p.role,
|
||||
}
|
||||
.said();
|
||||
journal::append(
|
||||
conn,
|
||||
&journal::New {
|
||||
actor: by,
|
||||
client: Some(head.client_id),
|
||||
contract: Some(head.id),
|
||||
event: "party removed",
|
||||
detail: format!("{}: {said}.", head.code),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ versions
|
||||
|
||||
#[derive(Clone, Debug, sqlx::FromRow, Serialize)]
|
||||
@@ -865,11 +1020,11 @@ mod tests {
|
||||
old.insert("status".into(), "draft".into());
|
||||
let mut new = old.clone();
|
||||
new.insert("status".into(), "active".into());
|
||||
new.insert("counterparty".into(), "Demo Records".into());
|
||||
new.insert("notes".into(), "Signed at the label".into());
|
||||
let opts = Options::default();
|
||||
assert_eq!(
|
||||
changes_of(&CONTRACTS, &old, &new, &opts),
|
||||
"Other party: (empty) -> Demo Records; Status: Draft -> In force"
|
||||
"Status: Draft -> In force; Notes: (empty) -> Signed at the label"
|
||||
);
|
||||
assert!(changes_of(&CONTRACTS, &old, &old, &opts).is_empty());
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user