select
a.voucher_value, a.voucher_number
as
exp_voucher,
date
(a.issue_time)
as
exp_issue_date, a.expiry_date
as
exp_expiry_date
, a.expiry_grace_date
as
exp_expiry_grace_date
, b.voucher_number
as
new_voucher,
date
(a.replace_time)
as
replace_date
, b.expiry_date
as
new_expiry_date
, b.expiry_grace_date
as
new_expiry_grace_date
from
(
select
a.expired_voucher_id, b.voucher_value, concat(prefix, voucher_text, suffix)
as
voucher_number,
max
(c.action_time)
as
issue_time, b.expiry_date
, DATE_ADD(b.expiry_date, INTERVAL $this->_grace_period_days
DAY
)
AS
expiry_grace_date
, a.replace_time
from
$this->_voucher_replace_details a, $this->_voucher b, $this->_voucher_action c
where
a.expired_voucher_id = b.voucher_id
and
b.voucher_id = c.voucher_id
and
c.action_id
in
(
'$issue_action_id'
,
'$issue_action_id2'
,
'$issue_action_id3'
)
$value_clause
AND
(a.replace_time
BETWEEN
'$quarter_start_date 00:00:00'
AND
'$quarter_end_date 23:59:59'
)
group
by
b.voucher_id
) a,
(
select
a.expired_voucher_id, b.voucher_value, concat(prefix, voucher_text, suffix)
as
voucher_number,
max
(c.action_time)
as
issue_time, b.expiry_date
, DATE_ADD(b.expiry_date, INTERVAL $this->_grace_period_days
DAY
)
AS
expiry_grace_date
from
$this->_voucher_replace_details a, $this->_voucher b, $this->_voucher_action c
where
a.new_voucher_id = b.voucher_id
and
b.voucher_id = c.voucher_id
and
c.action_id
in
(
'$issue_action_id'
,
'$issue_action_id2'
,
'$issue_action_id3'
)
AND
(a.replace_time
BETWEEN
'$quarter_start_date 00:00:00'
AND
'$quarter_end_date 23:59:59'
)
group
by
b.voucher_id
) b
where
a.expired_voucher_id = b.expired_voucher_id
ORDER
BY
2
ASC