# coding=utf-8
import json
import traceback

from django.db import transaction,IntegrityError
from django.db.models import ProtectedError,Q,Sum
from django.conf import settings

from apps.order.models import GoodsDeliver, GoodsDeliverReturn, GoodsDeliver, SaleOrder
from apps.goods.models import GoodsGodownEntry
from apps.plan.models import SalePlan,ProductionPlan
from apps.purchase.models import PurchasePlan, PurchaseOrder, PurchasePayment, PurchaseOrderDetail, GodownEntry, \
    GodownEntryReturn, PurchaseUser, PurchasePlanDetail, PurchasePrice, PurchasePay
from apps.material.models import Deliver
from apps.warehouse.models import Inventory, Warehouse, WarehouseStock
from apps.supplier.models import Supplier

from apps.base import Formater


def get_unchecks(user,department_ids,user_ids):
    data = []
    unchecks_total_count = 0
    warehouses_ids = Warehouse.getManagerWarehouses(user)
    if user.has_perm('plan.view_sale_plan'):
        sale_plan = SalePlan.objects.filter(Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) |
                                            Q(create_user=user))
        sale_plan = sale_plan.filter(status=settings.DEFAULT).count()
        if sale_plan > 0:
            item = {'stack': 'SalePlanList', 'count': sale_plan, 'name': u'销售计划'}
            unchecks_total_count += sale_plan
            data.append(item)

    if user.has_perm('order.view_sale_order'):
        sale_order = SaleOrder.objects.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        sale_order = sale_order.filter(status=settings.DEFAULT).count()
        if sale_order > 0:
            item = {'stack': 'SaleOrderList', 'count': sale_order, 'name': u'销售订单'}
            unchecks_total_count += sale_order
            data.append(item)

    if user.has_perm('purchase.view_purchase_plan'):
        purchase_plan = PurchasePlan.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        purchase_plan = purchase_plan.filter(status=settings.DEFAULT).count()
        if purchase_plan > 0:
            item = {'stack': 'PurchasePlanList', 'count': purchase_plan, 'name': u'采购计划'}
            unchecks_total_count += purchase_plan
            data.append(item)

    if user.has_perm('purchase.view_purchase_order'):
        purchase_order = PurchaseOrder.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        purchase_order = purchase_order.filter(status=settings.DEFAULT).count()
        if purchase_order > 0:
            item = {'stack': 'PurchaseOrderList', 'count': purchase_order, 'name': u'合同管理'}
            unchecks_total_count += purchase_order
            data.append(item)

    if user.has_perm('purchase.view_purchase_payment'):
        purchase_payment = PurchasePayment.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        purchase_payment = purchase_payment.filter(status=settings.DEFAULT).count()
        if purchase_payment > 0:
            item = {'stack': 'PurchasePaymentList', 'count': purchase_payment, 'name': u'付款管理'}
            unchecks_total_count += purchase_payment
            data.append(item)

    if user.has_perm('purchase.view_purchase_invoice'):
        purchase_invoice = PurchaseOrderDetail.objects.filter(Q(main__create_user_id__in=user_ids) |
                                                              Q(main__department_id__in=department_ids) |
                                                              Q(main__create_user=user))
        purchase_invoice = purchase_invoice.filter(check_status=settings.DEFAULT).count()
        if purchase_invoice > 0:
            item = {'stack': 'PurchaseInvoiceList', 'count': purchase_invoice, 'name': u'发票管理'}
            unchecks_total_count += purchase_invoice
            data.append(item)

    if user.has_perm('plan.view_production_plan'):
        production_plan = ProductionPlan.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        production_plan = production_plan.filter(status=settings.DEFAULT).count()
        if production_plan > 0:
            item = {'stack': 'ProductionPlanList', 'count': production_plan, 'name': u'生产计划'}
            unchecks_total_count += production_plan
            data.append(item)

    if user.has_perm('purchase.view_material_godown_entry'):
        material_godown_entry = GodownEntry.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        material_godown_entry = material_godown_entry.filter(warehouse_id__in=warehouses_ids,
                                                    product_type=GodownEntry.MATERIAL,status=settings.DEFAULT).count()
        if material_godown_entry > 0:
            item = {'stack': 'MaterialGodowentryList', 'count': material_godown_entry, 'name': u'原料入库'}
            unchecks_total_count += material_godown_entry
            data.append(item)

    if user.has_perm('material.view_material_deliver'):
        material_deliver = Deliver.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        material_deliver = material_deliver.filter(warehouse_id__in=warehouses_ids,
                                                   product_type=Deliver.MATERIAL,status=settings.DEFAULT).count()
        if material_deliver > 0:
            item = {'stack': 'MaterialDeliverList', 'count': material_deliver, 'name': u'原料出库'}
            unchecks_total_count += material_deliver
            data.append(item)

    if user.has_perm('purchase.view_material_godown_entry_return'):
        material_godown_entry_return = GodownEntryReturn.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        material_godown_entry_return = material_godown_entry_return.filter(warehouse_id__in=warehouses_ids,
                                                        type=GodownEntryReturn.MATERIAL,status=settings.DEFAULT).count()
        if material_godown_entry_return > 0:
            item = {'stack': 'MaterialReturnList', 'count': material_godown_entry_return, 'name': u'原料退货'}
            unchecks_total_count += material_godown_entry_return
            data.append(item)

    if user.has_perm('warehouse.view_material_inventory'):
        material_inventory = Inventory.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        material_inventory = material_inventory.filter(warehouse_id__in=warehouses_ids,
                                                product_type=Inventory.MATERIAL,check_status=settings.DEFAULT).count()
        if material_inventory > 0:
            item = {'stack': 'MaterialInventoryList', 'count': material_inventory, 'name': u'原料盘存'}
            unchecks_total_count += material_inventory
            data.append(item)

    if user.has_perm('purchase.view_consumable_godown_entry'):
        consumable_godown_entry = GodownEntry.objects.filter(
            Q(create_user_id__in=user_ids) | Q(department_id__in=department_ids) | Q(create_user=user))
        consumable_godown_entry = consumable_godown_entry.filter(warehouse_id__in=warehouses_ids,
                                                product_type=GodownEntry.CONSUMABLE,status=settings.DEFAULT).count()
        if consumable_godown_entry > 0:
            item = {'stack': 'ConsumableGodowentryList', 'count': consumable_godown_entry, 'name': u'耗材入库'}
            unchecks_total_count += consumable_godown_entry
            data.append(item)

    if user.has_perm('material.view_consumable_deliver'):
        consumable_deliver = Deliver.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        consumable_deliver = consumable_deliver.filter(warehouse_id__in=warehouses_ids,
                                                    product_type=Deliver.CONSUMABLE,status=settings.DEFAULT).count()
        if consumable_deliver > 0:
            item = {'stack': 'ConsumableDeliverList', 'count': consumable_deliver, 'name': u'耗材出库'}
            unchecks_total_count += consumable_deliver
            data.append(item)

    if user.has_perm('purchase.view_consumable_godown_entry_return'):
        consumable_godown_entry_return = GodownEntryReturn.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        consumable_godown_entry_return = consumable_godown_entry_return.filter(warehouse_id__in=warehouses_ids,
                                                type=GodownEntryReturn.CONSUMABLE,status=settings.DEFAULT).count()
        if consumable_godown_entry_return > 0:
            item = {'stack': 'ConsumableReturnList', 'count': consumable_godown_entry_return, 'name': u'耗材退货'}
            unchecks_total_count += consumable_godown_entry_return
            data.append(item)

    if user.has_perm('warehouse.view_consumable_inventory'):
        consumable_inventory = Inventory.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        consumable_inventory = consumable_inventory.filter(warehouse_id__in=warehouses_ids,
                                            product_type=Inventory.CONSUMABLE,check_status=settings.DEFAULT).count()
        if consumable_inventory > 0:
            item = {'stack': 'ConsumableInventoryList', 'count': consumable_inventory, 'name': u'耗材盘存'}
            unchecks_total_count += consumable_inventory
            data.append(item)

    if user.has_perm('goods.view_goods_godown_entry'):
        goods_godown_entry = GoodsGodownEntry.objects.filter(
                    Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        goods_godown_entry = goods_godown_entry.filter(warehouse_id__in=warehouses_ids,status=settings.DEFAULT).count()
        if goods_godown_entry > 0:
            item = {'stack': 'GoodsGodowentryList', 'count': goods_godown_entry, 'name': u'成品入库'}
            unchecks_total_count += goods_godown_entry
            data.append(item)

    if user.has_perm('order.view_goods_deliver'):
        goods_deliver = GoodsDeliver.objects.filter(
                    Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        goods_deliver = goods_deliver.filter(warehouse_id__in=warehouses_ids,status=settings.DEFAULT).count()
        if goods_deliver > 0:
            item = {'stack': 'GoodsDeliverList', 'count': goods_deliver, 'name': u'成品出库'}
            unchecks_total_count += goods_deliver
            data.append(item)

    if user.has_perm('goods.view_goods_inventory'):
        goods_inventory = Inventory.objects.filter(
                        Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        goods_inventory = goods_inventory.filter(warehouse_id__in=warehouses_ids,product_type=Inventory.GOODS,check_status=settings.DEFAULT).count()
        if goods_inventory > 0:
            item = {'stack': 'GoodsInventoryList', 'count': goods_inventory, 'name': u'成品盘存'}
            unchecks_total_count += goods_inventory
            data.append(item)

    return data,unchecks_total_count

def get_material_data(user, user_ids, department_ids, warehouses_ids):
    data = []
    if user.has_perm('purchase.view_material_godown_entry'):
        godownentrys = GodownEntry.objects.filter(warehouse_id__in=warehouses_ids, product_type=GodownEntry.MATERIAL,
                                                  status=settings.DEFAULT)
        godownentrys = godownentrys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'MaterialGodowentryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '入库管理'}
        data.append(item)

    if user.has_perm('material.view_material_deliver'):
        delivers = Deliver.objects.filter(product_type=Deliver.MATERIAL, warehouse_id__in=warehouses_ids,
                                          status=settings.DEFAULT)
        delivers = delivers.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))

        item = {'stack': 'MaterialDeliverList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_cost']),
                'name': '出库管理'}
        data.append(item)

    if user.has_perm('purchase.view_material_godown_entry_return'):
        returns = GodownEntryReturn.objects.filter(type=GodownEntryReturn.MATERIAL, warehouse_id__in=warehouses_ids,
                                                   status=settings.DEFAULT)
        returns = returns.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = returns.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'MaterialReturnList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '退货管理'}
        data.append(item)

    if user.has_perm('warehouse.view_material_inventory'):
        inventorys = Inventory.objects.filter(product_type=Inventory.MATERIAL, warehouse_id__in=warehouses_ids,
                                              check_status=settings.DEFAULT)
        inventorys = inventorys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'MaterialInventoryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '盘存管理'}
        data.append(item)

    if user.has_perm('warehouse.view_material_stock'):
        rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
                                                                                        '', 0, '', 1, 1, 20, '', '')
        item = {'stack': 'MaterialWarehouseStockList',
                'count': stock_count,
                'amount': warehouse_amount,
                'name': '库存查询'}
        data.append(item)
    return data

def get_consumable_data(user,user_ids, department_ids, warehouses_ids):
    data = []
    if user.has_perm('purchase.view_consumable_godown_entry'):
        godownentrys = GodownEntry.objects.filter(warehouse_id__in=warehouses_ids, product_type=GodownEntry.CONSUMABLE,
                                                  status=settings.DEFAULT)
        godownentrys = godownentrys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'ConsumableGodowentryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '入库管理'}
        data.append(item)

    if user.has_perm('material.view_consumable_deliver'):
        delivers = Deliver.objects.filter(product_type=Deliver.CONSUMABLE, warehouse_id__in=warehouses_ids,
                                          status=settings.DEFAULT)
        delivers = delivers.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))

        item = {'stack': 'ConsumableDeliverList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_cost']),
                'name': '出库管理'}
        data.append(item)

    if user.has_perm('purchase.view_consumable_godown_entry_return'):
        returns = GodownEntryReturn.objects.filter(type=GodownEntryReturn.CONSUMABLE, warehouse_id__in=warehouses_ids,
                                                   status=settings.DEFAULT)
        returns = returns.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = returns.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'ConsumableReturnList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '退货管理'}
        data.append(item)

    if user.has_perm('warehouse.view_consumable_inventory'):
        inventorys = Inventory.objects.filter(product_type=Inventory.CONSUMABLE, warehouse_id__in=warehouses_ids,
                                              check_status=settings.DEFAULT)
        inventorys = inventorys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'ConsumableInventoryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '盘存管理'}
        data.append(item)

    if user.has_perm('warehouse.view_consumable_stock'):
        rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
                                                                                        '', 1, '', 1, 1, 20, '', '')
        item = {'stack': 'ConsumableWarehouseStockList',
                'count': stock_count,
                'amount': warehouse_amount,
                'name': '库存查询'}
        data.append(item)
    return data

