package devopsdistilled.operp.client.business.sales.panes.controllers.impl;

import javax.inject.Inject;
import javax.swing.JPanel;

import devopsdistilled.operp.client.abstracts.EntityOperation;
import devopsdistilled.operp.client.business.sales.panes.SaleDescPane;
import devopsdistilled.operp.client.business.sales.panes.controllers.SaleDescPaneController;
import devopsdistilled.operp.client.business.sales.panes.controllers.SaleDescRowPaneController;
import devopsdistilled.operp.client.business.sales.panes.models.SaleDescPaneModel;
import devopsdistilled.operp.client.exceptions.EntityValidationException;
import devopsdistilled.operp.client.exceptions.NullFieldException;
import devopsdistilled.operp.server.data.entity.business.SaleDesc;
import devopsdistilled.operp.server.data.entity.business.SaleDescRow;

public class SaleDescPaneControllerImpl implements SaleDescPaneController {

	@Inject
	private SaleDescPane view;

	@Inject
	private SaleDescPaneModel model;

	@Inject
	private SaleDescRowPaneController saleDescRowPaneController;

	@Override
	public void validate() throws EntityValidationException {
		if (model.getSaleDescRow().getItem() == null
				|| model.getSaleDescRow().getWarehouse() == null
				|| model.getSaleDescRow().getQuantity().equals(0L)
				|| model.getSaleDescRow().getRate().equals(0.0)) {

			throw new NullFieldException();
		}

		// XXX More validation checking
	}

	@Override
	public SaleDesc save() {
		return null;
	}

	@Override
	public SaleDescPaneModel getModel() {
		return model;
	}

	@Override
	public SaleDescPane getView() {
		return view;
	}

	@Override
	public void init(SaleDesc saleDesc, EntityOperation entityOperation) {
		if (EntityOperation.Create == entityOperation) {
			SaleDescRow saleDescRow = new SaleDescRow();
			model.setSaleDescRow(saleDescRow);
			saleDescRowPaneController.init(saleDescRow, entityOperation);
		}

		view.setSaleDescRowpanel((JPanel) saleDescRowPaneController.getView()
				.getPane());
		view.setController(this);
		view.resetComponents();

		model.registerObserver(view);
		model.setEntityAndEntityOperation(saleDesc, entityOperation);
	}

	@Override
	public void addNewSaleDescRow() {

		model.getSaleDescRow().setBusinessDesc(model.getEntity());

		if (EntityOperation.Edit != model.getEntityOperation()) {
			model.getEntity().getDescRows().add(model.getSaleDescRow());
		}

		model.getEntity().setTotalAmount(
				model.getEntity().getTotalAmount()
						+ model.getSaleDescRow().getAmount());

		model.setEntityAndEntityOperation(model.getEntity(),
				EntityOperation.Create);

		SaleDescRow saleDescRow = new SaleDescRow();
		model.setSaleDescRow(saleDescRow);
		saleDescRowPaneController.init(saleDescRow, EntityOperation.Create);
	}

	@Override
	public void initEditSaleDescRow(SaleDescRow saleDescRow) {
		model.setSaleDescRow(saleDescRow);
		model.getEntity().setTotalAmount(
				model.getEntity().getTotalAmount()
						- model.getSaleDescRow().getAmount());
		model.setEntityAndEntityOperation(model.getEntity(),
				EntityOperation.Edit);
		saleDescRowPaneController.init(saleDescRow, EntityOperation.Edit);
	}
}

@@ -665,32 +665,93 @@
 FieldException;%0A
+import devopsdistilled.operp.client.stock.models.StockModel;%0A
 import devopsdis
@@ -801,16 +801,16 @@
 leDesc;%0A
-
 import d
@@ -870,16 +870,77 @@
 DescRow;
+%0Aimport devopsdistilled.operp.server.data.entity.stock.Stock;
 %0A%0Apublic
@@ -1092,16 +1092,58 @@
 model;%0A%0A
+%09@Inject%0A%09private StockModel stockModel;%0A%0A
 %09@Inject
@@ -1276,16 +1276,73 @@
 ption %7B%0A
+%09%09Long quantity = model.getSaleDescRow().getQuantity();%0A%0A
 %09%09if (mo
@@ -1444,44 +1444,16 @@
 %09%7C%7C 
-model.getSaleDescRow().getQ
+q
 uantity
-()
 .equ
@@ -1562,39 +1562,589 @@
 %0A%0A%09%09
-// XXX More validation checking
+if (quantity.compareTo(0L) %3C= 0)%0A%09%09%09throw new EntityValidationException(%0A%09%09%09%09%09%22Quantity shouldnt' be negative value%22);%0A%0A%09%09// Check if quantity exceeds stock%0A%09%09for (Stock stock : stockModel.getEntities())%0A%09%09%09if (stock.getWarehouse().compareTo(%0A%09%09%09%09%09model.getSaleDescRow().getWarehouse()) == 0%0A%09%09%09%09%09&& stock.getItem().compareTo(%0A%09%09%09%09%09%09%09model.getSaleDescRow().getItem()) == 0)%0A%0A%09%09%09%09if (stock.getQuantity().compareTo(quantity) %3C= 0)%0A%09%09%09%09%09throw new EntityValidationException(%22Only %22%0A%09%09%09%09%09%09%09+ stock.getQuantity() + %22 %22 + stock.getItem()%0A%09%09%09%09%09%09%09+ %22 available in %22 + stock.getWarehouse());
 %0A%09%7D%0A

Validate if sale quantity exceeds stock