Subversion Repositories SmartDukaan

Rev

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

Rev 2339 Rev 2536
Line 1... Line 1...
1
'''
1
'''
2
Created on 29-Mar-2010
2
Created on 29-Mar-2010
3
 
3
 
4
@author: ashish
4
@author: Chandranshu
5
'''
5
'''
-
 
6
 
-
 
7
import sys
-
 
8
import os
-
 
9
import datetime 
-
 
10
import time
-
 
11
from datetime import date
-
 
12
from textwrap import dedent
6
from string import Template
13
from string import Template
-
 
14
 
7
from shop2020.thriftpy.utils.ttypes import HelperServiceException, Mail
15
from sqlalchemy.sql.expression import and_, or_, desc
-
 
16
from sqlalchemy.sql import func
-
 
17
 
-
 
18
from elixir import *
-
 
19
 
-
 
20
from reportlab.pdfgen.canvas import Canvas
-
 
21
from reportlab.rl_config import defaultPageSize
-
 
22
from reportlab.lib.units import inch
-
 
23
PAGE_HEIGHT=defaultPageSize[1]
-
 
24
PAGE_WIDTH=defaultPageSize[0]
8
 
25
 
9
from shop2020.model.v1.order.impl.DataService import Transaction, LineItem,\
26
from shop2020.model.v1.order.impl.DataService import Transaction, LineItem,\
10
    Alerts, Order, BatchNoGenerator
27
    Alerts, Order, BatchNoGenerator
11
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
28
from shop2020.thriftpy.model.v1.order.ttypes import Transaction as T_Transaction,\
12
    TransactionServiceException, TransactionStatus, OrderStatus
29
    TransactionServiceException, TransactionStatus, OrderStatus
13
import datetime 
-
 
14
import time
-
 
15
from elixir import *
-
 
16
from shop2020.utils.Utils import to_py_date, to_java_date
30
from shop2020.utils.Utils import to_py_date, to_java_date
17
from sqlalchemy.sql.expression import and_, or_, desc
-
 
18
from sqlalchemy.sql import func
-
 
19
from shop2020.model.v1 import order
31
from shop2020.model.v1 import order
20
from shop2020.clients.LogisticsClient import LogisticsClient
32
from shop2020.clients.LogisticsClient import LogisticsClient
21
from shop2020.clients.InventoryClient import InventoryClient
33
from shop2020.clients.InventoryClient import InventoryClient
22
from shop2020.clients.UserClient import UserClient
34
from shop2020.clients.UserClient import UserClient
23
from shop2020.clients.HelperClient import HelperClient
35
from shop2020.clients.HelperClient import HelperClient
-
 
36
from shop2020.utils.EmailAttachmentSender import mail, get_attachment_part
24
 
37
 
25
def get_new_transaction():
38
def get_new_transaction():
26
    transaction = Transaction()
39
    transaction = Transaction()
27
    transaction.createdOn = datetime.datetime.now()
40
    transaction.createdOn = datetime.datetime.now()
28
    transaction.status = TransactionStatus.INIT
41
    transaction.status = TransactionStatus.INIT
Line 563... Line 576...
563
        query = query.limit(limit)
576
        query = query.limit(limit)
564
    elif limit < 0:
577
    elif limit < 0:
565
        raise TransactionServiceException(101, "Invalid argument limit " + limit)
578
        raise TransactionServiceException(101, "Invalid argument limit " + limit)
566
    return query.all()
579
    return query.all()
567
 
580
 
-
 
581
def toggle_doa_flag(order_id):
-
 
582
    order = get_order(order_id)
-
 
583
    if(order.doaFlag):
-
 
584
        order.doaFlag = False
-
 
585
    else:
-
 
586
        order.doaFlag = True
-
 
587
    session.commit()
-
 
588
    return order.doaFlag
-
 
589
 
-
 
590
def request_pickup_number(order_id):
-
 
591
    order = get_order(order_id)
-
 
