-- llx_guillotine_piece.sql
-- =====================================================================
--  Table: llx_guillotine_piece
--  Description: Individual pieces produced by a cut job.
--  Each piece gets a unique scannable ref (PCE-YYYY-NNNN).
--  status: 0=available, 1=assigned (to MO), 2=used, 3=scrapped
-- =====================================================================

CREATE TABLE IF NOT EXISTS llx_guillotine_piece (
  rowid       INT          NOT NULL AUTO_INCREMENT,
  ref         VARCHAR(64)  NOT NULL COMMENT 'Unique scannable ref, e.g. PCE-2026-0001',
  fk_cut      INT          NOT NULL COMMENT 'FK to llx_guillotine_cut',
  fk_mo       INT          DEFAULT NULL COMMENT 'Assigned MO (NULL = available)',
  width       DOUBLE       DEFAULT NULL COMMENT 'Actual width (mm)',
  length      DOUBLE       DEFAULT NULL COMMENT 'Actual length (mm)',
  status      TINYINT      NOT NULL DEFAULT 0 COMMENT '0=available,1=assigned,2=used,3=scrapped',
  entity      INT          NOT NULL DEFAULT 1,
  tms         TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (rowid),
  UNIQUE KEY uk_guillotine_piece_ref (ref, entity),
  KEY idx_guillotine_piece_entity (entity),
  KEY idx_guillotine_piece_cut    (fk_cut),
  KEY idx_guillotine_piece_mo     (fk_mo),
  KEY idx_guillotine_piece_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