def get_goods_data(user, user_ids, department_ids, warehouses_ids):
    data = []
    if user.has_perm('goods.view_goods_godown_entry'):
        godownentrys = GoodsGodownEntry.objects.filter(warehouse_id__in=warehouses_ids, status=settings.DEFAULT)
        godownentrys = godownentrys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = godownentrys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'GoodsGodowentryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '入库管理'}
        data.append(item)

    if user.has_perm('order.view_goods_deliver'):
        delivers = GoodsDeliver.objects.filter(warehouse_id__in=warehouses_ids, status=settings.DEFAULT)
        delivers = delivers.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = delivers.aggregate(total_count=Sum('total_count'), total_cost=Sum('total_cost'))

        item = {'stack': 'GoodsDeliverList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_cost']),
                'name': '出库管理'}
        data.append(item)

    if user.has_perm('goods.view_goods_inventory'):
        inventorys = Inventory.objects.filter(product_type=Inventory.GOODS, warehouse_id__in=warehouses_ids,
                                              check_status=settings.DEFAULT)
        inventorys = inventorys.filter(
            Q(department_id__in=department_ids) | Q(create_user_id__in=user_ids) | Q(create_user=user))
        total_row = inventorys.aggregate(total_count=Sum('total_count'), total_amount=Sum('total_amount'))

        item = {'stack': 'GoodsInventoryList',
                'count': Formater.formatCountShow(total_row['total_count']),
                'amount': Formater.formatAmountShow(total_row['total_amount']),
                'name': '盘存管理'}
        data.append(item)

    if user.has_perm('warehouse.view_goods_stock'):
        rows, total, stock_count, warehouse_amount = WarehouseStock.objects.fetch_stock(user, '', '', '', '',
                                                                                        '', 2, '', 1, 1, 20, '', '')
        item = {'stack': 'GoodsWarehouseStockList',
                'count': stock_count,
                'amount': warehouse_amount,
                'name': '库存查询'}
        data.append(item)
    return data

