|
@@ -0,0 +1,320 @@
|
|
|
+function widget_salesman_objective(widget) {
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var model = openerp;
|
|
|
+ var Qweb = openerp.web.qweb;
|
|
|
+
|
|
|
+ widget.WidgetSalesmanObjective = widget.Base.extend({
|
|
|
+ template: 'WidgetSalesmanObjective',
|
|
|
+ modules: ['point_of_sale'],
|
|
|
+
|
|
|
+ SaleObjectiveAmount: 0,
|
|
|
+ SaleMonth: 0,
|
|
|
+ content: [],
|
|
|
+
|
|
|
+ init: function (parent) {
|
|
|
+ this._super(parent, {
|
|
|
+ width: 12,
|
|
|
+ height: 6
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchUTC: function(){
|
|
|
+ var self = this;
|
|
|
+ var date = moment().format('YYYY-MM-01 00:00:00');
|
|
|
+ var utc = moment.utc(date,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ var first = moment('00:00:00','HH:mm:ss');
|
|
|
+ var second = moment(moment(utc._d,'HH:mm:ss'));
|
|
|
+ var data = moment.duration(first - second);
|
|
|
+ return data._data.hours;
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ self.fetchInitial();
|
|
|
+ },
|
|
|
+
|
|
|
+ checkModel : function(model){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.IrModuleModule,function(item){
|
|
|
+ return item.name === model;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInitial: function(){
|
|
|
+ var self = this;
|
|
|
+ self.fetchDataSQL().then(function (DataSQL) {
|
|
|
+ return DataSQL;
|
|
|
+ }).then(function(DataSQL) {
|
|
|
+ self.IrModuleModule = DataSQL.modules;
|
|
|
+ self.ResCompany = DataSQL.company;
|
|
|
+ self.DashboardObjective = DataSQL.objectives;
|
|
|
+ self.PosOrder = DataSQL.orders;
|
|
|
+ self.AccountInvoice = DataSQL.invoices;
|
|
|
+ return self.fetchMonth();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchDataSQL: function() {
|
|
|
+ var self = this;
|
|
|
+ var data = $.get('/dashboard-SalesmanObjective');
|
|
|
+ return data;
|
|
|
+ },
|
|
|
+
|
|
|
+ getPercentage: function(amount, objective){
|
|
|
+ var self = this;
|
|
|
+ var percentage = 0;
|
|
|
+ if(objective > 0){
|
|
|
+ percentage = (amount * 100)/objective;
|
|
|
+ }
|
|
|
+ return percentage;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchToday: function (type) {
|
|
|
+ var self = this;
|
|
|
+ var amount = 0;
|
|
|
+
|
|
|
+ if(type == "sale"){
|
|
|
+ var today = moment().format('YYYY-MM-DD');
|
|
|
+ var PosOrder = _.reduce(_.map(self.PosOrder,function(item) {
|
|
|
+ var utc = moment.utc(item.date,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ utc = moment(utc._d).format('YYYY-MM-DD');
|
|
|
+ if(utc == today){
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoice = _.reduce(_.map(self.AccountInvoice,function(item) {
|
|
|
+ if(item.date == moment().format('YYYY-MM-DD') && item.type == "out_invoice"){
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoiceRefund = _.reduce(_.map(self.AccountInvoice,function(item) {
|
|
|
+ if(item.date == moment().format('YYYY-MM-DD') && item.type == "out_refund"){
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ amount = PosOrder + AccountInvoice - AccountInvoiceRefund;
|
|
|
+ }
|
|
|
+ self.today = amount;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchWeek: function (type) {
|
|
|
+ var self = this;
|
|
|
+ var amount = 0;
|
|
|
+ var content = [];
|
|
|
+
|
|
|
+ if(type == "sale"){
|
|
|
+ var utc = self.fetchUTC();
|
|
|
+ var week_from = moment().weekday(0).format('YYYY-MM-DD 00:00:00');
|
|
|
+ week_from = moment(week_from).add(utc,'hours').format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ var week_to = moment(week_from).weekday(7).format('YYYY-MM-DD HH:mm:ss');
|
|
|
+
|
|
|
+ var PosOrder = _.reduce(_.map(self.PosOrder,function(item) {
|
|
|
+ if(item.date >= week_from && item.date < week_to){
|
|
|
+ var utc = moment.utc(item.date,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ content.push({
|
|
|
+ date: moment(utc._d).format('YYYY-MM-DD'),
|
|
|
+ amount: item.amount,
|
|
|
+ });
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoice = _.reduce(_.map(self.AccountInvoice,function(item) {
|
|
|
+ if(item.date >= moment().weekday(0).format('YYYY-MM-DD') && item.date < moment().weekday(7).format('YYYY-MM-DD') && item.type == "out_invoice"){
|
|
|
+ content.push({
|
|
|
+ date: item.date,
|
|
|
+ amount: item.amount,
|
|
|
+ });
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoiceRefund = _.reduce(_.map(self.AccountInvoiceRefund,function(item) {
|
|
|
+ if(item.date >= moment().weekday(0).format('YYYY-MM-DD') && item.date < moment().weekday(7).format('YYYY-MM-DD') && item.type == "out_refund"){
|
|
|
+ content.push({
|
|
|
+ date: item.date,
|
|
|
+ amount: item.amount * -1,
|
|
|
+ });
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ amount = PosOrder + AccountInvoice - AccountInvoiceRefund;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.week = amount;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchMonth: function () {
|
|
|
+ var self = this;
|
|
|
+ var content = [];
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+
|
|
|
+ var PosOrder = _.reduce(_.map(self.PosOrder,function(item) {
|
|
|
+ var utc = moment.utc(item.date,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ content.push({
|
|
|
+ date: moment(utc._d).format('YYYY-MM-DD'),
|
|
|
+ amount: item.amount,
|
|
|
+ });
|
|
|
+ utc = moment(utc._d).format('YYYY-MM-DD');
|
|
|
+ var value = 0;
|
|
|
+ if(moment(utc).format('YYYY-MM') == moment().format('YYYY-MM')){
|
|
|
+ value = item.amount;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoice = _.reduce(_.map(self.AccountInvoice,function(item) {
|
|
|
+ if(item.type == 'out_invoice'){
|
|
|
+ content.push({
|
|
|
+ date: item.date,
|
|
|
+ amount: item.amount,
|
|
|
+ });
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var AccountInvoiceRefund = _.reduce(_.map(self.AccountInvoice,function(item) {
|
|
|
+ if(item.type == 'out_refund'){
|
|
|
+ content.push({
|
|
|
+ date: item.date,
|
|
|
+ amount: item.amount * -1,
|
|
|
+ });
|
|
|
+ return item.amount;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+
|
|
|
+ var sale = PosOrder + AccountInvoice - AccountInvoiceRefund;
|
|
|
+ self.SaleMonth = sale;
|
|
|
+ self.content = content;
|
|
|
+ self.BuildWidget(sale);
|
|
|
+ },
|
|
|
+
|
|
|
+ BuildChartMonth: function(){
|
|
|
+ var self = this;
|
|
|
+ var data = [];
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+ var label = [];
|
|
|
+ var bodySale = [];
|
|
|
+ var name = '.salesman-objective-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;
|
|
|
+
|
|
|
+ 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.content);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ var ChartColor = '#43a047';
|
|
|
+ var chart = new widget.DashboardChartWidget2(self);
|
|
|
+ chart.BuildLineChart(name,label,bodySale,CurrencyBase,ChartColor,ChartColor);
|
|
|
+ },
|
|
|
+
|
|
|
+ getContentByDate:function(date, content) {
|
|
|
+ var self = this;
|
|
|
+ return _.flatten(_.filter(content,function (inv) {
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') === date;
|
|
|
+ }));
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ BuildWidget: function(sale){
|
|
|
+ var self = this;
|
|
|
+ var ObjectivePending;
|
|
|
+ var Objective;
|
|
|
+ var Today;
|
|
|
+ var Week;
|
|
|
+ var Month;
|
|
|
+ var ObjectivePercentage = 0;
|
|
|
+ var ChartColor;
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+ if(self.DashboardObjective.length > 0){
|
|
|
+ self.SaleObjectiveAmount = self.DashboardObjective[0].sale_objective;
|
|
|
+ ObjectivePercentage = self.getPercentage(sale, self.SaleObjectiveAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ self.fetchToday('sale');
|
|
|
+ self.fetchWeek('sale');
|
|
|
+ self.BuildChartMonth();
|
|
|
+ ChartColor = '#43a047';
|
|
|
+
|
|
|
+ $("#saleObjective").circliful({
|
|
|
+ animationStep: 10,
|
|
|
+ foregroundBorderWidth: 12,
|
|
|
+ backgroundBorderWidth: 1,
|
|
|
+ percent: ObjectivePercentage,
|
|
|
+ fontColor: ChartColor,
|
|
|
+ foregroundColor: ChartColor,
|
|
|
+ percentageTextSize: 35,
|
|
|
+ });
|
|
|
+
|
|
|
+ ObjectivePending = accounting.formatMoney(self.SaleObjectiveAmount - self.SaleMonth, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator);
|
|
|
+ Objective = accounting.formatMoney(self.SaleObjectiveAmount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator);
|
|
|
+ Today = accounting.formatMoney(self.today, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator);
|
|
|
+ Week = accounting.formatMoney(self.week, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator);
|
|
|
+ Month = accounting.formatMoney(self.SaleMonth, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator);
|
|
|
+
|
|
|
+ self.$el.find('#pending').text(ObjectivePending);
|
|
|
+ self.$el.find('#objective').text(Objective);
|
|
|
+ self.$el.find('#today').text(Today);
|
|
|
+ self.$el.find('#week').text(Week);
|
|
|
+ self.$el.find('#month').text(Month);
|
|
|
+
|
|
|
+
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|