invoice_info.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """CRM 客户开票信息 CRUD"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import Any, Dict, List, Optional
  5. from sqlalchemy.orm import Session
  6. from app.core.delete_guard import assert_row_deletable_by_creator
  7. from app.core.utils import generate_uuid
  8. from app.crud.crm.data_scope_helpers import (
  9. ensure_crm_customer_linked_row_in_scope,
  10. ensure_crm_owner_row_in_scope,
  11. )
  12. from app.models.crm_models import CrmInvoiceInfo
  13. _FMT = "%Y-%m-%d %H:%M:%S"
  14. def _row_to_dict(row: CrmInvoiceInfo) -> Dict[str, Any]:
  15. return {
  16. "id": row.id,
  17. "customerId": row.customer_id or "",
  18. "companyName": row.company_name or "",
  19. "taxNo": row.tax_no or "",
  20. "companyAddress": row.company_address or "",
  21. "legalRepresentative": row.legal_representative or "",
  22. "agent": row.agent or "",
  23. "phoneFax": row.phone_fax or "",
  24. "bankName": row.bank_name or "",
  25. "bankAccount": row.bank_account or "",
  26. "isDefault": row.is_default or "0",
  27. "orderNum": row.order_num if row.order_num is not None else 0,
  28. "remark": row.remark or "",
  29. "createBy": row.create_by or "",
  30. "createTime": row.create_time.strftime(_FMT) if row.create_time else "",
  31. "updateBy": row.update_by or "",
  32. "updateTime": row.update_time.strftime(_FMT) if row.update_time else "",
  33. }
  34. def list_invoice_info_by_customer(
  35. db: Session,
  36. tenant_no: str,
  37. customer_id: str,
  38. *,
  39. user_info=None,
  40. ) -> List[Dict[str, Any]]:
  41. if user_info:
  42. from app.crud.crm.customer import get_customer
  43. customer = get_customer(db, customer_id)
  44. if not customer or customer.tenant_no != tenant_no:
  45. raise ValueError("客户不存在")
  46. ensure_crm_owner_row_in_scope(
  47. db, customer, user_info, tenant_no, message="无权访问该客户"
  48. )
  49. rows = (
  50. db.query(CrmInvoiceInfo)
  51. .filter(
  52. CrmInvoiceInfo.tenant_no == tenant_no,
  53. CrmInvoiceInfo.customer_id == customer_id,
  54. CrmInvoiceInfo.del_flag == 0,
  55. )
  56. .order_by(CrmInvoiceInfo.is_default.desc(), CrmInvoiceInfo.create_time.desc())
  57. .all()
  58. )
  59. return [_row_to_dict(r) for r in rows]
  60. def get_invoice_info(db: Session, invoice_id: str) -> Optional[CrmInvoiceInfo]:
  61. return (
  62. db.query(CrmInvoiceInfo)
  63. .filter(
  64. CrmInvoiceInfo.id == invoice_id,
  65. CrmInvoiceInfo.del_flag == 0,
  66. )
  67. .first()
  68. )
  69. def _clear_default_for_customer(
  70. db: Session,
  71. tenant_no: str,
  72. customer_id: str,
  73. exclude_id: str = None,
  74. ) -> None:
  75. q = db.query(CrmInvoiceInfo).filter(
  76. CrmInvoiceInfo.tenant_no == tenant_no,
  77. CrmInvoiceInfo.customer_id == customer_id,
  78. CrmInvoiceInfo.del_flag == 0,
  79. CrmInvoiceInfo.is_default == "1",
  80. )
  81. if exclude_id:
  82. q = q.filter(CrmInvoiceInfo.id != exclude_id)
  83. q.update({"is_default": "0"})
  84. def create_invoice_info(
  85. db: Session,
  86. *,
  87. tenant_no: str,
  88. user_id: str = None,
  89. dept_id: str = "",
  90. user_info=None,
  91. **kwargs,
  92. ) -> CrmInvoiceInfo:
  93. customer_id = kwargs.get("customer_id")
  94. if not customer_id:
  95. raise ValueError("缺少客户ID")
  96. if user_info:
  97. from app.crud.crm.customer import get_customer
  98. customer = get_customer(db, customer_id)
  99. if not customer or customer.tenant_no != tenant_no:
  100. raise ValueError("客户不存在")
  101. ensure_crm_owner_row_in_scope(
  102. db, customer, user_info, tenant_no, message="无权访问该客户"
  103. )
  104. company_name = (kwargs.get("company_name") or "").strip()
  105. if not company_name:
  106. raise ValueError("单位名称不能为空")
  107. is_default = kwargs.get("is_default") or "0"
  108. if is_default == "1":
  109. _clear_default_for_customer(db, tenant_no, customer_id)
  110. obj = CrmInvoiceInfo(
  111. id=generate_uuid(),
  112. customer_id=customer_id,
  113. company_name=company_name,
  114. tax_no=kwargs.get("tax_no") or "",
  115. company_address=kwargs.get("company_address") or "",
  116. legal_representative=kwargs.get("legal_representative") or "",
  117. agent=kwargs.get("agent") or "",
  118. phone_fax=kwargs.get("phone_fax") or "",
  119. bank_name=kwargs.get("bank_name") or "",
  120. bank_account=kwargs.get("bank_account") or "",
  121. is_default=is_default,
  122. tenant_no=tenant_no,
  123. order_num=kwargs.get("order_num"),
  124. remark=kwargs.get("remark"),
  125. create_by=user_id,
  126. create_dept_id=dept_id,
  127. del_flag=0,
  128. )
  129. db.add(obj)
  130. db.commit()
  131. db.refresh(obj)
  132. return obj
  133. def update_invoice_info(
  134. db: Session,
  135. invoice_id: str,
  136. *,
  137. tenant_no: str = None,
  138. user_id: str = None,
  139. user_info=None,
  140. **kwargs,
  141. ) -> CrmInvoiceInfo:
  142. obj = get_invoice_info(db, invoice_id)
  143. if not obj:
  144. raise ValueError("开票信息不存在")
  145. if tenant_no and obj.tenant_no != tenant_no:
  146. raise ValueError("开票信息不存在")
  147. ensure_crm_customer_linked_row_in_scope(
  148. db, obj, user_info, obj.tenant_no, message="无权访问该开票信息"
  149. )
  150. _updatable = (
  151. "company_name",
  152. "tax_no",
  153. "company_address",
  154. "legal_representative",
  155. "agent",
  156. "phone_fax",
  157. "bank_name",
  158. "bank_account",
  159. "is_default",
  160. "order_num",
  161. "remark",
  162. )
  163. for field in _updatable:
  164. if field in kwargs and kwargs[field] is not None:
  165. setattr(obj, field, kwargs[field])
  166. if kwargs.get("is_default") == "1":
  167. _clear_default_for_customer(
  168. db,
  169. obj.tenant_no,
  170. obj.customer_id,
  171. exclude_id=invoice_id,
  172. )
  173. obj.is_default = "1"
  174. obj.update_by = user_id
  175. obj.update_time = datetime.now()
  176. db.commit()
  177. db.refresh(obj)
  178. return obj
  179. def set_default_invoice_info(
  180. db: Session,
  181. invoice_id: str,
  182. customer_id: str,
  183. *,
  184. tenant_no: str,
  185. user_id: str = None,
  186. user_info=None,
  187. ) -> CrmInvoiceInfo:
  188. obj = get_invoice_info(db, invoice_id)
  189. if not obj or obj.tenant_no != tenant_no:
  190. raise ValueError("开票信息不存在")
  191. if obj.customer_id != customer_id:
  192. raise ValueError("开票信息与客户不匹配")
  193. ensure_crm_customer_linked_row_in_scope(
  194. db, obj, user_info, tenant_no, message="无权访问该开票信息"
  195. )
  196. _clear_default_for_customer(db, tenant_no, customer_id, exclude_id=invoice_id)
  197. obj.is_default = "1"
  198. obj.update_by = user_id
  199. obj.update_time = datetime.now()
  200. db.commit()
  201. db.refresh(obj)
  202. return obj
  203. def soft_delete_invoice_info(
  204. db: Session,
  205. invoice_id: str,
  206. user_id: str = None,
  207. *,
  208. tenant_no: str = None,
  209. user_info=None,
  210. ) -> bool:
  211. obj = get_invoice_info(db, invoice_id)
  212. if not obj:
  213. raise ValueError("开票信息不存在")
  214. if tenant_no and obj.tenant_no != tenant_no:
  215. raise ValueError("开票信息不存在")
  216. ensure_crm_customer_linked_row_in_scope(
  217. db, obj, user_info, obj.tenant_no, message="无权访问该开票信息"
  218. )
  219. assert_row_deletable_by_creator(obj, user_info, user_id=user_id)
  220. obj.del_flag = 1
  221. obj.update_by = user_id
  222. obj.update_time = datetime.now()
  223. db.commit()
  224. return True