-
  I create a draft Sale Order
-
  !record {model: sale.order, id: sale_order_edi_1}:
    partner_id: base.res_partner_2
    partner_invoice_id: base.res_partner_address_3
    partner_shipping_id: base.res_partner_address_3
    pricelist_id: 1
    order_line:
      - product_id: product.product_product_3
        product_uom_qty: 1.0
        product_uom: 1
        price_unit: 150.0
        name: 'PC Assemble SC234'
    order_line:
      - product_id: product.product_product_5
        product_uom_qty: 10.0
        product_uom: 1
        price_unit: 200.0
        name: 'PC Assemble + Custom (PC on Demand)'
-
  I confirm the sale order
-
  !workflow {model: sale.order, ref: sale_order_edi_1, action: order_confirm}
-
  Then I export the sale order via EDI
-
  !python {model: edi.edi}: |
    import json
    sale_order = self.pool.get('sale.order')
    so = sale_order.browse(cr, uid, ref("sale_order_edi_1"))
    edi_doc = self.generate_edi(cr, uid, [so])
    assert isinstance(json.loads(edi_doc)[0], dict), 'EDI doc should be a JSON dict' 
-
  "Then I import a sample EDI document of a purchase order (v7.0)"
-
  !python {model: edi.edi}: |
    sale_order_pool = self.pool.get('sale.order')
    edi_document = {
        "__id": "purchase:5af12v70-dv70-1v70-bv70-701a04e25v70.purchase_order_test",
        "__module": "purchase",
        "__model": "purchase.order",
        "__import_module": "sale",
        "__import_model": "sale.order",
        "__version": [7,0,0],
        "name": "PO00011",
        "date_order": "2011-09-12",
        "currency": {
                "__id": "base:5af12v70-dv70-1v70-bv70-701a04e25v70.EUR",
                "__module": "base",
                "__model": "res.currency",
                "code": "EUR",
                "symbol": "€",
        },
        "company_id": ["base:5af12v70-dv70-1v70-bv70-701a04e25v70.main_company", "Client S.A."],
        "company_address": {
                "__id": "base:5af12v70-dv70-1v70-bv70-701a04e25v70.some_address",
                "__module": "base",
                "__model": "res.partner",
                "phone": "(+32).81.81.37.00",
                "street": "Chaussee de Namur 40",
                "city": "Gerompont",
                "zip": "1367",
                "country_id": ["base:5af12v70-dv70-1v70-bv70-701a04e25v70.be", "Belgium"],
                "bank_ids": [
                    ["base:5af12v70-dv70-1v70-bv70-701a04e25v70.res_partner_bank-adaWadsadasdDJzGbp","Another bank: 032465700-156700"]
                ],
        },
        "partner_id": ["purchase:5af12v70-dv70-1v70-bv70-701a04e25v70.res_partner_test20", "jones white"],
        "order_line": [{
                "__id": "purchase:5af12v70-dv70-1v70-bv70-701a04e25v70.purchase_order_line-AlhsVDZGoKvJ",
                "__module": "purchase",
                "__model": "purchase.order.line",
                "__import_module": "sale",
                "__import_model": "sale.order.line",
                "name": "PC Assemble SC234",
                "price_unit": 150.0,
                "product_id": ["product:5af12v70-dv70-1v70-bv70-701a04e25v70.product_product_3", "[PCSC234] PC Assemble SC234"],
                "product_qty": 1.0,
                "product_uom": ["product:5af12v70-dv70-1v70-bv70-701a04e25v70.product_uom_unit", "Unit"],
        },
        {
                "__id": "purchase:5af12v70-dv70-1v70-bv70-701a04e25v70.purchase_order_line-Alsads33e",
                "__module": "purchase",
                "__model": "purchase.order.line",
                "__import_module": "sale",
                "__import_model": "sale.order.line",
                "name": "PC on Demand",
                "price_unit": 100.0,
                "product_id": ["product:5af12v70-dv70-1v70-bv70-701a04e25v70.product_product_5", "[PC-DEM] PC on Demand"],
                "product_qty": 2.0,
                "product_uom": ["product:5af12v70-dv70-1v70-bv70-701a04e25v70.product_uom_unit", "Unit"],
        }],
    }
    new_sale_order_id = sale_order_pool.edi_import(cr, uid, edi_document, context=context)
    assert new_sale_order_id,  'Sale order import failed'
    order_new = sale_order_pool.browse(cr, uid, new_sale_order_id)

    # check bank info on partner
    assert order_new.partner_id.customer, "Imported partner should be a customer, as we just imported the document as a sale 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: 032465700-156700", 'Expected "Another bank: 032465700-156700", got %s' % bank_info.acc_number

    assert order_new.pricelist_id.name == 'Public Pricelist' , "Public Price list was not automatically assigned"
    assert order_new.amount_total == 350, "Amount total is wrong"
    assert order_new.amount_untaxed == 350, "Untaxed amount is wrong"
    assert len(order_new.order_line) == 2, "Sale order lines mismatch"
    for sale_line in order_new.order_line:
        if sale_line.name == 'PC Assemble SC234':
            assert sale_line.product_uom.name == "Unit" , "uom is not same"
            assert sale_line.price_unit == 150 , "unit price is not same, got %s, expected 150"%(sale_line.price_unit,)
            assert sale_line.product_uom_qty == 1 , "product qty is not same"
        elif sale_line.name == 'PC on Demand':
            assert sale_line.product_uom.name == "Unit" , "uom is not same"
            assert sale_line.price_unit == 100 , "unit price is not same, got %s, expected 100"%(sale_line.price_unit,)
            assert sale_line.product_uom_qty == 2 , "product qty is not same"
        else:
            raise AssertionError('unknown order line: %s' % sale_line)
