<commit_before># -*- coding: utf-8 -*-

import openerp.addons.decimal_precision as dp
from openerp import fields, models, api, tools

class sell_order_detail(models.Model):
    _name = 'sell.order.detail'
    _description = u'销售明细表'
    _auto = False

    date = fields.Date(u'销售日期')
    order_name = fields.Char(u'销售单据号')
    type = fields.Char(u'业务类型')
    staff_id = fields.Many2one('staff', u'销售员')
    partner_id = fields.Many2one('partner', u'客户')
    goods_code = fields.Char(u'商品编码')
    goods_id = fields.Many2one('goods', u'商品名称')
    attribute = fields.Char(u'属性')
    warehouse_id = fields.Many2one('warehouse', u'仓库')
    qty = fields.Float(u'数量', digits=dp.get_precision('Quantity'))
    uom = fields.Char(u'单位')
    price = fields.Float(u'单价', digits=dp.get_precision('Amount'))
    amount = fields.Float(u'销售收入', digits=dp.get_precision('Amount'))
    tax_amount = fields.Float(u'税额', digits=dp.get_precision('Amount'))
    subtotal = fields.Float(u'价税合计', digits=dp.get_precision('Amount'))
    margin = fields.Float(u'毛利', digits=dp.get_precision('Amount'))
    note = fields.Char(u'备注')

    def init(self, cr):
        tools.drop_view_if_exists(cr, 'sell_order_detail')
        cr.execute("""
            CREATE or REPLACE VIEW sell_order_detail AS (
                SELECT  MIN(wml.id) AS id,
                    wm.date AS date,
                    wm.name AS order_name,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN '销货'
                    ELSE '退货' END) AS type,
                    sd.staff_id AS staff_id,
                    wm.partner_id AS partner_id,
                    goods.code AS goods_code,
                    goods.id AS goods_id,
                    attr.name AS attribute,
                    wh.id AS warehouse_id,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
                        ELSE - wml.goods_qty END) AS qty,
                    uom.name AS uom,
                    wml.price AS price,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
                        ELSE - wml.amount END) AS amount,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.tax_amount
                        ELSE - wml.tax_amount END) AS tax_amount,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.subtotal
                        ELSE - wml.subtotal END) AS subtotal,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
                        ELSE - wml.goods_qty END) * (wml.price - wml.cost) AS margin,
                    wml.note AS note

                FROM wh_move_line AS wml
                    LEFT JOIN wh_move wm ON wml.move_id = wm.id
                    LEFT JOIN partner ON wm.partner_id = partner.id
                    LEFT JOIN goods ON wml.goods_id = goods.id
                    LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
                    LEFT JOIN warehouse AS wh ON wml.warehouse_id = wh.id
                         OR wml.warehouse_dest_id = wh.id
                    LEFT JOIN uom ON goods.uom_id = uom.id
                    LEFT JOIN sell_delivery AS sd ON wm.id = sd.sell_move_id

                WHERE wml.state = 'done'
                  AND wm.origin like 'sell.delivery%%'
                  AND wh.type = 'stock'

                GROUP BY wm.date, wm.name, origin, staff_id, partner_id,
                    goods_code, goods.id, attribute, wh.id, uom,
                    qty, wml.price, wml.amount, tax_amount, subtotal, margin, wml.note
                )
        """)

    @api.multi
    def view_detail(self):
        '''查看明细按钮'''
        order = self.env['sell.delivery'].search([('name', '=', self.order_name)])
        if order:
            if not order.is_return:
                view = self.env.ref('sell.sell_delivery_form')
            else:
                view = self.env.ref('sell.sell_return_form')
            
            return {
                'name': u'销售发货单',
                'view_type': 'form',
                'view_mode': 'form',
                'view_id': False,
                'views': [(view.id, 'form')],
                'res_model': 'sell.delivery',
                'type': 'ir.actions.act_window',
                'res_id': order.id,
            }<commit_msg>[FIX] SQL fail<commit_after># -*- coding: utf-8 -*-

import openerp.addons.decimal_precision as dp
from openerp import fields, models, api, tools

