Project

General

Profile

1
package eu.dnetlib.client.fundingrequest.newrequest.accountingstep;
2

    
3
import com.github.gwtbootstrap.client.ui.Alert;
4
import com.github.gwtbootstrap.client.ui.Button;
5
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
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.user.client.Window;
15
import com.google.gwt.user.client.ui.*;
16
import eu.dnetlib.client.fundingrequest.newrequest.FundingWizardStepWidget;
17
import eu.dnetlib.client.widgets.FormFieldSet;
18
import eu.dnetlib.client.widgets.TextBox;
19
import eu.dnetlib.client.widgets.ValueChangeEvent;
20
import eu.dnetlib.client.widgets.ValueChangeHandler;
21
import eu.dnetlib.goldoa.domain.*;
22
import eu.dnetlib.shared.FundingWizardState;
23

    
24
import java.util.Date;
25

    
26
/**
27
 * Created by stefania on 3/9/15.
28
 */
29
public class AccountingStepWidget extends FundingWizardStepWidget {
30

    
31
    private FlowPanel accountingStepPanel = new FlowPanel();
32
    private Alert errorLabel = new Alert();
33

    
34
    private FlowPanel uploadInvoicePanel = new FlowPanel();
35
    private Label uploadInvoiceLabel = new Label();
36
    private Alert uploadInvoiceInfoLabel = new Alert();
37
    private Alert uploadInvoiceErrorLabel = new Alert();
38
    private Alert uploadInvoiceSuccessLabel = new Alert();
39
    private Form uploadInvoiceForm = new Form();
40
    private TextBox requestIdHidden = new TextBox();
41
    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

    
46
    private FlowPanel accountingInfoPanel = new FlowPanel();
47
    private Label accountingInfoLabel = new Label();
48
    private Alert accountingInfoErrorLabel = new Alert();
49
    private Form accountingInfoForm = new Form();
50
    private TextBox apcTextBox = new TextBox();
51
    private TextBox goldDiscountTextBox = new TextBox();
52
    private TextBox projectParticipationTextBox = new TextBox();
53
    private TextBox fundingRequestedTextBox = new TextBox();
54

    
55
    private float apc = 0;
56
    private float goldDiscount = 0;
57
    private float fundingRequested = 0;
58

    
59
    private boolean isInvoiceUploaded = false;
60
    private String invoiceId = null;
61

    
62
    private FundingWizardStepCompleteListener fundingWizardStepCompleteListener;
63

    
64
    public AccountingStepWidget(String title) {
65
        super(title);
66

    
67
        accountingStepPanel.addStyleName("accountingForm");
68
        accountingStepPanel.addStyleName("fundingWizardStep");
69
        accountingStepPanel.addStyleName("inlineBlock");
70

    
71
        errorLabel.addStyleName("alertLabel");
72
        errorLabel.addStyleName("width80");
73
        errorLabel.setType(AlertType.ERROR);
74
        errorLabel.setVisible(false);
75
        errorLabel.setClose(false);
76

    
77
        uploadInvoiceLabel.addStyleName("uploadInvoiceLabel");
78
        uploadInvoiceLabel.setText("Upload your invoice");
79

    
80
        uploadInvoiceSuccessLabel.addStyleName("alertLabel");
81
        uploadInvoiceSuccessLabel.setType(AlertType.SUCCESS);
82
        uploadInvoiceSuccessLabel.setVisible(false);
83
        uploadInvoiceSuccessLabel.setClose(false);
84

    
85
        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

    
92
        uploadInvoiceErrorLabel.addStyleName("alertLabel");
93
        uploadInvoiceErrorLabel.setType(AlertType.ERROR);
94
        uploadInvoiceErrorLabel.setVisible(false);
95
        uploadInvoiceErrorLabel.setClose(false);
96

    
97
        uploadInvoicePanel.addStyleName("uploadInvoicePanel");
98
        uploadInvoicePanel.add(uploadInvoiceLabel);
99
        uploadInvoicePanel.add(uploadInvoiceInfoLabel);
100
        uploadInvoicePanel.add(uploadInvoiceSuccessLabel);
101
        uploadInvoicePanel.add(uploadInvoiceErrorLabel);
102

    
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
        requestIdHidden.setVisible(false);
110
        requestIdHidden.setName("requestId");
111
        uploadInvoiceForm.add(new FormFieldSet(null, requestIdHidden));
112

    
113
        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
        dateIssued.setFormat("yyyy/mm/dd");
120
        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
                uploadInvoiceErrorLabel.setVisible(false);
132

    
133
                if(!invoiceNumber.getValue().trim().equals("") && fileUpload.getFilename()!=null && !fileUpload.getFilename().equals(""))
134
                    uploadInvoiceForm.submit();
135
                else {
136
                    uploadInvoiceErrorLabel.setText("All asterisk (*) fields are required.");
137
                    uploadInvoiceErrorLabel.setVisible(true);
138
                }
139
            }
140
        });
