Esempio n. 1
0
File: utils.py Progetto: 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
Esempio n. 2
0
File: utils.py Progetto: 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