Example #1
0
File: utils.py Project: Arpaso/ETS
def send_dispatched(waybill, compas=None, cache_prefix='send_dispatched'):
    """Submits dispatched and validated waybills to COMPAS"""
    cache_key = "%s_%s" % (cache_prefix, waybill.pk)
    sending = cache.get(cache_key, False)
    if sending:
        return
    
    cache.set(cache_key, True)
    
    if not compas:
        compas = waybill.order.warehouse.compas.pk
    try:
        with transaction.commit_on_success(using=compas) as tr:
            CURR_CODE = waybill.pk[len(compas):]
            CONTAINER_NUMBER = waybill.container_one_number

            special_case = waybill.loading_details.count() == 2 and waybill.container_two_number
            code_letter = u'A'

            for index, loading in enumerate( waybill.loading_details.all() ):

                if special_case:
                    CURR_CODE = u"%s%s" % (code_letter, waybill.pk)
                    code_letter = u'B'
                    if index == 1:
                        CONTAINER_NUMBER = waybill.container_two_number

                is_bulk = loading.stock_item.is_bulk

                try:
                    order_item = loading.get_order_item()
                except ets_models.OrderItem.DoesNotExist:
                    raise ValidationError("System can not find order item. Check database.")
                try:
                    DestCompas = waybill.destination.compas.pk
                except:
                    try:
                        DestCompas = waybill.destination.compas_text
                    except:
                        DestCompas = ''
                try:
                    Destination = waybill.destination.pk
                except:
                    Destination = ''

                IsValid = False
                if order_item.lti_id != 1:
                    if bool(compas_models.LtiOriginal.objects.using(compas).filter(lti_id = order_item.lti_id)):
                        IsValid = True
                    else:
                        IsValid = False
                else:
                    IsValid = False

                if not IsValid:
                    message = "The LTI %s is not available in the COMPAS Station %s"%( waybill.order.code, compas)
                    waybill.validated = False
                    waybill.save()
                    raise ValidationError(message)
                    return 

                call_db_procedure('write_waybill.dispatch', (
                    CURR_CODE, 
                    waybill.dispatch_date.strftime("%Y%m%d"), 
                    waybill.order.origin_type, 
                    waybill.order.warehouse.location.pk, 
                    waybill.order.warehouse.pk,
                    waybill.order.warehouse.name, 
                    waybill.order.location.pk,
                    Destination,
                    order_item.lti_id, 
                    waybill.loading_date.strftime("%Y%m%d"),
                    waybill.order.consignee.pk, 

                    waybill.transaction_type, 
                    waybill.transport_vehicle_registration,
                    waybill.transport_trailer_registration,
                    waybill.transport_type,
                    waybill.dispatch_remarks, 

                    waybill.dispatcher_person.code, 
                    waybill.dispatcher_person.compas.pk, 
                    waybill.dispatcher_person.title, 

                    waybill.order.transport_code, 
                    waybill.order.transport_ouc,

                    waybill.transport_driver_name, 
                    waybill.transport_driver_licence,

                    CONTAINER_NUMBER,

                    DestCompas,

                    loading.stock_item.origin_id, 
                    loading.stock_item.commodity.category.pk, 
                    loading.stock_item.commodity.pk, 
                    loading.stock_item.package.pk, 
                    loading.stock_item.allocation_code, 
                    loading.stock_item.quality,

                    u'%.3f' % loading.total_weight_net, 
                    u'%.3f' % loading.total_weight_gross, 
                    u'%.3f' % (1 if is_bulk else loading.number_of_units), 
                    u'%.3f' % (1 if is_bulk else loading.unit_weight_net), 
                    u'%.3f' % (1 if is_bulk else loading.unit_weight_gross), 

                    None, #p_odaid
                    None, #p_losstype
                    None, #p_lossreason
                    '', #p_loannumber
                ), compas)

    except Exception, err:

        message = hasattr(err, 'messages') and u"\n".join(err.messages) or unicode(err)
        for error_message in reduce_compas_errors(message):
            ets_models.CompasLogger.objects.create(action=ets_models.CompasLogger.DISPATCH, 
                                                   compas_id=compas, waybill=waybill,
                                                   status=ets_models.CompasLogger.FAILURE, 
                                                   message=error_message)
        waybill.validated = False
