| [415] | 1 | using System;
|
|---|
| 2 | using System.ComponentModel;
|
|---|
| 3 | using System.Data;
|
|---|
| 4 | using System.Data.SqlClient;
|
|---|
| [482] | 5 | using System.Text;
|
|---|
| [415] | 6 | using System.Windows.Forms;
|
|---|
| 7 | using ExportFaktur.EXPODataSetTableAdapters;
|
|---|
| 8 | using ExportFaktur.CDNDataSetTableAdapters;
|
|---|
| 9 |
|
|---|
| 10 | namespace ExportFaktur
|
|---|
| 11 | {
|
|---|
| 12 | public partial class ExportForm : Form
|
|---|
| 13 | {
|
|---|
| 14 | private ExpoToCDNExport ex = new ExpoToCDNExport();
|
|---|
| 15 |
|
|---|
| 16 | KLIENCITableAdapter klientAdapter = new KLIENCITableAdapter();
|
|---|
| 17 | FAKTURA_DETAILSTableAdapter fakturyDetailsAdapter = new FAKTURA_DETAILSTableAdapter();
|
|---|
| [834] | 18 |
|
|---|
| [415] | 19 | KontrahenciTableAdapter kontrahenciAdapter = new KontrahenciTableAdapter();
|
|---|
| 20 | TraElemTableAdapter TraElemAdapter = new TraElemTableAdapter();
|
|---|
| 21 | TraNagTableAdapter TraNagAdapter = new TraNagTableAdapter();
|
|---|
| 22 | TraVatTableAdapter TraVatAdapter = new TraVatTableAdapter();
|
|---|
| 23 |
|
|---|
| [479] | 24 | SqlConnection conn = new SqlConnection(ConnString.CDNConnection);
|
|---|
| 25 | SqlConnection bazaReklamConn = new SqlConnection(ConnString.BazaReklamConnection);
|
|---|
| [415] | 26 |
|
|---|
| 27 | int nMaxProgress;
|
|---|
| [834] | 28 |
|
|---|
| [415] | 29 | public ExportForm()
|
|---|
| 30 | {
|
|---|
| 31 | InitializeComponent();
|
|---|
| 32 |
|
|---|
| 33 | ex.CDNDataset = new CDNDataSet();
|
|---|
| 34 |
|
|---|
| 35 | klientAdapter.ClearBeforeFill = true;
|
|---|
| 36 | fakturyDetailsAdapter.ClearBeforeFill = true;
|
|---|
| 37 |
|
|---|
| [479] | 38 | fAKTURYTableAdapter.Connection = bazaReklamConn;
|
|---|
| 39 | klientAdapter.Connection = bazaReklamConn;
|
|---|
| 40 | fakturyDetailsAdapter.Connection = bazaReklamConn;
|
|---|
| [415] | 41 |
|
|---|
| 42 | kontrahenciAdapter.Connection = conn;
|
|---|
| 43 | TraNagAdapter.Connection = conn;
|
|---|
| 44 | TraElemAdapter.Connection = conn;
|
|---|
| 45 | TraVatAdapter.Connection = conn;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | private void ExportForm_Load(object sender, EventArgs e)
|
|---|
| 49 | {
|
|---|
| [834] | 50 | RefreshView();
|
|---|
| [415] | 51 | }
|
|---|
| 52 |
|
|---|
| [834] | 53 | private void RefreshView()
|
|---|
| [415] | 54 | {
|
|---|
| [479] | 55 | fAKTURYTableAdapter.FillByNotExported(eXPODataSet.FAKTURY);
|
|---|
| [415] | 56 |
|
|---|
| 57 | statusLabel1.Text = "Liczba faktur " + eXPODataSet.FAKTURY.Count;
|
|---|
| 58 |
|
|---|
| 59 | nMaxProgress = eXPODataSet.FAKTURY.Count;
|
|---|
| [834] | 60 |
|
|---|
| [415] | 61 | toolStripProgressBar1.Value = 0;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | private void toolStripButton1_Click(object sender, EventArgs e)
|
|---|
| 65 | {
|
|---|
| 66 | dataGridView1.EndEdit();
|
|---|
| 67 | dataGridView1.Refresh();
|
|---|
| [834] | 68 |
|
|---|
| [415] | 69 | if (backgroundWorker1.IsBusy)
|
|---|
| 70 | {
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (fAKTURYBindingSource.List.Count == 0)
|
|---|
| 75 | {
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | backgroundWorker1.RunWorkerAsync();
|
|---|
| 80 |
|
|---|
| [834] | 81 |
|
|---|
| [415] | 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
|
|---|
| 85 | {
|
|---|
| 86 | int n = 0;
|
|---|
| [834] | 87 |
|
|---|
| [415] | 88 | conn.Open();
|
|---|
| [834] | 89 |
|
|---|
| [415] | 90 | foreach (EXPODataSet.FAKTURYRow row in eXPODataSet.FAKTURY.Rows)
|
|---|
| 91 | {
|
|---|
| 92 | if (!backgroundWorker1.CancellationPending)
|
|---|
| [834] | 93 | {
|
|---|
| 94 |
|
|---|
| [415] | 95 | if (row.Exportuj)
|
|---|
| 96 | {
|
|---|
| 97 | SqlTransaction transaction = conn.BeginTransaction();
|
|---|
| 98 |
|
|---|
| 99 | kontrahenciAdapter.AttachTransaction(transaction);
|
|---|
| 100 | TraNagAdapter.AttachTransaction(transaction);
|
|---|
| 101 | TraElemAdapter.AttachTransaction(transaction);
|
|---|
| 102 | TraVatAdapter.AttachTransaction(transaction);
|
|---|
| 103 |
|
|---|
| 104 | try
|
|---|
| 105 | {
|
|---|
| 106 | klientAdapter.FillByCustomerId(eXPODataSet.KLIENCI, Convert.ToInt32(row["id_nabywcy"]));
|
|---|
| 107 | fakturyDetailsAdapter.FillByIdFaktury(eXPODataSet.FAKTURA_DETAILS, Convert.ToInt32(row["id_faktury"]));
|
|---|
| 108 |
|
|---|
| 109 | ex.DodajKlienta(eXPODataSet.KLIENCI[0]);
|
|---|
| 110 | kontrahenciAdapter.Update(ex.CDNDataset.Kontrahenci);
|
|---|
| 111 |
|
|---|
| 112 | decimal sumaNetto = eXPODataSet.sumaNETTO();
|
|---|
| 113 | decimal vat = eXPODataSet.stawkaVAT();
|
|---|
| 114 | decimal rabat = eXPODataSet.rabatProcent();
|
|---|
| 115 |
|
|---|
| 116 | ex.DodajNaglowekFaktury(row, ex.CDNDataset.Kontrahenci[0], sumaNetto, vat, rabat);
|
|---|
| 117 | ex.DodajElementyFaktury(eXPODataSet.FAKTURA_DETAILS, ex.CDNDataset.TraNag[0]);
|
|---|
| 118 | ex.DodajVAT(ex.CDNDataset.TraNag[0], sumaNetto, vat);
|
|---|
| 119 |
|
|---|
| 120 | TraNagAdapter.Update(ex.CDNDataset.TraNag);
|
|---|
| 121 | TraElemAdapter.Update(ex.CDNDataset.TraElem);
|
|---|
| 122 | TraVatAdapter.Update(ex.CDNDataset.TraVat);
|
|---|
| 123 |
|
|---|
| 124 | transaction.Commit();
|
|---|
| 125 | row.EXPORTED = true;
|
|---|
| 126 | row.EXPORTED_DATA = DateTime.Now;
|
|---|
| 127 | fAKTURYTableAdapter.Update(row);
|
|---|
| 128 |
|
|---|
| 129 | n++;
|
|---|
| 130 | backgroundWorker1.ReportProgress(n);
|
|---|
| 131 | }
|
|---|
| [479] | 132 | catch (Exception exception)
|
|---|
| [415] | 133 | {
|
|---|
| 134 | transaction.Rollback();
|
|---|
| [834] | 135 |
|
|---|
| [482] | 136 | //Send Email
|
|---|
| 137 | StringBuilder stringBuilder = new StringBuilder();
|
|---|
| 138 | stringBuilder.AppendLine("Wyjatek przy imporcie faktury o id: " + row.ID_FAKTURY);
|
|---|
| 139 | stringBuilder.AppendLine("Szczegoly:");
|
|---|
| 140 | stringBuilder.AppendLine(exception.ToString());
|
|---|
| [834] | 141 |
|
|---|
| 142 | conn.Close();
|
|---|
| 143 | bazaReklamConn.Close();
|
|---|
| 144 |
|
|---|
| [482] | 145 | EmailSender.Send(stringBuilder.ToString());
|
|---|
| [834] | 146 |
|
|---|
| [415] | 147 | throw;
|
|---|
| 148 | }
|
|---|
| 149 | finally
|
|---|
| 150 | {
|
|---|
| 151 | eXPODataSet.KLIENCI.Clear();
|
|---|
| 152 | eXPODataSet.FAKTURA_DETAILS.Clear();
|
|---|
| 153 | ex.CDNDataset.TraVat.Clear();
|
|---|
| 154 | ex.CDNDataset.TraElem.Clear();
|
|---|
| 155 | ex.CDNDataset.TraNag.Clear();
|
|---|
| 156 | ex.CDNDataset.Kontrahenci.Clear();
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | else
|
|---|
| 161 | {
|
|---|
| 162 | conn.Close();
|
|---|
| [834] | 163 | e.Cancel = true;
|
|---|
| [415] | 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| [479] | 167 | if (bazaReklamConn.State == ConnectionState.Open)
|
|---|
| 168 | {
|
|---|
| 169 | bazaReklamConn.Close();
|
|---|
| 170 | }
|
|---|
| [415] | 171 | if (conn.State == ConnectionState.Open)
|
|---|
| 172 | {
|
|---|
| 173 | conn.Close();
|
|---|
| 174 | }
|
|---|
| [834] | 175 |
|
|---|
| [415] | 176 | }
|
|---|
| 177 |
|
|---|
| 178 | private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|---|
| 179 | {
|
|---|
| [479] | 180 | if (!Visible || WindowState != FormWindowState.Normal)
|
|---|
| [415] | 181 | {
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| [834] | 184 |
|
|---|
| [415] | 185 | if (toolStripProgressBar1.Maximum != nMaxProgress)
|
|---|
| 186 | {
|
|---|
| 187 | toolStripProgressBar1.Maximum = nMaxProgress;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if (e.ProgressPercentage < toolStripProgressBar1.Maximum)
|
|---|
| 191 | {
|
|---|
| 192 | toolStripProgressBar1.Value = e.ProgressPercentage;
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|---|
| [834] | 197 | {
|
|---|
| [415] | 198 | if (e.Cancelled)
|
|---|
| 199 | {
|
|---|
| 200 | statusLabel1.Text = "Export faktur anulowany";
|
|---|
| 201 | MessageBox.Show("Export faktur anulowany");
|
|---|
| 202 | }
|
|---|
| 203 | else if (e.Error != null)
|
|---|
| 204 | {
|
|---|
| 205 | statusLabel1.Text = "Wyst¹pi³ b³¹d.";
|
|---|
| 206 | MessageBox.Show("Wyst¹pi³ b³¹d: " + e.Error.Message);
|
|---|
| 207 | }
|
|---|
| 208 | else
|
|---|
| 209 | {
|
|---|
| 210 | toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
|
|---|
| 211 | statusLabel1.Text = "Export faktur zakoñczony";
|
|---|
| 212 | MessageBox.Show("Export faktur zakoñczony");
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| [834] | 215 | RefreshView();
|
|---|
| [415] | 216 | }
|
|---|
| 217 |
|
|---|
| 218 | private void toolStripButton2_Click(object sender, EventArgs e)
|
|---|
| 219 | {
|
|---|
| 220 | if (backgroundWorker1.IsBusy)
|
|---|
| [834] | 221 | {
|
|---|
| [415] | 222 | backgroundWorker1.CancelAsync();
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | private void refreshToolStripButton_Click(object sender, EventArgs e)
|
|---|
| 227 | {
|
|---|
| 228 | if (!backgroundWorker1.IsBusy)
|
|---|
| 229 | {
|
|---|
| [834] | 230 | RefreshView();
|
|---|
| [415] | 231 | }
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | private void ExportForm_FormClosing(object sender, FormClosingEventArgs e)
|
|---|
| 235 | {
|
|---|
| 236 | if (backgroundWorker1.IsBusy)
|
|---|
| 237 | {
|
|---|
| 238 | return;
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
|
|---|
| 243 | {
|
|---|
| 244 |
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| [479] | 247 | private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
|
|---|
| [415] | 248 | {
|
|---|
| [479] | 249 | cbSelectAll.Text = cbSelectAll.Checked ? "Odznacz wszystko" : "Zaznacz wszystko";
|
|---|
| 250 |
|
|---|
| 251 | foreach (EXPODataSet.FAKTURYRow row in eXPODataSet.FAKTURY.Rows)
|
|---|
| 252 | {
|
|---|
| 253 | row.Exportuj = cbSelectAll.Checked;
|
|---|
| 254 | }
|
|---|
| [415] | 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | } |
|---|