harvest_api/request/
create_invoice.rs

1use serde_json::json;
2use crate::model::*;
3use crate::HarvestClient;
4/**Create this with the associated client method.
5
6That method takes required values as arguments. Set optional values using builder methods on this struct.*/
7pub struct CreateInvoiceRequest<'a> {
8    pub(crate) client: &'a HarvestClient,
9    pub client_id: Option<i64>,
10    pub retainer_id: Option<i64>,
11    pub estimate_id: Option<i64>,
12    pub number: Option<String>,
13    pub purchase_order: Option<String>,
14    pub tax: Option<f64>,
15    pub tax2: Option<f64>,
16    pub discount: Option<f64>,
17    pub subject: Option<String>,
18    pub notes: Option<String>,
19    pub currency: Option<String>,
20    pub issue_date: Option<String>,
21    pub due_date: Option<String>,
22    pub payment_term: Option<String>,
23    pub line_items_import: Option<serde_json::Value>,
24    pub line_items: Option<Vec<serde_json::Value>>,
25}
26impl<'a> CreateInvoiceRequest<'a> {
27    pub async fn send(self) -> anyhow::Result<Invoice> {
28        let mut r = self.client.client.post("/invoices");
29        if let Some(ref unwrapped) = self.client_id {
30            r = r.push_json(json!({ "client_id" : unwrapped }));
31        }
32        if let Some(ref unwrapped) = self.retainer_id {
33            r = r.push_json(json!({ "retainer_id" : unwrapped }));
34        }
35        if let Some(ref unwrapped) = self.estimate_id {
36            r = r.push_json(json!({ "estimate_id" : unwrapped }));
37        }
38        if let Some(ref unwrapped) = self.number {
39            r = r.push_json(json!({ "number" : unwrapped }));
40        }
41        if let Some(ref unwrapped) = self.purchase_order {
42            r = r.push_json(json!({ "purchase_order" : unwrapped }));
43        }
44        if let Some(ref unwrapped) = self.tax {
45            r = r.push_json(json!({ "tax" : unwrapped }));
46        }
47        if let Some(ref unwrapped) = self.tax2 {
48            r = r.push_json(json!({ "tax2" : unwrapped }));
49        }
50        if let Some(ref unwrapped) = self.discount {
51            r = r.push_json(json!({ "discount" : unwrapped }));
52        }
53        if let Some(ref unwrapped) = self.subject {
54            r = r.push_json(json!({ "subject" : unwrapped }));
55        }
56        if let Some(ref unwrapped) = self.notes {
57            r = r.push_json(json!({ "notes" : unwrapped }));
58        }
59        if let Some(ref unwrapped) = self.currency {
60            r = r.push_json(json!({ "currency" : unwrapped }));
61        }
62        if let Some(ref unwrapped) = self.issue_date {
63            r = r.push_json(json!({ "issue_date" : unwrapped }));
64        }
65        if let Some(ref unwrapped) = self.due_date {
66            r = r.push_json(json!({ "due_date" : unwrapped }));
67        }
68        if let Some(ref unwrapped) = self.payment_term {
69            r = r.push_json(json!({ "payment_term" : unwrapped }));
70        }
71        if let Some(ref unwrapped) = self.line_items_import {
72            r = r.push_json(json!({ "line_items_import" : unwrapped }));
73        }
74        if let Some(ref unwrapped) = self.line_items {
75            r = r.push_json(json!({ "line_items" : unwrapped }));
76        }
77        r = self.client.authenticate(r);
78        let res = r.send().await.unwrap().error_for_status();
79        match res {
80            Ok(res) => res.json().await.map_err(|e| anyhow::anyhow!("{:?}", e)),
81            Err(res) => {
82                let text = res.text().await.map_err(|e| anyhow::anyhow!("{:?}", e))?;
83                Err(anyhow::anyhow!("{:?}", text))
84            }
85        }
86    }
87    pub fn client_id(mut self, client_id: i64) -> Self {
88        self.client_id = Some(client_id);
89        self
90    }
91    pub fn retainer_id(mut self, retainer_id: i64) -> Self {
92        self.retainer_id = Some(retainer_id);
93        self
94    }
95    pub fn estimate_id(mut self, estimate_id: i64) -> Self {
96        self.estimate_id = Some(estimate_id);
97        self
98    }
99    pub fn number(mut self, number: &str) -> Self {
100        self.number = Some(number.to_owned());
101        self
102    }
103    pub fn purchase_order(mut self, purchase_order: &str) -> Self {
104        self.purchase_order = Some(purchase_order.to_owned());
105        self
106    }
107    pub fn tax(mut self, tax: f64) -> Self {
108        self.tax = Some(tax);
109        self
110    }
111    pub fn tax2(mut self, tax2: f64) -> Self {
112        self.tax2 = Some(tax2);
113        self
114    }
115    pub fn discount(mut self, discount: f64) -> Self {
116        self.discount = Some(discount);
117        self
118    }
119    pub fn subject(mut self, subject: &str) -> Self {
120        self.subject = Some(subject.to_owned());
121        self
122    }
123    pub fn notes(mut self, notes: &str) -> Self {
124        self.notes = Some(notes.to_owned());
125        self
126    }
127    pub fn currency(mut self, currency: &str) -> Self {
128        self.currency = Some(currency.to_owned());
129        self
130    }
131    pub fn issue_date(mut self, issue_date: &str) -> Self {
132        self.issue_date = Some(issue_date.to_owned());
133        self
134    }
135    pub fn due_date(mut self, due_date: &str) -> Self {
136        self.due_date = Some(due_date.to_owned());
137        self
138    }
139    pub fn payment_term(mut self, payment_term: &str) -> Self {
140        self.payment_term = Some(payment_term.to_owned());
141        self
142    }
143    pub fn line_items_import(mut self, line_items_import: serde_json::Value) -> Self {
144        self.line_items_import = Some(line_items_import);
145        self
146    }
147    pub fn line_items(mut self, line_items: Vec<serde_json::Value>) -> Self {
148        self.line_items = Some(line_items);
149        self
150    }
151}