ผลต่างระหว่างรุ่นของ "Openerp/histest3"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(Openerp/histest ถูกเปลี่ยนชื่อเป็น Openerp/histest3)
 
(ไม่แสดง 21 รุ่นระหว่างกลางโดยผู้ใช้ 2 คน)
แถว 1: แถว 1:
 
หน้านี้รวมข้อมูลสำหรับการทดสอบการพัฒนาระบบด้วย OpenERP
 
หน้านี้รวมข้อมูลสำหรับการทดสอบการพัฒนาระบบด้วย OpenERP
== การแยกโค้ด javascript เป็นส่วน ==
+
== การสร้างรายงาน ==
  
'''ก่อนเริ่ม:''' ดาวน์โหลดโค้ด [http://theory.cpe.ku.ac.th/~jittat/openerp/histest.zip histest.zip] จากครั้งก่อน
+
* สร้างไดเรคตอรี่ชื่อ <code>report</code> และสร้างไฟล์ <code>__init__.py</code> ไว้ภายในนั้น
 +
* ดาวน์โหลด [[File:histest_patient.odt]] เพื่อใช้เตรียมเทมเพลทสำหรับรายงาน เซฟไฟล์นี้ไว้ในไดเรคตอรี <code>report</code>
 +
* ใช้คำสั่ง <code>openerp_sxw2rml.py</code> แปลงไฟล์ของ OpenOffice ให้เป็นไฟล์ RML (Report Markup Language) เพื่อนำไปใช้กับระบบสร้างรายงานของ OpenERP ต่อไป (หมายเหตุ: <code><openerp-path></code> หมายถึงพาธที่ติดตั้ง OpenERP)
  
=== histest.js ===
+
python <openerp-path>/openerp/addons/base_report_designer/openerp_sxw2rml/openerp_sxw2rml.py histest_patient.odt > histest_patient.rml
  
<syntaxhighlight lang="javascript">
+
=== ไฟล์ <code>histest_patient.rml</code> ===
openerp.histest = function(instance) {
+
อย่างไรก็ตาม หากไม่ต้องการใช้ OpenOffice/LibreOffice ในการสร้างเทมเพลท เราสามารถสร้างไฟล์ RML ขึ้นมาได้ด้วยตัวเองดังนี้
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0"?>
 +
<document filename="test.pdf">
 +
  <template pageSize="(595.0,842.0)" title="Test" author="Martin Simon" allowSplitting="20">
 +
    <pageTemplate id="first">
 +
      <frame id="first" x1="57.0" y1="57.0" width="481" height="728"/>
 +
    </pageTemplate>
 +
  </template>
 +
  <stylesheet>
 +
    <blockTableStyle id="Standard_Outline">
 +
      <blockAlignment value="LEFT"/>
 +
      <blockValign value="TOP"/>
 +
    </blockTableStyle>
 +
    <initialize>
 +
      <paraStyle name="all" alignment="justify"/>
 +
    </initialize>
 +
    <paraStyle name="P1" fontName="Helvetica"/>
 +
    <paraStyle name="P2" fontName="Helvetica"/>
 +
    <paraStyle name="Standard" fontName="Helvetica"/>
 +
    <paraStyle name="Heading" fontName="Helvetica" fontSize="12.0" leading="15" spaceBefore="12.0" spaceAfter="6.0"/>
 +
    <paraStyle name="Text_20_body" fontName="Helvetica" spaceBefore="0.0" spaceAfter="6.0"/>
 +
    <paraStyle name="List" fontName="Helvetica" spaceBefore="0.0" spaceAfter="6.0"/>
 +
    <paraStyle name="Caption" fontName="Helvetica-Oblique" fontSize="10.0" leading="13" spaceBefore="6.0" spaceAfter="6.0"/>
 +
    <paraStyle name="Index" fontName="Helvetica"/>
 +
    <paraStyle name="Heading_20_1" fontName="Helvetica-Bold" fontSize="115%" leading="NaN" spaceBefore="12.0" spaceAfter="6.0"/>
 +
    <paraStyle name="Table_20_Contents" fontName="Helvetica"/>
 +
    <images/>
 +
  </stylesheet>
 +
  <story>
 +
    <para style="P2">Patient Appointment Information</para>
 +
    <para style="P1">
 +
      <font color="white"> </font>
 +
    </para>
 +
    <section>
 +
      <para style="P1">[[ repeatIn(objects, 'patient') ]]</para>
 +
      <para style="P1">Patient ID: [[ patient.id_number ]]</para>
 +
      <para style="P1">Patient Name: [[ patient.full_name ]]</para>
 +
      <para style="P1">
 +
        <font color="white"> </font>
 +
      </para>
 +
      <para style="P1">Appointment Dates:</para>
 +
      <section>
 +
        <para style="P1">[[ repeatIn(patient.appointment_ids, 'appointment') ]]</para>
 +
        <para style="P1">- [[ appointment.date ]]</para>
 +
      </section>
 +
      <para style="P1">
 +
        <font color="white"> </font>
 +
      </para>
 +
    </section>
 +
    <para style="P1">
 +
      <font color="white"> </font>
 +
    </para>
 +
    <para style="P1">
 +
      <font color="white"> </font>
 +
    </para>
 +
  </story>
 +
</document>
 +
</syntaxhighlight>
  
     openerp.histest.message(instance);
+
=== ไฟล์ <code>histest.py</code> ===
     openerp.histest.patient(instance);
+
เพิ่มโมเดล HISAppointment และแก้ไขโมเดล HISPatient ให้เชื่อมโยงถึงกัน
 +
 
 +
<syntaxhighlight lang="python">
 +
from osv import osv, fields
 +
 
 +
class HISPatient(osv.Model):
 +
     _name='histest.patient'
 +
 
 +
    def get_full_name(self, cr, uid, ids, field_name, arg, context):
 +
        patients = self.browse(cr, uid, ids)
 +
        result = {}
 +
        for p in patients:
 +
            result[p.id] = (u'%s %s %s' % (p.title,
 +
                                          p.first_name,
 +
                                          p.last_name))
 +
        return result
 +
 
 +
 
 +
     _rec_name='full_name'
 +
 
 +
    _columns={
 +
        'title': fields.char(size=50,
 +
                            string=u'คำนำหน้าชื่อ',
 +
                            required=True),
 +
        'first_name': fields.char(size=256,
 +
                                  string=u'ชื่อ',
 +
                                  required=True),
 +
        'last_name': fields.char(size=256,
 +
                                string=u'นามสกุล',
 +
                                required=True),
 +
        'sex': fields.selection([('M',u'ชาย'),('F',u'หญิง'),],
 +
                                string=u'เพศ',
 +
                                required=True),
 +
        'id_number': fields.char(size=20,
 +
                                string=u'เลขบัตรประชาชน',
 +
                                required=True),
 +
        'full_name': fields.function(get_full_name,
 +
                                    type='char'),
 +
        'appointment_ids': fields.one2many('histest.appointment',
 +
                                          'patient_id',
 +
                                          'Appointments'),
 +
    }
 
      
 
      
    instance.web.client_actions.add('histest.indexAction',
+
:
                                    'instance.histest.indexAction');
 
  
};
+
class HISAppointment(osv.Model):
 +
    _name = 'histest.appointment'
 +
    _columns = {
 +
            'date': fields.date(string=u'วันนัดหมาย', required=True),
 +
            'patient_id': fields.many2one('histest.patient', 'Patient'),
 +
            }
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== histest_message.js ===
+
=== ไฟล์ <code>histest_view.xml</code> ===
 +
เพิ่มการแสดงผลฟิลด์ appointment_ids ในฟอร์มของโมเดล HISPatient
 +
 
 +
<syntaxhighlight lang="xml">
 +
    <record model="ir.ui.view" id="view_histest_patient_form">
 +
      <field name="name">histest.patient.form</field>
 +
      <field name="model">histest.patient</field>
 +
      <field name="arch" type="xml">
 +
        <form string="Patient" version="7.0">
 +
          <group cols="2">
 +
            <group colspan="1">
 +
              <field name="title"/>
 +
              <field name="first_name" widget="firstname" />
 +
              <field name="last_name" widget="lastname" />
 +
            </group>
 +
            <group colspan="1">
 +
              <field name="sex"/>
 +
              <field name="id_number" widget="person_id" />
 +
              <field name="appointment_ids" />
 +
            </group>
 +
          </group>
 +
        </form>
 +
      </field>
 +
    </record>
 +
</syntaxhighlight>
  
<syntaxhighlight lang="javascript">
+
=== ไฟล์ <code>histest_report.xml</code> ===
openerp.histest.message = function(instance) {
+
 
   
+
ใช้ไฟล์นี้ระบุองค์ประกอบของรายงานการนัดหมายผู้ป่วย
    instance.histest.MsgManagerWidget = instance.web.Widget.extend({
+
 
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<openerp>
 +
  <data>
  
        start: function() {
+
    <report
            var self = this;
+
      auto="True"
            console.log("MsgMan started...");
+
      id="report_histest_patient"
            setInterval(function() {
+
      model="histest.patient"
                self.popup();
+
      name="histest.patient"
            },5000);
+
      rml="histest/report/histest_patient.rml"
            this.shownMsgId = 0;
+
      string="Print Appointments"
        },
+
      header="False"
 +
      menu="True"
 +
      />
  
        popup: function() {
+
  </data>
            var model = new instance.web.Model('histest.message');
+
</openerp>
            var self = this;
 
            model.query().filter([['id','>',this.shownMsgId]]).all().done(function(results){
 
                if (results.length > 0) {
 
                    msgId = results[results.length-1].id;
 
                    self.do_action({
 
                        type: 'ir.actions.act_window',
 
                        res_model: "histest.message",
 
                        res_id: msgId,
 
                        views: [[false, 'form']],
 
                        target: 'new',
 
                        context: {},
 
                    });
 
                    self.shownMsgId = msgId;
 
                }
 
            });
 
        }
 
    });
 
};
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== histest_patient.js ===
+
=== ไฟล์ <code>__openerp__.py</code> ===
<syntaxhighlight lang="javascript">
+
 
openerp.histest.patient = function(instance) {
+
ระบุให้ใช้ไฟล์ <code>histest_report.xml</code> เป็น data
      
+
 
     instance.histest.indexAction = instance.web.Widget.extend({
+
<syntaxhighlight lang="python">
         template: 'histest.index',
+
{
         events: {
+
     :
            'click .oe_histest_patient_new_link': 'linkClick',
+
     'data': [
            'click .oe_histest_patient_query_link': 'patientQuery',
+
         'histest_view.xml',
        },
+
         'histest_report.xml',
 +
    ],
 +
    :
 +
}
 +
</syntaxhighlight>
  
        start: function() {
+
== การทำให้รายงานรองรับการใช้งานภาษาไทย ==
            var msgManager = new instance.histest.message.MsgManagerWidget(this);
+
แก้ไขไฟล์ <code><openerp-path>/openerp/report/render/rml2pdf/customfonts.py</code> เพื่อระบุให้ใช้ฟอนต์ Garuda แทนฟอนต์ Helvetica รวมถึงระบุพาธสำหรับการค้นหาไฟล์ True-Type Font ที่อ้างถึง
            msgManager.appendTo('.oe_application');
+
<syntaxhighlight lang="python">
         },
+
:
 +
CustomTTFonts = [ ('Helvetica',"Garuda", "Garuda.ttf", 'normal'),
 +
        ('Helvetica',"Garuda Bold", "Garuda-Bold.ttf", 'bold'),
 +
        ('Helvetica',"Garuda Oblique", "Garuda-Oblique.ttf", 'italic'),
 +
        ('Helvetica',"Garuda BoldOblique", "Garuda-BoldOblique.ttf", 'bolditalic'),
 +
         :
 +
]
  
        patientQuery: function() {
 
            var model = new instance.web.Model('histest.patient');
 
            var self = this;
 
            self.$(".oe_histest_patient_results").html("");
 
            model.query().all().done(function(results) {
 
                for(var i=0; i < results.length; i++) {
 
                    self.$(".oe_histest_patient_results")
 
                        .append('<li>' + results[i].first_name + '</li>');
 
                }
 
            });
 
        },
 
  
        linkClick: function() {
+
TTFSearchPath_Linux = [
             this.do_action({
+
             '/usr/share/fonts/truetype', # SuSE
                type: 'ir.actions.act_window',
+
            '/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
                res_model: 'histest.patient',
+
            '/usr/share/fonts/truetype/*', # Ubuntu,
                views: [[false,'form']],
+
            '/usr/share/fonts/thai-scalable', # CentOS
                target: 'current',
+
            '/usr/share/fonts/TTF/*', # at Mandriva/Mageia
                context: {},
+
            '/usr/share/fonts/TTF', # Arch Linux
             });
+
             ]
        }
+
:
    });
 
};
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Responsive CSS ==
 
 
== เนื้อหาช่วงบ่าย ==
 
 
== เอกสารครั้งก่อน ๆ ==
 
== เอกสารครั้งก่อน ๆ ==
 
* [[Openerp/histest1|โค้ดสำหรับการทดสอบครั้งที่ 1 วันที่ 27 มี.ค. 2557]] - เกี่ยวกับ web module และการเรียก message
 
* [[Openerp/histest1|โค้ดสำหรับการทดสอบครั้งที่ 1 วันที่ 27 มี.ค. 2557]] - เกี่ยวกับ web module และการเรียก message
 +
* [[Openerp/histest2|โค้ดสำหรับการทดสอบครั้งที่ 2 วันที่ 3 เม.ย. 2557]] - การทำ responsive css และ asynchronous request

รุ่นแก้ไขปัจจุบันเมื่อ 07:31, 24 เมษายน 2557

หน้านี้รวมข้อมูลสำหรับการทดสอบการพัฒนาระบบด้วย OpenERP

การสร้างรายงาน

  • สร้างไดเรคตอรี่ชื่อ report และสร้างไฟล์ __init__.py ไว้ภายในนั้น
  • ดาวน์โหลด ไฟล์:Histest patient.odt เพื่อใช้เตรียมเทมเพลทสำหรับรายงาน เซฟไฟล์นี้ไว้ในไดเรคตอรี report
  • ใช้คำสั่ง openerp_sxw2rml.py แปลงไฟล์ของ OpenOffice ให้เป็นไฟล์ RML (Report Markup Language) เพื่อนำไปใช้กับระบบสร้างรายงานของ OpenERP ต่อไป (หมายเหตุ: <openerp-path> หมายถึงพาธที่ติดตั้ง OpenERP)
python <openerp-path>/openerp/addons/base_report_designer/openerp_sxw2rml/openerp_sxw2rml.py histest_patient.odt > histest_patient.rml

ไฟล์ histest_patient.rml

อย่างไรก็ตาม หากไม่ต้องการใช้ OpenOffice/LibreOffice ในการสร้างเทมเพลท เราสามารถสร้างไฟล์ RML ขึ้นมาได้ด้วยตัวเองดังนี้

<?xml version="1.0"?>
<document filename="test.pdf">
  <template pageSize="(595.0,842.0)" title="Test" author="Martin Simon" allowSplitting="20">
    <pageTemplate id="first">
      <frame id="first" x1="57.0" y1="57.0" width="481" height="728"/>
    </pageTemplate>
  </template>
  <stylesheet>
    <blockTableStyle id="Standard_Outline">
      <blockAlignment value="LEFT"/>
      <blockValign value="TOP"/>
    </blockTableStyle>
    <initialize>
      <paraStyle name="all" alignment="justify"/>
    </initialize>
    <paraStyle name="P1" fontName="Helvetica"/>
    <paraStyle name="P2" fontName="Helvetica"/>
    <paraStyle name="Standard" fontName="Helvetica"/>
    <paraStyle name="Heading" fontName="Helvetica" fontSize="12.0" leading="15" spaceBefore="12.0" spaceAfter="6.0"/>
    <paraStyle name="Text_20_body" fontName="Helvetica" spaceBefore="0.0" spaceAfter="6.0"/>
    <paraStyle name="List" fontName="Helvetica" spaceBefore="0.0" spaceAfter="6.0"/>
    <paraStyle name="Caption" fontName="Helvetica-Oblique" fontSize="10.0" leading="13" spaceBefore="6.0" spaceAfter="6.0"/>
    <paraStyle name="Index" fontName="Helvetica"/>
    <paraStyle name="Heading_20_1" fontName="Helvetica-Bold" fontSize="115%" leading="NaN" spaceBefore="12.0" spaceAfter="6.0"/>
    <paraStyle name="Table_20_Contents" fontName="Helvetica"/>
    <images/>
  </stylesheet>
  <story>
    <para style="P2">Patient Appointment Information</para>
    <para style="P1">
      <font color="white"> </font>
    </para>
    <section>
      <para style="P1">[[ repeatIn(objects, 'patient') ]]</para>
      <para style="P1">Patient ID: [[ patient.id_number ]]</para>
      <para style="P1">Patient Name: [[ patient.full_name ]]</para>
      <para style="P1">
        <font color="white"> </font>
      </para>
      <para style="P1">Appointment Dates:</para>
      <section>
        <para style="P1">[[ repeatIn(patient.appointment_ids, 'appointment') ]]</para>
        <para style="P1">- [[ appointment.date ]]</para>
      </section>
      <para style="P1">
        <font color="white"> </font>
      </para>
    </section>
    <para style="P1">
      <font color="white"> </font>
    </para>
    <para style="P1">
      <font color="white"> </font>
    </para>
  </story>
</document>

ไฟล์ histest.py

เพิ่มโมเดล HISAppointment และแก้ไขโมเดล HISPatient ให้เชื่อมโยงถึงกัน

from osv import osv, fields

class HISPatient(osv.Model):
    _name='histest.patient'

    def get_full_name(self, cr, uid, ids, field_name, arg, context):
        patients = self.browse(cr, uid, ids)
        result = {}
        for p in patients:
            result[p.id] = (u'%s %s %s' % (p.title,
                                           p.first_name,
                                           p.last_name))
        return result


    _rec_name='full_name'

    _columns={
        'title': fields.char(size=50,
                             string=u'คำนำหน้าชื่อ',
                             required=True),
        'first_name': fields.char(size=256,
                                  string=u'ชื่อ',
                                  required=True),
        'last_name': fields.char(size=256,
                                 string=u'นามสกุล',
                                 required=True),
        'sex': fields.selection([('M',u'ชาย'),('F',u'หญิง'),],
                                string=u'เพศ',
                                required=True),
        'id_number': fields.char(size=20,
                                 string=u'เลขบัตรประชาชน',
                                 required=True),
        'full_name': fields.function(get_full_name,
                                     type='char'),
        'appointment_ids': fields.one2many('histest.appointment',
                                           'patient_id',
                                           'Appointments'),
    }
    
:

class HISAppointment(osv.Model):
    _name = 'histest.appointment'
    _columns = {
            'date': fields.date(string=u'วันนัดหมาย', required=True),
            'patient_id': fields.many2one('histest.patient', 'Patient'),
            }

ไฟล์ histest_view.xml

เพิ่มการแสดงผลฟิลด์ appointment_ids ในฟอร์มของโมเดล HISPatient

    <record model="ir.ui.view" id="view_histest_patient_form">
      <field name="name">histest.patient.form</field>
      <field name="model">histest.patient</field>
      <field name="arch" type="xml">
        <form string="Patient" version="7.0">
          <group cols="2">
            <group colspan="1">
              <field name="title"/>
              <field name="first_name" widget="firstname" />
              <field name="last_name" widget="lastname" />
            </group>
            <group colspan="1">
              <field name="sex"/>
              <field name="id_number" widget="person_id" />
              <field name="appointment_ids" />
            </group>
          </group>
        </form>
      </field>
    </record>

ไฟล์ histest_report.xml

ใช้ไฟล์นี้ระบุองค์ประกอบของรายงานการนัดหมายผู้ป่วย

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

    <report
      auto="True"
      id="report_histest_patient"
      model="histest.patient"
      name="histest.patient"
      rml="histest/report/histest_patient.rml"
      string="Print Appointments"
      header="False"
      menu="True"
      />

  </data>
</openerp>

ไฟล์ __openerp__.py

ระบุให้ใช้ไฟล์ histest_report.xml เป็น data

{
    :
    'data': [
        'histest_view.xml',
        'histest_report.xml',
    ],
    :
}

การทำให้รายงานรองรับการใช้งานภาษาไทย

แก้ไขไฟล์ <openerp-path>/openerp/report/render/rml2pdf/customfonts.py เพื่อระบุให้ใช้ฟอนต์ Garuda แทนฟอนต์ Helvetica รวมถึงระบุพาธสำหรับการค้นหาไฟล์ True-Type Font ที่อ้างถึง

:
CustomTTFonts = [ ('Helvetica',"Garuda", "Garuda.ttf", 'normal'),
        ('Helvetica',"Garuda Bold", "Garuda-Bold.ttf", 'bold'),
        ('Helvetica',"Garuda Oblique", "Garuda-Oblique.ttf", 'italic'),
        ('Helvetica',"Garuda BoldOblique", "Garuda-BoldOblique.ttf", 'bolditalic'),
        :
]


TTFSearchPath_Linux = [
            '/usr/share/fonts/truetype', # SuSE
            '/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
            '/usr/share/fonts/truetype/*', # Ubuntu,
            '/usr/share/fonts/thai-scalable', # CentOS
            '/usr/share/fonts/TTF/*', # at Mandriva/Mageia
            '/usr/share/fonts/TTF', # Arch Linux
            ]
:

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