github.com/schmorrison/Zoho@v1.1.4/invoice/create_invoice.go (about) 1 package invoice 2 3 import ( 4 "fmt" 5 6 zoho "github.com/schmorrison/Zoho" 7 ) 8 9 //https://www.zoho.com/invoice/api/v3/#Invoices_Create_an_invoice 10 //func (c *API) CreateInvoice(request interface{}, OrganizationID string, params map[string]zoho.Parameter) (data ListContactsResponse, err error) { 11 func (c *API) CreateInvoice(request interface{}) (data CreateInvoiceResponse, err error) { 12 13 endpoint := zoho.Endpoint{ 14 Name: InvoicesModule, 15 URL: fmt.Sprintf("https://invoice.zoho.%s/api/v3/%s", c.ZohoTLD, InvoicesModule), 16 Method: zoho.HTTPPost, 17 ResponseData: &CreateInvoiceResponse{}, 18 URLParameters: map[string]zoho.Parameter{ 19 "filter_by": "", 20 }, 21 RequestBody: request, 22 BodyFormat: zoho.JSON_STRING, 23 Headers: map[string]string{ 24 InvoiceAPIEndpointHeader: c.OrganizationID, 25 }, 26 } 27 28 /*for k, v := range params { 29 endpoint.URLParameters[k] = v 30 }*/ 31 32 err = c.Zoho.HTTPRequest(&endpoint) 33 if err != nil { 34 return CreateInvoiceResponse{}, fmt.Errorf("Failed to create invoice: %s", err) 35 } 36 37 // Mark the invoice as sent before returning details 38 if v, ok := endpoint.ResponseData.(*CreateInvoiceResponse); ok { 39 // Check if the creation succeeded 40 if v.Code != 0 { 41 return *v, fmt.Errorf("Failed to create invoice: %s", v.Message) 42 } 43 endpointSent := zoho.Endpoint{ 44 Name: InvoicesModule, 45 URL: fmt.Sprintf( 46 "https://invoice.zoho.%s/api/v3/%s/%s/status/sent", 47 c.ZohoTLD, 48 InvoicesModule, 49 v.Invoice.InvoiceId, 50 ), 51 Method: zoho.HTTPPost, 52 ResponseData: &InvoiceSent{}, 53 BodyFormat: zoho.JSON_STRING, 54 Headers: map[string]string{ 55 InvoiceAPIEndpointHeader: c.OrganizationID, 56 }, 57 } 58 err = c.Zoho.HTTPRequest(&endpointSent) 59 if err != nil { 60 return *v, fmt.Errorf("Failed to mark invoice as sent: %s", err) 61 } 62 return *v, nil 63 } 64 65 return CreateInvoiceResponse{}, fmt.Errorf("Data retrieved was not 'CreateInvoiceResponse'") 66 } 67 68 type CreateInvoiceRequest struct { 69 CustomerId string `json:"customer_id"` 70 //ContactName string `json:"contact_name,omitempty"` 71 ContactPersons []string `json:"contact_persons,omitempty"` 72 InvoiceNumber string `json:"invoice_number,omitempty"` 73 ReferenceNumber string `json:"reference_number,omitempty"` 74 PlaceOfSupply string `json:"place_of_supply,omitempty"` 75 GstTreatment string `json:"gst_treatment,omitempty"` 76 GstNo string `json:"gst_no,omitempty"` 77 TemplateId string `json:"template_id,omitempty"` 78 Date string `json:"date,omitempty"` 79 PaymentTerms int64 `json:"payment_terms,omitempty"` 80 PaymentTermsLabel string `json:"payment_terms_label,omitempty"` 81 DueDate string `json:"due_date,omitempty"` 82 Discount float64 `json:"discount,omitempty"` 83 IsDiscountBeforeTax bool `json:"is_discount_before_tax,omitempty"` 84 DiscountType string `json:"discount_type,omitempty"` 85 IsInclusiveTax bool `json:"is_inclusive_tax,omitempty"` 86 ExchangeRate float64 `json:"exchange_rate,omitempty"` 87 RecurringInvoiceId string `json:"recurring_invoice_id,omitempty"` 88 InvoicedEstimateId string `json:"invoiced_estimate_id,omitempty"` 89 SalespersonId string `json:"salesperson_id,omitempty"` 90 CustomFields []CustomFieldRequest `json:"custom_fields,omitempty"` 91 ProjectId string `json:"project_id,omitempty"` 92 LineItems []InvoiceLineItem `json:"line_items"` 93 PaymentOptions PaymentOptions `json:"payment_options"` 94 AllowPartialPayments bool `json:"allow_partial_payments"` 95 CustomBody string `json:"custom_body,omitempty"` 96 CustomSubject string `json:"custom_subject,omitempty"` 97 Notes string `json:"notes,omitempty"` 98 Terms string `json:"terms,omitempty"` 99 ShippingCharge float64 `json:"shipping_charge,omitempty"` 100 Adjustment float64 `json:"adjustment,omitempty"` 101 AdjustmentDescription string `json:"adjustment_description"` 102 Reason string `json:"reason,omitempty"` 103 TaxAuthorityId string `json:"tax_authority_id,omitempty"` 104 TaxExemptionId string `json:"tax_exemption_id,omitempty"` 105 } 106 107 type InvoiceSent struct { 108 Code int64 `json:"code"` 109 Message string `json:"message"` 110 } 111 112 type CreateInvoiceResponse struct { 113 Code int64 `json:"code"` 114 Message string `json:"message"` 115 Invoice struct { 116 InvoiceId string `json:"invoice_id"` 117 AchPaymentInitiated bool `json:"ach_payment_initiated"` 118 InvoiceNumber string `json:"invoice_number"` 119 IsPreGst bool `json:"is_pre_gst"` 120 PlaceOfSupply string `json:"place_of_supply"` 121 GstNo string `json:"gst_no"` 122 GstTreatment string `json:"gst_treatment"` 123 Date string `json:"date"` 124 Status string `json:"status"` 125 PaymentTerms int64 `json:"payment_terms"` 126 PaymentTermsLabel string `json:"payment_terms_label"` 127 DueDate string `json:"due_date"` 128 PaymentExpectedDate string `json:"payment_expected_date"` 129 LastPaymentDate string `json:"last_payment_date"` 130 ReferenceNumber string `json:"reference_number"` 131 CustomerId string `json:"customer_id"` 132 CustomerName string `json:"customer_name"` 133 ContactPersons []string `json:"contact_persons"` 134 CurrencyId string `json:"currency_id"` 135 CurrencyCode string `json:"currency_code"` 136 ExchangeRate float64 `json:"exchange_rate"` 137 Discount float64 `json:"discount"` 138 IsDiscountBeforeTax bool `json:"is_discount_before_tax"` 139 DiscountType string `json:"discount_type"` 140 IsInclusiveTax bool `json:"is_inclusive_tax"` 141 RecurringInvoiceId string `json:"recurring_invoice_id"` 142 IsViewedByClient bool `json:"is_viewed_by_client"` 143 HasAttachment bool `json:"has_attachment"` 144 ClientViewedTime string `json:"client_viewed_time"` 145 LineItems []InvoiceLineItem `json:"line_items"` 146 ShippingCharge float64 `json:"shipping_charge"` 147 Adjustment float64 `json:"adjustment"` 148 AdjustmentDescription string `json:"adjustment_description"` 149 SubTotal float64 `json:"sub_total"` 150 TaxTotal float64 `json:"tax_total"` 151 Total float64 `json:"total"` 152 Taxes []struct { 153 TaxName string `json:"tax_name"` 154 TaxAmount float64 `json:"tax_amount"` 155 } `json:"taxes"` 156 PaymentReminderEnabled bool `json:"payment_reminder_enabled"` 157 PaymentMade float64 `json:"payment_made"` 158 CreditsApplied float64 `json:"credits_applied"` 159 TaxAmountWithheld float64 `json:"tax_amount_withheld"` 160 Balance float64 `json:"balance"` 161 WriteOffAmount float64 `json:"write_off_amount"` 162 AllowPartialPayments bool `json:"allow_partial_payments"` 163 PricePrecision int64 `json:"price_precision"` 164 PaymentOptions PaymentOptions `json:"payment_options"` 165 IsEmailed bool `json:"is_emailed"` 166 RemindersSent int64 `json:"reminders_sent"` 167 LastReminderSentDate string `json:"last_reminder_sent_date"` 168 BillingAddress ContactAddress `json:"billing_address"` 169 ShippingAddress ContactAddress `json:"shipping_address"` 170 Notes string `json:"notes"` 171 Terms string `json:"terms"` 172 CustomFields []struct { 173 CustomfieldId string `json:"customfield_id"` 174 DataType string `json:"data_type"` 175 Index int64 `json:"index"` 176 Label string `json:"label"` 177 ShowOnPdf bool `json:"show_on_pdf"` 178 ShowInAllPdf bool `json:"show_in_all_pdf"` 179 Value string `json:"value"` 180 } `json:"custom_fields"` 181 TemplateId string `json:"template_id"` 182 TemplateName string `json:"template_name"` 183 CreatedTime string `json:"created_time"` 184 LastModifiedTime string `json:"last_modified_time"` 185 AttachmentName string `json:"attachment_name"` 186 CanSendInMail bool `json:"can_send_in_mail"` 187 SalespersonId string `json:"salesperson_id"` 188 SalespersonName string `json:"salesperson_name"` 189 InvoiceUrl string `json:"invoice_url"` 190 } `json:"invoice"` 191 } 192 193 type InvoiceLineItem struct { 194 LineItemId string `json:"line_item_id,omitempty"` 195 ItemId string `json:"item_id,omitempty"` 196 Description string `json:"description,omitempty"` 197 ProjectId string `json:"project_id,omitempty"` 198 ProjectName string `json:"project_name,omitempty"` 199 TimeEntryIds []string `json:"time_entry_ids,omitempty"` 200 ItemType string `json:"item_type,omitempty"` 201 ProductType string `json:"product_type,omitempty"` 202 ExpenseId string `json:"expense_id,omitempty"` 203 Name string `json:"name,omitempty"` 204 ItemOrder float64 `json:"item_order,omitempty"` 205 BcyRate float64 `json:"bcy_rate,omitempty"` 206 Rate float64 `json:"rate,omitempty"` 207 Quantity int64 `json:"quantity,omitempty"` 208 Unit string `json:"unit,omitempty"` 209 DiscountAmount float64 `json:"discount_amount,omitempty"` 210 Discount float64 `json:"discount,omitempty"` 211 TaxId string `json:"tax_id,omitempty"` 212 TaxExemptionId string `json:"tax_exemption_id,omitempty"` 213 TaxName string `json:"tax_name,omitempty"` 214 TaxType string `json:"tax_type,omitempty"` 215 TaxPercentage float64 `json:"tax_percentage,omitempty"` 216 ItemTotal float64 `json:"item_total,omitempty"` 217 HsnOrSac int64 `json:"hsn_or_sac,omitempty"` 218 ItemCustomFields []CustomFieldRequest `json:"item_custom_fields,omitempty"` 219 }