-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajout export produits et promotions vers Excel
- Loading branch information
1 parent
0a9d8be
commit 166de89
Showing
7 changed files
with
253 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useQuery } from 'blitz' | ||
|
||
import TableChartIcon from '@mui/icons-material/TableChart' | ||
import { Button } from '@mui/material' | ||
import xlsx from 'xlsx' | ||
import getArticlesWithStats from '../../../entities/articles/queries/getArticlesWithStats' | ||
|
||
export default function ExportArticles() { | ||
const [{ articles }] = useQuery<any>(getArticlesWithStats, {}) | ||
|
||
const exportToExcel = () => { | ||
const date = new Date(Date.now()) | ||
|
||
const workBook = xlsx.utils.book_new() | ||
|
||
workBook.Props = { | ||
Title: 'BDE ISIMA - Utilisateurs', | ||
Subject: 'Liste des utilisateurs', | ||
CreatedDate: date, | ||
} | ||
|
||
workBook.SheetNames.push('Articles') | ||
|
||
console.log(articles) | ||
console.log(articles[24].name) | ||
console.log(articles[24].totalCount) | ||
|
||
const workSheetData = [ | ||
[ | ||
'Nom', | ||
'Prix', | ||
'Prix cotisants', | ||
'Visible', | ||
'Ajouté le', | ||
'Nombre vente dernière semaine', | ||
'Nombre vente dernier mois', | ||
'Nombre vente dernière année', | ||
'Nombre vente total', | ||
], | ||
...articles.map((article) => [ | ||
article.name, | ||
article.price, | ||
article.member_price ?? article.price, | ||
article.is_enabled ? 'Oui' : 'Non', | ||
new Date(article.createdAt).toLocaleDateString('fr'), | ||
article.weekCount ?? 0, | ||
article.monthCount ?? 0, | ||
article.yearCount ?? 0, | ||
article.totalCount ?? 0, | ||
]), | ||
] | ||
|
||
const workSheet = xlsx.utils.aoa_to_sheet(workSheetData) | ||
workBook.Sheets['Articles'] = workSheet | ||
|
||
workSheet['!autofilter'] = { ref: 'A1:I1' } | ||
|
||
xlsx.writeFile( | ||
workBook, | ||
`bde_isima_articles-${date.getFullYear()}-${ | ||
date.getMonth() + 1 | ||
}-${date.getDate()}_${date.getHours()}-${date.getMinutes()}.xlsx` | ||
) | ||
} | ||
|
||
return ( | ||
<Button endIcon={<TableChartIcon />} variant={'contained'} onClick={exportToExcel}> | ||
Exporter vers Excel | ||
</Button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useQuery } from 'blitz' | ||
|
||
import TableChartIcon from '@mui/icons-material/TableChart' | ||
import { Button } from '@mui/material' | ||
import xlsx from 'xlsx' | ||
import getPromotions from '../../../entities/promotions/queries/getPromotions' | ||
|
||
export default function ExportPromotions() { | ||
const [{ promotions }] = useQuery<any>(getPromotions, { | ||
include: { | ||
_count: { | ||
select: { | ||
User: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
year: 'asc', | ||
}, | ||
}) | ||
|
||
const exportToExcel = () => { | ||
const date = new Date(Date.now()) | ||
|
||
const workBook = xlsx.utils.book_new() | ||
|
||
workBook.Props = { | ||
Title: 'BDE ISIMA - Utilisateurs', | ||
Subject: 'Liste des utilisateurs', | ||
CreatedDate: date, | ||
} | ||
|
||
workBook.SheetNames.push('Promotions') | ||
|
||
const workSheetData = [ | ||
['Année', 'ID Groupe Facebook', 'Liste de diffusion', "Nombre d'utilisateurs"], | ||
...promotions.map((promotion) => [ | ||
promotion.year, | ||
promotion.fb_group_id, | ||
promotion.list_email, | ||
promotion._count.User, | ||
]), | ||
] | ||
|
||
const workSheet = xlsx.utils.aoa_to_sheet(workSheetData) | ||
workBook.Sheets['Promotions'] = workSheet | ||
|
||
workSheet['!autofilter'] = { ref: 'A1:D1' } | ||
|
||
xlsx.writeFile( | ||
workBook, | ||
`bde_isima_promotions-${date.getFullYear()}-${ | ||
date.getMonth() + 1 | ||
}-${date.getDate()}_${date.getHours()}-${date.getMinutes()}.xlsx` | ||
) | ||
} | ||
|
||
return ( | ||
<Button endIcon={<TableChartIcon />} variant={'contained'} onClick={exportToExcel}> | ||
Exporter vers Excel | ||
</Button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { resolver } from 'blitz' | ||
|
||
import db, { Prisma } from 'db' | ||
|
||
type GetArticlesInput = Pick< | ||
Prisma.ArticleFindManyArgs, | ||
'include' | 'where' | 'orderBy' | 'skip' | 'take' | ||
> | ||
|
||
export default resolver.pipe(resolver.authorize(['*', 'bde']), async () => { | ||
const todayDate = new Date(Date.now()) | ||
const lastWeekDate = new Date( | ||
todayDate.getFullYear(), | ||
todayDate.getMonth(), | ||
todayDate.getDate() - 7 | ||
) | ||
const lastMonthDate = new Date( | ||
todayDate.getFullYear(), | ||
todayDate.getMonth() - 1, | ||
todayDate.getDate() | ||
) | ||
const lastYearDate = new Date( | ||
todayDate.getFullYear() - 1, | ||
todayDate.getMonth(), | ||
todayDate.getDate() | ||
) | ||
|
||
const articles = await db.$queryRaw<any>` | ||
SELECT | ||
"Article"."id", | ||
"Article"."name", | ||
"Article"."price", | ||
"Article"."member_price", | ||
"Article"."image", | ||
"Article"."is_enabled", | ||
"Article"."createdAt", | ||
"Article"."updatedAt", | ||
week.count AS "weekCount", | ||
month.count AS "monthCount", | ||
year.count AS "yearCount", | ||
total.count AS "totalCount" | ||
FROM "Article" | ||
LEFT JOIN ( | ||
SELECT "Transaction"."articleId", COUNT("Transaction"."id") AS count FROM "Transaction" | ||
GROUP BY "Transaction"."articleId" | ||
) total ON "Article"."id" = total."articleId" | ||
LEFT JOIN ( | ||
SELECT "Transaction"."articleId", COUNT("Transaction"."id") AS count FROM "Transaction" | ||
WHERE "Transaction"."createdAt" >= ${lastWeekDate}::date AND "Transaction"."createdAt" <= ${todayDate}::date | ||
GROUP BY "Transaction"."articleId" | ||
) week ON "Article".id = "week"."articleId" | ||
LEFT JOIN ( | ||
SELECT "Transaction"."articleId", COUNT("Transaction"."id") AS count FROM "Transaction" | ||
WHERE "Transaction"."createdAt" >= ${lastMonthDate}::date AND "Transaction"."createdAt" <= ${todayDate}::date | ||
GROUP BY "Transaction"."articleId" | ||
) month ON "Article".id = "month"."articleId" | ||
LEFT JOIN ( | ||
SELECT "Transaction"."articleId", COUNT("Transaction"."id") FROM "Transaction" | ||
WHERE "Transaction"."createdAt" >= ${lastYearDate}::date AND "Transaction"."createdAt" <= ${todayDate}::date | ||
GROUP BY "Transaction"."articleId" | ||
) year ON "Article".id = "year"."articleId" | ||
ORDER BY "Article"."name" | ||
` | ||
|
||
const count = articles.length ?? 0 | ||
const hasMore = false | ||
const nextPage = null | ||
|
||
return { | ||
articles, | ||
nextPage, | ||
hasMore, | ||
count, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters