70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
import time
 | 
						|
from calendar import monthrange
 | 
						|
 | 
						|
from netforce.model import Model,fields,get_model
 | 
						|
 | 
						|
 | 
						|
class ComputeLaborCost(Model):
 | 
						|
    _name="clinic.compute.labor.cost"
 | 
						|
    _string="Compute Labor Cost"
 | 
						|
    _transient=True
 | 
						|
    
 | 
						|
    _fields={
 | 
						|
        "date": fields.Date("Month"),
 | 
						|
        "date_from": fields.Date("From", required=True),
 | 
						|
        "date_to": fields.Date("To", required=True),
 | 
						|
        'branch_id': fields.Many2One("clinic.branch","Branch"),
 | 
						|
        'department_id': fields.Many2One("clinic.department","Department"),
 | 
						|
    }
 | 
						|
 | 
						|
    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,
 | 
						|
    }
 | 
						|
    
 | 
						|
    def compute(self,ids,context={}):
 | 
						|
        obj=self.browse(ids)[0]
 | 
						|
        dom=[]
 | 
						|
        dom.append(['date','>=', obj.date_from])
 | 
						|
        dom.append(['date','<=', obj.date_to])
 | 
						|
        if obj.branch_id:
 | 
						|
            dom.append(['branch_id','=',obj.branch_id.id])
 | 
						|
        if obj.department_id:
 | 
						|
            dom.append(['department_id','=',obj.department_id.id])
 | 
						|
        for lcost in get_model("clinic.labor.cost").search_browse(dom):
 | 
						|
            print("compute %s ....", lcost.cycle_item_id.name)
 | 
						|
            #item=lcost.cycle_item_id
 | 
						|
            #item.to_draft()
 | 
						|
            #item.validate()
 | 
						|
            lcost.compute()
 | 
						|
        print("Done!")
 | 
						|
        return {
 | 
						|
            'next': {
 | 
						|
                'name': 'clinic_compute_labor_cost',
 | 
						|
                'mode': 'form',
 | 
						|
                'active_id': obj.id,
 | 
						|
            },
 | 
						|
            'flash': 'Compute Successfully',
 | 
						|
        }
 | 
						|
 | 
						|
    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
 | 
						|
 | 
						|
ComputeLaborCost.register()
 |