init
							parent
							
								
									70e672057b
								
							
						
					
					
						commit
						358e923a9c
					
				| 
						 | 
					@ -1,5 +0,0 @@
 | 
				
			||||||
<inherit model="account.payment" inherit="payment_form">
 | 
					 | 
				
			||||||
    <field name="default_line_desc" position="before">
 | 
					 | 
				
			||||||
        <field name="rd_cust" span="2"/>
 | 
					 | 
				
			||||||
    </field>
 | 
					 | 
				
			||||||
</inherit>
 | 
					 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,5 @@
 | 
				
			||||||
    </field>
 | 
					    </field>
 | 
				
			||||||
    <field name="memo" position="after">
 | 
					    <field name="memo" position="after">
 | 
				
			||||||
        <field name="account_id" span="2"/>
 | 
					        <field name="account_id" span="2"/>
 | 
				
			||||||
        <field name="hdcase_reconcile" span="2"/>
 | 
					 | 
				
			||||||
    </field>
 | 
					    </field>
 | 
				
			||||||
</inherit>
 | 
					</inherit>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,9 +51,7 @@ from . import gen_visit_line
 | 
				
			||||||
from . import gen_visit_time
 | 
					from . import gen_visit_time
 | 
				
			||||||
from . import payment
 | 
					from . import payment
 | 
				
			||||||
from . import account_payment
 | 
					from . import account_payment
 | 
				
			||||||
from . import account_payment_line
 | 
					 | 
				
			||||||
from . import account_invoice
 | 
					from . import account_invoice
 | 
				
			||||||
from . import account_invoice_line
 | 
					 | 
				
			||||||
from . import staff
 | 
					from . import staff
 | 
				
			||||||
from . import staff_categ
 | 
					from . import staff_categ
 | 
				
			||||||
from . import staff_level
 | 
					from . import staff_level
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,8 +8,6 @@ class AccountInvoice(Model):
 | 
				
			||||||
        'clinic_expense_id': fields.Many2One("clinic.hd.case.expense","Expense"),
 | 
					        'clinic_expense_id': fields.Many2One("clinic.hd.case.expense","Expense"),
 | 
				
			||||||
        'department_id': fields.Many2One("clinic.department","Department",search=True),
 | 
					        'department_id': fields.Many2One("clinic.department","Department",search=True),
 | 
				
			||||||
        'patient_partner_id': fields.Many2One("partner","Patient",search=True),
 | 
					        'patient_partner_id': fields.Many2One("partner","Patient",search=True),
 | 
				
			||||||
        'hdcase_credit': fields.Boolean("HD Case Credit"),
 | 
					 | 
				
			||||||
        'hdcase_reconcile': fields.Boolean("HD Case Reconcile"),
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _get_number(self,context={}):
 | 
					    def _get_number(self,context={}):
 | 
				
			||||||
