import time from netforce.model import Model, fields, get_model from netforce.utils import get_data_path from netforce.access import get_active_company, get_active_user 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 total_fee=0 total_srv=0 total_epo=0 for line in obj.lines: matching=line.matching_id for mline in matching.lines: if mline.state!='match': continue 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 res[obj.id]={ 'total': total, 'total_epo': total_epo, 'total_fee': total_fee, 'total_srv': total_srv, } 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), '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'), 'date': fields.Date("Date"), 'user_id': fields.Many2One("base.user","Approver"), 'partner_id': fields.Many2One("partner","Contact"), 'payment_id': fields.Many2One("account.payment","Payment"), } _defaults={ 'state': 'draft', 'date': lambda *a: time.strftime("%Y-%m-%d"), } def send_to_payment(self,ids,context={}): count=0 for obj in self.browse(ids): res=obj.make_payment() count+=1 obj.write({ 'state': 'done', }) # support list view if count<=1: return res def approve(self,ids,context={}): for obj in self.browse(ids): obj.write({ 'state': 'approved', 'user_id': get_active_user(), }) def make_payment(self,ids,context={}): obj=self.browse(ids)[0] invoice_lines=[] for line in obj.lines: for mline in line.matching_id.lines: invoice=mline.invoice_id state=mline.state if invoice and state=='match': vals={ 'invoice_id': invoice.id, 'amount': invoice.amount_due or 0, } invoice_lines.append(('create', vals)) if not invoice_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.partner_id company_id=get_active_company() datenow=obj.date or time.strftime("%Y-%m-%d") memo='Payment; %s'%(partner.name) 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': invoice_lines, 'rd_cust': True, #XXX 'memo': memo, } payment_id=get_model("account.payment").create(vals,context={"type":"in"}) obj.write({ 'payment_id': payment_id, }) 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 submit(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") 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, }, } InvoicePayment.register()