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