@@ -165,13 +165,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
165165 ) ;
166166 }
167167 }
168-
169- // Show buttons only when pos view is active
170- if ( cint ( doc . docstatus == 0 ) && this . frm . page . current_view_name !== "pos" && ! doc . is_return ) {
171- this . frm . cscript . sales_order_btn ( ) ;
172- this . frm . cscript . delivery_note_btn ( ) ;
173- this . frm . cscript . quotation_btn ( ) ;
174- }
168+ this . toggle_get_items ( ) ;
175169
176170 this . set_default_print_format ( ) ;
177171 if ( doc . docstatus == 1 && ! doc . inter_company_invoice_reference ) {
@@ -260,6 +254,93 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
260254 }
261255 }
262256
257+ toggle_get_items ( ) {
258+ const buttons = [ "Sales Order" , "Quotation" , "Timesheet" , "Delivery Note" ] ;
259+
260+ buttons . forEach ( ( label ) => {
261+ this . frm . remove_custom_button ( label , "Get Items From" ) ;
262+ } ) ;
263+
264+ if ( cint ( this . frm . doc . docstatus ) !== 0 || this . frm . page . current_view_name === "pos" ) {
265+ return ;
266+ }
267+
268+ if ( ! this . frm . doc . is_return ) {
269+ this . frm . cscript . sales_order_btn ( ) ;
270+ this . frm . cscript . quotation_btn ( ) ;
271+ this . frm . cscript . timesheet_btn ( ) ;
272+ }
273+
274+ this . frm . cscript . delivery_note_btn ( ) ;
275+ }
276+
277+ timesheet_btn ( ) {
278+ var me = this ;
279+
280+ me . frm . add_custom_button (
281+ __ ( "Timesheet" ) ,
282+ function ( ) {
283+ let d = new frappe . ui . Dialog ( {
284+ title : __ ( "Fetch Timesheet" ) ,
285+ fields : [
286+ {
287+ label : __ ( "From" ) ,
288+ fieldname : "from_time" ,
289+ fieldtype : "Date" ,
290+ reqd : 1 ,
291+ } ,
292+ {
293+ label : __ ( "Item Code" ) ,
294+ fieldname : "item_code" ,
295+ fieldtype : "Link" ,
296+ options : "Item" ,
297+ get_query : ( ) => {
298+ return {
299+ query : "erpnext.controllers.queries.item_query" ,
300+ filters : {
301+ is_sales_item : 1 ,
302+ customer : me . frm . doc . customer ,
303+ has_variants : 0 ,
304+ } ,
305+ } ;
306+ } ,
307+ } ,
308+ {
309+ fieldtype : "Column Break" ,
310+ fieldname : "col_break_1" ,
311+ } ,
312+ {
313+ label : __ ( "To" ) ,
314+ fieldname : "to_time" ,
315+ fieldtype : "Date" ,
316+ reqd : 1 ,
317+ } ,
318+ {
319+ label : __ ( "Project" ) ,
320+ fieldname : "project" ,
321+ fieldtype : "Link" ,
322+ options : "Project" ,
323+ default : me . frm . doc . project ,
324+ } ,
325+ ] ,
326+ primary_action : function ( ) {
327+ const data = d . get_values ( ) ;
328+ me . frm . events . add_timesheet_data ( me . frm , {
329+ from_time : data . from_time ,
330+ to_time : data . to_time ,
331+ project : data . project ,
332+ item_code : data . item_code ,
333+ } ) ;
334+ d . hide ( ) ;
335+ } ,
336+ primary_action_label : __ ( "Get Timesheets" ) ,
337+ } ) ;
338+ d . show ( ) ;
339+ } ,
340+ __ ( "Get Items From" )
341+ ) ;
342+ }
343+
263344 sales_order_btn ( ) {
264345 var me = this ;
265346
@@ -331,6 +412,12 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
331412 this . $delivery_note_btn = this . frm . add_custom_button (
332413 __ ( "Delivery Note" ) ,
333414 function ( ) {
415+ if ( ! me . frm . doc . customer ) {
416+ frappe . throw ( {
417+ title : __ ( "Mandatory" ) ,
418+ message : __ ( "Please Select a Customer" ) ,
419+ } ) ;
420+ }
334421 erpnext . utils . map_current_doc ( {
335422 method : "erpnext.stock.doctype.delivery_note.delivery_note.make_sales_invoice" ,
336423 source_doctype : "Delivery Note" ,
@@ -343,7 +430,7 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
343430 var filters = {
344431 docstatus : 1 ,
345432 company : me . frm . doc . company ,
346- is_return : 0 ,
433+ is_return : me . frm . doc . is_return ,
347434 } ;
348435 if ( me . frm . doc . customer ) filters [ "customer" ] = me . frm . doc . customer ;
349436 return {
@@ -610,6 +697,10 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends (
610697 apply_tds ( frm ) {
611698 this . frm . clear_table ( "tax_withholding_entries" ) ;
612699 }
700+
701+ is_return ( ) {
702+ this . toggle_get_items ( ) ;
703+ }
613704} ;
614705
615706// for backward compatibility: combine new and previous states
@@ -1061,71 +1152,6 @@ frappe.ui.form.on("Sales Invoice", {
10611152 } ,
10621153
10631154 refresh : function ( frm ) {
1064- if ( frm . doc . docstatus === 0 && ! frm . doc . is_return ) {
1065- frm . add_custom_button (
1066- __ ( "Timesheet" ) ,
1067- function ( ) {
1068- let d = new frappe . ui . Dialog ( {
1069- title : __ ( "Fetch Timesheet" ) ,
1070- fields : [
1071- {
1072- label : __ ( "From" ) ,
1073- fieldname : "from_time" ,
1074- fieldtype : "Date" ,
1075- reqd : 1 ,
1076- } ,
1077- {
1078- label : __ ( "Item Code" ) ,
1079- fieldname : "item_code" ,
1080- fieldtype : "Link" ,
1081- options : "Item" ,
1082- get_query : ( ) => {
1083- return {
1084- query : "erpnext.controllers.queries.item_query" ,
1085- filters : {
1086- is_sales_item : 1 ,
1087- customer : frm . doc . customer ,
1088- has_variants : 0 ,
1089- } ,
1090- } ;
1091- } ,
1092- } ,
1093- {
1094- fieldtype : "Column Break" ,
1095- fieldname : "col_break_1" ,
1096- } ,
1097- {
1098- label : __ ( "To" ) ,
1099- fieldname : "to_time" ,
1100- fieldtype : "Date" ,
1101- reqd : 1 ,
1102- } ,
1103- {
1104- label : __ ( "Project" ) ,
1105- fieldname : "project" ,
1106- fieldtype : "Link" ,
1107- options : "Project" ,
1108- default : frm . doc . project ,
1109- } ,
1110- ] ,
1111- primary_action : function ( ) {
1112- const data = d . get_values ( ) ;
1113- frm . events . add_timesheet_data ( frm , {
1114- from_time : data . from_time ,
1115- to_time : data . to_time ,
1116- project : data . project ,
1117- item_code : data . item_code ,
1118- } ) ;
1119- d . hide ( ) ;
1120- } ,
1121- primary_action_label : __ ( "Get Timesheets" ) ,
1122- } ) ;
1123- d . show ( ) ;
1124- } ,
1125- __ ( "Get Items From" )
1126- ) ;
1127- }
1128-
11291155 if ( frm . doc . is_debit_note ) {
11301156 frm . set_df_property ( "return_against" , "label" , __ ( "Adjustment Against" ) ) ;
11311157 }
0 commit comments