// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: order.sql

package db

import (
	"context"
	"database/sql"
	"time"
)

const addExtraProductOnOrder = `-- name: AddExtraProductOnOrder :one
INSERT INTO order_crops (
     crop_id,
     order_id,
     weight
  )VALUES(
     $1,
     $2,
     $3
  )RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type AddExtraProductOnOrderParams struct {
	CropID  int32   `json:"crop_id"`
	OrderID int32   `json:"order_id"`
	Weight  float64 `json:"weight"`
}

func (q *Queries) AddExtraProductOnOrder(ctx context.Context, arg AddExtraProductOnOrderParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, addExtraProductOnOrder, arg.CropID, arg.OrderID, arg.Weight)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const checkConsignmentExist = `-- name: CheckConsignmentExist :one
 Select consignment_no from orders WHERE id = $1
`

func (q *Queries) CheckConsignmentExist(ctx context.Context, id int64) (sql.NullString, error) {
	row := q.db.QueryRowContext(ctx, checkConsignmentExist, id)
	var consignment_no sql.NullString
	err := row.Scan(&consignment_no)
	return consignment_no, err
}

const checkifProductExists = `-- name: CheckifProductExists :one
SELECT EXISTS (
    SELECT 1
    FROM order_crops
    WHERE crop_id = $1 
    AND   order_id = $2
)
`

type CheckifProductExistsParams struct {
	CropID  int32 `json:"crop_id"`
	OrderID int32 `json:"order_id"`
}

func (q *Queries) CheckifProductExists(ctx context.Context, arg CheckifProductExistsParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkifProductExists, arg.CropID, arg.OrderID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const createOrder = `-- name: CreateOrder :one
INSERT INTO orders (           
     client_id,
     user_id,
     exportcompany_id
 ) VALUES (
    $1, 
    $2, 
    $3
) 
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type CreateOrderParams struct {
	ClientID        int32 `json:"client_id"`
	UserID          int32 `json:"user_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) CreateOrder(ctx context.Context, arg CreateOrderParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, createOrder, arg.ClientID, arg.UserID, arg.ExportcompanyID)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const cropsOnOrders = `-- name: CropsOnOrders :many
SELECT DISTINCT crops.cropname,crops.id, crops.variant ,order_crops.weight, order_crops.weightreceived FROM order_crops
JOIN   crops on crops.id = order_crops.crop_id
WHERE  order_crops.order_id = $1
`

type CropsOnOrdersRow struct {
	Cropname       string          `json:"cropname"`
	ID             int64           `json:"id"`
	Variant        string          `json:"variant"`
	Weight         float64         `json:"weight"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
}

