Rev 2616 | Rev 2700 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 29-Mar-2010@author: ashish'''from shop2020.model.v1.order.impl import DataServicefrom shop2020.model.v1.order.impl.DataAccessors import create_transaction,\create_order, get_new_transaction, get_transactions_for_customer, get_transaction_status,\get_line_items_for_order, get_transaction, get_transactions_for_shopping_cart_id,\change_transaction_status, get_orders_for_customer,get_orders_for_transaction, get_order,\get_returnable_orders_for_customer, get_cancellable_orders_for_customer, get_orders_by_billing_date,\get_all_orders, change_order_status, get_alerts, set_alert, add_billing_details,\mark_orders_as_manifested, close_session, accept_order, bill_order, mark_orders_as_picked_up,\mark_orders_as_delivered, mark_orders_as_failed, add_jacket_number,\order_outofstock, batch_orders, update_non_delivery_reason, enqueue_transaction_info_email,\get_undelivered_orders, get_order_for_customer, get_valid_order_count,\get_cust_count_with_successful_txn, get_valid_orders_amount_range,\get_valid_orders, toggle_doa_flag, request_pickup_number, authorize_pickup,\receive_return, validate_doa, reship_order, refund_orderfrom shop2020.model.v1.order.impl.Convertors import to_t_transaction,\to_t_alert, to_t_order, to_t_lineitemfrom shop2020.thriftpy.model.v1.order.ttypes import Transaction, OrderStatus,\LineItem, Orderfrom shop2020.utils.Utils import to_py_date, get_fdate_tdateclass OrderServiceHandler:def __init__(self, dbname='transaction'):"""Constructor"""DataService.initialize(dbname)def createTransaction(self, transaction):"""Parameters:- transaction"""try:return create_transaction(transaction)finally:close_session()def getTransaction(self, id):"""Get transaction methods.Parameters:- id"""try:transaction = get_transaction(id)return to_t_transaction(transaction)finally:close_session()def getTransactionsForCustomer(self, customerId, from_date, to_date, status):"""Parameters:- customerId- from_date- to_date- status"""try:current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)transactions = get_transactions_for_customer(customerId, current_from_date, current_to_date, status)t_transaction = []for transaction in transactions:t_transaction.append(to_t_transaction(transaction))return t_transactionfinally:close_session()def getTransactionsForShoppingCartId(self, shoppingCartId):"""Parameters:- shoppingCartId"""try:transactions = get_transactions_for_shopping_cart_id(shoppingCartId)t_transaction = []for transaction in transactions:t_transaction.append(to_t_transaction(transaction))return t_transactionfinally:close_session()def getTransactionStatus(self, transactionId):"""Parameters:- transactionId"""try:return get_transaction_status(transactionId)finally:close_session()def changeTransactionStatus(self, transactionId, status, description):"""Parameters:- transactionId- status- description"""try:return change_transaction_status(transactionId, status, description)finally:close_session()def getOrdersForTransaction(self, transactionId, customerId):"""Returns list of orders for given transaction Id. Also filters based on customer Id so thatonly user who owns the transaction can view its order details.Parameters:- transactionId- customerId"""try:orders = get_orders_for_transaction(transactionId, customerId)return [to_t_order(order) for order in orders]finally:close_session()def getAllOrders(self, status, from_date, to_date, warehouse_id):"""Parameters:- status- from_date- to_date- warehouse_id"""try:current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)orders = get_all_orders(status, current_from_date, current_to_date, warehouse_id)return [to_t_order(order) for order in orders]finally:close_session()def getOrdersByBillingDate(self, status, start_billing_date, end_billing_date, warehouse_id):"""Parameters:- status- start_billing_date- end_billing_date- warehouse_id"""try:current_start_billing_date, current_end_billing_date = get_fdate_tdate(start_billing_date, end_billing_date)orders = get_orders_by_billing_date(status, current_start_billing_date, current_end_billing_date, warehouse_id)return [to_t_order(order) for order in orders]finally:close_session()def getReturnableOrdersForCustomer(self, customerId, limit = None):"""Parameters:- customerId- limit"""try:return get_returnable_orders_for_customer(customerId, limit)finally:close_session()def getCancellableOrdersForCustomer(self, customerId, limit = None):"""Parameters:- customerId- limit"""try:return get_cancellable_orders_for_customer(customerId, limit)finally:close_session()def changeOrderStatus(self, orderId, status, description):"""Parameters:- orderId- status- description-"""try:return change_order_status(self, orderId, status, description)finally:close_session()def acceptOrder(self, orderId):"""Parameters:- orderId"""try:return accept_order(self, orderId)finally:close_session()def billOrder(self, orderId):"""Parameters:- orderId"""try:return bill_order(self, orderId)finally:close_session()def getOrdersForCustomer(self, customerId, from_date, to_date, status):"""Parameters:- customerId- from_date- to_date- status"""try:current_from_date, current_to_date = get_fdate_tdate(from_date, to_date)orders = get_orders_for_customer(customerId, current_from_date, current_to_date, status)return [to_t_order(order) for order in orders]finally:close_session()def getOrderForCustomer(self, orderId, customerId):"""Returns an order for the order Id. Also checks if the order belongs to the customer whose Id is passed.Throws exception if either order Id is invalid or order does not below to the customer whose Id is passed.Parameters:- customerId- orderId"""try:return to_t_order(get_order_for_customer(orderId, customerId))finally:close_session()def getOrder(self, id):"""Parameters:- id"""try:return to_t_order(get_order(id))finally:close_session()def getLineItemsForOrder(self, orderId):"""Parameters:- orderId"""try:lineitems = get_line_items_for_order(orderId)t_lineitems = []for lineitem in lineitems:t_lineitems.append(to_t_lineitem(lineitem))return t_lineitemsfinally:close_session()def addBillingDetails(self, orderId, invoice_number, billed_by):"""Add billing details such as the bill number and the biller to the Order.Parameters:- orderId- invoice_number- billed_by"""try:return add_billing_details(orderId, invoice_number, billed_by)finally:close_session()def addJacketNumber(self, orderId, jacketNumber):"""Adds jacket number to the order. Return false if it doesn't find the order with the given ID.Parameters:- orderId- jacketNumber"""try:return add_jacket_number(orderId, jacketNumber)finally:close_session()def batchOrders(self, warehouseId):"""Create a batch of all the pending orders for the given warehouse.The returned list is orderd by created_timestamp.If there are no pending orders, an empty list is returned.Parameters:- warehouseId"""try:pending_orders = batch_orders(warehouseId)return [to_t_order(order) for order in pending_orders]finally:close_session()def markOrderAsOutOfStock(self, orderId):"""Mark the given order as out of stock. Throws an exception if the order with the given Id couldn't be found.Parameters:- orderId"""try:return order_outofstock(orderId)finally:close_session()def markOrdersAsManifested(self, warehouseId, providerId):"""Marks all BILLED orders for a warehouse and a provider as SHIPPED_FROM_WHParameters:- warehouseId- providerId"""try:return mark_orders_as_manifested(warehouseId, providerId)finally:close_session()def markOrdersAsPickedUp(self, providerId, pickupDetails):"""Marks all SHIPPED_FROM_WH orders of the previous day for a provider as SHIPPED_TO_LOGISTICS.Returns a list of orders that were shipped from warehouse but did not appear in the pick-up report.Raises an exception if we encounter report for an AWB number that we did not ship.Parameters:- providerId- pickupDetails"""try:orders_not_picked_up = mark_orders_as_picked_up(providerId, pickupDetails)return [to_t_order(order) for order in orders_not_picked_up]finally:close_session()def markOrdersAsDelivered(self, providerId, deliveredOrders):"""Marks all orders with AWBs in the given map as delivered. Also sets the delivery timestamp andthe name of the receiver.Raises an exception if we encounter report for an AWB number that we did not ship.Parameters:- providerId- deliveredOrders"""try:mark_orders_as_delivered(providerId, deliveredOrders)finally:close_session()def markOrdersAsFailed(self, providerId, returnedOrders):"""Mark all orders with AWBs in the given map as failed. Also sets the delivery timestamp.Raises an exception if we encounter report for an AWB number that we did not ship.Parameters:- providerId- returnedOrders"""try:mark_orders_as_failed(providerId, returnedOrders)finally:close_session()def updateNonDeliveryReason(self, providerId, undeliveredOrders):"""Update the status description of orders whose AWB numbers are keys of the Map.Parameters:- providerId- undelivered_orders"""try:update_non_delivery_reason(providerId, undeliveredOrders)finally:close_session()def getUndeliveredOrders(self, providerId, warehouseId):"""Returns the list of orders whose delivery time has passed but have not beendelivered yet for the given provider and warehouse. To get a complete list ofundelivered orders, pass them as -1.Returns an empty list if no such orders exist.Parameters:- providerId- warehouseId"""try:undelivered_orders = get_undelivered_orders(providerId, warehouseId)return [to_t_order(order) for order in undelivered_orders]finally:close_session()def enqueueTransactionInfoEmail(self, transactionId):"""Save the email containing order details to be sent to customer later by batch jobParameters:- transactionId"""try:return enqueue_transaction_info_email(transactionId)finally:close_session()def getAlerts(self, orderId, valid):"""Parameters:- orderId- valid"""try:alerts = get_alerts(orderId, valid)t_alerts = []for alert in alerts:t_alerts.append(to_t_alert(alert))return t_alertsfinally:close_session()def setAlert(self, orderId, unset, type, comment):"""Parameters:- orderId- unset- type- comment"""try:set_alert(orderId, unset, type, comment)finally:close_session()def getValidOrderCount(self, ):"""Return the number of valid orders. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)"""try:return get_valid_order_count()finally:close_session()def getNoOfCustomersWithSuccessfulTransaction(self, ):"""Returns the number of distinct customers who have done successful transactions"""try:return get_cust_count_with_successful_txn()finally:close_session()def getValidOrdersAmountRange(self, ):"""Returns the minimum and maximum amounts of a valid order. (OrderStatus >= OrderStatus.SUBMITTED_FOR_PROCESSING)List contains two values, first minimum amount and second maximum amount."""try:return get_valid_orders_amount_range()finally:close_session()def getValidOrders(self, limit):"""Returns list of Orders in descending order by Order creation date. List is restricted to limit Orders.If limit is passed as 0, then all valid Orders are returned.Parameters:- limit"""try:return [to_t_order(order) for order in get_valid_orders(limit)]finally:close_session()def toggleDOAFlag(self, orderId):"""Toggle the DOA flag of an order. This should be used to flag an order for follow-up and unflag it when the follow-up is complete.Returns the final flag status.Throws an exception if the order with the given id couldn't be found or if the order status is not DELVIERY_SUCCESS.Parameters:- orderId"""try:return toggle_doa_flag(orderId)finally:close_session()def requestPickupNumber(self, orderId):"""Sends out an email to the account manager of the original courier provider used to ship the order.If the order status was DELIVERY_SUCCESS, it is changed to be DOA_PICKUP_REQUESTED.If the order status was DOA_PICKUP_REQUESTED, it is left unchanged.For any other status, it returns false.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId"""try:return request_pickup_number(orderId)finally:close_session()def authorizePickup(self, orderId, pickupNumber):"""If the order status is DOA_PICKUP_REQUESTED, it does the following1. Sends out an email to the customer with the dispatch advice that he has to print as an attachment.2. Changes order status to be DOA_PICKUP_AUTHORIZED.3. Returns trueIf the order is any other status, it returns false.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId- pickupNumber"""try:return authorize_pickup(orderId, pickupNumber)finally:close_session()def receiveReturn(self, orderId):"""If the order status is DOA_RETURN_AUTHORIZED or DOA_RETURN_IN_TRANSIT, marks the order status as DOA_RECEIVED and returns true.If the order is in any other state, it returns false.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId"""try:return receive_return(orderId)finally:close_session()def validateDoa(self, orderId, isValid):"""Used to validate the DOA certificate for an order in the DOA_RECEIVED state. If the certificate is valid,the order state is changed to DOA_CERT_PENDING.If the certificate is invalid, the order state is changed to DOA_CERT_INVALID.If the order is in any other state, it returns false.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId- isValid"""try:return validate_doa(orderId, isValid)finally:close_session()def reshipOrder(self, orderId):"""If the order is in SALES_RET_RECEIVED or DOA_CERT_INVALID state, it does the following:1. Creates a new order for processing in the BILLED state. All billing information is saved.2. Marks the current order as one of the final states SALES_RET_RESHIPPED and DOA_INVALID_RESHIPPED depending on what state the order started in.If the order is in DOA_CERT_VALID state, it does the following:1. Creates a new order for processing in the SUBMITTED_FOR_PROCESSING state.2. Creates a return order for the warehouse executive to return the DOA material.3. Marks the current order as the final DOA_RESHIPPED state.Returns the id of the newly created order.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId"""try:return reship_order(orderId)finally:close_session()def refundOrder(self, orderId):"""If the order is in SALES_RET_RECEIVED, DOA_CERT_VALID or DOA_CERT_INVALID state, it does the following:1. Creates a refund request for batch processing.2. Creates a return order for the warehouse executive to return the shipped material.3. Marks the current order as SALES_RET_REFUNDED, DOA_VALID_REFUNDED or DOA_INVALID_REFUNDED final states.If the order is in SUBMITTED_FOR_PROCESSING or INVENTORY_LOW state, it does the following:1. Creates a refund request for batch processing.2. Marks the current order as the REFUNDED final state.Returns True if it is successful, False otherwise.Throws an exception if the order with the given id couldn't be found.Parameters:- orderId"""try:return refund_order(orderId)finally:close_session()def closeSession(self, ):close_session()