clinic/netforce_clinic/models/invoice_payment.py

197 lines
6.3 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-25 14:14:57 +00:00
from netforce.access import get_active_company, get_active_user
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
2015-02-25 14:14:57 +00:00
total_fee=0
total_srv=0
total_epo=0
2015-02-20 13:10:40 +00:00
for line in obj.lines:
matching=line.matching_id
for mline in matching.lines:
2015-02-25 15:23:16 +00:00
if mline.state!='match':
continue
2015-02-25 14:14:57 +00:00
fee=mline.fee or 0
total+=fee
total_fee+=fee
srv=mline.srv or 0
total+=srv
total_srv+=srv
epo=mline.epo or 0
total+=epo
total_epo+=epo
2015-02-20 13:10:40 +00:00
res[obj.id]={
'total': total,
2015-02-25 14:14:57 +00:00
'total_epo': total_epo,
'total_fee': total_fee,
'total_srv': total_srv,
2015-02-20 13:10:40 +00:00
}
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-25 14:14:57 +00:00
'total_fee': fields.Float("FEE",function="_get_all",function_multi=True),
'total_epo': fields.Float("EPO",function="_get_all",function_multi=True),
'total_srv': fields.Float("Service",function="_get_all",function_multi=True),
'state': fields.Selection([['draft','Draft'],['waiting_approve','Waiting Approval'],['approved','Approved'],['done','Done']],'State'),
2015-02-21 13:38:21 +00:00
'date': fields.Date("Date"),
2015-02-25 14:14:57 +00:00
'user_id': fields.Many2One("base.user","Approver"),
2015-02-25 15:23:16 +00:00
'partner_id': fields.Many2One("partner","Contact"),
'payment_id': fields.Many2One("account.payment","Payment"),
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-25 14:14:57 +00:00
def send_to_payment(self,ids,context={}):
2015-02-21 13:38:21 +00:00
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
2015-02-25 15:23:16 +00:00
obj.write({
'state': 'done',
})
# support list view
if count<=1:
2015-02-21 13:38:21 +00:00
return res
2015-02-25 14:14:57 +00:00
def approve(self,ids,context={}):
for obj in self.browse(ids):
obj.write({
2015-02-25 15:23:16 +00:00
'state': 'approved',
2015-02-25 14:14:57 +00:00
'user_id': get_active_user(),
})
2015-02-21 13:38:21 +00:00
def make_payment(self,ids,context={}):
obj=self.browse(ids)[0]
2015-02-25 15:23:16 +00:00
invoice_lines=[]
2015-02-21 13:38:21 +00:00
for line in obj.lines:
2015-02-25 15:23:16 +00:00
for mline in line.matching_id.lines:
invoice=mline.invoice_id
state=mline.state
if invoice and state=='match':
2015-02-21 13:38:21 +00:00
vals={
'invoice_id': invoice.id,
'amount': invoice.amount_due or 0,
}
2015-02-25 15:23:16 +00:00
invoice_lines.append(('create', vals))
if not invoice_lines:
2015-02-21 13:38:21 +00:00
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]
2015-02-25 15:23:16 +00:00
partner=obj.partner_id
2015-02-21 13:38:21 +00:00
company_id=get_active_company()
datenow=obj.date or time.strftime("%Y-%m-%d")
2015-02-25 15:23:16 +00:00
memo='Payment; %s'%(partner.name)
2015-02-21 13:38:21 +00:00
vals={
"partner_id": partner.id,
"company_id": company_id,
"type": "in",
"pay_type": "invoice",
'date': datenow,
"account_id": st.import_account_id.id,
2015-02-25 15:23:16 +00:00
'invoice_lines': invoice_lines,
2015-02-21 13:38:21 +00:00
'rd_cust': True, #XXX
2015-02-25 15:23:16 +00:00
'memo': memo,
2015-02-21 13:38:21 +00:00
}
payment_id=get_model("account.payment").create(vals,context={"type":"in"})
2015-02-25 15:23:16 +00:00
obj.write({
'payment_id': payment_id,
})
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',
})
2015-02-25 14:14:57 +00:00
def submit(self,ids,context={}):
2015-02-21 13:38:21 +00:00
obj=self.browse(ids)[0]
obj.write({
'state': 'waiting_approve',
})
2015-02-24 14:59:49 +00:00
def post(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'done',
})
print("Post")
return {
'next': {
'name': 'clinic_invoice_payment',
'mode': 'form',
'active_id': obj.id,
},
'flash': 'Posted.An message will send to you after finish',
}
def post_invoice(self,ids,context={}):
print("post_invoice")
2015-02-25 15:23:16 +00:00
def reject(self,ids,context={}):
# send msg to user
for obj in self.browse(ids):
obj.write({
'state': 'draft',
})
def view_payment(self,ids,context={}):
obj=self.browse(ids)[0]
payment_id=None
if obj.payment_id:
payment_id=obj.payment_id.id
return {
'next': {
'name': 'payment',
'mode': 'form',
'active_id': payment_id,
},
}
2015-02-20 13:10:40 +00:00
2015-02-21 13:38:21 +00:00
InvoicePayment.register()