Guest User

Untitled

a guest
Mar 16th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var controller_name = "consumptions";
  2. var table_id = "consumptions";
  3. var editor; // use a global for the submit and return data rendering in the examples
  4.  
  5. $(document).ready(function() {
  6.     openFilters();
  7.     year = filters.year==undefined?new Date().getFullYear():filters.year;
  8.     if(year != undefined) {
  9.         editor = new $.fn.dataTable.Editor({
  10.             ajax: {
  11.                 edit: {
  12.                     type: 'POST',
  13.                     url: "/" + controller_name +"/" + customer.id,
  14.                     contentType: "application/json",
  15.                     data: function (data) {
  16.                         //var editor = table.row(focused[0][0].row).data();
  17.                         var parent_row_id = $(focused.node()).parents("tr").attr("id");
  18.                         data.mirror_customer_id = parent_row_id.replace("row_" + customer.id + "_", "").replace("row_", "");
  19.                         data.customer_id = customer.id;
  20.                         data.is_contrasted = false;
  21.                         return JSON.stringify(data);
  22.                     }
  23.                 }
  24.             },
  25.             table: $('#' + table_id),
  26.             fields: [
  27.                 {
  28.                     name: "1_" + year
  29.                 },
  30.                 {
  31.                     name: "2_" + year
  32.                 },
  33.                 {
  34.                     name: "3_" + year
  35.                 },
  36.                 {
  37.                     name: "4_" + year
  38.                 },
  39.                 {
  40.                     name: "5_" + year
  41.                 },
  42.                 {
  43.                     name: "6_" + year
  44.                 },
  45.                 {
  46.                     name: "7_" + year
  47.                 },
  48.                 {
  49.                     name: "8_" + year
  50.                 },
  51.                 {
  52.                     name: "9_" + year
  53.                 },
  54.                 {
  55.                     name: "10_" + year
  56.                 },
  57.                 {
  58.                     name: "11_" + year
  59.                 },
  60.                 {
  61.                     name: "12_" + year
  62.                 }
  63.             ],
  64.             idSrc: 'id',
  65.             formOptions: {
  66.                 inline: {
  67.                     onBlur: 'submit',
  68.                     drawType: 'none'
  69.                 }
  70.             }
  71.         });
  72.  
  73.         $( 'input', editor.node() ).on( 'focus', function () {
  74.             this.select();
  75.         } );
  76.  
  77.         var focused;
  78.         editor.on( 'open', function ( e, mode, action ) {
  79.  
  80.             focused = table.cell({focused: true});
  81.             var current_now_number = focused[0][0].row;
  82.  
  83.             if ( mode === 'inline' ) {
  84.  
  85.                 editor.on( 'postSubmit.editorInline', function (e, json, data, action, xhr) {
  86.  
  87.                     this.close();
  88.  
  89.                     if ( focused.any() ) {
  90.  
  91.                         var next = $(focused.node()).next();
  92.  
  93.                         if ( next.length ) {
  94.                             editor.one( 'submitComplete', function () {
  95.                                 table.cell( next ).focus();
  96.                             } );
  97.                         }
  98.                     }
  99.                     table.row(current_now_number).data(json.data);
  100.                     table.row(current_now_number).node().className = "highlight";
  101.                     var current_row = $(table.row(current_now_number).node());
  102.  
  103.                     setTimeout(function(){ current_row.addClass("noHighlight").removeClass("highlight");
  104.                     setTimeout(function(){ current_row.removeClass("noHighlight")},550)},500);
  105.                 } );
  106.             }
  107.         } );
  108.  
  109.         editor.on( 'close', function () {
  110.             editor.off( 'postSubmit.editorInline' );
  111.         } );
  112.  
  113.         var firstLoad = true;
  114.         var table = $('#' + table_id).DataTable({
  115.             "ajax": {
  116.                 "type": "GET",
  117.                 "url": "/" + controller_name +"/" + customer.id,
  118.                 "dataSrc": function (json) {
  119.                     totalFooter = json.totalFooter;
  120.                     return json.data;
  121.                 }
  122.             },
  123.             "rowId" : "id",
  124.             "dom": 'Bfrtip',
  125.             "processing": true,
  126.             "searching": false,
  127.             "paging": false,
  128.             "info": false,
  129.             fixedHeader: {
  130.                 header: true,
  131.                 headerOffset: $('.page-header.navbar').height()
  132.             },
  133.             "serverSide": true,
  134.             keys: {
  135.                 columns: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
  136.                 keys: [ 9 ],
  137.                 editor: editor,
  138.                 editOnFocus: true
  139.             },
  140.             select: {
  141.                 style:    'os',
  142.                 selector: 'td:first-child',
  143.                 blurable: true
  144.             },
  145.             "fnServerParams": function (aoData) {
  146.                 aoData = prepareDefaultFilters(table_id, aoData);
  147.                 firstLoad = false;
  148.             },
  149.             "createdRow": function (row, data, dataIndex) {
  150.                 $(row).addClass("customer" + data.id);
  151.                 $(row).attr("id", data.id);
  152.             },
  153.             "footerCallback": function (row, data, start, end, display) {
  154.                 var api = this.api();
  155.                 $(api.column(2).footer()).html(roundNumber(totalFooter[1], 2));
  156.                 $(api.column(3).footer()).html(roundNumber(totalFooter[2], 2));
  157.                 $(api.column(4).footer()).html(roundNumber(totalFooter[3], 2));
  158.                 $(api.column(5).footer()).html(roundNumber(totalFooter[4], 2));
  159.                 $(api.column(6).footer()).html(roundNumber(totalFooter[5], 2));
  160.                 $(api.column(7).footer()).html(roundNumber(totalFooter[6], 2));
  161.                 $(api.column(8).footer()).html(roundNumber(totalFooter[7], 2));
  162.                 $(api.column(9).footer()).html(roundNumber(totalFooter[8], 2));
  163.                 $(api.column(10).footer()).html(roundNumber(totalFooter[9], 2));
  164.                 $(api.column(11).footer()).html(roundNumber(totalFooter[10], 2));
  165.                 $(api.column(12).footer()).html(roundNumber(totalFooter[11], 2));
  166.                 $(api.column(13).footer()).html(roundNumber(totalFooter[12], 2));
  167.                 $(api.column(14).footer()).html(roundNumber(totalFooter[13], 2));
  168.             },
  169.             "footerCallback": function (row, data, start, end, display) {
  170.                 var api = this.api();
  171.                 $(api.column(2).footer()).html(roundNumber(totalFooter[1], 2));
  172.                 $(api.column(3).footer()).html(roundNumber(totalFooter[2], 2));
  173.                 $(api.column(4).footer()).html(roundNumber(totalFooter[3], 2));
  174.                 $(api.column(5).footer()).html(roundNumber(totalFooter[4], 2));
  175.                 $(api.column(6).footer()).html(roundNumber(totalFooter[5], 2));
  176.                 $(api.column(7).footer()).html(roundNumber(totalFooter[6], 2));
  177.                 $(api.column(8).footer()).html(roundNumber(totalFooter[7], 2));
  178.                 $(api.column(9).footer()).html(roundNumber(totalFooter[8], 2));
  179.                 $(api.column(10).footer()).html(roundNumber(totalFooter[9], 2));
  180.                 $(api.column(11).footer()).html(roundNumber(totalFooter[10], 2));
  181.                 $(api.column(12).footer()).html(roundNumber(totalFooter[11], 2));
  182.                 $(api.column(13).footer()).html(roundNumber(totalFooter[12], 2));
  183.                 $(api.column(14).footer()).html(roundNumber(totalFooter[13], 2));
  184.             },
  185.             columns: [
  186.                 {"data": "id", "visible": false},
  187.                 {"data": "commercial_name", "render": function (data, type, full, meta){
  188.                         return data + '<a class="putZero" style="float:right" href="#"><i class="far fa-check-square"></i></a>';
  189.                     }
  190.                 },
  191.                 {
  192.                     "data": "1_" + year, "className": "editable month1 text-right",
  193.                     "createdCell": function (td, cellData, full, row, col) {
  194.                         if(!full.enabled[1]){
  195.                             $(td).addClass('disabled');
  196.                         }
  197.                     }
  198.                 },
  199.                 {
  200.                     "data": "2_" + year, "className": "editable month2 text-right",
  201.                     "createdCell": function (td, cellData, full, row, col) {
  202.                         if(!full.enabled[2]){
  203.                             $(td).addClass('disabled');
  204.                         }
  205.                     }
  206.                 },
  207.                 {
  208.                     "data": "3_" + year, "className": "editable month3 text-right",
  209.                     "createdCell": function (td, cellData, full, row, col) {
  210.                         if(!full.enabled[3]){
  211.                             $(td).addClass('disabled');
  212.                         }
  213.                     }
  214.                 },
  215.                 {
  216.                     "data": "4_" + year, "className": "editable month4 text-right",
  217.                     "createdCell": function (td, cellData, full, row, col) {
  218.                         if(!full.enabled[4]){
  219.                             $(td).addClass('disabled');
  220.                         }
  221.                     }
  222.                 },
  223.                 {
  224.                     "data": "5_" + year, "className": "editable month5 text-right",
  225.                     "createdCell": function (td, cellData, full, row, col) {
  226.                         if(!full.enabled[5]){
  227.                             $(td).addClass('disabled');
  228.                         }
  229.                     }
  230.                 },
  231.                 {
  232.                     "data": "6_" + year, "className": "editable month6 text-right",
  233.                     "createdCell": function (td, cellData, full, row, col) {
  234.                         if(!full.enabled[6]){
  235.                             $(td).addClass('disabled');
  236.                         }
  237.                     }
  238.                 },
  239.                 {
  240.                     "data": "7_" + year, "className": "editable month7 text-right",
  241.                     "createdCell": function (td, cellData, full, row, col) {
  242.                         if(!full.enabled[7]){
  243.                             $(td).addClass('disabled');
  244.                         }
  245.                     }
  246.                 },
  247.                 {
  248.                     "data": "8_" + year, "className": "editable month8 text-right",
  249.                     "createdCell": function (td, cellData, full, row, col) {
  250.                         if(!full.enabled[8]){
  251.                             $(td).addClass('disabled');
  252.                         }
  253.                     }
  254.                 },
  255.                 {
  256.                     "data": "9_" + year, "className": "editable month9 text-right",
  257.                     "createdCell": function (td, cellData, full, row, col) {
  258.                         if(!full.enabled[9]){
  259.                             $(td).addClass('disabled');
  260.                         }
  261.                     }
  262.                 },
  263.                 {
  264.                     "data": "10_" + year, "className": "editable month10 text-right",
  265.                     "createdCell": function (td, cellData, full, row, col) {
  266.                         if(!full.enabled[10]){
  267.                             $(td).addClass('disabled');
  268.                         }
  269.                     }
  270.                 },
  271.                 {
  272.                     "data": "11_" + year, "className": "editable month11 text-right",
  273.                     "createdCell": function (td, cellData, full, row, col) {
  274.                         if(!full.enabled[11]){
  275.                             $(td).addClass('disabled');
  276.                         }
  277.                     }
  278.                 },
  279.                 {
  280.                     "data": "12_" + year, "className": "editable month12 text-right",
  281.                     "createdCell": function (td, cellData, full, row, col) {
  282.                         if(!full.enabled[12]){
  283.                             $(td).addClass('disabled');
  284.                         }
  285.                     }
  286.                 },
  287.                 {
  288.                     "data": "total", "className": "text-right"
  289.                 }
  290.             ]
  291.         });
  292.         attachRemoveEvent(table);
  293.     }
  294.     loadFilters(table_id);
  295.  
  296.     $('#consumptions').on( 'click', '.putZero',function (evt) {
  297.  
  298.         evt.preventDefault();
  299.         evt.stopPropagation();
  300.  
  301.         var current_row = $(this).parents("tr").attr("id");
  302.         var current_data = table.row("#" + current_row.replace("row_", "")).data();
  303.         var customer_sales_id = current_row.replace("row_", "");
  304.  
  305.         var parent_row = $(this).parents("tr");
  306.         for(var i = 1; i < 13; i++){
  307.             var current_cell = parent_row.find(".month" + i);
  308.             if(current_data[i + "_" + year] == null && !$(current_cell).hasClass("disabled"))
  309.                 current_data[i + "_" + year] = 0;
  310.         }
  311.  
  312.         current_data.year = year;
  313.         current_data.customer_sales_id = customer_sales_id;
  314.         current_data.customer_id = customer.id;
  315.         current_data.is_contrasted = false;
  316.  
  317.         table.row("#" + current_row.replace("row_", "")).data(current_data);
  318.  
  319.         $.ajax({
  320.             url: "/consumptions/batchUpdate",
  321.             type: 'post',
  322.             data: current_data,
  323.             dataType: 'json',
  324.             success: function (data) {
  325.                 table.row("#" + current_row.replace("row_", "")).invalidate();
  326.             }
  327.         });
  328.  
  329.     });
  330. });
  331.  
Advertisement
Add Comment
Please, Sign In to add comment