Ejemplo n.º 1
0
def getBatchAndSerialNos(itemCode, warehouse):
    outerJson = []
    records = frappe.db.sql(
        """select sle.batch_no, sle.serial_no, sle.qty_after_transaction from `tabStock Ledger Entry` sle where sle.item_code = %(str1)s and sle.warehouse = %(str2)s """,
        {
            'str1': itemCode,
            'str2': warehouse
        })
    if records:
        for record in records:
            innerJson = {}
            if record[0]:
                innerJson['batch_no'] = record[0]
                innerJson['batch_qty_at_warehouse'] = get_batch_qty(
                    record[0], warehouse, itemCode)
            else:
                innerJson['batch_no'] = None
                innerJson['batch_qty_at_warehouse'] = None
            if record[1]:
                innerJson['serial_no'] = record[1]
            else:
                innerJson['serial_no'] = None
            outerJson.append(innerJson)
        return outerJson

    else:
        return None
Ejemplo n.º 2
0
def get_actual_qty(item_code, warehouse, batch=None):
    has_batch_no = frappe.db.get_value("Item", item_code, "has_batch_no")
    if has_batch_no and batch:
        batch_qty = get_batch_qty(batch, warehouse, item_code) or {}
        return batch_qty.get("actual_batch_qty", 0)
    return (frappe.db.get_value("Bin", {
        "item_code": item_code,
        "warehouse": warehouse
    }, "actual_qty") or 0)
