1. 月盘

1.1 月末负库存清零任务:

任务地址:https://store.lelecha.com/chronos.html
name:月盘负库存清零
topic: supply.stocktake.negative_stocktake_zero.init
和客户沟通过,暂时配置为每个月1号3:30执行,对应utc时间:就是每个月末最后一天的19:30执行

1.2 修改报废时间和盘点时间

由于报废日期和盘点日期是自然日,而库存切片是4点~4点,这样会导致报表和库存切片数据不一致,因此,就要求在凌晨0点~4点之间盘点或者报废的单子,应该算在前一天。这个需求已经实现并发布到生产,上个月的月结报废日期和盘点日期应该是对的,客户并没有让改。但为防止意外,附上对应的脚本

  1. 修改报废到日期 ```python

    更新报废单

    import os

import pymysql import xlrd

from util.read_file import read_excel

def update_adjust(created_at, adjust_date, codes):

  1. # 更新收货单状态
  2. db = pymysql.connect(
  3. host="pc-uf649stob1f3o4si8.rwlb.rds.aliyuncs.com",
  4. port=3306,
  5. user="lelecha_prod",
  6. passwd="EdCD4u69etNJHb7z",
  7. db="lelecha_boh_supply"
  8. )
  9. # 通过 cursor() 创建游标对象
  10. cur = db.cursor()
  11. # 更新报废单时间
  12. update_sql = """UPDATE supply_adjust_detail SET created_at = '{created_at}', adjust_date = '{adjust_date}' WHERE `code` in ({codes})""".\
  13. format(created_at=created_at, adjust_date=adjust_date, codes=codes)
  14. try:
  15. # 使用 execute() 执行sql
  16. cur.execute(update_sql)
  17. # 提交事务
  18. db.commit()
  19. except Exception as e:
  20. print("更新失败,error = {}".format(e))
  21. # 回滚所有的更改
  22. db.rollback()
  23. finally:
  24. print("更新成功")
  25. print("受影响行数:{}".format(cur.rowcount))
  26. # 关闭游标
  27. cur.close()
  28. # 关闭数据库连接
  29. db.close()

默认读出第一列

def read_excel(path=None, file_name=None): if not path: path = os.getcwd() + “/files/excel/“ rows = []

  1. # 打开文件
  2. workbook = xlrd.open_workbook(path + file_name)
  3. # 获取所有sheet
  4. sheets = workbook.sheet_names()
  5. for sheet in sheets:
  6. # 根据sheet索引或者名称获取sheet内容
  7. sheet = workbook.sheet_by_name(sheet)
  8. rows = sheet.nrows()
  9. cols = sheet.col_values(0) # 获取第1列内容
  10. for col in cols[1:]:
  11. col_str = str(int(col))
  12. rows.append(col_str)
  13. print("rows = {}".format(rows))
  14. return rows

if name == ‘main‘:

  1. # 读取指定列
  2. # path = os.getcwd() + "/"
  3. file_name = "报废单.xlsx"
  4. rows = read_excel(file_name=file_name)
  5. codes= ",".join(rows)
  6. # 更新至 2021-11-30
  7. # created_at = "2021-11-30 15:50:00"
  8. # adjust_date = "2021-11-30 15:50:00"
  9. # 更新至 2021-12-31
  10. created_at = "2021-12-31 15:50:00"
  11. adjust_date = "2021-12-31 15:50:00"
  12. update_adjust(created_at=created_at, adjust_date=adjust_date, codes=codes)
  1. 2. 修改盘点单日期
  2. ```python
  3. # 更新盘点单
  4. import os
  5. import pymysql
  6. import xlrd
  7. from db.lelecha_boh_prod import get_db
  8. from util.read_file import read_excel
  9. def update_stocktake(target_date, codes):
  10. # 更新收货单状态
  11. db = pymysql.connect(
  12. host="pc-uf649stob1f3o4si8.rwlb.rds.aliyuncs.com",
  13. port=3306,
  14. user="lelecha_prod",
  15. passwd="EdCD4u69etNJHb7z",
  16. db="lelecha_boh_supply"
  17. )
  18. # 通过 cursor() 创建游标对象
  19. cur = db.cursor()
  20. # 更新报废单时间
  21. update_sql = """UPDATE supply_st_doc_details SET target_date = '{target_date}' WHERE `code` in ({codes})""".format(
  22. target_date=target_date, codes=codes
  23. )
  24. try:
  25. # 使用 execute() 执行sql
  26. cur.execute(update_sql)
  27. # 提交事务
  28. db.commit()
  29. except Exception as e:
  30. print("更新失败,error = {}".format(e))
  31. # 回滚所有的更改
  32. db.rollback()
  33. finally:
  34. print("更新成功")
  35. print("受影响行数:{}".format(cur.rowcount))
  36. # 关闭游标
  37. cur.close()
  38. # 关闭数据库连接
  39. db.close()
  40. # 默认读出第一列
  41. def read_excel(path=None, file_name=None):
  42. if not path:
  43. path = os.getcwd() + "/files/excel/"
  44. rows = []
  45. # 打开文件
  46. workbook = xlrd.open_workbook(path + file_name)
  47. # 获取所有sheet
  48. sheets = workbook.sheet_names()
  49. for sheet in sheets:
  50. # 根据sheet索引或者名称获取sheet内容
  51. sheet = workbook.sheet_by_name(sheet)
  52. rows = sheet.nrows()
  53. cols = sheet.col_values(0) # 获取第1列内容
  54. for col in cols[1:]:
  55. col_str = str(int(col))
  56. rows.append(col_str)
  57. print("rows = {}".format(rows))
  58. return rows
  59. if __name__ == '__main__':
  60. # 读取指定列
  61. # path = os.getcwd() + "/"
  62. file_name = "盘点单.xlsx"
  63. rows = read_excel(file_name=file_name)
  64. codes= ",".join(rows)
  65. # 更新至 2021-12-31
  66. target_date = "2021-12-31 15:50:00"
  67. update_stocktake(target_date=target_date, codes=codes)

