HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /www/exchange0old/exchange/custom_actions/support_177.sql
/*
TRUNCATE new_rates;
LOAD DATA LOCAL INFILE 'D:\\Originator Rates Amended.csv' INTO TABLE `debxdemo_aprl_live`.`new_rates` CHARACTER SET cp1251 FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' (`start_date`, `end_date`, `organisation_reference_id`, `pp`, `process`, `sell_rate`);
UPDATE new_rates
SET end_date=CURDATE()
WHERE end_date="1900-01-00" OR end_date="0000-00-00";
UPDATE new_rates
SET start_date=CURDATE()
WHERE start_date="1900-01-00" OR start_date="0000-00-00";
DELETE FROM new_rates
WHERE start_date = end_date AND end_date= CURDATE();
UPDATE new_rates
SET sell_rate = sell_rate *100, pp = pp*100;
UPDATE `new_rates` SET `sell_rate`=0.875000000 WHERE `row_id`=230 LIMIT 1;
UPDATE `new_rates` SET `sell_rate`=0.875000000 WHERE `row_id`=294 LIMIT 1;
UPDATE new_rates SET start_date='2012-01-01' WHERE start_date='2013-07-04';
*/

/**
UPDATE invoice_master
SET invoice_master.logapprove_date = invoice_master.created_at
WHERE invoice_master.root_invoice_id>0 AND invoice_master.invoice_status=3 AND invoice_master.posting_interface=1 AND invoice_master.authorised_at!="0000-00-00 00:00:00";
**/

