Created
June 28, 2018 08:45
-
-
Save hieuhani/32d43814400aa8e8d585b69cb4ac5a30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AsyncStorage } from 'react-native' | |
import { OdooRPC } from '../node-odoorpc' | |
type QueryFirstParams = { | |
model: string, | |
id: number, | |
fields: [string], | |
} | |
type QueryParams = { | |
model: string, | |
fields: [string], | |
order?: [string], | |
domain?: [any], | |
context?: any, | |
offset?: number, | |
limit?: number, | |
} | |
type Pagination = { | |
offset: number, | |
limit: number, | |
context?: any, | |
} | |
const odooOptions = { | |
storage: AsyncStorage, | |
tokenKey: 'token', | |
dataKey: 'data', | |
} | |
let odooInst | |
function odoo() { | |
return odooInst | |
} | |
export function selectOdooDatabase(database: string) { | |
odooInst = new OdooRPC({ | |
domain: '10.0.3.2', | |
https: false, | |
port: 8069, | |
database, | |
useSaaS: false, | |
}, odooOptions) | |
} | |
export function findByIds({ | |
model, | |
ids, | |
fields, | |
}) { | |
return odoo().query({ | |
method: 'read', | |
model, | |
args: [ | |
ids, | |
fields, | |
], | |
}).then(({ data }) => Promise.resolve(data.result)) | |
} | |
export function findInIds({ | |
model, | |
ids, | |
fields, | |
}) { | |
return odoo().query({ | |
method: 'search_read', | |
model, | |
args: [ | |
[['id', 'in', ids]], | |
fields, | |
], | |
}).then(({ data }) => Promise.resolve(data.result)) | |
} | |
export function first({ | |
model, | |
id, | |
fields, | |
}: QueryFirstParams) { | |
return odoo().query({ | |
method: 'read', | |
model, | |
args: [ | |
[id], | |
fields, | |
], | |
}).then(({ data }) => { | |
if (data.error) { | |
throw new Error(data.error.message) | |
} | |
if (data.result[0]) { | |
return Promise.resolve(data.result[0]) | |
} | |
throw new Error(`${model} does not have ${id}`) | |
}) | |
} | |
export function query({ | |
model, | |
fields, | |
order, | |
domain, | |
context, | |
offset, | |
limit, | |
}: QueryParams) { | |
const params = { | |
method: 'search_read', | |
model, | |
fields, | |
limit: false, | |
} | |
if (order) { | |
params.order = order | |
} | |
if (domain) { | |
params.domain = domain | |
} | |
if (context) { | |
params.context = context | |
} | |
if (offset) { | |
params.offset = offset | |
} | |
if (limit) { | |
params.limit = limit | |
} | |
return odoo().query(params).then(({ data }) => Promise.resolve(data.result || [])) | |
} | |
export function getCategories() { | |
return query({ | |
model: 'pos.category', | |
fields: [ | |
'id', | |
'name', | |
'parent_id', | |
'child_id', | |
], | |
}) | |
} | |
export function getProducts(pricelistId: number) { | |
return query({ | |
method: 'search_read', | |
model: 'product.product', | |
fields: [ | |
'id', 'display_name', 'list_price', 'price', 'pos_categ_id', | |
'taxes_id', 'barcode', 'default_code', 'to_weight', 'uom_id', 'qty_available', | |
'description_sale', 'description', 'product_tmpl_id', 'tracking', 'image', | |
], | |
order: ['sequence', 'default_code', 'name'], | |
domain: [['sale_ok', '=', true], ['available_in_pos', '=', true]], | |
context: { pricelist: pricelistId, display_default_code: false }, | |
}) | |
} | |
export function getCustomers() { | |
return query({ | |
method: 'search_read', | |
model: 'res.partner', | |
fields: [ | |
'name', 'street', 'city', 'state_id', 'country_id', | |
'vat', 'phone', 'zip', 'mobile', 'email', 'barcode', | |
'write_date', 'property_account_position_id', | |
], | |
domain: [['customer', '=', true]], | |
}) | |
} | |
export function getUser(userId: number) { | |
return first({ | |
model: 'res.users', | |
id: userId, | |
fields: ['name', 'image', 'company_id'], | |
}).catch(() => { | |
throw new Error('Could not found the current user, please sign out and sign in again.') | |
}) | |
} | |
export function getPosSessions(userId: number) { | |
return query({ | |
model: 'pos.session', | |
fields: [ | |
'id', 'journal_ids', 'name', 'user_id', 'config_id', 'start_at', | |
'stop_at', 'sequence_number', 'login_number', | |
], | |
domain: [['state', '=', 'opened'], ['user_id', '=', userId]], | |
}) | |
} | |
export function getPosSession(userId: number) { | |
return getPosSessions(userId).then((sessions) => { | |
if (sessions[0]) { | |
return Promise.resolve(sessions[0]) | |
} | |
throw new Error('There is no active session') | |
}) | |
} | |
export function getPosConfigs(configId: number) { | |
return query({ | |
method: 'search_read', | |
model: 'pos.config', | |
fields: [], | |
domain: [['id', '=', configId]], | |
}) | |
} | |
export function getPosConfig(configId: number) { | |
return getPosConfigs(configId).then((configs) => { | |
if (configs[0]) { | |
return Promise.resolve(configs[0]) | |
} | |
throw new Error('POS configuration is not configured properly.') | |
}) | |
} | |
export function getPriceList(priceListId: number) { | |
return first({ | |
model: 'product.pricelist', | |
id: priceListId, | |
fields: ['currency_id'], | |
}).catch(() => { | |
throw new Error('POS price list is not configured properly.') | |
}) | |
} | |
export function getCurrency(currencyId: number) { | |
return first({ | |
model: 'res.currency', | |
id: currencyId, | |
fields: ['name', 'symbol', 'position', 'rounding'], | |
}).catch(() => { | |
throw new Error('POS Currency is not configured properly.') | |
}) | |
} | |
export function getDecimalPrecision() { | |
return query({ | |
method: 'search_read', | |
model: 'decimal.precision', | |
fields: [ | |
'name', | |
'digits', | |
], | |
}) | |
} | |
export function getProductUOM() { | |
return query({ | |
method: 'search_read', | |
model: 'product.uom', | |
fields: [], | |
context: { active_test: false }, | |
}) | |
} | |
export function getPOSOrders(params: Pagination) { | |
return query({ | |
method: 'search_read', | |
model: 'pos.order', | |
fields: [ | |
'id', | |
'name', | |
'pos_reference', | |
'partner_id', | |
'date_order', | |
'user_id', | |
'amount_total', | |
'company_id', | |
'state', | |
'session_id', | |
], | |
...params, | |
}) | |
} | |
export function getOrder(id: number) { | |
return first({ | |
model: 'pos.order', | |
id, | |
fields: [ | |
'sale_journal', 'pos_reference', 'account_move', 'date_order', | |
'location_id', 'user_id', 'statement_ids', 'partner_id', 'company_id', | |
'note', 'state', 'pricelist_id', 'amount_tax', 'fiscal_position_id', | |
'amount_total', 'name', 'lines', 'session_id', 'picking_id', | |
'display_name', '__last_update', | |
], | |
}) | |
} | |
export function getOrderLine(ids: [number]) { | |
return findByIds({ | |
model: 'pos.order.line', | |
ids, | |
fields: [ | |
'product_id', | |
'qty', | |
'price_unit', | |
'discount', | |
'tax_ids_after_fiscal_position', | |
'tax_ids', | |
'price_subtotal', | |
'price_subtotal_incl', | |
], | |
}) | |
} | |
export function getAccountBankStatements(posSessionId: number) { | |
return query({ | |
method: 'search_read', | |
model: 'account.bank.statement', | |
fields: [ | |
'account_id', 'currency_id', 'journal_id', | |
'state', 'name', 'user_id', 'pos_session_id', | |
], | |
domain: [['state', '=', 'open'], ['pos_session_id', '=', posSessionId]], | |
}) | |
} | |
export function getAccountJournals(journalIds: number[]) { | |
return query({ | |
method: 'search_read', | |
model: 'account.journal', | |
fields: ['type', 'sequence'], | |
domain: [['id', 'in', journalIds]], | |
}) | |
} | |
export function getBankStatementLine(ids: [number]) { | |
return findByIds({ | |
model: 'account.bank.statement.line', | |
ids, | |
fields: ['journal_id', 'statement_id', 'amount'], | |
}) | |
} | |
export function getAccountTaxes() { | |
return query({ | |
method: 'search_read', | |
model: 'account.tax', | |
fields: ['name', 'amount', 'price_include', 'include_base_amount', 'amount_type', 'children_tax_ids'], | |
}) | |
} | |
export function createModel(model: string, args: []) { | |
return odoo().query({ | |
method: 'create_from_ui', | |
model, | |
args, | |
}).then(({ data }) => { | |
if (data.error) { | |
return Promise.reject(data.error) | |
} | |
return Promise.resolve(data.result) | |
}) | |
} | |
export function create(model: string, args: []) { | |
return odoo().query({ | |
method: 'create', | |
model, | |
args, | |
}).then(({ data }) => { | |
if (data.error) { | |
return Promise.reject(data.error) | |
} | |
return Promise.resolve(data.result) | |
}) | |
} | |
export function defaultGet(model: string, args: []) { | |
return odoo().query({ | |
method: 'default_get', | |
model, | |
args: [args], | |
}).then(({ data }) => { | |
if (data.error) { | |
return Promise.reject(data.error) | |
} | |
return Promise.resolve(data.result) | |
}) | |
} | |
export function callFunction(model: string, method: string, id: number) { | |
return odoo().query({ | |
method, | |
model, | |
args: [[id]], | |
}).then(({ data }) => { | |
if (data.error) { | |
return Promise.reject(data.error) | |
} | |
return Promise.resolve(data.result) | |
}) | |
} | |
export function write(model: string, args: [any]) { | |
return odoo().query({ | |
method: 'write', | |
model, | |
args, | |
}).then(({ data }) => { | |
if (data.error) { | |
return Promise.reject(data.error) | |
} | |
return Promise.resolve(data.result) | |
}) | |
} | |
export function getCompany(companyId: number) { | |
return first({ | |
model: 'res.company', | |
id: companyId, | |
fields: [ | |
'currency_id', 'email', 'website', 'company_registry', | |
'vat', 'name', 'phone', 'partner_id', | |
'country_id', 'tax_calculation_rounding_method', | |
], | |
}) | |
} | |
export default odoo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment