Project

General

Profile

1 35644 stefania.m
package eu.dnetlib.client.fundingrequest.newrequest.accountingstep;
2 35292 stefania.m
3 35515 stefania.m
import com.github.gwtbootstrap.client.ui.Alert;
4 35292 stefania.m
import com.github.gwtbootstrap.client.ui.Button;
5 35515 stefania.m
import com.github.gwtbootstrap.client.ui.Form;
6
import com.github.gwtbootstrap.client.ui.constants.AlertType;
7
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
8
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
9
import com.github.gwtbootstrap.client.ui.constants.FormType;
10
import com.github.gwtbootstrap.datepicker.client.ui.DateBox;
11
import com.google.gwt.core.client.GWT;
12 35292 stefania.m
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14 35654 stefania.m
import com.google.gwt.user.client.Window;
15 35515 stefania.m
import com.google.gwt.user.client.ui.*;
16 35644 stefania.m
import eu.dnetlib.client.fundingrequest.newrequest.FundingWizardStepWidget;
17 35515 stefania.m
import eu.dnetlib.client.widgets.FormFieldSet;
18
import eu.dnetlib.client.widgets.TextBox;
19 35520 stefania.m
import eu.dnetlib.client.widgets.ValueChangeEvent;
20
import eu.dnetlib.client.widgets.ValueChangeHandler;
21 35640 stefania.m
import eu.dnetlib.goldoa.domain.*;
22 35292 stefania.m
import eu.dnetlib.shared.FundingWizardState;
23
24 35700 stefania.m
import java.util.Date;
25
26 35292 stefania.m
/**
27
 * Created by stefania on 3/9/15.
28
 */