class sell_order_detail(models.Model):
    _name = 'sell.order.detail'
    _description = u'销售明细表'
    _auto = False

    date = fields.Date(u'销售日期')
    order_name = fields.Char(u'销售单据号')
    type = fields.Char(u'业务类型')
    staff_id = fields.Many2one('staff', u'销售员')
    partner_id = fields.Many2one('partner', u'客户')
    goods_code = fields.Char(u'商品编码')
    goods_id = fields.Many2one('goods', u'商品名称')
    attribute = fields.Char(u'属性')
    warehouse_id = fields.Many2one('warehouse', u'仓库')
    qty = fields.Float(u'数量', digits=dp.get_precision('Quantity'))
    uom = fields.Char(u'单位')
    price = fields.Float(u'单价', digits=dp.get_precision('Amount'))
    amount = fields.Float(u'销售收入', digits=dp.get_precision('Amount'))
    tax_amount = fields.Float(u'税额', digits=dp.get_precision('Amount'))
    subtotal = fields.Float(u'价税合计', digits=dp.get_precision('Amount'))
    margin = fields.Float(u'毛利', digits=dp.get_precision('Amount'))
    note = fields.Char(u'备注')

    def init(self, cr):
        tools.drop_view_if_exists(cr, 'sell_order_detail')
        cr.execute("""
            CREATE or REPLACE VIEW sell_order_detail AS (
                SELECT  MIN(wml.id) AS id,
                    wm.date AS date,
                    wm.name AS order_name,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN '销货'
                    ELSE '退货' END) AS type,
                    sd.staff_id AS staff_id,
                    wm.partner_id AS partner_id,
                    goods.code AS goods_code,
                    goods.id AS goods_id,
                    attr.name AS attribute,
                    wh.id AS warehouse_id,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
                        ELSE - wml.goods_qty END) AS qty,
                    uom.name AS uom,
                    wml.price AS price,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
                        ELSE - wml.amount END) AS amount,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.tax_amount
                        ELSE - wml.tax_amount END) AS tax_amount,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.subtotal
                        ELSE - wml.subtotal END) AS subtotal,
                    (CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
                        ELSE - wml.goods_qty END) * (wml.price - wml.cost) AS margin,
                    wml.note AS note

                FROM wh_move_line AS wml
                    LEFT JOIN wh_move wm ON wml.move_id = wm.id
                    LEFT JOIN partner ON wm.partner_id = partner.id
                    LEFT JOIN goods ON wml.goods_id = goods.id
                    LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
                    LEFT JOIN warehouse AS wh ON wml.warehouse_id = wh.id
                         OR wml.warehouse_dest_id = wh.id
                    LEFT JOIN uom ON goods.uom_id = uom.id
                    LEFT JOIN sell_delivery AS sd ON wm.id = sd.sell_move_id

                WHERE wml.state = 'done'
                  AND wm.origin like 'sell.delivery%%'
                  AND wh.type = 'stock'

                GROUP BY wm.date, wm.name, origin, staff_id, partner_id,
                    goods_code, goods.id, attribute, wh.id, uom,
                    qty, wml.price, wml.amount, wml.cost, tax_amount, subtotal, margin, wml.note
                )
        """)

    @api.multi
    def view_detail(self):
        '''查看明细按钮'''
        order = self.env['sell.delivery'].search([('name', '=', self.order_name)])
        if order:
            if not order.is_return:
                view = self.env.ref('sell.sell_delivery_form')
            else:
                view = self.env.ref('sell.sell_return_form')
            
            return {
                'name': u'销售发货单',
                'view_type': 'form',
                'view_mode': 'form',
                'view_id': False,
                'views': [(view.id, 'form')],
                'res_model': 'sell.delivery',
                'type': 'ir.actions.act_window',
                'res_id': order.id,
            }
{'fraction_non_alphanumeric': 0.11127750259186729, 'fraction_numerical': 0.0011519410206197443, 'mean_word_length': 1.6017380881030867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4772473', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2795, 'n_words': 609}