133 lines
4.6 KiB
Python
133 lines
4.6 KiB
Python
import time
|
|
from calendar import monthrange
|
|
|
|
from netforce.model import Model, fields , get_model
|
|
from netforce.database import get_connection
|
|
|
|
class CreateInvoicePayment(Model):
|
|
_name="create.invoice.payment"
|
|
_transient=True
|
|
|
|
_fields={
|
|
"date": fields.Date("Month", required=True),
|
|
'date_from': fields.Date("From",required=True),
|
|
'date_to': fields.Date("To",required=True),
|
|
'partner_id': fields.Many2One("partner","Contact",required=True),
|
|
'invoices': fields.One2Many("account.invoice","create_invoice_id","Invoices"),
|
|
'state': fields.Selection([["find_invoice","Find Invoice"],["ready_payment","Ready Payment"]],'State'),
|
|
'account_id': fields.Many2One("account.account","Account"),
|
|
}
|
|
|
|
def _get_date_from(self,context={}):
|
|
year,month=time.strftime("%Y-%m").split("-")
|
|
return '%s-%s-01'%(year,month)
|
|
|
|
def _get_date_to(self,context={}):
|
|
year,month,day=time.strftime("%Y-%m-%d").split("-")
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
return "%s-%s-%s"%(year,month,total_day)
|
|
|
|
_defaults={
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
|
'date_from': _get_date_from,
|
|
'date_to': _get_date_to,
|
|
'state': 'find_invoice',
|
|
}
|
|
|
|
def onchange_date(self,context={}):
|
|
data=context['data']
|
|
date=data['date']
|
|
year,month,day=date.split("-")
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
data['date_from']="%s-%s-01"%(year,month)
|
|
data['date_to']="%s-%s-%s"%(year,month,total_day)
|
|
return data
|
|
|
|
def back_step(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
state='find_invoice'
|
|
if obj.state=='ready_payment':
|
|
db=get_connection()
|
|
res=db.query("""
|
|
select id from account_invoice where create_invoice_id=%s
|
|
""",obj.id)
|
|
if res:
|
|
inv_ids=[r['id'] for r in res]
|
|
db.execute("update account_invoice set create_invoice_id=null where id in %s",tuple(inv_ids))
|
|
|
|
obj.write({
|
|
'state': state,
|
|
})
|
|
|
|
def find_invoice(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
obj.write({
|
|
'state': 'ready_payment',
|
|
})
|
|
db=get_connection()
|
|
res=db.query("""
|
|
select id from account_invoice where create_invoice_id=%s
|
|
""",obj.id)
|
|
if res:
|
|
inv_ids=[r['id'] for r in res]
|
|
db.execute("update account_invoice set create_invoice_id=null where id in %s",tuple(inv_ids))
|
|
|
|
res=db.query("""
|
|
select id from account_invoice where date>=%s and date <=%s and state='waiting_payment'
|
|
and partner_id=%s
|
|
""",obj.date_from, obj.date_to,obj.partner_id.id)
|
|
if res:
|
|
inv_ids=[r['id'] for r in res]
|
|
db.execute("update account_invoice set create_invoice_id=%s where id in %s",obj.id,tuple(inv_ids))
|
|
|
|
|
|
def create_payment(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
if not obj.account_id:
|
|
raise Exception("Missing account")
|
|
if not obj.invoices:
|
|
raise Exception("Nothing to pay!")
|
|
partner=obj.partner_id
|
|
type="in"
|
|
vals={
|
|
'type': type, # receivable
|
|
'account_id': obj.account_id.id,
|
|
'partner_id': partner.id,
|
|
'pay_type': 'invoice',
|
|
'invoice_lines': [],
|
|
}
|
|
datenow=time.strftime("%Y-%m-%d")
|
|
rate_type="sell" # receivable
|
|
settings=get_model("settings").browse(1)
|
|
currency=settings.currency_id
|
|
if not currency:
|
|
raise Exception("Missing setting.currency")
|
|
#FIXME process only selected invoice
|
|
for inv in obj.invoices:
|
|
vals['invoice_lines'].append(('create',{
|
|
'invoice_id': inv.id,
|
|
"amount": get_model("currency").convert(inv.amount_due,inv.currency_id.id,currency.id,date=datenow,rate_type=rate_type),
|
|
}))
|
|
ctx={
|
|
'type': type,
|
|
}
|
|
payment_id=get_model("account.payment").create(vals,context=ctx)
|
|
return {
|
|
'next': {
|
|
'name': 'payment',
|
|
'mode': 'form',
|
|
'active_id': payment_id,
|
|
},
|
|
'flash': 'Create Payment Successfull',
|
|
}
|
|
|
|
def do_next(self,ids,context={}):
|
|
obj=self.browse(ids)[0]
|
|
if obj.state=='find_invoice':
|
|
obj.find_invoice()
|
|
elif obj.state=='ready_payment':
|
|
res=obj.create_payment()
|
|
return res
|
|
|
|
CreateInvoicePayment.register()
|