@@ -439,6 +439,85 @@ async function showInsertDocDataDialog(doctype, bucket = "Current Doc", tabId) {
439439 , args , tabId ) ;
440440}
441441
442+ async function showCSVToolDialog ( payload , tabId ) {
443+ let args = { }
444+ idfExec ( ( args ) => {
445+ let csv_tool_dialog = new frappe . ui . Dialog ( {
446+ title : `${ window . idfState . idfLogoUrl1 } CSV Tool` ,
447+ fields : [
448+ { label : __ ( "Doctype" ) , fieldtype : "Data" , fieldname : "current_doctype" , read_only : 1 , default : cur_list . doctype } ,
449+ { fieldtype : "Column Break" } ,
450+ {
451+ label : __ ( "Get Import Template" ) ,
452+ fieldtype : "Button" ,
453+ description : __ ( "Note: Child Table Fields not supported when importing with this tool" ) ,
454+ click : ( ) => {
455+ frappe . new_doc ( "Data Import" , {
456+ reference_doctype : csv_tool_dialog . get_field ( "current_doctype" ) . value ,
457+ import_type : __ ( "Update Existing Records" )
458+ } , ( ) => {
459+ setTimeout ( ( ) => {
460+ cur_frm . script_manager . trigger ( "download_template" )
461+ } , 2000 )
462+ } )
463+
464+
465+ }
466+ } ,
467+ { label : __ ( "Data Import" ) , fieldtype : "Section Break" } ,
468+ { label : __ ( "Import Type" ) , fieldtype : "Select" , options : [ "Create Records" , "Update Records" ] , default : "Update Records" , read_only : 1 } ,
469+ {
470+ fieldtype : "HTML" ,
471+ fieldname : "file_type" ,
472+ options : `
473+ <div>
474+ <label for="formFileLg" class="form-label">CSV File</label>
475+ <input class="form-control form-control-lg" id="formFileLg" type="file" accept=".csv">
476+ </div>
477+ `
478+ } ,
479+ ] ,
480+ primary_action_label : `${ window . idfState . idfLogoUrl1 } Apply` ,
481+ primary_action : async function ( values ) {
482+ csv_tool_dialog . hide ( ) ;
483+ let csvFile = document . querySelector ( "#formFileLg" ) . files [ 0 ] ;
484+ postMessage ( {
485+ eventName : "idf_cs_request__csv-tool-read_file" ,
486+ payload : {
487+ doctype : cur_list . doctype ,
488+ docfields : frappe . meta . get_docfields ( cur_list . doctype ) . map ( df => {
489+ return { label : df . label , fieldname : df . fieldname }
490+ } ) ,
491+ file : csvFile
492+ }
493+ } ) ;
494+ frappe . show_alert ( `${ window . idfState . idfLogoUrl1 } in progress, pelase wait...` ) ;
495+ }
496+ } )
497+ csv_tool_dialog . show ( ) ;
498+ }
499+ , args , tabId ) ;
500+ }
501+
502+ async function handleCSVToolBulkUpdate ( docs , tabId ) {
503+ const args = { docs : docs }
504+
505+ idfExec ( ( args ) => {
506+ frappe . call ( {
507+ method : "frappe.client.bulk_update" ,
508+ args : {
509+ docs : args . docs
510+ } ,
511+ callback : function ( r ) {
512+ console . log ( r . message ) ;
513+ frappe . show_alert ( `${ window . idfState . idfLogoUrl1 } Imports (${ args . docs . length } ), Fails(${ r . message . failed_docs . length } )` ) ;
514+ }
515+ } )
516+ }
517+ , args , tabId ) ;
518+
519+ }
520+
442521async function saveChildTableData ( payload ) {
443522 await chrome . storage . local . set ( {
444523 "storage__childtable_data" : payload
@@ -539,7 +618,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
539618 if ( ! window . idfState . pageInit ) {
540619 window . idfState . pageInit = true ;
541620
542- // paching frappe script manager to add forms scripting features
621+ // hooking frappe script manager to add forms scripting features
543622 let oriTrigger = frappe . ui . form . ScriptManager . prototype . trigger ;
544623 frappe . ui . form . ScriptManager . prototype . trigger = function ( ...args ) {
545624 setTimeout ( ( ) => {
@@ -551,7 +630,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
551630 , 100 ) ;
552631 return oriTrigger . call ( this , ...args ) ;
553632 }
554- // paching frappe setup_view to add listview scripting features
633+ // hooking frappe setup_view to add listview scripting features
555634 let oriViewSetup = frappe . views . ListView . prototype . setup_view ;
556635 frappe . views . ListView . prototype . setup_view = function ( ...args ) {
557636 setTimeout ( ( ) => {
@@ -595,6 +674,11 @@ chrome.runtime.onMessage.addListener(async (event, sender, sendResponse) => {
595674 case "idf_bg_request__listview_show-insert-doc-data-dialog" :
596675 showInsertDocDataDialog ( event . payload . doctype , event . payload . bucket , tabId ) ;
597676 break ;
677+ case "idf_bg_request__listview_show-csv-tool-dialog" :
678+ showCSVToolDialog ( event . payload , tabId ) ;
679+ break ;
680+ case "idf_bg_request__csv-tool-bulk_update" :
681+ handleCSVToolBulkUpdate ( event . payload , tabId ) ;
598682 case "idf_bg_request__childtable_save" :
599683 saveChildTableData ( event . payload ) ;
600684 break ;
0 commit comments