func (q *Queries) CropsOnOrders(ctx context.Context, orderID int32) ([]CropsOnOrdersRow, error) {
	rows, err := q.db.QueryContext(ctx, cropsOnOrders, orderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []CropsOnOrdersRow{}
	for rows.Next() {
		var i CropsOnOrdersRow
		if err := rows.Scan(
			&i.Cropname,
			&i.ID,
			&i.Variant,
			&i.Weight,
			&i.Weightreceived,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const deleteOrder = `-- name: DeleteOrder :exec
DELETE FROM orders WHERE id= $1
`

func (q *Queries) DeleteOrder(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deleteOrder, id)
	return err
}

const getCompanyWithConsignmentNo = `-- name: GetCompanyWithConsignmentNo :one
SELECT id, client_id, exportcompany_id, consignment_no, status, user_id, created_at FROM orders WHERE consignment_no = $1
`

func (q *Queries) GetCompanyWithConsignmentNo(ctx context.Context, consignmentNo sql.NullString) (Order, error) {
	row := q.db.QueryRowContext(ctx, getCompanyWithConsignmentNo, consignmentNo)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const getConsignmentWithOrderID = `-- name: GetConsignmentWithOrderID :one


 Select id, client_id, exportcompany_id, consignment_no, status, user_id, created_at from orders WHERE id = $1
`

// -- name: GetOrderProductRegime :many
// Select crops.cropname,receptionform.traceability_code,
// qcp2_recommendation.status qcp2recstatus,
// qcp2_recommendation.netweightrejected,qcp2_recommendation.netweightpassed,
// qcp1_recommendation.status qcp1recstatus,qcp1_recommendation.netweightrejected,
// qcp1_recommendation.netweightpassed,qcp1_attributes.qtyRemoved,
// qcp1_attributes.qtyTaken from receptionform
// LEFT JOIN qcp1 on qcp1.receptionform_id = receptionform.id
// LEFT JOIN qcp1_attributes on qcp1.qcp1_id = qcp1.id
// LEFT JOIN qcp1_recommendation ON qcp1_recommendation.qcp1_id = qcp1.id
// LEFT JOIN qcp2 on qcp2.reception_id   = receptionform.id
// LEFT JOIN qcp2_attributes ON qcp2_attributes.qcp2_id = qcp2.id
// LEFT JOIN qcp2_recommendation ON qcp2_recommendation.qcp2_id = qcp2.id
func (q *Queries) GetConsignmentWithOrderID(ctx context.Context, id int64) (Order, error) {
	row := q.db.QueryRowContext(ctx, getConsignmentWithOrderID, id)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const getOrder = `-- name: GetOrder :one
 Select clients.companyname,clients.id as client_id,orders.id as order_id,clients.contactemail,clients.contactphone,
 orders.created_at,orders.user_id as qualitycontroller_id,exportcompanies.expname,users.fullname,
 countries.countryname,exportcompanies.id as exportcompany_id, orders.status,orders.consignment_no from orders
 JOIN clients on clients.id = orders.client_id
 JOIN exportcompanies on exportcompanies.id = orders.exportcompany_id
 JOIN countries on countries.id  = clients.country_id
 JOIN users     on users.id      = orders.user_id
 WHERE orders.id = $1
`

type GetOrderRow struct {
	Companyname         string         `json:"companyname"`
	ClientID            int64          `json:"client_id"`
	OrderID             int64          `json:"order_id"`
	Contactemail        string         `json:"contactemail"`
	Contactphone        string         `json:"contactphone"`
	CreatedAt           time.Time      `json:"created_at"`
	QualitycontrollerID int32          `json:"qualitycontroller_id"`
	Expname             string         `json:"expname"`
	Fullname            string         `json:"fullname"`
	Countryname         string         `json:"countryname"`
	ExportcompanyID     int64          `json:"exportcompany_id"`
	Status              string         `json:"status"`
	ConsignmentNo       sql.NullString `json:"consignment_no"`
}

func (q *Queries) GetOrder(ctx context.Context, id int64) (GetOrderRow, error) {
	row := q.db.QueryRowContext(ctx, getOrder, id)
	var i GetOrderRow
	err := row.Scan(
		&i.Companyname,
		&i.ClientID,
		&i.OrderID,
		&i.Contactemail,
		&i.Contactphone,
		&i.CreatedAt,
		&i.QualitycontrollerID,
		&i.Expname,
		&i.Fullname,
		&i.Countryname,
		&i.ExportcompanyID,
		&i.Status,
		&i.ConsignmentNo,
	)
	return i, err
}

const getOrderDetailCrops = `-- name: GetOrderDetailCrops :many
SELECT orders.id order_id,orders.created_at created_at,crops.id crop_id,countries.id country_id,
clients.companyname,countries.countryname,crops.cropname,crops.variant,crops.crophscode,
crops.riskstatus,order_crops.weightreceived,order_crops.weight,order_crops.status,
crops.cic_code FROM orders
JOIN   clients on clients.id = orders.client_id
JOIN   countries  on countries.id = clients.country_id
JOIN   order_crops on order_crops.order_id = orders.id
JOIN   crops on crops.id = order_crops.crop_id 
WHERE  orders.id = $1 AND order_crops.status='Active'
`

type GetOrderDetailCropsRow struct {
	OrderID        int64           `json:"order_id"`
	CreatedAt      time.Time       `json:"created_at"`
	CropID         int64           `json:"crop_id"`
	CountryID      int64           `json:"country_id"`
	Companyname    string          `json:"companyname"`
	Countryname    string          `json:"countryname"`
	Cropname       string          `json:"cropname"`
	Variant        string          `json:"variant"`
	Crophscode     string          `json:"crophscode"`
	Riskstatus     string          `json:"riskstatus"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
	Weight         float64         `json:"weight"`
	Status         string          `json:"status"`
	CicCode        string          `json:"cic_code"`
}

func (q *Queries) GetOrderDetailCrops(ctx context.Context, id int64) ([]GetOrderDetailCropsRow, error) {
	rows, err := q.db.QueryContext(ctx, getOrderDetailCrops, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetOrderDetailCropsRow{}
	for rows.Next() {
		var i GetOrderDetailCropsRow
		if err := rows.Scan(
			&i.OrderID,
			&i.CreatedAt,
			&i.CropID,
			&i.CountryID,
			&i.Companyname,
			&i.Countryname,
			&i.Cropname,
			&i.Variant,
			&i.Crophscode,
			&i.Riskstatus,
			&i.Weightreceived,
			&i.Weight,
			&i.Status,
			&i.CicCode,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getOrderDetails = `-- name: GetOrderDetails :many
SELECT orders.id order_id,orders.created_at created_at,crops.id crop_id,countries.id country_id,
clients.companyname,countries.countryname,crops.cropname,crops.variant,crops.crophscode,
crops.riskstatus,order_crops.weightreceived,order_crops.weight,order_crops.status,
crops.cic_code FROM orders
JOIN   clients on clients.id = orders.client_id
JOIN   countries  on countries.id = clients.country_id
JOIN   order_crops on order_crops.order_id = orders.id
JOIN   crops on crops.id = order_crops.crop_id 
WHERE  orders.id = $1 
AND    order_crops.status = 'Active'
`

type GetOrderDetailsRow struct {
	OrderID        int64           `json:"order_id"`
	CreatedAt      time.Time       `json:"created_at"`
	CropID         int64           `json:"crop_id"`
	CountryID      int64           `json:"country_id"`
	Companyname    string          `json:"companyname"`
	Countryname    string          `json:"countryname"`
	Cropname       string          `json:"cropname"`
	Variant        string          `json:"variant"`
	Crophscode     string          `json:"crophscode"`
	Riskstatus     string          `json:"riskstatus"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
	Weight         float64         `json:"weight"`
	Status         string          `json:"status"`
	CicCode        string          `json:"cic_code"`
}

func (q *Queries) GetOrderDetails(ctx context.Context, id int64) ([]GetOrderDetailsRow, error) {
	rows, err := q.db.QueryContext(ctx, getOrderDetails, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetOrderDetailsRow{}
	for rows.Next() {
		var i GetOrderDetailsRow
		if err := rows.Scan(
			&i.OrderID,
			&i.CreatedAt,
			&i.CropID,
			&i.CountryID,
			&i.Companyname,
			&i.Countryname,
			&i.Cropname,
			&i.Variant,
			&i.Crophscode,
			&i.Riskstatus,
			&i.Weightreceived,
			&i.Weight,
			&i.Status,
			&i.CicCode,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getOrderDetailsNotRecieved = `-- name: GetOrderDetailsNotRecieved :many
SELECT orders.id order_id,orders.created_at created_at,crops.id crop_id,countries.id country_id,
clients.companyname,countries.countryname,crops.cropname,crops.variant,crops.crophscode,
crops.riskstatus,order_crops.weightreceived,order_crops.weight,order_crops.status,
crops.cic_code FROM orders
JOIN   clients on clients.id = orders.client_id
JOIN   countries  on countries.id = clients.country_id
JOIN   order_crops on order_crops.order_id = orders.id
JOIN   crops on crops.id = order_crops.crop_id 
WHERE  orders.id = $1 
AND    order_crops.status = 'NotRecieved'
`

type GetOrderDetailsNotRecievedRow struct {
	OrderID        int64           `json:"order_id"`
	CreatedAt      time.Time       `json:"created_at"`
	CropID         int64           `json:"crop_id"`
	CountryID      int64           `json:"country_id"`
	Companyname    string          `json:"companyname"`
	Countryname    string          `json:"countryname"`
	Cropname       string          `json:"cropname"`
	Variant        string          `json:"variant"`
	Crophscode     string          `json:"crophscode"`
	Riskstatus     string          `json:"riskstatus"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
	Weight         float64         `json:"weight"`
	Status         string          `json:"status"`
	CicCode        string          `json:"cic_code"`
}

func (q *Queries) GetOrderDetailsNotRecieved(ctx context.Context, id int64) ([]GetOrderDetailsNotRecievedRow, error) {
	rows, err := q.db.QueryContext(ctx, getOrderDetailsNotRecieved, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetOrderDetailsNotRecievedRow{}
	for rows.Next() {
		var i GetOrderDetailsNotRecievedRow
		if err := rows.Scan(
			&i.OrderID,
			&i.CreatedAt,
			&i.CropID,
			&i.CountryID,
			&i.Companyname,
			&i.Countryname,
			&i.Cropname,
			&i.Variant,
			&i.Crophscode,
			&i.Riskstatus,
			&i.Weightreceived,
			&i.Weight,
			&i.Status,
			&i.CicCode,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getOrdersWithConsignment = `-- name: GetOrdersWithConsignment :many
Select orders.id order_id, orders.client_id,users.fullname as qc,
orders.exportcompany_id, countries.countryname,
countries.id country_id,orders.status, 
orders.created_at,COALESCE(packinglist.id, 0) AS packinglist_id,
clients.companyname from orders  
LEFT JOIN    packinglist            on packinglist.order_id                            = orders.id
LEFT JOIN    clients                on clients.id                                      = orders.client_id
LEFT JOIN    countries              on countries.id                                    = clients.country_id
LEFT JOIN    users                  on users.id                                        = orders.user_id
WHERE   orders.consignment_no  = $1
`

type GetOrdersWithConsignmentRow struct {
	OrderID         int64          `json:"order_id"`
	ClientID        int32          `json:"client_id"`
	Qc              sql.NullString `json:"qc"`
	ExportcompanyID int32          `json:"exportcompany_id"`
	Countryname     sql.NullString `json:"countryname"`
	CountryID       sql.NullInt64  `json:"country_id"`
	Status          string         `json:"status"`
	CreatedAt       time.Time      `json:"created_at"`
	PackinglistID   int64          `json:"packinglist_id"`
	Companyname     sql.NullString `json:"companyname"`
}

func (q *Queries) GetOrdersWithConsignment(ctx context.Context, consignmentNo sql.NullString) ([]GetOrdersWithConsignmentRow, error) {
	rows, err := q.db.QueryContext(ctx, getOrdersWithConsignment, consignmentNo)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetOrdersWithConsignmentRow{}
	for rows.Next() {
		var i GetOrdersWithConsignmentRow
		if err := rows.Scan(
			&i.OrderID,
			&i.ClientID,
			&i.Qc,
			&i.ExportcompanyID,
			&i.Countryname,
			&i.CountryID,
			&i.Status,
			&i.CreatedAt,
			&i.PackinglistID,
			&i.Companyname,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const globalConsignments = `-- name: GlobalConsignments :many
SELECT orders.id,orders.consignment_no,orders.created_at,
orders.status,exportcompanies.expname from orders
JOIN exportcompanies on exportcompanies.id =  orders.exportcompany_id
ORDER BY orders.created_at DESC
LIMIT $1 OFFSET $2
`

type GlobalConsignmentsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type GlobalConsignmentsRow struct {
	ID            int64          `json:"id"`
	ConsignmentNo sql.NullString `json:"consignment_no"`
	CreatedAt     time.Time      `json:"created_at"`
	Status        string         `json:"status"`
	Expname       string         `json:"expname"`
}

func (q *Queries) GlobalConsignments(ctx context.Context, arg GlobalConsignmentsParams) ([]GlobalConsignmentsRow, error) {
	rows, err := q.db.QueryContext(ctx, globalConsignments, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GlobalConsignmentsRow{}
	for rows.Next() {
		var i GlobalConsignmentsRow
		if err := rows.Scan(
			&i.ID,
			&i.ConsignmentNo,
			&i.CreatedAt,
			&i.Status,
			&i.Expname,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listOrders = `-- name: ListOrders :many
SELECT companyname,contactname,countryname,
orders.created_at,orders.id order_id, orders.status, 
orders.user_id, orders.consignment_no FROM orders 
JOIN   clients     on clients.id    = orders.client_id
JOIN   countries   on countries.id  = clients.country_id
WHERE  orders.exportcompany_id = $3 
AND    orders.status = $4 
ORDER BY orders.created_at DESC
LIMIT  $1
OFFSET $2
`

type ListOrdersParams struct {
	Limit           int32  `json:"limit"`
	Offset          int32  `json:"offset"`
	ExportcompanyID int32  `json:"exportcompany_id"`
	Status          string `json:"status"`
}

type ListOrdersRow struct {
	Companyname   string         `json:"companyname"`
	Contactname   string         `json:"contactname"`
	Countryname   string         `json:"countryname"`
	CreatedAt     time.Time      `json:"created_at"`
	OrderID       int64          `json:"order_id"`
	Status        string         `json:"status"`
	UserID        int32          `json:"user_id"`
	ConsignmentNo sql.NullString `json:"consignment_no"`
}

func (q *Queries) ListOrders(ctx context.Context, arg ListOrdersParams) ([]ListOrdersRow, error) {
	rows, err := q.db.QueryContext(ctx, listOrders,
		arg.Limit,
		arg.Offset,
		arg.ExportcompanyID,
		arg.Status,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListOrdersRow{}
	for rows.Next() {
		var i ListOrdersRow
		if err := rows.Scan(
			&i.Companyname,
			&i.Contactname,
			&i.Countryname,
			&i.CreatedAt,
			&i.OrderID,
			&i.Status,
			&i.UserID,
			&i.ConsignmentNo,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const orderStatsActive = `-- name: OrderStatsActive :one
  Select COUNT(id) from orders
  WHERE exportcompany_id = $1 
  AND   status in ('Active','UnderInspection')
`

func (q *Queries) OrderStatsActive(ctx context.Context, exportcompanyID int32) (int64, error) {
	row := q.db.QueryRowContext(ctx, orderStatsActive, exportcompanyID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const orderStatsCancelled = `-- name: OrderStatsCancelled :one
Select COUNT(id) from orders
WHERE exportcompany_id = $1 
AND   status = 'Canceled'
`

func (q *Queries) OrderStatsCancelled(ctx context.Context, exportcompanyID int32) (int64, error) {
	row := q.db.QueryRowContext(ctx, orderStatsCancelled, exportcompanyID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const orderStatsCompleted = `-- name: OrderStatsCompleted :one
Select COUNT(id) from orders
WHERE exportcompany_id = $1 
AND   status = 'Completed'
`

func (q *Queries) OrderStatsCompleted(ctx context.Context, exportcompanyID int32) (int64, error) {
	row := q.db.QueryRowContext(ctx, orderStatsCompleted, exportcompanyID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const orderStatsIntercepted = `-- name: OrderStatsIntercepted :one
Select COUNT(id) from orders
WHERE exportcompany_id = $1
AND   status in ('Intercepted','Rejected')
`

func (q *Queries) OrderStatsIntercepted(ctx context.Context, exportcompanyID int32) (int64, error) {
	row := q.db.QueryRowContext(ctx, orderStatsIntercepted, exportcompanyID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const returnOrderByConsignmentNo = `-- name: ReturnOrderByConsignmentNo :many
Select orders.id,orders.created_at,orders.status,clients.companyname,
countries.countryname from orders 
JOIN  clients   on clients.id   = orders.client_id
JOIN  countries on countries.id = clients.country_id
WHERE consignment_no = $1
`

type ReturnOrderByConsignmentNoRow struct {
	ID          int64     `json:"id"`
	CreatedAt   time.Time `json:"created_at"`
	Status      string    `json:"status"`
	Companyname string    `json:"companyname"`
	Countryname string    `json:"countryname"`
}

func (q *Queries) ReturnOrderByConsignmentNo(ctx context.Context, consignmentNo sql.NullString) ([]ReturnOrderByConsignmentNoRow, error) {
	rows, err := q.db.QueryContext(ctx, returnOrderByConsignmentNo, consignmentNo)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnOrderByConsignmentNoRow{}
	for rows.Next() {
		var i ReturnOrderByConsignmentNoRow
		if err := rows.Scan(
			&i.ID,
			&i.CreatedAt,
			&i.Status,
			&i.Companyname,
			&i.Countryname,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const returnOrderByExportCompany = `-- name: ReturnOrderByExportCompany :many
SELECT orders.id order_id,orders.consignment_no,orders.client_id client_id,orders.status,
orders.created_at,clients.companyname,countries.countryname,countries.id country_id,orders.exportcompany_id 
FROM  orders 
JOIN  clients   ON clients.id   = orders.client_id  
JOIN  countries ON countries.id = clients.country_id
WHERE orders.exportcompany_id= $1 AND orders.status = $2
ORDER BY orders.id desc
`

type ReturnOrderByExportCompanyParams struct {
	ExportcompanyID int32  `json:"exportcompany_id"`
	Status          string `json:"status"`
}

type ReturnOrderByExportCompanyRow struct {
	OrderID         int64          `json:"order_id"`
	ConsignmentNo   sql.NullString `json:"consignment_no"`
	ClientID        int32          `json:"client_id"`
	Status          string         `json:"status"`
	CreatedAt       time.Time      `json:"created_at"`
	Companyname     string         `json:"companyname"`
	Countryname     string         `json:"countryname"`
	CountryID       int64          `json:"country_id"`
	ExportcompanyID int32          `json:"exportcompany_id"`
}

func (q *Queries) ReturnOrderByExportCompany(ctx context.Context, arg ReturnOrderByExportCompanyParams) ([]ReturnOrderByExportCompanyRow, error) {
	rows, err := q.db.QueryContext(ctx, returnOrderByExportCompany, arg.ExportcompanyID, arg.Status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnOrderByExportCompanyRow{}
	for rows.Next() {
		var i ReturnOrderByExportCompanyRow
		if err := rows.Scan(
			&i.OrderID,
			&i.ConsignmentNo,
			&i.ClientID,
			&i.Status,
			&i.CreatedAt,
			&i.Companyname,
			&i.Countryname,
			&i.CountryID,
			&i.ExportcompanyID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const returnOrderID = `-- name: ReturnOrderID :one
SELECT orders.id from orders
WHERE  orders.consignment_no=$1
`

func (q *Queries) ReturnOrderID(ctx context.Context, consignmentNo sql.NullString) (int64, error) {
	row := q.db.QueryRowContext(ctx, returnOrderID, consignmentNo)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const searchCompanyOrderByClient = `-- name: SearchCompanyOrderByClient :many
SELECT orders.client_id,orders.id as order_id, 
orders.created_at,orders.user_id,
clients.companyname,countries.countryname,clients.contactname,
orders.status,orders.consignment_no,
orders.exportcompany_id,exportcompanies.expname 
FROM  orders
JOIN  clients on clients.id = orders.client_id
JOIN  exportcompanies on exportcompanies.id = orders.exportcompany_id
JOIN  countries on countries.id = clients.country_id
WHERE orders.exportcompany_id = $1 
AND   orders.status= $3 
AND   LOWER(clients.companyname) LIKE '%' || LOWER($2) || '%'
`

type SearchCompanyOrderByClientParams struct {
	ExportcompanyID int32  `json:"exportcompany_id"`
	Lower           string `json:"lower"`
	Status          string `json:"status"`
}

type SearchCompanyOrderByClientRow struct {
	ClientID        int32          `json:"client_id"`
	OrderID         int64          `json:"order_id"`
	CreatedAt       time.Time      `json:"created_at"`
	UserID          int32          `json:"user_id"`
	Companyname     string         `json:"companyname"`
	Countryname     string         `json:"countryname"`
	Contactname     string         `json:"contactname"`
	Status          string         `json:"status"`
	ConsignmentNo   sql.NullString `json:"consignment_no"`
	ExportcompanyID int32          `json:"exportcompany_id"`
	Expname         string         `json:"expname"`
}

func (q *Queries) SearchCompanyOrderByClient(ctx context.Context, arg SearchCompanyOrderByClientParams) ([]SearchCompanyOrderByClientRow, error) {
	rows, err := q.db.QueryContext(ctx, searchCompanyOrderByClient, arg.ExportcompanyID, arg.Lower, arg.Status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchCompanyOrderByClientRow{}
	for rows.Next() {
		var i SearchCompanyOrderByClientRow
		if err := rows.Scan(
			&i.ClientID,
			&i.OrderID,
			&i.CreatedAt,
			&i.UserID,
			&i.Companyname,
			&i.Countryname,
			&i.Contactname,
			&i.Status,
			&i.ConsignmentNo,
			&i.ExportcompanyID,
			&i.Expname,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchGlobalByConsignmentNo = `-- name: SearchGlobalByConsignmentNo :many
SELECT orders.id,orders.consignment_no,orders.created_at,
orders.status,exportcompanies.expname from orders
JOIN exportcompanies on exportcompanies.id =  orders.exportcompany_id
WHERE LOWER(orders.consignment_no) LIKE '%' || LOWER($1) || '%'
ORDER BY orders.created_at DESC
`

type SearchGlobalByConsignmentNoRow struct {
	ID            int64          `json:"id"`
	ConsignmentNo sql.NullString `json:"consignment_no"`
	CreatedAt     time.Time      `json:"created_at"`
	Status        string         `json:"status"`
	Expname       string         `json:"expname"`
}

func (q *Queries) SearchGlobalByConsignmentNo(ctx context.Context, lower string) ([]SearchGlobalByConsignmentNoRow, error) {
	rows, err := q.db.QueryContext(ctx, searchGlobalByConsignmentNo, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchGlobalByConsignmentNoRow{}
	for rows.Next() {
		var i SearchGlobalByConsignmentNoRow
		if err := rows.Scan(
			&i.ID,
			&i.ConsignmentNo,
			&i.CreatedAt,
			&i.Status,
			&i.Expname,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchOrderByConsignmentNo = `-- name: SearchOrderByConsignmentNo :many
SELECT id, client_id, exportcompany_id, consignment_no, status, user_id, created_at FROM orders
WHERE exportcompany_id = $1
AND   status= $2
AND   LOWER(consignment_no) LIKE '%' || LOWER($3) || '%'
`

type SearchOrderByConsignmentNoParams struct {
	ExportcompanyID int32  `json:"exportcompany_id"`
	Status          string `json:"status"`
	Lower           string `json:"lower"`
}

func (q *Queries) SearchOrderByConsignmentNo(ctx context.Context, arg SearchOrderByConsignmentNoParams) ([]Order, error) {
	rows, err := q.db.QueryContext(ctx, searchOrderByConsignmentNo, arg.ExportcompanyID, arg.Status, arg.Lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Order{}
	for rows.Next() {
		var i Order
		if err := rows.Scan(
			&i.ID,
			&i.ClientID,
			&i.ExportcompanyID,
			&i.ConsignmentNo,
			&i.Status,
			&i.UserID,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const showTopCompaniesPrevious = `-- name: ShowTopCompaniesPrevious :many
SELECT
  ec.expname,
  COALESCE(SUM(oc.weightreceived), 0) AS total_weight_exported
FROM
  exportcompanies ec
LEFT JOIN
  orders o ON o.exportcompany_id = ec.id
  AND o.status IN ('Completed')
  AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month')
LEFT JOIN
  order_crops oc ON o.id = oc.order_id
GROUP BY
  ec.expname
ORDER BY
  total_weight_exported DESC
LIMIT 5
`

type ShowTopCompaniesPreviousRow struct {
	Expname             string      `json:"expname"`
	TotalWeightExported interface{} `json:"total_weight_exported"`
}

func (q *Queries) ShowTopCompaniesPrevious(ctx context.Context) ([]ShowTopCompaniesPreviousRow, error) {
	rows, err := q.db.QueryContext(ctx, showTopCompaniesPrevious)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ShowTopCompaniesPreviousRow{}
	for rows.Next() {
		var i ShowTopCompaniesPreviousRow
		if err := rows.Scan(&i.Expname, &i.TotalWeightExported); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const showTotalTopCompanies = `-- name: ShowTotalTopCompanies :many
SELECT
  ec.expname,
  COALESCE(SUM(oc.weight), 0)            AS total_weight_requested,
  COALESCE(SUM(oc.weightreceived), 0)    AS total_weight_exported
FROM
  exportcompanies ec
LEFT JOIN
  orders o 
    ON o.exportcompany_id = ec.id
   AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE)
LEFT JOIN
  order_crops oc 
    ON oc.order_id = o.id 
   AND o.status IN ('Completed')
GROUP BY
  ec.expname
ORDER BY
  total_weight_exported DESC
LIMIT 5
`

type ShowTotalTopCompaniesRow struct {
	Expname              string      `json:"expname"`
	TotalWeightRequested interface{} `json:"total_weight_requested"`
	TotalWeightExported  interface{} `json:"total_weight_exported"`
}

func (q *Queries) ShowTotalTopCompanies(ctx context.Context) ([]ShowTotalTopCompaniesRow, error) {
	rows, err := q.db.QueryContext(ctx, showTotalTopCompanies)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ShowTotalTopCompaniesRow{}
	for rows.Next() {
		var i ShowTotalTopCompaniesRow
		if err := rows.Scan(&i.Expname, &i.TotalWeightRequested, &i.TotalWeightExported); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const showTotalWeightExported = `-- name: ShowTotalWeightExported :one
SELECT 
  CAST(SUM(order_crops.weightreceived) AS DOUBLE PRECISION) AS total_weight_exported
FROM 
  order_crops
JOIN orders 
  ON orders.id = order_crops.order_id
WHERE 
  orders.status = 'Completed'
  AND DATE_TRUNC('month', orders.created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) ShowTotalWeightExported(ctx context.Context) (float64, error) {
	row := q.db.QueryRowContext(ctx, showTotalWeightExported)
	var total_weight_exported float64
	err := row.Scan(&total_weight_exported)
	return total_weight_exported, err
}

const showTotalWeightExporterPrevious = `-- name: ShowTotalWeightExporterPrevious :one
SELECT 
  CAST(SUM(order_crops.weightreceived) AS BIGINT) AS total_weight_exported
FROM 
  order_crops
JOIN 
  orders ON orders.id = order_crops.order_id
WHERE 
  orders.status = 'Completed' AND
  DATE_TRUNC('month', orders.created_at) = DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month')
`

func (q *Queries) ShowTotalWeightExporterPrevious(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, showTotalWeightExporterPrevious)
	var total_weight_exported int64
	err := row.Scan(&total_weight_exported)
	return total_weight_exported, err
}

const showTotalWeightReceived = `-- name: ShowTotalWeightReceived :many
WITH country_crop_totals AS (
    SELECT
        countries.id AS country_id,
        countries.countryname AS Countryname,
        crops.id AS crop_id,
        crops.cropname AS Cropname,
        SUM(order_crops.weightreceived) AS TotalWeightReceived,
        ROW_NUMBER() OVER (
            PARTITION BY countries.id
            ORDER BY SUM(order_crops.weightreceived) DESC
        ) AS crop_rank
    FROM 
        order_crops
    JOIN orders ON orders.id = order_crops.order_id
    JOIN clients ON clients.id = orders.client_id
    JOIN countries ON countries.id = clients.country_id
    JOIN crops ON crops.id = order_crops.crop_id
    WHERE 
        order_crops.weightreceived IS NOT NULL
        AND orders.status = 'Completed'
    GROUP BY 
        countries.id, countries.countryname, crops.id, crops.cropname
),
country_totals AS (
    SELECT 
        country_id,
        countryname AS Countryname,
        SUM(total_weight_received) AS TotalCountryWeight
    FROM country_crop_totals
    GROUP BY country_id, countryname
)
SELECT 
    cct.Countryname,
    cct.Cropname,
    cct.TotalWeightReceived,
    ct.TotalCountryWeight
FROM 
    country_crop_totals cct
JOIN 
    country_totals ct ON ct.country_id = cct.country_id
WHERE 
    cct.crop_rank <= 2
ORDER BY 
    ct.TotalCountryWeight DESC,
    cct.Countryname,
    cct.TotalWeightReceived DESC
`

type ShowTotalWeightReceivedRow struct {
	Countryname         string `json:"countryname"`
	Cropname            string `json:"cropname"`
	Totalweightreceived int64  `json:"totalweightreceived"`
	Totalcountryweight  int64  `json:"totalcountryweight"`
}

func (q *Queries) ShowTotalWeightReceived(ctx context.Context) ([]ShowTotalWeightReceivedRow, error) {
	rows, err := q.db.QueryContext(ctx, showTotalWeightReceived)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ShowTotalWeightReceivedRow{}
	for rows.Next() {
		var i ShowTotalWeightReceivedRow
		if err := rows.Scan(
			&i.Countryname,
			&i.Cropname,
			&i.Totalweightreceived,
			&i.Totalcountryweight,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const topDestinations = `-- name: TopDestinations :many
SELECT 
    cs.countryname AS destination_country,
    COUNT(DISTINCT o.id) AS total_orders,
    COALESCE(SUM(oc.weightreceived), 0) AS total_weight_received
FROM 
    orders o
JOIN 
    clients c ON o.client_id = c.id
JOIN 
    countries cs ON cs.id = c.country_id
LEFT JOIN 
    order_crops oc ON o.id = oc.order_id
WHERE 
    o.status = 'Completed' 
    AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
    cs.countryname
ORDER BY 
    total_weight_received DESC
LIMIT 5
`

type TopDestinationsRow struct {
	DestinationCountry  string      `json:"destination_country"`
	TotalOrders         int64       `json:"total_orders"`
	TotalWeightReceived interface{} `json:"total_weight_received"`
}

func (q *Queries) TopDestinations(ctx context.Context) ([]TopDestinationsRow, error) {
	rows, err := q.db.QueryContext(ctx, topDestinations)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TopDestinationsRow{}
	for rows.Next() {
		var i TopDestinationsRow
		if err := rows.Scan(&i.DestinationCountry, &i.TotalOrders, &i.TotalWeightReceived); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const topDestinationsTotalWeight = `-- name: TopDestinationsTotalWeight :one
SELECT 
  COALESCE(SUM(oc.weightreceived), 0) AS total_weight_received
FROM 
  orders o
LEFT JOIN 
  order_crops oc ON o.id = oc.order_id
WHERE 
  o.status = 'Completed'
  AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TopDestinationsTotalWeight(ctx context.Context) (interface{}, error) {
	row := q.db.QueryRowContext(ctx, topDestinationsTotalWeight)
	var total_weight_received interface{}
	err := row.Scan(&total_weight_received)
	return total_weight_received, err
}

const topDestinationsWithWeight = `-- name: TopDestinationsWithWeight :many
SELECT 
  cs.countryname AS destination_country,
  COUNT(DISTINCT o.id) AS total_orders, 
  SUM(oc.weightreceived) AS total_weight_exported
FROM  
  orders o
JOIN  
  order_crops oc ON o.id = oc.order_id
JOIN  
  clients c ON o.client_id = c.id
JOIN  
  countries cs ON cs.id = c.country_id
WHERE 
  o.status = 'Completed'
  AND oc.status = 'Active'
  AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  cs.countryname
ORDER BY 
  total_weight_exported DESC
LIMIT 5
`

type TopDestinationsWithWeightRow struct {
	DestinationCountry  string `json:"destination_country"`
	TotalOrders         int64  `json:"total_orders"`
	TotalWeightExported int64  `json:"total_weight_exported"`
}

func (q *Queries) TopDestinationsWithWeight(ctx context.Context) ([]TopDestinationsWithWeightRow, error) {
	rows, err := q.db.QueryContext(ctx, topDestinationsWithWeight)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TopDestinationsWithWeightRow{}
	for rows.Next() {
		var i TopDestinationsWithWeightRow
		if err := rows.Scan(&i.DestinationCountry, &i.TotalOrders, &i.TotalWeightExported); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const topProductsExported = `-- name: TopProductsExported :many
SELECT 
  oc.crop_id, 
  c.cropname,
  COUNT(*) AS crop_appearance_count,
  COALESCE(SUM(oc.weightreceived), 0) AS total_weight_received
FROM 
  order_crops oc
JOIN crops c ON c.id = oc.crop_id
JOIN orders o ON o.id = oc.order_id
WHERE 
  oc.status = 'Active' 
  AND o.status = 'Completed'
  AND DATE_TRUNC('month', o.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  oc.crop_id, 
  c.cropname
ORDER BY 
  crop_appearance_count DESC, 
  total_weight_received DESC
LIMIT 5
`

type TopProductsExportedRow struct {
	CropID              int32       `json:"crop_id"`
	Cropname            string      `json:"cropname"`
	CropAppearanceCount int64       `json:"crop_appearance_count"`
	TotalWeightReceived interface{} `json:"total_weight_received"`
}

func (q *Queries) TopProductsExported(ctx context.Context) ([]TopProductsExportedRow, error) {
	rows, err := q.db.QueryContext(ctx, topProductsExported)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TopProductsExportedRow{}
	for rows.Next() {
		var i TopProductsExportedRow
		if err := rows.Scan(
			&i.CropID,
			&i.Cropname,
			&i.CropAppearanceCount,
			&i.TotalWeightReceived,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalActiveOrders = `-- name: TotalActiveOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Active'
  AND DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalActiveOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalActiveOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCanceledOrders = `-- name: TotalCanceledOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Canceled'
  AND DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalCanceledOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCanceledOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCompletedOrders = `-- name: TotalCompletedOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Completed'
  AND DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalCompletedOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCompletedOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeActiveOrders = `-- name: TotalCumulativeActiveOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Active'
`

func (q *Queries) TotalCumulativeActiveOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeActiveOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeCanceledOrders = `-- name: TotalCumulativeCanceledOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Canceled'
`

func (q *Queries) TotalCumulativeCanceledOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeCanceledOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeCompletedOrders = `-- name: TotalCumulativeCompletedOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Completed'
`

func (q *Queries) TotalCumulativeCompletedOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeCompletedOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeGlobalOrders = `-- name: TotalCumulativeGlobalOrders :one
SELECT  COUNT(*) from orders
`

func (q *Queries) TotalCumulativeGlobalOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeGlobalOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeInterceptedOrders = `-- name: TotalCumulativeInterceptedOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Intercepted'
`

func (q *Queries) TotalCumulativeInterceptedOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeInterceptedOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalCumulativeOrdersUnderInspection = `-- name: TotalCumulativeOrdersUnderInspection :one
SELECT COUNT(*)
FROM orders
WHERE status = 'UnderInspection'
`

func (q *Queries) TotalCumulativeOrdersUnderInspection(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalCumulativeOrdersUnderInspection)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalGlobalCountriesByCrop = `-- name: TotalGlobalCountriesByCrop :many
SELECT 
  oc.crop_id, 
    c.cropname,
    COUNT(*) AS crop_appearance_count,
    SUM(oc.weightreceived) AS total_weight_received
FROM 
    order_crops oc
JOIN 
    crops   c ON c.id   = oc.crop_id
JOIN orders o ON o.id   = oc.order_id

WHERE 
    oc.status = 'Active' 
AND o.status='Completed'
GROUP BY 
    oc.crop_id, 
    c.cropname
ORDER BY 
    crop_appearance_count DESC, 
    total_weight_received DESC
LIMIT 5
`

type TotalGlobalCountriesByCropRow struct {
	CropID              int32  `json:"crop_id"`
	Cropname            string `json:"cropname"`
	CropAppearanceCount int64  `json:"crop_appearance_count"`
	TotalWeightReceived int64  `json:"total_weight_received"`
}

func (q *Queries) TotalGlobalCountriesByCrop(ctx context.Context) ([]TotalGlobalCountriesByCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalGlobalCountriesByCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalGlobalCountriesByCropRow{}
	for rows.Next() {
		var i TotalGlobalCountriesByCropRow
		if err := rows.Scan(
			&i.CropID,
			&i.Cropname,
			&i.CropAppearanceCount,
			&i.TotalWeightReceived,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalGlobalCountriesPerCrop = `-- name: TotalGlobalCountriesPerCrop :many
SELECT 
  crops.id AS crop_id,
  crops.cropname,
  countries.countryname, 
  SUM(order_crops.weightreceived) AS total_weight_received 
FROM 
  order_crops 
JOIN orders ON orders.id = order_crops.order_id
JOIN clients ON clients.id = orders.client_id
JOIN countries ON countries.id = clients.country_id
JOIN crops ON crops.id = order_crops.crop_id
WHERE 
  order_crops.crop_id = $1
  AND order_crops.weightreceived IS NOT NULL 
  AND orders.status = 'Completed' 
GROUP BY 
  crops.id, crops.cropname, countries.countryname 
ORDER BY 
  total_weight_received DESC 
LIMIT 2
`

type TotalGlobalCountriesPerCropRow struct {
	CropID              int64  `json:"crop_id"`
	Cropname            string `json:"cropname"`
	Countryname         string `json:"countryname"`
	TotalWeightReceived int64  `json:"total_weight_received"`
}

func (q *Queries) TotalGlobalCountriesPerCrop(ctx context.Context, cropID int32) ([]TotalGlobalCountriesPerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalGlobalCountriesPerCrop, cropID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalGlobalCountriesPerCropRow{}
	for rows.Next() {
		var i TotalGlobalCountriesPerCropRow
		if err := rows.Scan(
			&i.CropID,
			&i.Cropname,
			&i.Countryname,
			&i.TotalWeightReceived,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalGlobalFarmers = `-- name: TotalGlobalFarmers :one
SELECT  COUNT(*) from farmers
WHERE DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalGlobalFarmers(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalGlobalFarmers)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalGlobalOrders = `-- name: TotalGlobalOrders :one
SELECT  COUNT(*) from orders
WHERE DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalGlobalOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalGlobalOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalGlobalOrdersUnderInspectionCompleted = `-- name: TotalGlobalOrdersUnderInspectionCompleted :many
SELECT
    crops.cropname,
    SUM(qcp5_products.totalproductnetweight) AS total_productnetweight
FROM
    qcp5_products
JOIN 
    qcp5 on qcp5.id = qcp5_products.qcp5_id
JOIN 
    packinglist on packinglist.id = qcp5.packinglist_id
JOIN 
    orders      on orders.id = packinglist.order_id
JOIN
    crops       on crops.id = qcp5_products.product_id
WHERE
    orders.status IN ('Completed', 'UnderInspection')
GROUP BY
    crops.cropname
ORDER BY
    total_productnetweight DESC LIMIT 5
`

type TotalGlobalOrdersUnderInspectionCompletedRow struct {
	Cropname              string `json:"cropname"`
	TotalProductnetweight int64  `json:"total_productnetweight"`
}

func (q *Queries) TotalGlobalOrdersUnderInspectionCompleted(ctx context.Context) ([]TotalGlobalOrdersUnderInspectionCompletedRow, error) {
	rows, err := q.db.QueryContext(ctx, totalGlobalOrdersUnderInspectionCompleted)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalGlobalOrdersUnderInspectionCompletedRow{}
	for rows.Next() {
		var i TotalGlobalOrdersUnderInspectionCompletedRow
		if err := rows.Scan(&i.Cropname, &i.TotalProductnetweight); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalGlobalProductNetweightPerQcp5 = `-- name: TotalGlobalProductNetweightPerQcp5 :many
SELECT
    crops.cropname,
    SUM(qcp5_products.productnetweight) AS total_productnetweight
FROM
    qcp5_products
JOIN
    crops ON crops.id = qcp5_products.product_id
WHERE
    qcp5_products.status = 'FitForExport'
GROUP BY
    crops.cropname
ORDER BY
    total_productnetweight DESC LIMIT 5
`

type TotalGlobalProductNetweightPerQcp5Row struct {
	Cropname              string `json:"cropname"`
	TotalProductnetweight int64  `json:"total_productnetweight"`
}

func (q *Queries) TotalGlobalProductNetweightPerQcp5(ctx context.Context) ([]TotalGlobalProductNetweightPerQcp5Row, error) {
	rows, err := q.db.QueryContext(ctx, totalGlobalProductNetweightPerQcp5)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalGlobalProductNetweightPerQcp5Row{}
	for rows.Next() {
		var i TotalGlobalProductNetweightPerQcp5Row
		if err := rows.Scan(&i.Cropname, &i.TotalProductnetweight); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalGlobalTraceabilities = `-- name: TotalGlobalTraceabilities :one
SELECT COUNT(*) from farmer_farms 
WHERE DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalGlobalTraceabilities(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalGlobalTraceabilities)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalInterceptedOrders = `-- name: TotalInterceptedOrders :one
SELECT COUNT(*)
FROM orders
WHERE status = 'Intercepted'
  AND DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalInterceptedOrders(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalInterceptedOrders)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalOrdersUnderInspection = `-- name: TotalOrdersUnderInspection :one
SELECT COUNT(*)
FROM orders
WHERE status = 'UnderInspection'
  AND DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalOrdersUnderInspection(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalOrdersUnderInspection)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const totalQcp1WastegePerCrop = `-- name: TotalQcp1WastegePerCrop :many
SELECT  
  crops.id AS crop_id, 
  crops.cropname, 
  CAST(SUM(qcp1_recommendation.netweightrejected) AS  BIGINT) AS total_net_weight_rejected
FROM  
  qcp1_recommendation
JOIN  
  qcp1 ON qcp1.id = qcp1_recommendation.qcp1_id
JOIN  
  receptionform ON receptionform.id = qcp1.receptionform_id
JOIN  
  crops ON crops.id = receptionform.crop_id
WHERE 
  DATE_TRUNC('month', qcp1.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  crops.id, crops.cropname 
ORDER BY 
  total_net_weight_rejected DESC 
LIMIT 5
`

type TotalQcp1WastegePerCropRow struct {
	CropID                 int64  `json:"crop_id"`
	Cropname               string `json:"cropname"`
	TotalNetWeightRejected int64  `json:"total_net_weight_rejected"`
}

func (q *Queries) TotalQcp1WastegePerCrop(ctx context.Context) ([]TotalQcp1WastegePerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalQcp1WastegePerCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalQcp1WastegePerCropRow{}
	for rows.Next() {
		var i TotalQcp1WastegePerCropRow
		if err := rows.Scan(&i.CropID, &i.Cropname, &i.TotalNetWeightRejected); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalQcp1WeightRejected = `-- name: TotalQcp1WeightRejected :one
SELECT 
  CAST(SUM(qcp1_recommendation.netweightrejected) AS BIGINT) AS total_weight_rejected
FROM 
  qcp1_recommendation
WHERE 
  DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalQcp1WeightRejected(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalQcp1WeightRejected)
	var total_weight_rejected int64
	err := row.Scan(&total_weight_rejected)
	return total_weight_rejected, err
}

const totalQcp2WastegePerCrop = `-- name: TotalQcp2WastegePerCrop :many
SELECT  
  crops.id AS crop_id, 
  crops.cropname, 
  CAST(SUM(qcp2_recommendation.netweightrejected) AS  BIGINT) AS total_net_weight_rejected
FROM  
  qcp2_recommendation
JOIN  
  qcp2 ON qcp2.reception_id = qcp2_recommendation.qcp2_id
JOIN  
  receptionform ON receptionform.id = qcp2.reception_id
JOIN  
  crops ON crops.id = receptionform.crop_id
WHERE 
  DATE_TRUNC('month', qcp2.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  crops.id, crops.cropname 
ORDER BY 
  total_net_weight_rejected DESC 
LIMIT 5
`

type TotalQcp2WastegePerCropRow struct {
	CropID                 int64  `json:"crop_id"`
	Cropname               string `json:"cropname"`
	TotalNetWeightRejected int64  `json:"total_net_weight_rejected"`
}

func (q *Queries) TotalQcp2WastegePerCrop(ctx context.Context) ([]TotalQcp2WastegePerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalQcp2WastegePerCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalQcp2WastegePerCropRow{}
	for rows.Next() {
		var i TotalQcp2WastegePerCropRow
		if err := rows.Scan(&i.CropID, &i.Cropname, &i.TotalNetWeightRejected); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalQcp2WeightRejected = `-- name: TotalQcp2WeightRejected :one
SELECT 
  CAST(SUM(qcp2_recommendation.netweightrejected) AS BIGINT) AS total_weight_rejected
FROM 
  qcp2_recommendation
WHERE 
  DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalQcp2WeightRejected(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalQcp2WeightRejected)
	var total_weight_rejected int64
	err := row.Scan(&total_weight_rejected)
	return total_weight_rejected, err
}

const totalQcp3WastegePerCrop = `-- name: TotalQcp3WastegePerCrop :many
SELECT  
  crops.id AS crop_id, 
  crops.cropname, 
  CAST(SUM(qcp3_recommendation.netweightrejected) AS  BIGINT) AS total_net_weight_rejected
FROM  
  qcp3_recommendation
JOIN  
  qcp3 ON qcp3.reception_id = qcp3_recommendation.qcp3_id
JOIN  
  receptionform ON receptionform.id = qcp3.reception_id
JOIN  
  crops ON crops.id = receptionform.crop_id
WHERE 
  DATE_TRUNC('month', qcp3.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  crops.id, crops.cropname 
ORDER BY 
  total_net_weight_rejected DESC 
LIMIT 5
`

type TotalQcp3WastegePerCropRow struct {
	CropID                 int64  `json:"crop_id"`
	Cropname               string `json:"cropname"`
	TotalNetWeightRejected int64  `json:"total_net_weight_rejected"`
}

func (q *Queries) TotalQcp3WastegePerCrop(ctx context.Context) ([]TotalQcp3WastegePerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalQcp3WastegePerCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalQcp3WastegePerCropRow{}
	for rows.Next() {
		var i TotalQcp3WastegePerCropRow
		if err := rows.Scan(&i.CropID, &i.Cropname, &i.TotalNetWeightRejected); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalQcp3WeightRejected = `-- name: TotalQcp3WeightRejected :one
SELECT 
  CAST(SUM(qcp3_recommendation.netweightrejected) AS BIGINT) AS total_weight_rejected
FROM 
  qcp3_recommendation
WHERE 
  DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalQcp3WeightRejected(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalQcp3WeightRejected)
	var total_weight_rejected int64
	err := row.Scan(&total_weight_rejected)
	return total_weight_rejected, err
}

const totalQcp4WastegePerCrop = `-- name: TotalQcp4WastegePerCrop :many
SELECT  
  CAST(SUM(weight_rejected) AS  BIGINT) AS total_net_weight_rejected, 
  crops.cropname, 
  crops.id 
FROM 
  qcp4_recommendations
JOIN 
  qcp4_products ON qcp4_products.id = qcp4_recommendations.qcp4_product_id
JOIN 
  crops ON crops.id = qcp4_products.product_id
WHERE 
  DATE_TRUNC('month', qcp4_recommendations.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  crops.id, crops.cropname 
ORDER BY 
  total_net_weight_rejected DESC 
LIMIT 5
`

type TotalQcp4WastegePerCropRow struct {
	TotalNetWeightRejected int64  `json:"total_net_weight_rejected"`
	Cropname               string `json:"cropname"`
	ID                     int64  `json:"id"`
}

func (q *Queries) TotalQcp4WastegePerCrop(ctx context.Context) ([]TotalQcp4WastegePerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalQcp4WastegePerCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalQcp4WastegePerCropRow{}
	for rows.Next() {
		var i TotalQcp4WastegePerCropRow
		if err := rows.Scan(&i.TotalNetWeightRejected, &i.Cropname, &i.ID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalQcp4WeightRejected = `-- name: TotalQcp4WeightRejected :one
SELECT  
  CAST(SUM(weight_rejected) AS  BIGINT) AS total_weight_rejected 
FROM 
  qcp4_recommendations
WHERE 
  DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalQcp4WeightRejected(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalQcp4WeightRejected)
	var total_weight_rejected int64
	err := row.Scan(&total_weight_rejected)
	return total_weight_rejected, err
}

const totalQcp5WastegePerCrop = `-- name: TotalQcp5WastegePerCrop :many
SELECT  
  CAST(SUM(weight_rejected) AS  BIGINT) AS total_net_weight_rejected, 
  crops.cropname, 
  crops.id 
FROM 
  qcp5_recommendations
JOIN 
  qcp5_products ON qcp5_products.id = qcp5_recommendations.qcp5_product_id
JOIN 
  crops ON crops.id = qcp5_products.product_id
WHERE 
  DATE_TRUNC('month', qcp5_recommendations.created_at) = DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
  crops.id, crops.cropname 
ORDER BY 
  total_net_weight_rejected DESC 
LIMIT 5
`

type TotalQcp5WastegePerCropRow struct {
	TotalNetWeightRejected int64  `json:"total_net_weight_rejected"`
	Cropname               string `json:"cropname"`
	ID                     int64  `json:"id"`
}

func (q *Queries) TotalQcp5WastegePerCrop(ctx context.Context) ([]TotalQcp5WastegePerCropRow, error) {
	rows, err := q.db.QueryContext(ctx, totalQcp5WastegePerCrop)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []TotalQcp5WastegePerCropRow{}
	for rows.Next() {
		var i TotalQcp5WastegePerCropRow
		if err := rows.Scan(&i.TotalNetWeightRejected, &i.Cropname, &i.ID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const totalQcp5WeightRejected = `-- name: TotalQcp5WeightRejected :one
SELECT  
  CAST(SUM(weight_rejected) AS  BIGINT) AS total_weight_rejected 
FROM 
  qcp5_recommendations
WHERE 
  DATE_TRUNC('month', created_at) = DATE_TRUNC('month', CURRENT_DATE)
`

func (q *Queries) TotalQcp5WeightRejected(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, totalQcp5WeightRejected)
	var total_weight_rejected int64
	err := row.Scan(&total_weight_rejected)
	return total_weight_rejected, err
}

const updateOrder = `-- name: UpdateOrder :one
UPDATE orders
  set 
     client_id = $2
WHERE id = $1
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type UpdateOrderParams struct {
	ID       int64 `json:"id"`
	ClientID int32 `json:"client_id"`
}

func (q *Queries) UpdateOrder(ctx context.Context, arg UpdateOrderParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, updateOrder, arg.ID, arg.ClientID)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderConsignment = `-- name: UpdateOrderConsignment :one
UPDATE orders
  set 
    consignment_no = COALESCE($2, consignment_no)
WHERE id = $1
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type UpdateOrderConsignmentParams struct {
	ID            int64          `json:"id"`
	ConsignmentNo sql.NullString `json:"consignment_no"`
}

func (q *Queries) UpdateOrderConsignment(ctx context.Context, arg UpdateOrderConsignmentParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, updateOrderConsignment, arg.ID, arg.ConsignmentNo)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderConsignmentStatus = `-- name: UpdateOrderConsignmentStatus :one
UPDATE orders
  set 
     status = $2
WHERE consignment_no = $1
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type UpdateOrderConsignmentStatusParams struct {
	ConsignmentNo sql.NullString `json:"consignment_no"`
	Status        string         `json:"status"`
}

func (q *Queries) UpdateOrderConsignmentStatus(ctx context.Context, arg UpdateOrderConsignmentStatusParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, updateOrderConsignmentStatus, arg.ConsignmentNo, arg.Status)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderStatus = `-- name: UpdateOrderStatus :one
UPDATE orders
  set 
     status = $2
WHERE id = $1
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type UpdateOrderStatusParams struct {
	ID     int64  `json:"id"`
	Status string `json:"status"`
}

func (q *Queries) UpdateOrderStatus(ctx context.Context, arg UpdateOrderStatusParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, updateOrderStatus, arg.ID, arg.Status)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderStatusByConsignment = `-- name: UpdateOrderStatusByConsignment :one
UPDATE orders
  set 
     status = $2
WHERE consignment_no = $1
RETURNING id, client_id, exportcompany_id, consignment_no, status, user_id, created_at
`

type UpdateOrderStatusByConsignmentParams struct {
	ConsignmentNo sql.NullString `json:"consignment_no"`
	Status        string         `json:"status"`
}

func (q *Queries) UpdateOrderStatusByConsignment(ctx context.Context, arg UpdateOrderStatusByConsignmentParams) (Order, error) {
	row := q.db.QueryRowContext(ctx, updateOrderStatusByConsignment, arg.ConsignmentNo, arg.Status)
	var i Order
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ExportcompanyID,
		&i.ConsignmentNo,
		&i.Status,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}
