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

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 3 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 174: แถว 174:
 
   </data>
 
   </data>
 
</openerp>
 
</openerp>
 +
</syntaxhighlight>
 +
 +
== ezbill.py version 2 ==
 +
 +
<syntaxhighlight lang="python">
 +
from osv import osv, fields
 +
import time
 +
 +
class Product(osv.Model):
 +
    _name = 'ezbill.product'
 +
    _columns = {
 +
        'name': fields.char('Name', size=50, required=True),
 +
        'price_per_unit': fields.float('Price per unit', required=True),
 +
    }
 +
 +
class Customer(osv.Model):
 +
    _name = 'ezbill.customer'
 +
    _rec_name = 'first_name'
 +
    _columns = {
 +
        'first_name': fields.char('First name', size=50, required=True),
 +
        'last_name': fields.char('Last name', size=50, required=True),
 +
    }
 +
 +
class Bill(osv.Model):
 +
    _name = 'ezbill.bill'
 +
    _columns = {
 +
        'customer': fields.many2one('ezbill.customer', string='Customer', required=True),
 +
        'bill_date': fields.datetime('Bill Date', required=True),
 +
        'bill_line': fields.one2many('ezbill.bill.line','bill', string='Bill Lines'),
 +
    }
 +
 +
    def _get_date(self, cr, uid, context):
 +
        return time.strftime('%Y-%m-%d %H:%M:%S')
 +
 +
    _defaults = {
 +
        'bill_date': _get_date,
 +
    }
 +
 +
class BillLine(osv.Model):
 +
    _name = 'ezbill.bill.line'
 +
 +
    def _get_name(self, cr, uid, ids, field_name, arg, context):
 +
        res = {}
 +
        for line in self.browse(cr, uid, ids, context=context):
 +
            res[line.id] = line.product.name + ' (' + str(line.quantity) + ')'
 +
        return res
 +
 +
 +
    _columns = {
 +
        'name': fields.function(_get_name, type='char', string='Bill Line', readonly=True),
 +
        'bill': fields.many2one('ezbill.bill', string='Bill ID', readonly=True),
 +
        'product': fields.many2one('ezbill.product', string='Product', required=True),
 +
        'quantity': fields.integer('Quantity', required=True),
 +
    }
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขปัจจุบันเมื่อ 08:54, 4 ธันวาคม 2556

Template: view

module_view.xml

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

views / actions

    <record model="ir.ui.view" id="view_MODULE_MODEL_tree">
      <field name="name">MODULE.MODEL.tree</field>
      <field name="model">MODULE.MODEL</field>
      <field name="arch" type="xml">
        <tree string="MODEL">
          <field name="xxxxxxxxx"/>
        </tree>
      </field>
    </record>

    <record model="ir.ui.view" id="view_MODULE_MODEL_form">
      <field name="name">MODULE.MODEL.form</field>
      <field name="model">MODULE.MODEL</field>
      <field name="arch" type="xml">
        <form string="MODEL">
          <field name="xxxxxxxxxxx"/>
        </form>
      </field>
    </record>

    <record model="ir.actions.act_window" id="action_MODULE_MODEL">
      <field name="name">MODEL</field>
      <field name="res_model">MODULE.MODEL</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="context">{'search_default_open_true':True}</field>
      <field name="help" type="html">
        <p class="oe_view_nocontent_create">
          Click to add a ..............
        </p>
      </field>
    </record>

menu

    <menuitem name="MENUTITLETOP" id="menu_MODULE_top" 
              parent="" sequence="1" 
              action="MODULE.action_MODULE_MODEL"/>

    <menuitem name="MENUTITLE" id="menu_MODULE_main" 
              parent="menu_MODULE_top" sequence="1"/>
 
    <menuitem name="MODELTITLE" id="menu_MODULE_MODEL" 
              parent="menu_MODULE_main" sequence="1" 
              action="MODULE.action_MODULE_MODEL"/>

ezbill: version 1

ezbill.py

from osv import osv, fields

class Product(osv.Model):
    _name = 'ezbill.product'
    _columns = {
        'name': fields.char('Name', size=50, required=True),
        'price_per_unit': fields.float('Price per unit', required=True),
    }

class Customer(osv.Model):
    _name = 'ezbill.customer'
    _rec_name = 'first_name'
    _columns = {
        'first_name': fields.char('First name', size=50, required=True),
        'last_name': fields.char('Last name', size=50, required=True),
    }

