def export_pdf():
    company_info = get_info()
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }
    year = datetime.datetime.now().year
    month = datetime.datetime.now().month - 1
    if month < 10:
        month = "0" + str(month)
    for ipp in company_info:
        cs = c48_sys(ipp)
        cs.login_c48()
        cookies = cs.get_cookie()
        cs.close_browse()
        cookie = {}
        for c in cookies:
            cookie[c['name']] = c['value']
        for i in company_info[ipp]:
            company_name = i[2]
            shuihao = i[0]
            fjh = i[1]
            url = "http://{ipp}/zzs_kpfw_manager/tax/monthly_statistics/doExportPdf.htm?nsrsbh={shuihao}&cardno={fjh}&year={year}&month={month}&fpzl=51".format(
                ipp=ipp, shuihao=shuihao, fjh=fjh, year=year, month=month)
            pdf_path = "C:/Users/lgx/Downloads/{}.pdf".format(company_name)
            res = requests.get(url, headers=headers, cookies=cookie)
            with open(pdf_path, "wb") as f:
                f.write(res.content)
            print("{}pdf导出完毕!!!!".format(company_name))
def buy():

    company_info = get_info()
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }

    s = input("输入申领发票的税号:")
    for i in company_info:
        for j in company_info[i]:
            # print(j)
            if s in j:
                ipp = i
                shuihao = j[0]
                fjh = j[1]

    # print(ipp,shuihao,fjh)
    cs = c48_sys(ipp)
    cs.login_c48()
    cookies = cs.get_cookie()
    cs.close_browse()
    cookie = {}
    for c in cookies:
        cookie[c['name']] = c['value']
    print(cookie)
    req_url = "http://{}/zzs_kpfw_manager/invoice/purchase/list.htm".format(
        ipp)
    r1 = requests.get(req_url, headers=headers, cookies=cookie)
    # print(r1.cookies)
    pycx_url = "http://{ipp}/zzs_kpfw_manager/invoice/purchase/pycx.htm?nsrxxStr={shuihao},{fjh},,{shuihao},{fjh},{fjh},51,end&totalRows=1".format(
        ipp=ipp, shuihao=shuihao, fjh=fjh)  # 票源查询的url
    n = 0
    while n < 3:
        try:
            res = requests.get(pycx_url,
                               headers=headers,
                               cookies=cookie,
                               timeout=20)
            soup = BS(res.text, "lxml")
            table = soup.find_all(class_="commonTableNew")
            td = table[0].find_all("td")
            fp_num = td[1].text
            fp_num = fp_num.replace("份", "")
            if fp_num == "":
                print("局端没有可以下载票源")
                n += 1
            else:
                print("局端的票源份数:", fp_num)
                fplg_url = "http://{ipp}/zzs_kpfw_manager/invoice/purchase/wslp.htm?nsrxxStr={shuihao},{fjh},{fp_num},{shuihao},{fjh},{fjh},51,end&totalRows=1".format(
                    ipp=ipp, shuihao=shuihao, fjh=fjh, fp_num=fp_num)
                r2 = requests.get(fplg_url,
                                  headers=headers,
                                  cookies=cookie,
                                  timeout=20)
                print("下载成功")
        except requests.exceptions.ReadTimeout:
            print("请求超时")
            n += 1
