├── .gitignore ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-erpnext", 3 | "version": "1.4.0", 4 | "description": "This library let you call REST API of erpnext.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/pawangspandey/node_erpnext.git" 12 | }, 13 | "keywords": [], 14 | "author": "pawan ", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/pawangspandey/node_erpnext/issues" 18 | }, 19 | "homepage": "https://github.com/pawangspandey/node_erpnext#readme", 20 | "dependencies": { 21 | "bluebird": "3.5.0", 22 | "request": "2.81.0", 23 | "request-promise": "4.2.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-erpnext 2 | This library let you call REST API of erpnext. 3 | 4 | ```js 5 | var ERPNext = require('node-erpnext'); 6 | 7 | var erpnext = new ERPNext({ 8 | username : 'username', 9 | password : 'password', 10 | baseUrl : 'http://localhost:8000' 11 | }) 12 | 13 | ``` 14 | 15 | ## Installation 16 | 17 | ```bash 18 | $ npm install node-erpnext 19 | ``` 20 | 21 | ## Customers 22 | 23 | Get list of customers 24 | 25 | ```js 26 | 27 | erpnext.getCustomers().then(function(customers){ 28 | console.log(customers); 29 | }) 30 | 31 | ``` 32 | Get customer's name list. 33 | 34 | ```js 35 | 36 | erpnext.getCustomersName().then(function(customers){ 37 | console.log(customers); 38 | }) 39 | 40 | ``` 41 | Get customer's info by customer name 42 | 43 | ```js 44 | 45 | erpnext.getCustomerByName('ram').then(function(customer){ 46 | console.log(customer); 47 | }) 48 | 49 | ``` 50 | Create customer for parameters follow [this](https://frappe.github.io/erpnext/current/models/selling/customer). 51 | 52 | ```js 53 | 54 | erpnext.createCustomer({ 55 | "naming_series": "CUST-", 56 | "customer_group": "Commercial", 57 | "doctype": "Customer", 58 | "communications": [], 59 | "customer_type": "Company", 60 | "accounts": [], 61 | "docstatus": 0, 62 | "territory": "India", 63 | "sales_team": [], 64 | "customer_name": "ram" 65 | }) 66 | 67 | ``` 68 | update customer by name. for parameters follow [this](https://frappe.github.io/erpnext/current/models/selling/customer). 69 | 70 | ```js 71 | 72 | erpnext.updateCustomerByName('ram',{ 73 | "customer_name": "shyam", 74 | }) 75 | 76 | ``` 77 | 78 | ## Customer Group 79 | 80 | Create a new customer group. 81 | For parameters follow [this](https://frappe.github.io/erpnext/current/models/setup/customer_group). 82 | 83 | ```js 84 | 85 | erpnext.createCustomerGroup({ 86 | 'customer_group_name' : 'new Group', 87 | 'parent_customer_group': 'All Customer Groups', 88 | 'is_group': 'No' 89 | }) 90 | 91 | ``` 92 | 93 | update an existing customer group by group name. 94 | For parameters follow [this](https://frappe.github.io/erpnext/current/models/setup/customer_group). 95 | 96 | ```js 97 | 98 | erpnext.updateCustomerGroupByName('new Group',{ 99 | 'customer_group_name' : 'new group', 100 | 'parent_customer_group': 'All Customer Groups', 101 | 'is_group': 'No' 102 | }) 103 | 104 | ``` 105 | 106 | Get list of customer groups. 107 | 108 | ```js 109 | 110 | erpnext.getCustomerGroups().then(function(customerGroups){ 111 | console.log(customerGroups); 112 | }) 113 | 114 | ``` 115 | Get customer group's name list. 116 | 117 | ```js 118 | 119 | erpnext.getCustomerGroupsName().then(function(customerGroups){ 120 | console.log(customerGroups); 121 | }) 122 | 123 | ``` 124 | Get customer group's info by group name 125 | 126 | ```js 127 | 128 | erpnext.getCustomerGroupByName('new Group').then(function(customerGroups){ 129 | console.log(customerGroups); 130 | }) 131 | 132 | ``` 133 | 134 | ## Sales Order 135 | 136 | 137 | Create a Sales Order. 138 | For parameters follow [this](https://frappe.github.io/erpnext/current/models/selling/sales_order). 139 | 140 | ```js 141 | 142 | erpnext.createSalesOrder({ 143 | "status": "Draft", 144 | "naming_series": "SO-", 145 | "currency": "INR", 146 | "billing_status": "Not Billed", 147 | "order_type": "Sales", 148 | "transaction_date": "2017-05-10", 149 | "territory": "India", 150 | "delivery_status": "Not Delivered", 151 | "customer": "Camelport Internal", 152 | "items": [ 153 | { 154 | "qty": 5, 155 | "rate": 2000, 156 | "stock_uom": "Nos", 157 | "item_code": "i01", 158 | "parentfield": "items" 159 | } 160 | ], 161 | "delivery_date": "2017-05-18", 162 | "sales_team": [] 163 | }) 164 | 165 | ``` 166 | 167 | update an existing sales order by sales order name. 168 | For parameters follow [this](https://frappe.github.io/erpnext/current/models/selling/sales_order). 169 | ```js 170 | 171 | erpnext.updateSalesOrderByName('SO-00003',{ 172 | "status": "Submitted", 173 | "docstatus" : 1 174 | }) 175 | 176 | ``` 177 | 178 | Get list of sales order. 179 | 180 | ```js 181 | 182 | erpnext.getSalesOrder().then(function(salesOrder){ 183 | console.log(salesOrder); 184 | }) 185 | 186 | ``` 187 | Get sales order's name list. 188 | 189 | ```js 190 | 191 | erpnext.getSalesOrdersName().then(function(salesOrder){ 192 | console.log(salesOrder); 193 | }) 194 | 195 | ``` 196 | Get sales order's info by order name 197 | 198 | ```js 199 | 200 | erpnext.getSalesOrderByName('new Group').then(function(customer){ 201 | console.log(customer); 202 | }) 203 | 204 | ``` 205 | 206 | ## Item 207 | 208 | Create an Item. 209 | For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice_item. 210 | 211 | ```js 212 | 213 | erpnext.createAnItem({ 214 | "has_variants": 0, 215 | "is_stock_item": "No", 216 | "valuation_method": "", 217 | "min_order_qty": 0, 218 | "is_asset_item": "No", 219 | "has_batch_no": "No", 220 | "has_serial_no": "No", 221 | "is_purchase_item": "Yes", 222 | "is_sales_item": "Yes", 223 | "is_service_item": "No", 224 | "inspection_required": "No", 225 | "item_code": "item code", 226 | "item_name": "item name", 227 | "description": "description", 228 | "item_group": "Services" 229 | }) 230 | 231 | ``` 232 | 233 | Update an Item. 234 | For param follow https://frappe.github.io/erpnext/current/models/selling/sales_order 235 | 236 | ```js 237 | 238 | erpnext.updateItemByName("item code",{ 239 | "has_variants": 0, 240 | "is_stock_item": "No", 241 | "valuation_method": "", 242 | "min_order_qty": 0, 243 | "is_asset_item": "No", 244 | "has_batch_no": "No", 245 | "has_serial_no": "No", 246 | "is_purchase_item": "Yes", 247 | "is_sales_item": "Yes", 248 | "is_service_item": "No", 249 | "inspection_required": "No", 250 | "item_code": "item code", 251 | "item_name": "item name", 252 | "description": "description", 253 | "item_group": "Services" 254 | }) 255 | 256 | ``` 257 | 258 | ## Supplier 259 | 260 | Create a Supplier. 261 | For param follow https://frappe.github.io/erpnext/current/models/buying/supplier. 262 | 263 | ```js 264 | 265 | erpnext.createSupplier({"supplier_type":"Services","supplier_name":"ram"}); 266 | 267 | ``` 268 | Update Supplier. 269 | For param follow https://frappe.github.io/erpnext/current/models/buying/supplier. 270 | 271 | ```js 272 | 273 | erpnext.updateSupplierByName("ram",{ 274 | "supplier_type":"Services", 275 | "supplier_name":"ram" 276 | }) 277 | 278 | ``` 279 | 280 | ## Purchase Invoice 281 | 282 | Create a Purchase Invoice 283 | For param follow https://frappe.github.io/erpnext/current/models/accounts/purchase_invoice 284 | 285 | ```js 286 | 287 | erpnext.createPurchaseInvoice({ 288 | "supplier": "ram", 289 | 290 | "items": [{ 291 | "item_code": "item code", 292 | "qty": 4, 293 | "price_list_rate": 5000, 294 | "schedule_date": "2017-05-31" 295 | }] 296 | }) 297 | 298 | ``` 299 | 300 | Update Purchase Invoice. 301 | For param follow https://frappe.github.io/erpnext/current/models/accounts/purchase_invoice. 302 | 303 | ```js 304 | 305 | erpnext.updatePurchaseInvoiceByName("name",{ 306 | "supplier": "ram" 307 | }) 308 | 309 | ``` 310 | 311 | ## Sales Invoice 312 | 313 | Create a Sales Invoice. 314 | For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice 315 | 316 | ```js 317 | 318 | erpnext.createSalesInvoice( 319 | { 320 | "due_date": "2017-05-14", 321 | "customer": "ram", 322 | "items":[{ 323 | "item_code": "item code", 324 | "rate": 15000, 325 | "qty": 3 326 | }] 327 | } 328 | ) 329 | 330 | ``` 331 | Update Sales Invoice. 332 | For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice 333 | 334 | ```js 335 | 336 | erpnext.updateSalesInvoiceByName( 337 | "Sales Invoice", 338 | 339 | { 340 | "due_date": "2017-05-14", 341 | "customer": "ram", 342 | "items":[{ 343 | "item_code": "item code", 344 | "rate": 15000, 345 | "qty": 3 346 | }] 347 | } 348 | ) 349 | 350 | ``` 351 | ## Purchase Order 352 | 353 | Create a Purchase Order. 354 | For param follow https://frappe.github.io/erpnext/current/models/buying/purchase_order 355 | 356 | ```js 357 | 358 | erpnext.createPurchaseOrder({ 359 | "supplier": "ram", 360 | "items": [{ 361 | 362 | "item_code": "item code", 363 | "qty": 4, 364 | "price_list_rate": 5000, 365 | "schedule_date": "2017-05-31" 366 | } 367 | ] 368 | }) 369 | 370 | ``` 371 | 372 | Update Purchase Order. 373 | For param follow https://frappe.github.io/erpnext/current/models/buying/purchase_order 374 | 375 | ```js 376 | 377 | erpnext.updatePurchaseOrderByName("name",{ 378 | "supplier": "ram", 379 | "items": [{ 380 | 381 | "item_code": "item code", 382 | "qty": 4, 383 | "price_list_rate": 5000, 384 | "schedule_date": "2017-05-31" 385 | } 386 | ] 387 | }) 388 | 389 | ``` 390 | 391 | Get All Purchase Order. 392 | 393 | ```js 394 | 395 | erpnext.getPurchaseOrders() 396 | .then(function(data){ console.log(data) }) 397 | 398 | ``` 399 | 400 | Get Info of a Purchase Order by name. 401 | 402 | ```js 403 | 404 | erpnext.getPurchaseOrderByName('PO-00003') 405 | .then(function(data){ console.log(data) }) 406 | 407 | ``` 408 | 409 | Get List of purchase orders 410 | 411 | ```js 412 | 413 | erpnext.getPurchaseOrdersName() 414 | .then(function(data){ console.log(data) }) 415 | 416 | ``` 417 | 418 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * ERPNext class will exports public api. 5 | */ 6 | 7 | var request = require('request'); 8 | var requestPromise = require('request-promise'); 9 | var querystring = require('querystring'); 10 | var Promise = require('bluebird'); 11 | 12 | var ERPNext = function (options) { 13 | this.username = options.username; 14 | this.password = options.password; 15 | this.baseUrl = options.baseUrl; 16 | this.cookieJar = request.jar(); 17 | }; 18 | 19 | ERPNext.prototype.constructor = ERPNext; 20 | 21 | 22 | /** 23 | * Doing Login of a user and stores session cookie into cookieJar. 24 | * @return {Promise} resolve response. 25 | */ 26 | 27 | ERPNext.prototype.login = function () { 28 | var _this = this; 29 | var formData = querystring.stringify({ usr: _this.username, pwd: _this.password }); 30 | var contentLength = formData.length; 31 | return requestPromise.post({ 32 | url: _this.baseUrl + "/api/method/login", 33 | jar: _this.cookieJar, 34 | body: formData, 35 | headers: { 36 | 'Content-Length': contentLength, 37 | 'Content-Type': 'application/x-www-form-urlencoded' 38 | } 39 | }); 40 | } 41 | 42 | /** 43 | * Will Call REST API to get customer list. 44 | * @return {Promise} resolve customer list. 45 | */ 46 | 47 | ERPNext.prototype.getCustomersName = function () { 48 | var _this = this; 49 | return _this.login().then(function (res) { 50 | return requestPromise.get({ 51 | url: _this.baseUrl + "/api/resource/Customer", 52 | jar: _this.cookieJar, 53 | }).then(function (customers) { 54 | customers = JSON.parse(customers); 55 | return customers.data; 56 | }); 57 | }); 58 | } 59 | 60 | /** 61 | * Will Call REST API to get Customer detail by name. 62 | * @param {String} name name of the customer. 63 | */ 64 | 65 | ERPNext.prototype.getCustomerByName = function (name) { 66 | var _this = this; 67 | return _this.login().then(function (res) { 68 | return requestPromise.get({ 69 | url: _this.baseUrl + "/api/resource/Customer/" + name, 70 | jar: _this.cookieJar, 71 | }).then(function (customer) { 72 | customer = JSON.parse(customer); 73 | return customer.data; 74 | }) 75 | }); 76 | } 77 | 78 | /** 79 | * Will Call REST API to create customer. 80 | * for parameters follow https://frappe.github.io/erpnext/current/models/selling/customer 81 | * @param {Object} customerData customer data object. 82 | * @return {Promise} resolve with customer data. 83 | */ 84 | 85 | ERPNext.prototype.createCustomer = function (object) { 86 | var _this = this; 87 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 88 | var contentLength = formData.length; 89 | return _this.login().then(function (res) { 90 | return requestPromise.post({ 91 | url: _this.baseUrl + "/api/resource/Customer", 92 | jar: _this.cookieJar, 93 | body: formData, 94 | headers: { 95 | 'Content-Length': contentLength, 96 | 'Content-Type': 'application/x-www-form-urlencoded' 97 | } 98 | }).then(function (customer) { 99 | customer = JSON.parse(customer); 100 | return customer.data; 101 | }) 102 | }) 103 | } 104 | 105 | /** 106 | * Get Customer info array 107 | * @return {Promise} resolve with array of clients info 108 | */ 109 | 110 | ERPNext.prototype.getCustomers = function () { 111 | var _this = this; 112 | return _this.getCustomersName().then(function (customers) { 113 | return Promise.map(customers, function (customer) { 114 | return _this.getCustomerByName(customer.name); 115 | }); 116 | }) 117 | } 118 | 119 | /** 120 | * Update Customer by name. 121 | * @param {String} name name of the customer. 122 | * @param {Object} object data to be update. 123 | * @return {Promise} resolve with customer data. 124 | */ 125 | 126 | ERPNext.prototype.updateCustomerByName = function(name, object){ 127 | var _this = this; 128 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 129 | var contentLength = formData.length; 130 | return _this.login().then(function (res) { 131 | return requestPromise.put({ 132 | url: _this.baseUrl + "/api/resource/Customer/"+ name, 133 | jar: _this.cookieJar, 134 | body: formData, 135 | headers: { 136 | 'Content-Length': contentLength, 137 | 'Content-Type': 'application/x-www-form-urlencoded' 138 | } 139 | }).then(function (customer) { 140 | customer = JSON.parse(customer); 141 | return customer.data; 142 | }) 143 | }) 144 | } 145 | 146 | 147 | 148 | 149 | /** 150 | * Create Customer Group. 151 | * For param follow https://frappe.github.io/erpnext/current/models/setup/customer_group 152 | * @param {Object} object customer group data. 153 | * @return {Promise} resolve with customer group data. 154 | */ 155 | 156 | ERPNext.prototype.createCustomerGroup = function(object){ 157 | var _this = this; 158 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 159 | var contentLength = formData.length; 160 | return _this.login().then(function (res) { 161 | return requestPromise.post({ 162 | url: _this.baseUrl + "/api/resource/Customer Group", 163 | jar: _this.cookieJar, 164 | body: formData, 165 | headers: { 166 | 'Content-Length': contentLength, 167 | 'Content-Type': 'application/x-www-form-urlencoded' 168 | } 169 | }).then(function (customer) { 170 | customer = JSON.parse(customer); 171 | return customer.data; 172 | }) 173 | }) 174 | } 175 | 176 | 177 | /** 178 | * Update Customer Group by name. 179 | * For param follow https://frappe.github.io/erpnext/current/models/setup/customer_group 180 | * @param {String} name customer group name. 181 | * @param {Object} object customer group data. 182 | * @return {Promise} resolve with customer group data. 183 | */ 184 | 185 | ERPNext.prototype.updateCustomerGroupByName = function(name, object){ 186 | var _this = this; 187 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 188 | var contentLength = formData.length; 189 | return _this.login().then(function (res) { 190 | return requestPromise.put({ 191 | url: _this.baseUrl + "/api/resource/Customer Group/"+name, 192 | jar: _this.cookieJar, 193 | body: formData, 194 | headers: { 195 | 'Content-Length': contentLength, 196 | 'Content-Type': 'application/x-www-form-urlencoded' 197 | } 198 | }).then(function (customerGroup) { 199 | customerGroup = JSON.parse(customerGroup); 200 | return customerGroup.data; 201 | }) 202 | }) 203 | } 204 | 205 | 206 | /** 207 | * Get Customer Group's name. 208 | * For param follow https://frappe.github.io/erpnext/current/models/setup/customer_group 209 | * @param {Object} object customer group data. 210 | * @return {Promise} resolve with customer group data. 211 | */ 212 | 213 | ERPNext.prototype.getCustomerGroupsName = function(){ 214 | var _this = this; 215 | return _this.login().then(function (res) { 216 | return requestPromise.get({ 217 | url: _this.baseUrl + "/api/resource/Customer Group", 218 | jar: _this.cookieJar, 219 | }).then(function (customer) { 220 | customer = JSON.parse(customer); 221 | return customer.data; 222 | }) 223 | }) 224 | } 225 | 226 | /** 227 | * Get Customer Group's info by name. 228 | * For param follow https://frappe.github.io/erpnext/current/models/setup/customer_group 229 | * @param {String} name customer group's name. 230 | * @return {Promise} resolve with customer group data. 231 | */ 232 | 233 | ERPNext.prototype.getCustomerGroupByName = function(name){ 234 | var _this = this; 235 | return _this.login().then(function (res) { 236 | return requestPromise.get({ 237 | url: _this.baseUrl + "/api/resource/Customer Group/"+ name, 238 | jar: _this.cookieJar, 239 | }).then(function (customer) { 240 | customer = JSON.parse(customer); 241 | return customer.data; 242 | }) 243 | }) 244 | } 245 | 246 | /** 247 | * Get Customer Group's info array. 248 | * @return {Promise} resolve customer group data array. 249 | */ 250 | 251 | ERPNext.prototype.getCustomerGroups = function(){ 252 | var _this = this; 253 | return _this.getCustomerGroupsName().then(function (customersGroups) { 254 | return Promise.map(customersGroups, function (group) { 255 | return _this.getCustomerGroupByName(group.name); 256 | }); 257 | }) 258 | } 259 | 260 | 261 | /** 262 | * Get Sales Order's name array. 263 | * @return {Promise} resolve customer group data array. 264 | */ 265 | 266 | ERPNext.prototype.getSalesOrdersName = function () { 267 | var _this = this; 268 | return _this.login().then(function (res) { 269 | return requestPromise.get({ 270 | url: _this.baseUrl + "/api/resource/Sales Order", 271 | jar: _this.cookieJar, 272 | }).then(function (salesOrder) { 273 | salesOrder = JSON.parse(salesOrder); 274 | return salesOrder.data; 275 | }) 276 | }) 277 | } 278 | 279 | 280 | /** 281 | * Get Sales Order's name array. 282 | * @param {String} name name of the sales order 283 | * @return {Promise} resolve customer group data array. 284 | */ 285 | 286 | ERPNext.prototype.getSalesOrderByName = function (name) { 287 | var _this = this; 288 | return _this.login().then(function (res) { 289 | return requestPromise.get({ 290 | url: _this.baseUrl + "/api/resource/Sales Order/"+name, 291 | jar: _this.cookieJar, 292 | }).then(function (salesOrder) { 293 | salesOrder = JSON.parse(salesOrder); 294 | return salesOrder.data; 295 | }) 296 | }) 297 | } 298 | 299 | 300 | /** 301 | * Get Sales Order info array. 302 | * @return {Promise} resolve Sales Orders array list. 303 | */ 304 | 305 | ERPNext.prototype.getSalesOrder = function(){ 306 | var _this = this; 307 | return _this.getSalesOrdersName().then(function (salesOrders) { 308 | return Promise.map(salesOrders, function (saleOrder) { 309 | return _this.getSalesOrderByName(saleOrder.name); 310 | }); 311 | }) 312 | } 313 | 314 | 315 | /** 316 | * Create Sales Order. 317 | * For param follow https://frappe.github.io/erpnext/current/models/selling/sales_order 318 | * @param {Object} object Sales Order. 319 | * @return {Promise} resolve Created Sales Order. 320 | */ 321 | 322 | ERPNext.prototype.createSalesOrder = function(object){ 323 | var _this = this; 324 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 325 | var contentLength = formData.length; 326 | return _this.login().then(function (res) { 327 | return requestPromise.post({ 328 | url: _this.baseUrl + "/api/resource/Sales Order", 329 | jar: _this.cookieJar, 330 | body: formData, 331 | headers: { 332 | 'Content-Length': contentLength, 333 | 'Content-Type': 'application/x-www-form-urlencoded' 334 | } 335 | }).then(function (salesOrder) { 336 | salesOrder = JSON.parse(salesOrder); 337 | return salesOrder.data; 338 | }) 339 | }) 340 | } 341 | 342 | 343 | /** 344 | * Update Sales Order by name. 345 | * For param follow https://frappe.github.io/erpnext/current/models/selling/sales_order 346 | * @param {String} name name of the sales order. 347 | * @param {Object} object data of sales order. 348 | * @return {Promise} resolve Created Sales Order. 349 | */ 350 | 351 | ERPNext.prototype.updateSalesOrderByName = function(name, object){ 352 | var _this = this; 353 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 354 | var contentLength = formData.length; 355 | return _this.login().then(function (res) { 356 | return requestPromise.put({ 357 | url: _this.baseUrl + "/api/resource/Sales Order/"+name, 358 | jar: _this.cookieJar, 359 | body: formData, 360 | headers: { 361 | 'Content-Length': contentLength, 362 | 'Content-Type': 'application/x-www-form-urlencoded' 363 | } 364 | }).then(function (salesOrder) { 365 | salesOrder = JSON.parse(salesOrder); 366 | return salesOrder.data; 367 | }) 368 | }) 369 | } 370 | 371 | /** 372 | * Create an Item. 373 | * For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice_item. 374 | * @param {Object} object item object. 375 | * @return {Promise} resolve Created item. 376 | */ 377 | 378 | ERPNext.prototype.createAnItem = function(object){ 379 | var _this = this; 380 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 381 | var contentLength = formData.length; 382 | return _this.login().then(function (res) { 383 | return requestPromise.post({ 384 | url: _this.baseUrl + "/api/resource/Item", 385 | jar: _this.cookieJar, 386 | body: formData, 387 | headers: { 388 | 'Content-Length': contentLength, 389 | 'Content-Type': 'application/x-www-form-urlencoded' 390 | } 391 | }).then(function (item) { 392 | item = JSON.parse(item); 393 | return item.data; 394 | }) 395 | }) 396 | } 397 | 398 | /** 399 | * Update an Item. 400 | * For param follow https://frappe.github.io/erpnext/current/models/selling/sales_order 401 | * @param {String} name name of the item. 402 | * @param {Object} object data of item. 403 | * @return {Promise} resolve updated item 404 | */ 405 | 406 | ERPNext.prototype.updateItemByName = function(name, object){ 407 | var _this = this; 408 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 409 | var contentLength = formData.length; 410 | return _this.login().then(function (res) { 411 | return requestPromise.put({ 412 | url: _this.baseUrl + "/api/resource/Item/"+name, 413 | jar: _this.cookieJar, 414 | body: formData, 415 | headers: { 416 | 'Content-Length': contentLength, 417 | 'Content-Type': 'application/x-www-form-urlencoded' 418 | } 419 | }).then(function (item) { 420 | item = JSON.parse(item); 421 | return item.data; 422 | }) 423 | }) 424 | } 425 | 426 | 427 | /** 428 | * Create a Supplier. 429 | * For param follow https://frappe.github.io/erpnext/current/models/buying/supplier. 430 | * @param {Object} object Supplier object. 431 | * @return {Promise} resolve Created Supplier. 432 | */ 433 | 434 | ERPNext.prototype.createSupplier = function(object){ 435 | var _this = this; 436 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 437 | var contentLength = formData.length; 438 | return _this.login().then(function (res) { 439 | return requestPromise.post({ 440 | url: _this.baseUrl + "/api/resource/Supplier", 441 | jar: _this.cookieJar, 442 | body: formData, 443 | headers: { 444 | 'Content-Length': contentLength, 445 | 'Content-Type': 'application/x-www-form-urlencoded' 446 | } 447 | }).then(function (Supplier) { 448 | Supplier = JSON.parse(Supplier); 449 | return Supplier.data; 450 | }) 451 | }) 452 | } 453 | 454 | /** 455 | * Update Supplier. 456 | * For param follow https://frappe.github.io/erpnext/current/models/buying/supplier. 457 | * @param {String} name name of the Supplier. 458 | * @param {Object} object data of Supplier. 459 | * @return {Promise} resolve updated Supplier 460 | */ 461 | 462 | ERPNext.prototype.updateSupplierByName = function(name, object){ 463 | var _this = this; 464 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 465 | var contentLength = formData.length; 466 | return _this.login().then(function (res) { 467 | return requestPromise.put({ 468 | url: _this.baseUrl + "/api/resource/Supplier/"+name, 469 | jar: _this.cookieJar, 470 | body: formData, 471 | headers: { 472 | 'Content-Length': contentLength, 473 | 'Content-Type': 'application/x-www-form-urlencoded' 474 | } 475 | }).then(function (Supplier) { 476 | Supplier = JSON.parse(Supplier); 477 | return Supplier.data; 478 | }) 479 | }) 480 | } 481 | 482 | /** 483 | * Create a Purchase Invoice. 484 | * For param follow https://frappe.github.io/erpnext/current/models/accounts/purchase_invoice. 485 | * @param {Object} object Purchase Invoice object. 486 | * @return {Promise} resolve Created Purchase Invoice . 487 | */ 488 | 489 | ERPNext.prototype.createPurchaseInvoice = function(object){ 490 | var _this = this; 491 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 492 | var contentLength = formData.length; 493 | return _this.login().then(function (res) { 494 | return requestPromise.post({ 495 | url: _this.baseUrl + "/api/resource/Purchase Invoice", 496 | jar: _this.cookieJar, 497 | body: formData, 498 | headers: { 499 | 'Content-Length': contentLength, 500 | 'Content-Type': 'application/x-www-form-urlencoded' 501 | } 502 | }).then(function (PurchaseInvoice ) { 503 | PurchaseInvoice = JSON.parse(PurchaseInvoice); 504 | return PurchaseInvoice.data; 505 | }) 506 | }) 507 | } 508 | 509 | /** 510 | * Update Purchase Invoice. 511 | * For param follow https://frappe.github.io/erpnext/current/models/accounts/purchase_invoice. 512 | * @param {String} name name of the Purchase Invoice. 513 | * @param {Object} object data of Purchase Invoice. 514 | * @return {Promise} resolve updated Purchase Invoice. 515 | */ 516 | 517 | ERPNext.prototype.updatePurchaseInvoiceByName = function(name, object){ 518 | var _this = this; 519 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 520 | var contentLength = formData.length; 521 | return _this.login().then(function (res) { 522 | return requestPromise.put({ 523 | url: _this.baseUrl + "/api/resource/Purchase Invoice/"+name, 524 | jar: _this.cookieJar, 525 | body: formData, 526 | headers: { 527 | 'Content-Length': contentLength, 528 | 'Content-Type': 'application/x-www-form-urlencoded' 529 | } 530 | }).then(function (PurchaseInvoice) { 531 | PurchaseInvoice = JSON.parse(PurchaseInvoice); 532 | return PurchaseInvoice.data; 533 | }) 534 | }) 535 | } 536 | 537 | /** 538 | * Create a Sales Invoice. 539 | * For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice 540 | * @param {Object} object Sales Invoice object. 541 | * @return {Promise} resolve Created Sales Invoice. 542 | */ 543 | 544 | ERPNext.prototype.createSalesInvoice = function(object){ 545 | var _this = this; 546 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 547 | var contentLength = formData.length; 548 | return _this.login().then(function (res) { 549 | return requestPromise.post({ 550 | url: _this.baseUrl + "/api/resource/Sales Invoice", 551 | jar: _this.cookieJar, 552 | body: formData, 553 | headers: { 554 | 'Content-Length': contentLength, 555 | 'Content-Type': 'application/x-www-form-urlencoded' 556 | } 557 | }).then(function (SalesInvoice ) { 558 | SalesInvoice = JSON.parse(SalesInvoice); 559 | return SalesInvoice.data; 560 | }) 561 | }) 562 | } 563 | 564 | /** 565 | * Update Sales Invoice. 566 | * For param follow https://frappe.github.io/erpnext/current/models/accounts/sales_invoice 567 | * @param {String} name name of the Sales Invoice. 568 | * @param {Object} object data of Sales Invoice. 569 | * @return {Promise} resolve updated Sales Invoice. 570 | */ 571 | 572 | ERPNext.prototype.updateSalesInvoiceByName = function(name, object){ 573 | var _this = this; 574 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 575 | var contentLength = formData.length; 576 | return _this.login().then(function (res) { 577 | return requestPromise.put({ 578 | url: _this.baseUrl + "/api/resource/Sales Invoice/"+name, 579 | jar: _this.cookieJar, 580 | body: formData, 581 | headers: { 582 | 'Content-Length': contentLength, 583 | 'Content-Type': 'application/x-www-form-urlencoded' 584 | } 585 | }).then(function (SalesInvoice) { 586 | SalesInvoice = JSON.parse(SalesInvoice); 587 | return SalesInvoice.data; 588 | }) 589 | }) 590 | } 591 | 592 | /** 593 | * Create a Purchase Order. 594 | * For param follow https://frappe.github.io/erpnext/current/models/buying/purchase_order 595 | * @param {Object} object Purchase Order object. 596 | * @return {Promise} resolve Created Purchase Order. 597 | */ 598 | 599 | ERPNext.prototype.createPurchaseOrder = function(object){ 600 | var _this = this; 601 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 602 | var contentLength = formData.length; 603 | return _this.login().then(function (res) { 604 | return requestPromise.post({ 605 | url: _this.baseUrl + "/api/resource/Purchase Order", 606 | jar: _this.cookieJar, 607 | body: formData, 608 | headers: { 609 | 'Content-Length': contentLength, 610 | 'Content-Type': 'application/x-www-form-urlencoded' 611 | } 612 | }).then(function (SalesInvoice ) { 613 | SalesInvoice = JSON.parse(SalesInvoice); 614 | return SalesInvoice.data; 615 | }) 616 | }) 617 | } 618 | 619 | /** 620 | * Update Purchase Order. 621 | * For param follow https://frappe.github.io/erpnext/current/models/buying/purchase_order 622 | * @param {String} name name of the Purchase Order. 623 | * @param {Object} object data of Purchase Order. 624 | * @return {Promise} resolve updated Purchase Order. 625 | */ 626 | 627 | ERPNext.prototype.updatePurchaseOrderByName = function(name, object){ 628 | var _this = this; 629 | var formData = querystring.stringify({ data: JSON.stringify(object) }); 630 | var contentLength = formData.length; 631 | return _this.login().then(function (res) { 632 | return requestPromise.put({ 633 | url: _this.baseUrl + "/api/resource/Purchase Order/"+name, 634 | jar: _this.cookieJar, 635 | body: formData, 636 | headers: { 637 | 'Content-Length': contentLength, 638 | 'Content-Type': 'application/x-www-form-urlencoded' 639 | } 640 | }).then(function (PurchaseOrder) { 641 | PurchaseOrder = JSON.parse(PurchaseOrder); 642 | return PurchaseOrder.data; 643 | }) 644 | }) 645 | } 646 | 647 | 648 | 649 | /** 650 | * Will Call REST API to get Purchase Order list. 651 | * @return {Promise} resolve Purchase Order list. 652 | */ 653 | 654 | ERPNext.prototype.getPurchaseOrdersName = function () { 655 | var _this = this; 656 | return _this.login().then(function (res) { 657 | return requestPromise.get({ 658 | url: _this.baseUrl + "/api/resource/Purchase Order", 659 | jar: _this.cookieJar, 660 | }).then(function (purchaseOrders) { 661 | purchaseOrders = JSON.parse(purchaseOrders); 662 | return purchaseOrders.data; 663 | }); 664 | }); 665 | } 666 | 667 | /** 668 | * Will Call REST API to get Purchase Order detail by name. 669 | * @param {String} name name of the Purchase Order. 670 | */ 671 | 672 | ERPNext.prototype.getPurchaseOrderByName = function (name) { 673 | var _this = this; 674 | return _this.login().then(function (res) { 675 | return requestPromise.get({ 676 | url: _this.baseUrl + "/api/resource/Purchase Order/" + name, 677 | jar: _this.cookieJar, 678 | }).then(function (purchaseOrders) { 679 | purchaseOrders = JSON.parse(purchaseOrders); 680 | return purchaseOrders.data; 681 | }) 682 | }); 683 | } 684 | 685 | 686 | /** 687 | * Get Purchase Order info array 688 | * @return {Promise} resolve with array of Purchase Order info 689 | */ 690 | 691 | ERPNext.prototype.getPurchaseOrders = function () { 692 | var _this = this; 693 | return _this.getPurchaseOrdersName().then(function (PurchaseOrders) { 694 | return Promise.map(PurchaseOrders, function (PurchaseOrder) { 695 | return _this.getPurchaseOrderByName(PurchaseOrder.name); 696 | }); 697 | }) 698 | } 699 | 700 | module.exports = ERPNext; 701 | 702 | --------------------------------------------------------------------------------