DROP FUNCTION copy_default_pricemodel(INT);
CREATE OR REPLACE FUNCTION copy_default_pricemodel(accountid INT) RETURNS INT AS $$
 DECLARE
  pricemodelid INT;
  pricemodelname TEXT;
  pricemodeltype TEXT;
  defaultmod BOOLEAN;
  newpricemodelid INT;
  squarepix INT;
  singlepric REAL;
  minbulkpric REAL;
 BEGIN
  FOR pricemodelid, pricemodelname, pricemodeltype, defaultmod IN SELECT id, name, type, defaultmodel FROM filepricemodel WHERE setupaccountid = 8 AND deleted = FALSE LOOP
   INSERT INTO filepricemodel (name, type, setupaccountid, defaultmodel) VALUES (pricemodelname, pricemodeltype, accountid, defaultmod) RETURNING id INTO newpricemodelid;
   FOR squarepix, singlepric, minbulkpric IN SELECT squarepixels, singleprice, minbulkprice FROM filepricemodel_royaltyfree WHERE filepricemodelid = pricemodelid LOOP
    INSERT INTO filepricemodel_royaltyfree (filepricemodelid, squarepixels, singleprice, minbulkprice, setupaccountid) 
     VALUES (newpricemodelid, squarepix, singlepric, minbulkpric, accountid);
   END LOOP;
  END LOOP;
  RETURN 0;
 END
$$ LANGUAGE plpgsql;

DROP FUNCTION copy_default_license(INT);
CREATE OR REPLACE FUNCTION copy_default_license(accountid INT) RETURNS INT AS $$
 DECLARE
  defaultmod BOOLEAN;
  tagtext TEXT;
  tagvalue TEXT;
  licenseid INT;
  newlicenseid INT;
 BEGIN
  FOR licenseid, defaultmod IN SELECT id, defaultmodel FROM filelicense WHERE setupaccountid = 8 AND deleted = FALSE LOOP
   INSERT INTO filelicense (setupaccountid, defaultmodel) VALUES (accountid, defaultmod) RETURNING id INTO newlicenseid;
   FOR tagtext, tagvalue IN SELECT tag, value FROM filelicense_details WHERE filelicenseid = licenseid LOOP
    INSERT INTO filelicense_details (filelicenseid, tag, value, setupaccountid) VALUES (newlicenseid, tagtext, tagvalue, accountid);
   END LOOP;
  END LOOP;
  RETURN 0;
 END
$$ LANGUAGE plpgsql;



