|
|
@ -1,11 +1,39 @@ |
|
|
|
require "json" |
|
|
|
require "./invoice_line" |
|
|
|
require "./address" |
|
|
|
require "../config" |
|
|
|
require "atomic" |
|
|
|
require "file" |
|
|
|
|
|
|
|
class Invoice |
|
|
|
@@last_invoice : Atomic(Int64) |
|
|
|
|
|
|
|
def initialize |
|
|
|
@@last_invoice = Atomic(Int64).new(1) |
|
|
|
if(File.exists? Statics.data_path+"invoices_meta.json") |
|
|
|
@@last_invoice.set Int64.from_json File.read Statics.data_path+"invoices_meta.json" |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
def get_new_id |
|
|
|
ret : Int64 =-1 |
|
|
|
begin |
|
|
|
metaf = File.open(Statics.data_path+"invoices_meta.json") |
|
|
|
metaf.flock_exclusive do |
|
|
|
metaf.truncate |
|
|
|
ret = @@last_invoice.add(1) |
|
|
|
metaf.write @@last_invoice.get.to_json |
|
|
|
end |
|
|
|
ensure |
|
|
|
metaf.close |
|
|
|
end |
|
|
|
return ret |
|
|
|
end |
|
|
|
|
|
|
|
JSON.mapping( |
|
|
|
items: Array(InvoiceLine), |
|
|
|
invoicing_address: Address, |
|
|
|
delivery_address: Address |
|
|
|
delivery_address: Address, |
|
|
|
id: Int64 |
|
|
|
) |
|
|
|
end |