Files
zibo-dashboard/db/migrations/20261027104527_add_mago_mix_purchases.php
T
2026-10-27 11:50:50 +01:00

63 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
/**
* Acquisti mescole da Mago:
* - suppliers.mago_code: codice fornitore Mago (es. C063 = COMET)
* - mago_mix_purchases: righe delle fatture fornitore con articolo MESCOLE (copia locale, sola lettura da Mago)
* - fornitore HEXPOL (mancava)
*/
final class AddMagoMixPurchases extends AbstractMigration
{
public function up(): void
{
$t = $this->table('suppliers');
if (!$t->hasColumn('mago_code')) {
$t->addColumn('mago_code', 'string', ['limit' => 20, 'null' => true, 'after' => 'vat'])
->addIndex(['mago_code'], ['name' => 'idx_suppliers_mago_code'])
->update();
}
// codici Mago già noti dalla prova acquisti (DER-GOM e HEXPOL si collegano da soli al primo sync)
foreach (['COMET' => 'C063', 'SIGEA' => 'S022', 'TOVO' => 'T050', 'EVER COMPOUND' => 'E025'] as $name => $code) {
$this->execute("UPDATE suppliers SET mago_code = '$code' WHERE supplier_name = '$name' AND (mago_code IS NULL OR mago_code = '')");
}
if (!$this->fetchRow("SELECT idsupplier FROM suppliers WHERE supplier_name LIKE 'HEXPOL%'")) {
$this->execute("INSERT INTO suppliers (supplier_name) VALUES ('HEXPOL')");
}
if (!$this->hasTable('mago_mix_purchases')) {
$this->table('mago_mix_purchases', ['id' => false, 'primary_key' => ['purchase_doc_id', 'line'], 'collation' => 'utf8mb4_general_ci'])
->addColumn('purchase_doc_id', 'integer')
->addColumn('line', 'integer')
->addColumn('document_type', 'integer', ['null' => true])
->addColumn('doc_date', 'date', ['null' => true])
->addColumn('doc_no', 'string', ['limit' => 40, 'null' => true])
->addColumn('supplier_code', 'string', ['limit' => 20, 'null' => true])
->addColumn('supplier_name', 'string', ['limit' => 150, 'null' => true])
->addColumn('item', 'string', ['limit' => 40, 'null' => true])
->addColumn('description', 'string', ['limit' => 500, 'null' => true])
->addColumn('uom', 'string', ['limit' => 10, 'null' => true])
->addColumn('qty', 'decimal', ['precision' => 14, 'scale' => 3, 'default' => 0])
->addColumn('unit_value', 'decimal', ['precision' => 14, 'scale' => 5, 'default' => 0])
->addColumn('taxable_amount', 'decimal', ['precision' => 14, 'scale' => 2, 'default' => 0])
->addColumn('notes', 'string', ['limit' => 500, 'null' => true])
->addColumn('purchase_ord_no', 'string', ['limit' => 40, 'null' => true])
->addColumn('synced_at', 'datetime', ['null' => true])
->addIndex(['doc_date'])
->addIndex(['supplier_code'])
->create();
}
}
public function down(): void
{
if ($this->hasTable('mago_mix_purchases')) $this->table('mago_mix_purchases')->drop()->save();
$t = $this->table('suppliers');
if ($t->hasColumn('mago_code')) $t->removeIndexByName('idx_suppliers_mago_code')->removeColumn('mago_code')->update();
}
}