592
    if order.status != OrderStatus.DELIVERY_SUCCESS and order.status != OrderStatus.DOA_PICKUP_REQUESTED:
-
 
593
        return False
-
 
594
    from_user = 'cnc.center@shop2020.in'
-
 
595
    from_pwd = '5h0p2o2o'
-
 
596
    
-
 
597
    subject = "Pickup request"
-
 
598
    try:
-
 
599
        catalog_client = InventoryClient().get_client()
-
 
600
        warehouse = catalog_client.getWarehouse(order.warehouse_id)
-
 
601
        
-
 
602
        logistics_client = LogisticsClient().get_client()
-
 
603
        provider = logistics_client.getProvider(order.logistics_provider_id)
-
 
604
        to_addr = provider.email
-
 
605
        raw_message = '''
-
 
606
        Dear Sir/Madam,
-
 
607
        
-
 
608
        Kindly arrange a pickup today from Pune, Pickup and delivery address are mentioned below.
-
 
609
 
-
 
610
        Pickup CODE: %(provider_code)s
-
 
611
        Total Weight: %(order_weight)s kg
-
 
612
        
-
 
613
        Pickup Address
-
 
614
 
-
 
615
        %(customer_address)s
-
 
616
 
-
 
617
        Delivery Address:
-
 
618
 
-
 
619
        %(warehouse_executive)s
-
 
620
        %(warehouse_address)s
-
 
621
        PIN %(warehouse_pin)s
-
 
622
        Phone: %(warehouse_phone)s
-
 
623
        
-
 
624
        Thanks and Regards
-
 
625
        Anand Sinha
-
 
626
        '''
-
 
627
        message = dedent(raw_message) % { 'provider_code' : provider.accountNo,
-
 
628
                                          'order_weight' : str(order.total_weight),
-
 
629
                                          'customer_address' : __get_order_address(order),
-
 
630
                                          'warehouse_executive' : 'Parmod Kumar',
-
 
631
                                          'warehouse_address' : warehouse.location,
-
 
632
                                          'warehouse_pin' : warehouse.pincode,
-
 
633
                                          'warehouse_phone': '9971573026'}
-
 
634
        mail(from_user, from_pwd, to_addr, subject, message)
-
 
635
        order.status = OrderStatus.DOA_PICKUP_REQUESTED
-
 
636
        order.statusDescription = "Pick up requested for DOA"
-
 
637
        session.commit()
-
 
638
        return True
-
 
639
    except Exception as e:
-
 
640
        print sys.exc_info()[0]
-
 
641
        return False
-
 
642
 
-
 
643
def authorize_pickup(order_id, pickup_number):
-
 
644
    order = get_order(order_id)
-
 
645
    if order.status != OrderStatus.DOA_PICKUP_REQUESTED:
-
 
646
        return False
-
 
647
    from_user = 'help@shop2020.in'
-
 
648
    from_pwd = '5h0p2o2o'
-
 
649
    subject = 'Pickup details'
-
 
650
    filename = "/tmp/" +  str(order.id) +".pdf" 
-
 
651
    try:
-
 
652
        catalog_client = InventoryClient().get_client()
-
 
653
        warehouse = catalog_client.getWarehouse(order.warehouse_id)
-
 
654
        
-
 
655
        logistics_client = LogisticsClient().get_client()
-
 
656
        provider = logistics_client.getProvider(order.logistics_provider_id)
-
 
657
        provider_name = provider.name
-
 
658
        
-
 
659
        to_addr = order.customer_email
-
 
660
        today = date.today().strftime("%d-%B-%Y (%A)")
-
 
661
 
-
 
