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

namespace HH.YiDaSyncNC.Models
{
    class CGDD_SyncServiceModel
    {
        public class DataModel
        {
            public string 集团 { get; set; }
            public string 组织 { get; set; }

            public string 部门 { get; set; } = "06";

            public string 订单编号 { get; set; }
            public string 订单日期 { get; set; }
            public string 供应商 { get; set; }
            public string 订单类型 { get; set; } = "21-Cxx-02";
            public string 单据状态 { get; set; } = "0";

            public string 审批人 { get; set; }
            public string 审批时间 { get; set; }
            public string 制单人 { get; set; }
            public string 制单时间 { get; set; }
            public string 创建人 { get; set; }
            public string 创建时间 { get; set; }

            public string 换算率 { get; set; } = "1.00/1.00";
            public string 折扣 { get; set; } = "100";

            public string 数量 { get; set; }
            public string 价税合计 { get; set; }
            public string 业务流程 { get; set; } = "Cxx-CG-02";
            public string 币种 { get; set; } = "CNY";

            public List<OrderB> pk_order_b { get; set; }

            public class OrderB
            {
                public string 物料编码 { get; set; }
                public string 物料名称 { get; set; }
                public string 计量单位 { get; set; }
                public string 项目 { get; set; }
                public string 数量 { get; set; }
                public string 无税单价 { get; set; }
                public string 含税单价 { get; set; }
                public string 无税金额 { get; set; }
                public string 税额 { get; set; }
                public string 税率 { get; set; } = "13";
                public string 税码 { get; set; } = "CN15";
                public string 价税合计 { get; set; }
                public string 联系人 { get; set; }
                public string 收货地址 { get; set; }
            }
        }

        public class ImportXml
        {
            /// <summary>
            /// 模板头部份
            /// </summary>
            /// <returns></returns>
            public static XElement TemplateHeade()
            {

                return new XElement(new XElement("ufinterface", new object[]
                {
                new XAttribute("sender","dingding"),
                new XAttribute("roottag",""),
                new XAttribute("replace","Y"),
                new XAttribute("isexchange","Y"),
                new XAttribute("groupcode","0"),
                new XAttribute("filename",""),
                new XAttribute("billtype","21"),
                new XAttribute("account","develop")
                }));
            }