Example #2
0
File: utils.py Project: Arpaso/ETS
def send_received(waybill, compas=None, cache_prefix='send_received'):
    """Submits received and validated waybills to COMPAS"""
    cache_key = "%s_%s" % (cache_prefix, waybill.pk)
    sending = cache.get(cache_key, False)
    if sending:
        return
    
    cache.set(cache_key, True)
    
    if not compas:
        compas = waybill.receipt_warehouse.compas.pk

    try:
        with transaction.commit_on_success(using=compas) as tr:
            CURR_CODE = waybill.pk[len(compas):]
            ## Check if dispatch_master is there...
            CURR_CODE_FULL = u'%s%sP'%( compas ,CURR_CODE)
            if bool(compas_models.DispatchMaster.objects.filter(code=CURR_CODE_FULL).using(compas)):
                IsValid = True
            else:
                message = "The Dispatch %s is not available in the COMPAS Station %s"%( waybill.pk, compas)
                raise ValidationError(message)
                waybill.receipt_validated = False
                waybill.save()
                return 
                
            ## check if containers = 2 & lines = 2
            special_case = waybill.loading_details.count() == 2 and waybill.container_two_number
            code_letter = u'A'
            p_receiving_location = waybill.receipt_warehouse.location.pk
            p_receiving_wh      = waybill.receipt_warehouse.pk #
            for loading in waybill.loading_details.all():
                #fix when fields available
                p_good_net          =loading.total_weight_net_received and u'%.3f' % (loading.total_weight_net_received) or None
                p_good_gross        =loading.total_weight_gross_received and u'%.3f' % (loading.total_weight_gross_received) or None
                p_damage_net        =loading.number_units_damaged and u'%.3f' % (loading.number_units_damaged * loading.unit_weight_net/1000) or None
                p_damage_gross      =loading.number_units_damaged and u'%.3f' % (loading.number_units_damaged * loading.unit_weight_gross/1000) or None
                p_loss_net          =loading.number_units_lost and u'%.3f' % (loading.number_units_lost * loading.unit_weight_net/1000) or None
                p_loss_gross        =loading.number_units_lost and u'%.3f' % (loading.number_units_lost * loading.unit_weight_gross/1000) or None 

                if special_case:
                    CURR_CODE = u"%s%s" % (code_letter, waybill.pk)
                    code_letter = u'B'
                
                if get_version(using=compas) < '1.4':
                    call_db_procedure('write_waybill.receipt', (
                        CURR_CODE, 
                        waybill.receipt_person.compas.pk, 
                        waybill.receipt_person.code, 
                        waybill.arrival_date.strftime("%Y%m%d"),
    
                        loading.number_units_good and u'%.3f' % loading.number_units_good or None,
                        loading.units_damaged_reason and loading.units_damaged_reason.cause or None, 
                        loading.number_units_damaged and u'%.3f' % loading.number_units_damaged or None, 
                        loading.units_lost_reason and loading.units_lost_reason.cause or None, 
                        loading.number_units_lost and u'%.3f' % loading.number_units_lost or None, 
    
                        loading.stock_item.origin_id, 
                        loading.stock_item.commodity.category.pk,
                        loading.stock_item.commodity.pk, 
                        loading.stock_item.package.pk, 
                        loading.stock_item.allocation_code, 
                        loading.stock_item.quality
                    ), compas)
                    
                else:
                    #version 1.4 should have additional fields
                    call_db_procedure('write_waybill.receipt', (
                        CURR_CODE, 
                        waybill.receipt_person.compas.pk, 
                        waybill.receipt_person.code, 
                        waybill.arrival_date.strftime("%Y%m%d"),
    
                        loading.number_units_good and u'%.3f' % loading.number_units_good or None,
                        loading.units_damaged_reason and loading.units_damaged_reason.cause or None, 
                        loading.number_units_damaged and u'%.3f' % loading.number_units_damaged or None, 
                        loading.units_lost_reason and loading.units_lost_reason.cause or None, 
                        loading.number_units_lost and u'%.3f' % loading.number_units_lost or None, 
                        
                        ## add fields
    
                        loading.stock_item.origin_id, 
                        loading.stock_item.commodity.category.pk,
                        loading.stock_item.commodity.pk, 
                        loading.stock_item.package.pk, 
                        loading.stock_item.allocation_code, 
                        loading.stock_item.quality,

                        ## New Fields
                        p_good_net,#(number_units_good * unit_weight_net) in varchar2 default null,   --> new parameter for received net qty
                        p_good_gross,# (number_units_good * unit_weight_gross) in varchar2 default null, --> new parameter for received gross qty
                        p_damage_net,# (number_units_damaged * unit_weight_net )in varchar2 default null,   --> new parameter for received damage net qty
                        p_damage_gross,# in varchar2 default null, --> new parameter for received damage gross qty
                        p_loss_net,# in varchar2 default null,   --> new parameter for received loss net qty
                        p_loss_gross,# in varchar2 default null, --> new parameter for received loss gross qty
                        p_receiving_location,# in varchar2 default null, -->        new parameter for diverted delivery
                        p_receiving_wh,# in varchar2 default null --> new parameter for diverted delivery     
                        
                    ), compas)
                



    except Exception, err:
        message = hasattr(err, 'messages') and u"\n".join(err.messages) or unicode(err)
        ets_models.CompasLogger.objects.create(action=ets_models.CompasLogger.RECEIPT, 
                                               compas_id=compas, waybill=waybill,
                                               status=ets_models.CompasLogger.FAILURE, 
                                               message=message)

        waybill.receipt_validated = False
