<commit_before># -*- coding: utf-8 -*-
# © <2016> <<NAME>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import api, _, fields, models
import openerp.addons.decimal_precision as dp
from openerp.exceptions import UserError


class MrpSegment(models.Model):
    _name = "mrp.segment"
    _inherit = ['mail.thread']
    _description = "MRP Segment"
    _rec_name = 'folio'

    def _default_stock_location(self):
        try:
            warehouse = self.env['ir.model.data'].get_object(
                'stock', 'warehouse0')
            return warehouse.lot_stock_id.id
        except:
            return False

    name = fields.Char(
        string=_('Segment Reference'),
        required=True,
        readonly=True,
        states={'draft': [('readonly', False)]},
        track_visibility='always',
        help=_('Segment Name.'))

    folio = fields.Char(
        string='Folio',
        required=True,
        readonly=True,
        copy=False,
        track_visibility='always',
        default='new',
    )

    date = fields.Datetime(
        string=_('Segment Date'),
        required=True,
        readonly=True,
        default=fields.Datetime.now,
        help=_('The date that will be used for the segment.'))

    state = fields.Selection(
        [('draft', 'Draft'),
         ('cancel', 'Cancelled'),
         ('construction', 'In Construction'),
         ('confirm', 'In Progress'),
         ('done', 'Validated')],
        string=_('Status'),
        readonly=True,
        select=True,
        default='draft',
        track_visibility='onchange',
        copy=False)

    location_id = fields.Many2one(
        'stock.location',
        string=_('Segment Location'),
        required=True,
        readonly=True,
        states={'draft': [('readonly', False)]},
        track_visibility='onchange',
        default=_default_stock_location)

    company_id = fields.Many2one(
        'res.company',
        string=_('Company'),
        required=True,
        select=True,
        readonly=True,
        default=lambda self: self.env.user.company_id,
        states={'draft': [('readonly', False)]})

    line_ids = fields.One2many(
        'mrp.segment.line',
        'segment_id',
        string=_('Manufacturing Order Line'),
        readonly=False,
        states={'done': [('readonly', True)]},
        help=_("Segment Lines."),
        copy=True,
        ondelete='cascade')

    product_lines_ids = fields.One2many(
        'mrp.production.product.line',
        'production_id',
        compute='_compute_product_lines_ids',
        string='Scheduled goods',
    )

    @api.depends('line_ids.mrp_production_id')
    def _compute_product_lines_ids(self):
        product_lines = []
        for line in self.line_ids:
            for pro_lin in line.mrp_production_id.product_lines:
                product_lines.append(pro_lin.id)
        self.product_lines_ids = product_lines

    _sql_constraints = [
        ('folio_uniq', 'unique (folio)',
         'This field must be unique!')
    ]

    @api.model
    def create(self, vals):
        if vals.get('folio', 'New') == 'New':
            vals['folio'] = self.env['ir.sequence'].next_by_code(
                'mrp.segment') or '/'
        return super(MrpSegment, self).create(vals)

    @api.multi
    def prepare_segment(self):
        for segment in self:
            line_ids = [line.id for line in segment.line_ids]
            if not line_ids:
                vals = self._get_segment_lines()

                for production_line in vals:
                    self.env['mrp.segment.line'].create(production_line)

        return self.write({'state': 'construction'})

    def _get_segment_lines(self):
        domain = [('segment_line_ids', '=', False),
                  ('location_dest_id', '=', self.location_id.id),
                  ('state', 'in', ["confirmed", "ready"])]

        segment_lines = self.env['mrp.production'].search(domain)

        vals = []
        for produ in segment_lines:
            product_line = dict(
                (fn, 0.0) for fn in [
                    'segment_id', 'mrp_production_id',
                    'product_id', 'sale_name', 'product_qty',
                    'quantity'])

            product_line['segment_id'] = self.id
            product_line['mrp_production_id'] = produ.id
            product_line['product_id'] = produ.product_id.id
            product_line['sale_name'] = produ.origin
            product_line['product_qty'] = produ.product_qty
            product_line['quantity'] = 0
            vals.append(product_line)
        return vals

    @api.multi
    def process_segment(self):
        produce_obj = self.env['mrp.production']
        for produ in self.line_ids:
            if produ.quantity > 0:
                produce_obj.action_produce(produ.mrp_production_id.id,
                                           produ.quantity,
                                           'consume_produce',
                                           )
        done = True
        for produ in self.line_ids:
            if produ.manufacture_qty > 0:
                done = False
            produ.quantity = 0
        if done:
            self.state = 'done'
        return True

    @api.multi
    def validate_segment(self):
        procurement_obj = self.env['procurement.order']
        picking_obj = self.env['stock.picking']
        move_obj = self.env['stock.move']
        purchase_obj = self.env['purchase.order']
        for segment in self:
            for line in segment.line_ids:
                procurement = procurement_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for proc in procurement:
                    proc.related_segment += segment.folio + ', '
                picking = picking_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for pick in picking:
                    pick.related_segment += segment.folio + ', '
                move = move_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for mov in move:
                    mov.related_segment += segment.folio + ', '
                purchase = purchase_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for pur in purchase:
                    pur.related_segment += segment.folio + ', '
                sale = line.mrp_production_id.sale_id
                if not sale.related_segment:
                    sale.related_segment = ''
                if segment.folio not in sale.related_segment:
                    sale.related_segment += segment.folio + ', '

        return self.write({'state': 'confirm'})<commit_msg>[IMP] valid sale_order in mrp_production<commit_after># -*- coding: utf-8 -*-
# © <2016> <<NAME>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import api, _, fields, models
import openerp.addons.decimal_precision as dp
from openerp.exceptions import UserError


class MrpSegment(models.Model):
    _name = "mrp.segment"
    _inherit = ['mail.thread']
    _description = "MRP Segment"
    _rec_name = 'folio'

    def _default_stock_location(self):
        try:
            warehouse = self.env['ir.model.data'].get_object(
                'stock', 'warehouse0')
            return warehouse.lot_stock_id.id
        except:
            return False

    name = fields.Char(
        string=_('Segment Reference'),
        required=True,
        readonly=True,
        states={'draft': [('readonly', False)]},
        track_visibility='always',
        help=_('Segment Name.'))

    folio = fields.Char(
        string='Folio',
        required=True,
        readonly=True,
        copy=False,
        track_visibility='always',
        default='new',
    )

    date = fields.Datetime(
        string=_('Segment Date'),
        required=True,
        readonly=True,
        default=fields.Datetime.now,
        help=_('The date that will be used for the segment.'))

    state = fields.Selection(
        [('draft', 'Draft'),
         ('cancel', 'Cancelled'),
         ('construction', 'In Construction'),
         ('confirm', 'In Progress'),
         ('done', 'Validated')],
        string=_('Status'),
        readonly=True,
        select=True,
        default='draft',
        track_visibility='onchange',
        copy=False)

    location_id = fields.Many2one(
        'stock.location',
        string=_('Segment Location'),
        required=True,
        readonly=True,
        states={'draft': [('readonly', False)]},
        track_visibility='onchange',
        default=_default_stock_location)

    company_id = fields.Many2one(
        'res.company',
        string=_('Company'),
        required=True,
        select=True,
        readonly=True,
        default=lambda self: self.env.user.company_id,
        states={'draft': [('readonly', False)]})

    line_ids = fields.One2many(
        'mrp.segment.line',
        'segment_id',
        string=_('Manufacturing Order Line'),
        readonly=False,
        states={'done': [('readonly', True)]},
        help=_("Segment Lines."),
        copy=True,
        ondelete='cascade')

    product_lines_ids = fields.One2many(
        'mrp.production.product.line',
        'production_id',
        compute='_compute_product_lines_ids',
        string='Scheduled goods',
    )

    @api.depends('line_ids.mrp_production_id')
    def _compute_product_lines_ids(self):
        product_lines = []
        for line in self.line_ids:
            for pro_lin in line.mrp_production_id.product_lines:
                product_lines.append(pro_lin.id)
        self.product_lines_ids = product_lines

    _sql_constraints = [
        ('folio_uniq', 'unique (folio)',
         'This field must be unique!')
    ]

    @api.model
    def create(self, vals):
        if vals.get('folio', 'New') == 'New':
            vals['folio'] = self.env['ir.sequence'].next_by_code(
                'mrp.segment') or '/'
        return super(MrpSegment, self).create(vals)

    @api.multi
    def prepare_segment(self):
        for segment in self:
            line_ids = [line.id for line in segment.line_ids]
            if not line_ids:
                vals = self._get_segment_lines()

                for production_line in vals:
                    self.env['mrp.segment.line'].create(production_line)

        return self.write({'state': 'construction'})

    def _get_segment_lines(self):
        domain = [('segment_line_ids', '=', False),
                  ('location_dest_id', '=', self.location_id.id),
                  ('state', 'in', ["confirmed", "ready"])]

        segment_lines = self.env['mrp.production'].search(domain)

        vals = []
        for produ in segment_lines:
            product_line = dict(
                (fn, 0.0) for fn in [
                    'segment_id', 'mrp_production_id',
                    'product_id', 'sale_name', 'product_qty',
                    'quantity'])

            product_line['segment_id'] = self.id
            product_line['mrp_production_id'] = produ.id
            product_line['product_id'] = produ.product_id.id
            product_line['sale_name'] = produ.origin
            product_line['product_qty'] = produ.product_qty
            product_line['quantity'] = 0
            vals.append(product_line)
        return vals

    @api.multi
    def process_segment(self):
        produce_obj = self.env['mrp.production']
        for produ in self.line_ids:
            if produ.quantity > 0:
                produce_obj.action_produce(produ.mrp_production_id.id,
                                           produ.quantity,
                                           'consume_produce',
                                           )
        done = True
        for produ in self.line_ids:
            if produ.manufacture_qty > 0:
                done = False
            produ.quantity = 0
        if done:
            self.state = 'done'
        return True

    @api.multi
    def validate_segment(self):
        procurement_obj = self.env['procurement.order']
        picking_obj = self.env['stock.picking']
        move_obj = self.env['stock.move']
        purchase_obj = self.env['purchase.order']
        for segment in self:
            for line in segment.line_ids:
                procurement = procurement_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for proc in procurement:
                    proc.related_segment += segment.folio + ', '
                picking = picking_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for pick in picking:
                    pick.related_segment += segment.folio + ', '
                move = move_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for mov in move:
                    mov.related_segment += segment.folio + ', '
                purchase = purchase_obj.search([
                    ('origin', 'like', line.mrp_production_id.name)])
                for pur in purchase:
                    pur.related_segment += segment.folio + ', '
                sale = line.mrp_production_id.sale_id
                if sale:
                    if not sale.related_segment:
                        sale.related_segment = ' '
                    if segment.folio not in sale.related_segment:
                        sale.related_segment += segment.folio + ', '

        return self.write({'state': 'confirm'})
{'fraction_non_alphanumeric': 0.12154331401745765, 'fraction_numerical': 0.002640651360668965, 'mean_word_length': 1.8345114345114346, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '344026', 'n_tokens_mistral': 3921, 'n_tokens_neox': 3766, 'n_words': 755}