import { ItemVenda } from './itemVenda'; import { sumBy, get } from 'lodash'; import { Entrega } from './entrega'; export class Total { vbc: number = 0; vbcst: number = 0; vcofins: number = 0; vicms: number = 0; vst: number = 0; vipi: number = 0; vpis: number = 0; vdesc: number = 0; vfrete: number = 0; vseg: number = 0; voutro: number = 0; vtottrib: number = 0; vfcpufdest: number = 0; vicmsdeson: number = 0; vicmsufdest: number = 0; vicmsufremet: number = 0; vii: number = 0; vprod: number = 0; vnf: number = 0; vcomissao: number = 0; vqtd: number = 0; total_cashback: number = 0; constructor(json ? : any) { json = json || {}; this.vbc = json.vbc || this.vbc; this.vbcst = json.vbcst || this.vbcst; this.vcofins = json.vcofins || this.vcofins; this.vicms = json.vicms || this.vicms; this.vst = json.vst || this.vst; this.vipi = json.vipi || this.vipi; this.vpis = json.vpis || this.vpis; this.vdesc = json.vdesc || this.vdesc; this.vfrete = json.vfrete || this.vfrete; this.vseg = json.vseg || this.vseg; this.voutro = json.voutro || this.voutro; this.vtottrib = json.vtottrib || this.vtottrib; this.vfcpufdest = json.vfcpufdest || this.vfcpufdest; this.vicmsdeson = json.vicmsdeson || this.vicmsdeson; this.vicmsufdest = json.vicmsufdest || this.vicmsufdest; this.vicmsufremet = json.vicmsufremet || this.vicmsufremet; this.vii = json.vii || this.vii; this.vprod = json.vprod || this.vprod; this.vcomissao = json.vcomissao || this.vcomissao; this.vnf = json.vnf || this.vnf; this.vqtd = json.vqtd || this.vqtd; this.total_cashback = json.total_cashback || this.total_cashback; } public zerarTotal(): Total { Object.getOwnPropertyNames(this).map(key => this[key] = 0.0) return this; } public static totalize(itens: Array < ItemVenda >, sem_comissao: boolean, entrega : Entrega): Total { let total = new Total(); let getSum = function (property) { return sumBy(itens, function (o) { //em cada item, pega o valor de 'property' e o converte para float; //se 'property' não for encontrada no item, retorna 'defaultValue'. if (o['compoe_total'] === false) return 0.0; return parseFloat(parseFloat(get(o, property, 0)).toFixed(2)) || 0.0; }); }; total.vqtd = getSum('quantidade'); total.vbc = getSum('imposto.icms.vbc'); total.vbcst = getSum('imposto.icms.vbcst'); total.vcofins = getSum('imposto.cofins.vcofins'); total.vicms = getSum('imposto.icms.vicms'); total.vst = getSum('imposto.icms.vicmsst'); total.vipi = getSum('imposto.ipi.vipi'); total.vpis = getSum('imposto.pis.vpis'); total.vdesc = getSum('desconto'); total.vfrete = getSum('frete'); total.vseg = getSum('seguro'); total.voutro = getSum('outro'); total.vtottrib = getSum('imposto.total_tributos'); total.vfcpufdest = 0.0; total.vicmsdeson = getSum('imposto.icms.vicmsdeson'); total.vicmsufdest = 0.0; total.vicmsufremet = 0.0; total.vii = 0.0; total.vprod = getSum('valor_total'); total.vcomissao = sem_comissao ? 0.0 : getSum('comissao'); total.vnf = total.vprod + (get(entrega, 'cobrada') && entrega.valor > 0 ? entrega.valor : total.vfrete) + total.voutro + total.vseg - total.vdesc + total.vcomissao; return total; } }