Test Setup Failed
Push — master ( 6e2987...bf1336 )
by
unknown
03:38
created

  B

Complexity

Conditions 9
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 2
dl 0
loc 19
rs 7.756
c 0
b 0
f 0
nop 1
1
define(function(require) {
2
    'use strict';
3
4
    var _ = require('underscore');
5
    var $ = require('jquery');
6
    var __ = require('orotranslation/js/translator');
7
    var messenger = require('oroui/js/messenger');
8
9
    var defaultParam = {
10
        message: 'All product prices should be unique.'
11
    };
12
13
    /**
14
     * @param {HTMLElement} element
15
     * @return {string}
16
     */
17
    function getStringifiedValues(element) {
18
        var price = $(element);
19
20
        var priceList = price.find('input[name$="[priceList]"]').val();
21
        var quantity = price.find('input[name$="[quantity]"]').val();
22
        var unit = price.find('select[name$="[unit]"] option:selected').val();
23
        var currency = price.find('select[name$="[currency]"] option:selected').val();
24
25
        if (
26
            !priceList || !priceList.trim() ||
27
            !quantity || !quantity.trim() ||
28
            !unit || !unit.trim() ||
29
            !currency || !currency.trim()
30
        ) {
31
            return '';
32
        }
33
34
        return [priceList, parseFloat(quantity), unit, currency].join(':');
35
    }
36
37
    /**
38
     * @export oropricing/js/validator/unique-product-prices
39
     */
40
    return [
41
        'Oro\\Bundle\\PricingBundle\\Validator\\Constraints\\UniqueProductPrices',
42
        function(value, element) {
43
            var processedPrices = [];
44
            var $container = $(element).closest('.oro-item-collection');
45
46
            var duplicate = _.find($container.find('.oro-multiselect-holder'), function(price) {
47
                var stringifiedPrice = getStringifiedValues(price);
48
49
                if (stringifiedPrice === '') {
50
                    return;
51
                }
52
                if (processedPrices.indexOf(stringifiedPrice) !== -1) {
53
                    // duplicates are found
54
                    return true;
55
                } else {
56
                    processedPrices.push(stringifiedPrice);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
57
                }
58
            });
59
60
            return duplicate === void 0;
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
61
        },
62
        function(param) {
63
            param = _.extend({}, defaultParam, param);
64
            messenger.notificationFlashMessage('error', __(param.message, {}));
65
66
            return false;
67
        }
68
    ];
69
});
70