clinic/netforce_clinic/models/account_payment.py

176 lines
6.2 KiB
Python

from netforce.model import Model, fields, get_model
class AccountPayment(Model):
_inherit="account.payment"
_fields={
'rd_cust': fields.Boolean("RD Customize"),
'number': fields.Char("Number",required=True,search=True),
}
def _get_number(self,context={}):
type=context.get("type")
if type=="in":
seq_type="pay_in"
elif type=="out":
seq_type="pay_out"
else:
return
seq_id=get_model("sequence").find_sequence(type=seq_type,context=context) # force to use context
if not seq_id:
return None
while 1:
num=get_model("sequence").get_next_number(seq_id,context=context)
res=self.search([["number","=",num]])
if not res:
return num
get_model("sequence").increment_number(seq_id,context=context)
_defaults={
'number': _get_number,
}
def run_report(self,ids,context={}):
obj=self.browse(ids)[0]
hd_case_id=obj.related_id.id
hd_case=get_model("clinic.hd.case").browse(hd_case_id)
# TODO
# set payment_id on hd case
# send to action print form payment
hd_case.write({
'payment_id': obj.id,
})
return {
'next': {
'name': 'report_clinic_payment_form',
'refer_id': hd_case_id,
'payment_id': obj.id,
'convert': 'pdf',
},
}
def import_payment(self,ids,context={}):
if not ids:
raise Exception("Please save payment before import")
return {
'next': {
'name': 'clinic_import_uc',
'refer_id': ids[0], #XXX
}
}
def post(self,ids,context={}):
obj=self.browse(ids)[0]
res=None
if obj.rd_cust:
res={}
print("RD Customize")
desc="Recieved %s"%obj.partner_id.name
for ivline in obj.invoice_lines:
invoice=ivline.invoice_id
partner_id=invoice.partner_id.id
for mline in invoice.move_id.lines:
if mline.debit>0:
amt=mline.debit or 0
account_id=mline.account_id.id
if not res.get(account_id):
res[account_id]={
'credit': 0,
'debit': 0,
'description': desc,
'partner_id': partner_id
}
res[account_id]['credit']+=amt
settings=get_model("settings").browse(1)
if obj.type=="in":
journal_id=obj.journal_id.id or settings.pay_in_journal_id.id
if not journal_id:
raise Exception("Receipts journal not found")
elif obj.type=="out":
journal_id=obj.journal_id.id or settings.pay_out_journal_id.id
if not journal_id:
raise Exception("Disbursements journal not found")
if not obj.number:
raise Exception("Missing payment number")
move_vals={
"journal_id": journal_id,
"number": obj.number,
"date": obj.date,
"narration": desc,
"related_id": "account.payment,%s"%obj.id,
"company_id": obj.company_id.id,
}
move_id=get_model("account.move").create(move_vals)
track_id=None
for line in obj.lines: # XXX
if line.track_id:
if track_id:
track_id=None
break
else:
track_id=line.track_id.id
lines1=[]
lines1.append(('create',{
"move_id": move_id,
"account_id": obj.account_id.id,
"description": desc,
"track_id": track_id,
'debit': 0,
'credit':0,
}))
lines2=[]
for account_id, rvals in res.items():
amt=rvals['credit'] or 0
lines1[0][1]['debit']+=amt #XXX
lines2.append(('create',{
"move_id": move_id,
"account_id": account_id,
"description": rvals['description'],
'partner_id': rvals['partner_id'],
'debit': rvals['debit'],
'credit': rvals['credit'],
"track_id": track_id,
}))
if obj.type=="in":
rate_type="sell"
else:
rate_type="buy"
adjust_lines=[]
adjust_amt=0
for jline in obj.adjust_lines:
cur_amt=get_model("currency").convert(line.amount,obj.currency_id.id,settings.currency_id.id,date=obj.date,rate_type=rate_type)
tax_base=get_model("currency").convert(line.tax_base or 0,obj.currency_id.id,settings.currency_id.id,date=obj.date,rate_type=rate_type)
cur_amt=abs(cur_amt)
adjust_lines.append(('create',{
"move_id": move_id,
"description": desc,
"account_id": line.account_id.id,
"tax_comp_id": line.tax_comp_id.id,
"tax_base": tax_base,
"track_id": line.track_id.id,
"partner_id": obj.partner_id.id,
"credit":0,
"debit": cur_amt,
}))
#XXX
adjust_amt+=cur_amt
lines1[0][1]['debit']-=cur_amt
lines=lines1+adjust_lines+lines2 #debit, debit, credit
move=get_model("account.move").browse(move_id)
move.write({
'lines': lines,
})
move.post()
obj.write({
'move_id': move.id,
'state': 'posted',
})
print("Done!")
else:
res=super().post(ids,context=context)
return res
AccountPayment.register()