Guest User

Untitled

a guest
Dec 3rd, 2019
236
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. #    This module uses OpenERP, Open Source Management Solution Framework.
  5. #    Copyright (C) 2017-Today Sitaram
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>
  19. #
  20. ##############################################################################
  21.  
  22. from openerp import api, fields, models, _
  23. import logging
  24. from openerp.osv import osv
  25. _logger = logging.getLogger(__name__)
  26.  
  27.  
  28.  
  29. class split_quotation_line(models.Model):
  30.     _name = 'split.quotation.line'
  31.  
  32.     quotation_id = fields.Many2one(comodel_name = 'split.quotation', store = True)
  33.     qty = fields.Integer(string = 'Quantity')
  34.     product = fields.Char(string = 'Product')
  35.     uom = fields.Char(string = 'Unit of Measure')
  36.     sol_id = fields.Integer()
  37.  
  38. class split_quotation(models.Model):
  39.     _name = "split.quotation"
  40.  
  41.  
  42.     sol_rows = fields.One2many(comodel_name='split.quotation.line',
  43.                                              inverse_name='quotation_id',
  44.                                              string='Quotation Lines to Split',
  45.                                )
  46.     @api.multi
  47.     def split_quotes(self):
  48.  
  49.         sale_order_ids = []
  50.         sale_order_line_ids_wih_qtys = dict()
  51.         for id in self._context.get('active_ids'):
  52.             current_order_id = self.env['split.quotation.line.temp'].browse(id).old_order_id
  53.             current_line_id = self.env['split.quotation.line.temp'].browse(id).sol_id
  54.             sale_order_ids.append(current_order_id)
  55.             if sale_order_line_ids_wih_qtys.has_key(current_line_id):
  56.                 sale_order_line_ids_wih_qtys[current_line_id] += 1
  57.             else:
  58.                 sale_order_line_ids_wih_qtys[current_line_id] = 1
  59.  
  60.         sale_order_id = self.env['sale.order'].browse(sale_order_ids[0])
  61.  
  62.         qty_dict = dict()
  63.         all_lines_temp = self.env['split.quotation.line.temp'].search([])
  64.         if len(all_lines_temp) <= 1:
  65.             raise osv.except_osv(('Error!'), ("Can't split an order with only 1 quantity!"))
  66.  
  67.         for line in all_lines_temp:
  68.             current_sol = line.sol_id
  69.             if qty_dict.has_key(current_sol):
  70.                 qty_dict[current_sol] += 1
  71.             else:
  72.                 qty_dict[current_sol] = 1
  73.  
  74.         orders_to_split = 0
  75.         active_ids = dict()
  76.         for id in self._context.get('active_ids'):
  77.             orders_to_split += 1
  78.             current_sol = self.env['split.quotation.line.temp'].browse(id).sol_id
  79.             if active_ids.has_key(current_sol):
  80.                 active_ids[current_sol] += 1
  81.             else:
  82.                 active_ids[current_sol] = 1
  83.  
  84.         if len(all_lines_temp) == orders_to_split:
  85.             raise osv.except_osv(('Error!'), ("Can't select all the lines in the order to split!"))
  86.  
  87.         try:
  88.             old_delivery_order = self.env['stock.picking'].search([('origin', '=', sale_order_id.name)])[0]
  89.  
  90.         except:
  91.             _logger.info('asd')
  92.  
  93.  
  94.  
  95.         cpy_sale = sale_order_id.copy({'order_line': False,
  96.                                        'state': sale_order_id.state,
  97.                                        'date_confirm':sale_order_id.date_confirm,
  98.                                        'date_order': sale_order_id.date_order,
  99.                                        'commitment_date': sale_order_id.commitment_date})
  100.  
  101.         reference = sale_order_id.client_order_ref
  102.  
  103.         # added partition nr field in SO to keep track of the current numbers
  104.  
  105.         if reference:
  106.             all_splits = self.env['sale.order'].search([('date_order','=',sale_order_id.date_order)])
  107.             max_value = 0
  108.             for record in all_splits:
  109.                 if record.partition_nr > max_value:
  110.                     max_value = record.partition_nr
  111.             new_reference = reference[:15] + ' ' + str(max_value + 1)
  112.             cpy_sale.write({'client_order_ref' : new_reference,
  113.                             'partition_nr': max_value+1})
  114.             # Must fix if a second split is made from same order - to keep track of the others made
  115.         else:
  116.             cpy_sale.write({'client_order_ref': sale_order_id.name + ' Part 2',
  117.                             'partition_nr': 2})
  118.             sale_order_id.write({'client_order_ref': sale_order_id.name + ' Part 1',
  119.                                  'partition_nr': 1})
  120.  
  121.         if sale_order_id.state == 'manual':
  122.             cpy_procurement = sale_order_id.procurement_group_id.copy({'name': cpy_sale.name})
  123.  
  124.             cpy_procurement_line = self.env['procurement.order'].search([('origin', '=', sale_order_id.name)])[0].copy(
  125.                 {'origin': cpy_sale.name})
  126.  
  127.             cpy_sale.write({'procurement_group_id': cpy_procurement.id})
  128.             cpy_delivery_order = old_delivery_order.copy({'move_lines': False,
  129.                                                           'pack_operation_ids': False,
  130.                                                           'origin': cpy_sale.name,})
  131.             self._cr.execute(
  132.                 "UPDATE stock_picking SET group_id = {} WHERE id = {}".format(cpy_procurement.id,
  133.                                                                               cpy_delivery_order.id))
  134.  
  135.             # cpy_delivery_order.write({'group_id': cpy_procurement.id})
  136.  
  137.         for sol_id in active_ids.keys():
  138.             if qty_dict[sol_id] > active_ids[sol_id]:
  139.                 old_sol = self.env['sale.order.line'].browse(sol_id)
  140.                 difference_left = qty_dict[sol_id] - active_ids[sol_id]
  141.                 if sale_order_id.state == 'manual':
  142.                     # _logger.info("DO %s" % old_delivery_order)
  143.                     # for record in old_delivery_order.pack_operation_ids:
  144.                     #     _logger.info("PACK OPERATION ID %s " %record)
  145.                     for record in old_delivery_order.move_lines:
  146.                         _logger.info("SOL %s" %record)
  147.                         if record.product_id.id == old_sol.product_id.id \
  148.                                 and record.product_uom_qty == old_sol.product_uom_qty:
  149.                             # TODO : Currently updates with last line difference, because this nested loop is run there last
  150.                             record.write({'product_uom_qty' : difference_left,
  151.                                           'product_uos_qty' : difference_left,
  152.                                           'note': 'split'})
  153.  
  154.                             record.copy({'product_uom_qty': active_ids[sol_id],
  155.                                          'picking_id' : cpy_delivery_order.id,
  156.                                          'origin' : cpy_sale.name,
  157.                                          'procurement_id': cpy_procurement_line.id,
  158.                                          'group_id': cpy_procurement.id,
  159.                                          'note': 'split',
  160.                                          })
  161.  
  162.                 _logger.info(difference_left)
  163.                 old_sol.write({'product_uom_qty':difference_left})
  164.                 old_sol.copy({'product_uom_qty' : active_ids[sol_id],
  165.                               'order_id':cpy_sale.id})
  166.             else:
  167.                 # #TODO : Find it (change sol_id thing)
  168.                 old_sol = self.env['sale.order.line'].browse(sol_id)
  169.                 if sale_order_id.state == 'manual':
  170.                     for record in old_delivery_order.move_lines:
  171.                         _logger.info("SOL %s" % record)
  172.                         if record.product_id.id == old_sol.product_id.id \
  173.                                 and record.product_uom_qty == old_sol.product_uom_qty:
  174.                             # TODO : change here to new DO
  175.                             sm = self.env['stock.move'].browse(record.id)
  176.                             self._cr.execute("DELETE FROM sm_data WHERE sm_id = {}".format(sm.id))
  177.                             # sm.write({'picking_id':cpy_delivery_order.id})
  178.                             self.env['stock.move'].browse(record.id).picking_id = cpy_delivery_order.id
  179.                             self._cr.execute(
  180.                                 "UPDATE stock_picking SET group_id = {} WHERE id = {}".format(cpy_procurement.id,
  181.                                                                                               cpy_delivery_order.id))
  182.                 self.env['sale.order.line'].browse(sol_id).order_id = cpy_sale.id
  183.                 # todo: if equal, use the old logic and remove the entire line
  184.  
  185.         return {
  186.             'view_type': 'form',
  187.             'view_mode': 'form',
  188.             'res_model': 'sale.order',
  189.             'target': 'current',
  190.             'res_id': sale_order_id.id,
  191.             'type': 'ir.actions.act_window'
  192.         }
  193.  
  194. class SaleOrder(models.Model):
  195.     _inherit = "sale.order"
  196.  
  197.     partition_nr = fields.Integer()
  198.  
  199.     @api.multi
  200.     def split_quotation(self):
  201.         self._cr.execute("DELETE FROM split_quotation_line_temp")
  202.  
  203.         #PREPARE ROWS
  204.         order_lines = self.env['sale.order.line'].search([('order_id','=',self.id)])
  205.         products_list = []
  206.         order_lines_list = []
  207.         for line in order_lines:
  208.             # Where SOL has more than 1 QTY create a new line in the temp table
  209.             if line.product_uom_qty > 1:
  210.                 n= 0
  211.                 while n < line.product_uom_qty:
  212.                     products_list.append(line.product_id.id)
  213.                     order_lines_list.append(line.id)
  214.                     n += 1
  215.             else:
  216.                 products_list.append(line.product_id.id)
  217.                 order_lines_list.append(line.id)
  218.  
  219.         for product,line_id in zip(products_list,order_lines_list):
  220.             product_id = self.env['product.product'].browse(product)
  221.             self.env['split.quotation.line.temp'].create({'product_id': product_id.id,
  222.                                                           'old_order_id':self.id,
  223.                                                           'sol_id': line_id})
  224.         # CREATE HERE
  225.  
  226.         imd = self.env['ir.model.data']
  227.         # action = imd.xmlid_to_object('sale.action_product_sale_list')
  228.         # _logger.info(action)
  229.  
  230.         list_view_id = imd.xmlid_to_res_id('sale.view_order_line_tree')
  231.         _logger.info(list_view_id)
  232.         _logger.info(self.id)
  233.  
  234.         result = {
  235.             'view_type': 'form',
  236.             'view_mode': 'tree',
  237.             'res_model': 'split.quotation.line.temp',
  238.             'target': 'current',
  239.             'type': 'ir.actions.act_window',
  240.             'view_id': self.env.ref('split_sales_order.split_order_tree_temp').id,
  241.             'res_id': 1,
  242.             # 'domain': [('order_id', '=', self.id)]
  243.         }
  244.         return result
  245.  
  246. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
  247.  
  248.     #TODO:
  249.     # create field for reference to main SO
  250.  
  251.  
  252. class SaleOrder(models.Model):
  253.     _inherit = "sale.order.line"
  254.  
  255.     @api.multi
  256.     def split_quotes(self):
  257.         _logger.info(self._context.get('active_ids'))
  258.  
  259. class split_quotation_temp(models.Model):
  260.     _name = 'split.quotation.temp'
  261.  
  262.     # product = fields.Char(string='Product')
  263.     product_ids = fields.One2many(comodel_name='split.quotation.line.temp',
  264.                                              inverse_name='quotation_id',
  265.                                              string='Quotation Lines to Split',)
  266.     # old_order_id = fields.Integer()
  267.  
  268. class split_quotation_line_temp(models.Model):
  269.     _name = 'split.quotation.line.temp'
  270.  
  271.     quotation_id = fields.Many2one(comodel_name='split.quotation.temp')
  272.     product_id = fields.Many2one(comodel_name = "product.product",string='Product')
  273.     old_order_id = fields.Integer()
  274.     sol_id = fields.Integer()
  275.     #TODO: Add boolean if the line is with qty > 1 and fix it
RAW Paste Data