Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using APQuickCheck = PX.Objects.AP.Standalone.APQuickCheck;
using CRLocation = PX.Objects.CR.Standalone.Location;
using IRegister = PX.Objects.CM.IRegister;
using PX.Common;
using PX.Data.EP;
using PX.Data;
using PX.Objects.AP.BQL;
using PX.Objects.AP;
using PX.Objects.CM;
using PX.Objects.Common;
using PX.Objects.CR;
using PX.Objects.CS;
using PX.Objects.GL;
using PX.Objects;
using PX.TM;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;
namespace PX.Objects.AP
{
public class APRegisterMyExt : PXCacheExtension<PX.Objects.AP.APRegister>
{
[ToWordsRU(typeof(APPayment.curyOrigDocAmt))]
public virtual string AmountToWords {get; set;}
}
public class ToWordsRUAttribute : PXEventSubscriberAttribute, IPXFieldSelectingSubscriber
{
protected string _DecimalField = null;
protected short? _Precision = null;
public ToWordsRUAttribute(Type DecimalField)
{
_DecimalField = DecimalField.Name;
}
public ToWordsRUAttribute(short Precision)
{
_Precision = Precision; ;
}
public virtual void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
{
e.ReturnState = PXStringState.CreateInstance(e.ReturnState, 255, null, _FieldName, null, null, null, null, null, false, null);
object DecimalVal;
if (!string.IsNullOrEmpty(_DecimalField))
{
DecimalVal = sender.GetValue(e.Row, _DecimalField);
sender.RaiseFieldSelecting(_DecimalField, e.Row, ref DecimalVal, true);
}
else
{
DecimalVal = PXDecimalState.CreateInstance(e.ReturnValue, (short)_Precision, _FieldName, false, 0, Decimal.MinValue, Decimal.MaxValue);
}
if (DecimalVal is PXDecimalState)
{
if (((PXDecimalState)DecimalVal).Value == null)
{
e.ReturnValue = string.Empty;
return;
}
e.ReturnValue = LangRU.ToWords((decimal)((PXDecimalState)DecimalVal).Value, ((PXDecimalState)DecimalVal).Precision);
}
}
}
public static class LangRU
{
public static string ToWords(Decimal amt, int Precision)
{
StringBuilder sb = new StringBuilder(ToWords(amt));
Decimal Cents = Math.Floor((amt - Math.Truncate(amt)) * (decimal)Math.Pow(10, Precision));
if (amt != 0m)
{
if (Cents != 0m)
{
sb.Append(" и ");
sb.Append((int)Cents);
sb.Append("/");
sb.Append((int)Math.Pow(10, Precision));
}
else
{
sb.Append(" Только");
}
}
else
{
sb.Append("Ноль");
}
return sb.ToString();
}
public static string ToWords(Decimal? amt)
{
Decimal baseamt = Math.Floor((Decimal)amt);
string[] less10 = new string[] { "Один", "Два", "Три", "Четыре", "Пять", "Шесть", "Семь", "Восемь", "Девять" };
string[] less20 = new string[] { "Одиннадцать", "Двенадцать", "Тринадцать", "Четырнадцать", "Пятнадцать", "Шестнадцать", "Семнадцать", "Восемнадцать", "Девятнадцать", };
string[] less100 = new string[] { "Десять", "Двадцать", "Тридцать", "Сорок", "Пятьдесят", "Шестьдесят", "Семьдесят", "Восемьдесят", "Девяносто", };
string[] great100 = new string[] { "", "Тысяча", "Миллион", "Миллиард", "Триллион", "Квадриллион", };
string is100 = "Сто";
string space = " ";
int count = (int)Math.Floor(Math.Log10((double)baseamt));
int tricount = (int)Math.Floor((double)count / 3) + 1;
StringBuilder sb = new StringBuilder();
for (int i = tricount; i > 0; i--)
{
int prevlen = sb.Length;
int triamt = (int)Math.Floor((double)baseamt / Math.Pow(10, (i - 1) * 3));
{
if (triamt >= 100)
{
int h = (int)Math.Floor((double)triamt / 100);
sb.Append(less10[h - 1]);
sb.Append(space);
sb.Append(is100);
sb.Append(space);
triamt = triamt - 100 * h;
//Six Hundred
}
if (triamt >= 20 || triamt == 10)
{
int h = (int)Math.Floor((double)triamt / 10);
sb.Append(less100[h - 1]);
sb.Append(space);
triamt = triamt - 10 * h;
//Six Hundred Twenty
}
if (triamt < 20 && triamt > 10)
{
sb.Append(less20[triamt - 10 - 1]);
sb.Append(space);
//Six Hundred Eleven
}
if (triamt > 0 && triamt < 10)
{
sb.Append(less10[triamt - 1]);
sb.Append(space);
//Six Hundred Twenty One
}
}
if (sb.Length > prevlen)
{
sb.Append(great100[i - 1]);
sb.Append(space);
}
long newbase;
Math.DivRem((long)baseamt, (long)Math.Pow(10, (i - 1) * 3), out newbase);
baseamt = (Decimal)newbase;
}
return sb.ToString().TrimEnd();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.