Openerp/module 4

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา

สร้างโมเดล Department และเชื่อมโยงกับ Appointment เพื่อค้นหาและสร้างรายงาน

histest.py

class HISAppointment(osv.Model):
    _name = 'histest.appointment'
    _columns = {
        'date': fields.date(string=u'วันที่',
                            required=True),
        'patient_id': fields.many2one('histest.patient',
                                      'Patient'),
        'department_id': fields.many2one('histest.department',
                                         'Department'),
    }

class HISDepartment(osv.Model):
    _name = 'histest.department'
    _columns = {
        'name': fields.char(size=200,
                            string=u'แผนก',
                            required=True),
        'appointment_ids': fields.one2many('histest.appointment',
                                           'department_id',
                                           'Appointments'),
    }

class HISDepartmentSearch(osv.TransientModel):
    _name = 'histest.departmentsearch'

    def appointment_search(self, cr, uid, ids, field_name=None,
                           arg=None, context=None):

        appointment_obj = self.pool.get('histest.appointment')

        dsearches = self.browse(cr, uid, ids)
        result = {}
        for dsearch in dsearches:
            result[dsearch.id] = (
                appointment_obj.search(cr, uid,
                                       [('department_id','=',dsearch.department_id.id),
                                        ('date','=',dsearch.search_date)],
                                       context=context))
        return result

    _columns = {
        'department_id': fields.many2one('histest.department',
                                         'Department'),
        'search_date': fields.date(string=u'วันที่ต้องการหา'),

        'appointment_ids': fields.function(appointment_search,
                                           type='one2many',
                                           relation='histest.appointment',
                                           string='Appointment by date'),
    }

histest_view.xml

ส่วน department form:

    <record model="ir.ui.view" id="view_histest_department_form">
      <field name="name">histest.department.form</field>
      <field name="model">histest.department</field>
      <field name="arch" type="xml">
        <form string="Patient" version="7.0">
          <group>
            <field name="name"/>
            <field name="appointment_ids"/>
          </group>
        </form>
      </field>
    </record>

    <record model="ir.actions.act_window" id="action_histest_department">
      <field name="name">Department</field>
      <field name="res_model">histest.department</field>
      <field name="view_type">form</field>
      <field name="view_mode">form,tree</field>
    </record>

    <menuitem name="แผนก" id="menu_histest_department"
              parent="menu_histest_main" sequence="4"
              action="histest.action_histest_department"/>

ส่วน department search form:

    <record model="ir.ui.view" id="view_histest_departmentsearch_form">
      <field name="name">histest.departmentsearch.form</field>
      <field name="model">histest.departmentsearch</field>
      <field name="arch" type="xml">
        <form string="Patient" version="7.0">
          <group>
            <field name="department_id"/>
            <field name="search_date"/>
            <field name="appointment_ids"/>
          </group>
        </form>
      </field>
    </record>

    <record model="ir.actions.act_window" id="action_histest_departmentsearch">
      <field name="name">Department Appointment Search</field>
      <field name="res_model">histest.departmentsearch</field>
      <field name="view_type">form</field>
      <field name="view_mode">form,tree</field>
    </record>

    <menuitem name="แผนก search" id="menu_histest_departmentsearch"
              parent="menu_histest_main" sequence="5"
              action="histest.action_histest_departmentsearch"/>

appointment tree view:

    <record model="ir.ui.view" id="view_histest_appointment_tree">
      <field name="name">histest.appointment.tree</field>
      <field name="model">histest.appointment</field>
      <field name="arch" type="xml">
        <tree string="Message">
          <field name="patient_id"/>
          <field name="date"/>
        </tree>
      </field>
    </record>

แฟ้มเกี่ยวกับรายงาน

เพิ่มรายละเอียดใน histest_report.xml

    <report
      auto="True"
      id="report_histest_departmentsearch"
      model="histest.departmentsearch"
      name="histest.departmentsearch"
      rml="histest/report/histest_departmentsearch.rml"
      string="Print Appointments"
      header="False"
      menu="True"
      />

