import time from netforce.model import Model, fields, get_model from netforce.utils import get_data_path from netforce.access import get_active_company class InvoicePayment(Model): _name="clinic.invoice.payment" _string="Invoice Payment" 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), 'lines': fields.One2Many("clinic.invoice.payment.line","invoice_payment_id", "Lines"), 'total': fields.Float("Total",function="_get_all",function_multi=True), 'state': fields.Selection([['draft','Draft'],['waiting_approve','Waiting Approve'],['approved','Approved'],['done','Done']],'State'), 'date': fields.Date("Date"), } _defaults={ 'state': 'draft', 'date': lambda *a: time.strftime("%Y-%m-%d"), } def approve(self,ids,context={}): count=0 for obj in self.browse(ids): 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"}) return { 'next': { 'name': 'payment', 'mode': 'form', 'active_id': payment_id, }, 'flash': 'Create Payment successfully', } 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 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', }) 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") InvoicePayment.register()