Subversion Repositories SmartDukaan

Rev

Rev 8195 | Rev 8790 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8195 Rev 8788
Line 12... Line 12...
12
from elixir import *
12
from elixir import *
13
from sqlalchemy.sql import func
13
from sqlalchemy.sql import func
14
import urllib
14
import urllib
15
import httplib
15
import httplib
16
from shop2020.utils.EmailAttachmentSender import mail, mail_html
16
from shop2020.utils.EmailAttachmentSender import mail, mail_html
-
 
17
from shop2020.model.v1.order.impl.model.UserWalletHistory import UserWalletHistory
-
 
18
from shop2020.utils.Utils import to_java_date, to_py_date
-
 
19
from shop2020.clients.PaymentClient import PaymentClient
17
 
20
 
18
 
21
 
19
 
22
 
20
if __name__ == '__main__' and __package__ is None:
23
if __name__ == '__main__' and __package__ is None:
21
    import os
24
    import os
Line 26... Line 29...
26
    OrderType
29
    OrderType
27
from shop2020.model.v1.order.impl.DataAccessors import get_recharge_orders_for_status, update_recharge_order_status,\
30
from shop2020.model.v1.order.impl.DataAccessors import get_recharge_orders_for_status, update_recharge_order_status,\
28
    update_recharge_transaction_status, get_next_invoice_number
31
    update_recharge_transaction_status, get_next_invoice_number
29
from shop2020.model.v1.order.impl import DataService
32
from shop2020.model.v1.order.impl import DataService
30
from shop2020.model.v1.order.impl.DataService import RechargeTransaction, HotspotStore,\
33
from shop2020.model.v1.order.impl.DataService import RechargeTransaction, HotspotStore,\
31
    WalletForCompany, WalletHistoryForCompany, RechargeCollection, Company, HotspotServiceMatrix
34
    WalletForCompany, WalletHistoryForCompany, RechargeCollection, Company, HotspotServiceMatrix,\
-
 
35
    RechargeVoucherTracker
32
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
36
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
33
from shop2020.model.v1.order.impl.RechargeService import checkTransactionStatus, getRefunds, getBalance 
37
from shop2020.model.v1.order.impl.RechargeService import checkTransactionStatus, getRefunds, getBalance 
34
 
-
 
-
 
38
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, between
35
 
39
 
36
def main():
40
def main():
37
    parser = optparse.OptionParser()
41
    parser = optparse.OptionParser()
38
    parser.add_option("-H", "--host", dest="hostname",
42
    parser.add_option("-H", "--host", dest="hostname",
39
                      default="localhost",
43
                      default="localhost",
Line 49... Line 53...
49
                      action="store_true",
53
                      action="store_true",
50
                      help="")
54
                      help="")
51
    parser.add_option("-c", "--collection", dest="collection",
55
    parser.add_option("-c", "--collection", dest="collection",
52
                      action="store_true",
56
                      action="store_true",
53
                      help="")
57
                      help="")
-
 
58
    parser.add_option("-R", "--recon", dest="recon",
-
 
59
                      action="store_true",
-
 
60
                      help="")
54
    parser.add_option("-T", "--topup", dest="topup",
61
    parser.add_option("-T", "--topup", dest="topup",
55
                      action="store_true",
62
                      action="store_true",
56
                      help="")    
63
                      help="")    
