CompanyCatProfit.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\facade\Validate;
  5. //公司分类毛利
  6. class CompanyCatProfit extends Base
  7. {
  8. //列表
  9. public function getList()
  10. {
  11. $param = $this->request->only(['page' => 1, 'size' => 10, 'companyNo' => '', 'cat_name' => '', 'pid' => 0], 'post', 'trim');
  12. if ($param['companyNo'] == '') return json_show(1004, '业务公司编号不能为空');
  13. $where = [['a.is_del', '=', 0], ['a.pid', '=', $param['pid']]];
  14. if ($param['cat_name'] !== '') $where[] = ['a.cat_name', 'like', '%' . $param['cat_name'] . '%'];
  15. $count = Db::name('cat')
  16. ->alias('a')
  17. ->leftJoin('company_cat_profit b', 'b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'')
  18. ->where($where)
  19. ->count('a.id');
  20. $list = Db::name('cat')
  21. ->alias('a')
  22. ->field('a.id cat_id,a.cat_name,a.level,b.rate,b.order_rate,b.money_rate,b.low_rate,b.sale_rate,b.status')
  23. ->leftJoin('company_cat_profit b', 'b.is_del=0 AND b.cat_id=a.id AND companyNo=\'' . $param['companyNo'] . '\'')
  24. ->where($where)
  25. ->withAttr('status', function ($val) {
  26. return (int)$val;
  27. })
  28. ->order('a.id', 'desc')
  29. ->page($param['page'], $param['size'])
  30. ->select()
  31. ->toArray();
  32. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  33. }
  34. //设置毛利率相关
  35. public function set()
  36. {
  37. $param = $this->request->only(['cat_id', 'companyNo', 'rate', 'order_rate', 'money_rate', 'low_rate', 'sale_rate'], 'post', 'trim');
  38. $val = Validate::rule([
  39. 'cat_id|分类id' => 'require|number|gt:0',
  40. 'companyNo|公司编码' => 'require|length:18',
  41. 'rate|主管利率' => 'require|float|between:0,100',
  42. 'order_rate|预算成本' => 'require|float|between:0,100',
  43. 'money_rate|财务利率' => 'require|float|between:0,100',
  44. 'low_rate|最低利率' => 'require|float|between:0,100',
  45. 'sale_rate|成本售价' => 'require|float|between:0,100',
  46. ]);
  47. if (!$val->check($param)) return json_show(1004, $val->getError());
  48. $tmp = Db::name('company_cat_profit')
  49. ->field('id')
  50. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  51. ->findOrEmpty();
  52. $date = date('Y-m-d H:i:s');
  53. if (empty($tmp)) {
  54. $rs = Db::name('company_cat_profit')
  55. ->insert([
  56. 'companyNo' => $param['companyNo'],
  57. 'cat_id' => $param['cat_id'],
  58. 'rate' => $param['rate'],
  59. 'order_rate' => $param['order_rate'],
  60. 'money_rate' => $param['money_rate'],
  61. 'low_rate' => $param['low_rate'],
  62. 'sale_rate' => $param['sale_rate'],
  63. 'status' => 0,
  64. 'is_del' => 0,
  65. 'addtime' => $date,
  66. 'updatetime' => $date
  67. ]);
  68. } else {
  69. $rs = Db::name('company_cat_profit')
  70. ->where(['is_del' => 0, 'id' => $tmp['id']])
  71. ->update([
  72. 'rate' => $param['rate'],
  73. 'order_rate' => $param['order_rate'],
  74. 'money_rate' => $param['money_rate'],
  75. 'low_rate' => $param['low_rate'],
  76. 'sale_rate' => $param['sale_rate'],
  77. 'updatetime' => $date
  78. ]);
  79. }
  80. return $rs ? json_show(0, '设置成功') : json_show(1004, '设置失败');
  81. }
  82. //启禁用
  83. public function status()
  84. {
  85. $param = $this->request->only(['cat_id', 'companyNo', 'status'], 'post', 'trim');
  86. $val = Validate::rule([
  87. 'cat_id|分类id' => 'require|number|gt:0',
  88. 'companyNo|公司编码' => 'require|length:18',
  89. 'status|状态' => 'require|number|in:0,1',
  90. ]);
  91. if ($val->check($param) == false) return json_show(1004, $val->getError());
  92. $cat = Db::name('cat')
  93. ->field('id,level')
  94. ->where(['id' => $param['cat_id'], 'is_del' => 0])
  95. ->findOrEmpty();
  96. if (empty($cat)) return json_show(1005, '该分类不存在');
  97. $tmp = Db::name('company_cat_profit')
  98. ->field('id,status')
  99. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  100. ->findOrEmpty();
  101. if (empty($tmp)) return json_show(1004, '该分类尚未设置毛利,无法操作');
  102. if ($tmp['status'] == $param['status']) return json_show(1004, '重复操作');
  103. //要操作的分类id集合
  104. switch ($cat['level']) {
  105. case 1:
  106. $first_ids = Db::name('cat')
  107. ->where(['is_del' => 0, 'pid' => $param['cat_id']])
  108. ->column('id');
  109. $second_ids = Db::name('cat')
  110. ->where('is_del', 0)
  111. ->whereIn('pid', $first_ids)
  112. ->column('id');
  113. $cat_ids = array_merge($first_ids, $second_ids);
  114. break;
  115. case 2:
  116. $cat_ids = Db::name('cat')
  117. ->where(['is_del' => 0, 'pid' => $param['cat_id']])
  118. ->column('id');
  119. break;
  120. }
  121. $cat_ids[] = $param['cat_id'];
  122. $rs = Db::name('company_cat_profit')
  123. ->where(['is_del' => 0, 'companyNo' => $param['companyNo']])
  124. ->whereIn('cat_id', $cat_ids)
  125. ->where('status', '<>', $param['status'])
  126. ->update([
  127. 'status' => $param['status'],
  128. 'updatetime' => date('Y-m-d H:i:s')
  129. ]);
  130. return $rs ? json_show(0, '操作成功') : json_show(1004, '操作失败,该记录不存在或重复操作');
  131. }
  132. //删除
  133. public function delete()
  134. {
  135. $param = $this->request->only(['cat_id', 'companyNo'], 'post', 'trim');
  136. $val = Validate::rule([
  137. 'cat_id|分类id' => 'require|number|gt:0',
  138. 'companyNo|公司编码' => 'require|length:18',
  139. ]);
  140. if ($val->check($param) == false) return json_show(1004, $val->getError());
  141. $rs = Db::name('company_cat_profit')
  142. ->where(['is_del' => 0, 'companyNo' => $param['companyNo'], 'cat_id' => $param['cat_id']])
  143. ->update([
  144. 'is_del' => 1,
  145. 'updatetime' => date('Y-m-d H:i:s')
  146. ]);
  147. return $rs ? json_show(0, '删除成功') : json_show(1004, '删除失败');
  148. }
  149. //筛选指定公司下的所有分类
  150. public function getListByCompany()
  151. {
  152. $param = $this->request->only(['companyNo', 'cat_name' => '', 'page' => 1, 'size' => 10], 'post', 'trim');
  153. $val = Validate::rule([
  154. 'companyNo|公司编码' => 'require|length:18',
  155. 'cat_name|分类名称' => 'max:255'
  156. ]);
  157. if ($val->check($param) == false) return json_show(1004, $val->getError());
  158. $where = [
  159. ['a.companyNo', '=', $param['companyNo']],['a.is_del', '=', 0],['b.level', '=', 3],
  160. ];
  161. if ($param['cat_name'] !== '') $where[] = ['b.search', 'like', '%' . $param['cat_name'] . '%'];
  162. $count = Db::name('company_cat_profit')
  163. ->alias('a')
  164. ->leftJoin('cat b', 'b.id=a.cat_id')
  165. ->where($where)
  166. ->count('a.id');
  167. $list = Db::name('company_cat_profit')
  168. ->alias('a')
  169. ->leftJoin('cat b', 'b.id=a.cat_id')
  170. ->field('a.id,b.search cat_name,a.cat_id,a.rate,a.order_rate,a.money_rate,a.low_rate,a.sale_rate,a.status,b.pid,a.cat_id is_gold')
  171. ->where($where)
  172. ->order(['a.id' => 'desc'])
  173. ->withAttr("is_gold",function ($value){ return cat_is_gold($value);})
  174. ->page($param['page'], $param['size'])
  175. ->select()
  176. ->toArray();
  177. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  178. }
  179. public function copyProfit(){
  180. $param = $this->request->only(['companyNo', 'cat_id','copy_companyNo', 'copy_cat_id',], 'post', 'trim');
  181. $val = Validate::rule([
  182. 'companyNo|公司编码' => 'requireIf:cat_id,0|length:18',
  183. 'cat_id|分类id' => 'requireIf:cat_id,0|number|egt:0',
  184. 'copy_companyNo|源头公司编码' => 'requireWith:companyNo|alphaNum|length:18',
  185. 'copy_cat_id|源头分类id' => 'requireWith:cat_id|number|egt:0',
  186. ]);
  187. if ($val->check($param) == false)$this->error($val->getError());
  188. $where = [['is_del','=',0]];
  189. if($param['copy_companyNo']!=0){
  190. $where[]=['companyNo','=',$param['copy_companyNo']];
  191. }
  192. if($param['copy_cat_id']!=0){
  193. $where[]=['cat_id','=',$param['copy_cat_id']];
  194. }
  195. if($param['copy_companyNo']==$param['companyNo']&&
  196. $param['copy_cat_id']==$param['cat_id']){
  197. $this->error('数据已存在,请删除后在复制');
  198. }
  199. $rs = Db::name('company_cat_profit')->where($where)->field('companyNo,cat_id,rate,order_rate,money_rate,low_rate,sale_rate ')->select();
  200. if(empty($rs)) $this->error('未找到可复制数据');
  201. Db::startTrans();
  202. try{
  203. $temp=[];
  204. foreach ($rs as $key=>$value){
  205. if($param['cat_id']!=0){
  206. $value['cat_id'] = $param['cat_id'];
  207. }
  208. if($param['companyNo']!=0){
  209. $value['companyNo'] = $param['companyNo'];
  210. }
  211. $temp[]=$value;
  212. if($key%1000==0){
  213. Db::name('company_cat_profit')->insertAll($temp);
  214. $temp=[];
  215. }
  216. }
  217. if(empty($temp)==false) Db::name('company_cat_profit')->insertAll($temp);
  218. Db::commit();
  219. $this->success('复制成功');
  220. }catch (\Exception $exception)
  221. {
  222. Db::rollback();
  223. $this->error($exception->getMessage());
  224. }
  225. }
  226. }