Form1.cs
14.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Diagnostics;
using Microsoft.Win32;
namespace imprime
{
public partial class Form1 : Form
{
private string fileName;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.url))
url.Text = imprimeFacturas.Properties.Configuraciones.Default.url;
if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.idInicio))
desde.Text = imprimeFacturas.Properties.Configuraciones.Default.idInicio;
if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.idFin))
hasta.Text = imprimeFacturas.Properties.Configuraciones.Default.idFin;
if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe))
ruta.Text = imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe;
if (!string.IsNullOrWhiteSpace(imprimeFacturas.Properties.Configuraciones.Default.accionDescarga))
comboBox1.SelectedIndex = Int32.Parse(imprimeFacturas.Properties.Configuraciones.Default.accionDescarga);
comenzarTimer();
muestro();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = "La aplicacion sigue en abierta";
notifyIcon1.BalloonTipTitle = "Atencion";
//notifyIcon1.Ba
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
MessageBox.Show("v 1.1");
return true; // indicate that you handled this keystroke
}
// Call the base class
return base.ProcessCmdKey(ref msg, keyData);
}
private void button1_Click(object sender, EventArgs e)
{
imprimeFacturas.Properties.Configuraciones.Default.url = url.Text ;
imprimeFacturas.Properties.Configuraciones.Default.idInicio = desde.Text;
imprimeFacturas.Properties.Configuraciones.Default.idFin = hasta.Text;
imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString();
imprimeFacturas.Properties.Configuraciones.Default.Save();
int inicio = int.Parse(desde.Text);
int fin = int.Parse(hasta.Text);
for (int i = inicio; i <= fin; i++) {
string remoteUri = url.Text + "?action=dameFactura&idPedido=" + i;
string myStringWebResource = null;
fileName = "pdf_" + i + ".pdf";
WebClient myWebClient = new WebClient();
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
myWebClient.DownloadFile(myStringWebResource, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
imprimirPdf(fileName);
}
}
protected void imprimirPdf(string fileName)
{
try
{
long length = new System.IO.FileInfo(fileName).Length;
string nomFile = Application.StartupPath + "\\" + fileName;
if (length == 0)
{
System.IO.File.Delete(nomFile);
File.Delete(nomFile);
logAcciones.Text = logAcciones.Text + "Descarga incorrecta: " + fileName + " en ......." + Application.StartupPath + "\n";
}
else {
logAcciones.Text = logAcciones.Text + "Descarga correcta: " + fileName + " en ......." + Application.StartupPath + "\n";
int opcion = comboBox1.SelectedIndex;
if (opcion == 0)
{
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";
String ruta = @"";
//Define location of adobe reader/command line
//switches to launch adobe in "print" mode
proc.StartInfo.FileName = ruta;
proc.StartInfo.Arguments = String.Format(@"/p /h /s {0}", nomFile);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
}
proc.EnableRaisingEvents = true;
proc.Close();
KillAdobe("AcroRd32");
logAcciones.Text = logAcciones.Text + "Documento Impreso: " + fileName ;
notifyIcon1.BalloonTipText = "Fichero" + fileName + " Impreso";
notifyIcon1.BalloonTipTitle = "Atencion";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
}
}
}
catch (Exception fallo)
{
MessageBox.Show("No se ha podid imprimir el archivo: " + fileName + " con error:" + fallo.Message);
}
}
protected bool KillAdobe(string name)
{
foreach (Process clsProcess in Process.GetProcesses().Where(
clsProcess => clsProcess.ProcessName.StartsWith(name)))
{
clsProcess.Kill();
return true;
}
return false;
}
private void comenzarTimer()
{
int opcion = comboBox1.SelectedIndex;
Console.WriteLine("Viendo timer" + opcion);
if (opcion == 2)
{
Console.WriteLine("Activo timer" + opcion);
timer1.Enabled = true;
timer1.Start();
Console.WriteLine("enciendo Timer");
}
else {
Console.WriteLine("Activo timer" + opcion);
timer1.Stop();
timer1.Enabled = false;
Console.WriteLine("Paro Timer");
}
}
private void LoadEstados() {
/* var request = (HttpWebRequest)WebRequest.Create("http://obi.dropwp.com/modules/comunicacionesbyimax/comunicacionesbyimaxAjax.php?action=getEstados");
var response = (HttpWebResponse)request.GetResponse();
string content = string.Empty;
using (var stream = response.GetResponseStream()) {
using (var sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
List<estados> o = JsonConvert.DeserializeObject<List<estados>>(content);
foreach (var objeto in o) {
if(objeto.invoice == "1") {
ComboboxItem item = new ComboboxItem();
item.Text = objeto.name;
item.Value = objeto.id_order_state;
comboBox1.Items.Add(item);
}
}
comboBox1.SelectedIndex = 0;
//MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
}*/
}
private void button2_Click(object sender, EventArgs e)
{
imprimeFacturas.Properties.Configuraciones.Default.url = url.Text;
imprimeFacturas.Properties.Configuraciones.Default.rutaAdobe = ruta.Text;
imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString();
imprimeFacturas.Properties.Configuraciones.Default.Save();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
imprimeFacturas.Properties.Configuraciones.Default.accionDescarga = comboBox1.SelectedIndex.ToString();
imprimeFacturas.Properties.Configuraciones.Default.Save();
comenzarTimer();
muestro();
}
private void muestro() {
int opcion = comboBox1.SelectedIndex;
Console.WriteLine("Viendo timer" + opcion);
if (opcion == 2)
{
label1.Hide();
label2.Hide();
desde.Hide();
hasta.Hide();
}
else
{
label1.Show();
label2.Show();
desde.Show();
hasta.Show();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
string urlTexto = url.Text;
if (!String.IsNullOrEmpty(urlTexto))
{
string remoteUri = url.Text + "?action=dameFacturasPP&down=0";
string i = "0";
using (WebClient wc = new System.Net.WebClient())
{
var json = wc.DownloadString(remoteUri);
IPdf pdf = JsonConvert.DeserializeObject<IPdf>(json);
i = pdf.idPedido.ToString();
}
if (i != "false")
{
remoteUri = url.Text + "?action=dameFacturasPP&down=1";
string myStringWebResource = null;
fileName = "pdf_" + i + ".pdf";
WebClient myWebClient = new WebClient();
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
myWebClient.DownloadFile(myStringWebResource, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
imprimirPdf(fileName);
notifyIcon1.BalloonTipText = "Fichero" + fileName + " Guardado";
notifyIcon1.BalloonTipTitle = "Atencion";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
this.Hide();
}
}
else {
MessageBox.Show("Tiene que existir la url");
}
}
private void autorun_CheckedChanged(object sender, EventArgs e)
{
if (autorun.Checked)
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (!IsStartupItem())
// Add the value in the registry so that the application runs at startup
rkApp.SetValue("My app's name", Application.ExecutablePath.ToString());
Console.WriteLine("Regisrando APP en Inicio" );
}
else {
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (IsStartupItem())
// Remove the value from the registry so that the application doesn't start
rkApp.DeleteValue("My app's name", false);
Console.WriteLine("Borrando APP en Inicio");
}
}
private bool IsStartupItem()
{
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rkApp.GetValue("My app's name") == null)
// The value doesn't exist, the application is not set to run at startup
return false;
else
// The value exists, the application is set to run at startup
return true;
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void menuAbrir_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
private void menuSalir_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
}
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
public class IPdf
{
public string idPedido { get; set; }
}
}