<?php<hyzwd@outlook.com>namespace app\index\controller;
use app\index\model\Purchase;
use app\index\model\Finance;
use app\index\model\PurchaseAffirm;
use think\Request;
use think\Session;
use think\Db;
use think\Url;
class Handle extends Common_base
{
protected $handle; protected $name; protected $pnumber; protected $sycitcn;
public function _initialize()
{
IS_ROOT([1,4]) ? true : $this->error('没有权限');
$Request = Request::instance();
$this->handle = $Request->param('handle');
$this->name = $Request->param('name');
$this->pnumber = $Request->param('pid');
$this->sycitcn = $Request->param('sycitcn');
return parent::_initialize(); }
public function affirm() {
IS_ROOT([1,2]) ? true : $this->error('没有权限');
$result = $this->validate(['__token__' => $this->sycitcn],['__token__'=>'token']);
if(true !== $result){
}
if ($this->handle !='affirm' || !Purchase::get(['pnumber' => $this->pnumber])) {
$this->error('数据提交错误,请返回刷新2');
}
$Purchase = new Purchase();
$Request = Request::instance();
if ($Request->isPost()) {
$data=array();
$file = request()->file('pc_img'); if (!empty($file)) {
$info = $file->validate([
'type' => 'image/jpeg,image/gif,image/png', 'ext' => 'jpeg,jpg,png', 'size'=> 3 * 1024 * 1024, ])->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'images');
if ($info) {
$pc_img = DS . 'uploads'. DS .'images'. DS . $info->getSaveName();
$data['a_img'] = str_replace("\\",'/',$pc_img);
} else {
$this->error($file->getError());
}
}
$data['a_pnumber'] = $this->pnumber;
$data['a_affirm'] = $Request->param('affirm_ok');
$data['a_remark'] = $Request->param('remark');
$model = new PurchaseAffirm();
$model->data($data);
$result = $model->allowField(true)->save();
if ($result) {
$Purchase->allowField('affirm')->save(['affirm'=>1],['pnumber'=>$this->pnumber]);
$this->success('此订单操作成功',Url::build('product/color'));
} else {
$this->error('服务器错误,请通知管理员');
}
} else {
$data = $Purchase->where('pnumber', $this->pnumber)->where('affirm','=',0)->field('pnumber,pbname,pcsname,pcus_id,pamount')->find();
if (empty($data)) {
$this->error('数据提交错误,请返回刷新', Url::build('orders/index'));
}
$assign = [
'title' => '确认订单',
'data' => $data,
];
$this->assign($assign);
return $this->fetch();
}
}
public function deposit() {
IS_ROOT([1,4]) ? true : $this->error('没有权限');
$result = $this->validate(['__token__' => $this->sycitcn],['__token__'=>'token']);
if(true !== $result){
$this->error('数据提交错误,请返回刷新', Url::build('finance/index'));
}
if ($this->handle !='deposit' || !Purchase::get(['pnumber' => $this->pnumber])) {
$this->error('数据提交错误,请返回刷新2');
}
$Purchase = new Purchase();
$Request = Request::instance();
if ($Request->isPost()) {
$file = request()->file('pc_img');
if (!empty($file)) {
$info = $file->validate([
'type' => 'image/jpeg,image/gif,image/png', 'ext' => 'jpeg,jpg,png', 'size'=> 3 * 1024 * 1024, ])->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'images');
if ($info) {
$pc_img = DS . 'uploads'. DS .'images'. DS . $info->getSaveName();
$fs_img = str_replace("\\",'/',$pc_img);
} else {
$this->error($file->getError());
}
} else {
$fs_img='';
}
$data = $Purchase->where('pnumber', $this->pnumber)->field('pcus_id,pbname,pcsname,pamount')->find();
$djin_amount = sprintf("%.2f", $Request->param('djin_amount')); $shoukuan_time = strtotime($Request->param('shoukuan_time')); if ($djin_amount > $data['pamount'] || $djin_amount < '0') {
$this->error('订金金额不能大过订单金额和小于0');
}
$finance = new Finance();
$data = [
'fpnumber' => $this->pnumber,
'fcus_id' => $data['pcus_id']['cus_id'],
'fcus_name' => $data['pcus_id']['cus_duty'],
'sort' => '1',
'amount' => $djin_amount,
'fuid' => Session::get("user_id"),
'shoukuan_time' => $shoukuan_time,
];
$finance->data($data);
$result = $finance->allowField(true)->save();
if ($result) {
Db::name('finance_schedule')->insert([
'fs_fid' => $finance->fid,
'fs_img' => $fs_img,
'fs_remark' => $Request->param('remark')
]);
$Purchase->allowField(['pshoudj','status'])->save(['pshoudj'=>'1','status'=>'1'],['pnumber'=>$this->pnumber]);
$this->success('操作成功',Url::build('product/color'));
} else {
$this->error('保存数据错误,请联系管理员');
}
} else {
$data = $Purchase->where('pnumber', $this->pnumber)->where('affirm','=',1)->where('status', '=', 0)->find();
if (empty($data)) {
$this->error('参数错误', Url::build('finance/index'));
}
$sum_amount = Db::name('purchase_orders')->where('ord_pnumber',$data['pnumber'])->sum('amount'); $youhuije = ($sum_amount * 1) - ($data['pamount'] * 1);
$assign = [
'title' => '销售收款',
'data' => $data,
'sum_amount' => $sum_amount,
'youhuije' => $youhuije,
];
$this->assign($assign);
return $this->fetch();
}
}
public function balance() {
IS_ROOT([1,4]) ? true : $this->error('没有权限'); $result = $this->validate(['__token__' => $this->sycitcn],['__token__'=>'token']);
if(true !== $result){
}
if ($this->handle !='deposit' || !Purchase::get(['pnumber' => $this->pnumber])) {
$this->error('数据提交错误,请返回刷新2');
}
$Purchase = new Purchase();
$Request = Request::instance();
if ($Request->isPost()) {
$file = request()->file('pc_img');
if (!empty($file)) {
$info = $file->validate([
'type' => 'image/jpeg,image/gif,image/png', 'ext' => 'jpeg,jpg,png', 'size'=> 3 * 1024 * 1024, ])->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'images');
if ($info) {
$pc_img = DS . 'uploads'. DS .'images'. DS . $info->getSaveName();
$fs_img = str_replace("\\",'/',$pc_img);
} else {
$this->error($file->getError());
}
} else {
$fs_img='';
}
$data = $Purchase->where('pnumber', $this->pnumber)->field('pcus_id,pbname,pamount,pshengcwc')->find();
$djin_amount = sprintf("%.2f", $Request->param('djin_amount')); $shoukuan_time = strtotime($Request->param('shoukuan_time')); if ($djin_amount > $data['pamount'] || $djin_amount < '0') {
$this->error('金额不能大过订单金额和小于0');
}
$finance = new Finance();
$save = [
'fpnumber' => $this->pnumber,
'fcus_id' => $data['pcus_id']['cus_id'],
'fcus_name' => $data['pcus_id']['cus_duty'],
'sort' => '2',
'amount' => $djin_amount,
'fuid' => Session::get("user_id"),
'shoukuan_time' => $shoukuan_time,
];
$finance->data($save);
$result = $finance->allowField(true)->save();
if ($result) {
Db::name('finance_schedule')->insert([
'fs_fid' => $finance->fid,
'fs_img' => $fs_img,
'fs_remark' => $Request->param('remark')
]);
if ($data['pshengcwc'] == 1) {
$Purchase->allowField(['pshoudj','status'])->save(['pshoudj'=>'2', 'status'=>'4'],['pnumber'=>$this->pnumber]);
} else {
$Purchase->allowField(['pshoudj'])->save(['pshoudj'=>'2'],['pnumber'=>$this->pnumber]);
}
$this->success('操作成功',Url::build('product/color'));
} else {
$this->error('保存数据错误,请联系管理员');
}
} else {
$data = $Purchase->where('pnumber', $this->pnumber)->where('pshoudj','=',1)->where('status', '<', 5)->find();
if (empty($data)) {
$this->error('参数错误', Url::build('finance/index'));
}
$sum_amount = Db::name('purchase_orders')->where('ord_pnumber',$data['pnumber'])->sum('amount'); $youhuije = ($sum_amount * 1) - ($data['pamount'] * 1); $finance_model = new Finance();
$finance = $finance_model->where('fpnumber', $this->pnumber)->select();
$fcount = $finance_model->where('fpnumber', $this->pnumber)->count();
$fsum = $finance_model->where('fpnumber', $this->pnumber)->sum('amount');
$yukuan = sprintf("%.2f", $data['pamount']) - sprintf("%.2f", $fsum);
$assign = [
'title' => '销售收款',
'data' => $data,
'sum_amount' => $sum_amount,
'youhuije' => $youhuije,
'finance' => $finance,
'fcount' => $fcount,
'fsum' => $fsum,
'yukuan' => $yukuan,
];
$this->assign($assign);
return $this->fetch();
}
}
}