| 
						 | 
					@ -78,249 +76,4 @@ class AccountInvoice(Model):
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                get_model("account.fixed.asset").create(vals)
 | 
					                get_model("account.fixed.asset").create(vals)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def post(self,ids,context={}):
 | 
					 | 
				
			||||||
        t0=time.time()
 | 
					 | 
				
			||||||
        settings=get_model("settings").browse(1)
 | 
					 | 
				
			||||||
        for obj in self.browse(ids):
 | 
					 | 
				
			||||||
            obj.check_related()
 | 
					 | 
				
			||||||
            if abs(obj.amount_total)<0.001:
 | 
					 | 
				
			||||||
                raise Exception("Invoice total is zero")
 | 
					 | 
				
			||||||
            partner=obj.partner_id
 | 
					 | 
				
			||||||
            if obj.type=="out":
 | 
					 | 
				
			||||||
                account_id=partner.account_receivable_id.id or settings.account_receivable_id.id
 | 
					 | 
				
			||||||
                if not account_id:
 | 
					 | 
				
			||||||
                    raise Exception("Account receivable not found")
 | 
					 | 
				
			||||||
            elif obj.type=="in":
 | 
					 | 
				
			||||||
                account_id=partner.account_payable_id.id or settings.account_payable_id.id
 | 
					 | 
				
			||||||
                if not account_id:
 | 
					 | 
				
			||||||
                    raise Exception("Account payable not found")
 | 
					 | 
				
			||||||
            sign=obj.type=="out" and 1 or -1
 | 
					 | 
				
			||||||
            if obj.inv_type=="credit":
 | 
					 | 
				
			||||||
                sign*=-1
 | 
					 | 
				
			||||||
            obj.write({"account_id": account_id})
 | 
					 | 
				
			||||||
            if obj.type=="out":
 | 
					 | 
				
			||||||
                desc="Sale; "+partner.name
 | 
					 | 
				
			||||||
            elif obj.type=="in":
 | 
					 | 
				
			||||||
                desc="Purchase; "+partner.name
 | 
					 | 
				
			||||||
            if obj.type=="out":
 | 
					 | 
				
			||||||
                journal_id=settings.sale_journal_id.id
 | 
					 | 
				
			||||||
                if not journal_id:
 | 
					 | 
				
			||||||
                    raise Exception("Sales journal not found")
 | 
					 | 
				
			||||||
            elif obj.type=="in":
 | 
					 | 
				
			||||||
                journal_id=settings.purchase_journal_id.id
 | 
					 | 
				
			||||||
                if not journal_id:
 | 
					 | 
				
			||||||
                    raise Exception("Purchases journal not found")
 | 
					 | 
				
			||||||
            if obj.currency_rate:
 | 
					 | 
				
			||||||
                currency_rate=obj.currency_rate
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                if obj.currency_id.id==settings.currency_id.id:
 | 
					 | 
				
			||||||
                    currency_rate=1.0
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    rate_from=obj.currency_id.get_rate(date=obj.date)
 | 
					 | 
				
			||||||
                    if not rate_from:
 | 
					 | 
				
			||||||
                        raise Exception("Missing currency rate for %s"%obj.currency_id.code)
 | 
					 | 
				
			||||||
                    rate_to=settings.currency_id.get_rate(date=obj.date)
 | 
					 | 
				
			||||||
                    if not rate_to:
 | 
					 | 
				
			||||||
                        raise Exception("Missing currency rate for %s"%settings.currency_id.code)
 | 
					 | 
				
			||||||
                    currency_rate=rate_from/rate_to 
 | 
					 | 
				
			||||||
                obj.write({"currency_rate":currency_rate})
 | 
					 | 
				
			||||||
            move_vals={
 | 
					 | 
				
			||||||
                "journal_id": journal_id,
 | 
					 | 
				
			||||||
                "number": obj.number,
 | 
					 | 
				
			||||||
                "date": obj.date,
 | 
					 | 
				
			||||||
                "ref": obj.ref,
 | 
					 | 
				
			||||||
                "narration": desc,
 | 
					 | 
				
			||||||
                "related_id": "account.invoice,%s"%obj.id,
 | 
					 | 
				
			||||||
                "company_id": obj.company_id.id,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            lines=[]
 | 
					 | 
				
			||||||
            taxes={}
 | 
					 | 
				
			||||||
            tax_nos=[]
 | 
					 | 
				
			||||||
            t01=time.time()
 | 
					 | 
				
			||||||
            total_amt=0.0
 | 
					 | 
				
			||||||
            total_base=0.0
 | 
					 | 
				
			||||||
            total_tax=0.0
 | 
					 | 
				
			||||||
            for line in obj.lines:
 | 
					 | 
				
			||||||
                cur_amt=get_model("currency").convert(line.amount,obj.currency_id.id,settings.currency_id.id,rate=currency_rate)
 | 
					 | 
				
			||||||
                total_amt+=cur_amt
 | 
					 | 
				
			||||||
                tax_id=line.tax_id
 | 
					 | 
				
			||||||
                if tax_id and obj.tax_type!="no_tax":
 | 
					 | 
				
			||||||
                    base_amt=get_model("account.tax.rate").compute_base(tax_id,cur_amt,tax_type=obj.tax_type)
 | 
					 | 
				
			||||||
                    tax_comps=get_model("account.tax.rate").compute_taxes(tax_id,base_amt,when="invoice")
 | 
					 | 
				
			||||||
                    for comp_id,tax_amt in tax_comps.items():
 | 
					 | 
				
			||||||
                        tax_vals=taxes.setdefault(comp_id,{"tax_amt":0,"base_amt":0})
 | 
					 | 
				
			||||||
                        tax_vals["tax_amt"]+=tax_amt
 | 
					 | 
				
			||||||
                        tax_vals["base_amt"]+=base_amt
 | 
					 | 
				
			||||||
                        total_tax+=tax_amt
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    base_amt=cur_amt
 | 
					 | 
				
			||||||
                total_base+=base_amt
 | 
					 | 
				
			||||||
                acc_id=line.account_id.id
 | 
					 | 
				
			||||||
                if not acc_id:
 | 
					 | 
				
			||||||
                    raise Exception("Missing line account for invoice line '%s'"%line.description)
 | 
					 | 
				
			||||||
                amt=base_amt*sign
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "description": line.description,
 | 
					 | 
				
			||||||
                    "account_id": acc_id,
 | 
					 | 
				
			||||||
                    "credit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
                    "debit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
                    "track_id": line.track_id.id,
 | 
					 | 
				
			||||||
                    "track2_id": line.track2_id.id,
 | 
					 | 
				
			||||||
                    "partner_id": partner.id,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                lines.append(line_vals)
 | 
					 | 
				
			||||||
            for comp_id,tax_vals in taxes.items():
 | 
					 | 
				
			||||||
                comp=get_model("account.tax.component").browse(comp_id)
 | 
					 | 
				
			||||||
                acc_id=comp.account_id.id
 | 
					 | 
				
			||||||
                if not acc_id:
 | 
					 | 
				
			||||||
                    raise Exception("Missing account for tax component %s"%comp.name)
 | 
					 | 
				
			||||||
                amt=tax_vals["tax_amt"]*sign
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "description": desc,
 | 
					 | 
				
			||||||
                    "account_id": acc_id,
 | 
					 | 
				
			||||||
                    "credit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
                    "debit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
                    "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                    "tax_base": tax_vals["base_amt"],
 | 
					 | 
				
			||||||
                    "partner_id": partner.id,
 | 
					 | 
				
			||||||
                    "invoice_id": obj.id,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if comp.type in ("vat","vat_exempt"):
 | 
					 | 
				
			||||||
                    if obj.type=="out":
 | 
					 | 
				
			||||||
                        if obj.tax_no:
 | 
					 | 
				
			||||||
                            tax_no=obj.tax_no
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            tax_no=self.gen_tax_no(exclude=tax_nos,context={"date":obj.date})
 | 
					 | 
				
			||||||
                            tax_nos.append(tax_no)
 | 
					 | 
				
			||||||
                            obj.write({"tax_no":tax_no})
 | 
					 | 
				
			||||||
                        line_vals["tax_no"]=tax_no
 | 
					 | 
				
			||||||
                    elif obj.type=="in":
 | 
					 | 
				
			||||||
                        line_vals["tax_no"]=obj.tax_no
 | 
					 | 
				
			||||||
                lines.append(line_vals)
 | 
					 | 
				
			||||||
            if obj.tax_type=="tax_in":
 | 
					 | 
				
			||||||
                rounding=total_amt-(total_base+total_tax)
 | 
					 | 
				
			||||||
                if abs(rounding)>0.00499: # XXX
 | 
					 | 
				
			||||||
                    amt=rounding*sign
 | 
					 | 
				
			||||||
                    if not settings.rounding_account_id.id:
 | 
					 | 
				
			||||||
                        raise Exception("Missing rounding account in financial settings")
 | 
					 | 
				
			||||||
                    line_vals={
 | 
					 | 
				
			||||||
                        "description": desc,
 | 
					 | 
				
			||||||
                        "account_id": settings.rounding_account_id.id,
 | 
					 | 
				
			||||||
                        "credit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
                        "debit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
                        "partner_id": partner.id,
 | 
					 | 
				
			||||||
                        "invoice_id": obj.id,
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    lines.append(line_vals)
 | 
					 | 
				
			||||||
            t02=time.time()
 | 
					 | 
				
			||||||
            dt01=(t02-t01)*1000
 | 
					 | 
				
			||||||
            print("post dt01",dt01)
 | 
					 | 
				
			||||||
            groups={}
 | 
					 | 
				
			||||||
            keys=["description","account_id","track_id","tax_comp_id","partner_id","invoice_id","reconcile_id"]
 | 
					 | 
				
			||||||
            for line in lines:
 | 
					 | 
				
			||||||
                key_val=tuple(line.get(k) for k in keys)
 | 
					 | 
				
			||||||
                if key_val in groups:
 | 
					 | 
				
			||||||
                    group=groups[key_val]
 | 
					 | 
				
			||||||
                    group["debit"]+=line["debit"]
 | 
					 | 
				
			||||||
                    group["credit"]+=line["credit"]
 | 
					 | 
				
			||||||
                    if line.get("tax_base"):
 | 
					 | 
				
			||||||
                        if "tax_base" not in group:
 | 
					 | 
				
			||||||
                            group["tax_base"]=0
 | 
					 | 
				
			||||||
                        group["tax_base"]+=line["tax_base"]
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    groups[key_val]=line.copy()
 | 
					 | 
				
			||||||
            group_lines=sorted(groups.values(),key=lambda l: (l["debit"],l["credit"]))
 | 
					 | 
				
			||||||
            for line in group_lines:
 | 
					 | 
				
			||||||
                amt=line["debit"]-line["credit"]
 | 
					 | 
				
			||||||
                amt=get_model("currency").round(settings.currency_id.id,amt)
 | 
					 | 
				
			||||||
                if amt>=0:
 | 
					 | 
				
			||||||
                    line["debit"]=amt
 | 
					 | 
				
			||||||
                    line["credit"]=0
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    line["debit"]=0
 | 
					 | 
				
			||||||
                    line["credit"]=-amt
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            is_match=False
 | 
					 | 
				
			||||||
            if obj.hdcase_reconcile and obj.type=='out':
 | 
					 | 
				
			||||||
                print("#POST: clinic customize")
 | 
					 | 
				
			||||||
                cst=get_model('clinic.setting').browse(1)
 | 
					 | 
				
			||||||
                prod_acc=cst.get_product_account
 | 
					 | 
				
			||||||
                move_vals["lines"]=[]
 | 
					 | 
				
			||||||
                for line in group_lines:
 | 
					 | 
				
			||||||
                    desc=line['description']
 | 
					 | 
				
			||||||
                    if not desc:
 | 
					 | 
				
			||||||
                        print("skip no description ", obj.number)
 | 
					 | 
				
			||||||
                        continue
 | 
					 | 
				
			||||||
                    ar_debit_id=None
 | 
					 | 
				
			||||||
                    #ar_credit_id=None
 | 
					 | 
				
			||||||
                    # search from patient_type
 | 
					 | 
				
			||||||
                    for prod_id in get_model('product').search([['name','=',desc]]):
 | 
					 | 
				
			||||||
                        for ptype_id in get_model("clinic.patient.type").search([['contact_id','=',partner.id]]):
 | 
					 | 
				
			||||||
                            acc=prod_acc(prod_id,ptype_id,'credit')
 | 
					 | 
				
			||||||
                            #ar_credit_id=acc.get("ar_credit_id")
 | 
					 | 
				
			||||||
                            ar_debit_id=acc.get("ar_debit_id")
 | 
					 | 
				
			||||||
                        if ar_debit_id:
 | 
					 | 
				
			||||||
                            break
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    # search from patient
 | 
					 | 
				
			||||||
                    if not ar_debit_id:
 | 
					 | 
				
			||||||
                        for pt in get_model('clinic.patient').search_browse([['partner_id','=',partner.id]]):
 | 
					 | 
				
			||||||
                            acc=prod_acc(prod_id,pt.type_id.id,'credit')
 | 
					 | 
				
			||||||
                            ar_debit_id=acc.get("ar_debit_id")
 | 
					 | 
				
			||||||
                            if ar_debit_id:
 | 
					 | 
				
			||||||
                                break
 | 
					 | 
				
			||||||
                    if not ar_debit_id:
 | 
					 | 
				
			||||||
                        raise Exception("Missing AR Debit Account for product %s"%(desc), partner.id, partner.name)
 | 
					 | 
				
			||||||
                    line_vals={
 | 
					 | 
				
			||||||
                        "description": desc,
 | 
					 | 
				
			||||||
                        "account_id": ar_debit_id,
 | 
					 | 
				
			||||||
                        "debit": line['credit'],
 | 
					 | 
				
			||||||
                        "credit": 0,
 | 
					 | 
				
			||||||
                        "due_date": obj.due_date,
 | 
					 | 
				
			||||||
                        "partner_id": partner.id,
 | 
					 | 
				
			||||||
                        'track_id': line['track_id'],
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    move_vals["lines"]+=[("create",line_vals)]
 | 
					 | 
				
			||||||
                move_vals["lines"]+=[("create",vals) for vals in group_lines]
 | 
					 | 
				
			||||||
                is_match=True
 | 
					 | 
				
			||||||
            if not is_match:
 | 
					 | 
				
			||||||
                print("#POST: standard account")
 | 
					 | 
				
			||||||
                amt=0
 | 
					 | 
				
			||||||
                for line in group_lines:
 | 
					 | 
				
			||||||
                    amt-=line["debit"]-line["credit"]
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "description": desc,
 | 
					 | 
				
			||||||
                    "account_id": account_id,
 | 
					 | 
				
			||||||
                    "debit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
                    "credit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
                    "due_date": obj.due_date,
 | 
					 | 
				
			||||||
                    "partner_id": partner.id,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                acc=get_model("account.account").browse(account_id)
 | 
					 | 
				
			||||||
                if acc.currency_id.id!=settings.currency_id.id:
 | 
					 | 
				
			||||||
                    if acc.currency_id.id!=obj.currency_id.id:
 | 
					 | 
				
			||||||
                        raise Exception("Invalid account currency for this invoice: %s"%acc.code)
 | 
					 | 
				
			||||||
                    line_vals["amount_cur"]=obj.amount_total*sign
 | 
					 | 
				
			||||||
                move_vals["lines"]=[("create",line_vals)]
 | 
					 | 
				
			||||||
                move_vals["lines"]+=[("create",vals) for vals in group_lines]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            t03=time.time()
 | 
					 | 
				
			||||||
            dt02=(t03-t02)*1000
 | 
					 | 
				
			||||||
            print("post dt02",dt02)
 | 
					 | 
				
			||||||
            move_id=get_model("account.move").create(move_vals)
 | 
					 | 
				
			||||||
            t04=time.time()
 | 
					 | 
				
			||||||
            dt03=(t04-t03)*1000
 | 
					 | 
				
			||||||
            print("post dt03",dt03)
 | 
					 | 
				
			||||||
            get_model("account.move").post([move_id])
 | 
					 | 
				
			||||||
            t05=time.time()
 | 
					 | 
				
			||||||
            dt04=(t05-t04)*1000
 | 
					 | 
				
			||||||
            print("post dt04",dt04)
 | 
					 | 
				
			||||||
            obj.write({"move_id":move_id,"state":"waiting_payment"})
 | 
					 | 
				
			||||||
            t06=time.time()
 | 
					 | 
				
			||||||
            dt05=(t06-t05)*1000
 | 
					 | 
				
			||||||
            print("post dt05",dt05)
 | 
					 | 
				
			||||||
        t1=time.time()
 | 
					 | 
				
			||||||
        dt=(t1-t0)*1000
 | 
					 | 
				
			||||||
        print("invoice.post <<< %d ms"%dt)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
AccountInvoice.register()
 | 
					AccountInvoice.register()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,13 +0,0 @@
 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from netforce.model import Model, fields, get_model
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class AccountInvoiceLine(Model):
 | 
					 | 
				
			||||||
    _inherit="account.invoice.line"
 | 
					 | 
				
			||||||
    _fields={
 | 
					 | 
				
			||||||
        'ar_credit_id': fields.Many2One("account.account","Account Credit"),
 | 
					 | 
				
			||||||
        "ar_debit_id": fields.Many2One("account.account","Account Debit"),
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
AccountInvoiceLine.register()
 | 
					 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,6 @@ from netforce.model import Model, fields, get_model
 | 
				
			||||||
class AccountPayment(Model):
 | 
					class AccountPayment(Model):
 | 
				
			||||||
    _inherit="account.payment"
 | 
					    _inherit="account.payment"
 | 
				
			||||||
    _fields={
 | 
					    _fields={
 | 
				
			||||||
        'rd_cust': fields.Boolean("RD Customize"),
 | 
					 | 
				
			||||||
        'number': fields.Char("Number",required=True,search=True),
 | 
					        'number': fields.Char("Number",required=True,search=True),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,564 +50,4 @@ class AccountPayment(Model):
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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 clinic_post(self,ids,context={}):
 | 
					 | 
				
			||||||
        print("account_payment.post")
 | 
					 | 
				
			||||||
        obj=self.browse(ids)[0]
 | 
					 | 
				
			||||||
        settings=get_model("settings").browse(1)
 | 
					 | 
				
			||||||
        if obj.currency_rate:
 | 
					 | 
				
			||||||
            currency_rate=obj.currency_rate
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            if obj.currency_id.id==settings.currency_id.id:
 | 
					 | 
				
			||||||
                currency_rate=1.0
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                rate_from=obj.currency_id.get_rate(date=obj.date)
 | 
					 | 
				
			||||||
                if not rate_from:
 | 
					 | 
				
			||||||
                    raise Exception("Missing currency rate for %s"%obj.currency_id.code)
 | 
					 | 
				
			||||||
                rate_to=settings.currency_id.get_rate(date=obj.date)
 | 
					 | 
				
			||||||
                if not rate_to:
 | 
					 | 
				
			||||||
                    raise Exception("Missing currency rate for %s"%settings.currency_id.code)
 | 
					 | 
				
			||||||
                currency_rate=rate_from/(rate_to or 1)
 | 
					 | 
				
			||||||
            obj.write({"currency_rate":currency_rate})
 | 
					 | 
				
			||||||
        if obj.pay_type=="direct":
 | 
					 | 
				
			||||||
            desc=obj.memo or obj.ref or obj.partner_id.name or obj.number # XXX: as in myob?
 | 
					 | 
				
			||||||
        elif obj.pay_type=="invoice":
 | 
					 | 
				
			||||||
            desc=obj.memo or "Payment; %s"%obj.partner_id.name # XXX: as in myob?
 | 
					 | 
				
			||||||
        elif obj.pay_type=="prepay":
 | 
					 | 
				
			||||||
            desc="Prepayment: %s"%obj.partner_id.name
 | 
					 | 
				
			||||||
        elif obj.pay_type=="overpay":
 | 
					 | 
				
			||||||
            desc="Overpayment: %s"%obj.partner_id.name
 | 
					 | 
				
			||||||
        elif obj.pay_type=="refund":
 | 
					 | 
				
			||||||
            desc="Refund: %s"%obj.partner_id.name
 | 
					 | 
				
			||||||
        elif obj.pay_type=="claim":
 | 
					 | 
				
			||||||
            desc="Expense claim payment"
 | 
					 | 
				
			||||||
        elif obj.pay_type=="adjust":
 | 
					 | 
				
			||||||
            desc="Adjustment"
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            desc="Payment: %s"%obj.partner_id.name
 | 
					 | 
				
			||||||
        if obj.type=="in":
 | 
					 | 
				
			||||||
            journal_id=settings.pay_in_journal_id.id
 | 
					 | 
				
			||||||
            if not journal_id:
 | 
					 | 
				
			||||||
                raise Exception("Receipts journal not found")
 | 
					 | 
				
			||||||
        elif obj.type=="out":
 | 
					 | 
				
			||||||
            journal_id=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)
 | 
					 | 
				
			||||||
        lines=[]
 | 
					 | 
				
			||||||
        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
 | 
					 | 
				
			||||||
        amt=get_model("currency").convert(obj.amount_payment,obj.currency_id.id,settings.currency_id.id,rate=currency_rate)
 | 
					 | 
				
			||||||
        if obj.type=="out":
 | 
					 | 
				
			||||||
            amt=-amt
 | 
					 | 
				
			||||||
        line_vals={
 | 
					 | 
				
			||||||
            "move_id": move_id,
 | 
					 | 
				
			||||||
            "account_id": obj.account_id.id,
 | 
					 | 
				
			||||||
            "description": desc,
 | 
					 | 
				
			||||||
            "track_id": track_id,
 | 
					 | 
				
			||||||
            "debit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
            "credit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        if obj.account_id.currency_id.id!=settings.currency_id.id:
 | 
					 | 
				
			||||||
            if obj.account_id.currency_id.id!=obj.currency_id.id:
 | 
					 | 
				
			||||||
                raise Exception("Invalid account currency for this payment: %s"%obj.account_id.code)
 | 
					 | 
				
			||||||
            line_vals["amount_cur"]=obj.amount_payment if obj.type=="in" else -obj.amount_payment
 | 
					 | 
				
			||||||
        get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
        taxes={}
 | 
					 | 
				
			||||||
        reconcile_ids=[]
 | 
					 | 
				
			||||||
        total_over=0
 | 
					 | 
				
			||||||
        for line in obj.lines:
 | 
					 | 
				
			||||||
            if line.type in ("direct","prepay"):
 | 
					 | 
				
			||||||
                cur_amt=get_model("currency").convert(line.amount,obj.currency_id.id,settings.currency_id.id,rate=currency_rate)
 | 
					 | 
				
			||||||
                tax=line.tax_id
 | 
					 | 
				
			||||||
                if tax and obj.tax_type!="no_tax":
 | 
					 | 
				
			||||||
                    base_amt=get_model("account.tax.rate").compute_base(tax.id,cur_amt,tax_type=obj.tax_type)
 | 
					 | 
				
			||||||
                    tax_comps=get_model("account.tax.rate").compute_taxes(tax.id,base_amt,when="direct_payment")
 | 
					 | 
				
			||||||
                    for comp_id,tax_amt in tax_comps.items():
 | 
					 | 
				
			||||||
                        if comp_id in taxes:
 | 
					 | 
				
			||||||
                            tax_vals=taxes[comp_id]
 | 
					 | 
				
			||||||
                            tax_vals["amount_base"]+=base_amt
 | 
					 | 
				
			||||||
                            tax_vals["amount_tax"]+=tax_amt
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            tax_vals={
 | 
					 | 
				
			||||||
                                "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                                "amount_base": base_amt,
 | 
					 | 
				
			||||||
                                "amount_tax": tax_amt,
 | 
					 | 
				
			||||||
                            }
 | 
					 | 
				
			||||||
                            taxes[comp_id]=tax_vals
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    base_amt=cur_amt
 | 
					 | 
				
			||||||
                if obj.type=="out":
 | 
					 | 
				
			||||||
                    amt=base_amt
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    amt=-base_amt
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "move_id": move_id,
 | 
					 | 
				
			||||||
                    "description": line.description or desc,
 | 
					 | 
				
			||||||
                    "account_id": line.account_id.id,
 | 
					 | 
				
			||||||
                    "debit": amt>0 and amt or 0,
 | 
					 | 
				
			||||||
                    "credit": amt<0 and -amt or 0,
 | 
					 | 
				
			||||||
                    "track_id": line.track_id.id,
 | 
					 | 
				
			||||||
                    "track2_id": line.track2_id.id,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                print("direct")
 | 
					 | 
				
			||||||
                pprint(line_vals)
 | 
					 | 
				
			||||||
                get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
            elif line.type in ("invoice","refund"):
 | 
					 | 
				
			||||||
                inv=line.invoice_id
 | 
					 | 
				
			||||||
                inv_taxes={}
 | 
					 | 
				
			||||||
                if inv.inv_type in ("invoice","credit","debit"):
 | 
					 | 
				
			||||||
                    line_vals={
 | 
					 | 
				
			||||||
                        "move_id": move_id,
 | 
					 | 
				
			||||||
                        "description": desc,
 | 
					 | 
				
			||||||
                        "account_id": inv.account_id.id,
 | 
					 | 
				
			||||||
                        "due_date": inv.due_date,
 | 
					 | 
				
			||||||
                        "partner_id": inv.partner_id.id,
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    if line.amount_currency>inv.amount_due:
 | 
					 | 
				
			||||||
                        pay_amt=inv.amount_due
 | 
					 | 
				
			||||||
                        over_amt=line.amount_currency-inv.amount_due
 | 
					 | 
				
			||||||
                        total_over+=get_model("currency").convert(over_amt,inv.currency_id.id,settings.currency_id.id,rate=inv.currency_rate)
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        pay_amt=line.amount_currency
 | 
					 | 
				
			||||||
                    pay_ratio=pay_amt/inv.amount_total
 | 
					 | 
				
			||||||
                    inv_amt=inv.amount_total*pay_ratio
 | 
					 | 
				
			||||||
                    cur_inv_amt=get_model("currency").convert(inv_amt,inv.currency_id.id,settings.currency_id.id,rate=inv.currency_rate)
 | 
					 | 
				
			||||||
                    if obj.type=="out":
 | 
					 | 
				
			||||||
                        amt=cur_inv_amt
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        amt=-cur_inv_amt
 | 
					 | 
				
			||||||
                    if amt>0:
 | 
					 | 
				
			||||||
                        line_vals["debit"]=amt
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        line_vals["credit"]=-amt
 | 
					 | 
				
			||||||
                    if inv.account_id.currency_id.id!=settings.currency_id.id:
 | 
					 | 
				
			||||||
                        if obj.type=="out":
 | 
					 | 
				
			||||||
                            line_vals["amount_cur"]=inv_amt
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            line_vals["amount_cur"]=-inv_amt
 | 
					 | 
				
			||||||
                    print("invoice")
 | 
					 | 
				
			||||||
                    pprint(line_vals)
 | 
					 | 
				
			||||||
                    pay_line_id=get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
                    if inv.reconcile_move_line_id:
 | 
					 | 
				
			||||||
                        inv_line_id=inv.reconcile_move_line_id.id
 | 
					 | 
				
			||||||
                    elif inv.move_id: # XXX
 | 
					 | 
				
			||||||
                        inv_line_id=inv.move_id.lines[0].id
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        inv_line_id=None
 | 
					 | 
				
			||||||
                    if inv_line_id:
 | 
					 | 
				
			||||||
                        reconcile_ids.append([pay_line_id,inv_line_id])
 | 
					 | 
				
			||||||
                    for invline in inv.lines:
 | 
					 | 
				
			||||||
                        tax=invline.tax_id
 | 
					 | 
				
			||||||
                        if tax and inv.tax_type!="no_tax": # XXX: simplify this
 | 
					 | 
				
			||||||
                            cur_line_amt_inv=get_model("currency").convert(invline.amount*pay_ratio,inv.currency_id.id,settings.currency_id.id,rate=inv.currency_rate)
 | 
					 | 
				
			||||||
                            base_amt=get_model("account.tax.rate").compute_base(tax.id,cur_line_amt_inv,tax_type=inv.tax_type)
 | 
					 | 
				
			||||||
                            tax_comps=get_model("account.tax.rate").compute_taxes(tax.id,base_amt,when="invoice_payment_inv")
 | 
					 | 
				
			||||||
                            for comp_id,tax_amt in tax_comps.items():
 | 
					 | 
				
			||||||
                                if comp_id in inv_taxes:
 | 
					 | 
				
			||||||
                                    tax_vals=inv_taxes[comp_id]
 | 
					 | 
				
			||||||
                                    tax_vals["amount_base"]+=base_amt
 | 
					 | 
				
			||||||
                                    tax_vals["amount_tax"]+=tax_amt
 | 
					 | 
				
			||||||
                                else:
 | 
					 | 
				
			||||||
                                    tax_vals={
 | 
					 | 
				
			||||||
                                        "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                                        "amount_base": base_amt,
 | 
					 | 
				
			||||||
                                        "amount_tax": tax_amt,
 | 
					 | 
				
			||||||
                                    }
 | 
					 | 
				
			||||||
                                    inv_taxes[comp_id]=tax_vals
 | 
					 | 
				
			||||||
                            cur_line_amt_pmt=get_model("currency").convert(invline.amount*pay_ratio,inv.currency_id.id,settings.currency_id.id,rate=inv.currency_rate) # XXX: check this
 | 
					 | 
				
			||||||
                            base_amt=get_model("account.tax.rate").compute_base(tax.id,cur_line_amt_pmt,tax_type=inv.tax_type)
 | 
					 | 
				
			||||||
                            tax_comps=get_model("account.tax.rate").compute_taxes(tax.id,base_amt,when="invoice_payment_pmt")
 | 
					 | 
				
			||||||
                            for comp_id,tax_amt in tax_comps.items():
 | 
					 | 
				
			||||||
                                if comp_id in inv_taxes:
 | 
					 | 
				
			||||||
                                    tax_vals=inv_taxes[comp_id]
 | 
					 | 
				
			||||||
                                    tax_vals["amount_base"]+=base_amt
 | 
					 | 
				
			||||||
                                    tax_vals["amount_tax"]+=tax_amt
 | 
					 | 
				
			||||||
                                else:
 | 
					 | 
				
			||||||
                                    tax_vals={
 | 
					 | 
				
			||||||
                                        "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                                        "amount_base": base_amt,
 | 
					 | 
				
			||||||
                                        "amount_tax": tax_amt,
 | 
					 | 
				
			||||||
                                    }
 | 
					 | 
				
			||||||
                                    inv_taxes[comp_id]=tax_vals
 | 
					 | 
				
			||||||
                elif inv.inv_type=="overpay":
 | 
					 | 
				
			||||||
                    line_vals={
 | 
					 | 
				
			||||||
                        "move_id": move_id,
 | 
					 | 
				
			||||||
                        "description": desc,
 | 
					 | 
				
			||||||
                        "account_id": inv.account_id.id,
 | 
					 | 
				
			||||||
                        "partner_id": inv.partner_id.id,
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    amt=line.amount
 | 
					 | 
				
			||||||
                    if obj.type=="out":
 | 
					 | 
				
			||||||
                        line_vals["debit"]=amt
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        line_vals["credit"]=amt
 | 
					 | 
				
			||||||
                    print("overpay")
 | 
					 | 
				
			||||||
                    pprint(line_vals)
 | 
					 | 
				
			||||||
                    get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
                elif inv.inv_type=="prepay":
 | 
					 | 
				
			||||||
                    for oline in inv.lines:
 | 
					 | 
				
			||||||
                        line_vals={
 | 
					 | 
				
			||||||
                            "move_id": move_id,
 | 
					 | 
				
			||||||
                            "description": desc,
 | 
					 | 
				
			||||||
                            "account_id": oline.account_id.id,
 | 
					 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                        amt=oline.amount*line.amount/inv.amount_total # XXX: currency
 | 
					 | 
				
			||||||
                        tax=oline.tax_id
 | 
					 | 
				
			||||||
                        if tax:
 | 
					 | 
				
			||||||
                            base_amt=get_model("account.tax.rate").compute_base(tax.id,amt,tax_type=inv.tax_type)
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            base_amt=amt
 | 
					 | 
				
			||||||
                        if obj.type=="out":
 | 
					 | 
				
			||||||
                            line_vals["debit"]=base_amt
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            line_vals["credit"]=base_amt
 | 
					 | 
				
			||||||
                        print("prepay")
 | 
					 | 
				
			||||||
                        pprint(line_vals)
 | 
					 | 
				
			||||||
                        get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
                        if tax and inv.tax_type!="no_tax":
 | 
					 | 
				
			||||||
                            tax_comps=get_model("account.tax.rate").compute_taxes(tax.id,base_amt,when="invoice") # XXX
 | 
					 | 
				
			||||||
                            for comp_id,tax_amt in tax_comps.items():
 | 
					 | 
				
			||||||
                                if comp_id in inv_taxes:
 | 
					 | 
				
			||||||
                                    tax_vals=inv_taxes[comp_id]
 | 
					 | 
				
			||||||
                                    tax_vals["amount_base"]+=base_amt
 | 
					 | 
				
			||||||
                                    tax_vals["amount_tax"]+=tax_amt
 | 
					 | 
				
			||||||
                                else:
 | 
					 | 
				
			||||||
                                    tax_vals={
 | 
					 | 
				
			||||||
                                        "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                                        "amount_base": base_amt,
 | 
					 | 
				
			||||||
                                        "amount_tax": tax_amt,
 | 
					 | 
				
			||||||
                                    }
 | 
					 | 
				
			||||||
                                    inv_taxes[comp_id]=tax_vals
 | 
					 | 
				
			||||||
                for comp_id,inv_tax_vals in inv_taxes.items():
 | 
					 | 
				
			||||||
                    comp=get_model("account.tax.component").browse(comp_id)
 | 
					 | 
				
			||||||
                    if comp.type in ("vat","vat_defer"):
 | 
					 | 
				
			||||||
                        acc_id=comp.account_id.id
 | 
					 | 
				
			||||||
                        if not acc_id:
 | 
					 | 
				
			||||||
                            raise Exception("Missing account for tax component %s"%comp.name)
 | 
					 | 
				
			||||||
                        line_vals={
 | 
					 | 
				
			||||||
                            "move_id": move_id,
 | 
					 | 
				
			||||||
                            "description": desc,
 | 
					 | 
				
			||||||
                            "account_id": acc_id,
 | 
					 | 
				
			||||||
                            "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                            "tax_base": inv_tax_vals["amount_base"],
 | 
					 | 
				
			||||||
                            "partner_id": obj.partner_id.id,
 | 
					 | 
				
			||||||
                            "invoice_id": inv.id,
 | 
					 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                        if comp.type=="vat":
 | 
					 | 
				
			||||||
                            if inv.type=="out":
 | 
					 | 
				
			||||||
                                if line.tax_no:
 | 
					 | 
				
			||||||
                                    tax_no=line.tax_no
 | 
					 | 
				
			||||||
                                else:
 | 
					 | 
				
			||||||
                                    tax_no=get_model("account.invoice").gen_tax_no(context={"date":obj.date})
 | 
					 | 
				
			||||||
                                    line.write({"tax_no":tax_no})
 | 
					 | 
				
			||||||
                                line_vals["tax_no"]=tax_no
 | 
					 | 
				
			||||||
                            elif inv.type=="in":
 | 
					 | 
				
			||||||
                                line_vals["tax_no"]=line.tax_no
 | 
					 | 
				
			||||||
                        amt=inv_tax_vals["amount_tax"]
 | 
					 | 
				
			||||||
                        if obj.type=="in":
 | 
					 | 
				
			||||||
                            amt=-amt
 | 
					 | 
				
			||||||
                        if amt>0:
 | 
					 | 
				
			||||||
                            line_vals["debit"]=amt
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            line_vals["credit"]=-amt
 | 
					 | 
				
			||||||
                        print("tax")
 | 
					 | 
				
			||||||
                        pprint(line_vals)
 | 
					 | 
				
			||||||
                        get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
                    elif comp.type=="wht":
 | 
					 | 
				
			||||||
                        if comp_id in taxes:
 | 
					 | 
				
			||||||
                            tax_vals=taxes[comp_id]
 | 
					 | 
				
			||||||
                            tax_vals["amount_base"]+=inv_tax_vals["amount_base"]
 | 
					 | 
				
			||||||
                            tax_vals["amount_tax"]+=inv_tax_vals["amount_tax"]
 | 
					 | 
				
			||||||
                        else:
 | 
					 | 
				
			||||||
                            taxes[comp_id]=inv_tax_vals.copy()
 | 
					 | 
				
			||||||
            elif line.type=="claim":
 | 
					 | 
				
			||||||
                expense=line.expense_id
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "move_id": move_id,
 | 
					 | 
				
			||||||
                    "description": desc,
 | 
					 | 
				
			||||||
                    "account_id": settings.unpaid_claim_id.id,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                amt=line.amount
 | 
					 | 
				
			||||||
                if obj.type=="out":
 | 
					 | 
				
			||||||
                    line_vals["debit"]=amt
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    line_vals["credit"]=amt
 | 
					 | 
				
			||||||
                print("claim")
 | 
					 | 
				
			||||||
                pprint(line_vals)
 | 
					 | 
				
			||||||
                get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
            elif line.type=="adjust":
 | 
					 | 
				
			||||||
                cur_amt=get_model("currency").convert(line.amount,obj.currency_id.id,settings.currency_id.id,rate=currency_rate)
 | 
					 | 
				
			||||||
                tax_base=get_model("currency").convert(line.tax_base or 0,obj.currency_id.id,settings.currency_id.id,rate=currency_rate)
 | 
					 | 
				
			||||||
                line_vals={
 | 
					 | 
				
			||||||
                    "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,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if obj.type=="in":
 | 
					 | 
				
			||||||
                    cur_amt=-cur_amt
 | 
					 | 
				
			||||||
                if cur_amt>0:
 | 
					 | 
				
			||||||
                    line_vals["debit"]=cur_amt
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    line_vals["credit"]=-cur_amt
 | 
					 | 
				
			||||||
                print("adjust")
 | 
					 | 
				
			||||||
                pprint(line_vals)
 | 
					 | 
				
			||||||
                get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
        if total_over>0.01:
 | 
					 | 
				
			||||||
            partner=obj.partner_id
 | 
					 | 
				
			||||||
            if obj.type=="in":
 | 
					 | 
				
			||||||
                account_id=partner.account_receivable_id.id or settings.account_receivable_id.id
 | 
					 | 
				
			||||||
                if not account_id:
 | 
					 | 
				
			||||||
                    raise Exception("Account receivable not found")
 | 
					 | 
				
			||||||
            elif obj.type=="out":
 | 
					 | 
				
			||||||
                account_id=partner.account_payable_id.id or settings.account_payable_id.id
 | 
					 | 
				
			||||||
                if not account_id:
 | 
					 | 
				
			||||||
                    raise Exception("Account payable not found")
 | 
					 | 
				
			||||||
            line_vals={
 | 
					 | 
				
			||||||
                "move_id": move_id,
 | 
					 | 
				
			||||||
                "description": context.get("overpay_description",""),
 | 
					 | 
				
			||||||
                "account_id": account_id,
 | 
					 | 
				
			||||||
                "track_id": line.track_id.id,
 | 
					 | 
				
			||||||
                "partner_id": obj.partner_id.id,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if obj.type=="out":
 | 
					 | 
				
			||||||
                line_vals["debit"]=total_over
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                line_vals["credit"]=total_over
 | 
					 | 
				
			||||||
            print("overpay")
 | 
					 | 
				
			||||||
            pprint(line_vals)
 | 
					 | 
				
			||||||
            get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
            inv_line_vals={
 | 
					 | 
				
			||||||
                "description": context.get("overpay_description",""),
 | 
					 | 
				
			||||||
                "account_id": account_id,
 | 
					 | 
				
			||||||
                "amount": total_over,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            inv_vals={
 | 
					 | 
				
			||||||
                "type": obj.type=="in" and "out" or "in",
 | 
					 | 
				
			||||||
                "inv_type": "overpay",
 | 
					 | 
				
			||||||
                "partner_id": obj.partner_id.id,
 | 
					 | 
				
			||||||
                "date": obj.date,
 | 
					 | 
				
			||||||
                "tax_type": "no_tax",
 | 
					 | 
				
			||||||
                "lines": [("create",inv_line_vals)],
 | 
					 | 
				
			||||||
                "state": "waiting_payment",
 | 
					 | 
				
			||||||
                "payment_id": obj.id,
 | 
					 | 
				
			||||||
                "account_id": account_id,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            inv_id=get_model("account.invoice").create(inv_vals)
 | 
					 | 
				
			||||||
        wht_no=get_model("account.payment").gen_wht_no(context={"date":obj.date})
 | 
					 | 
				
			||||||
        for comp_id,tax_vals in sorted(taxes.items()):
 | 
					 | 
				
			||||||
            comp=get_model("account.tax.component").browse(comp_id)
 | 
					 | 
				
			||||||
            acc_id=comp.account_id.id
 | 
					 | 
				
			||||||
            if not acc_id:
 | 
					 | 
				
			||||||
                raise Exception("Missing account for tax component %s"%comp.name)
 | 
					 | 
				
			||||||
            line_vals={
 | 
					 | 
				
			||||||
                "move_id": move_id,
 | 
					 | 
				
			||||||
                "description": desc,
 | 
					 | 
				
			||||||
                "account_id": acc_id,
 | 
					 | 
				
			||||||
                "tax_comp_id": comp_id,
 | 
					 | 
				
			||||||
                "tax_base": tax_vals["amount_base"],
 | 
					 | 
				
			||||||
                "partner_id": obj.partner_id.id,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            if comp.type=="vat":
 | 
					 | 
				
			||||||
                if obj.type=="in":
 | 
					 | 
				
			||||||
                    if obj.tax_no:
 | 
					 | 
				
			||||||
                        tax_no=obj.tax_no
 | 
					 | 
				
			||||||
                    else:
 | 
					 | 
				
			||||||
                        tax_no=get_model("account.invoice").gen_tax_no(context={"date":obj.date})
 | 
					 | 
				
			||||||
                        obj.write({"tax_no":tax_no})
 | 
					 | 
				
			||||||
                    line_vals["tax_no"]=tax_no
 | 
					 | 
				
			||||||
                elif obj.type=="out":
 | 
					 | 
				
			||||||
                    line_vals["tax_no"]=obj.tax_no
 | 
					 | 
				
			||||||
            elif comp.type=="wht":
 | 
					 | 
				
			||||||
                if obj.type=="out":
 | 
					 | 
				
			||||||
                    # 1 Payment should have same wht_no
 | 
					 | 
				
			||||||
                    #wht_no=get_model("account.payment").gen_wht_no(context={"date":obj.date})
 | 
					 | 
				
			||||||
                    line_vals["tax_no"]=wht_no
 | 
					 | 
				
			||||||
            amt=tax_vals["amount_tax"]
 | 
					 | 
				
			||||||
            if obj.type=="in":
 | 
					 | 
				
			||||||
                amt=-amt
 | 
					 | 
				
			||||||
            if amt>0:
 | 
					 | 
				
			||||||
                line_vals["debit"]=amt
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                line_vals["credit"]=-amt
 | 
					 | 
				
			||||||
            print("tax")
 | 
					 | 
				
			||||||
            pprint(line_vals)
 | 
					 | 
				
			||||||
            get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
        amt=0
 | 
					 | 
				
			||||||
        move=get_model("account.move").browse(move_id)
 | 
					 | 
				
			||||||
        for line in move.lines:
 | 
					 | 
				
			||||||
            amt+=line.credit-line.debit
 | 
					 | 
				
			||||||
        if amt>0.001:
 | 
					 | 
				
			||||||
            if not settings.currency_loss_id:
 | 
					 | 
				
			||||||
                raise Exception("Missing currency loss account")
 | 
					 | 
				
			||||||
            line_vals={
 | 
					 | 
				
			||||||
                "move_id": move_id,
 | 
					 | 
				
			||||||
                "description": desc,
 | 
					 | 
				
			||||||
                "account_id": settings.currency_loss_id.id,
 | 
					 | 
				
			||||||
                "debit": amt,
 | 
					 | 
				
			||||||
                "credit": 0,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
        elif amt<-0.001:
 | 
					 | 
				
			||||||
            if not settings.currency_gain_id:
 | 
					 | 
				
			||||||
                raise Exception("Missing currency gain account")
 | 
					 | 
				
			||||||
            line_vals={
 | 
					 | 
				
			||||||
                "move_id": move_id,
 | 
					 | 
				
			||||||
                "description": desc,
 | 
					 | 
				
			||||||
                "account_id": settings.currency_loss_id.id,
 | 
					 | 
				
			||||||
                "debit": 0,
 | 
					 | 
				
			||||||
                "credit": -amt,
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            get_model("account.move.line").create(line_vals)
 | 
					 | 
				
			||||||
        get_model("account.move").post([move_id])
 | 
					 | 
				
			||||||
        obj.write({"move_id":move_id,"state":"posted"})
 | 
					 | 
				
			||||||
        for rec_lines in reconcile_ids:
 | 
					 | 
				
			||||||
            get_model("account.move.line").reconcile(rec_lines,context=context)
 | 
					 | 
				
			||||||
        obj.create_prepay_invoice()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def post(self,ids,context={}):
 | 
					 | 
				
			||||||
        obj=self.browse(ids)[0]
 | 
					 | 
				
			||||||
        res=None
 | 
					 | 
				
			||||||
        hdcase_reconcile=context.get('hdcase_reconcile')
 | 
					 | 
				
			||||||
        if hdcase_reconcile and obj.pay_type=="invoice":
 | 
					 | 
				
			||||||
            obj.clinic_post(context=context)
 | 
					 | 
				
			||||||
        elif 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()
 | 
					AccountPayment.register()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,13 +0,0 @@
 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from netforce.model import Model, fields, get_model
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class AccountPaymentLine(Model):
 | 
					 | 
				
			||||||
    _inherit="account.payment.line"
 | 
					 | 
				
			||||||
    _fields={
 | 
					 | 
				
			||||||
        'ar_credit_id': fields.Many2One("account.account","Account Credit"), # no long use
 | 
					 | 
				
			||||||
        "ar_debit_id": fields.Many2One("account.account","Account Debit"), # no long use
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
AccountPaymentLine.register()
 | 
					 | 
				
			||||||
| 
						 | 
					@ -687,14 +687,11 @@ class HDCase(Model):
 | 
				
			||||||
                "company_id": company_id,
 | 
					                "company_id": company_id,
 | 
				
			||||||
                "lines": [],
 | 
					                "lines": [],
 | 
				
			||||||
                "company_id": company_id,
 | 
					                "company_id": company_id,
 | 
				
			||||||
                'hdcase_credit': False,
 | 
					 | 
				
			||||||
                'hdcase_reconcile': True,
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            vals["partner_id"]=partner.id
 | 
					            vals["partner_id"]=partner.id
 | 
				
			||||||
            vals['lines']=rmb_lines
 | 
					            vals['lines']=rmb_lines
 | 
				
			||||||
            if patient_partner:
 | 
					            if patient_partner:
 | 
				
			||||||
                vals['patient_partner_id']=patient_partner.id,
 | 
					                vals['patient_partner_id']=patient_partner.id,
 | 
				
			||||||
            #XXX
 | 
					 | 
				
			||||||
            if obj.branch_id:
 | 
					            if obj.branch_id:
 | 
				
			||||||
                context['branch_id']=obj.branch_id.id
 | 
					                context['branch_id']=obj.branch_id.id
 | 
				
			||||||
            get_model("account.invoice").create(vals,context=context)
 | 
					            get_model("account.invoice").create(vals,context=context)
 | 
				
			||||||
| 
						 | 
					@ -718,13 +715,11 @@ class HDCase(Model):
 | 
				
			||||||
                "lines": [],
 | 
					                "lines": [],
 | 
				
			||||||
                "company_id": company_id,
 | 
					                "company_id": company_id,
 | 
				
			||||||
                'partner_id':partner.id,
 | 
					                'partner_id':partner.id,
 | 
				
			||||||
                'hdcase_credit': True,
 | 
					 | 
				
			||||||
                'hdcase_reconcile': True,
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            vals['lines']=normb_lines
 | 
					            vals['lines']=normb_lines
 | 
				
			||||||
            if patient_partner:
 | 
					            if patient_partner:
 | 
				
			||||||
                vals['patient_partner_id']=patient_partner.id,
 | 
					                vals['patient_partner_id']=patient_partner.id,
 | 
				
			||||||
            get_model("account.invoice").create(vals,context) # create alway
 | 
					            get_model("account.invoice").create(vals,context)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        obj.make_pickings()
 | 
					        obj.make_pickings()
 | 
				
			||||||
        # prevent douplicate create invoice & picking 
 | 
					        # prevent douplicate create invoice & picking 
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -333,7 +333,6 @@ class Shop(Model):
 | 
				
			||||||
            "currency_id": currency_id,
 | 
					            "currency_id": currency_id,
 | 
				
			||||||
            "company_id": company_id,
 | 
					            "company_id": company_id,
 | 
				
			||||||
            'partner_id': partner.id,
 | 
					            'partner_id': partner.id,
 | 
				
			||||||
            'hdcase_reconcile': True,
 | 
					 | 
				
			||||||
            "lines": [],
 | 
					            "lines": [],
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        track_id=obj.branch_id.track_id.id
 | 
					        track_id=obj.branch_id.track_id.id
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue