
    ,{j$                     f    d dl mZmZmZmZ d dlmZmZ g dZg dZ	 G d dej                        Zy)    )_apifieldsmodels)	UserErrorValidationError))
exhibition
Exhibition)supplier_visitzSupplier Visit)customer_visitzCustomer Visit)demoDemo)serviceService)othersOthers))draftDraft)confirmz
Pending HR)acknowledgedAcknowledged)rejectedRejectedc                       e Zd ZdZdZdZdgZdZ ej                  dd      Z
 ej                  d	ddd
       Z ej                  dddd      Z ej                  ddd      Z ej                  ddd      Z ej"                  d      Z ej&                  eddd      Z ej                  dd      Z ej                         Z ej                         Z ej                  dd      Z ej4                         Z ej4                  d      Z ej4                         Z ej&                  edddd      Z ej                  d      Z  ej                  d dd!      Z! ejD                  dd!      Z# ejH                  d"      Z% ejH                  d"      Z& e'jP                  d#       e'jR                  d$d%      d&               Z* e'jR                  d$d'd(      d)        Z+ e'jR                  d'd(      d*        Z, e'jZ                  d'd(      d+        Z. e'jZ                  d,d-      d.        Z/d/ Z0e'jb                   fd0       Z2h d1Z3h d2Z4 fd3Z5 fd4Z6d5 Z7d6 Z8d9d7Z9d8 Z: xZ;S ):HrBusinessTripa.  Employee request for HR to prepare/authorize a business trip. Once HR
    Acknowledges it, the employee is exempt from check-in/out for the whole
    date_from..date_to range and it shows as "BT" on the Monthly Attendance
    report (attendance_report.py threads it through the same way as
    xeno.attendance.dayoff) and as a "Business Trip . BT" row on the
    Employee Attendance Report / daily attendance email (xeno_leave's
    xeno_get_attendance_report and xeno_hr_dashboard's
    get_leave_attendance_rows -- both modules already depend on this one).zhr.business.tripzBusiness Trip Requestzmail.threadzdate_from desc, id desc_compute_nameT)computestorezhr.employeec                 V    | j                   j                  j                  j                  S )N)envuseremployee_ididselfs    *xeno_attendance/models/hr_business_trip.py<lambda>zHrBusinessTrip.<lambda>'   s    TXX]]6699     )requiredtrackingdefaultzemployee_id.xeno_employee_codezbHRSystem employee_code -- the join key the Monthly Attendance report matches this request against.)relatedr   indexhelpz
Start Date)stringr)   r*   zEnd Date_compute_number_of_days)r   Typez
Other TypezPRequired when Type is "Others" -- shown on the report tooltip and requests list.)r/   r.   zres.countryCountry)r/   Requirementsr   F)r+   r)   r*   copy)r4   z	res.users)readonlyr4   _compute_can_flagsuidr"   statec                     | j                         }| D ]R  }| j                  j                  j                  |j                  k(  }|xs |xr |j                  dk(  |_        ||_        T y )Nr   )_xeno_is_hrr    r!   r"   r8   can_submit_requestcan_hr_decide)r%   is_hrrecis_owners       r&   r6   z!HrBusinessTrip._compute_can_flagsF   s`       " 	&Cxx}}00COOCH&+&7x%QSYY'=QC" %C	&r(   	date_fromdate_toc                     | D ]J  }t        d|j                  j                  xs d|j                  xs d|j                  xs d      |_        L y )Nz9%(employee)s - Business Trip %(date_from)s to %(date_to)s )employeer@   rA   )r   r"   namer@   rA   r%   r>   s     r&   r   zHrBusinessTrip._compute_nameO   sL     	JCK--3---2s{{7HbJCH	Jr(   c                     | D ]`  }|j                   rK|j                  r?|j                  |j                   k\  r&|j                  |j                   z
  j                  dz   nd|_        b y )N   r   )r@   rA   daysnumber_of_daysrF   s     r&   r0   z&HrBusinessTrip._compute_number_of_daysW   sY     	ZC ==S[[S[[CMM5Q s}},22Q6WX 	Zr(   c                     | D ]I  }|j                   s|j                  s|j                  |j                   k  s7t        t        d             y )Nz(End Date must be on or after Start Date.)r@   rA   r   r   rF   s     r&   _check_dateszHrBusinessTrip._check_dates^   s@     	UC}}s}}1L%a(R&STT	Ur(   	trip_typetrip_type_otherc                     | D ]D  }|j                   dk(  s|j                  xs dj                         r2t        t	        d             y )Nr   rC   z2Please specify the type when "Others" is selected.)rM   rN   stripr   r   rF   s     r&   _check_other_typez HrBusinessTrip._check_other_typed   sF     	_C}}(#2E2E2K1R1R1T%a(\&]^^	_r(   c                     | j                   j                         xs% | j                   j                  j                  d      S )Nzhr.group_hr_user)r    is_superuserr!   	has_groupr$   s    r&   r:   zHrBusinessTrip._xeno_is_hrj   s.    xx$$&U$((--*A*ABT*UUr(   c                 .   | j                   j                  xs | j                         }| j                   j                  j                  j
                  }|s2|D ]-  }|j                  d|      }||k7  st        t        d             t        | )  |      S )Nr"   zhYou can only create a Business Trip request for yourself. Ask HR to submit one on your behalf if needed.)r    sur:   r!   r"   r#   getr   r   supercreate)r%   	vals_listr=   own_employee_idvalstarget_employee_id	__class__s         r&   rY   zHrBusinessTrip.createo   s     1t//1((--3366! K%)XXm_%M"%8#AI%J K KK w~i((r(   >
   cityreasonremarkaddressrA   r@   rM   
