1
1
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
2
2
# For license information, please see license.txt
3
-
3
+ import json
4
4
import frappe
5
5
from frappe import _
6
6
from frappe .model .document import Document
7
7
from frappe .utils import get_link_to_form
8
+ from erpnext .accounts .general_ledger import make_gl_entries
8
9
9
10
10
11
class SalesCommission (Document ):
@@ -29,10 +30,70 @@ def validate_salary_component(self):
29
30
30
31
def on_submit (self ):
31
32
self .validate_amount ()
33
+ self .make_gl_entries ()
32
34
self .db_set ("status" , "Unpaid" )
33
35
34
36
def on_cancel (self ):
35
37
self .ignore_linked_doctypes = ('GL Entry' ,)
38
+ self .make_gl_entries (cancel = 1 )
39
+
40
+ def add_party_gl_entries (self , gl_entries ):
41
+ return
42
+ if self .party_account :
43
+ if self .payment_type == "Receive" :
44
+ against_account = self .paid_to
45
+ else :
46
+ against_account = self .paid_from
47
+
48
+ party_gl_dict = self .get_gl_dict ({
49
+ "account" : self .party_account ,
50
+ "party_type" : self .party_type ,
51
+ "party" : self .party ,
52
+ "against" : against_account ,
53
+ "account_currency" : self .party_account_currency ,
54
+ "cost_center" : self .cost_center
55
+ }, item = self )
56
+
57
+ dr_or_cr = "credit" if erpnext .get_party_account_type (self .party_type ) == 'Receivable' else "debit"
58
+
59
+ for d in self .get ("references" ):
60
+ cost_center = self .cost_center
61
+ if d .reference_doctype == "Sales Invoice" and not cost_center :
62
+ cost_center = frappe .db .get_value (d .reference_doctype , d .reference_name , "cost_center" )
63
+ gle = party_gl_dict .copy ()
64
+ gle .update ({
65
+ "against_voucher_type" : d .reference_doctype ,
66
+ "against_voucher" : d .reference_name ,
67
+ "cost_center" : cost_center
68
+ })
69
+
70
+ allocated_amount_in_company_currency = flt (flt (d .allocated_amount ) * flt (d .exchange_rate ),
71
+ self .precision ("paid_amount" ))
72
+
73
+ gle .update ({
74
+ dr_or_cr + "_in_account_currency" : d .allocated_amount ,
75
+ dr_or_cr : allocated_amount_in_company_currency
76
+ })
77
+
78
+ gl_entries .append (gle )
79
+
80
+ if self .unallocated_amount :
81
+ exchange_rate = self .get_exchange_rate ()
82
+ base_unallocated_amount = (self .unallocated_amount * exchange_rate )
83
+
84
+ gle = party_gl_dict .copy ()
85
+
86
+ gle .update ({
87
+ dr_or_cr + "_in_account_currency" : self .unallocated_amount ,
88
+ dr_or_cr : base_unallocated_amount
89
+ })
90
+
91
+ gl_entries .append (gle )
92
+
93
+ def make_gl_entries (self , cancel = 0 ):
94
+ gl_entries = []
95
+ self .add_party_gl_entries (gl_entries )
96
+ make_gl_entries (gl_entries , cancel = cancel )
36
97
37
98
@frappe .whitelist ()
38
99
def add_contributions (self , process_sales_commission ):
@@ -166,3 +227,19 @@ def add_record(record, sales_person):
166
227
if frappe .db .get_value ("Sales Commission" , {"name" : contributions ["parent" ]}, fieldname = ["sales_person" ]) == sales_person :
167
228
return False
168
229
return True
230
+
231
+
232
+ @frappe .whitelist ()
233
+ def payout_entries (names , mode_of_payment = None , reference_no = None , reference_date = None ):
234
+ if not frappe .has_permission ("Sales Commission" , "write" ):
235
+ frappe .throw (_ ("Not permitted" ), frappe .PermissionError )
236
+
237
+ names = json .loads (names )
238
+ for name in names :
239
+ sales_commission = frappe .get_doc ("Sales Commission" , name )
240
+ if sales_commission .docstatus != 1 or sales_commission .status == 'Paid' :
241
+ continue
242
+
243
+ sales_commission .payout_entry (mode_of_payment , reference_no , reference_date )
244
+
245
+ frappe .local .message_log = []
0 commit comments