29
public class AccountingStepWidget extends FundingWizardStepWidget {
30
31
    private FlowPanel accountingStepPanel = new FlowPanel();
32 35638 stefania.m
    private Alert errorLabel = new Alert();
33 35292 stefania.m
34 35515 stefania.m
    private FlowPanel uploadInvoicePanel = new FlowPanel();
35
    private Label uploadInvoiceLabel = new Label();
36 35638 stefania.m
    private Alert uploadInvoiceInfoLabel = new Alert();
37
    private Alert uploadInvoiceErrorLabel = new Alert();
38 35515 stefania.m
    private Alert uploadInvoiceSuccessLabel = new Alert();
39
    private Form uploadInvoiceForm = new Form();
40 35520 stefania.m
    private TextBox requestIdHidden = new TextBox();
41 35515 stefania.m
    private TextBox invoiceNumber = new TextBox();
42
    private DateBox dateIssued = new DateBox();
43
    private FileUpload fileUpload = new FileUpload();
44
    private Button upload = new Button("Upload");
45 35292 stefania.m
46 35515 stefania.m
    private FlowPanel accountingInfoPanel = new FlowPanel();
47
    private Label accountingInfoLabel = new Label();
48
    private Alert accountingInfoErrorLabel = new Alert();
49
    private Form accountingInfoForm = new Form();
50 35520 stefania.m
    private TextBox apcTextBox = new TextBox();
51
    private TextBox goldDiscountTextBox = new TextBox();
52
    private TextBox projectParticipationTextBox = new TextBox();
53
    private TextBox fundingRequestedTextBox = new TextBox();
54 35515 stefania.m
55 35520 stefania.m
    private float apc = 0;
56
    private float goldDiscount = 0;
57
    private float fundingRequested = 0;
58
59 35640 stefania.m
    private boolean isInvoiceUploaded = false;
60 35650 stefania.m
    private String invoiceId = null;
61 35520 stefania.m
62 35347 stefania.m
    private FundingWizardStepCompleteListener fundingWizardStepCompleteListener;
63 35292 stefania.m
64
    public AccountingStepWidget(String title) {
65
        super(title);
66
67
        accountingStepPanel.addStyleName("accountingForm");
68 35700 stefania.m
        accountingStepPanel.addStyleName("fundingWizardStep");
69
        accountingStepPanel.addStyleName("inlineBlock");
70 35292 stefania.m
71 35638 stefania.m
        errorLabel.addStyleName("alertLabel");
72
        errorLabel.addStyleName("width80");
73
        errorLabel.setType(AlertType.ERROR);
74
        errorLabel.setVisible(false);
75
        errorLabel.setClose(false);
76
77 35515 stefania.m
        uploadInvoiceLabel.addStyleName("uploadInvoiceLabel");
78
        uploadInvoiceLabel.setText("Upload your invoice");
79 35292 stefania.m
80 35515 stefania.m
        uploadInvoiceSuccessLabel.addStyleName("alertLabel");
81
        uploadInvoiceSuccessLabel.setType(AlertType.SUCCESS);
82
        uploadInvoiceSuccessLabel.setVisible(false);
83
        uploadInvoiceSuccessLabel.setClose(false);
84 35292 stefania.m
85 35638 stefania.m
        uploadInvoiceInfoLabel.addStyleName("alertLabel");
86
        uploadInvoiceInfoLabel.setText("You are not required to upload your invoice at this point if you don't have it, " +
87
                "but you are required to come back later and upload it to complete your request.");
88
        uploadInvoiceInfoLabel.setType(AlertType.INFO);
89
        uploadInvoiceInfoLabel.setVisible(true);
90
        uploadInvoiceInfoLabel.setClose(false);
91 35292 stefania.m
92 35638 stefania.m
        uploadInvoiceErrorLabel.addStyleName("alertLabel");
93
        uploadInvoiceErrorLabel.setType(AlertType.ERROR);
94
        uploadInvoiceErrorLabel.setVisible(false);
95
        uploadInvoiceErrorLabel.setClose(false);
96
97 35515 stefania.m
        uploadInvoicePanel.addStyleName("uploadInvoicePanel");
98
        uploadInvoicePanel.add(uploadInvoiceLabel);
99 35638 stefania.m
        uploadInvoicePanel.add(uploadInvoiceInfoLabel);
100 35515 stefania.m
        uploadInvoicePanel.add(uploadInvoiceSuccessLabel);
101 35638 stefania.m
        uploadInvoicePanel.add(uploadInvoiceErrorLabel);
102 35515 stefania.m
103
        uploadInvoiceForm.setType(FormType.HORIZONTAL);
104
        uploadInvoiceForm.setAction(GWT.getModuleBaseURL() + "fileupload");
105
        uploadInvoiceForm.setEncoding(FormPanel.ENCODING_MULTIPART);
106
        uploadInvoiceForm.setMethod(FormPanel.METHOD_POST);
107
        uploadInvoicePanel.add(uploadInvoiceForm);
108
109 35520 stefania.m
        requestIdHidden.setVisible(false);
110
        requestIdHidden.setName("requestId");
111
        uploadInvoiceForm.add(new FormFieldSet(null, requestIdHidden));
112
113 35515 stefania.m
        invoiceNumber.setAlternateSize(AlternateSize.LARGE);
114
        invoiceNumber.setName("invoiceNumber");
115
        uploadInvoiceForm.add(new FormFieldSet("Invoice number (*)", invoiceNumber));
116
117
        dateIssued.setAlternateSize(AlternateSize.LARGE);
118
        dateIssued.setName("dateIssued");
119 35651 stefania.m
        dateIssued.setFormat("yyyy/mm/dd");
120 35515 stefania.m
        uploadInvoiceForm.add(new FormFieldSet("Date issued (*)", dateIssued));
121
122
        fileUpload.setName("fileUpload");
123
        uploadInvoiceForm.add(new FormFieldSet("Invoice (*)", fileUpload));
124
125
        upload.setType(ButtonType.SUCCESS);
126
        upload.addClickHandler(new ClickHandler() {
127
            @Override
128
            public void onClick(ClickEvent clickEvent) {
129
130
                uploadInvoiceSuccessLabel.setVisible(false);
131 35638 stefania.m
                uploadInvoiceErrorLabel.setVisible(false);
132 35515 stefania.m
133
                if(!invoiceNumber.getValue().trim().equals("") && fileUpload.getFilename()!=null && !fileUpload.getFilename().equals(""))
134
                    uploadInvoiceForm.submit();
135
                else {
136 35638 stefania.m
                    uploadInvoiceErrorLabel.setText("All asterisk (*) fields are required.");
137
                    uploadInvoiceErrorLabel.setVisible(true);
138 35292 stefania.m
                }
139
            }
140
        });
141 35515 stefania.m
        uploadInvoiceForm.add(new FormFieldSet(null, upload));
142
143
        uploadInvoiceForm.addSubmitCompleteHandler(new Form.SubmitCompleteHandler() {
144
            @Override
145
            public void onSubmitComplete(Form.SubmitCompleteEvent submitCompleteEvent) {
146
147 35654 stefania.m
                invoiceId = submitCompleteEvent.getResults().replace("<pre>", "").replace("</pre>", "");
148 35640 stefania.m
                isInvoiceUploaded = true;
149
150 35515 stefania.m
                uploadInvoiceSuccessLabel.setVisible(false);
151 35638 stefania.m
                uploadInvoiceErrorLabel.setVisible(false);
152 35515 stefania.m
153
                uploadInvoiceSuccessLabel.setText("Upload completed successfully");
154
                uploadInvoiceSuccessLabel.setVisible(true);
155 35292 stefania.m
            }
156
        });
157
158 35515 stefania.m
159 35520 stefania.m
        accountingInfoLabel.addStyleName("accountingInformationLabel");
160 35515 stefania.m
        accountingInfoLabel.setText("Accounting Information");
161
162
        accountingInfoErrorLabel.addStyleName("alertLabel");
163
        accountingInfoErrorLabel.setType(AlertType.ERROR);
164
        accountingInfoErrorLabel.setVisible(false);
165
        accountingInfoErrorLabel.setClose(false);
166
167 35520 stefania.m
        accountingInfoPanel.addStyleName("accountingInformationPanel");
168 35515 stefania.m
        accountingInfoPanel.add(accountingInfoLabel);
169
        accountingInfoPanel.add(accountingInfoErrorLabel);
170
171
        accountingInfoForm.setType(FormType.HORIZONTAL);
172
        accountingInfoPanel.add(accountingInfoForm);
173
174 35520 stefania.m
        apcTextBox.setAlternateSize(AlternateSize.LARGE);
175
        apcTextBox.setEnabled(false);
176
        accountingInfoForm.add(new FormFieldSet("APC", apcTextBox));
177 35515 stefania.m
178 35638 stefania.m
        Label goldDiscountPercentageLabel = new Label("  %");
179
        goldDiscountPercentageLabel.setStyleName("inlineBlock");
180
181 35520 stefania.m
        goldDiscountTextBox.setAlternateSize(AlternateSize.LARGE);
182
        goldDiscountTextBox.setEnabled(false);
183 35638 stefania.m
        goldDiscountTextBox.addStyleName("inlineBlock");
184
        accountingInfoForm.add(new FormFieldSet("OpenAIRE Gold Discount", goldDiscountTextBox, goldDiscountPercentageLabel));
185 35515 stefania.m
186 35638 stefania.m
        Label projectParticipationPercentageLabel = new Label("  %");
187
        projectParticipationPercentageLabel.setStyleName("inlineBlock");
188 35575 stefania.m
189 35520 stefania.m
        projectParticipationTextBox.setAlternateSize(AlternateSize.LARGE);
190 35638 stefania.m
        projectParticipationTextBox.setValue("100");
191 35575 stefania.m
        projectParticipationTextBox.addStyleName("inlineBlock");
192 35520 stefania.m
        projectParticipationTextBox.setValueChangeHandler(new ValueChangeHandler() {
193 35515 stefania.m
194 35520 stefania.m
            @Override
195
            public void handle(ValueChangeEvent valueChangeEvent) {
196 35515 stefania.m
197 35520 stefania.m
                accountingInfoErrorLabel.setVisible(false);
198
                String percentage = projectParticipationTextBox.getValue().trim();
199 35515 stefania.m
200 35520 stefania.m
                if(!percentage.equals("")) {
201
                    if (isPercentage(percentage)) {
202
203
                        fundingRequested = (apc / (1 + (goldDiscount / 100))) * (Float.parseFloat(percentage) / 100);
204
                        fundingRequestedTextBox.setValue(fundingRequested + "");
205
206
                    } else {
207 35638 stefania.m
                        accountingInfoErrorLabel.setText("Project participation must be a number greater than 0 and less or equal than 100");
208 35520 stefania.m
                        accountingInfoErrorLabel.setVisible(true);
209
                    }
210
                }
211
            }
212
        });
213 35638 stefania.m
        accountingInfoForm.add(new FormFieldSet("Project participation (*)", projectParticipationTextBox, projectParticipationPercentageLabel));
214 35520 stefania.m
215
        fundingRequestedTextBox.setAlternateSize(AlternateSize.LARGE);
216 35638 stefania.m
        fundingRequested = (apc / (1 + (goldDiscount / 100)));
217
        fundingRequestedTextBox.setValue(fundingRequested + "");
218
        accountingInfoForm.add(new FormFieldSet("Funding Requested (*)", fundingRequestedTextBox));
219 35520 stefania.m
220 35638 stefania.m
        accountingStepPanel.add(errorLabel);
221 35515 stefania.m
        accountingStepPanel.add(uploadInvoicePanel);
222
        accountingStepPanel.add(accountingInfoPanel);
223 35292 stefania.m
    }
224
225
    @Override
226
    public Widget asWidget() {
227
        return accountingStepPanel;
228
    }
229
230
    @Override
231
    public void updateState(FundingWizardState fundingWizardState) {
232
233 35638 stefania.m
        Request request = fundingWizardState.getRequest();
234 35668 stefania.m
        request.setFundingRequested(Float.parseFloat(fundingRequestedTextBox.getValue().trim()));
235 35640 stefania.m
        request.setProjectParticipation(Float.parseFloat(projectParticipationTextBox.getValue().trim()));
236
237
        if(isInvoiceUploaded) {
238
239
            Invoice invoice = new Invoice();
240
            invoice.setNumber(invoiceNumber.getValue().trim());
241
            invoice.setDate(dateIssued.getValue());
242 35654 stefania.m
            invoice.setId(invoiceId);
243 35640 stefania.m
            fundingWizardState.setInvoice(invoice);
244
245 35654 stefania.m
            request.setInvoice(invoiceId);
246 35640 stefania.m
        }
247 35292 stefania.m
    }
248
249
    @Override
250 35347 stefania.m
    public void save() {
251 35292 stefania.m
252 35638 stefania.m
        errorLabel.setVisible(false);
253
254
        if(isComplete()) {
255
            if(fundingWizardStepCompleteListener!=null)
256
                fundingWizardStepCompleteListener.setStepComplete(true);
257
        }
258 35292 stefania.m
    }
259
260 35347 stefania.m
    @Override
261 35700 stefania.m
    public void clear() {
262
263
//        errorLabel.setVisible(false);
264
//        uploadInvoiceErrorLabel.setVisible(false);
265
//        uploadInvoiceSuccessLabel.setVisible(false);
266
//
267
//        requestIdHidden.setValue("");
268
//        invoiceNumber.setValue("");
269
//        dateIssued.setValue(new Date());
270
//        fileUpload
271
    }
272
273
    @Override
274 35515 stefania.m
    public void loadContent(FundingWizardState fundingWizardState) {
275
276 35520 stefania.m
        accountingInfoErrorLabel.setVisible(false);
277
278
        Request request = fundingWizardState.getRequest();
279
        requestIdHidden.setValue(request.getId());
280
281 35660 stefania.m
        if(fundingWizardState.getPublication().getJournal()!=null) {
282 35520 stefania.m
283 35660 stefania.m
            apcTextBox.setValue(fundingWizardState.getPublication().getJournal().getApc() + "");
284
            apc = fundingWizardState.getPublication().getJournal().getApc();
285 35640 stefania.m
            if(apc>0)
286
                fundingRequestedTextBox.setEnabled(false);
287 35520 stefania.m
288 35660 stefania.m
            goldDiscountTextBox.setValue(fundingWizardState.getPublication().getJournal().getDiscount() + "");
289
            goldDiscount = fundingWizardState.getPublication().getJournal().getDiscount();
290 35520 stefania.m
291 35660 stefania.m
        } else if(fundingWizardState.getPublication().getPublisher()!=null) {
292 35520 stefania.m
293 35640 stefania.m
            //TODO uncomment when Antonis updates publisher
294 35660 stefania.m
//                    apcTextBox.setValue(fundingWizardState.getPublication().getPublisher().getApc() + "");
295
//                    apc = fundingWizardState.getPublication().getPublisher().getApc();
296 35638 stefania.m
//                    if(apc>0)
297
//                        fundingRequestedTextBox.setEnabled(false);
298 35520 stefania.m
299 35660 stefania.m
            goldDiscountTextBox.setValue(fundingWizardState.getPublication().getPublisher().getDiscount() + "");
300
            goldDiscount = fundingWizardState.getPublication().getPublisher().getDiscount();
301 35520 stefania.m
        }
302 35515 stefania.m
    }
303
304
    @Override
305 35347 stefania.m
    public void setFundingWizardStepCompleteListener(FundingWizardStepCompleteListener fundingWizardStepCompleteListener) {
306
        this.fundingWizardStepCompleteListener = fundingWizardStepCompleteListener;
307
    }
308 35520 stefania.m
309 35660 stefania.m
    @Override
310
    public void setFundingWizardJumpToStepListener(FundingWizardJumpToStepListener fundingWizardJumpToStepListener) {
311
312
    }
313
314 35520 stefania.m
    private boolean isPercentage(String percentage) {
315
316
        try {
317
318
            Float percentageValue = Float.parseFloat(percentage);
319 35638 stefania.m
            if(percentageValue>0 && percentageValue<=100)
320 35520 stefania.m
                return true;
321
322
        } catch(NumberFormatException nfe) {
323
            return false;
324
        }
325
326
        return false;
327
    }
328 35638 stefania.m
329
    private boolean isFloat(String number) {
330
331
        try {
332
            Float numberValue = Float.parseFloat(number);
333
            return true;
334
        } catch(NumberFormatException nfe) {
335
            return false;
336
        }
337
    }
338
339
    private boolean isPositiveNumber(float number) {
340
341
        if(number>0)
342
            return true;
343
344
        return false;
345
    }
346
347
    private boolean isComplete() {
348
349
        if(!projectParticipationTextBox.getValue().trim().equals("") && !fundingRequestedTextBox.getValue().trim().equals("")) {
350
351
            if(!isPercentage(projectParticipationTextBox.getValue().trim())) {
352
                return false;
353
            }
354
355
            if(!isFloat(fundingRequestedTextBox.getValue().trim()) || !isPositiveNumber(Float.parseFloat(fundingRequestedTextBox.getValue().trim()))) {
356
                errorLabel.setText("Funding requested must be a number greater than 0");
357
                errorLabel.setVisible(true);
358
                return false;
359
            }
360
361
            return true;
362
        } else {
363
            errorLabel.setText("You need to complete the Project participation and the Funding requested fields");
364
            errorLabel.setVisible(true);
365
        }
366
367
        return false;
368
    }
369 35292 stefania.m
}