Commit 02d68539 by aagusti

price history penambahan graphic dan pivot

1 parent 7faf58b9
...@@ -8,24 +8,28 @@ _logger = logging.getLogger(__name__) ...@@ -8,24 +8,28 @@ _logger = logging.getLogger(__name__)
def get_selection_label(self, obj, field_name, field_value): def get_selection_label(self, obj, field_name, field_value):
return dict(self.env[obj].fields_get(allfields=[field_name])[field_name]['selection'])[field_value] return dict(self.env[obj].fields_get(allfields=[field_name])[field_name][
'selection'])[field_value]
class CountryState(models.Model): class CountryState(models.Model):
_inherit = 'res.country.state' _inherit = 'res.country.state'
district_ids = fields.One2many('res.district', 'state_id', string='Kota/Kabupaten') district_ids = fields.One2many('res.district', 'state_id',
string='Kota/Kabupaten')
def name_get(self): def name_get(self):
result = [] result = []
for record in self: for record in self:
result.append((record.id, "{} ({})".format(record.name, record.code))) result.append(
(record.id, "{} ({})".format(record.name, record.code)))
return result return result
class District(models.Model): class District(models.Model):
_name = 'res.district' _name = 'res.district'
_description = 'Kota/Kabupaten' _description = 'Kota/Kabupaten'
state_id = fields.Many2one('res.country.state', string='Provinsi', required=True) state_id = fields.Many2one('res.country.state', string='Provinsi',
required=True)
typ = fields.Selection([ typ = fields.Selection([
('kab', 'Kabupaten'), ('kab', 'Kabupaten'),
('kabtif', 'Kabupaten Administratif'), ('kabtif', 'Kabupaten Administratif'),
...@@ -36,7 +40,8 @@ class District(models.Model): ...@@ -36,7 +40,8 @@ class District(models.Model):
name = fields.Char(string="Nama Kota/Kabupaten", index=True, required=True) name = fields.Char(string="Nama Kota/Kabupaten", index=True, required=True)
# display_code = fields.Char(compute='_compute_display_code', store=True, index=True) # display_code = fields.Char(compute='_compute_display_code', store=True, index=True)
# display_name = fields.Char(compute='_compute_display_name', store=True, index=True) # display_name = fields.Char(compute='_compute_display_name', store=True, index=True)
sub_district_ids = fields.One2many('res.district.sub', 'district_id', string='Kecamatan') sub_district_ids = fields.One2many('res.district.sub', 'district_id',
string='Kecamatan')
# address_view_id = fields.Many2one( # address_view_id = fields.Many2one(
# comodel_name='ir.ui.view', string="Input View", # comodel_name='ir.ui.view', string="Input View",
# domain=[('model', '=', 'res.partner'), ('type', '=', 'form')], # domain=[('model', '=', 'res.partner'), ('type', '=', 'form')],
...@@ -50,15 +55,19 @@ class District(models.Model): ...@@ -50,15 +55,19 @@ class District(models.Model):
# help="Url of static flag image", # help="Url of static flag image",
# ) # )
_sql_constraints = [ _sql_constraints = [
('code_uniq', 'unique (state_id,code)', 'Kode Kabupaten/Kota Harus Unik !'), ('code_uniq', 'unique (state_id,code)',
('name_uniq', 'unique (state_id,typ,name)', 'Nama Kabupaten/Kota Harus Unik !'), 'Kode Kabupaten/Kota Harus Unik !'),
('name_uniq', 'unique (state_id,typ,name)',
'Nama Kabupaten/Kota Harus Unik !'),
] ]
@api.model @api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): def _name_search(self, name, args=None, operator='ilike', limit=100,
name_get_uid=None):
args = args or [] args = args or []
if self.env.context.get('state_id'): if self.env.context.get('state_id'):
args = expression.AND([args, [('state_id', '=', self.env.context.get('state_id'))]]) args = expression.AND(
[args, [('state_id', '=', self.env.context.get('state_id'))]])
if operator == 'ilike' and not (name or '').strip(): if operator == 'ilike' and not (name or '').strip():
first_domain = [] first_domain = []
...@@ -67,12 +76,14 @@ class District(models.Model): ...@@ -67,12 +76,14 @@ class District(models.Model):
first_domain = [('code', '=ilike', name)] first_domain = [('code', '=ilike', name)]
domain = [('name', operator, name)] domain = [('name', operator, name)]
first_district_ids = self._search(expression.AND([first_domain, args]), limit=limit, first_district_ids = self._search(expression.AND([first_domain, args]),
limit=limit,
access_rights_uid=name_get_uid) if first_domain else [] access_rights_uid=name_get_uid) if first_domain else []
first_ids = list(first_district_ids) + [ first_ids = list(first_district_ids) + [
district_id district_id
for district_id in self._search(expression.AND([domain, args]), for district_id in self._search(expression.AND([domain, args]),
limit=limit, access_rights_uid=name_get_uid) limit=limit,
access_rights_uid=name_get_uid)
if district_id not in first_district_ids if district_id not in first_district_ids
] ]
# _logger.info(self.env.context.get('country_id')) # _logger.info(self.env.context.get('country_id'))
...@@ -92,14 +103,15 @@ class District(models.Model): ...@@ -92,14 +103,15 @@ class District(models.Model):
result = [] result = []
for record in self: for record in self:
result.append((record.id, "{} {} ({}.{})".format( result.append((record.id, "{} {} ({}.{})".format(
get_selection_label(self, self._name, 'typ', record.typ), record.name, get_selection_label(self, self._name, 'typ', record.typ),
record.state_id.code, record.code))) record.name, record.state_id.code, record.code)))
return result return result
def code_get(self): def code_get(self):
result = [] result = []
for record in self: for record in self:
result.append((record.id, "{}.{}".format(record.state_id.code, record.code))) result.append(
(record.id, "{}.{}".format(record.state_id.code, record.code)))
return result return result
# @api.model_create_multi # @api.model_create_multi
...@@ -131,19 +143,24 @@ class District(models.Model): ...@@ -131,19 +143,24 @@ class District(models.Model):
class SubDistrict(models.Model): class SubDistrict(models.Model):
_name = 'res.district.sub' _name = 'res.district.sub'
_description = 'Kecamatan' _description = 'Kecamatan'
district_id = fields.Many2one('res.district', string='Kabupaten/Kota', required=True) district_id = fields.Many2one('res.district', string='Kabupaten/Kota',
required=True)
code = fields.Char(string="Kode Kecamatan") code = fields.Char(string="Kode Kecamatan")
name = fields.Char(string="Nama Kecamatan", index=True) name = fields.Char(string="Nama Kecamatan", index=True)
_sql_constraints = [ _sql_constraints = [
('code_uniq', 'unique (district_id,code)', 'Kode Kecamatan Harus Unik !'), ('code_uniq', 'unique (district_id,code)',
('name_uniq', 'unique (district_id,name)', 'Nama Kecamatan Harus Unik !'), 'Kode Kecamatan Harus Unik !'),
('name_uniq', 'unique (district_id,name)',
'Nama Kecamatan Harus Unik !'),
] ]
@api.model @api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): def _name_search(self, name, args=None, operator='ilike', limit=100,
name_get_uid=None):
args = args or [] args = args or []
if self.env.context.get('district_id'): if self.env.context.get('district_id'):
args = expression.AND([args, [('district_id', '=', self.env.context.get('district_id'))]]) args = expression.AND([args, [
('district_id', '=', self.env.context.get('district_id'))]])
if operator == 'ilike' and not (name or '').strip(): if operator == 'ilike' and not (name or '').strip():
first_domain = [] first_domain = []
...@@ -152,12 +169,14 @@ class SubDistrict(models.Model): ...@@ -152,12 +169,14 @@ class SubDistrict(models.Model):
first_domain = [('code', '=ilike', name)] first_domain = [('code', '=ilike', name)]
domain = [('name', operator, name)] domain = [('name', operator, name)]
first_sub_district_ids = self._search(expression.AND([first_domain, args]), limit=limit, first_sub_district_ids = self._search(
expression.AND([first_domain, args]), limit=limit,
access_rights_uid=name_get_uid) if first_domain else [] access_rights_uid=name_get_uid) if first_domain else []
return list(first_sub_district_ids) + [ return list(first_sub_district_ids) + [
district_id district_id
for district_id in self._search(expression.AND([domain, args]), for district_id in self._search(expression.AND([domain, args]),
limit=limit, access_rights_uid=name_get_uid) limit=limit,
access_rights_uid=name_get_uid)
if district_id not in first_sub_district_ids if district_id not in first_sub_district_ids
] ]
...@@ -191,15 +210,20 @@ class Village(models.Model): ...@@ -191,15 +210,20 @@ class Village(models.Model):
name = fields.Char(string="Nama Desa/Kelurahan", index=True) name = fields.Char(string="Nama Desa/Kelurahan", index=True)
zip = fields.Char(string="Kode Pos Desa/Kelurahan", size=5, ) zip = fields.Char(string="Kode Pos Desa/Kelurahan", size=5, )
_sql_constraints = [ _sql_constraints = [
('village_code_uniq', 'unique (sub_district_id,code)', 'Kode Kelurahan/Desa Harus Unik !'), ('village_code_uniq', 'unique (sub_district_id,code)',
('village_name_uniq', 'unique (sub_district_id,typ,name)', 'Nama Kelurahan/Desa Harus Unik !'), 'Kode Kelurahan/Desa Harus Unik !'),
('village_name_uniq', 'unique (sub_district_id,typ,name)',
'Nama Kelurahan/Desa Harus Unik !'),
] ]
@api.model @api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): def _name_search(self, name, args=None, operator='ilike', limit=100,
name_get_uid=None):
args = args or [] args = args or []
if self.env.context.get('sub_district_id'): if self.env.context.get('sub_district_id'):
args = expression.AND([args, [('sub_district_id', '=', self.env.context.get('sub_district_id'))]]) args = expression.AND([args, [('sub_district_id', '=',
self.env.context.get(
'sub_district_id'))]])
# _logger.info(self.env.context.get('sub_district_id')) # _logger.info(self.env.context.get('sub_district_id'))
# _logger.info(args) # _logger.info(args)
...@@ -210,12 +234,14 @@ class Village(models.Model): ...@@ -210,12 +234,14 @@ class Village(models.Model):
first_domain = [('code', '=ilike', name)] first_domain = [('code', '=ilike', name)]
domain = [('name', operator, name)] domain = [('name', operator, name)]
first_village_ids = self._search(expression.AND([first_domain, args]), limit=limit, first_village_ids = self._search(expression.AND([first_domain, args]),
limit=limit,
access_rights_uid=name_get_uid) if first_domain else [] access_rights_uid=name_get_uid) if first_domain else []
first_ids = list(first_village_ids) + [ first_ids = list(first_village_ids) + [
sub_district_id sub_district_id
for sub_district_id in self._search(expression.AND([domain, args]), for sub_district_id in self._search(expression.AND([domain, args]),
limit=limit, access_rights_uid=name_get_uid) limit=limit,
access_rights_uid=name_get_uid)
if sub_district_id not in first_village_ids if sub_district_id not in first_village_ids
] ]
# _logger.info(first_ids) # _logger.info(first_ids)
...@@ -235,7 +261,8 @@ class Village(models.Model): ...@@ -235,7 +261,8 @@ class Village(models.Model):
result = [] result = []
for record in self: for record in self:
result.append((record.id, "{} {} ({}.{}.{}.{})".format( result.append((record.id, "{} {} ({}.{}.{}.{})".format(
get_selection_label(self, self._name, 'typ', record.typ), record.name, get_selection_label(self, self._name, 'typ', record.typ),
record.name,
record.sub_district_id.district_id.state_id.code, record.sub_district_id.district_id.state_id.code,
record.sub_district_id.district_id.code, record.sub_district_id.district_id.code,
record.sub_district_id.code, record.code))) record.sub_district_id.code, record.code)))
......
...@@ -18,11 +18,20 @@ class TandurPriceHist(models.Model): ...@@ -18,11 +18,20 @@ class TandurPriceHist(models.Model):
_name = 'tandur.price.hist' _name = 'tandur.price.hist'
_description = 'History of Price' _description = 'History of Price'
_order = "product_id" _order = "product_id"
@api.depends('year', 'month')
def _compute_period(self):
for record in self:
self.period = "{}-{}".format(str(int(record.year)),
str(int(record.month)).zfill(2))
year = fields.Float(string='Year', group_operator=False) year = fields.Float(string='Year', group_operator=False)
month = fields.Float(string='Month', group_operator=False) month = fields.Float(string='Month', group_operator=False)
product_id = fields.Many2one('product.product', 'Product', required=True) period = fields.Char(compute='_compute_period', store=True,
price = fields.Float(string='Price', group_operator=False) index=True)
product_id = fields.Many2one('product.product', 'Product',
required=True)
price = fields.Float(string='Price', group_operator="avg")
@api.model @api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
submenu=False): submenu=False):
...@@ -41,3 +50,11 @@ class TandurPriceHist(models.Model): ...@@ -41,3 +50,11 @@ class TandurPriceHist(models.Model):
# res['arch'] = ElementTree.tostring(root) # res['arch'] = ElementTree.tostring(root)
# #
# return res # return res
def name_get(self):
result = []
for record in self:
result.append(
(record.id, "{}-{}".format(str(int(record.year)),
str(int(record.month)).zfill(2))))
return result
...@@ -175,7 +175,7 @@ ...@@ -175,7 +175,7 @@
<menuitem id="tandur_price_menu" <menuitem id="tandur_price_menu"
name="Price History" name="Price History"
parent="tandur_config_menu" parent="tandur_config_menu"
action="tandur_price_hist_action" action="action_tandur_price_hist"
sequence="2"/> sequence="2"/>
<data> <data>
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<data> <data>
<record id="tandur_price_hist_view_tree" model="ir.ui.view"> <record id="view_tandur_price_hist_tree" model="ir.ui.view">
<field name="name">view.tandur.price.hist.tree</field>
<field name="model">tandur.price.hist</field> <field name="model">tandur.price.hist</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Price History" editable="top" sample="1" create="1" delete="1" multi_edit="0"> <tree string="Price History" sample="1" create="1" delete="1">
<field name="year"/> <!-- editable="top" -->
<field name="month"/> <field name="period"/>
<field name="product_id"/> <field name="product_id"/>
<field name="price"/> <field name="price"/>
</tree> </tree>
</field> </field>
</record> </record>
<!--editable="top" sample="1" create="1" delete="1" multi_edit="0"-->
<!-- <record id="view_tandur_price_hist_tree" model="ir.ui.view">-->
<!-- <field name="name">view.tandur.price.hist.tree</field>-->
<!-- <field name="model">tandur.price.hist</field>-->
<!-- <field name="arch" type="xml">-->
<!-- <tree string="Price History" editable="top" sample="1" multi_edit="0">-->
<!-- <field name="year"/>-->
<!-- <field name="month"/>-->
<!-- <field name="period"/>-->
<!-- <field name="product_id"/>-->
<!-- <field name="price"/>-->
<!-- </tree>-->
<!-- </field>-->
<!-- </record>-->
<record id="view_tandur_price_hist_form" model="ir.ui.view">
<field name="name">view.tandur.price.hist.form</field>
<field name="model">tandur.price.hist</field>
<field name="arch" type="xml">
<form string="Price History">
<sheet>
<div class="oe_title">
<h1>
<field name="product_id"/>
</h1>
</div>
<group>
<field name="year" options="{'format': 0}"/>
<field name="month" options="{'format': 00}"/>
<field name="price"/>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.ui.view" id="tandur_price_hist_graph">
<field name="name">tandur.price.hist.graph</field>
<field name="model">tandur.price.hist</field>
<field name="arch" type="xml">
<graph string="Price History" sample="1">
<field name="period"/>
<field name="product_id"/>
<field name="price" type="measure"/>
</graph>
</field>
</record>
<record model="ir.ui.view" id="tandur_price_hist_pivot">
<field name="name">tandur.price.hist.pivot</field>
<field name="model">tandur.price.hist</field>
<field name="arch" type="xml">
<pivot string="Price History" display_quantity="True" sample="1">
<field name="product_id" type="row"/>
<field name="period" type="row"/>
<field name="price" type="measure"/>
</pivot>
</field>
</record>
<record id="view_tandur_price_hist_filter" model="ir.ui.view"> <record id="view_tandur_price_hist_filter" model="ir.ui.view">
<field name="name">tandur.price.hist.select</field> <field name="name">view.tandur.price.hist.filter</field>
<field name="model">tandur.price.hist</field> <field name="model">tandur.price.hist</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<search string="Search Product"> <search string="Search Product">
<field name="product_id" string="Product"/> <field name="product_id" string="Product"/>
<separator/> <separator/>
<group expand="0" string="Group By..."> <group expand="0" string="Group By...">
<filter string="Product" name="group_by_product" domain="[]" context="{'group_by': 'product_id'}"/> <filter string="Product" name="group_by_product" domain="[]"
context="{'group_by': 'product_id'}"/>
<filter string='Year' name="group_by_year" domain="[]" context="{'group_by' : 'year'}"/> <filter string='Year' name="group_by_year" domain="[]" context="{'group_by' : 'year'}"/>
</group> </group>
</search> </search>
</field> </field>
</record> </record>
<record id="tandur_price_hist_search_action" model="ir.actions.act_window"> <!-- <record id="action_tandur_price_hist_search" model="ir.actions.act_window">-->
<field name="context">{'default_product_id': active_id, 'search_default_product_id': active_id}</field> <!-- <field name="context">{'default_product_id': active_id, 'search_default_product_id': active_id}</field>-->
<field name="name">Price History</field> <!-- <field name="name">Price History</field>-->
<field name="res_model">tandur.price.hist</field> <!-- <field name="res_model">tandur.price.hist</field>-->
<field name="domain">[]</field> <!-- Force empty --> <!-- <field name="view_type">form</field>-->
</record> <!-- <field name="domain">[]</field> &lt;!&ndash; Force empty &ndash;&gt;-->
<!-- </record>-->
<record id="tandur_price_hist_action" model="ir.actions.act_window"> <record id="action_tandur_price_hist" model="ir.actions.act_window">
<field name="name">History of Price </field> <field name="name">History of Price</field>
<field name="res_model">tandur.price.hist</field> <field name="res_model">tandur.price.hist</field>
<field name="context">{'search_default_group_by_product': 1}</field> <field name="context">{'search_default_group_by_product': 1}</field>
<field name="view_mode">tree</field> <field name="view_mode">tree,form,graph,pivot</field>
<field name="search_view_id" ref="view_tandur_price_hist_filter"/> <field name="search_view_id" ref="view_tandur_price_hist_filter"/>
<field name="view_id" ref="tandur_price_hist_view_tree"/> <field name="view_id" ref="view_tandur_price_hist_tree"/>
<field name="help" type="html"> <field name="help" type="html">
<p class="o_view_nocontent_smiling_face"> <p class="o_view_nocontent_smiling_face">
History of Price Not Found History of Price Not Found
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!