| 115 | | SqlConnection conn = new SqlConnection(ConnString.getConnString().Value); |
| 116 | | |
| 117 | | fAKTURYTableAdapter.Connection = conn; |
| 118 | | fakturA_DETAILSTableAdapter.Connection = conn; |
| 119 | | wplatyTableAdapter.Connection = conn; |
| 120 | | |
| 121 | | conn.Open(); |
| 122 | | |
| 123 | | SqlTransaction transaction = conn.BeginTransaction(); |
| 124 | | fAKTURYTableAdapter.AttachTransaction(transaction); |
| 125 | | fakturA_DETAILSTableAdapter.AttachTransaction(transaction); |
| 126 | | wplatyTableAdapter.AttachTransaction(transaction); |
| 127 | | |
| 128 | | try |
| 129 | | { |
| 130 | | fAKTURYTableAdapter.Update(rEKLAMADataSet.FAKTURY); |
| 131 | | fakturA_DETAILSTableAdapter.Update(rEKLAMADataSet.FAKTURA_DETAILS); |
| 132 | | wplatyTableAdapter.Update(rEKLAMADataSet.Wplaty); |
| 133 | | transaction.Commit(); |
| 134 | | dtpZmianaDaty.Value = DateTime.Today; |
| 135 | | } |
| 136 | | catch (Exception) |
| 137 | | { |
| 138 | | transaction.Rollback(); |
| 139 | | conn.Close(); |
| 140 | | rEKLAMADataSet.FAKTURA_DETAILS.Clear(); |
| 141 | | rEKLAMADataSet.FAKTURY.RemoveFAKTURYRow(naglowekFaktury); |
| 142 | | fAKTURYBindingSource.ResetBindings(false); |
| 143 | | fAKTURYDataGridView.Refresh(); |
| 144 | | throw; |
| 145 | | } |
| 146 | | } |
| 147 | | catch (Exception e2) |
| 148 | | { |
| 149 | | MessageBox.Show("Wyst¹pi³ b³¹d: \n" + e2); |
| | 115 | FakturaHelper.AddInvoice(rEKLAMADataSet, |
| | 116 | fAKTURYTableAdapter, |
| | 117 | fakturA_DETAILSTableAdapter, |
| | 118 | wplatyTableAdapter, |
| | 119 | naglowekFaktury); |
| | 120 | |
| | 121 | dtpZmianaDaty.Value = DateTime.Today; |
| | 122 | } |
| | 123 | catch (Exception ex) |
| | 124 | { |
| | 125 | MessageBox.Show("Wyst¹pi³ b³¹d: \n" + ex); |
| 159 | | } |
| | 136 | |
| | 137 | if (fAKTURYTableAdapter.Connection != null && |
| | 138 | fAKTURYTableAdapter.Connection.State == ConnectionState.Open) |
| | 139 | { |
| | 140 | fAKTURYTableAdapter.Connection.Close(); |
| | 141 | fAKTURYTableAdapter.Connection.Dispose(); |
| | 142 | } |
| | 143 | } |
| | 144 | } |
| | 145 | |
| | 146 | private REKLAMADataSet.FAKTURYRow utworzNaglowekFaktury(REKLAMADataSet.KLIENCIRow klientAgencja, |
| | 147 | SLOWNIKDataSet.AGENCJERow agencja, |
| | 148 | FakturaHelper.PaymentType paymentType) |
| | 149 | { |
| | 150 | REKLAMADataSet.FAKTURYRow naglowekFaktury = (REKLAMADataSet.FAKTURYRow) rEKLAMADataSet.FAKTURY.NewRow(); |
| | 151 | |
| | 152 | naglowekFaktury.NUMER_ROZ = agencjaListBox.SelectedItem.ToString(); |
| | 153 | naglowekFaktury.NUMER = Utils.numerNowejFaktury(naglowekFaktury.NUMER_ROZ, DateTime.Today.Year); |
| | 154 | naglowekFaktury.NUMER_ROK = DateTime.Today.Year; |
| | 155 | |
| | 156 | naglowekFaktury.DATA_WYSTAWIENIA = dtpZmianaDaty.Value != DateTime.Today ? dtpZmianaDaty.Value : DateTime.Today; |
| | 157 | |
| | 158 | naglowekFaktury.idZamowienia = agencja.Id_agencji == (int)FakturaHelper.OfficeId.Poznan ? |
| | 159 | (int)FakturaHelper.OrderId.Poznan : (int)FakturaHelper.OrderId.Katowice; |
| | 160 | |
| | 161 | naglowekFaktury.ID_SPRZEDAWCY = agencja.Id_agencji; |
| | 162 | |
| | 163 | naglowekFaktury.EKSPORT = false; |
| | 164 | |
| | 165 | naglowekFaktury.MIEJSCOWOSC_WYSTAWIENIA = agencjaRedakcja.miasto; |
| | 166 | naglowekFaktury.SPRZEDAWCA_ADRES = agencjaRedakcja.Adres_Fk; |
| | 167 | naglowekFaktury.SPRZEDAWCA_NIP = agencjaRedakcja.NIP; |
| | 168 | |
| | 169 | naglowekFaktury.PODPIS_WYSTAWIL = string.Empty; |
| | 170 | naglowekFaktury.DATA_SPRZEDAZY = naglowekFaktury.DATA_WYSTAWIENIA; |
| | 171 | |
| | 172 | naglowekFaktury.ID_NABYWCY = klientAgencja.CustomerID; |
| | 173 | naglowekFaktury.NABYWCA_ADRES = klientAgencja.Adres_Fkatura; |
| | 174 | naglowekFaktury.NABYWCA_NIP = klientAgencja.Nip; |
| | 175 | naglowekFaktury.opis = string.Empty; |
| | 176 | |
| | 177 | naglowekFaktury.SPOSOB_ZAPLATY = FakturaHelper.GetPaymentType((int) paymentType); |
| | 178 | |
| | 179 | naglowekFaktury.TERMIN_ZAPLATY = naglowekFaktury.DATA_SPRZEDAZY.AddDays(14); |
| | 180 | naglowekFaktury.ZAPLACONO = false; |
| | 181 | |
| | 182 | naglowekFaktury.FAKTURA_TYP = 2; |
| | 183 | naglowekFaktury.FAKTURA_PODTYP = 1; |
| | 184 | |
| | 185 | naglowekFaktury.ID_KONTA = Convert.ToInt16(konto.id); |
| | 186 | |
| | 187 | naglowekFaktury.EndEdit(); |
| | 188 | return naglowekFaktury; |
| | 189 | } |
| | 190 | |
| | 191 | private void utworzPozycjeFaktury(REKLAMADataSet.FAKTURYRow naglowekFaktury, REKLAMADataSet.FAKTURYDataTable naglowkiFakturDlaKlientow, REKLAMADataSet.FAKTURA_DETAILSDataTable pozycjeFakturDlaKlientow) |
| | 192 | { |
| | 193 | |
| | 194 | foreach (REKLAMADataSet.FAKTURA_DETAILSRow pozycjaFaktury in pozycjeFakturDlaKlientow) |
| | 195 | { |
| | 196 | |
| | 197 | REKLAMADataSet.FAKTURYRow faktura = naglowkiFakturDlaKlientow.FindByID_FAKTURY(pozycjaFaktury.ID_FAKTURY); |
| | 198 | |
| | 199 | FakturaHelper.AddInvoiceDetails(naglowekFaktury, faktura, pozycjaFaktury, rEKLAMADataSet.FAKTURA_DETAILS); |
| | 200 | } |
| | 201 | } |
| | 202 | |
| | 203 | private REKLAMADataSet.FAKTURA_DETAILSDataTable GetFakturaDetailsRows(int year, int month, int week, int sellerId) |
| | 204 | { |
| | 205 | return fakturA_DETAILSTableAdapter.GetDataByNrTygodniaWRoku(year, week, sellerId, month); |
| | 206 | } |
| | 207 | |
| | 208 | private REKLAMADataSet.FAKTURYDataTable GetFakturaRows(int year, int month, int week, int sellerId) |
| | 209 | { |
| | 210 | return fAKTURYTableAdapter.GetDataByNrTygodniaWRoku(year, week, sellerId, month); |
| | 211 | |
| | 212 | } |
| | 213 | |
| | 214 | private void dodajWplate(REKLAMADataSet.FAKTURYRow naglowekFaktury) |
| | 215 | { |
| | 216 | REKLAMADataSet.WplatyRow wplata = rEKLAMADataSet.Wplaty.NewWplatyRow(); |
| | 217 | wplata.idFaktury = naglowekFaktury.ID_FAKTURY; |
| | 218 | |
| | 219 | wplata.idZamowienia = naglowekFaktury.ID_SPRZEDAWCY == (int) FakturaHelper.OfficeId.Poznan |
| | 220 | ? (int) FakturaHelper.OrderId.Poznan |
| | 221 | : (int) FakturaHelper.OrderId.Katowice; |
| | 222 | } |
| | 223 | |
| | 224 | private void wydrukButton_Click(object sender, EventArgs e) |
| | 225 | { |
| | 226 | if (fAKTURYBindingSource.Current != null) |
| | 227 | { |
| | 228 | REKLAMADataSet.FAKTURYRow row = |
| | 229 | (REKLAMADataSet.FAKTURYRow) |
| | 230 | ((DataRowView) fAKTURYBindingSource.Current).Row; |
| | 231 | |
| | 232 | if (row.IsidFakturyKorektaNull()) |
| | 233 | { |
| | 234 | FactureViewer fv = new FactureViewer(row.ID_FAKTURY, false); |
| | 235 | fv.ShowDialog(); |
| | 236 | } |
| | 237 | else |
| | 238 | { |
| | 239 | FactureViewer fv = new FactureViewer(row.ID_FAKTURY, row.idFakturyVAT, row.idFakturyKorekta); |
| | 240 | fv.ShowDialog(); |
| | 241 | } |
| | 242 | } |
| | 243 | } |
| | 244 | |
| | 245 | |
| | 246 | private void usunFaktureButton_Click(object sender, EventArgs e) |
| | 247 | { |
| | 248 | if (fAKTURYBindingSource.Current == null) return; |
| | 249 | |
| | 250 | DialogResult dialogResult = MessageBox.Show("Czy na pewno chcesz usun¹æ fakturê?", |
| | 251 | "Faktura - usuwanie", |
| | 252 | MessageBoxButtons.YesNo, |
| | 253 | MessageBoxIcon.Question); |
| | 254 | if (dialogResult != DialogResult.Yes) return; |
| | 255 | |
| | 256 | Cursor = Cursors.WaitCursor; |
| | 257 | |
| | 258 | REKLAMADataSet.FAKTURYRow faktura = (REKLAMADataSet.FAKTURYRow) |
| | 259 | ((DataRowView) fAKTURYBindingSource.Current).Row; |
| | 260 | |
| | 261 | string kod = faktura.NUMER_ROZ; |
| | 262 | |
| | 263 | if (faktura.KOREKTA) |
| | 264 | { |
| | 265 | int i = fAKTURYBindingSource.Find("idFakturyKorekta", faktura.ID_FAKTURY); |
| | 266 | if (i > -1) |
| | 267 | { |
| | 268 | MessageBox.Show("Nie mo¿na usun¹æ faktury"); |
| | 269 | } |
| | 270 | else |
| | 271 | { |
| | 272 | usunKorekte(faktura); |
| | 273 | } |
| | 274 | } |
| | 275 | else |
| | 276 | { |
| | 277 | usunFakture(faktura); |
| | 278 | } |
| | 279 | |
| | 280 | fAKTURYTableAdapter.FillByKodAgenta(rEKLAMADataSet.FAKTURY, kod); |
| | 281 | fAKTURYDataGridView.Refresh(); |
| 290 | | private REKLAMADataSet.FAKTURYRow utworzNaglowekFaktury(REKLAMADataSet.KLIENCIRow klientAgencja, |
| 291 | | SLOWNIKDataSet.AGENCJERow agencja, |
| 292 | | FakturaHelper.PaymentType paymentType) |
| 293 | | { |
| 294 | | REKLAMADataSet.FAKTURYRow naglowekFaktury = (REKLAMADataSet.FAKTURYRow) rEKLAMADataSet.FAKTURY.NewRow(); |
| 295 | | |
| 296 | | naglowekFaktury.NUMER_ROZ = agencjaListBox.SelectedItem.ToString(); |
| 297 | | naglowekFaktury.NUMER = Utils.numerNowejFaktury(naglowekFaktury.NUMER_ROZ, DateTime.Today.Year); |
| 298 | | naglowekFaktury.NUMER_ROK = DateTime.Today.Year; |
| 299 | | naglowekFaktury.DATA_WYSTAWIENIA = DateTime.Today; |
| 300 | | |
| 301 | | if (dtpZmianaDaty.Value != DateTime.Today) |
| 302 | | { |
| 303 | | naglowekFaktury.DATA_WYSTAWIENIA = dtpZmianaDaty.Value; |
| 304 | | } |
| 305 | | |
| 306 | | naglowekFaktury.idZamowienia = agencja.Id_agencji == (int)FakturaHelper.OfficeId.Poznan ? |
| 307 | | (int)FakturaHelper.OrderId.Poznan : (int)FakturaHelper.OrderId.Katowice; |
| 308 | | |
| 309 | | naglowekFaktury.ID_SPRZEDAWCY = agencja.Id_agencji; |
| 310 | | |
| 311 | | naglowekFaktury.EKSPORT = false; |
| 312 | | |
| 313 | | naglowekFaktury.MIEJSCOWOSC_WYSTAWIENIA = agencjaRedakcja.miasto; |
| 314 | | naglowekFaktury.SPRZEDAWCA_ADRES = agencjaRedakcja.Adres_Fk; |
| 315 | | naglowekFaktury.SPRZEDAWCA_NIP = agencjaRedakcja.NIP; |
| 316 | | |
| 317 | | naglowekFaktury.PODPIS_WYSTAWIL = string.Empty; |
| 318 | | naglowekFaktury.DATA_SPRZEDAZY = naglowekFaktury.DATA_WYSTAWIENIA; |
| 319 | | |
| 320 | | naglowekFaktury.ID_NABYWCY = klientAgencja.CustomerID; |
| 321 | | naglowekFaktury.NABYWCA_ADRES = klientAgencja.Adres_Fkatura; |
| 322 | | naglowekFaktury.NABYWCA_NIP = klientAgencja.Nip; |
| 323 | | naglowekFaktury.opis = string.Empty; |
| 324 | | |
| 325 | | naglowekFaktury.SPOSOB_ZAPLATY = FakturaHelper.GetPaymentType((int) paymentType); |
| 326 | | |
| 327 | | naglowekFaktury.TERMIN_ZAPLATY = naglowekFaktury.DATA_SPRZEDAZY.AddDays(14); |
| 328 | | naglowekFaktury.ZAPLACONO = false; |
| 329 | | |
| 330 | | naglowekFaktury.FAKTURA_TYP = 2; |
| 331 | | naglowekFaktury.FAKTURA_PODTYP = 1; |
| 332 | | |
| 333 | | naglowekFaktury.ID_KONTA = Convert.ToInt16(konto.id); |
| 334 | | |
| 335 | | naglowekFaktury.EndEdit(); |
| 336 | | return naglowekFaktury; |
| 337 | | } |
| 338 | | |
| 339 | | private void utworzPozycjeFaktury(REKLAMADataSet.FAKTURYRow naglowekFaktury, REKLAMADataSet.FAKTURYDataTable naglowkiFakturDlaKlientow, REKLAMADataSet.FAKTURA_DETAILSDataTable pozycjeFakturDlaKlientow) |
| 340 | | { |
| 341 | | |
| 342 | | foreach (REKLAMADataSet.FAKTURA_DETAILSRow pozycjaFaktury in pozycjeFakturDlaKlientow) |
| 343 | | { |
| 344 | | |
| 345 | | REKLAMADataSet.FAKTURYRow faktura = naglowkiFakturDlaKlientow.FindByID_FAKTURY(pozycjaFaktury.ID_FAKTURY); |
| 346 | | |
| 347 | | AddFakturaDetails(naglowekFaktury, faktura, pozycjaFaktury, rEKLAMADataSet.FAKTURA_DETAILS); |
| 348 | | } |
| 349 | | } |
| 350 | | |
| 351 | | private REKLAMADataSet.FAKTURA_DETAILSDataTable GetFakturaDetailsRows(int year, int month, int week, int sellerId) |
| 352 | | { |
| 353 | | return fakturA_DETAILSTableAdapter.GetDataByNrTygodniaWRoku(year, week, sellerId, month); |
| 354 | | } |
| 355 | | |
| 356 | | private REKLAMADataSet.FAKTURYDataTable GetFakturaRows(int year, int month, int week, int sellerId) |
| 357 | | { |
| 358 | | return fAKTURYTableAdapter.GetDataByNrTygodniaWRoku(year, week, sellerId, month); |
| 359 | | |
| 360 | | } |
| 361 | | |
| 362 | | private void AddFakturaDetails(REKLAMADataSet.FAKTURYRow newInvoice, REKLAMADataSet.FAKTURYRow clientInvoice, REKLAMADataSet.FAKTURA_DETAILSRow detailsRow, REKLAMADataSet.FAKTURA_DETAILSDataTable detailsTable) |
| 363 | | { |
| 364 | | REKLAMADataSet.FAKTURA_DETAILSRow row = detailsTable.NewFAKTURA_DETAILSRow(); |
| 365 | | |
| 366 | | row.ROK = detailsRow.ROK; |
| 367 | | row.MIESIAC = detailsRow.MIESIAC; |
| 368 | | row.TYTUL = detailsRow.TYTUL; |
| 369 | | row.NAZWA_USLUGI = clientInvoice != null ? clientInvoice.NUMER + "/" + clientInvoice.NUMER_ROZ + "/" + clientInvoice.NUMER_ROK + " | " + detailsRow.NAZWA_USLUGI : ""; |
| 370 | | row.reklamaId = detailsRow.reklamaId; |
| 371 | | row.NR_WYDANIA = detailsRow.NR_WYDANIA; |
| 372 | | |
| 373 | | row.CENA_JEDN = detailsRow.CENA_JEDN; |
| 374 | | row.ILOSC = 1; |
| 375 | | row.JM = "szt."; |
| 376 | | |
| 377 | | row.UPUST_NETTO = detailsRow.UPUST_NETTO; |
| 378 | | row.UPUST_PR = detailsRow.UPUST_PR; |
| 379 | | row.NETTO = detailsRow.NETTO; |
| 380 | | row.S_VAT = VatHelper.PL22; |
| 381 | | row.VAT = (decimal)VatHelper.PL22 * detailsRow.NETTO; |
| 382 | | row.BRUTTO = detailsRow.NETTO * (decimal)(1 + VatHelper.PL22); |
| 383 | | |
| 384 | | row.TYP = 2; |
| 385 | | row.PODTYP = 1; |
| 386 | | row.ID_FAKTURY = newInvoice.ID_FAKTURY; |
| 387 | | |
| 388 | | row.wyroznienie = (!detailsRow.IswyroznienieNull() && detailsRow.wyroznienie); |
| 389 | | |
| 390 | | detailsTable.AddFAKTURA_DETAILSRow(row); |
| 391 | | |
| 392 | | } |
| 393 | | |
| 453 | | private string GetNazwaUslugiPozKat(REKLAMADataSet.FAKTURYRow faktura, REKLAMADataSet.FAKTURA_DETAILSRow pozycjaFaktury) |
| 454 | | { |
| 455 | | if (faktura == null) return string.Empty; |
| 456 | | |
| 457 | | return faktura.NUMER + "/" + faktura.NUMER_ROZ + "/" + faktura.NUMER_ROK + " | " + pozycjaFaktury.NAZWA_USLUGI; |
| 458 | | } |
| 459 | | |
| 460 | | |
| 461 | | private void dodajWplate(REKLAMADataSet.FAKTURYRow naglowekFaktury) |
| 462 | | { |
| 463 | | REKLAMADataSet.WplatyRow wplata = rEKLAMADataSet.Wplaty.NewWplatyRow(); |
| 464 | | wplata.idFaktury = naglowekFaktury.ID_FAKTURY; |
| 465 | | |
| 466 | | if (naglowekFaktury.ID_SPRZEDAWCY == (int) FakturaHelper.OfficeId.Poznan) |
| 467 | | wplata.idZamowienia = (int) FakturaHelper.OrderId.Poznan; |
| 468 | | else |
| 469 | | wplata.idZamowienia = (int) FakturaHelper.OrderId.Katowice; |
| 470 | | } |
| 471 | | |
| 472 | | private void wydrukButton_Click(object sender, EventArgs e) |
| 473 | | { |
| 474 | | if (fAKTURYBindingSource.Current != null) |
| 475 | | { |
| 476 | | REKLAMADataSet.FAKTURYRow row = |
| 477 | | (REKLAMADataSet.FAKTURYRow) |
| 478 | | ((DataRowView) fAKTURYBindingSource.Current).Row; |
| 479 | | |
| 480 | | if (row.IsidFakturyKorektaNull()) |
| 481 | | { |
| 482 | | FactureViewer fv = new FactureViewer(row.ID_FAKTURY, false); |
| 483 | | fv.ShowDialog(); |
| 484 | | } |
| 485 | | else |
| 486 | | { |
| 487 | | FactureViewer fv = new FactureViewer(row.ID_FAKTURY, row.idFakturyVAT, row.idFakturyKorekta); |
| 488 | | fv.ShowDialog(); |
| 489 | | } |
| 490 | | } |
| 491 | | } |
| 492 | | |
| 493 | | private void usunFaktureButton_Click(object sender, EventArgs e) |
| 494 | | { |
| 495 | | if (fAKTURYBindingSource.Current == null) return; |
| 496 | | |
| 497 | | DialogResult dialogResult = MessageBox.Show("Czy na pewno chcesz usun¹æ fakturê?", |
| 498 | | "Faktura - usuwanie", |
| 499 | | MessageBoxButtons.YesNo, |
| 500 | | MessageBoxIcon.Question); |
| 501 | | if (dialogResult != DialogResult.Yes) return; |
| 502 | | |
| 503 | | Cursor = Cursors.WaitCursor; |
| 504 | | |
| 505 | | REKLAMADataSet.FAKTURYRow faktura = (REKLAMADataSet.FAKTURYRow) |
| 506 | | ((DataRowView) fAKTURYBindingSource.Current).Row; |
| 507 | | |
| 508 | | string kod = faktura.NUMER_ROZ; |
| 509 | | |
| 510 | | if (faktura.KOREKTA) |
| 511 | | { |
| 512 | | int i = fAKTURYBindingSource.Find("idFakturyKorekta", faktura.ID_FAKTURY); |
| 513 | | if (i > -1) |
| 514 | | { |
| 515 | | MessageBox.Show("Nie mo¿na usun¹æ faktury"); |
| 516 | | } |
| 517 | | else |
| 518 | | { |
| 519 | | usunKorekte(faktura); |
| 520 | | } |
| 521 | | } |
| 522 | | else |
| 523 | | { |
| 524 | | usunFakture(faktura); |
| 525 | | } |
| 526 | | |
| 527 | | fAKTURYTableAdapter.FillByKodAgenta(rEKLAMADataSet.FAKTURY, kod); |
| 528 | | fAKTURYDataGridView.Refresh(); |
| 529 | | |
| 530 | | Cursor = Cursors.Default; |
| 531 | | } |