using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Distribution.Migrations { /// public partial class AddInvoiceItems : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "InvoiceItems", columns: table => new { Id = table.Column(type: "char(36)", nullable: false, collation: "ascii_general_ci"), FK_InvoiceId = table.Column(type: "char(36)", nullable: false, collation: "ascii_general_ci"), FK_ShopPOItemId = table.Column(type: "char(36)", nullable: false, collation: "ascii_general_ci"), FK_ItemId = table.Column(type: "char(36)", nullable: false, collation: "ascii_general_ci"), Quantity = table.Column(type: "int", nullable: false), UnitPrice = table.Column(type: "decimal(65,30)", nullable: false), CreatedAt = table.Column(type: "datetime(6)", nullable: false), IsActive = table.Column(type: "tinyint(1)", nullable: false) }, constraints: table => { table.PrimaryKey("PK_InvoiceItems", x => x.Id); table.ForeignKey( name: "FK_InvoiceItems_Invoices_FK_InvoiceId", column: x => x.FK_InvoiceId, principalTable: "Invoices", principalColumn: "Id", onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_InvoiceItems_Items_FK_ItemId", column: x => x.FK_ItemId, principalTable: "Items", principalColumn: "Id", onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_InvoiceItems_ShopPOItems_FK_ShopPOItemId", column: x => x.FK_ShopPOItemId, principalTable: "ShopPOItems", principalColumn: "Id", onDelete: ReferentialAction.Restrict); }) .Annotation("MySql:CharSet", "utf8mb4"); migrationBuilder.CreateIndex( name: "IX_InvoiceItems_FK_InvoiceId", table: "InvoiceItems", column: "FK_InvoiceId"); migrationBuilder.CreateIndex( name: "IX_InvoiceItems_FK_ItemId", table: "InvoiceItems", column: "FK_ItemId"); migrationBuilder.CreateIndex( name: "IX_InvoiceItems_FK_ShopPOItemId", table: "InvoiceItems", column: "FK_ShopPOItemId"); // Backfill: snapshot each existing invoice's PO lines so older invoices can be edited/finalized. migrationBuilder.Sql(@" INSERT INTO InvoiceItems (Id, FK_InvoiceId, FK_ShopPOItemId, FK_ItemId, Quantity, UnitPrice, CreatedAt, IsActive) SELECT UUID(), inv.Id, poi.Id, poi.FK_ItemId, poi.Quantity, poi.UnitPrice, NOW(6), 1 FROM Invoices inv JOIN ShopPOItems poi ON poi.FK_ShopPOId = inv.FK_ShopPOId AND poi.IsActive = 1;"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "InvoiceItems"); } } }