<?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\logic\quotation;


use app\common\model\custom\Custom;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
use app\common\model\dict\DictData;
use app\common\model\quotation\Quotation;
use app\common\model\quotation\QuotationDetail;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * 报价单逻辑
 * Class QuotationLogic
 * @package app\adminapi\logic\quotation
 */
class QuotationLogic extends BaseLogic
{


    /**
     * @notes 添加报价单
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/11/27 17:23
     */
    public static function add(array $params): bool
    {
	    $quotation_detail = $params['quotation_detail'];
	    $tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value');
		foreach($quotation_detail as &$v){
			$v['tax_inclusive_amount'] = $v['num'] * $v['tax_inclusive_price'];
			$v['tax_exclusive_amount'] = $v['num'] * $v['tax_inclusive_price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100);
		}
        Db::startTrans();
        try {
            $quotation = Quotation::create([
                'org_id' => $params['org_id'],
                'dept_id' => $params['dept_id'],
                'custom_id' => $params['custom_id'],
				'code' => data_unique_code('报价单'),
                'quotation_date' => !empty($params['quotation_date']) ? strtotime($params['quotation_date']) : 0,
                'create_user' => $params['create_user'] ?? '',
                'invoice_type' => $params['invoice_type'] ?? 0,
                'freight' => $params['freight'],
                'other_fee' => $params['other_fee'],
                'customer_require' => $params['customer_require'] ?? '',
                'remark' => $params['remark'] ?? '',
                'annex' => $params['annex']? json_encode($params['annex']) : null,
            ]);
            foreach ($quotation_detail as $item)
            {
                QuotationDetail::create([
                    'quotation_id' => $quotation->id,
                    'product_id' => $item['product_id'],
                    'num' => $item['num'],
                    'tax_rate' => $item['tax_rate'],
					'tax_inclusive_price' => $item['tax_inclusive_price'],
					'tax_inclusive_amount' => $item['tax_inclusive_price'] * $item['num'],
					'tax_exclusive_amount' => $item['tax_inclusive_price'] * $item['num']  * (1- $tax_rate_dict[$item['tax_rate']] / 100),
                    'remark' => $item['remark'] ?? ''
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
	
    /**
     * @notes 获取报价单详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2023/11/27 17:23
     */
    public static function detail($params): array
    {
        $data = Quotation::field('id,org_id,dept_id,custom_id,quotation_date,create_user,invoice_type,freight,other_fee,customer_require,remark,annex')->findOrEmpty($params['id']);
		$org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty();
		$dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty();
		$custom = Custom::field('name,master_name,master_phone')->where('id',$data['custom_id'])->findOrEmpty();
		$data['org_name'] = $org['name'];
		$data['dept_name'] = $dept['name'];
		$data['custom_name'] = $custom['name'];
	    $data['custom_master_name'] = $custom['master_name'];
	    $data['custom_master_phone'] = $custom['master_phone'];
		$data['invoice_type_text'] = $data->invoice_type_text;
		$data['amount_including_tax'] = QuotationDetail::where('quotation_id',$data['id'])->sum('tax_inclusive_amount');
	    $data['total_amount'] = $data['amount_including_tax'] + $data['freight'] + $data['other_fee'];
        return $data->toArray();
    }
}