package app

import (
	"database/sql"
	"time"

	"code.tnxs.net/taxnexus/lib/api/ops/ops_models"
	"github.com/google/uuid"
)

// UnMarshalCashReceipt decodes swagger to a first class object
func UnMarshalCashReceipt(s *ops_models.CashReceipt) *CashReceipt {
	if s.ID == "" {
		s.ID = uuid.New().String()
	}
	createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate)
	lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate)
	cashReceiptDate, e2 := time.Parse(dateTimeFormat, s.CashReceiptDate)
	return &CashReceipt{
		ID:                 s.ID,
		AccountID:          s.AccountID,
		Amount:             s.Amount,
		AppliedAmount:      s.AppliedAmount,
		BillingContactID:   s.BillingContactID,
		CashReceiptNumber:  s.CashReceiptNumber,
		CreatedByID:        s.CreatedByID,
		CustomerID:         s.CustomerID,
		Description:        s.Description,
		Gateway:            s.Gateway,
		GatewayKey:         s.GatewayKey,
		GatewayMessage:     s.GatewayMessage,
		GatewayTransaction: s.GatewayTransaction,
		IsValid:            s.IsValid,
		LastModifiedByID:   s.LastModifiedByID,
		PartnerAccountID:   s.PartnerAccountID,
		PaymentMethodID:    s.PaymentMethodID,
		PeriodID:           s.PeriodID,
		Posted:             s.Posted,
		RecordType:         s.RecordType,
		Ref:                s.Ref,
		Rejected:           s.Rejected,
		Source:             s.Source,
		Status:             s.Status,
		TenantID:           s.TenantID,
		TemplateID:         s.TemplateID,
		Type:               s.Type,
		UnappliedAmount:    s.UnappliedAmount,
		ValidPayment:       s.ValidPayment,
		XeroID:             s.XeroID,
		BillingRunID:       s.BillingRunID,
		InvoiceID:          s.InvoiceID,
		CreatedDate: sql.NullTime{
			Time:  createdDate,
			Valid: e0 == nil,
		},
		LastModifiedDate: sql.NullTime{
			Time:  lastModfiedDate,
			Valid: e1 == nil,
		},
		CashReceiptDate: sql.NullTime{
			Time:  cashReceiptDate,
			Valid: e2 == nil,
		},
	}
}

// MarshalToSwagger encodes a first class object to swagger
func (obj *CashReceipt) MarshalToSwagger() *ops_models.CashReceipt {
	return &ops_models.CashReceipt{
		ID:                 obj.ID,
		AccountID:          obj.AccountID,
		Amount:             obj.Amount,
		AppliedAmount:      obj.AppliedAmount,
		BillingContactID:   obj.BillingContactID,
		BillingRunID:       obj.BillingRunID,
		CashReceiptDate:    obj.CashReceiptDate.Time.Format(dateTimeFormat),
		CashReceiptNumber:  obj.CashReceiptNumber,
		CreatedByID:        obj.CreatedByID,
		CreatedDate:        obj.CreatedDate.Time.Format(dateTimeFormat),
		CustomerID:         obj.CustomerID,
		Description:        obj.Description,
		Gateway:            obj.Gateway,
		GatewayKey:         obj.GatewayKey,
		GatewayMessage:     obj.GatewayMessage,
		GatewayTransaction: obj.GatewayTransaction,
		InvoiceID:          obj.InvoiceID,
		IsValid:            obj.IsValid,
		LastModifiedByID:   obj.LastModifiedByID,
		LastModifiedDate:   obj.LastModifiedDate.Time.Format(dateTimeFormat),
		PartnerAccountID:   obj.PartnerAccountID,
		PaymentMethodID:    obj.PaymentMethodID,
		PeriodID:           obj.PeriodID,
		Posted:             obj.Posted,
		RecordType:         obj.RecordType,
		Ref:                obj.Ref,
		Rejected:           obj.Rejected,
		Source:             obj.Source,
		Status:             obj.Status,
		TemplateID:         obj.TemplateID,
		TenantID:           obj.TenantID,
		Type:               obj.Type,
		UnappliedAmount:    obj.UnappliedAmount,
		ValidPayment:       obj.ValidPayment,
		XeroID:             obj.XeroID,
	}
}
