-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary.sc
52 lines (42 loc) · 1.68 KB
/
summary.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import $ivy.`com.github.zhongl::captabula:0.0.7`, captabula._, tabula._, dsl._, xssf._
import java.io.File
import java.io.FilenameFilter
import java.nio.file.Files
import java.nio.file.Path
import cats.Id
import technology.tabula.Page
@main
def main(dir: os.Path = os.pwd) = {
sys.props.addOne("org.slf4j.simpleLogger.defaultLogLevel" -> "error")
val invoices = list(dir, ".pdf").flatMap { f =>
try {
val i = f.as[Id, Page].read[Invoice]
rename(f.toPath(), f"${i.`发票代码`}-${i.`发票号码`}-${i.`价税合计`}%.2f.pdf")
println(s"$f -> $i")
List(i)
} catch {
case x: Throwable =>
println(s"$f -> ${x.getMessage()}")
List.empty[Invoice]
}
}
if (invoices.nonEmpty) {
val sum = invoices.map(_.`价税合计`).sum
(dir / f"summary-$sum%.2f.xlsx").toNIO.as[Excel].write(invoices)
}
}
case class Invoice(`发票代码`: String, `发票号码`: String, `开票日期`: String, `价税合计`: Double) extends Prehead
implicit val captureInvoice: Page => Invoice = (for {
basic <- rect(0, 400, 200, 85)
total <- rect(275, 400, 200, 25)
regex"(\d{12})$code\D+(\d{8})$no\D+(\d{4})$year\D*(\d{2})$month\D*(\d{2})$day" = basic.replaceAll("\\s", "")
regex"(\d+\.\d{2})$pwt" = total
} yield Invoice(code, no, s"$year-$month-$day", pwt.toDouble)).run
def list(dir: os.Path, suffix: String): List[File] = {
def filter = new FilenameFilter { def accept(dir: File, name: String) = name.endsWith(suffix) }
val f = dir.toIO
if (f.isFile()) List.empty else f.listFiles(filter).toList
}
def rename(p: Path, name: String): Unit = {
Files.move(p, p.resolveSibling(name))
}