Advertisement
dzimmo

Untitled

Apr 1st, 2021
4,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 32.82 KB | None | 0 0
  1. SELECT CASE
  2.                                         WHEN trip.TYPE = 'transfer'
  3.                                             THEN 'transfer'
  4.                                         WHEN order_profit.debit_company_id IS NULL
  5.                                                 AND waybill_netting_partner_city.company_id IS NULL
  6.                                                 AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  7.                                                 AND order_profit.TYPE != 'shipping_satellite'
  8.                                             THEN 'local_gd'
  9.                                         WHEN (order_profit.credit_company_id = 62
  10.                                         AND order_profit.debit_company_id IS NOT NULL
  11.                                         AND order_profit.TYPE IN ('forwarding_netting')
  12.                                         AND (`order`.service_id IN (11662, 1001790)
  13.                                             OR `order`.category_id = 113) OR order_profit.credit_company_id IS NULL
  14.                                         AND waybill_netting_partner_city.company_id IS NOT NULL
  15.                                         AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  16.                                         AND order_profit.TYPE != 'shipping_satellite')
  17.                                             THEN 'local_partners'
  18.                                         WHEN IFNULL(waybill.parent_service_id, waybill.service_id) NOT IN (91,92)
  19.                                             THEN 'intercity'
  20.                                         ELSE
  21.                                             'other'
  22.                                     END AS TYPE,
  23.                                     DATE_FORMAT(`order`.report_date, '%Y-%m') AS MONTH,
  24.                                     SUM(order_profit.VALUE) AS VALUE,
  25.  
  26.                                     ORDER.order_id,
  27.                                     ORDER.title,
  28.                                 ORDER.report_date,
  29.                                 ORDER.giveout_date,
  30.                                 shipping.reduced_volume,
  31.                                 ORDER.STATUS,
  32.                                 IF(ORDER.service_id IN (46, 11662) AND ORDER.map_from_city_id = ORDER.map_to_city_id, 0, 1) AS shipping_different_city,
  33.                                 from_city.title AS from_city,
  34.                                 to_city.title AS to_city
  35.                                
  36.                     FROM (
  37.                             (
  38.                                 SELECT `order`.order_id,
  39.                                     `order`.map_from_city_id,
  40.                                     `order`.map_to_city_id,
  41.                                     0 AS trip_id
  42.                                 FROM `order`
  43.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  44.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  45.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  46.                                     AND `order`.service_id IN (333196)
  47.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  48.                                     AND client.client_id NOT IN (3837237,4340396)
  49.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  50.                                 GROUP BY `order`.order_id
  51.                             )
  52.                             UNION
  53.                             (
  54.                                 SELECT `order`.order_id,
  55.                                     `order`.map_from_city_id,
  56.                                     `order`.map_to_city_id,
  57.                                     0 AS trip_id
  58.                                 FROM `order`
  59.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  60.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  61.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 17:20:07'
  62.                                     AND `order`.service_id = 1001790
  63.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  64.                                     AND client.client_id NOT IN (3837237,4340396)
  65.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  66.                                 GROUP BY `order`.order_id
  67.                             )
  68.                             UNION
  69.                             (
  70.                                 SELECT `order`.order_id,
  71.                                     `order`.map_from_city_id,
  72.                                     `order`.map_to_city_id,
  73.                                     SUM(IF(from_contact.TYPE = 'storehouse' AND to_contact.TYPE = 'storehouse' AND trip.STATUS <> 'complete', 1, 0)) AS trip_id
  74.                                 FROM `order`
  75.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  76.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  77.                                     LEFT JOIN waybill ON (`order`.order_id = waybill.order_id AND waybill.service_id = 90)
  78.                                     LEFT JOIN trip_contents ON (waybill.waybill_id = trip_contents.waybill_id)
  79.                                     LEFT JOIN trip ON (trip_contents.trip_id = trip.trip_id AND trip.TYPE = 'intercity')
  80.                                     LEFT JOIN route ON (trip.trip_id = route.trip_id)
  81.                                     LEFT JOIN contact from_contact ON (route.from_contact_id = from_contact.contact_id)
  82.                                     LEFT JOIN contact to_contact ON (route.to_contact_id = to_contact.contact_id)
  83.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  84.                                     AND `order`.service_id IN (46,11662,942285,100)
  85.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  86.                                     AND client.client_id NOT IN (3837237,4340396)
  87.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  88.                                 GROUP BY `order`.order_id
  89.                                
  90.                             )
  91.                         ) t1
  92.                         LEFT JOIN `order` ON (t1.order_id = `order`.order_id)
  93.                         LEFT JOIN waybill shipping ON (`order`.order_id = shipping.order_id
  94.                             AND shipping.service_id = 90
  95.                             AND `order`.service_id != 1001790
  96.                             AND shipping.from_city_id <> shipping.to_city_id)
  97.                         LEFT JOIN account ON (`order`.account_id = account.account_id)
  98.                         LEFT JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  99.                         LEFT JOIN employee ON (client.user_id = employee.user_id)
  100.                         LEFT JOIN contact office_contact ON (employee.office_contact_id = office_contact.contact_id)
  101.                         LEFT JOIN geo AS from_city ON from_city.geo_id = ORDER.map_from_city_id
  102.                         LEFT JOIN geo AS to_city ON to_city.geo_id = ORDER.map_to_city_id
  103.                         LEFT JOIN geo office_geo ON (office_contact.geo_id = office_geo.geo_id)
  104. # @todo переделать на geo_representation
  105.                         LEFT JOIN geo_representation_tmp netting_partner_city ON (DATE(`order`.report_date) = netting_partner_city.DATE
  106.                             AND netting_partner_city.geo_id = office_contact.geo_id)
  107.                         LEFT JOIN company netting_partner_company ON (netting_partner_city.company_id = netting_partner_company.company_id)
  108.                         LEFT JOIN company employee_company ON (IFNULL(employee.company_id, 62) = employee_company.company_id)
  109.                         LEFT JOIN company ON (IFNULL(IFNULL(netting_partner_company.parent_id, netting_partner_company.company_id), IF(employee_company.TYPE = 'main', 62, employee.company_id)) = company.company_id)
  110.                         LEFT JOIN contact company_contact ON (company.contact_id = company_contact.contact_id)
  111.                         LEFT JOIN geo company_geo ON (company_contact.geo_id = company_geo.geo_id)
  112.                         LEFT JOIN order_profit ON (`order`.order_id = order_profit.order_id)
  113.                         LEFT JOIN trip ON (order_profit.trip_id = trip.trip_id)
  114.                         LEFT JOIN waybill ON (order_profit.waybill_id = waybill.waybill_id)
  115.                         LEFT JOIN waybill parent_waybill ON (waybill.order_id = parent_waybill.order_id AND waybill.parent_service_id = parent_waybill.service_id)
  116.                         LEFT JOIN service inner_service ON (waybill.service_id = inner_service.service_id)
  117.                         LEFT JOIN geo waybill_geo ON (IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_geo.geo_id)
  118. # @todo переделать на geo_representation
  119.                         LEFT JOIN geo_representation_tmp waybill_netting_partner_city ON (DATE(`order`.report_date) = waybill_netting_partner_city.DATE
  120.                             AND IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_netting_partner_city.geo_id)
  121.                         LEFT JOIN company waybill_company ON (waybill_company.company_id = waybill_netting_partner_city.company_id)
  122.                         LEFT JOIN company debit_company ON (order_profit.debit_company_id = debit_company.company_id)
  123.                         LEFT JOIN contact debit_company_contact ON (debit_company.contact_id = debit_company_contact.contact_id)
  124.                         LEFT JOIN geo debit_company_geo ON (debit_company_contact.geo_id = debit_company_geo.geo_id)
  125.                         LEFT JOIN company credit_company ON (order_profit.credit_company_id = credit_company.company_id)
  126.                         LEFT JOIN contact credit_company_contact ON (credit_company.contact_id = credit_company_contact.contact_id)
  127.                         LEFT JOIN geo credit_company_geo ON (credit_company_contact.geo_id = credit_company_geo.geo_id)
  128.                    
  129.                                 WHERE IF(company.company_id IN (71,163,167,302) AND `order`.report_date < '2018-08-01 00:00:00' OR company.company_id IN (105, 119) AND `order`.report_date < '2019-11-01 00:00:00' OR company.company_id IN (41) AND `order`.report_date < '2020-02-01 00:00:00', 'processing', IFNULL(company.netting_scheme, 'main')) != 'processing'
  130.  AND `order`.order_id IN (25861471)
  131.                                     AND (
  132.                                         order_profit.debit_company_id IS NULL
  133.                                             AND order_profit.credit_company_id IS NOT NULL
  134.                                             AND order_profit.TYPE IN ('shipping','shipping_trip_cost')
  135.                                             AND IF(credit_company.company_id IN (71,163,167,302) AND `order`.report_date < '2018-08-01 00:00:00' OR credit_company.company_id IN (105, 119) AND `order`.report_date < '2019-11-01 00:00:00' OR credit_company.company_id IN (41) AND `order`.report_date < '2020-02-01 00:00:00', 'processing', IFNULL(credit_company.netting_scheme, 'main')) != 'processing'
  136.                                         OR
  137.                                         order_profit.credit_company_id = 62
  138.                                             AND order_profit.debit_company_id IS NOT NULL
  139.                                             AND order_profit.TYPE IN ('shipping_route_segment')
  140.                                         OR
  141.                                         order_profit.debit_company_id IS NULL
  142.                                                 AND waybill_netting_partner_city.company_id IS NULL
  143.                                                 AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  144.                                                 AND order_profit.TYPE != 'shipping_satellite'
  145.                                         OR
  146.                                         order_profit.credit_company_id = 62
  147.                                         AND order_profit.debit_company_id IS NOT NULL
  148.                                         AND order_profit.TYPE IN ('forwarding_netting')
  149.                                         AND (`order`.service_id IN (11662, 1001790)
  150.                                             OR `order`.category_id = 113)
  151.                                         OR
  152.                                         order_profit.credit_company_id IS NULL
  153.                                         AND waybill_netting_partner_city.company_id IS NOT NULL
  154.                                         AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  155.                                         AND order_profit.TYPE != 'shipping_satellite'
  156.                                         OR
  157.                                             order_profit.debit_company_id IS NULL
  158.                                             AND order_profit.TYPE IN ('extra_service')
  159.                                             AND waybill_netting_partner_city.company_id IS NULL
  160.                                             AND waybill.parent_service_id = 90
  161.                                     )
  162.                                 GROUP BY ORDER.order_id, TYPE
  163.  
  164.                                 UNION ALL
  165.  
  166.                                 SELECT CASE
  167.                                         WHEN trip.TYPE = 'transfer'
  168.                                             THEN 'transfer'
  169.                                         WHEN IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  170.                                                 AND order_profit.debit_company_id IS NULL
  171.                                                 AND waybill_netting_partner_city.company_id IS NULL
  172.                                                 AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  173.                                                 AND order_profit.TYPE != 'shipping_satellite'
  174.                                             THEN 'local_gd'
  175.                                         WHEN IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  176.                                                 AND (order_profit.credit_company_id = 62
  177.                                         AND order_profit.debit_company_id IS NOT NULL
  178.                                         AND order_profit.TYPE IN ('forwarding_netting')
  179.                                         AND (`order`.service_id IN (11662, 1001790)
  180.                                             OR `order`.category_id = 113) OR order_profit.credit_company_id IS NULL
  181.                                         AND waybill_netting_partner_city.company_id IS NOT NULL
  182.                                         AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  183.                                         AND order_profit.TYPE != 'shipping_satellite')
  184.                                             THEN 'local_partners'
  185.                                         WHEN IFNULL(waybill.parent_service_id, waybill.service_id) NOT IN (91,92)
  186.                                             THEN 'intercity'
  187.                                         ELSE
  188.                                             'other'
  189.                                     END AS TYPE,
  190.                                     DATE_FORMAT(`order`.report_date, '%Y-%m') AS MONTH,
  191.                                     SUM(order_profit.VALUE) AS VALUE,
  192.                                     ORDER.order_id,
  193.                                     ORDER.title,
  194.                                 ORDER.report_date,
  195.                                 ORDER.giveout_date,
  196.                                 shipping.reduced_volume,
  197.                                 ORDER.STATUS,
  198.                                 IF(ORDER.service_id IN (46, 11662) AND ORDER.map_from_city_id = ORDER.map_to_city_id, 0, 1) AS shipping_different_city,
  199.                                 from_city.title AS from_city,
  200.                                 to_city.title AS to_city
  201.                                
  202.                     FROM (
  203.                             (
  204.                                 SELECT `order`.order_id,
  205.                                     `order`.map_from_city_id,
  206.                                     `order`.map_to_city_id,
  207.                                     0 AS trip_id
  208.                                 FROM `order`
  209.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  210.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  211.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  212.                                     AND `order`.service_id IN (333196)
  213.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  214.                                     AND client.client_id NOT IN (3837237,4340396)
  215.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  216.                                 GROUP BY `order`.order_id
  217.                             )
  218.                             UNION
  219.                             (
  220.                                 SELECT `order`.order_id,
  221.                                     `order`.map_from_city_id,
  222.                                     `order`.map_to_city_id,
  223.                                     0 AS trip_id
  224.                                 FROM `order`
  225.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  226.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  227.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 17:20:07'
  228.                                     AND `order`.service_id = 1001790
  229.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  230.                                     AND client.client_id NOT IN (3837237,4340396)
  231.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  232.                                 GROUP BY `order`.order_id
  233.                             )
  234.                             UNION
  235.                             (
  236.                                 SELECT `order`.order_id,
  237.                                     `order`.map_from_city_id,
  238.                                     `order`.map_to_city_id,
  239.                                     SUM(IF(from_contact.TYPE = 'storehouse' AND to_contact.TYPE = 'storehouse' AND trip.STATUS <> 'complete', 1, 0)) AS trip_id
  240.                                 FROM `order`
  241.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  242.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  243.                                     LEFT JOIN waybill ON (`order`.order_id = waybill.order_id AND waybill.service_id = 90)
  244.                                     LEFT JOIN trip_contents ON (waybill.waybill_id = trip_contents.waybill_id)
  245.                                     LEFT JOIN trip ON (trip_contents.trip_id = trip.trip_id AND trip.TYPE = 'intercity')
  246.                                     LEFT JOIN route ON (trip.trip_id = route.trip_id)
  247.                                     LEFT JOIN contact from_contact ON (route.from_contact_id = from_contact.contact_id)
  248.                                     LEFT JOIN contact to_contact ON (route.to_contact_id = to_contact.contact_id)
  249.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  250.                                     AND `order`.service_id IN (46,11662,942285,100)
  251.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  252.                                     AND client.client_id NOT IN (3837237,4340396)
  253.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  254.                                 GROUP BY `order`.order_id
  255.                                
  256.                             )
  257.                         ) t1
  258.                         LEFT JOIN `order` ON (t1.order_id = `order`.order_id)
  259.                         LEFT JOIN waybill shipping ON (`order`.order_id = shipping.order_id
  260.                             AND shipping.service_id = 90
  261.                             AND `order`.service_id != 1001790
  262.                             AND shipping.from_city_id <> shipping.to_city_id)
  263.                         LEFT JOIN account ON (`order`.account_id = account.account_id)
  264.                         LEFT JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  265.                         LEFT JOIN employee ON (client.user_id = employee.user_id)
  266.                         LEFT JOIN contact office_contact ON (employee.office_contact_id = office_contact.contact_id)
  267.                         LEFT JOIN geo AS from_city ON from_city.geo_id = ORDER.map_from_city_id
  268.                         LEFT JOIN geo AS to_city ON to_city.geo_id = ORDER.map_to_city_id
  269.                         LEFT JOIN geo office_geo ON (office_contact.geo_id = office_geo.geo_id)
  270. # @todo переделать на geo_representation
  271.                         LEFT JOIN geo_representation_tmp netting_partner_city ON (DATE(`order`.report_date) = netting_partner_city.DATE
  272.                             AND netting_partner_city.geo_id = office_contact.geo_id)
  273.                         LEFT JOIN company netting_partner_company ON (netting_partner_city.company_id = netting_partner_company.company_id)
  274.                         LEFT JOIN company employee_company ON (IFNULL(employee.company_id, 62) = employee_company.company_id)
  275.                         LEFT JOIN company ON (IFNULL(IFNULL(netting_partner_company.parent_id, netting_partner_company.company_id), IF(employee_company.TYPE = 'main', 62, employee.company_id)) = company.company_id)
  276.                         LEFT JOIN contact company_contact ON (company.contact_id = company_contact.contact_id)
  277.                         LEFT JOIN geo company_geo ON (company_contact.geo_id = company_geo.geo_id)
  278.                         LEFT JOIN order_profit ON (`order`.order_id = order_profit.order_id)
  279.                         LEFT JOIN trip ON (order_profit.trip_id = trip.trip_id)
  280.                         LEFT JOIN waybill ON (order_profit.waybill_id = waybill.waybill_id)
  281.                         LEFT JOIN waybill parent_waybill ON (waybill.order_id = parent_waybill.order_id AND waybill.parent_service_id = parent_waybill.service_id)
  282.                         LEFT JOIN service inner_service ON (waybill.service_id = inner_service.service_id)
  283.                         LEFT JOIN geo waybill_geo ON (IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_geo.geo_id)
  284. # @todo переделать на geo_representation
  285.                         LEFT JOIN geo_representation_tmp waybill_netting_partner_city ON (DATE(`order`.report_date) = waybill_netting_partner_city.DATE
  286.                             AND IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_netting_partner_city.geo_id)
  287.                         LEFT JOIN company waybill_company ON (waybill_company.company_id = waybill_netting_partner_city.company_id)
  288.                         LEFT JOIN company debit_company ON (order_profit.debit_company_id = debit_company.company_id)
  289.                         LEFT JOIN contact debit_company_contact ON (debit_company.contact_id = debit_company_contact.contact_id)
  290.                         LEFT JOIN geo debit_company_geo ON (debit_company_contact.geo_id = debit_company_geo.geo_id)
  291.                         LEFT JOIN company credit_company ON (order_profit.credit_company_id = credit_company.company_id)
  292.                         LEFT JOIN contact credit_company_contact ON (credit_company.contact_id = credit_company_contact.contact_id)
  293.                         LEFT JOIN geo credit_company_geo ON (credit_company_contact.geo_id = credit_company_geo.geo_id)
  294.                    
  295.                                 WHERE IF(company.company_id IN (71,163,167,302) AND `order`.report_date < '2018-08-01 00:00:00' OR company.company_id IN (105, 119) AND `order`.report_date < '2019-11-01 00:00:00' OR company.company_id IN (41) AND `order`.report_date < '2020-02-01 00:00:00', 'processing', IFNULL(company.netting_scheme, 'main')) = 'processing'
  296.  AND `order`.order_id IN (25861471)
  297.                                     AND (
  298.                                         order_profit.credit_company_id = 62
  299.                                             AND order_profit.debit_company_id IS NULL
  300.                                             AND order_profit.TYPE IN ('shipping')
  301.                                         OR
  302.                                         order_profit.debit_company_id IS NULL
  303.                                             AND order_profit.credit_company_id IS NOT NULL
  304.                                             AND waybill_netting_partner_city.company_id IS NULL
  305.                                             AND IFNULL(waybill.parent_service_id, waybill.service_id) IN (91,92)
  306.                                         OR
  307.                                             order_profit.debit_company_id IS NULL
  308.                                             AND order_profit.TYPE IN ('extra_service')
  309.                                             AND waybill_netting_partner_city.company_id IS NULL
  310.                                             AND waybill.parent_service_id = 90
  311.                                     )
  312.                                 GROUP BY ORDER.order_id, TYPE
  313.  
  314.                                 UNION ALL
  315.  
  316.                                 SELECT
  317.                                     'local_partners' AS TYPE,
  318.                                     DATE_FORMAT(`order`.report_date, '%Y-%m') AS MONTH,
  319.                                     -SUM(order_profit.VALUE) AS VALUE,
  320.                                     ORDER.order_id,
  321.                                     ORDER.title,
  322.                                 ORDER.report_date,
  323.                                 ORDER.giveout_date,
  324.                                 shipping.reduced_volume,
  325.                                 ORDER.STATUS,
  326.                                 IF(ORDER.service_id IN (46, 11662) AND ORDER.map_from_city_id = ORDER.map_to_city_id, 0, 1) AS shipping_different_city,
  327.                                 from_city.title AS from_city,
  328.                                 to_city.title AS to_city
  329.                                
  330.                     FROM (
  331.                             (
  332.                                 SELECT `order`.order_id,
  333.                                     `order`.map_from_city_id,
  334.                                     `order`.map_to_city_id,
  335.                                     0 AS trip_id
  336.                                 FROM `order`
  337.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  338.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  339.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  340.                                     AND `order`.service_id IN (333196)
  341.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  342.                                     AND client.client_id NOT IN (3837237,4340396)
  343.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  344.                                 GROUP BY `order`.order_id
  345.                             )
  346.                             UNION
  347.                             (
  348.                                 SELECT `order`.order_id,
  349.                                     `order`.map_from_city_id,
  350.                                     `order`.map_to_city_id,
  351.                                     0 AS trip_id
  352.                                 FROM `order`
  353.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  354.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  355.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 17:20:07'
  356.                                     AND `order`.service_id = 1001790
  357.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  358.                                     AND client.client_id NOT IN (3837237,4340396)
  359.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  360.                                 GROUP BY `order`.order_id
  361.                             )
  362.                             UNION
  363.                             (
  364.                                 SELECT `order`.order_id,
  365.                                     `order`.map_from_city_id,
  366.                                     `order`.map_to_city_id,
  367.                                     SUM(IF(from_contact.TYPE = 'storehouse' AND to_contact.TYPE = 'storehouse' AND trip.STATUS <> 'complete', 1, 0)) AS trip_id
  368.                                 FROM `order`
  369.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  370.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  371.                                     LEFT JOIN waybill ON (`order`.order_id = waybill.order_id AND waybill.service_id = 90)
  372.                                     LEFT JOIN trip_contents ON (waybill.waybill_id = trip_contents.waybill_id)
  373.                                     LEFT JOIN trip ON (trip_contents.trip_id = trip.trip_id AND trip.TYPE = 'intercity')
  374.                                     LEFT JOIN route ON (trip.trip_id = route.trip_id)
  375.                                     LEFT JOIN contact from_contact ON (route.from_contact_id = from_contact.contact_id)
  376.                                     LEFT JOIN contact to_contact ON (route.to_contact_id = to_contact.contact_id)
  377.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  378.                                     AND `order`.service_id IN (46,11662,942285,100)
  379.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  380.                                     AND client.client_id NOT IN (3837237,4340396)
  381.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  382.                                 GROUP BY `order`.order_id
  383.                                
  384.                             )
  385.                         ) t1
  386.                         LEFT JOIN `order` ON (t1.order_id = `order`.order_id)
  387.                         LEFT JOIN waybill shipping ON (`order`.order_id = shipping.order_id
  388.                             AND shipping.service_id = 90
  389.                             AND `order`.service_id != 1001790
  390.                             AND shipping.from_city_id <> shipping.to_city_id)
  391.                         LEFT JOIN account ON (`order`.account_id = account.account_id)
  392.                         LEFT JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  393.                         LEFT JOIN employee ON (client.user_id = employee.user_id)
  394.                         LEFT JOIN contact office_contact ON (employee.office_contact_id = office_contact.contact_id)
  395.                         LEFT JOIN geo AS from_city ON from_city.geo_id = ORDER.map_from_city_id
  396.                         LEFT JOIN geo AS to_city ON to_city.geo_id = ORDER.map_to_city_id
  397.                         LEFT JOIN geo office_geo ON (office_contact.geo_id = office_geo.geo_id)
  398. # @todo переделать на geo_representation
  399.                         LEFT JOIN geo_representation_tmp netting_partner_city ON (DATE(`order`.report_date) = netting_partner_city.DATE
  400.                             AND netting_partner_city.geo_id = office_contact.geo_id)
  401.                         LEFT JOIN company netting_partner_company ON (netting_partner_city.company_id = netting_partner_company.company_id)
  402.                         LEFT JOIN company employee_company ON (IFNULL(employee.company_id, 62) = employee_company.company_id)
  403.                         LEFT JOIN company ON (IFNULL(IFNULL(netting_partner_company.parent_id, netting_partner_company.company_id), IF(employee_company.TYPE = 'main', 62, employee.company_id)) = company.company_id)
  404.                         LEFT JOIN contact company_contact ON (company.contact_id = company_contact.contact_id)
  405.                         LEFT JOIN geo company_geo ON (company_contact.geo_id = company_geo.geo_id)
  406.                         LEFT JOIN order_profit ON (`order`.order_id = order_profit.order_id)
  407.                         LEFT JOIN trip ON (order_profit.trip_id = trip.trip_id)
  408.                         LEFT JOIN waybill ON (order_profit.waybill_id = waybill.waybill_id)
  409.                         LEFT JOIN waybill parent_waybill ON (waybill.order_id = parent_waybill.order_id AND waybill.parent_service_id = parent_waybill.service_id)
  410.                         LEFT JOIN service inner_service ON (waybill.service_id = inner_service.service_id)
  411.                         LEFT JOIN geo waybill_geo ON (IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_geo.geo_id)
  412. # @todo переделать на geo_representation
  413.                         LEFT JOIN geo_representation_tmp waybill_netting_partner_city ON (DATE(`order`.report_date) = waybill_netting_partner_city.DATE
  414.                             AND IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_netting_partner_city.geo_id)
  415.                         LEFT JOIN company waybill_company ON (waybill_company.company_id = waybill_netting_partner_city.company_id)
  416.                         LEFT JOIN company debit_company ON (order_profit.debit_company_id = debit_company.company_id)
  417.                         LEFT JOIN contact debit_company_contact ON (debit_company.contact_id = debit_company_contact.contact_id)
  418.                         LEFT JOIN geo debit_company_geo ON (debit_company_contact.geo_id = debit_company_geo.geo_id)
  419.                         LEFT JOIN company credit_company ON (order_profit.credit_company_id = credit_company.company_id)
  420.                         LEFT JOIN contact credit_company_contact ON (credit_company.contact_id = credit_company_contact.contact_id)
  421.                         LEFT JOIN geo credit_company_geo ON (credit_company_contact.geo_id = credit_company_geo.geo_id)
  422.                    
  423.                                 WHERE IF(company.company_id IN (71,163,167,302) AND `order`.report_date < '2018-08-01 00:00:00' OR company.company_id IN (105, 119) AND `order`.report_date < '2019-11-01 00:00:00' OR company.company_id IN (41) AND `order`.report_date < '2020-02-01 00:00:00', 'processing', IFNULL(company.netting_scheme, 'main')) = 'processing'
  424.  AND `order`.order_id IN (25861471)
  425.                                     AND order_profit.debit_company_id = 62
  426.                                         AND order_profit.credit_company_id IS NOT NULL
  427.                                         AND order_profit.TYPE IN ('forwarding_netting')
  428.                                         AND (`order`.service_id IN (11662, 1001790 )
  429.                                             OR `order`.category_id = 113)
  430.                                    
  431.                                 UNION ALL
  432.  
  433.                                 SELECT
  434.                                     'local_partners' AS TYPE,
  435.                                     DATE_FORMAT(`order`.report_date, '%Y-%m') AS MONTH,
  436.                                     -SUM(order_profit.VALUE) AS VALUE,
  437.                                     ORDER.order_id,
  438.                                     ORDER.title,
  439.                                 ORDER.report_date,
  440.                                 ORDER.giveout_date,
  441.                                 shipping.reduced_volume,
  442.                                 ORDER.STATUS,
  443.                                 IF(ORDER.service_id IN (46, 11662) AND ORDER.map_from_city_id = ORDER.map_to_city_id, 0, 1) AS shipping_different_city,
  444.                                 from_city.title AS from_city,
  445.                                 to_city.title AS to_city
  446.                                
  447.                     FROM (
  448.                             (
  449.                                 SELECT `order`.order_id,
  450.                                     `order`.map_from_city_id,
  451.                                     `order`.map_to_city_id,
  452.                                     0 AS trip_id
  453.                                 FROM `order`
  454.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  455.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  456.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  457.                                     AND `order`.service_id IN (333196)
  458.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  459.                                     AND client.client_id NOT IN (3837237,4340396)
  460.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  461.                                 GROUP BY `order`.order_id
  462.                             )
  463.                             UNION
  464.                             (
  465.                                 SELECT `order`.order_id,
  466.                                     `order`.map_from_city_id,
  467.                                     `order`.map_to_city_id,
  468.                                     0 AS trip_id
  469.                                 FROM `order`
  470.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  471.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  472.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 17:20:07'
  473.                                     AND `order`.service_id = 1001790
  474.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  475.                                     AND client.client_id NOT IN (3837237,4340396)
  476.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  477.                                 GROUP BY `order`.order_id
  478.                             )
  479.                             UNION
  480.                             (
  481.                                 SELECT `order`.order_id,
  482.                                     `order`.map_from_city_id,
  483.                                     `order`.map_to_city_id,
  484.                                     SUM(IF(from_contact.TYPE = 'storehouse' AND to_contact.TYPE = 'storehouse' AND trip.STATUS <> 'complete', 1, 0)) AS trip_id
  485.                                 FROM `order`
  486.                                     INNER JOIN account ON (`order`.account_id = account.account_id)
  487.                                     INNER JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  488.                                     LEFT JOIN waybill ON (`order`.order_id = waybill.order_id AND waybill.service_id = 90)
  489.                                     LEFT JOIN trip_contents ON (waybill.waybill_id = trip_contents.waybill_id)
  490.                                     LEFT JOIN trip ON (trip_contents.trip_id = trip.trip_id AND trip.TYPE = 'intercity')
  491.                                     LEFT JOIN route ON (trip.trip_id = route.trip_id)
  492.                                     LEFT JOIN contact from_contact ON (route.from_contact_id = from_contact.contact_id)
  493.                                     LEFT JOIN contact to_contact ON (route.to_contact_id = to_contact.contact_id)
  494.                                 WHERE `order`.report_date BETWEEN '2021-01-01 00:00:00' AND '2021-04-01 00:00:00'
  495.                                     AND `order`.service_id IN (46,11662,942285,100)
  496.                                     AND `order`.STATUS IN ('store','sf','trip','dep_store','closed')
  497.                                     AND client.client_id NOT IN (3837237,4340396)
  498.                                      AND `order`.service_id IN (46,11662,333196,1001790,942285,100)
  499.                                 GROUP BY `order`.order_id
  500.                                
  501.                             )
  502.                         ) t1
  503.                         LEFT JOIN `order` ON (t1.order_id = `order`.order_id)
  504.                         LEFT JOIN waybill shipping ON (`order`.order_id = shipping.order_id
  505.                             AND shipping.service_id = 90
  506.                             AND `order`.service_id != 1001790
  507.                             AND shipping.from_city_id <> shipping.to_city_id)
  508.                         LEFT JOIN account ON (`order`.account_id = account.account_id)
  509.                         LEFT JOIN client ON (account.subject_id = client.client_id AND account.type_id = 21)
  510.                         LEFT JOIN employee ON (client.user_id = employee.user_id)
  511.                         LEFT JOIN contact office_contact ON (employee.office_contact_id = office_contact.contact_id)
  512.                         LEFT JOIN geo AS from_city ON from_city.geo_id = ORDER.map_from_city_id
  513.                         LEFT JOIN geo AS to_city ON to_city.geo_id = ORDER.map_to_city_id
  514.                         LEFT JOIN geo office_geo ON (office_contact.geo_id = office_geo.geo_id)
  515. # @todo переделать на geo_representation
  516.                         LEFT JOIN geo_representation_tmp netting_partner_city ON (DATE(`order`.report_date) = netting_partner_city.DATE
  517.                             AND netting_partner_city.geo_id = office_contact.geo_id)
  518.                         LEFT JOIN company netting_partner_company ON (netting_partner_city.company_id = netting_partner_company.company_id)
  519.                         LEFT JOIN company employee_company ON (IFNULL(employee.company_id, 62) = employee_company.company_id)
  520.                         LEFT JOIN company ON (IFNULL(IFNULL(netting_partner_company.parent_id, netting_partner_company.company_id), IF(employee_company.TYPE = 'main', 62, employee.company_id)) = company.company_id)
  521.                         LEFT JOIN contact company_contact ON (company.contact_id = company_contact.contact_id)
  522.                         LEFT JOIN geo company_geo ON (company_contact.geo_id = company_geo.geo_id)
  523.                         LEFT JOIN order_profit ON (`order`.order_id = order_profit.order_id)
  524.                         LEFT JOIN trip ON (order_profit.trip_id = trip.trip_id)
  525.                         LEFT JOIN waybill ON (order_profit.waybill_id = waybill.waybill_id)
  526.                         LEFT JOIN waybill parent_waybill ON (waybill.order_id = parent_waybill.order_id AND waybill.parent_service_id = parent_waybill.service_id)
  527.                         LEFT JOIN service inner_service ON (waybill.service_id = inner_service.service_id)
  528.                         LEFT JOIN geo waybill_geo ON (IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_geo.geo_id)
  529. # @todo переделать на geo_representation
  530.                         LEFT JOIN geo_representation_tmp waybill_netting_partner_city ON (DATE(`order`.report_date) = waybill_netting_partner_city.DATE
  531.                             AND IFNULL(waybill.from_city_id, waybill.to_city_id) = waybill_netting_partner_city.geo_id)
  532.                         LEFT JOIN company waybill_company ON (waybill_company.company_id = waybill_netting_partner_city.company_id)
  533.                         LEFT JOIN company debit_company ON (order_profit.debit_company_id = debit_company.company_id)
  534.                         LEFT JOIN contact debit_company_contact ON (debit_company.contact_id = debit_company_contact.contact_id)
  535.                         LEFT JOIN geo debit_company_geo ON (debit_company_contact.geo_id = debit_company_geo.geo_id)
  536.                         LEFT JOIN company credit_company ON (order_profit.credit_company_id = credit_company.company_id)
  537.                         LEFT JOIN contact credit_company_contact ON (credit_company.contact_id = credit_company_contact.contact_id)
  538.                         LEFT JOIN geo credit_company_geo ON (credit_company_contact.geo_id = credit_company_geo.geo_id)
  539.                    
  540.                                 WHERE IF(company.company_id IN (71,163,167,302) AND `order`.report_date < '2018-08-01 00:00:00' OR company.company_id IN (105, 119) AND `order`.report_date < '2019-11-01 00:00:00' OR company.company_id IN (41) AND `order`.report_date < '2020-02-01 00:00:00', 'processing', IFNULL(company.netting_scheme, 'main')) != 'processing'
  541.  AND `order`.order_id IN (25861471)
  542.                                     AND order_profit.debit_company_id = 62
  543.                                         AND order_profit.credit_company_id IS NOT NULL
  544.                                         AND order_profit.TYPE IN ('forwarding_netting')
  545.                                         AND (`order`.service_id IN (11662, 1001790 )
  546.                                             OR `order`.category_id = 113)
  547.                                    
  548.                                 GROUP BY ORDER.order_id, TYPE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement