Import.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Import;
  3. use App\Model\Finance;
  4. use App\Model\FinanceDetail;
  5. use App\Model\Settings;
  6. use Illuminate\Support\Facades\DB;
  7. use Maatwebsite\Excel\Concerns\ToArray;
  8. class Import implements ToArray {
  9. private $msg = '';
  10. public $crt_id = 0;
  11. public function array (array $array){
  12. $this->handleData($array);
  13. }
  14. public function setCrt($crt_id){
  15. $this->crt_id = $crt_id;
  16. }
  17. public function getMsg(){
  18. return $this->msg;
  19. }
  20. public function setMsg($msg){
  21. $this->msg = $msg;
  22. }
  23. function splitAmount($amount, $defaultAmount) {
  24. $result = array();
  25. while ($amount > $defaultAmount) {
  26. $result[] = $defaultAmount;
  27. $amount -= $defaultAmount;
  28. }
  29. if ($amount > 0) {
  30. $result[] = $amount;
  31. }
  32. return $result;
  33. }
  34. public function handleData (array $array) {
  35. // 去除表头
  36. unset($array[0]);
  37. if(empty($array)) {
  38. $this->setMsg('数据不能为空!');
  39. return ;
  40. }
  41. //第一次表格数据校验 非空 已经过滤数据
  42. foreach ($array as $key => $value){
  43. if(empty($value[0]) && empty($value[1]) && empty($value[2]) && empty($value[3])) {
  44. unset($array[$key]);
  45. }elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3])) {
  46. $this->setMsg('数据必须完整填写!');
  47. return ;
  48. }else{
  49. $value[3] = trim($value[3]);
  50. if(! is_numeric($value[3])){
  51. $this->setMsg('请输入正确的金额!');
  52. return ;
  53. }
  54. }
  55. }
  56. if(empty($array)) {
  57. $this->setMsg('数据不能为空!');
  58. return ;
  59. }
  60. $array = array_values($array);
  61. //默认拆分金额
  62. $setting = Settings::where('name','defaultAmount')->first();
  63. if(empty($setting) || ! is_numeric($setting->value) || $setting->value <= 0) {
  64. $this->setMsg('请确认默认拆分金额是否正确!');
  65. return ;
  66. }
  67. $defaultAmount = (float)$setting->value;
  68. //生成时间
  69. $crt_time = time();
  70. //出账主表和子表数据
  71. $main = $sub = [];
  72. foreach ($array as $key => $value){
  73. //主表数据
  74. $main[] = [
  75. 'finance_account_name' => $value[0],
  76. 'account' => $value[1],
  77. 'ifsc' => $value[2],
  78. 'amount' => trim($value[3]),
  79. 'crt_id' => $this->crt_id,
  80. 'crt_time' => $crt_time
  81. ];
  82. $totalAmount = (float)$value[3];
  83. $return = $this->splitAmount($totalAmount,$defaultAmount);
  84. $sub[$key] = $return;
  85. }
  86. try{
  87. DB::beginTransaction();
  88. Finance::insert($main);
  89. //获取上一次插入的所有id
  90. $last_insert_id = Finance::where('crt_time',$crt_time)->select('id')->get()->toArray();
  91. $last_insert_id = array_column($last_insert_id,'id');
  92. $insert_sub = [];
  93. foreach ($sub as $key => $value){
  94. foreach ($value as $v){
  95. $insert_sub[] = [
  96. 'amount' => $v,
  97. 'finance_id' => $last_insert_id[$key],
  98. 'crt_time' => $crt_time
  99. ];
  100. }
  101. }unset($main);unset($sub);
  102. FinanceDetail::insert($insert_sub);
  103. DB::commit();
  104. }catch (\Exception $e){
  105. DB::rollBack();
  106. $this->setMsg($e->getMessage());
  107. }
  108. }
  109. }