def get_plan_data(id):
    purchaseplan = PurchasePlan.getById(id)
    purchase_users = []
    rows = PurchaseUser.objects.filter(purchase=purchaseplan)
    purchase_users.extend([s[0] for s in rows.values_list('purchase_user__name')])
    purchase_user_text = ','.join(purchase_users)
    main_data = {
        'id': purchaseplan.id,
        'no': purchaseplan.no,
        'name': purchaseplan.name,
        'total_count': Formater.formatCountShow(purchaseplan.total_count),
        'demend_user_text': purchaseplan.demend_user and purchaseplan.demend_user.name or '',
        'create_user_text': purchaseplan.create_user and purchaseplan.create_user.name or '',
        'create_time': Formater.formatStrTime(purchaseplan.create_time),
        'status':purchaseplan.status,
        'status_text': settings.CHECK_STATUS_CHOICES[purchaseplan.status][1],
        'check_user_text': purchaseplan.check_user and purchaseplan.check_user.name or '',
        'check_time': Formater.formatStrTime(purchaseplan.check_time),
        'purchase_user_text': purchase_user_text,
        'notes': purchaseplan.notes,
        # 'check_user2': purchaseplan.check_user2 and purchaseplan.check_user2.name or '',
        # 'check_time2': Formater.formatStrTime(purchaseplan.check_time2),
        # 'check_user3': purchaseplan.check_user3 and purchaseplan.check_user3.name or '',
        # 'check_time3': Formater.formatStrTime(purchaseplan.check_time3),
    }
    data = {
        'main_data': main_data,
        'plan_details': [],
    }
    purchasedetails = PurchasePlanDetail.objects.filter(purchase=purchaseplan)
    for purchasedetail in purchasedetails:
        count = purchasedetail.purchase_count - purchasedetail.product.stock_count
        if count < 0:
            count = 0
        item = {
            'id': purchasedetail.id,
            'name': purchasedetail.product.name,
            'model': purchasedetail.product.model,
            'unit': purchasedetail.product.unit,
            'product_time': Formater.formatStrDate(purchasedetail.product_time),
            'quality_request': purchasedetail.quality_request_text or '',
            'count': Formater.formatCountShow(count),
            'stock_count': Formater.formatCountShow(purchasedetail.product.stock_count),
            'purchase_count': Formater.formatCountShow(purchasedetail.purchase_count),
            'purchase_price_item': []
        }
        purchaseprices = PurchasePrice.objects.filter(purchase_detail=purchasedetail, report=True)
        for purchaseprice in purchaseprices:
            price_item = {
                'id': purchaseprice.id,
                'purchase_user_text': purchaseprice.purchase_user and purchaseprice.purchase_user.name or '',
                'supplier_text': purchaseprice.supplier and purchaseprice.supplier.name or '',
                'price': Formater.formatPriceShow(purchaseprice.price),
                'notes': purchaseprice.notes,
                'detail_id': purchasedetail.id,
            }
            item['purchase_price_item'].append(price_item)

        data['plan_details'].append(item)
    return data

