-- P5-6129 Save market and locale to orders

-- Add new order fields
INSERT INTO "order-fields" (name) VALUES ('market'), ('locale');

-- Add to orders table
ALTER TABLE orders
    ADD COLUMN market text,
    ADD COLUMN locale text
;

-- Update trigger to include new fields
CREATE OR REPLACE FUNCTION function_copy_order_field() RETURNS TRIGGER AS
$BODY$
BEGIN
    -- comment
    IF new.fieldid = 203 THEN
        UPDATE orders SET comment = new.value WHERE id = new.orderid;
    -- country code
    ELSIF new.fieldid = 151 THEN
        UPDATE orders SET country_code = new.value WHERE id = new.orderid;
    -- delivery method
    ELSIF new.fieldid = 152 THEN
        UPDATE orders SET delivery_method = new.value WHERE id = new.orderid;
    -- delivery price
    ELSIF new.fieldid = 153 THEN
        UPDATE orders SET delivery_price = new.value::numeric WHERE id = new.orderid;
    -- delivery country code
    ELSIF new.fieldid = 213 THEN
        UPDATE orders SET delivery_country_code = new.value WHERE id = new.orderid;
    -- delivery vat
    ELSIF new.fieldid = 154 THEN
        UPDATE orders SET delivery_vat = new.value::numeric WHERE id = new.orderid;
    -- attention
    ELSIF new.fieldid = 162 THEN
        UPDATE orders SET attention = new.value WHERE id = new.orderid;
    -- paid
    ELSIF new.fieldid = 168 THEN
        UPDATE orders SET paid = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- currency
    ELSIF new.fieldid = 169 THEN
        UPDATE orders SET currency = new.value WHERE id = new.orderid;
    -- payment type
    ELSIF new.fieldid = 170 THEN
        UPDATE orders SET payment_type = new.value WHERE id = new.orderid;
    -- exchange rate
    ELSIF new.fieldid = 171 THEN
        UPDATE orders SET exchange_rate = new.value::numeric WHERE id = new.orderid;
    -- invoice id
    ELSIF new.fieldid = 172 THEN
        UPDATE orders SET invoice_id = new.value WHERE id = new.orderid;
    -- confirmed
    ELSIF new.fieldid = 175 THEN
        UPDATE orders SET confirmed = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- delivered
    ELSIF new.fieldid = 176 THEN
        UPDATE orders SET delivered = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- email
    ELSIF new.fieldid = 184 THEN
        UPDATE orders SET email = new.value WHERE id = new.orderid;
    -- phone
    ELSIF new.fieldid = 185 THEN
        UPDATE orders SET phone = new.value WHERE id = new.orderid;
    -- newsletter
    ELSIF new.fieldid = 188 THEN
        UPDATE orders SET newsletter = CASE WHEN new.value = 'true' THEN 1 WHEN new.value = 'false' THEN 0 ELSE new.value::numeric END
            WHERE id = new.orderid;
    -- language
    ELSIF new.fieldid = 190 THEN
        UPDATE orders SET language = new.value WHERE id = new.orderid;
    -- contract customer id
    ELSIF new.fieldid = 191 THEN
        UPDATE orders SET contract_customer_id = new.value::int WHERE id = new.orderid;
    -- credit invoice
    ELSIF new.fieldid = 199 THEN
        UPDATE orders SET credit_invoice = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- canceled
    ELSIF new.fieldid = 200 THEN
        UPDATE orders SET canceled = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- vat number
    ELSEIF new.fieldid = 202 THEN
        UPDATE orders SET vat_number = new.value WHERE id = new.orderid;
    -- customer firstname
    ELSIF new.fieldid = 209 THEN
        UPDATE orders SET customer_firstname = new.value WHERE id = new.orderid;
    -- customer lastname
    ELSIF new.fieldid = 210 THEN
        UPDATE orders SET customer_lastname = new.value WHERE id = new.orderid;
    -- customer email
    ELSIF new.fieldid = 211 THEN
        UPDATE orders SET customer_email = new.value WHERE id = new.orderid;
    -- customer cellphone
    ELSIF new.fieldid = 212 THEN
        UPDATE orders SET customer_cellphone = new.value WHERE id = new.orderid;
    -- reminder
    ELSEIF new.fieldid = 219 THEN
        UPDATE orders SET reminder = CASE WHEN new.value = 'true' THEN 1 ELSE 0 END WHERE id = new.orderid;
    -- exchange rate eur
    ELSIF new.fieldid = 223 THEN
        UPDATE orders SET exchange_rate_eur = new.value::numeric WHERE id = new.orderid;
    -- delivered_date
    ELSIF new.fieldid = 225 THEN
        UPDATE orders SET delivered_date = new.value::date WHERE id = new.orderid;
    -- customer type
    ELSIF new.fieldid = 227 THEN
        UPDATE orders SET customer_type = new.value WHERE id = new.orderid;
    -- cancels order id
    ELSIF new.fieldid = 237 THEN
        UPDATE orders SET cancels_order_id = new.value::int WHERE id = new.orderid;
    -- cancelled by order id
    ELSIF new.fieldid = 238 THEN
        UPDATE orders SET cancelled_by_order_id = new.value::int WHERE id = new.orderid;
    -- orderlog_id
    ELSIF new.fieldid = 244 THEN
        UPDATE orders SET orderlog_id = new.value::int WHERE id = new.orderid;
    -- reseller store
    ELSIF new.fieldid = 245 THEN
        UPDATE orders SET reseller_store = new.value WHERE id = new.orderid;
    -- order_sum
    ELSIF new.fieldid = 246 THEN
        UPDATE orders SET order_sum = new.value::numeric WHERE id = new.orderid;
    -- klarna order id
    ELSIF new.fieldid = 247 THEN
        UPDATE orders SET klarna_order_id = new.value WHERE id = new.orderid;
    -- delivery email
    ELSIF new.fieldid = 248 THEN
        UPDATE orders SET delivery_email = new.value WHERE id = new.orderid;
    -- delivery phone
    ELSIF new.fieldid = 249 THEN
        UPDATE orders SET delivery_phone = new.value WHERE id = new.orderid;
    ELSIF new.fieldid = 250 THEN
        UPDATE orders SET pwinty_id = new.value::int WHERE id = new.orderid;
    -- delivery_address_id
    ELSIF new.fieldid = 254 THEN
        UPDATE orders SET delivery_address_id = new.value::int WHERE id = new.orderid;
    -- billing_address_id
    ELSIF new.fieldid = 255 THEN
        UPDATE orders SET billing_address_id = new.value::int WHERE id = new.orderid;
    -- flyer ids
    ELSIF new.fieldid = 240 THEN
        UPDATE orders SET flyer_ids = new.value WHERE id = new.orderid;
	-- market
    ELSIF new.fieldid = 256 THEN
        UPDATE orders SET market = new.value WHERE id = new.orderid;
	-- locale
    ELSIF new.fieldid = 257 THEN
        UPDATE orders SET locale = new.value WHERE id = new.orderid;
    END IF;
    RETURN new;
END;
$BODY$
language plpgsql;
