99 lines
6.1 KiB
C#
99 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Globalization;
|
|
using System.Web.Script.Serialization;
|
|
|
|
namespace Jingyao.AIEmployee
|
|
{
|
|
public sealed partial class PersonalInboxHandler
|
|
{
|
|
const string ContractDraftSave = "ai_合同管理_报价转合同_保存草稿";
|
|
const string ContractDraftProof = "ai_合同管理_报价转合同_保存回执";
|
|
|
|
static decimal ContractAmount(decimal value,int places)
|
|
{
|
|
decimal result = decimal.Round(value,places,MidpointRounding.AwayFromZero);
|
|
decimal limit = places==2 ? 9999999999999999.99m : 99999999999999.9999m;
|
|
if(result<0 || result>limit) throw BadInput();
|
|
return result;
|
|
}
|
|
|
|
static void BindContractLines(SqlCommand cmd,object[] rows,decimal taxRate,JavaScriptSerializer json)
|
|
{
|
|
if(rows==null || rows.Length<1 || rows.Length>100) throw BadInput();
|
|
var table=new DataTable();
|
|
table.Columns.Add("行号",typeof(int));
|
|
table.Columns.Add("来源报价明细流水号",typeof(long));
|
|
table.Columns.Add("产品",typeof(string));
|
|
table.Columns.Add("数量",typeof(decimal));
|
|
table.Columns.Add("单位",typeof(string));
|
|
foreach(string field in new[]{"单价","未税金额","税额","含税金额"}) table.Columns.Add(field,typeof(decimal));
|
|
decimal grandTotal=0;
|
|
for(int i=0;i<rows.Length;i++) {
|
|
var row=rows[i] as Dictionary<string,object>;
|
|
if(row==null) throw BadInput();
|
|
long sourceLine=Positive(row,"sourceQuotationLineId",9007199254740991L,0);
|
|
string product=QuoteString(row,"product",500,true), unit=QuoteString(row,"unit",20,true);
|
|
decimal qty=QuoteDecimal(row,"quantity",4,99999999999999.9999m,false);
|
|
decimal price=QuoteDecimal(row,"unitPrice",4,99999999999999.9999m,true);
|
|
object amount;
|
|
decimal gross;
|
|
try {
|
|
gross=row.TryGetValue("totalAmount",out amount) && amount!=null
|
|
? QuoteDecimal(row,"totalAmount",2,9999999999999999.99m,true) : ContractAmount(qty*price*(1m+taxRate),2);
|
|
} catch(OverflowException) {throw BadInput();}
|
|
decimal net=ContractAmount(gross/(1m+taxRate),2);
|
|
decimal calculatedPrice=ContractAmount(net/qty,4);
|
|
decimal tax=gross-net;
|
|
table.Rows.Add(i+1,sourceLine,product,qty,unit,calculatedPrice,net,tax,gross);
|
|
row["lineNo"]=i+1; row["product"]=product; row["unit"]=unit; row["quantity"]=qty;
|
|
row["unitPrice"]=calculatedPrice; row["totalAmount"]=gross; row["untaxedAmount"]=net; row["taxAmount"]=tax;
|
|
row["taxRate"]=taxRate;
|
|
grandTotal+=gross;
|
|
}
|
|
ContractAmount(grandTotal,2);
|
|
AddQuoteTable(cmd,"@产品行","ai_合同管理_报价来源行",table);
|
|
cmd.Parameters.Add("@明细快照",SqlDbType.NVarChar,-1).Value=json.Serialize(rows);
|
|
}
|
|
|
|
static void BindContractDraft(SqlCommand cmd,string name,Dictionary<string,object> inputs)
|
|
{
|
|
if(name==ContractDraftProof) Only(inputs,"报价流水号","需求流水号","客户流水号","客户端请求号");
|
|
else Only(inputs,"报价流水号","需求流水号","客户流水号","原修订","客户端请求号","合同模板","提成模式","付款方式流水号","税率","交付日期","负责人","质保条款","备注","乙方资料","合同协议","产品行");
|
|
cmd.Parameters.Add("@报价流水号",SqlDbType.BigInt).Value=Positive(inputs,"报价流水号",9007199254740991L,0);
|
|
cmd.Parameters.Add("@需求流水号",SqlDbType.BigInt).Value=Positive(inputs,"需求流水号",9007199254740991L,0);
|
|
cmd.Parameters.Add("@客户流水号",SqlDbType.Int).Value=(int)Positive(inputs,"客户流水号",Int32.MaxValue,0);
|
|
Guid request;
|
|
if(!Guid.TryParseExact(Text(inputs,"客户端请求号",36,null),"D",out request) || request==Guid.Empty) throw BadInput();
|
|
cmd.Parameters.Add("@客户端请求号",SqlDbType.UniqueIdentifier).Value=request;
|
|
if(name==ContractDraftProof) return;
|
|
BindReviewVersion(cmd,inputs,"原修订");
|
|
cmd.Parameters.Add("@合同模板",SqlDbType.VarChar,30).Value=QuoteString(inputs,"合同模板",30,true);
|
|
cmd.Parameters.Add("@提成模式",SqlDbType.VarChar,40).Value=QuoteString(inputs,"提成模式",40,true);
|
|
cmd.Parameters.Add("@付款方式流水号",SqlDbType.Int).Value=(int)Positive(inputs,"付款方式流水号",Int32.MaxValue,0);
|
|
decimal tax=QuoteDecimal(inputs,"税率",6,1,true);
|
|
var taxParam=cmd.Parameters.Add("@税率",SqlDbType.Decimal);
|
|
taxParam.Scale=6; taxParam.Precision=9; taxParam.Value=tax;
|
|
DateTime delivery;
|
|
if(!DateTime.TryParseExact(Text(inputs,"交付日期",10,null),"yyyy-MM-dd",Invariant,DateTimeStyles.None,out delivery)) throw BadInput();
|
|
cmd.Parameters.Add("@交付日期",SqlDbType.Date).Value=delivery;
|
|
foreach(string field in new[]{"负责人","质保条款","备注","合同协议"}) {
|
|
int max=field=="负责人"?100:field=="合同协议"?6000:500;
|
|
cmd.Parameters.Add("@"+field,SqlDbType.NVarChar,max>4000?-1:max).Value=QuoteString(inputs,field,max,field!="备注");
|
|
}
|
|
string seller=QuoteString(inputs,"乙方资料",20000,true);
|
|
if(!(Parse(seller,false) is Dictionary<string,object>)) throw BadInput();
|
|
cmd.Parameters.Add("@乙方资料",SqlDbType.NVarChar,-1).Value=seller;
|
|
var json=new JavaScriptSerializer {MaxJsonLength=QuoteBodyLimit};
|
|
object lines;
|
|
if(!inputs.TryGetValue("产品行",out lines)) throw BadInput();
|
|
BindContractLines(cmd,lines as object[],tax,json);
|
|
string digest=QuoteFingerprint(cmd);
|
|
cmd.Parameters.Add("@请求指纹",SqlDbType.Char,64).Value=digest;
|
|
}
|
|
}
|
|
}
|
|
|