Ejemplo n.º 3
0
def validate(doc, method):

    print "+VALIDAR SALES INVOICE+"
    print "+VALIDAR SALES INVOICE+"
    print "+VALIDAR SALES INVOICE+"
    print "+VALIDAR SALES INVOICE+"
    print "+VALIDAR SALES INVOICE+"

    taxavenda = cambios("BNA")
    lista_retencoes = get_lista_retencoes()
    lista_retencao = get_taxa_retencao()
    lista_impostos = get_taxa_ipc()

    lista_iva = get_taxa_iva()

    temretencao = False
    temimpostoconsumo = False
    retencaofonte = 0
    retencaopercentagem = 0
    totalpararetencao = 0
    totalgeralimpostoconsumo = 0
    totalgeralretencaofonte = 0
    totalbaseretencaofonte = 0
    retencaofonteDESC = ""
    totalservicos_retencaofonte = 0
    totaldespesas_noretencaofonte = 0
    totaldescontos_linha = 0

    impostoselotransit = []
    totalimpostoselotrans = 0
    impostoselotranspercentagem = 0
    metadedovalor = False

    percentagem = 0

    percentagemiva = 0
    totalgeraliva = 0

    ii = 0

    numISelo = 0  #contador Imposto de Selo

    for x in lista_retencoes:
        if x.descricao.upper() == 'Retencao na Fonte'.upper():
            print('pertagem ', x.percentagem)
            retencaopercentagem = x.percentagem
        elif (x.descricao.upper()
              == 'IPC'.upper()) or (x.descricao.upper()
                                    == 'Imposto de Consumo'.upper()):
            print('IPC % ', x.percentagem)
            percentagem = x.percentagem
        elif ('Imposto de Selo'.upper() in x.descricao.upper()):

            print('Imposto de Selo % ', x.percentagem)
            print(x.descricao)
            print('metade '), x.metade_do_valor
            impostoselotransit.append(
                [x.descricao, x.percentagem, x.metade_do_valor])

            #impostoselotranspercentagem = x.percentagem
            #if (x.metade_do_valor):
            #	metadedovalor = True

        elif (x.descricao.upper() == 'IVA'.upper()) or (
                "Imposto Valor Acrescentado".upper() == x.descricao.upper()
                or 'Acrescentado'.upper() in x.descricao.upper()):
            print('IVA % ', x.percentagem)
            percentagemiva = x.percentagem

    for i in doc.get("items"):
        if i.item_code != None:
            prod = frappe.db.sql(
                """SELECT item_code,imposto_de_consumo,retencao_na_fonte,imposto_de_selo,que_imposto_de_selo FROM `tabItem` WHERE item_code = %s """,
                i.item_code,
                as_dict=True)
            if prod[0].imposto_de_consumo == 1:
                print("IMPOSTO CONSUMO")
                if percentagem == 0:
                    i.imposto_de_consumo = (i.amount * 5) / 100
                else:
                    i.imposto_de_consumo = (i.amount * percentagem) / 100

            if prod[0].retencao_na_fonte == 1:
                print("RETENCAO FONTE")
                i.retencao_na_fonte = (i.amount * retencaopercentagem) / 100
                totalbaseretencaofonte += i.amount
                totalservicos_retencaofonte += totalbaseretencaofonte
            else:
                totaldespesas_noretencaofonte += i.amount

            if prod[0].imposto_de_selo == 1:
                print("IMPOSTO DE SELO TRANS")
                print("IMPOSTO DE SELO TRANS")
                for x1 in impostoselotransit:
                    print 'loop no imposto selo'
                    print x1
                    print x1[0]
                    print x1[1]
                    print x1[2]
                    if x1[0] == prod[0].que_imposto_de_selo:
                        print 'Imposto CORRETO!!!!!'
                        print 'Imposto CORRETO!!!!!'
                        print 'Imposto CORRETO!!!!!'

                        if x1[2] == 1:  #metade do valor TRUE
                            print 'METADE DO VALOR!!!'
                            metadedovalor = True
                        else:
                            metadedovalor = False

                        impostoselotranspercentagem = x1[1]

                        print(flt(i.amount) * x1[1])
                        print('Selo % ',
                              ((i.amount * impostoselotranspercentagem) / 100))
                        break
                print 'continua....'
                print metadedovalor
                #i.retencao_na_fonte = (i.amount * retencaopercentagem) / 100
                if (metadedovalor):
                    totalimpostoselotrans += (
                        (i.amount / 2) * impostoselotranspercentagem) / 100
                    i.imposto_de_selo_trans = (
                        (i.amount / 2) * impostoselotranspercentagem) / 100
                else:
                    totalimpostoselotrans += (
                        i.amount * impostoselotranspercentagem) / 100
                    i.imposto_de_selo_trans = (
                        i.amount * impostoselotranspercentagem) / 100
                print totalimpostoselotrans

            if prod[0].iva_isencao == 0:  #TEM IVA
                print "IVA IVA"
                print "IVA IVA"

            totalgeralimpostoconsumo += i.imposto_de_consumo
            totalgeralretencaofonte += i.retencao_na_fonte

            #BATCH Qty
            if i.warehouse and i.item_code and i.batch_no:
                print 'BATCH NO verifica a QTD'
                print i.batch_no
                print i.warehouse
                print i.item_code
                print('qtd atual ', i.actual_qty)
                print get_batch_qty(i.batch_no, i.warehouse,
                                    i.item_code)['actual_batch_qty']
                #print get_batch_qty(i.warehouse,i.item_code)['actual_batch_qty']
                #for xx in get_batch_qty(i.batch_no,i.warehouse,i.item_code)['actual_batch_qty']:
                #	print "LISTA BATCHES....."
                #	print xx

                i.actual_batch_qty = get_batch_qty(
                    i.batch_no, i.warehouse, i.item_code)['actual_batch_qty']
                if i.actual_qty == 0:
                    print 'ACTUAL QTY ZEROOOOOOOO'
                    #i.actual_qty = get_batch_qty(i.batch_no,i.warehouse,i.item_code)['actual_batch_qty']

            #Total Desconto Linha
            if i.margin_type == "Percentage":
                totaldescontos_linha += i.amount

    #Save retencao na INVoice
    doc.total_retencao_na_fonte = totalgeralretencaofonte
    doc.base_retencao_fonte = totalbaseretencaofonte

    #Save Descontos linha
    doc.total_desconto_linha = totaldescontos_linha

    #Save Imposto de Selo Trans
    doc.total_imposto_selo_trans = totalimpostoselotrans

    #Calcula_despesas Ticked

    iii = 0
    print("Despesas")
    for ai in doc.get("taxes"):
        if ai.parent == doc.name and ai.charge_type != "":
            if ai.calcula_despesas:
                totalgeralimpostoconsumo = 0
                if totaldespesas_noretencaofonte == 0:
                    #recalcula
                    print("RECALCULA")
                    print("RECALCULA")
                    print("RECALCULA")
                    if (ai.rate == 0) and (percentagem == 0):
                        percentagem = 5
                    else:
                        percentagem = ai.rate

                    for aii in doc.get("items"):
                        if aii.parent == doc.name:
                            prod = frappe.db.sql(
                                """SELECT item_code,imposto_de_consumo,retencao_na_fonte FROM `tabItem` WHERE item_code = %s """,
                                aii.item_code,
                                as_dict=True)

                            #if (iii==0){iii=0}

                            if prod[0].imposto_de_consumo == 1:

                                if aii.imposto_de_consumo == 0:
                                    print ""

                                if aii.retencao_na_fonte == 1:

                                    totalgeralretencaofonte += (
                                        aii.amount * retencaopercentagem) / 100
                                    totalbaseretencaofonte += aii.amount
                                    totalservicos_retencaofonte += totalbaseretencaofonte

                                totalgeralimpostoconsumo += aii.imposto_de_consumo

                                despesas = (
                                    percentagem *
                                    totaldespesas_noretencaofonte) / 100
                                print percentagem
                                print totaldespesas_noretencaofonte
                                print despesas
                                print totalgeralimpostoconsumo
                                print ai.account_head

                                ai.charge_type = "Actual"
                                #ai.tax_amount=despesas
                                ai.tax_amount = despesas  #totalgeralimpostoconsumo
                            else:
                                ai.tax_amount = 0

                else:
                    print("CALCULA DESPESAS")
                    print("CALCULA DESPESAS")
                    print("CALCULA DESPESAS")
                    if (ai.rate == 0) and (percentagem == 0):
                        percentagem = 5
                    else:
                        percentagem = ai.rate

                    despesas = (ai.rate * totaldespesas_noretencaofonte) / 100

                    print percentagem
                    print totaldespesas_noretencaofonte
                    print despesas

                    print totalgeralimpostoconsumo
                    if despesas != totalgeralimpostoconsumo:

                        ai.charge_type = "Actual"
                        ai.tax_amount = totalgeralimpostoconsumo

                    else:
                        ai.charge_type = "Actual"
                        ai.tax_amount = despesas
            elif "34220000" in ai.account_head:  #IVA
                print "TEM IVA......"
                print "TEM IVA......"
                print "TEM IVA......"
                print "TEM IVA......"

                for aii in doc.get("items"):
                    if aii.parent == doc.name:
                        prod = frappe.db.sql(
                            """SELECT item_code,imposto_de_consumo,retencao_na_fonte,iva_isencao FROM `tabItem` WHERE item_code = %s """,
                            aii.item_code,
                            as_dict=True)

                        #if (iii==0){iii=0}
                        '''
						if prod[0].iva_isencao == 0:	#Has IVA

							if aii.imposto_de_consumo == 0:
								print ""
							'''
                        #	if aii.retencao_na_fonte == 1:

                        #		totalgeralretencaofonte +=  (aii.amount * retencaopercentagem) / 100
                        #		totalbaseretencaofonte += aii.amount
                        #		totalservicos_retencaofonte += totalbaseretencaofonte
                        '''
						totalgeralimpostoconsumo += aii.imposto_de_consumo					


						despesas = (percentagem * totaldespesas_noretencaofonte)/100
						print percentagem
						print totaldespesas_noretencaofonte
						print despesas
						print totalgeralimpostoconsumo
						print ai.account_head

						ai.charge_type="Actual"
						#ai.tax_amount=despesas
						ai.tax_amount = despesas #totalgeralimpostoconsumo 
						'''
                        if prod[0].iva_isencao == 1:  #NO NO IVA
                            print "sem iva ", prod[0].item_code
                            ai.charge_type = "Actual"
                            ai.tax_amount = totalgeraliva
                            ai.total = totalgeraliva + doc.net_total

                        elif prod[0].iva_isencao == 0:  #IVA
                            print "IVA"
                            print "IVA ", aii.item_code
                            print aii.amount
                            print percentagemiva
                            totalgeraliva += (aii.amount *
                                              percentagemiva) / 100
                            if ai.charge_type == "Actual":
                                ai.tax_amount = totalgeraliva
                                ai.total = totalgeraliva + doc.net_total

                        else:
                            ai.tax_amount = 0

            else:
                print "SEM DESPESAS MAS CALCULA IPC"
                print "SEM DESPESAS MAS CALCULA IPC"
                ai.charge_type = "Actual"
                ai.tax_amount = totalgeralimpostoconsumo
                ai.total = totalgeralimpostoconsumo + doc.net_total

    print "VALOR POR EXTENSO"

    print totalgeralimpostoconsumo

    #Save Total Taxes and Charges if IPC exists
    if totalgeralimpostoconsumo:
        if doc.currency == "KZ":
            doc.base_total_taxes_and_charges = totalgeralimpostoconsumo
            doc.total_taxes_and_charges = totalgeralimpostoconsumo
        if not doc.additional_discount_percentage:
            doc.grand_total = totalgeralimpostoconsumo + doc.net_total
            doc.rounded_total = doc.grand_total
            doc.base_grand_total = doc.grand_total
            doc.base_rounded_total = doc.grand_total
            doc.outstanding_amount = doc.grand_total
    elif totalgeraliva:
        #IVA
        print "iva nos TOTAIS"
        if doc.currency == "KZ":
            doc.base_total_taxes_and_charges = totalgeraliva
            doc.total_taxes_and_charges = totalgeraliva
            print 'totalgeraliva ', totalgeraliva
        if not doc.additional_discount_percentage:
            doc.grand_total = totalgeraliva + doc.net_total
            print 'totalgeraliva + net total ', totalgeraliva + doc.net_total
            doc.rounded_total = doc.grand_total
            print 'Grand total ', doc.grand_total
            doc.base_grand_total = doc.grand_total
            doc.base_rounded_total = doc.grand_total
            doc.outstanding_amount = doc.grand_total

        print "Rouding ....."
        print "Rouding ....."
        print doc.base_rounding_adjustment
        #print 'tem centimos ',str(doc.base_rounding_adjustment).find('.')
        #if str(doc.base_rounding_adjustment).find('.'):
        #	doc.base_rounding_adjustment = round(doc.base_rounding_adjustment)

    company_currency = erpnext.get_company_currency(doc.company)
    print company_currency
    if (company_currency == 'KZ'):
        doc.in_words = num2words(doc.rounded_total,
                                 lang='pt_BR').title() + ' Kwanzas.'
    else:
        doc.in_words = money_in_words(doc.rounded_total, company_currency)

    ultimodoc = frappe.db.sql(
        """ select max(name),creation,docstatus,hash_erp,hashcontrol_erp from `tabSales Invoice` where (docstatus = 1 or docstatus = 2)  and hash_erp <> '' """,
        as_dict=True)
    print 'VALIDARrrrrrrrrrrrrrrrrrr'
    print ultimodoc
    global ultimoreghash
    ultimoreghash = ultimodoc
