326 lines
9.1 KiB
JavaScript
326 lines
9.1 KiB
JavaScript
// Ügyfél kezelés
|
|
const express = require('express');
|
|
const router = express.Router();
|
|
// User Model
|
|
let User = require('../models/user');
|
|
var utils = require('../js/utils');
|
|
const path = require('path');
|
|
let Client = require('../models/client');
|
|
var formidable = require('formidable');
|
|
var csv = require("fast-csv");
|
|
var fs = require('fs');
|
|
// Lista feltöltése
|
|
router.get('/clientupload', utils.ensureAuthenticated,function(req,res){
|
|
|
|
res.render('client_list_upload', {
|
|
title: 'Ügyfél lista feltöltése CSV fájlból',
|
|
user:req.user,
|
|
});
|
|
});
|
|
router.post('/uploadnew', function (req, res){
|
|
var form = new formidable.IncomingForm();
|
|
|
|
form.parse(req);
|
|
|
|
|
|
form.on('file', function (name, file)
|
|
{
|
|
console.log('Uploaded ' +file.path);
|
|
|
|
var stream = fs.createReadStream(file.path);
|
|
Client.findById(req.params.id, function(err,manuf)
|
|
{
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
res.send('Hiba történt az oldal betöltése során. ');
|
|
}
|
|
else
|
|
{
|
|
var clients=[];
|
|
|
|
console.log('Kiválasztott fájl: '+file.name);
|
|
csv
|
|
.fromStream(stream, {delimiter:';',headers : ["name","address","adoszam"]})
|
|
.on("data", function(data){
|
|
var client= new Client();
|
|
|
|
client.name=data.name;
|
|
client.shortname='';
|
|
client.adoszam=data.adoszam;
|
|
client.address=data.address;
|
|
client.author=req.user.id;
|
|
client.manufacturerDiscount=0;
|
|
|
|
clients.push(client);
|
|
|
|
// console.log(data);
|
|
})
|
|
.on("end", function(){
|
|
console.log("Start import...");
|
|
Client.deleteMany({},function(err, result){
|
|
if (err)
|
|
{
|
|
req.flash('error','Hiba a feltöltés során');
|
|
console.log("Removing error!");
|
|
res.redirect('/client/uploadnew');
|
|
}
|
|
else
|
|
{
|
|
console.log("Removing Ok.");
|
|
console.log("Start importing...");
|
|
console.log("Sikeres import!");
|
|
req.flash('success','Ügfél lista sikeresen feltöltve!');
|
|
res.redirect('/client/clients');
|
|
Client.insertMany(clients,function(err){
|
|
|
|
if(err){
|
|
console.log(err);
|
|
return;
|
|
}
|
|
});
|
|
|
|
console.log("done");
|
|
}
|
|
});
|
|
|
|
|
|
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
});
|
|
// Új ügyfél
|
|
router.get('/clientnew', utils.ensureAuthenticated,function(req,res){
|
|
|
|
Client.find({}, function(err,client)
|
|
{
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
res.send('Hiba történt az oldal betöltése során. ');
|
|
}
|
|
else
|
|
{
|
|
/* client.forEach(element => {
|
|
console.log('Ügyfél: '+element.name);
|
|
});*/
|
|
res.render('client_add', {
|
|
title: 'Új ügyfél',
|
|
manufacturers: client,
|
|
user:req.user,
|
|
//rot:ad,
|
|
//wid:req.params.wid
|
|
//artice: workSign
|
|
});
|
|
}
|
|
});
|
|
});
|
|
// Új ügyfél mentése
|
|
// Add Submit POST Route
|
|
router.post('/add', utils.ensureAuthenticated,function(req,res){
|
|
req.checkBody('Name','Ügyfél megnevezése kötelező!').notEmpty();
|
|
req.checkBody('Address','Cím megadása kötelező!').notEmpty();
|
|
req.checkBody('Adoszam','Adószám gyártási szám kötelező!').notEmpty();
|
|
|
|
// Get errors
|
|
let errors = req.validationErrors();
|
|
|
|
if(errors)
|
|
{
|
|
res.render('client_add', {
|
|
title: 'Új ügyfél',
|
|
//manufacturers: client,
|
|
error:errors,
|
|
user:req.user,
|
|
})
|
|
} else {
|
|
let client=new Client();
|
|
|
|
client.name=req.body.Name;
|
|
client.shortName=req.body.shortName;
|
|
client.author=req.user.id;
|
|
client.manufacturerDiscount=req.body.Kedvezmeny;
|
|
client.adoszam=req.body.Adoszam;
|
|
client.address=req.body.Address;
|
|
client.save(function(err){
|
|
if(err){
|
|
console.log(err);
|
|
return;
|
|
}else{
|
|
req.flash('success','Ügyfél sikeresen felvéve');
|
|
res.redirect('/');
|
|
}
|
|
});
|
|
|
|
}
|
|
});
|
|
|
|
// Ügyfél lista
|
|
// Works Route
|
|
router.get('/find', utils.ensureAuthenticated, function(req, res) {
|
|
var response = {
|
|
status : 200,
|
|
data : 'null'
|
|
}
|
|
Client.find({} ,function(err, clients){
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
}
|
|
else {
|
|
response = {
|
|
status : 200,
|
|
data : clients
|
|
}
|
|
res.send(response);
|
|
|
|
|
|
}
|
|
});
|
|
|
|
});
|
|
// Ügyfél lista
|
|
// Works Route
|
|
router.get('/clients', utils.ensureAuthenticated, function(req, res) {
|
|
var admin=false;
|
|
|
|
/* if(req.user)
|
|
{
|
|
checkUserAdmin2(req,function(err,res){
|
|
admin=res;
|
|
console.log(admin);
|
|
});
|
|
|
|
|
|
// if(req.user.username==='admisJacica') ad=true;
|
|
}*/
|
|
Client.find({} ,function(err, clients){
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
}
|
|
else {
|
|
// lastId=articles.workNumber;
|
|
res.render('client_list', {
|
|
title: 'Ügyfelek',
|
|
clients: clients,
|
|
|
|
// articles: workSign
|
|
});
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
// Ügyfél szerkesztése
|
|
router.get('/edit/:id', utils.ensureAuthenticated,function(req,res){
|
|
Client.findById(req.params.id, function(err,client)
|
|
{
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
res.send('Hiba történt az oldal betöltése során. ');
|
|
}
|
|
else
|
|
{
|
|
res.render('client_edit', {
|
|
title: 'Ügyféladat módosítás',
|
|
client: client,
|
|
|
|
});
|
|
}
|
|
});
|
|
});
|
|
// Meglévő ügyfél mentése
|
|
router.post('/edit/:id', utils.ensureAuthenticated,function(req,res){
|
|
req.checkBody('Name','Ügyfél megnevezése kötelező!').notEmpty();
|
|
req.checkBody('Address','Cím megadása kötelező!').notEmpty();
|
|
req.checkBody('Adoszam','Adószám gyártási szám kötelező!').notEmpty();
|
|
|
|
// Get errors
|
|
let errors = req.validationErrors();
|
|
|
|
if(errors)
|
|
{
|
|
res.render('client_edit', {
|
|
title: 'Ügyféladat módosítás',
|
|
errors:errors
|
|
})
|
|
} else {
|
|
let client={};
|
|
|
|
client.name=req.body.Name;
|
|
client.shortName=req.body.shortName;
|
|
client.author=req.user.id;
|
|
client.manufacturerDiscount=req.body.Kedvezmeny;
|
|
client.adoszam=req.body.Adoszam;
|
|
client.address=req.body.Address;
|
|
|
|
let query = {_id:req.params.id}
|
|
Client.update(query,client,function(err){
|
|
if(err){
|
|
console.log(err);
|
|
return;
|
|
}else{
|
|
req.flash('success','Ügyféladatok sikeresen módosítva');
|
|
res.redirect('/client/clients');
|
|
}
|
|
});
|
|
|
|
}
|
|
});
|
|
|
|
|
|
router.post('/print/:id', utils.ensureAuthenticated,function(req,res){
|
|
var fs = require('fs');
|
|
var PDFDocument = require('pdfkit');
|
|
|
|
Client.findById(req.params.id, function(err,client)
|
|
{
|
|
if (err)
|
|
{
|
|
console.log(err);
|
|
res.send('Hiba történt az oldal betöltése során. ');
|
|
}
|
|
else
|
|
{
|
|
var pdf = new PDFDocument({
|
|
size: 'LEGAL', // See other page sizes here: https://github.com/devongovett/pdfkit/blob/d95b826475dd325fb29ef007a9c1bf7a527e9808/lib/page.coffee#L69
|
|
info: {
|
|
Title: 'Tile of File Here',
|
|
Author: 'Some Author',
|
|
}
|
|
});
|
|
|
|
// Write stuff into PDF
|
|
pdf.text('Ügyfél adatok:');
|
|
pdf.text(client.name);
|
|
// Stream contents to a file
|
|
pdf.pipe(
|
|
fs.createWriteStream('./uploads/file.pdf')
|
|
)
|
|
.on('finish', function () {
|
|
console.log('PDF closed');
|
|
|
|
res.download(path.normalize('./uploads/file.pdf'),'file.pdf');
|
|
});
|
|
|
|
// Close PDF and write file.
|
|
pdf.end();
|
|
/* res.render('client_edit', {
|
|
title: 'Ügyféladat módosítás',
|
|
client: client,
|
|
|
|
});*/
|
|
}
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
module.exports = router;
|
|
|