-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added credit notes and attachements to invoice object
- Loading branch information
Showing
5 changed files
with
308 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
package com.invoiced.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.invoiced.exception.EntityException; | ||
import com.invoiced.util.Util; | ||
|
||
|
||
public class CreditNote extends AbstractEntity<CreditNote> { | ||
|
||
public CreditNote(Connection conn) { | ||
super(conn, CreditNote.class); | ||
} | ||
|
||
CreditNote() { | ||
super(CreditNote.class); | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected long getEntityId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected String getEntityName() { | ||
return "credit_notes"; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected boolean hasCRUD() { | ||
return true; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected boolean hasList() { | ||
return true; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected boolean isSubEntity() { | ||
return false; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected void setParentID(long parentID) { | ||
|
||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
protected long getParentID() { | ||
return -1; | ||
} | ||
|
||
@JsonInclude(JsonInclude.Include.NON_DEFAULT) | ||
@JsonProperty("id") | ||
public long id; | ||
|
||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public String object; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_DEFAULT) | ||
@JsonProperty("customer") | ||
public long customer; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_DEFAULT) | ||
public long invoice; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("name") | ||
public String name; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("number") | ||
public String number; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("currency") | ||
public String currency; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("draft") | ||
public boolean draft; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("closed") | ||
public boolean closed; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public boolean paid; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public String status; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_DEFAULT) | ||
@JsonProperty("date") | ||
public long date; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("items") | ||
public LineItem[] items; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("notes") | ||
public String notes; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public double subtotal; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("discounts") | ||
public Discount[] discounts; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("taxes") | ||
public Tax[] taxes; | ||
|
||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public double total; | ||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public double balance; | ||
|
||
|
||
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | ||
public String url; | ||
|
||
@JsonProperty(value = "pdf_url", access = JsonProperty.Access.WRITE_ONLY) | ||
public String pdfUrl; | ||
|
||
@JsonProperty(value = "created_at", access = JsonProperty.Access.WRITE_ONLY) | ||
public long createdAt; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("metadata") | ||
public Object metadata; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
@JsonProperty("attachments") | ||
public long[] attachments; | ||
|
||
|
||
@JsonIgnore | ||
public Email[] send(EmailRequest emailRequest) throws EntityException { | ||
|
||
String url = this.getConnection().baseUrl() + "/" + this.getEntityName() + "/" | ||
+ String.valueOf(this.getEntityId()) + "/emails"; | ||
|
||
Email[] emails = null; | ||
|
||
try { | ||
|
||
String emailRequestJson = emailRequest.toJsonString(); | ||
|
||
String response = this.getConnection().post(url, null, emailRequestJson); | ||
|
||
emails = Util.getMapper().readValue(response, Email[].class); | ||
|
||
} catch (Throwable c) { | ||
|
||
throw new EntityException(c); | ||
} | ||
|
||
return emails; | ||
} | ||
|
||
|
||
@JsonIgnore | ||
public Attachment[] listAttachments() throws EntityException { | ||
|
||
String url = this.getConnection().baseUrl() + "/" + this.getEntityName() + "/" | ||
+ String.valueOf(this.getEntityId()) + "/attachments"; | ||
|
||
Attachment[] attachments = null; | ||
|
||
try { | ||
|
||
String response = this.getConnection().post(url, null, ""); | ||
|
||
attachments = Util.getMapper().readValue(response, Attachment[].class); | ||
|
||
} catch (Throwable c) { | ||
|
||
throw new EntityException(c); | ||
} | ||
|
||
return attachments; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
package com.invoiced.entity; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
import java.sql.Timestamp; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerationException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
|
||
public class CreditNoteTest { | ||
|
||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(); | ||
|
||
@Test | ||
public void testParentID() { | ||
Connection conn = new Connection("", true); | ||
conn.testModeOn(); | ||
|
||
CreditNote creditNote = conn.newCreditNote(); | ||
assertTrue("Credit Note Parent Id is incorrect", creditNote .getParentID() == -1); | ||
creditNote.setParentID(-4); | ||
assertTrue("Credit Note Parent Id is incorrect", creditNote .getParentID() == -1); | ||
|
||
} | ||
|
||
|
||
|
||
@Test | ||
public void testJsonSerialization() { | ||
new CreditNote(null); | ||
|
||
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
|
||
try { | ||
String jsonInString = "{\"id\":2048,\"object\":\"credit_note\",\"customer\":15444,\"invoice\":46225,\"name\":null,\"currency\":\"usd\",\"draft\":false,\"closed\":false,\"paid\":false,\"status\":\"open\",\"number\":\"CN-0016\",\"date\":1416290400,\"items\":[{\"id\":7,\"object\":\"line_item\",\"catalog_item\":null,\"type\":\"product\",\"name\":\"Copy Paper, Case\",\"description\":null,\"quantity\":1,\"unit_cost\":45,\"amount\":45,\"discountable\":true,\"discounts\":[],\"taxable\":true,\"taxes\":[],\"metadata\":{}},{\"id\":8,\"object\":\"line_item\",\"catalog_item\":\"delivery\",\"type\":\"service\",\"name\":\"Delivery\",\"description\":null,\"quantity\":1,\"unit_cost\":10,\"amount\":10,\"discountable\":true,\"discounts\":[],\"taxable\":true,\"taxes\":[],\"metadata\":{}}],\"notes\":null,\"subtotal\":55,\"discounts\":[],\"taxes\":[{\"id\":20554,\"object\":\"tax\",\"amount\":3.85,\"tax_rate\":null}],\"total\":51.15,\"balance\":51.15,\"url\":\"https://dundermifflin.invoiced.com/credit_notes/IZmXbVOPyvfD3GPBmyd6FwXY\",\"pdf_url\":\"https://dundermifflin.invoiced.com/credit_notes/IZmXbVOPyvfD3GPBmyd6FwXY/pdf\",\"created_at\":1415229884,\"metadata\":{}}"; | ||
|
||
CreditNote i1 = mapper.readValue(jsonInString, CreditNote.class); | ||
|
||
assertTrue("Id is incorrect", i1.id == 2048L); | ||
assertTrue("Customer is incorrect", i1.customer == 15444L); | ||
assertTrue("Name is incorrect", i1.name == null); | ||
assertTrue("Object is credit_note", i1.name == null); | ||
assertTrue("Invoice is incorrect", i1.invoice == 46225L); | ||
assertTrue("Currency is incorrect", i1.currency.equals("usd")); | ||
assertTrue("Draft is incorrect", i1.draft == false); | ||
assertTrue("Closed is incorrect", i1.closed == false); | ||
assertTrue("Paid is incorrect", i1.paid == false); | ||
assertTrue("Status is incorrect", i1.status.equals("open")); | ||
|
||
|
||
assertTrue("Number is incorrect", i1.number.equals("CN-0016")); | ||
|
||
assertTrue("Date is incorrect", i1.date == 1416290400L); | ||
|
||
|
||
assertTrue("Metadata is incorrect", i1.metadata != null); | ||
|
||
assertTrue("There should be 2 items", i1.items.length == 2); | ||
|
||
assertTrue("Line item amount should be 45.0", i1.items[0].amount == 45.0); | ||
|
||
} catch (JsonGenerationException e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} catch (JsonMappingException e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} | ||
} | ||
|
||
|
||
} |