Ejemplo n.º 4
0
def validate(doc, method):

    taxavenda = cambios("BNA")
    lista_retencoes = get_lista_retencoes()
    lista_retencao = get_taxa_retencao()
    lista_impostos = get_taxa_ipc()

    temretencao = False
    temimpostoconsumo = False
    retencaofonte = 0
    retencaopercentagem = 0
    totalpararetencao = 0
    totalgeralimpostoconsumo = 0
    totalgeralretencaofonte = 0
    totalbaseretencaofonte = 0
    retencaofonteDESC = ""
    totalservicos_retencaofonte = 0
    totaldespesas_noretencaofonte = 0

    ii = 0

    for x in lista_retencoes:
        if x.descricao == 'Retencao na Fonte':
            print('pertagem ', x.percentagem)
            retencaopercentagem = x.percentagem

    for i in doc.get("items"):

        prod = frappe.db.sql(
            """SELECT item_code,imposto_de_consumo,retencao_na_fonte FROM `tabItem` WHERE item_code = %s """,
            i.item_code,
            as_dict=True)
        if prod[0].imposto_de_consumo == 1:
            print("IMPOSTO CONSUMO")
            if i.imposto_de_consumo == 0:
                i.imposto_de_consumo = (i.amount * 10) / 100

        if prod[0].retencao_na_fonte == 1:
            print("RETENCAO FONTE")
            i.retencao_na_fonte = (i.amount * retencaopercentagem) / 100
            totalbaseretencaofonte += i.amount
            totalservicos_retencaofonte += totalbaseretencaofonte
        else:
            totaldespesas_noretencaofonte += i.amount

        totalgeralimpostoconsumo += i.imposto_de_consumo
        totalgeralretencaofonte += i.retencao_na_fonte

        #BATCH Qty
        if i.warehouse and i.item_code and i.batch_no:
            print 'BATCH NO verifica a QTD'
            print get_batch_qty(i.batch_no, i.warehouse,
                                i.item_code)['actual_batch_qty']
            i.actual_batch_qty = get_batch_qty(i.batch_no, i.warehouse,
                                               i.item_code)['actual_batch_qty']

    #Save retencao na INVoice
    doc.total_retencao_na_fonte = totalgeralretencaofonte
    doc.base_retencao_fonte = totalbaseretencaofonte

    #Calcula_despesas Ticked
    iii = 0
    print("Despesas")
    for ai in doc.get("taxes"):
        if ai.parent == doc.name and ai.charge_type != "":
            if ai.calcula_despesas:
                if totaldespesas_noretencaofonte == 0:
                    #recalcula
                    print("RECALCULA")
                    percentagem = ai.rate
                    for aii in doc.get("items"):
                        if aii.parent == doc.name:
                            prod = frappe.db.sql(
                                """SELECT item_code,imposto_de_consumo,retencao_na_fonte FROM `tabItem` WHERE item_code = %s """,
                                aii.item_code,
                                as_dict=True)

                            #if (iii==0){iii=0}

                            if prod[0].imposto_de_consumo == 1:

                                if aii.imposto_de_consumo == 0:
                                    print ""

                                if aii.retencao_na_fonte == 1:

                                    totalgeralretencaofonte += (
                                        aii.amount * retencaopercentagem) / 100
                                    totalbaseretencaofonte += aii.amount
                                    totalservicos_retencaofonte += totalbaseretencaofonte

                                totalgeralimpostoconsumo += aii.imposto_de_consumo

                                despesas = (
                                    percentagem *
                                    totaldespesas_noretencaofonte) / 100
                                ai.charge_type = "Actual"
                                ai.tax_amount = despesas

                else:
                    print("CALCULA DESPESAS")
                    despesas = (ai.rate * totaldespesas_noretencaofonte) / 100

                    ai.charge_type = "Actual"
                    ai.tax_amount = despesas

    print "VALOR POR EXTENSO"

    company_currency = erpnext.get_company_currency(doc.company)
    print company_currency
    if (company_currency == 'KZ'):
        doc.in_words = num2words(doc.rounded_total,
                                 lang='pt_BR').title() + ' Kwanzas.'
    else:
        doc.in_words = money_in_words(doc.rounded_total, company_currency)