<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\quotation;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExcelInterface;
use app\common\model\custom\Custom;
use app\common\model\material\Material;
use app\common\model\material\MaterialClassify;
use app\common\model\quotation\Quotation;
use app\common\model\quotation\QuotationDetail;
use app\common\lists\ListsSearchInterface;


/**
 * 报价明细列表
 * Class QuotationDetailLists
 * @package app\adminapi\listsquotation
 */
class QuotationDetailLists extends BaseAdminDataLists implements ListsSearchInterface,ListsExcelInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2023/11/27 17:59
     */
    public function setSearch(): array
    {
        return [
            '=' => ['quotation_id'],
        ];
    }


    /**
     * @notes 获取报价明细列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2023/11/27 17:59
     */
    public function lists(): array
    {
        return QuotationDetail::where($this->searchWhere)
            ->field(['id', 'quotation_id', 'product_id', 'num', 'tax_rate', 'tax_inclusive_price', 'tax_inclusive_amount', 'tax_exclusive_amount', 'remark', 'create_time'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()->each(function($item){
				$quotation = Quotation::field('custom_id,code,quotation_date,create_user')->where('id',$item['quotation_id'])->findOrEmpty();
				$custom = Custom::field('name')->where('id',$quotation['custom_id'])->findOrEmpty();
				$material = Material::field('first_level,second_level,three_level,name,code,specs,brand,parameter_description,unit')->where('id',$item['product_id'])->findOrEmpty();
		        $classify = MaterialClassify::where('id','in',[$material['first_level'],$material['second_level'],$material['three_level']])->column('name','id');
				$item['quotation_code'] = $quotation['code'];
		        $item['quotation_date'] = $quotation['quotation_date'];
		        $item['quotation_create_user'] = $quotation['create_user'];
		        $item['custom_name'] = $custom['name'];
		        $item['product_first_level_name'] = !empty($classify[$material['first_level']]) ? $classify[$material['first_level']] : '';
		        $item['product_second_level_name'] = !empty($classify[$material['second_level']]) ? $classify[$material['second_level']] : '';
		        $item['product_three_level_name'] = !empty($classify[$material['three_level']]) ? $classify[$material['three_level']] : '';
				$item['product_name'] = $material['name'];
		        $item['product_code'] = $material['code'];
		        $item['product_specs'] = $material['specs'];
		        $item['product_brand'] = $material['brand'];
		        $item['product_parameter_description'] = $material['parameter_description'];
		        $item['product_unit'] = $material['unit'];
				$item['tax_rate'] = $item->tax_rate_text.'%';
	        })
            ->toArray();
    }


    /**
     * @notes 获取报价明细数量
     * @return int
     * @author likeadmin
     * @date 2023/11/27 17:59
     */
    public function count(): int
    {
        return QuotationDetail::where($this->searchWhere)->count();
    }
	
	public function setFileName(): string
	{
		return '报价单明细列表';
	}
	
	/**
	 * @notes 导出字段
	 * @return string[]
	 * @author 段誉
	 * @date 2022/11/24 16:17
	 */
	public function setExcelFields(): array
	{
		return [
			'id' => 'id',
			'quotation_code' => '报价单号',
			'custom_name' => '客户名称',
			'quotation_create_user' => '制单人',
			'quotation_date' => '报价日期',
			'product_first_level_name' => '产品类别',
			'product_second_level_name' => '产品中类',
			'product_three_level_name' => '产品小类',
			'product_name' => '产品名称',
			'product_code' => '产品编码',
			'product_specs' => '规格型号',
			'product_brand' => '品牌',
			'product_parameter_description' => '参数说明',
			'product_unit' => '单位',
			'num' => '数量',
			'tax_rate' => '税率',
			'tax_inclusive_price' => '含税单价',
			'tax_exclusive_amount' => '不含税金额',
			'tax_inclusive_amount' => '含税金额',
			'remark' => '备注',
		];
	}

}