|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import quickbooks from "../../quickbooks.app.mjs"; |
| 3 | +import { parseLineItems } from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "quickbooks-create-invoice-advanced", |
| 7 | + name: "Create Invoice - Advanced", |
| 8 | + description: "Creates an invoice with all available fields. [See the documentation](https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/invoice#create-an-invoice)", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + quickbooks, |
| 13 | + customerRefValue: { |
| 14 | + propDefinition: [ |
| 15 | + quickbooks, |
| 16 | + "customer", |
| 17 | + ], |
| 18 | + }, |
| 19 | + billEmail: { |
| 20 | + type: "string", |
| 21 | + label: "Bill Email", |
| 22 | + description: "Email address where the invoice should be sent", |
| 23 | + optional: true, |
| 24 | + }, |
| 25 | + dueDate: { |
| 26 | + type: "string", |
| 27 | + label: "Due Date", |
| 28 | + description: "Date when the payment of the transaction is due (YYYY-MM-DD)", |
| 29 | + optional: true, |
| 30 | + }, |
| 31 | + allowOnlineCreditCardPayment: { |
| 32 | + type: "boolean", |
| 33 | + label: "Allow Online Credit Card Payment", |
| 34 | + description: "Allow online credit card payment", |
| 35 | + optional: true, |
| 36 | + }, |
| 37 | + allowOnlineACHPayment: { |
| 38 | + type: "boolean", |
| 39 | + label: "Allow Online Bank Transfer Payment", |
| 40 | + description: "Allow online bank transfer payment", |
| 41 | + optional: true, |
| 42 | + }, |
| 43 | + currencyRefValue: { |
| 44 | + propDefinition: [ |
| 45 | + quickbooks, |
| 46 | + "currency", |
| 47 | + ], |
| 48 | + }, |
| 49 | + docNumber: { |
| 50 | + type: "string", |
| 51 | + label: "Document Number", |
| 52 | + description: "Reference number for the transaction", |
| 53 | + optional: true, |
| 54 | + }, |
| 55 | + billAddr: { |
| 56 | + type: "object", |
| 57 | + label: "Billing Address", |
| 58 | + description: "Billing address details", |
| 59 | + optional: true, |
| 60 | + }, |
| 61 | + shipAddr: { |
| 62 | + type: "object", |
| 63 | + label: "Shipping Address", |
| 64 | + description: "Shipping address details", |
| 65 | + optional: true, |
| 66 | + }, |
| 67 | + trackingNum: { |
| 68 | + type: "string", |
| 69 | + label: "Tracking Number", |
| 70 | + description: "Shipping tracking number", |
| 71 | + optional: true, |
| 72 | + }, |
| 73 | + privateNote: { |
| 74 | + type: "string", |
| 75 | + label: "Private Note", |
| 76 | + description: "Private note for internal use", |
| 77 | + optional: true, |
| 78 | + }, |
| 79 | + customerMemo: { |
| 80 | + type: "string", |
| 81 | + label: "Customer Memo", |
| 82 | + description: "Memo visible to customer", |
| 83 | + optional: true, |
| 84 | + }, |
| 85 | + taxCodeId: { |
| 86 | + propDefinition: [ |
| 87 | + quickbooks, |
| 88 | + "taxCodeId", |
| 89 | + ], |
| 90 | + }, |
| 91 | + lineItemsAsObjects: { |
| 92 | + propDefinition: [ |
| 93 | + quickbooks, |
| 94 | + "lineItemsAsObjects", |
| 95 | + ], |
| 96 | + reloadProps: true, |
| 97 | + }, |
| 98 | + }, |
| 99 | + async additionalProps() { |
| 100 | + const props = {}; |
| 101 | + if (this.lineItemsAsObjects) { |
| 102 | + props.lineItems = { |
| 103 | + type: "string[]", |
| 104 | + label: "Line Items", |
| 105 | + description: "Line items of an invoice. Set DetailType to `SalesItemLineDetail`, `GroupLineDetail`, or `DescriptionOnly`. Example: `{ \"DetailType\": \"SalesItemLineDetail\", \"Amount\": 100.0, \"SalesItemLineDetail\": { \"ItemRef\": { \"name\": \"Services\", \"value\": \"1\" } } }`", |
| 106 | + }; |
| 107 | + return props; |
| 108 | + } |
| 109 | + props.numLineItems = { |
| 110 | + type: "integer", |
| 111 | + label: "Number of Line Items", |
| 112 | + description: "The number of line items to enter", |
| 113 | + reloadProps: true, |
| 114 | + }; |
| 115 | + if (!this.numLineItems) { |
| 116 | + return props; |
| 117 | + } |
| 118 | + for (let i = 1; i <= this.numLineItems; i++) { |
| 119 | + props[`item_${i}`] = { |
| 120 | + type: "string", |
| 121 | + label: `Line ${i} - Item ID`, |
| 122 | + options: async ({ page }) => { |
| 123 | + return this.quickbooks.getPropOptions({ |
| 124 | + page, |
| 125 | + resource: "Item", |
| 126 | + mapper: ({ |
| 127 | + Id: value, Name: label, |
| 128 | + }) => ({ |
| 129 | + value, |
| 130 | + label, |
| 131 | + }), |
| 132 | + }); |
| 133 | + }, |
| 134 | + }; |
| 135 | + props[`amount_${i}`] = { |
| 136 | + type: "string", |
| 137 | + label: `Line ${i} - Amount`, |
| 138 | + }; |
| 139 | + } |
| 140 | + return props; |
| 141 | + }, |
| 142 | + methods: { |
| 143 | + buildLineItems() { |
| 144 | + const lineItems = []; |
| 145 | + for (let i = 1; i <= this.numLineItems; i++) { |
| 146 | + lineItems.push({ |
| 147 | + DetailType: "SalesItemLineDetail", |
| 148 | + Amount: this[`amount_${i}`], |
| 149 | + SalesItemLineDetail: { |
| 150 | + ItemRef: { |
| 151 | + value: this[`item_${i}`], |
| 152 | + }, |
| 153 | + }, |
| 154 | + }); |
| 155 | + } |
| 156 | + return lineItems; |
| 157 | + }, |
| 158 | + }, |
| 159 | + async run({ $ }) { |
| 160 | + if ((!this.numLineItems && !this.lineItemsAsObjects) || !this.customerRefValue) { |
| 161 | + throw new ConfigurationError("Must provide lineItems, and customerRefValue parameters."); |
| 162 | + } |
| 163 | + |
| 164 | + const lines = this.lineItemsAsObjects |
| 165 | + ? parseLineItems(this.lineItems) |
| 166 | + : this.buildLineItems(); |
| 167 | + |
| 168 | + lines.forEach((line) => { |
| 169 | + if (line.DetailType !== "SalesItemLineDetail" && line.DetailType !== "GroupLineDetail" && line.DetailType !== "DescriptionOnly") { |
| 170 | + throw new ConfigurationError("Line Item DetailType must be `SalesItemLineDetail`, `GroupLineDetail`, or `DescriptionOnly`"); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + const params = {}; |
| 175 | + const data = { |
| 176 | + Line: lines, |
| 177 | + CustomerRef: { |
| 178 | + value: this.customerRefValue, |
| 179 | + }, |
| 180 | + DueDate: this.dueDate, |
| 181 | + AllowOnlineCreditCardPayment: this.allowOnlineCreditCardPayment, |
| 182 | + AllowOnlineACHPayment: this.allowOnlineACHPayment, |
| 183 | + DocNumber: this.docNumber, |
| 184 | + BillAddr: this.billAddr, |
| 185 | + ShipAddr: this.shipAddr, |
| 186 | + TrackingNum: this.trackingNum, |
| 187 | + PrivateNote: this.privateNote, |
| 188 | + }; |
| 189 | + |
| 190 | + if (this.billEmail) { |
| 191 | + params.include = "invoiceLink"; |
| 192 | + data.BillEmail = { |
| 193 | + Address: this.billEmail, |
| 194 | + }; |
| 195 | + } |
| 196 | + if (this.currencyRefValue) { |
| 197 | + data.CurrencyRef = { |
| 198 | + value: this.currencyRefValue, |
| 199 | + }; |
| 200 | + } |
| 201 | + if (this.customerMemo) { |
| 202 | + data.CustomerMemo = { |
| 203 | + value: this.customerMemo, |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + const response = await this.quickbooks.createInvoice({ |
| 208 | + $, |
| 209 | + params, |
| 210 | + data, |
| 211 | + }); |
| 212 | + |
| 213 | + if (response) { |
| 214 | + $.export("summary", `Successfully created invoice with ID ${response.Invoice.Id}`); |
| 215 | + } |
| 216 | + |
| 217 | + return response; |
| 218 | + }, |
| 219 | +}; |
0 commit comments