country_idrN   requirements_description>   r8   hr_decided_athr_decided_byrejection_reasonc                    | j                   j                  s| j                         st        |      }|| j                  z  rt        t        d            d|v rt        t        d            | D ]v  }|| j                  z  s|j                  dk7  rt        t        d            |j                   j                  j                  |j                  k7  sdt        t        d             t        | 1  |      S )NzhUse Submit/Acknowledge/Reject to change a Business Trip request's status -- it can't be edited directly.r"   z_The employee on a Business Trip request can't be changed. Ask HR if this needs to be corrected.r   zSThis request can no longer be edited once it's been submitted. Ask HR to update it.z3Only the requesting employee can edit this request.)r    rV   r:   set_XENO_TRANSITION_FIELDSr   r   _XENO_REQUEST_FIELDSr8   r!   r"   rX   write)r%   r\   touchedr>   r^   s       r&   rl   zHrBusinessTrip.write   s    xx{{4#3#3#5$iG 555G!H I I '<!= > >  bT666yyG+'>)? @ @ ww||//3??B'*_(`aab w}T""r(   c                    | j                   j                  sg| j                         sW| D ]R  }|j                  dk7  s.|j                   j                  j
                  |j
                  k7  s@t        t        d             t        | %         S )Nr   zIOnly your own still-Draft request can be deleted; once submitted, ask HR.)
r    rV   r:   r8   r!   r"   r   r   rX   unlink)r%   r>   r^   s     r&   ro   zHrBusinessTrip.unlink   su    xx{{4#3#3#5 499'377<<+C+Cs+V#A2%3 4 44
 w~r(   c                    | D ]  }|j                   dk7  rt        t        d            |j                  st        t        d            |j	                         j                  ddi       |j                  t        d              y )Nr   z&Only a Draft request can be submitted.z5Only the requesting employee can submit this request.r8   r   z Submitted for HR acknowledgment.body)r8   r   r   r;   sudorl   message_postrF   s     r&   action_submitzHrBusinessTrip.action_submit   sz     	ICyyG#"J KLL))"Y Z[[HHJgy12!$F"GH	Ir(   c                    | D ]  }|j                   dk7  rt        t        d            |j                         st        t        d            |j	                         j                  d| j                  j                  j                  t        j                  j                         d       |j                  t        d| j                  j                  j                                y )Nr   z+Only a Pending request can be acknowledged.z%Only HR can acknowledge this request.r   )r8   rf   re   zAcknowledged by HR: %srq   )r8   r   r   r:   rs   rl   r    r!   r#   r   Datetimenowrt   rE   rF   s     r&   action_acknowledgez!HrBusinessTrip.action_acknowledge   s     
	SCyyI%"O PQQ??$"I JKKHHJ'!%!1!1!'!4!4!6 
 !$<dhhmm>P>P"QR
	Sr(   c                 `   | D ]  }|j                   dk7  rt        t        d            |j                         st        t        d            |j	                         j                  d|xs dd       |j                  t        d| j                  j                  j                                y )	Nr   z'Only a Pending request can be rejected.z Only HR can reject this request.r   Fr8   rg   zRejected by HR: %srq   )
r8   r   r   r:   rs   rl   rt   r    r!   rE   )r%   r`   r>   s      r&   action_rejectzHrBusinessTrip.action_reject   s     	OCyyI%"K LMM??$"D EFFHHJzvQVWX!$8$((--:L:L"MN	Or(   c                 t   | D ]  }|j                   dk7  rt        t        d            |j                         sA|j                  j
                  j                  |j                  k(  st        t        d            |j                         j                  ddd       |j                  t        d              y )	Nr   z.Only a Rejected request can be reset to Draft.z4Only the requesting employee can reset this request.r   Fr{   zReset to Draft.rq   )
r8   r   r   r:   r    r!   r"   rs   rl   rt   rF   s     r&   action_reset_to_draftz$HrBusinessTrip.action_reset_to_draft   s     	8CyyJ&"R STTOO%)A)AS__)T"X YZZHHJwEJK!$5"67	8r(   )F)<__name__
__module____qualname____doc___name_description_inherit_orderr   CharrE   Many2oner"   employee_codeDater@   rA   IntegerrJ   	Selection
TRIP_TYPESrM   rN   rb   r_   rc   Textr`   rd   ra   STATESr8   rg   rf   rw   re   Booleanr;   r<   r   depends_contextdependsr6   r   r0   
constrainsrL   rQ   r:   model_create_multirY   rk   rj   rl   ro   ru   ry   r|   r~   __classcell__)r^   s   @r&   r   r      s   N E*LH&F6;;d;D!&//t9;K  FKK0D45M
 <$NIfkkdTJG#V^^,EFN   FTTXYI!fkk_aO fkkmG6;;=D yAJV[[]F*v{{.AV[[]FFVWtdY^_E"v{{.#FOOK$UKM#FOOT>M'0DE"FNN+?@MSS[[(& )  & S[[Y7J 8J S[[i(Z )Z S^^K+U ,U
 S^^K!23_ 4_
V
 	) ) ^#. ISO8r(   r   N)odoor   r   r   r   odoo.exceptionsr   r   r   r   Modelr    r(   r&   <module>r      s.    ' ' 6

w8V\\ w8r(   