Skip to content

Commit

Permalink
Enhances ist-dsi#90. Adds api. Check supplier limit on After the Fact…
Browse files Browse the repository at this point in the history
…. Adds cpv and goodsServicies classification to after the fact.
  • Loading branch information
Joao Quiterio committed Mar 30, 2014
1 parent e29576c commit a5d3efd
Show file tree
Hide file tree
Showing 12 changed files with 2,893 additions and 2,268 deletions.
5 changes: 5 additions & 0 deletions expenditure-tracking/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
<artifactId>tomcat-jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.fenixedu</groupId>
<artifactId>fenixedu-commons</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<repositories>
Expand Down
8 changes: 8 additions & 0 deletions expenditure-tracking/src/main/dml/expenditure-tracking-1.dml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class acquisitions.Acquisition implements .pt.ist.bennu.core.domain.VirtualHostA
BigDecimal vatValue;
String description;
Boolean deletedState;
AcquisitionItemClassification classification;
}

class acquisitions.AcquisitionRequest extends acquisitions.RequestWithPayment {
Expand Down Expand Up @@ -723,6 +724,13 @@ relation AcquisitionAfterTheFactSupplier {
organization.Supplier playsRole supplier;
}

relation AcquisitionAfterTheFactCpvReference {
acquisitions.afterthefact.AcquisitionAfterTheFact playsRole acquisitionsAfterTheFact {
multiplicity *;
}
acquisitions.CPVReference playsRole cpvReference;
}

relation ExpenditureTrackingSystemAnnouncement {
ExpenditureTrackingSystem playsRole expenditureTrackingSystem;
announcements.Announcement playsRole announcements {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package module.webservice.api;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Set;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import module.finance.domain.SupplierContact;
import module.workflow.activities.ActivityInformation;
import module.workflow.activities.WorkflowActivity;
import module.workflow.domain.WorkflowProcess;
import module.workflow.domain.WorkflowSystem;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pt.ist.bennu.core.domain.MyOrg;
import pt.ist.bennu.core.domain.exceptions.DomainException;
import pt.ist.bennu.core.domain.util.Money;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.AcquisitionItemClassification;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.CPVReference;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.AfterTheFactAcquisitionProcess;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.AfterTheFactAcquisitionType;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.activities.DeleteAfterTheFactAcquisitionProcess;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.activities.EditAfterTheFactProcessActivityInformation.AfterTheFactAcquisitionProcessBean;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.simplified.activities.CancelAcquisitionRequest;
import pt.ist.expenditureTrackingSystem.domain.organization.Supplier;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

@Path("expenditures-tracking/v1")
public class ExpenditureAPIv1 {

private static final Logger logger = LoggerFactory.getLogger(ExpenditureAPIv1.class);

public final static String JSON_UTF8 = "application/json; charset=utf-8";

DateTimeFormatter formatDayHour = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");

public static final DateTimeFormatter formatDay = DateTimeFormat.forPattern("dd/MM/yyyy");
public static final SimpleDateFormat dataFormatDay = new SimpleDateFormat("dd/MM/yyyy");
public static final SimpleDateFormat dataFormatHour = new SimpleDateFormat("HH:mm");

private static Gson gson;

static {
gson = new GsonBuilder().setPrettyPrinting().create();
}

@GET
@Produces(JSON_UTF8)
@Path("suppliers")
public String suppliers() {
Set<Supplier> suppliers = MyOrg.getInstance().getSuppliersSet();
JsonArray toReturn = new JsonArray();
for (Supplier supplier : suppliers) {
if (supplier != null) {
JsonObject obj = new JsonObject();

obj.addProperty("supplierID", supplier.getExternalId());
obj.addProperty("fiscalID", supplier.getFiscalIdentificationCode());
obj.addProperty("name", supplier.getName());
obj.addProperty("shortName", supplier.getAbbreviatedName());
obj.addProperty("limit", supplier.getSupplierLimit().toFormatString());
JsonArray contacts = new JsonArray();
for (SupplierContact contact : supplier.getSupplierContactSet()) {
JsonObject contactObj = new JsonObject();
if (contact.getAddress() != null) {
JsonObject addressObj = new JsonObject();
addressObj.addProperty("line1", contact.getAddress().getLine1());
addressObj.addProperty("line2", contact.getAddress().getLine2());
addressObj.addProperty("country", contact.getAddress().getCountry());
contactObj.add("address", addressObj);
}
contactObj.addProperty("phone", contact.getPhone());
contactObj.addProperty("fax", contact.getFax());
contactObj.addProperty("email", contact.getEmail());
contacts.add(contactObj);
}
obj.add("contacts", contacts);
obj.addProperty("totalAllocated", supplier.getTotalAllocated().toFormatString());
JsonArray byCpv = new JsonArray();
for (CPVReference cpv : supplier.getAllocationsByCPVReference().keySet()) {
if (cpv != null) {
JsonObject cpvObj = new JsonObject();
cpvObj.addProperty("cpvCode", cpv.getCode());
cpvObj.addProperty("cpvDescription", cpv.getDescription());
cpvObj.addProperty("totalAllocated", supplier.getTotalAllocated(cpv).toFormatString());
byCpv.add(cpvObj);;

}
}
obj.add("allocationsByCPV", byCpv);
toReturn.add(obj);
}
}

return gson.toJson(toReturn);
}

@GET
@Produces(JSON_UTF8)
@Path("allocateFunds")
public String allocateFunds(@QueryParam("supplierID") String supplierID, @QueryParam("value") String value,
@QueryParam("valueVat") String valueVAT, @QueryParam("cpvCode") String cpvcode,
@QueryParam("goodsOrService") String goodsOrServices, @QueryParam("description") String description) {

AfterTheFactAcquisitionProcessBean bean = new AfterTheFactAcquisitionProcessBean();

Set<Supplier> suppliers = MyOrg.getInstance().getSuppliersSet();
Supplier supplier = null;

for (Supplier sup : suppliers) {
if (sup.getExternalId().equals(supplierID)) {
supplier = sup;
break;
}
}
if (supplier == null) {
throw newApplicationError(Status.NOT_FOUND, "resource_not_found", "Can't find the supplier : " + supplierID);
}

bean.setSupplier(supplier);
bean.setAfterTheFactAcquisitionType(AfterTheFactAcquisitionType.PURCHASE);

Money itemValue = new Money(value);
bean.setValue(itemValue);

double VAT = Double.parseDouble(valueVAT);
bean.setVatValue(new BigDecimal(VAT));

bean.setYear(new LocalDate().getYear());
bean.setDescription(description);
bean.setClassification(AcquisitionItemClassification.valueOf(goodsOrServices.toUpperCase()));
CPVReference cpvReference = CPVReference.getCPVCode(cpvcode);
if (cpvReference == null) {
throw newApplicationError(Status.NOT_FOUND, "resource_not_found", "Can't find the cpv code : " + cpvcode);
}
bean.setCpvReference(cpvReference);

AfterTheFactAcquisitionProcess process;
try {
process = AfterTheFactAcquisitionProcess.createNewAfterTheFactAcquisitionProcess(bean);
} catch (DomainException e) {
throw newApplicationError(Status.PRECONDITION_FAILED, "precondition_failed", e.getLocalizedMessage());
}
JsonObject obj = new JsonObject();
obj.addProperty("processID", process.getProcessNumber());

return gson.toJson(obj);
}

//change to put
@GET
@Produces(JSON_UTF8)
@Path("cancelFundAllocation")
public String cancelFundAllocation(@QueryParam("processID") String processID) {
WorkflowSystem ws = WorkflowSystem.getInstance();
Set<WorkflowProcess> processes = ws.getProcessesSet();
for (WorkflowProcess workflowProcess : processes) {
if (workflowProcess.getProcessNumber() != null) {
if (workflowProcess.getProcessNumber().equals(processID)) {
WorkflowActivity<WorkflowProcess, ActivityInformation<WorkflowProcess>> cancelAcquisitionRequest =
workflowProcess.getActivity(DeleteAfterTheFactAcquisitionProcess.class.getSimpleName());
if (cancelAcquisitionRequest == null) { //is not after the fact
cancelAcquisitionRequest = workflowProcess.getActivity(CancelAcquisitionRequest.class.getSimpleName());
}
try {
cancelAcquisitionRequest.execute(new ActivityInformation<WorkflowProcess>(workflowProcess,
cancelAcquisitionRequest));
} catch (Exception e) {
throw newApplicationError(Status.NOT_ACCEPTABLE, "cancelation_not_acceptable", e.getLocalizedMessage());
}
JsonObject obj = new JsonObject();
obj.addProperty("status", Status.OK.toString());
return gson.toJson(obj);
}
}
}
//No process was found
throw newApplicationError(Status.NOT_FOUND, "resource_not_found", "Can't find the process : " + processID);
}

private WebApplicationException newApplicationError(Status status, String error, String description) {
JSONObject errorObject = new JSONObject();
errorObject.put("error", error);
errorObject.put("description", description);
return new WebApplicationException(Response.status(status).entity(errorObject.toJSONString()).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import pt.ist.bennu.core.domain.exceptions.DomainException;
import pt.ist.bennu.core.domain.util.Money;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.AcquisitionItemClassification;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.CPVReference;
import pt.ist.expenditureTrackingSystem.domain.organization.Supplier;

/**
Expand All @@ -50,12 +52,19 @@ public AcquisitionAfterTheFact(final AfterTheFactAcquisitionProcess afterTheFact
public void edit(AfterTheFactAcquisitionType type, Money value, BigDecimal vatValue, Supplier supplier, String description) {
setDeletedState(Boolean.FALSE);
setAfterTheFactAcquisitionType(type);
setSupplier(supplier);
setValue(value);
setVatValue(vatValue);
setSupplier(supplier);
setDescription(description);
}

public void edit(AfterTheFactAcquisitionType type, Money value, BigDecimal vatValue, Supplier supplier, String description,
CPVReference cpvReference, AcquisitionItemClassification classification) {
edit(type, value, vatValue, supplier, description);
setClassification(classification);
setCpvReference(cpvReference);
}

public void delete() {
setDeletedState(Boolean.TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static AfterTheFactAcquisitionProcess createNewAfterTheFactAcquisitionPro
final AfterTheFactAcquisitionProcess afterTheFactAcquisitionProcess = new AfterTheFactAcquisitionProcess();
afterTheFactAcquisitionProcess.edit(afterTheFactAcquisitionProcessBean.getAfterTheFactAcquisitionType(),
afterTheFactAcquisitionProcessBean.getValue(), afterTheFactAcquisitionProcessBean.getVatValue(),
afterTheFactAcquisitionProcessBean.getSupplier(), afterTheFactAcquisitionProcessBean.getDescription());
afterTheFactAcquisitionProcessBean.getSupplier(), afterTheFactAcquisitionProcessBean.getDescription(),
afterTheFactAcquisitionProcessBean.getCpvReference(), afterTheFactAcquisitionProcessBean.getClassification());
final Person loggedPerson = Person.getLoggedPerson();
new LabelLog(afterTheFactAcquisitionProcess, loggedPerson.getUser(), "label."
+ afterTheFactAcquisitionProcess.getClass().getName() + ".Create", "resources/AcquisitionResources");
Expand All @@ -98,9 +99,10 @@ protected int getYearForConstruction() {
return threadLocal.get().getYear().intValue();
}

public void edit(AfterTheFactAcquisitionType type, Money value, BigDecimal vatValue, Supplier supplier, String description) {
public void edit(AfterTheFactAcquisitionType type, Money value, BigDecimal vatValue, Supplier supplier, String description,
CPVReference cpvReference, AcquisitionItemClassification classification) {
final AcquisitionAfterTheFact acquisitionAfterTheFact = getAcquisitionAfterTheFact();
acquisitionAfterTheFact.edit(type, value, vatValue, supplier, description);
acquisitionAfterTheFact.edit(type, value, vatValue, supplier, description, cpvReference, classification);
}

public void delete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public ActivityInformation<AfterTheFactAcquisitionProcess> getActivityInformatio
protected void process(EditAfterTheFactProcessActivityInformation activityInformation) {
activityInformation.getProcess().edit(activityInformation.getAfterTheFactAcquisitionType(),
activityInformation.getValue(), activityInformation.getVatValue(), activityInformation.getSupplier(),
activityInformation.getDescription());
activityInformation.getDescription(), activityInformation.getCpvReference(),
activityInformation.getClassification());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.joda.time.LocalDate;

import pt.ist.bennu.core.domain.util.Money;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.AcquisitionItemClassification;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.CPVReference;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.AcquisitionAfterTheFact;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.AfterTheFactAcquisitionProcess;
import pt.ist.expenditureTrackingSystem.domain.acquisitions.afterthefact.AfterTheFactAcquisitionType;
Expand All @@ -55,6 +57,8 @@ public static class AfterTheFactAcquisitionProcessBean implements Serializable {
private AfterTheFactAcquisitionProcess afterTheFactAcquisitionProcess;
private String description;
private Integer year = Integer.valueOf(new LocalDate().getYear());
private AcquisitionItemClassification classification;
private CPVReference cpvReference;

public AfterTheFactAcquisitionProcessBean() {

Expand All @@ -69,6 +73,24 @@ public AfterTheFactAcquisitionProcessBean(final AfterTheFactAcquisitionProcess a
setVatValue(acquisitionAfterTheFact.getVatValue());
setDescription(acquisitionAfterTheFact.getDescription());
setYear(acquisitionAfterTheFact.getAfterTheFactAcquisitionProcess().getYear());
setClassification(acquisitionAfterTheFact.getClassification());
setCpvReference(acquisitionAfterTheFact.getCpvReference());
}

public CPVReference getCpvReference() {
return cpvReference;
}

public void setCpvReference(CPVReference cpvReference) {
this.cpvReference = cpvReference;
}

public AcquisitionItemClassification getClassification() {
return classification;
}

public void setClassification(AcquisitionItemClassification classification) {
this.classification = classification;
}

public void setSupplier(Supplier supplier) {
Expand Down Expand Up @@ -198,4 +220,12 @@ public boolean hasAllneededInfo() {
&& getValue() != null && getVatValue() != null;
}

public CPVReference getCpvReference() {
return this.bean.getCpvReference();
}

public AcquisitionItemClassification getClassification() {
return this.bean.getClassification();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,17 @@ protected SimplifiedProcedureProcess(Supplier supplier, Person person) {
protected SimplifiedProcedureProcess(ProcessClassification classification, List<Supplier> suppliers, Person person) {
super();
inGenesis();
System.out.println("Creating aquisition request ");
AcquisitionRequest acquisitionRequest = new AcquisitionRequest(this, suppliers, person);
System.out.println("Created aquisition request Suppliers size =" + suppliers.size());
if (suppliers.size() == 0) {
throw new DomainException("acquisitionProcess.message.exception.needsMoreSuppliers",
DomainException.getResourceFor("resources/AcquisitionResources"));
}
if (suppliers.size() == 1) {
acquisitionRequest.setSelectedSupplier(suppliers.get(0));
}
System.out.println("Setting process classification");
setProcessClassification(classification);
}

Expand All @@ -285,13 +288,18 @@ public static SimplifiedProcedureProcess createNewAcquisitionProcess(
SimplifiedProcedureProcess process =
new SimplifiedProcedureProcess(createAcquisitionProcessBean.getClassification(),
createAcquisitionProcessBean.getSuppliers(), createAcquisitionProcessBean.getRequester());
System.out.println("Line 288 ");
AcquisitionRequest acquisitionRequest = process.getAcquisitionRequest();
System.out.println("Line 290 ");
acquisitionRequest.setRequestingUnit(createAcquisitionProcessBean.getRequestingUnit());
System.out.println("Line 292 ");
acquisitionRequest.setContractSimpleDescription(createAcquisitionProcessBean.getContractSimpleDescription());
System.out.println("Line 294 ");
if (createAcquisitionProcessBean.isRequestUnitPayingUnit()) {
final Unit unit = createAcquisitionProcessBean.getRequestingUnit();
acquisitionRequest.addFinancers(unit.finance(acquisitionRequest));
}
System.out.println("Line 299 ");
if (createAcquisitionProcessBean.isForMission()) {
if (createAcquisitionProcessBean.getMissionProcess() == null) {
throw new DomainException("mission.process.is.mandatory",
Expand Down
Loading

0 comments on commit a5d3efd

Please sign in to comment.