Contract.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *合同表
  8. */
  9. class Contract Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdong_contract';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. //创建合同
  20. public static function createContract($params) {
  21. //自定义字段
  22. $other = [];
  23. foreach ($params as $name => $val) {
  24. if (strstr($name, 'other_') !== false) {
  25. if(is_array($val)){
  26. $other[$name] = implode(',',$val);
  27. }else{
  28. $other[$name] = $val;
  29. }
  30. unset($params[$name]);
  31. }else{
  32. if($params[$name] === ''){
  33. $params[$name]=NULL;
  34. }
  35. }
  36. }
  37. $product = [];
  38. if (isset($params['product']) && $params['product']) {
  39. $product = $params['product'];
  40. unset($params['product']);
  41. foreach ($product as $tkey => $t) {
  42. $parts=$t['parts']??[];
  43. $new=[];
  44. if($parts){
  45. foreach ($parts as $v){
  46. if(!isset($v['part_id'])){
  47. continue;
  48. }
  49. $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
  50. }
  51. }
  52. $product[$tkey]['parts'] = json_encode($new);
  53. }
  54. }
  55. if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
  56. $params['ratios'] = '';
  57. }
  58. if (isset($params['ratios']) && $params['ratios']) {
  59. foreach ($params['ratios'] as $v) {
  60. if (empty($v['staff_id'])) {
  61. throw new Exception('业绩归属人必须全部选择');
  62. }
  63. }
  64. $params['ratios'] = json_encode($params['ratios']);
  65. }
  66. $customer=Customer::where(['id'=>$params['customer_id']])->find();
  67. if(empty($customer)){
  68. throw new Exception('客户不存在');
  69. }
  70. $params['owner_staff_id'] = $customer->owner_staff_id;
  71. $staff = Staff::info();
  72. if (!empty($staff)) {
  73. $params['create_staff_id'] = $staff->id;
  74. }
  75. $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
  76. $params['flow_id'] = $flow['flow_id'];
  77. $params['order_id'] = $flow['order_id'];
  78. if ($flow['status'] == 0) {//发起人自选
  79. if (empty($params['flow_staff_ids'])) {
  80. throw new Exception('审批人必须选择');
  81. }
  82. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  83. } else {
  84. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  85. }
  86. $Model = new self;
  87. $result = $Model->allowField(true)->save($params);
  88. if (false === $result) {
  89. // 验证失败 输出错误信息
  90. throw new Exception($Model->getError());
  91. }
  92. $lastId = $Model->id;
  93. $otherModel = new ContractOther();
  94. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  95. // 验证失败 输出错误信息
  96. throw new Exception($otherModel->getError());
  97. }
  98. $addRatios = [];
  99. if (isset($params['ratios']) && $params['ratios']) {
  100. $ratios=json_decode($params['ratios'],true);
  101. foreach ($ratios as $v) {
  102. $addRatios[] = [
  103. 'contract_id' => $lastId,
  104. 'ratio' => $v['ratio'],
  105. 'staff_id' => $v['staff_id'],
  106. 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
  107. ];
  108. }
  109. }
  110. //业绩分成 默认自己全部
  111. if (empty($addRatios)) {
  112. $addRatios[] = [
  113. 'contract_id' => $lastId,
  114. 'ratio' => 100,
  115. 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id ,
  116. 'ratio_money' => $params['money']
  117. ];
  118. }
  119. if ($addRatios) {
  120. $ratioModel = new ContractRatio();
  121. $ratioModel->insertAll($addRatios);
  122. }
  123. $addProduct = [];
  124. foreach ($product as $v) {
  125. if (isset($v['id'])){
  126. unset($v['id']);
  127. }
  128. $v['contract_id'] = $lastId;
  129. $addProduct[] = $v;
  130. }
  131. if ($addProduct) {
  132. $productModel = new ContractProduct();
  133. $productModel->allowField(true)->saveAll($addProduct);
  134. }
  135. if ($flow['status'] == 1) {//固定审批
  136. //发送审批通知
  137. Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $lastId);
  138. } else {//发起人自选 依次审批
  139. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  140. if ($staff_id) {
  141. ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $lastId, $staff_id);
  142. }
  143. }
  144. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $lastId, '创建合同');
  145. return true;
  146. }
  147. //修改合同
  148. public static function updateContract($params) {
  149. //自定义字段
  150. $other = [];
  151. foreach ($params as $name => $val) {
  152. if (strstr($name, 'other_') !== false) {
  153. if(is_array($val)){
  154. $other[$name] = implode(',',$val);
  155. }else{
  156. $other[$name] = $val;
  157. }
  158. unset($params[$name]);
  159. }else{
  160. if($params[$name] === ''){
  161. $params[$name]=NULL;
  162. }
  163. }
  164. }
  165. $product = [];
  166. if (isset($params['product'])) {
  167. $product = $params['product'];
  168. unset($params['product']);
  169. foreach ($product as $tkey => $t) {
  170. $parts=$t['parts']??[];
  171. $new=[];
  172. if($parts){
  173. foreach ($parts as $v){
  174. if(!isset($v['part_id'])){
  175. continue;
  176. }
  177. $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
  178. }
  179. }
  180. $product[$tkey]['parts'] = json_encode($new);
  181. }
  182. }
  183. if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
  184. $params['ratios'] = '';
  185. }
  186. if (isset($params['ratios']) && $params['ratios']) {
  187. foreach ($params['ratios'] as $v) {
  188. if (empty($v['staff_id'])) {
  189. throw new Exception('业绩归属人必须全部选择');
  190. }
  191. }
  192. $params['ratios'] = json_encode($params['ratios']);
  193. }
  194. $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
  195. $params['flow_id'] = $flow['flow_id'];
  196. $params['order_id'] = $flow['order_id'];
  197. if ($flow['status'] == 0) {//发起人自选
  198. if (empty($params['flow_staff_ids'])) {
  199. throw new Exception('审批人必须选择');
  200. }
  201. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  202. } else {
  203. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  204. }
  205. $Model = new self;
  206. $params['check_status'] = 1;
  207. // 调用当前模型对应的User验证器类进行数据验证
  208. $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
  209. if (false === $result) {
  210. // 验证失败 输出错误信息
  211. throw new Exception($Model->getError());
  212. }
  213. $otherModel = new ContractOther();
  214. if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
  215. // 验证失败 输出错误信息
  216. throw new Exception($otherModel->getError());
  217. }
  218. $addRatios = [];
  219. if (isset($params['ratios']) && $params['ratios']) {
  220. $ratios=json_decode($params['ratios'],true);
  221. foreach ($ratios as $v) {
  222. $addRatios[] = [
  223. 'contract_id' => $params['id'],
  224. 'ratio' => $v['ratio'],
  225. 'staff_id' => $v['staff_id'],
  226. 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
  227. ];
  228. }
  229. }
  230. //业绩分成 默认自己全部
  231. $staff = Staff::info();
  232. if (empty($addRatios)) {
  233. $addRatios[] = [
  234. 'contract_id' => $params['id'],
  235. 'ratio' => 100,
  236. 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id,
  237. 'ratio_money' => $params['money']
  238. ];
  239. }
  240. if ($addRatios) {
  241. $ratioModel = new ContractRatio();
  242. $ratioModel->where(['contract_id' => $params['id']])->delete();
  243. $ratioModel->insertAll($addRatios);
  244. }
  245. $addProduct = [];
  246. if($product){
  247. foreach ($product as $v) {
  248. if (isset($v['id'])){
  249. unset($v['id']);
  250. }
  251. $v['contract_id'] = $params['id'];
  252. $addProduct[] = $v;
  253. }
  254. }
  255. if ($addProduct) {
  256. $productModel = new ContractProduct();
  257. $productModel->where(['contract_id' => $params['id']])->delete();
  258. $productModel = new ContractProduct();
  259. $productModel->allowField(true)->saveAll($addProduct);
  260. }
  261. if ($flow['status'] == 1) {//固定审批
  262. //发送审批通知
  263. Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $params['id']);
  264. } else {//发起人自选 依次审批
  265. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  266. if ($staff_id) {
  267. ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $params['id'], $staff_id);
  268. }
  269. }
  270. return true;
  271. }
  272. //获取更新时间
  273. public function getUpdatetimeAttr($value) {
  274. return date('Y-m-d H:i:s', $value);
  275. }
  276. //负责人
  277. public function ownerStaff() {
  278. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name');
  279. }
  280. //获取合同相关信息
  281. public function contractOther() {
  282. return $this->belongsTo(ContractOther::class,'id','id');
  283. }
  284. //客户
  285. public function customer() {
  286. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');
  287. }
  288. //商机
  289. public function business() {
  290. return $this->hasOne(Business::class, 'id', 'business_id')->field('id,name');
  291. }
  292. //联系人
  293. public function contacts() {
  294. return $this->hasOne(Contacts::class, 'id', 'contacts_id')->field('id,name');
  295. }
  296. //员工
  297. public function staff() {
  298. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
  299. }
  300. //签约人
  301. public function orderStaff() {
  302. return $this->hasOne(Staff::class, 'id', 'order_staff_id')->field('id,name');
  303. }
  304. //产品
  305. public function product(){
  306. return $this->hasMany(ContractProduct::class,'contract_id','id')->with('product');
  307. }
  308. //回款统计
  309. public function receivables() {
  310. return $this->hasOne(Receivables::class, 'contract_id', 'id')->where(['check_status' => 2])->group('contract_id')->field('contract_id,sum(money) as repayment_money');
  311. }
  312. //导入
  313. public static function importContract($data) {
  314. $addContacts = [];
  315. $addOther = [];
  316. $addratio = [];
  317. foreach ($data as $params) {
  318. //自定义字段
  319. $other = [];
  320. foreach ($params as $name => $val) {
  321. if (strstr($name, 'other_') !== false) {
  322. if(is_array($val)){
  323. $other[$name] = implode(',',$val);
  324. }else{
  325. $other[$name] = $val;
  326. }
  327. unset($params[$name]);
  328. }else{
  329. if($params[$name] === ''){
  330. $params[$name]=NULL;
  331. }
  332. }
  333. }
  334. $params['createtime'] = time();
  335. $params['updatetime'] = time();
  336. $params['check_status'] = 2;
  337. $other['id'] = $params['id'];
  338. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  339. $addContacts[] = $params;
  340. $addratio[] = array(
  341. 'contract_id'=>$params['id'],
  342. 'ratio'=>100,
  343. 'staff_id'=>isset($params['owner_staff_id'])?$params['owner_staff_id']:0,
  344. 'ratio_money'=>isset($params['money'])?$params['money']:0,
  345. );
  346. }
  347. $model = new self;
  348. // 调用当前模型对应的User验证器类进行数据验证
  349. $result = $model->allowField(true)->insertAll($addContacts);
  350. $otherModel = new ContractOther();
  351. $otherModel->allowField(true)->insertAll($addOther);
  352. $modelRatio = new ContractRatio();
  353. $modelRatio->allowField(true)->insertAll($addratio);
  354. return true;
  355. }
  356. public static function getNum()
  357. {
  358. return 'C' . date('Ymd') . rand(10000,99999);
  359. }
  360. }