Skip to content

Instantly share code, notes, and snippets.

@skyzh
Created February 22, 2019 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyzh/d4eb1288f2bfcc664068194551f7b87c to your computer and use it in GitHub Desktop.
Save skyzh/d4eb1288f2bfcc664068194551f7b87c to your computer and use it in GitHub Desktop.
Generate textbook order for classmates
const parse = require('csv').parse;
const RenderPDF = require('chrome-headless-render-pdf');
const fs = require('fs');
const _ = require('lodash');
var static = require('node-static');
var fileServer = new static.Server('./output');
fs.readFile(`${__dirname}/test_data.csv`, (err, data) => {
parse(data, {}, (err, data) => {
const textbook_name = _.drop(data[1], 3);
const textbook_price = _.drop(data[2], 3);
const students = _.drop(data, 3).slice(0, -2);
const _students = _.map(students, student => {
const student_data = {
name: student[0],
addr: student[1],
total: student[2],
amount: _.drop(student, 3)
};
return student_data;
});
const html = require('./page')(textbook_name, textbook_price, _students);
const path = `${__dirname}/output/order`;
fs.writeFileSync(`${path}.html`, html);
})
});
/*
pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
if (err) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});*/
const moment = require('moment');
const _ = require('lodash');
module.exports = (textbook_name, textbook_price, students) => {
moment.locale('zh-cn');
const orders = [];
_.forEach(students, student => {
const items = [];
_.forEach(student.amount, (amount, idx) => {
const _amount = amount || 0;
const _price = parseFloat(Math.round(textbook_price[idx] * 10) / 10).toFixed(1);
if (_amount != 0) items.push(item(textbook_name[idx], _price, _amount));
});
orders.push(order(
moment(Date.now()).format('LLL'),
items.join('\n'),
student.total,
student
));
});
return html(orders.join('\n'));
}
const item = (name, price, qty) => `
<li class="list-group-item h3">
<span><i class="fa fa-check-square-o" aria-hidden="true"></i> ${name}</span>
<span class="float-right text-monospace">${price} <small class="text-muted">CNY x ${qty}</small></span>
</li>`
const order = (time, items, total, student) => `
<div class="container pagebreak mt-3">
<div class="card">
<div class="card-body">
<h5 class="card-title display-3">
<span>订单</span>
<span class="float-right badge badge-primary">${student.name}</span>
</h5>
</div>
<ul class="list-group list-group-flush">
${items}
<li class="list-group-item h3 bg-info text-light text-center">
<i class="fa fa-shopping-cart" aria-hidden="true"></i>
总计 <span class="text-monospace">${total} <small>CNY</small></span>
</li>
</ul>
<div class="card-body text-center">
生成于${time}<br>请核对订单信息,谢谢!
</div>
</div>
<p class="text-muted text-center text-monospace mt-3">Made with <i class="fa fa-heart" aria-hidden="true"></i> by Alex Chi</p>
</div>`
const html = data => `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Order</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>.pagebreak { page-break-before: always; } body { font-family: serif !important; }</style>
</head>
<body>
${data}
</body>
</html>`
1 2 3 4 5 6
中国近现代史纲要(2018版) 新视野大学英语渎写教程(第2册) 新世纪大学英语视听说教程(第3册) 高起点大学英语读写教程(1) 高起点大学英语读写教程(2) 讲演的艺术 新编跨文化交际英语教程 学术英语交流——写作与演讲 实战口译(学生用书) 创桥标准商务英语教程(第二版)学生用书 学术英语写作(原版教材) 微积分(下) 高等数学解题方法与解答 高等数学证明题解题方法与技巧 高等数学试题分析与解答 大学物理教程(上) 大学物理教程(下) 大学物理精讲与典型难题详解(上) 大学物理精讲与典型难题详解(下) 大学物理精析精讲精练 大学物理解题方法与技巧(第三版) 大学物理实验(1) 大学物理实验报告纸(配套) 电路基础 电路基础试题集与考研指导 电路基础教学指导书 电路基础 (影印版) (英文教材) 电路实验教程 数据结构:思想与实现(第2版) 数据结构:题解与拓展
姓名 地址 总价 26 39 38.3 35.7 35.7 51 34 33.8 33.1 0 0 32.3 27.2 40.8 22.1 34 34 32.3 40.8 30.6 30.6 9 3 52.7 50.9 33.8 83.3 27 44.2 32.3
迟先生 460.2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4471.9 总价 260 39 38.3 71.4 142.8 0 0 0 33.1 0 0 32.3 54.4 40.8 22.1 306 306 226.1 285.6 244.8 214.2 72 36 316.2 305.4 135.2 249.9 243 442 355.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment