madrahimov

Untitled

Feb 7th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ----------------------Список счетов c лимитом----------------------------------------------------------
  2. DROP FUNCTION bill.bill_report_json_limit( date, date, uuid[], uuid[], uuid[], uuid[], integer[], uuid[], uuid, character varying, integer, integer);
  3. DROP FUNCTION bill.bill_report_json_limit( date, date, uuid[], uuid[], uuid[], uuid[], integer[], uuid[], uuid[], uuid, character varying, integer, integer);
  4. DROP FUNCTION bill.bill_report_json_limit( date, date, uuid[], uuid[], uuid[], uuid[], integer[], uuid[], uuid[], uuid, character varying, integer, integer, integer, integer);
  5. DROP FUNCTION bill.bill_report_json_limit( date, date, uuid[], uuid[], uuid[], uuid[], integer[], uuid[], uuid[], uuid, character varying, integer, integer, numeric, integer, integer);
  6. CREATE OR REPLACE FUNCTION bill.bill_report_json_limit (
  7.   _from date,
  8.   _to date,
  9.   _waiter_ids uuid [],
  10.   _hall_ids uuid [],
  11.   _table_ids uuid [],
  12.   _terminal_ids uuid [],
  13.   _bill_types integer [],
  14.   _client_ids uuid [],
  15.   _pay_type_ids uuid [],
  16.   _restaurant_id uuid,
  17.   _sort_by character varying,
  18.   _service integer,
  19.   _discount integer,
  20.   _discount_sum numeric,
  21.   _limit integer,
  22.   _offset integer
  23. )
  24. RETURNS TABLE (
  25.   json json
  26. ) AS
  27. $body$
  28. declare
  29.   item varchar[];
  30.   where_clause varchar(300) = '';
  31.   waiterFilter character varying = '';
  32.   hallFilter character varying = '';
  33.   tableFilter character varying = '';
  34.   terminalFilter character varying = '';
  35.   bill_typeFilter character varying = '';
  36.   clientFilter character varying = '';
  37.   pay_typeFilter character varying = '';
  38.   pay_typeInFilter character varying = '';
  39.   qFilter character varying = '';
  40.   serviceFilter character varying = '';
  41.   discountFilter character varying = '';
  42.   discount_sumFilter character varying = '';
  43.  
  44.   waiter_id uuid;
  45.   hall_id uuid;
  46.   table_id uuid;
  47.   terminal_id uuid;
  48.   bill_type integer;
  49.   client_id uuid;
  50.   pay_type_id uuid;
  51. begin
  52.   if ( _service is not null ) then
  53.     serviceFilter = 'and b.service = '||  _service ||'';
  54.   end if;
  55.  
  56.   if ( _discount is not null ) then
  57.     discountFilter = 'and b.discount = '||  _discount ||'';
  58.   end if;
  59.  
  60.   if ( _discount_sum is not null ) then
  61.     discountFilter = 'and b.discount_sum = '||  _discount_sum ||'';
  62.   end if;
  63.  
  64.   if ( _waiter_ids is not null ) then
  65.     waiterFilter = 'and b.waiter_id IN (';
  66.     FOREACH waiter_id in array _waiter_ids loop
  67.       waiterFilter := waiterFilter || '''' ||  waiter_id ||  ''',';
  68.     end loop;
  69.     Select trim(trailing ',' from waiterFilter) into waiterFilter;
  70.     waiterFilter := waiterFilter || ') ';
  71.   end if;
  72.  
  73.   if ( _hall_ids is not null ) then
  74.     hallFilter = 'and h.id IN (';
  75.     FOREACH hall_id in array _hall_ids loop
  76.       hallFilter := hallFilter || '''' ||  hall_id ||  ''',';
  77.     end loop;
  78.     Select trim(trailing ',' from hallFilter) into hallFilter;
  79.     hallFilter := hallFilter || ') ';
  80.   end if;
  81.  
  82.   if ( _table_ids is not null ) then
  83.     tableFilter = 'and t.id IN (';
  84.     FOREACH table_id in array _table_ids loop
  85.       tableFilter := tableFilter || '''' ||  table_id ||  ''',';
  86.     end loop;
  87.     Select trim(trailing ',' from tableFilter) into tableFilter;
  88.     tableFilter := tableFilter || ') ';
  89.   end if;
  90.  
  91.   if ( _terminal_ids is not null ) then
  92.     terminalFilter = 'and b.terminal_id IN (';
  93.     FOREACH terminal_id in array _terminal_ids loop
  94.       terminalFilter := terminalFilter || '''' ||  terminal_id ||  ''',';
  95.     end loop;
  96.     Select trim(trailing ',' from terminalFilter) into terminalFilter;
  97.     terminalFilter := terminalFilter || ') ';
  98.   end if;
  99.  
  100.   if ( _bill_types is not null ) then
  101.     bill_typeFilter = 'and b.bill_type IN (';
  102.     FOREACH bill_type in array _bill_types loop
  103.       bill_typeFilter := bill_typeFilter || '''' ||  bill_type ||  ''',';
  104.     end loop;
  105.     Select trim(trailing ',' from bill_typeFilter) into bill_typeFilter;
  106.     bill_typeFilter := bill_typeFilter || ') ';
  107.   end if;
  108.  
  109.   if ( _client_ids is not null ) then
  110.     clientFilter = 'and c.id IN (';
  111.     FOREACH client_id in array _client_ids loop
  112.       clientFilter := clientFilter || '''' ||  client_id ||  ''',';
  113.     end loop;
  114.     Select trim(trailing ',' from clientFilter) into clientFilter;
  115.     clientFilter := clientFilter || ') ';
  116.   end if;
  117.  
  118.   if ( _pay_type_ids is not null ) then
  119.     pay_typeFilter = 'and b.id IN (Select bill_id from bill.bill_pays Where pay_type_id IN (';
  120.     FOREACH pay_type_id in array _pay_type_ids loop
  121.       pay_typeFilter := pay_typeFilter || '''' ||  pay_type_id ||  ''',';
  122.     end loop;
  123.     Select trim(trailing ',' from pay_typeFilter) into pay_typeFilter;
  124.     pay_typeFilter := pay_typeFilter || ')) ';
  125.   end if;
  126.  
  127.   if ( _pay_type_ids is not null ) then
  128.     pay_typeInFilter = '';
  129.     FOREACH pay_type_id in array _pay_type_ids loop
  130.       pay_typeInFilter := pay_typeInFilter || '''' ||  pay_type_id ||  ''',';
  131.     end loop;
  132.     Select trim(trailing ',' from pay_typeInFilter) into pay_typeInFilter;
  133.     pay_typeInFilter := pay_typeInFilter || ' ';
  134.   end if;
  135.  
  136.   qFilter := qFilter || ' and b."work_date" between ''' || _from || ''' and ''' || _to || ''' ';
  137.  
  138.  
  139.   if ( _pay_type_ids is not null ) then
  140.     return query execute
  141.         'select array_to_json(array_agg(row_to_json(t)))
  142.          from (
  143.            SELECT
  144.              b.id, b.number, b.waiter_id, u.first_name as waiter_first_name, h.id as hall_id,
  145.              h.title as hall_title, b.table_id, t.number as table_number,
  146.              b.courses_amount, b.entry, b.service,
  147.              b.discount, sum(b.courses_amount / 100 * b.discount) AS sum_discount,
  148.              b.discount_sum,
  149.              b.work_date, b.open_date, b.close_date, b.restaurant_id,
  150.              b.bill_type,
  151.              c.id as client_id, c.first_name as client_first_name,
  152.              cdm.amount as "acc",
  153.              (
  154.                Select sum(bp.sum)
  155.                From bill.bill_pays bp
  156.                Where
  157.                  bp.bill_id = b.id
  158.                  And bp.pay_type_id IN (' || pay_typeInFilter || ')
  159.              ) AS amount
  160.            FROM bill.bills b
  161.            join local.users u on u.id = b.waiter_id
  162.            left join restaurant.tables t on t.id = b.table_id
  163.            left join restaurant.halls h on h.id = t.hall_id
  164.            left join bill.bill_clients bc on bc.bill_id = b.id And bc.is_active = true
  165.            left join clients c on c.id = bc.client_id
  166.            left join restaurant.client_deposit_modify_bills cdmb on cdmb.bill_id = b.id
  167.            left join restaurant.client_deposit_modifies cdm on cdm.id = cdmb.client_deposit_modify_id And cdm.modify_type = 2
  168.            WHERE
  169.              b.restaurant_id = ''' || _restaurant_id || '''
  170.              ' || waiterFilter || '
  171.              ' || hallFilter || '
  172.              ' || tableFilter || '
  173.              ' || terminalFilter || '
  174.              ' || bill_typeFilter || '
  175.              ' || clientFilter || '
  176.              ' || pay_typeFilter || '
  177.              ' || serviceFilter || '
  178.              ' || discountFilter || '
  179.              ' || discount_sumFilter || '
  180.              ' || qFilter || '
  181.              Group by b.id, u.first_name, h.id, t.number, c.id, cdm.amount
  182.              order by ' || _sort_by || '
  183.              limit ' || _limit || '
  184.              offset ' || _offset || '
  185.           ) t';
  186.   else
  187.     return query execute
  188.         'select array_to_json(array_agg(row_to_json(t)))
  189.          from (
  190.            SELECT
  191.              b.id, b.number, b.waiter_id, u.first_name as waiter_first_name, h.id as hall_id,
  192.              h.title as hall_title, b.table_id, t.number as table_number,
  193.              b.courses_amount, b.entry, b.service,
  194.              b.discount, sum(b.courses_amount / 100 * b.discount) AS sum_discount,
  195.              b.amount, b.discount_sum,
  196.              b.work_date, b.open_date, b.close_date, b.restaurant_id, b.bill_type,
  197.              c.id as client_id, c.first_name as client_first_name,
  198.              cdm.amount as acc
  199.            FROM bill.bills b
  200.            join local.users u on u.id = b.waiter_id
  201.            left join restaurant.tables t on t.id = b.table_id
  202.            left join restaurant.halls h on h.id = t.hall_id
  203.            left join bill.bill_clients bc on bc.bill_id = b.id And bc.is_active = true
  204.            left join clients c on c.id = bc.client_id
  205.            left join restaurant.client_deposit_modify_bills cdmb on cdmb.bill_id = b.id
  206.            left join restaurant.client_deposit_modifies cdm on cdm.id = cdmb.client_deposit_modify_id And cdm.modify_type = 2
  207.            WHERE
  208.              b.restaurant_id = ''' || _restaurant_id || '''
  209.              ' || waiterFilter || '
  210.              ' || hallFilter || '
  211.              ' || tableFilter || '
  212.              ' || terminalFilter || '
  213.              ' || bill_typeFilter || '
  214.              ' || clientFilter || '
  215.              ' || serviceFilter || '
  216.              ' || discountFilter || '
  217.              ' || discount_sumFilter || '
  218.              ' || qFilter || '
  219.              Group by b.id, u.first_name, h.id, t.number, c.id, cdm.amount
  220.              order by ' || _sort_by || '
  221.              limit ' || _limit || '
  222.              offset ' || _offset || '
  223.           ) t';
  224.   end if;
  225.  
  226. end;
  227. $body$
  228. LANGUAGE 'plpgsql' STABLE;
  229. ALTER FUNCTION bill.bill_report_json_limit( date, date, uuid[], uuid[], uuid[], uuid[], integer[], uuid[], uuid[], uuid, character varying, integer, integer, numeric, integer, integer)
  230.   OWNER TO developer;
Advertisement
Add Comment
Please, Sign In to add comment