57
    parser.add_option("-t", "--txn-id", dest="txn_id",
64
    parser.add_option("-t", "--txn-id", dest="txn_id",
58
                   type="int",
65
                   type="int",
Line 64... Line 71...
64
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
71
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
65
    DataService.initialize(db_hostname=options.hostname, echoOn=True)
72
    DataService.initialize(db_hostname=options.hostname, echoOn=True)
66
        
73
        
67
    if options.refund:
74
    if options.refund:
68
        processRefunds()
75
        processRefunds()
-
 
76
    if options.recon:
-
 
77
        processRecon()
69
    if options.unknown:
78
    if options.unknown:
70
        processUnknownTransactions()
79
        processUnknownTransactions()
71
    if options.authorized:
80
    if options.authorized:
72
        processAuthorizedTransactions(options.txn_id)
81
        processAuthorizedTransactions(options.txn_id)
73
    if options.collection:
82
    if options.collection:
Line 82... Line 91...
82
        compute_recharge_collection(d)
91
        compute_recharge_collection(d)
83
        compute_website_recharge_collection(d, oldBalance, newBalance)
92
        compute_website_recharge_collection(d, oldBalance, newBalance)
84
    if options.topup:
93
    if options.topup:
85
        topup_company_wallet(1,100000)
94
        topup_company_wallet(1,100000)
86
 
95
 
-
 
96
def processRecon():
-
 
97
    cdate = datetime.datetime.now() + timedelta(days = -1)
-
 
98
    startTime = datetime.datetime(cdate.year, cdate.month, cdate.day)
-
 
99
    endTime = startTime + timedelta(days=1)
-
 
100
 
-
 
101
    '''
-
 
102
    A - All such orders for which we have attempted the recharge and recharge is either successful or unknown.
-
 
103
    B - All such orders for which we have attempted the recharge and we received the refund after this time window.
-
 
104
    C - All such orders for which we have received the refund in this time window, although the recharge was attempted before this window.
-
 
105
    X1 = A + B - C         Net amount debited for all the customers in this time window.
-
 
106
    X2 - Total amount credited to all customers in their wallet under some promotion.
-
 
107
    X3 - Net difference in wallet amount between this time window.
-
 
108
    X4 - Payment received through gateway from all customer in this time window.
-
 
109
    X5 - Payment refunded through gateway by all customer in this time window.
-
 
110
    '''
-
 
111
    
-
 
112
    A = 0
-
 
113
    B = 0
-
 
114
    C = 0
-
 
115
    X1 = 0
-
 
116
    X2 = 0
-
 
117
    X3 = 0
-
 
118
    X4 = 0
-
 
119
    X5 = 0
-
 
120
    D = 0
-
 
121
    
-
 
122
    sorder = session.query(func.sum(RechargeOrder.totalAmount - RechargeOrder.couponAmount), func.sum(RechargeOrder.totalAmount), func.sum(RechargeOrder.couponAmount)).filter(RechargeOrder.status.in_([RechargeOrderStatus.RECHARGE_SUCCESSFUL, RechargeOrderStatus.PAYMENT_SUCCESSFUL])).filter(RechargeOrder.creationTimestamp.between(startTime,endTime)).first()    
-
 
123
    if sorder and sorder[0]:
-
 
124
        A = int(sorder[0])
-
 
125
    
-
 
126
    forder = session.query(func.sum(RechargeOrder.totalAmount - RechargeOrder.couponAmount), func.sum(RechargeOrder.totalAmount), func.sum(RechargeOrder.couponAmount)).filter(RechargeOrder.status.in_([RechargeOrderStatus.RECHARGE_FAILED, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, RechargeOrderStatus.REFUNDED, RechargeOrderStatus.PARTIALLY_REFUNDED])).filter(RechargeOrder.creationTimestamp.between(startTime,endTime)).filter(not_(RechargeOrder.responseTimestamp.between(startTime,endTime))).first()
-
 
127
    if forder and forder[0]:
-
 
128
        B = int(forder[0])
-
 
129
    
-
 
130
    rorder = session.query(func.sum(RechargeOrder.totalAmount - RechargeOrder.couponAmount), func.sum(RechargeOrder.totalAmount), func.sum(RechargeOrder.couponAmount)).filter(RechargeOrder.status.in_([RechargeOrderStatus.RECHARGE_FAILED, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, RechargeOrderStatus.REFUNDED, RechargeOrderStatus.PARTIALLY_REFUNDED])).filter(RechargeOrder.responseTimestamp.between(startTime,endTime)).filter(not_(RechargeOrder.creationTimestamp.between(startTime,endTime))).first()
-
 
131
    if rorder and rorder[0]:
-
 
132
        C = int(rorder[0])
-
 
133
 
-
 
134
    X1 = X1 + A + B - C
-
 
135
    
-
 
136
    rv = session.query(func.sum(RechargeVoucherTracker.amount)).filter(RechargeVoucherTracker.issuedOn.between(startTime,endTime)).first()
-
 
137
    if rv and rv[0]:
-
 
138
        X2 = int(rv[0])
-
 
139
        
-
 
140
    uw = session.query(func.sum(UserWalletHistory.amount)).filter(UserWalletHistory.timestamp.between(startTime,endTime)).first()
-
 
141
    if uw and uw[0]:
-
 
142
        X3 = int(uw[0])
-
 
143
    
-
 
144
    
-
 
145
    
-
 
146
    pc = PaymentClient().get_client()
-
 
147
    payments = pc.getPayments(to_java_date(startTime - datetime.timedelta(minutes = 30)), to_java_date(endTime + datetime.timedelta(minutes = 30)), 2, 1) + pc.getPayments(to_java_date(startTime - datetime.timedelta(minutes = 30)), to_java_date(endTime + datetime.timedelta(minutes = 30)), 2, 2) + pc.getPayments(to_java_date(startTime - datetime.timedelta(minutes = 30)), to_java_date(endTime + datetime.timedelta(minutes = 30)), 8, 1) + pc.getPayments(to_java_date(startTime - datetime.timedelta(minutes = 30)), to_java_date(endTime + datetime.timedelta(minutes = 30)), 8, 2)
-
 
148
    for payment in payments:
-
 
149
        if payment.isDigital and to_py_date(payment.successTimestamp) >= startTime and to_py_date(payment.successTimestamp) <= endTime:  
-
 
150
            X4 = X4 + payment.amount
-
 
151
 
-
 
152
    
-
 
153
    
-
 
154
    refunds = RechargeOrder.query.filter(RechargeOrder.status.in_([RechargeOrderStatus.PARTIALLY_REFUNDED, RechargeOrderStatus.REFUNDED])).filter(RechargeOrder.refundTimestamp.between(startTime, endTime)).all()
-
 
155
    pc = PaymentClient().get_client()
-
 
156
    for refund in refunds:
-
 
157
        payments = pc.getPaymentForRechargeTxnId(refund.transaction_id)
-
 
158
        for payment in payments:
-
 
159
            if payment.gatewayId in (1,2) and payment.status == 8 and payment.isDigital:
-
 
160
                X5 = X5 + payment.refundAmount
-
 
161
    
-
 
162
    D = X1+X3+X5-X2-X4
-
 
163
    
-
 
164
    maildata = "<html><body><table border='1'><thead><th>Symbol</th><th>Type</th><th>Amount</th></thead><tbody>"
-
 
165
    maildata += "<tr><td>A</td><td>Recharge Amount</td><td>" + str(A) + "</td></tr>"
-
 
166
    maildata += "<tr><td>B</td><td>Recharge Amount (Refunded in Future)</td><td>" + str(B) + "</td></tr>"
-
 
167
    maildata += "<tr><td>C</td><td>Recharge Refund Amount</td><td>" + str(C) + "</td></tr>"
-
 
168
    maildata += "<tr><td>X1=A+B-C</td><td>Net Recharge Amount</td><td>" + str(X1) + "</td></tr>"
-
 
169
    maildata += "<tr><td>X2</td><td>Gift Amount</td><td>" + str(X2) + "</td></tr>"
-
 
170
    maildata += "<tr><td>X3</td><td>Wallet Difference</td><td>" + str(X3) + "</td></tr>"
-
 
171
    maildata += "<tr><td>X4</td><td>Payment Amount</td><td>" + str(X4) + "</td></tr>"
-
 
172
    maildata += "<tr><td>X5</td><td>Payment Refund Amount</td><td>" + str(X5) + "</td></tr>"
-
 
173
    maildata += "<tr><td>D=X1+X3+X5-X2-X4</td><td>Net Reconciliation Difference</td><td>" + str(D) + "</td></tr>"
-
 
174
    maildata += "</tbody></table>"
-
 
175
 
-
 
176
    if D != 0:
-
 
177
        mismatches = RechargeOrder.query.filter(RechargeOrder.status.in_([RechargeOrderStatus.RECHARGE_SUCCESSFUL, RechargeOrderStatus.PAYMENT_SUCCESSFUL])).filter(RechargeOrder.creationTimestamp.between(startTime, endTime)).filter(RechargeOrder.responseTimestamp.between(endTime, endTime + timedelta(minutes = 10))).all()
-
 
178
        if mismatches and len(mismatches) > 0:
-
 
179
            maildata += "<h3>Possible mismatch orders</h3><table style='margin-top:30px;' border='1'><thead><th>OrderId</th><th>Total Amount</th><th>Wallet Amount</th><th>Coupon Amount</th><th>Creation Time</th><th>Response Time</th></thead><tbody>"
-
 
180
            for mismatch in mismatches:
-
 
181
                maildata += "<tr><td>" + str(mismatch.id) + "</td><td>" + str(mismatch.totalAmount) + "</td><td>" + str(mismatch.walletAmount) + "</td><td>" + str(mismatch.couponAmount) + "</td><td>" + str(mismatch.creationTimestamp) + "</td><td>" + str(mismatch.responseTimestamp) + "</td></tr>"        
-
 
182
            maildata += "</tbody></table>"
-
 
183
    
-
 
184
    maildata += "</body></html>"
-
 
185
    mail_html("cnc.center@shop2020.in", "5h0p2o2o", ["rajveer.singh@shop2020.in", "anupam.singh@shop2020.in", "rajneesh.arora@shop2020.in"], "Customer Recharge Reconciliation for Date:- " + startTime.strftime("%d-%m-%Y"), maildata, [])
-
 
186
        
-
 
187
 
87
def processRefunds():
188
def processRefunds():
88
    todate = datetime.datetime.now()
189
    todate = datetime.datetime.now()
89
    for i in range(15):
190
    for i in range(10):
90
        orderDate = todate + datetime.timedelta(days= -i)
191
        orderDate = todate + datetime.timedelta(days= -i)
91
        refunds = getRefunds(orderDate)
192
        refunds = getRefunds(orderDate)
92
        for key in refunds.keys():
193
        for key in refunds.keys():
93
            refund = refunds.get(key)
194
            refund = refunds.get(key)
94
            refundAmount = refund[0]
195
            refundAmount = refund[0]