Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
balajahe committed Feb 1, 2020
1 parent 820ee1d commit 67c34f9
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 424 deletions.
26 changes: 22 additions & 4 deletions FuncDB.deno/core/DBCore.ts
Expand Up @@ -9,7 +9,7 @@ export class DBCore implements IDBCore {
private cache_top = new Map<string, Document>()
private cache_reduce = new Map<string, Result>()

private constructor(dbpath) {
constructor(dbpath) {
this.dbpath = dbpath
}

Expand All @@ -19,7 +19,7 @@ export class DBCore implements IDBCore {
return db
}

private init(no_cache: boolean = false) {
protected init(no_cache: boolean = false) {
console.log('\ndatabase initialization started...')

if (!no_cache) {
Expand Down Expand Up @@ -113,6 +113,24 @@ export class DBCore implements IDBCore {
}
}

reduce_top(
filter: (result: Result, doc: Document) => boolean,
reducer: (result: Result, doc: Document) => void,
result: Result,
): Result {
console.log('\nreduce_top() started...')
for (let doc of this.cache_top.values()) {
try {
if(filter(result, doc)) {
reducer(result, doc)
}
} catch(e) {
console.log(JSON.stringify(doc, null, '\t') + '\n' + e + '\n' + e.stack)
}
}
return result
}

get(id: string, no_scan: boolean = false): Document | undefined {
const cached = this.cache_doc.get(id)
if (cached !== undefined) {
Expand Down Expand Up @@ -161,7 +179,7 @@ export class DBCore implements IDBCore {

add_mut(doc: Document): [boolean, string?] {
try {
if (doc.id === undefined) doc.id = doc.key + '^' + Date.now()
if (doc.id === undefined || doc.id === null || doc.id === '') doc.id = doc.key + '^' + Date.now()
attach_doc_class(doc)
const [ok, msg] = doc.class.before_add(doc, this)
if (ok) {
Expand All @@ -171,7 +189,7 @@ export class DBCore implements IDBCore {
}
return [ok, msg]
} catch(e) {
console.log(JSON.stringify(doc, null, '\t') + '\n' + e)
console.log(JSON.stringify(doc, null, '\t') + '\n' + e + '\n' + e.stack)
console.log('Process is aborted !')
Deno.exit()
}
Expand Down
5 changes: 5 additions & 0 deletions FuncDB.deno/core/DBMeta.ts
Expand Up @@ -28,6 +28,11 @@ export interface IDBCore {
result: Result,
no_cache?: boolean,
): Result;
reduce_top(
filter: (result: Result, doc: Document) => boolean,
reducer: (result: Result, doc: Document) => void,
result: Result,
): Result;
get(id: string, no_scan?: boolean): Document | undefined
get_top(key: string, no_scan?: boolean): Document | undefined
add_mut(doc: Document): [boolean, string?]
Expand Down
43 changes: 43 additions & 0 deletions FuncDB.deno/core/ERPCore.ts
@@ -0,0 +1,43 @@
import { Balance, IERPCore } from './ERPMeta.ts'
import { DBCore } from './DBCore.ts'

export class ERPCore extends DBCore implements IERPCore {
static open(dbpath: string, no_cache: boolean = false): ERPCore {
const db = new ERPCore(dbpath)
db.init()
return db
}

bal_key_from_ids(ids: string[]): string {
let key = 'bal'
for (const id of ids) {
key += '|' + this.key_from_id(id)
}
return key
}

get_bal_by_key(key: string): Balance {
let bal = this.get_top(key, true)
if (bal === undefined) {
bal = {
type: 'bal',
key: key,
id: undefined,
qty: 0,
val: 0,
iqty: 0,
ival: 0,
oqty: 0,
oval: 0,
}
} else {
bal = Object.assign({}, bal)
}
return bal
}

get_bal(ids: string[]): Balance {
const key = this.bal_key_from_ids(ids)
return this.get_bal_by_key(key)
}
}
21 changes: 21 additions & 0 deletions FuncDB.deno/core/ERPMeta.ts
@@ -0,0 +1,21 @@
import { Document, Result, DocClass, IDBCore } from './DBMeta.ts'

export { Document, Result, DocClass }

export interface Balance {
type?: string,
key: string,
id?: string,
qty: number, // остаток в наличии
val: number,
iqty: number, // ожидаемый приход
ival: number,
oqty: number, // ожидаемый расход
oval: number,
}

export interface IERPCore extends IDBCore {
get_bal(ids: string[]): Balance
get_bal_by_key(key: string): Balance
bal_key_from_ids(ids: string[]): string
}
18 changes: 11 additions & 7 deletions FuncDB.deno/doc_classes/.get_doc_class.ts
@@ -1,20 +1,24 @@
import { DocClass } from '../core/DBMeta.ts'
import { DocClass } from '../core/ERPMeta.ts'

import ref from './ref.ts'
import bal from './bal.ts'
import purch from './purch.ts'
import sale from './sale.ts'
import transfer from './transfer.ts'
import post_purch from './post_purch.ts'
import post_sale from './post_sale.ts'
import post_transfer from './post_transfer.ts'
import open_purch from './open_purch.ts'
import open_sale from './open_sale.ts'

export function get_doc_class(type: string) { // : DocClass { как указать что возвращается не инстанс, а сам класс ?
switch (type) {
case 'person': return ref
case 'nomen': return ref
case 'stock': return ref
case 'bal': return bal
case 'purch': return purch
case 'sale': return sale
case 'transfer': return transfer
case 'post.purch': return post_purch
case 'post.sale': return post_sale
case 'post.transfer': return post_transfer
case 'open.purch': return open_purch
case 'open.sale': return open_sale
default: throw `Error: document type "${type}" is not registered in doc_classes !`
}
}
2 changes: 1 addition & 1 deletion FuncDB.deno/doc_classes/bal.ts
@@ -1,4 +1,4 @@
import { Document, DocClass, IDBCore } from '../core/DBMeta.ts'
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class extends DocClass {
static cache_top = true
Expand Down
12 changes: 12 additions & 0 deletions FuncDB.deno/doc_classes/open_purch.ts
@@ -0,0 +1,12 @@
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class OpenPurch extends DocClass {
static after_add(doc: Document, db: IERPCore): void {
doc.lines.forEach(line => {
const bal = db.get_bal([line.nomen, doc.stock])
bal.iqty += line.qty
bal.ival += line.qty * line.price
db.add_mut(bal)
})
}
}
14 changes: 14 additions & 0 deletions FuncDB.deno/doc_classes/open_sale.ts
@@ -0,0 +1,14 @@
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class OpenSale extends DocClass {
static after_add(doc: Document, db: IERPCore): void {
doc.lines.forEach(line => {
const bal = db.get_bal([line.nomen, doc.stock])
line.from = bal.id
line.cost = (bal.val + bal.ival) / (bal.qty + bal.iqty) // себестоимость в момент списания с учетом ожидаемых приходов
bal.oqty -= line.qty
bal.oval -= line.qty * line.cost
db.add_mut(bal)
})
}
}
12 changes: 12 additions & 0 deletions FuncDB.deno/doc_classes/post_purch.ts
@@ -0,0 +1,12 @@
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class PostPurch extends DocClass {
static after_add(doc: Document, db: IERPCore): void {
doc.lines.forEach(line => {
const bal = db.get_bal([line.nomen, doc.stock])
bal.qty += line.qty
bal.val += line.qty * line.price
db.add_mut(bal)
})
}
}
26 changes: 26 additions & 0 deletions FuncDB.deno/doc_classes/post_sale.ts
@@ -0,0 +1,26 @@
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class PostSale extends DocClass {
static before_add(doc: Document, db: IERPCore): [boolean, string?] {
let err = ''
doc.lines.forEach(line => {
const key = [line.nomen, doc.stock]
const bal = db.get_bal(key)
if (bal.qty < line.qty) {
err += '\n"' + key + '": requested ' + line.qty + ' but balance is only ' + bal.qty
}
})
return err !== '' ? [false, err] : [true,]
}

static after_add(doc: Document, db: IERPCore): void {
doc.lines.forEach(line => {
const bal = db.get_bal([line.nomen, doc.stock])
line.from = bal.id
line.cost = (bal.val + bal.ival) / (bal.qty + bal.iqty) // себестоимость в момент списания с учетом ожидаемых приходов
bal.qty -= line.qty
bal.val -= line.qty * line.cost
db.add_mut(bal)
})
}
}
31 changes: 31 additions & 0 deletions FuncDB.deno/doc_classes/post_transfer.ts
@@ -0,0 +1,31 @@
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class PostTransfer extends DocClass {
static before_add(doc: Document, db: IERPCore): [boolean, string?] {
let err = ''
doc.lines.forEach(line => {
const key = [line.nomen, doc.stock1]
const bal = db.get_bal(key)
if (bal.qty < line.qty) {
err += '\n"' + key + '": requested ' + line.qty + ' but balance is only ' + bal.qty
}
})
return err !== '' ? [false, err] : [true,]
}

static after_add(doc: Document, db: IERPCore): void {
doc.lines.forEach(line => {
let bal = db.get_bal([line.nomen, doc.stock1])
line.from = bal.id
line.cost = (bal.val + bal.ival) / (bal.qty + bal.iqty) // себестоимость в момент списания с учетом ожидаемых приходов
bal.qty -= line.qty
bal.val -= line.qty * line.cost
db.add_mut(bal)

bal = db.get_bal([line.nomen, doc.stock2])
bal.qty += line.qty
bal.val += line.qty * line.cost
db.add_mut(bal)
})
}
}
20 changes: 0 additions & 20 deletions FuncDB.deno/doc_classes/purch.ts

This file was deleted.

2 changes: 1 addition & 1 deletion FuncDB.deno/doc_classes/ref.ts
@@ -1,4 +1,4 @@
import { Document, DocClass, IDBCore } from '../core/DBMeta.ts'
import { Document, DocClass, IERPCore } from '../core/ERPMeta.ts'

export default class extends DocClass {
static cache_doc = true
Expand Down
37 changes: 0 additions & 37 deletions FuncDB.deno/doc_classes/sale.ts

This file was deleted.

49 changes: 0 additions & 49 deletions FuncDB.deno/doc_classes/transfer.ts

This file was deleted.

0 comments on commit 67c34f9

Please sign in to comment.