@@ -22,6 +22,11 @@ class CreateInvoiceLines extends Migration
|
||||
'unsigned' => true,
|
||||
'null' => false,
|
||||
],
|
||||
'school_year' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 9,
|
||||
'null' => false,
|
||||
],
|
||||
'line_type' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 50,
|
||||
@@ -101,6 +106,7 @@ class CreateInvoiceLines extends Migration
|
||||
$this->forge->createTable('invoice_lines', true);
|
||||
}
|
||||
|
||||
$this->ensureSchoolYearColumn();
|
||||
$this->backfillLegacyInvoiceLines();
|
||||
}
|
||||
|
||||
@@ -125,7 +131,7 @@ class CreateInvoiceLines extends Migration
|
||||
$existing = array_fill_keys(array_map(static fn ($row) => (int) ($row['invoice_id'] ?? 0), $existingRows), true);
|
||||
|
||||
$invoices = $this->db->table('invoices')
|
||||
->select('id, total_amount, invoice_number, created_at, updated_at')
|
||||
->select('id, total_amount, invoice_number, school_year, created_at, updated_at')
|
||||
->orderBy('id', 'ASC')
|
||||
->get()
|
||||
->getResultArray();
|
||||
@@ -143,6 +149,7 @@ class CreateInvoiceLines extends Migration
|
||||
|
||||
$this->db->table('invoice_lines')->insert([
|
||||
'invoice_id' => $invoiceId,
|
||||
'school_year' => (string)($invoice['school_year'] ?? ''),
|
||||
'line_type' => 'legacy_invoice_total',
|
||||
'source_type' => 'legacy_invoice',
|
||||
'source_id' => $invoiceId,
|
||||
@@ -164,4 +171,33 @@ class CreateInvoiceLines extends Migration
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureSchoolYearColumn(): void
|
||||
{
|
||||
if (!$this->db->tableExists('invoice_lines')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->db->fieldExists('school_year', 'invoice_lines')) {
|
||||
$this->forge->addColumn('invoice_lines', [
|
||||
'school_year' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 9,
|
||||
'null' => true,
|
||||
'after' => 'invoice_id',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->db->tableExists('invoices')) {
|
||||
$this->db->query(
|
||||
"UPDATE invoice_lines il
|
||||
INNER JOIN invoices i ON i.id = il.invoice_id
|
||||
SET il.school_year = i.school_year
|
||||
WHERE il.school_year IS NULL OR TRIM(il.school_year) = ''"
|
||||
);
|
||||
}
|
||||
|
||||
$this->db->query("ALTER TABLE invoice_lines MODIFY school_year VARCHAR(9) NOT NULL");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user