Skip to content

Instantly share code, notes, and snippets.

@nocubicles
Created February 1, 2020 15:13
Show Gist options
  • Save nocubicles/b0bd7c78ab4816ed4fe136e6e80d3281 to your computer and use it in GitHub Desktop.
Save nocubicles/b0bd7c78ab4816ed4fe136e6e80d3281 to your computer and use it in GitHub Desktop.
ERPNext Random Data Generator
package main
import (
"encoding/csv"
"fmt"
"math/rand"
"net/http"
"os"
"strings"
)
func readCsv(filename string) ([][]string, error) {
f, err := os.Open(filename)
if err != nil {
return [][]string{}, err
}
defer f.Close()
lines, err := csv.NewReader(f).ReadAll()
if err != nil {
return [][]string{}, err
}
return lines, nil
}
func createCustomer(response chan<- bool, name string) {
url := "http://erp.integrated.ee/api/resource/Customer"
payload := strings.NewReader(fmt.Sprintf(`{"customer_name":"%s"}`, name))
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("authorization", "Basic xxx==")
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
response <- true
}
func createSalesOrder(response chan<- bool, customerName string, service string) {
url := "http://erp.integrated.ee/api/resource/Sales%20Order"
randomMonth := rand.Intn(12-1) + 1
randomDate := rand.Intn(28-1) + 1
randomPrice := rand.Intn(12003121-2121212) + 1
payload := strings.NewReader(fmt.Sprintf(
`{"owner":"Administrator",
"creation":"2020-01-02",
"title":"%s",
"customer":"%s",
"order_type":"Sales",
"delivery_date":"2020-%v-%v",
"customer_group":"All Customer Groups",
"conversion_rate": 1,
"selling_price_list":"Standard Selling",
"price_list_currency":"EUR",
"items": [{ "item_code":"100",
"item_name":"%s",
"ensure_delivery_based_on_produced_serial_no": 0,
"delivery_date":"2020-02-01",
"qty": 1, "uom":"Nos",
"conversion_factor": 1,
"rate": %v}] }`, service, customerName, randomMonth, randomDate, service, randomPrice))
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("authorization", "Basic xx==")
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
response <- true
}
func main() {
lines, err := readCsv("MOCK_DATA.csv")
if err != nil {
panic(err)
}
for _, line := range lines {
nameCreated := make(chan bool)
orderCreated := make(chan bool)
var fullName = line[1]
var serviceName = line[2]
go createCustomer(nameCreated, fullName)
<-nameCreated
go createSalesOrder(orderCreated, fullName, serviceName)
fmt.Println(<-orderCreated)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment