You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

235 lines
11 KiB

# -*- coding: utf-8 -*-
#############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2025-TODAY Cybrosys Technologies(<https://www.cybrosys.com>).
# Author: Cybrosys Techno Solutions(<https://www.cybrosys.com>)
#
# You can modify it under the terms of the GNU AFFERO
# GENERAL PUBLIC LICENSE (AGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# (AGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
import math
import re
from odoo import Command, fields, models
from odoo.exceptions import ValidationError
class SplitOrder(models.TransientModel):
""" Split manufacture order by different methods"""
_name = 'split.order'
_description = "Split Orders"
splitting_method = fields.Selection([
('by_no_of_quantity', 'By Number of Quantity'),
('by_no_of_split', 'By Number of Split'),
('split_manually', 'Split Manually')], string="Splitting Method",
required=True, help="To select splitting method")
no_of_quantity = fields.Integer(string="No of Quantity",
help="Number of quantity splitting")
no_of_split = fields.Integer(string="No of Split",
help="Number of splits quantity")
order_id = fields.Many2one('mrp.production', string="Order",
help="Manufacturing production order")
split_manually = fields.Char(string="Splitting Quantities",
help="Can split mrp manually")
work_center_id = fields.Many2one('mrp.workcenter',
string="Work Center",
required=True, help="work center")
works = fields.Boolean(string="Works",
help="For visible work center on wizard")
def action_split_done(self):
""" Split by no of quantity,If no of quantity
is greater than product quantity no action required."""
vals = {
'workcenter_id': self.work_center_id.id,
'product_uom_id': self.order_id.product_uom_id.id,
'name': self.work_center_id.name,
}
order = self.order_id
qty = order.product_qty
no_qty = self.no_of_quantity
no_split = self.no_of_split
manually = self.split_manually
if qty > 1 and no_qty:
if no_qty > qty:
raise ValidationError("Choose Valid Quantity")
elif no_qty == qty:
return False
else:
if order.move_raw_ids and not order.bom_id:
move_raw_ids_list = []
for res in order.move_raw_ids:
uom = math.floor(
((1 / qty) * (qty - no_qty)) * res.product_uom_qty)
by_product_uom = res.product_uom_qty - uom
components = {
'product_id': res.product_id.id,
'product_uom_qty': uom
}
res.update(components)
move_raw_ids_list.append({
'product_id': res.product_id.id,
'product_uom_qty': abs(by_product_uom)
})
self.env['mrp.production'].create({
'product_qty': no_qty,
'product_id': order.product_id.id,
'product_uom_id': order.product_uom_id.id,
'move_raw_ids': [Command.create(data) for data in
move_raw_ids_list],
})
order.write(
{
'product_qty': qty - no_qty,
})
else:
order.write(
{
'product_qty': qty - no_qty,
'workorder_ids': [Command.create(vals)]
})
self.env['mrp.production'].create({
'product_qty': no_qty,
'product_id': order.product_id.id,
'product_uom_id': order.product_uom_id.id,
})
"""Split manufacture order by no of splits,If no of quantity
is greater than product quantity no action required."""
if qty > 1 and no_split:
if no_split > qty:
raise ValidationError("Choose Valid Quantity")
elif no_split == qty:
return False
else:
split = qty // no_split
reminder = qty % no_split
value = qty - reminder
if order.move_raw_ids and not order.bom_id:
mrp_order=False
if value == 0:
return False
else:
for val in range(int(split)):
move_raw_ids_list = []
for rec in order.move_raw_ids:
by_product_split = rec.product_uom_qty // no_split
by_product_reminder = rec.product_uom_qty % no_split
by_product_value = rec.product_uom_qty - by_product_reminder
by_product_split_qty = by_product_value // by_product_split
move_raw_ids_list.append({
'product_id': rec.product_id.id,
'product_uom_qty': by_product_split_qty
})
mrp_order=self.env[
'mrp.production'].create(
{
'product_qty': value // split,
'product_id': order.product_id.id,
'workorder_ids': [Command.create(vals)],
'move_raw_ids': [Command.create(data)
for data in
move_raw_ids_list],
})
if reminder > 0:
order.write(
{
'product_qty': reminder,
'workorder_ids': [Command.create(vals)]
})
else:
order.unlink()
if mrp_order:
return {
"type": "ir.actions.act_window",
"res_model": "mrp.production",
"res_id": mrp_order.id,
"view_mode": "form",
"views": [[False, "form"]],
"target": "current",
}
else:
split = qty // no_split
reminder = qty % no_split
value = qty - reminder
if value == 0:
return False
else:
for val in range(int(split)):
create_mo_split = self.env['mrp.production'].create(
{
'product_qty': value // split,
'product_id': order.product_id.id
})
create_mo_split.write(
{'workorder_ids': [Command.create(vals)]})
if reminder > 0:
order.write(
{
'product_qty': reminder,
'workorder_ids': [Command.create(vals)]
})
else:
order.unlink()
""" Split manufacture order by Manually"""
if qty > 1 and manually:
split_value = manually.split(",")
total = sum(map(int, split_value))
split_sum = 0
val = []
split_manually = self.split_manually
pattern = r'^[\d,]+$'
validation = re.match(pattern, split_manually) is not None
if total<qty:
raise ValidationError("The total of split must be equal to the order quantity")
if validation:
for rec in split_value:
split_sum += int(rec)
val.append(int(rec))
for record in val:
if record == 0:
val.remove(0)
if split_sum > qty or split_sum == 0:
raise ValidationError("Choose Valid Quantity")
if order.move_raw_ids and not order.bom_id:
for rec in val:
move_raw_ids_list = []
for c in order.move_raw_ids:
move_raw_ids_list.append({
'product_id': c.product_id.id,
'product_uom_qty': round(
(1 / split_sum) * c.product_uom_qty * rec)
})
self.env['mrp.production'].create({
'product_qty': int(rec),
'product_id': order.product_id.id,
'move_raw_ids': [Command.create(data) for data in
move_raw_ids_list],
})
order.unlink()
else:
order.write(
{
'product_qty': int(val[0]),
'workorder_ids': [Command.create(vals)],
})
val.pop(0)
for rec in val:
self.env['mrp.production'].create({
'product_qty': int(rec),
'product_id': order.product_id.id
})
else:
raise ValidationError("Enter the correct value")