-- INSERT INTO domain__domain_option 
-- 	SELECT domain_option_id, domain_id FROM domain
-- 		JOIN unixuser on ( domain.domain_owner = unixuser.unixuser_id ) 
-- 		JOIN packet on ( unixuser.packet_id = packet.packet_id ) 
-- 		JOIN basepacket on ( packet.bp_id = basepacket.basepacket_id ), domain_option
-- 		WHERE basepacket.basepacket_code = 'PAC/DW'  
-- 			AND domain_option.domain_option_name = 'php' ;
				
-- DELETE FROM domain__domain_option 
-- 	USING domain_option, domain
-- 		JOIN unixuser on ( domain.domain_owner = unixuser.unixuser_id ) 
-- 		JOIN packet on ( unixuser.packet_id = packet.packet_id ) 
-- 		JOIN basepacket on ( packet.bp_id = basepacket.basepacket_id )
-- 		WHERE basepacket.basepacket_code != 'PAC/DW'
-- 			AND domain__domain_option.domain_option_id = domain_option.domain_option_id
-- 			AND domain_option.domain_option_name = 'php' ;

--- Updates related to HSBilling

UPDATE bank_account SET autodebit_ar = false WHERE autodebit_ar IS NULL;
ALTER TABLE bank_account ALTER COLUMN autodebit_ar SET NOT NULL;

UPDATE bank_account SET autodebit_ga = false WHERE autodebit_ga IS NULL;
ALTER TABLE bank_account ALTER COLUMN autodebit_ga SET NOT NULL;

UPDATE bank_account SET autodebit_op = false WHERE autodebit_op IS NULL;
ALTER TABLE bank_account ALTER COLUMN autodebit_op SET NOT NULL;

ALTER TABLE business_partner ADD COLUMN free boolean NOT NULL DEFAULT false;
ALTER TABLE business_partner ADD COLUMN indicator_vat character varying(20) NOT NULL DEFAULT 'GROSS';
ALTER TABLE business_partner ADD COLUMN exempt_vat boolean NOT NULL DEFAULT false;
ALTER TABLE business_partner ADD UNIQUE(bp_id);
 
DROP TABLE billdata CASCADE;

ALTER TABLE basepacket ADD COLUMN article_number integer NOT NULL DEFAULT 0;
ALTER TABLE basepacket ALTER COLUMN article_number DROP DEFAULT;

ALTER TABLE component ADD COLUMN article_number integer NOT NULL DEFAULT 0;
ALTER TABLE component ALTER COLUMN article_number DROP DEFAULT;
ALTER TABLE component DROP CONSTRAINT ckt_component;
ALTER TABLE component 
	ADD CONSTRAINT ckt_component CHECK (
    	(0 <= min_quantity) AND 
    	(min_quantity <= default_quantity) AND 
    	(default_quantity <= max_quantity) AND 
    	(include_quantity <= default_quantity) AND 
    	(0 <= include_quantity) AND 
    	(mod(min_quantity, increment_quantity) = 0) AND 
    	(mod(max_quantity, increment_quantity) = 0) AND 
    	(mod(default_quantity, increment_quantity) = 0) AND 
    	(mod(include_quantity, increment_quantity) = 0)
    );

ALTER TABLE packet DROP COLUMN order_number;
ALTER TABLE packet DROP COLUMN webserver_group;
<<<<<<< HEAD
ALTER TABLE packet ADD COLUMN free boolean NOT NULL DEFAULT false;
=======
>>>>>>> branch 'master' of ssh://hsh04-source@hsh04.hostsharing.net/home/doms/source.hostsharing.net/source/hsadmin.git

ALTER TABLE domain DROP COLUMN domain_status;
ALTER TABLE domain DROP COLUMN domain_status_changed;
ALTER TABLE domain DROP COLUMN domain_filed;
ALTER TABLE domain DROP COLUMN domain_until;
ALTER TABLE domain DROP COLUMN domain_reminder;
ALTER TABLE domain DROP COLUMN domain_free;
ALTER TABLE domain DROP COLUMN domain_template;

CREATE TABLE price_list (
  id serial PRIMARY KEY,                                                                   
  name varchar(20)
);       
INSERT INTO price_list (name) VALUES ('Default Price List');

CREATE TABLE customer_price_list_mapping (
	customer integer REFERENCES business_partner(bp_id),
	price_list integer REFERENCES price_list(id),
	PRIMARY KEY (customer, price_list)
);
INSERT INTO customer_price_list_mapping 
	(SELECT bp_id, (SELECT id FROM price_list WHERE name LIKE 'Default%') FROM business_partner);

CREATE TABLE price (                                                                
	id serial PRIMARY KEY,
	article_number integer NOT NULL,
	price decimal(10, 2) NOT NULL,
	vat decimal(4,2) NOT NULL,
	price_list integer REFERENCES price_list(id)                                             
);
INSERT INTO price (article_number, price, vat, price_list) 
	VALUES (0, 0, 0, (SELECT id FROM price_list WHERE name LIKE 'Default%')); 

