82 lines
3.6 KiB
C#
82 lines
3.6 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Distribution.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class AddInvoiceItems : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.CreateTable(
|
|
name: "InvoiceItems",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_InvoiceId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_ShopPOItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_ItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
Quantity = table.Column<int>(type: "int", nullable: false),
|
|
UnitPrice = table.Column<decimal>(type: "decimal(65,30)", nullable: false),
|
|
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
|
|
IsActive = table.Column<bool>(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;");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropTable(
|
|
name: "InvoiceItems");
|
|
}
|
|
}
|
|
}
|