def get_payment_data(id):
    company = PurchasePayment.getById(id).department.getCompany()
    payment = PurchasePayment.getById(id)
    rows = PurchaseOrderDetail.objects.filter(main_id=payment.order_id)
    order_id = None

    main_data = {
        'no': payment.no,
        'order_no': payment.order.no,
        'supplier_name': payment.order.supplier and payment.order.supplier.name or '',
        'supplier_account': payment.order.supplier and payment.order.supplier.account or '',
        'amount':Formater.formatAmountShow(payment.amount),
        'status':payment.status,
        'status_text':settings.CHECK_STATUS_CHOICES[payment.status][1],
        'apply_amount':Formater.formatAmountShow(payment.apply_amount),
        'actual_amount':Formater.formatAmountShow(payment.actual_amount),
        'create_time': Formater.formatStrTime(payment.create_time),
        'create_user_text': payment.create_user and payment.create_user.name or '',
        'check_time': Formater.formatStrTime(payment.check_time),
        'check_user_text': payment.check_user and payment.check_user.name or '',
        'notes': payment.notes,
        'review_user_text': payment.review_user and payment.review_user.name or '',
        'review_time': Formater.formatStrTime(payment.review_time),
        'ratify_user_text': payment.ratify_user and payment.ratify_user.name or '',
        'ratify_time': Formater.formatStrTime(payment.ratify_time),
    }
    data = {
        'items_data': [],
        'payment_data': [],
        'main_data': main_data
    }
    for row in rows:
        item = {
            'id': row.id,
            'name': row.product.name,
            'model': row.product.model,
            'order_no': row.main.no,
            'amount': Formater.formatAmountShow(row.amount),
        }
        data['items_data'].append(item)
    item_rows = PurchasePay.objects.filter(main_id=payment.id)
    for row in item_rows:
        item = {
            'id': row.id,
            'payment_type': row.payment_type and row.payment_type.name or '',
            'payment_time': Formater.formatStrTime(row.payment_time),
            'actual_amount': Formater.formatAmountShow(row.actual_amount),
            'payment_user': row.payment_user and row.payment_user.name or '',
            'notes': row.notes,
        }
        data['payment_data'].append(item)

    return data
