A referee may be appointed upon the agreement of the parties filed with the clerk, or judge, or entered in the minutes, or upon the motion of a party to a written contract or lease that provides that any controversy arising therefrom shall be heard by a referee if the court finds a reference agreement exists between the parties: (a) To hear and determine any or all of the issues in an action or proceeding, whether of fact or of law, and to report a statement of decision. (b) To ascertain a fact necessary