CREATE OR REPLACE FUNCTION core.get_month_start_date(_office_id integer)
RETURNS date
LANGUAGE plpgsql
AS $function$
DECLARE _date date;
BEGIN
SELECT MAX(value_date) + 1
INTO _date
FROM core.frequency_setups
WHERE value_date <
(
SELECT MIN(value_date)
FROM core.frequency_setups
WHERE value_date >= transactions.get_value_date($1)
);
IF(_date IS NULL) THEN
SELECT starts_from
INTO _date
FROM core.fiscal_year;
END IF;
RETURN _date;
END
$function$