-
  I create a draft Purchase Order
-
  !record {model: purchase.order, id: purchase_order_edi_1}:
    partner_id: base.res_partner_3
    location_id: stock.stock_location_3
    pricelist_id: 1
    minimum_planned_date: '2011-08-31'
    order_line:
      - product_id: product.product_product_33
        product_qty: 10.0
        product_uom: 1
        price_unit: 60.0
        name: 'Headset USB'
        date_planned: '2011-08-31'
    order_line:
      - product_id: product.product_product_29
        product_qty: 12.0
        product_uom: 1
        price_unit: 89.0
        name: 'Pen drive, 8GB'
        date_planned: '2011-08-31'

-
  I confirm the purchase order
-
  !workflow {model: purchase.order, ref: purchase_order_edi_1, action: purchase_confirm}
-
  Then I export the purchase order via EDI
-
  !python {model: edi.edi}: |
    import json
    order_pool = self.pool.get('purchase.order')
    order = order_pool.browse(cr, uid, ref("purchase_order_edi_1"))
    edi_doc = self.generate_edi(cr, uid, [order])
    assert isinstance(json.loads(edi_doc)[0], dict), 'EDI doc should be a JSON dict' 
-
  Then I import a sample EDI document of a sale order (v7.0)
-
  !python {model: edi.edi}: |
    purchase_order_pool = self.pool.get('purchase.order')
    edi_document = {
        "__id": "sale:724f9v70-dv70-1v70-8v70-701a04e25v70.sale_order_test", 
        "__module": "sale",
        "__model": "sale.order",
        "__import_module": "purchase",
        "__import_model": "purchase.order",
        "__version": [7,0,0],
        "name": "SO008", 
        "currency": {
                "__id": "base:724f9v70-dv70-1v70-8v70-701a04e25v70.EUR",
                "__module": "base",
                "__model": "res.currency",
                "code": "EUR",
                "symbol": "€",
        },
        "date_order": "2011-09-13 12:00:00", 
        "partner_id": ["sale:724f9v70-dv70-1v70-8v70-701a04e25v70.res_partner_test22", "Junjun wala"],
        "partner_address": {
                "__id": "base:724f9v70-dv70-1v70-8v70-701a04e25v70.res_partner_address_7wdsjasdjh",
                "__module": "base",
                "__model": "res.partner",
                "phone": "(+32).81.81.37.00", 
                "street": "Chaussee de Namur 40", 
                "city": "Gerompont", 
                "zip": "1367", 
                "country_id": ["base:5af1272e-dd26-11e0-b65e-701a04e25543.be", "Belgium"], 
        },
        "company_id": ["base:724f9v70-dv70-1v70-8v70-701a04e25v70.main_company", "Supplier S.A."], 
        "company_address": {
                "__id": "base:724f9v70-dv70-1v70-8v70-701a04e25v70.main_address", 
                "__module": "base",
                "__model": "res.partner",
                "city": "Gerompont", 
                "zip": "1367", 
                "country_id": ["base:724f9v70-dv70-1v70-8v70-701a04e25v70.be", "Belgium"], 
                "phone": "(+32).81.81.37.00", 
                "street": "Chaussee de Namur 40",
                "street2": "mailbox 34",
                "bank_ids": [
                    ["base:724f9v70-dv70-1v70-8v70-701a04e25v70.res_partner_bank-XiwqnxKWzGbp","Guys bank: 123477777-156113"]
                ],
        }, 
        "order_line": [{
            "__id": "sale:724f9v70-dv70-1v70-8v70-701a04e25v70.sale_order_line-LXEqeuI-SSP0", 
            "__module": "sale", 
            "__model": "sale.order.line",
            "__import_module": "purchase", 
            "__import_model": "purchase.order.line", 
            "name": "PC Assemble SC234", 
            "product_uom": ["product:724f9v70-dv70-1v70-8v70-701a04e25v70.product_uom_unit", "Unit"], 
            "product_qty": 1.0,
            "date_planned": "2011-09-30",
            "sequence": 10, 
            "price_unit": 150.0, 
            "product_id": ["product:724f9v70-dv70-1v70-8v70-701a04e25v70.product_product_3", "[PCSC234] PC Assemble SC234"],
        },
        {
            "__id": "sale:724f9v70-dv70-1v70-8v70-701a04e25v70.sale_order_line-LXEqeadasdad", 
            "__module": "sale", 
            "__model": "sale.order.line",
            "__import_module": "purchase", 
            "__import_model": "purchase.order.line", 
            "name": "PC on Demand", 
            "product_uom": ["product:724f9v70-dv70-1v70-8v70-701a04e25v70.product_uom_unit", "Unit"], 
            "product_qty": 10.0, 
            "sequence": 11, 
            "date_planned": "2011-09-15", 
            "price_unit": 20.0, 
            "product_id": ["product:724f9v70-dv70-1v70-8v70-701a04e25v70.product_product_5", "[PC-DEM] PC on Demand"],
        }], 
    }
    new_purchase_order_id = purchase_order_pool.edi_import(cr, uid, edi_document, context=context)
    assert new_purchase_order_id,  'Purchase order import failed'
    order_new = purchase_order_pool.browse(cr, uid, new_purchase_order_id)

    # check bank info on partner
    assert order_new.partner_id.supplier, "Imported partner should be a supplier, as we just imported the document as a purchase order"
    assert len(order_new.partner_id.bank_ids) == 1, "Expected 1 bank entry related to partner"
    bank_info = order_new.partner_id.bank_ids[0]
    assert bank_info.acc_number == "Guys bank: 123477777-156113", 'Expected "Guys bank: 123477777-156113", got %s' % bank_info.acc_number

    assert order_new.pricelist_id.name == 'Default Purchase Pricelist' , "Default Purchase Pricelist was not automatically assigned"
    assert order_new.amount_total == 350, "Amount total is not same"
    assert order_new.amount_untaxed == 350, "untaxed amount is not same"
    assert len(order_new.order_line) == 2, "Purchase order lines number mismatch"
    for purchase_line in order_new.order_line:
        if purchase_line.name == 'PC Assemble SC234':
            assert purchase_line.product_uom.name == "Unit" , "uom is not same"
            assert purchase_line.price_unit == 150 , "unit price is not same, got %s, expected 150"%(purchase_line.price_unit,)
            assert purchase_line.product_qty == 1 , "product qty is not same"
        elif purchase_line.name == 'PC on Demand':
            assert purchase_line.product_uom.name == "Unit" , "uom is not same"
            assert purchase_line.price_unit == 20 , "unit price is not same, got %s, expected 20"%(purchase_line.price_unit,)
            assert purchase_line.product_qty == 10 , "product qty is not same"
        else:
            raise AssertionError('unknown order line: %s' % purchase_line)
