﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using LJH.Inventory.BusinessModel;
using LJH.Inventory.BusinessModel.Resource;
using LJH.Inventory.BLL;
using LJH.GeneralLibrary;

namespace LJH.Inventory.UI.Forms.Inventory.Print
{
    /// <summary>
    /// 表示一个送货单导出电子表格类
    /// </summary>
    public class 送货单Exporter : SheetExporter<StackOutSheet, StackOutItem>
    {
        #region 构造函数
        public 送货单Exporter(string modalPath)
            : base(modalPath)
        {
        }

        public 送货单Exporter(string modalPath, bool showPrice)
            : base(modalPath, showPrice)
        {
        }
        #endregion

        #region 重写基类方法
        protected override bool FillSheetInfo(ICell cell, string express, StackOutSheet info, StackOutItem[] allItems, StackOutItem[] curPageItems)
        {
            var ret = true;
            if (express == "[送货单号]") cell.SetCellValue(info.ID);
            else if (express == "[送货日期]") cell.SetCellValue(info.SheetDate.ToString("yyyy年MM月dd日"));
            else if (express == "[客户]")
            {
                Company customer = new CompanyBLL(AppSettings.Current.ConnStr).GetByID(info.CustomerID).QueryObject;
                if (customer != null) cell.SetCellValue(customer.Name);
            }
            else if (express == "[联系人]") cell.SetCellValue(info.Linker);
            else if (express == "[联系人电话]") cell.SetCellValue(info.LinkerCall);
            else if (express == "[送货司机]") cell.SetCellValue(info.Driver);
            else if (express == "[司机电话]") cell.SetCellValue(info.DriverCall);
            else if (express == "[送货车牌]") cell.SetCellValue(info.CarPlate);
            else if (express == "[送货地址]") cell.SetCellValue(info.Address);
            else if (express == "[开票]") cell.SetCellValue(info.WithTax ? "KP" : "BK");
            else if (express == "[领料人]") cell.SetCellValue(info.SalesPerson);
            else if (express == "[加工单号]" || express == "[生产单号]") cell.SetCellValue(info.PurchaseID);
            else if (express == "[业务员]" || express == "[业务]") cell.SetCellValue(info.SalesPerson);
            else if (express == "[仓库]")
            {
                if (!string.IsNullOrEmpty(info.WareHouseID))
                {
                    var w = new LJH.Inventory.BLL.WareHouseBLL(AppSettings.Current.ConnStr).GetByID(info.WareHouseID).QueryObject;
                    if (w != null) cell.SetCellValue(w.Name);
                }
            }
            else if (express == "[合同编号]") cell.SetCellValue(info.GetProperty(ItemNote.合同编号));
            else if (express == "[付款金额]")
            {
                if (_ShowPrice)
                {
                    var finance = info.GetStatePropertyOfDecimal(ItemNote.已付金额);
                    if (finance.HasValue) cell.SetCellValue((double)(finance != null ? finance.Value : 0));
                    else cell.SetCellValue(string.Empty);
                }
                else
                {
                    cell.SetCellValue(string.Empty);
                }
            }
            else if (express == "[本次欠款]")
            {
                if (_ShowPrice)
                {
                    var finance = info.GetStatePropertyOfDecimal(ItemNote.已付金额);
                    if (finance.HasValue) cell.SetCellValue((double)(info.Amount - finance.Value));
                    else cell.SetCellValue(string.Empty);
                }
                else
                {
                    cell.SetCellValue(string.Empty);
                }
            }
            else if (express == "[累计欠款]")
            {
                if (_ShowPrice)
                {
                    var customerState = new CompanyBLL(AppSettings.Current.ConnStr).GetCustomerState(info.CustomerID).QueryObject;
                    decimal total = customerState != null ? (customerState.Recievables - customerState.Prepay) : 0;
                    if (info.State != LJH.Inventory.BusinessModel.SheetState.已发货) total += info.Amount; //如果送货单还未处于送货状态，说明此单的还没有加到应收里面，此时总欠款要加上这一笔
                    cell.SetCellValue((double)total);
                }
                else
                {
                    cell.SetCellValue(string.Empty);
                }
            }
            else ret = false;
            return ret;
        }

        protected override bool FillSheetItemInfo(ICell cell, string express, StackOutItem item)
        {
            return false;
        }
        #endregion
    }
}
