@@ -9,44 +9,33 @@ const downloadReport = require('./downloadGr');
99 * @param {number } year Year to download
1010 * @param {number } month Month of the year to download, 1-12
1111 */
12- async function downloadSalesReportCsv ( year , month ) {
13- const json = await downloadReport ( year , month , 'sales' ) ;
12+ async function downloadSalesReportCsv ( year , month , type = 'sales' ) {
13+ const json = await downloadReport ( year , month , type ) ;
1414 const orders = { } ;
1515
1616 json . forEach ( ( row ) => {
17- const id = row [ 'Order Number' ] ;
18- const date = row [ 'Order Charged Date' ] ;
19- const product = row [ 'Product Title' ] ;
20- const productId = row [ 'SKU ID' ] ;
21- const cityOfBuyer = row [ 'City of Buyer' ] ;
22- const stateOfBuyer = row [ 'State of Buyer' ] ;
23- const countryOfBuyer = row [ 'Country of Buyer' ] ;
24- const chargedAmount = row [ 'Charged Amount' ] . replace ( ',' , '' ) ;
25-
26- if ( orders [ id ] ) {
27- const order = orders [ id ] ;
28- order . chargedAmount = new Decimal ( order . chargedAmount ) . add ( chargedAmount ) . toString ( ) ;
29- return ;
17+ if ( type === 'sales' ) {
18+ sales ( row , orders ) ;
19+ } else {
20+ earnings ( row , orders ) ;
3021 }
31-
32- orders [ id ] = {
33- id,
34- date : date . replace ( / , / g, '' ) ,
35- product,
36- productId,
37- cityOfBuyer,
38- stateOfBuyer,
39- countryOfBuyer,
40- chargedAmount,
41- } ;
4222 } ) ;
4323
44- let csv = 'Order Id,Date,Product,Product Id,Country of Buyer,State of Buyer,City of Buyer,Amount in Buyer Currency\n' ;
45- Object . values ( orders ) . forEach ( ( order ) => {
46- csv += `${ order . id } ,${ order . date } ,${ order . product } ,${ order . productId } ,${ order . countryOfBuyer } ,${ order . stateOfBuyer } ,${ order . cityOfBuyer } ,${ order . chargedAmount } \n` ;
47- } ) ;
24+ let csv ;
25+
26+ if ( type === 'earnings' ) {
27+ csv = 'ID,Date,Product,Amount\n' ;
28+ Object . values ( orders ) . forEach ( ( order ) => {
29+ csv += `${ order . id } ,${ order . date } ,${ order . product } ,${ order . amount } \n` ;
30+ } ) ;
31+ } else {
32+ csv = 'Order Id,Date,Product,Product Id,Country of Buyer,State of Buyer,City of Buyer,Amount in Buyer Currency\n'
33+ Object . values ( orders ) . forEach ( ( order ) => {
34+ csv += `${ order . id } ,${ order . date } ,${ order . product } ,${ order . productId } ,${ order . countryOfBuyer } ,${ order . stateOfBuyer } ,${ order . cityOfBuyer } ,${ order . chargedAmount } \n` ;
35+ } ) ;
36+ }
4837
49- const fileName = `sales -${ year } -${ month } .csv` ;
38+ const fileName = `${ type } -${ year } -${ month } .csv` ;
5039 const dirname = path . resolve ( '../reports' ) ;
5140 const filePath = path . resolve ( dirname , fileName ) ;
5241 if ( ! fileSystem . existsSync ( dirname ) ) {
@@ -57,4 +46,52 @@ async function downloadSalesReportCsv(year, month) {
5746 return filePath ;
5847}
5948
49+ function sales ( row , orders ) {
50+ const id = row [ 'Order Number' ] ;
51+ const date = row [ 'Order Charged Date' ] ;
52+ const product = row [ 'Product Title' ] ;
53+ const productId = row [ 'SKU ID' ] ;
54+ const cityOfBuyer = row [ 'City of Buyer' ] ;
55+ const stateOfBuyer = row [ 'State of Buyer' ] ;
56+ const countryOfBuyer = row [ 'Country of Buyer' ] ;
57+ const chargedAmount = row [ 'Charged Amount' ] . replace ( ',' , '' ) ;
58+
59+ if ( orders [ id ] ) {
60+ const order = orders [ id ] ;
61+ order . chargedAmount = new Decimal ( order . chargedAmount ) . add ( chargedAmount ) . toString ( ) ;
62+ return ;
63+ }
64+
65+ orders [ id ] = {
66+ id,
67+ date : date . replace ( / , / g, '' ) ,
68+ product,
69+ productId,
70+ cityOfBuyer,
71+ stateOfBuyer,
72+ countryOfBuyer,
73+ chargedAmount,
74+ } ;
75+ }
76+
77+ function earnings ( row , orders ) {
78+ const id = row [ 'Description' ] ;
79+ const date = row [ 'Transaction Date' ] ;
80+ const product = row [ 'Product Title' ] ;
81+ const amount = row [ 'Amount (Merchant Currency)' ] ;
82+
83+ if ( orders [ id ] ) {
84+ const order = orders [ id ] ;
85+ order . amount = new Decimal ( order . amount ) . add ( amount ) . toString ( ) ;
86+ return ;
87+ }
88+
89+ orders [ id ] = {
90+ id,
91+ date : date . replace ( / , / g, '' ) ,
92+ product,
93+ amount,
94+ } ;
95+ }
96+
6097module . exports = downloadSalesReportCsv ;
0 commit comments