def export_fp():
    year=datetime.now().year
    month=datetime.now().month-1
    company_info = get_info()
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

    for ipp in company_info:
        req_url="http://{}/zzs_kpfw_manager/invoice/query/exportAll.htm".format(ipp)
        cs=c48_sys(ipp)
        cs.login_c48()
        cookies=cs.get_cookie()
        cookie={}
        for c in cookies:
            cookie[c['name']]=c['value']
        print(cookie)
        for i in company_info[ipp]:
            shuihao=i[0]
            fjh=i[1]
            company_name=i[2]
            if len(i) == 4:
                path = "D:/发票明细/{}月/其他".format(month)
            else:
                path = "D:/发票明细/{}月/捷顺".format(month)
            if not os.path.exists(path):
                    os.makedirs(path)
            data2={
                    "export_nsrsbh": shuihao,
                    "export_cardno":fjh ,
                    "export_fpdm": "",
                    "export_fphm": "",
                    "export_upload_status": "",
                    "export_kprq_start": "{year}-{month}-01 00:00:00".format(year=year,month=month),
                    "export_kprq_end": "{year}-{month}-31 23:59:59".format(year=year,month=month),
                    "export_kplx": "",
                    "export_serial_num": "",
            }
            r2=requests.post(req_url,headers=headers,data=data2,cookies=cookie)


            fp_file=path+"/{}.xlsx".format(company_name)
            with open(fp_file,"wb") as f:
                f.write(r2.content)

            print("{}导出完毕".format(company_name))
def get_status():

    company_info = get_info()
    print(company_info)
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }

    for ipp in company_info:
        req_url = "http://{}/zzs_kpfw_manager/statistic/queryServiceData.htm".format(
            ipp)
        cs = c48_sys(ipp)
        cs.login_c48()
        cookies = cs.get_cookie()
        cs.close_browse()
        cookie = {}
        for c in cookies:
            cookie[c['name']] = c['value']
        print(cookie)
        wrong_status = {}
        for i in company_info[ipp]:
            company_name = i[2]
            shuihao = i[0]
            data = {"search_nsrmc": company_name, "search_nsrsbh": shuihao}
            res = requests.post(req_url,
                                headers=headers,
                                cookies=cookie,
                                data=data)
            # print(res.status_code)
            soup = BS(res.text, 'lxml')
            # print(soup)
            tbody = soup.find_all("tbody")[0]
            status = tbody.find_all("span")[0].text
            print("{}的状态是:{}".format(company_name, status))
            if status != "可用":
                wrong_status[company_name] = status

    print(wrong_status)
def nsr_info():
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
    }
    company_info=get_info()
    for ipp in company_info:
        cs=c48_sys(ipp)
        cs.login_c48()
        cookies=cs.get_cookie()
        cs.close_browse()
        cookie={}
        for c in cookies:
            cookie[c['name']]=c['value']

        req_url="http://{}/zzs_kpfw_manager/init/nsr_info/list.htm".format(ipp)
        no_update={}
        d_error={}
        for i in company_info[ipp]:
            shuihao=i[0]
            fjh=i[1]
            name=i[2]
            form_data={

                "search_nsrmc":name,
                "search_nsrsbh":shuihao,
                "search_fjh":fjh
            }
            print("查询的是:",name)
            res=requests.post(req_url,headers=headers,data=form_data,cookies=cookie)
            try:
                soup=BS(res.text,"lxml")
                a=soup.find_all('a')
                str=a[2]['href']
                num=re.search("\d+",str).group()
                # print(num)

                sync_url="http://{}/zzs_kpfw_manager/init/nsr_info/synchroStatus/{}.htm".format(ipp,num)
                query_url="http://{}/zzs_kpfw_manager/init/nsr_info/taxInfoDetail/{}.htm".format(ipp,num)

                r1=requests.get(query_url,headers=headers,cookies=cookie)

                tr=BS(r1.text,'lxml').find_all("tr")
                td=tr[3].find_all('td')
                no_line_time=td[7].text
                no_line_money=td[8].text
                odd_money=td[9].text

                print("离线时间:",no_line_time)
                print("离线金额:",no_line_money)
                print("剩余离线时间:",odd_money)

                if int(no_line_time) < 168:
                    no_update[name]=no_line_time
                    try:
                        r2=requests.get(sync_url,headers=headers,cookies=cookie,timeout=20)
                        print("同步成功")
                    except requests.exceptions.ReadTimeout:
                        d_error[shuihao] = name

            except IndexError:
                d_error[shuihao]=name
                continue

    print(no_update)
    print(d_error)