def save_trade_commission_data(cursor):
    print cursor
    for row in cursor:
        file_id = row[0]
        contract_id = row[1]
        buyer = BpBasic.get_business_with_database_id(database_id=row[2])
        seller = BpBasic.get_business_with_database_id(database_id=row[3])
        product = ProductItem.get_product_with_database_id(database_id=row[4])
        price = float(row[5])
        quantity = float(row[6])
        packing = Packaging.objects.get(name=row[7])

        ship_start = row[8]
        ship_end = row[9]
        other_info = row[10]

        seller_broker = None if not row[
            11] else BpBasic.get_business_with_database_id(database_id=row[11])
        buyer_broker = None if not row[
            12] else BpBasic.get_business_with_database_id(database_id=row[12])
        commission_type = CommissionType.objects.get(name=row[13])

        commission = float(row[14])
        buyer_broker_commission_type = CommissionType.objects.get(name=row[15])
        buyer_broker_commission = 0.00 if not row[16] else float(row[16])
        difference = float(row[17])
        discount = float(row[18])
        date = row[19]

        if row[20]:
            contractual_buyer = BpBasic.get_business_with_database_id(
                database_id=row[20])
        else:
            contractual_buyer = buyer

        if not Transaction.objects.filter(file_id=file_id).exists():
            new_trade = Transaction()
            new_trade.date = date
            new_trade.buyer = buyer
            new_trade.seller = seller
            new_trade.contractual_buyer = contractual_buyer
            new_trade.product_item = product
            new_trade.quantity = quantity
            new_trade.price = price
            new_trade.packaging = packing
            new_trade.shipment_start = ship_start
            new_trade.shipment_end = ship_end
            new_trade.file_id = file_id
            new_trade.contract_id = contract_id
            new_trade.other_info = other_info
            new_trade.created_by = created_by
            new_trade.save()
            new_commission = TrCommission()
            new_commission.seller_broker = seller_broker
            new_commission.transaction = new_trade
            new_commission.buyer_broker = buyer_broker
            new_commission.buyer_broker_comm_type = buyer_broker_commission_type
            new_commission.buyer_broker_comm = buyer_broker_commission
            new_commission.commission_type = commission_type
            new_commission.commission = commission
            new_commission.difference = difference
            new_commission.discount = discount
            new_commission.save()
            print 'File Created'
        else:
            print 'File Already Exist in the System'
