-- name: CreateReceptionAttribute :one
INSERT INTO reception_attributes (           
    qualityattribute_id,
    qtyremoved,
    qtytaken,
    others,
    receptionform_id,
    percentage
 ) VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6
) 
RETURNING *;

 -- name: GetReceptionAttribute :one
SELECT * FROM reception_attributes
WHERE id = $1 LIMIT 1;

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

-- name: UpdateReceptionAttribute :one
UPDATE reception_attributes
  set 
    qualityattribute_id = COALESCE(sqlc.narg(qualityattribute_id), qualityattribute_id),
    others              = COALESCE(sqlc.narg(others), others),
    qtyremoved          = COALESCE(sqlc.narg(qtyremoved), qtyremoved),
    qtytaken            = COALESCE(sqlc.narg(qtytaken), qtytaken),
    receptionform_id    =  sqlc.arg(receptionform_id),
    percentage          =  sqlc.arg(percentage)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteReceptionAttribute :exec
DELETE FROM reception_attributes WHERE id= $1;

-- name: GetReceptionFormAttributes :many
SELECT  qtyremoved,qtytaken,reception_attributes.id reception_attribute_id,others,percentage,qualityattributes.description,qualityattribute_id as qualityattributes_id 
FROM    reception_attributes
JOIN    qualityattributes on qualityattributes.id = reception_attributes.qualityattribute_id
WHERE   receptionform_id = $1;

-- name: DeleteReceptionAttributeByRecepForm :exec
DELETE FROM reception_attributes WHERE receptionform_id= $1;