Activity.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\Ocr;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\DataGroup as DataGroupModel;
  6. use app\admin\model\ProcessOrder;
  7. use app\BaseController;
  8. use think\App;
  9. use think\facade\Db;
  10. class Activity extends Base
  11. {
  12. public $noble=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->noble=\think\facade\Config::get("noble");
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  21. $where = [["ga.is_del","=",0]];
  22. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  23. if($activity_name!=""){
  24. $where[]=['ga.activity_name',"like","%$activity_name%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. $where[]=['ga.status',"=",$status];
  29. }
  30. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  31. if($activity_code!==""){
  32. $where[]=['ga.activity_code',"like","%$activity_code%"];
  33. }
  34. $starttime = isset($this->post['starttime']) && $this->post['starttime']!=="" ? $this->post['starttime']:"";
  35. if($starttime!==""){
  36. $where[]=['ga.start',">=",date('Y-m-d H:i:s',strtotime($starttime))];
  37. }
  38. $endtime = isset($this->post['endtime']) && $this->post['endtime']!=="" ? $this->post['endtime']:"";
  39. if($endtime!==""){
  40. $where[]=['ga.end',"<",date('Y-m-d H:i:s',strtotime($endtime)+24*3600)];
  41. }
  42. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  43. if($platform_code!==""){
  44. $where[]=['ga.platform_code',"=",$platform_code];
  45. }
  46. $company_id = isset($this->post['company_id']) && $this->post['company_id'] !=="" ? trim($this->post['company_id']):"";
  47. if($company_id!==""){
  48. $where[]=['ga.company_id',"=",$company_id];
  49. }
  50. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']) :"";
  51. if($creater!=""){
  52. $where[]=['ga.creater',"like","%$creater%"];
  53. }
  54. $createrid = isset($this->post['createrid']) && $this->post['createrid'] !=="" ? trim($this->post['createrid']) :"";
  55. if($createrid!==""){
  56. $where[]=['ga.createrid',"=",$createrid];
  57. }
  58. // $role=$this->checkRole();
  59. // if(!empty($role['write'])){
  60. // $where[]=["ga.createrid","in",$role['write']];
  61. // }
  62. // $role = $this->checkDataShare();
  63. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['ga.createrid', 'in', $role[DataGroupModel::$type_全部]];
  64. $role = $this->checkDataShare();
  65. $hand = resign_hand_user($this->uid,0);
  66. if (!empty($role[DataGroupModel::$type_全部])) {
  67. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  68. $where[] = ['ga.createrid', 'in',$arr];
  69. }
  70. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  71. if ($company_name !== "") $where[] = ["ga.createrid", 'in', get_company_item_user_by_name($company_name)];
  72. $count = Db::name('good_activity')
  73. ->alias('ga')
  74. ->where($where)
  75. ->count();
  76. $total = ceil($count / $size);
  77. $page = $page >= $total ? $total : $page;
  78. $list = Db::name('good_activity')
  79. ->alias('ga')
  80. ->field('ga.*,p.platform_name,u.itemid')
  81. ->leftJoin("depart_user u", "u.uid=ga.createrid AND u.is_del=0")
  82. ->leftJoin("platform p", "p.id=ga.platform_code")
  83. ->append(['company_name','is_allow_update'])
  84. ->withAttr('company_name',function($val,$data){
  85. return implode('/', array_column(GetPart($data['itemid']), 'name'));
  86. })->withAttr('is_allow_update',function($val,$data)use($role){
  87. return (in_array($this->roleid, [1, 33]) || in_array($data['createrid'], $role[DataGroupModel::$type_可编辑])) ? 1 : 0;//是否具有编辑权限
  88. })->where($where)
  89. ->page($page,$size)
  90. ->order("ga.addtime desc")
  91. ->select()
  92. ->toArray();
  93. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  94. }
  95. public function linst()
  96. {
  97. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  98. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  99. $where = [["a.is_del", "=", 0]];
  100. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] !== "" ? trim($this->post['skuCode']) : "";
  101. if ($skuCode !== "") {
  102. $where[] = ['a.skuCode', "like", "%$skuCode%"];
  103. }
  104. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  105. if ($good_name != "") {
  106. $where[] = ['a.good_name', "like", "%$good_name%"];
  107. }
  108. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  109. if($activity_code!==""){
  110. $where[]=['a.activity_code',"like","%$activity_code%"];
  111. }
  112. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  113. if($status!==""){
  114. $where[]=['a.status',"=",$status];
  115. }
  116. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  117. if($activity_name!=""){
  118. $where[]=['b.activity_name',"like","%$activity_name%"];
  119. }
  120. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  121. if($platform_code!=""){
  122. $where[]=['b.platform_code',"=",$platform_code];
  123. }
  124. $count = Db::name('activity_info')->alias("a")
  125. ->join("good_activity b","b.activity_code=a.activity_code","left")
  126. ->join("platform c","c.id=b.platform_code","left")
  127. ->where($where)->count();
  128. $total = ceil($count / $size);
  129. $page = $page >= $total ? $total : $page;
  130. $list = Db::name('activity_info')->alias("a")
  131. ->join("good_activity b","b.activity_code=a.activity_code","left")
  132. ->join("platform c","c.id=b.platform_code","left")
  133. ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.activity_name,c.platform_name,c.platform_code
  134. ")->select();
  135. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  136. }
  137. public function zilist(){
  138. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  139. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  140. $where = [["a.is_del", "=", 0]];
  141. $spuCode = isset($this->post['spuCode']) && $this->post['spuCode'] !== "" ? trim($this->post['spuCode']) : "";
  142. if ($spuCode != "") $where[] = ['a.spuCode', "like", "%$spuCode%"];
  143. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  144. if ($good_name != "") $where[] = ['a.good_name', "like", "%$good_name%"];
  145. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  146. if($status!="") $where[]=['a.status',"=",$status];
  147. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  148. if ($supplierNo != "") $where[] = ['a.supplierNo', "like", "%$supplierNo%"];
  149. $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
  150. if ($name != "") $where[] = ['b.name', "like", "%$name%"];
  151. $start = isset($this->post['start']) && $this->post['start'] !== "" ? trim($this->post['start']) : "";
  152. if ($start != "") $where[] = ['a.addtime', ">=", $start];
  153. $end = isset($this->post['end']) && $this->post['end'] !== "" ? trim($this->post['end']) : "";
  154. if ($end != "") $where[] = ['a.addtime', "<=", $end.' 23:59:59'];
  155. $count = Db::name('good_zixun')
  156. ->alias("a")
  157. ->join("supplier b","b.code=a.supplierNo","left")
  158. ->where($where)
  159. ->count();
  160. $total = ceil($count / $size);
  161. $page = $page >= $total ? $total : $page;
  162. $list = Db::name('good_zixun')
  163. ->alias("a")
  164. ->join("supplier b","b.code=a.supplierNo","left")
  165. ->where($where)
  166. ->page($page,$size)
  167. ->order("a.addtime desc")
  168. ->field("a.*,b.name")
  169. ->select()
  170. ->toArray();
  171. //halt($list);
  172. $data=[];
  173. //分类/规格/品牌 等字段取出对应文字
  174. foreach ($list as $value){
  175. $value['cat_info']= made($value['cat_id'],[]);
  176. $platform = Db::name("platform")->where(["id"=>$value['platform_id']])->find();
  177. $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
  178. $value['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en']:"";
  179. $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  180. $value['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
  181. $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
  182. $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  183. $unit = Db::name("unit")->where(["id"=>$value['good_unit']])->find();
  184. $value['unit'] =isset($unit['unit'])?$unit['unit']:"";
  185. $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  186. $value['company'] = isset($company['company'])?$company['company']:"";
  187. // $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  188. // $value['company'] = isset($company['company'])?$company['company']:"";
  189. $value['stock_total'] = Db::name("good_stock")->where(['spuCode'=>$value['spuCode'],"is_del"=>0])->sum("usable_stock");
  190. // $value['status']=$value['exam_status'];
  191. // $value['exclusive']=makeExcluse($value['is_exclusive']);
  192. $value['good_info_img']=$value['good_info_img'];
  193. $value['good_img']=$value['good_img'];
  194. $spec = Db::name("good_spec")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->select()->toArray();
  195. $speclist=[];
  196. if(!empty($spec)){
  197. foreach ($spec as $val){
  198. $temp=[];
  199. $temp['spec_id']=$val['spec_id'];
  200. $temp['spec_value_id']=$val['spec_value_id'];
  201. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  202. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  203. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  204. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  205. $speclist[]=$temp;
  206. }
  207. }
  208. $value['specinfo']=$speclist;
  209. $value['noble_name']=isset($value['noble_metal'])&&$value['noble_metal']!=0?$this->noble[$value['noble_metal']] :"";
  210. $data[]=$value;
  211. }
  212. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  213. }
  214. //创建活动
  215. public function create(){
  216. $activity_name = isset($this->post['activity_name'])&&$this->post['activity_name']!="" ? trim($this->post['activity_name']):"";
  217. if($activity_name==""){
  218. return error_show(1004,"参数activity_name不能为空");
  219. }
  220. $platform_code = isset($this->post['platform_code'])&&$this->post['platform_code']!="" ? trim($this->post['platform_code']):"";
  221. if($platform_code==""){
  222. return error_show(1004,"参数platform_code不能为空");
  223. }
  224. $company_id = isset($this->post['company_id'])&&$this->post['company_id']!="" ? trim($this->post['company_id']):"";
  225. if($company_id==""){
  226. return error_show(1004,"参数company_id不能为空");
  227. }
  228. $activity_start = isset($this->post['activity_start'])&&$this->post['activity_start']!="" ? $this->post['activity_start']:"";
  229. if($activity_start==""){
  230. return error_show(1004,"参数activity_start不能为空");
  231. }
  232. $activity_end = isset($this->post['activity_end'])&&$this->post['activity_end']!="" ? $this->post['activity_end']:"";
  233. if($activity_end==""){
  234. return error_show(1004,"参数activity_end不能为空");
  235. }
  236. $activity_desc = isset($this->post['activity_desc'])&&$this->post['activity_desc']!="" ?
  237. trim($this->post['activity_desc']):"";
  238. if($activity_desc==""){
  239. return error_show(1004,"参数activity_desc不能为空");
  240. }
  241. $is_stock = isset($this->post['is_stock'])&&$this->post['is_stock']!=="" ?
  242. intval($this->post['is_stock']):"";
  243. if($is_stock===""){
  244. return error_show(1004,"参数is_stock不能为空");
  245. }
  246. $good_list= isset($this->post['good_list'])&&$this->post['good_list']!=="" ?$this->post['good_list']:"";
  247. if($good_list==""){
  248. return error_show(1004,"参数good_list不能为空");
  249. }
  250. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  251. if($token==''){
  252. return error_show(105,"参数token不能为空");
  253. }
  254. $user =GetUserInfo($token);
  255. if(empty($user)||$user['code']!=0){
  256. return error_show(102,"用户数据不存在");
  257. }
  258. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  259. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  260. $activity_code = makeNo("AT");
  261. Db::startTrans();
  262. try{
  263. $data=[
  264. "activity_name"=>$activity_name,
  265. "activity_code"=>$activity_code,
  266. "is_stock"=>$is_stock,
  267. "start"=>$activity_start,
  268. "end"=>$activity_end,
  269. "platform_code"=>$platform_code,
  270. "activity_desc"=>$activity_desc,
  271. "company_id"=>$company_id,
  272. "creater"=>$creater,
  273. "createrid"=>$createrid,
  274. "status"=>1,
  275. "is_del"=>0,
  276. "addtime"=>date("Y-m-d H:i:s"),
  277. "updatetime"=>date("Y-m-d H:i:s")
  278. ];
  279. $inset = Db::name("good_activity")->insertGetId($data);
  280. if($inset>0){
  281. if($good_list!=""&&!empty($good_list)){
  282. $temp=[];
  283. $skuCode =array_column($good_list,"skuCode");
  284. $ist =Db::name("good_activity")->alias("a")
  285. ->leftJoin("activity_info b","a.activity_code = b.activity_code and b.is_del=0")
  286. ->where(["b.skuCode"=>$skuCode,"a.is_del"=>0])->where([["a.status","<>",8]])->where(function
  287. ($query)use($activity_end,$activity_start){
  288. return $query->whereOr([[["start","<=",$activity_start],["end",">=",$activity_end]],["start|end","between",[$activity_start, $activity_end]]]);
  289. })->select()->toArray();
  290. if(!empty($ist)){
  291. Db::rollback();
  292. $limit=[];
  293. foreach ($ist as $value){
  294. $limit[]="活动 ".$value['activity_name']. ' 已存在商品 '. $value['good_name'];
  295. }
  296. return app_show(1007,'活动商品已存在', $limit);
  297. }
  298. foreach ($good_list as $value){
  299. $lemp=[];
  300. $good = Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")
  301. ->where(["skuCode"=>$value['skuCode'],"a.is_del"=>0])->find();
  302. $lemp['skuCode']=$value['skuCode'];
  303. $lemp['good_name']=isset($good['good_name']) ?$good['good_name']:"" ;
  304. $lemp['activity_code']=$activity_code;
  305. $lemp['activity_stock']=$value['activity_stock'];
  306. $lemp['moq_num']=0;
  307. $lemp['cost_price']=0;
  308. $lemp['sale_price']=0;
  309. $lemp['activity_price']=0;
  310. $lemp['status']=0;
  311. $lemp['remark']="";
  312. $lemp['is_del']=0;
  313. $lemp['addtime']=date("Y-m-d H:i:s");
  314. $lemp['updatetime']=date("Y-m-d H:i:s");
  315. $temp[]=$lemp;
  316. //当是库存品的时候校验可用库存数,不满足的时候不能添加
  317. if ($is_stock == 1) {
  318. $usable_stock = Db::name('good_stock')
  319. ->where(['spuCode' => $good['spuCode'], 'is_del' => 0])
  320. ->sum('usable_stock');
  321. if ($value['activity_stock'] > $usable_stock) {
  322. Db::rollback();
  323. // throw new \Exception($lemp['good_name'] . '的库存只有' . $usable_stock);
  324. return app_show(1005,'库存不足',[$lemp['good_name']. '的库存只有' . $usable_stock]);
  325. }
  326. }
  327. }
  328. $iall=Db::name("activity_info")->insertAll($temp);
  329. if($iall==0){
  330. Db::rollback();
  331. return error_show(1005,"新建失败");
  332. }
  333. }
  334. $stn = ["order_code" => $activity_code, "status" => 0, "action_remark" => '', "action_type" => "create"];
  335. ActionLog::logAdd($this->post['token'], $stn, "HD", $data['status'], $data);
  336. $process = ["order_code" => $activity_code, "order_id" => $inset, "order_status" => $data['status'], "order_type" => 'HD', "before_status" => 0, 'holder_id' => $data['createrid']];
  337. ProcessOrder::AddProcess($this->post['token'], $process);
  338. Db::commit();
  339. return app_show(0,"新建成功");
  340. }
  341. }catch (\Exception $e){
  342. Db::rollback();
  343. return error_show(1005,$e->getMessage());
  344. }
  345. }
  346. //采购定价
  347. public function cost(){
  348. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  349. if($good_list==""){
  350. return error_show(1004,"参数good_list不能为空");
  351. }
  352. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  353. if($activity_code==""){
  354. return error_show(1004,"参数activity_code不能为空");
  355. }
  356. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  357. if($activity==false){
  358. return error_show(1004,"未找到活动数据");
  359. }
  360. Db::startTrans();
  361. try{
  362. foreach ($good_list as $value){
  363. $temp=[];
  364. $ladd = Db::name("good_ladder")->where(["skuCode"=>$value['skuCode'],"is_del"=>0])->where
  365. ([["min_num","<=",$value['moq_num']]])->order("min_num desc")->find();
  366. $temp["id"]=$value['id'];
  367. $temp["cost_price"]=$value['cost_price'];
  368. $temp["moq_num"]=$value['moq_num'];
  369. $temp["sale_price"]=isset($ladd['sale_price'])?$ladd['sale_price']:0;
  370. $temp["updatetime"]=date("Y-m-d H:i:s");
  371. $up=Db::name("activity_info")->save($temp);
  372. if($up==false){
  373. Db::rollback();
  374. return error_show(1004,"商品数据更新失败");
  375. }
  376. }
  377. $old_activity_status = $activity['status'];
  378. $activity['status']=2;
  379. $activity['updatetime']=date("Y-m-d H:i:s");
  380. $act = Db::name("good_activity")->save($activity);
  381. if($act){
  382. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  383. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  384. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  385. ProcessOrder::AddProcess($this->post['token'], $process);
  386. Db::commit();
  387. return error_show(0,"商品数据更新成功");
  388. }else{
  389. Db::rollback();
  390. return error_show(1004,"商品数据更新失败");
  391. }
  392. }catch (\Exception $e){
  393. Db::rollback();
  394. return error_show(1004,$e->getMessage());
  395. }
  396. }
  397. //财务定价
  398. public function activity(){
  399. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  400. if($good_list==""){
  401. return error_show(1004,"参数good_list不能为空");
  402. }
  403. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  404. if($activity_code==""){
  405. return error_show(1004,"参数activity_code不能为空");
  406. }
  407. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  408. if($activity==false){
  409. return error_show(1004,"未找到活动数据");
  410. }
  411. Db::startTrans();
  412. try{
  413. foreach ($good_list as $value){
  414. $temp=[];
  415. $temp["id"]=$value['id'];
  416. $temp["activity_price"]=$value['activity_price'];
  417. $temp["updatetime"]=date("Y-m-d H:i:s");
  418. $up=Db::name("activity_info")->save($temp);
  419. if($up==false){
  420. Db::rollback();
  421. return error_show(1004,"商品数据更新失败");
  422. }
  423. }
  424. $old_activity_status = $activity['status'];
  425. $activity['status']=3;
  426. $activity['updatetime']=date("Y-m-d H:i:s");
  427. $act = Db::name("good_activity")->save($activity);
  428. if($act){
  429. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  430. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  431. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  432. ProcessOrder::AddProcess($this->post['token'], $process);
  433. Db::commit();
  434. return error_show(0,"商品数据更新成功");
  435. }else{
  436. Db::rollback();
  437. return error_show(1004,"商品数据更新失败");
  438. }
  439. }catch (\Exception $e){
  440. Db::rollback();
  441. return error_show(1004,$e->getMessage());
  442. }
  443. }
  444. //财务审核定价
  445. public function exam(){
  446. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  447. if($good_list==""){
  448. return error_show(1004,"参数good_list不能为空");
  449. }
  450. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  451. if($activity_code==""){
  452. return error_show(1004,"参数activity_code不能为空");
  453. }
  454. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  455. if($activity==false){
  456. return error_show(1004,"未找到活动数据");
  457. }
  458. Db::startTrans();
  459. try{
  460. foreach ($good_list as $value){
  461. $temp=[];
  462. $temp["id"]=$value['id'];
  463. $temp["status"]=$value['status'];
  464. $temp["remark"]=$value['remark'];
  465. $temp["updatetime"]=date("Y-m-d H:i:s");
  466. $up=Db::name("activity_info")->save($temp);
  467. if($up==false){
  468. Db::rollback();
  469. return error_show(1004,"商品数据更新失败");
  470. }
  471. }
  472. $old_activity_status = $activity['status'];
  473. $activity['status']=4;
  474. $activity['updatetime']=date("Y-m-d H:i:s");
  475. $act = Db::name("good_activity")->save($activity);
  476. if($act){
  477. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  478. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  479. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  480. ProcessOrder::AddProcess($this->post['token'], $process);
  481. Db::commit();
  482. return error_show(0,"商品数据审核成功");
  483. }else{
  484. Db::rollback();
  485. return error_show(1004,"商品数据审核失败");
  486. }
  487. }catch (\Exception $e){
  488. Db::rollback();
  489. return error_show(1004,$e->getMessage());
  490. }
  491. }
  492. //活动详情
  493. public function info()
  494. {
  495. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !== "" ? trim($this->post['activity_code']) : "";
  496. if ($activity_code == "") {
  497. return error_show(1004, "参数activity_code不能为空");
  498. }
  499. $activity = Db::name("good_activity")
  500. ->alias('ga')
  501. ->field('ga.*,p.platform_name')
  502. ->leftJoin('platform p', 'p.id=ga.platform_code AND p.is_del=0')
  503. ->where(["ga.activity_code" => $activity_code, "ga.is_del" => 0])
  504. ->find();
  505. if ($activity == false) {
  506. return error_show(1004, "未找到活动数据");
  507. }
  508. $activity_info = Db::name("activity_info")
  509. ->where(["activity_code" => $activity_code, "is_del" => 0])
  510. ->select()
  511. ->toArray();
  512. if (!empty($activity_info)) {
  513. //状态不是(0待产品部审核,1待采购定价),才会有起订量,才可以考虑计算售价
  514. if ($activity['status'] > 1) {
  515. //判断是否是启用实时金价的贵金属
  516. $rs = Db::name('good_platform')
  517. ->alias('a')
  518. ->join('good g', 'g.spuCode=a.spuCode')
  519. ->join('good_nake c', 'c.spuCode=a.spuCode')
  520. ->whereIn('a.skuCode', array_column($activity_info, 'skuCode'))
  521. ->column('g.cat_id,g.is_gold_price,g.demo_fee,g.open_fee,g.weight,c.cost_fee,c.package_fee,c.mark_fee,c.cert_fee,g.noble_metal,c.nake_fee,c.delivery_fee', 'a.skuCode');
  522. foreach ($activity_info as &$value) {
  523. //如果启用了实时金价,且是贵金属
  524. if (isset($rs[$value['skuCode']])) {
  525. $top_cat = made($rs[$value['skuCode']]['cat_id']);
  526. if (isset($top_cat[0]['id']) && $top_cat[0]['id'] == 6 && $rs[$value['skuCode']]['is_gold_price'] == 1) {
  527. $gold_price = Db::name("gold_price1")->where(["type" => $rs[$value['skuCode']]['noble_metal'], "is_del" => 0, "status" => 1])->order("addtime desc")->value('price');
  528. $value['sale_price'] = round($rs[$value['skuCode']]['demo_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['open_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['weight'] * $gold_price + $rs[$value['skuCode']]['cost_fee'] * $rs[$value['skuCode']]['weight'] + $rs[$value['skuCode']]['package_fee'] + $rs[$value['skuCode']]['mark_fee'] + $rs[$value['skuCode']]['cert_fee'] + $rs[$value['skuCode']]['nake_fee'] + $rs[$value['skuCode']]['delivery_fee'], 2);
  529. }
  530. }
  531. }
  532. }
  533. $activity['info'] = $activity_info;
  534. } else $activity['info'] = [];
  535. // $activity['info'] = empty($activity_info) ? [] : $activity_info;
  536. return app_show(0, "获取成功", $activity);
  537. }
  538. //修改活动状态
  539. public function actstatus(){
  540. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  541. if($activity_code==""){
  542. return error_show(1004,"参数activity_code不能为空");
  543. }
  544. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  545. if($activity==false){
  546. return error_show(1004,"未找到活动数据");
  547. }
  548. $status = isset($this->post['status'])&&$this->post['status']!=="" ?intval($this->post['status']):"";
  549. if($status==""){
  550. return error_show(1004,"参数status不能为空");
  551. }
  552. $remark =isset($tihs->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  553. if($remark=="" &&$status==8){
  554. return error_show(1004,"参数remark不能为空");
  555. }
  556. $old_status = $activity['status'];
  557. $activity['status']=$status;
  558. $activity['remark']=$remark;
  559. $activity['updatetime']=date("Y-m-d H:i:s");
  560. $up = Db::name("good_activity")->save($activity);
  561. if($up){
  562. $stn = ["order_code" => $activity_code, "status" => $old_status, "action_remark" => '', "action_type" => "status"];
  563. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  564. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_status, 'holder_id' => $activity['createrid']];
  565. ProcessOrder::AddProcess($this->post['token'], $process);
  566. return app_show(0,"修改成功");
  567. }else{
  568. return error_show(1004,"修改失败");
  569. }
  570. }
  571. public function query(){
  572. $skuCode= isset($this->post['skuCode'])&&$this->post['skuCode']!=""?trim($this->post['skuCode']):"";
  573. if($skuCode==""){
  574. return error_show(1004,"参数skuCode不能为空");
  575. }
  576. $where=['a.is_del'=>0,"b.is_del"=>0,"a.status"=>6,"b.status"=>1,"b.skuCode"=>$skuCode];
  577. $condition=['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6];
  578. $company = isset($this->post['companyNo'])&&$this->post['companyNo']!=""?trim($this->post['companyNo']):"";
  579. if($company!=""){
  580. $where['a.company_id'] = $company;
  581. $condition['companyNo'] = $company;
  582. }
  583. $plat =isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id'])
  584. :"";
  585. if($plat!==""){
  586. $where['a.platform_code'] = $plat;
  587. $condition['platform_code'] = $plat;
  588. }
  589. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  590. ->where($condition)->select()->toArray();
  591. $goodlist=[];
  592. if(!empty($good)){
  593. foreach ($good as $value){
  594. $temp=[];
  595. $stock =Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->sum('usable_stock');
  596. //如果是非库存商品,库存数改为最大(即将库存设为无限)
  597. $temp['stock_num'] = $value['is_stock'] == 1 ? intval($stock) : 9999;
  598. $temp['skuCode']=$value['skuCode'];
  599. $temp['spuCode']=$value['spuCode'];
  600. $temp['is_stock']=$value['is_stock'];
  601. $temp['good_name']=$value['good_name'];
  602. $temp['good_img']=$value['good_img'];
  603. $temp['good_info_img']=$value['good_info_img'];
  604. $temp['noble_metal']=$value['noble_metal'];
  605. $temp['noble_name']=$value['noble_metal']!=0?$this->noble[$value['noble_metal']]:"";
  606. $goodlist[]=$temp;
  607. }
  608. }
  609. $list = Db::name('good_activity')->alias("a")
  610. ->join("activity_info b","b.activity_code=a.activity_code","left")
  611. ->where($where)->field('b.id,a.activity_name,a.activity_code,a.is_stock,a.start,a.end,b.skuCode,b.good_name,b.activity_stock,
  612. b.cost_price,b.moq_num,b.sale_price,b.activity_price')->select()->toArray();
  613. return app_show(0,"获取成功",["act"=>$list,"good"=>$goodlist]);
  614. }
  615. //新增库存
  616. public function editStock(){
  617. $actCode=isset($this->post['actCode'])&&$this->post['actCode']!='' ? trim($this->post['actCode']):"";
  618. if($actCode==''){
  619. return error_show(1004,"参数 actCode 不能为空");
  620. }
  621. $skuCode=isset($this->post['skuCode'])&&$this->post['skuCode']!='' ? trim($this->post['skuCode']):"";
  622. if($skuCode==''){
  623. return error_show(1004,"参数 skuCode 不能为空");
  624. }
  625. $act_stock=isset($this->post['act_stock'])&&$this->post['act_stock']!='' ? intval($this->post['act_stock']):"";
  626. if($act_stock==''){
  627. return error_show(1004,"参数 act_stock 不能为空");
  628. }
  629. $act =Db::name("good_activity")->where(["activity_code"=>$actCode,"is_del"=>0])->findOrEmpty();
  630. if(empty($act)){
  631. return error_show(1005,"活动信息不存在");
  632. }
  633. if(!in_array($act['status'],[4,5,6]))return error_show(1005,"活动暂时不能修改");
  634. $goodinfo=Db::name("activity_info")->where(["activity_code"=>$actCode,"skuCode"=>$skuCode,"is_del"=>0])
  635. ->findOrEmpty();
  636. if(empty($goodinfo)){
  637. return error_show(1005,"活动商品信息不存在");
  638. }
  639. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  640. ->where(['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6])->findOrEmpty();
  641. if(empty($good)){
  642. return error_show(1005,"商品信息不存在或已下架");
  643. }
  644. if($good['is_stock']==1){
  645. $stock =Db::name("good_stock")->where(["spuCode"=>$good['spuCode'],"is_del"=>0])->sum('usable_stock');
  646. if($act_stock+$goodinfo['activity_stock']> $stock){
  647. return error_show(1005,"商品库存不足");
  648. }
  649. }
  650. $update =["activity_stock"=>$act_stock+$goodinfo['activity_stock'],"updatetime"=>date("Y-m-d H:i:s")];
  651. $up=Db::name("activity_info")->where($goodinfo)->update($update);
  652. return $up ? app_show(0,"活动库存增加成功"):error_show(1006,"活动库存增加失败");
  653. }
  654. }