ref: master
plugins/suppliers/public/javascripts/suppliers.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
suppliers = { filter: { form: function() { return table_filter.form() }, apply: function() { this.form().submit() }, }, add_link: function () { if (toggle_edit.isEditing()) toggle_edit.value_row.toggle_edit(); var supplier_add = $('#supplier-add'); toggle_edit.setEditing(supplier_add); toggle_edit.value_row.toggle_edit(); }, toggle_edit: function () { if (toggle_edit.editing().is('#supplier-add')) toggle_edit.editing().toggle(toggle_edit.isEditing()); toggle_edit.editing().find('.box-view').toggle(!toggle_edit.isEditing()); toggle_edit.editing().find('.box-edit').toggle(toggle_edit.isEditing()); }, add: { supplier_added: function () { $('#results').html(''); }, create_dummy: function () { $('#find-enterprise, #create-dummy, #create-dummy .box-edit').toggle(); }, search: function (input) { query = $(input).val(); if (query.length < 3) return; input.form.onsubmit(); }, }, // core uses (future product plugin) product: { pmsync: function (to_price) { var margin_input = $('#product_margin_percentage') var price_input = $('#product_price') var base_price_input = $('#product_supplier_product_attributes_price') if (to_price) suppliers.price.calculate(price_input, margin_input, base_price_input) else suppliers.margin.calculate(margin_input, price_input, base_price_input) }, updateBasePrice: function () { this.pmsync(this) }, }, our_product: { toggle_edit: function () { toggle_edit.editing().find('.box-edit').toggle(toggle_edit.isEditing()) }, default_change: function (event) { var block = $(this).parents('.block') var nonDefaults = block.find('div[data-non-defaults]') nonDefaults.toggle(!this.checked) nonDefaults.find('input,select,textarea').prop('disabled', this.checked) }, load: function(id) { $('#our-product-'+id+' div[data-default-toggle] input').change(suppliers.our_product.default_change).change(); }, pmsync: function (context, to_price) { var p = $(context).parents('.our-product') var margin_input = p.find('.product-margin-percentage') var price_input = p.find('.product-price') var buy_price_input = p.find('.product-base-price') if (to_price) suppliers.price.calculate(price_input, margin_input, buy_price_input) else suppliers.margin.calculate(margin_input, price_input, buy_price_input) }, select: { all: function() { $('.our-product #product_ids_').attr('checked', true) }, none: function() { $('.our-product #product_ids_').attr('checked', false) }, activate: function(state) { var selection = $('.our-product #product_ids_:checked').parents('.our-product') selection.find('.available input[type=checkbox]').each(function() { this.checked = state $(this.form).submit() }); }, }, import: { confirmRemoveAll: function(checkbox, text) { if (checkbox.checked && !confirm(text)) checkbox.checked = false }, }, }, basket: { searchUrl: null, addUrl: null, removeUrl: null, remove: function (id) { var self = this $.ajax(self.removeUrl, {method: 'DELETE', data: {aggregate_id: id}, success: function(data) { self.reload(data) }, }) }, reload: function (data) { $('#product-basket').html(data) $('#basket-add').focus() }, load: function () { var self = this var input = $('#basket-add') self.source = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: this.searchUrl+'?query=%QUERY', }) self.source.initialize() input.typeahead({ minLength: 2, highlight: true, }, { displayKey: 'label', source: this.source.ttAdapter(), }).on('typeahead:selected', function(e, item) { input.val('') $.post(self.addUrl, {aggregate_id: item.value}, function(data) { self.reload(data) }) }) }, }, price: { calculate: function (price_input, margin_input, base_price_input) { var price = unlocalize_currency($(price_input).val()); var base_price = unlocalize_currency($(base_price_input).val()); var margin = unlocalize_currency($(margin_input).val()); var value = base_price + (margin / 100) * base_price; if (isNaN(value)) value = unlocalize_currency(base_price_input.val()); $(price_input).val(value); }, }, margin: { calculate: function (margin_input, price_input, base_price_input) { var price = unlocalize_currency($(price_input).val()); var base_price = unlocalize_currency($(base_price_input).val()); var margin = unlocalize_currency($(margin_input).val()); var value = ((price - base_price) / base_price ) * 100; value = !isFinite(value) ? 0.0 : value; $(margin_input).val(value); }, }, }; |