diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index e0f17cde..3bc315c5 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -318,10 +318,11 @@ class PosConfig(models.Model): def name_get(self): result = [] for config in self: - if (not config.session_ids) or (config.session_ids[0].state == 'closed'): + last_session = self.env['pos.session'].search([('config_id', '=', config.id)], limit=1) + if (not last_session) or (last_session.state == 'closed'): result.append((config.id, config.name + ' (' + _('not used') + ')')) continue - result.append((config.id, config.name + ' (' + config.session_ids[0].user_id.name + ')')) + result.append((config.id, config.name + ' (' + last_session.user_id.name + ')')) return result @api.model @@ -350,11 +351,12 @@ class PosConfig(models.Model): @api.multi def write(self, vals): - if (self.is_posbox or vals.get('is_posbox')) and (self.iface_customer_facing_display or vals.get('iface_customer_facing_display')): - facing_display = (self.customer_facing_display_html or vals.get('customer_facing_display_html') or '').strip() - if not facing_display: - vals['customer_facing_display_html'] = self._compute_default_customer_html() result = super(PosConfig, self).write(vals) + + config_display = self.filtered(lambda c: c.is_posbox and c.iface_customer_facing_display and not (c.customer_facing_display_html or '').strip()) + if config_display: + super(PosConfig, config_display).write({'customer_facing_display_html': self._compute_default_customer_html()}) + self.sudo()._set_fiscal_position() self.sudo()._check_modules_to_install() self.sudo()._check_groups_implied() diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py index dc3120f8..58cb4872 100644 --- a/addons/point_of_sale/models/pos_order.py +++ b/addons/point_of_sale/models/pos_order.py @@ -46,9 +46,11 @@ class PosOrder(models.Model): } def _payment_fields(self, ui_paymentline): + payment_date = ui_paymentline['name'] + payment_date = fields.Date.context_today(self, fields.Datetime.from_string(payment_date)) return { 'amount': ui_paymentline['amount'] or 0.0, - 'payment_date': ui_paymentline['name'], + 'payment_date': payment_date, 'statement_id': ui_paymentline['statement_id'], 'payment_name': ui_paymentline.get('note', False), 'journal': ui_paymentline['journal_id'], @@ -173,7 +175,7 @@ class PosOrder(models.Model): 'comment': self.note or '', # considering partner's sale pricelist's currency 'currency_id': self.pricelist_id.currency_id.id, - 'user_id': self.env.uid, + 'user_id': self.user_id.id, } @api.model @@ -258,7 +260,7 @@ class PosOrder(models.Model): line = grouped_data[product_key][0] product = Product.browse(line['product_id']) # In the SO part, the entries will be inverted by function compute_invoice_totals - price_unit = - product._get_anglo_saxon_price_unit() + price_unit = self._get_pos_anglo_saxon_price_unit(product, line['partner_id'], line['quantity']) account_analytic = Analytic.browse(line.get('analytic_account_id')) res = Product._anglo_saxon_sale_move_lines( line['name'], product, product.uom_id, line['quantity'], price_unit, @@ -412,6 +414,18 @@ class PosOrder(models.Model): move.sudo().post() return True + def _get_pos_anglo_saxon_price_unit(self, product, partner_id, quantity): + price_unit = product._get_anglo_saxon_price_unit() + if product._get_invoice_policy() == "delivery": + moves = self.filtered(lambda o: o.partner_id.id == partner_id)\ + .mapped('picking_id.move_lines')\ + .filtered(lambda m: m.product_id.id == product.id)\ + .sorted(lambda x: x.date) + average_price_unit = product._compute_average_price(0, quantity, moves) + price_unit = average_price_unit or price_unit + # In the SO part, the entries will be inverted by function compute_invoice_totals + return - price_unit + def _reconcile_payments(self): for order in self: aml = order.statement_ids.mapped('journal_entry_ids') | order.account_move.line_ids | order.invoice_id.move_id.line_ids @@ -801,7 +815,7 @@ class PosOrder(models.Model): """Create a new payment for the order""" args = { 'amount': data['amount'], - 'date': data.get('payment_date', fields.Date.today()), + 'date': data.get('payment_date', fields.Date.context_today(self)), 'name': self.name + ': ' + (data.get('payment_name', '') or ''), 'partner_id': self.env["res.partner"]._find_accounting_partner(self.partner_id).id or False, } diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index 038632c3..f38e1969 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -38,8 +38,8 @@ class PosSession(models.Model): paid=order.amount_paid, )) order.action_pos_order_done() - orders = session.order_ids.filtered(lambda order: order.state in ['invoiced', 'done']) - orders.sudo()._reconcile_payments() + orders_to_reconcile = session.order_ids.filtered(lambda order: order.state in ['invoiced', 'done'] and order.partner_id) + orders_to_reconcile.sudo()._reconcile_payments() config_id = fields.Many2one( 'pos.config', string='Point of Sale', diff --git a/addons/point_of_sale/models/product.py b/addons/point_of_sale/models/product.py index 2969cac5..05044508 100644 --- a/addons/point_of_sale/models/product.py +++ b/addons/point_of_sale/models/product.py @@ -21,6 +21,23 @@ class ProductTemplate(models.Model): raise UserError(_('You cannot delete a product saleable in point of sale while a session is still opened.')) return super(ProductTemplate, self).unlink() + @api.onchange('sale_ok') + def _onchange_sale_ok(self): + if not self.sale_ok: + self.available_in_pos = False + + +class ProductProduct(models.Model): + _inherit = 'product.product' + + @api.multi + def unlink(self): + product_ctx = dict(self.env.context or {}, active_test=False) + if self.env['pos.session'].search_count([('state', '!=', 'closed')]): + if self.with_context(product_ctx).search_count([('id', 'in', self.ids), ('product_tmpl_id.available_in_pos', '=', True)]): + raise UserError(_('You cannot delete a product saleable in point of sale while a session is still opened.')) + return super(ProductProduct, self).unlink() + class ProductUomCateg(models.Model): _inherit = 'product.uom.categ' diff --git a/addons/point_of_sale/views/point_of_sale_dashboard.xml b/addons/point_of_sale/views/point_of_sale_dashboard.xml index c2a8756b..fd904e52 100644 --- a/addons/point_of_sale/views/point_of_sale_dashboard.xml +++ b/addons/point_of_sale/views/point_of_sale_dashboard.xml @@ -134,7 +134,7 @@ -
+
diff --git a/addons/point_of_sale/views/report_saledetails.xml b/addons/point_of_sale/views/report_saledetails.xml index cf9348ff..1be628d9 100644 --- a/addons/point_of_sale/views/report_saledetails.xml +++ b/addons/point_of_sale/views/report_saledetails.xml @@ -22,7 +22,8 @@ - + +