from testcase.test_scene1.scene1_mainflow import *


SIT_URL = 'http://scdev.cmic.com.cn:8088/beta/'#SIT环境地址
#通行证
usr1,pwd1 = 'bj_01','Gyxc1234'
usr2,pwd2 = 'bj_02','Gyxc1234'
usr3,pwd3 = 'bj_03','Gyxc1234'
usr4,pwd4 = 'bj_04','Gyxc1234'
usr5,pwd5 = 'bj_05','Gyxc1234'
outwarehouse= '103356'#调拨出库仓
inwarehouse= '103396'#调拨入库仓
salesman = '杨春刚'#业务员名称
productcode = 10005528#商品编码

STASHIP_TEMP = 24#启运温度
ARRIVAL_TEMP = 24#到货温度
STASHIP_DATE = '2023-10-01'#启运日期
STASHIP_TIME = '00:00:00'#启运时间
ARRIVAL_TIME = '00:00:00'#启运时间
TRANS_MACHINE = '冷冻车'#运输工具

errmsg4 = '流程四：调拨出库-入库-红冲流程异常，请检查！'#流程异常提示信息

class TransferOut():
    def data_add_allocate(self):
        ###打开调拨出库单###
        wait.until(ec.presence_of_element_located((By.XPATH,'//span[text()="调拨出库单"]')))
        order_link = chrome.find_element_by_xpath('//span[text()="调拨出库单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(0.5)
        # 点击新建按钮
        wait.until(ec.visibility_of_element_located((By.XPATH,'//span[text()="新建"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="新建"]').click()
        sleep(4)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(2)

        ###获取订单号###
        global orderno1
        orderno1 = chrome.find_element_by_xpath('//div[text() = "调拨出库订单号 "]/following-sibling::div/span').text
        print('调拨出库订单号为:', orderno1)

        #点击业务员名称并输入查询条件，查询出结果
        chrome.find_element_by_xpath(
            '//div[text()="业务员 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:业务员名称,业务员编码,部门名称,部门编码,部门类型"]').send_keys(salesman)
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:业务员名称,业务员编码,部门名称,部门编码,部门类型"]').send_keys(Keys.ENTER)
        sleep(1)
        #勾选查询出的业务员并点击确定按钮
        btn_div= chrome.find_element_by_xpath('//span/input[@type="radio" and @value="053bd01a610bae0ff25d8a6dca563b2fa"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//span[@class="ensureBtn"]/button[1])').click()
        sleep(1)

        # 切到仓库信息页
        chrome.find_element_by_xpath('//div[text()="仓库信息"]').click()
        sleep(1)

        #点击调拨出库仓库名称，输入仓库名并查询出结果
        chrome.find_element_by_xpath('//div[text()="调拨出库仓库名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:调拨出库仓库编码,调拨出库仓库名称,是否WMS仓"]').send_keys(outwarehouse)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:调拨出库仓库编码,调拨出库仓库名称,是否WMS仓"]').send_keys(Keys.ENTER)
        sleep(1)
        #勾选查询出的仓库并点击确定按钮
        btn_div= chrome.find_element_by_xpath('//span/input[@type="radio" and @value="0bc65b238650bc6abf44397198474a21d"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        #点击调拨入库仓库名称，输入仓库名并查询出结果
        chrome.find_element_by_xpath('//div[text()="调拨入库仓库名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:调拨入库仓库编码,调拨入库仓库名称,是否WMS"]').send_keys(inwarehouse)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:调拨入库仓库编码,调拨入库仓库名称,是否WMS"]').send_keys(Keys.ENTER)
        sleep(1)
        #勾选查询出的仓库并点击确定按钮
        btn_div= chrome.find_element_by_xpath('//span/input[@type="radio" and @value="026f9c1311022a92efbee8ec9597317f3"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        ###子表信息录入###
        chrome.find_element_by_xpath('//div[text()="主体信息"]').click()
        sleep(1)
        # 点击添加商品并输入商品编码，查询出结果
        chrome.find_element_by_xpath('//span[text()="添加商品"]').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:商品编码,商品全称,规格型号,主计量单位,采购计量单位,厂家物料编码,存储条件,存储条件说明,生产企业编码,行类型,件包数,产品备注,产品描述,商品性质,是否免备案"]').send_keys(productcode)
        sleep(0.5)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:商品编码,商品全称,规格型号,主计量单位,采购计量单位,厂家物料编码,存储条件,存储条件说明,生产企业编码,行类型,件包数,产品备注,产品描述,商品性质,是否免备案"]').send_keys(Keys.ENTER)
        sleep(2)
        # 勾选查询出的商品并点击确定按钮
        chrome.find_element_by_xpath(
            '//th[@class="el-table_15_column_135 multiple-column el-table-column--selection is-leaf headerCellClassName el-table__cell"]/div/label/span/span').click()
        sleep(0.5)
        chrome.find_element_by_xpath('(//div[@class="el-dialog__body"]/div[2]/span/button[1]/span)[4]').click()
        sleep(3)

        ###填写数量价格###
        for i in range(1):
            wait.until(ec.visibility_of_element_located(
                (By.XPATH, '(//td[@colid="col_108" and @f="F_QUANTITY_ORD" and @i=%s])' % i)))
            # 定位数量框并输入数量
            element = chrome.find_element_by_xpath('(//td[@colid="col_108" and @f="F_QUANTITY_ORD" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys('1')
            sleep(1)
            # 点击其它地方让数量框失焦，让数量真正填入数量框(平台设计如此，不用纠结为啥要此步)
            chrome.find_element_by_xpath('(//td[@colid="col_110" and @f="F_PURUNIT_CODE" and @i=%s])' % i).click()
            sleep(1)

    def return_sttorder(self):  # 返回采购单号
        return orderno1

class TransferIn():
    def transfer_in(self,orderno):
        # 打开菜单
        order_link = chrome.find_element_by_xpath('//span[text()="调拨入库单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        sleep(5)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(1)

        # 切换到已办页面
        chrome.find_element_by_xpath('//li[text()="已办"]').click()
        sleep(0.5)
        wait.until_not(ec.presence_of_element_located((By.CLASS_NAME, 'el-loading-mask')))

        # 输入orderno并查询出订单
        chrome.find_element_by_xpath('//label[text()="调拨出库单号"]/parent::div/div/div/div/input').send_keys(orderno)
        sleep(0.5)
        chrome.find_element_by_xpath('//label[text()="调拨出库单号"]/parent::div/div/div/div/input').send_keys(Keys.ENTER)
        wait.until(ec.presence_of_element_located((By.XPATH, '//div[text()="%s"]'%orderno)))
        sleep(0.5)

        #获取调拨入库单
        global orderno2
        orderno2 = chrome.find_element_by_xpath(
            '//td[@class="vxe-body--column col_68 col--ellipsis dw-click-cell"]/div/div').text
        print('调拨入库单号为:', orderno2)
        status = chrome.find_element_by_xpath('//td[@class="vxe-body--column col_74 col--ellipsis"]/div/div').text
        assert status == '待收货', f'调拨入库单状态不正确，当前状态为:"{status}"，正确状态为"待收货"！'
        print('调拨入库单自动审核成功，状态为:待收货！')

    def return_ottorder(self):
        return orderno2

class Instore1(Instore):
    def goods_check(self, orderno, type=2):  # 仓库验收
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '仓库验收', orderno,
                                  'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="提交"]')))
        sleep(1.5)
        # 浏览器页面右滑
        chrome.execute_script(
            "document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollLeft = 5500")
        sleep(1)
        # chrome.execute_script("document.getElementsByClassName('vxe-table--footer-wrapper body--wrapper')[0].scrollLeft = 2000")
        # chrome.execute_script("document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollTop = %s*30;" % i)
        # 填写商品信息
        for i in range(1):

            #填写启运温度
            chrome.find_element_by_xpath('(//td[@colid="col_185" and @f="F_STASHIP_TEMP" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(STASHIP_TEMP)
            sleep(0.5)

            #填写到货温度
            chrome.find_element_by_xpath('(//td[@colid="col_186" and @f="F_ARRIVAL_TEMP" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(ARRIVAL_TEMP)
            sleep(0.5)

            #填写启运日期
            chrome.find_element_by_xpath('(//td[@colid="col_187" and @f="F_STASHIP_DATE" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(STASHIP_DATE)
            sleep(0.5)

            #填写启运时间
            chrome.find_element_by_xpath('(//td[@colid="col_188" and @f="F_STASHIP_TIME" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(STASHIP_TIME)
            sleep(0.5)

            #填写到货时间
            chrome.find_element_by_xpath('(//td[@colid="col_190" and @f="F_ARRIVAL_TIME" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(ARRIVAL_TIME)
            sleep(0.5)

            #填写运输工具
            chrome.find_element_by_xpath('(//td[@colid="col_191" and @f="F_TRANS_MACHINE" and @i=%s])' % i).click()
            sleep(0.5)
            # chrome.find_element_by_class_name('el-input__inner').send_keys(TRANS_MACHINE)
            chrome.find_element_by_xpath('//li[text()="%s"]'%TRANS_MACHINE).click()
            sleep(0.5)

        # 调用button_click1保存/提交订单
        PurchaseOrder.button_click1(self, type)
        print('仓库验收成功')

class TransferOffSetting(PurchaseOrder):
    def ottorder_cancel(self):
        wait.until(ec.visibility_of_element_located((By.XPATH,'//span[@title="全选/取消"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[@title="全选/取消"]').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="整单取消"]').click()
        sleep(1)
        chrome.find_element_by_xpath('//span[text()="是"]').click()#操作成功
        wait.until(ec.presence_of_element_located((By.XPATH, '//p[text()="操作成功"]')))
        PurchaseOrder.closetab(self)
        print('订单取消成功')

    def ottorder_offset(self,orderno):
        # 打开菜单
        order_link = chrome.find_element_by_xpath('//span[text()="调拨出库红冲单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        sleep(5)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(1)

        #点击红冲按钮
        wait.until(ec.presence_of_element_located((By.XPATH,'//span[text()="红冲"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="红冲"]').click()
        sleep(1)
        # wait.until(ec.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[contains(@name,"awsui-dialog-iframe")]')))
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))

        # 输入orderno并查询出订单
        wait.until(ec.visibility_of_element_located((By.XPATH,'//label[text()="调拨出库单号"]/parent::div/div/div/div/input')))
        sleep(0.5)
        chrome.find_element_by_xpath('//label[text()="调拨出库单号"]/parent::div/div/div/div/input').send_keys(orderno)
        sleep(1)
        chrome.find_element_by_xpath('//label[text()="调拨出库单号"]/parent::div/div/div/div/input').send_keys(Keys.ENTER)

        #勾选订单点击红冲
        wait.until(ec.visibility_of_element_located((By.XPATH, '//td[@class="vxe-body--column col_19 col--left col--ellipsis dw-click-cell"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[@title="全选/取消"]').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="整单红冲"]').click()
        #点击确定按钮
        wait.until(ec.visibility_of_element_located((By.XPATH,'//div[@class="el-message-box__btns"]/button[2]/span')))
        sleep(0.5)
        chrome.find_element_by_xpath('//div[@class="el-message-box__btns"]/button[2]/span').click()
        sleep(2)

        ###获取订单号###
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        wait.until(ec.visibility_of_element_located((By.XPATH,'//div[text() = "订单号 "]/following-sibling::div/span')))
        sleep(0.5)
        global orderno3
        orderno3 = chrome.find_element_by_xpath('//div[text() = "订单号 "]/following-sibling::div/span').text
        #提交红冲单
        PurchaseOrder.button_click1(self,el2='//p[text()="发送成功"]')
        print('调拨出库红冲单提交成功，单号为:',orderno3)
    def return_smtorder(self):
        return orderno3





if __name__ == '__main__':
    try:
        step1 = LoginSys()
        step2_1 = TransferOut()
        step2_2 = PurchaseOrder()
        step3 = Outstore()
        step4 = TransferIn()
        step5 = Instore1()
        step6 = TransferOffSetting()
        step1.loginsys(SIT_URL,usr2,pwd2)
        step2_1.data_add_allocate()#填写主表数据
        sttorder = step2_1.return_sttorder()
        step2_2.button_click1()#提交
        step2_2.switch_loggin(usr1,pwd1)
        step2_2.query_click('调拨出库单', sttorder, 'vxe-body--column col_19 col--left col--ellipsis dw-click-cell',qrcond='调拨出库单号')
        step2_2.button_click2(msg1='发送成功')#审核
        step3.switch_loggin(usr4, pwd4)
        step3.deliver_goods(sttorder)  # 仓库发货
        step3.picking_goods(sttorder)  # 仓库拣货
        step3.switch_loggin(usr5, pwd5)
        step3.outbound_review(sttorder,type=3)  # 出库复核
        step3.switch_loggin(usr1, pwd1)
        step4.transfer_in(sttorder)#检查自动审配订单状态
        ottorder = step4.return_ottorder()#获取调拨入库单号
        # step5.switch_loggin(usr4, pwd4)
        # step5.take_over_goods(ottorder)  # 仓库收货：1为收货单据撤回，2为提交，其余为保存
        # step5.switch_loggin(usr5, pwd5)
        # step5.goods_check(ottorder,type=3)  # 仓库验收：1为保存，否则为提交
        # step5.switch_loggin(usr4, pwd4)
        # step5.goods_grounding(ottorder)  # 仓库上架：1为保存，否则为提交
        step6.ottorder_cancel()#整单取消调拨入库单
        step6.switch_loggin(usr2,pwd2)
        step6.ottorder_offset(sttorder)#新建调拨出库红冲单
        smtorder = step6.return_smtorder()
        step5.switch_loggin(usr4, pwd4)
        step5.take_over_goods(smtorder)  # 仓库收货：1为收货单据撤回，2为提交，其余为保存
        step5.switch_loggin(usr5, pwd5)
        step5.goods_check(smtorder,type=3)  # 仓库验收：1为保存，否则为提交
        step5.switch_loggin(usr4, pwd4)
        step5.goods_grounding(smtorder)  # 仓库上架：1为保存，否则为提交
        print('流程四：调拨出库-入库-红冲流程结束！！！')
    except (Exception,AssertionError) as e:
        step7 =ExceptionHandling()
        traceback.print_exc()
        step7.handle(errmsg4,e)

    finally:
        chrome.quit()


