Phase 5: the Immigration Bureau's forms, filled in from the records
CI / secret scan (push) Successful in 5s
CI / fmt, clippy, tests (push) Successful in 39s

A Forms tab on each person fills in TM.7, STM.2, STM.9, STM.11 and STM.1
for an extension of stay, TM.47 for a 90-day report and TM.8 for a re-entry
permit. The Bureau's blank pages (forms/, with their sources and checksums)
go unchanged into a new PDF, and the values are written on the dotted lines
in Sarabun; boxes are ticked and the title circled where a form asks.
scripts/gen_layout.py measures the blanks into src/papers/layout.rs.

The tab says what would stay blank and links to where it is added. What only
the application says (the date, the office, the reason, the days, a trip) is
carried in the links and never stored. The extension's five forms also come
in one PDF. Opening or downloading a form is audited as entity "form" with
the forms' codes, never their values.

Migration 0005 adds what STM.1 and STM.11 ask of the company (capital,
revenue, equity, staff, a coordinator), a work permit's salary and contract
dates, and where an arrival came from. The Nix package now takes forms/.
This commit is contained in:
grabowski committed 2026-11-18 12:30:03 +07:00
1 parent 2a6e972b68
commit a2cedcdd7e
40 files changed
+3404 -35

No files matched your search

