Activity.php 40 KB

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