Files
U8-Import/NX.ERP.U8.App/Voucher/VoucherDAL.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2020-12-19 15:30:38 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
2021-01-05 11:11:28 +08:00
2020-12-19 15:30:38 +08:00
using Dapper;
using System.Data.SqlClient;
namespace U8AppMain.Voucher
{
public class IdEnity
{
public string cAcc_Id { get; set; }
public string cVouchType { get; set; }
public int iAmount { get; set; }
//public int iFatherId { get; set; }
//public int iChildId { get; set; }
public bool bEnableNewRule { get; set; } = true;
}
public class VoucherDAL
{
private string _connStr;
public VoucherDAL(string connStr)
{
_connStr = connStr;
}
2020-12-19 16:33:33 +08:00
public bool Add(Vouch vouch)
2020-12-19 15:30:38 +08:00
{
2021-01-19 11:10:39 +08:00
string accid = NX.ERP.U8.U8LoginContext.Instance.AccID;
2021-01-18 15:07:20 +08:00
2020-12-19 15:30:38 +08:00
var conn = new SqlConnection(this._connStr);
conn.Open();
var trans = conn.BeginTransaction();
try
{
//获取ID
2020-12-19 16:33:33 +08:00
var idEnity = new IdEnity
{
2021-01-18 15:07:20 +08:00
cAcc_Id = accid,//TODO
2020-12-19 16:33:33 +08:00
cVouchType = "mv",
iAmount = vouch.Vouchs.Count()
};
2020-12-19 15:30:38 +08:00
DynamicParameters sp = new DynamicParameters(idEnity);
2021-01-05 11:11:28 +08:00
sp.Add("@iFatherId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output,0);
sp.Add("@iChildId", 0, System.Data.DbType.Int32, System.Data.ParameterDirection.Output,0);
2020-12-19 15:30:38 +08:00
conn.Execute("sp_GetID", sp, trans, commandType: System.Data.CommandType.StoredProcedure);
int iFatherId = sp.Get<int>("@iFatherId");
int iChildId = sp.Get<int>("@iChildId");
//获取最大值
var cSeed = DateTime.Today.ToString("yyyyMM");
var sqlMax = $@"select cNumber as Maxnumber From VoucherHistory with (ROWLOCK) Where CardNumber='0413' and cContent='日期' and cSeed=@cSeed";
2021-01-05 11:11:28 +08:00
var maxCode = conn.ExecuteScalar<int>(sqlMax, new { cSeed }, trans,200,System.Data.CommandType.Text);
2020-12-19 15:30:38 +08:00
maxCode++;
//更新最大之
2021-01-05 11:11:28 +08:00
var sqlUpdate = "update VoucherHistory set cNumber=@cNumber Where CardNumber='0413' and cContent='日期' and cSeed=@cSeed ";
2020-12-19 15:30:38 +08:00
conn.Execute(sqlUpdate, new { cNumber = maxCode, cSeed }, trans);
//获取前缀
var sqlPrefix = " Select Prefix1Rule From VoucherNumber Where CardNumber = '0413'";
2021-01-05 11:11:28 +08:00
var prefix = conn.ExecuteScalar<string>(sqlPrefix, null, trans,200,System.Data.CommandType.Text);
2020-12-19 15:30:38 +08:00
//组装Code
2020-12-25 11:07:27 +08:00
var cCode = prefix + cSeed.Substring(2, cSeed.Length - 2) + maxCode.ToString().PadLeft(5, '0');
2020-12-19 15:30:38 +08:00
//主表
2020-12-19 16:33:33 +08:00
var sql = @"insert into MaterialAppVouch(id,ddate,ccode,crdcode,cdepcode,cpersoncode,cmaker,vt_id,iswfcontrolled,dnmaketime,cDefine12)
values(@id,@ddate,@ccode,@crdcode,@cdepcode,@cpersoncode,@cmaker,@vt_id,@iswfcontrolled, getdate(),@cDefine12)";
2020-12-19 15:30:38 +08:00
vouch.ID = iFatherId;
2020-12-19 16:33:33 +08:00
vouch.cDefine12 = vouch.CCode;
2020-12-19 15:30:38 +08:00
vouch.CCode = cCode;
conn.Execute(sql, vouch, trans);
sql = @"INSERT INTO MaterialAppVouchs (autoid, id, cinvcode, inum, iquantity , cwhcode, iexpiratdatecalcu, irowno, iordertype, isotype , ipesotype)
VALUES (@autoid, @id, @cinvcode, @inum, @iquantity , @cwhcode, @iexpiratdatecalcu, @irowno, @iordertype, @isotype , @ipesotype)";
//明细
2020-12-25 11:07:27 +08:00
var n = vouch.Vouchs.Count() - 1;
2020-12-19 15:30:38 +08:00
foreach (var item in vouch.Vouchs)
{
item.autoid = iChildId - n;
2020-12-25 11:07:27 +08:00
item.id = iFatherId;
2020-12-19 15:30:38 +08:00
n--;
item.IRowNO = n;
}
conn.Execute(sql, vouch.Vouchs, trans);
trans.Commit();
}
2020-12-25 11:07:27 +08:00
catch (Exception err)
2020-12-19 15:30:38 +08:00
{
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
trans.Rollback();
throw err;
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
}
finally
{
2020-12-25 11:07:27 +08:00
2020-12-19 15:30:38 +08:00
conn.Close();
}
2020-12-19 16:33:33 +08:00
return true;
2020-12-19 15:30:38 +08:00
}
}
}