Guest User

Untitled

a guest
Jan 6th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.   alert("you are in");
  4.  
  5.   //The app
  6.   var dbid = "appid";
  7.  
  8.   //This is the time card table
  9.   var dbidTable = "timecardtableid";
  10.  
  11.   //This is the rates table
  12.   var dbidxTable = "ratestableid";
  13.  
  14.   //App token
  15.   var myapptoken = "apptokenhere";
  16.  
  17.   $.ajaxSetup({
  18.     data: {
  19.       apptoken: myapptoken
  20.     }
  21.   }, {
  22.     async: false
  23.   });
  24.  
  25.   var csvData;
  26.   var promiseD3 = $.getScript("https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.13/d3.min.js");
  27.  
  28.   var markup = '';
  29.   markup += '<table>';
  30.   markup += '  <tr>';
  31.   markup += '    <td><label for=QBU_File><b>File</b></label></td>';
  32.   markup += '    <td><input id=QBU_File name=QBU_File type=file></td>';
  33.   markup += '  </tr>';
  34.   markup += '</table>';
  35.  
  36.   var $mydialog = $("<div>").html(markup).dialog({
  37.         title: "Select Time Card CSV file to Import",
  38.         modal: true,
  39.         autoOpen: false,
  40.         width: 400,
  41.         resizable: false,
  42.  
  43.         buttons: {
  44.           Proceed: function() {
  45.             $.post(dbidTable, {
  46.               act: "API_ImportFromCSV",
  47.               records_csv: csvData,
  48.               clist: "171.28.29.30.31.32.89.41"
  49.             }).then(function(xml) {
  50.               document.location.href = dbidTable + "?a=td";
  51.             });
  52.  
  53.             $(this).dialog("close");
  54.           },
  55.  
  56.           Cancel: function() {
  57.             $(this).dialog("close");
  58.           }
  59.         },
  60.  
  61.         open: function(event, ui) {
  62.           $mydialog.show();
  63.           $mydialog.html(markup);
  64.  
  65.           $("#QBU_File").on("change", function(e) {
  66.               var file = e.target.files[0];
  67.               var reader = new FileReader();
  68.               reader.onload = function(e) {
  69.                 var contents = e.target.result;
  70.  
  71.                 $.when(promiseD3).then(function(argsD3) {
  72.                   alert("D3 was promised");
  73.                   var data = d3.csv.parseRows(contents);
  74.  
  75.                   //For each row in the CSV file, query the rates table for the item id and find the latest record who's effective date is not greater than the invoice date                           _.each(data, function(item) {                                                                                             varInvDate = item[1];                                                                                                  varItemID = item[6];                                                              
  76.  
  77.                   processRecords(varItemID, varInvDate);
  78.                 });
  79.  
  80.                 var csv = [];
  81.                 var index = 0;
  82.               });
  83.           }
  84.  
  85.           reader.readAsText(file);
  86.         });
  87.     },
  88.  
  89.     close: function(event, ui) {}
  90.  
  91. });
  92.  
  93. $mydialog.dialog("open");
  94.  
  95. function processRecords(vitem, vdate) {
  96.  
  97.   alert(vitem);
  98.   alert(vdate);
  99.  
  100.   //This is a query on the rates table. We want to get the max effective date for the current itemID as compared to the current invoice date.
  101.  
  102.   //So we query for records with an effective date on or before the invoice date. The sort is descending on effective date, so the first record in the result set will be the on we want.
  103.  
  104.   //We need to take the Record ID from this first record and stamp it on the Related Record Id field on the time card table, which we are importing into.
  105.  
  106.   var promiseMaxEffDate = $.get(dbidxTable, {
  107.     act: "API_DoQuery",
  108.     query: "{'30'.EQ.'" + vitem + "'} AND {'45'.OBF.'" + vdate + "'}",
  109.     clist: "45.3",
  110.     slist: "45",
  111.     includeRids: "1",
  112.     options: "num-1.sortorder-D"
  113.   });
  114.  
  115.   $.when(promiseMaxEffDate).then(function(argMaxEffDate) {
  116.     alert("promiseMaxEffDate"); //Gets reference record id of the rates record for the itmeID/InvoiceDate
  117.     //var refid = parseInt($("Record ID #", argMaxEffDate[0]).text() || "0", 10);               //alert("THEREFID " + refid);
  118.   });
  119.  
  120. }
  121.  
  122.  
  123. alert("you are out");
  124.  
  125. //Based on code from Dan Diebolt - phone number (734-985-0721)- email address (dandiebolt@yahoo.com) .
  126.  
  127. })();
Add Comment
Please, Sign In to add comment