2025-06-25 10:53:11 +08:00

270 lines
9.6 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @author 李志强
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\business\controller;
use app\base\BaseController;
use app\model\Supplier as SupplierList;
use app\business\validate\SupplierCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Supplier extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
$list = SupplierList::whereNull('delete_time')
->field('id, supplier_name, contact_name, contact_phone, contact_email, business_scope, company_size, remarks')
->order('id desc')
->paginate($rows, false);
return table_assign(0, '', $list->toArray());
} else {
return view();
}
}
//编辑
public function edit()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$supplier = SupplierList::where('id', $id)
->field('id, supplier_name, company_size, registered_address, business_scope, contact_name, contact_phone, contact_email, contact_wechat, remarks, intention')
->find();
if ($supplier) {
View::assign('detail', $supplier);
} else {
View::assign('message', '没有详细数据');
}
if (request()->isPost()) {
$data = [
'supplier_name' => $param['supplier_name'],
'company_size' => $param['company_size'],
'registered_address' => $param['registered_address'],
'business_scope' => $param['business_scope'],
'contact_name' => $param['contact_name'],
'contact_phone' => $param['contact_phone'],
'contact_email' => $param['contact_email'],
'contact_wechat' => $param['contact_wechat'],
'remarks' => $param['remarks'],
'intention' => $param['intention'],
'update_time' => date('Y-m-d H:i:s'),
];
$res = SupplierList::where('id', $id)->update($data);
if ($res) {
return to_assign(2, '更新成功!');
} else {
return to_assign(1, '更新失败');
}
}
return view();
}
//添加
public function add()
{
$param = get_params();
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$newsupplier = (new SupplierList())->detail($param['id']);
try {
validate(SupplierCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = SupplierList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $newsupplier);
}
return to_assign();
} else {
try {
validate(SupplierCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['create_time'] = date('Y-m-d H:i:s', time());
$param['admin_id'] = $this->uid;
$sid = SupplierList::strict(false)->field(true)->insertGetId($param);
if ($sid) {
add_log('add', $sid, $param);
$log_data = array(
'module' => 'plans',
// 'id' => $sid,
'new_content' => $param['supplier_name'],
'field' => 'new',
'action' => 'add',
'admin_id' => $this->uid,
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
return to_assign(2, '添加成功!');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new SupplierList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//删除
public function delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$id = get_params("id");
$detail = Db::name('Supplier')->where('id', $id)->find();
if (!isset($detail['id'])) {
return to_assign(1, "账号不存在");
}
// Convert the current timestamp to a MySQL DATETIME format
$currentDateTime = date('Y-m-d H:i:s', time());
if (Db::name('Supplier')->where('id', $id)->update(['delete_time' => $currentDateTime]) !== false) {
$log_data = array(
'module' => 'supplier',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => '',
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功");
} else {
return to_assign(0, "删除失败");
}
} else {
return to_assign(1, "错误的请求");
}
}
//查看
public function view()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = (new SupplierList())->detail($id);
// print_r($id);
if (empty($detail)) {
return to_assign(1, '无法查询到详情信息');
} else {
View::assign('detail', $detail);
// print_r($detail);
return view();
}
}
//开票信息填写(新增与编辑)
public function invoice()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$supplier = SupplierList::where('id', $id)
->field('id, invoice_company_name, taxpayer_id, invoice_address, invoice_phone, bank_name, bank_account')
->find();
if ($supplier) {
View::assign('detail', $supplier);
} else {
View::assign('message', '没有详细数据');
}
if (request()->isPost()) {
$data = [
'invoice_company_name' => $param['invoice_company_name'],
'taxpayer_id' => $param['taxpayer_id'],
'invoice_address' => $param['invoice_address'],
'invoice_phone' => $param['invoice_phone'],
'bank_name' => $param['bank_name'],
'bank_account' => $param['bank_account'],
'update_time' => date('Y-m-d H:i:s'),
];
$res = SupplierList::where('id', $id)->update($data);
if ($res) {
return to_assign(2, '更新成功!');
} else {
return to_assign(1, '更新失败');
}
}
return view();
}
//发送意向
public function sendintention()
{
$param = get_params();
// 提取并检查必要参数
$supplierName = isset($param['supplier_name']) ? trim($param['supplier_name']) : '';
if (empty($supplierName)) {
return json_encode(['status' => 0, 'message' => '供应商名称不能为空']);
}
// 检查供应商名称是否重复
$existingSupplier = SupplierList::where('supplier_name', $supplierName)->find();
if ($existingSupplier) {
return json_encode(['status' => 1, 'message' => '供应商名称已存在']);
}
// 准备插入数据
$data = [
'supplier_name' => $supplierName,
'contact_name' => isset($param['contact_name']) ? $param['contact_name'] : '',
'contact_phone' => isset($param['contact_phone']) ? $param['contact_phone'] : '',
'contact_email' => isset($param['contact_email']) ? $param['contact_email'] : '',
'contact_wechat' => isset($param['contact_wechat']) ? $param['contact_wechat'] : '',
'registered_address' => isset($param['registered_address']) ? $param['registered_address'] : '',
'business_scope' => isset($param['business_scope']) ? $param['business_scope'] : '',
'company_size' => isset($param['company_size']) ? $param['company_size'] : '',
'intention' => isset($param['intention']) ? $param['intention'] : '',
'create_time' => date('Y-m-d H:i:s'),
];
// 插入数据到数据库
$res = SupplierList::insert($data);
if ($res) {
return json_encode(['status' => 2, 'message' => '添加成功']);
} else {
return json_encode(['status' => 3, 'message' => '添加失败']);
}
}
}