-
  "Then I import a sample EDI document of a purchase order (v6.1 - to test backwards compatibility)"
-
  !python {model: edi.edi}: |
    sale_order_pool = self.pool.get('sale.order')
    edi_document = {
        "__id": "purchase:5af1272e-dd26-11e0-b65e-701a04e25543.purchase_order_test",
        "__module": "purchase",
        "__model": "purchase.order",
        "__import_module": "sale",
        "__import_model": "sale.order",
        "__version": [6,1,0],
        "name": "PO00011-v61",
        "date_order": "2011-09-12",
        "currency": {
                "__id": "base:5af1272e-dd26-11e0-b65e-701a04e25543.EUR",
                "__module": "base",
                "__model": "res.currency",
                "code": "EUR",
                "symbol": "€",
        },
        "company_id": ["base:5af1272e-dd26-11e0-b65e-701a04e25543.main_company", "Client S.A."],
        "company_address": {
                "__id": "base:5af1272e-dd26-11e0-b65e-701a04e25543.some_address",
                "__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"],
                "bank_ids": [
                    ["base:5af1272e-dd26-11e0-b65e-701a04e25543.res_partner_bank-adaWadsadasdDJzGbp","Ladies bank: 032465789-156113"]
                ],
        },
        "partner_id": ["purchase:5af1272e-dd26-11e0-b65e-701a04e25543.res_partner_test20", "jones white"],
        "partner_address": {
                "__id": "base:5af1272e-dd26-11e0-b65e-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"],
        },
        "order_line": [{
                "__id": "purchase:5af1272e-dd26-11e0-b65e-701a04e25543.purchase_order_line-AlhsVDZGoKvJ",
                "__module": "purchase",
                "__model": "purchase.order.line",
                "__import_module": "sale",
                "__import_model": "sale.order.line",
                "name": "Basic PC",
                "price_unit": 150.0,
                "product_id": ["product:5af1272e-dd26-11e0-b65e-701a04e25543.product_product_pc1", "[PC1] Basic PC"],
                "product_qty": 1.0,
                "product_uom": ["product:5af1272e-dd26-11e0-b65e-701a04e25543.product_uom_unit", "PCE"],
        },
        {
                "__id": "purchase:5af1272e-dd26-11e0-b65e-701a04e25543.purchase_order_line-Alsads33e",
                "__module": "purchase",
                "__model": "purchase.order.line",
                "__import_module": "sale",
                "__import_model": "sale.order.line",
                "name": "Medium PC",
                "price_unit": 100.0,
                "product_id": ["product:5af1272e-dd26-11e0-b65e-701a04e25543.product_product_pc3", "[PC3] Medium PC"],
                "product_qty": 2.0,
                "product_uom": ["product:5af1272e-dd26-11e0-b65e-701a04e25543.product_uom_unit", "PCE"],
        }],
    }
    new_sale_order_id = sale_order_pool.edi_import(cr, uid, edi_document, context=context)
    assert new_sale_order_id,  'Sale order import failed'
    order_new = sale_order_pool.browse(cr, uid, new_sale_order_id)

    # check bank info on partner
    assert order_new.partner_id.customer, "Imported partner should be a customer, as we just imported the document as a sale 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 == "Ladies bank: 032465789-156113", 'Expected "Ladies bank: 032465789-156113", got %s' % bank_info.acc_number

    assert order_new.pricelist_id.name == 'Public Pricelist' , "Public Price list was not automatically assigned"
    assert order_new.amount_total == 350, "Amount total is wrong"
    assert order_new.amount_untaxed == 350, "Untaxed amount is wrong"
    assert len(order_new.order_line) == 2, "Sale order lines mismatch"
    for sale_line in order_new.order_line:
        if sale_line.name == 'Basic PC':
            assert sale_line.product_uom.name == "PCE" , "uom is not same"
            assert sale_line.price_unit == 150 , "unit price is not same, got %s, expected 150"%(sale_line.price_unit,)
            assert sale_line.product_uom_qty == 1 , "product qty is not same"
        elif sale_line.name == 'Medium PC':
            assert sale_line.product_uom.name == "PCE" , "uom is not same"
            assert sale_line.price_unit == 100 , "unit price is not same, got %s, expected 100"%(sale_line.price_unit,)
            assert sale_line.product_uom_qty == 2 , "product qty is not same"
        else:
            raise AssertionError('unknown order line: %s' % sale_line)