141
        uploadInvoiceForm.add(new FormFieldSet(null, upload));
142

    
143
        uploadInvoiceForm.addSubmitCompleteHandler(new Form.SubmitCompleteHandler() {
144
            @Override
145
            public void onSubmitComplete(Form.SubmitCompleteEvent submitCompleteEvent) {
146

    
147
                invoiceId = submitCompleteEvent.getResults().replace("<pre>", "").replace("</pre>", "");
148
                isInvoiceUploaded = true;
149

    
150
                uploadInvoiceSuccessLabel.setVisible(false);
151
                uploadInvoiceErrorLabel.setVisible(false);
152

    
153
                uploadInvoiceSuccessLabel.setText("Upload completed successfully");
154
                uploadInvoiceSuccessLabel.setVisible(true);
155
            }
156
        });
157

    
158

    
159
        accountingInfoLabel.addStyleName("accountingInformationLabel");
160
        accountingInfoLabel.setText("Accounting Information");
161

    
162
        accountingInfoErrorLabel.addStyleName("alertLabel");
163
        accountingInfoErrorLabel.setType(AlertType.ERROR);
164
        accountingInfoErrorLabel.setVisible(false);
165
        accountingInfoErrorLabel.setClose(false);
166

    
167
        accountingInfoPanel.addStyleName("accountingInformationPanel");
168
        accountingInfoPanel.add(accountingInfoLabel);
169
        accountingInfoPanel.add(accountingInfoErrorLabel);
170

    
171
        accountingInfoForm.setType(FormType.HORIZONTAL);
172
        accountingInfoPanel.add(accountingInfoForm);
173

    
174
        apcTextBox.setAlternateSize(AlternateSize.LARGE);
175
        apcTextBox.setEnabled(false);
176
        accountingInfoForm.add(new FormFieldSet("APC", apcTextBox));
177

    
178
        Label goldDiscountPercentageLabel = new Label("  %");
179
        goldDiscountPercentageLabel.setStyleName("inlineBlock");
180

    
181
        goldDiscountTextBox.setAlternateSize(AlternateSize.LARGE);
182
        goldDiscountTextBox.setEnabled(false);
183
        goldDiscountTextBox.addStyleName("inlineBlock");
184
        accountingInfoForm.add(new FormFieldSet("OpenAIRE Gold Discount", goldDiscountTextBox, goldDiscountPercentageLabel));
185

    
186
        Label projectParticipationPercentageLabel = new Label("  %");
187
        projectParticipationPercentageLabel.setStyleName("inlineBlock");
188

    
189
        projectParticipationTextBox.setAlternateSize(AlternateSize.LARGE);
190
        projectParticipationTextBox.setValue("100");
191
        projectParticipationTextBox.addStyleName("inlineBlock");
192
        projectParticipationTextBox.setValueChangeHandler(new ValueChangeHandler() {
193

    
194
            @Override
195
            public void handle(ValueChangeEvent valueChangeEvent) {
196

    
197
                accountingInfoErrorLabel.setVisible(false);
198
                String percentage = projectParticipationTextBox.getValue().trim();
199

    
200
                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
                        accountingInfoErrorLabel.setText("Project participation must be a number greater than 0 and less or equal than 100");
208
                        accountingInfoErrorLabel.setVisible(true);
209
                    }
210
                }
211
            }
