Sunday, June 25, 2017

Excel Uplaod through Node JS to MySQL

"use strict";

var parseXlsx = require('excel');
var express =   require("express");
var multer  =   require('multer');
var mysql   = require("mysql");
var dateTime  = require("node-datetime");
var bodyParser = require('body-parser');
var async = require('async');
var http=require("http");
var fs=require("fs");
var path = require('path');
var url = require("url");
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var app         =   express();


var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now()+'.xlsx');
  }
});


var upload = multer({ storage : storage}).single('userPhoto');

var connection = mysql.createConnection({
host     : 'xxxx.ap-south-1.rds.amazonaws.com',
        user     : 'xxx123',
        password : 'xxx',
        database : 'xxx',
multipleStatements: true
});

app.get('/',function(req,res){
      res.send("ref. Administrator");
});

app.get('/branch/:id',function(req,res){
      res.sendFile(__dirname + "/index.html");
});

app.post('/dharama',function(req,res){
     connection.query("SELECT count(lm.drm_id) as cnt from dharamart1 lm "+
"left join dharamart_master drm on drm.drm_id=lm.drm_id "+
"where "+
"drm.response_id in (select max(drm.response_id) from dharamart_master drm group by drm.drm_id) "+
"and lm.drm_status=1 "+
"and lm.status_drm in (3,12) "+
"and lm.branch_drm=1", function (error, rows) {
if(error) {
                //res.json({"Error" : true, "Message" : "Error executing MySQL query"});
console.log(req.body);
            } else {
                //res.json({"Error" : false, "Message" : "Success ", "Login" : rows});
console.log(req.body);
            }
});
});

