summaryrefslogtreecommitdiff
path: root/src/app/Cmpp298.Assignment3.Desktop.Presentation/EditInvoicePresenter.cs
blob: eaaf8e6cd8e6fd83962a1acbfc2d4cb2cc1ec937 (plain)
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
using Cmpp298.Assignment3.Domain;
using Cmpp298.Assignment3.Dto;
using Cmpp298.Assignment3.Task;

namespace Cmpp298.Assignment3.Presentation {
	public class EditInvoicePresenter {
		private readonly string _invoiceId;
		private readonly IEditInvoiceView _view;
		private readonly IInvoiceTask _task;
		private readonly ITermTask _termTask;

		public EditInvoicePresenter( string invoiceId, IEditInvoiceView view )
			: this( invoiceId, view, new InvoiceTask( ), new TermTask( ) ) {}

		public EditInvoicePresenter( string invoiceId, IEditInvoiceView view, IInvoiceTask task, ITermTask termTask ) {
			_view = view;
			_task = task;
			_termTask = termTask;
			_invoiceId = invoiceId;
		}

		public void Initialize( ) {
			_view.TermsDropDown.BindTo( _termTask.GetAll( ) );
			UpdateViewFrom( _task.GetInvoiceBy( _invoiceId ) );
		}

		public void UpdateInvoice( ) {
			if ( IsValidInput( ) ) {
				_task.UpdateInvoice( CreateDtoFromView( ) );
			}
			else {
				_view.ShowError( "Invalid input detected" );
			}
		}

		private void UpdateViewFrom( DisplayInvoiceDto dto ) {
			_view.VendorName = dto.VendorName;
			_view.InvoiceNumber = dto.InvoiceNumber;
			_view.InvoiceDate = dto.InvoiceDate;
			_view.InvoiceTotal = dto.InvoiceTotal;
			_view.PaymentTotal = dto.PaymentTotal;
			_view.CreditTotal = dto.CreditTotal;
			_view.DueDate = dto.DueDate;
			_view.PaymentDate = dto.PaymentDate;
			_view.TermsDropDown.SetSelectedItemTo( dto.Terms );
		}

		private bool IsValidInput( ) {
			AmountEntrySpecification specification = new AmountEntrySpecification( );
			return
				specification.IsSatisfiedBy( _view.CreditTotal )
				&& specification.IsSatisfiedBy( _view.PaymentTotal )
				&& specification.IsSatisfiedBy( _view.InvoiceTotal );
		}

		private UpdateInvoiceDto CreateDtoFromView( ) {
			return
				new UpdateInvoiceDto( _invoiceId, _view.InvoiceNumber, _view.InvoiceDate, _view.InvoiceTotal, _view.PaymentTotal,
				                      _view.CreditTotal, _view.DueDate, _view.PaymentDate, _view.TermsDropDown.SelectedItem.Value );
		}
	}
}