-
  "Then I import a sample EDI document of a sale order (v6.1 - to test backwards compatibility)"
-
  !python {model: edi.edi}: |
    purchase_order_pool = self.pool.get('purchase.order')
    edi_document = { 
        "__id": "sale:724f93ec-ddd0-11e0-88ec-701a04e25543.sale_order_test",
        "__module": "sale",
        "__model": "sale.order",
        "__import_module": "purchase",
        "__import_model": "purchase.order",
        "__version": [6,1,0],
        "name": "SO08v61",
        "currency": {
                "__id": "base:724f93ec-ddd0-11e0-88ec-701a04e25543.EUR",
                "__module": "base",
                "__model": "res.currency",
                "code": "EUR",
                "symbol": "€",
        },  
        "date_order": "2011-09-13 12:00:00",
        "partner_id": ["sale:724f93ec-ddd0-11e0-88ec-701a04e25543.res_partner_test22", "Junjun wala"],
        "partner_address": {
                "__id": "base:724f93ec-ddd0-11e0-88ec-701a04e25543.res_partner_address_7wdsjasdjh",
                "__module": "base",
                "__model": "res.partner.address",
                "phone": "(+32).81.81.37.00",
                "street": "Chaussee de Namur 40",
                "city": "Gerompont",
                "zip": "1367",
                "country_id": ["base:5af1272e-dd26-11e0-b65e-701a04e25543.be", "Belgium"],
        },  
        "company_id": ["base:724f93ec-ddd0-11e0-88ec-701a04e25543.main_company", "Supplier S.A."],
        "company_address": {
                "__id": "base:724f93ec-ddd0-11e0-88ec-701a04e25543.main_address",
                "__module": "base",
                "__model": "res.partner.address",
                "city": "Gerompont",
                "zip": "1367",
                "country_id": ["base:724f93ec-ddd0-11e0-88ec-701a04e25543.be", "Belgium"],
                "phone": "(+32).81.81.37.00",
                "street": "Chaussee de Namur 40",
                "street2": "mailbox 34",
                "bank_ids": [
                    ["base:724f93ec-ddd0-11e0-88ec-701a04e25543.res_partner_bank-XiwqnxKWzGbp","Another bank: 123477700-156113"]
                ],  
        },
        "order_line": [{
            "__id": "sale:724f93ec-ddd0-11e0-88ec-701a04e25543.sale_order_line-LXEqeuI-SSP0",
            "__module": "sale",
            "__model": "sale.order.line",
            "__import_module": "purchase",
            "__import_model": "purchase.order.line",
            "name": "Basic PC",
            "product_uom": ["product:724f93ec-ddd0-11e0-88ec-701a04e25543.product_uom_unit", "PCE"],
            "product_qty": 1.0,
            "date_planned": "2011-09-30",
            "sequence": 10,
            "price_unit": 150.0,
            "product_id": ["product:724f93ec-ddd0-11e0-88ec-701a04e25543.product_product_pc1", "[PC1] Basic PC"],
        },  
        {   
            "__id": "sale:724f93ec-ddd0-11e0-88ec-701a04e25543.sale_order_line-LXEqeadasdad",
            "__module": "sale",
            "__model": "sale.order.line",
            "__import_module": "purchase",
            "__import_model": "purchase.order.line",
            "name": "Medium PC",
            "product_uom": ["product:724f93ec-ddd0-11e0-88ec-701a04e25543.product_uom_unit", "PCE"],
            "product_qty": 10.0,
            "sequence": 11,
            "date_planned": "2011-09-15",
            "price_unit": 20.0,
            "product_id": ["product:724f93ec-ddd0-11e0-88ec-701a04e25543.product_product_pc3", "[PC3] Medium PC"],
        }],
    }
    new_purchase_order_id = purchase_order_pool.edi_import(cr, uid, edi_document, context=context)
    assert new_purchase_order_id,  'Purchase order import failed'
    order_new = purchase_order_pool.browse(cr, uid, new_purchase_order_id)

    # check bank info on partner
    assert order_new.partner_id.supplier, "Imported partner should be a supplier, as we just imported the document as a purchase order"
    assert len(order_new.partner_id.bank_ids) == 1, "Expected 1 bank entry related to partner"
    bank_info = order_new.partner_id.bank_ids[0]
    assert bank_info.acc_number == "Another bank: 123477700-156113", 'Expected "Another bank: 123477700-156113", got %s' % bank_info.acc_number

    assert order_new.pricelist_id.name == 'Default Purchase Pricelist' , "Default Purchase Pricelist was not automatically assigned"
    assert order_new.amount_total == 350, "Amount total is not same"
    assert order_new.amount_untaxed == 350, "untaxed amount is not same"
    assert len(order_new.order_line) == 2, "Purchase order lines number mismatch"
    for purchase_line in order_new.order_line:
        if purchase_line.name == 'Basic PC':
            assert purchase_line.product_uom.name == "PCE" , "uom is not same"
            assert purchase_line.price_unit == 150 , "unit price is not same, got %s, expected 150"%(purchase_line.price_unit,)
            assert purchase_line.product_qty == 1 , "product qty is not same"
        elif purchase_line.name == 'Medium PC':
            assert purchase_line.product_uom.name == "PCE" , "uom is not same"
            assert purchase_line.price_unit == 20 , "unit price is not same, got %s, expected 20"%(purchase_line.price_unit,)
            assert purchase_line.product_qty == 10 , "product qty is not same"
        else:
            raise AssertionError('unknown order line: %s' % purchase_line)
