Advertisement
Guest User

Untitled

a guest
Aug 16th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.58 KB | None | 0 0
  1. SELECT *,
  2.        (`o`.`total_inc_vat` - `o`.`__total_paid`) AS `total_remaining`,
  3.        `o`.`__total_paid` AS `total_amount_paid`,
  4.        IF((`o`.`total_inc_vat` - `o`.`__total_paid`) = 0, 1, 0) AS `order_fully_paid`,
  5.        IF(`o`.`recurring_id` IS NOT NULL, 1, 0) AS `is_recurring_order` FROM
  6. (
  7.     SELECT *,
  8.             ROUND((`o`.`total` * (`o`.`vat` / 100)), 2) AS `total_vat`,
  9.             ROUND(`o`.`total` + (`o`.`total` * (`o`.`vat` / 100)), 2) AS `total_inc_vat`,
  10.             ROUND(`o`.`real_total` + (`o`.`real_total` * (`o`.`vat` / 100)), 2) AS `total_before_discounts`,
  11.             ROUND(`o`.`total` + (`o`.`total` * (`o`.`vat` / 100)), 2) AS `_total_remaining`,
  12.             SUM(`o`.`trays_left`) AS `tray_count`,
  13.             IF(`o`.`_total_paid` > 0, `o`.`_total_paid`, 0) AS `__total_paid`
  14.     FROM
  15.     (
  16.          SELECT `o`.`order_id`,
  17.                 `o`.`order_number`,
  18.                 `o`.`customer_id`,
  19.                 `o`.`ordered_date`,
  20.                 `o`.`delivery_date`,
  21.                 `o`.`discount`,
  22.                 `o`.`discount_type`,
  23.                 `o`.`delivery_notes`,
  24.                 `o`.`bakers_notes`,
  25.                 `o`.`alt_address`,
  26.                 `o`.`invoice_address`,
  27.                 `o`.`payment_type`,
  28.                 `o`.`additional_charges`,
  29.                 `o`.`vat`,
  30.                 `o`.`priority`,
  31.                 `o`.`trays_left`,
  32.                 `os`.`status` AS `order_status`,
  33.                 IF(`os`.`status` = 3, 1, 0) AS `has_dispatched`,
  34.                 IF(`os`.`status` = 3, `os`.`date_changed`, 0) AS `dispatched_date`,
  35.                 `o`.`dispatch_email_sent`,
  36.                
  37.                 IF(`os`.`status` = 2 OR `os`.`status` = 3, 1, 0) AS `is_completed`,
  38.                 IF(`os`.`status` = 2 OR `os`.`status` = 3, `os`.`date_changed`, 0) AS `completed_date`,
  39.  
  40.                 `o`.`invoice_last_sent`,
  41.                 `o`.`ref_order_number`,
  42.                 `o`.`duplicate_order_id`,
  43.                 `ro`.`recurring_id`,
  44.                 `ro`.`legacy_id` AS `legacy_recurring_id`,
  45.                 `ro`.`repeat_date`,
  46.                 `ro`.`repeat_day`,
  47.                 `ro`.`repeat_week`,
  48.                 `ro`.`repeat_month`,
  49.                 `ro`.`repeat_from`,
  50.                 `ro`.`repeat_till`,
  51.                 `ro`.`last_run`,
  52.                 `ro`.`next_run`,
  53.                 `ro`.`is_finished`,
  54.                 `o`.`recurred_from`,
  55.                 `o`.`recur_identifier`,
  56.                 `o`.`is_deleted`,
  57.  
  58.                 `oi`.`total` AS `real_total`,
  59.                 `op`.`_total_paid`,
  60.  
  61.                 IF(`o`.`discount_type` = 2,
  62.                     `oi`.`total` - `o`.`discount`,
  63.  
  64.                     IF(`o`.`discount_type` = 1,
  65.                         ROUND(`oi`.`total` - ((`oi`.`total` / 100) * `o`.`discount`), 2),
  66.                         `oi`.`total`
  67.                     )
  68.                 ) AS `total`,
  69.  
  70.                 IF(`o`.`discount_type` = 2,
  71.                     `o`.`discount`,
  72.  
  73.                     IF(`o`.`discount_type` = 1,
  74.                         ROUND(((`oi`.`total` / 100) * `o`.`discount`), 2),
  75.                         0
  76.                     )
  77.                 ) AS `discounted_amount`,
  78.  
  79.                 `oi`.`total_items`,
  80.                 `c`.`first_name`,
  81.                 `c`.`last_name`,
  82.                 `c`.`business`,
  83.                 `c`.`address_1`,
  84.                 `c`.`address_2`,
  85.                 `c`.`address_3`,
  86.                 `c`.`town`,
  87.                 `c`.`county`,
  88.                 `c`.`postcode`,
  89.  
  90.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',
  91.                     `ca`.`first_name`,
  92.                     `c`.`first_name`) AS `delivery_first_name`,
  93.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',
  94.                     `ca`.`last_name`,
  95.                     `c`.`last_name`) AS `delivery_last_name`,
  96.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',
  97.                     `ca`.`business`,
  98.                     `c`.`business`) AS `delivery_business`,
  99.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',
  100.                     `ca`.`address_1`,
  101.                     `c`.`address_1`) AS `delivery_address_1`,
  102.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  103.                     `ca`.`address_2`,  
  104.                     `c`.`address_2`) AS `delivery_address_2`,
  105.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  106.                     `ca`.`address_3`,  
  107.                     `c`.`address_3`) AS `delivery_address_3`,
  108.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  109.                     `ca`.`town`,  
  110.                     `c`.`town`) AS `delivery_town`,
  111.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  112.                     `ca`.`county`,  
  113.                     `c`.`county`) AS `delivery_county`,
  114.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  115.                     `ca`.`postcode`,  
  116.                     `c`.`postcode`) AS `delivery_postcode`,
  117.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  118.                     `ca`.`email`,  
  119.                     `c`.`email`) AS `delivery_email`,
  120.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  121.                     `ca`.`phone`,  
  122.                     `c`.`phone`) AS `delivery_phone`,
  123.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  124.                     `ca`.`mobile`,  
  125.                     `c`.`mobile`) AS `delivery_mobile`,
  126.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  127.                     `ca`.`latitude`,  
  128.                     `c`.`latitude`) AS `delivery_latitude`,
  129.                 IF(`o`.`alt_address` IS NOT NULL AND `ca`.`address_type` = 'D',  
  130.                     `ca`.`longitude`,  
  131.                     `c`.`longitude`) AS `delivery_longitude`,
  132.  
  133.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',
  134.                     `ca2`.`first_name`,
  135.                     `c`.`first_name`) AS `invoice_first_name`,
  136.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',
  137.                     `ca2`.`last_name`,
  138.                     `c`.`last_name`) AS `invoice_last_name`,
  139.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',
  140.                     `ca2`.`business`,
  141.                     `c`.`business`) AS `invoice_business`,
  142.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',
  143.                     `ca2`.`address_1`,
  144.                     `c`.`address_1`) AS `invoice_address_1`,
  145.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  146.                     `ca2`.`address_2`,  
  147.                     `c`.`address_2`) AS `invoice_address_2`,
  148.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  149.                     `ca2`.`address_3`,  
  150.                     `c`.`address_3`) AS `invoice_address_3`,
  151.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  152.                     `ca2`.`town`,  
  153.                     `c`.`town`) AS `invoice_town`,
  154.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  155.                     `ca2`.`county`,  
  156.                     `c`.`county`) AS `invoice_county`,
  157.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  158.                     `ca2`.`postcode`,  
  159.                     `c`.`postcode`) AS `invoice_postcode`,
  160.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  161.                     `ca2`.`email`,  
  162.                     `c`.`email`) AS `invoice_email`,
  163.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  164.                     `ca2`.`phone`,  
  165.                     `c`.`phone`) AS `invoice_phone`,
  166.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca2`.`address_type` = 'I',  
  167.                     `ca2`.`mobile`,  
  168.                     `c`.`mobile`) AS `invoice_mobile`,
  169.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca`.`address_type` = 'I',  
  170.                     `ca`.`latitude`,  
  171.                     `c`.`latitude`) AS `invoice_latitude`,
  172.                 IF(`o`.`invoice_address` IS NOT NULL AND `ca`.`address_type` = 'I',  
  173.                     `ca`.`longitude`,  
  174.                     `c`.`longitude`) AS `invoice_longitude`,
  175.                    
  176.                 `c`.`sage_account_code`,
  177.  
  178.                 `o`.`retail_order`,
  179.                 `c`.`is_deleted` AS `deleted_customer`
  180.      FROM
  181.      (
  182.          `orders` `o`
  183.  
  184.          INNER JOIN
  185.          (
  186.              SELECT `oi`.`order_id`,
  187.                     `oi`.`total_items`,
  188.                     `oi`.`_total` AS `total`
  189.              FROM
  190.              (
  191.                  SELECT SUM(`oi`.`quantity` * `oi`.`price`) + `o`.`additional_charges` AS `_total`,
  192.                         COUNT(`oi`.`item_id`) AS `total_items`,
  193.                         `oi`.`order_id`,
  194.                         `oi`.`item_id`
  195.                  FROM
  196.                  (
  197.                      `order_items` `oi`
  198.  
  199.                      INNER JOIN `orders` `o` ON `oi`.`order_id` = `o`.`order_id`
  200.                  )
  201.                  WHERE `oi`.`is_deleted` = 0
  202.                  GROUP BY `oi`.`order_id`
  203.              ) AS `oi`
  204.              GROUP BY `oi`.`order_id`
  205.          ) AS `oi` ON `oi`.`order_id` = `o`.`order_id`
  206.          
  207.          LEFT OUTER JOIN
  208.          (
  209.              SELECT SUM(`amount`) AS `_total_paid`,
  210.                     `order_id`
  211.              FROM `order_payments`
  212.              WHERE `is_deleted` = 0
  213.              GROUP BY `order_id`
  214.          ) AS `op` ON `op`.`order_id` = `o`.`order_id`
  215.  
  216.          LEFT OUTER JOIN `customers` `c` ON `c`.`customer_id` = `o`.`customer_id`
  217.  
  218.          LEFT OUTER JOIN
  219.          (
  220.              SELECT `address_id`,
  221.                     `customer_id`,
  222.                     `address_type`,
  223.                     `first_name`,
  224.                     `last_name`,
  225.                     `business`,
  226.                     `address_1`,
  227.                     `address_2`,
  228.                     `address_3`,
  229.                     `town`,
  230.                     `county`,
  231.                     `postcode`,
  232.                     `email`,
  233.                     `phone`,
  234.                     `mobile`,
  235.                     `latitude`,
  236.                     `longitude`
  237.              FROM `customer_addresses`
  238.          ) AS `ca` ON `ca`.`address_id` = `o`.`alt_address`
  239.          
  240.          LEFT OUTER JOIN
  241.          (
  242.              SELECT `address_id`,
  243.                     `customer_id`,
  244.                     `address_type`,
  245.                     `first_name`,
  246.                     `last_name`,
  247.                     `business`,
  248.                     `address_1`,
  249.                     `address_2`,
  250.                     `address_3`,
  251.                     `town`,
  252.                     `county`,
  253.                     `postcode`,
  254.                     `email`,
  255.                     `phone`,
  256.                     `mobile`,
  257.                     `latitude`,
  258.                     `longitude`
  259.              FROM `customer_addresses`
  260.          ) AS `ca2` ON `ca2`.`address_id` = `o`.`invoice_address`
  261.          
  262.          LEFT OUTER JOIN
  263.          (
  264.              SELECT IF(`parent_id` IS NULL, `id`, `parent_id`) AS `recurring_id`,
  265.                     `legacy_id`,
  266.                     `order_id`,
  267.                     GROUP_CONCAT(`repeat_date`) AS `repeat_date`,
  268.                     GROUP_CONCAT(`repeat_day`) AS `repeat_day`,
  269.                     GROUP_CONCAT(`repeat_week`) AS `repeat_week`,
  270.                     GROUP_CONCAT(`repeat_month`) AS `repeat_month`,
  271.                     GROUP_CONCAT(`repeat_from`) AS `repeat_from`,
  272.                     GROUP_CONCAT(`repeat_till`) AS `repeat_till`,
  273.                     GROUP_CONCAT(`last_run`) AS `last_run`,
  274.                     GROUP_CONCAT(`next_run`) AS `next_run`,
  275.                     GROUP_CONCAT(`is_finished`) AS `is_finished`
  276.              FROM `recurring_orders`
  277.              WHERE `is_deleted` = 0
  278.              GROUP BY `order_id`
  279.          ) AS `ro` ON `ro`.`order_id` = `o`.`order_id`
  280.          
  281.          LEFT OUTER JOIN
  282.          (
  283.              SELECT `os`.`order_id`,
  284.                     `os`.`status`,
  285.                     `os`.`date_changed`
  286.              FROM
  287.              (
  288.                  `order_status` `os`
  289.                  
  290.                  INNER JOIN `status_types` `st` ON `st`.`status_id` = `os`.`status`
  291.              )
  292.              ORDER BY `os`.`date_changed` DESC
  293.          ) AS `os` ON `os`.`order_id` = `o`.`order_id`
  294.      )
  295.      WHERE `o`.`is_deleted` = 0  AND `o`.`retail_order` = 0  
  296.   ) AS `o`
  297.   GROUP BY  `o`.`order_id`  
  298. ) AS `o`  
  299. ORDER BY `o`.`ordered_date` DESC
  300. LIMIT 0, 150
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement