<?php
namespace app\warehouse\admin;
use app\system\admin\Admin;
use app\warehouse\model\Purchase as PurchaseModel;
use app\warehouse\model\Stock as StockModel;
use app\warehouse\model\Arrival as ArrivalModel;
use app\product\model\Product as ProductModel;
use think\Db;
class Arrival extends Admin
{
protected $hisiModel = 'Arrival'
public function add()
{
$sn = create_order_sn();
$this->assign('sn', $sn);
$admins = Db::name('system_user')
->field('id,nick')
->select();
$this->assign('admins', $admins);
$shops = Db::name('shop')->field('id as value,name as label')->select();
foreach ($shops as $key => $value) {
$shops[$key]['children'] = Db::name('warehouse')->field('id as value, name as label')->where('shop_id',$value['value'])->where('type',0)->select();
}
$this->assign('warehouses', $shops);
$products = ProductModel::field('*,supplier_id as supplier_text,unit_id as unit_text, id as product_id')->where('status',1)->all();
$this->assign('products',$products);
$chukuType = Db::name('directtype')->order('sort asc')->select();
$this->assign('chukuType', $chukuType);
return $this->fetch('form');
}
public function edit()
{
$id = $this->request->param('id','');
if($id == ''){
return $this->error('数据不存在');
}
$formData = ArrivalModel::get($id);
$this->assign('formData',$formData);
$tableDate = $formData->product_json;
$this->assign('tableDate',json_decode($tableDate));
$admins = Db::name('system_user')
->field('id,nick')
->select();
$this->assign('admins', $admins);
$shops = Db::name('shop')->field('id as value,name as label')->select();
foreach ($shops as $key => $value) {
$shops[$key]['children'] = Db::name('warehouse')->field('id as value, name as label')->where('shop_id',$value['value'])->select();
}
$this->assign('warehouses', $shops);
$products = ProductModel::field('*,supplier_id as supplier_text,unit_id as unit_text, id as product_id')->where('status',1)->all();
$this->assign('products',$products);
$chukuType = Db::name('directtype')->order('sort asc')->select();
$this->assign('chukuType', $chukuType);
if(!is_empty($this->request->param('xx')) && $this->request->param('xx') == 2){
$formData['shop'] = Db::name('shop')->where('id',$formData['shop_id'])->value('name');
$formData['warehouse'] = Db::name('warehouse')->where('id',$formData['warehouse_id'])->value('name');
$this->assign('formData',$formData);
$this->view->engine->layout(false);
return $this->fetch('dayin');
}
return $this->fetch('check');
}
public function index()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$status = $this->request->param('status/d', '');
$start = $this->request->param('start', '2000-01-01');
$end = $this->request->param('end', date('Y-m-d H:i:s',time()));
if(empty($start)){
$start = '2000-01-01';
}
if(empty($end)){
$end = date('Y-m-d H:i:s',time());
}
if(!empty($status)){
$where['status'] = $status;
}
$where['shop_id'] = session('shop_id');
if($this->request->param('operation') == 2){
$xxx = ArrivalModel::where($where)
->field('*, admin_id as admin_id_text,shop_id as shop_name,warehouse_id as warehouse_name,out_type as out_type_text')
->whereTime('createtime', [strtotime($start), strtotime($end)])
->order('id desc')
->select();
$data['data'] = [];
$o = 0;
foreach ($xxx as $key => $value) {
$arr = json_decode($value['product_json'],true);
foreach ($arr as $k => $v) {
$data['data'][$o]['sn'] = $value['sn'];
$data['data'][$o]['admin_id_text'] = $value['admin_id_text'];
$data['data'][$o]['shop_name'] = $value['shop_name'];
$data['data'][$o]['warehouse_name'] = $value['warehouse_name'];
$data['data'][$o]['out_type_text'] = $value['out_type_text'];
$data['data'][$o]['bak'] = $value['bak'];
switch ($value['status']) {
case '1':
$data['data'][$o]['status'] = '待审核';
break;
case '2':
$data['data'][$o]['status'] = '通过';
break;
case '3':
$data['data'][$o]['status'] = '退回';
break;
}
$data['data'][$o]['createtime'] = $value['createtime'];
$data['data'][$o]['name'] = $v['name'];
$data['data'][$o]['specs'] = $v['specs'];
$data['data'][$o]['unit_text'] = $v['unit_text'];
$data['data'][$o]['supplier_text'] = $v['supplier_text'];
$data['data'][$o]['num'] = $v['num'];
$data['data'][$o]['pbak'] = isset($v['bak']) ? $v['bak'] :'';
$o++;
}
}
}else{
$data['data'] = ArrivalModel::where($where)
->field('*, admin_id as admin_id_text,shop_id as shop_name,warehouse_id as warehouse_name,out_type as out_type_text')
->whereTime('createtime', [strtotime($start), strtotime($end)])
->page($page)
->limit($limit)
->order('id desc')
->select();
}
$data['count'] = ArrivalModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
public function addDo()
{
$postData = $this->request->post();
if($postData['admin_id'] == ''){
return $this->axiosMsg('操作员为必填项',false);
}
if($postData['warehouse_id'] == ''){
return $this->axiosMsg('仓库为必填项',false);
}
if(empty($postData['product'])){
return $this->axiosMsg('没有产品提交',false);
}
$sw = $postData['warehouse_id'];
unset($postData['warehouse_id']);
$postData['shop_id'] = $sw[0];
$postData['warehouse_id'] = $sw[1];
$postData['product_json'] = json_encode($postData['product']);
if(ArrivalModel::create($postData) === false){
return $this->axiosMsg('创建失败',false);
}
return $this->axiosMsg('创建成功!',true);
}
public function editDo()
{
$postData = $this->request->post();
if($postData['admin_id'] == ''){
return $this->axiosMsg('采购员为必填项',false);
}
if($postData['warehouse_id'] == ''){
return $this->axiosMsg('仓库为必填项',false);
}
if(empty($postData['product'])){
return $this->axiosMsg('没有产品提交',false);
}
$sw = $postData['warehouse_id'];
unset($postData['warehouse_id']);
$postData['shop_id'] = $sw[0];
$postData['warehouse_id'] = $sw[1];
$products = $postData['product'];
$postData['product_json'] = json_encode($postData['product']);
Db::startTrans();
try {
if($postData['status'] == 2){
$ins = [];
$StockModel = new StockModel;
foreach($products as $key=>$value){
$begin_num = $StockModel->getBeginNum($postData['warehouse_id'],$value['product_id']);
$begin_price = $StockModel->getBeginPrice($postData['warehouse_id'],$value['product_id']);
$end_price = $StockModel->getEndPrice($postData['warehouse_id'],$value['product_id'],$value['num'],$value['price1']);
$ins[$key]['product_id'] = $value['product_id'];
$ins[$key]['product_num'] = $value['num'];
$ins[$key]['product_name'] = $value['name'];
$ins[$key]['shop_id'] = $postData['shop_id'];
$ins[$key]['sn'] = $postData['sn'];
$ins[$key]['type'] = '入库';
$ins[$key]['detailed_type'] = 8;
$ins[$key]['warehouse_id'] = $postData['warehouse_id'];
$ins[$key]['createtime'] = time();
$ins[$key]['status'] = 1;
$ins[$key]['bak'] = isset($value['bak']) ? $value['bak'] : '';
$ins[$key]['updatetime'] = time();
$ins[$key]['unit_price'] = $value['num'] * $value['price1'];
$ins[$key]['price'] = $value['num'] * $value['price1'];
$ins[$key]['begin_num'] = $begin_num;
$ins[$key]['end_num'] = $begin_num + $value['num'];
$ins[$key]['begin_price'] = $begin_price;
$ins[$key]['end_price'] = $end_price;
}
$StockModel->saveAll($ins);
}
ArrivalModel::update($postData);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return $this->axiosMsg($e->getMessage(), false);
}
return $this->axiosMsg('保存成功!',true);
}
public function check()
{
return $this->fetch();
}
public function look()
{
$type = $this->request->param('type',1);
$id = $this->request->param('id','');
if($id == ''){
$this->error('采购单不存在!');
}
$Direct = ArrivalModel::get($id);
$products = json_decode($Direct->product_json,true);
$zong = 0;
foreach ($products as $key => $value) {
$products[$key]['jine'] = $value['price1'] * $value['num'];
$zong += $products[$key]['jine'];
}
$this->assign('zong', $zong);
$this->assign('products', $products);
$this->assign('id',$id);
$this->assign('type',$type);
return $this->fetch();
}
}
?>