Contract.php 13 KB

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