Browse Source

Made atomic and disk stored invoice numbers (UNTESTED)

master
Archivist 6 years ago
parent
commit
83b619536a
4 changed files with 43 additions and 5 deletions
  1. +2
    -2
      src/sales_backend/address.cr
  2. +9
    -0
      src/sales_backend/http/product.cr
  3. +29
    -1
      src/sales_backend/invoice.cr
  4. +3
    -2
      src/sales_backend/product.cr

+ 2
- 2
src/sales_backend/address.cr View File

@ -19,7 +19,7 @@ class Address
country: Country,
is_default: {type: Bool, default: false},
)
def == (other : Address)
self.to_json==other.to_json
def <=> (other : Address)
self.to_json<=>other.to_json
end
end

+ 9
- 0
src/sales_backend/http/product.cr View File

@ -0,0 +1,9 @@
require "kemal"
require "../*"
require "io"
require "file"
require "exception"
require "crypto/bcrypt/password"
require "uuid"
require "uuid/json"
require "../../config"

+ 29
- 1
src/sales_backend/invoice.cr View File

@ -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

+ 3
- 2
src/sales_backend/product.cr View File

@ -1,5 +1,6 @@
require "json"
require "uuid"
require "uuid/json"
class Product
JSON.mapping(
@ -8,6 +9,6 @@ class Product
name: {type: String, nilable: true},
description: {type: String, nilable: true},
pic_url: {type: String, nilable: true},
id: b">Int64,
id: o">UUID,
)
end

Loading…
Cancel
Save