Skip to content

Commit

Permalink
Merge pull request #714 from ecomplus/date-end-with-hour-report
Browse files Browse the repository at this point in the history
chore(campaign-report): fix date and link to order with filter
  • Loading branch information
matheusgnreis authored Sep 26, 2023
2 parents 8abe29c + e119d2e commit 4a5937e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
47 changes: 34 additions & 13 deletions src/controllers/campaign-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export default function () {
{
$match : {
created_at: {
$gte: `${start}T03:00:00.000Z`,
$lte: `${end}T02:59:59.999Z`,
$gte: start.toISOString(),
$lte: end.toISOString(),
},
'financial_status.current': 'paid',
'utm.campaign': {
Expand All @@ -156,11 +156,11 @@ export default function () {
source: [
{
$group: {
_id: { $toLower: '$utm.source'},
_id: '$utm.source',
count: { $sum: 1 },
total: { $sum: '$amount.total' },
subtotal: { $sum: '$amount.subtotal' },
discounts: { $sum: '$amount.discount' },
discounts: { $sum: '$amount.discount' }
}
},
{
Expand All @@ -170,7 +170,7 @@ export default function () {
campaign: [
{
$group: {
_id: { $toLower: '$utm.campaign'},
_id: '$utm.campaign',
count: { $sum: 1 },
total: { $sum: '$amount.total' },
subtotal: { $sum: '$amount.subtotal' },
Expand All @@ -187,23 +187,32 @@ export default function () {
})
}

const currentYear = new Date().getFullYear()
const normalizeDate = (hour, min, sec, ms, sub, date) => {
const currentDate = date || new Date()
if (sub) {
const dateWithSub = currentDate.getDate() - sub
currentDate.setDate(dateWithSub)
}
const finalDate = new Date(currentDate)
const dateWithHours = finalDate.setHours(hour, min, sec, ms)
return new Date(dateWithHours)
}
let start, end, type
start = `${currentYear}-01-01`
end = `${currentYear}-12-31`
start = normalizeDate(0, 0, 0, 0, 30, false)
end = normalizeDate(23, 59, 59, 999, false, false)
renderGraphs(start, end)

$('#datepicker [data-when="start"]').datepicker('setDate', `01/01/${currentYear}`);
$('#datepicker [data-when="end"]').datepicker('setDate', `31/12/${currentYear}`);
$('#datepicker-utm [data-when="start"]').datepicker('setDate', new Intl.DateTimeFormat('pt-br').format(start));
$('#datepicker-utm [data-when="end"]').datepicker('setDate', new Intl.DateTimeFormat('pt-br').format(end));

$('#datepicker').datepicker({}).on('changeDate', (e) => {
$('#datepicker-utm').datepicker({}).on('changeDate', (e) => {
if (e.date) {
if (e.target && e.target.dataset && e.target.dataset.when) {
type = e.target.dataset.when
if (type === 'start') {
start = e.date.toISOString().slice(0,10)
start = normalizeDate(0, 0, 0, 0, false, e.date)
} else if (type === 'end') {
end = e.date.toISOString().slice(0,10)
end = normalizeDate(23, 59, 59, 59, false, e.date)
}
if (start && end) {
renderGraphs(start, end, true)
Expand All @@ -212,6 +221,18 @@ export default function () {
}
})

$('#source-list').on('click', 'td', function () {
const table = $('#source-list').DataTable();
const data = table.cell( this ).data();
window.location.href = `/#/resources/orders?utm.source=${data}&financial_status.current=paid&created_at>=${start.toISOString()}&created_at<=${end.toISOString()}`
})

$('#campaign-list').on('click', 'td', function () {
const table = $('#campaign-list').DataTable();
const data = table.cell( this ).data();
window.location.href = `/#/resources/orders?utm.campaign=${data}&financial_status.current=paid&created_at>=${start.toISOString()}&created_at<=${end.toISOString()}`
})

const $exportSource = $('#export-source')
const $exportCampaign = $('#export-campaign')
const downloadCsv = (exportData, name) => {
Expand Down
6 changes: 3 additions & 3 deletions src/views/campaign-report.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<span data-lang="pt_br">Relatório de campanhas</span>
</big>

<div class="input-group float-right" id="datepicker" data-provide="datepicker" style="width: 300px">
<div class="input-group float-right" id="datepicker-utm" data-provide="datepicker" style="width: 300px">
<div class="input-group-prepend">
<span class="input-group-text i18n">
<span data-lang="en_us">From</span>
Expand Down Expand Up @@ -59,7 +59,7 @@ <h4 class="card-title i18n">
</span>
</a>
</div>
<table id="source-list" class="display" style="width:100%">
<table id="source-list" class="display" style="width:100%; cursor: pointer">
<thead>
<tr>
<th class="i18n">
Expand Down Expand Up @@ -123,7 +123,7 @@ <h4 class="card-title i18n">
</span>
</a>
</div>
<table id="campaign-list" class="display" style="width:100%">
<table id="campaign-list" class="display" style="width:100%; cursor: pointer">
<thead>
<tr>
<th class="i18n" width="50%">
Expand Down

0 comments on commit 4a5937e

Please sign in to comment.