This repository has been archived by the owner on May 28, 2019. It is now read-only.
/
frmPutniNalozi.cs
135 lines (116 loc) · 4.49 KB
/
frmPutniNalozi.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using foi.pi;
namespace PutniNalozi.NET
{
public partial class frmPutniNalozi : Form
{
Login piLogin;
int STATUS_ODOBREN = 2;
public frmPutniNalozi(Login piLoginInstance)
{
InitializeComponent();
// set login component passed when instancing
this.piLogin = piLoginInstance;
}
/*
* Returns currently selected row in DataGridView dgvPutniNalog
*/
private piDB9DS.putni_nalogRow getSelectedRow()
{
DataRowView current = (DataRowView)dgvPutniNalozi.CurrentRow.DataBoundItem;
piDB9DS.putni_nalogRow putniNalogRow = (piDB9DS.putni_nalogRow)current.Row;
return putniNalogRow;
}
/*
* Returns full name of given username via piLogin component
*/
public string getFullName(string username)
{
// retrieve users matching username
List<iUser> korisnici = this.piLogin.GetUser(username, true);
if (korisnici.Count > 0)
{
// if user exists, return concationation of users first and last name
iUser korisnik = korisnici.First();
return korisnik.Name + " " + korisnik.Surname;
}
// if user doesn't exists, fallback on username given as argument
return username;
}
/*
* Method refreshes left side of form where information about "nalog" are displayed
*/
private void refreshNalog()
{
// if user select's header of DataGridView, break don't refresh nalog
if (dgvPutniNalozi.CurrentCellAddress.Y == -1)
{
return;
}
// get currently selected row of DataGridView
piDB9DS.putni_nalogRow putniNalogRow = getSelectedRow();
// "zahtjevatelj" must exist in database, so we just can show full name in text field
txtZahtjevatelj.Text = getFullName(putniNalogRow.zahtjevatelj);
// "odobravatelj" may/may not exist, so we need to check first
if (putniNalogRow.odobravatelj.Trim() != "")
{
txtOdobravatelj.Text = getFullName(putniNalogRow.odobravatelj);
}
// disable button for approval if nalog already approved
if (putniNalogRow.status_id == this.STATUS_ODOBREN)
{
btnOdobri.Enabled = false;
}
else
{
btnOdobri.Enabled = true;
}
}
/*
* Calls method for updating status of nalog and returns true if number of rows affected > 0
*/
private bool odobriNalog(int putniNalogId, string korIme)
{
return this.putniNalogTA.UpdateStatus(this.STATUS_ODOBREN, korIme, putniNalogId) > 0;
}
private void frmPutniNalozi_Load(object sender, EventArgs e)
{
// fills putniNalogTA with data with values of foreign keys
this.putniNalogTA.FillWithRelations(this.piDB9DS.putni_nalog);
// refresh left pane
refreshNalog();
}
private void frmPutniNalozi_FormClosing(object sender, FormClosingEventArgs e)
{
// set datasource to null to avoid crashing
dgvPutniNalozi.DataSource = null;
}
private void dgvPutniNalozi_SelectionChanged(object sender, EventArgs e)
{
// when user clicks on other row, refresh left pane
refreshNalog();
}
private void btnOdobri_Click(object sender, EventArgs e)
{
// get username of currenty logged user from main form
string korIme = ((frmMain)this.ParentForm).current_user.UserName;
// get currenty selected nalog from DataGridView and fetch id
int putniNalogId = getSelectedRow().id;
// finnaly, call method for approving
if (odobriNalog(putniNalogId, korIme))
{
// if succeded, refresh DataGridView and display status message
this.putniNalogTA.FillWithRelations(this.piDB9DS.putni_nalog);
((frmMain)this.ParentForm).writeToStatus("Nalog odobren", 3000);
dgvPutniNalozi.Refresh();
}
}
}
}