ezbill_view.xml

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

    <record model="ir.ui.view" id="view_ezbill_customer_tree">
      <field name="name">ezbill.customer.tree</field>
      <field name="model">ezbill.customer</field>
      <field name="arch" type="xml">
        <tree string="customer">
          <field name="first_name"/>
          <field name="last_name"/>
        </tree>
      </field>
    </record>
 
    <record model="ir.ui.view" id="view_ezbill_customer_form">
      <field name="name">ezbill.customer.form</field>
      <field name="model">ezbill.customer</field>
      <field name="arch" type="xml">
        <form string="customer">
          <field name="first_name"/>
          <field name="last_name"/>
        </form>
      </field>
    </record>
 
    <record model="ir.actions.act_window" id="action_ezbill_customer">
      <field name="name">customer</field>
      <field name="res_model">ezbill.customer</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="context">{'search_default_open_true':True}</field>
      <field name="help" type="html">
        <p class="oe_view_nocontent_create">
          Click to add a ..............
        </p>
      </field>
    </record>

    <record model="ir.ui.view" id="view_ezbill_product_tree">
      <field name="name">ezbill.product.tree</field>
      <field name="model">ezbill.product</field>
      <field name="arch" type="xml">
        <tree string="product">
          <field name="name"/>
          <field name="price_per_unit"/>
        </tree>
      </field>
    </record>
 
    <record model="ir.ui.view" id="view_ezbill_product_form">
      <field name="name">ezbill.product.form</field>
      <field name="model">ezbill.product</field>
      <field name="arch" type="xml">
        <form string="product">
          <field name="name"/>
          <field name="price_per_unit"/>
        </form>
      </field>
    </record>
 
    <record model="ir.actions.act_window" id="action_ezbill_product">
      <field name="name">product</field>
      <field name="res_model">ezbill.product</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="context">{'search_default_open_true':True}</field>
      <field name="help" type="html">
        <p class="oe_view_nocontent_create">
          Click to add a new product
        </p>
      </field>
    </record>

    <menuitem name="Bills" id="menu_ezbill_top" 
              parent="" sequence="1" 
              action="ezbill.action_ezbill_product"/>

    <menuitem name="Billing System" id="menu_ezbill_main" 
              parent="menu_ezbill_top" sequence="1"/>

    <menuitem name="Products" id="menu_ezbill_product" 
              parent="menu_ezbill_main" sequence="1" 
              action="ezbill.action_ezbill_product"/>

    <menuitem name="Customers" id="menu_ezbill_customer" 
              parent="menu_ezbill_main" sequence="2" 
              action="ezbill.action_ezbill_customer"/>
  </data>
</openerp>

ezbill.py version 2

from osv import osv, fields
import time 

class Product(osv.Model):
    _name = 'ezbill.product'
    _columns = {
        'name': fields.char('Name', size=50, required=True),
        'price_per_unit': fields.float('Price per unit', required=True),
    }

class Customer(osv.Model):
    _name = 'ezbill.customer'
    _rec_name = 'first_name'
    _columns = {
        'first_name': fields.char('First name', size=50, required=True),
        'last_name': fields.char('Last name', size=50, required=True),
    }

class Bill(osv.Model):
    _name = 'ezbill.bill'
    _columns = {
        'customer': fields.many2one('ezbill.customer', string='Customer', required=True),
        'bill_date': fields.datetime('Bill Date', required=True),
        'bill_line': fields.one2many('ezbill.bill.line','bill', string='Bill Lines'),
    }

    def _get_date(self, cr, uid, context):
        return time.strftime('%Y-%m-%d %H:%M:%S')

    _defaults = {
        'bill_date': _get_date,
    }

class BillLine(osv.Model):
    _name = 'ezbill.bill.line'

    def _get_name(self, cr, uid, ids, field_name, arg, context):
        res = {}
        for line in self.browse(cr, uid, ids, context=context):
            res[line.id] = line.product.name + ' (' + str(line.quantity) + ')'
        return res


    _columns = {
        'name': fields.function(_get_name, type='char', string='Bill Line', readonly=True),
        'bill': fields.many2one('ezbill.bill', string='Bill ID', readonly=True),
        'product': fields.many2one('ezbill.product', string='Product', required=True),
        'quantity': fields.integer('Quantity', required=True),
    }