สร้าง histest_departmentsearch.rml โดยอาจจะสร้างจากการแก้แฟ้ม odt ด้วย LibreOffice ก็ได้ ให้สุดท้ายใน rml มีข้อมูลประมาณด้านล่างนี้:

    <section>
      <para style="P1">[[ repeatIn(objects, 'departmentsearch') ]]</para>
      <para style="P1">Department: [[ departmentsearch.department_id.name ]]</para>
      <para style="P1">Date: [[ departmentsearch.search_date ]]</para>
      <para style="P1">
        <font color="white"> </font>
      </para>
      <para style="P1">Appointment Dates:</para>
      <para>Patients: [[ len(departmentsearch.appointment_ids) ]]</para>
      <section>
        <para style="P1">[[ repeatIn(departmentsearch.appointment_ids, 'appointment') ]]</para>
        <para style="P1">- [[ appointment.patient_id.full_name ]]</para>
      </section>
      <para style="P1">
        <font color="white"> </font>
      </para>
    </section>

กระบวนการทำงาน

Openerp-histest-workflow.png

เพิ่ม columns ชื่อ state ใน HISAppointment

class HISAppointment(osv.Model):
    _name = 'histest.appointment'
    _columns = {
        'date': fields.date(string=u'วันที่',
                            required=True),
        'patient_id': fields.many2one('histest.patient',
                                      'Patient'),
        'department_id': fields.many2one('histest.department',
                                         'Department'),
        'state': fields.selection([('new', 'New Appointment'),
                                   ('arrived', 'Arrived'),
                                   ('examining', 'Examining'),
                                   ('wait_medicine','Awaiting Medicine'),
                                   ('done','Done'),
                                   ('cancel','Canceled')],
                                  'Status', readonly=False),
    }

histest_workflow.xml

ระบบ workflow ในแฟ้ม histest_workflow.xml โดยระบุสามส่วน

  • workflow model
  • activities
  • transitions

ดังด้านล่าง

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>

    <!-- Workflow model -->

    <record model="workflow" id="wkf_appointment">
      <field name="name">histest.appointment.workflow</field>
      <field name="osv">histest.appointment</field>
      <field name="on_create">True</field>
    </record>
    
    <!-- Activities -->

    <record model="workflow.activity" id="act_new">
      <field name="wkf_id" ref="wkf_appointment"/>
      <field name="name">new</field>
      <field name="flow_start">True</field>
      <field name="kind">function</field>
      <field name="action">write({ 'state': 'new' })</field>
    </record>

    <record model="workflow.activity" id="act_arrived">
      <field name="wkf_id" ref="wkf_appointment"/>
      <field name="name">arrived</field>
      <field name="kind">function</field>
      <field name="action">write({ 'state': 'arrived' })</field>
    </record>

    <record model="workflow.activity" id="act_done">
      <field name="wkf_id" ref="wkf_appointment"/>
      <field name="name">done</field>
      <field name="flow_stop">True</field>
      <field name="kind">function</field>
      <field name="action">write({ 'state': 'done' })</field>
    </record>

    <!-- Transitions -->

    <record model="workflow.transition" id="new_arrived">
      <field name="act_from" ref="act_new"/>
      <field name="act_to" ref="act_arrived"/>
      <field name="signal">arrive</field>
    </record>
    
    <record model="workflow.transition" id="arrived_done">
      <field name="act_from" ref="act_arrived"/>
      <field name="act_to" ref="act_done"/>
      <field name="signal">done</field>
    </record>

  </data>
</openerp>

เพิ่มรายการไฟล์ใน __openerp__.py

    'data': [
        'histest_view.xml',
        'histest_report.xml',
        'histest_workflow.xml',
    ],

เพิ่ม form view เพื่อให้ส่ง signal ไปที่ workflow

    <record model="ir.ui.view" id="view_histest_appointment_form">
      <field name="name">histest.appointment.form</field>
      <field name="model">histest.appointment</field>
      <field name="arch" type="xml">
        <form string="Patient" version="7.0">
          <group>
            <field name="patient_id"/>
            <field name="date"/>
            <field name="department_id"/>
            <field name="state"/>
            <button name="arrive" string="Patient Arrived" states="new"/>
            <button name="done" string="Done" states="arrived"/>
          </group>
        </form>
      </field>
    </record>

เอกสารครั้งก่อน ๆ