212
        });
213
        accountingInfoForm.add(new FormFieldSet("Project participation (*)", projectParticipationTextBox, projectParticipationPercentageLabel));
214

    
215
        fundingRequestedTextBox.setAlternateSize(AlternateSize.LARGE);
216
        fundingRequested = (apc / (1 + (goldDiscount / 100)));
217
        fundingRequestedTextBox.setValue(fundingRequested + "");
218
        accountingInfoForm.add(new FormFieldSet("Funding Requested (*)", fundingRequestedTextBox));
219

    
220
        accountingStepPanel.add(errorLabel);
221
        accountingStepPanel.add(uploadInvoicePanel);
222
        accountingStepPanel.add(accountingInfoPanel);
223
    }
224

    
225
    @Override
226
    public Widget asWidget() {
227
        return accountingStepPanel;
228
    }
229

    
230
    @Override
231
    public void updateState(FundingWizardState fundingWizardState) {
232

    
233
        Request request = fundingWizardState.getRequest();
234
        request.setFundingRequested(Float.parseFloat(fundingRequestedTextBox.getValue().trim()));
235
        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
            invoice.setId(invoiceId);
243
            fundingWizardState.setInvoice(invoice);
244

    
245
            request.setInvoice(invoiceId);
246
        }
247
    }
248

    
249
    @Override
250
    public void save() {
251

    
252
        errorLabel.setVisible(false);
253

    
254
        if(isComplete()) {
255
            if(fundingWizardStepCompleteListener!=null)
256
                fundingWizardStepCompleteListener.setStepComplete(true);
257
        }
258
    }
259

    
260
    @Override
261
    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
    public void loadContent(FundingWizardState fundingWizardState) {
275

    
276
        accountingInfoErrorLabel.setVisible(false);
277

    
278
        Request request = fundingWizardState.getRequest();
279
        requestIdHidden.setValue(request.getId());
280

    
281
        if(fundingWizardState.getPublication().getJournal()!=null) {
282

    
283
            apcTextBox.setValue(fundingWizardState.getPublication().getJournal().getApc() + "");
284
            apc = fundingWizardState.getPublication().getJournal().getApc();
285
            if(apc>0)
286
                fundingRequestedTextBox.setEnabled(false);
287

    
288
            goldDiscountTextBox.setValue(fundingWizardState.getPublication().getJournal().getDiscount() + "");
289
            goldDiscount = fundingWizardState.getPublication().getJournal().getDiscount();
290

    
291
        } else if(fundingWizardState.getPublication().getPublisher()!=null) {
292

    
293
            //TODO uncomment when Antonis updates publisher
294
//                    apcTextBox.setValue(fundingWizardState.getPublication().getPublisher().getApc() + "");
295
//                    apc = fundingWizardState.getPublication().getPublisher().getApc();
296
//                    if(apc>0)
297
//                        fundingRequestedTextBox.setEnabled(false);
298

    
299
            goldDiscountTextBox.setValue(fundingWizardState.getPublication().getPublisher().getDiscount() + "");
300
            goldDiscount = fundingWizardState.getPublication().getPublisher().getDiscount();
301
        }
302
    }
303

    
304
    @Override
305
    public void setFundingWizardStepCompleteListener(FundingWizardStepCompleteListener fundingWizardStepCompleteListener) {
306
        this.fundingWizardStepCompleteListener = fundingWizardStepCompleteListener;
307
    }
308

    
309
    @Override
310
    public void setFundingWizardJumpToStepListener(FundingWizardJumpToStepListener fundingWizardJumpToStepListener) {
311

    
312
    }
313

    
314
    private boolean isPercentage(String percentage) {
315

    
316
        try {
317

    
318
            Float percentageValue = Float.parseFloat(percentage);
319
            if(percentageValue>0 && percentageValue<=100)
320
                return true;
321

    
322
        } catch(NumberFormatException nfe) {
323
            return false;
324
        }
325

    
326
        return false;
327
    }
328

    
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
}
    (1-1/1)