001/*
002 * Copyright 2010-2014 Ning, Inc.
003 * Copyright 2014-2015 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.ning.billing.recurly.model;
019
020import com.google.common.base.Objects;
021import org.joda.time.DateTime;
022
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlElementWrapper;
026import javax.xml.bind.annotation.XmlList;
027import java.math.BigDecimal;
028import java.util.List;
029
030@XmlRootElement(name = "adjustment")
031public class Adjustment extends RecurlyObject {
032
033    @XmlElement(name = "account")
034    private Account account;
035
036    @XmlElement(name = "subscription")
037    private Subscription subscription;
038
039    @XmlElement(name = "uuid")
040    private String uuid;
041
042    @XmlElement(name = "type")
043    private String type;
044
045    @XmlElement(name = "description")
046    private String description;
047
048    @XmlElement(name = "accounting_code")
049    private String accountingCode;
050
051    @XmlElement(name = "origin")
052    private String origin;
053
054    @XmlElement(name = "unit_amount_in_cents")
055    private Integer unitAmountInCents;
056
057    @XmlElement(name = "quantity")
058    private Integer quantity;
059
060    @XmlElement(name = "discount_in_cents")
061    private Integer discountInCents;
062
063    @XmlElement(name = "tax_in_cents")
064    private Integer taxInCents;
065
066    @XmlElement(name = "total_in_cents")
067    private Integer totalInCents;
068
069    @XmlElement(name = "currency")
070    private String currency;
071
072    @XmlElement(name = "taxable")
073    private Boolean taxable;
074
075    @XmlElement(name = "tax_type")
076    private String taxType;
077
078    @XmlElement(name = "tax_region")
079    private String taxRegion;
080
081    @XmlElement(name = "tax_rate")
082    private String taxRate;
083
084    @XmlElement(name = "tax_code")
085    private String taxCode;
086
087    @XmlElement(name = "tax_exempt")
088    private Boolean taxExempt;
089
090    @XmlList
091    @XmlElementWrapper(name = "tax_details")
092    private List<TaxDetail> taxDetails;
093
094    @XmlElement(name = "product_code")
095    private String productCode;
096
097    @XmlElement(name = "item_code")
098    private String itemCode;
099
100    @XmlElement(name = "external_sku")
101    private String externalSku;
102
103    @XmlElement(name = "start_date")
104    private DateTime startDate;
105
106    @XmlElement(name = "end_date")
107    private DateTime endDate;
108
109    @XmlElement(name = "created_at")
110    private DateTime createdAt;
111
112    @XmlElement(name = "updated_at")
113    private DateTime updatedAt;
114
115    @XmlElement(name = "revenue_schedule_type")
116    private RevenueScheduleType revenueScheduleType;
117
118    @XmlElement(name = "credit_reason_code")
119    private String creditReasonCode;
120
121    @XmlElement(name = "original_adjustment_uuid")
122    private String originalAdjustmentUuid;
123
124    @XmlElement(name = "shipping_address")
125    private ShippingAddress shippingAddress;
126
127    @XmlElement(name = "shipping_address_id")
128    private Long shippingAddressId;
129
130    @XmlElement(name = "refundable_total_in_cents")
131    private Integer refundableTotalInCents;
132
133    @XmlElement(name = "state")
134    private String state;
135
136    @XmlElement(name = "proration_rate")
137    private BigDecimal prorationRate;
138
139    @XmlElement(name = "surcharge_in_cents")
140    private Integer surchargeInCents;
141
142    public Account getAccount() {
143        if (account != null && account.getCreatedAt() == null) {
144            account = fetch(account, Account.class);
145        }
146        return account;
147    }
148
149    public void setAccount(final Account account) {
150        this.account = account;
151    }
152
153    public String getSubscriptionId() {
154        if (subscription != null && subscription.getHref() != null) {
155            String[] parts = subscription.getHref().split("/");
156            return parts[parts.length - 1];
157        }
158        return null;
159    }
160
161    public void setSubscription(final Subscription subscription) {
162        this.subscription = subscription;
163    }
164
165    public String getUuid() {
166        return uuid;
167    }
168
169    public void setUuid(final Object uuid) {
170        this.uuid = stringOrNull(uuid);
171    }
172
173    public String getType() {
174        return type;
175    }
176
177    public void setType(final Object type) {
178        this.type = stringOrNull(type);
179    }
180
181    public String getDescription() {
182        return description;
183    }
184
185    public void setDescription(final Object description) {
186        this.description = stringOrNull(description);
187    }
188
189    public String getAccountingCode() {
190        return accountingCode;
191    }
192
193    public void setAccountingCode(final Object accountingCode) {
194        this.accountingCode = stringOrNull(accountingCode);
195    }
196
197    public String getOrigin() {
198        return origin;
199    }
200
201    public void setOrigin(final Object origin) {
202        this.origin = stringOrNull(origin);
203    }
204
205    public Integer getUnitAmountInCents() {
206        return unitAmountInCents;
207    }
208
209    public void setUnitAmountInCents(final Object unitAmountInCents) {
210        this.unitAmountInCents = integerOrNull(unitAmountInCents);
211    }
212
213    public Integer getQuantity() {
214        return quantity;
215    }
216
217    public void setQuantity(final Object quantity) {
218        this.quantity = integerOrNull(quantity);
219    }
220
221    public Integer getDiscountInCents() {
222        return discountInCents;
223    }
224
225    public void setDiscountInCents(final Object discountInCents) {
226        this.discountInCents = integerOrNull(discountInCents);
227    }
228
229    public Integer getTaxInCents() {
230        return taxInCents;
231    }
232
233    public void setTaxInCents(final Object taxInCents) {
234        this.taxInCents = integerOrNull(taxInCents);
235    }
236
237    public Integer getTotalInCents() {
238        return totalInCents;
239    }
240
241    public void setTotalInCents(final Object totalInCents) {
242        this.totalInCents = integerOrNull(totalInCents);
243    }
244
245    public String getCurrency() {
246        return currency;
247    }
248
249    public void setCurrency(final Object currency) {
250        this.currency = stringOrNull(currency);
251    }
252
253    public Boolean getTaxable() {
254        return taxable;
255    }
256
257    public void setTaxable(final Object taxable) {
258        this.taxable = booleanOrNull(taxable);
259    }
260
261    public String getTaxType() {
262        return taxType;
263    }
264
265    public void setTaxType(final Object taxType) {
266        this.taxType = stringOrNull(taxType);
267    }
268
269    public String getTaxRegion() {
270        return taxRegion;
271    }
272
273    public void setTaxRegion(final Object taxRegion) {
274        this.taxRegion = stringOrNull(taxRegion);
275    }
276
277    public String getTaxRate() {
278        return taxRate;
279    }
280
281    public void setTaxRate(final Object taxRate) {
282        this.taxRate = stringOrNull(taxRate);
283    }
284
285    public String getTaxCode() {
286        return taxCode;
287    }
288
289    public void setTaxCode(final Object taxCode) {
290        this.taxCode = stringOrNull(taxCode);
291    }
292
293    public Boolean getTaxExempt() {
294        return taxExempt;
295    }
296
297    public void setTaxExempt(final Object taxExempt) {
298        this.taxExempt = booleanOrNull(taxExempt);
299    }
300
301    public List<TaxDetail> getTaxDetails() {
302        return taxDetails;
303    }
304
305    public void setTaxDetails(final List<TaxDetail> taxDetails) {
306        this.taxDetails = taxDetails;
307    }
308
309    public String getProductCode() { return productCode; }
310
311    public void setProductCode(final Object productCode) { this.productCode = stringOrNull(productCode); }
312
313    public String getItemCode() { return itemCode; }
314
315    public void setItemCode(final Object itemCode) { this.itemCode = stringOrNull(itemCode); }
316
317    public String getExternalSku() { return externalSku; }
318
319    public void setExternalSku(final Object externalSku) { this.externalSku = stringOrNull(externalSku); }
320
321    public DateTime getStartDate() {
322        return startDate;
323    }
324
325    public void setStartDate(final Object startDate) {
326        this.startDate = dateTimeOrNull(startDate);
327    }
328
329    public DateTime getEndDate() {
330        return endDate;
331    }
332
333    public void setEndDate(final Object endDate) {
334        this.endDate = dateTimeOrNull(endDate);
335    }
336
337    public DateTime getCreatedAt() {
338        return createdAt;
339    }
340
341    public void setCreatedAt(final Object createdAt) {
342        this.createdAt = dateTimeOrNull(createdAt);
343    }
344
345    public DateTime getUpdatedAt() {
346        return updatedAt;
347    }
348
349    public void setUpdatedAt(final Object updatedAt) {
350        this.updatedAt = dateTimeOrNull(updatedAt);
351    }
352
353    public AdjustmentRefund toAdjustmentRefund() {
354        final AdjustmentRefund adjustmentRefund = new AdjustmentRefund();
355        adjustmentRefund.setUuid(uuid);
356        adjustmentRefund.setQuantity(quantity);
357        adjustmentRefund.setProrate(false);
358        return adjustmentRefund;
359    }
360
361    public RevenueScheduleType getRevenueScheduleType() {
362        return revenueScheduleType;
363    }
364
365    public void setRevenueScheduleType(final Object revenueScheduleType) {
366        this.revenueScheduleType = enumOrNull(RevenueScheduleType.class, revenueScheduleType, true);
367    }
368
369    public String getCreditReasonCode() {
370        return creditReasonCode;
371    }
372
373    public void setCreditReasonCode(final Object creditReasonCode) {
374        this.creditReasonCode = stringOrNull(creditReasonCode);
375    }
376
377    public String getOriginalAdjustmentUuid() {
378        return originalAdjustmentUuid;
379    }
380
381    public void setOriginalAdjustmentUuid(final Object originalAdjustmentUuid) {
382        this.originalAdjustmentUuid = stringOrNull(originalAdjustmentUuid);
383    }
384
385    public ShippingAddress getShippingAddress() {
386        return shippingAddress;
387    }
388
389    public void setShippingAddress(final ShippingAddress shippingAddress) {
390        this.shippingAddress = shippingAddress;
391    }
392
393    public Long getShippingAddressId() {
394        return shippingAddressId;
395    }
396
397    public void setShippingAddressId(final Object shippingAddressId) {
398        this.shippingAddressId = longOrNull(shippingAddressId);
399    }
400
401    public Integer getRefundableTotalInCents() {
402        return refundableTotalInCents;
403    }
404
405    public void setRefundableTotalInCents(final Object refundableTotalInCents) {
406        this.refundableTotalInCents = integerOrNull(refundableTotalInCents);
407    }
408
409    public String getState() {
410        return state;
411    }
412
413    public void setState(final Object state) {
414        this.state = stringOrNull(state);
415    }
416
417    public BigDecimal getProrationRate() {
418        return prorationRate;
419    }
420
421    public void setProrationRate(final Object prorationRate) {
422        this.prorationRate = bigDecimalOrNull(prorationRate);
423    }
424
425    public Integer getSurchargeInCents() {
426        return surchargeInCents;
427    }
428
429    public void setSurchargeInCents(final Object surchargeInCents) {
430        this.surchargeInCents = integerOrNull(surchargeInCents);
431    }
432
433    @Override
434    public String toString() {
435        final StringBuilder sb = new StringBuilder();
436        sb.append("Adjustment");
437        sb.append("{account=").append(account);
438        sb.append(", uuid='").append(uuid).append('\'');
439        sb.append(", type=").append(type);
440        sb.append(", description='").append(description).append('\'');
441        sb.append(", accountingCode='").append(accountingCode).append('\'');
442        sb.append(", origin='").append(origin).append('\'');
443        sb.append(", unitAmountInCents=").append(unitAmountInCents);
444        sb.append(", quantity=").append(quantity);
445        sb.append(", discountInCents=").append(discountInCents);
446        sb.append(", taxInCents=").append(taxInCents);
447        sb.append(", taxType=").append(taxType);
448        sb.append(", taxRegion=").append(taxRegion);
449        sb.append(", taxRate=").append(taxRate);
450        sb.append(", taxCode=").append(taxCode);
451        sb.append(", taxExempt=").append(taxExempt);
452        sb.append(", taxDetails=").append(taxDetails);
453        sb.append(", totalInCents=").append(totalInCents);
454        sb.append(", currency='").append(currency).append('\'');
455        sb.append(", taxable=").append(taxable);
456        sb.append(", productCode=").append(productCode);
457        sb.append(", itemCode=").append(itemCode);
458        sb.append(", externalSku=").append(externalSku);
459        sb.append(", startDate=").append(startDate);
460        sb.append(", endDate=").append(endDate);
461        sb.append(", createdAt=").append(createdAt);
462        sb.append(", updatedAt=").append(updatedAt);
463        sb.append(", revenueScheduleType=").append(revenueScheduleType);
464        sb.append(", creditReasonCode=").append(creditReasonCode);
465        sb.append(", originalAdjustmentUuid=").append(originalAdjustmentUuid);
466        sb.append(", shippingAddress=").append(shippingAddress);
467        sb.append(", shippingAddressId=").append(shippingAddressId);
468        sb.append(", refundableTotalInCents=").append(refundableTotalInCents);
469        sb.append(", state=").append(state);
470        sb.append(", prorationRate=").append(prorationRate);
471        sb.append(", surchargeInCents=").append(surchargeInCents);
472        sb.append('}');
473        return sb.toString();
474    }
475
476    @Override
477    public boolean equals(final Object o) {
478        if (this == o) return true;
479        if (o == null || getClass() != o.getClass()) return false;
480
481        final Adjustment that = (Adjustment) o;
482
483        if (account != null ? !account.equals(that.account) : that.account != null) {
484            return false;
485        }
486        if (accountingCode != null ? !accountingCode.equals(that.accountingCode) : that.accountingCode != null) {
487            return false;
488        }
489        if (createdAt != null ? createdAt.compareTo(that.createdAt) != 0 : that.createdAt != null) {
490            return false;
491        }
492        if (currency != null ? !currency.equals(that.currency) : that.currency != null) {
493            return false;
494        }
495        if (creditReasonCode != null ? !creditReasonCode.equals(that.creditReasonCode) : that.creditReasonCode != null) {
496            return false;
497        }
498        if (description != null ? !description.equals(that.description) : that.description != null) {
499            return false;
500        }
501        if (discountInCents != null ? !discountInCents.equals(that.discountInCents) : that.discountInCents != null) {
502            return false;
503        }
504        if (endDate != null ? !endDate.equals(that.endDate) : that.endDate != null) {
505            return false;
506        }
507        if (origin != null ? !origin.equals(that.origin) : that.origin != null) {
508            return false;
509        }
510        if (originalAdjustmentUuid != null ? !originalAdjustmentUuid.equals(that.originalAdjustmentUuid) : that.originalAdjustmentUuid != null) {
511            return false;
512        }
513        if (productCode != null ? !productCode.equals(that.productCode) : that.productCode != null) {
514            return false;
515        }
516        if (itemCode != null ? !itemCode.equals(that.itemCode) : that.itemCode != null) {
517            return false;
518        }
519        if (externalSku != null ? !externalSku.equals(that.externalSku) : that.externalSku != null) {
520            return false;
521        }
522        if (quantity != null ? !quantity.equals(that.quantity) : that.quantity != null) {
523            return false;
524        }
525        if (shippingAddress != null ? !shippingAddress.equals(that.shippingAddress) : that.shippingAddress != null) {
526            return false;
527        }
528        if (shippingAddressId != null ? !shippingAddressId.equals(that.shippingAddressId) : that.shippingAddressId != null) {
529            return false;
530        }
531        if (startDate != null ? !startDate.equals(that.startDate) : that.startDate != null) {
532            return false;
533        }
534        if (surchargeInCents != null ? !surchargeInCents.equals(that.surchargeInCents) : that.surchargeInCents != null) {
535            return false;
536        }
537        if (taxInCents != null ? !taxInCents.equals(that.taxInCents) : that.taxInCents != null) {
538            return false;
539        }
540        if (taxable != null ? !taxable.equals(that.taxable) : that.taxable != null) {
541            return false;
542        }
543        if (type != null ? !type.equals(that.type) : that.type != null) {
544            return false;
545        }
546        if (taxType != null ? !taxType.equals(that.taxType) : that.taxType != null) {
547            return false;
548        }
549        if (taxRegion != null ? !taxRegion.equals(that.taxRegion) : that.taxRegion != null) {
550            return false;
551        }
552        if (taxRate != null ? !taxRate.equals(that.taxRate) : that.taxRate != null) {
553            return false;
554        }
555        if (taxCode != null ? !taxCode.equals(that.taxCode) : that.taxCode != null) {
556            return false;
557        }
558        if (taxExempt != null ? !taxExempt.equals(that.taxExempt) : that.taxExempt != null) {
559            return false;
560        }
561        if (taxDetails != null ? !taxDetails.equals(that.taxDetails) : that.taxDetails != null) {
562            return false;
563        }
564        if (totalInCents != null ? !totalInCents.equals(that.totalInCents) : that.totalInCents != null) {
565            return false;
566        }
567        if (unitAmountInCents != null ? !unitAmountInCents.equals(that.unitAmountInCents) : that.unitAmountInCents != null) {
568            return false;
569        }
570        if (uuid != null ? !uuid.equals(that.uuid) : that.uuid != null) {
571            return false;
572        }
573        if (updatedAt != null ? updatedAt.compareTo(that.updatedAt) != 0 : that.updatedAt != null) {
574            return false;
575        }
576        if (revenueScheduleType != null ? !revenueScheduleType.equals(that.revenueScheduleType) : that.revenueScheduleType != null) {
577            return false;
578        }
579        if (refundableTotalInCents != null ? !refundableTotalInCents.equals(that.refundableTotalInCents) : that.refundableTotalInCents != null) {
580            return false;
581        }
582        if (state != null ? !state.equals(that.state) : that.state != null) {
583            return false;
584        }
585        if (prorationRate != null ? !prorationRate.equals(that.prorationRate) : that.prorationRate != null) {
586            return false;
587        }
588        return true;
589    }
590
591    @Override
592    public int hashCode() {
593        return Objects.hashCode(
594                account,
595                uuid,
596                type,
597                description,
598                accountingCode,
599                origin,
600                unitAmountInCents,
601                quantity,
602                productCode,
603                itemCode,
604                externalSku,
605                discountInCents,
606                taxInCents,
607                totalInCents,
608                currency,
609                taxable,
610                taxType,
611                taxRegion,
612                taxRate,
613                taxCode,
614                taxExempt,
615                taxDetails,
616                startDate,
617                endDate,
618                createdAt,
619                updatedAt,
620                revenueScheduleType,
621                creditReasonCode,
622                originalAdjustmentUuid,
623                shippingAddress,
624                shippingAddressId,
625                refundableTotalInCents,
626                state,
627                prorationRate,
628                surchargeInCents
629        );
630    }
631
632}