clinic/netforce_clinic/models/invoice_payment.py

123 lines
3.9 KiB
Python
Raw Normal View History

2015-02-21 13:38:21 +00:00
import time
2015-02-20 13:10:40 +00:00
from netforce.model import Model, fields, get_model
from netforce.utils import get_data_path
2015-02-21 13:38:21 +00:00
from netforce.access import get_active_company
2015-02-20 13:10:40 +00:00
2015-02-21 13:38:21 +00:00
class InvoicePayment(Model):
_name="clinic.invoice.payment"
_string="Invoice Payment"
2015-02-20 13:10:40 +00:00
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
total=0
for line in obj.lines:
matching=line.matching_id
for mline in matching.lines:
total+=mline.fee or 0
total+=mline.srv or 0
total+=mline.epo or 0
res[obj.id]={
'total': total,
}
return res
_fields={
'name': fields.Char("Name",required=True),
2015-02-21 13:38:21 +00:00
'lines': fields.One2Many("clinic.invoice.payment.line","invoice_payment_id", "Lines"),
2015-02-20 13:10:40 +00:00
'total': fields.Float("Total",function="_get_all",function_multi=True),
2015-02-21 13:38:21 +00:00
'state': fields.Selection([['draft','Draft'],['waiting_approve','Waiting Approve'],['approved','Approved']],'State'),
'date': fields.Date("Date"),
2015-02-20 13:10:40 +00:00
}
_defaults={
'state': 'draft',
2015-02-21 13:38:21 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d"),
2015-02-20 13:10:40 +00:00
}
2015-02-21 13:38:21 +00:00
def approve(self,ids,context={}):
count=0
2015-02-20 13:10:40 +00:00
for obj in self.browse(ids):
2015-02-21 13:38:21 +00:00
res=obj.make_payment()
count+=1
if res<=1:
return res
def make_payment(self,ids,context={}):
obj=self.browse(ids)[0]
lines=[]
for line in obj.lines:
matches=line.matching_id
for match in matches:
invoice=match.invoice_id
if invoice:
vals={
'invoice_id': invoice.id,
'amount': invoice.amount_due or 0,
}
lines.append(('create', vals))
if not lines:
raise Exception("Nothing to approve")
st=get_model("clinic.setting").browse(1)
if not st.import_account_id:
raise Exception("Import account not found (Ratchawat Setting -> Accounting)")
obj=self.browse(ids)[0]
partner=obj.patient_type_id.contact_id
company_id=get_active_company()
datenow=obj.date or time.strftime("%Y-%m-%d")
vals={
"partner_id": partner.id,
"company_id": company_id,
"type": "in",
"pay_type": "invoice",
'date': datenow,
"account_id": st.import_account_id.id,
'invoice_lines': [],
'rd_cust': True, #XXX
}
payment_id=get_model("account.payment").create(vals,context={"type":"in"})
2015-02-20 13:10:40 +00:00
return {
'next': {
2015-02-21 13:38:21 +00:00
'name': 'payment',
2015-02-20 13:10:40 +00:00
'mode': 'form',
2015-02-21 13:38:21 +00:00
'active_id': payment_id,
2015-02-20 13:10:40 +00:00
},
2015-02-21 13:38:21 +00:00
'flash': 'Create Payment successfully',
2015-02-20 13:10:40 +00:00
}
2015-02-21 13:38:21 +00:00
2015-02-20 13:10:40 +00:00
def update_amount(self,context={}):
data=context['data']
data['total']=0
for line in data['lines']:
data['total']+=line['amount'] or 0
return data
def onchange_matching(self,context={}):
data=context['data']
path=context['path']
line=get_data_path(data,path,parent=True)
matching_id=line['matching_id']
matching=get_model('clinic.matching.payment').browse(matching_id)
line['srv']=matching['total_srv'] or 0
line['epo']=matching['total_epo'] or 0
line['fee']=matching['total_fee'] or 0
line['amount']=line['fee']+line['epo']+line['srv']
data=self.update_amount(context=context)
return data
2015-02-21 13:38:21 +00:00
def to_draft(self,ids,context={}):
for obj in self.browse(ids):
obj.write({
'state': 'draft',
})
def sumbit(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'waiting_approve',
})
2015-02-20 13:10:40 +00:00
2015-02-21 13:38:21 +00:00
InvoicePayment.register()