using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HH.YiDaSyncNC.Models
{
    public class FKGL_SyncServiceModel
    {
        #region 凭证数据模型
        /// <summary>
        /// 凭证模型
        /// </summary>
        public class DataModel
        {
            /// <summary>
            /// 头部
            /// </summary>
            public class voucher_header
            {
                /// <summary>
                /// 会计年度
                /// </summary>
                public string year { get; set; }

                /// <summary>
                /// 会计期间
                /// </summary>
                public string period { get; set; }

                /// <summary>
                /// 凭证类别
                /// </summary>
                public string pk_vouchertype { get; set; }

                /// <summary>
                /// 制单日期
                /// </summary>
                public string prepareddate { get; set; }

                /// <summary>
                /// 凭证号
                /// </summary>
                public string no { get; set; }

                /// <summary>
                /// 账簿
                /// </summary>
                public string pk_accountingbook { get; set; }

                /// <summary>
                /// 制单人
                /// </summary>
                public string pk_prepared { get; set; }

                /// <summary>
                /// 附件张数
                /// </summary>
                public string attachment { get; set; }

                /// <summary>
                /// 组织
                /// </summary>
                public string org { get; set; }
            }

            /// <summary>
            /// 头部分录
            /// </summary>
            public class voucher_body
            {
                /// <summary>
                /// 分录序号
                /// </summary>
                public int detailindex { get; set; }
                /// <summary>
                /// 科目编码
                /// </summary>
                public string accsubjcode { get; set; }
                /// <summary>
                /// 摘要
                /// </summary>
                public string explanation { get; set; }

                public string verifydate { get; set; }

                public string price { get; set; }

                public string excrate2 { get; set; }

                public string debitquantity { get; set; }
                /// <summary>
                /// 借方金额
                /// </summary>
                public string debitamount { get; set; }
                public string groupdebitamount { get; set; }

                public string globaldebitamount { get; set; }
                /// <summary>
                /// 借方金额
                /// </summary>
                public string localdebitamount { get; set; }

                public string creditquantity { get; set; }
                /// <summary>
                /// 贷方金额
                /// </summary>
                public string creditamount { get; set; }
                public string groupcreditamount { get; set; }

                public string globalcreditamount { get; set; }
                /// <summary>
                /// 贷方金额
                /// </summary>
                public string localcreditamount { get; set; }
                public string billtype { get; set; }
                public string bankaccount { get; set; }
                public string checkstyle { get; set; }
                public string checkno { get; set; }
                public string checkdate { get; set; }
                public string bill_id { get; set; }
                public string bill_date { get; set; }
                public string pk_currtype { get; set; }
                /// <summary>
                /// 科目编码
                /// </summary>
                public string pk_accasoa { get; set; }

                public string pk_unit { get; set; }

                public string pk_unit_v { get; set; }
                /// <summary>
                /// 辅助核算
                /// </summary>
                public List<DataModel.ass> ass { get; set; }
                /// <summary>
                /// 现金流量
                /// </summary>
                public DataModel.cash_flow cash_flow { get; set; }
            }

            public class cash_flow
            {

                public string m_pk_currtype { get; set; }

                public string money { get; set; }

                public string moneyglobal { get; set; }

                public string moneygroup { get; set; }

                public string moneymain { get; set; }
                /// <summary>
                /// 现金主键
                /// </summary>
                public string pk_cashflow { get; set; }

                public string pk_innercorp { get; set; }
            }

            public class ass
            {
                /// <summary>
                /// 辅助核算类型 编码
                /// </summary>
                public string pk_Checktype { get; set; }
                /// <summary>
                /// 值
                /// </summary>
                public string pk_Checkvalue { get; set; }
            }
        }
        #endregion

        public class ImportXml_凭证
        {
            public static XElement TemplateHeade(DataModel.voucher_header item)
            {

                return new XElement(new XElement("ufinterface", new object[]
                {
                    new XAttribute("proc","add"),
                    new XAttribute("sender","dingding"),
                    new XAttribute("receiver",item.org),
                    new XAttribute("roottag",""),
                    new XAttribute("replace","N"),
                    new XAttribute("orgcode",item.org),
                    new XAttribute("isexchange",""),
                    new XAttribute("groupcode","0"),
                    new XAttribute("filename",""),
                    new XAttribute("businessunitcode","develop"),
                    new XAttribute("billtype","vouchergl"),
                    new XAttribute("account","develop")
                }));
            }
            /// <summary>
            /// 组建表头xml
            /// </summary>
            /// <param name="item">表头参数</param>
            /// <returns></returns>
            public static XElement Header(DataModel.voucher_header item)
            {
                return new XElement("voucher", new object[]
                {
                "",
				//new XAttribute("id",""),
				new XElement("voucher_head", new object[]
                {
                    new XElement("pk_voucher",""),
                    new XElement("pk_vouchertype",item.pk_vouchertype),
                    new XElement("year",item.year),
                    new XElement("pk_system","GL"),
                    new XElement("voucherkind","0"),
                    new XElement("pk_accountingbook",item.pk_accountingbook),
                    new XElement("discardflag","N"),
                    new XElement("period",item.period),
                    new XElement("no",item.no),
                    new XElement("attachment",""),
                    new XElement("prepareddate",item.prepareddate),
                    new XElement("pk_prepared",/*"DINGDING"*/item.pk_prepared),
                    new XElement("pk_casher",""),
                    new XElement("signflag","Y"),
                    new XElement("pk_checked",""),
                    new XElement("tallydate",""),
                    new XElement("pk_manager",""),
                    new XElement("memo1",""),
                    new XElement("memo2",""),
                    new XElement("reserve1",""),
                    new XElement("reserve2","N"),
                    new XElement("siscardflag",""),
                    new XElement("pk_org",item.org),
                    new XElement("pk_org_v",item.org),
                    new XElement("pk_group","0"),
                    new XElement("details","")
                }),
                });
            }

            /// <summary>
            /// 组建表体xml
            /// </summary>
            /// <param name="items">表体</param>
            /// <returns></returns>
            public static List<XElement> Bodydebit(List<DataModel.voucher_body> items)
            {
                List<XElement> list = new List<XElement>();

                foreach (DataModel.voucher_body current in items)
                {
                    List<XElement> lstItem = new List<XElement>();
                    foreach (var ite in current.ass)
                    {
                        if (!string.IsNullOrWhiteSpace(ite.pk_Checktype) && !string.IsNullOrWhiteSpace(ite.pk_Checkvalue))
                        {
                            XElement ites = new XElement("item", new object[] {
                            new XElement("pk_Checktype",ite.pk_Checktype),
                            new XElement("pk_Checkvalue",ite.pk_Checkvalue)
                        });
                            lstItem.Add(ites);
                        }
                    }

                    XElement item = new XElement("item", new object[]
                    {
                    new XElement("detailindex",current.detailindex),
                    new XElement("accsubjcode",current.pk_accasoa),
                    new XElement("explanation",current.explanation),
                    new XElement("verifydate",current.verifydate),
                    new XElement("price",current.price),
                    new XElement("excrate2",current.excrate2),
                    new XElement("debitquantity",current.debitquantity),
                    new XElement("debitamount",current.debitamount),
                    new XElement("groupdebitamount",current.groupdebitamount),
                    new XElement("globaldebitamount",current.globaldebitamount),
                    new XElement("localdebitamount",current.localdebitamount),
                    new XElement("creditquantity",current.creditquantity),
                    new XElement("creditamount",current.creditamount),
                    new XElement("groupcreditamount",current.groupcreditamount),
                    new XElement("globalcreditamount",current.globalcreditamount),
                    new XElement("localcreditamount",current.localcreditamount),
                    new XElement("billtype",current.billtype),
                    new XElement("bankaccount",current.bankaccount),
                    new XElement("checkstyle",current.checkstyle),
                    new XElement("checkno",current.checkno),
                    new XElement("checkdate",current.checkdate),
                    new XElement("bill_id",current.bill_id),
                    new XElement("bill_date",current.bill_date),
                    new XElement("pk_currtype",current.pk_currtype),
                    new XElement("pk_accasoa",current.pk_accasoa),
                    new XElement("ass",new object[]
                        {
                            lstItem,
                        }),
                        });

                    if (!string.IsNullOrWhiteSpace(current.cash_flow.m_pk_currtype) && !string.IsNullOrWhiteSpace(current.cash_flow.money) && !string.IsNullOrWhiteSpace(current.cash_flow.moneymain) && !string.IsNullOrWhiteSpace(current.cash_flow.pk_cashflow))
                    {
                        XElement cashFlow = new XElement("cashFlow", new XElement("item", new object[]
                           {
                            new XElement("m_pk_currtype",current.cash_flow.m_pk_currtype),
                            new XElement("money",current.cash_flow.money),
                            new XElement("moneyglobal",current.cash_flow.moneyglobal),
                            new XElement("moneygroup",current.cash_flow.moneygroup),
                            new XElement("moneymain",current.cash_flow.moneymain),
                            new XElement("pk_cashflow",current.cash_flow.pk_cashflow),
                            new XElement("pk_innercorp",current.cash_flow.pk_innercorp)
                           }));
                        item.Add(cashFlow);
                    }
                    list.Add(item);
                }
                return list;
            }
        }
    }
}
