新版订单消耗系统

CustomerInvoices.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomerInvoices extends Model
  6. {
  7. protected $table = 'customer_invoices';
  8. public $timestamps = false;
  9. protected static $unguarded = true;
  10. public static $typeList = [1,3,4,5,6];
  11. public static function invoiceHistory($orderIds)
  12. {
  13. $details = CustomerInvoiceOrders::select('inv_id', 'order_id', 'invoice_amount')
  14. ->whereIn('order_id', $orderIds)->where('enable', 1)
  15. ->orderBy('id', 'asc')->get();
  16. $orders = Order::select('id', 'task_id', 'customer_name', 'billing_date', 'final_amount')
  17. ->whereIn('id', $orderIds)->where('enable', 1)
  18. ->get()->keyBy('id')->toArray();
  19. $inv_ids = $details->pluck('inv_id');
  20. $inv_ids = json_decode(json_encode($inv_ids), 1);
  21. $inv_ids = array_unique($inv_ids);
  22. $typeList = self::$typeList;
  23. if (empty($inv_ids)) {
  24. $invoices = [];
  25. } else {
  26. $sql = 'select ci.* from customer_invoices as ci left join statements as s on s.id = ci.statements_id where
  27. s.type in ('.implode(',', $typeList).') and ci.status = 4 and ci.is_invoiced=1 and ci.id in ('.implode(',', $inv_ids) . ') and ci.enable = 1
  28. and s.enable = 1';
  29. $invoices = \DB::select($sql);
  30. $invoices = json_decode(json_encode($invoices), 1);
  31. $invoices = array_column($invoices, null, 'id');
  32. }
  33. $data = array();
  34. foreach($details->toArray() as $k=>&$v){
  35. $inv_id = $v['inv_id'];
  36. if(!isset($invoices[$inv_id])){
  37. continue;
  38. }
  39. $invoice = $invoices[$inv_id];
  40. $order = $orders[$v['order_id']];
  41. if(!isset($data[$v['order_id']])) {
  42. //第一次开票
  43. $data[$v['order_id']] = [
  44. 'first_invoice_amount' => $v['invoice_amount'],
  45. 'first_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  46. 'invoice_num' => 1,
  47. 'second_invoice_amount' => 0,
  48. 'second_invoice_date' => null,
  49. 'uninvoiced_amount' => $order['final_amount'] - $v['invoice_amount'],
  50. 'invoice_amount' => $v['invoice_amount'],
  51. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  52. ];
  53. } else {
  54. //第二次开票
  55. $data[$v['order_id']] = [
  56. 'invoice_num' => 2,
  57. 'first_invoice_amount' => $data[$v['order_id']]['first_invoice_amount'],
  58. 'first_invoice_date' => $data[$v['order_id']]['first_invoice_date'],
  59. 'second_invoice_amount' => $v['invoice_amount'],
  60. 'second_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  61. 'uninvoiced_amount' => $data[$v['order_id']]['uninvoiced_amount'] - $v['invoice_amount'],
  62. 'invoice_amount' => ($data[$v['order_id']]['first_invoice_amount'] + $v['invoice_amount']),
  63. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  64. ];
  65. }
  66. // $data[] = [
  67. // 'task_id' => $order['task_id'],
  68. // 'invoice_amount' => $v['invoice_amount'],
  69. // 'cdate' => $order['billing_date'],
  70. // 'created_at' => $invoice['created_at']
  71. // ];
  72. }
  73. return $data;
  74. }
  75. public static function getOrderIdsByHandler($search) {
  76. # 根据月份以及经手人姓名获取初始订单id
  77. $orderIdList = Order::query()->where('handler', current($search['handler']))
  78. ->where(function($query) use ($search) {
  79. if(isset($search['month']) && $search['month']) {
  80. if(count($search['month']) == 1) {
  81. $query->where('month', '=', date('Y-m-01', strtotime(current($search['month'])) ) );
  82. } else {
  83. $monthList = array_map(function($value){
  84. return date('Y-m-01', strtotime($value));
  85. }, $search['month']);
  86. $query->whereIn('month', $monthList);
  87. }
  88. }
  89. })->where('enable', 1)->pluck('id')->toArray();
  90. # 根据订单id获取对应的发票id
  91. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  92. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  93. # 根据查询到的发票id获取已经成功开票的发票id
  94. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  95. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', '!=', 2)->pluck('customer_invoices.id')->toArray();
  96. # 根据成功开票的发票id再次查询对应的订单id
  97. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  98. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  99. # 对订单id进行交集运算
  100. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  101. return $actualOrderIdList;
  102. }
  103. public static function getOrderIdsByMcnHandler($search) {
  104. # 根据月份以及经手人姓名获取初始订单id
  105. $orderIdList = Order::where('month', $search['month'])->where('mcn_handler', current($search['mcn_handler']))
  106. ->where('enable', 1)->pluck('id')->toArray();
  107. # 根据订单id获取对应的发票id
  108. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  109. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  110. # 根据查询到的发票id获取已经成功开票的发票id
  111. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  112. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', 2)->pluck('customer_invoices.id')->toArray();
  113. # 根据成功开票的发票id再次查询对应的订单id
  114. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  115. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  116. # 对订单id进行交集运算
  117. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  118. return $actualOrderIdList;
  119. }
  120. public static function getSearchModel($search, $user) {
  121. $query = self::query();
  122. if (isset($search['id']) && !empty($search['id'])) {
  123. $query->where('id', $search['id']);
  124. }
  125. if (isset($search['statements_id']) && !empty($search['statements_id'])) {
  126. $query->where('statements_id', $search['statements_id']);
  127. }
  128. if (isset($search['status']) && is_numeric($search['status']) ){
  129. $query->where('status', $search['status'])->where('is_show', 1);
  130. }
  131. if( isset($search['customer_name']) && !empty($search['customer_name']) ){
  132. $query->where('customer_name', 'like', '%'.$search['customer_name'].'%');
  133. }
  134. if (isset($search['cdate']) && !empty($search['cdate'])) {
  135. $query->where('applicant_time', '>=', $search['cdate'].' 00:00:00');
  136. $query->where('applicant_time', '<=', $search['cdate'].' 23:59:59');
  137. }
  138. if (isset($search['handler_id']) && !empty($search['handler_id'])) {
  139. $query->where('handler_id', $search['handler_id']);
  140. }
  141. if (isset($search['company']) && !empty($search['company'])) {
  142. $query->where('company', $search['company']);
  143. }
  144. if (in_array($user->role_id, [10, 17, 18, 21])) {
  145. // 财务不看商务待审核的发票
  146. $query->where('status', '>=', 2);
  147. }
  148. if (21 == $user->role_id) {
  149. $query->where('company', 'like', '%海南%');
  150. }
  151. if (17 == $user->role_id) {
  152. $query->where('company', '上饶市合一科技有限公司');
  153. }
  154. if (isset($search['inv_list']) && is_array($search['inv_list']) && !empty($search['inv_list'])) {
  155. $query->whereIn('id', $search['inv_list']);
  156. }
  157. if (isset($search['approved_start_date']) && !empty($search['approved_start_date'])) {
  158. $query->where('confirm_verify_at', '>=', $search['approved_start_date'])->where('status', 4);
  159. }
  160. if (isset($search['approved_end_date']) && !empty($search['approved_end_date'])) {
  161. $query->where('confirm_verify_at', '<=', $search['approved_end_date'].'23:59:59')->where('status', 4);
  162. }
  163. if (isset($search['pre_audit_status']) && is_numeric($search['pre_audit_status'])) {
  164. $query->where('pre_audit_status', $search['pre_audit_status']);
  165. }
  166. if (isset($serach['reject_inv_list']) && !empty($search['reject_inv_list'])) {
  167. $query->whereIn('reject_inv_id', $search['reject_inv_list']);
  168. }
  169. if (isset($search['enable']) && is_numeric($search['enable'])) {
  170. $query->where('enable', 1);
  171. }
  172. return $query;
  173. }
  174. public static function getCustomerInvoiceList($search, $user, $offset, $pageSize) {
  175. $listQuery = self::getSearchModel($search, $user);
  176. $count = $listQuery->count();
  177. $list = $listQuery->select('id', 'customer_name', 'handler', 'created_at', 'status', 'applicant_time',
  178. 'is_invoiced', 'invoice_amount', 'invoiced_time', 'company', 'statements_id', 'voided_inv_id', 'date_of_collection',
  179. 'is_voided', 'is_show', 'verify_at', 'confirm_verify_at', 'pre_audit_status', 'pre_audit_content', 'collection_remark');
  180. if(in_array($user->role_id, [10, 17, 18, 21])) {
  181. $list->orderByRaw('is_show desc,if(`status`=2,0,1) asc,id desc');
  182. } else if (15 == $user->role_id){
  183. $list->where('is_show', 1)
  184. ->orderByRaw('if(`status`=1,0,1) asc,id desc');
  185. } else {
  186. $list->where('is_show', 1)
  187. ->orderBy('id', 'desc');
  188. }
  189. $list = $list->offset($offset)
  190. ->limit($pageSize)
  191. ->get();
  192. return [$list, $count];
  193. }
  194. public static function getCustomerInvoiceInfo($invoiceId) {
  195. return self::query()->where('id', $invoiceId)->where('enable', 1)->first();
  196. }
  197. public static function updateData($customerInvoiceId, $collectionData) {
  198. $res = self::query()->where('id', $customerInvoiceId)->update($collectionData);
  199. if($res) return 0;
  200. return 500;
  201. }
  202. public static function getNewRejectInvListBySearch($search, $user) {
  203. $listQuery = self::getSearchModel($search, $user);
  204. return $listQuery->get();
  205. }
  206. }