Blame view

application/admin/controller/kuerp/Payment.php 54.3 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
  <?php

  

  namespace app\admin\controller\kuerp;

  

  use app\common\controller\Backend;

  use think\Db;

  use Exception;

  use think\Session;

  /**

   * 付款单

   *

   * @icon fa fa-circle-o

   */

  class Payment extends Backend

  {

  

      /**

       * Payment模型对象

       * @var \app\admin\model\kuerp\Payment

       */

      protected $model = null;

  

      public function _initialize()

      {

          parent::_initialize();

          $this->model = new \app\admin\model\kuerp\Payment;

          $setData = (new Goods())->getSetData();

          $this->assign('set', $setData['setList']);

          $this->assignconfig('set',$setData['setList']);

          $this->assign('nowmenu', $setData['nowmenu']);

          $this->view->assign("examineStatusList", $this->model->getExamineStatusList());

          $this->view->assign("customers", $this->model->getCustomersList());

          $this->view->assign("accountsList", $this->model->getAccountsList());

          $this->view->assign("saleList", $this->model->getSaleList());

          $this->view->assign("settlementtype", $this->model->getSettlementtype());

      }

  

      public function import()

      {

          parent::import();

      }

  

      /**

       * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法

       * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑

       * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改

       */

  

  

      /**

       * 查看

       */

      public function index()

      {

          //当前是否为关联查询

          $this->relationSearch = true;

          //设置过滤方法

          $this->request->filter(['strip_tags', 'trim']);

          if ($this->request->isAjax()) {

              //如果发送的来源是Selectpage,则转发到Selectpage

              if ($this->request->request('keyField')) {

                  return $this->selectpage();

              }

              list($where, $sort, $order, $offset, $limit) = $this->buildparams();

  

              $list = $this->model

                  ->with(['admin','staffmember'])

                  ->where($where)

                  ->order($sort, $order)

                  ->paginate($limit);

  

              foreach ($list as $row) {

                  $row->getRelation('admin')->visible(['username']);

                  $row->getRelation('staffmember')->visible(['name']);

              }

              $lists = [];

              $moneys = 0;$write_off = 0;

              $settlementtype = Db::name('kuerp_settlementtype')->where((new Goods())->setWhere())->column('name','id');

              foreach ($list as $list_k=>$list_v){

                  if(isset($list_v['print_num']) && empty($list_v['print_num'])){

                      $list_v['print_num'] = 0;

                  }

                  $customers = Db::name('kuerp_suppliers')->where(['id'=>$list_v['customers_id']])->value('supplyer_name');

                  $staff_member = Db::name('kuerp_staff_member')->where(['id'=>$list_v['staff_member_id']])->value('name');

                  $preparation = Db::name('admin')->where(['id'=>$list_v['preparation_id']])->value('username');

                  $examine = Db::name('admin')->where(['id'=>$list_v['examine_id']])->value('username');

  

                  $account = Db::name('kuerp_payment_account')->alias('co')

                      ->join('kuerp_accounts a','a.id=co.accounts_id')

                      ->where((new Goods())->setWhere(['co.collection_id'=>$list_v['id']]))

                      ->field('co.money,a.account_name,co.settlementtype,co.number,co.remark')

                      ->select();

                  $moneys += $list_v['money'];$write_off += $list_v['write_off'];

                  foreach ($account as $account_k=>$account_v){

                      $lists[] = [

                          'id'            => $list_v['id'],

                          'purchase_code' => $list_v['purchase_code'],

                          'purchase_time' => date('Y-m-d',$list_v['purchase_time']),

                          'customers_id'  => $customers,

                          'accounts_id'   => $account_v['account_name'],

                          'money'         => $account_v['money'],

                          'settlementtype'=> isset($settlementtype[$account_v['settlementtype']])?$settlementtype[$account_v['settlementtype']]:'',

                          'number'        => $account_v['number'],

                          'remark'        => $account_v['remark'],

                          'moneys'        => $list_v['money'],

                          'write_off'     => $list_v['write_off'],

                          'discount'      => $list_v['discount'],

                          'advance'       => $list_v['advance'],

                          'staff_member'  => $staff_member,

                          'examine'       => $examine,

                          'preparation'   => $preparation,

                          'remarks'       => $list_v['remarks'],

                          'print_num'     => $list_v['print_num'],

                      ];

                  }

              }

  

              $result = array("total" => $list->total(), "rows" => $lists, "moneys" => $moneys, "write_off" => $write_off);

  

              return json($result);

          }

          return $this->view->fetch();

      }

  

      public function add($ids = null)

      {

          if ($this->request->isPost()) {

              $params = $this->request->post("row/a");

              if ($params) {

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($params['purchase_time']);

                  if(empty($params['customers_id'])){

                      $this->error('请选择供应商');

                  }

                  $examine= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

                  $params = $this->preExcludeFields($params);

  

                  if(empty($params['purchase_code'])){

                      $this->error('请填写单据编号');

                  }

                  $admin = Session::get('admin');

  

                  //账户信息

                  $account = [];

                  $account_id = [];

                  foreach ($params['accounts_id'] as $k=>$v){

                      if(!empty($v)){//有账户

                          $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$v]))->find();

                          if(empty($accounts)){

                              $this->error('账户信息错误');

                          }

                          if(in_array($v,$account_id)){

                              $this->error('不可多次选择相同的结算账户');

                          }else{

                              $account_id[] = $v;

                          }

                          $account[] = (new Goods())->setDataInfo([

                              'purchase_code'    => $params['purchase_code'],

                              'purchase_time'    => strtotime($params['purchase_time']),

                              'accounts_id'      => $v,

                              'money'            => isset($params['money'][$k])?(float)$params['money'][$k]:0,

                              'settlementtype'   => isset($params['settlementtype'][$k])?(float)$params['settlementtype'][$k]:0,

                              'number'           => isset($params['number'][$k])?(float)$params['number'][$k]:'',

                              'remark'           => isset($params['remark'][$k])?$params['remark'][$k]:'',

                          ]);

                      }

                  }

                  $account_money = 0;//账户收支总金额

                  foreach ($account as $account_k=>$account_v){

                      $account_money += $account_v['money'];

                  }

                  //账户信息结束

  

                  //源单信息

                  $sale = [];

                  $sale_id = [];

                  $log_arr = [];

                  $log_add = [];

                  foreach ($params['purchase_number'] as $k=>$v){

                      if(!empty($v) && !empty($params['purchase_type'][$k])){//有单号

                          $order = Db::name('kuerp_writeoff_source')->where(['id'=>$params['purchase_type'][$k]])->find();

                          if(empty($order)){

                              $this->error('源单信息查询错误');

                          }

                          if(in_array($v,$sale_id)){

                              $this->error('不可多次选择相同的源单');

                          }else{

                              $sale_id[] = $v;

                          }

                          $thisrow = (new Goods())->setDataInfo([

                              'purchase_code'    => $params['purchase_code'],

                              'purchase_time'    => strtotime($params['purchase_time']),

                              'purchase_number'  => $order['purchase_code'],

                              'business_name'    => isset($params['business_type'][$k])?$params['business_type'][$k]:null,

                              'business_type'    => $order['purchase_type'],

                              'times'            => $order['purchase_time'],

                              'money'            => $order['price'],

                              'write_off'        => isset($params['write_off'][$k])?(float)$params['write_off'][$k]:0,

                              'write_off_money'  => isset($params['write_off_money'][$k])?(float)$params['write_off_money'][$k]:0,

                              'write_off_price'  => isset($params['write_off_price'][$k])?(float)$params['write_off_price'][$k]:0,

                              'remark'           => $order['remark'],

                              'source_id'        => $order['id'],

                              'type'             => $order['type'],

                          ]);

                          $sale[] = $thisrow;

  

                          $where = [];

                          $where['source_id'] = $order['id'];

                          $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere($where))->find();

                          if($log){

                              $status = true;

                              foreach ($log_arr as $log_arr_k=>$log_arr_v){

                                  if($log_arr_v['id']==$log['id']){

                                      $status = false;

                                      $log_arr[$log_arr_k]['money'] += $thisrow['write_off_price'];

                                      $log_arr[$log_arr_k]['write_off'] -= $thisrow['write_off_price'];

                                  }

                              }

                              if($status){

                                  $log_arr[] = [

                                      'id' => $log['id'],

                                      'write_off' => $log['write_off']-$thisrow['write_off_price'],

                                      'money' => $log['money']+$thisrow['write_off_price'],

                                  ];

                              }

                          }else{

                              $log_add[] = [

                                  'source_id' => $order['id'],

                                  'write_off' => $thisrow['write_off_money']-$thisrow['write_off_price'],

                                  'money' => $thisrow['write_off_price'],

                              ];

                          }

                      }

                  }

                  $sale_money = 0;//账户收支总金额

                  foreach ($sale as $sale_k=>$sale_v){

                      $sale_money += $sale_v['write_off_price'];

                  }

                  $business_type = !empty($sale)?2:1;

                  $row = (new Goods())->setDataInfo([

                      'purchase_code'     =>  $params['purchase_code'],

                      'purchase_time'     =>  strtotime($params['purchase_time']),

                      'business_type'     =>  $business_type,

                      'customers_id'      =>  $params['customers_id'],

                      'money'             =>  $account_money,

                      'write_off'         =>  $sale_money,

                      'discount'          =>  isset($params['discount'])?(float)$params['discount']:0,

                      'advance'           =>  isset($params['advance'])?(float)$params['advance']:0,

                      'staff_member_id'   =>  isset($params['staff_member_id'])?(float)$params['staff_member_id']:null,

                      'remarks'           =>  $params['note'],

                      'preparation_id'    =>  $admin['id'],

                      'operation_id'      =>  $admin['id'],

                      'createtime'        => time(),

                      'updatetime'        => time(),

                  ]);

  

                  if(empty($account)){

                      $this->error('请填写账户结算信息');

                  }

                  if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code'])]))->find()){

                      $this->error('单据编号已占用');

                  }

  

                  $result = false;

                  Db::startTrans();

                  try {

                      if(!empty($row)){

                          $result = $this->model->insertGetId($row);

                      }

  

                      foreach ($account as $account_k=>$account_v){

                          $account[$account_k]['collection_id'] = $result;

                      }

                      Db::name('kuerp_payment_account')->insertAll($account);

  

                      foreach ($sale as $sale_k=>$sale_v){

                          $sale[$sale_k]['collection_id'] = $result;

                      }

                      Db::name('kuerp_payment_sale')->insertAll($sale);

                      if(!empty($log_arr)){

                          (new \app\admin\model\kuerp\WriteoffLog())->saveAll($log_arr);

                      }

                      if(!empty($log_add)){

                          (new \app\admin\model\kuerp\WriteoffLog())->insertAll($log_add);

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

                      $this->error($e->getMessage());

                  }

                  if (isset($result) && $result !== false) {

                      if($examine!=1){

                          try {

                              $exa = $this->examine($result,1);

                          } catch (Exception $e) {

                          }

                          if(!isset($exa) || $exa['status']!=1){

                              $exa = $this->examine($result,1);

                              try {

                                  $this->del($result);

                              } catch (Exception $e) {

                              }

                              $this->error($exa['msg']);

                          }

                      }

                      $this->success();

                  } else {

                      $this->error(__('No rows were inserted'));

                  }

              }

              $this->error(__('Parameter %s can not be empty', ''));

          }

          $code = (new Goods())->getCodes('',22);

          $this->view->assign("code", $code);

          return $this->view->fetch();

      }

  

      public function edit($ids = null)

      {

          $row = $this->model->get($ids);

          if (!$row) {

              $this->error(__('No Results were found'));

          }

          $adminIds = $this->getDataLimitAdminIds();

          if (is_array($adminIds)) {

              if (!in_array($row[$this->dataLimitField], $adminIds)) {

                  $this->error(__('You have no permission'));

              }

          }

          if ($this->request->isPost()) {

              (new \app\admin\controller\kuerp\Checkout())->checkOutTime($row['purchase_time']);

              $examine= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

              if($row['examine_status']==1 && $examine==1){

                  $this->error('不能编辑已审核的数据');

              }

  

              $params = $this->request->post("row/a");

              if ($params) {

                  $params = $this->preExcludeFields($params);

                  if(empty($params['customers_id'])){

                      $this->error('请选择供应商');

                  }

                  if(empty($params['purchase_code'])){

                      $this->error('请填写单据编号');

                  }

                  $admin = Session::get('admin');

  

                  //账户信息

                  $account = [];

                  $account_id = [];

                  foreach ($params['accounts_id'] as $k=>$v){

                      if(!empty($v)){//有账户

                          $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$v]))->find();

                          if(empty($accounts)){

                              $this->error('账户信息错误');

                          }

                          if(in_array($v,$account_id)){

                              $this->error('不可多次选择相同的结算账户');

                          }else{

                              $account_id[] = $v;

                          }

                          $account[] = (new Goods())->setDataInfo([

                              'purchase_code'    => $params['purchase_code'],

                              'purchase_time'    => strtotime($params['purchase_time']),

                              'collection_id'    => $ids,

                              'accounts_id'      => $v,

                              'money'            => isset($params['money'][$k])?(float)$params['money'][$k]:0,

                              'settlementtype'   => isset($params['settlementtype'][$k])?(float)$params['settlementtype'][$k]:0,

                              'number'           => isset($params['number'][$k])?(float)$params['number'][$k]:'',

                              'remark'           => isset($params['remark'][$k])?$params['remark'][$k]:'',

                          ]);

                      }

                  }

                  $account_money = 0;//账户收支总金额

                  foreach ($account as $account_k=>$account_v){

                      $account_money += $account_v['money'];

                  }

                  //账户信息结束

  

                  //源单信息

                  $sale = [];

                  $sale_id = [];

                  $log_arr = [];

                  $log_add = [];

                  foreach ($params['purchase_number'] as $k=>$v){

                      if(!empty($v) && !empty($params['purchase_type'][$k])){//有单号

                          $order = Db::name('kuerp_writeoff_source')->where(['id'=>$params['purchase_type'][$k]])->find();

                          if(empty($order)){

                              $this->error('源单信息查询错误');

                          }

                          if(in_array($v,$sale_id)){

                              $this->error('不可多次选择相同的源单');

                          }else{

                              $sale_id[] = $v;

                          }

                          $thisrow = (new Goods())->setDataInfo([

                              'purchase_code'    => $params['purchase_code'],

                              'purchase_time'    => strtotime($params['purchase_time']),

                              'purchase_number'  => $order['purchase_code'],

                              'collection_id'    => $ids,

                              'business_name'    => isset($params['business_type'][$k])?$params['business_type'][$k]:null,

                              'business_type'    => $order['purchase_type'],

                              'times'            => $order['purchase_time'],

                              'money'            => $order['price'],

                              'write_off'        => isset($params['write_off'][$k])?(float)$params['write_off'][$k]:0,

                              'write_off_money'  => isset($params['write_off_money'][$k])?(float)$params['write_off_money'][$k]:0,

                              'write_off_price'  => isset($params['write_off_price'][$k])?(float)$params['write_off_price'][$k]:0,

                              'remark'           => $order['remark'],

                              'source_id'        => $order['id'],

                              'type'             => $order['type'],

                          ]);

                          $sale[] = $thisrow;

  

                          $where = [];

                          $where['source_id'] = $order['id'];

                          $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere($where))->find();

                          if($log){

                              $status = true;

                              foreach ($log_arr as $log_arr_k=>$log_arr_v){

                                  if($log_arr_v['id']==$log['id']){

                                      $status = false;

                                      $log_arr[$log_arr_k]['money'] += $thisrow['write_off_price']-(float)$params['write_off_prices'][$k];

                                      $log_arr[$log_arr_k]['write_off'] -= $thisrow['write_off_price']+(float)$params['write_off_prices'][$k];

                                  }

                              }

                              if($status){

                                  $log_arr[] = [

                                      'id' => $log['id'],

                                      'write_off' => $log['write_off']-$thisrow['write_off_price']+(float)$params['write_off_prices'][$k],

                                      'money' => $log['money']+$thisrow['write_off_price']-(float)$params['write_off_prices'][$k],

                                  ];

                              }

                          }else{

                              $log_add[] = [

                                  'source_id' => $order['id'],

                                  'write_off' => $thisrow['write_off_money']-$thisrow['write_off_price']+(float)$params['write_off_prices'][$k],

                                  'money' => $thisrow['write_off_price']-(float)$params['write_off_prices'][$k],

                              ];

                          }

                      }

                  }

                  $sale_money = 0;//账户收支总金额

                  foreach ($sale as $sale_k=>$sale_v){

                      $sale_money += $sale_v['write_off_price'];

                  }

                  $business_type = !empty($sale)?2:1;

                  $rowdata = (new Goods())->setDataInfo([

                      'purchase_code'     =>  $params['purchase_code'],

                      'purchase_time'     =>  strtotime($params['purchase_time']),

                      'business_type'     => $business_type,

                      'customers_id'      =>  $params['customers_id'],

                      'money'             =>  $account_money,

                      'write_off'         =>  $sale_money,

                      'discount'          =>  isset($params['discount'])?(float)$params['discount']:0,

                      'advance'           =>  isset($params['advance'])?(float)$params['advance']:0,

                      'staff_member_id'   =>  isset($params['staff_member_id'])?(float)$params['staff_member_id']:null,

                      'remarks'           =>  $params['note'],

                      'preparation_id'    =>  $admin['id'],

                      'operation_id'      =>  $admin['id'],

                      'updatetime'        => time(),

                  ]);

  

                  if(empty($account)){

                      $this->error('请填写账户结算信息');

                  }

  

                  if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code']),'id'=>['<>',$ids]]))->find()){

                      $this->error('单据编号已占用');

                  }

  

                  //详情

                  $info_list = Db::name('kuerp_writeoff_info')->where((new Goods())->setWhere(['collection_id'=>$ids]))->select();

                  $writeoff_log = [];

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      $log = Db::name('kuerp_writeoff_log')->where(['source_id'=>$info_list_v['source_id']])->find();

                      if($log){

                          $addstatus = true;

                          foreach ($writeoff_log as $writeoff_log_k=>$writeoff_log_v){

                              if($writeoff_log_v['id']==$log['id']){

                                  $addstatus = false;

                                  $writeoff_log[$writeoff_log_k]['write_off'] = $writeoff_log_v['write_off']+$info_list_v['write_off_price'];

                                  $writeoff_log[$writeoff_log_k]['money'] = $writeoff_log_v['money']-$info_list_v['money'];

                              }

                          }

                          if($addstatus){

                              $writeoff_log[] = [

                                  'id' => $log['id'],

                                  'write_off'=>($log['write_off']+$info_list_v['write_off_price']),

                                  'money'=>($log['money']-$info_list_v['write_off_price'])

                              ];

                          }

                      }

                  }

                  foreach ($log_arr as $log_arr_k=>$log_arr_v){

                      $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere(['id'=>$log_arr_v['id']]))->find();

                      if($log){

                          $addstatus = true;

                          foreach ($writeoff_log as $writeoff_log_k=>$writeoff_log_v){

                              if($writeoff_log_v['id']==$log['id']){

                                  $addstatus = false;

                                  $writeoff_log[$writeoff_log_k]['write_off'] = $writeoff_log_v['write_off']-$log_arr_v['write_off'];

                                  $writeoff_log[$writeoff_log_k]['money'] = $writeoff_log_v['money']+$log_arr_v['money'];

                              }

                          }

                          if($addstatus){

                              $writeoff_log[] = [

                                  'id' => $log['id'],

                                  'write_off'=>($log['write_off']-$log_arr_v['write_off']),

                                  'money'=>($log['money']+$log_arr_v['write_off'])

                              ];

                          }

                      }

                  }

                  $result = false;

                  Db::startTrans();

                  try {

                      if($examine!=1 && $row['examine_status']==1){

                          $this->examine($ids,2);

                      }

                      if(!empty($row)){

                          $result = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->update($rowdata);

                      }

                      Db::name('kuerp_payment_account')->where((new Goods())->setWhere(['collection_id'=>$ids]))->delete();

                      Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>$ids]))->delete();

  

                      Db::name('kuerp_payment_account')->insertAll($account);

                      Db::name('kuerp_payment_sale')->insertAll($sale);

                      if(!empty($log_add)){

                          (new \app\admin\model\kuerp\WriteoffLog())->insertAll($log_add);

                      }

                      if(!empty($writeoff_log)){

                          (new \app\admin\model\kuerp\WriteoffLog())->saveAll($writeoff_log);

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

                      $this->error($e->getMessage());

                  }

                  if ($result !== false) {

                      if($examine!=1){

                          try {

                              $exa = $this->examine($ids,1);

                          } catch (Exception $e) {

                          }

                          if(!isset($exa) || $exa['status']!=1){

                              $this->error(__('No rows were inserted'));

                          }

                      }

                      $this->success();

                  } else {

                      $this->error(__('No rows were inserted'));

                  }

              }

              $this->error(__('Parameter %s can not be empty', ''));

          }

          $this->view->assign("row", $row);

  

          $account = Db::name('kuerp_payment_account')->where((new Goods())->setWhere(['collection_id'=>$row['id']]))->select();

          $this->view->assign("account", $account);

  

          $sale = Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>$row['id']]))->select();

          $this->view->assign("sale", $sale);

  

          $give_account = Db::name('kuerp_suppliers')->where(['id'=>$row['customers_id']])->value('need_give_account');

          if($row['examine_status']==1){

              $this->assignconfig("give_account", $give_account-$row['money']);

              $this->view->assign("give_account", (float)$give_account);

          }else{

              $this->assignconfig("give_account", $give_account);

              $this->view->assign("give_account", $give_account);

          }

  

          $kuerp_suppliers = Db::name('kuerp_suppliers')->where(['id'=>$row['customers_id']])->field('id,supplyer_name')->find();

          $this->assignconfig("kuerp_suppliers", $kuerp_suppliers);

  

          //审核禁止输入

          $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

          if($examine_set==1 && $row['examine_status']==1){

              $this->assign('exclass','hiddens');

          }

          $this->assignconfig("examine_status", $row['examine_status']);

          return $this->view->fetch();

      }

  

      /**

       * 删除

       */

      public function del($ids = "")

      {

          if (!$this->request->isPost()) {

              $this->error(__("Invalid parameters"));

          }

          $ids = $ids ? $ids : $this->request->post("ids");

          if ($ids) {

              $pk = $this->model->getPk();

              $adminIds = $this->getDataLimitAdminIds();

              $examine= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');

              if (is_array($adminIds)) {

                  $this->model->where($this->dataLimitField, 'in', $adminIds);

              }

              $list = $this->model->where($pk, 'in', $ids)->select();

              $del_id = [];

              foreach ($list as $list_k=>$list_v){

                  if($list_v['examine_status']==1){

                      (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

                  }

                  if($list_v['examine_status']==1 && $examine==1){

                      $this->error('不能删除已审核的数据');

                  }elseif ($list_v['examine_status']==1 && $examine!=1){

                      try {

                          $this->examine($list_v['id'],2);

                          $del_id[] = $list_v['id'];

                      } catch (Exception $e) {

                      }

                  }else{

                      $del_id[] = $list_v['id'];

                  }

              }

              $list = $this->model->where((new Goods())->setWhere(['id'=>['in',$del_id]]))->select();

              //详情

              $info_list = Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->select();

  

              $count = 0;

              Db::startTrans();

              try {

                  foreach ($list as $k => $v) {

                      $count += $v->delete();

                  }

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      $log = Db::name('kuerp_writeoff_log')->where((new Goods())->setWhere(['source_id'=>$info_list_v['source_id']]))->find();

                      if($log){

                          Db::name('kuerp_writeoff_log')->where(['id'=>$log['id']])->update([

                              'write_off'=>($log['write_off']+$info_list_v['write_off_price']),

                              'money'=>($log['money']-$info_list_v['write_off_price'])

                          ]);

                      }

                  }

                  Db::name('kuerp_payment_account')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->delete();

                  Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>['in',$del_id]]))->delete();

                  Db::commit();

              }catch (Exception $e) {

                  Db::rollback();

                  $this->error($e->getMessage());

              }

              if ($count) {

                  $this->success();

              } else {

                  $this->error(__('No rows were deleted'));

              }

          }

          $this->error(__('Parameter %s can not be empty', 'ids'));

      }

  

      //审核/反审核

      public function examine($ids = null,$examine=1){

          $examine_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');

          if(empty($ids)){

              if($examine_set!=1){

                  return ['status'=>0,'msg'=>'系统错误'];

              }

              $this->error('请选择要操作的数据');

          }

          $admin = Session::get('admin');

          if($examine==1){

              $list = Db::name('kuerp_payment')->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'0']))->select();

              foreach ($list as $list_k=>$list_v){

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

              }

              if(empty($list)){

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'系统错误'];

                  }

                  $purchase_code = Db::name('kuerp_payment')->where(['id'=>['in',$ids],'examine_status'=>'1'])->column('purchase_code');

                  if(!empty($purchase_code)){

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'付款单'.implode(',',$purchase_code).'已审核,请重新选择'];

                      }

                      $this->error('付款单'.implode(',',$purchase_code).'已审核,请重新选择');

                  }else{

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'请选择需要审核的数据'];

                      }

                      $this->error('请选择需要审核的数据');

                  }

              }

              $success_count = 0;

              foreach ($list as $list_k=>$list_v){

                  //账户信息

                  $accountData = [];$log = [];

                  $account = Db::name('kuerp_payment_account')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();

                  foreach ($account as $account_k=>$account_v){

                      $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$account_v['accounts_id']]))->find();

                      $log[] = [

                          'account_coding'    => $accounts['account_coding'],

                          'account_name'      => $accounts['account_name'],

                          'balance'           => $accounts['balance']+$account_v['money'],

                          'expenditure'       => $account_v['money'],

                          'pay_time'          => $list_v['purchase_time'],

                          'bill_code'         => $account_v['purchase_code'],

                          'business_type'     => '8',

                          'customers_id'      => $list_v['customers_id'],

                          'payment'           => $list_v['staff_member_id'],

                          'remark'            => $list_v['remarks'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ];

                      $accountData[] = [

                          'balance'  => $account_v['money'],

                          'account'  => $accounts['id'],

                      ];

                  }

                  //账户信息结束

                  //客户对账单

                  $accounts_receivable = [];

                  if(!empty($list_v['customers_id'])){

                      $accounts_receivable[] = (new Goods())->setDataInfo([

                          'suppliers_id'      => $list_v['customers_id'],

                          'purchase_time'     => $list_v['purchase_time'],

                          'purchase_code'     => $list_v['purchase_code'],

                          'business_type'     => '9',

                          'receivable'        => -$list_v['money'],

                          'remark'            => $list_v['remarks'],

                          'real_money'        => $list_v['money'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ]);

                  }

                  $info_list = Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();

                  $suppliers = Db::name('kuerp_suppliers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->find();

                  $gave_money = $suppliers['need_give_account'] - $list_v['money'];

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      if($info_list_v['business_type']==4 && $info_list_v['type']==1){

                          //普通购货

                          $order = Db::name('kuerp_goods_purchase')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                          if($order){

                              $all_money = $order['real_money'];

                              $pay_money = $order['pay_money']+$info_list_v['write_off_price'];

                              if($all_money<$pay_money){

                                  if($examine_set!=1){

                                      return ['status'=>0,'msg'=>$info_list_v['purchase_number'].'核销金额不能大于未核销金额'];

                                  }

                                  $this->error($info_list_v['purchase_number'].'核销金额不能大于未核销金额');

                              }

                          }

                      }else if($info_list_v['business_type']==4 && $info_list_v['type']==2){

                          //购货退货

                          $order = Db::name('kuerp_goods_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                          if($order){

                              $all_money = $order['real_money'];

                              $pay_money = $order['retreat_money']+(-$info_list_v['write_off_price']);

                              if($all_money<$pay_money){

                                  if($examine_set!=1){

                                      return ['status'=>0,'msg'=>$info_list_v['purchase_number'].'核销金额不能大于未核销金额'];

                                  }

                                  $this->error($info_list_v['purchase_number'].'核销金额不能大于未核销金额');

                              }

                          }

                      }

                  }

  

                  Db::startTrans();

                  try {

                      if(!empty($accounts_receivable)){

                          Db::name('kuerp_accounts_copewith')->insertAll($accounts_receivable);

                      }

                      if($list_v['business_type']==1){

                          Db::name('kuerp_writeoff_source')->insert([

                              'purchase_type' => 2,

                              'type' => 1,

                              'suppliers_code' => $suppliers['supplyer_code'],

                              'purchase_code' => $list_v['purchase_code'],

                              'purchase_time' => $list_v['purchase_time'],

                              'price' => $list_v['advance'],

                              'money' => 0,

                              'write_off' => $list_v['advance'],

                              'remark' => $list_v['remarks'],

                          ]);

                      }

  

                      foreach ($info_list as $info_list_k=>$info_list_v){

                          $income = Db::name('kuerp_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))->find();

                          if($info_list_v['business_type']==4 && $info_list_v['type']==1){

                              //普通购货

                              $order = Db::name('kuerp_goods_purchase')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                              if($order){

                                  Db::name('kuerp_payment_log')->insert((new Goods())->setDataInfo([

                                      'suppliers_code' => $order['supplyer_code'],

                                      'business_type'  => '3',

                                      'bill_time'      => time(),

                                      'bill_code'      => $order['purchase_code'],

                                      'bill_codes'     => $info_list_v['purchase_code'],

                                      //'pay_money'      => $order['preferential_money']+$order['real_money'],

                                      'pay_money'      => $info_list_v['write_off_price'],

                                      'reamrk'         => $list_v['remarks'],

                                      'createtime'     => time(),

                                  ]));

                                  $more_money = $order['real_money']-$info_list_v['write_off_price']-$order['pay_money'];

                                  $status = $more_money<=0?1:2;

                                  $pay_money = $order['pay_money']+$info_list_v['write_off_price'];

                                  Db::name('kuerp_goods_purchase')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))

                                      ->update(['pay_money'=>$pay_money,'pay_status'=>$status]);

                                  Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['id'=>$info_list_v['id']]))->update([

                                      'old_money'=>$order['pay_money'],

                                      'old_status'=>$order['pay_status'],

                                      'old_status_i'=>$income['status']

                                  ]);

                                  if($income){

                                      Db::name('kuerp_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))

                                          ->update(['status'=>$status]);

                                      Db::name('kuerp_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])

                                          ->update(['status'=>$status]);

                                  }

                              }

                          }

                          if($info_list_v['business_type']==4 && $info_list_v['type']==2){

                              //购货退货

                              $order = Db::name('kuerp_goods_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                              Db::name('kuerp_payment_log')->insert((new Goods())->setDataInfo([

                                  'suppliers_code' => $order['supplyer_code'],

                                  'business_type'  => '4',

                                  'bill_time'      => time(),

                                  'bill_code'      => $order['purchase_code'],

                                  'bill_codes'     => $info_list_v['purchase_code'],

                                  //'pay_money'      => $order['preferential_money']+$order['real_money'],

                                  'pay_money'      => $info_list_v['write_off_price'],

                                  'reamrk'         => $list_v['remarks'],

                                  'createtime'     => time(),

                              ]));

                              if($order){

                                  $more_money = $order['real_money']+$info_list_v['write_off_price']-$order['retreat_money'];;

                                  $status = $more_money==0?1:2;

                                  $pay_money = $order['retreat_money']+(-$info_list_v['write_off_price']);

                                  Db::name('kuerp_goods_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))

                                      ->update(['retreat_money'=>$pay_money,'retreat_status'=>$status]);

                                  Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['id'=>$info_list_v['id']]))->update([

                                      'old_money'=>$order['retreat_money'],

                                      'old_status'=>$order['retreat_status'],

                                      'old_status_i'=>$income['status']

                                  ]);

                              }

                              if($income){

                                  $status += 3;

                                  Db::name('kuerp_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))

                                      ->update(['status'=>$status]);

                                  Db::name('kuerp_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])

                                      ->update(['status'=>$status]);

                              }

                          }

                      }

  

                      foreach ($accountData as $accountData_k=>$accountData_v){

                          if($accountData_v['balance']<0){

                              $accountData_v['balance'] = -$accountData_v['balance'];

                              Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->inc('balance',$accountData_v['balance'])->update();

                          }else{

                              Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->dec('balance',$accountData_v['balance'])->update();

                          }

                      }

                      if(!empty($log)){

                          Db::name('kuerp_accounts_log')->insertAll($log);

                      }

  

                      Db::name('kuerp_suppliers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->update(['need_give_account'=>$gave_money]);

                      $result = $this->model->where((new Goods())->setWhere(['id'=>$list_v['id'],'examine_status'=>'0']))->update(['examine_status'=>1,'examine_id'=>$admin['id'],'examine_time'=>time(),'operation_id'=>$admin['id']]);

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>$e->getMessage()];

                      }

                      $this->error($e->getMessage());

                  }

                  if(isset($result) && $result !== false){

                      $success_count += 1;

                  }

              }

              if($success_count>0){

                  if($examine_set!=1){

                      return ['status'=>1,'msg'=>'操作成功'];

                  }

                  $this->success($success_count.'条数据审核成功');

              }else{

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'操作失败'];

                  }

                  $this->error('审核失败');

              }

          }else{

              $list = Db::name('kuerp_payment')->where(['id'=>['in',$ids],'examine_status'=>'1'])->select();

              foreach ($list as $list_k=>$list_v){

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

              }

              if(empty($list)){

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'系统错误'];

                  }

                  $purchase_code = Db::name('kuerp_payment')->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code');

                  if(!empty($purchase_code)){

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'付款单'.implode(',',$purchase_code).'未审核,请重新选择'];

                      }

                      $this->error('付款单'.implode(',',$purchase_code).'未审核,请重新选择');

                  }else{

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'请选择需要反审核的数据'];

                      }

                      $this->error('请选择需要反审核的数据');

                  }

              }

              $success_count = 0;

              foreach ($list as $list_k=>$list_v){

                  //账户信息

                  $accountData = [];$log = [];

                  $account = Db::name('kuerp_payment_account')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();

                  foreach ($account as $account_k=>$account_v){

                      $accounts = Db::name('kuerp_accounts')->where((new Goods())->setWhere(['id'=>$account_v['accounts_id']]))->find();

                      $accountData[] = [

                          'balance'  => $account_v['money'],

                          'account'  => $accounts['id'],

                      ];

                      $log[] = $account_v['purchase_code'];

                  }

  

                  $info_list = Db::name('kuerp_payment_sale')->where((new Goods())->setWhere(['collection_id'=>$list_v['id']]))->select();

                  $suppliers = Db::name('kuerp_suppliers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->find();

                  $gave_money = $suppliers['need_give_account'] + $list_v['money'];

                  //账户信息结束

                  Db::startTrans();

                  try {

                      Db::name('kuerp_accounts_copewith')->where(['purchase_code'=>$list_v['purchase_code'],'business_type'=>'9'])->delete();

                      Db::name('kuerp_writeoff_source')->where(['purchase_code'=>$list_v['purchase_code'],'purchase_type'=>'2','type'=>'1'])->delete();

  

                      foreach ($info_list as $info_list_k=>$info_list_v){

                          if($info_list_v['business_type']==4 && $info_list_v['type']==1){

                              //普通购货

                              $order = Db::name('kuerp_goods_purchase')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                              if($order){

                                  Db::name('kuerp_goods_purchase')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))

                                      ->update(['pay_money'=>$info_list_v['old_money'],'pay_status'=>$info_list_v['old_status']]);

                              }

                          }

                          if($info_list_v['business_type']==4 && $info_list_v['type']==2){

                              //购货退货

                              $order = Db::name('kuerp_goods_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))->find();

                              if($order){

                                  Db::name('kuerp_goods_exchange')->where((new Goods())->setWhere(['purchase_code'=>$info_list_v['purchase_number']]))

                                      ->update(['retreat_money'=>$info_list_v['old_money'],'retreat_status'=>$info_list_v['old_status']]);

                              }

                          }

                          $income = Db::name('kuerp_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))->find();

                          if($income){

                              if($info_list_v['business_type']==4 && $info_list_v['type']==2){

                                  $info_list_v['old_status'] += 3;

                              }

                              Db::name('kuerp_invoicing')->where((new Goods())->setWhere(['bill_code'=>$info_list_v['purchase_number']]))

                                  ->update(['status'=>$info_list_v['old_status']]);

                              Db::name('kuerp_invoicing_log')->where(['bill_code'=>$info_list_v['purchase_number']])

                                  ->update(['status'=>$info_list_v['old_status']]);

                          }

                          Db::name('kuerp_payment_log')->where((new Goods())->setWhere(['bill_codes'=>$info_list_v['purchase_code']]))->delete();

                      }

                      foreach ($accountData as $accountData_k=>$accountData_v){

                          if($accountData_v['balance']<0){

                              $accountData_v['balance'] = -$accountData_v['balance'];

                              Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->dec('balance',$accountData_v['balance'])->update();

                          }else{

                              Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->inc('balance',$accountData_v['balance'])->update();

                          }

                      }

                      if(!empty($log)){

                          Db::name('kuerp_accounts_log')->where((new Goods())->setWhere(['bill_code'=>['in',$log],'business_type'=>'8']))->delete();

                      }

  

                      Db::name('kuerp_suppliers')->where((new Goods())->setWhere(['id'=>$list_v['customers_id']]))->update(['need_give_account'=>$gave_money]);

                      $result = $this->model->where((new Goods())->setWhere(['id'=>$list_v['id'],'examine_status'=>'1']))->update(['examine_status'=>'0','examine_id'=>null,'examine_time'=>null,'operation_id'=>$admin['id']]);

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

                      $this->error($e->getMessage());

                  }

                  if(isset($result) && $result !== false){

                      $success_count += 1;

                  }

              }

              if($success_count>0){

                  if($examine_set!=1){

                      return ['status'=>1,'msg'=>'操作成功'];

                  }

                  $this->success($success_count.'条数据反审核成功');

              }else{

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'操作失败'];

                  }

                  $this->error('反审核失败');

              }

          }

      }

  

  }