Example #3
0
File: utils.py Project: rdidaci/ETS
def send_received(waybill, compas=None, cache_prefix='send_received'):
    """Submits received and validated waybills to COMPAS"""
    cache_key = "%s_%s" % (cache_prefix, waybill.pk)
    sending = cache.get(cache_key, False)
    if sending:
        return
    
    cache.set(cache_key, True)
    
    if not compas:
        compas = waybill.destination.compas.pk

    try:
        with transaction.commit_on_success(using=compas) as tr:
            
            #Check COMPAS version. We support only old versions lower than 1.3
            if get_version(using=compas) > '1.3':
                raise ValidationError(_("Inappropriate version of COMPAS. Please, contact your administrator."))
            
            CURR_CODE = waybill.pk[len(compas):]

            ## Check if dispatch_master is there...
            if not compas_models.DispatchMaster.objects.using(compas) \
                                .filter(code__contains=waybill.pk, destination_code=waybill.destination.pk) \
                                .exists():  
                #Push dispatched waybill to COMPAS station 
                if not send_dispatched(waybill, compas) and not send_dispatched(waybill, waybill.destination.compas):
                    raise ValidationError(_("The Dispatch %s is not available in the COMPAS Station %s") % ( waybill.pk, compas))
                
                
            ## check if containers = 2 & lines = 2
            special_case = waybill.loading_details.count() == 2 and waybill.container_two_number
            code_letter = u'A'

            for loading in waybill.loading_details.all():

                if special_case:
                    CURR_CODE = u"%s%s" % (code_letter, waybill.pk)
                    code_letter = u'B'

                call_db_procedure('write_waybill.receipt', (
                    CURR_CODE, 
                    waybill.receipt_person.compas.pk, 
                    waybill.receipt_person.code, 
                    waybill.arrival_date.strftime("%Y%m%d"),

                    loading.number_units_good and u'%.3f' % loading.number_units_good or None,
                    loading.units_damaged_reason and loading.units_damaged_reason.cause or None, 
                    loading.number_units_damaged and u'%.3f' % loading.number_units_damaged or None, 
                    loading.units_lost_reason and loading.units_lost_reason.cause or None, 
                    loading.number_units_lost and u'%.3f' % loading.number_units_lost or None, 

                    loading.stock_item.origin_id, 
                    loading.stock_item.commodity.category.pk,
                    loading.stock_item.commodity.pk, 
                    loading.stock_item.package.pk, 
                    loading.stock_item.allocation_code, 
                    loading.stock_item.quality
                ), compas)

    except Exception, err:
        message = hasattr(err, 'messages') and u"\n".join(err.messages) or unicode(err)
        ets_models.CompasLogger.objects.create(action=ets_models.CompasLogger.RECEIPT, 
                                               compas_id=compas, waybill=waybill,
                                               status=ets_models.CompasLogger.FAILURE, 
                                               message=message)

        waybill.receipt_validated = False