Contract.php 13 KB

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