Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • 4a4b0e5608fefcd76e0decdc81945186abc8fedc
  • 16.0 par défaut protégée
  • 16.0-admin-menu-refactoring
3 résultats

canvas.js

Blame
  • Bifurcation depuis Le Filament / Opération Auto-Consommation Collective / oacc_portal_overview_cdc
    Le projet source a une visibilité limitée.
    invoice_allocation.js 7,91 Kio
    // © 2019 Le Filament (<http://www.le-filament.com>)
    // License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
    
    odoo.define('legicoop_account.invoice_allocation', function (require) {
        "use strict";
    
        var AbstractAction = require('web.AbstractAction');
        var ControlPanelMixin = require('web.ControlPanelMixin');
        var SearchView = require('web.SearchView');
        var core = require('web.core');
        var data = require('web.data');
        var pyUtils = require('web.py_utils');
    
        var _t = core._t;
        var QWeb = core.qweb;
    
        var AccountInvoiceAllocation = AbstractAction.extend(ControlPanelMixin, {
            events: {
                "submit form[name='allocation_form']": "_onSubmit",
            },
            /**
             * @override
             */
             init: function (parent, action) {
                this._super.apply(this, arguments);
                this.action = action;
                this.action_manager = parent;
                this.set('title', action.name || _t('Factures à répartir'));
                this.invoice_ids = [];
            },
            /**
             * @override
             */
            willStart: function () {
                var self = this;
                var view_id = this.action && this.action.search_view_id && this.action.search_view_id[0];
                var def = this
                    .loadViews('account.invoice', this.action.context || {}, [[view_id, 'search']])
                    .then(function (result) {
                        self.fields_view = result.search;
                    });
                return $.when(this._super(), def);
            },
            /**
             * @override
             */
            start: function () {
                var self = this;
    
                // find default search from context
                var search_defaults = {};
                var context = this.action.context || [];
                _.each(context, function (value, key) {
                    var match = /^search_default_(.*)$/.exec(key);
                    if (match) {
                        search_defaults[match[1]] = value;
                    }
                });
    
                // create searchview
                var options = {
                    $buttons: $("<div>"),
                    action: this.action,
                    disable_groupby: true,
                    search_defaults: search_defaults,
                };
    
                var dataset = new data.DataSetSearch(this, 'account.invoice');
                this.searchview = new SearchView(this, dataset, this.fields_view, options);
                this.searchview.on('search', this, this._onSearch);
    
                var def1 = this._super.apply(this, arguments);
                var def2 = this.searchview.appendTo($("<div>")).then(function () {
                    self.$searchview_buttons = self.searchview.$buttons.contents();
                });
    
                return $.when(def1, def2).then(function(){
                    self.searchview.do_search();
                });
            },
    
            //--------------------------------------------------------------------------
            // Public
            //--------------------------------------------------------------------------
    
            /**
             * @override
             */
            do_show: function () {
                this._super.apply(this, arguments);
                this.searchview.do_search();
                this.action_manager.do_push_state({
                    action: this.action.id,
                    active_id: this.action.context.active_id,
                });
            },
    
            //--------------------------------------------------------------------------
            // Private
            //--------------------------------------------------------------------------
    
            /**
             * Refresh the DOM html
             *
             * @private
             * @param {string|html} dom
             */
            _refreshAllocation: function (dom) {
                var $dom = $(dom);
                this.$el.html($dom);
            },
    
            /**
             * Call controller to get the html content
             *
             * @private
             * @param {string[]}
             * @returns {Deferred}
             */
            _fetchAllocation: function (domain) {
                var self = this;
                return this._rpc({
                    route:"/account/allocation",
                    params: {domain: domain},
                }).then(function(result) {
                    self._refreshAllocation(result.html_content);
                    self._updateControlPanel();
                    self.invoice_ids = result.invoice_ids;
                });
            },
    
            /**
             * @private
             */
            _updateControlPanel: function () {
    
                this.update_control_panel({
                    cp_content: {
                        $searchview: this.searchview.$el,
                        $searchview_buttons: this.$searchview_buttons,
                    },
                    searchview: this.searchview,
                });
            },
    
    
            //--------------------------------------------------------------------------
            // Handlers
            //--------------------------------------------------------------------------
    
            /**
             *
             * @private
             * @param {MouseEvent} event
             */
            _onSubmit: function (ev) {
                ev.preventDefault();
                var self = this;
                let invoice_id = parseInt(ev.target.id)
                let elements = Array.from(ev.target.elements)
                // Get employees with percent > 0 & total percent
                let employees = elements.filter(function (el) {
                  return el.dataset.inputName === "employee" && el.valueAsNumber > 0;
                });
                let employeeTotal = employees.reduce(function (accumulator, employee) {
                  return accumulator + employee.valueAsNumber;
                }, 0);
                // Get Subcontracting amount
                let subcontracting = elements.find(el => el.name === "subcontracting_amount");
                // Get Subcontracting amount
                let expense = elements.find(el => el.name === "expense_amount");
    
                if (employeeTotal != 100) {
                    var msg = $(ev.target).find(".error-message")
                    msg.html("<div class='alert alert-danger mt16' role='alert'>Mise à jour impossible : le total des pourcentage n'est pas égal à 100.</div>")
                    return
                } else {
                    var new_allocation = []
                    _.each(employees, function (emp) {
                        new_allocation.push([0, 0, {
                            "partner_id": parseInt(emp.dataset.partnerId),
                            "percentage": emp.valueAsNumber,
                        }])
                    });
                    var new_values = {
                        "subcontracting_amount": subcontracting.valueAsNumber,
                        "expense_amount": expense.valueAsNumber,
                        "employee_allocation_ids": new_allocation,
                    }
                    this._rpc({
                        model: "account.invoice",
                        method: "create_employee_lines",
                        args: [[invoice_id], new_values],
                    }).then(function (result) {
    //                    self._fetchAllocation()
                          self.searchview.do_search();
                    });
                }
            },
            /**
             * This client action has its own search view and already listen on it. If
             * we let the event pass through, it will be caught by the action manager
             * which will do its work.  This may crash the web client, if the action
             * manager tries to notify the previous action.
             *
             * @private
             * @param {OdooEvent} event
             */
            _onSearch: function (event) {
                event.stopPropagation();
                var session = this.getSession();
                // group by are disabled, so we don't take care of them
                var result = pyUtils.eval_domains_and_contexts({
                    domains: event.data.domains,
                    contexts: [session.user_context].concat(event.data.contexts)
                });
    
                this._fetchAllocation(result.domain);
            },
        });
    
        core.action_registry.add('legicoop_account.invoice_allocation', AccountInvoiceAllocation);
    });