Extension that allows the user to make the invoice items tax free by uploading tax exemption certificate.
Javascript
define(
'JJ.TaxFreeInvoice.TaxFreeInvoice', [
'JJ.TaxFreeInvoice.TaxFreeInvoice.View', 'Invoice.Details.View', 'Invoice.Model', 'sweetAlert', 'Utils', 'Backbone', 'jQuery', 'underscore'
],
function(
TaxFreeInvoiceView, InvoiceDetailsView, InvoiceModel, swal, Utils, Backbone, jQuery, _
) {
var FILE = '';
'use strict';
return {
mountToApp: function mountToApp(container) {
console.log("container", container)
_.extend(InvoiceDetailsView.prototype, {
initialize: _.wrap(InvoiceDetailsView.prototype.initialize, function wrappedInitialize(fn, options) {
var self = this;
fn.apply(this, _.toArray(arguments).slice(1));
//this.model.on('change', this.render, this);
var service_url = Utils.getAbsoluteUrl(getExtensionAssetsPath('services/TaxFreeInvoice.Service.ss'));
fetch(service_url, {
method: 'post',
body: '{"invid":"' + options.routerArguments[0] + '"}',
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}).then(function(response) {
return response.json()
}).then(function(res) {
var data = res;
if (data.success) {
//swal("Success", "YOur GET FUNCTION WORKED", "success");
self.model.iscert = true;
} else {
//swal("Oops!", "YOur GET FUNCTION DID NOT WORKED", "error");
self.model.iscert = false;
}
});
}),
getContext: _.wrap(InvoiceDetailsView.prototype.getContext, function wrappedInitialize(fn, options) {
var res = fn.apply(this, _.toArray(arguments).slice(1));
_.extend(res, {
istaxfreeextension: true,
dispUplField: this.model.iscert
});
return res;
}),
events: _.extend(InvoiceDetailsView.prototype.events, {
'change [data-action="filechange"]': 'filechange',
'click [data-action="pofile"]': 'pofile'
}),
filechange: function(input) {
if (input.target.files && input.target.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(input.target.files[0]);
FILE = input.target.files[0];
reader.onload = function(e) {
$('#base64full').val(e.target.result.split(',')[1]);
};
}
},
pofile: function(e) {
var fileName = FILE.name;
var self = this;
//If filereader error then error thrown
if ($('#base64full').val() == '') {
swal('Oops!', 'An Unexpected error occured', 'error');
return false;
}
var fileName = "taxfreeDoc " + Date.now() + ' ' + fileName;
var file = $('#base64full').val();
var fileExtension = fileName.replace(/^.*\./, '');
var invInternalid = $("#invIdforTaxFreeinv").attr("class");
console.log("invInternalid", invInternalid);
var service_url = Utils.getAbsoluteUrl(getExtensionAssetsPath('services/TaxFreeInvoice.Service.ss'));
swal("Uploading...", {
button: false,
icon: "/img/uploadGif.gif",
closeOnClickOutside: false
});
fetch(service_url, {
method: 'post',
body: '{"fileName":"' + fileName + '","file":"' + file + '","invInternalid":"' + invInternalid + '","fileExtension":"' + fileExtension + '"}',
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}).then(function(response) {
return response.json()
}).then(function(res) {
var data = res;
if (data.success) {
$("#pofile").attr('disabled', true);
$(".submit-certificate-bttn").attr('disabled', true);
window.location.reload();
} else {
swal("Oops!", data.message, "error");
}
});
}
});
}
}
});
Service controller
define(
'JJ.TaxFreeInvoice.TaxFreeInvoice.ServiceController', [
'ServiceController', 'JJ.TaxFreeInvoice.TaxFreeInvoice'
],
function(
ServiceController, UploadFile
) {
'use strict';
return ServiceController.extend({
name: 'JJ.TaxFreeInvoice.TaxFreeInvoice.ServiceController'
// The values in this object are the validation needed for the current service.
,
options: {
common: {}
}
,
get: function get() {
console.log('Test@get');
var body = this.request.getBody();
body = JSON.parse(body);
var invid = body.invid;
console.log('invid in SCget');
// return 'This is test';
if (invid)
return UploadFile.get(invid);
else
return JSON.stringify({ success: false, message: "No invoice is found" });
}
,
post: function post() {
var body = this.request.getBody();
body = JSON.parse(body);
var fileName = body.fileName;
var file = body.file;
var fileExtension = body.fileExtension;
var invInternalid = body.invInternalid;
var invid = body.invid;
console.log('fileName inside taxfreeInvoice SC', fileName);
if (invid){
return UploadFile.get(invid);
}
if (file && file && fileExtension){
return UploadFile.uploadAction(fileName, fileExtension, file, invInternalid);
}
else{
return JSON.stringify({ success: false, message: "No file is found" });
}
}
,
put: function put() {
// not implemented
}
,
delete: function() {
// not implemented
}
});
}
);
SC Model
// JJ.TaxFreeInvoice.TaxFreeInvoice.js
// Load all your starter dependencies in backend for your extension here
// ----------------
define('JJ.TaxFreeInvoice.TaxFreeInvoice', [
'JJ.TaxFreeInvoice.TaxFreeInvoice.ServiceController', 'SC.Model'
], function(
TaxFreeInvoiceServiceController, SCModel
) {
'use strict';
return SCModel.extend({
name: 'JJ.TaxFreeInvoice.TaxFreeInvoice'
,
get: function(INVID) {
console.log("INSIDE SS MODEL GET##");
var CrtField = nlapiLookupField("invoice",INVID,"custbody_tax_free_certificate",false);
if(CrtField==""|| CrtField==''|| CrtField==null || CrtField==undefined){
return JSON.stringify({ success: true, message: "The tax free certificate field is empty" });
}
else {
return JSON.stringify({ success: false, fileId: CrtField, message: "The tax free certificate field is not empty" });
}
},
uploadAction: function(FILENAME, EXTENSION, FILE, INVINTERNALID) {
var FOLDERID = 233734; //Webstore tax Free Certificates in filecabinet
var fileUploaded = false;
console.log('FILENAME inside Tax free invoice SS MODEL ', FILENAME)
if (EXTENSION == "pdf") {
var newFile = nlapiCreateFile(FILENAME, 'PDF', FILE);
fileUploaded = true;
} else if (EXTENSION == "doc") {
var newFile = nlapiCreateFile(FILENAME, 'WORD', FILE);
fileUploaded = true;
} else if (EXTENSION == "docx") {
var newFile = nlapiCreateFile(FILENAME, 'WORD', FILE);
fileUploaded = true;
}
if (fileUploaded) {
newFile.setFolder(FOLDERID);
var newFileId = nlapiSubmitFile(newFile);
console.log('newFileId', newFileId);
var rec = nlapiLoadRecord('invoice', INVINTERNALID, false);
rec.setFieldValue('custbody_tax_free_certificate', newFileId);
var lineNum = rec.getLineItemCount('item');
for (i = 1; i <= lineNum; i++) {
rec.setLineItemValue('item', 'taxcode', i, -8);
}
nlapiSubmitRecord(rec);
return JSON.stringify({ success: true, fileId: newFileId });
} else {
return JSON.stringify({ success: false, message: "You're trying to upload a file that is not acceptable" });
}
}
});
});
Template
<div id="fileContents">
<textarea id="base64full" name="imageBase64" hidden></textarea>
<input type="file" id="pofile" name="file" {{#if IspofileDisabled}} disabled="disabled" {{/if}} accept=".xls,.xlsx,.pdf,.doc,.docx" data-action="filechange">
<button class="submit-certificate-bttn" {{#if IspofileDisabled}} disabled="disabled" {{/if}} data-action="pofile">CLICK TO UPLOAD TAX EXEMPTION CERTIFICATE</button>
</div>