11import { useState } from 'react' ;
22import Head from '../../components/head' ;
33import Layout from '../../components/layout' ;
4- import DatePicker from 'react-datepicker' ;
54import { fetchInvoices , markReconciled , unmarkReconciled } from '../../controllers/v2/invoices'
65import moment from 'moment' ;
76import { useEffect } from 'react' ;
@@ -20,6 +19,7 @@ import Button from '@material-ui/core/Button';
2019import Dialog from "@material-ui/core/Dialog" ;
2120import DialogContent from "@material-ui/core/DialogContent" ;
2221import Swal from 'sweetalert2' ;
22+ import * as XLSX from 'xlsx' ;
2323
2424const PaginationTheme = withStyles ( {
2525 actions : {
@@ -29,8 +29,8 @@ const PaginationTheme = withStyles({
2929} ) ( TablePagination ) ;
3030
3131export default function Reconcile ( ) {
32- const [ startDate , setStartDate ] = useState ( moment ( ) . toDate ( ) ) ;
33- const [ endDate , setEndDate ] = useState ( ) ;
32+ const [ startDate , setStartDate ] = useState ( null ) ;
33+ const [ endDate , setEndDate ] = useState ( null ) ;
3434 const [ comment , setComment ] = useState ( '' )
3535 const [ showModal , setShowModal ] = useState ( false )
3636 const [ offset , setOffset ] = useState ( 0 )
@@ -115,6 +115,46 @@ export default function Reconcile() {
115115 }
116116 }
117117
118+ async function handleDownloadCsv ( ) {
119+ try {
120+ const response = await fetchInvoices ( { startDate, endDate } )
121+ const invoices = response . data . data
122+ const data = [ [ 'Name' , 'Email' , 'Product' , 'List Price' , 'Final Price' , 'Discount' , 'CGST' , 'IGST' , 'SGST' , 'Tax' , 'Final Amount' , 'RazorPay Order Id' , 'RazorPay Payment Id' , 'Status' , 'Date of Sale' , 'Invoice Link' , 'Reconciled By' ] ]
123+
124+ invoices . map ( invoice => {
125+ const row = [ ]
126+ row . push ( invoice . cart . buyer . firstname + ' ' + invoice . cart . buyer . lastname )
127+ row . push ( invoice . cart . buyer . email )
128+ row . push ( invoice . product . name )
129+ row . push ( invoice . list_price / 100 )
130+ row . push ( invoice . final_price / 100 )
131+ row . push ( invoice . cart . total_discount / 100 )
132+ row . push ( invoice . cgst / 100 )
133+ row . push ( invoice . igst / 100 )
134+ row . push ( invoice . sgst / 100 )
135+ row . push ( invoice . cart . total_tax_collected / 100 )
136+ row . push ( invoice . amount / 100 )
137+ row . push ( invoice . transaction ?. razorpay ?. order_id || invoice . transaction ?. razorpay_order_id || 'N/A' )
138+ row . push ( invoice . transaction ?. razorpay ?. payment_id || invoice . transaction ?. razorpay_payment_id || 'N/A' )
139+ row . push ( invoice . transaction ?. status )
140+ row . push ( invoice . transaction ?. date_of_sale )
141+ row . push ( invoice . invoice_link || 'N/A' )
142+ row . push ( invoice . reconciledBy ? `${ invoice . reconciledBy . firstname } ${ invoice . reconciledBy . lastname } (${ invoice . reconciledBy . oneauth_id } ` : 'N/A' )
143+ data . push ( row )
144+ } )
145+ let workbook = XLSX . utils . book_new ( ) ;
146+ let worksheet = XLSX . utils . json_to_sheet ( data ) ;
147+ XLSX . utils . book_append_sheet ( workbook , worksheet ) ;
148+ XLSX . writeFile ( workbook , `invoices-${ startDate } -to-${ endDate } .csv` ) ;
149+ } catch ( err ) {
150+ Swal . fire ( {
151+ title : "Some Error occured!" ,
152+ type : "error" ,
153+ showConfirmButton : true
154+ } )
155+ }
156+ }
157+
118158 useEffect ( ( ) => {
119159 async function fetchData ( ) {
120160 const response = await fetchInvoices ( { offset, limit } )
@@ -131,20 +171,21 @@ export default function Reconcile() {
131171 < div className = "my-4" >
132172 < h3 className = "t-align-c" > Reconciler</ h3 >
133173
134- { /* <div className="my-3">
135- <span>Select Date Range</span>
174+ < div className = "my-3 px-5" >
175+ < span className = "bold font-md" > Select Date Range</ span >
176+ < div >
177+ < label for = "start-date" > Start Date:</ label >
178+ < input type = "date" id = "start-date" name = "start0date" onChange = { ( e ) => setStartDate ( e . target . value ) } > </ input >
179+ </ div >
180+ < div >
181+ < label for = "end-date" > End Date:</ label >
182+ < input type = "date" id = "end-date" name = "end-date" onChange = { ( e ) => setEndDate ( e . target . value ) } > </ input >
183+ </ div >
136184 </ div >
137- <DatePicker
138- className="border"
139- selectsRange
140- onChange={(update) => {
141- console.log(update)
142- }}
143- isClearable={true}
144- /> */ }
185+
145186
146- < div className = "my-4 d-flex justify-content-center" >
147- < Grid xs = { 11 } className = { "mt-5 mr-5" } >
187+ < div className = " d-flex justify-content-center" >
188+ < Grid xs = { 11 } className = { "mt-4 mr-5" } >
148189 < Paper >
149190 < TableContainer >
150191 < Grid container justify = "center" className = { "mb-1" } >
@@ -163,6 +204,10 @@ export default function Reconcile() {
163204 < div className = "flex-row justify-content-center" >
164205 < button className = "p-2" disabled = { ! ! ! selectedInvoiceIds . length } onClick = { ( ) => handleUnmarkReconciled ( ) } > Mark Un-Reconcile</ button >
165206 </ div >
207+ < div className = "divider-h" />
208+ < div className = "flex-row justify-content-center" >
209+ < button className = "p-2" disabled = { ! ! ! startDate || ! ! ! endDate } onClick = { ( ) => handleDownloadCsv ( ) } > Download Csv</ button >
210+ </ div >
166211 </ div >
167212 </ li >
168213 </ div >
@@ -176,6 +221,7 @@ export default function Reconcile() {
176221 < TableCell align = "center" className = { "red" } > Product</ TableCell >
177222 < TableCell align = "center" className = { "red" } > List Price</ TableCell >
178223 < TableCell align = "center" className = { "red" } > Final Price</ TableCell >
224+ < TableCell align = "center" className = { "red" } > Discount</ TableCell >
179225 < TableCell align = "center" className = { "red" } > CGST</ TableCell >
180226 < TableCell align = "center" className = { "red" } > IGST</ TableCell >
181227 < TableCell align = "center" className = { "red" } > SGST</ TableCell >
@@ -200,6 +246,7 @@ export default function Reconcile() {
200246 < TableCell > { invoice . product . name } </ TableCell >
201247 < TableCell > { invoice . list_price / 100 } </ TableCell >
202248 < TableCell > { invoice . final_price / 100 } </ TableCell >
249+ < TableCell > { invoice . cart . total_discount / 100 } </ TableCell >
203250 < TableCell > { invoice . cgst / 100 } </ TableCell >
204251 < TableCell > { invoice . igst / 100 } </ TableCell >
205252 < TableCell > { invoice . sgst / 100 } </ TableCell >
0 commit comments