Beispiel #1
0
 def testuseoneof(self):
     self.assertEqual(u'test',transform.useoneof(None,'test'),'test useoneof')
     self.assertEqual(u'test',transform.useoneof('test','test1','test2'),'test useoneof')
     self.assertEqual(None,transform.useoneof(),'test useoneof')
     self.assertEqual(None,transform.useoneof(()),'test useoneof')
     self.assertEqual(None,transform.useoneof(''),'test useoneof')
     self.assertEqual(None,transform.useoneof(None,None,None,None),'test useoneof')
Beispiel #2
0
 def testuseoneof(self):
     self.assertEqual("test", transform.useoneof(None, "test"),
                      "test useoneof")
     self.assertEqual("test", transform.useoneof("test", "test1", "test2"),
                      "test useoneof")
     self.assertEqual(None, transform.useoneof(), "test useoneof")
     self.assertEqual(None, transform.useoneof(()), "test useoneof")
     self.assertEqual(None, transform.useoneof(""), "test useoneof")
     self.assertEqual(None, transform.useoneof(None, None, None, None),
                      "test useoneof")
Beispiel #3
0
 def testuseoneof(self):
     self.assertEqual(u'test', transform.useoneof(None, 'test'),
                      'test useoneof')
     self.assertEqual(u'test', transform.useoneof('test', 'test1', 'test2'),
                      'test useoneof')
     self.assertEqual(None, transform.useoneof(), 'test useoneof')
     self.assertEqual(None, transform.useoneof(()), 'test useoneof')
     self.assertEqual(None, transform.useoneof(''), 'test useoneof')
     self.assertEqual(None, transform.useoneof(None, None, None, None),
                      'test useoneof')
Beispiel #4
0
def main(inn, out):
    #sender, receiver is correct via QUERIES in grammar.
    out.put({
        'BOTSID': 'ST',
        'ST01': '856',
        'ST02': out.ta_info['reference'].zfill(4)
    })

    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BSN', 'BSN01': '00'})
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'BSN',
        'BSN02': inn.get({
            'BOTSID': 'message',
            'docnum': None
        })
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'BSN',
        'BSN03':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'docdtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'BSN',
        'BSN04':
        transform.useoneof(inn.get({
            'BOTSID': 'message',
            'doctime': None
        }), '0000')
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'BSN',
        'BSN05': '0001'
    })  #0001: Shipment, Order, Packaging, Item

    #***********************************************************************************************
    #shipment level*********************************************************************************
    hlcounter = 1  #HL segments have sequentail count
    shipment = out.putloop({'BOTSID': 'ST'}, {
        'BOTSID': 'HL',
        'HL01': hlcounter,
        'HL03': 'S'
    })
    currentshipment = hlcounter  #remember the current counter, as child-HL segments have to point to this shipment
    hlcounter += 1

    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID': 'TD5',
        'TD501': 'O',
        'TD502': '2',
        'TD503': inn.get({
            'BOTSID': 'message',
            'scac': None
        })
    })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID': 'REF',
        'REF01': 'BM',
        'REF02': inn.get({
            'BOTSID': 'message',
            'bol': None
        })
    })
    shipment.put(
        {'BOTSID': 'HL'}, {
            'BOTSID': 'REF',
            'REF01': 'CN',
            'REF02': inn.get({
                'BOTSID': 'message',
                'carrierreferencenumber': None
            })
        })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID':
        'DTM',
        'DTM01':
        '011',
        'DTM02':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'shipdtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID':
        'DTM',
        'DTM01':
        '017',
        'DTM02':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'deldtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })

    #loop over partys (all on shipment level for simplicity ;-)
    for party in inn.getloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                             {'BOTSID': 'party'}):
        pou = shipment.putloop({'BOTSID': 'HL'}, {'BOTSID': 'N1'})
        pou.put({
            'BOTSID': 'N1',
            'N101': party.get({
                'BOTSID': 'party',
                'qual': None
            })
        })
        #write partyID: if gln write it, else  DUNS etc
        #this uses the fact the out.put returns True if succeeded.
        if pou.put({
                'BOTSID': 'N1',
                'N103': 'UL',
                'N104': party.get({
                    'BOTSID': 'party',
                    'gln': None
                })
        }):
            pass
        elif pou.put({
                'BOTSID': 'N1',
                'N103': '1',
                'N104': party.get({
                    'BOTSID': 'party',
                    'DUNS': None
                })
        }):
            pass
        elif pou.put({
                'BOTSID': 'N1',
                'N103': '92',
                'N104': party.get({
                    'BOTSID': 'party',
                    'externalID': None
                })
        }):
            pass
        else:
            pou.put({
                'BOTSID': 'N1',
                'N103': '91',
                'N104': party.get({
                    'BOTSID': 'party',
                    'internalID': None
                })
            })
        pou.put({
            'BOTSID': 'N1',
            'N102': party.get({
                'BOTSID': 'party',
                'name1': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N2',
            'N201': party.get({
                'BOTSID': 'party',
                'name2': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N3',
            'N301': party.get({
                'BOTSID': 'party',
                'address1': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N3',
            'N302': party.get({
                'BOTSID': 'party',
                'address2': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N401': party.get({
                'BOTSID': 'party',
                'city': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N402': party.get({
                'BOTSID': 'party',
                'state': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N403': party.get({
                'BOTSID': 'party',
                'pcode': None
            })
        })
        pou.put({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N404': party.get({
                'BOTSID': 'party',
                'country': None
            })
        })

    #********************************************************************************************
    #order level*********************************************************************************
    for order in inn.getloop({'BOTSID': 'message'}, {'BOTSID': 'orders'},
                             {'BOTSID': 'order'}):
        ordernode = out.putloop({'BOTSID': 'ST'}, {
            'BOTSID': 'HL',
            'HL01': hlcounter,
            'HL02': currentshipment,
            'HL03': 'O'
        })
        currentorder = hlcounter
        hlcounter += 1
        ordernode.put(
            {'BOTSID': 'HL'}, {
                'BOTSID': 'PRF',
                'PRF01': order.get({
                    'BOTSID': 'order',
                    'ordernumber': None
                })
            })
        ordernode.put({'BOTSID': 'HL'}, {
            'BOTSID':
            'PRF',
            'PRF04':
            transform.datemask(
                order.get({
                    'BOTSID': 'order',
                    'orderdtm': None
                }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
        })

        #************************************************************************************************
        #pack/sscc level*********************************************************************************
        #in xml; inhouse file sscc number is included in each line.
        #so first: get a list of all sscc's for this order
        list_of_sscc = [
            orderline.get({
                'BOTSID': 'line',
                'sscc': None
            }) for orderline in order.getloop(
                {'BOTSID': 'order'}, {'BOTSID': 'lines'}, {'BOTSID': 'line'})
        ]
        #list_of_sscc has double sscc's, so remove doubles
        list_of_sscc = set(list_of_sscc)
        if None in list_of_sscc:
            raise Exception(
                'found lines in ASN without SSCC. SSCC is really needed for this customer.'
            )
        #now write sscc/pack level for each sscc
        for sscc in list_of_sscc:
            ssccnode = out.putloop({'BOTSID': 'ST'}, {
                'BOTSID': 'HL',
                'HL01': hlcounter,
                'HL02': currentorder,
                'HL03': 'P'
            })
            currentsscc = hlcounter
            hlcounter += 1
            ssccnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'MAN',
                'MAN01': 'GM',
                'MAN02': '00' + sscc
            })

            #***************************************************************************************************
            #line/article level*********************************************************************************
            #loop over all lines that have this sscc
            for lin in order.getloop({'BOTSID': 'order'}, {'BOTSID': 'lines'},
                                     {
                                         'BOTSID': 'line',
                                         'sscc': sscc
                                     }):
                itemnode = out.putloop({'BOTSID': 'ST'}, {
                    'BOTSID': 'HL',
                    'HL01': hlcounter,
                    'HL02': currentsscc,
                    'HL03': 'I'
                })
                hlcounter += 1
                itemnode.put(
                    {'BOTSID': 'HL'}, {
                        'BOTSID': 'LIN',
                        'LIN01': lin.get({
                            'BOTSID': 'line',
                            'linenum': None
                        })
                    })
                itemnode.put({'BOTSID': 'HL'}, {
                    'BOTSID': 'LIN',
                    'LIN02': 'UP',
                    'LIN03': lin.get({
                        'BOTSID': 'line',
                        'gtin': None
                    })
                })
                itemnode.put(
                    {'BOTSID': 'HL'}, {
                        'BOTSID': 'LIN',
                        'LIN04': 'VN',
                        'LIN05': lin.get({
                            'BOTSID': 'line',
                            'suart': None
                        })
                    })
                itemnode.put(
                    {'BOTSID': 'HL'}, {
                        'BOTSID': 'LIN',
                        'LIN06': 'BP',
                        'LIN07': lin.get({
                            'BOTSID': 'line',
                            'byart': None
                        })
                    })
                itemnode.put(
                    {'BOTSID': 'HL'}, {
                        'BOTSID': 'SN1',
                        'SN103': 'EA',
                        'SN102': lin.get({
                            'BOTSID': 'line',
                            'delqua': None
                        })
                    })
                itemnode.put(
                    {'BOTSID': 'HL'}, {
                        'BOTSID': 'SN1',
                        'SN106': 'EA',
                        'SN105': lin.get({
                            'BOTSID': 'line',
                            'ordqua': None
                        })
                    })
                itemnode.put({'BOTSID': 'HL'}, {
                    'BOTSID': 'PID',
                    'PID01': 'F',
                    'PID02': '08',
                    'PID05': lin.get({
                        'BOTSID': 'line',
                        'desc': None
                    })
                })

    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'CTT',
        'CTT01':
        out.getcountoccurrences({'BOTSID': 'ST'}, {'BOTSID': 'HL'},
                                {'BOTSID': 'LIN'})
    })  #bots counts line items
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'SE',
        'SE01': out.getcount() + 1,
        'SE02': out.ta_info['reference'].zfill(4)
    })  #SE01: bots counts the segments produced in the X12 message.
Beispiel #5
0
def main(inn, out):
    #sender, receiver is correct via QUERIES in grammar.
    out.put({
        'BOTSID': 'ST',
        'ST01': '856',
        'ST02': out.ta_info['reference'].zfill(4)
    })

    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BSN', 'BSN01': '00'})
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'BSN',
        'BSN02': inn.get({
            'BOTSID': 'message',
            'docnum': None
        })
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'BSN',
        'BSN03':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'docdtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'BSN',
        'BSN04':
        transform.useoneof(inn.get({
            'BOTSID': 'message',
            'doctime': None
        }), '0000')
    })
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'BSN',
        'BSN05': '0002'
    })  #0001: Shipment, Order, Item (pfff, just making this up)

    #***********************************************************************************************
    #shipment level*********************************************************************************
    hlcounter = 1  #HL segments have sequentail count
    shipment = out.putloop({'BOTSID': 'ST'}, {
        'BOTSID': 'HL',
        'HL01': hlcounter,
        'HL03': 'S'
    })
    currentshipment = hlcounter  #remember the current counter, as child-HL segments have to point to this shipment
    hlcounter += 1

    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID': 'TD5',
        'TD501': 'O',
        'TD502': '2',
        'TD503': inn.get({
            'BOTSID': 'message',
            'scac': None
        })
    })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID': 'REF',
        'REF01': 'BM',
        'REF02': inn.get({
            'BOTSID': 'message',
            'bol': None
        })
    })
    shipment.put(
        {'BOTSID': 'HL'}, {
            'BOTSID': 'REF',
            'REF01': 'CN',
            'REF02': inn.get({
                'BOTSID': 'message',
                'carrierreferencenumber': None
            })
        })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID':
        'DTM',
        'DTM01':
        '011',
        'DTM02':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'shipdtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })
    shipment.put({'BOTSID': 'HL'}, {
        'BOTSID':
        'DTM',
        'DTM01':
        '017',
        'DTM02':
        transform.datemask(inn.get({
            'BOTSID': 'message',
            'deldtm': None
        }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
    })

    #mapping for partners is in default 856 mapping
    asn_xml2x12_default.party_mapping(inn, shipment)

    #********************************************************************************************
    #order level*********************************************************************************
    for order in inn.getloop({'BOTSID': 'message'}, {'BOTSID': 'orders'},
                             {'BOTSID': 'order'}):
        ordernode = out.putloop({'BOTSID': 'ST'}, {
            'BOTSID': 'HL',
            'HL01': hlcounter,
            'HL02': currentshipment,
            'HL03': 'O'
        })
        currentorder = hlcounter
        hlcounter += 1
        ordernode.put(
            {'BOTSID': 'HL'}, {
                'BOTSID': 'PRF',
                'PRF01': order.get({
                    'BOTSID': 'order',
                    'ordernumber': None
                })
            })
        ordernode.put({'BOTSID': 'HL'}, {
            'BOTSID':
            'PRF',
            'PRF04':
            transform.datemask(
                order.get({
                    'BOTSID': 'order',
                    'orderdtm': None
                }), 'CCYY-MM-DD HH:mm', 'CCYYMMDD')
        })

        #***************************************************************************************************
        #line/article level*********************************************************************************
        for lin in order.getloop({'BOTSID': 'order'}, {'BOTSID': 'lines'},
                                 {'BOTSID': 'line'}):
            itemnode = out.putloop({'BOTSID': 'ST'}, {
                'BOTSID': 'HL',
                'HL01': hlcounter,
                'HL02': currentorder,
                'HL03': 'I'
            })
            hlcounter += 1
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'LIN',
                'LIN01': lin.get({
                    'BOTSID': 'line',
                    'linenum': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'LIN',
                'LIN02': 'UP',
                'LIN03': lin.get({
                    'BOTSID': 'line',
                    'gtin': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'LIN',
                'LIN04': 'VN',
                'LIN05': lin.get({
                    'BOTSID': 'line',
                    'suart': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'LIN',
                'LIN06': 'BP',
                'LIN07': lin.get({
                    'BOTSID': 'line',
                    'byart': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'SN1',
                'SN103': 'EA',
                'SN102': lin.get({
                    'BOTSID': 'line',
                    'delqua': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'SN1',
                'SN106': 'EA',
                'SN105': lin.get({
                    'BOTSID': 'line',
                    'ordqua': None
                })
            })
            itemnode.put({'BOTSID': 'HL'}, {
                'BOTSID': 'PID',
                'PID01': 'F',
                'PID02': '08',
                'PID05': lin.get({
                    'BOTSID': 'line',
                    'desc': None
                })
            })

    out.put({'BOTSID': 'ST'}, {
        'BOTSID':
        'CTT',
        'CTT01':
        out.getcountoccurrences({'BOTSID': 'ST'}, {'BOTSID': 'HL'},
                                {'BOTSID': 'LIN'})
    })  #bots counts line items
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'SE',
        'SE01': out.getcount() + 1,
        'SE02': out.ta_info['reference'].zfill(4)
    })  #SE01: bots counts the segments produced in the X12 message.
def main(inn, out):
    #pick up some values from ISA envelope
    out.put({'BOTSID': 'message', 'sender': inn.ta_info['frompartner']})
    out.put({'BOTSID': 'message', 'receiver': inn.ta_info['topartner']})
    out.put({
        'BOTSID': 'message',
        'testindicator': inn.ta_info['testindicator']
    })

    out.put({
        'BOTSID':
        'message',
        'docdtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'BIG',
                'BIG01': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')
    })
    out.put({
        'BOTSID':
        'message',
        'docnum':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BIG',
            'BIG02': None
        })
    })
    out.put({
        'BOTSID':
        'message',
        'ordernumber':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BIG',
            'BIG04': None
        })
    })
    #~ out.put({'BOTSID':'ST'},{'BOTSID':'BIG','BIG07':'DR'})      #credit or debit

    out.put({
        'BOTSID':
        'message',
        'deldtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'DTM',
                'DTM01': '999',
                'DTM02': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')
    })
    out.put({
        'BOTSID':
        'message',
        'VendorID':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'REF',
            'REF01': 'VR',
            'REF02': None
        })
    })

    out.put({
        'BOTSID':
        'message',
        'termsdiscountpercent':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'ITD',
            'ITD03': None
        })
    })
    out.put({
        'BOTSID':
        'message',
        'termsdiscountdaysdue':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'ITD',
            'ITD05': None
        })
    })
    out.put({
        'BOTSID':
        'message',
        'termsnetdays':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'ITD',
            'ITD07': None
        })
    })
    out.put({
        'BOTSID':
        'message',
        'totaltermsdiscount':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'ITD',
            'ITD08': None
        })
    })

    #loop over partys
    for party in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'N1'}):
        pou = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                          {'BOTSID': 'party'})
        pou.put({
            'BOTSID': 'party',
            'qual': party.get({
                'BOTSID': 'N1',
                'N101': None
            })
        })
        pou.put({
            'BOTSID': 'party',
            'gln': party.get({
                'BOTSID': 'N1',
                'N103': 'UL',
                'N104': None
            })
        })
        #get DUNS number. 2 qualifiers are used; helper function transform.useoneof checks both
        pou.put({
            'BOTSID':
            'party',
            'DUNS':
            transform.useoneof(
                party.get({
                    'BOTSID': 'N1',
                    'N103': '1',
                    'N104': None
                }), party.get({
                    'BOTSID': 'N1',
                    'N103': '9',
                    'N104': None
                }))
        })
        pou.put({
            'BOTSID':
            'party',
            'externalID':
            party.get({
                'BOTSID': 'N1',
                'N103': '92',
                'N104': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'internalID':
            party.get({
                'BOTSID': 'N1',
                'N103': '91',
                'N104': None
            })
        })
        pou.put({
            'BOTSID': 'party',
            'name1': party.get({
                'BOTSID': 'N1',
                'N102': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'name2':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N2',
                'N201': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'address1':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N301': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'address2':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N302': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'city':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N401': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'state':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N402': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'pcode':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N403': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'country':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N404': None
            })
        })

    #loop over lines***************************************
    for lin in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'IT1'}):
        lou = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'lines'},
                          {'BOTSID': 'line'})
        lou.put({
            'BOTSID': 'line',
            'linenum': lin.get({
                'BOTSID': 'IT1',
                'IT101': None
            })
        })
        lou.put({
            'BOTSID': 'line',
            'invqua': lin.get({
                'BOTSID': 'IT1',
                'IT102': None
            })
        })
        lou.put({
            'BOTSID':
            'line',
            'ordunit':
            transform.useoneof(lin.get({
                'BOTSID': 'IT1',
                'IT103': None
            }), 'EA')
        })
        lou.put({
            'BOTSID': 'line',
            'price': lin.get({
                'BOTSID': 'IT1',
                'IT104': None
            })
        })

        lou.put({'BOTSID': 'line', 'gtin': get_art_num(lin, 'UP')})
        lou.put({'BOTSID': 'line', 'suart': get_art_num(lin, 'VN')})
        #get buyers article number; 1 different qualifiers are used; helper function transform.useoneof checks both
        lou.put({
            'BOTSID':
            'line',
            'byart':
            transform.useoneof(get_art_num(lin, 'IN'), get_art_num(lin, 'BP'))
        })
        lou.put({
            'BOTSID':
            'line',
            'desc':
            lin.get({'BOTSID': 'IT1'}, {
                'BOTSID': 'PID',
                'PID01': 'F',
                'PID05': None
            })
        })

    out.put({
        'BOTSID':
        'message',
        'totalinvoiceamount':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'TDS',
            'TDS01': None
        })
    })
def main(inn, out):

    # ISA,GS,ST
    sender = inn.ta_info['frompartner']
    receiver = inn.ta_info['topartner']
    testindicator = inn.ta_info['testindicator']

    out.put({'BOTSID': 'message', 'sender': sender})
    out.put({'BOTSID': 'message', 'receiver': receiver})
    out.put({'BOTSID': 'message', 'testindicator': testindicator})

    # BSN

    shipmentnum = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BSN', 'BSN02': None})
    out.ta_info['botskey'] = shipmentnum
    asndate = transform.datemask(
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BSN',
            'BSN03': None
        }), 'CCYYMMDD', 'CCYY-MM-DD')
    asntime = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BSN', 'BSN04': None})

    out.put({'BOTSID': 'message', 'docnum': shipmentnum})
    out.put({'BOTSID': 'message', 'docdtm': asndate})
    out.put({'BOTSID': 'message', 'doctime': asntime})

    # shipment level/loop
    shipmentLoop = inn.getloop({'BOTSID': 'ST'}, {
        'BOTSID': 'HL',
        'HL01': '1',
        'HL03': 'S'
    })

    for shipment in shipmentLoop:

        #TD5
        scac = shipment.get({'BOTSID': 'HL'}, {
            'BOTSID': 'TD5',
            'TD501': 'O',
            'TD502': '2',
            'TD503': None
        })
        out.put({'BOTSID': 'message', 'scac': scac})

        # REF
        bol = shipment.get({'BOTSID': 'HL'}, {
            'BOTSID': 'REF',
            'REF01': 'BM',
            'REF02': None
        })
        carrierref = shipment.get({'BOTSID': 'HL'}, {
            'BOTSID': 'REF',
            'REF01': 'CN',
            'REF02': None
        })

        out.put({'BOTSID': 'message', 'bol': bol})
        out.put({'BOTSID': 'message', 'carrierreferencenumber': carrierref})

        # DTM
        shipdate = transform.datemask(
            shipment.get({'BOTSID': 'HL'}, {
                'BOTSID': 'DTM',
                'DTM01': '011',
                'DTM02': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')
        deliverydate = transform.datemask(
            shipment.get({'BOTSID': 'HL'}, {
                'BOTSID': 'DTM',
                'DTM01': '017',
                'DTM02': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')

        out.put({'BOTSID': 'message', 'shipdtm': shipdate})
        out.put({'BOTSID': 'message', 'deldtm': deliverydate})

        # N1 loop on shipment level
        partysLoop = shipment.getloop({'BOTSID': 'HL'}, {'BOTSID': 'N1'})

        for party in partysLoop:
            partyout = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                                   {'BOTSID': 'party'})

            # N1
            partyQual = party.get({'BOTSID': 'N1', 'N101': None})
            # get DUNS number. 2 qualifiers are used; helper function transform.useoneof checks both
            duns = transform.useoneof(
                party.get({
                    'BOTSID': 'N1',
                    'N103': '1',
                    'N104': None
                }), party.get({
                    'BOTSID': 'N1',
                    'N103': '9',
                    'N104': None
                }))
            externalID = party.get({
                'BOTSID': 'N1',
                'N103': '92',
                'N104': None
            })
            internalID = party.get({
                'BOTSID': 'N1',
                'N103': '91',
                'N104': None
            })
            name1 = party.get({'BOTSID': 'N1', 'N102': None})

            partyout.put({'BOTSID': 'party', 'qual': partyQual})
            partyout.put({'BOTSID': 'party', 'DUNS': duns})
            partyout.put({'BOTSID': 'party', 'externalID': externalID})
            partyout.put({'BOTSID': 'party', 'internalID': internalID})
            partyout.put({'BOTSID': 'party', 'name1': name1})

            # N2
            name2 = party.get({'BOTSID': 'N1'}, {'BOTSID': 'N2', 'N201': None})

            partyout.put({'BOTSID': 'party', 'name2': name2})

            # N3
            address1 = party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N301': None
            })
            address2 = party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N302': None
            })

            partyout.put({'BOTSID': 'party', 'address1': address1})
            partyout.put({'BOTSID': 'party', 'address2': address2})

            # N4
            city = party.get({'BOTSID': 'N1'}, {'BOTSID': 'N4', 'N401': None})
            region = party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N402': None
            })
            zipcode = party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N403': None
            })
            country = party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N404': None
            })

            partyout.put({'BOTSID': 'party', 'city': city})
            partyout.put({'BOTSID': 'party', 'state': region})
            partyout.put({'BOTSID': 'party', 'pcode': zipcode})
            partyout.put({'BOTSID': 'party', 'country': country})

    # order level/loop
    orderLoop = inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'HL', 'HL03': 'O'})
    for in_order in orderLoop:
        out_order = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'orders'},
                                {'BOTSID': 'order'})
        current_order = in_order.get({'BOTSID': 'HL', 'HL01': None})

        # PRF
        order_number = in_order.get({'BOTSID': 'HL'}, {
            'BOTSID': 'PRF',
            'PRF01': None
        })
        orderdate = transform.datemask(
            in_order.get({'BOTSID': 'HL'}, {
                'BOTSID': 'PRF',
                'PRF04': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')

        out_order.put({'BOTSID': 'order', 'ordernumber': order_number})
        out_order.put({'BOTSID': 'order', 'orderdtm': orderdate})

        # pack/sscc level/loop
        packs = inn.getloop({'BOTSID': 'ST'}, {
            'BOTSID': 'HL',
            'HL02': current_order,
            'HL03': 'P'
        })
        for in_pack in packs:
            current_sscc = in_pack.get({'BOTSID': 'HL', 'HL01': None})
            sscc = in_pack.get({'BOTSID': 'HL'}, {
                'BOTSID': 'MAN',
                'MAN01': 'GM',
                'MAN02': None
            })

            # linelevel/loop
            # loop over all lines that have this sscc
            linesinpack = inn.getloop({'BOTSID': 'ST'}, {
                'BOTSID': 'HL',
                'HL02': current_sscc,
                'HL03': 'I'
            })
            for line in linesinpack:
                lineout = out_order.putloop({'BOTSID': 'order'},
                                            {'BOTSID': 'lines'},
                                            {'BOTSID': 'line'})

                # LIN
                linenum = line.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'LIN',
                    'LIN01': None
                })
                gtin = line.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'LIN',
                    'LIN02': 'UP',
                    'LIN03': None
                })

                lineout.put({'BOTSID': 'line', 'linenum': linenum})
                lineout.put({'BOTSID': 'line', 'gtin': gtin})

                # SN1
                deliveryquantity = line.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'SN1',
                    'SN103': 'EA',
                    'SN102': None
                })
                orderquantity = line.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'SN1',
                    'SN106': 'EA',
                    'SN105': None
                })

                lineout.put({'BOTSID': 'line', 'delqua': deliveryquantity})
                lineout.put({'BOTSID': 'line', 'ordqua': orderquantity})

                # PID
                descritpion = line.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'PID',
                    'PID01': 'F',
                    'PID02': '08',
                    'PID05': None
                })

                lineout.put({'BOTSID': 'line', 'desc': descritpion})
                lineout.put({'BOTSID': 'line', 'sscc': sscc})
def main(inn, out):
    #pick up some values from ISA envelope
    out.put({'BOTSID': 'message', 'sender': inn.ta_info['frompartner']})
    out.put({'BOTSID': 'message', 'receiver': inn.ta_info['topartner']})
    out.put({
        'BOTSID': 'message',
        'testindicator': inn.ta_info['testindicator']
    })

    out.put({
        'BOTSID':
        'message',
        'docnum':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BSN',
            'BSN02': None
        })
    })
    out.put({
        'BOTSID':
        'message',
        'docdtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'BSN',
                'BSN03': None
            }), 'CCYYMMDD', 'CCYY-MM-DD')
    })
    out.put({
        'BOTSID':
        'message',
        'doctime':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BSN',
            'BSN04': None
        })
    })

    #***********************************************************************************************
    #shipment level*********************************************************************************
    for shipment in inn.getloop({'BOTSID': 'ST'}, {
            'BOTSID': 'HL',
            'HL01': '1',
            'HL03': 'S'
    }):
        #~ hlcounter = 1       #HL segments have sequentail count
        #~ shipment = out.putloop({'BOTSID':'ST'},{'BOTSID':'HL','HL01':hlcounter,'HL03':'S'})
        #~ currentshipment = hlcounter     #remember the current counter, as child-HL segments have to point to this shipment
        #~ hlcounter += 1

        out.put({
            'BOTSID':
            'message',
            'scac':
            shipment.get({'BOTSID': 'HL'}, {
                'BOTSID': 'TD5',
                'TD501': 'O',
                'TD502': '2',
                'TD503': None
            })
        })
        out.put({
            'BOTSID':
            'message',
            'bol':
            shipment.get({'BOTSID': 'HL'}, {
                'BOTSID': 'REF',
                'REF01': 'BM',
                'REF02': None
            })
        })
        out.put({
            'BOTSID':
            'message',
            'carrierreferencenumber':
            shipment.get({'BOTSID': 'HL'}, {
                'BOTSID': 'REF',
                'REF01': 'CN',
                'REF02': None
            })
        })
        out.put({
            'BOTSID':
            'message',
            'shipdtm':
            transform.datemask(
                shipment.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'DTM',
                    'DTM01': '011',
                    'DTM02': None
                }), 'CCYYMMDD', 'CCYY-MM-DD')
        })
        out.put({
            'BOTSID':
            'message',
            'deldtm':
            transform.datemask(
                shipment.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'DTM',
                    'DTM01': '017',
                    'DTM02': None
                }), 'CCYYMMDD', 'CCYY-MM-DD')
        })

        #loop over partys on shipment level
        for party in shipment.getloop({'BOTSID': 'HL'}, {'BOTSID': 'N1'}):
            pou = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                              {'BOTSID': 'party'})
            pou.put({
                'BOTSID': 'party',
                'qual': party.get({
                    'BOTSID': 'N1',
                    'N101': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'gln':
                party.get({
                    'BOTSID': 'N1',
                    'N103': 'UL',
                    'N104': None
                })
            })
            #get DUNS number. 2 qualifiers are used; helper function transform.useoneof checks both
            pou.put({
                'BOTSID':
                'party',
                'DUNS':
                transform.useoneof(
                    party.get({
                        'BOTSID': 'N1',
                        'N103': '1',
                        'N104': None
                    }), party.get({
                        'BOTSID': 'N1',
                        'N103': '9',
                        'N104': None
                    }))
            })
            pou.put({
                'BOTSID':
                'party',
                'externalID':
                party.get({
                    'BOTSID': 'N1',
                    'N103': '92',
                    'N104': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'internalID':
                party.get({
                    'BOTSID': 'N1',
                    'N103': '91',
                    'N104': None
                })
            })
            pou.put({
                'BOTSID': 'party',
                'name1': party.get({
                    'BOTSID': 'N1',
                    'N102': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'name2':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N2',
                    'N201': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'address1':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N3',
                    'N301': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'address2':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N3',
                    'N302': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'city':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N4',
                    'N401': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'state':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N4',
                    'N402': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'pcode':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N4',
                    'N403': None
                })
            })
            pou.put({
                'BOTSID':
                'party',
                'country':
                party.get({'BOTSID': 'N1'}, {
                    'BOTSID': 'N4',
                    'N404': None
                })
            })

    #********************************************************************************************
    #order level*********************************************************************************
    for in_order in inn.getloop({'BOTSID': 'ST'}, {
            'BOTSID': 'HL',
            'HL03': 'O'
    }):
        out_order = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'orders'},
                                {'BOTSID': 'order'})
        out_order.put({
            'BOTSID':
            'order',
            'ordernumber':
            in_order.get({'BOTSID': 'HL'}, {
                'BOTSID': 'PRF',
                'PRF01': None
            })
        })
        out_order.put({
            'BOTSID':
            'order',
            'orderdtm':
            transform.datemask(
                in_order.get({'BOTSID': 'HL'}, {
                    'BOTSID': 'PRF',
                    'PRF04': None
                }), 'CCYYMMDD', 'CCYY-MM-DD')
        })
        current_order = in_order.get({'BOTSID': 'HL', 'HL01': None})

        #pack/sscc level*********************************************************************************
        for in_sscc in inn.getloop({'BOTSID': 'ST'}, {
                'BOTSID': 'HL',
                'HL02': current_order,
                'HL03': 'P'
        }):
            current_sscc = in_sscc.get({'BOTSID': 'HL', 'HL01': None})
            sscc = in_sscc.get({'BOTSID': 'HL'}, {
                'BOTSID': 'MAN',
                'MAN01': 'GM',
                'MAN02': None
            })
            #line/article level*********************************************************************************
            #loop over all lines that have this sscc
            for lin in inn.getloop({'BOTSID': 'ST'}, {
                    'BOTSID': 'HL',
                    'HL02': current_sscc,
                    'HL03': 'I'
            }):
                lou = out_order.putloop({'BOTSID': 'order'},
                                        {'BOTSID': 'lines'},
                                        {'BOTSID': 'line'})
                lou.put({
                    'BOTSID':
                    'line',
                    'linenum':
                    lin.get({'BOTSID': 'HL'}, {
                        'BOTSID': 'LIN',
                        'LIN01': None
                    })
                })
                lou.put({
                    'BOTSID':
                    'line',
                    'gtin':
                    lin.get({'BOTSID': 'HL'}, {
                        'BOTSID': 'LIN',
                        'LIN02': 'UP',
                        'LIN03': None
                    })
                })
                lou.put({
                    'BOTSID':
                    'line',
                    'delqua':
                    lin.get({'BOTSID': 'HL'}, {
                        'BOTSID': 'SN1',
                        'SN103': 'EA',
                        'SN102': None
                    })
                })
                lou.put({
                    'BOTSID':
                    'line',
                    'ordqua':
                    lin.get({'BOTSID': 'HL'}, {
                        'BOTSID': 'SN1',
                        'SN106': 'EA',
                        'SN105': None
                    })
                })
                lou.put({
                    'BOTSID':
                    'line',
                    'desc':
                    lin.get({'BOTSID': 'HL'}, {
                        'BOTSID': 'PID',
                        'PID01': 'F',
                        'PID02': '08',
                        'PID05': None
                    })
                })
                lou.put({'BOTSID': 'line', 'sscc': sscc})
Beispiel #9
0
def main(inn, out):

    # Set Constants
    D = 'urn:oasis:names:specification:ubl:schema:xsd:Order-2'
    S = 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'
    B = 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2'
    E = 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'
    UBLVersionID = "2.1"
    CustomizationID = "LDC"
    ProfileID_Content = "Basic-v1.0"
    ProfileID_Agency = "LDC"
    ProfileID_Identifier = "Profile"
    ProfileExecutionID = "Basic-Order"
    now = datetime.utcnow()

    out.put({'BOTSID': 'ROOT', '_D': D})
    out.put({'BOTSID': 'ROOT', '_S': S})
    out.put({'BOTSID': 'ROOT', '_B': B})
    out.put({'BOTSID': 'ROOT', '_E': E})

    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'UBLVersionID',
        'IdentifierContent': UBLVersionID
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'CustomizationID',
        'IdentifierContent': CustomizationID
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'ProfileID',
        'IdentifierContent': ProfileID_Content
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'ProfileID',
        'IdentificationSchemeAgencyIdentifier': ProfileID_Agency
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'ProfileID',
        'IdentificationSchemeIdentifier': ProfileID_Identifier
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'ProfileExecutionID',
        'IdentifierContent': ProfileExecutionID
    })

    UBLExtensionLoop1 = out.putloop({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                                    {'BOTSID': 'UBLExtensions'},
                                    {'BOTSID': 'UBLExtension'})
    UBLExtensionLoop2 = out.putloop({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                                    {'BOTSID': 'UBLExtensions'},
                                    {'BOTSID': 'UBLExtension'})

    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'}, {
        'BOTSID': 'Name',
        'TextContent': "Loren Data Transport"
    })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'}, {
        'BOTSID':
        'ExtensionReason',
        'TextContent':
        "Aggregate extension component containing information for the Loren Data transport values."
    })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ParcelID',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'NetworkIDFrom',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'NetworkIDFrom',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'NetworkIDTo',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'NetworkIDTo',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'MailboxIDFrom',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'MailboxIDFrom',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'MailboxIDTo',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'MailboxIDTo',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ECGridIDFrom',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ECGridIDFrom',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ECGridIDTo',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ECGridIDTo',
                              'IdentificationSchemeNameText': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ElectronicMailFrom',
                              'TextContent': ""
                          })
    UBLExtensionLoop1.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'LorenDataTransport'}, {
                              'BOTSID': 'ElectronicMailTo',
                              'TextContent': ""
                          })

    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'}, {
        'BOTSID': 'Name',
        'TextContent': "Document Envelope"
    })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'}, {
        'BOTSID':
        'ExtensionReason',
        'TextContent':
        "Aggregate extention component containing information for the document envelope used for translation."
    })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'ID',
                              'IdentifierContent': inn.ta_info['ISA13']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'ID',
                              'IdentificationSchemeNameText': "ISA"
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'CopyIndicator',
                              'IdentifierContent': inn.ta_info['ISA15']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'UUID',
                              'IdentifierContent': ""
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'},
                          {'BOTSID': 'SenderParty'},
                          {'BOTSID': 'PartyIdentification'}, {
                              'BOTSID': 'ID',
                              'IdentifierContent': inn.ta_info['frompartner']
                          })
    UBLExtensionLoop2.put(
        {'BOTSID': 'UBLExtension'}, {'BOTSID': 'ExtensionContent'},
        {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'SenderParty'},
        {'BOTSID': 'PartyIdentification'}, {
            'BOTSID': 'ID',
            'IdentificationSchemeNameText': inn.ta_info['ISA05']
        })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'},
                          {'BOTSID': 'ReceiverParty'},
                          {'BOTSID': 'PartyIdentification'}, {
                              'BOTSID': 'ID',
                              'IdentifierContent': inn.ta_info['topartner']
                          })
    UBLExtensionLoop2.put(
        {'BOTSID': 'UBLExtension'}, {'BOTSID': 'ExtensionContent'},
        {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'ReceiverParty'},
        {'BOTSID': 'PartyIdentification'}, {
            'BOTSID': 'ID',
            'IdentificationSchemeNameText': inn.ta_info['ISA07']
        })
    UBLExtensionLoop2.put(
        {'BOTSID': 'UBLExtension'}, {'BOTSID': 'ExtensionContent'},
        {'BOTSID': 'DocumentEnvelope'}, {
            'BOTSID':
            'IssueDate',
            'DateContent':
            now.strftime('%Y') +
            transform.datemask(inn.ta_info['ISA09'], "YYmmDD", "-mm-DD")
        })
    UBLExtensionLoop2.put(
        {'BOTSID': 'UBLExtension'}, {'BOTSID': 'ExtensionContent'},
        {'BOTSID': 'DocumentEnvelope'}, {
            'BOTSID':
            'IssueTime',
            'TimeContent':
            transform.datemask(inn.ta_info['ISA10'], "0000", "00:00")
        })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'SpecificationID',
                              'IdentifierContent': inn.ta_info['GS06']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'SpecificationID',
                              'IdentificationSchemeNameText': "GS"
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'AgencyID',
                              'IdentifierContent': inn.ta_info['GS07']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'VersionID',
                              'IdentifierContent': inn.ta_info['version']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'DocumentType',
                              'TextContent': inn.ta_info['ST01']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'SequenceNumberID',
                              'IdentifierContent': inn.ta_info['reference']
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {
                              'BOTSID': 'SequenceNumberID',
                              'IdentificationSchemeNameText': "ST"
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'Status'},
                          {
                              'BOTSID': 'TextConent',
                              'TextContent': "New"
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'Status'},
                          {
                              'BOTSID': 'ReferenceDate',
                              'DateContent': now.strftime('%Y-%m-%d')
                          })
    UBLExtensionLoop2.put({'BOTSID': 'UBLExtension'},
                          {'BOTSID': 'ExtensionContent'},
                          {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'Status'},
                          {
                              'BOTSID': 'ReferenceTime',
                              'TimeContent': now.strftime('%H:%M:%S')
                          })

    # BEG Segment
    purpose_code = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG01': None})
    purpose_code_text = transform.ccode('X12_ELEM_353_TO_TEXT', purpose_code)
    ordertype_code = inn.get({'BOTSID': 'ST'}, {
        'BOTSID': 'BEG',
        'BEG02': None
    })
    ordertype_code_text = transform.ccode('X12_ELEM_92_TO_TEXT',
                                          ordertype_code)

    order_number = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG03': None})
    out.ta_info['botskey'] = order_number
    order_date = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG05': None})
    contract_number = inn.get({'BOTSID': 'ST'}, {
        'BOTSID': 'BEG',
        'BEG06': None
    })

    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'PurposeCode',
        'CodeContent': purpose_code
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'PurposeCode',
        'CodeNameText': purpose_code_text
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'OrderTypeCode',
        'CodeContent': ordertype_code
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'OrderTypeCode',
        'CodeNameText': ordertype_code_text
    })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'ID',
        'IdentifierContent': order_number
    })
    out.put(
        {'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
            'BOTSID': 'IssueDate',
            'DateContent': transform.datemask(order_date, 'YYYYmmDD',
                                              'mm/DD/YYYY')
        })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {'BOTSID': 'Contract'}, {
        'BOTSID': 'ID',
        'IdentifierContent': contract_number
    })

    # REF Segment
    EstimatedDeliveryDateQual = inn.get({'BOTSID': 'ST'}, {
        'BOTSID': 'DTM',
        'DTM01': None
    })
    EstimatedDeliveryDate = inn.get({'BOTSID': 'ST'}, {
        'BOTSID': 'DTM',
        'DTM01': '017',
        'DTM02': None
    })

    if EstimatedDeliveryDateQual != '':
        out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                {'BOTSID': 'Delivery'}, {'BOTSID': 'RequestedDeliveryPeriod'},
                {
                    'BOTSID':
                    'StartDate',
                    'DateContent':
                    transform.datemask(EstimatedDeliveryDate, 'YYYYmmDD',
                                       'mm/DD/YYYY')
                })
        out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                {'BOTSID': 'Delivery'}, {'BOTSID': 'RequestedDeliveryPeriod'},
                {
                    'BOTSID': 'DescriptionCode',
                    'CodeContent': '017'
                })
        out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                {'BOTSID': 'Delivery'}, {'BOTSID': 'RequestedDeliveryPeriod'},
                {
                    'BOTSID': 'DescriptionCode',
                    'CodeNameText': 'Estimated Delivery Date'
                })

    # TD5 Segment
    SCAC = inn.get({'BOTSID': 'ST'}, {
        'BOTSID': 'TD5',
        'TD502': '2',
        'TD503': None
    })
    CarrierName = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'TD5', 'TD505': None})

    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
            {'BOTSID': 'CarrierParty'}, {'BOTSID': 'PartyIdentification'}, {
                'BOTSID': 'ID',
                'IdentifierContent': SCAC
            })
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
            {'BOTSID': 'CarrierParty'}, {'BOTSID': 'PartyName'}, {
                'BOTSID': 'Name',
                'TextContent': CarrierName
            })

    # N1 Loops
    for party in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'N1'}):
        PartyQual = party.get({'BOTSID': 'N1', 'N101': None})
        PartyName = party.get({'BOTSID': 'N1', 'N102': None})
        PartyAgency = party.get({'BOTSID': 'N1', 'N103': None})
        PartyNumber = party.get({'BOTSID': 'N1', 'N104': None})

        PartyAddress1 = party.get({'BOTSID': 'N1'}, {
            'BOTSID': 'N3',
            'N301': None
        })
        PartyAddress2 = party.get({'BOTSID': 'N1'}, {
            'BOTSID': 'N3',
            'N302': None
        })

        PartyCity = party.get({'BOTSID': 'N1'}, {'BOTSID': 'N4', 'N401': None})
        PartyCountrySubentity = party.get({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N402': None
        })
        PartyPostal = party.get({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N403': None
        })
        PartyCountry = party.get({'BOTSID': 'N1'}, {
            'BOTSID': 'N4',
            'N404': None
        })

        if PartyQual == 'BT':
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'}, {
                        'BOTSID': 'PartyIdentification',
                        'IdentifierContent': PartyNumber
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'}, {
                        'BOTSID': 'PartyIdentification',
                        'IdentificationSchemeIdentifier': PartyQual
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'}, {
                        'BOTSID': 'PartyIdentification',
                        'IdentificationSchemeAgencyIdentifier': PartyAgency
                    })

            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PartyName'}, {
                        'BOTSID': 'Name',
                        'TextContent': PartyName
                    })

            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {
                        'BOTSID': 'StreetName',
                        'TextContent': PartyAddress1
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {
                        'BOTSID': 'AdditionalStreetName',
                        'TextContent': PartyAddress2
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {
                        'BOTSID': 'CityName',
                        'TextContent': PartyCity
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {
                        'BOTSID': 'PostalZone',
                        'TextContent': PartyPostal
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {
                        'BOTSID': 'CountrySubentity',
                        'TextContent': PartyCountrySubentity
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'BuyerCustomerParty'}, {'BOTSID': 'Party'},
                    {'BOTSID': 'PostalAddress'}, {'BOTSID': 'Country'}, {
                        'BOTSID': 'IdentificationCode',
                        'CodeContent': PartyCountry[:-1]
                    })

        elif PartyQual == 'ST':
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'ID',
                        'IdentifierContent': PartyNumber
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'ID',
                        'IdentificationSchemeIdentifier': PartyQual
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'ID',
                        'IdentificationSchemeAgencyIdentifier': PartyAgency
                    })

            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'BuildingName',
                        'TextContent': PartyName
                    })

            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'StreetName',
                        'TextContent': PartyAddress1
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'AdditionalStreetName',
                        'TextContent': PartyAddress2
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'CityName',
                        'TextContent': PartyCity
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'PostalZone',
                        'TextContent': PartyPostal
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'}, {
                        'BOTSID': 'CountrySubentity',
                        'TextContent': PartyCountrySubentity
                    })
            out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                    {'BOTSID': 'Delivery'}, {'BOTSID': 'DeliveryAddress'},
                    {'BOTSID': 'Country'}, {
                        'BOTSID': 'IdentificationCode',
                        'CodeContent': PartyCountry[:-1]
                    })

    # PO1 Loops
    for po1 in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'PO1'}):

        # Out Object Array
        orderLineOut = out.putloop({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'},
                                   {'BOTSID': 'OrderLine'})

        # Get Values
        LineNumber = po1.get({'BOTSID': 'PO1', 'PO101': None})

        # Get UOM, if empty use 'EA'
        X12_UOM = transform.useoneof(po1.get({
            'BOTSID': 'PO1',
            'PO103': None
        }), 'EA')
        ISO_UOM = transform.ccode('X12_ELEM_355_TO_ISO_UOM', X12_UOM)
        QuantityOrdered = po1.get({'BOTSID': 'PO1', 'PO102': None})

        X12_PriceCode = transform.useoneof(
            po1.get({
                'BOTSID': 'PO1',
                'PO105': None
            }), 'PE')
        ISO_PriceCode = transform.ccode('X12_ELEM_639_TO_ISO_PRICETYPE',
                                        X12_PriceCode)
        ISO_PriceCode_Text = transform.ccode('ISO_PRICETYPE_TO_TEXT',
                                             ISO_PriceCode)
        UnitPrice = po1.get({'BOTSID': 'PO1', 'PO104': None})

        # Get Item Values by Qualifier
        BuyerItem = get_item_number_by_qual(po1, 'SK')
        SellerItem = get_item_number_by_qual(po1, 'CB')
        UPC = get_item_number_by_qual(po1, 'UP')

        Description = po1.get({'BOTSID': 'PO1'}, {
            'BOTSID': 'PID',
            'PID01': 'F',
            'PID05': None
        })

        # Write output
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'}, {
            'BOTSID': 'ID',
            'IdentifierContent': LineNumber
        })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'}, {
            'BOTSID': 'Quantity',
            'QuantityContent': QuantityOrdered
        })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'}, {
            'BOTSID': 'Quantity',
            'QuantityUnitCode': ISO_UOM
        })

        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Price'}, {
                             'BOTSID': 'PriceAmount',
                             'AmountContent': UnitPrice
                         })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Price'}, {
                             'BOTSID': 'PriceTypeCode',
                             'CodeContent': ISO_PriceCode
                         })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Price'}, {
                             'BOTSID': 'PriceTypeCode',
                             'CodeNameText': ISO_PriceCode_Text
                         })

        orderLineOut.put(
            {'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'}, {
                'BOTSID': 'LineExtensionAmount',
                'AmountContent': Decimal(UnitPrice) * Decimal(QuantityOrdered)
            })

        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'},
                         {'BOTSID': 'BuyersItemIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentifierContent': BuyerItem
                         })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'},
                         {'BOTSID': 'BuyersItemIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentificationSchemeIdentifier': ''
                         })

        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'},
                         {'BOTSID': 'SellersItemIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentifierContent': SellerItem
                         })

        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'},
                         {'BOTSID': 'StandardItemIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentifierContent': UPC
                         })
        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'},
                         {'BOTSID': 'StandardItemIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentificationSchemeIdentifier': 'UPC'
                         })

        orderLineOut.put({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                         {'BOTSID': 'Item'}, {
                             'BOTSID': 'Description',
                             'TextContent': Description
                         })

    # CTT Segment
    CTT = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'CTT', 'CTT01': None})
    out.put({'BOTSID': 'ROOT'}, {'BOTSID': 'Order'}, {
        'BOTSID': 'LineCountNumeric',
        'NumericContent': CTT
    })
def main(inn, out):
    sender = inn.ta_info['frompartner']
    sendername = inn.get({'BOTSID': 'STX', 'FROM.02': None})
    receiver = inn.ta_info['topartner']
    receivername = inn.get({'BOTSID': 'STX', 'UNTO.02': None})
    envid = inn.get({'BOTSID': 'STX', 'SNRF': None})
    envdtm = datewithcc(inn.get({'BOTSID': 'STX', 'TRDT.01': None}))
    envtime = inn.get({'BOTSID': 'STX', 'TRDT.02': None})
    if envtime:
        envdtm += envtime
    applref = inn.get({'BOTSID': 'STX', 'APRF': None})
    orgeditype = inn.ta_info['editype']
    orgmessagetype = inn.ta_info['messagetype']
    type = 'orders'
    version = '1'

    #do the ORDHDR
    for ord in inn.getloop({'BOTSID': 'STX'}, {
            'BOTSID': 'MHD',
            'TYPE.01': 'ORDHDR'
    }):
        docsrt = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'TYP', 'TCDE': None})
        mesfunc = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'TYP', 'TTYP': None})
        mesnum = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'FIL', 'FLGN': None})
        #get supplier
        SUgln = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'SDT', 'SIDN.01': None})
        SUbilnum = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SIDN.02': None
        })
        SUname = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'SDT', 'SNAM': None})
        SUaddressline1 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SADD.01': None
        })
        SUaddressline2 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SADD.02': None
        })
        SUaddressline3 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SADD.03': None
        })
        SUaddressline4 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SADD.04': None
        })
        SUpcode = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'SDT',
            'SADD.05': None
        })
        SUvatnum = transform.useoneof(
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'SDT',
                'VATN.02': None
            }), ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'SDT',
                'VATN.01': None
            }))
        #get buyer
        BYgln = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'CDT', 'CIDN.01': None})
        BYbilnum = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CIDN.02': None
        })
        BYname = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'CDT', 'CNAM': None})
        BYaddressline1 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CADD.01': None
        })
        BYaddressline2 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CADD.02': None
        })
        BYaddressline3 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CADD.03': None
        })
        BYaddressline4 = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CADD.04': None
        })
        BYpcode = ord.get({'BOTSID': 'MHD'}, {
            'BOTSID': 'CDT',
            'CADD.05': None
        })
        BYvatnum = transform.useoneof(
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CDT',
                'VATR.02': None
            }), ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CDT',
                'VATR.01': None
            }))
        #DNA/text
        headerbilas = []
        headertexts = []
        for DNA in ord.getloop({'BOTSID': 'MHD'}, {'BOTSID': 'DNA'}):
            code = DNA.get({'BOTSID': 'DNA', 'DNAC.01': None})
            val = DNA.get({'BOTSID': 'DNA', 'DNAC.02': None})
            if code:
                headerbilas.append((code, val))
            for dataelement in [('RTEX.01', 'RTEX.02'), ('RTEX.03', 'RTEX.04'),
                                ('RTEX.05', 'RTEX.06'),
                                ('RTEX.07', 'RTEX.08')]:
                code = DNA.get({'BOTSID': 'DNA', dataelement[0]: None})
                val = DNA.get({'BOTSID': 'DNA', dataelement[1]: None})
                if code:
                    headerbilas.append((code, val))
            for dataelement in ['GNAR.01', 'GNAR.02', 'GNAR.03', 'GNAR.04']:
                val = DNA.get({'BOTSID': 'DNA', dataelement: None})
                if val:
                    headertexts.append(('ORDHDR', val))

    out.put({'BOTSID': 'envelope'})  #needed to initialise the envelope
    #do the ORDERS
    for ord in inn.getloop({'BOTSID': 'STX'}, {
            'BOTSID': 'MHD',
            'TYPE.01': 'ORDERS'
    }):
        xord = out.putloop({'BOTSID': 'envelope'}, {'BOTSID': 'message'})
        #place envelope info in order
        xord.put({'BOTSID': 'message', 'sender': sender})
        xord.put({'BOTSID': 'message', 'sendername': sendername})
        xord.put({'BOTSID': 'message', 'receiver': receiver})
        xord.put({'BOTSID': 'message', 'receivername': receivername})
        xord.put({'BOTSID': 'message', 'envid': envid})
        xord.put({'BOTSID': 'message', 'envdtm': envdtm})
        xord.put({'BOTSID': 'message', 'applref': applref})
        xord.put({'BOTSID': 'message', 'orgeditype': orgeditype})
        xord.put({'BOTSID': 'message', 'orgmessagetype': orgmessagetype})
        xord.put({'BOTSID': 'message', 'type': type})
        xord.put({'BOTSID': 'message', 'version': version})
        xord.put({'BOTSID': 'message', 'docsrt': docsrt})
        xord.put({'BOTSID': 'message', 'mesfunc': mesfunc})
        xord.put({'BOTSID': 'message', 'mesnum': mesnum})

        #place order header info in order
        xord.put({'BOTSID': 'message', 'mesnum': mesnum})
        xord.put({'BOTSID': 'message', 'docsrt': docsrt})
        xord.put({'BOTSID': 'message', 'mesfunc': mesfunc})
        #write supplier
        party = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                             {'BOTSID': 'party'})
        party.put({'BOTSID': 'party', 'qual': 'SU'})
        party.put({'BOTSID': 'party', 'gln': SUgln})
        party.put({'BOTSID': 'party', 'bilnum': SUbilnum})
        party.put({'BOTSID': 'party', 'name': SUname})
        party.put({'BOTSID': 'party', 'addressline1': SUaddressline1})
        party.put({'BOTSID': 'party', 'addressline2': SUaddressline1})
        party.put({'BOTSID': 'party', 'addressline3': SUaddressline1})
        party.put({'BOTSID': 'party', 'addressline4': SUaddressline1})
        party.put({'BOTSID': 'party', 'pcode': SUpcode})
        party.put({'BOTSID': 'party', 'vatnum': SUvatnum})
        #write buyer
        party = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                             {'BOTSID': 'party'})
        party.put({'BOTSID': 'party', 'qual': 'BY'})
        party.put({'BOTSID': 'party', 'gln': BYgln})
        party.put({'BOTSID': 'party', 'bilnum': BYbilnum})
        party.put({'BOTSID': 'party', 'name': BYname})
        party.put({'BOTSID': 'party', 'addressline1': BYaddressline1})
        party.put({'BOTSID': 'party', 'addressline2': BYaddressline1})
        party.put({'BOTSID': 'party', 'addressline3': BYaddressline1})
        party.put({'BOTSID': 'party', 'addressline4': BYaddressline1})
        party.put({'BOTSID': 'party', 'pcode': BYpcode})
        party.put({'BOTSID': 'party', 'vatnum': BYvatnum})
        #DNA/text
        for bila in headerbilas:
            regel = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'bilas'},
                                 {'BOTSID': 'bila'})
            regel.put({'BOTSID': 'bila', 'qual': bila[0]})
            regel.put({'BOTSID': 'bila', 'val': bila[1]})
        for txt in headertexts:
            regel = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'txts'},
                                 {'BOTSID': 'txt'})
            regel.put({'BOTSID': 'txt', 'qual': txt[0]})
            regel.put({'BOTSID': 'txt', 'val': txt[1]})
        #end order header info
        #write delivery place
        party = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                             {'BOTSID': 'party'})
        party.put({'BOTSID': 'party', 'qual': 'DP'})
        party.put({
            'BOTSID':
            'party',
            'gln':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CLOC.01': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'bilnum':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CLOC.02': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'intcustomernum':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CLOC.03': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'name':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CNAM': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'addressline1':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CADD.01': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'addressline2':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CADD.02': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'addressline3':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CADD.03': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'addressline4':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CADD.04': None
            })
        })
        party.put({
            'BOTSID':
            'party',
            'pcode':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'CLO',
                'CADD.05': None
            })
        })

        xord.put({
            'BOTSID':
            'message',
            'docnum':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'ORNO.01': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'docnumvv':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'ORNO.02': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'docdtm':
            datewithcc(
                ord.get({'BOTSID': 'MHD'}, {
                    'BOTSID': 'ORD',
                    'ORNO.03': None
                }))
        })
        xord.put({
            'BOTSID':
            'message',
            'docdtmvv':
            datewithcc(
                ord.get({'BOTSID': 'MHD'}, {
                    'BOTSID': 'ORD',
                    'ORNO.04': None
                }))
        })
        xord.put({
            'BOTSID':
            'message',
            'classification':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'CLAS': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'orcd':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'ORCD': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'projectnum':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'SCRF.01': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'contractnum':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'ORD',
                'SCRF.02': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'earldeldtm':
            datewithcc(
                ord.get({'BOTSID': 'MHD'}, {
                    'BOTSID': 'DIN',
                    'EDAT': None
                }))
        })
        xord.put({
            'BOTSID':
            'message',
            'latedeldtm':
            datewithcc(
                ord.get({'BOTSID': 'MHD'}, {
                    'BOTSID': 'DIN',
                    'LDAT': None
                }))
        })
        xord.put({
            'BOTSID':
            'message',
            'deltimefrom':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'DIN',
                'RATM.01': None
            })
        })
        xord.put({
            'BOTSID':
            'message',
            'deltimetill':
            ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'DIN',
                'RATM.02': None
            })
        })
        #DIN/text
        val = ord.get({'BOTSID': 'MHD'}, {'BOTSID': 'DIN', 'DINN': None})
        if val:
            regel = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'bilas'},
                                 {'BOTSID': 'bila'})
            regel.put({'BOTSID': 'bila', 'qual': 'DELSTD'})
            regel.put({'BOTSID': 'bila', 'val': val})
        for dataelement in ['DINS.01', 'DINS.02', 'DINS.03', 'DINS.04']:
            val = ord.get({'BOTSID': 'MHD'}, {
                'BOTSID': 'DIN',
                dataelement: None
            })
            if val:
                regel = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'txts'},
                                     {'BOTSID': 'txt'})
                regel.put({'BOTSID': 'txt', 'qual': 'DEL'})
                regel.put({'BOTSID': 'txt', 'val': val})

        #DNA/text
        for DNA in ord.getloop({'BOTSID': 'MHD'}, {'BOTSID': 'DNA'}):
            code = DNA.get({'BOTSID': 'DNA', 'DNAC.01': None})
            val = DNA.get({'BOTSID': 'DNA', 'DNAC.02': None})
            if code:
                regel = xord.putloop({'BOTSID': 'message'},
                                     {'BOTSID': 'bilas'}, {'BOTSID': 'bila'})
                regel.put({'BOTSID': 'bila', 'qual': code})
                regel.put({'BOTSID': 'bila', 'val': val})
            for dataelement in [('RTEX.01', 'RTEX.02'), ('RTEX.03', 'RTEX.04'),
                                ('RTEX.05', 'RTEX.06'),
                                ('RTEX.07', 'RTEX.08')]:
                code = DNA.get({'BOTSID': 'DNA', dataelement[0]: None})
                val = DNA.get({'BOTSID': 'DNA', dataelement[1]: None})
                if code:
                    regel = xord.putloop({'BOTSID': 'message'},
                                         {'BOTSID': 'bilas'},
                                         {'BOTSID': 'bila'})
                    regel.put({'BOTSID': 'bila', 'qual': code})
                    regel.put({'BOTSID': 'bila', 'val': val})
            for dataelement in ['GNAR.01', 'GNAR.02', 'GNAR.03', 'GNAR.04']:
                val = DNA.get({'BOTSID': 'DNA', dataelement: None})
                if val:
                    regel = xord.putloop({'BOTSID': 'message'},
                                         {'BOTSID': 'txts'}, {'BOTSID': 'txt'})
                    regel.put({'BOTSID': 'txt', 'qual': 'ORDERS'})
                    regel.put({'BOTSID': 'txt', 'val': val})

        for lin in ord.getloop({'BOTSID': 'MHD'}, {'BOTSID': 'OLD'}):
            xlin = xord.putloop({'BOTSID': 'message'}, {'BOTSID': 'lines'},
                                {'BOTSID': 'line'})
            xlin.put({
                'BOTSID':
                'line',
                'gtin':
                transform.useoneof(
                    lin.get({
                        'BOTSID': 'OLD',
                        'SPRO.01': None
                    }),
                    lin.get({
                        'BOTSID': 'OLD',
                        'CPRO.01': None
                    }),
                    lin.get({
                        'BOTSID': 'OLD',
                        'SPRO.03': None
                    }),
                )
            })
            xlin.put({
                'BOTSID': 'line',
                'gtincu': lin.get({
                    'BOTSID': 'OLD',
                    'SACU': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'suart': lin.get({
                    'BOTSID': 'OLD',
                    'SPRO.02': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'byart': lin.get({
                    'BOTSID': 'OLD',
                    'CPRO.02': None
                })
            })
            xlin.put({
                'BOTSID':
                'line',
                'num':
                transform.useoneof(lin.get({
                    'BOTSID': 'OLD',
                    'OQTY.01': None
                }), lin.get({
                    'BOTSID': 'OLD',
                    'OQTY.02': None
                }))
            })
            xlin.put({
                'BOTSID': 'line',
                'ordqua': lin.get({
                    'BOTSID': 'OLD',
                    'UNOR.02': None
                })
            })
            xlin.put({
                'BOTSID':
                'line',
                'ordunit':
                transform.useoneof(lin.get({
                    'BOTSID': 'OLD',
                    'OQTY.03': None
                }), lin.get({
                    'BOTSID': 'OLD',
                    'UNOR.03': None
                }))
            })
            xlin.put({
                'BOTSID':
                'line',
                'numbergtincu':
                lin.get({
                    'BOTSID': 'OLD',
                    'UNOR.01': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'desc': lin.get({
                    'BOTSID': 'OLD',
                    'TDES.01': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'pdnum': lin.get({
                    'BOTSID': 'OLD',
                    'PIND': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'projectnum': lin.get({
                    'BOTSID': 'OLD',
                    'SCRF.01': None
                })
            })
            xlin.put({
                'BOTSID': 'line',
                'contractnum': lin.get({
                    'BOTSID': 'OLD',
                    'SCRF.02': None
                })
            })
            price = lin.get({'BOTSID': 'OLD', 'OUCT.01': None})
            if price:
                xpri = xlin.putloop({'BOTSID': 'line'}, {'BOTSID': 'pris'},
                                    {'BOTSID': 'pri'})
                xpri.put({'BOTSID': 'pri', 'qual': 'XXX'})
                xpri.put({'BOTSID': 'pri', 'price': price})
                xpri.put({
                    'BOTSID': 'pri',
                    'unit': lin.get({
                        'BOTSID': 'OLD',
                        'OUCT.02': None
                    })
                })
            #DNB/text line level
            for DNA in lin.getloop({'BOTSID': 'OLD'}, {'BOTSID': 'DNB'}):
                code = DNA.get({'BOTSID': 'DNB', 'DNAC.01': None})
                val = DNA.get({'BOTSID': 'DNB', 'DNAC.02': None})
                if code:
                    regel = xlin.putloop({'BOTSID': 'line'},
                                         {'BOTSID': 'bilas'},
                                         {'BOTSID': 'bila'})
                    regel.put({'BOTSID': 'bila', 'qual': code})
                    regel.put({'BOTSID': 'bila', 'val': val})
                for dataelement in [('RTEX.01', 'RTEX.02'),
                                    ('RTEX.03', 'RTEX.04'),
                                    ('RTEX.05', 'RTEX.06'),
                                    ('RTEX.07', 'RTEX.08')]:
                    code = DNA.get({'BOTSID': 'DNB', dataelement[0]: None})
                    val = DNA.get({'BOTSID': 'DNB', dataelement[1]: None})
                    if code:
                        regel = xlin.putloop({'BOTSID': 'line'},
                                             {'BOTSID': 'bilas'},
                                             {'BOTSID': 'bila'})
                        regel.put({'BOTSID': 'bila', 'qual': code})
                        regel.put({'BOTSID': 'bila', 'val': val})
                for dataelement in [
                        'GNAR.01', 'GNAR.02', 'GNAR.03', 'GNAR.04'
                ]:
                    val = DNA.get({'BOTSID': 'DNB', dataelement: None})
                    if val:
                        regel = xlin.putloop({'BOTSID': 'line'},
                                             {'BOTSID': 'txts'},
                                             {'BOTSID': 'txt'})
                        regel.put({'BOTSID': 'txt', 'qual': 'ORDLINE'})
                        regel.put({'BOTSID': 'txt', 'val': val})
Beispiel #11
0
def main(inn, out):

    # Get Header and Partner Information
    test_indicator = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                             {'BOTSID': 'UBLExtension'},
                             {'BOTSID': 'ExtensionContent'},
                             {'BOTSID': 'DocumentEnvelope'}, {
                                 'BOTSID': 'CopyIndicator',
                                 'IdentifierContent': None
                             })
    inn.ta_info['testindicator'] = test_indicator
    out.ta_info['ISA15'] = test_indicator
    out.ta_info['testindicator'] = test_indicator

    from_partner = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                           {'BOTSID': 'UBLExtension'},
                           {'BOTSID': 'ExtensionContent'},
                           {'BOTSID': 'DocumentEnvelope'},
                           {'BOTSID': 'SenderParty'},
                           {'BOTSID': 'PartyIdentification'}, {
                               'BOTSID': 'ID',
                               'IdentifierContent': None
                           })
    inn.ta_info['frompartner'] = from_partner
    out.ta_info['frompartner'] = from_partner

    from_partner_qual = inn.get(
        {'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
        {'BOTSID': 'UBLExtension'}, {'BOTSID': 'ExtensionContent'},
        {'BOTSID': 'DocumentEnvelope'}, {'BOTSID': 'SenderParty'},
        {'BOTSID': 'PartyIdentification'}, {
            'BOTSID': 'ID',
            'IdentificationSchemeNameText': None
        })
    inn.ta_info['ISA05'] = from_partner_qual
    out.ta_info['ISA05'] = from_partner_qual

    to_partner = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                         {'BOTSID': 'UBLExtension'},
                         {'BOTSID': 'ExtensionContent'},
                         {'BOTSID': 'DocumentEnvelope'},
                         {'BOTSID': 'ReceiverParty'},
                         {'BOTSID': 'PartyIdentification'}, {
                             'BOTSID': 'ID',
                             'IdentifierContent': None
                         })
    inn.ta_info['topartner'] = to_partner
    out.ta_info['topartner'] = to_partner

    to_partner_qual = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                              {'BOTSID': 'UBLExtension'},
                              {'BOTSID': 'ExtensionContent'},
                              {'BOTSID': 'DocumentEnvelope'},
                              {'BOTSID': 'ReceiverParty'},
                              {'BOTSID': 'PartyIdentification'}, {
                                  'BOTSID': 'ID',
                                  'IdentificationSchemeNameText': None
                              })
    inn.ta_info['ISA07'] = to_partner_qual
    out.ta_info['ISA07'] = to_partner_qual

    isa_date = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                       {'BOTSID': 'UBLExtension'},
                       {'BOTSID': 'ExtensionContent'},
                       {'BOTSID': 'DocumentEnvelope'}, {
                           'BOTSID': 'IssueDate',
                           'DateContent': None
                       })
    isa_date = transform.datemask(isa_date, 'YYYY-mm-DD', 'YYmmDD')
    out.ta_info['ISA09'] = isa_date

    isa_Time = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                       {'BOTSID': 'UBLExtension'},
                       {'BOTSID': 'ExtensionContent'},
                       {'BOTSID': 'DocumentEnvelope'}, {
                           'BOTSID': 'IssueTime',
                           'TimeContent': None
                       })
    isa_Time = transform.datemask(isa_Time, "00:00", "0000")
    out.ta_info['ISA10'] = isa_Time

    version = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'UBLExtensions'},
                      {'BOTSID': 'UBLExtension'},
                      {'BOTSID': 'ExtensionContent'},
                      {'BOTSID': 'DocumentEnvelope'}, {
                          'BOTSID': 'VersionID',
                          'IdentifierContent': None
                      })
    out.ta_info['version'] = version

    # ST Segment
    out.put({'BOTSID': 'ST', 'ST01': '850'})
    out.put({'BOTSID': 'ST', 'ST02': out.ta_info['reference'].zfill(4)})

    # BEG Segemnt
    purpose_code = inn.get({'BOTSID': 'Order'}, {
        'BOTSID': 'PurposeCode',
        'CodeContent': None
    })
    ordertype_code = inn.get({'BOTSID': 'Order'}, {
        'BOTSID': 'OrderTypeCode',
        'CodeContent': None
    })

    order_number = inn.get({'BOTSID': 'Order'}, {
        'BOTSID': 'ID',
        'IdentifierContent': None
    })
    inn.ta_info['botskey'] = order_number
    out.ta_info['botskey'] = order_number

    order_date = inn.get({'BOTSID': 'Order'}, {
        'BOTSID': 'IssueDate',
        'DateContent': None
    })
    order_date = transform.datemask(order_date, 'mm/DD/YYYY', 'YYYYmmDD')

    contract_number = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Contract'}, {
        'BOTSID': 'ID',
        'IdentifierContent': None
    })

    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG01': purpose_code})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG02': ordertype_code})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG03': order_number})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG05': order_date})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG06': contract_number})

    # DTM Segment
    EstimatedDeliveryDateQual = inn.get({'BOTSID': 'Order'},
                                        {'BOTSID': 'Delivery'},
                                        {'BOTSID': 'RequestedDeliveryPeriod'},
                                        {
                                            'BOTSID': 'DescriptionCode',
                                            'CodeContent': None
                                        })
    EstimatedDeliveryDate = inn.get({'BOTSID': 'Order'},
                                    {'BOTSID': 'Delivery'},
                                    {'BOTSID': 'RequestedDeliveryPeriod'}, {
                                        'BOTSID': 'StartDate',
                                        'DateContent': None
                                    })
    EstimatedDeliveryDate = transform.datemask(EstimatedDeliveryDate,
                                               'mm/DD/YYYY', 'YYYYmmDD')

    if EstimatedDeliveryDateQual == '017':
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'DTM',
            'DTM01': EstimatedDeliveryDateQual
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'DTM',
            'DTM02': EstimatedDeliveryDate
        })

    # TD5 Segment
    SCAC = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                   {'BOTSID': 'CarrierParty'},
                   {'BOTSID': 'PartyIdentification'}, {
                       'BOTSID': 'ID',
                       'IdentifierContent': None
                   })
    CarrierName = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                          {'BOTSID': 'CarrierParty'}, {'BOTSID': 'PartyName'},
                          {
                              'BOTSID': 'Name',
                              'TextContent': None
                          })

    out.put({'BOTSID': 'ST'}, {'BOTSID': 'TD5', 'TD502': '2'})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'TD5', 'TD503': SCAC})
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'TD5', 'TD505': CarrierName})

    # N1  - BT
    PartyQual = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'BuyerCustomerParty'},
                        {'BOTSID': 'Party'}, {
                            'BOTSID': 'PartyIdentification',
                            'IdentificationSchemeIdentifier': None
                        })
    PartyName = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'BuyerCustomerParty'},
                        {'BOTSID': 'Party'}, {'BOTSID': 'PartyName'}, {
                            'BOTSID': 'Name',
                            'TextContent': None
                        })
    PartyAgency = inn.get({'BOTSID': 'Order'},
                          {'BOTSID': 'BuyerCustomerParty'},
                          {'BOTSID': 'Party'}, {
                              'BOTSID': 'PartyIdentification',
                              'IdentificationSchemeAgencyIdentifier': None
                          })
    PartyNumber = inn.get({'BOTSID': 'Order'},
                          {'BOTSID': 'BuyerCustomerParty'},
                          {'BOTSID': 'Party'}, {
                              'BOTSID': 'PartyIdentification',
                              'IdentifierContent': None
                          })

    if PartyQual == 'BT':
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N101': PartyQual})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N102': PartyName})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N103': PartyAgency})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N104': PartyNumber})

        PartyAddress1 = inn.get({'BOTSID': 'Order'},
                                {'BOTSID': 'BuyerCustomerParty'},
                                {'BOTSID': 'Party'},
                                {'BOTSID': 'PostalAddress'}, {
                                    'BOTSID': 'StreetName',
                                    'TextContent': None
                                })
        PartyAddress2 = inn.get({'BOTSID': 'Order'},
                                {'BOTSID': 'BuyerCustomerParty'},
                                {'BOTSID': 'Party'},
                                {'BOTSID': 'PostalAddress'}, {
                                    'BOTSID': 'AdditionalStreetName',
                                    'TextContent': None
                                })

        PartyCity = inn.get({'BOTSID': 'Order'},
                            {'BOTSID': 'BuyerCustomerParty'},
                            {'BOTSID': 'Party'}, {'BOTSID': 'PostalAddress'}, {
                                'BOTSID': 'CityName',
                                'TextContent': None
                            })
        PartyCountrySubentity = inn.get({'BOTSID': 'Order'},
                                        {'BOTSID': 'BuyerCustomerParty'},
                                        {'BOTSID': 'Party'},
                                        {'BOTSID': 'PostalAddress'}, {
                                            'BOTSID': 'CountrySubentity',
                                            'TextContent': None
                                        })
        PartyPostal = inn.get({'BOTSID': 'Order'},
                              {'BOTSID': 'BuyerCustomerParty'},
                              {'BOTSID': 'Party'}, {'BOTSID': 'PostalAddress'},
                              {
                                  'BOTSID': 'PostalZone',
                                  'TextContent': None
                              })
        PartyCountry = inn.get({'BOTSID': 'Order'},
                               {'BOTSID': 'BuyerCustomerParty'},
                               {'BOTSID': 'Party'},
                               {'BOTSID': 'PostalAddress'},
                               {'BOTSID': 'Country'}, {
                                   'BOTSID': 'IdentificationCode',
                                   'CodeContent': None
                               })

        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N3',
            'N301': PartyAddress1
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N3',
            'N302': PartyAddress2
        })

        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N4',
            'N401': PartyCity
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N4',
            'N402': PartyCountrySubentity
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N4',
            'N403': PartyPostal
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'BT',
        }, {
            'BOTSID': 'N4',
            'N404': PartyCountry
        })

    # N1  - ST
    PartyQual = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                        {'BOTSID': 'DeliveryAddress'}, {
                            'BOTSID': 'ID',
                            'IdentificationSchemeIdentifier': None
                        })
    PartyName = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                        {'BOTSID': 'DeliveryAddress'}, {
                            'BOTSID': 'BuildingName',
                            'TextContent': None
                        })
    PartyAgency = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                          {'BOTSID': 'DeliveryAddress'}, {
                              'BOTSID': 'ID',
                              'IdentificationSchemeAgencyIdentifier': None
                          })
    PartyNumber = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                          {'BOTSID': 'DeliveryAddress'}, {
                              'BOTSID': 'ID',
                              'IdentifierContent': None
                          })

    if PartyQual == 'ST':
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N101': PartyQual})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N102': PartyName})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N103': PartyAgency})
        out.put({'BOTSID': 'ST'}, {'BOTSID': 'N1', 'N104': PartyNumber})

        PartyAddress1 = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                                {'BOTSID': 'DeliveryAddress'}, {
                                    'BOTSID': 'StreetName',
                                    'TextContent': None
                                })
        PartyAddress2 = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                                {'BOTSID': 'DeliveryAddress'}, {
                                    'BOTSID': 'AdditionalStreetName',
                                    'TextContent': None
                                })

        PartyCity = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                            {'BOTSID': 'DeliveryAddress'}, {
                                'BOTSID': 'CityName',
                                'TextContent': None
                            })
        PartyCountrySubentity = inn.get({'BOTSID': 'Order'},
                                        {'BOTSID': 'Delivery'},
                                        {'BOTSID': 'DeliveryAddress'}, {
                                            'BOTSID': 'CountrySubentity',
                                            'TextContent': None
                                        })
        PartyPostal = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                              {'BOTSID': 'DeliveryAddress'}, {
                                  'BOTSID': 'PostalZone',
                                  'TextContent': None
                              })
        PartyCountry = inn.get({'BOTSID': 'Order'}, {'BOTSID': 'Delivery'},
                               {'BOTSID': 'DeliveryAddress'},
                               {'BOTSID': 'Country'}, {
                                   'BOTSID': 'IdentificationCode',
                                   'CodeContent': None
                               })

        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N3',
            'N301': PartyAddress1
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N3',
            'N302': PartyAddress2
        })

        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N4',
            'N401': PartyCity
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N4',
            'N402': PartyCountrySubentity
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N4',
            'N403': PartyPostal
        })
        out.put({'BOTSID': 'ST'}, {
            'BOTSID': 'N1',
            'N101': 'ST',
        }, {
            'BOTSID': 'N4',
            'N404': PartyCountry
        })

    # Item Lines
    for line in inn.getloop({'BOTSID': 'Order'}, {'BOTSID': 'OrderLine'}):

        # Out Object Array
        lineout = out.putloop({'BOTSID': 'ST'}, {'BOTSID': 'PO1'})

        # Get Values
        LineNumber = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                              {
                                  'BOTSID': 'ID',
                                  'IdentifierContent': None
                              })

        # Get UOM, if empty use 'EA'
        ISO_UOM = transform.useoneof(
            line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'}, {
                'BOTSID': 'Quantity',
                'QuantityUnitCode': None
            }), 'EA')
        X12_UOM = transform.reverse_ccode('X12_ELEM_355_TO_ISO_UOM', ISO_UOM)

        QuantityOrdered = line.get({'BOTSID': 'OrderLine'},
                                   {'BOTSID': 'LineItem'}, {
                                       'BOTSID': 'Quantity',
                                       'QuantityContent': None
                                   })

        ISO_PriceCode = transform.useoneof(
            line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                     {'BOTSID': 'Price'}, {
                         'BOTSID': 'PriceTypeCode',
                         'CodeContent': None
                     }), 'PE')
        X12_PriceCode = transform.reverse_ccode(
            'X12_ELEM_639_TO_ISO_PRICETYPE', ISO_PriceCode)
        UnitPrice = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                             {'BOTSID': 'Price'}, {
                                 'BOTSID': 'PriceAmount',
                                 'AmountContent': None
                             })

        # Get Item Values by Qualifier
        BuyerItem = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                             {'BOTSID': 'Item'},
                             {'BOTSID': 'BuyersItemIdentification'}, {
                                 'BOTSID': 'ID',
                                 'IdentifierContent': None
                             })
        SellerItem = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                              {'BOTSID': 'Item'},
                              {'BOTSID': 'SellersItemIdentification'}, {
                                  'BOTSID': 'ID',
                                  'IdentifierContent': None
                              })

        StandardItemQual = line.get({'BOTSID': 'OrderLine'},
                                    {'BOTSID': 'LineItem'}, {'BOTSID': 'Item'},
                                    {'BOTSID': 'StandardItemIdentification'}, {
                                        'BOTSID': 'ID',
                                        'IdentificationSchemeIdentifier': None
                                    })
        UPC = ""
        if StandardItemQual == 'UPC':
            UPC = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                           {'BOTSID': 'Item'},
                           {'BOTSID': 'StandardItemIdentification'}, {
                               'BOTSID': 'ID',
                               'IdentifierContent': None
                           })

        Description = line.get({'BOTSID': 'OrderLine'}, {'BOTSID': 'LineItem'},
                               {'BOTSID': 'Item'}, {
                                   'BOTSID': 'Description',
                                   'TextContent': None
                               })

        # Write output
        lineout.put({'BOTSID': 'PO1', 'PO101': LineNumber})
        lineout.put({'BOTSID': 'PO1', 'PO102': QuantityOrdered})
        lineout.put({'BOTSID': 'PO1', 'PO103': X12_UOM})
        lineout.put({'BOTSID': 'PO1', 'PO104': UnitPrice})
        lineout.put({'BOTSID': 'PO1', 'PO105': X12_PriceCode})

        lineout.put({'BOTSID': 'PO1', 'PO106': 'IN'})
        lineout.put({'BOTSID': 'PO1', 'PO107': SellerItem})

        lineout.put({'BOTSID': 'PO1', 'PO108': 'SK'})
        lineout.put({'BOTSID': 'PO1', 'PO109': BuyerItem})

        if StandardItemQual == 'UPC' and UPC != "":
            lineout.put({'BOTSID': 'PO1', 'PO110': 'UP'})
            lineout.put({'BOTSID': 'PO1', 'PO111': UPC})

        lineout.put({'BOTSID': 'PO1'}, {
            'BOTSID': 'PID',
            'PID01': 'F',
            'PID05': Description
        })

    # last line (counts the segments produced in out-message)
    out.put({'BOTSID': 'ST'}, {'BOTSID': 'SE', 'SE01': out.getcount() + 1})
    out.put({'BOTSID': 'ST'}, {
        'BOTSID': 'SE',
        'SE02': (out.ta_info['reference']).zfill(4)
    })
def main(inn, out):
    #pick up some values from ISA envelope
    out.put({'BOTSID': 'message', 'sender': inn.ta_info['frompartner']})
    out.put({'BOTSID': 'message', 'receiver': inn.ta_info['topartner']})
    out.put({
        'BOTSID': 'message',
        'testindicator': inn.ta_info['testindicator']
    })

    #pick up document number. is used in bots to give 'document-view'
    docnum = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG03': None})
    out.put({'BOTSID': 'message', 'docnum': docnum})
    inn.ta_info['botskey'] = docnum
    out.ta_info['botskey'] = docnum

    out.put({
        'BOTSID':
        'message',
        'docsrt':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'BEG',
            'BEG02': None
        })
    })
    #convert dates to right internal format
    docdtm = inn.get({'BOTSID': 'ST'}, {'BOTSID': 'BEG', 'BEG05': None})
    docdtm = transform.datemask(docdtm, 'CCYYMMDDHHMM', 'CCYY-MM-DD')
    out.put({'BOTSID': 'message', 'docdtm': docdtm})
    #same date handling as above, now in a one-liner.
    out.put({
        'BOTSID':
        'message',
        'deldtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'DTM',
                'DTM01': '002',
                'DTM02': None
            }), 'CCYYMMDDHHMM', 'CCYY-MM-DD')
    })
    out.put({
        'BOTSID':
        'message',
        'earldeldtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'DTM',
                'DTM01': '064',
                'DTM02': None
            }), 'CCYYMMDDHHMM', 'CCYY-MM-DD')
    })
    out.put({
        'BOTSID':
        'message',
        'latedeldtm':
        transform.datemask(
            inn.get({'BOTSID': 'ST'}, {
                'BOTSID': 'DTM',
                'DTM01': '063',
                'DTM02': None
            }), 'CCYYMMDDHHMM', 'CCYY-MM-DD')
    })

    out.put({
        'BOTSID':
        'message',
        'currency':
        inn.get({'BOTSID': 'ST'}, {
            'BOTSID': 'CUR',
            'CUR01': 'BY',
            'CUR02': None
        })
    })

    #loop over partys
    for party in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'N1'}):
        pou = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'partys'},
                          {'BOTSID': 'party'})
        pou.put({
            'BOTSID': 'party',
            'qual': party.get({
                'BOTSID': 'N1',
                'N101': None
            })
        })
        pou.put({
            'BOTSID': 'party',
            'gln': party.get({
                'BOTSID': 'N1',
                'N103': 'UL',
                'N104': None
            })
        })
        #get DUNS number. 2 qualifiers are used; helper function transform.useoneof checks both
        pou.put({
            'BOTSID':
            'party',
            'DUNS':
            transform.useoneof(
                party.get({
                    'BOTSID': 'N1',
                    'N103': '1',
                    'N104': None
                }), party.get({
                    'BOTSID': 'N1',
                    'N103': '9',
                    'N104': None
                }))
        })
        pou.put({
            'BOTSID':
            'party',
            'externalID':
            party.get({
                'BOTSID': 'N1',
                'N103': '92',
                'N104': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'internalID':
            party.get({
                'BOTSID': 'N1',
                'N103': '91',
                'N104': None
            })
        })
        pou.put({
            'BOTSID': 'party',
            'name1': party.get({
                'BOTSID': 'N1',
                'N102': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'name2':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N2',
                'N201': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'address1':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N301': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'address2':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N3',
                'N302': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'city':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N401': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'state':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N402': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'pcode':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N403': None
            })
        })
        pou.put({
            'BOTSID':
            'party',
            'country':
            party.get({'BOTSID': 'N1'}, {
                'BOTSID': 'N4',
                'N404': None
            })
        })

    #mapping above is for full addresses. Often only party-IDs are used (good EDI practices.....
    #mapping could be simpler, eg:
    #~ out.put({'BOTSID':'message','buyer_ID':inn.get({'BOTSID':'ST'},{'BOTSID':'N1','N101':'BY','N4':None})})
    #~ out.put({'BOTSID':'message','delivery_ID':inn.get({'BOTSID':'ST'},{'BOTSID':'N1','N101':'ST','N4':None})})

    #loop over lines
    for po1 in inn.getloop({'BOTSID': 'ST'}, {'BOTSID': 'PO1'}):
        lou = out.putloop({'BOTSID': 'message'}, {'BOTSID': 'lines'},
                          {'BOTSID': 'line'})
        lou.put({
            'BOTSID': 'line',
            'linenum': po1.get({
                'BOTSID': 'PO1',
                'PO101': None
            })
        })
        lou.put({
            'BOTSID': 'line',
            'ordqua': po1.get({
                'BOTSID': 'PO1',
                'PO102': None
            })
        })
        #get ordering unit; if not there use 'EA' 9each, pice) as default
        lou.put({
            'BOTSID':
            'line',
            'ordunit':
            transform.useoneof(po1.get({
                'BOTSID': 'PO1',
                'PO103': None
            }), 'EA')
        })
        lou.put({
            'BOTSID': 'line',
            'price': po1.get({
                'BOTSID': 'PO1',
                'PO104': None
            })
        })
        lou.put({'BOTSID': 'line', 'gtin': get_art_num(po1, 'UP')})
        lou.put({'BOTSID': 'line', 'suart': get_art_num(po1, 'VN')})
        #get buyers article number; 1 different qualifiers are used; helper function transform.useoneof checks both
        lou.put({
            'BOTSID':
            'line',
            'byart':
            transform.useoneof(get_art_num(po1, 'IN'), get_art_num(po1, 'BP'))
        })
        lou.put({
            'BOTSID':
            'line',
            'desc':
            po1.get({'BOTSID': 'PO1'}, {
                'BOTSID': 'PID',
                'PID01': 'F',
                'PID05': None
            })
        })
def main(inn,out):
    #sender, receiver is correct via QUERIES in grammar. 
    out.put({'BOTSID':'ST','ST01':'856','ST02':out.ta_info['reference'].zfill(4)})
    
    out.put({'BOTSID':'ST'},{'BOTSID':'BSN','BSN01':'00'})
    out.put({'BOTSID':'ST'},{'BOTSID':'BSN','BSN02':inn.get({'BOTSID':'message','docnum':None})})
    out.put({'BOTSID':'ST'},{'BOTSID':'BSN','BSN03':transform.datemask(inn.get({'BOTSID':'message','docdtm':None}),'CCYY-MM-DD HH:mm','CCYYMMDD')})
    out.put({'BOTSID':'ST'},{'BOTSID':'BSN','BSN04':transform.useoneof(inn.get({'BOTSID':'message','doctime':None}),'0000')})
    out.put({'BOTSID':'ST'},{'BOTSID':'BSN','BSN05':'0002'})        #0001: Shipment, Order, Item (pfff, just making this up)

    #***********************************************************************************************
    #shipment level*********************************************************************************
    hlcounter = 1       #HL segments have sequentail count
    shipment = out.putloop({'BOTSID':'ST'},{'BOTSID':'HL','HL01':hlcounter,'HL03':'S'})
    currentshipment = hlcounter     #remember the current counter, as child-HL segments have to point to this shipment
    hlcounter += 1
    
    shipment.put({'BOTSID':'HL'},{'BOTSID':'TD5','TD501':'O','TD502':'2','TD503':inn.get({'BOTSID':'message','scac':None})})
    shipment.put({'BOTSID':'HL'},{'BOTSID':'REF','REF01':'BM','REF02':inn.get({'BOTSID':'message','bol':None})})
    shipment.put({'BOTSID':'HL'},{'BOTSID':'REF','REF01':'CN','REF02':inn.get({'BOTSID':'message','carrierreferencenumber':None})})
    shipment.put({'BOTSID':'HL'},{'BOTSID':'DTM','DTM01':'011','DTM02':transform.datemask(inn.get({'BOTSID':'message','shipdtm':None}),'CCYY-MM-DD HH:mm','CCYYMMDD')})
    shipment.put({'BOTSID':'HL'},{'BOTSID':'DTM','DTM01':'017','DTM02':transform.datemask(inn.get({'BOTSID':'message','deldtm':None}),'CCYY-MM-DD HH:mm','CCYYMMDD')})

    #loop over partys (all on shipment level for simplicity ;-)
    for party in inn.getloop({'BOTSID':'message'},{'BOTSID':'partys'},{'BOTSID':'party'}): 
        pou = shipment.putloop({'BOTSID':'HL'},{'BOTSID':'N1'})
        pou.put({'BOTSID':'N1','N101':party.get({'BOTSID':'party','qual':None})})
        #write partyID: if gln write it, else  DUNS etc
        #this uses the fact the out.put returns True if succeeded. 
        if pou.put({'BOTSID':'N1','N103':'UL','N104':party.get({'BOTSID':'party','gln':None})}):
            pass
        elif pou.put({'BOTSID':'N1','N103':'1','N104':party.get({'BOTSID':'party','DUNS':None})}):
            pass
        elif pou.put({'BOTSID':'N1','N103':'92','N104':party.get({'BOTSID':'party','externalID':None})}):
            pass
        else:
            pou.put({'BOTSID':'N1','N103':'91','N104':party.get({'BOTSID':'party','internalID':None})})
        pou.put({'BOTSID':'N1','N102':party.get({'BOTSID':'party','name1':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N2','N201':party.get({'BOTSID':'party','name2':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N3','N301':party.get({'BOTSID':'party','address1':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N3','N302':party.get({'BOTSID':'party','address2':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N4','N401':party.get({'BOTSID':'party','city':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N4','N402':party.get({'BOTSID':'party','state':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N4','N403':party.get({'BOTSID':'party','pcode':None})})
        pou.put({'BOTSID':'N1'},{'BOTSID':'N4','N404':party.get({'BOTSID':'party','country':None})})

    #********************************************************************************************
    #order level*********************************************************************************
    for order in inn.getloop({'BOTSID':'message'},{'BOTSID':'orders'},{'BOTSID':'order'}):
        ordernode = out.putloop({'BOTSID':'ST'},{'BOTSID':'HL','HL01':hlcounter,'HL02':currentshipment,'HL03':'O'})
        currentorder = hlcounter
        hlcounter += 1
        ordernode.put({'BOTSID':'HL'},{'BOTSID':'PRF','PRF01':order.get({'BOTSID':'order','ordernumber':None})})
        ordernode.put({'BOTSID':'HL'},{'BOTSID':'PRF','PRF04':transform.datemask(order.get({'BOTSID':'order','orderdtm':None}),'CCYY-MM-DD HH:mm','CCYYMMDD')})


        #***************************************************************************************************
        #line/article level*********************************************************************************
        for lin in order.getloop({'BOTSID':'order'},{'BOTSID':'lines'},{'BOTSID':'line'}):
            itemnode = out.putloop({'BOTSID':'ST'},{'BOTSID':'HL','HL01':hlcounter,'HL02':currentorder,'HL03':'I'})
            hlcounter += 1
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'LIN','LIN01':lin.get({'BOTSID':'line','linenum':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'LIN','LIN02':'UP','LIN03':lin.get({'BOTSID':'line','gtin':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'LIN','LIN04':'VN','LIN05':lin.get({'BOTSID':'line','suart':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'LIN','LIN06':'BP','LIN07':lin.get({'BOTSID':'line','byart':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'SN1','SN103':'EA','SN102':lin.get({'BOTSID':'line','delqua':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'SN1','SN106':'EA','SN105':lin.get({'BOTSID':'line','ordqua':None})})
            itemnode.put({'BOTSID':'HL'},{'BOTSID':'PID','PID01':'F','PID02':'08','PID05':lin.get({'BOTSID':'line','desc':None})})


    out.put({'BOTSID':'ST'},{'BOTSID':'CTT','CTT01':out.getcountoccurrences({'BOTSID':'ST'},{'BOTSID':'HL'},{'BOTSID':'LIN'}) }) #bots counts line items
    out.put({'BOTSID':'ST'},{'BOTSID':'SE','SE01':out.getcount()+1,'SE02':out.ta_info['reference'].zfill(4)})  #SE01: bots counts the segments produced in the X12 message.