-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_to_custom_unit_converter.sql
114 lines (94 loc) · 2.84 KB
/
custom_to_custom_unit_converter.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
This function is called only when:
1. from_unit is not null and NOT IN standard_units_definition.
2. to_unit is NOT '' AND NOT IN standard_units_definition.
*/
-- custom_to_custom_unit_converter sql function
CREATE OR REPLACE
FUNCTION inventory.custom_to_custom_unit_converter(
quantity numeric,
from_unit text,
from_bulk_density numeric,
to_unit text,
to_unit_bulk_density numeric,
from_unit_id integer,
to_unit_id integer
)
RETURNS SETOF crm."customerData"
LANGUAGE plpgsql STABLE AS $function$
DECLARE
from_custom_rule record;
to_custom_rule record;
result jsonb := '{"error": null, "result": null}'::jsonb;
proceed text := NULL;
from_in_standard jsonb;
BEGIN
SELECT "inputUnitName" input_unit, "outputUnitName" output_unit, "conversionFactor" conversion_factor
FROM master."unitConversion"
WHERE id = to_unit_id
into to_custom_rule;
SELECT "inputUnitName" input_unit, "outputUnitName" output_unit, "conversionFactor" conversion_factor
FROM master."unitConversion"
WHERE id = from_unit_id
into from_custom_rule;
IF to_custom_rule IS NULL THEN
proceed := 'to_unit';
ELSEIF from_custom_rule IS NULL THEN
proceed := 'from_unit';
END IF;
IF proceed IS NULL THEN
SELECT data->'result'->'custom'->from_custom_rule.input_unit
FROM inventory.custom_to_standard_unit_converter(
quantity,
from_custom_rule.input_unit,
from_bulk_density,
to_custom_rule.output_unit::text,
to_unit_bulk_density,
from_unit_id,
'',
'',
0
) INTO from_in_standard;
SELECT data
FROM inventory.standard_to_custom_unit_converter(
(from_in_standard->'equivalentValue')::numeric,
(from_in_standard->>'toUnitName')::text,
from_bulk_density,
to_unit,
to_unit_bulk_density,
to_unit_id
) INTO result;
result := jsonb_build_object(
'error',
'null'::jsonb,
'result',
jsonb_build_object(
'value',
quantity,
'toUnitName',
to_unit,
'fromUnitName',
from_unit,
'equivalentValue',
(result->'result'->'equivalentValue')::numeric
)
);
ELSEIF proceed = 'to_unit' THEN
result :=
format(
'{"error": "no custom unit is defined with the id: %s for argument to_unit, create a conversion rule in the master.\"unitConversion\" table."}',
to_unit_id
)::jsonb;
ELSEIF proceed = 'from_unit' THEN
result :=
format(
'{"error": "no custom unit is defined with the id: %s for argument from_unit, create a conversion rule in the master.\"unitConversion\" table."}',
from_unit_id
)::jsonb;
END IF;
RETURN QUERY
SELECT
1 AS id,
result as data;
END;
$function$