Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 15.12 KB | None | 0 0
  1. SET sql_mode = ''
  2. SELECT
  3.   DATE(DATE_ADD(main_table.created_at, INTERVAL 0 SECOND))                                                     AS `period`,
  4.  
  5.   `main_table`.`store_id`,
  6.   `main_table`.`status`                                                                                        AS `order_status`,
  7.   `main_table`.`entity_id`                                                                                     AS `order_id`,
  8.   `main_table`.`increment_id`                                                                                  AS `order_increment_id`,
  9.   `main_table`.`created_at`                                                                                    AS `order_date`,
  10.   IFNULL(item2.product_id,
  11.          item2.product_id)                                                                                     AS `product_id`,
  12.   `item2`.`name`                                                                                               AS `product_name`,
  13.   item2.qty_ordered as ordered,
  14.   IFNULL(item2.sku, catalog_product.sku)                                                                        AS `sku`,
  15.   IFNULL(manufacturer_value.value,
  16.          "Not Set")                                                                                            AS `manufacturer`,
  17.   `main_table`.`customer_id`,
  18.   `main_table`.`customer_email`,
  19.   IFNULL(CONCAT(main_table.customer_firstname, " ", main_table.customer_lastname),
  20.          CONCAT(billing_address.firstname, " ",
  21.                 billing_address.lastname))                                                                     AS `customer_name`,
  22.   `c_group`.`customer_group_code`                                                                              AS `customer_group`,
  23.   COALESCE(shipping_address.country_id, billing_address.country_id,
  24.            "")                                                                                                 AS `country`,
  25.   COALESCE(shipping_address.region, billing_address.region,
  26.            "")                                                                                                 AS `region`,
  27.   COALESCE(shipping_address.city, billing_address.city,
  28.            "")                                                                                                 AS `city`,
  29.   COALESCE(shipping_address.postcode, billing_address.postcode,
  30.            "")                                                                                                 AS `zip_code`,
  31.   COALESCE(shipping_address.street, billing_address.street,
  32.            "")                                                                                                 AS `address`,
  33.   COALESCE(shipping_address.telephone, billing_address.telephone,
  34.            "")                                                                                                 AS `phone`,
  35.   `main_table`.`coupon_code`,
  36.   COALESCE(IFNULL(item2.qty_ordered, item2.qty_ordered),
  37.            0)                                                                                                  AS `qty_ordered`,
  38.   COALESCE(IFNULL(item2.qty_invoiced, item2.qty_invoiced),
  39.            0)                                                                                                  AS `qty_invoiced`,
  40.   COALESCE(IFNULL(item2.qty_shipped, item2.qty_shipped),
  41.            0)                                                                                                  AS `qty_shipped`,
  42.   COALESCE(IFNULL(item2.qty_refunded, item2.qty_refunded),
  43.            0)                                                                                                  AS `qty_refunded`,
  44.   COALESCE(IFNULL(item2.base_price, item2.base_price),
  45.            0)                                                                                                  AS `item_price`,
  46.   CASE item2.product_type
  47.   WHEN "configurable"
  48.     THEN COALESCE(SUM(item2.base_cost), item2.base_cost, 0)
  49.   WHEN "bundle"
  50.     THEN IF(bundle_item_price.price = 0, COALESCE(item2.base_cost, SUM(item2.base_cost), 0),
  51.             COALESCE(SUM(item2.base_cost), 0))
  52.   ELSE COALESCE(item2.base_cost,
  53.                 0) END                                                                                         AS `item_cost`,
  54.   COALESCE((IFNULL(item2.qty_ordered, item2.qty_ordered) * item2.base_price))                                             AS `subtotal`,
  55.   COALESCE((IF(item2.base_discount_amount = 0, SUM(item2.base_discount_amount), item2.base_discount_amount)),
  56.            0)                                                                                                  AS `discount`,
  57.   COALESCE((IFNULL(item2.base_tax_amount, item2.base_tax_amount)), 0)                                           AS `tax`,
  58.   COALESCE((IFNULL(item2.base_row_total, item2.base_row_total) +
  59.             IFNULL(item2.base_discount_tax_compensation_amount, 0.0000) +
  60.             IFNULL(COALESCE(item2.base_weee_tax_applied_amount, 0), COALESCE(item2.base_weee_tax_applied_amount, 0)) -
  61.             COALESCE(IF(item2.base_discount_amount = 0, SUM(item2.base_discount_amount), item2.base_discount_amount), 0)),
  62.            0)                                                                                                  AS `total`,
  63.   (IFNULL(item2.base_row_total, item2.base_row_total) + IFNULL(item2.base_tax_amount, item2.base_tax_amount) +
  64.    IFNULL(item2.base_discount_tax_compensation_amount, 0.0000) +
  65.    IFNULL(COALESCE(item2.base_weee_tax_applied_amount, 0), COALESCE(item2.base_weee_tax_applied_amount, 0)) -
  66.    COALESCE(IF(item2.base_discount_amount = 0, SUM(item2.base_discount_amount), item2.base_discount_amount),
  67.             0))                                                                                                AS `total_incl_tax`,
  68.   COALESCE((COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_row_invoiced), NULL),
  69.                      item2.base_row_invoiced, item2.base_row_invoiced) +
  70.             IFNULL(COALESCE(item2.base_discount_tax_compensation_invoiced, 0),
  71.                    COALESCE(item2.base_discount_tax_compensation_invoiced, 0)) -
  72.             COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_discount_invoiced), NULL),
  73.                      item2.base_discount_invoiced, item2.base_discount_invoiced)),
  74.            0)                                                                                                  AS `invoiced`,
  75.   COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_tax_invoiced), NULL),
  76.            item2.base_tax_invoiced, item2.base_tax_invoiced,
  77.            0)                                                                                                  AS `tax_invoiced`,
  78.   COALESCE((COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_row_invoiced), NULL),
  79.                      item2.base_row_invoiced, item2.base_row_invoiced) +
  80.             IFNULL(COALESCE(item2.base_discount_tax_compensation_invoiced, 0),
  81.                    COALESCE(item2.base_discount_tax_compensation_invoiced, 0)) +
  82.             COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_tax_invoiced), NULL),
  83.                      item2.base_tax_invoiced, item2.base_tax_invoiced) -
  84.             COALESCE(IF(item2.product_type = "bundle" AND item2.base_price > 0, SUM(item2.base_discount_invoiced), NULL),
  85.                      item2.base_discount_invoiced, item2.base_discount_invoiced)),
  86.            0)                                                                                                  AS `invoiced_incl_tax`,
  87.   COALESCE(((IF((IFNULL(item2.qty_refunded, item2.qty_refunded) > 0), 1, 0) *
  88.              ((IFNULL(item2.qty_refunded, item2.qty_refunded) / IFNULL(item2.qty_invoiced, item2.qty_invoiced)) *
  89.               (IFNULL(item2.qty_invoiced, item2.qty_invoiced) * IFNULL(item2.base_price, item2.base_price) - ABS(COALESCE(
  90.                                                                                                                    IF(
  91.                                                                                                                        item2.base_discount_amount
  92.                                                                                                                        =
  93.                                                                                                                        0,
  94.                                                                                                                        SUM(
  95.                                                                                                                            item2.base_discount_amount),
  96.                                                                                                                        item2.base_discount_amount),
  97.                                                                                                                    0)))))),
  98.            0)                                                                                                  AS `refunded`,
  99.   COALESCE(IF((IFNULL(item2.qty_refunded, item2.qty_refunded) > 0),
  100.               (IFNULL(item2.qty_refunded, item2.qty_refunded) / IFNULL(item2.qty_invoiced, item2.qty_invoiced) *
  101.                IFNULL(item2.base_tax_invoiced, item2.base_tax_invoiced)), 0),
  102.            0)                                                                                                  AS `tax_refunded`,
  103.   COALESCE(((IF((IFNULL(item2.qty_refunded, item2.qty_refunded) > 0), 1, 0) * ((IFNULL(item2.qty_refunded,
  104.                                                                                       item2.qty_refunded) * (
  105.                                                                                  IFNULL(item2.qty_invoiced,
  106.                                                                                         item2.qty_invoiced) *
  107.                                                                                  IFNULL(item2.base_price,
  108.                                                                                         item2.base_price) - ABS(
  109.                                                                                      COALESCE(IF(
  110.                                                                                                   item2.base_discount_amount
  111.                                                                                                   = 0, SUM(
  112.                                                                                                       item2.base_discount_amount),
  113.                                                                                                   item2.base_discount_amount),
  114.                                                                                               0))) /
  115.                                                                                IFNULL(item2.qty_invoiced,
  116.                                                                                       item2.qty_invoiced)) + IF((IFNULL(
  117.                                                                                                                      item2.qty_refunded,
  118.                                                                                                                      item2.qty_refunded)
  119.                                                                                                                  > 0), (
  120.                                                                                                                   IFNULL(
  121.                                                                                                                       item2.qty_refunded,
  122.                                                                                                                       item2.qty_refunded)
  123.                                                                                                                   /
  124.                                                                                                                   IFNULL(
  125.                                                                                                                       item2.qty_invoiced,
  126.                                                                                                                       item2.qty_invoiced)
  127.                                                                                                                   *
  128.                                                                                                                   IFNULL(
  129.                                                                                                                       item2.base_tax_invoiced,
  130.                                                                                                                       item2.base_tax_invoiced)),
  131.                                                                                                                 0)))),
  132.            0)                                                                                                  AS `refunded_incl_tax`,
  133.   `main_table`.`base_to_global_rate`                                                                           AS `to_global_rate`
  134. FROM `sales_order` AS `main_table`
  135.   INNER JOIN `sales_order_item` AS `item` ON (item2.order_id = main_table.entity_id AND item2.parent_item_id IS NULL)
  136.   LEFT JOIN `sales_order_item` AS `item2` ON (
  137.     item2.order_id = main_table.entity_id AND item2.parent_item_id IS NOT NULL AND item2.parent_item_id = item2.item_id
  138.     AND item2.product_type IN ("configurable", "bundle"))
  139.   LEFT JOIN (SELECT
  140.                `bundle_items`.`parent_item_id`,
  141.                `bundle_items`.`order_id`,
  142.                Sum(`bundle_items`.`price`)     AS price,
  143.                Sum(`bundle_items`.`base_cost`) AS items_cost
  144.              FROM `sales_order_item` AS `bundle_items`
  145.                INNER JOIN `sales_order_item` AS `bundle`
  146.                  ON bundle_items.parent_item_id = bundle.item_id AND bundle.product_type IN ("bundle")
  147.              GROUP BY `bundle_items`.`parent_item_id`,
  148.                `bundle_items`.`order_id`) AS `bundle_item_price`
  149.     ON (main_table.entity_id = bundle_item_price.order_id AND item2.item_id = bundle_item_price.parent_item_id)
  150.   LEFT JOIN `catalog_product_entity` AS `catalog_product` ON item2.product_id = catalog_product.entity_id
  151.   LEFT JOIN `customer_group` AS `c_group` ON IFNULL(main_table.customer_group_id, 0) = c_group.customer_group_id
  152.   LEFT JOIN `sales_order_address` AS `shipping_address`
  153.     ON shipping_address.parent_id = main_table.entity_id AND shipping_address.address_type = "shipping"
  154.   LEFT JOIN `sales_order_address` AS `billing_address`
  155.     ON billing_address.parent_id = main_table.entity_id AND billing_address.address_type = "billing"
  156.   LEFT JOIN `catalog_product_entity_int` AS `item_manufacturer`
  157.     ON item_manufacturer.entity_id = item2.product_id AND item_manufacturer.attribute_id = 83
  158.   LEFT JOIN `eav_attribute_option_value` AS `manufacturer_value`
  159.     ON item_manufacturer.value = manufacturer_value.option_id AND manufacturer_value.store_id = 0
  160.  
  161. WHERE (item2.product_type = 'bundle') and item.order_id = 4
  162. GROUP BY `item2`.`item_id`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement