from odoo import models, fields, api from odoo.exceptions import UserError class ClinicPrescription(models.Model): _name = 'clinic.prescription' _description = 'Đơn thuốc' _rec_name = 'patient_id' patient_id = fields.Many2one('clinic.patient', string='Bệnh nhân', required=True) doctor_id = fields.Many2one('res.partner', string='Bác sĩ', required=True) date = fields.Date(string='Ngày kê đơn', default=fields.Date.today) prescription_line_ids = fields.One2many('clinic.prescription.line', 'prescription_id', string='Chi tiết đơn thuốc') # khi kê đơn thì trừ số lượng thuốc trong kho def action_prescribe(self): for line in self.prescription_line_ids: if line.quantity <= 0: raise UserError(f"Số lượng thuốc {line.medicine_id.name} phải lớn hơn 0!") else: if line.medicine_id.stock < line.quantity: raise UserError(f"Số lượng thuốc {line.medicine_id.name} không đủ trong kho!") else: line.medicine_id.stock -= line.quantity