Exemplo n.º 1
0
 def get_all_business_drop_down(self, type, page, business_id):
     if not page:
         return BpBasic.get_drop_down_obj(type)
     elif page == 'tradeBook':
         return Transaction.get_business_type_drop_down_for_transaction_page(type, business_id)
     elif page == 'localTradeBook':
         return LocalTrade.get_business_type_drop_down_for_transaction_page(type, business_id)
Exemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        user = request.user
        has_business_analytics_access = user.profile.right_business_analytics
        start_date = request.GET.get(u'startDate')
        end_date = request.GET.get(u'endDate')
        start_date = dateutil.parser.parse(str(start_date).replace('"', ''))
        end_date = dateutil.parser.parse(str(end_date).replace('"', ''))
        all_transaction = Transaction.get_trades_in_date_range(business, start_date, end_date)
        all_transaction = TransactionListAPI.get_transaction_list(all_transaction, page_type, user)
        if not has_business_analytics_access:
            return Response({
                success: False,
                message: 'No access for business analytics on you account.'
            })



        return Response({
            'allTransaction': all_transaction
        }, status=status.HTTP_200_OK)
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        user = request.user
        business = user.profile.business
        page_type = request.GET.get(u'pageType')

        start_date = request.GET.get(u'startDate')
        end_date = request.GET.get(u'endDate')

        if page_type == 'tradeBook':
            if end_date and start_date:
                start_date = dateutil.parser.parse(
                    str(start_date).replace('"', ''))
                end_date = dateutil.parser.parse(
                    str(end_date).replace('"', ''))
                all_transaction = Transaction.get_trades_in_date_range(
                    business, start_date, end_date)
            else:
                all_transaction = Transaction.objects
            all_transaction = Transaction.get_status(all_transaction)
        elif page_type == 'expectedArrival':
            all_transaction = Transaction.get_expected_arrival(business)
        elif page_type == 'tradeDispute':
            all_transaction = Transaction.get_business_all_disputed_trades(
                business)
        elif page_type == 'arrivedList':
            all_transaction = Transaction.get_arrived_at_port_not_completed(
                business)
        elif page_type == 'expiredShipment':
            all_transaction = Transaction.get_not_shipped_not_washout_not_completed(
                business)
        elif page_type == 'next30DaysExpiration':
            all_transaction = Transaction.get_expiration_next_30_days(business)

        all_transaction, column_header = self.get_transaction_list(
            all_transaction, page_type, user)

        return Response(
            {
                'success': True,
                'columnHeader': column_header,
                'transactions': all_transaction
            },
            status=status.HTTP_200_OK)
def get_vessel_position_for_all_non_shipped(business):
    return get_vessel_position_transactions(
        Transaction.get_expected_arrival(business))
