private void btnPrint_Click(object sender, EventArgs e) { String opdrachtenInhoud = ""; decimal totalAmount = 0; foreach (DataGridViewRow row in facturenGridView.Rows) { if ((Boolean)row.Cells[0].Value != false) { int opdrachtID = 0; if (!int.TryParse(row.Cells[1].Value.ToString(), out opdrachtID)) { continue; } opdracht op = OpdrachtManagement.getOpdracht(opdrachtID); opdracht_factuur of = FactuurManagement.getFactuurVanOpdracht(op); foreach (opdracht_factuur_detail ofd in FactuurManagement.getFactuurDetails(of)) { opdrachtenInhoud += of.opdracht_id + "\t"; opdrachtenInhoud += ofd.omschrijving + "\t"; opdrachtenInhoud += ofd.bedrag_basis + "\t"; opdrachtenInhoud += ofd.btw_percent + "\t"; opdrachtenInhoud += ofd.btw_bedrag + "\t"; opdrachtenInhoud += ofd.bedrag_inclusief + "\t"; opdrachtenInhoud += System.Environment.NewLine; totalAmount += ofd.bedrag_inclusief.Value; FactuurNummering nummer = new FactuurNummering(); nummer.bedrijf = opdracht.FactuurNummering.bedrijf; nummer.FactuurNr = opdracht.FactuurNummering.FactuurNr; nummer.FactuurJaar = opdracht.FactuurNummering.FactuurJaar; op.FactuurNummering = nummer; Backend.DataContext.dc.SubmitChanges(); } //opdrachtenInhoud += row.Cells[1].Value + "\t" + row.Cells[2].Value + "\t" + row.Cells[4].Value + "\t" + row.Cells[5].Value + "\t€" + row.Cells[6].Value + System.Environment.NewLine; //totalAmount += Convert.ToDouble(row.Cells[6].Value.ToString()); } } if (totalAmount > 0) { locatie adres = KlantManagement.getAdresVanKlant(opdracht.klant.klant_id); //Convert date to acceptable format for use in file name String datum = DateTime.Today.ToString("yyyy-MM-dd"); //missing oject to use with various word commands object missing = System.Reflection.Missing.Value; //the template file you will be using object fileToOpen = (object)@"R:\CarGo\opdracht_template.docx"; //Where the new file will be saved to. object fileToSave = (object)@"R:\CarGo\opdrachten\opdracht_" + opdracht.klant.naam + "_" + datum + ".docx"; //Create new instance of word and create a new document Word.Application wordApp = new Word.Application(); Word.Document doc = null; //Properties for the new word document object readOnly = false; object isVisible = false; //Settings the application to invisible, so the user doesn't notice that anything is going on wordApp.Visible = false; try { try { File.Copy(fileToOpen.ToString(), fileToSave.ToString(), true); } catch { MessageBox.Show("Loading the template file failed.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } readOnly = false; doc = wordApp.Documents.Open(ref fileToSave, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); } catch { } PrintManagement.findAndReplace(doc, "factuurdatum", datum); PrintManagement.findAndReplace(doc, "factuurnummer", opdracht.FactuurNummering.FactuurNr + " " + opdracht.FactuurNummering.FactuurJaar); PrintManagement.findAndReplace(doc, "startOfDocument", opdrachtenInhoud); PrintManagement.findAndReplace(doc, "totaal", "Totaal: €" + totalAmount); Console.WriteLine(opdrachtenInhoud); doc.Save(); doc.Activate(); //Making word visible to be able to show the print preview. wordApp.Visible = true; wordApp.Activate(); //doc.PrintPreview(); } else { MessageBox.Show("Onvoldoende gegevens om factuur op te stellen. Kijk Detail Facturatie na", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }
private void btnPrintCredit_Click(object sender, EventArgs e) { String opdrachtenInhoud = ""; decimal totalAmount = 0; if (cbbID.SelectedItem == null) { return; } opdracht op = (opdracht)cbbID.SelectedItem; if (op.FactuurNummering1 == null) { return; } if (op.FactuurNummering1.FactuurJaar == null || op.FactuurNummering1.FactuurNr == null) { return; } opdracht_factuur of = FactuurManagement.getFactuurVanOpdracht(op); opdrachtenInhoud += of.opdracht_id + "\t"; opdrachtenInhoud += of.credit_omschrijving + "\t"; opdrachtenInhoud += of.credit_basis + "\t"; opdrachtenInhoud += of.credit_btwpercent + "\t"; opdrachtenInhoud += of.credit_btwbedrag + "\t"; opdrachtenInhoud += of.credit_inc + "\t"; opdrachtenInhoud += System.Environment.NewLine; totalAmount += of.credit_inc.Value; //opdrachtenInhoud += row.Cells[1].Value + "\t" + row.Cells[2].Value + "\t" + row.Cells[4].Value + "\t" + row.Cells[5].Value + "\t€" + row.Cells[6].Value + System.Environment.NewLine; //totalAmount += Convert.ToDouble(row.Cells[6].Value.ToString()); if (totalAmount > 0) { locatie adres = KlantManagement.getAdresVanKlant(opdracht.klant.klant_id); //Convert date to acceptable format for use in file name String datum = DateTime.Today.ToString("yyyy-MM-dd"); //missing oject to use with various word commands object missing = System.Reflection.Missing.Value; //the template file you will be using //object fileToOpen = (object)@"R:\CarGo\CreditNota_template.docx"; object fileToOpen = (object)@"R:\CarGo\opdracht_template.docx"; //Where the new file will be saved to. object fileToSave = (object)@"R:\CarGo\opdrachten\Credit_" + opdracht.klant.naam + "_" + datum + ".docx"; //Create new instance of word and create a new document Word.Application wordApp = new Word.Application(); Word.Document doc = null; //Properties for the new word document object readOnly = false; object isVisible = false; //Settings the application to invisible, so the user doesn't notice that anything is going on wordApp.Visible = false; try { try { File.Copy(fileToOpen.ToString(), fileToSave.ToString(), true); } catch { MessageBox.Show("Loading the template file failed.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } readOnly = false; doc = wordApp.Documents.Open(ref fileToSave, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); } catch { } PrintManagement.findAndReplace(doc, "factuurdatum", datum); PrintManagement.findAndReplace(doc, "factuurnummer", opdracht.FactuurNummering1.FactuurNr + " " + opdracht.FactuurNummering1.FactuurJaar); PrintManagement.findAndReplace(doc, "startOfDocument", opdrachtenInhoud); PrintManagement.findAndReplace(doc, "totaal", "Totaal: €" + totalAmount); Console.WriteLine(opdrachtenInhoud); doc.Save(); doc.Activate(); //Making word visible to be able to show the print preview. wordApp.Visible = true; wordApp.Activate(); //doc.PrintPreview(); } else { MessageBox.Show("Credit bedrag moet groter dan 0 zijn.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); } }
private void btnPrint_Click(object sender, EventArgs e) { //opdracht ophalen opdracht opdracht = (opdracht)cbbID.SelectedItem; if (opdracht == null) { MainForm.updateStatus = "Er is geen factuur geselecteerd, selecteer een opdracht en probeer dan opnieuw."; } else { //adres ophalen van klant locatie adres = KlantManagement.getAdresVanKlant(opdracht.klant.klant_id); //Convert date to acceptable format for use in file name DateTime begindatum = (DateTime)dtPeriodeBegin2.Value; String datum1 = begindatum.ToString("yyyy-MM-dd"); DateTime einddatum = (DateTime)dtPeriodeEinde2.Value; String datum2 = einddatum.ToString("yyyy-MM-dd"); //missing oject to use with various word commands object missing = System.Reflection.Missing.Value; //the template file you will be using object fileToOpen = (object)@"R:\CarGo\factuur_contract_template.docx"; //Where the new file will be saved to. object fileToSave = (object)@"R:\CarGo\contractfacturen\factuur_" + opdracht.klant.naam + "_" + datum1 + "-" + datum2 + ".docx"; //Create new instance of word and create a new document Word.Application wordApp = new Word.Application(); Word.Document doc = null; //Properties for the new word document object readOnly = false; object isVisible = false; //Settings the application to invisible, so the user doesn't notice that anything is going on wordApp.Visible = false; try { File.Copy(fileToOpen.ToString(), fileToSave.ToString(), true); } catch { MainForm.updateStatus = "Kan nieuw document niet opslaan"; return; } doc = wordApp.Documents.Open(ref fileToSave, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); //Search for bookmarks and replace them with the text you want PrintManagement.findAndReplace(doc, "naam_bedrijf", opdracht.klant.naam); PrintManagement.findAndReplace(doc, "straat_bedrijf", adres.straat); PrintManagement.findAndReplace(doc, "huisnummer_bedrijf", adres.nr); PrintManagement.findAndReplace(doc, "postcode_bedrijf", adres.postcode); PrintManagement.findAndReplace(doc, "gemeente_bedrijf", adres.plaats); PrintManagement.findAndReplace(doc, "id", opdracht.factuur_id_full); PrintManagement.findAndReplace(doc, "telefoon_klant", opdracht.klant.telefoon); PrintManagement.findAndReplace(doc, "fax_klant", opdracht.klant.fax); PrintManagement.findAndReplace(doc, "contractnummer", opdracht.contract_id_full); PrintManagement.findAndReplace(doc, "periode_begin1", datum1); PrintManagement.findAndReplace(doc, "periode_tot1", datum2); PrintManagement.findAndReplace(doc, "omschrijving", opdracht.ritomschrijving); PrintManagement.findAndReplace(doc, "vertrek", cbbVertrek.Text.ToString()); PrintManagement.findAndReplace(doc, "bestemming", cbbBestemming.Text.ToString()); PrintManagement.findAndReplace(doc, "aantal_plaatsen", opdracht.aantal_personen); PrintManagement.findAndReplace(doc, "opstap1", ContractManagement.getLocatie(opdracht.opdracht_id, "opstap_1").FullAdress); PrintManagement.findAndReplace(doc, "opstap2", ContractManagement.getLocatie(opdracht.opdracht_id, "opstap_1").FullAdress); PrintManagement.findAndReplace(doc, "dagprijs", opdracht.contract_dagprijs.ToString()); PrintManagement.findAndReplace(doc, "saldo", txtPrijs.Text); ////Tabel invullen, lukte mij niet //Word.Tables tables = doc.Tables; //if (tables.Count > 0) //{ // //Eerste tabel eruit halen // Word.Table table = tables[2]; // int rowsCount = table.Rows.Count; // int coulmnsCount = table.Columns.Count; // foreach (contract_rit rit in ContractManagement.getRitten(opdracht.opdracht_id)) // { // Word.Row row = table.Rows.Add(ref missing); // for (int j = 1; j <= coulmnsCount; j++) // { // row.Cells[j].Range.Text = string.Format("{0} : {1}", ofd.omschrijving, ofd.bedrag_inclusief.ToString()); // row.Cells[j].WordWrap = true; // row.Cells[j].Range.Underline = Word.WdUnderline.wdUnderlineNone; // row.Cells[j].Range.Bold = 0; // } // } //} //rest aan te vullen... doc.Save(); doc.Activate(); //Making word visible to be able to show the print preview. wordApp.Visible = true; //Close the document and the application (otherwise the process will keep running) /*doc.Close(ref missing, ref missing, ref missing); * wordApp.Quit(ref missing, ref missing, ref missing);*/ } }
//EditTim: Printen van alle ritten van de gekozen chauffeur op de gekozen datum private void button1_Click(object sender, EventArgs e) { DateTime date = dateTimePicker1.Value.Date; //alle informatie van contracten ophalen IEnumerable <rit_instantie> ritten = ContractManagement.getRitten(date); foreach (rit_instantie ri in ritten) { opdracht od = ContractManagement.getContract(ri.contract_rit); if (date.ToString("dddd", new CultureInfo("en-US")) == (ri.contract_rit.dag)) { var idfull = od.contract_id_full; var ritnummer = od.ritomschrijving; var rit1vertr = ri.contract_rit.rit1_vertrek; var rit2vertr = ri.contract_rit.rit2_vertrek; var rit1terug = ri.contract_rit.rit1_terug; var rit2terug = ri.contract_rit.rit2_terug; if (ContractManagement.hasRitInfo(ri)) { rit_info info = ContractManagement.getRitInfo(ri); chauffeurnaam = info.chauffeur.naam; voertuignaam1 = info.voertuig.identificatie; voertuignaam2 = info.voertuig1.identificatie; } var vertrek = OpdrachtManagement.getVertrek(od.opdracht_id); var bestemming = OpdrachtManagement.getBestemming(od.opdracht_id); if (chauffeurnaam.ToString() == cbxChauffeurs.SelectedItem.ToString()) { //missing oject to use with various word commands object missing = System.Reflection.Missing.Value; //the template file you will be using object fileToOpen = (object)@"R:\CarGo\ritblad_chauffeur_CONTRACT.docx"; //object fileToOpen = (object)@"\\172.16.10.2\Users\jeroen\CarGo\ritblad_template.docx"; //Where the new file will be saved to. object fileToSave = (object)@"R:\CarGo\printouts\ritblad_chauffeur_CONTRACT" + "" + DateTime.Now.Second + DateTime.Now.Millisecond + ".docx"; //object fileToSave = (object)@"\\172.16.10.2\Users\jeroen\CarGo\ritbladen\ritblad_" + opdracht.opdracht_id_full + "_" + OpdrachtManagement.getChauffeursVanOpdracht(opdracht).First().naam + ".docx"; //Create new instance of word and create a new document Word.Application wordApp = new Word.Application(); Word.Document doc = null; //MessageBox.Show(System.IO.Path.GetTempPath(), "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //Properties for the new word document object readOnly = false; object isVisible = false; //Settings the application to invisible, so the user doesn't notice that anything is going on wordApp.Visible = false; //Open and activate the chosen template doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); try { File.Copy(fileToOpen.ToString(), fileToSave.ToString(), true); } catch { MainForm.updateStatus = "Kan nieuw document niet opslaan"; return; } //Search for bookmarks and replace them with the text you want PrintManagement.findAndReplace(doc, "Datum", date.ToString("dddd dd MMMM yyyy")); PrintManagement.findAndReplace(doc, "Van1", rit1vertr); PrintManagement.findAndReplace(doc, "Tot1", rit1terug); PrintManagement.findAndReplace(doc, "Van2", rit2vertr); PrintManagement.findAndReplace(doc, "Tot2", rit2terug); PrintManagement.findAndReplace(doc, "Naam", chauffeurnaam); PrintManagement.findAndReplace(doc, "Ritnummer1", ritnummer); PrintManagement.findAndReplace(doc, "Ritnummer", ritnummer); PrintManagement.findAndReplace(doc, "Bestemming1", vertrek.FullAdress); PrintManagement.findAndReplace(doc, "Bestemming2", vertrek.FullAdress); PrintManagement.findAndReplace(doc, "Vertrekplaats1", bestemming.FullAdress); PrintManagement.findAndReplace(doc, "Vertrekplaats2", bestemming.FullAdress); PrintManagement.findAndReplace(doc, "Voertuignummer1", voertuignaam1); PrintManagement.findAndReplace(doc, "Voertuignummer2", voertuignaam2); //doc = wordApp.Documents.Open(fileToSave, ReadOnly: false, Visible: true); doc.Save(); doc.Activate(); //Making word visible to be able to show the print preview. wordApp.Visible = true; } } } foreach (opdracht o in OpdrachtManagement.getOpdrachten(date)) { rit_info info = ContractManagement.getRitInfo(null); int counter = 0; chauffeur firstChauff = null; chauffeur secChauff = null; var idfull = o.contract_id_full; var ritnummer = o.ritomschrijving; var rit1vertr = o.vanaf_uur; var rit1terug = o.tot_uur; foreach (opdracht_chauffeur cha in OpdrachtManagement.getChauffeursVanOpdract(o)) { if (counter == 0) { firstChauff = cha.chauffeur; } else if (counter == 1) { secChauff = cha.chauffeur; } counter++; } var vertrek = OpdrachtManagement.getVertrek(o.opdracht_id); var bestemming = OpdrachtManagement.getBestemming(o.opdracht_id); IEnumerable <opdracht_voertuig> voertuigen = OpdrachtManagement.getVoertuigenVanOpdracht(o); var voertuignaam3 = voertuigen.First().voertuig.identificatie; if (firstChauff.naam.ToString() == cbxChauffeurs.SelectedItem.ToString()) { //missing oject to use with various word commands object missing = System.Reflection.Missing.Value; //the template file you will be using object fileToOpen = (object)@"R:\CarGo\ritblad_chauffeur_CONTRACT.docx"; //object fileToOpen = (object)@"\\172.16.10.2\Users\jeroen\CarGo\ritblad_template.docx"; //Where the new file will be saved to. object fileToSave = (object)@"R:\CarGo\printouts\ritblad_chauffeur_CONTRACT" + "" + DateTime.Now.Second + DateTime.Now.Millisecond + ".docx"; //object fileToSave = (object)@"\\172.16.10.2\Users\jeroen\CarGo\ritbladen\ritblad_" + opdracht.opdracht_id_full + "_" + OpdrachtManagement.getChauffeursVanOpdracht(opdracht).First().naam + ".docx"; //Create new instance of word and create a new document Word.Application wordApp = new Word.Application(); Word.Document doc = null; //MessageBox.Show(System.IO.Path.GetTempPath(), "Error Title", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //Properties for the new word document object readOnly = false; object isVisible = false; //Settings the application to invisible, so the user doesn't notice that anything is going on wordApp.Visible = false; //Open and activate the chosen template doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); try { File.Copy(fileToOpen.ToString(), fileToSave.ToString(), true); } catch { MainForm.updateStatus = "Kan nieuw document niet opslaan"; return; } //Search for bookmarks and replace them with the text you want PrintManagement.findAndReplace(doc, "Datum", date.ToString("dddd dd MMMM yyyy")); PrintManagement.findAndReplace(doc, "Van1", rit1vertr); PrintManagement.findAndReplace(doc, "Tot1", rit1terug); PrintManagement.findAndReplace(doc, "Van2", ""); PrintManagement.findAndReplace(doc, "Tot2", ""); PrintManagement.findAndReplace(doc, "Naam", firstChauff.naam.ToString()); PrintManagement.findAndReplace(doc, "Ritnummer1", ritnummer); PrintManagement.findAndReplace(doc, "Ritnummer", ""); PrintManagement.findAndReplace(doc, "Bestemming1", vertrek.FullAdress); PrintManagement.findAndReplace(doc, "Bestemming2", ""); PrintManagement.findAndReplace(doc, "Vertrekplaats1", bestemming.FullAdress); PrintManagement.findAndReplace(doc, "Vertrekplaats2", ""); PrintManagement.findAndReplace(doc, "Voertuignummer1", voertuignaam3); PrintManagement.findAndReplace(doc, "Voertuignummer2", ""); //doc = wordApp.Documents.Open(fileToSave, ReadOnly: false, Visible: true); doc.Save(); doc.Activate(); //Making word visible to be able to show the print preview. wordApp.Visible = true; } } }