123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- function widget_balance(widget) {
- "use strict";
- var model = openerp;
- var Qweb = openerp.web.qweb;
- widget.WidgetBalance = widget.Base.extend({
- template: 'WidgetBalance',
- ContentSale: [],
- ContentPurchase: [],
- ContentExpense: [],
- init: function (parent) {
- this._super(parent, {
- width: 12,
- height: 5
- });
- },
- start: function () {
- var self = this;
- self.fetchInitial();
- },
- fetchInitial: function(){
- var self = this;
- self.fetchDataSQL().then(function (DataSQL) {
- return DataSQL;
- }).then(function(DataSQL) {
- self.ResCompany = DataSQL.company;
- self.PosOrder = DataSQL.orders;
- self.AccountInvoice = DataSQL.invoices;
- self.HrPayslip = DataSQL.payslips;
- return self.showThisMonth();
- });
- },
- fetchDataSQL: function() {
- var self = this;
- var data = $.get('/dashboard-Balance');
- return data;
- },
- getContentByDate:function(date, content) {
- var self = this;
- return _.flatten(_.filter(content,function (inv) {
- return moment(inv.date).format('YYYY-MM-DD') === date;
- }));
- },
- showThisMonth: function () {
- var self = this;
- var ContentSale = [];
- var ContentPurchase = [];
- var ContentExpense = [];
- /*
- ==============
- VENTAS
- ==============
- */
- _.each(self.PosOrder,function(item){
- var utc = moment.utc(item.date,'YYYY-MM-DD h:mm:ss A');
- ContentSale.push({
- date: moment(utc._d).format('YYYY-MM-DD'),
- amount: item.amount,
- });
- });
- _.each(self.AccountInvoice,function(item){
- if(item.type == "out_invoice" && item.origin.match(/SO/) != null){
- ContentSale.push({
- date: item.date,
- amount: item.amount,
- });
- }
- if(item.type == "out_refund"){
- ContentSale.push({
- date: item.date,
- amount: item.amount * -1,
- });
- }
- });
- /*
- ==============
- COMPRAS
- ==============
- */
- _.each(self.AccountInvoice,function(item){
- var origin = item.origin;
- if(item.type == "in_invoice" && item.origin != null){
- if(origin.match(/PO/) != null){
- ContentPurchase.push({
- date: item.date,
- amount: item.amount,
- });
- }
- }
- });
- /*
- ==============
- GASTOS
- ==============
- */
- _.each(self.AccountInvoice,function(item){
- var origin = item.origin;
- if(item.type == "in_invoice"){
- if(origin == null){
- ContentExpense.push({
- date: item.date,
- amount: item.amount,
- });
- }else{
- if(origin != null){
- if(origin.match(/PO/) == null){
- ContentExpense.push({
- date: item.date,
- amount: item.amount,
- });
- }
- }
- }
- }
- });
- /*
- =====================
- RECURSOS HUMANOS
- =====================
- */
- var net = _.reduce(_.map(self.HrPayslip,function(item) {
- ContentExpense.push({
- date: item.date,
- amount: item.net,
- });
- return item.net;
- }),function(memo, num) {
- return memo + num;
- },0);
- var ipsc = _.reduce(_.map(self.HrPayslip,function(item) {
- if(item.code == 'IPSC' && item.ipsc != null){
- ContentExpense.push({
- date: item.date,
- amount: item.ipsc,
- });
- return item.ipsc;
- }else{
- return 0;
- }
- }),function(memo, num) {
- return memo + num;
- },0);
- var ipse = _.reduce(_.map(self.HrPayslip,function(item) {
- if(item.code == 'IPSE' && item.ipse != null){
- ContentExpense.push({
- date: item.date,
- amount: item.ipse * -1,
- });
- return item.ipse * -1;
- }else{
- return 0;
- }
- }),function(memo, num) {
- return memo + num;
- },0);
- self.ContentSale = ContentSale;
- self.ContentPurchase = ContentPurchase;
- self.ContentExpense = ContentExpense;
- self.BuildChartMonth();
- },
- /*
- =====================================================
- BUILD CHART - MONTH
- =====================================================
- */
- BuildChartMonth: function(){
- var self = this;
- var data = [];
- var CurrencyBase = self.ResCompany[0].currency_id;
- var label = [];
- var bodySale = [];
- var bodyPurchase = [];
- var bodyExpense = [];
- var name = '.month-dashboard-chart';
- var date_start = moment().format('YYYY-MM-01');
- var date_stop = moment().add(1,'months').format('YYYY-MM-01');
- var date = date_start;
- var total, i;
- /*
- ===================
- VENTAS
- ===================
- */
- for (i = 0; i < 32; i++) {
- total = 0;
- if(i > 0){
- date = moment(date).add(1,'days').format('YYYY-MM-DD');
- }
- if(date == date_stop){
- break;
- }
- data = self.getContentByDate(date, self.ContentSale);
- if(data.length > 0){
- total = _.reduce(_.map(data,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- }
- label.push(moment(date).format('DD'));
- bodySale.push(total);
- }
- /*
- ===================
- COMPRAS
- ===================
- */
- date = date_start;
- for (i = 0; i < 32; i++) {
- total = 0;
- if(i > 0){
- date = moment(date).add(1,'days').format('YYYY-MM-DD');
- }
- if(date == date_stop){
- break;
- }
- data = self.getContentByDate(date, self.ContentPurchase);
- if(data.length > 0){
- total = _.reduce(_.map(data,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- }
- bodyPurchase.push(total);
- }
- /*
- ===================
- GASTOS
- ===================
- */
- date = date_start;
- for (i = 0; i < 32; i++) {
- total = 0;
- if(i > 0){
- date = moment(date).add(1,'days').format('YYYY-MM-DD');
- }
- if(date == date_stop){
- break;
- }
- data = self.getContentByDate(date, self.ContentExpense);
- if(data.length > 0){
- total = _.reduce(_.map(data,function(item) {
- return item.amount;
- }),function(memo, num) {
- return memo + num;
- },0);
- }
- bodyExpense.push(total);
- }
- var chart = new widget.DashboardChartWidget(self);
- chart.BuildBalanceLineChart(name,label,bodySale,bodyPurchase,bodyExpense,CurrencyBase);
- },
- });
- }
|