clinic/netforce_clinic/migrations/repost_invoice.py

62 lines
2.1 KiB
Python

from netforce.model import get_model
from netforce import migration
from netforce.access import set_active_user, set_active_company
class Migration(migration.Migration):
_name="clinic.repos.invoice"
_version="2.10.0"
def migrate(self):
set_active_user(1)
set_active_company(1)
dom=[
#['department_id','=',2],
#['date','>=','2015-08-18'],
#['date','<=','2015-08-18'],
]
hdcase_ids=set()
for hdcase in get_model('clinic.hd.case').search_browse(dom):
reset=False
for inv in hdcase.invoices:
if inv.state=='paid':
inv.write({
'state': 'waiting_payment',
})
for pm_line in get_model("account.payment.line").search_browse([['invoice_id','=',inv.id]]):
pm=pm_line.payment_id
pm.to_draft()
pm.delete()
inv.to_draft()
inv.delete()
elif inv.state=='waiting_payment':
inv.to_draft()
inv.delete()
reset=True
hdcase_ids.update({hdcase.id})
if reset:
for line in hdcase.lines:
line.write({
'state': 'draft',
})
ids=list(hdcase_ids)
for seq in get_model("sequence").search_browse([['type','in',['cust_invoice','clinic_invoice_noclaim']]]):
for run in seq.running:
run.delete()
dom2=[
['date','>=','2015-07-01'],
['state','=','waiting_payment'],
]
#for hdcase in get_model('clinic.hd.case').browse(ids):
for hdcase in get_model('clinic.hd.case').search_browse(dom2):
print('remake_invoice ---> ', hdcase.number)
ctx={
'is_migrate': True,
}
hdcase.make_invoices(context=ctx)
hdcase.post_invoices()
print("Done!")
return True
Migration.register()