Exemplo n.º 2
0
    def save_transaction(self, data, user):
        try:
            tran_id = data.get('id')
            product_specs = data.get('productSpecification')
            commission_data = data.get('commission')
            basic = data.get('basic')
            buyer_id = basic.get('buyerId')
            contractual_buyer_id = basic.get('contractualBuyerId')
            seller_id = basic.get('sellerId')
            packaging_id = basic.get('packagingId')
            product_item_id = basic.get('productItemId')

            transaction_date = dateutil.parser.parse(
                str(basic.get('date')).replace('"', ''))
            transaction_date = transaction_date.replace(hour=0,
                                                        minute=0,
                                                        second=0,
                                                        microsecond=0)

            shipment_start = dateutil.parser.parse(
                str(basic.get('shipmentStart')).replace('"', ''))
            shipment_start = shipment_start.replace(hour=0,
                                                    minute=0,
                                                    second=0,
                                                    microsecond=0)

            shipment_end = dateutil.parser.parse(
                str(basic.get('shipmentEnd')).replace('"', ''))
            shipment_end = shipment_end.replace(hour=0,
                                                minute=0,
                                                second=0,
                                                microsecond=0)

            packaging = Packaging.objects.get(id=packaging_id)
            commission_type_id = commission_data.get('typeId')
            seller_broker_id = commission_data.get('sellerBrokerId')
            buyer_broker_id = commission_data.get('buyerBrokerId')
            buyer_broker_commission_type_id = commission_data.get(
                'buyerBrokerCommissionTypeId')

            if buyer_id == seller_id:
                return Response({
                    'success': False,
                    'message': 'Buyer and Seller can not be same'
                })
            buyer = BpBasic.objects.get(bp_id=buyer_id)
            seller = BpBasic.objects.get(bp_id=seller_id)
            product_item = ProductItem.objects.get(id=product_item_id)
            contractual_buyer = BpBasic.objects.get(bp_id=contractual_buyer_id)
            commission_type = CommissionType.objects.get(id=commission_type_id)
            buyer_broker_commission_type = None if not buyer_broker_id else CommissionType.objects.get(
                id=buyer_broker_commission_type_id)
            seller_broker = None if not seller_broker_id else BpBasic.objects.get(
                bp_id=seller_broker_id)
            buyer_broker = None if not buyer_broker_id else BpBasic.objects.get(
                bp_id=buyer_broker_id)

            #update
            if tran_id:
                success_message = self.messages['successPUT']
                transaction = Transaction.objects.get(tr_id=tran_id)
                commission = TrCommission.objects.get(
                    transaction__tr_id=tran_id)
                transaction.updated_by = user
                transaction.updated_at = dt.now()

            #add
            else:
                success_message = self.messages['successPOST']
                transaction = Transaction()
                commission = TrCommission()
                transaction.created_by = user

            # saving all transaction details
            transaction.seller = seller
            transaction.buyer = buyer
            transaction.product_item = product_item
            transaction.contractual_buyer = contractual_buyer
            transaction.other_info = basic.get('otherInfo')
            transaction.file_id = basic.get('fileId')
            transaction.contract_id = basic.get('contractId')
            transaction.product_specification = product_specs
            transaction.shipment_start = shipment_start.date()
            transaction.shipment_end = shipment_end.date()
            transaction.price = float(basic.get('price'))
            transaction.quantity = float(basic.get('quantity'))
            transaction.quantity_fcl = float(basic.get('quantityFcl'))
            transaction.date = transaction_date.date()
            transaction.packaging = packaging
            # saving all commission details
            commission.discount = float(commission_data.get('discount'))
            commission.difference = float(commission_data.get('difference'))
            commission.commission = float(commission_data.get('commission'))
            commission.commission_type = commission_type
            commission.seller_broker = seller_broker
            commission.buyer_broker = buyer_broker
            commission.buyer_broker_comm_type = buyer_broker_commission_type
            commission.buyer_broker_comm = float(
                commission_data.get('buyerBrokerCommission', 0.00))
            transaction.save()
            commission.transaction = transaction
            commission.save()

            # Assigning not shipped status to new transaction
            if not tran_id:
                shipment = TrShipment()
                shipment.transaction = transaction
                shipment.not_shipped = True
                shipment.created_by = user
                shipment.save()

            primary_transaction_id = basic.get('primaryTransaction')
            primary_shipment_id = basic.get('primaryShipment')

            if primary_transaction_id:
                primary_transaction = Transaction.objects.get(
                    file_id=primary_transaction_id)
                if hasattr(transaction, 'secondary_trade'):
                    secondary_trade = transaction.secondary_trade
                else:
                    secondary_trade = SecondaryTrades()
                secondary_trade.transaction = transaction
                secondary_trade.primary_trade = primary_transaction
                secondary_trade.save()
            else:
                if hasattr(transaction, 'secondary_trade'):
                    transaction.secondary_trade.delete()

            if primary_shipment_id:
                primary_shipment = Transaction.objects.get(
                    file_id=primary_shipment_id)
                if hasattr(transaction, 'partial_shipment'):
                    partial_shipment = transaction.partial_shipment
                else:
                    partial_shipment = PartialShipments()
                partial_shipment.transaction = transaction
                partial_shipment.primary_shipment = primary_shipment
                partial_shipment.save()
            else:
                if hasattr(transaction, 'partial_shipment'):
                    transaction.primary_trade.all().delete()

            # Making Transaction Change Log

            if tran_id:
                #TODO: Have to make detail log message
                log = '<span class="titled">Updated Transaction</span>'
                TransactionChangeLog.add_change_log(user, log, transaction)
            else:
                log = '<span class="titled">Created Transaction</span>'
                TransactionChangeLog.add_change_log(user, log, transaction)

            return Response(
                {
                    'fileId': transaction.file_id,
                    'success': True,
                    'message': success_message % transaction.file_id
                },
                status=status.HTTP_200_OK)
        except Exception, e:
            return Response({'success': False, 'message': str(e)})