Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
453 lines (328 sloc) 12.7 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FiscalHelper;
namespace AtolFiscalPrinter
{
enum DriverMode { Select, Command, zReport = 3 }
struct Result
{
public int code; public string description;
public override string ToString()
{
return "id: " + code.ToString() + " message: " + description;
}
}
enum AttributeId { clientAddress = 1008, cassirName = 1021 }
public class Printer : IFiscalPrinter
{
dynamic _driver = null;
string objectName = "AddIn.FprnM45";
string accessPassword = "0";
string defaultPassword = "30";
string cassir1Password = "1";
string password = "30";
void SetMode(DriverMode mode)
{
_driver.Mode = (int)mode;
_driver.SetMode();
}
void Password()
{
_driver.AccessPassword = accessPassword;
_driver.DefaultPassword = defaultPassword;
}
Result GetResult()
{
Result r;
r.code = _driver.ResultCode;
r.description = _driver.ResultDescription;
return r;
}
public void GetRegister(int num)
{
_driver.DeviceEnabled = true;
_driver.RegisterNumber = num;
_driver.GetRegister();
var r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,r.ToString());
}
public int OFD_NotSendedDocuments()
{
try
{
GetRegister(44);
int i = _driver.Count;
var r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,r.ToString());
return i;
}
finally
{
_driver.DeviceEnabled = false;
}
}
public DateTime OFD_FirstNotSended()
{
try
{
GetRegister(45);
DateTime dt;
try
{
dt = new DateTime(_driver.Year, _driver.Month, _driver.Day, _driver.Hour, _driver.Minute, 0);
}
catch
{
dt = new DateTime(_driver.Year, 1, 1);
}
return dt;
}
finally
{
_driver.DeviceEnabled = false;
}
}
public DateTime OFD_LastSended()
{
try
{
GetRegister(51);
DateTime dt;
try
{
dt = new DateTime(_driver.Year, _driver.Month, _driver.Day, _driver.Hour, _driver.Minute, 0);
}
catch
{
dt = new DateTime(_driver.Year, 1, 1);
}
return dt;
}
finally
{
_driver.DeviceEnabled = false;
}
}
public bool isSessionOpened(out DateTime d)
{
bool res = false;
try
{
GetRegister(18);
var r = GetResult();
d = new DateTime(_driver.Year, _driver.Month, _driver.Day, _driver.Hour, _driver.Minute, 0);
res = _driver.SessionOpened;
}
finally
{
_driver.DeviceEnabled = false;
}
return res;
}
public void CloseCheck()
{
try
{
_driver.DeviceEnabled = true;
_driver.CloseCheck();
}
finally
{
_driver.DeviceEnabled = false;
}
}
public void CloseCheckWithDelivery()
{
try
{
_driver.DeviceEnabled = true;
_driver.Delivery();
}
finally
{
_driver.DeviceEnabled = false;
}
}
public FiscalError PrintCheck(Check check)
{
try
{
if (check == null) return FiscalError.nullRef;
//Password();
_driver.Password = password;
_driver.DeviceEnabled = true;
var r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка подключения "+r.ToString());
_driver.ResetMode();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при выборе режима "+r.ToString());
_driver.OperatorPassword = cassir1Password;
SetMode(DriverMode.Command);
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при установке режима "+r.ToString());
_driver.NewDocument();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при создании документа "+r.ToString());
_driver.CheckType = (int)check.checkType;
_driver.CheckMode = (int)check.checkMode;
_driver.OpenCheck();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при открытии чека "+r.ToString());
if ((check.checkMode == CheckMode.Electronic) && (check.address != ""))
{
_driver.AttrNumber = AttributeId.clientAddress;
_driver.AttrValue = check.address;
_driver.WriteAttribute();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при записи аттрибута "+r.ToString());
}
if (check.cassir != "")
{
_driver.AttrNumber = AttributeId.cassirName;
_driver.AttrValue = check.cassir;
_driver.WriteAttribute();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при записи фамилии кассира "+r.ToString());
}
for (var i = 0; i < check.items.Count; i++)
{
_driver.Name = check.items[i].name;
_driver.Quantity = check.items[i].quantity;
_driver.Price = check.items[i].price;
_driver.TaxTypeNumber = 4;//НДС не облагается
if ((check.discount >= 0) && (check.discount <= 100))
{
_driver.DiscountType = 1;
_driver.DiscountValue = check.discount;
}
_driver.Registration();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при регистрации продажи "+r.ToString());
}
//Скидка округления
var d = 0m;
if(check.totalR>check.paySumm_to_Client)
{
d = check.totalR - check.paySumm_to_Client;
if ((d > 0) && (d < 1))
{
_driver.Summ = d;
_driver.Destination = 0;
_driver.SummDiscount();
if (r.code != 0) throw new FiscalException(r.code, "Ошибка при начислении скидки округления " + r.ToString());
}
else
throw new Exception("Ошибка подсчет скидки округления!");
}
_driver.TypeClose = (int)check.payType;
if (check.payType == PayType.Cash)
{
//оплата со сдачей
if(check.paySumma>check.totalR)
{
_driver.Summ = check.paySumma;
_driver.Delivery();
if (r.code != 0) throw new FiscalException(r.code, "Ошибка при закрытии со сдачей " + r.ToString());
return FiscalError.none;
}
if (check.paySumma > 0)
{
_driver.Summ = check.paySumm_to_Client;
_driver.Payment();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при регистрации платежа 1 " + r.ToString());
}
}
else
{
if (check.paySumma > 0)
{
//MessageBox.Show("Пытаемся "+(check.paySumma - check.cashInclude ).ToString());
_driver.Summ = check.paySumm_to_Client - check.cashInclude;
_driver.Payment();
r = GetResult();
int k = 1;
//переплата
while( (r.code==-3896) && (k<6) )
{
// MessageBox.Show((check.paySumma - check.cashInclude - k).ToString());
_driver.Summ = check.paySumm_to_Client - check.cashInclude-k*0.01m;
_driver.Payment();
r = GetResult();
k++;
}
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при регистрации платежа 2 " + r.ToString());
if (check.cashInclude > 0)
{
_driver.TypeClose = (int)PayType.Cash;
_driver.Summ =check.cashInclude;
_driver.Payment();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при регистрации платежа 3 " + r.ToString());
}
}
}
_driver.CloseCheck();
r = GetResult();
if (r.code != 0) throw new FiscalException(r.code,"Ошибка при закрытии чека "+r.ToString());
return FiscalError.none;
}
finally
{
_driver.DeviceEnabled = false;
}
}
public void ShowSettings()
{
if (_driver != null)
_driver.ShowProperties();
}
public Printer()
{
Type f = Type.GetTypeFromProgID(objectName, true);
_driver = Activator.CreateInstance(f);
Password();
}
public void PrintString(string s)
{
_driver.DeviceEnabled = true;
_driver.Caption = s;
_driver.PrintString();
}
public void OpenSession()
{
Password();
_driver.DeviceEnabled = true;
// if (_driver.SessionOpened)
{
_driver.Password = password;
SetMode(DriverMode.zReport);
if (GetResult().code != 0)
throw new FiscalException(GetResult().code,"Ошибка при снятии z-report "+GetResult().ToString());
_driver.ReportType = 1;
_driver.Report();
}
_driver.Password = "1";
_driver.DeviceEnabled = true;
SetMode(DriverMode.Command);
if (GetResult().code != 0)
throw new FiscalException(GetResult().code,"Ошибка при установке режима " + GetResult().ToString());
_driver.OpenSession();
if (GetResult().code != 0)
throw new FiscalException(GetResult().code,"Ошибка при открытии смены "+GetResult().ToString());
}
}
public class Factory : IFiscalPrinterFactory
{
public IFiscalPrinter create(FiscalType ft)
{
if (ft == FiscalType.atol)
return (new Printer());
else
return null;
}
}
}
You can’t perform that action at this time.