Scotland's only oil refinery is to close by next summer with the loss of 400 jobs, it has been confirmed.