# generate_locations.py
import os
import django
import sys


def setup_django():
    # 使用原始字符串处理Windows路径
    project_path = "D:/Document/code/vue/greater_wms"
    sys.path.append(project_path)
    
    # 根据实际目录名设置(注意下划线)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'greaterwms.settings')
    django.setup()

def main():
    try:
        # 从正确的应用导入模型
        from erp.views import AccessToken ,ProductionInboundAuditSync,PurchaseInboundAuditSync,OtherInboundAuditSync,OtherOutboundAuditSync,ProductionOutboundAuditSync,PurchaseInboundSaveSync,SaleOutboundSaveSync
        from erp.models import InboundBill
        from warehouse.models import baseset
   
        
        token = AccessToken.get_current_token()
        print('【1】token:',token)

       

        # 所有审核操作统一入口
        # inbound_bill = InboundBill.objects.filter(billId=2210144887247006720).first()
        inbound_bill = InboundBill.objects.filter(billId=2210170242427054080).first()
        print(f"ERP单号: {inbound_bill.number}")
        if not inbound_bill:
            print("❌ 未找到ERP单据")
            return

        # ProductionInboundAuditsync = ProductionInboundAuditSync(inbound_bill,[])
        # success = ProductionInboundAuditsync.execute_sync()
        

        # 审核采购入库单
        PurchaseInboundAuditsync = PurchaseInboundAuditSync(inbound_bill,[])
        success = PurchaseInboundAuditsync.execute_sync()


        # # 审核其它入库单
        # OtherInboundAuditsync = OtherInboundAuditSync(inbound_bill,[])
        # success = OtherInboundAuditsync.execute_sync()
       
        # # 审核其它出库单
        # OtherOutboundAuditsync = OtherOutboundAuditSync(inbound_bill,[])
        # success = OtherOutboundAuditsync.execute_sync()

        # # 审核生产出库单
        # ProductionOutboundAuditsync = ProductionOutboundAuditSync(inbound_bill,[])
        # success = ProductionOutboundAuditsync.execute_sync()
        
        # # 保存采购入库单
        # PurchaseInboundSavesync = PurchaseInboundSaveSync(inbound_bill,[])
        # success = PurchaseInboundSavesync.execute_sync()
        
        # # 保存销售出库单
        # SaleOutboundSavesync = SaleOutboundSaveSync(inbound_bill,[])
        # success = SaleOutboundSavesync.execute_sync()


        print("✅ 方法生成成功!")
    except Exception as e:
        print(f"❌ 生成失败:{str(e)}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    setup_django()
    main()