662
        raw_message = '''
-
 
663
        Dear Mr. %(customer_name)s,
-
 
664
 
-
 
665
        In reference to the return of order ID %(order_id)d. We request you to kindly pack the box properly with bubble wrap, and send it along with the original "DOA" certificate and the invoice.
-
 
666
        Take a printout of the attachment in this mail which contains the delivery address and paste it on the packed parcel.
-
 
667
        We have instructed %(provider_name)s to pick up the material from the below mentioned address on %(date)s. %(provider_name)s pickup request number is %(pickup_request_no)s. Kindly keep the parcel ready.
-
 
668
 
-
 
669
        Pickup will happen from the below mentioned address:-
-
 
670
 
-
 
671
        %(customer_address)s
-
 
672
 
-
 
673
        Do let us know in case of any concerns
-
 
674
 
-
 
675
        Thanks & Regards
-
 
676
        Saholic Team
-
 
677
        '''
-
 
678
        message = dedent(raw_message) % {'customer_name' : order.customer_name,
-
 
679
                                         'order_id' : order_id,
-
 
680
                                         'provider_name' : provider_name,
-
 
681
                                         'date': today,
-
 
682
                                         'pickup_request_no' : pickup_number,
-
 
683
                                         'customer_address' : __get_order_address(order)}
-
 
684
        print message
-
 
685
        __generate_return_advice(order, warehouse, provider, filename)
-
 
686
        mail(from_user, from_pwd, to_addr, subject, message, get_attachment_part(filename))
-
 
687
        order.pickupRequestNo = pickup_number
-
 
688
        order.status = OrderStatus.DOA_RETURN_AUTHORIZED
-
 
689
        order.statusDescription = "DOA pick up authorized"
-
 
690
        session.commit()
-
 
691
        return True
-
 
692
    except Exception as e:
-
 
693
        print sys.exc_info()[0]
-
 
694
        return False
-
 
695
    finally:
-
 
696
        if os.path.exists(filename):
-
 
697
            os.remove(filename)
-
 
698
 
-
 
699
def __get_order_address(order):
-
 
700
    address = order.customer_name + "\n" + order.customer_address1 + "\n"
-
 
701
    if order.customer_address2:
-
 
702
        address = address + order.customer_address2 + "\n"
-
 
703
    address = address + order.customer_city + "\n"
-
 
704
    address = address + order.customer_state + "\n"
-
 
705
    address = address + "PIN " + order.customer_pincode + "\n"
-
 
706
    address = address + "Phone: " + order.customer_mobilenumber
-
 
707
    return address
-
 
708
 
-
 
709
def __generate_return_advice(order, warehouse, provider, filename):    
-
 
710
    pdf = Canvas(filename)
-
 
711
    pdf.setFont('Times-Bold',16)
-
 
712
    address_text = pdf.beginText(inch, PAGE_HEIGHT - inch)
-
 
713
    address_text.textLine("To")
-
 
714
    address_text.textLine("Parmod Kumar")
-
 
715
    for line in warehouse.location.split("\n"):
-
 
716
        address_text.textLine(line)
-
 
717
    address_text.textLine("PIN " + warehouse.pincode)
-
 
718
    address_text.textLine("")
-
 
719
    address_text.textLine("Phone: 9971573026")
-
 
720
    pdf.drawText(address_text)
-
 
721
    
-
 
722
    pdf.setFont('Times-Roman',12)
-
 
723
    order_text = pdf.beginText(inch, PAGE_HEIGHT - 4 * inch)
-
 
724
    order_text.textLine("Pickup CODE: " + provider.accountNo)
-
 
725
    lineitem = order.lineitems[0] 
-
 
726
    order_text.textLine("Content : " + lineitem.brand + " " + lineitem.model_number + " " + lineitem.color)
-
 
727
    order_text.textLine("Declared Value: Rs. " + str(order.total_amount))
-
 
728
    order_text.textLine("Order ID : " + str(order.id))
-
 
729
    pdf.drawText(order_text)
-
 
730
    pdf.showPage()
-
 
731
    pdf.save()
-
 
732
    return filename
-
 
733
 
568
def close_session():
734
def close_session():
569
    if session.is_active:
735
    if session.is_active:
570
        print "session is active. closing it."
736
        print "session is active. closing it."
571
        session.close()
737
        session.close()
572
738