app.post('/uploadsdata',function(req,res){
    upload(req,res,function(err) {
        if(err) {
            return res.end("Error uploading file.");
        }
        res.end("File is uploaded"+ req.file);
console.log('status: ' + req.body.status1);
console.log('dharama: ' + req.body.dharama);
console.log('lead_date: ' + req.body.lead_date);
//console.log('App Date: ' + req.body.app_date);
console.log('Branch: ' + req.body.branch_drm);
console.log(__dirname+'/uploads/'+req.file.filename);
//console.log(req.file.filename);
parseXlsx('http://xxxx/upload'+req.file.filename, function(err, data) {
 if(err) throw err;
// data is an array of arrays
if(data){
connection.connect();

for(var i=1;i //console.log(data[i][2]);

if(req.body.dharama==1){
if(req.body.status1==5){
var condition = {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:'',apptime:data[i][14],apploc:data[i][16]};

connection.query("INSERT INTO dharamart1 SET ?", condition, function (error, results, fields) {
 if (error) throw error;
 var drm_id=results.insertId;
 var query1 = connection.query('select * from dharamart1 where drm_id in(?)', drm_id , function(err, rows1) {
var tweet1 = JSON.parse(JSON.stringify(rows1));
for(var i in tweet1){
//res.write("Hashtag: ");
//res.write(tweet1[i].drm_id);
var appadd='',apppin='';
if(tweet1[i].apploc=='Office'){
appadd=tweet1[i].off_address;
apppin=tweet1[i].off_pincode;
}else{
appadd=tweet1[i].res_data;
apppin=tweet1[i].res_pin;
}
var condition1 = {drm_id:tweet1[i].drm_id, mobile:tweet1[i].mobile, status_drm:req.body.status1, dharama_id:req.body.dharama, response:'', app_date:req.body.app_date, app_time:tweet1[i].apptime, app_location:tweet1[i].apploc, doc_collected:'', updatedby:'', updatedfor:'', res_timestamp:formatted, branch_drm:req.params.branch_drm, app_no:'', app_remark:'', sub_type:'', dr_reason:'', doc:tweet1[i].doc,app_add: appadd, app_pin:apppin};connection.query("INSERT INTO dharamart_master SET ?", condition1, function (error, results, fields) {
 if (error) throw error;
 console.log( tweet1[i].drm_id+ " >> Inserted Successfully");
});
}
});
});
}
if(req.body.status1==1){
connection.query('INSERT INTO dharamart1 SET ?', {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:''}, function(err, result) {
 if (err) throw err;
 console.log(" >> Inserted Successfully");
});
}
}

if(req.body.dharama==2){
if(req.body.status1==5){
var condition = {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:'',apptime:data[i][14],apploc:data[i][16]};

connection.query("INSERT INTO dharamart1 SET ?", condition, function (error, results, fields) {
 if (error) throw error;
 var drm_id=results.insertId;
 var query1 = connection.query('select * from dharamart1 where drm_id in(?)', drm_id , function(err, rows1) {
var tweet1 = JSON.parse(JSON.stringify(rows1));
for(var i in tweet1){
//res.write("Hashtag: ");
//res.write(tweet1[i].drm_id);
var appadd='',apppin='';
if(tweet1[i].apploc=='Office'){
appadd=tweet1[i].off_address;
apppin=tweet1[i].off_pincode;
}else{
appadd=tweet1[i].res_data;
apppin=tweet1[i].res_pin;
}
var condition1 = {drm_id:tweet1[i].drm_id, mobile:tweet1[i].mobile, status_drm:req.body.status1, dharama_id:req.body.dharama, response:'', app_date:req.body.app_date, app_time:tweet1[i].apptime, app_location:tweet1[i].apploc, doc_collected:'', updatedby:'', updatedfor:'', res_timestamp:formatted, branch_drm:req.params.branch_drm, app_no:'', app_remark:'', sub_type:'', dr_reason:'', doc:tweet1[i].doc,app_add: appadd, app_pin:apppin};
connection.query("INSERT INTO dharamart_master SET ?", condition1, function (error, results, fields) {
 if (error) throw error;
 console.log( tweet1[i].drm_id+ " >> Inserted Successfully");
});
}
});
});
}
if(req.body.status1==1){
connection.query('INSERT INTO dharamart1 SET ?', {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:''}, function(err, result) {
 if (err) throw err;
 console.log(" >> Inserted Successfully");
});
}
}

if(req.body.dharama==3){
if(req.body.status1==5){
var condition = {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:'',apptime:data[i][14],apploc:data[i][16]};

connection.query("INSERT INTO dharamart1 SET ?", condition, function (error, results, fields) {
 if (error) throw error;
 var drm_id=results.insertId;
 var query1 = connection.query('select * from dharamart1 where drm_id in(?)', drm_id , function(err, rows1) {
var tweet1 = JSON.parse(JSON.stringify(rows1));
for(var i in tweet1){
//res.write("Hashtag: ");
//res.write(tweet1[i].drm_id);
var appadd='',apppin='';
if(tweet1[i].apploc=='Office'){
appadd=tweet1[i].off_address;
apppin=tweet1[i].off_pincode;
}else{
appadd=tweet1[i].res_data;
apppin=tweet1[i].res_pin;
}
var condition1 = {drm_id:tweet1[i].drm_id, mobile:tweet1[i].mobile, status_drm:req.body.status1, dharama_id:req.body.dharama, response:'', app_date:req.body.app_date, app_time:tweet1[i].apptime, app_location:tweet1[i].apploc, doc_collected:'', updatedby:'', updatedfor:'', res_timestamp:formatted, branch_drm:req.params.branch_drm, app_no:'', app_remark:'', sub_type:'', dr_reason:'', doc:tweet1[i].doc,app_add: appadd, app_pin:apppin};
connection.query("INSERT INTO dharamart_master SET ?", condition1, function (error, results, fields) {
 if (error) throw error;
 console.log( tweet1[i].drm_id+ " >> Inserted Successfully");
});
}
});
});
}
if(req.body.status1==1){
connection.query('INSERT INTO dharamart1 SET ?', {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:'', off_pincode:data[i][12], doc:data[i][15], remarks:data[i][17], campaign_name:''}, function(err, result) {
 if (err) throw err;
 console.log(" >> Inserted Successfully");
});
}
}

if(req.body.dharama==4){
if(req.body.status1==5){
var condition = {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:data[i][13], off_pincode:data[i][12], doc:data[i][16], remarks:data[i][18], campaign_name:'',apptime:data[i][15],apploc:data[i][17],drm_lead:data[i][22],annual_salary:data[i][23],YBLCustomer:data[i][24],source_code:data[i][25],ASM_code:data[i][26],LC_code:data[i][27],DV_name:data[i][28],Token_no:data[i][29]};

connection.query("INSERT INTO dharamart1 SET ?", condition, function (error, results, fields) {
 if (error) throw error;
 var drm_id=results.insertId;
 var query1 = connection.query('select * from dharamart1 where drm_id in(?)', drm_id , function(err, rows1) {
var tweet1 = JSON.parse(JSON.stringify(rows1));
for(var i in tweet1){
//res.write("Hashtag: ");
//res.write(tweet1[i].drm_id);
var appadd='',apppin='';
if(tweet1[i].apploc=='Office'){
appadd=tweet1[i].off_address;
apppin=tweet1[i].off_pincode;
}else{
appadd=tweet1[i].res_data;
apppin=tweet1[i].res_pin;
}
var condition1 = {drm_id:tweet1[i].drm_id, mobile:tweet1[i].mobile, status_drm:req.body.status1, dharama_id:req.body.dharama, response:'', app_date:req.body.app_date, app_time:tweet1[i].apptime, app_location:tweet1[i].apploc, doc_collected:'', updatedby:'', updatedfor:'', res_timestamp:formatted, branch_drm:req.params.branch_drm, app_no:'', app_remark:'', sub_type:'', dr_reason:'', doc:tweet1[i].doc,app_add: appadd, app_pin:apppin};
connection.query("INSERT INTO dharamart_master SET ?", condition1, function (error, results, fields) {
 if (error) throw error;
 console.log( tweet1[i].drm_id+ " >> Inserted Successfully");
});
}
});
});
}
if(req.body.status1==1){
connection.query('INSERT INTO dharamart1 SET ?', {mobile:data[i][4], branch_drm:req.params.branch_drm, status_drm:req.body.status1, lead_date:req.body.lead_date, drm_status:1, dharama_id:req.body.dharama, customer_name:data[i][3], email:'', product:data[i][6], product_code:'', form_no:data[i][1],source:data[i][2], source2:'', source3:'', call_date:req.body.lead_date, res_data:data[i][7],res_pin:data[i][8], off_name:data[i][10], off_address:data[i][11], off_no:data[i][13], off_pincode:data[i][12], doc:data[i][16], remarks:data[i][18], campaign_name:'',apptime:data[i][15],apploc:data[i][17],drm_lead:data[i][22],annual_salary:data[i][23],YBLCustomer:data[i][24],source_code:data[i][25],ASM_code:data[i][26],LC_code:data[i][27],DV_name:data[i][28],Token_no:data[i][29]}, function(err, result) {
 if (err) throw err;
 console.log(" >> Inserted Successfully");
});
}
}

if(req.body.dharama==5){
if(req.body.status1==5){
var condition = {lead_date:req.body.lead_date,dharama_id:req.body.dharama,status_drm:req.body.status1,branch_drm:req.params.branch_drm,form_no:data[i][1],source:data[i][6],customer_name:data[i][2],mobile:data[i][3],product:data[i][5],res_add:data[i][4],Remarks:data[i][7],drm_status:1};

connection.query("INSERT INTO dharamart1 SET ?", condition, function (error, results, fields) {
 if (error) throw error;
 var drm_id=results.insertId;
 var query1 = connection.query('select * from dharamart1 where drm_id in(?)', drm_id , function(err, rows1) {
var tweet1 = JSON.parse(JSON.stringify(rows1));
for(var i in tweet1){
//res.write("Hashtag: ");
//res.write(tweet1[i].drm_id);
var appadd='',apppin='';
if(tweet1[i].apploc=='Office'){
appadd=tweet1[i].off_address;
apppin=tweet1[i].off_pincode;
}else{
appadd=tweet1[i].res_data;
apppin=tweet1[i].res_pin;
}
var condition1 = {drm_id:tweet1[i].drm_id, mobile:tweet1[i].mobile, status_drm:req.body.status1, dharama_id:req.body.dharama, response:'', app_date:req.body.app_date, app_time:tweet1[i].apptime, app_location:tweet1[i].apploc, doc_collected:'', updatedby:'', updatedfor:'', res_timestamp:formatted, branch_drm:req.params.branch_drm, app_no:'', app_remark:'', sub_type:'', dr_reason:'', doc:tweet1[i].doc,app_add: appadd, app_pin:apppin};
connection.query("INSERT INTO dharamart_master SET ?", condition1, function (error, results, fields) {
 if (error) throw error;
 console.log( tweet1[i].drm_id+ " >> Inserted Successfully");
});
}
});
});
}
if(req.body.status1==1){
connection.query('INSERT INTO dharamart1 SET ?', {lead_date:req.body.lead_date,dharama_id:req.body.dharama,status_drm:req.body.status1,branch_drm:req.params.branch_drm,form_no:data[i][1],source:data[i][6],customer_name:data[i][2],mobile:data[i][3],product:data[i][5],res_add:data[i][4],Remarks:data[i][7],drm_status:1}, function(err, result) {
 if (err) throw err;
 console.log(" >> Inserted Successfully");
});
}
}

}
console.log("No of Insrt data: "+i);
}
});
    });
});

app.listen(3000,function(){
    console.log("Working on port 3000");
});




HTML:::












Excel (*.xlsx)
Status



Client



Lead Date
Appointment Date




No comments:

Post a Comment

Dharamart.blogspot.in