Subversion Repositories SmartDukaan

Rev

Rev 21162 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 22-Jun-2012

This script fetches the scan mismatches as per the invoices received
and sends an email to concerned people.

@author: mandeep
'''
from optparse import OptionParser
from shop2020.clients.WarehouseClient import WarehouseClient
from shop2020.utils import EmailAttachmentSender
from shop2020.utils.Utils import to_java_date
import datetime

def generateScanMismatches(date):
    warehouseClient = WarehouseClient().get_client()
    return warehouseClient.fetchScansPerInvoiceNumber(to_java_date(datetime.datetime.strptime(date, '%Y-%m-%d')))

def reportMismatches(mismatches, date):
    print mismatches
    body = ["\t".join(['Date', 'Invoice Number', 'Supplier', 'No. of Items as per Invoice', 'No. of Items actually scanned'])]
    unscannedCount = 0
    for invoiceScan in mismatches:
        body.append("\t".join([date, invoiceScan.invoiceNumber, invoiceScan.supplierName, str(invoiceScan.numItems), str(invoiceScan.scannedQuantity)]))
        unscannedCount += invoiceScan.numItems - invoiceScan.scannedQuantity
    subject = date + ': All items scanned IN!'
    if body.__len__() > 1:
        subject = date + ': ' + str(abs(unscannedCount))
        if unscannedCount > 0:
            subject += ' items not scanned IN yet'
        else:
            subject += ' extra items got scanned IN'
    EmailAttachmentSender.mail('cnc.center@shop2020.in', '5h0p2o2o', ['amit.gupta@shop2020.in','shivam9718617332@gmail.com', 'deena.nath@smartdukaan.com'], subject, "\n".join(body))

def generateAndReportScanMismatches(date):
    reportMismatches(generateScanMismatches(date), date)

def main():
    parser = OptionParser()
    parser.add_option("-d", "--date", dest="date", default = datetime.date.today().isoformat(), type='string', help='date in YYYY-MM-DD format')
    (options, args) = parser.parse_args()
    generateAndReportScanMismatches(options.date)

if __name__ == '__main__':
    main()