+104 -11
View File
@@ -42,6 +42,12 @@ pub enum Input {
Company,
/// A GPS position, kept as "18.796500, 99.008200" (`gps`).
Gps,
/// Whole baht of any size, also below zero (a company's equity).
Amount,
/// A number of people.
Count,
/// A year, the Western one; a Thai year (B.E.) is turned into it.
Year,
}
#[derive(Debug)]
@@ -56,6 +62,8 @@ pub struct Field {
/// A `<datalist>` with suggestions: "ports", "offices" or "provinces".
pub suggest: &'static str,
pub wide: bool,
/// A heading the form shows before this field.
pub section: &'static str,
}
const fn field(name: &'static str, label: &'static str, input: Input) -> Field {
@@ -67,6 +75,7 @@ const fn field(name: &'static str, label: &'static str, input: Input) -> Field {
hint: "",
suggest: "",
wide: false,
section: "",
}
}
@@ -83,6 +92,9 @@ impl Field {
const fn wide(self) -> Field {
Field { wide: true, ..self }
}
const fn section(self, section: &'static str) -> Field {
Field { section, ..self }
}
}
/// The later date may not come before the earlier one; the message goes to
@@ -252,6 +264,8 @@ const ARRIVAL_FIELDS: &[Field] = &[
.hint("The date in the arrival stamp."),
field("passport_id", "Passport", Input::Passport),
field("flight", "Flight or vehicle", Input::Code).hint("Such as TG110."),
field("came_from", "Came from", Input::Text)
.hint("Where the flight or the journey started, such as Frankfurt; TM.7 asks for it."),
field("tdac_no", "TDAC number", Input::Code).hint(
"The Thailand Digital Arrival Card, filed online within the three days before arrival.",
),
@@ -400,6 +414,10 @@ const WORK_PERMIT_FIELDS: &[Field] = &[
field("position", "Position", Input::Text),
field("issued_on", "Issued on", Input::Date),
field("valid_until", "Valid until", Input::Date).req("Enter the date the permit ends."),
field("monthly_salary_thb", "Monthly salary in THB", Input::Money)
.hint("As in the employment contract; the employer's certificate (STM.1) states it."),
field("contract_from", "Contract from", Input::Date),
field("contract_until", "Contract until", Input::Date),
field("notes", "Notes", Input::Area).wide(),
];
@@ -413,12 +431,20 @@ pub static WORK_PERMITS: Kind = Kind {
fields: WORK_PERMIT_FIELDS,
headline: &["company_id", "number"],
order: "valid_until DESC, id DESC",
date_order: &[DateOrder {
earlier: "issued_on",
later: "valid_until",
target: "valid_until",
message: "The permit cannot end before it was issued.",
}],
date_order: &[
DateOrder {
earlier: "issued_on",
later: "valid_until",
target: "valid_until",
message: "The permit cannot end before it was issued.",
},
DateOrder {
earlier: "contract_from",
later: "contract_until",
target: "contract_until",
message: "The contract cannot end before it starts.",
},
],
check: no_check,
status: work_permit_status,
per_person: true,
@@ -451,6 +477,23 @@ const COMPANY_FIELDS: &[Field] = &[
field("signatory", "Authorized director", Input::Text)
.hint("Signs for the company on STM.1 and on the employer's part of TM.7."),
field("signatory_position", "Director's position", Input::Text),
field("capital_thb", "Registered capital in THB", Input::Amount)
.section("For the employer's certificate (STM.1)"),
field("paid_up_thb", "Paid-up capital in THB", Input::Amount),
field("figures_year", "Year of the latest financial statements", Input::Year)
.hint("Such as 2025. A Thai year, such as 2568, is turned into the Western one."),
field("revenue_thb", "Total revenue in that year, in THB", Input::Amount)
.hint("As in the profit and loss statement."),
field("equity_thb", "Owners' equity in THB", Input::Amount)
.hint("As in the balance sheet of that year."),
field("foreign_costs_thb", "Foreign staff's pay next year, in THB", Input::Amount)
.hint("Salaries, wages and every benefit of all foreign employees in the next fiscal year."),
field("thai_staff", "Permanent Thai employees", Input::Count),
field("foreign_staff", "Foreign employees", Input::Count),
field("coordinator", "Coordinator for immigration", Input::Text)
.section("For the consent form (STM.11)")
.hint("The person at the company whom the officers may call about an application."),
field("coordinator_phone", "Coordinator's phone", Input::Text),
field("notes", "Notes", Input::Area).wide(),
];
@@ -520,6 +563,26 @@ fn company_check(v: &Values, e: &mut Errors) {
if !email.is_empty() && !crate::people::looks_like_email(email) {
err(e, "email", "This does not look like an email address.");
}
let amount = |k: &str| val(v, k).parse::<i64>().ok();
for k in [
"capital_thb",
"paid_up_thb",
"revenue_thb",
"foreign_costs_thb",
] {
if amount(k).is_some_and(|n| n < 0) {
err(e, k, "Enter an amount of zero or more.");
}
}
if let (Some(capital), Some(paid)) = (amount("capital_thb"), amount("paid_up_thb"))
&& paid > capital
{
err(
e,
"paid_up_thb",
"The paid-up capital is at most the registered capital.",
);
}
}
/// The check digit of a Thai 13-digit number (juristic persons and
@@ -649,7 +712,13 @@ pub fn read(kind: &Kind, f: &Fields) -> Values {
let v = match fl.input {
Input::Code => compact_upper(raw),
Input::Digits => raw.trim().replace([' ', '-'], ""),
Input::Money => raw.trim().replace([',', ' '], ""),
Input::Money | Input::Count => raw.trim().replace([',', ' '], ""),
Input::Amount => raw.trim().replace([',', ' ', '\u{0e3f}'], ""),
Input::Year => match raw.trim().parse::<i32>() {
// the Thai year: 2568 is 2025
Ok(y) if (2443..=2643).contains(&y) => (y - 543).to_string(),
_ => raw.trim().to_string(),
},
Input::Area => raw.trim().replace("\r\n", "\n"),
Input::Gps => gps::parse(raw).map_or_else(|| raw.trim().to_string(), |p| p.text()),
_ => raw.trim().to_string(),
@@ -708,6 +777,18 @@ pub fn validate(kind: &Kind, v: &Values, opts: &Options) -> Errors {
err(&mut e, fl.name, msg);
}
}
Input::Amount => match value.parse::<i64>() {
Ok(n) if n.abs() <= 10_000_000_000_000 => {}
_ => err(&mut e, fl.name, "Enter whole baht, such as 2,000,000."),
},
Input::Count => match value.parse::<i32>() {
Ok(n) if (0..=1_000_000).contains(&n) => {}
_ => err(&mut e, fl.name, "Enter a number of people, such as 12."),
},
Input::Year => match value.parse::<i32>() {
Ok(n) if (1900..=2100).contains(&n) => {}
_ => err(&mut e, fl.name, "Enter the year, such as 2025."),
},
Input::Text | Input::Code | Input::Area => {}
}
}
@@ -761,7 +842,13 @@ fn columns(kind: &Kind) -> String {
.iter()
.map(|f| match f.input {
Input::Date => format!("COALESCE(to_char({0}, 'YYYY-MM-DD'), '') AS {0}", f.name),
Input::Money | Input::Passport | Input::Visa | Input::Company => {
Input::Money
| Input::Amount
| Input::Count
| Input::Year
| Input::Passport
| Input::Visa
| Input::Company => {
format!("COALESCE({0}::text, '') AS {0}", f.name)
}
_ => f.name.to_string(),
@@ -841,9 +928,12 @@ fn bind_value(qb: &mut QueryBuilder<'_, Postgres>, f: &Field, v: &Values) {
Input::Date => {
qb.push_bind(dates::parse_date(&s));
}
Input::Money => {
Input::Money | Input::Count | Input::Year => {
qb.push_bind(s.parse::<i32>().ok());
}
Input::Amount => {
qb.push_bind(s.parse::<i64>().ok());
}
Input::Passport | Input::Visa | Input::Company => {
qb.push_bind(s.parse::<i64>().ok());
}
@@ -935,6 +1025,7 @@ pub struct FormField {
pub required: bool,
pub suggest: &'static str,
pub wide: bool,
pub section: &'static str,
}
pub fn form_fields(kind: &Kind, opts: &Options) -> Vec<FormField> {
@@ -950,7 +1041,8 @@ pub fn form_fields(kind: &Kind, opts: &Options) -> Vec<FormField> {
let (k, options) = match fl.input {
Input::Date => ("date", vec![]),
Input::Area => ("area", vec![]),
Input::Money => ("number", vec![]),
Input::Money | Input::Count | Input::Year => ("number", vec![]),
Input::Amount => ("text", vec![]),
Input::Choice(list) => (
"select",
list.iter()
@@ -972,6 +1064,7 @@ pub fn form_fields(kind: &Kind, opts: &Options) -> Vec<FormField> {
required,
suggest: fl.suggest,
wide: fl.wide || fl.input == Input::Area,
section: fl.section,
}
})
.collect()
@@ -1023,7 +1116,7 @@ pub fn shown_value(fl: &Field, raw: &str, opts: &Options) -> Part {
.map(dates::fmt_date)
.unwrap_or_else(|| raw.to_string()),
Input::Choice(list) => label(list, raw).unwrap_or(raw).to_string(),
Input::Money => raw
Input::Money | Input::Amount => raw
.parse::<i64>()
.map(|n| format!("{} THB", thousands(n)))
.unwrap_or_else(|_| raw.to_string()),