-
  Create a partner
-
  !record {model: res.partner, id: res_partner_cal}:
    name: Supplier
    supplier: 1
- 
  Create a calendar
- 
  !record {model: resource.calendar, id: res_cal}:
    name: Calendar
    attendance_ids:
      - name: Thursday
        dayofweek: '3'
        hour_from: 08
        hour_to: 09
- 
  Create a product A with orderpoint with a calendar
- 
  !record {model: product.product, id: product_calendar, view: False}:
    name: Calendar Product
    seller_ids:
      - name: res_partner_cal
        delay: 1
    orderpoint_ids:
      - name: Product A Truck
        calendar_id: res_cal
        product_min_qty: 0
        product_max_qty: 10
        warehouse_id: stock.warehouse0
- 
  Create delivery order with product A
- 
  !record {model: stock.picking, id: pick_out_calendar}:
    name: Delivery order for procurement (calendar)
    partner_id: res_partner_cal
    picking_type_id: stock.picking_type_out
    move_lines:
        - product_id: product_calendar
          product_uom_qty: 3.00
          location_id: stock.stock_location_stock
          location_dest_id: stock.stock_location_customers
          procure_method: make_to_stock
- 
  Create other delivery order with product A that we will give a later date afterwards (date between the two) 
- 
  !record {model: stock.picking, id: pick_out_calendar2}:
    name: Delivery order for procurement (calendar2)
    partner_id: res_partner_cal
    picking_type_id: stock.picking_type_out
    move_lines:
        - product_id: product_calendar
          product_uom_qty: 4.00
          location_id: stock.stock_location_stock
          location_dest_id: stock.stock_location_customers
          procure_method: make_to_stock
- 
  Create other delivery order with product A that we will give an even later date, so it should not be taken into account
- 
  !record {model: stock.picking, id: pick_out_calendar3}:
    name: Delivery order for procurement (calendar3)
    partner_id: res_partner_cal
    picking_type_id: stock.picking_type_out
    move_lines:
        - product_id: product_calendar
          product_uom_qty: 11.00
          location_id: stock.stock_location_stock
          location_dest_id: stock.stock_location_customers
          procure_method: make_to_stock
- 
  Put different dates in pickings, confirm them, run schedulers and check that the procurement generated by the orderpoint is taking the calendar into account.  Make sure also that the purchase order creates a datetime.
- 
  !python {model: stock.picking}: |
    move_obj = self.pool.get('stock.move')
    product_obj = self.pool.get('product.product')
    product_obj.write(cr, uid, [ref('product_calendar')], {'route_ids': [(4, ref('purchase.route_warehouse0_buy'))]})
    import datetime
    from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
    today8 = datetime.datetime.today() + datetime.timedelta(days = 7)
    today21 = datetime.datetime.today() + datetime.timedelta(days = 21)
    cal1 = self.browse(cr, uid, ref('pick_out_calendar'))
    cal2 = self.browse(cr, uid, ref('pick_out_calendar2'))
    move_obj.write(cr, uid, cal2.move_lines[0].id, {'date': today8.strftime(DEFAULT_SERVER_DATETIME_FORMAT), 'date_expected': today8.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
    cal3 = self.browse(cr, uid, ref('pick_out_calendar3'))
    move_obj.write(cr, uid, cal3.move_lines[0].id, {'date_expected': today21.strftime(DEFAULT_SERVER_DATETIME_FORMAT), 
                                                    'date': today21.strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
    #We need to confirm pickings and run the schedulers
    self.action_confirm(cr, uid, [cal1.id, cal2.id, cal3.id])
    # Check procurements generated
    proc_obj = self.pool.get("procurement.order")
    proc_obj.run_scheduler(cr, uid)
    proc_ids = proc_obj.search(cr, uid, [('product_id', '=', ref('product_calendar'))])
    assert len(proc_ids) == 1, 'should have one procurement'
    procurement = proc_obj.browse(cr, uid, proc_ids[0])
    assert procurement.product_qty == 17, 'It should have taken the two first pickings into account for the virtual stock for the orderpoint, not the third'
    assert datetime.datetime.strptime(procurement.next_delivery_date, DEFAULT_SERVER_DATETIME_FORMAT).weekday() == 3, 'The next delivery date should be on a Thursday'
    purchase_line_id_date_planned = datetime.datetime.strptime(procurement.purchase_line_id.date_planned, DEFAULT_SERVER_DATETIME_FORMAT).weekday()
    assert purchase_line_id_date_planned == 3, 'Check it has been put on the purchase line also, got %d' %purchase_line_id_date_planned
