Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION public.update_stats()
  2.  RETURNS trigger
  3.  LANGUAGE plpgsql
  4. AS $function$
  5. declare
  6.     recon reconciliations;
  7.     device_id uuid;
  8.     conductor_id uuid;
  9.     company_id uuid;
  10.     driver_id uuid;
  11.     route_id uuid;
  12.  
  13.     amount int;
  14.     formatted_month text;
  15.     tx_year text;
  16.     tx_month text;
  17.     tx_day text;
  18. begin
  19.     tx_year := '20' || substr(new.datetime_transmission, 1, 2);
  20.     tx_month := substr(new.datetime_transmission, 3, 2);
  21.     tx_day := substr(new.datetime_transmission, 5, 2);
  22.  
  23.     device_id := (select id from devices where terminal_id = new.terminal_id);
  24.    
  25.     amount = cast(trim(both 'C' from new.transaction_amount) as int);
  26.  
  27.     if new.type_id = '0521' then
  28.    
  29.         company_id := (select id from companies where merchant_id = new.merchant_id);
  30.         conductor_id := cast(new.additional_data::json->>'conductor' as uuid);
  31.         route_id := cast(new.additional_data::json->>'route' as uuid);
  32.         driver_id := cast(new.additional_data::json->>'driver' as uuid);
  33.    
  34.         insert into transactions_stats
  35.         (
  36.             entity,
  37.             entity_id,
  38.             day,
  39.             month,
  40.             year,
  41.             total,
  42.             total_cash,
  43.             total_cashless,
  44.             count,
  45.             count_cash,
  46.             count_cashless,
  47.             ticket_amount
  48.         )
  49.         values
  50.         (
  51.             'device',
  52.             device_id,
  53.             tx_day,
  54.             tx_month,
  55.             tx_year,
  56.             amount,
  57.             amount,
  58.             0,
  59.             amount / 2000,
  60.             amount / 2000,
  61.             0,
  62.             2000
  63.         );
  64.        
  65.         insert into transactions_stats
  66.         (
  67.             entity,
  68.             entity_id,
  69.             day,
  70.             month,
  71.             year,
  72.             total,
  73.             total_cash,
  74.             total_cashless,
  75.             count,
  76.             count_cash,
  77.             count_cashless,
  78.             ticket_amount
  79.         )
  80.         values
  81.         (
  82.             'company',
  83.             company_id,
  84.             tx_day,
  85.             tx_month,
  86.             tx_year,
  87.             amount,
  88.             amount,
  89.             0,
  90.             amount / 2000,
  91.             amount / 2000,
  92.             0,
  93.             2000
  94.         );
  95.    
  96.         insert into transactions_stats
  97.         (
  98.             entity,
  99.             entity_id,
  100.             day,
  101.             month,
  102.             year,
  103.             total,
  104.             total_cash,
  105.             total_cashless,
  106.             count,
  107.             count_cash,
  108.             count_cashless,
  109.             ticket_amount
  110.         )
  111.         values
  112.         (
  113.             'conductor',
  114.             conductor_id,
  115.             tx_day,
  116.             tx_month,
  117.             tx_year,
  118.             amount,
  119.             amount,
  120.             0,
  121.             amount / 2000,
  122.             amount / 2000,
  123.             0,
  124.             2000
  125.         );
  126.    
  127.         insert into transactions_stats
  128.         (
  129.             entity,
  130.             entity_id,
  131.             day,
  132.             month,
  133.             year,
  134.             total,
  135.             total_cash,
  136.             total_cashless,
  137.             count,
  138.             count_cash,
  139.             count_cashless,
  140.             ticket_amount
  141.         )
  142.         values
  143.         (
  144.             'driver',
  145.             driver_id,
  146.             tx_day,
  147.             tx_month,
  148.             tx_year,
  149.             amount,
  150.             amount,
  151.             0,
  152.             amount / 2000,
  153.             amount / 2000,
  154.             0,
  155.             2000
  156.         );
  157.    
  158.         insert into transactions_stats
  159.         (
  160.             entity,
  161.             entity_id,
  162.             day,
  163.             month,
  164.             year,
  165.             total,
  166.             total_cash,
  167.             total_cashless,
  168.             count,
  169.             count_cash,
  170.             count_cashless,
  171.             ticket_amount
  172.         )
  173.         values
  174.         (
  175.             'route',
  176.             route_id,
  177.             tx_day,
  178.             tx_month,
  179.             tx_year,
  180.             amount,
  181.             amount,
  182.             0,
  183.             amount / 2000,
  184.             amount / 2000,
  185.             0,
  186.             2000
  187.         );
  188.     end if;
  189.     if new.type_id = '0530' then
  190.    
  191.         if char_length(tx_day) = 1 then
  192.             tx_day := '0' || tx_day;
  193.         end if;
  194.    
  195.         if char_length(tx_month) = 1 then
  196.             tx_month := '0' || tx_month;
  197.         end if;
  198.    
  199.         if char_length(tx_year) = 2 then
  200.             tx_year := '20' || tx_year;
  201.         end if;
  202.    
  203.         conductor_id := (
  204.             select cast(additional_data::json->>'conductor' as uuid) as conductor_id
  205.             from reconciliations
  206.             where type_id = '0521'
  207.             and substring(cast(created_at::timestamp::date as text) from 1 for 4) = tx_year
  208.             and substring(cast(created_at::timestamp::date as text) from 6 for 2) = tx_month
  209.             and substring(cast(created_at::timestamp::date as text) from 9 for 2) = tx_day
  210.             limit 1
  211.         );
  212.    
  213.         if exists (select 1
  214.             from transactions_stats
  215.             where entity_id = conductor_id
  216.             and entity = 'conductor'
  217.             and day = tx_day
  218.             and month = tx_month
  219.             and year = tx_year
  220.             and is_closed = 0
  221.         ) then
  222.             update transactions_stats
  223.             set
  224.                 total_cashless = amount,
  225.                 count_cashless = (amount / 2000),
  226.                 count = (amount / 2000) + count,
  227.                 total = amount + total,
  228.                 is_closed = 1
  229.             where entity_id = conductor_id
  230.             and entity = 'conductor'
  231.             and day = tx_day
  232.             and month = tx_month
  233.             and year = tx_year
  234.             and is_closed = 0;
  235.         end if;
  236.    
  237.         driver_id := (
  238.             select cast(additional_data::json->>'driver' as uuid) as driver_id
  239.             from reconciliations
  240.             where type_id = '0521'
  241.             and substring(cast(created_at::timestamp::date as text) from 1 for 4) = tx_year
  242.             and substring(cast(created_at::timestamp::date as text) from 6 for 2) = tx_month
  243.             and substring(cast(created_at::timestamp::date as text) from 9 for 2) = tx_day
  244.             limit 1
  245.         );
  246.    
  247.         if exists (select 1
  248.             from transactions_stats
  249.             where entity_id = driver_id
  250.             and entity = 'driver'
  251.             and day = tx_day
  252.             and month = tx_month
  253.             and year = tx_year
  254.             and is_closed = 0
  255.         ) then
  256.             update transactions_stats
  257.             set
  258.                 total_cashless = amount,
  259.                 count_cashless = (amount / 2000),
  260.                 count = (amount / 2000) + count,
  261.                 total = amount + total,
  262.                 is_closed = 1
  263.             where entity_id = driver_id
  264.             and entity = 'driver'
  265.             and day = tx_day
  266.             and month = tx_month
  267.             and year = tx_year
  268.             and is_closed = 0;
  269.         end if;
  270.    
  271.         route_id := (
  272.             select cast(additional_data::json->>'route' as uuid) as route_id
  273.             from reconciliations
  274.             where type_id = '0521'
  275.             and substring(cast(created_at::timestamp::date as text) from 1 for 4) = tx_year
  276.             and substring(cast(created_at::timestamp::date as text) from 6 for 2) = tx_month
  277.             and substring(cast(created_at::timestamp::date as text) from 9 for 2) = tx_day
  278.             limit 1
  279.         );
  280.    
  281.         if exists (select 1
  282.             from transactions_stats
  283.             where entity_id = route_id
  284.             and entity = 'route'
  285.             and day = tx_day
  286.             and month = tx_month
  287.             and year = tx_year
  288.             and is_closed = 0
  289.         ) then
  290.             update transactions_stats
  291.             set
  292.                 total_cashless = amount,
  293.                 count_cashless = (amount / 2000),
  294.                 count = (amount / 2000) + count,
  295.                 total = amount + total,
  296.                 is_closed = 1
  297.             where entity_id = route_id
  298.             and entity = 'route'
  299.             and day = tx_day
  300.             and month = tx_month
  301.             and year = tx_year
  302.             and is_closed = 0;
  303.         end if;
  304.    
  305.        
  306.         company_id := (
  307.             select id
  308.             from companies
  309.             where merchant_id = (select merchant_id from reconciliations where terminal_id = new.terminal_id
  310.             and substring(cast(created_at::timestamp::date as text) from 1 for 4) = tx_year
  311.             and substring(cast(created_at::timestamp::date as text) from 6 for 2) = tx_month
  312.             and substring(cast(created_at::timestamp::date as text) from 9 for 2) = tx_day
  313.             limit 1)
  314.         );
  315.    
  316.         if exists (select 1
  317.             from transactions_stats
  318.             where entity_id = company_id
  319.             and entity = 'company'
  320.             and day = tx_day
  321.             and month = tx_month
  322.             and year = tx_year
  323.             and is_closed = 0
  324.         ) then
  325.             update transactions_stats
  326.             set
  327.                 total_cashless = amount,
  328.                 count_cashless = (amount / 2000),
  329.                 count = (amount / 2000) + count,
  330.                 total = amount + total,
  331.                 is_closed = 1
  332.             where entity_id = company_id
  333.             and entity = 'company'
  334.             and day = tx_day
  335.             and month = tx_month
  336.             and year = tx_year
  337.             and is_closed = 0;
  338.         end if;
  339.        
  340.         device_id := (
  341.             select id
  342.             from devices
  343.             where terminal_id = (select merchant_id from reconciliations where terminal_id = new.terminal_id
  344.             and substring(cast(created_at::timestamp::date as text) from 1 for 4) = tx_year
  345.             and substring(cast(created_at::timestamp::date as text) from 6 for 2) = tx_month
  346.             and substring(cast(created_at::timestamp::date as text) from 9 for 2) = tx_day
  347.             limit 1)
  348.         );
  349.    
  350.         if exists (select 1
  351.             from transactions_stats
  352.             where entity_id = device_id
  353.             and entity = 'device'
  354.             and day = tx_day
  355.             and month = tx_month
  356.             and year = tx_year
  357.             and is_closed = 0
  358.         ) then
  359.             update transactions_stats
  360.             set
  361.                 total_cashless = amount,
  362.                 count_cashless = (amount / 2000),
  363.                 count = (amount / 2000) + count,
  364.                 total = amount + total,
  365.                 is_closed = 1
  366.             where entity_id = device_id
  367.             and entity = 'device'
  368.             and day = tx_day
  369.             and month = tx_month
  370.             and year = tx_year
  371.             and is_closed = 0;
  372.         end if;
  373.        
  374.     end if;
  375.    
  376.     return new;
  377. end;
  378. $function$
  379. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement