
from testcase.test_scene1.scene1_mainflow import *
from selenium.webdriver.common.action_chains import ActionChains


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'
warehousename = '103999'#仓库名称
purcontractnumber = 41007#采购合同号
salcontractnumber = 'YLTEST88'#销售合同号
salesman = '杨春刚'#业务员名称
salcust = 1003758#销售客户名称
productcode = 10156326#商品编码
productdate = '2023-10-01'#生产日期
expirdate = '2026-09-30'#失效日期
errmsg3 = '流程三：经销商向总代采购，货从供应商发（经销商快采快销场景）流程异常，请检查！'#流程异常提示信息

class QuickPurchase():#快速采购单界面操作
    def data_add_quickpurchase(self):#填写表单数据
        chrome.implicitly_wait(10)
        ###打开快速采购订单###
        order_link = chrome.find_element_by_xpath('//span[text()="快速采购订单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        sleep(0.5)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        #点击新建按钮
        wait.until(ec.visibility_of_element_located((By.XPATH,'//span[text()="新建"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="新建"]').click()
        sleep(5)
        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(warehousename)
        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="00c286e0a9e96a7ba5ed168b3103a719a"]')
        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(
            '//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(
            purcontractnumber)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码"]').send_keys(
            Keys.ENTER)
        sleep(1)
        # chrome.find_element_by_xpath(
        #     '//div[@class="multiple-header string_col" and text()="38443"]/ancestor-or-self::td/preceding-sibling::td/div/label/span/span').click()
        #勾选查询出的合同号并点击确定按钮
        btn_div= chrome.find_element_by_xpath('//span/input[@type="radio" and @value="03992a2172b8f7327a077e60ac1da11b8"]')
        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(2)

        #点击业务员名称并输入查询条件，查询出结果
        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="06c32a810e3dabf2a50d1ab040c7951c8"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//span[@class="ensureBtn"]/button[1])[3]').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="模糊检索:合同协议号,对方编码,销售项目编码,销售项目名称"]').send_keys(salcontractnumber)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,对方编码,销售项目编码,销售项目名称"]').send_keys(Keys.ENTER)
        sleep(1)
        # chrome.find_element_by_xpath(
        #     '//div[@class="multiple-header string_col" and text()="38443"]/ancestor-or-self::td/preceding-sibling::td/div/label/span/span').click()
        # 勾选查询出的合同号并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="0b1ab563e8d07fdfe3a08737ef3b9863c"]')
        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="模糊检索:客户编码,客户名称"]').send_keys(salcust)
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:客户编码,客户名称"]').send_keys(Keys.ENTER)
        sleep(1)
        # chrome.find_element_by_xpath(
        #     '//div[@class="multiple-header string_col" and text()="38443"]/ancestor-or-self::td/preceding-sibling::td/div/label/span/span').click()
        # 勾选查询出的合同号并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="0326acb1c3399e0e83b3758a32a675057"]')
        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('//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_21_column_237 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)[6]').click()
        sleep(5)

        ###填写数量价格###
        for i in range(1):
            wait.until(ec.visibility_of_element_located((By.XPATH,'(//td[@colid="col_166" and @f="F_QUANTITY_ORD" and @i=%s])'%i)))
            #定位数量框并输入数量
            element = chrome.find_element_by_xpath('(//td[@colid="col_166" 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_173" and @f="F_PURUNIT_TXT" and @i=%s])' % i).click()
            sleep(1)

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

class SalesOrder():
    def push_wms(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(5)
        wait.until_not(ec.presence_of_element_located((By.CLASS_NAME,'el-loading-mask')))

        #操作上面滚动条
        ac = ActionChains(chrome)
        el1 = chrome.find_element_by_xpath('//div[text()="订单日期"]')
        el2 = chrome.find_element_by_xpath('//div[@id="dw-searcher-area-div"]/div[1]/div/div[3]/div')
        ac.move_to_element(el1).perform()
        ac.drag_and_drop_by_offset(el2, 0, 100).perform()
        sleep(0.5)

        # 输入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)
        sleep(2)
        wait.until(ec.visibility_of_element_located((By.XPATH,'//td[@class="vxe-body--column col_104 col--ellipsis dw-click-cell"]/div/div')))
        salesorder = chrome.find_element_by_xpath('//td[@class="vxe-body--column col_104 col--ellipsis dw-click-cell"]/div/div').text
        print('生成的销售订单号为:',salesorder)
        status = chrome.find_element_by_xpath('//td[@class="vxe-body--column col_107 col--ellipsis"]/div/div').text
        assert status == '待发货',f'销售订单推送WMS失败，当前状态为:"{status}"，正确状态为"待发货"！'
        print('销售订单推送WMS成功')




if __name__ == '__main__':
    try:
        step1 = LoginSys()
        step2_1 = QuickPurchase()
        step2_2 = PurchaseOrder()
        step3 = SalesOrder()
        step1.loginsys(SIT_URL,usr2,pwd2)
        step2_1.data_add_quickpurchase()
        owtorder = step2_1.return_owtorder()
        step2_2.button_click1()
        print("快速采购单提交成功,订单号为:",owtorder)
        step2_2.switch_loggin(usr1,pwd1)
        step2_2.query_click('快速采购订单',owtorder,'vxe-body--column col_19 col--ellipsis dw-click-cell')
        step2_2.button_click2()
        step3.push_wms(owtorder)
        print('流程三：经销商向总代采购，货从供应商发（经销商快采快销场景）平台端流程结束！！！')
    except (Exception,AssertionError) as e:
        step6 =ExceptionHandling()
        traceback.print_exc()
        step6.handle(errmsg3,e)

    # finally:
    #     chrome.quit()

