Restlet to create a Quote

Restlet to create Quote from Magento website. The restlet is the backend of the API communication. Token-based authentication (TBA) is used for authentication purpose.

/*******************************************************************************
 * 
 * Developer: Suravi Lahiri,Anukaran Development Head: Mr. Sumeet Kulkarni
 * Company Name: CEINO Purpose: Create a Quotation through restlet Update: Date:
 * 15/09/2017 change summary: Added Item Quantity & Rate instead of Item amount.
 * Date: 25/09/17 . Date: 11/10/17 change summary: getting contact id & passing
 * through JSON
 * 
 * © Copyright All Right
 * 
 ******************************************************************************/
function POST_data(datain) {
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'IN POST');
	var customerObj = datain.data;
	var responseObj = new Array();
	responseObj['message'] = '';
	responseObj['responseCode'] = '';
	responseObj['record_id'] = '';
	responseObj['Quotation_id'] = '';
	responseObj['contact_id'] = ''; // passing customer contact id

	// nlapiLogExecution('DEBUG','RESTLET
	// POST','customerObj=='+customerObj.length);

	// Start:LMS Dropbox
	var customer_id = datain.customer_id; // getting customer id
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile==' + customer_id);
	var quotation_status = datain.quotation_status; // getting quotation status
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile==' + quotation_status);
	var customer_ship_address = datain.customer_ship_address; // getting
	// customer
	// shipping
	// address
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile=='
			+ customer_ship_address);
	var customer_bill_address = datain.customer_bill_address; // getting
	// customer
	// billing
	// address
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile=='
			+ customer_bill_address);
	var department = datain.department; // getting customer department
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile==' + department);
	var shipping_cost = datain.shipping_cost; // getting shipping cost
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile==' + shipping_cost);

	var contact_id = datain.contact_id; // getting shipping cost
	nlapiLogExecution('DEBUG', 'RESTLET POST', 'mobile==' + contact_id);

	var item = new Array();
	var item = datain.item;
	var len = item.length;

	var rec_quote = nlapiCreateRecord('estimate');
	rec_quote.setFieldValue('customform', 106); // setting customer_for as us
	nlapiLogExecution('DEBUG', 'PBTI-log', 'New_Qutation_Creation=' + rec_quote);
	rec_quote.setFieldValue('entity', customer_id); // setting customer_id
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Customer Name =' + customer_id);
	rec_quote.setFieldValue('entitystatus', quotation_status); // setting
	// quotation
	// status
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Existing Status ='
			+ quotation_status);
	rec_quote.setFieldValue('department', department); // setting quotation
	// status
	// Update
	rec_quote.setFieldValue('custbody_nugen_is_online', 'T'); // setting
																// quotation

	nlapiLogExecution('DEBUG', 'PBTI-log', 'department =' + department);
	rec_quote.setFieldValue('shippingcost', shipping_cost); // setting shipping
	// cost
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Address =' + shipping_cost);
	rec_quote.setFieldValue('shipaddress', customer_ship_address); // setting
	// customer
	// shipping
	// address
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Address =' + customer_ship_address);
	rec_quote.setFieldValue('billaddress', customer_bill_address); // setting
	// customer
	// billing
	// address
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Address =' + customer_bill_address);

	rec_quote.setFieldValue('custbody_nugen_contact', contact_id);
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Address =' + contact_id);

	var j = 1;
	for (var i = 0; i < len; i++) {

		rec_quote.selectNewLineItem('item');
		rec_quote.setCurrentLineItemValue('item', 'item',
				datain.item[i].item_id); // setting item id
		// rec_quote.setCurrentLineItemValue('item','amount',datain.item[i].item_price);
		// //setting item amount
		nlapiLogExecution('DEBUG', 'RESTLET POST', 'item[i][item_id]=='
				+ datain.item[i].item_id);
		// nlapiLogExecution('DEBUG','RESTLET
		// POST','item[i][item_price]=='+datain.item[i].item_price);
		rec_quote.setCurrentLineItemValue('item', 'quantity',
				datain.item[i].item_quantity);
		nlapiLogExecution('DEBUG', 'RESTLET POST', 'item[i][item_id]=='
				+ datain.item[i].item_quantity);
		rec_quote.setCurrentLineItemValue('item', 'rate',
				datain.item[i].item_rate);
		nlapiLogExecution('DEBUG', 'RESTLET POST', 'item[i][item_id]=='
				+ datain.item[i].item_rate);

		j++;
		rec_quote.commitLineItem('item');
	}

	var record_id = nlapiSubmitRecord(rec_quote, false, true);
	nlapiLogExecution('DEBUG', 'PBTI-log', 'Record Sbubmited =' + record_id);

	if (record_id != null || record_id != '') {
		responseObj['message'] = 'Record added';
		responseObj['responseCode'] = '1';
		responseObj['record_id'] = record_id;
		responseObj['Quotation_id'] = nlapiLookupField('estimate', record_id,
				'tranid');
		responseObj['contact_id'] = contact_id;
	}
	var JSONObj = {
		"message" : responseObj['message'],
		"responseCode" : responseObj['responseCode'],
		"data" : [ {
			"record_id" : responseObj['record_id']
		}, {
			"Contact_id" : responseObj['contact_id']
		}, {
			"Quotation_id" : responseObj['Quotation_id']
		} ]
	}

	return JSONObj;
}

Leave a comment

Your email address will not be published. Required fields are marked *