Exemplo n.º 5
0
    def get_trade_dashboard_data(self, user):
        business = user.profile.business

        ## Get Get Graph Data

        today = dt.now()
        one_year = today - rd(years=1)
        six_month = today - rd(months=6)
        last_month = today - rd(months=1)
        last_week = today - rd(days=1)

        date_limits = [one_year, six_month, last_month, last_week]

        #New Trades
        new_trades = map(
            lambda end_date: Transaction.get_trades_in_date_range(
                business, end_date, today).count(), date_limits)
        #Completed Trades
        complete_trades = map(
            lambda end_date: Transaction.
            get_all_business_completed_trades_in_date_range(
                business, end_date, today).count(), date_limits)

        #Washout Trades
        washout_trades = map(
            lambda end_date: Transaction.get_all_washout_trades_in_date_range(
                business, end_date, today).count(), date_limits)

        #Arrived Not Completed Trades

        arrived_trades = map(
            lambda end_date: Transaction.
            get_arrived_at_port_not_completed_in_date_range(
                business, end_date, today).count(), date_limits)

        #Buyer Contract
        buyer_contract_trades = map(
            lambda end_date:
            Transaction.get_buyer_contract_trades_in_date_range(
                business, end_date, today).count(), date_limits)

        ## Count of trades

        #Total Count
        business_trades = Transaction.objects.filter(
            created_by__profile__business=business)
        total_trades = business_trades.count()
        total_completed_trades = Transaction.get_all_business_completed_trades(
            business)
        total_completed_trades = total_completed_trades.count()

        #contract count
        self_contract_count = Transaction.get_self_contract_trades(
            business).count()
        own_contract_count = total_trades - self_contract_count

        #Washout Count
        washout_count = Transaction.get_business_all_washout(business).count()
        washout_at_x_count = Transaction.objects.filter(created_by__profile__business=business) \
            .filter(washout__total_difference=0.00).count()

        #commissionCount

        fixed_count = Transaction.objects.filter(commission__commission_type__name='Fixed') \
            .filter(created_by__profile__business=user.profile.business).count()
        percentage_count = Transaction.objects.filter(commission__commission_type__name= 'Percentage') \
            .filter(created_by__profile__business=user.profile.business).count()

        # Trades Arrived At Port But Not Complete
        arrived_at_port_not_completed = Transaction.get_arrived_at_port_not_completed(
            business)
        arrived_at_port_not_completed_count = arrived_at_port_not_completed.count(
        )

        # Trades Shipped Expected Arrival
        expected_arrival = Transaction.get_expected_arrival(business)
        expected_arrival_count = expected_arrival.count()

        #Trades Not Shipped
        not_shipped = Transaction.get_not_shipped_not_washout_not_completed(
            business)
        not_shipped_count = not_shipped.count()

        #Trade Dispute Count
        disputed_trades = Transaction.get_business_all_disputed_trades(
            business)
        disputed_trades_count = disputed_trades.count()

        #Secondary Trades
        secondary_trades_count = SecondaryTrades.objects.count()
        partial_keys = ['A', 'B', 'C', 'D']
        partial_shipment = Transaction.objects.filter(
            reduce(lambda x, y: x | y,
                   [Q(file_id__icontains=key) for key in partial_keys]))
        partial_shipment_count = partial_shipment.count()

        return {
            'multiBar': {
                'newTrades': new_trades,
                'completeTrades': complete_trades,
                'washoutTrades': washout_trades,
                'arrivedTrades': arrived_trades,
                'buyerContractTrades': buyer_contract_trades
            },
            'tradesCount': {
                'total':
                total_trades,
                'completed':
                total_completed_trades,
                'percentageCompleted':
                (float(total_completed_trades) / float(total_trades)) * 100
            },
            'commissionCount': {
                'fixedCount':
                fixed_count,
                'percentageCount':
                percentage_count,
                'percentagePercentageCommission':
                (float(percentage_count) / float(total_trades)) * 100
            },
            'contractCount': {
                'selfContractCount':
                self_contract_count,
                'ownContractCount':
                own_contract_count,
                'percentageOwnContractCount':
                (float(own_contract_count) / float(total_trades)) * 100
            },
            'washoutCount': {
                'washoutCount':
                washout_count,
                'washoutAtXCount':
                washout_at_x_count,
                'washoutPercentage':
                (float(washout_count) / float(total_trades)) * 100
            },
            'subTrades': {
                'secondaryTradeCount':
                secondary_trades_count,
                'partialShipmentCount':
                partial_shipment_count,
                'secondaryTradePercentage':
                (float(secondary_trades_count) / float(total_trades)) * 100
            },
            'tradeDisputeCount': disputed_trades_count,
            'arrivedNotCompletedCount': arrived_at_port_not_completed_count,
            'expectedArrivalCount': expected_arrival_count,
            'notShippedCount': not_shipped_count
        }
Exemplo n.º 6
0
 def get(self, request, *args, **kwargs):
     arrived_at_port_not_completed = Transaction.get_arrived_at_port_not_completed(
     )
     return Response({'arrivedNotCompletedList': []})
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.º 8
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)})