Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- ##############################################################################
- #
- # This module uses OpenERP, Open Source Management Solution Framework.
- # Copyright (C) 2017-Today Sitaram
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # 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 General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>
- #
- ##############################################################################
- from openerp import api, fields, models, _
- import logging
- from openerp.osv import osv
- _logger = logging.getLogger(__name__)
- class split_quotation_line(models.Model):
- _name = 'split.quotation.line'
- quotation_id = fields.Many2one(comodel_name = 'split.quotation', store = True)
- qty = fields.Integer(string = 'Quantity')
- product = fields.Char(string = 'Product')
- uom = fields.Char(string = 'Unit of Measure')
- sol_id = fields.Integer()
- class split_quotation(models.Model):
- _name = "split.quotation"
- sol_rows = fields.One2many(comodel_name='split.quotation.line',
- inverse_name='quotation_id',
- string='Quotation Lines to Split',
- )
- @api.multi
- def split_quotes(self):
- sale_order_ids = []
- sale_order_line_ids_wih_qtys = dict()
- for id in self._context.get('active_ids'):
- current_order_id = self.env['split.quotation.line.temp'].browse(id).old_order_id
- current_line_id = self.env['split.quotation.line.temp'].browse(id).sol_id
- sale_order_ids.append(current_order_id)
- if sale_order_line_ids_wih_qtys.has_key(current_line_id):
- sale_order_line_ids_wih_qtys[current_line_id] += 1
- else:
- sale_order_line_ids_wih_qtys[current_line_id] = 1
- sale_order_id = self.env['sale.order'].browse(sale_order_ids[0])
- qty_dict = dict()
- all_lines_temp = self.env['split.quotation.line.temp'].search([])
- if len(all_lines_temp) <= 1:
- raise osv.except_osv(('Error!'), ("Can't split an order with only 1 quantity!"))
- for line in all_lines_temp:
- current_sol = line.sol_id
- if qty_dict.has_key(current_sol):
- qty_dict[current_sol] += 1
- else:
- qty_dict[current_sol] = 1
- orders_to_split = 0
- active_ids = dict()
- for id in self._context.get('active_ids'):
- orders_to_split += 1
- current_sol = self.env['split.quotation.line.temp'].browse(id).sol_id
- if active_ids.has_key(current_sol):
- active_ids[current_sol] += 1
- else:
- active_ids[current_sol] = 1
- if len(all_lines_temp) == orders_to_split:
- raise osv.except_osv(('Error!'), ("Can't select all the lines in the order to split!"))
- try:
- old_delivery_order = self.env['stock.picking'].search([('origin', '=', sale_order_id.name)])[0]
- except:
- _logger.info('asd')
- cpy_sale = sale_order_id.copy({'order_line': False,
- 'state': sale_order_id.state,
- 'date_confirm':sale_order_id.date_confirm,
- 'date_order': sale_order_id.date_order,
- 'commitment_date': sale_order_id.commitment_date})
- reference = sale_order_id.client_order_ref
- # added partition nr field in SO to keep track of the current numbers
- if reference:
- all_splits = self.env['sale.order'].search([('date_order','=',sale_order_id.date_order)])
- max_value = 0
- for record in all_splits:
- if record.partition_nr > max_value:
- max_value = record.partition_nr
- new_reference = reference[:15] + ' ' + str(max_value + 1)
- cpy_sale.write({'client_order_ref' : new_reference,
- 'partition_nr': max_value+1})
- # Must fix if a second split is made from same order - to keep track of the others made
- else:
- cpy_sale.write({'client_order_ref': sale_order_id.name + ' Part 2',
- 'partition_nr': 2})
- sale_order_id.write({'client_order_ref': sale_order_id.name + ' Part 1',
- 'partition_nr': 1})
- if sale_order_id.state == 'manual':
- cpy_procurement = sale_order_id.procurement_group_id.copy({'name': cpy_sale.name})
- cpy_procurement_line = self.env['procurement.order'].search([('origin', '=', sale_order_id.name)])[0].copy(
- {'origin': cpy_sale.name})
- cpy_sale.write({'procurement_group_id': cpy_procurement.id})
- cpy_delivery_order = old_delivery_order.copy({'move_lines': False,
- 'pack_operation_ids': False,
- 'origin': cpy_sale.name,})
- self._cr.execute(
- "UPDATE stock_picking SET group_id = {} WHERE id = {}".format(cpy_procurement.id,
- cpy_delivery_order.id))
- # cpy_delivery_order.write({'group_id': cpy_procurement.id})
- for sol_id in active_ids.keys():
- if qty_dict[sol_id] > active_ids[sol_id]:
- old_sol = self.env['sale.order.line'].browse(sol_id)
- difference_left = qty_dict[sol_id] - active_ids[sol_id]
- if sale_order_id.state == 'manual':
- # _logger.info("DO %s" % old_delivery_order)
- # for record in old_delivery_order.pack_operation_ids:
- # _logger.info("PACK OPERATION ID %s " %record)
- for record in old_delivery_order.move_lines:
- _logger.info("SOL %s" %record)
- if record.product_id.id == old_sol.product_id.id \
- and record.product_uom_qty == old_sol.product_uom_qty:
- # TODO : Currently updates with last line difference, because this nested loop is run there last
- record.write({'product_uom_qty' : difference_left,
- 'product_uos_qty' : difference_left,
- 'note': 'split'})
- record.copy({'product_uom_qty': active_ids[sol_id],
- 'picking_id' : cpy_delivery_order.id,
- 'origin' : cpy_sale.name,
- 'procurement_id': cpy_procurement_line.id,
- 'group_id': cpy_procurement.id,
- 'note': 'split',
- })
- _logger.info(difference_left)
- old_sol.write({'product_uom_qty':difference_left})
- old_sol.copy({'product_uom_qty' : active_ids[sol_id],
- 'order_id':cpy_sale.id})
- else:
- # #TODO : Find it (change sol_id thing)
- old_sol = self.env['sale.order.line'].browse(sol_id)
- if sale_order_id.state == 'manual':
- for record in old_delivery_order.move_lines:
- _logger.info("SOL %s" % record)
- if record.product_id.id == old_sol.product_id.id \
- and record.product_uom_qty == old_sol.product_uom_qty:
- # TODO : change here to new DO
- sm = self.env['stock.move'].browse(record.id)
- self._cr.execute("DELETE FROM sm_data WHERE sm_id = {}".format(sm.id))
- # sm.write({'picking_id':cpy_delivery_order.id})
- self.env['stock.move'].browse(record.id).picking_id = cpy_delivery_order.id
- self._cr.execute(
- "UPDATE stock_picking SET group_id = {} WHERE id = {}".format(cpy_procurement.id,
- cpy_delivery_order.id))
- self.env['sale.order.line'].browse(sol_id).order_id = cpy_sale.id
- # todo: if equal, use the old logic and remove the entire line
- return {
- 'view_type': 'form',
- 'view_mode': 'form',
- 'res_model': 'sale.order',
- 'target': 'current',
- 'res_id': sale_order_id.id,
- 'type': 'ir.actions.act_window'
- }
- class SaleOrder(models.Model):
- _inherit = "sale.order"
- partition_nr = fields.Integer()
- @api.multi
- def split_quotation(self):
- self._cr.execute("DELETE FROM split_quotation_line_temp")
- #PREPARE ROWS
- order_lines = self.env['sale.order.line'].search([('order_id','=',self.id)])
- products_list = []
- order_lines_list = []
- for line in order_lines:
- # Where SOL has more than 1 QTY create a new line in the temp table
- if line.product_uom_qty > 1:
- n= 0
- while n < line.product_uom_qty:
- products_list.append(line.product_id.id)
- order_lines_list.append(line.id)
- n += 1
- else:
- products_list.append(line.product_id.id)
- order_lines_list.append(line.id)
- for product,line_id in zip(products_list,order_lines_list):
- product_id = self.env['product.product'].browse(product)
- self.env['split.quotation.line.temp'].create({'product_id': product_id.id,
- 'old_order_id':self.id,
- 'sol_id': line_id})
- # CREATE HERE
- imd = self.env['ir.model.data']
- # action = imd.xmlid_to_object('sale.action_product_sale_list')
- # _logger.info(action)
- list_view_id = imd.xmlid_to_res_id('sale.view_order_line_tree')
- _logger.info(list_view_id)
- _logger.info(self.id)
- result = {
- 'view_type': 'form',
- 'view_mode': 'tree',
- 'res_model': 'split.quotation.line.temp',
- 'target': 'current',
- 'type': 'ir.actions.act_window',
- 'view_id': self.env.ref('split_sales_order.split_order_tree_temp').id,
- 'res_id': 1,
- # 'domain': [('order_id', '=', self.id)]
- }
- return result
- # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
- #TODO:
- # create field for reference to main SO
- class SaleOrder(models.Model):
- _inherit = "sale.order.line"
- @api.multi
- def split_quotes(self):
- _logger.info(self._context.get('active_ids'))
- class split_quotation_temp(models.Model):
- _name = 'split.quotation.temp'
- # product = fields.Char(string='Product')
- product_ids = fields.One2many(comodel_name='split.quotation.line.temp',
- inverse_name='quotation_id',
- string='Quotation Lines to Split',)
- # old_order_id = fields.Integer()
- class split_quotation_line_temp(models.Model):
- _name = 'split.quotation.line.temp'
- quotation_id = fields.Many2one(comodel_name='split.quotation.temp')
- product_id = fields.Many2one(comodel_name = "product.product",string='Product')
- old_order_id = fields.Integer()
- sol_id = fields.Integer()
- #TODO: Add boolean if the line is with qty > 1 and fix it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement