biz.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. # coding=utf-8
  2. import json
  3. import traceback
  4. from django.db import transaction,IntegrityError
  5. from django.db.models import ProtectedError,Q,Sum
  6. from django.conf import settings
  7. from apps.order.models import GoodsDeliver, GoodsDeliverReturn, GoodsDeliver, SaleOrder
  8. from apps.goods.models import GoodsGodownEntry
  9. from apps.plan.models import SalePlan,ProductionPlan
  10. from apps.purchase.models import PurchasePlan, PurchaseOrder, PurchasePayment, PurchaseOrderDetail, GodownEntry, \
  11. GodownEntryReturn, PurchaseUser, PurchasePlanDetail, PurchasePrice, PurchasePay
  12. from apps.material.models import Deliver
  13. from apps.warehouse.models import Inventory, Warehouse, WarehouseStock
  14. from apps.supplier.models import Supplier
  15. from apps.base import Formater
  16. def get_unchecks(user,department_ids,user_ids):
  17. data = []
  18. unchecks_total_count = 0
  19. warehouses_ids = Warehouse.getManagerWarehouses(user)
  20. if user.has_perm('plan.view_sale_plan'):
  21. sale_plan = SalePlan.objects.filter(Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) |
  22. Q(create_user=user))
  23. sale_plan = sale_plan.filter(status=settings.DEFAULT).count()
  24. if sale_plan > 0:
  25. item = {'stack': 'SalePlanList', 'count': sale_plan, 'name': u'销售计划'}
  26. unchecks_total_count += sale_plan
  27. data.append(item)
  28. if user.has_perm('order.view_sale_order'):
  29. sale_order = SaleOrder.objects.filter(
  30. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  31. sale_order = sale_order.filter(status=settings.DEFAULT).count()
  32. if sale_order > 0:
  33. item = {'stack': 'SaleOrderList', 'count': sale_order, 'name': u'销售订单'}
  34. unchecks_total_count += sale_order
  35. data.append(item)
  36. if user.has_perm('purchase.view_purchase_plan'):
  37. purchase_plan = PurchasePlan.objects.filter(
  38. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  39. purchase_plan = purchase_plan.filter(status=settings.DEFAULT).count()
  40. if purchase_plan > 0:
  41. item = {'stack': 'PurchasePlanList', 'count': purchase_plan, 'name': u'采购计划'}
  42. unchecks_total_count += purchase_plan
  43. data.append(item)
  44. if user.has_perm('purchase.view_purchase_order'):
  45. purchase_order = PurchaseOrder.objects.filter(
  46. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  47. purchase_order = purchase_order.filter(status=settings.DEFAULT).count()
  48. if purchase_order > 0:
  49. item = {'stack': 'PurchaseOrderList', 'count': purchase_order, 'name': u'合同管理'}
  50. unchecks_total_count += purchase_order
  51. data.append(item)
  52. if user.has_perm('purchase.view_purchase_payment'):
  53. purchase_payment = PurchasePayment.objects.filter(
  54. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  55. purchase_payment = purchase_payment.filter(status=settings.DEFAULT).count()
  56. if purchase_payment > 0:
  57. item = {'stack': 'PurchasePaymentList', 'count': purchase_payment, 'name': u'付款管理'}
  58. unchecks_total_count += purchase_payment
  59. data.append(item)
  60. if user.has_perm('purchase.view_purchase_invoice'):
  61. purchase_invoice = PurchaseOrderDetail.objects.filter(Q(main__create_user_id__in=user_ids) |
  62. Q(main__department_id__in=department_ids) |
  63. Q(main__create_user=user))
  64. purchase_invoice = purchase_invoice.filter(check_status=settings.DEFAULT).count()
  65. if purchase_invoice > 0:
  66. item = {'stack': 'PurchaseInvoiceList', 'count': purchase_invoice, 'name': u'发票管理'}
  67. unchecks_total_count += purchase_invoice
  68. data.append(item)
  69. if user.has_perm('plan.view_production_plan'):
  70. production_plan = ProductionPlan.objects.filter(
  71. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  72. production_plan = production_plan.filter(status=settings.DEFAULT).count()
  73. if production_plan > 0:
  74. item = {'stack': 'ProductionPlanList', 'count': production_plan, 'name': u'生产计划'}
  75. unchecks_total_count += production_plan
  76. data.append(item)
  77. if user.has_perm('purchase.view_material_godown_entry'):
  78. material_godown_entry = GodownEntry.objects.filter(
  79. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  80. material_godown_entry = material_godown_entry.filter(warehouse_id__in=warehouses_ids,
  81. product_type=GodownEntry.MATERIAL,status=settings.DEFAULT).count()
  82. if material_godown_entry > 0:
  83. item = {'stack': 'MaterialGodowentryList', 'count': material_godown_entry, 'name': u'原料入库'}
  84. unchecks_total_count += material_godown_entry
  85. data.append(item)
  86. if user.has_perm('material.view_material_deliver'):
  87. material_deliver = Deliver.objects.filter(
  88. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  89. material_deliver = material_deliver.filter(warehouse_id__in=warehouses_ids,
  90. product_type=Deliver.MATERIAL,status=settings.DEFAULT).count()
  91. if material_deliver > 0:
  92. item = {'stack': 'MaterialDeliverList', 'count': material_deliver, 'name': u'原料出库'}
  93. unchecks_total_count += material_deliver
  94. data.append(item)
  95. if user.has_perm('purchase.view_material_godown_entry_return'):
  96. material_godown_entry_return = GodownEntryReturn.objects.filter(
  97. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  98. material_godown_entry_return = material_godown_entry_return.filter(warehouse_id__in=warehouses_ids,
  99. type=GodownEntryReturn.MATERIAL,status=settings.DEFAULT).count()
  100. if material_godown_entry_return > 0:
  101. item = {'stack': 'MaterialReturnList', 'count': material_godown_entry_return, 'name': u'原料退货'}
  102. unchecks_total_count += material_godown_entry_return
  103. data.append(item)
  104. if user.has_perm('warehouse.view_material_inventory'):
  105. material_inventory = Inventory.objects.filter(
  106. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  107. material_inventory = material_inventory.filter(warehouse_id__in=warehouses_ids,
  108. product_type=Inventory.MATERIAL,check_status=settings.DEFAULT).count()
  109. if material_inventory > 0:
  110. item = {'stack': 'MaterialInventoryList', 'count': material_inventory, 'name': u'原料盘存'}
  111. unchecks_total_count += material_inventory
  112. data.append(item)
  113. if user.has_perm('purchase.view_consumable_godown_entry'):
  114. consumable_godown_entry = GodownEntry.objects.filter(
  115. Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
  116. consumable_godown_entry = consumable_godown_entry.filter(warehouse_id__in=warehouses_ids,
  117. product_type=GodownEntry.CONSUMABLE,status=settings.DEFAULT).count()
  118. if consumable_godown_entry > 0:
  119. item = {'stack': 'ConsumableGodowentryList', 'count': consumable_godown_entry, 'name': u'耗材入库'}
  120. unchecks_total_count += consumable_godown_entry
  121. data.append(item)
  122. if user.has_perm('material.view_consumable_deliver'):
  123. consumable_deliver = Deliver.objects.filter(
  124. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  125. consumable_deliver = consumable_deliver.filter(warehouse_id__in=warehouses_ids,
  126. product_type=Deliver.CONSUMABLE,status=settings.DEFAULT).count()
  127. if consumable_deliver > 0:
  128. item = {'stack': 'ConsumableDeliverList', 'count': consumable_deliver, 'name': u'耗材出库'}
  129. unchecks_total_count += consumable_deliver
  130. data.append(item)
  131. if user.has_perm('purchase.view_consumable_godown_entry_return'):
  132. consumable_godown_entry_return = GodownEntryReturn.objects.filter(
  133. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  134. consumable_godown_entry_return = consumable_godown_entry_return.filter(warehouse_id__in=warehouses_ids,
  135. type=GodownEntryReturn.CONSUMABLE,status=settings.DEFAULT).count()
  136. if consumable_godown_entry_return > 0:
  137. item = {'stack': 'ConsumableReturnList', 'count': consumable_godown_entry_return, 'name': u'耗材退货'}
  138. unchecks_total_count += consumable_godown_entry_return
  139. data.append(item)
  140. if user.has_perm('warehouse.view_consumable_inventory'):
  141. consumable_inventory = Inventory.objects.filter(
  142. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  143. consumable_inventory = consumable_inventory.filter(warehouse_id__in=warehouses_ids,
  144. product_type=Inventory.CONSUMABLE,check_status=settings.DEFAULT).count()
  145. if consumable_inventory > 0:
  146. item = {'stack': 'ConsumableInventoryList', 'count': consumable_inventory, 'name': u'耗材盘存'}
  147. unchecks_total_count += consumable_inventory
  148. data.append(item)
  149. if user.has_perm('goods.view_goods_godown_entry'):
  150. goods_godown_entry = GoodsGodownEntry.objects.filter(
  151. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  152. goods_godown_entry = goods_godown_entry.filter(warehouse_id__in=warehouses_ids,status=settings.DEFAULT).count()
  153. if goods_godown_entry > 0:
  154. item = {'stack': 'GoodsGodowentryList', 'count': goods_godown_entry, 'name': u'成品入库'}
  155. unchecks_total_count += goods_godown_entry
  156. data.append(item)
  157. if user.has_perm('order.view_goods_deliver'):
  158. goods_deliver = GoodsDeliver.objects.filter(
  159. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  160. goods_deliver = goods_deliver.filter(warehouse_id__in=warehouses_ids,status=settings.DEFAULT).count()
  161. if goods_deliver > 0:
  162. item = {'stack': 'GoodsDeliverList', 'count': goods_deliver, 'name': u'成品出库'}
  163. unchecks_total_count += goods_deliver
  164. data.append(item)
  165. if user.has_perm('goods.view_goods_inventory'):
  166. goods_inventory = Inventory.objects.filter(
  167. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  168. goods_inventory = goods_inventory.filter(warehouse_id__in=warehouses_ids,product_type=Inventory.GOODS,check_status=settings.DEFAULT).count()
  169. if goods_inventory > 0:
  170. item = {'stack': 'GoodsInventoryList', 'count': goods_inventory, 'name': u'成品盘存'}
  171. unchecks_total_count += goods_inventory
  172. data.append(item)
  173. return data,unchecks_total_count
  174. def get_material_data(user, user_ids, department_ids, warehouses_ids):
  175. data = []
  176. if user.has_perm('purchase.view_material_godown_entry'):
  177. godownentrys = GodownEntry.objects.filter(warehouse_id__in=warehouses_ids, product_type=GodownEntry.MATERIAL,
  178. status=settings.DEFAULT)
  179. godownentrys = godownentrys.filter(
  180. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  181. total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  182. item = {'stack': 'MaterialGodowentryList',
  183. 'count': Formater.formatCountShow(total_row['total_count']),
  184. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  185. 'name': '入库管理'}
  186. data.append(item)
  187. if user.has_perm('material.view_material_deliver'):
  188. delivers = Deliver.objects.filter(product_type=Deliver.MATERIAL, warehouse_id__in=warehouses_ids,
  189. status=settings.DEFAULT)
  190. delivers = delivers.filter(
  191. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  192. total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))
  193. item = {'stack': 'MaterialDeliverList',
  194. 'count': Formater.formatCountShow(total_row['total_count']),
  195. 'amount': Formater.formatAmountShow(total_row['total_cost']),
  196. 'name': '出库管理'}
  197. data.append(item)
  198. if user.has_perm('purchase.view_material_godown_entry_return'):
  199. returns = GodownEntryReturn.objects.filter(type=GodownEntryReturn.MATERIAL, warehouse_id__in=warehouses_ids,
  200. status=settings.DEFAULT)
  201. returns = returns.filter(
  202. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  203. total_row = returns.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  204. item = {'stack': 'MaterialReturnList',
  205. 'count': Formater.formatCountShow(total_row['total_count']),
  206. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  207. 'name': '退货管理'}
  208. data.append(item)
  209. if user.has_perm('warehouse.view_material_inventory'):
  210. inventorys = Inventory.objects.filter(product_type=Inventory.MATERIAL, warehouse_id__in=warehouses_ids,
  211. check_status=settings.DEFAULT)
  212. inventorys = inventorys.filter(
  213. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  214. total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  215. item = {'stack': 'MaterialInventoryList',
  216. 'count': Formater.formatCountShow(total_row['total_count']),
  217. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  218. 'name': '盘存管理'}
  219. data.append(item)
  220. if user.has_perm('warehouse.view_material_stock'):
  221. rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
  222. '', 0, '', 1, 1, 20, '', '')
  223. item = {'stack': 'MaterialWarehouseStockList',
  224. 'count': stock_count,
  225. 'amount': warehouse_amount,
  226. 'name': '库存查询'}
  227. data.append(item)
  228. return data
  229. def get_consumable_data(user,user_ids, department_ids, warehouses_ids):
  230. data = []
  231. if user.has_perm('purchase.view_consumable_godown_entry'):
  232. godownentrys = GodownEntry.objects.filter(warehouse_id__in=warehouses_ids, product_type=GodownEntry.CONSUMABLE,
  233. status=settings.DEFAULT)
  234. godownentrys = godownentrys.filter(
  235. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  236. total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  237. item = {'stack': 'ConsumableGodowentryList',
  238. 'count': Formater.formatCountShow(total_row['total_count']),
  239. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  240. 'name': '入库管理'}
  241. data.append(item)
  242. if user.has_perm('material.view_consumable_deliver'):
  243. delivers = Deliver.objects.filter(product_type=Deliver.CONSUMABLE, warehouse_id__in=warehouses_ids,
  244. status=settings.DEFAULT)
  245. delivers = delivers.filter(
  246. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  247. total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))
  248. item = {'stack': 'ConsumableDeliverList',
  249. 'count': Formater.formatCountShow(total_row['total_count']),
  250. 'amount': Formater.formatAmountShow(total_row['total_cost']),
  251. 'name': '出库管理'}
  252. data.append(item)
  253. if user.has_perm('purchase.view_consumable_godown_entry_return'):
  254. returns = GodownEntryReturn.objects.filter(type=GodownEntryReturn.CONSUMABLE, warehouse_id__in=warehouses_ids,
  255. status=settings.DEFAULT)
  256. returns = returns.filter(
  257. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  258. total_row = returns.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  259. item = {'stack': 'ConsumableReturnList',
  260. 'count': Formater.formatCountShow(total_row['total_count']),
  261. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  262. 'name': '退货管理'}
  263. data.append(item)
  264. if user.has_perm('warehouse.view_consumable_inventory'):
  265. inventorys = Inventory.objects.filter(product_type=Inventory.CONSUMABLE, warehouse_id__in=warehouses_ids,
  266. check_status=settings.DEFAULT)
  267. inventorys = inventorys.filter(
  268. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  269. total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  270. item = {'stack': 'ConsumableInventoryList',
  271. 'count': Formater.formatCountShow(total_row['total_count']),
  272. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  273. 'name': '盘存管理'}
  274. data.append(item)
  275. if user.has_perm('warehouse.view_consumable_stock'):
  276. rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
  277. '', 1, '', 1, 1, 20, '', '')
  278. item = {'stack': 'ConsumableWarehouseStockList',
  279. 'count': stock_count,
  280. 'amount': warehouse_amount,
  281. 'name': '库存查询'}
  282. data.append(item)
  283. return data
  284. def get_goods_data(user, user_ids, department_ids, warehouses_ids):
  285. data = []
  286. if user.has_perm('goods.view_goods_godown_entry'):
  287. godownentrys = GoodsGodownEntry.objects.filter(warehouse_id__in=warehouses_ids, status=settings.DEFAULT)
  288. godownentrys = godownentrys.filter(
  289. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  290. total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  291. item = {'stack': 'GoodsGodowentryList',
  292. 'count': Formater.formatCountShow(total_row['total_count']),
  293. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  294. 'name': '入库管理'}
  295. data.append(item)
  296. if user.has_perm('order.view_goods_deliver'):
  297. delivers = GoodsDeliver.objects.filter(warehouse_id__in=warehouses_ids, status=settings.DEFAULT)
  298. delivers = delivers.filter(
  299. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  300. total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))
  301. item = {'stack': 'GoodsDeliverList',
  302. 'count': Formater.formatCountShow(total_row['total_count']),
  303. 'amount': Formater.formatAmountShow(total_row['total_cost']),
  304. 'name': '出库管理'}
  305. data.append(item)
  306. if user.has_perm('goods.view_goods_inventory'):
  307. inventorys = Inventory.objects.filter(product_type=Inventory.GOODS, warehouse_id__in=warehouses_ids,
  308. check_status=settings.DEFAULT)
  309. inventorys = inventorys.filter(
  310. Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
  311. total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))
  312. item = {'stack': 'GoodsInventoryList',
  313. 'count': Formater.formatCountShow(total_row['total_count']),
  314. 'amount': Formater.formatAmountShow(total_row['total_amount']),
  315. 'name': '盘存管理'}
  316. data.append(item)
  317. if user.has_perm('warehouse.view_goods_stock'):
  318. rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
  319. '', 2, '', 1, 1, 20, '', '')
  320. item = {'stack': 'GoodsWarehouseStockList',
  321. 'count': stock_count,
  322. 'amount': warehouse_amount,
  323. 'name': '库存查询'}
  324. data.append(item)
  325. return data
  326. def get_plan_data(id):
  327. purchaseplan = PurchasePlan.getById(id)
  328. purchase_users = []
  329. rows = PurchaseUser.objects.filter(purchase=purchaseplan)
  330. purchase_users.extend([s[0] for s in rows.values_list('purchase_user__name')])
  331. purchase_user_text = ','.join(purchase_users)
  332. main_data = {
  333. 'id': purchaseplan.id,
  334. 'no': purchaseplan.no,
  335. 'name': purchaseplan.name,
  336. 'total_count': Formater.formatCountShow(purchaseplan.total_count),
  337. 'demend_user_text': purchaseplan.demend_user and purchaseplan.demend_user.name or '',
  338. 'create_user_text': purchaseplan.create_user and purchaseplan.create_user.name or '',
  339. 'create_time': Formater.formatStrTime(purchaseplan.create_time),
  340. 'status':purchaseplan.status,
  341. 'status_text': settings.CHECK_STATUS_CHOICES[purchaseplan.status][1],
  342. 'check_user_text': purchaseplan.check_user and purchaseplan.check_user.name or '',
  343. 'check_time': Formater.formatStrTime(purchaseplan.check_time),
  344. 'purchase_user_text': purchase_user_text,
  345. 'notes': purchaseplan.notes,
  346. # 'check_user2': purchaseplan.check_user2 and purchaseplan.check_user2.name or '',
  347. # 'check_time2': Formater.formatStrTime(purchaseplan.check_time2),
  348. # 'check_user3': purchaseplan.check_user3 and purchaseplan.check_user3.name or '',
  349. # 'check_time3': Formater.formatStrTime(purchaseplan.check_time3),
  350. }
  351. data = {
  352. 'main_data': main_data,
  353. 'plan_details': [],
  354. }
  355. purchasedetails = PurchasePlanDetail.objects.filter(purchase=purchaseplan)
  356. for purchasedetail in purchasedetails:
  357. count = purchasedetail.purchase_count - purchasedetail.product.stock_count
  358. if count < 0:
  359. count = 0
  360. item = {
  361. 'id': purchasedetail.id,
  362. 'name': purchasedetail.product.name,
  363. 'model': purchasedetail.product.model,
  364. 'unit': purchasedetail.product.unit,
  365. 'product_time': Formater.formatStrDate(purchasedetail.product_time),
  366. 'quality_request': purchasedetail.quality_request_text or '',
  367. 'count': Formater.formatCountShow(count),
  368. 'stock_count': Formater.formatCountShow(purchasedetail.product.stock_count),
  369. 'purchase_count': Formater.formatCountShow(purchasedetail.purchase_count),
  370. 'purchase_price_item': []
  371. }
  372. purchaseprices = PurchasePrice.objects.filter(purchase_detail=purchasedetail, report=True)
  373. for purchaseprice in purchaseprices:
  374. price_item = {
  375. 'id': purchaseprice.id,
  376. 'purchase_user_text': purchaseprice.purchase_user and purchaseprice.purchase_user.name or '',
  377. 'supplier_text': purchaseprice.supplier and purchaseprice.supplier.name or '',
  378. 'price': Formater.formatPriceShow(purchaseprice.price),
  379. 'notes': purchaseprice.notes,
  380. 'detail_id': purchasedetail.id,
  381. }
  382. item['purchase_price_item'].append(price_item)
  383. data['plan_details'].append(item)
  384. return data
  385. def get_payment_data(id):
  386. company = PurchasePayment.getById(id).department.getCompany()
  387. payment = PurchasePayment.getById(id)
  388. rows = PurchaseOrderDetail.objects.filter(main_id=payment.order_id)
  389. order_id = None
  390. main_data = {
  391. 'no': payment.no,
  392. 'order_no': payment.order.no,
  393. 'supplier_name': payment.order.supplier and payment.order.supplier.name or '',
  394. 'supplier_account': payment.order.supplier and payment.order.supplier.account or '',
  395. 'amount':Formater.formatAmountShow(payment.amount),
  396. 'status':payment.status,
  397. 'status_text':settings.CHECK_STATUS_CHOICES[payment.status][1],
  398. 'apply_amount':Formater.formatAmountShow(payment.apply_amount),
  399. 'actual_amount':Formater.formatAmountShow(payment.actual_amount),
  400. 'create_time': Formater.formatStrTime(payment.create_time),
  401. 'create_user_text': payment.create_user and payment.create_user.name or '',
  402. 'check_time': Formater.formatStrTime(payment.check_time),
  403. 'check_user_text': payment.check_user and payment.check_user.name or '',
  404. 'notes': payment.notes,
  405. 'review_user_text': payment.review_user and payment.review_user.name or '',
  406. 'review_time': Formater.formatStrTime(payment.review_time),
  407. 'ratify_user_text': payment.ratify_user and payment.ratify_user.name or '',
  408. 'ratify_time': Formater.formatStrTime(payment.ratify_time),
  409. }
  410. data = {
  411. 'items_data': [],
  412. 'payment_data': [],
  413. 'main_data': main_data
  414. }
  415. for row in rows:
  416. item = {
  417. 'id': row.id,
  418. 'name': row.product.name,
  419. 'model': row.product.model,
  420. 'order_no': row.main.no,
  421. 'amount': Formater.formatAmountShow(row.amount),
  422. }
  423. data['items_data'].append(item)
  424. item_rows = PurchasePay.objects.filter(main_id=payment.id)
  425. for row in item_rows:
  426. item = {
  427. 'id': row.id,
  428. 'payment_type': row.payment_type and row.payment_type.name or '',
  429. 'payment_time': Formater.formatStrTime(row.payment_time),
  430. 'actual_amount': Formater.formatAmountShow(row.actual_amount),
  431. 'payment_user': row.payment_user and row.payment_user.name or '',
  432. 'notes': row.notes,
  433. }
  434. data['payment_data'].append(item)
  435. return data