            /// <summary>
            /// 组建表头xml
            /// </summary>
            /// <param name="item">表头参数</param>
            /// <returns></returns>
            public static XElement Header(DataModel item, string id)
            {
                var pk_order_b = new List<XElement>();

                if (item.pk_order_b != null && item.pk_order_b.Count > 0)
                {
                    var crowno = 10; // 行号

                    foreach (var itemData in item.pk_order_b)
                    {
                        pk_order_b.Add(new XElement("item", new object[] {
                            new XElement("pk_group",item.集团),
                            new XElement("pk_org",item.组织),
                            new XElement("pk_org_v",item.组织),
                            new XElement("pk_reqcorp",item.组织),
                            new XElement("pk_reqstoorg",item.组织),
                            new XElement("pk_reqstoorg_v",item.组织),
                            new XElement("pk_arrvstoorg",item.组织),
                            new XElement("pk_arrvstoorg_v",item.组织),
                            new XElement("pk_psfinanceorg",item.组织),
                            new XElement("pk_psfinanceorg_v",item.组织),
                            new XElement("pk_apfinanceorg",item.组织),
                            new XElement("pk_apfinanceorg_v",item.组织),

                            new XElement("crowno",crowno.ToString()),

                            new XElement("pk_material",itemData.物料编码),
                            new XElement("pk_srcmaterial",itemData.物料编码),
                            new XElement("vvendinventorycode",itemData.物料编码),
                            new XElement("vvendinventoryname",itemData.物料名称),
                            new XElement("cunitid",itemData.计量单位),
                            new XElement("castunitid",itemData.计量单位),
                            new XElement("cqtunitid",itemData.计量单位),

                            new XElement("cprojectid",itemData.项目),
                            new XElement("dbilldate",item.订单日期),
                            new XElement("pk_supplier",item.供应商),
                            new XElement("dsenddate",Convert.ToDateTime(item.订单日期).AddDays(1).ToString("yyyy-MM-dd HH:mm:ss")),

                            new XElement("nnum",itemData.数量),
                            new XElement("nastnum",itemData.数量),
                            new XElement("vchangerate",item.换算率),
                            new XElement("nqtunitnum",itemData.数量),
                            new XElement("vqtunitrate",item.换算率),

                            new XElement("nqtorigprice",itemData.无税单价),
                            new XElement("nqtorignetprice",itemData.无税单价),
                            new XElement("nqtnetprice",itemData.无税单价),
                            new XElement("norigprice",itemData.无税单价),
                            new XElement("norignetprice",itemData.无税单价),
                            new XElement("nnetprice",itemData.无税单价),
                            new XElement("nqtprice",itemData.无税单价),
                            new XElement("nprice",itemData.无税单价),

                            new XElement("nqtorigtaxprice",itemData.含税单价),
                            new XElement("nqtorigtaxnetprc",itemData.含税单价),
                            new XElement("nqttaxnetprice",itemData.含税单价),
                            new XElement("norigtaxprice",itemData.含税单价),
                            new XElement("norigtaxnetprice",itemData.含税单价),
                            new XElement("ntaxnetprice",itemData.含税单价),
                            new XElement("nqttaxprice",itemData.含税单价),
                            new XElement("ntaxprice",itemData.含税单价),

                            new XElement("norigmny",itemData.无税金额),
                            new XElement("nmny",itemData.无税金额),
                            new XElement("ncaltaxmny",itemData.无税金额),
                            new XElement("ncalcostmny",itemData.无税金额),

                            new XElement("ntax",itemData.税额),
                            new XElement("ntaxrate",itemData.税率),
                            new XElement("ctaxcodeid",itemData.税码),

                            new XElement("norigtaxmny",itemData.价税合计),
                            new XElement("ntaxmny",itemData.价税合计),

                            new XElement("nitemdiscountrate","100"),
                            new XElement("breceiveplan","N"),
                            new XElement("blargess","N"),
                            new XElement("btransclosed","N"),
                            new XElement("bborrowpur","N"),

                            new XElement("bstockclose","N"),
                            new XElement("binvoiceclose","N"),
                            new XElement("barriveclose","N"),
                            new XElement("bpayclose","N"),
                            new XElement("ftaxtypeflag","1"),

                            new XElement("vbdef3",itemData.联系人),
                            new XElement("vbdef4",itemData.收货地址),

                            new XElement("ccurrencyid",item.币种),
                            new XElement("nexchangerate","1"),

                            new XElement("corigcurrencyid",item.币种),

                            new XElement("csendcountryid","CN"),
                            new XElement("crececountryid","CN"),
                            new XElement("ctaxcountryid","CN"),

                            new XElement("fbuysellflag","2"),
                            new XElement("btriatradeflag","N"),

                        }));

                        crowno = crowno + 10;
                    }
                }

                return new XElement("bill", new object[]
                {
                    "",
                    new XAttribute("id",id),
                    new XElement("billhead", new object[]
                    {
                        new XElement("pk_group",item.集团),
                        new XElement("pk_org",item.组织),
                        new XElement("pk_org_v",item.组织),
                        new XElement("vbillcode",item.订单编号),
                        new XElement("dbilldate",item.订单日期),

                        new XElement("pk_supplier",item.供应商),
                        new XElement("pk_invcsupllier",item.供应商),

                        new XElement("vtrantypecode",item.订单类型),
                        new XElement("ctrantypeid",item.订单类型),

                        new XElement("pk_dept",item.部门),
                        new XElement("pk_dept_v",item.部门),

                        new XElement("approver",item.审批人),
                        new XElement("taudittime",item.审批时间),
                        new XElement("billmaker",item.制单人),
                        new XElement("dmakedate",item.制单时间),
                        new XElement("creator",item.创建人),
                        new XElement("creationtime",item.创建时间),


                        new XElement("forderstatus",item.单据状态),
                        new XElement("nversion","1"),
                        new XElement("bislatest","Y"),
                        new XElement("vdef10",item.订单编号),
                    
                        new XElement("ntotalastnum",item.数量),
                        new XElement("ntotalorigmny",item.价税合计),

                        new XElement("pk_busitype",item.业务流程),
                        new XElement("fhtaxtypeflag","1"),

                        new XElement("corigcurrencyid",item.币种),

                        new XElement("pk_order_b",pk_order_b),
                    }),
                });
            }
        }
    }
}
