tecnospeed-nfe
Version:
Emit NFe through TecnoSpeed API
161 lines (134 loc) • 3.66 kB
JavaScript
'use strict'
const _ = require('lodash')
const Item = require('./item')
const TX2 = require('./tx2')
class Order extends TX2 {
constructor(settings = { items: [], discount: 0 }) {
super()
this.settings = settings
}
/**
* @returns {Order}
* @param {Item} item
*/
addItem(item) {
this.settings.items.push(item)
return this
}
/**
* @returns {Array<Item>}
*/
getItems() {
return this.settings.items
}
/**
* @returns {Order}
* @param {Item} item
*/
removeItem(item) {
// TODO: Implementar
return this
}
/**
* @returns {Order}
* @param {Number} discount
*/
setDiscount(discount) {
this.settings.discount = discount
return this
}
/**
* @returns {Number}
*/
getDiscount() {
return this.settings.discount
}
/**
* @returns {Order}
* @param {Object} accounting
*/
setAccounting(accounting) {
this.settings.accounting = accounting
return this
}
getPercentage(value) {
const totalCost = this.getCost()
const givenPercentage = Number.parseFloat(value)
return ((givenPercentage / 100) * totalCost)
}
/**
* @returns {Object}
*/
getAccounting() {
return this.settings.accounting
}
getAccountingParam(param, defaultValue) {
const value = _.get(this.getAccounting(), param) || this.getAccounting()[param] || defaultValue
return value
}
getAccountingNumber(param, defaultValue = '0.00') {
return Number.parseFloat(this.getAccountingParam(param, defaultValue)).toFixed(2)
}
getAccountingPercentage(param, defaultValue = '0.00') {
return Number.parseFloat(this.getPercentage(this.getAccountingParam(param, defaultValue))).toFixed(2)
}
/**
* @returns {String}
*/
getCost() {
const costs = _.map(this.getItems(), item => item.getCost())
return _.sum(costs)
}
/**
* @returns {String}
*/
getTotal() {
return Number.parseFloat(this.getCost()).toFixed(2)
}
/**
* @returns {String}
*/
toTX2() {
const tx2 = []
for (const item of this.getItems()) {
tx2.push(item.toTX2())
}
// Meio de pagamento e valor
tx2.push('INCLUIRPARTE=YA')
tx2.push('tPag_YA02=01')
tx2.push('vPag_YA03=' + this.getTotal())
tx2.push('SALVARPARTE=YA')
// Delivery e Custo
tx2.push('modFrete_X02=9')
tx2.push('vFrete_W08=0.00')
// Preço: ICMS
tx2.push('vBC_W03=' + this.getAccountingPercentage('icms.base', "60.00"))
tx2.push('vICMS_W04=' + this.getAccountingPercentage('icms.total', "12.00"))
tx2.push('vICMSDeson_W04a=0.00')
tx2.push('vFCPUFDest_W04c=0.00')
tx2.push('vICMSUFDest_W04e=' + this.getAccountingPercentage('icms.dest', "12.00"))
tx2.push('vICMSUFRemet_W04g=' + this.getAccountingPercentage('icms.remet', "12.00"))
tx2.push('vBCST_W05=0.00')
tx2.push('vST_W06=0.00')
// Preço: Fundo de Combate a Pobreza
tx2.push('vFCP_W04h=0.00')
tx2.push('vFCPST_W06a=0.00')
tx2.push('vFCPSTRet_W06b=0.00')
// Preço: Valor total bruto
tx2.push('vProd_W07=' + this.getTotal())
tx2.push('vNF_W16=' + this.getTotal())
// Preço: Seguro
tx2.push('vSeg_W09=0.00')
tx2.push('vDesc_W10=0.00')
// Preço: Outros
tx2.push('vII_W11=0.00')
tx2.push('vIPI_W12=0.00')
tx2.push('vIPIDevol_W12a=0.00')
tx2.push('vPIS_W13=' + this.getAccountingPercentage('pis', "0.65"))
tx2.push('vCOFINS_W14=' + this.getAccountingPercentage('cofins', "3.00"))
tx2.push('vOutro_W15=0.00')
tx2.push('vTotTrib_W16a=' + this.getAccountingPercentage('tribute_total', "13.82"))
return tx2.join('\n')
}
}
module.exports = Order