-- name: CreateQcp2Attributes :one
INSERT INTO qcp2_attributes (
     qcp2_id,
     qualityattribute_id,
     percentage,
     qtyremoved,
     qtytaken,
     other
 ) VALUES (
     $1, 
     $2, 
     $3,
     $4,
     $5,
     $6
    ) 
RETURNING *;

 -- name: GetQcp2Attributes :one
SELECT * FROM qcp2_attributes
WHERE id = $1 LIMIT 1;

-- name: ListQcp2Attributes :many
SELECT * FROM qcp2_attributes
ORDER BY id LIMIT $1
OFFSET $2;

-- name: UpdateQcp2Attributes :one
UPDATE qcp2_attributes
  set 
     qcp2_id = $2,
     qualityattribute_id  = $3,
     percentage = $4,
     qtyremoved = $5,
     qtytaken  = $6,
     other = $7
WHERE id = $1
RETURNING *;

-- name: DeleteQcp2Attributes :exec
DELETE FROM qcp2_attributes WHERE qcp2_id= $1;

-- name: AllQcP2Attributes :many
SELECT qualityattribute_id,qtyremoved qty_removed,
crops.cropname,qtytaken qty_taken,other,qcp2.batchno,qcp2.id,
receptionform.traceability_code,qualityattributes.description FROM qcp2_attributes
LEFT JOIN  qcp2 on qcp2.id = qcp2_attributes.qcp2_id
LEFT JOIN  receptionform on receptionform.id = qcp2.reception_id
LEFT JOIN  qualityattributes on qualityattributes.id  = qcp2_attributes.qualityattribute_id
LEFT JOIN  crops on crops.id = receptionform.crop_id
WHERE qcp2_attributes.qcp2_id = $1;

-- name: CheckQcp2ID :many
SELECT * FROM qcp2_attributes
where qcp2_id = $1;
