text: # -*- coding: utf-8 -*-
  ##############################################################################
  #
  #    OpenERP, Open Source Management Solution
  #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  #
  #    This program is free software: you can redistribute it and/or modify
  #    it under the terms of the GNU Affero 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 Affero General Public License for more details.
  #
  #    You should have received a copy of the GNU Affero General Public License
  #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  #
  ##############################################################################
  
  from osv import fields, osv
  from openerp import netsvc
  
  class sale_order(osv.osv):
  
      def _prepare_order_picking(self, cr, uid, order, context=None):
          res = super(sale_order, self)._prepare_order_picking(cr, uid, order, context=context)
          res['x_shipper'] = order.x_shipper
          return res
  
      def _is_bangkok_customer(self, cursor, user, ids, name, arg, context=None):
          res = {}
          cursor.execute("select pick.id, (case when bkk.id is not null then true else false end) is_bkk \
                          from stock_picking pick left outer join \
                          (select distinct p.id from res_partner p, res_partner_res_partner_category_rel r \
                          where r.partner_id = p.id and r.category_id in \
                          (select id from res_partner_category categ where name like %s or name ilike %s)) bkk \
                          on pick.partner_id = bkk.id \
                          where pick.id in %s", ('%กรุงเทพ%', '%bangkok%', tuple(ids),))
          rs = cursor.fetchall()
          for r in rs:
              res[r[0]] = r[1]
          return res
  
      _inherit = 'sale.order'
  
      _columns = {
          'x_shipper': fields.char('Shipper Text'),
      }
  
      # kittiu. This is a complete overwrite of sale_stock.sale_order._create_pickings_and_procurements
      def _create_pickings_and_procurements(self, cr, uid, order, order_lines, picking_id=False, context=None):
          """Create the required procurements to supply sales order lines, also connecting
          the procurements to appropriate stock moves in order to bring the goods to the
          sales order's requested location.
  
          If ``picking_id`` is provided, the stock moves will be added to it, otherwise
          a standard outgoing picking will be created to wrap the stock moves, as returned
          by :meth:`~._prepare_order_picking`.
  
          Modules that wish to customize the procurements or partition the stock moves over
          multiple stock pickings may override this method and call ``super()`` with
          different subsets of ``order_lines`` and/or preset ``picking_id`` values.
  
          :param browse_record order: sales order to which the order lines belong
          :param list(browse_record) order_lines: sales order line records to procure
          :param int picking_id: optional ID of a stock picking to which the created stock moves
                                 will be added. A new picking will be created if ommitted.
          :return: True
          """
          move_obj = self.pool.get('stock.move')
          picking_obj = self.pool.get('stock.picking.out') # kittiu: changed from stock.picking -> stock.picking.out. To make sure it have functionality of this object in this addon.
          procurement_obj = self.pool.get('procurement.order')
          proc_ids = []
  
          for line in order_lines:
              if line.state == 'done':
                  continue
  
              date_planned = self._get_date_planned(cr, uid, order, line, order.date_order, context=context)
  
              if line.product_id:
                  if line.product_id.type in ('product', 'consu'):
                      if not picking_id:
                          picking_id = picking_obj.create(cr, uid, self._prepare_order_picking(cr, uid, order, context=context))
                          # start kittiu
                          picking_obj.write(cr, uid, [picking_id], {'po_reference': order.po_reference or False,
                                                            'po_date': order.po_date or False
                                                            }, context)
                          # end kittiu
                      move_id = move_obj.create(cr, uid, self._prepare_order_line_move(cr, uid, order, line, picking_id, date_planned, context=context))
                  else:
                      # a service has no stock move
                      move_id = False
  
                  proc_id = procurement_obj.create(cr, uid, self._prepare_order_line_procurement(cr, uid, order, line, move_id, date_planned, context=context))
                  proc_ids.append(proc_id)
                  line.write({'procurement_id': proc_id})
                  self.ship_recreate(cr, uid, order, line, move_id, proc_id)
  
          wf_service = netsvc.LocalService("workflow")
          if picking_id:
              wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
          for proc_id in proc_ids:
              wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
  
          val = {}
          if order.state == 'shipping_except':
              val['state'] = 'progress'
              val['shipped'] = False
  
              if (order.order_policy == 'manual'):
                  for line in order.order_line:
                      if (not line.invoiced) and (line.state not in ('cancel', 'draft')):
                          val['state'] = 'manual'
                          break
          order.write(val)
          return True
  # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
  