2. 变更收货单状态

之前客户有遇到过明明已经收货,但收货单仍旧显示 未到店,就需要手动 更改 单据的状态

  1. import pymysql, xlrd, os
  2. class UpdateReceiptReceive(object):
  3. # 更新门店收货单状态:未到店 ———> 待收货
  4. def __init__(self):
  5. # 初始化
  6. pass
  7. def run(self):
  8. # 主程序
  9. # 读取要更新的收货单code_list
  10. path = os.getcwd() + "/"
  11. file_name = "未到店.xlsx"
  12. code_list = self.get_receive_codes(path=path, file_name=file_name)
  13. codes = ",".join(code_list)
  14. # 更新收货单状态
  15. self.update_receive(codes=codes)
  16. def update_receive(self, codes):
  17. # 更新收货单状态
  18. print("codes = {}".format(codes))
  19. # 建立数据库连接
  20. db = pymysql.connect(
  21. host="pc-uf649stob1f3o4si8.rwlb.rds.aliyuncs.com",
  22. port=3306,
  23. user="lelecha_prod",
  24. passwd="EdCD4u69etNJHb7z",
  25. db="lelecha_boh_receipt"
  26. )
  27. # 通过 cursor() 创建游标对象
  28. cur = db.cursor()
  29. # 未到店 ———> 待收货
  30. update_sql = """UPDATE receipt_receive SET `status` = 'INITED' WHERE `status` = 'SUSPENDED' AND `code` in ({})""".format(codes)
  31. # 待收货 ---> 已收货
  32. # update_sql = """UPDATE receipt_receive SET `status` = 'SUCCESS' WHERE `status` = 'INITED' AND `code` in ({})""".format(codes)
  33. try:
  34. # 使用 execute() 执行sql
  35. cur.execute(update_sql)
  36. # 提交事务
  37. db.commit()
  38. except Exception as e:
  39. print("更新失败,error = {}".format(e))
  40. # 回滚所有的更改
  41. db.rollback()
  42. finally:
  43. print("更新成功")
  44. print("受影响行数:{}".format(cur.rowcount))
  45. # 关闭游标
  46. cur.close()
  47. # 关闭数据库连接
  48. db.close()
  49. def get_receive_codes(self, path, file_name):
  50. # 获取收货单编码列表
  51. code_list = list()
  52. # 打开文件
  53. workbook = xlrd.open_workbook(path + file_name)
  54. # 获取所有sheet
  55. sheets = workbook.sheet_names()
  56. for sheet_name in sheets:
  57. # 根据sheet索引或者名称获取sheet内容
  58. sheet = workbook.sheet_by_name(sheet_name)
  59. cols = sheet.col_values(1) # 获取第2列内容
  60. for col in cols[1:]:
  61. col_str = str(int(col))
  62. code_list.append(col_str)
  63. return code_list
  64. if __name__ == '__main__':
  65. URR = UpdateReceiptReceive()
  66. URR.run()