diff options
Diffstat (limited to 'addons/website_sale_digital/models')
| -rw-r--r-- | addons/website_sale_digital/models/__init__.py | 6 | ||||
| -rw-r--r-- | addons/website_sale_digital/models/account_invoice.py | 32 | ||||
| -rw-r--r-- | addons/website_sale_digital/models/ir_attachment.py | 11 | ||||
| -rw-r--r-- | addons/website_sale_digital/models/product.py | 61 |
4 files changed, 110 insertions, 0 deletions
diff --git a/addons/website_sale_digital/models/__init__.py b/addons/website_sale_digital/models/__init__.py new file mode 100644 index 00000000..dc2af98c --- /dev/null +++ b/addons/website_sale_digital/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import ir_attachment +from . import account_invoice +from . import product diff --git a/addons/website_sale_digital/models/account_invoice.py b/addons/website_sale_digital/models/account_invoice.py new file mode 100644 index 00000000..a79f3c7c --- /dev/null +++ b/addons/website_sale_digital/models/account_invoice.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class AccountInvoiceLine(models.Model): + + _inherit = ['account.move.line'] + + def get_digital_purchases(self): + partner = self.env.user.partner_id + + # Get paid invoices + purchases = self.sudo().search_read( + domain=[ + ('move_id.payment_state', 'in', ['paid', 'in_payment']), + ('move_id.partner_id', '=', partner.id), + ('product_id', '!=', False), + ], + fields=['product_id'], + ) + + # Get free products + purchases += self.env['sale.order.line'].sudo().search_read( + domain=[('display_type', '=', False), ('order_id.partner_id', '=', partner.id), '|', ('price_subtotal', '=', 0.0), ('order_id.amount_total', '=', 0.0)], + fields=['product_id'], + ) + + # I only want product_ids, but search_read insists in giving me a list of + # (product_id: <id>, name: <product code> <template_name> <attributes>) + return [line['product_id'][0] for line in purchases] diff --git a/addons/website_sale_digital/models/ir_attachment.py b/addons/website_sale_digital/models/ir_attachment.py new file mode 100644 index 00000000..46c26271 --- /dev/null +++ b/addons/website_sale_digital/models/ir_attachment.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import fields, models + + +class Attachment(models.Model): + + _inherit = ['ir.attachment'] + + product_downloadable = fields.Boolean("Downloadable from product portal", default=False) diff --git a/addons/website_sale_digital/models/product.py b/addons/website_sale_digital/models/product.py new file mode 100644 index 00000000..a5512dc9 --- /dev/null +++ b/addons/website_sale_digital/models/product.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, _ + + +class ProductTemplate(models.Model): + _inherit = ['product.template'] + + attachment_count = fields.Integer(compute='_compute_attachment_count', string="File") + + def _compute_attachment_count(self): + attachment_data = self.env['ir.attachment'].read_group([('res_model', '=', self._name), ('res_id', 'in', self.ids), ('product_downloadable', '=', True)], ['res_id'], ['res_id']) + mapped_data = dict([(data['res_id'], data['res_id_count']) for data in attachment_data]) + for product_template in self: + product_template.attachment_count = mapped_data.get(product_template.id, 0) + + def action_open_attachments(self): + self.ensure_one() + return { + 'name': _('Digital Attachments'), + 'domain': [('res_model', '=', self._name), ('res_id', '=', self.id), ('product_downloadable', '=', True)], + 'res_model': 'ir.attachment', + 'type': 'ir.actions.act_window', + 'view_mode': 'kanban,form', + 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), + 'help': """ + <p class="o_view_nocontent_smiling_face">Add attachments for this digital product</p> + <p>The attached files are the ones that will be purchased and sent to the customer.</p> + """, + } + + +class Product(models.Model): + _inherit = 'product.product' + + attachment_count = fields.Integer(compute='_compute_attachment_count', string="File") + + def _compute_attachment_count(self): + for product in self: + product.attachment_count = self.env['ir.attachment'].search_count([ + '|', + '&', '&', ('res_model', '=', 'product.template'), ('res_id', '=', product.product_tmpl_id.id), ('product_downloadable', '=', True), + '&', '&', ('res_model', '=', 'product.product'), ('res_id', '=', product.id), ('product_downloadable', '=', True)]) + + def action_open_attachments(self): + self.ensure_one() + return { + 'name': _('Digital Attachments'), + 'domain': [('product_downloadable', '=', True), '|', + '&', ('res_model', '=', 'product.template'), ('res_id', '=', self.product_tmpl_id.id), + '&', ('res_model', '=', self._name), ('res_id', '=', self.id)], + 'res_model': 'ir.attachment', + 'type': 'ir.actions.act_window', + 'view_mode': 'kanban,form', + 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), + 'help': """ + <p class="o_view_nocontent_smiling_face">Add attachments for this digital product</p> + <p>The attached files are the ones that will be purchased and sent to the customer.</p> + """, + } |
