# -*- coding: utf-8 -*-
# Copyright (c) 2021, Noah Jacob and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils import flt
from accounting.accounting.general_ledger import make_gl_entry, make_reverse_gl_entry

class JournalEntry(Document):

	def validate(self):
		calc_total_debit_credit(self)
		if self.difference:
			frappe.throw("The total debit and credit must be equal. The current difference is {}".format(self.difference))
		if self.total_credit == 0 or self.total_debit == 0 :
			frappe.throw('Total Cannot be Zero')
		if not self.accounts:
			frappe.throw('Account Entries are required')
		else:
			self.title = self.accounts[0].account





	def on_submit(self):
		for entry in self.accounts:
			make_gl_entry(self,entry.account,entry.debit,entry.credit)
			

	def on_cancel(self):
		# cancel gl entry
		make_reverse_gl_entry(self,self.doctype,self.name)
		
	
def calc_total_debit_credit(self):
	self.total_debit, self.total_credit,self.difference = 0,0,0
	for entry in self.accounts:
		self.total_debit = flt(self.total_debit) +flt(entry.debit) 
		self.total_credit = flt(self.total_credit) + flt(entry.credit)

	self.difference = flt(self.total_debit) - (self.total_credit)
[((35, 2, 35, 52), 'accounting.accounting.general_ledger.make_reverse_gl_entry', 'make_reverse_gl_entry', ({(35, 24, 35, 28): 'self', (35, 29, 35, 41): 'self.doctype', (35, 42, 35, 51): 'self.name'}, {}), '(self, self.doctype, self.name)', False, 'from accounting.accounting.general_ledger import make_gl_entry, make_reverse_gl_entry\n'), ((44, 19, 44, 40), 'frappe.utils.flt', 'flt', ({(44, 23, 44, 39): 'self.total_debit'}, {}), '(self.total_debit)', False, 'from frappe.utils import flt\n'), ((18, 3, 18, 39), 'frappe.throw', 'frappe.throw', ({(18, 16, 18, 38): '"""Total Cannot be Zero"""'}, {}), "('Total Cannot be Zero')", False, 'import frappe\n'), ((20, 3, 20, 47), 'frappe.throw', 'frappe.throw', ({(20, 16, 20, 46): '"""Account Entries are required"""'}, {}), "('Account Entries are required')", False, 'import frappe\n'), ((30, 3, 30, 61), 'accounting.accounting.general_ledger.make_gl_entry', 'make_gl_entry', ({(30, 17, 30, 21): 'self', (30, 22, 30, 35): 'entry.account', (30, 36, 30, 47): 'entry.debit', (30, 48, 30, 60): 'entry.credit'}, {}), '(self, entry.account, entry.debit, entry.credit)', False, 'from accounting.accounting.general_ledger import make_gl_entry, make_reverse_gl_entry\n'), ((41, 21, 41, 42), 'frappe.utils.flt', 'flt', ({(41, 25, 41, 41): 'self.total_debit'}, {}), '(self.total_debit)', False, 'from frappe.utils import flt\n'), ((41, 44, 41, 60), 'frappe.utils.flt', 'flt', ({(41, 48, 41, 59): 'entry.debit'}, {}), '(entry.debit)', False, 'from frappe.utils import flt\n'), ((42, 22, 42, 44), 'frappe.utils.flt', 'flt', ({(42, 26, 42, 43): 'self.total_credit'}, {}), '(self.total_credit)', False, 'from frappe.utils import flt\n'), ((42, 47, 42, 64), 'frappe.utils.flt', 'flt', ({(42, 51, 42, 63): 'entry.credit'}, {}), '(entry.credit)', False, 'from frappe.utils import flt\n')]