DROP TABLE IF EXISTS `new_rates`;
CREATE TABLE `new_rates` (
    `row_id` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `reference_id` BIGINT(10) UNSIGNED NULL DEFAULT NULL,
    `start_date` DATE NULL DEFAULT NULL,
    `end_date` DATE NULL DEFAULT NULL,
    `sell_rate` DOUBLE(9,7) UNSIGNED NULL DEFAULT NULL,
    `pp` DOUBLE(6,3) UNSIGNED NULL DEFAULT NULL,
    `cetr_discount` DOUBLE(6,3) UNSIGNED NULL DEFAULT NULL,
    `process` DOUBLE(5,3) UNSIGNED NULL DEFAULT NULL,
    `type` VARCHAR(50) NULL DEFAULT 'originator',
    PRIMARY KEY (`row_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=517;

DELETE FROM `new_rates`;

INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (2, 11307000040, '2012-01-01', '2014-01-29', 0.8250000, 94.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (3, 11307000041, '2012-01-01', '2014-07-19', 0.8250000, 94.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (4, 11307000043, '2012-01-01', '2014-02-05', 0.8250000, 91.500, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (5, 11307000332, '2015-09-11', '2017-09-16', 0.8750000, 15.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (6, 11308000053, '2012-01-01', '2014-05-23', 0.8250000, 80.000, NULL, 3.330, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (7, 11309000063, '2012-01-01', '2015-07-09', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (8, 11309000064, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (9, 11309000065, '2012-01-01', '2017-09-16', 0.8320000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (10, 11310000066, '2012-01-01', '2014-09-11', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (11, 11310000067, '2012-01-01', '2014-05-15', 0.8500000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (12, 11310000068, '2012-01-01', '2014-07-01', 0.8750000, 90.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (13, 11310000070, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (14, 11310000071, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (15, 11310000073, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (16, 11401000099, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (17, 11401000106, '2012-01-01', '2014-06-04', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (18, 11402000107, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (19, 11403000111, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (20, 11403000118, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (21, 11405000126, '2012-01-01', '2015-06-18', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (22, 11405000128, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (23, 11405000135, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (24, 11406000142, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (25, 11406000143, '2012-01-01', '2014-11-05', 1.0000000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (26, 11407000153, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (27, 11408000158, '2012-01-01', '2015-08-17', 0.8750000, 90.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (28, 11409000165, '2012-01-01', '2017-09-16', 0.8750000, 80.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (29, 11409000171, '2012-01-01', '2017-09-16', 1.0000000, NULL, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (30, 11411000184, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (31, 11411000191, '2012-01-01', '2017-09-16', 1.0000000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (32, 11412000193, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (33, 11412000206, '2012-01-01', '2015-01-22', 0.8750000, 52.870, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (34, 11501000212, '2012-01-01', '2017-09-16', 0.8750000, NULL, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (35, 11501000221, '2012-01-01', '2015-02-03', 0.8750000, 85.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (36, 11502000237, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (37, 11502000241, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (38, 11502000243, '2012-01-01', '2015-02-21', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (39, 11503000250, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (40, 11503000258, '2012-01-01', '2017-09-16', 0.8750000, 80.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (41, 11503000259, '2012-01-01', '2015-09-07', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (42, 11503000261, '2012-01-01', '2017-09-16', 0.8750000, 80.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (43, 11503000262, '2012-01-01', '2015-05-12', 0.8750000, 65.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (44, 11503000263, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (45, 11503000266, '2012-01-01', '2017-09-16', 0.8750000, NULL, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (46, 11503000267, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (47, 11504000271, '2012-01-01', '2017-09-16', 1.0000000, NULL, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (48, 11504000272, '2012-01-01', '2017-09-16', 0.8750000, 60.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (49, 11504000275, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (50, 11504000277, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (51, 11504000278, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (52, 11504000280, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (53, 11504000282, '2012-01-01', '2015-06-19', 0.8750000, 62.770, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (54, 11504000284, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (55, 11504000289, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (56, 11505000293, '2012-01-01', '2015-08-01', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (57, 11506000307, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (58, 11506000310, '2012-01-01', '2017-09-16', 0.8750000, NULL, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (59, 11502000244, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (60, 11507000321, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (61, 11307000332, '2012-01-01', '2015-09-11', 0.8750000, 20.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (62, 11508000333, '2012-01-01', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (63, 11507000325, '2012-01-01', '2017-09-16', 0.8750000, 80.000, 0.000, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (64, 11507000330, '2012-01-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (65, 11508000343, '2012-01-01', '2017-09-16', 0.8750000, 75.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (66, 11307000040, '2014-01-29', '2014-04-30', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (67, 11307000041, '2014-07-19', '2014-08-20', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (68, 11307000043, '2014-02-05', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (70, 11308000053, '2014-05-23', '2014-05-31', 0.8750000, 50.000, NULL, 3.330, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (71, 11309000063, '2015-07-09', '2017-09-16', 0.8750000, 70.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (74, 11310000066, '2014-09-11', '2015-01-12', 0.8750000, 85.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (75, 11310000067, '2014-05-15', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (76, 11310000068, '2014-07-01', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (81, 11401000106, '2014-06-04', '2015-03-30', 0.8750000, 70.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (85, 11405000126, '2015-06-18', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (89, 11406000143, '2014-11-05', '2017-09-16', 2.0000000, 88.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (91, 11408000158, '2015-08-17', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (97, 11412000206, '2015-01-22', '2017-09-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (99, 11501000221, '2015-02-03', '2015-02-04', 0.8750000, 53.270, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (102, 11502000243, '2015-02-21', '2015-04-16', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (105, 11503000259, '2015-09-07', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (107, 11503000262, '2015-05-12', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (117, 11504000282, '2015-06-19', '2015-08-18', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (120, 11505000293, '2015-08-01', '2017-09-16', 0.8750000, 70.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (130, 11307000040, '2014-04-30', '2014-05-22', 0.8750000, 75.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (131, 11307000041, '2014-08-20', '2014-09-19', 0.8750000, 75.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (134, 11308000053, '2014-05-31', '2017-09-16', 0.8750000, 60.000, NULL, 3.330, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (138, 11310000066, '2015-01-12', '2015-05-06', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (145, 11401000106, '2015-03-30', '2017-09-16', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (163, 11501000221, '2015-02-04', '2017-09-16', 0.8750000, 85.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (166, 11502000243, '2015-04-16', '2015-04-29', 0.8750000, 80.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (181, 11504000282, '2015-08-18', '2017-09-16', 0.8750000, 70.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (194, 11307000040, '2014-05-22', '2017-09-16', 0.8750000, 50.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (195, 11307000041, '2014-09-19', '2017-09-16', 0.8750000, 85.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (202, 11310000066, '2015-05-06', '2017-09-16', 0.8750000, 85.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (230, 11502000243, '2015-04-29', '2015-04-30', 0.8750000, 90.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (294, 11502000243, '2015-04-30', '2017-09-16', 0.8750000, 70.000, NULL, 2.500, 'originator');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (512, 20130710000023, '2014-09-19', '2017-09-16', NULL, 80.000, NULL, NULL, 'debtor');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (513, 20130710000027, '2014-09-19', '2017-09-16', NULL, 80.000, NULL, NULL, 'debtor');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (514, 20130710000032, '2014-09-19', '2017-09-16', NULL, 80.000, NULL, NULL, 'debtor');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (515, 20130711000057, '2014-09-24', '2017-09-16', NULL, 80.000, NULL, NULL, 'debtor');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (517, 150622019966, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (518, 150626020328, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (519, 150626020329, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (520, 150626020330, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (521, 150626020331, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (522, 150626020332, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (523, 150626020333, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');
INSERT INTO `new_rates` (`row_id`, `reference_id`, `start_date`, `end_date`, `sell_rate`, `pp`, `cetr_discount`, `process`, `type`) VALUES (524, 150715022397, NULL, NULL, 0.8750000, 90.000, NULL, 2.500, 'invoice');

UPDATE invoice_closed
LEFT JOIN invoice_closed_attributes ON invoice_closed_attributes.invoice_id = invoice_closed.invoice_id
LEFT JOIN invoice_master ON invoice_master.invoice_id = invoice_closed_attributes.invoice_id
LEFT JOIN organisation_details ON organisation_details.user_id = invoice_master.user_id
LEFT JOIN debtors_detail ON debtors_detail.debtor_id = invoice_master.debtor_id
LEFT JOIN new_rates ON organisation_details.organisation_reference_id = new_rates.reference_id AND DATE(invoice_master.logapprove_date) >= new_rates.start_date AND DATE(invoice_master.logapprove_date)< new_rates.end_date 
SET invoice_closed_attributes.purchase_discount = new_rates.pp,  
invoice_closed_attributes.processing_fee = new_rates.`process`, 
invoice_closed.purchase_payment=  (invoice_master.face_value * (new_rates.pp/100)) - new_rates.`process`,
invoice_master.max_thirty_day_reserve = new_rates.sell_rate
WHERE new_rates.reference_id IS NOT NULL AND new_rates.`type`='originator' AND debtors_detail.`type`='debtor' AND new_rates.pp IS NOT NULL;

UPDATE invoice_master
LEFT JOIN organisation_details ON organisation_details.user_id = invoice_master.user_id
LEFT JOIN debtors_detail ON debtors_detail.debtor_id = invoice_master.debtor_id
LEFT JOIN new_rates ON organisation_details.organisation_reference_id = new_rates.reference_id AND DATE(invoice_master.logapprove_date) >= new_rates.start_date AND DATE(invoice_master.logapprove_date)< new_rates.end_date 
SET invoice_master.payment_discount = new_rates.cetr_discount
WHERE new_rates.reference_id IS NOT NULL AND new_rates.`type`='originator' AND debtors_detail.`type`='creditor' AND new_rates.cetr_discount IS NOT NULL;


UPDATE invoice_closed
LEFT JOIN invoice_closed_attributes ON invoice_closed_attributes.invoice_id = invoice_closed.invoice_id
LEFT JOIN invoice_master ON invoice_master.invoice_id = invoice_closed_attributes.invoice_id
LEFT JOIN debtors_detail ON debtors_detail.debtor_id = invoice_master.debtor_id
LEFT JOIN new_rates ON debtors_detail.debtor_reference_id = new_rates.reference_id AND DATE(invoice_master.logapprove_date) >= new_rates.start_date AND DATE(invoice_master.logapprove_date)< new_rates.end_date 
SET invoice_closed_attributes.purchase_discount = new_rates.pp,  
invoice_closed.purchase_payment=  (invoice_master.face_value * (new_rates.pp/100)) - invoice_closed_attributes.processing_fee
WHERE new_rates.reference_id IS NOT NULL AND new_rates.`type`='debtor';


UPDATE invoice_closed
LEFT JOIN invoice_closed_attributes ON invoice_closed_attributes.invoice_id = invoice_closed.invoice_id
LEFT JOIN invoice_master ON invoice_master.invoice_id = invoice_closed_attributes.invoice_id
LEFT JOIN new_rates ON invoice_master.trade_reference_id = new_rates.reference_id AND DATE(invoice_master.logapprove_date) >= new_rates.start_date AND DATE(invoice_master.logapprove_date)< new_rates.end_date 
SET invoice_closed_attributes.purchase_discount = new_rates.pp,  
invoice_closed.purchase_payment=  (invoice_master.face_value * (new_rates.pp/100)) - invoice_closed_attributes.processing_fee
WHERE new_rates.reference_id IS NOT NULL AND new_rates.`type`='invoice';

DROP TABLE IF EXISTS `new_rates`;